3739|17

650

帖子

8

TA的资源

纯净的硅(初级)

楼主
 

【STM32H5开发板】第三帖 stm32h563以太网测试 [复制链接]

 

STM32H563 Nucleo开发板是一款基于STM32H563微控制器的开发板,它具有强大的性能和丰富的外设,可以广泛应用于工业自动化、智能家居、智能交通等领域。下面我们介绍如何使用STM32H563 Nucleo开发板进行以太网测试。

 

首先,使用网线连接开发板和路由器,电脑连接到相同路由器下。接着,我们使用st的代码生成工具cubemx生成代码,直接选择对应开发板,并且默认初始化板载资源,接下来配置以太网相关的库,这里用到了NETXDUO和THREADX库,把库相关设置配置好,如下图所示:

 

生成代码后,我们做UDP测试,具体代码添加到app_netxduo.c文件中,如下:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * [url=home.php?mod=space&uid=1307177]@File[/url]  app_netxduo.c
  * [url=home.php?mod=space&uid=1315547]@author[/url]  MCD Application Team
  * [url=home.php?mod=space&uid=159083]@brief[/url]  NetXDuo applicative file
  ******************************************************************************
    * [url=home.php?mod=space&uid=1020061]@attention[/url]  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "app_netxduo.h"

/* Private includes ----------------------------------------------------------*/
#include "nxd_dhcp_client.h"
/* USER CODE BEGIN Includes */
#include   "main.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
TX_THREAD      NxAppThread;
NX_PACKET_POOL NxAppPool;
NX_IP          NetXDuoEthIpInstance;
TX_SEMAPHORE   DHCPSemaphore;
NX_DHCP        DHCPClient;
/* USER CODE BEGIN PV */
TX_THREAD AppUDPThread;
TX_THREAD AppLinkThread;

NX_UDP_SOCKET UDPSocket;
ULONG IpAddress;
ULONG NetMask;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
static VOID nx_app_thread_entry (ULONG thread_input);
static VOID ip_address_change_notify_callback(NX_IP *ip_instance, VOID *ptr);
/* USER CODE BEGIN PFP */
static VOID App_UDP_Thread_Entry(ULONG thread_input);
static VOID App_Link_Thread_Entry(ULONG thread_input);

/* USER CODE END PFP */

/**
  * @brief  Application NetXDuo Initialization.
  * @param memory_ptr: memory pointer
  * @retval int
  */
