[应用相关] STM32 ENC28J60移植与入门

[复制链接]
2014|10
 楼主| dongnanxibei 发表于 2018-5-13 21:59 | 显示全部楼层 |阅读模式
(2013年)的整理了LwIP相关代码,并在STM32上“裸奔”成功。一直没有时间深入整理,在这里借博文整理总结。LwIP的移植过程细节很多,博文也不可能一一详解个别部分只能点到为止。
    【本文要点】
    【1】不带操作系统的LwIP移植,LwIP版本为1.4.1。
    【2】MCU为STM32F103VE,网卡为ENC28J60。
    【3】移植过程重点描述ethernetif.c和LwIP宏配置等。
    【4】一个简单的TCP echo例子。
    【5】力求简单,没有DHCP功能,甚至没有用到网卡中断。
    【代码仓库】
    代码仓库位于Bitbucket(要源代码请点击这里)。博文中不能把每个细节描述清楚,更多内容请参考代码仓库中的具体代码。

    【硬件说明】
    测试平台使用奋斗版,原理图请参考代码仓库中的DOC目录。
1.png


 楼主| dongnanxibei 发表于 2018-5-13 22:07 | 显示全部楼层
1.ethernetif.c的相关修改
    虽然LwIP移植过程比较复杂,但是只要结合网卡具体功能,耐心修改ethernetif.c即可。ethernetif.c重点实现网卡的三个功能,初始化,发送和接收。
    为了更好的配合lwIP,修改了ENC28J60学习笔记中部分驱动函数。(换句话说,想要从0开始移植LwIP必须对操作网卡非常熟悉)
    【1】初始化
  1. static void
  2. low_level_init(struct netif *netif)
  3. {
  4.   struct ethernetif *ethernetif = netif->state;

  5.   /* set MAC hardware address length */
  6.   netif->hwaddr_len = ETHARP_HWADDR_LEN;

  7.   /* set MAC hardware address */
  8.   netif->hwaddr[0] = 'A';
  9.   netif->hwaddr[1] = 'R';
  10.   netif->hwaddr[2] = 'M';
  11.   netif->hwaddr[3] = 'N';
  12.   netif->hwaddr[4] = 'E';
  13.   netif->hwaddr[5] = 'T';

  14.   /* maximum transfer unit */
  15.   netif->mtu = 1500;

  16.   /* device capabilities */
  17.   /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  18.   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;

  19.   /* Do whatever else is needed to initialize interface. */
  20.   enc28j60_init(netif->hwaddr); // 【1】
  21. }
【说明】
        【1】 enc28j60_init(netif->hwaddr); low_level_init中指定了enc28j60中的网卡地址。



 楼主| dongnanxibei 发表于 2018-5-13 22:08 | 显示全部楼层
【2】发送
  1. static err_t
  2. low_level_output(struct netif *netif, struct pbuf *p)
  3. {
  4.   struct ethernetif *ethernetif = netif->state;
  5.   struct pbuf *q;

  6.   enc28j60_init_send(p->tot_len); //【1】initiate transfer();

  7. #if ETH_PAD_SIZE
  8.   pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
  9. #endif

  10.   for(q = p; q != NULL; q = q->next) {
  11.     /* Send the data from the pbuf to the interface, one pbuf at a
  12.        time. The size of the data in each pbuf is kept in the ->len
  13.        variable. */
  14.     enc28j60_writebuf( q->payload, q->len ); //【2】send data from(q->payload, q->len);
  15.   }

  16.   enc28j60_start_send(); //【3】signal that packet should be sent();

  17. #if ETH_PAD_SIZE
  18.   pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
  19. #endif

  20.   LINK_STATS_INC(link.xmit);

  21.   return ERR_OK;
  22. }
【说明】
        【1】enc28j60_init_send(p->tot_len); 初始化发送缓冲区大小, pbuf结构为一个链表,第一个pbuf结构体中的tot_len字段代表整个以太网数据包的大小。
        【2】enc28j60_writebuf( q->payload, q->len ); 通过遍历链表把内容填入ENC28J60的缓冲区中。
        【3】enc28j60_start_send();启动网卡发送。

 楼主| dongnanxibei 发表于 2018-5-13 22:09 | 显示全部楼层
