STM32F407+LAN8720A +LWIP +FreeRTOS ping通
上一篇实现了LWIP ping 通
本篇实现UDP通讯
实现如下功能:
- 串口1空闲中断+DMA接收,收到数据用UDP发送
- UDP接收,收到数据用串口1发送
STM32CUBEIDE配置和代码
1. 配置UARAT1的空闲中断+DMA接收
UART1接收到数据,释放信号量,在任务中进行数据处理(UDP发送)
/* USER CODE END Header_StartTask03 */
void StartTask03(void const * argument)
{
/* USER CODE BEGIN StartTask03 */
uint16_t len_data;
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, buletooth_rxBuf, 250);
hdma_usart1_rx.Instance->CR &= ~(DMA_IT_HT);//取消接收一半中断
/* Infinite loop */
for(;;)
{
//UART RECEIVED, UDP SEND
osSemaphoreWait (BinarySem_BLUE_RecHandle, osWaitForever);
if(ble_rxBuf_len > 0)
{
len_data = ble_rxBuf_len;
udp_client_send_data(buletooth_rxBuf, len_data);
}
}
/* USER CODE END StartTask03 */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
if(huart->Instance == USART1)
{
ble_rxBuf_len = Size;
osSemaphoreRelease (BinarySem_BLUE_RecHandle);
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, buletooth_rxBuf, 250);
}
}
2. 配置UDP
void udp_client_init(void)
{
ip_addr_t serverIP;
err_t err;
IP4_ADDR(&serverIP, 192, 168, 111, 11);
upcb = udp_new();
if (upcb != NULL)
{
/* 配置本地端口 */
upcb->local_port = UDP_LOCAL_PORT;
/* 配置服务器IP和端口号*/
err = udp_connect(upcb, &serverIP, UDP_REMOTE_PORT);
if (err == ERR_OK)
{
/* 注册接收回调函数 */
udp_recv(upcb, udp_receive_callback, NULL);
printf("UDP INIT SUCCESS\r\n");
}
else
{
udp_remove(upcb);
}
}
}
static void udp_receive_callback(void *arg, struct udp_pcb *upcb,struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
if (p != NULL)
{
struct pbuf *ptmp = p;
while (ptmp != NULL)
{
memset(cache_udprece, 0, sizeof(cache_udprece));