UINT MX_NetXDuo_Init(VOID *memory_ptr)
{
  UINT ret = NX_SUCCESS;
  TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr;

   /* USER CODE BEGIN App_NetXDuo_MEM_POOL */

  /* USER CODE END App_NetXDuo_MEM_POOL */
  /* USER CODE BEGIN 0 */
  printf("Nx_UDP_Echo_Client application started..\n");
  /* USER CODE END 0 */

  /* Initialize the NetXDuo system. */
  CHAR *pointer;
  nx_system_initialize();

    /* Allocate the memory for packet_pool.  */
  if (tx_byte_allocate(byte_pool, (VOID **) &pointer, NX_APP_PACKET_POOL_SIZE, TX_NO_WAIT) != TX_SUCCESS)
  {
    return TX_POOL_ERROR;
  }

  /* Create the Packet pool to be used for packet allocation,
   * If extra NX_PACKET are to be used the NX_APP_PACKET_POOL_SIZE should be increased
   */
  ret = nx_packet_pool_create(&NxAppPool, "NetXDuo App Pool", DEFAULT_PAYLOAD_SIZE, pointer, NX_APP_PACKET_POOL_SIZE);

  if (ret != NX_SUCCESS)
  {
    return NX_POOL_ERROR;
  }

    /* Allocate the memory for Ip_Instance */
  if (tx_byte_allocate(byte_pool, (VOID **) &pointer, Nx_IP_INSTANCE_THREAD_SIZE, TX_NO_WAIT) != TX_SUCCESS)
  {
    return TX_POOL_ERROR;
  }

   /* Create the main NX_IP instance */
  ret = nx_ip_create(&NetXDuoEthIpInstance, "NetX Ip instance", NX_APP_DEFAULT_IP_ADDRESS, NX_APP_DEFAULT_NET_MASK, &NxAppPool, nx_stm32_eth_driver,
                     pointer, Nx_IP_INSTANCE_THREAD_SIZE, NX_APP_INSTANCE_PRIORITY);

  if (ret != NX_SUCCESS)
  {
    return NX_NOT_SUCCESSFUL;
  }

    /* Allocate the memory for ARP */
  if (tx_byte_allocate(byte_pool, (VOID **) &pointer, DEFAULT_ARP_CACHE_SIZE, TX_NO_WAIT) != TX_SUCCESS)
  {
    return TX_POOL_ERROR;
  }

  /* Enable the ARP protocol and provide the ARP cache size for the IP instance */

  /* USER CODE BEGIN ARP_Protocol_Initialization */

  /* USER CODE END ARP_Protocol_Initialization */

  ret = nx_arp_enable(&NetXDuoEthIpInstance, (VOID *)pointer, DEFAULT_ARP_CACHE_SIZE);

  if (ret != NX_SUCCESS)
  {
    return NX_NOT_SUCCESSFUL;
  }

  /* Enable the ICMP */

  /* USER CODE BEGIN ICMP_Protocol_Initialization */

  /* USER CODE END ICMP_Protocol_Initialization */

  ret = nx_icmp_enable(&NetXDuoEthIpInstance);

  if (ret != NX_SUCCESS)
  {
    return NX_NOT_SUCCESSFUL;
  }

  /* Enable TCP Protocol */

  /* USER CODE BEGIN TCP_Protocol_Initialization */

  /* USER CODE END TCP_Protocol_Initialization */

  ret = nx_tcp_enable(&NetXDuoEthIpInstance);

  if (ret != NX_SUCCESS)
  {
    return NX_NOT_SUCCESSFUL;
  }

  /* Enable the UDP protocol required for  DHCP communication */

  /* USER CODE BEGIN UDP_Protocol_Initialization */

  /* USER CODE END UDP_Protocol_Initialization */

  ret = nx_udp_enable(&NetXDuoEthIpInstance);

  if (ret != NX_SUCCESS)
  {
    return NX_NOT_SUCCESSFUL;
  }

   /* Allocate the memory for main thread   */
  if (tx_byte_allocate(byte_pool, (VOID **) &pointer, NX_APP_THREAD_STACK_SIZE, TX_NO_WAIT) != TX_SUCCESS)
  {
    return TX_POOL_ERROR;
  }

  /* Create the main thread */
  ret = tx_thread_create(&NxAppThread, "NetXDuo App thread", nx_app_thread_entry , 0, pointer, NX_APP_THREAD_STACK_SIZE,
                         NX_APP_THREAD_PRIORITY, NX_APP_THREAD_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);

  if (ret != TX_SUCCESS)
  {
    return TX_THREAD_ERROR;
  }

  /* Create the DHCP client */

  /* USER CODE BEGIN DHCP_Protocol_Initialization */
  /* USER CODE END DHCP_Protocol_Initialization */

  ret = nx_dhcp_create(&DHCPClient, &NetXDuoEthIpInstance, "DHCP Client");

  if (ret != NX_SUCCESS)
  {
    return NX_DHCP_ERROR;
  }

  /* set DHCP notification callback  */
  tx_semaphore_create(&DHCPSemaphore, "DHCP Semaphore", 0);

  /* USER CODE BEGIN MX_NetXDuo_Init */
    /* Allocate the memory for UDP client thread   */
  if (tx_byte_allocate(byte_pool, (VOID **) &pointer,NX_APP_THREAD_STACK_SIZE, TX_NO_WAIT) != TX_SUCCESS)
  {
    return TX_POOL_ERROR;
  }
  /* create the UDP client thread */
  ret = tx_thread_create(&AppUDPThread, "App UDP Thread", App_UDP_Thread_Entry, 0, pointer, NX_APP_THREAD_STACK_SIZE,
                         NX_APP_THREAD_PRIORITY, NX_APP_THREAD_PRIORITY, TX_NO_TIME_SLICE, TX_DONT_START);

  if (ret != TX_SUCCESS)
  {
    return NX_NOT_ENABLED;
  }
  /* Allocate the memory for Link thread   */
  if (tx_byte_allocate(byte_pool, (VOID **) &pointer, NX_APP_THREAD_STACK_SIZE, TX_NO_WAIT) != TX_SUCCESS)
  {
    return TX_POOL_ERROR;
  }

  /* create the Link thread */
  ret = tx_thread_create(&AppLinkThread, "App Link Thread", App_Link_Thread_Entry, 0, pointer, NX_APP_THREAD_STACK_SIZE,
                         LINK_PRIORITY, LINK_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);

  if (ret != TX_SUCCESS)
  {
    return NX_NOT_ENABLED;
  }
  /* USER CODE END MX_NetXDuo_Init */

  return ret;
}

