dsjsjf 发表于 2017-7-22 16:30:01

【STM32以太网在线培训】+ F767 以太网初体验

首先,非常感谢社区有这么好的活动,虽然我没拿到769DISCOVERY板,但我看了完整的直播,知识才是无价的嘛;P;P;P;P;P。
不过嘛,我有块NUCLEO-767ZI的板子,可以先玩玩。
工欲善其事必先利其器,先更新工具及库,STM32CubeMX,HAL库,IAR,均升级到最新版本。


这次培训是进阶培训,对我这种没有以太网经验的,还是有些深奥,不过不怕,一步一步的来。最新的F7 HAL库中刚好有Nucleo-F767ZI的LwIP例程,那我就从这里开始吧。首先找到这个工程所在的目录,如果是默认安装的话,应该在这个路径下C:\Users\Administrator\STM32Cube\Repository\STM32Cube_FW_F7_V1.7.0\Projects\STM32F767ZI-Nucleo\Applications\LwIP\LwIP_HTTP_Server_Netconn_RTOS。
我习惯用IAR,所以打开用IAR打开EWARM\Project.eww。

打开后如图,先编译一下,应该是没错误没警告,如果有,一般是MCU选择或者头文件包含路径等不对,检查一下就可以了。


编译后就可以下载了吗?当然不是,还是先了解一下程序内容吧。打开main.c文件,找到main()函数。
int main(void)
{
/* Configure the MPU attributes as Device memory for ETH DMA descriptors */
MPU_Config();

/* Enable the CPU Cache */
CPU_CACHE_Enable();
/* STM32F7xx HAL library initialization:
       - Configure the Flash ART accelerator on ITCM interface
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
   */
HAL_Init();

/* Configure the system clock to 200 MHz */
SystemClock_Config();

/*configure LED1 and LED3 */
BSP_LED_Init(LED1);
BSP_LED_Init(LED3);
/* Init thread */
#if defined(__GNUC__)
osThreadDef(Start, StartThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 5);
#else
osThreadDef(Start, StartThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 2);
#endif

osThreadCreate (osThread(Start), NULL);

/* Start scheduler */
osKernelStart();

/* We should never get here as control is now taken by the scheduler */
for( ;; );
}

这里面初始化了MCU时钟及LED等,然后创建了一个任务,那就再看这个任务
static void StartThread(void const * argument)
{
/* Create tcp_ip stack thread */
tcpip_init(NULL, NULL);

/* Initialize the LwIP stack */
Netif_Config();

/* Initialize webserver demo */
http_server_netconn_init();

/* Notify user about the network interface config */
User_notification(&gnetif);

#ifdef USE_DHCP
/* Start DHCPClient */
osThreadDef(DHCP, DHCP_thread, osPriorityBelowNormal, 0, configMINIMAL_STACK_SIZE * 2);
osThreadCreate (osThread(DHCP), &gnetif);
#endif
for( ;; )
{
    /* Delete the Init Thread */
    osThreadTerminate(NULL);
}
}
这个函数里东西就比较多了,网络接口的配置,tcpip初始化及任务,HTTP任务,DHCP任务等,这里应该是需要路由器分配IP地址,但我的板子是直连电脑的,所以需要找到固定IP配置的地方,在Netif_Config()中
static void Netif_Config(void)
{
ip_addr_t ipaddr;
ip_addr_t netmask;
ip_addr_t gw;

#ifdef USE_DHCP
ip_addr_set_zero_ip4(&ipaddr);
ip_addr_set_zero_ip4(&netmask);
ip_addr_set_zero_ip4(&gw);
#else
IP_ADDR4(&ipaddr,IP_ADDR0,IP_ADDR1,IP_ADDR2,IP_ADDR3);
IP_ADDR4(&netmask,NETMASK_ADDR0,NETMASK_ADDR1,NETMASK_ADDR2,NETMASK_ADDR3);
IP_ADDR4(&gw,GW_ADDR0,GW_ADDR1,GW_ADDR2,GW_ADDR3);
#endif /* USE_DHCP */

/* add the network interface */   
netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);

/*Registers the default network interface. */
netif_set_default(&gnetif);

if (netif_is_link_up(&gnetif))
{
    /* When the netif is fully configured this function must be called.*/
    netif_set_up(&gnetif);
}
else
{
    /* When the netif link is down this function must be called */
    netif_set_down(&gnetif);
}
}
其中ipaddr,netmask,gw分别是IP地址,子网掩码,网关,找到宏定义的地方,改成与电脑同一网关

/*Static IP ADDRESS*/
#define IP_ADDR0   172
#define IP_ADDR1   16
#define IP_ADDR2   11
#define IP_ADDR3   10
   
/*NETMASK*/
#define NETMASK_ADDR0   255
#define NETMASK_ADDR1   255
#define NETMASK_ADDR2   255
#define NETMASK_ADDR3   0
/*Gateway Address*/
#define GW_ADDR0   172
#define GW_ADDR1   16
#define GW_ADDR2   11
#define GW_ADDR3   1

其他暂时不用管,编译下载运行,一气呵成。。。

运行后,板上LD3(红色)先亮,大概二三十秒后,LD1(绿灯)亮,对照找一下程序
case DHCP_WAIT_ADDRESS:
      {               
      if (dhcp_supplied_address(netif))
      {
          DHCP_state = DHCP_ADDRESS_ASSIGNED;
         
          BSP_LED_Off(LED3);
          BSP_LED_On(LED1);
      }
      else
      {
          dhcp = (struct dhcp *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
   
          /* DHCP timeout */
          if (dhcp->tries > MAX_DHCP_TRIES)
          {
            DHCP_state = DHCP_TIMEOUT;
            
            /* Stop DHCP */
            dhcp_stop(netif);
            
            /* Static address used */
            IP_ADDR4(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
            IP_ADDR4(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
            IP_ADDR4(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
            netif_set_addr(netif, ip_2_ip4(&ipaddr), ip_2_ip4(&netmask), ip_2_ip4(&gw));
            BSP_LED_Off(LED3);
            BSP_LED_On(LED1);
            
          }
          else
          {
            BSP_LED_On(LED3);
          }
      }
      }
      break;
理解为,等待DHCP分配IP时是红灯,分配好了或者超时后配置成固定IP是绿灯,当然我用的是固定IP。
既然IP已经配置好了,那就来Ping一下吧。果然可以Ping通。至此,官方例程运行OK。


对了,刚才在程序中看到一个Http任务,那是什么,打开IE,在地址栏中输入开发板的IP:172.16.11.10,网页可以打开,如下:


通过官方例程,体验了一次STM32F7的以太网,接下来,要利用STM32CubeMX创建属于我自己的以太网工程……

Inc_brza 发表于 2017-7-22 16:52:00

培训的时候,FAE说,虽然CubeMax可以很方便的生成一个lwip的工程,但是仍然建议去了解学习一下啊
lwip的源码!

zero99 发表于 2017-7-24 09:51:56

谢谢分享~

jy04706985 发表于 2018-10-20 10:59:28

买了块STM32F767ZI_NUCLEO,不知道哪里能下载到基础例程?

dsjsjf 发表于 2018-10-20 13:05:02

jy04706985 发表于 2018-10-20 10:59
买了块STM32F767ZI_NUCLEO,不知道哪里能下载到基础例程?

官方自带,HAL库的目录下有的

damiaa 发表于 2018-11-9 12:08:22

好 到时候也玩玩
页: [1]
查看完整版本: 【STM32以太网在线培训】+ F767 以太网初体验