【FreeRTOS】基于STM32移植LWIP 2.1.2详细步骤

  • Post author:
  • Post category:其他




1. 前言

在STM32平台移植LWIP 2.1.2功能,首先需要做一些准备工作,例如:

  1. 下载资料:

    lwip-2.1.2



    contrib-2.1.0



    STM32F4x7_ETH_LwIP_V1.1.1

    ,参考:

    《基于STM32移植LWIP的资料准备》
  2. 准备好已经在STM32移植好的FreeRTOS的工程。

移植的目标平台是


STM32F429


,在以前资料准备中有提到STM32官网有


STM32F4x7


微控制器的

LwIP TCP/IP协议栈

的演示代码,我们在很大程度上可以参考移植到目标平台。



2. STM32运行LwIP的系统框图

我们在STM32平台上面移植LwIP,主要完成以下


4


个部分:



  • RTOS


    :在STM32运行FreeRTOS,并且为


    LwIP


    协议栈提供

    Mutex



    Mail box



    Create Thread

    等API接口。


  • Network System Config


    :主要对


    LwIP


    协议栈的系统设置。


  • LwIP Stack


    :这个是


    LwIP 2.1.2 TCP/IP


    协议栈,我们只需要将源码添加到工程即可。


  • Hardware Driver


    :主要是STM32平台ETH接口的驱动层,例如:

    GPIOs

    ,

    clocks

    ,

    MAC

    ,

    DMA

    的设置。




4


个部分之间的联系框图如下:



3. LwIP RTOS部分的移植

在LwIP RTOS部分的移植我们可以参考


contrib-2.1.0


,这部分主要是为


LwIP


协议栈提供

Mutex



Mail box



Create Thread

等API接口。因为每个OS对这些API接口的具体实现都会有差异,所以LwIP官方提供了一个模板。正好


contrib-2.1.0


里面有提供FreeRTOS这些接口的实现,我们可以直接移植过来,代码路径如下:

PATH:

contrib-2.1.0\ports\freertos\sys_arch.c

RTOS提供提供架构的接口,如下:



4. LwIP System Config部分的移植

在LwIP System Config部分的移植我们可以参考


STM32F4x7_ETH_LwIP_V1.1.1


