GD32F450使用

[复制链接]
1181|10
 楼主| 鱿鱼丝 发表于 2023-4-30 20:39 | 显示全部楼层 |阅读模式
固件库
  1. gpio_af_set();                                //pin复用模式
  2. gpio_mode_set();                        //IO模式:输入输出、上下拉
  3. gpio_output_options_set();        //推拉、速率
选择型号

gd32f4xx.h中要选择对应型号的定义:GD32F450

  1. /* define GD32F4xx */
  2. #if !defined (GD32F450)  && !defined (GD32F405) && !defined (GD32F407)
  3.   /* #define GD32F450 */
  4.    #define GD32F450
  5.   /* #define GD32F405 */
  6.   /* #define GD32F407 */
  7. #endif /* define GD32F4xx */



 楼主| 鱿鱼丝 发表于 2023-4-30 20:40 | 显示全部楼层
网络
lwip的mac地址和芯片mac寄存器中的mac地址要一致。
 楼主| 鱿鱼丝 发表于 2023-4-30 20:40 | 显示全部楼层
(1)lwip的mac地址在drv_enet.c中的rt_hw_gd32_eth_init(void)函数中定义:
  1. // OUI 00-00-0E FUJITSU LIMITED
  2.     gd32_emac_device0.dev_addr[0] = 0x00;                   //mac地址
  3.     gd32_emac_device0.dev_addr[1] = 0x00;
  4.     gd32_emac_device0.dev_addr[2] = 0x0E;
  5.     /* set mac address: (only for test) */
  6.     gd32_emac_device0.dev_addr[3] = 0x12;
  7.     gd32_emac_device0.dev_addr[4] = 0x34;
  8.     gd32_emac_device0.dev_addr[5] = 0x56;
 楼主| 鱿鱼丝 发表于 2023-4-30 20:40 | 显示全部楼层
(2)程序寄存器的mac地址:
drv_enet.c中的rt_hw_gd32_eth_init(void)函数中调用EMAC_MAC_Addr_config():

  1. //drv_enet.c
  2. rt_hw_gd32_eth_init(void)
  3.          EMAC_MAC_Addr_config(ETHERNET_MAC, EMAC_MAC_Address0, (uint8_t*)&gd32_emac_device->dev_addr[0]);
  4. //synopsys_emac.c
  5. void EMAC_MAC_Addr_config(struct rt_synopsys_eth * ETHERNET_MAC, rt_uint32_t MacAddr, rt_uint8_t *Addr)
 楼主| 鱿鱼丝 发表于 2023-4-30 20:40 | 显示全部楼层
lwip
3.3.0 设置固定IP
注意要把lwip的DCHP功能去掉,并把网关的初始值也去掉。分别去掉“Disable alloc ip address through DCHP”、“Static IPv4 Address”中设置。
 楼主| 鱿鱼丝 发表于 2023-4-30 20:41 | 显示全部楼层
 楼主| 鱿鱼丝 发表于 2023-4-30 20:41 | 显示全部楼层
  1. //gd32f4xx_enet.h中
  2. #if(PHY_TYPE == LAN8700)
  3.     #define PHY_SR                           31U                                    /*!< tranceiver status register */
  4.     #define PHY_SPEED_STATUS                 ((uint16_t)0x0004)                     /*!< configured information of speed: 10Mbit/s */
  5.     #define PHY_DUPLEX_STATUS                ((uint16_t)0x0010)                     /*!< configured information of duplex: full-duplex */
  6. #elif(PHY_TYPE == DP83848)
  7.     #define PHY_SR                           16U                                    /*!< tranceiver status register */
  8.     #define PHY_SPEED_STATUS                 ((uint16_t)0x0002)                     /*!< configured information of speed: 10Mbit/s */
  9.     #define PHY_DUPLEX_STATUS                ((uint16_t)0x0004)                     /*!< configured information of duplex: full-duplex */
  10. #elif(PHY_TYPE == YT8512)
  11.     #define PHY_SR                           17U                                    /*!< tranceiver status register */
  12.     #define PHY_SPEED_STATUS                 ((uint16_t)0x4000)                     /*!< configured information of speed: 10Mbit/s */
  13.     #define PHY_DUPLEX_STATUS                ((uint16_t)0x2000)                     /*!< configured information of duplex: full-duplex */
  14. #endif /* PHY_TYPE */
 楼主| 鱿鱼丝 发表于 2023-4-30 20:42 | 显示全部楼层
  1. //gd32f4xx_enet.c中的
  2. ErrStatus enet_init(enet_mediamode_enum mediamode, enet_chksumconf_enum checksum, enet_frmrecept_enum recept)
  3.         if((uint16_t)RESET != (phy_value & PHY_DUPLEX_STATUS)){
  4.             media_temp = ENET_MODE_FULLDUPLEX;
  5.         }else{
  6.             media_temp = ENET_MODE_HALFDUPLEX;
  7.         }
  8.         /* configure the communication speed of MAC following the auto-negotiation result */
  9.         if((uint16_t)RESET !=(phy_value & PHY_SPEED_STATUS)){               //设置速率---
  10.             media_temp |= ENET_SPEEDMODE_100M;                              //为1时
  11.         }else{
  12.             media_temp |= ENET_SPEEDMODE_10M;
  13.         }
 楼主| 鱿鱼丝 发表于 2023-4-30 20:43 | 显示全部楼层
