MII_MODE SNI_MODE MAC Interface Mode
1(电路强制为1,但这脚还是低电平) 0 (电路强制为0,接地) RMII Mode
加上拉2.2K电阻,外部晶振50M
硬件上:MII_MODE脚还是低电平,规格书上这脚为1和SNI_MODE为0,开启RMII,现在二个脚都是低电平0
软件:
int main(void)
{
unsigned char tcp_data[] = "tcp dkfdjkf!\n";
struct tcp_pcb *pcb;
System_Setup();
LwIP_Init();
TCP_Client_Init(TCP_LOCAL_PORT,TCP_SERVER_PORT,TCP_SERVER_IP);
while(1)
{
pcb = Check_TCP_Connect();
if(pcb != 0)
{
TCP_Client_Send_Data(pcb,tcp_data,sizeof(tcp_data));
}
Delay_s(0xfffff);
System_Periodic_Handle();
}
}void LwIP_Init(void)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
uint8_t macaddress[6]={BOARD_MAC_ADDR};
/* Initializes the dynamic memory heap defined by MEM_SIZE.*/
mem_init();
/* Initializes the memory pools defined by MEMP_NUM_x.*/
memp_init();
//#if LWIP_DHCP //ip×Ô¶¯»ñÈ¡
// ipaddr.addr = 0;
// netmask.addr = 0;
// gw.addr = 0;
//#else
My_IP4_ADDR(&ipaddr,BOARD_IP);
My_IP4_ADDR(&netmask, BOARD_NETMASK);
My_IP4_ADDR(&gw, BOARD_WG);
//#endif
Set_MAC_Address(macaddress);
netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
/* Registers the default network interface.*/
netif_set_default(&netif);
//#if LWIP_DHCP
// /* Creates a new DHCP client for this interface on the first call.
// Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at
// the predefined regular intervals after starting the client.
// You can peek in the netif->dhcp struct for the actual DHCP status.*/
// dhcp_start(&netif);
//#endif
/* When the netif is fully configured this function must be called.*/
netif_set_up(&netif);
}
void System_Setup(void)
{
RCC_ClocksTypeDef RCC_Clocks;
SystemInit();
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ETH_MAC | RCC_AHBPeriph_ETH_MAC_Tx |
RCC_AHBPeriph_ETH_MAC_Rx, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO , ENABLE);
NVIC_Configuration();
GPIO_Configuration();
Ethernet_Configuration();
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.SYSCLK_Frequency / 100);
NVIC_SetPriority (SysTick_IRQn, 1);
}
defined RMII_MODE
defined PHY_ADDRESS 0x01
void Ethernet_Configuration(void)
{
ETH_InitTypeDef ETH_InitStructure;
#ifdef MII_MODE
GPIO_ETH_MediaInterfaceConfig(GPIO_ETH_MediaInterface_MII);
#elif defined RMII_MODE /* Mode RMII with STM3210C-EVAL */
GPIO_ETH_MediaInterfaceConfig(GPIO_ETH_MediaInterface_RMII);
#endif
ETH_DeInit();
ETH_SoftwareReset();
while (ETH_GetSoftwareResetStatus() == SET);
/* ETHERNET Configuration ------------------------------------------------------*/
/* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
ETH_StructInit(Ð_InitStructure);
/* Fill ETH_InitStructure parametrs */
/*------------------------ MAC -----------------------------------*/
ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable ;
ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
#ifdef CHECKSUM_BY_HARDWARE
ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
#endif
/*------------------------ DMA -----------------------------------*/
/* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
ETH_InitStructure.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_2_1;
ETH_Init(Ð_InitStructure, PHY_ADDRESS);
ETH_DMAITConfig(ETH_DMA_IT_NIS | ETH_DMA_IT_R, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//ETH_RMII_MDIO/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PA1 =ETH_RMII_REF_CLK/PA7=ETH_CRS_DV
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //GPIO_Mode_IN_FLOATING
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//ETH_RMII_MDC
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// PC4 =ETH_RMII_RXD0/ PC5 =RXD1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//ETH_RX_ER
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// ETH_TX_EN /ETH_TXD_0 /ETH_TXD_1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//LEDµÆ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_ETH, ENABLE);
// PD8 =ETH_CRS_DV
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
struct tcp_pcb *Check_TCP_Connect(void)
{
struct tcp_pcb *cpcb = 0;
connect_flag = 0;
for(cpcb = tcp_active_pcbs;cpcb != NULL; cpcb = cpcb->next)
{
if(cpcb->local_port == TCP_LOCAL_PORT && cpcb->remote_port == TCP_SERVER_PORT)
if(cpcb -> state == ESTABLISHED)
{
connect_flag = 1;
break;
}
}
if(connect_flag == 0)
{
TCP_Client_Init(TCP_LOCAL_PORT,TCP_SERVER_PORT,TCP_SERVER_IP);
cpcb = 0;
}
return cpcb;
}
void TCP_Client_Init(u16_t local_port,u16_t remote_port,unsigned char a,unsigned char b,unsigned char c,unsigned char d)
{
//TCP_LOCAL_PORT=1030,TCP_SERVER_PORT=1031,TCP_SERVER_IP=192,168,1,100/
struct ip_addr ipaddr;
struct tcp_pcb *tcp_client_pcb;
err_t err;
IP4_ADDR(&ipaddr,a,b,c,d);
tcp_client_pcb = tcp_new();
if (!tcp_client_pcb)
{
return ;
}
err = tcp_bind(tcp_client_pcb,IP_ADDR_ANY,local_port);
if(err != ERR_OK)
{
return ;
}
tcp_connect(tcp_client_pcb,&ipaddr,remote_port,TCP_Connected);
tcp_recv(tcp_client_pcb,TCP_Client_Recv);
}
err_t TCP_Connected(void *arg,struct tcp_pcb *pcb,err_t err)
{
//tcp_client_pcb = pcb;
return ERR_OK;
}
err_t TCP_Client_Send_Data(struct tcp_pcb *cpcb,unsigned char *buff,unsigned int length)
{
err_t err;
err = tcp_write(cpcb,buff,length,TCP_WRITE_FLAG_COPY);
tcp_output(cpcb);
//tcp_close(cpcb);
return err;
}
|