/**
* @brief  ip address change callback.
* @param ip_instance: NX_IP instance
* @param ptr: user data
* @retval none
*/
static VOID ip_address_change_notify_callback(NX_IP *ip_instance, VOID *ptr)
{
  /* USER CODE BEGIN ip_address_change_notify_callback */

  /* USER CODE END ip_address_change_notify_callback */

  /* release the semaphore as soon as an IP address is available */
  tx_semaphore_put(&DHCPSemaphore);
}

/**
* @brief  Main thread entry.
* @param thread_input: ULONG user argument used by the thread entry
* @retval none
*/
static VOID nx_app_thread_entry (ULONG thread_input)
{
  /* USER CODE BEGIN Nx_App_Thread_Entry 0 */

  /* USER CODE END Nx_App_Thread_Entry 0 */

  UINT ret = NX_SUCCESS;

  /* USER CODE BEGIN Nx_App_Thread_Entry 1 */

  /* USER CODE END Nx_App_Thread_Entry 1 */

  /* register the IP address change callback */
  ret = nx_ip_address_change_notify(&NetXDuoEthIpInstance, ip_address_change_notify_callback, NULL);
  if (ret != NX_SUCCESS)
  {
    /* USER CODE BEGIN IP address change callback error */
    Error_Handler();
    /* USER CODE END IP address change callback error */
  }

  /* start the DHCP client */
  ret = nx_dhcp_start(&DHCPClient);
  if (ret != NX_SUCCESS)
  {
    /* USER CODE BEGIN DHCP client start error */
    Error_Handler();
    /* USER CODE END DHCP client start error */
  }

  /* wait until an IP address is ready */
  if(tx_semaphore_get(&DHCPSemaphore, NX_APP_DEFAULT_TIMEOUT) != TX_SUCCESS)
  {
    /* USER CODE BEGIN DHCPSemaphore get error */
    Error_Handler();
    /* USER CODE END DHCPSemaphore get error */
  }

  /* USER CODE BEGIN Nx_App_Thread_Entry 2 */
/* get IP address */
  ret = nx_ip_address_get(&NetXDuoEthIpInstance, &IpAddress, &NetMask);

  /* print the IP address */
  PRINT_IP_ADDRESS(IpAddress);

  if (ret != TX_SUCCESS)
  {
    Error_Handler();
  }
  /* the network is correctly initialized, start the UDP thread */
  tx_thread_resume(&AppUDPThread);

  /* this thread is not needed any more, we relinquish it */
  tx_thread_relinquish();

  return;
  /* USER CODE END Nx_App_Thread_Entry 2 */

}
/* USER CODE BEGIN 1 */
static VOID App_UDP_Thread_Entry(ULONG thread_input)
{
  UINT ret;
  UINT count = 0;
  ULONG bytes_read;
  NX_PACKET *server_packet;
  UCHAR data_buffer[512];

  NX_PACKET *data_packet;

  /* create the UDP socket */
  ret = nx_udp_socket_create(&NetXDuoEthIpInstance, &UDPSocket, "UDP Client Socket", NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, QUEUE_MAX_SIZE);

  if (ret != NX_SUCCESS)
  {
    Error_Handler();
  }

  /* bind UDP socket to the DEFAULT PORT */
  ret = nx_udp_socket_bind(&UDPSocket, DEFAULT_PORT, TX_WAIT_FOREVER);

  if (ret != NX_SUCCESS)
  {
    Error_Handler();
  }

  while(count++ < MAX_PACKET_COUNT)
  {
    TX_MEMSET(data_buffer, '\0', sizeof(data_buffer));

    /* create the packet to send over the UDP socket */
    ret = nx_packet_allocate(&NxAppPool, &data_packet, NX_UDP_PACKET, TX_WAIT_FOREVER);

    if (ret != NX_SUCCESS)
    {
      Error_Handler();
    }

    ret = nx_packet_data_append(data_packet, (VOID *)DEFAULT_MESSAGE, sizeof(DEFAULT_MESSAGE), &NxAppPool, TX_WAIT_FOREVER);

    if (ret != NX_SUCCESS)
    {
      Error_Handler();
    }

    /* send the message */
    ret = nx_udp_socket_send(&UDPSocket, data_packet, UDP_SERVER_ADDRESS, UDP_SERVER_PORT);

    /* wait 10 sec to receive response from the server */
    ret = nx_udp_socket_receive(&UDPSocket, &server_packet, NX_APP_DEFAULT_TIMEOUT);

    if (ret == NX_SUCCESS)
    {
      ULONG source_ip_address;
      UINT source_port;

      /* get the server IP address and  port */
      nx_udp_source_extract(server_packet, &source_ip_address, &source_port);

      /* retrieve the data sent by the server */
      nx_packet_data_retrieve(server_packet, data_buffer, &bytes_read);

      /* print the received data */
     PRINT_DATA(source_ip_address, source_port, data_buffer);

      /* release the server packet */
      nx_packet_release(server_packet);

      /* toggle the green led on success */
      HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin);
    }
    else
    {
      /* connection lost with the server, exit the loop */
      break;
    }
    /* Add a short timeout to let the echool tool correctly
    process the just sent packet before sending a new one */
    tx_thread_sleep(20);
  }
  /* unbind the socket and delete it */
  nx_udp_socket_unbind(&UDPSocket);
  nx_udp_socket_delete(&UDPSocket);

  if (count == MAX_PACKET_COUNT + 1)
  {
    printf("\n-------------------------------------\n\tSUCCESS : %u / %u packets sent\n-------------------------------------\n", count - 1, MAX_PACKET_COUNT);
    Success_Handler();
  }
  else
  {
    printf("\n-------------------------------------\n\tFAIL : %u / %u packets sent\n-------------------------------------\n", count - 1, MAX_PACKET_COUNT);
    Error_Handler();
  }
}