3.3.2 启动流程
1、lwip初始化在:int lwip_system_init(void)
2、使用lwip注册设备:

  1. int rt_hw_gd32_eth_init(void)
  2. {
  3.     rt_kprintf("rt_gd32_eth_init...\n");
  4.    
  5.     /* enable ethernet clock  */
  6.     rcu_periph_clock_enable(RCU_ENET);
  7.     rcu_periph_clock_enable(RCU_ENETTX);
  8.     rcu_periph_clock_enable(RCU_ENETRX);
  9.    
  10.     nvic_configuration();                                   //中断配置
  11.   
  12.     /* configure the GPIO ports for ethernet pins */
  13.     enet_gpio_config();                                     //管脚配置
  14.    
  15.     /* set autonegotiation mode */
  16.     gd32_emac_device0.phy_mode = EMAC_PHY_AUTO;
  17.     gd32_emac_device0.ETHERNET_MAC = ETHERNET_MAC0;         //寄存器地址
  18.     gd32_emac_device0.ETHER_MAC_IRQ  = ENET_IRQn;

  19.     // OUI 00-00-0E FUJITSU LIMITED
  20.     gd32_emac_device0.dev_addr[0] = 0x00;                   //mac地址
  21.     gd32_emac_device0.dev_addr[1] = 0x00;
  22.     gd32_emac_device0.dev_addr[2] = 0x0E;
  23.     /* set mac address: (only for test) */
  24.     gd32_emac_device0.dev_addr[3] = 0x12;
  25.     gd32_emac_device0.dev_addr[4] = 0x34;
  26.     gd32_emac_device0.dev_addr[5] = 0x56;

  27.     gd32_emac_device0.parent.parent.init                = gd32_emac_init;       //mac初始化
  28.     gd32_emac_device0.parent.parent.open                = gd32_emac_open;
  29.     gd32_emac_device0.parent.parent.close            = gd32_emac_close;
  30.     gd32_emac_device0.parent.parent.read                = gd32_emac_read;
  31.     gd32_emac_device0.parent.parent.write            = gd32_emac_write;
  32.     gd32_emac_device0.parent.parent.control            = gd32_emac_control;
  33.     gd32_emac_device0.parent.parent.user_data   = RT_NULL;

  34.     gd32_emac_device0.parent.eth_rx                            = gd32_emac_rx;
  35.     gd32_emac_device0.parent.eth_tx                            = gd32_emac_tx;

  36.     /* init tx buffer free semaphore */
  37.     rt_sem_init(&gd32_emac_device0.tx_buf_free, "tx_buf0", EMAC_TXBUFNB, RT_IPC_FLAG_FIFO);
  38.     eth_device_init(&(gd32_emac_device0.parent), "e0");                                         //lwip向系统注册网卡e0-----
  39.    
  40.     /* change device link status */
  41.     eth_device_linkchange(&(gd32_emac_device0.parent), RT_TRUE);
  42.    
  43.     return 0;
  44. }
  45. INIT_DEVICE_EXPORT(rt_hw_gd32_eth_init);
 楼主| 鱿鱼丝 发表于 2023-4-30 20:43 | 显示全部楼层
管脚配置要根据板子实际来初始化:

60350644e6280b46bb.png
 楼主| 鱿鱼丝 发表于 2023-4-30 20:43 | 显示全部楼层
3、netif_set_status_callback()函数可以注册NETIF_STATUS_CALLBACK回调函数,有3个地方调用:
netif_set_up、netif_set_down、netif_set_ipaddr
您需要登录后才可以回帖 登录 | 注册

本版积分规则

47

主题

480

帖子

2

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