【3】接收
  1. static struct pbuf *
  2. low_level_input(struct netif *netif)
  3. {
  4.   struct ethernetif *ethernetif = netif->state;
  5.   struct pbuf *p, *q;
  6.   u16_t len;

  7.   len = enc28j60_packet_getlen(); // 【1】

  8. #if ETH_PAD_SIZE
  9.   len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
  10. #endif

  11.   /* We allocate a pbuf chain of pbufs from the pool. */
  12.   p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);

  13.   if (p != NULL) {

  14. #if ETH_PAD_SIZE
  15.     pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
  16. #endif

  17.     for(q = p; q != NULL; q = q->next) {
  18.       enc28j60_readbuf (q->payload, q->len ); //【2】read data into(q->payload, q->len);
  19.     }
  20.     enc28j60_finish_receive(); //【3】acknowledge that packet has been read();

  21. #if ETH_PAD_SIZE
  22.     pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
  23. #endif

  24.     LINK_STATS_INC(link.recv);
  25.   } else {
  26.     enc28j60_finish_receive(); //【4】drop packet();
  27.     LINK_STATS_INC(link.memerr);
  28.     LINK_STATS_INC(link.drop);
  29.   }

  30.   return p;
  31. }
【说明】
        【1】len = enc28j60_packet_getlen(); 获得网卡中数据包的长度。
        【2】enc28j60_readbuf (q->payload, q->len);把网卡中的内容复制到内存池中。
        【3】enc28j60_finish_receive();接收完成,移动网卡中缓冲区指针。

    【4】应用
        【1】LwIP网卡硬件初始化调用ethernetif_init即可,该函数中调用了low_level_init,并指定了网卡输出函数low_level_output。
        【2】一旦网卡有数据进入,应立即代用ethernetif_input函数。可以使用中断方法或查询方法。

 楼主| dongnanxibei 发表于 2018-5-13 22:09 | 显示全部楼层
2.lwipopt.h配置简述
    lwip中的配置选项非常的多,了解所有的配置非常不容易。本博文参考STM32官方的两个例子总结得到。
  1. #ifndef __LWIPOPTS_H__
  2. #define __LWIPOPTS_H__

  3. #define SYS_LIGHTWEIGHT_PROT 0

  4. #define NO_SYS 1

  5. #define NO_SYS_NO_TIMERS 1

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

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

  14. /* MEMP_NUM_PBUF: the number of memp struct pbufs. If the application
  15.    sends a lot of data out of ROM (or other static memory), this
  16.    should be set high. */
  17. #define MEMP_NUM_PBUF 10
  18. /* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One
  19.    per active UDP "connection". */
  20. #define MEMP_NUM_UDP_PCB 6
  21. /* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP
  22.    connections. */
  23. #define MEMP_NUM_TCP_PCB 10
  24. /* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP
  25.    connections. */
  26. #define MEMP_NUM_TCP_PCB_LISTEN 6
  27. /* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP
  28.    segments. */
  29. #define MEMP_NUM_TCP_SEG 12
  30. /* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active
  31.    timeouts. */
  32. #define MEMP_NUM_SYS_TIMEOUT 3


  33. /* ---------- Pbuf options ---------- */
  34. /* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */
  35. #define PBUF_POOL_SIZE 10

  36. /* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */
  37. #define PBUF_POOL_BUFSIZE 1500


  38. /* ---------- TCP options ---------- */
  39. #define LWIP_TCP 1
  40. #define TCP_TTL 255

  41. /* Controls if TCP should queue segments that arrive out of
  42.    order. Define to 0 if y**ice is low on memory. */
  43. #define TCP_QUEUE_OOSEQ 0

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

  46. /* TCP sender buffer space (bytes). */
  47. #define TCP_SND_BUF (2*TCP_MSS)

  48. /* TCP sender buffer space (pbufs). This must be at least = 2 *
  49.    TCP_SND_BUF/TCP_MSS for things to work. */
  50. #define TCP_SND_QUEUELEN (6 * TCP_SND_BUF)/TCP_MSS

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


  53. /* ---------- ICMP options ---------- */
  54. #define LWIP_ICMP 1

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

  60. /* ---------- UDP options ---------- */
  61. #define LWIP_UDP 1
  62. #define UDP_TTL 255

  63. /* ---------- Statistics options ---------- */
  64. #define LWIP_STATS 0
  65. #define LWIP_PROVIDE_ERRNO 1

  66. /**
  67. * LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c)
  68. */
  69. #define LWIP_NETCONN 0

  70. /**
  71. * LWIP_SOCKET==1: Enable Socket API (require to use sockets.c)
  72. */
  73. #define LWIP_SOCKET 0

  74. #endif /* __LWIPOPTS_H__ */
 楼主| dongnanxibei 发表于 2018-5-13 22:10 | 显示全部楼层
  【具体说明和修改】
    【1】未使用操作系统,所有NO_SYS定义为1,LWIP_NETCONN定义为0(表示不使用),LWIP_SOCKET定义为0(表示不使用)。
    【2】NO_SYS_NO_TIMERS定义为1,该定义为LwIP1.4.0以上版本增加,具体可参考LwIP修改文档。
    【3】LWIP_DHCP被定义为0,关闭了DHCP功能以简化代码。
    【4】相比STM32官方例子,去除了校验码相关配置全部使用软件校验。STM32官方案例中使用了代码EMAC功能的MCU,该系列MCU中包括硬件校验功能,但是ENC28J60并没有此功能,所以只能开启LwIP中的软件校验功能。
 楼主| dongnanxibei 发表于 2018-5-13 22:10 | 显示全部楼层