/**
* @brief  Link thread entry
* @param thread_input: ULONG thread parameter
* @retval none
*/
static VOID App_Link_Thread_Entry(ULONG thread_input)
{
  ULONG actual_status;
  UINT linkdown = 0, status;

  while(1)
  {
    /* Get Physical Link stackavailtus. */
    status = nx_ip_interface_status_check(&NetXDuoEthIpInstance, 0, NX_IP_LINK_ENABLED,
                                      &actual_status, 10);

    if(status == NX_SUCCESS)
    {
      if(linkdown == 1)
      {
        linkdown = 0;
        status = nx_ip_interface_status_check(&NetXDuoEthIpInstance, 0, NX_IP_ADDRESS_RESOLVED,
                                      &actual_status, 10);
        if(status == NX_SUCCESS)
        {
          /* The network cable is connected again. */
          printf("The network cable is connected again.\n");
          /* Print UDP Echo Client is available again. */
          printf("UDP Echo Client is available again.\n");
        }
        else
        {
          /* The network cable is connected. */
          printf("The network cable is connected.\n");
          /* Send command to Enable Nx driver. */
          nx_ip_driver_direct_command(&NetXDuoEthIpInstance, NX_LINK_ENABLE,
                                      &actual_status);
          /* Restart DHCP Client. */
          nx_dhcp_stop(&DHCPClient);
          nx_dhcp_start(&DHCPClient);
        }
      }
    }
    else
    {
      if(0 == linkdown)
      {
        linkdown = 1;
        /* The network cable is not connected. */
        printf("The network cable is not connected.\n");
      }
    }

    tx_thread_sleep(NX_APP_CABLE_CONNECTION_CHECK_PERIOD);
  }
}
/* USER CODE END 1 */

相应的配置UDP服务器相关信息如下:


#define DEFAULT_PORT             6000
#define UDP_SERVER_PORT          DEFAULT_PORT
#define UDP_SERVER_ADDRESS       IP_ADDRESS(192, 168, 32, 21)

#define MAX_PACKET_COUNT         100
#define DEFAULT_MESSAGE          "NetXDuo On STM32H735"

电脑的IP:192, 168, 32, 21  端口:6000   开发板IP:192.168.32.25 端口:3000

下载程序后通信如下图:

开发板状态如下图:

此帖出自stm32/stm8论坛

最新回复

开发板的IP是DHCP server分配的;   \STM32CubeH5\STM32CubeH5\Projects\NUCLEO-H563ZI\Applications\NetXDuo\Nx_UDP_Echo_Client\NetXDuo\App\app_netxduo.h==这里满可以修改与之通信的对端ip和端口   详情 回复 发表于 2024-1-31 10:31
点赞(1) 关注
 

回复
举报

7048

帖子

11

TA的资源

版主

沙发
 
这个可以跑lwip吧,板子很强呀!
此帖出自stm32/stm8论坛

点评

可以的,只是cubemx上面直接有这个库,生成代码比较方便  详情 回复 发表于 2023-6-14 09:27
 
 

回复

650

帖子

8

TA的资源

纯净的硅(初级)

板凳
 
lugl4313820 发表于 2023-6-14 08:58 这个可以跑lwip吧,板子很强呀!

可以的,只是cubemx上面直接有这个库,生成代码比较方便

此帖出自stm32/stm8论坛
 
 

回复

6534

帖子

9

TA的资源

版主

4
 

这以太网速度能跑多快啊?  

此帖出自stm32/stm8论坛

点评

还没加iperf网速测试代码,后面添加测试看看  详情 回复 发表于 2023-6-14 21:38
 
