[STM32H5] 【NUCLEO-H563ZI测评】5.NetXDuo UDP服务器

[复制链接]
 楼主| dami 发表于 2023-8-17 00:10 | 显示全部楼层 |阅读模式
本帖最后由 dami 于 2023-8-17 10:53 编辑

                        【NUCLEO-H563ZI测评】5.NetXDuo UDP服务器


玩了一下THREADX后,发现NetXDuo这个东西蛮好的,那不也得玩一下啊。

下载解压en.stm32cubeh5-v1-1-0。
mdk打开STM32Cube_FW_H5_V1.1.0\Projects\NUCLEO-H563ZI\Applications\NetXDuo\Nx_UDP_Echo_Server\MDK-ARM目录里的Nx_UDP_Echo_Server。
9e63be85140d8478c46c94d528f7bc52

编译下载运行
连接串口和以太网口 串口显示如下
c8a5286071430d207a37675f9826b9cd

找一个udp的调试软件并创建一个客户端连接这个板子的服务器,发送数据,那边就收到了
106be6390ce32f183f3c845b1245323d

主要代码在app_netxduo.c中
MX_NetXDuo_Init(VOID *memory_ptr)中创建各种任务

  1. UINT MX_NetXDuo_Init(VOID *memory_ptr)
  2. {
  3.   UINT ret = NX_SUCCESS;
  4.   TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr;
  5.    /* USER CODE BEGIN App_NetXDuo_MEM_POOL */
  6.   /* USER CODE END App_NetXDuo_MEM_POOL */
  7.   /* USER CODE BEGIN 0 */
  8.   printf("Nx_UDP_Echo_Server application started..\n");
  9.   /* USER CODE END 0 */
  10.   /* Initialize the NetXDuo system. */
  11.   CHAR *pointer;
  12.   nx_system_initialize();
  13.     /* Allocate the memory for packet_pool.  */
  14.   if (tx_byte_allocate(byte_pool, (VOID **) &pointer, NX_APP_PACKET_POOL_SIZE, TX_NO_WAIT) != TX_SUCCESS)
  15.   {
  16.     return TX_POOL_ERROR;
  17.   }
  18.   /* Create the Packet pool to be used for packet allocation,
  19.    * If extra NX_PACKET are to be used the NX_APP_PACKET_POOL_SIZE should be increased
  20.    */
  21.   ret = nx_packet_pool_create(&NxAppPool, "NetXDuo App Pool", DEFAULT_PAYLOAD_SIZE, pointer, NX_APP_PACKET_POOL_SIZE);
  22.   if (ret != NX_SUCCESS)
  23.   {
  24.     return NX_POOL_ERROR;
  25.   }
  26.     /* Allocate the memory for Ip_Instance */
  27.   if (tx_byte_allocate(byte_pool, (VOID **) &pointer, Nx_IP_INSTANCE_THREAD_SIZE, TX_NO_WAIT) != TX_SUCCESS)
  28.   {
  29.     return TX_POOL_ERROR;
  30.   }
  31.    /* Create the main NX_IP instance */
  32.   ret = nx_ip_create(&NetXDuoEthIpInstance, "NetX Ip instance", NX_APP_DEFAULT_IP_ADDRESS, NX_APP_DEFAULT_NET_MASK, &NxAppPool, nx_stm32_eth_driver,
  33.                      pointer, Nx_IP_INSTANCE_THREAD_SIZE, NX_APP_INSTANCE_PRIORITY);
  34.   if (ret != NX_SUCCESS)
  35.   {
  36.     return NX_NOT_SUCCESSFUL;
  37.   }
  38.     /* Allocate the memory for ARP */
  39.   if (tx_byte_allocate(byte_pool, (VOID **) &pointer, DEFAULT_ARP_CACHE_SIZE, TX_NO_WAIT) != TX_SUCCESS)
  40.   {
  41.     return TX_POOL_ERROR;
  42.   }
  43.   /* Enable the ARP protocol and provide the ARP cache size for the IP instance */
  44.   /* USER CODE BEGIN ARP_Protocol_Initialization */
  45.   /* USER CODE END ARP_Protocol_Initialization */
  46.   ret = nx_arp_enable(&NetXDuoEthIpInstance, (VOID *)pointer, DEFAULT_ARP_CACHE_SIZE);
  47.   if (ret != NX_SUCCESS)
  48.   {
  49.     return NX_NOT_SUCCESSFUL;
  50.   }
  51.   /* Enable the ICMP */
  52.   /* USER CODE BEGIN ICMP_Protocol_Initialization */
  53.   /* USER CODE END ICMP_Protocol_Initialization */
  54.   ret = nx_icmp_enable(&NetXDuoEthIpInstance);
  55.   if (ret != NX_SUCCESS)
  56.   {
  57.     return NX_NOT_SUCCESSFUL;
  58.   }
  59.   /* Enable TCP Protocol */
  60.   /* USER CODE BEGIN TCP_Protocol_Initialization */
  61.   /* USER CODE END TCP_Protocol_Initialization */
  62.   ret = nx_tcp_enable(&NetXDuoEthIpInstance);
  63.   if (ret != NX_SUCCESS)
  64.   {
  65.     return NX_NOT_SUCCESSFUL;
  66.   }
  67.   /* Enable the UDP protocol required for  DHCP communication */
  68.   /* USER CODE BEGIN UDP_Protocol_Initialization */
  69.   /* USER CODE END UDP_Protocol_Initialization */
  70.   ret = nx_udp_enable(&NetXDuoEthIpInstance);
  71.   if (ret != NX_SUCCESS)
  72.   {
  73.     return NX_NOT_SUCCESSFUL;
  74.   }
  75.    /* Allocate the memory for main thread   */
  76.   if (tx_byte_allocate(byte_pool, (VOID **) &pointer, NX_APP_THREAD_STACK_SIZE, TX_NO_WAIT) != TX_SUCCESS)
  77.   {
  78.     return TX_POOL_ERROR;
  79.   }
  80.   /* Create the main thread */
  81.   ret = tx_thread_create(&NxAppThread, "NetXDuo App thread", App_Main_Thread_Entry , 0, pointer, NX_APP_THREAD_STACK_SIZE,
  82.                          NX_APP_THREAD_PRIORITY, NX_APP_THREAD_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
  83.   if (ret != TX_SUCCESS)
  84.   {
  85.     return TX_THREAD_ERROR;
  86.   }
  87.   /* Create the DHCP client */
  88.   /* USER CODE BEGIN DHCP_Protocol_Initialization */
  89.   /* USER CODE END DHCP_Protocol_Initialization */
  90.   ret = nx_dhcp_create(&DHCPClient, &NetXDuoEthIpInstance, "DHCP Client");
  91.   if (ret != NX_SUCCESS)
  92.   {
  93.     return NX_DHCP_ERROR;
  94.   }
  95.   /* set DHCP notification callback  */
  96.   tx_semaphore_create(&DHCPSemaphore, "DHCP Semaphore", 0);
  97.   /* USER CODE BEGIN MX_NetXDuo_Init */
  98.   /* Allocate the app UDP thread entry pool. */
  99.   ret = tx_byte_allocate(byte_pool, (VOID **) &pointer, 2 * DEFAULT_MEMORY_SIZE, TX_NO_WAIT);
  100.   if (ret != TX_SUCCESS)
  101.   {
  102.     return TX_POOL_ERROR;
  103.   }
  104.   /* create the UDP server thread */
  105.   ret = tx_thread_create(&AppUDPThread, "App UDP Thread", App_UDP_Thread_Entry, 0, pointer, 2 * DEFAULT_MEMORY_SIZE,
  106.                         DEFAULT_PRIORITY, DEFAULT_PRIORITY, TX_NO_TIME_SLICE, TX_DONT_START);
  107.   if (ret != TX_SUCCESS)
  108.   {
  109.     return TX_THREAD_ERROR;
  110.   }
  111.   /* Allocate the memory for Link thread   */
  112.   if (tx_byte_allocate(byte_pool, (VOID **) &pointer,2 *  DEFAULT_MEMORY_SIZE, TX_NO_WAIT) != TX_SUCCESS)
  113.   {
  114.     return TX_POOL_ERROR;
  115.   }
  116.   /* create the Link thread */
  117.   ret = tx_thread_create(&AppLinkThread, "App Link Thread", App_Link_Thread_Entry, 0, pointer, 2 * DEFAULT_MEMORY_SIZE,
  118.                          LINK_PRIORITY, LINK_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
  119.   if (ret != TX_SUCCESS)
  120.   {
  121.     return TX_THREAD_ERROR;
  122.   }
  123.   /* USER CODE END MX_NetXDuo_Init */
  124.   return ret;
  125. }
复制代码
相应的入口函数中处理

  1. static VOID ip_address_change_notify_callback(NX_IP *ip_instance, VOID *ptr)
  2. {
  3.   /* USER CODE BEGIN ip_address_change_notify_callback */
  4.   /* USER CODE END ip_address_change_notify_callback */
  5.   /* release the semaphore as soon as an IP address is available */
  6.   tx_semaphore_put(&DHCPSemaphore);
  7. }
  8. /**
  9. * [url=home.php?mod=space&uid=247401]@brief[/url]  Main thread entry.
  10. * @param thread_input: ULONG user argument used by the thread entry
  11. * @retval none
  12. */
  13. static VOID App_Main_Thread_Entry (ULONG thread_input)
  14. {
  15.   /* USER CODE BEGIN Nx_App_Thread_Entry 0 */
  16.   /* USER CODE END Nx_App_Thread_Entry 0 */
  17.   UINT ret = NX_SUCCESS;
  18.   /* USER CODE BEGIN Nx_App_Thread_Entry 1 */
  19.   /* USER CODE END Nx_App_Thread_Entry 1 */
  20.   /* register the IP address change callback */
  21.   ret = nx_ip_address_change_notify(&NetXDuoEthIpInstance, ip_address_change_notify_callback, NULL);
  22.   if (ret != NX_SUCCESS)
  23.   {
  24.     /* USER CODE BEGIN IP address change callback error */
  25.     Error_Handler();
  26.     /* USER CODE END IP address change callback error */
  27.   }
  28.   /* start the DHCP client */
  29.   ret = nx_dhcp_start(&DHCPClient);
  30.   if (ret != NX_SUCCESS)
  31.   {
  32.     /* USER CODE BEGIN DHCP client start error */
  33.     Error_Handler();
  34.     /* USER CODE END DHCP client start error */
  35.   }
  36.   /* wait until an IP address is ready */
  37.   if(tx_semaphore_get(&DHCPSemaphore, NX_APP_DEFAULT_TIMEOUT) != TX_SUCCESS)
  38.   {
  39.     /* USER CODE BEGIN DHCPSemaphore get error */
  40.     Error_Handler();
  41.     /* USER CODE END DHCPSemaphore get error */
  42.   }
  43.   /* USER CODE BEGIN Nx_App_Thread_Entry 2 */
  44.   /* get IP address */
  45.   ret = nx_ip_address_get(&NetXDuoEthIpInstance, &IpAddress, &NetMask);
  46.   /* print the IP address */
  47.   PRINT_IP_ADDRESS(IpAddress);
  48.   if (ret != TX_SUCCESS)
  49.   {
  50.     Error_Handler();
  51.   }
  52.   /* Now the network is correctly initialized, start the UDP server thread */
  53.   tx_thread_resume(&AppUDPThread);
  54.   /* this thread is not needed any more, we relinquish it */
  55.   tx_thread_relinquish();
  56.   /* USER CODE END Nx_App_Thread_Entry 2 */
  57. }
  58. /* USER CODE BEGIN 1 */
  59. static VOID App_UDP_Thread_Entry(ULONG thread_input)
  60. {
  61.   UINT ret;
  62.   ULONG bytes_read;
  63.   UINT source_port;
  64.   UCHAR data_buffer[512];
  65.   ULONG source_ip_address;
  66.   NX_PACKET *data_packet;
  67.   /* create the UDP socket */
  68.   ret = nx_udp_socket_create(&NetXDuoEthIpInstance, &UDPSocket, "UDP Server Socket", NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, QUEUE_MAX_SIZE);
  69.   if (ret != NX_SUCCESS)
  70.   {
  71.      Error_Handler();
  72.   }
  73.   /* bind the socket indefinitely on the required port */
  74.   ret = nx_udp_socket_bind(&UDPSocket, DEFAULT_PORT, TX_WAIT_FOREVER);
  75.   if (ret != NX_SUCCESS)
  76.   {
  77.      Error_Handler();
  78.   }
  79.   else
  80.   {
  81.     printf("UDP Server listening on PORT %d.. \n", DEFAULT_PORT);
  82.   }
  83.   while(1)
  84.   {
  85.     TX_MEMSET(data_buffer, '\0', sizeof(data_buffer));
  86.     /* wait for data for 1 sec */
  87.     ret = nx_udp_socket_receive(&UDPSocket, &data_packet, 100);
  88.     if (ret == NX_SUCCESS)
  89.     {
  90.       /* data is available, read it into the data buffer */
  91.       nx_packet_data_retrieve(data_packet, data_buffer, &bytes_read);
  92.       /* get info about the client address and port */
  93.       nx_udp_source_extract(data_packet, &source_ip_address, &source_port);
  94.       /* print the client address, the remote port and the received data */
  95.       PRINT_DATA(source_ip_address, source_port, data_buffer);
  96.       /* resend the same packet to the client */
  97.       ret =  nx_udp_socket_send(&UDPSocket, data_packet, source_ip_address, source_port);
  98.       /* toggle the green led to monitor visually the traffic */
  99.       HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
  100.     }
  101.     else
  102.     {
  103.         /* the server is in idle state, toggle the green led */
  104.         HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
  105.     }
  106.   }
  107. }
  108. /**
  109. * [url=home.php?mod=space&uid=247401]@brief[/url]  Link thread entry
  110. * @param thread_input: ULONG thread parameter
  111. * @retval none
  112. */
  113. static VOID App_Link_Thread_Entry(ULONG thread_input)
  114. {
  115.   ULONG actual_status;
  116.   UINT linkdown = 0, status;
  117.   while(1)
  118.   {
  119.     /* Get Physical Link status. */
  120.     status = nx_ip_interface_status_check(&NetXDuoEthIpInstance, 0, NX_IP_LINK_ENABLED,
  121.                                       &actual_status, 10);
  122.     if(status == NX_SUCCESS)
  123.     {
  124.       if(linkdown == 1)
  125.       {
  126.         linkdown = 0;
  127.         status = nx_ip_interface_status_check(&NetXDuoEthIpInstance, 0, NX_IP_ADDRESS_RESOLVED,
  128.                                       &actual_status, 10);
  129.         if(status == NX_SUCCESS)
  130.         {
  131.           /* The network cable is connected again. */
  132.           printf("The network cable is connected again.\n");
  133.           /* Print UDP Echo Server is available again. */
  134.           printf("UDP Echo Server is available again.\n");
  135.         }
  136.         else
  137.         {
  138.           /* The network cable is connected. */
  139.           printf("The network cable is connected.\n");
  140.           /* Send command to Enable Nx driver. */
  141.           nx_ip_driver_direct_command(&NetXDuoEthIpInstance, NX_LINK_ENABLE,
  142.                                       &actual_status);
  143.           /* Restart DHCP Client. */
  144.           nx_dhcp_stop(&DHCPClient);
  145.           nx_dhcp_start(&DHCPClient);
  146.         }
  147.       }
  148.     }
  149.     else
  150.     {
  151.       if(0 == linkdown)
  152.       {
  153.         linkdown = 1;
  154.         /* The network cable is not connected. */
  155.         printf("The network cable is not connected.\n");
  156.       }
  157.     }
  158.     tx_thread_sleep(NX_ETH_CABLE_CONNECTION_CHECK_PERIOD);
  159.   }
  160. }
  161. /* USER CODE END 1 */
复制代码



  
yangjiaxu 发表于 2024-1-12 16:40 | 显示全部楼层
我也想玩,最近玩网络玩的很嗨,我觉得很有意思
Allison8859 发表于 2024-1-12 16:41 | 显示全部楼层
UDP确实简单了一些,其实玩一些复杂的协议也是可以的了
Candic12e 发表于 2024-1-12 16:41 | 显示全部楼层
这个和普通的lwip来说,有啥优势不?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

66

主题

1078

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部