探究WaveShare的F407的板子好幾天了,這兩天一直止步于網(wǎng)卡模塊的udp發(fā)包,因?yàn)榻匕ぞ呖偸鞘詹坏轿掖a里面發(fā)出去的數(shù)據(jù)包,我的要求是只發(fā)特定數(shù)據(jù),不管收到的數(shù)據(jù)包。
記錄下整個(gè)測(cè)試過(guò)程,以便以后翻閱
使用開發(fā)板自帶的例程,udp_echo_server的例子,在此基礎(chǔ)上修改,main函數(shù)是
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);
}
}
我發(fā)現(xiàn)串口不能用,可能是其初始化方式有些問(wèn)題,我做了些修改
將DebugComPort_Init函數(shù)改成下面兩個(gè)函數(shù)
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)里面添加一下函數(shù)
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();//建立一個(gè)新的UDP包
udp_bind(upcb, IP_ADDR_ANY, 7);
IP4_ADDR(&ipaddr, 192, 168, 1, 11); //切記,此處的IP是PC的IP,因?yàn)槭褂肞C的截包軟件接收
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函數(shù)是
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函數(shù)里面的ip,一開始我就是因?yàn)榘汛颂幍腎P設(shè)置成開發(fā)板的IP了,后來(lái)就沒(méi)想到是這出了問(wèn)題,所以截包工具一直截不到數(shù)據(jù),后來(lái)單步調(diào)試,發(fā)現(xiàn)udp_recv的回調(diào)函數(shù)udp_echoserver_receive_callback的結(jié)構(gòu)體udp_pcb的upcb的remote_ip一直是PC端的IP才想起來(lái)是這個(gè)問(wèn)題。
評(píng)論
查看更多