个人签名

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

回复

6107

帖子

4

TA的资源

版主

5
 

板子真好。

此帖出自stm32/stm8论坛
 
 
 

回复

7

帖子

0

TA的资源

一粒金砂(中级)

6
 

我连接了st link typec, 然后usb连接到电脑,电脑识别不到板子,大佬们知道怎么回事吗?驱动都安装了。设备管理器什么也看不到

 

此帖出自stm32/stm8论坛

点评

确认你usb线有没有问题  详情 回复 发表于 2023-6-14 20:50
 
 
 

回复

650

帖子

8

TA的资源

纯净的硅(初级)

7
 
amdyes 发表于 2023-6-14 20:44 我连接了st link typec, 然后usb连接到电脑,电脑识别不到板子,大佬们知道怎么回事吗?驱动都安装了。设 ...

确认你usb线有没有问题

此帖出自stm32/stm8论坛
 
 
 

回复

7

帖子

0

TA的资源

一粒金砂(中级)

8
 
29447945 发表于 2023-6-14 20:50 确认你usb线有没有问题

换了typec转typec的就好了 但是现在就是软件连不上了

 

此帖出自stm32/stm8论坛

点评

按照提示,进入配置把under reset勾上试试,感觉你的线不太好是不是?  详情 回复 发表于 2023-6-14 21:41
 
 
 

回复

650

帖子

8

TA的资源

纯净的硅(初级)

9
 
秦天qintian0303 发表于 2023-6-14 10:55 这以太网速度能跑多快啊?  

还没加iperf网速测试代码,后面添加测试看看

此帖出自stm32/stm8论坛
 
 
 

回复

650

帖子

8

TA的资源

纯净的硅(初级)

10
 
amdyes 发表于 2023-6-14 21:32 换了typec转typec的就好了 但是现在就是软件连不上了  

按照提示,进入配置把under reset勾上试试,感觉你的线不太好是不是?

此帖出自stm32/stm8论坛
 
 
 

回复

7

帖子

0

TA的资源

一粒金砂(中级)

11
 

也试过 不行 按照提示 方便微信私聊吗

此帖出自stm32/stm8论坛
 
 
 

回复

7

帖子

0

TA的资源

一粒金砂(中级)

12
 
29447945 发表于 2023-6-14 21:41 按照提示,进入配置把under reset勾上试试,感觉你的线不太好是不是?

在线等,搞了一个晚上了 头都大了 

此帖出自stm32/stm8论坛

点评

根据软件提示操作,把under reset勾上,降低swd速率,不过感觉你的问题可能出在你的USB线上。  详情 回复 发表于 2023-6-14 22:08
 
 
 

回复

650

帖子

8

TA的资源

纯净的硅(初级)

13
 
amdyes 发表于 2023-6-14 21:54 在线等,搞了一个晚上了 头都大了 

根据软件提示操作,把under reset勾上,降低swd速率,不过感觉你的问题可能出在你的USB线上。

此帖出自stm32/stm8论坛
 
 
 

回复

7

帖子

0

TA的资源

一粒金砂(中级)

14
 
29447945 发表于 2023-6-14 22:08 根据软件提示操作,把under reset勾上,降低swd速率,不过感觉你的问题可能出在你的USB线上。

但是我在设备管理器上可以看到我的com口是打开的,也按照提示的勾上了

 

此帖出自stm32/stm8论坛
 
 
 

回复

7

帖子

0

TA的资源

一粒金砂(中级)

15
 

如果有小伙伴连不上的话,推荐使用cubeprogammer连接

此帖出自stm32/stm8论坛
 
 
 

回复

162

帖子

0

TA的资源

一粒金砂(高级)

16
 

st的mxcube确实好用,网口通讯程序,原来也如此好用,牛逼,

此帖出自stm32/stm8论坛
 
 
 

回复

1

帖子

0

TA的资源

一粒金砂(初级)

17
 

请问,开发板的IP和端口号在哪个地方设置,谢谢。

此帖出自stm32/stm8论坛
 
 
 

回复

1

帖子

0

TA的资源

一粒金砂(初级)

18
 
496916943 发表于 2023-7-8 18:10 请问,开发板的IP和端口号在哪个地方设置,谢谢。

开发板的IP是DHCP server分配的;

 

\STM32CubeH5\STM32CubeH5\Projects\NUCLEO-H563ZI\Applications\NetXDuo\Nx_UDP_Echo_Client\NetXDuo\App\app_netxduo.h==这里满可以修改与之通信的对端ip和端口

此帖出自stm32/stm8论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/7 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表