3.LwIP相关初始化
  1. void LwIP_Config (void)
  2. {
  3.     struct ip_addr ipaddr;
  4.     struct ip_addr netmask;
  5.     struct ip_addr gw;
  6.    
  7.     // 调用LWIP初始化函数
  8.     lwip_init();
  9.    
  10.     IP4_ADDR(&ipaddr, 192, 168, 1, 16); // 设置网络接口的ip地址
  11.     IP4_ADDR(&netmask, 255, 255, 255, 0);         // 子网掩码
  12.     IP4_ADDR(&gw, 192, 168, 1, 1);         // 网关
  13.    
  14.     // 初始化enc28j60与LWIP的接口,参数为网络接口结构体、ip地址、
  15.     // 子网掩码、网关、网卡信息指针、初始化函数、输入函数
  16.     netif_add(&enc28j60, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
  17.    
  18.     // 把enc28j60设置为默认网卡
  19.     netif_set_default(&enc28j60);
  20.    
  21.     netif_set_up(&enc28j60);
  22. }

【说明】
        【1】通过netif_add初始化网卡IP地址,子网掩码和网关地址。此处使用静态IP地址。
        【2】netif_add需要传入两个函数指针,分别是网卡初始化函数和接收内容处理函数。ethernetif_init位于ethernetif.c而ethernet_input并不位于ethernetif.c,此处也不能使用ethernetif_input,其实ethernet_input在函数ethernetif_input被调用,但是ethernet_input变了一个样子:
        netif->input(p, netif)!=ERR_OK
        【3】在带操作系统的移植中,最后一个参数使用tcpip_input。
 楼主| dongnanxibei 发表于 2018-5-13 22:11 | 显示全部楼层
4.while(1)部分
  1.     timer_typedef tcp_timer, arp_timer;
  2.    
  3.     /* 设定查询定时器 ARP定时器 */
  4.     timer_set(&tcp_timer, CLOCK_SECOND / 10); // tcp处理定时器 100ms
  5.     timer_set(&arp_timer, CLOCK_SECOND * 5); // arp处理定时器 5s
  6.     while (1) {
  7.       
  8.         if (enc28j60_packet_getcount() != 0) {
  9.             ethernetif_input(&enc28j60);
  10.         }
  11.       
  12.         // TCP 定时处理
  13.         if (timer_expired(&tcp_timer)) {
  14.             timer_set(&tcp_timer, CLOCK_SECOND / 4);
  15.             tcp_tmr();
  16.         }
  17.       
  18.         // ARP 定时处理
  19.         if (timer_expired(&arp_timer)) {
  20.             timer_set(&arp_timer, CLOCK_SECOND * 5);
  21.             etharp_tmr();
  22.         }
  23.     }

【说明】
    while(1)循环包括3个主要功能
    【1】一旦接受到数据包,立刻调用 ethernetif_input。此处使用查询法而不是中断法(中断法效果相似)
    【2】定期处理TCP链接,定时时间为100ms,可根据情况适当缩小时间间隔。
    【3】定期更新ARP缓冲,可根据情况适当扩大时间间隔。
    【4】此处的timer通过systick实现,具体实现请参考代码仓库。
 楼主| dongnanxibei 发表于 2018-5-13 22:12 | 显示全部楼层
4.基本测试
    【1】ping实验
    此时网卡的静态IP地址为192.168.1.16,通过ping指令发送16个数据包
    ping 192.168.1.16 -n 16
QQ截图20180513221156.png

 楼主| dongnanxibei 发表于 2018-5-13 22:13 | 显示全部楼层
【2】TCP Echo例子
    LwIP提供很多示例,TCP Echo示例位于contrib-1.4.1的apps文件夹中,文件夹名为tcpecho_raw)。修改TCP侦听端口为10086。
    err = tcp_bind(echo_pcb, IP_ADDR_ANY, 10086);
11111111111.png
TCP Echo例子

5.总结
    【1】移植和应用LwIP一定要耐心细致。
    【2】一旦网卡接收到数据,应调用ethernetif_input函数,调用该函数让数据进入LwIP协议栈。
    【3】 netif_add(&enc28j60, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);最后一个参数为ethernet_input,千万必要写成ethernetif_input。


767657686 发表于 2019-7-4 11:26 | 显示全部楼层
源代码在哪里呀?没看到在哪里下载
您需要登录后才可以回帖 登录 | 注册

本版积分规则

225

主题

3848

帖子

18

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