(PATH:

STM32F4x7_ETH_LwIP_V1.1.1\Project\FreeRTOS\udptcp_echo_server_netconn\src\netconf.c

),主要的步骤如下:



  1. tcpip_init()


    : 主要是创建tcp_ip stack线程,并且调用

    lwip_init()

    函数初始化LwIP协议栈。


  2. netif_add()


    : 申请一个

    struct netif

    ,设置默认的IP地址、子网掩码和网关,并且将其网络接口添加到

    netif_list



  3. netif_set_default()


    :注册新申请的网络接口为默认网络接口。


  4. netif_set_up()


    :启动网络接口。


  5. dhcp_start()


    :开始DHCP自动获取IP地址,如果设置默认IP地址,可不调用。(


    备注:非必须调用


LwIP System Config还有一个更重要部分,就是LwIP系统选项配置的头文件

lwipopts.h

。我这里的配置如下:(仅供参考)

#ifndef __LWIPOPTS_H__
#define __LWIPOPTS_H__

/**
 * SYS_LIGHTWEIGHT_PROT==1: if you want inter-task protection for certain
 * critical regions during buffer allocation, deallocation and memory
 * allocation and deallocation.
 */
#define SYS_LIGHTWEIGHT_PROT    0

#define ETHARP_TRUST_IP_MAC     0
#define IP_REASSEMBLY           0
#define IP_FRAG                 0
#define ARP_QUEUEING            0

#define LWIP_IPV4               1

/**
 * NO_SYS==1: Provides VERY minimal functionality. Otherwise,
 * use lwIP facilities.
 */
#define NO_SYS                  0

/* ---------- Memory options ---------- */
/* MEM_ALIGNMENT: should be set to the alignment of the CPU for which
   lwIP is compiled. 4 byte alignment -> define MEM_ALIGNMENT to 4, 2
   byte alignment -> define MEM_ALIGNMENT to 2. */
#define MEM_ALIGNMENT           4

/* MEM_SIZE: the size of the heap memory. If the application will send
a lot of data that needs to be copied, this should be set high. */
#define MEM_SIZE                (5*1024)

/* MEMP_NUM_PBUF: the number of memp struct pbufs. If the application
   sends a lot of data out of ROM (or other static memory), this
   should be set high. */
#define MEMP_NUM_PBUF           100
/* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One
   per active UDP "connection". */
#define MEMP_NUM_UDP_PCB        6
/* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP
   connections. */
#define MEMP_NUM_TCP_PCB        10
/* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP
   connections. */
#define MEMP_NUM_TCP_PCB_LISTEN 5
/* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP
   segments. */
#define MEMP_NUM_TCP_SEG        20
/* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active
   timeouts. */
#define MEMP_NUM_SYS_TIMEOUT    10


/* ---------- Pbuf options ---------- */
/* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */
#define PBUF_POOL_SIZE          20

/* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */
#define PBUF_POOL_BUFSIZE       500


/* ---------- TCP options ---------- */
#define LWIP_TCP                1
#define TCP_TTL                 255

/* Controls if TCP should queue segments that arrive out of
   order. Define to 0 if your device is low on memory. */
#define TCP_QUEUE_OOSEQ         0

/* TCP Maximum segment size. */
#define TCP_MSS                 (1500 - 40)	  /* TCP_MSS = (Ethernet MTU - IP header size - TCP header size) */

/* TCP sender buffer space (bytes). */
#define TCP_SND_BUF             (5*TCP_MSS)

/*  TCP_SND_QUEUELEN: TCP sender buffer space (pbufs). This must be at least
  as much as (2 * TCP_SND_BUF/TCP_MSS) for things to work. */

#define TCP_SND_QUEUELEN        (4* TCP_SND_BUF/TCP_MSS)

/* TCP receive window. */
#define TCP_WND                 (2*TCP_MSS)


/* ---------- ICMP options ---------- */
#define LWIP_ICMP                       1


/* ---------- DHCP options ---------- */
/* Define LWIP_DHCP to 1 if you want DHCP configuration of
   interfaces. DHCP is not implemented in lwIP 0.5.1, however, so
   turning this on does currently not work. */
#define LWIP_DHCP               1


/* ---------- UDP options ---------- */
#define LWIP_UDP                1
#define UDP_TTL                 255


/* ---------- Statistics options ---------- */
#define LWIP_STATS 0
#define LWIP_PROVIDE_ERRNO 1

/* ---------- link callback options ---------- */
/* LWIP_NETIF_LINK_CALLBACK==1: Support a callback function from an interface
 * whenever the link changes (i.e., link down)
 */
#define LWIP_NETIF_LINK_CALLBACK        1

/*
   --------------------------------------
   ---------- Checksum options ----------
   --------------------------------------
*/

/* 
The STM32F4x7 allows computing and verifying the IP, UDP, TCP and ICMP checksums by hardware:
 - To use this feature let the following define uncommented.
 - To disable it and process by CPU comment the  the checksum.
*/
#define CHECKSUM_BY_HARDWARE 


#ifdef CHECKSUM_BY_HARDWARE
  /* CHECKSUM_GEN_IP==0: Generate checksums by hardware for outgoing IP packets.*/
  #define CHECKSUM_GEN_IP                 0
  /* CHECKSUM_GEN_UDP==0: Generate checksums by hardware for outgoing UDP packets.*/
  #define CHECKSUM_GEN_UDP                0
  /* CHECKSUM_GEN_TCP==0: Generate checksums by hardware for outgoing TCP packets.*/
  #define CHECKSUM_GEN_TCP                0 
  /* CHECKSUM_CHECK_IP==0: Check checksums by hardware for incoming IP packets.*/
  #define CHECKSUM_CHECK_IP               0
  /* CHECKSUM_CHECK_UDP==0: Check checksums by hardware for incoming UDP packets.*/
  #define CHECKSUM_CHECK_UDP              0
  /* CHECKSUM_CHECK_TCP==0: Check checksums by hardware for incoming TCP packets.*/
  #define CHECKSUM_CHECK_TCP              0
  /* CHECKSUM_CHECK_ICMP==0: Check checksums by hardware for incoming ICMP packets.*/
  #define CHECKSUM_GEN_ICMP               0
#else
  /* CHECKSUM_GEN_IP==1: Generate checksums in software for outgoing IP packets.*/
  #define CHECKSUM_GEN_IP                 1
  /* CHECKSUM_GEN_UDP==1: Generate checksums in software for outgoing UDP packets.*/
  #define CHECKSUM_GEN_UDP                1
  /* CHECKSUM_GEN_TCP==1: Generate checksums in software for outgoing TCP packets.*/
  #define CHECKSUM_GEN_TCP                1
  /* CHECKSUM_CHECK_IP==1: Check checksums in software for incoming IP packets.*/
  #define CHECKSUM_CHECK_IP               1
  /* CHECKSUM_CHECK_UDP==1: Check checksums in software for incoming UDP packets.*/
  #define CHECKSUM_CHECK_UDP              1
  /* CHECKSUM_CHECK_TCP==1: Check checksums in software for incoming TCP packets.*/
  #define CHECKSUM_CHECK_TCP              1
  /* CHECKSUM_CHECK_ICMP==1: Check checksums by hardware for incoming ICMP packets.*/
  #define CHECKSUM_GEN_ICMP               1
#endif


/*
   ----------------------------------------------
   ---------- Sequential layer options ----------
   ----------------------------------------------
*/
/**
 * LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c)
 */
#define LWIP_NETCONN                    1

/*
   ------------------------------------
   ---------- Socket options ----------
   ------------------------------------
*/
/**
 * LWIP_SOCKET==1: Enable Socket API (require to use sockets.c)
 */
#define LWIP_SOCKET                     0

/*
   -----------------------------------
   ---------- DEBUG options ----------
   -----------------------------------
*/

#define LWIP_DEBUG                      0


/*
   ---------------------------------
   ---------- OS options ----------
   ---------------------------------
*/

#define TCPIP_THREAD_NAME              "TCP/IP"
#define TCPIP_THREAD_STACKSIZE          1000
#define TCPIP_MBOX_SIZE                 5
#define DEFAULT_UDP_RECVMBOX_SIZE       2000
#define DEFAULT_TCP_RECVMBOX_SIZE       2000
#define DEFAULT_ACCEPTMBOX_SIZE         2000
#define DEFAULT_THREAD_STACKSIZE        500
#define TCPIP_THREAD_PRIO               3 

//#define LWIP_COMPAT_MUTEX 1

#endif /* __LWIPOPTS_H__ */



5. LwIP Stack部分的移植

  • 将LwIP 2.1.2的源码添加到工程,分别为

    api



    core



    netif

    ,详细如下:



  • 将LwIP 2.1.2的头文件路径添加到Keil:



6. LwIP Hardware Driver部分的移植

LwIP Hardware Driver部分的移植主要是2个文件,如下:



  1. bsp_eth.c


    : 主要是设置Ethernet的GPIOs, clocks, MAC, DMA,参考:

    STM32F4x7_ETH_LwIP_V1.1.1\Project\FreeRTOS\udptcp_echo_server_netconn\src\stm32f4x7_eth_bsp.c


  2. ethernetif.c


    : 主要是创建了一个

    ETH_MAC

    线程,读取以太网的消息。

    low_level_input

    收到消息后,然后通过call back调用

    ethernet_input

    传递到LwIP层去解析。如果LwIP层有消息需要发送,就通过

    ethernet_output

    调用ETH Hardware Driver层的

    low_level_output

    发送出去。参考:

    STM32F4x7_ETH_LwIP_V1.1.1\Utilities\Third_Party\lwip-1.4.1\port\STM32F4x7\FreeRTOS\ethernetif.c



7. 验证测试

最后验证测试成功,如下

在这里插入图片描述



8. 资料下载地址

移植成功的完整代码下载地址如下:


https://download.csdn.net/download/ZHONGCAI0901/13100126



版权声明:本文为ZHONGCAI0901原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。