探究WaveShare的F407的板子好几天了,这两天一直止步于网卡模块的udp发包,因为截包工具总是收不到我代码里面发出去的数据包,我的要求是只发特定数据,不管收到的数据包
记录下整个测试过程,以便以后翻阅
使用开发板自带的例程,udp_echo_server的例子,在此基础上修改,main函数是
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured to
168 MHz, this is done through SystemInit() function which is called from
startup file (startup_stm32f4xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f4xx.c file
*/
#ifdef SERIAL_DEBUG
DebugComPort_Init();
#endif
/*Initialize LCD and Leds */
LCD_LED_Init();
/* configure ethernet */
ETH_BSP_Config();
/* Initilaize the LwIP stack */
LwIP_Init();
/* UDP echoserver */
udp_echoserver_init();
/* Infinite loop */
while (1)
{
/* check if any packet received */
if (ETH_CheckFrameReceived())
{
/* process received ethernet packet */
LwIP_Pkt_Handle();
}
/* handle periodic timers for LwIP */
LwIP_Periodic_Handle(LocalTime);
}
}
我发现串口不能用,可能是其初始化方式有些问题,我做了些修改 将DebugComPort_Init函数改成下面两个函数 USART_Configuration();
USART_NVIC_Config();
串口1就可以正常使用了 void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(Open_USART_TX_GPIO_CLK,ENABLE);
RCC_AHB1PeriphClockCmd(Open_USART_RX_GPIO_CLK,ENABLE);
#ifdef USART1_OPEN
RCC_APB2PeriphClockCmd(Open_USART_CLK,ENABLE);
#else
RCC_APB1PeriphClockCmd(Open_USART_CLK,ENABLE);
#endif
GPIO_PinAFConfig(Open_USART_TX_GPIO_PORT, Open_USART_TX_SOURCE, Open_USART_TX_AF);
GPIO_PinAFConfig(Open_USART_RX_GPIO_PORT, Open_USART_RX_SOURCE, Open_USART_RX_AF);
/*
* Open_USART_TX -> PA9 , Open_USART_RX -PA10
*/
GPIO_InitStructure.GPIO_Pin = Open_USART_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(Open_USART_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Open_USART_RX_PIN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(Open_USART_RX_GPIO_PORT, &GPIO_InitStructure);
/*
USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit
*/
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(Open_USART, &USART_InitStructure);
/* Enable the Open_USART Transmit interrupt: this interrupt is generated when the
Open_USART transmit data register is empty */
USART_ITConfig(Open_USART,USART_IT_RXNE,ENABLE);
USART_Cmd(Open_USART, ENABLE);
}
void USART_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = Open_USART_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
然后将udp_echoserver_init();注释掉 在while(1)里面添加一下函数 udp_send_data(udp_data, sizeof(udp_data)); 代码是 unsigned char udp_data[]= "hello world!";
void udp_send_data(uint8_t* pData, uint16_t len)
{
struct udp_pcb *upcb;
struct pbuf* buff;
struct ip_addr ipaddr;
err_t err;
buff = pbuf_alloc(PBUF_TRANSPORT, 1024, PBUF_ROM);
buff->payload = pData;
buff->len = len;
buff->tot_len = len;
upcb = udp_new();//建立一个新的UDP包
udp_bind(upcb, IP_ADDR_ANY, 7);
IP4_ADDR(&ipaddr, 192, 168, 1, 11); //切记,此处的IP是PC的IP,因为使用PC的截包软件接收
err = udp_connect(upcb, &ipaddr, 7);
if(err == ERR_OK)
{
err = udp_send(upcb, buff);
if(ERR_IS_FATAL(err))
printf("udp_send...%d\r\n",err);
}
udp_disconnect(upcb);
pbuf_free(buff);
udp_remove(upcb);
}
最终main函数是 int main(void)
{
USART_Configuration();
USART_NVIC_Config();
/*Initialize LCD and Leds */
LCD_LED_Init();
/* configure ethernet */
ETH_BSP_Config();
/* Initilaize the LwIP stack */
LwIP_Init();
/* UDP echoserver */
//udp_echoserver_init();
/* Infinite loop */
while (1)
{
udp_send_data(udp_data, sizeof(udp_data));
//Delay(100);
/* check if any packet received */
if (ETH_CheckFrameReceived())
{
/* process received ethernet packet */
LwIP_Pkt_Handle();
}
/* handle periodic timers for LwIP */
LwIP_Periodic_Handle(LocalTime);
}
}
注意 udp_send_data函数里面的ip,一开始我就是因为把此处的IP设置成开发板的IP了,后来就没想到是这出了问题,所以截包工具一直截不到数据,后来单步调试,发现udp_recv的回调函数udp_echoserver_receive_callback的结构体udp_pcb的upcb的remote_ip一直是PC端的IP才想起来是这个问题
|