STM32F107移植LWIP

STM32F107上移植LWIP2.0.3 

      因为最近需要在STM32F107上实现TCP/IP协议栈,所以网上查了一下,准备使用LWIP,虽然大多数用的是1.4.1版本但是官方说2系大版本修复了1.4.1的大量bug所以这里用的版本是2.0.3,其实移植上没有什么太大区别。STM32F107集成了以太网媒体接入控制器MAC(主要负责控制和连接物理层的物理介质),所以按四层分层模型组成来看底层物理层组成还缺少一个物理接口收发器(PHY)。网上常见的应用于STM32上的PHY芯片有DP83848和LAN8720/LAN8700,开源电子网有关于LAN8720在LWIP上的移植应用,本质上两个PHY的差别不是很大,只是在内部寄存器上有细小差别(因为PHY芯片的寄存器里面的大部分寄存器是IEEE定义的)。说到这里就要说一下MAC和PHY的关系,PHY把自己的目前的状态反映到寄存器里面,MAC通过SMI总线(物理接口是MII或RMII等)不断的读取PHY的状态寄存器以得知目前PHY的状态,当然也可以通过SMI设置PHY的寄存器达到控制的目的,例如流控的打开关闭,自协商模式还是强制模式等.不论是物理连接的MII总线和SMI总线还是PHY的状态寄存器和控制寄存器都是有IEEE的规范的,因此不同公司的MAC和PHY一样可以协调工作。

  目前常见的MAC连接PHY的媒体独立接口主要有MII,RMII, SMII,GMII等 ,在STM32F107上提供了两种(RMII和MII)MAC连接PHY的物理接口,在应用不同的PHY芯片时,可以根据PHY芯片支持的类型选择配置成不同的接口工作模式,在这里我使用的是RMII即简化的MII接口他需要的I/O是MII的一半(实际不止)。最后整体的物理层组成就是如下图:

 

                 

1,什么是MAC?

      MAC即Media Access Control,即媒体访问控制子层协议。该协议位于OSI七层协议中数据链路层的下半部分,主要负责控制与连接物理层的物理介质.在发送数据的时候,MAC协议可以事先判断是否可以发送数据,如果可以发送将给数据加上一些控制信息,最终将数据以及控制信息以规定的格式发送到物理层;在接收数据的时候,MAC协议首先判断输入的信息并是否发生传输错误,如果没有错误,则去掉控制信息发送至网络层.该层协议是以太网MAC由IEEE-802.3以太网标准定义.最新的MAC同时支持10Mbps和100Mbps两种速率.

2,PHY什么?

       PHY是物理接口收发器,它实现物理层,PHY在发送数据的时候,收到MAC过来的数据(对PHY来说,没有帧的概念,对它来说,都是数据而不管什么地址,数据还是CRC.对于100BaseTX因为使用4B/5B编码,每4bit就增加1bit的检错码),然后把并行数据转化为串行流数据,再按照物理层的编码规则把数据编码,再变为模拟信号把数据送出去.收数据时的流程反之.PHY还有个重要的功能就是实现CSMA/CD的部分功能.它可以检测到网络上是否有数据在传送,如果有数据在传送中就等待,一旦检测到网络空闲,再等待一个随机时间后将送数据出去.如果两个碰巧同时送出了数据,那样必将造成冲突,这时候,冲突检测机构可以检测到冲突,然后各等待一个随机的时间重新发送数据.这个随机时间很有讲究的,并不是一个常数,在不同的时刻计算出来的随机时间都是不同的,而且有多重算法来应付出现概率很低的同两台主机之间的第二次冲突.

 3,隔离变压器

  PHY和MAC是网卡的主要组成部分,网卡一般用RJ-45插口,10M网卡的RJ-45插口也只用了1,2,3,6四根针,而100M或1000M网卡的则是八根针都是全的。除此以外,还需要其它元件,因为虽然PHY提供绝大多数模拟支持,但在一个典型实现中,仍需外接6,7只分立元件及一个局域网绝缘模块。绝缘模块一般采用一个1:1的变压器.这些部件的主要功能是为了保护PHY免遭由于电气失误而引起的损坏.一颗CMOS制程的芯片工作的时候产生的信号电平总是大于0V的(这取决于芯片的制程和设计需求),但是这样的信号送到100米甚至更长的地方会有很大的直流分量的损失.而且如果外部网线直接和芯片相连的话,电磁感应(打雷)和静电,很容易造成芯片的损坏.再就是设备接地方法不同,电网环境不同会导致双方的0V电平不一致,这样信号从A传到B,由于A设备的0V电平和B点的0V电平不一样,这样会导致很大的电流从电势高的设备流向电势低的设备。为了解决以上问题隔离变压器这个器件就应运而生.它把PHY送出来的差分信号用差模耦合的线圈耦合滤波以增强信号,并且通过电磁场的转换耦合到连接网线的另外一端.这样不但使网线和PHY之间没有物理上的连接而换传递了信号,隔断了信号中的直流分量,还可以在不同0V电平的设备中传送数据.隔离变压器本身就是设计为耐2KV~3KV的电压的。

(以上内容参考这个链接的博主,他进行了非常极其全面的介绍关于物理层,看完大彻大悟呀推荐 http://www.cnblogs.com/jason-lu/articles/3195473.html)

  继续回到移植物理层上来说,在这里以太网控制器的驱动代码代码参考ST官方的demo程序,但是如果物理层的芯片不同,驱动还是需要针对不同物理层PHY芯片进行细微改动(如果你是使用的DP83848芯片就不需要改),STM32支持RMll和Mll两种MAC连接PHY芯片的总线接口,本次实验使用的是DP83848作为PHY芯片采用RMII接口连接MAC层物理层驱动参考ST的DEMO程序。其实无论是那种芯片都可以使用这个demo程序进行移植,不同的PHY芯片可以通过修改一些宏来完成移植。

stm32_eth.h文件中详细展示了针对不同PHY芯片所需要做的改动,如果你和文件中的两种都不同可以通过芯片手册查看确定。

 1 /** @defgroup PHY_status_register 
 2   * @{
 3   */ 
 4 /* The PHY status register value change from a PHY to another so the user have 
 5    to update this value depending on the used external PHY */
 6 /** 
 7   * @brief  For LAN8700  
 8   */ 
 9 //#define PHY_SR                           31         /*!< Tranceiver Status Register */
10 /** 
11   * @brief  For DP83848  
12   */ 
13 #define PHY_SR                           16     /*!< Tranceiver Status Register */
14 
15 /* The Speed and Duplex mask values change from a PHY to another so the user have to update
16    this value depending on the used external PHY */
17 /** 
18   * @brief  For LAN8700  
19   */ 
20 //#define PHY_Speed_Status            ((u16)0x0004)       /*!< Configured information of Speed: 10Mbps */
21 //#define PHY_Duplex_Status           ((u16)0x0010)       /*!< Configured information of Duplex: Full-duplex */
22 
23 /** 
24   * @brief  For DP83848  
25   */ 
26 #define PHY_Speed_Status            ((u16)0x0002)    /*!< Configured information of Speed: 10Mbps */
27 #define PHY_Duplex_Status           ((u16)0x0004)    /*!< Configured information of Duplex: Full-duplex */
28 #define IS_ETH_PHY_ADDRESS(ADDRESS) ((ADDRESS) <= 0x20)
29 #define IS_ETH_PHY_REG(REG) (((REG) == PHY_BCR) || \
30                              ((REG) == PHY_BSR) || \
31                              ((REG) == PHY_SR))

#define LAN8720_PHY_ADDRESS 0x00
#define DP83848_PHY_ADDRESS 0x01

其中最后两个宏是我在其他地方借鉴过来的……。其实根据ST的demo程序移植物理层并不复杂,只需要进行简单的修改就可以了,难点在于理解三个层(MAC,PHY,RJ45)的工作方式。最后最复杂的就是关于ST 的demo中关于DAM的哪一些操作,移植是基本不需要改动,但是要理解其工作方式,本人目前还是一头雾水的这一部分可以看正点原子的F4的移植文档。修改到这里物理层就应该可以工作了。

  接下来就是将物理层的数据交给IWIP协议栈内核了,在这里LWIP在ethernetif.c中给我们提供了一个软件框架没有具体的实现,需要我们自己根据实际用用加入。

  1 /**
  2  * @file
  3  * Ethernet Interface Skeleton
  4  *
  5  */
  6 
  7 /*
  8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  9  * All rights reserved.
 10  *
 11  * Redistribution and use in source and binary forms, with or without modification,
 12  * are permitted provided that the following conditions are met:
 13  *
 14  * 1. Redistributions of source code must retain the above copyright notice,
 15  *    this list of conditions and the following disclaimer.
 16  * 2. Redistributions in binary form must reproduce the above copyright notice,
 17  *    this list of conditions and the following disclaimer in the documentation
 18  *    and/or other materials provided with the distribution.
 19  * 3. The name of the author may not be used to endorse or promote products
 20  *    derived from this software without specific prior written permission.
 21  *
 22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 31  * OF SUCH DAMAGE.
 32  *
 33  * This file is part of the lwIP TCP/IP stack.
 34  *
 35  * Author: Adam Dunkels 
 36  *
 37  */
 38 
 39 /*
 40  * This file is a skeleton for developing Ethernet network interface
 41  * drivers for lwIP. Add code to the low_level functions and do a
 42  * search-and-replace for the word "ethernetif" to replace it with
 43  * something that better describes your network interface.
 44  */
 45 
 46 #include "lwip/opt.h"
 47 
 48 #if 0 /* don't build, this is only a skeleton, see previous comment */
 49 
 50 #include "lwip/def.h"
 51 #include "lwip/mem.h"
 52 #include "lwip/pbuf.h"
 53 #include "lwip/stats.h"
 54 #include "lwip/snmp.h"
 55 #include "lwip/ethip6.h"
 56 #include "lwip/etharp.h"
 57 #include "netif/ppp/pppoe.h"
 58 
 59 /* Define those to better describe your network interface. */
 60 #define IFNAME0 'e'
 61 #define IFNAME1 'n'
 62 
 63 /**
 64  * Helper struct to hold private data used to operate your ethernet interface.
 65  * Keeping the ethernet address of the MAC in this struct is not necessary
 66  * as it is already kept in the struct netif.
 67  * But this is only an example, anyway...
 68  */
 69 struct ethernetif {
 70   struct eth_addr *ethaddr;
 71   /* Add whatever per-interface state that is needed here. */
 72 };
 73 
 74 /* Forward declarations. */
 75 static void  ethernetif_input(struct netif *netif);
 76 
 77 /**
 78  * In this function, the hardware should be initialized.
 79  * Called from ethernetif_init().
 80  *
 81  * @param netif the already initialized lwip network interface structure
 82  *        for this ethernetif
 83  */
 84 static void
 85 low_level_init(struct netif *netif)
 86 {
 87   struct ethernetif *ethernetif = netif->state;
 88 
 89   /* set MAC hardware address length */
 90   netif->hwaddr_len = ETHARP_HWADDR_LEN;
 91 
 92   /* set MAC hardware address */
 93   netif->hwaddr[0] = ;
 94   ...
 95   netif->hwaddr[5] = ;
 96 
 97   /* maximum transfer unit */
 98   netif->mtu = 1500;
 99 
100   /* device capabilities */
101   /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
102   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
103 
104 #if LWIP_IPV6 && LWIP_IPV6_MLD
105   /*
106    * For hardware/netifs that implement MAC filtering.
107    * All-nodes link-local is handled by default, so we must let the hardware know
108    * to allow multicast packets in.
109    * Should set mld_mac_filter previously. */
110   if (netif->mld_mac_filter != NULL) {
111     ip6_addr_t ip6_allnodes_ll;
112     ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
113     netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
114   }
115 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
116 
117   /* Do whatever else is needed to initialize interface. */
118 }
119 
120 /**
121  * This function should do the actual transmission of the packet. The packet is
122  * contained in the pbuf that is passed to the function. This pbuf
123  * might be chained.
124  *
125  * @param netif the lwip network interface structure for this ethernetif
126  * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
127  * @return ERR_OK if the packet could be sent
128  *         an err_t value if the packet couldn't be sent
129  *
130  * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
131  *       strange results. You might consider waiting for space in the DMA queue
132  *       to become available since the stack doesn't retry to send a packet
133  *       dropped because of memory failure (except for the TCP timers).
134  */
135 
136 static err_t
137 low_level_output(struct netif *netif, struct pbuf *p)
138 {
139   struct ethernetif *ethernetif = netif->state;
140   struct pbuf *q;
141 
142   initiate transfer();
143 
144 #if ETH_PAD_SIZE
145   pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
146 #endif
147 
148   for (q = p; q != NULL; q = q->next) {
149     /* Send the data from the pbuf to the interface, one pbuf at a
150        time. The size of the data in each pbuf is kept in the ->len
151        variable. */
152     send data from(q->payload, q->len);
153   }
154 
155   signal that packet should be sent();
156 
157   MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
158   if (((u8_t*)p->payload)[0] & 1) {
159     /* broadcast or multicast packet*/
160     MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
161   } else {
162     /* unicast packet */
163     MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
164   }
165   /* increase ifoutdiscards or ifouterrors on error */
166 
167 #if ETH_PAD_SIZE
168   pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
169 #endif
170 
171   LINK_STATS_INC(link.xmit);
172 
173   return ERR_OK;
174 }
175 
176 /**
177  * Should allocate a pbuf and transfer the bytes of the incoming
178  * packet from the interface into the pbuf.
179  *
180  * @param netif the lwip network interface structure for this ethernetif
181  * @return a pbuf filled with the received packet (including MAC header)
182  *         NULL on memory error
183  */
184 static struct pbuf *
185 low_level_input(struct netif *netif)
186 {
187   struct ethernetif *ethernetif = netif->state;
188   struct pbuf *p, *q;
189   u16_t len;
190 
191   /* Obtain the size of the packet and put it into the "len"
192      variable. */
193   len = ;
194 
195 #if ETH_PAD_SIZE
196   len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
197 #endif
198 
199   /* We allocate a pbuf chain of pbufs from the pool. */
200   p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
201 
202   if (p != NULL) {
203 
204 #if ETH_PAD_SIZE
205     pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
206 #endif
207 
208     /* We iterate over the pbuf chain until we have read the entire
209      * packet into the pbuf. */
210     for (q = p; q != NULL; q = q->next) {
211       /* Read enough bytes to fill this pbuf in the chain. The
212        * available data in the pbuf is given by the q->len
213        * variable.
214        * This does not necessarily have to be a memcpy, you can also preallocate
215        * pbufs for a DMA-enabled MAC and after receiving truncate it to the
216        * actually received size. In this case, ensure the tot_len member of the
217        * pbuf is the sum of the chained pbuf len members.
218        */
219       read data into(q->payload, q->len);
220     }
221     acknowledge that packet has been read();
222 
223     MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
224     if (((u8_t*)p->payload)[0] & 1) {
225       /* broadcast or multicast packet*/
226       MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
227     } else {
228       /* unicast packet*/
229       MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
230     }
231 #if ETH_PAD_SIZE
232     pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
233 #endif
234 
235     LINK_STATS_INC(link.recv);
236   } else {
237     drop packet();
238     LINK_STATS_INC(link.memerr);
239     LINK_STATS_INC(link.drop);
240     MIB2_STATS_NETIF_INC(netif, ifindiscards);
241   }
242 
243   return p;
244 }
245 
246 /**
247  * This function should be called when a packet is ready to be read
248  * from the interface. It uses the function low_level_input() that
249  * should handle the actual reception of bytes from the network
250  * interface. Then the type of the received packet is determined and
251  * the appropriate input function is called.
252  *
253  * @param netif the lwip network interface structure for this ethernetif
254  */
255 static void
256 ethernetif_input(struct netif *netif)
257 {
258   struct ethernetif *ethernetif;
259   struct eth_hdr *ethhdr;
260   struct pbuf *p;
261 
262   ethernetif = netif->state;
263 
264   /* move received packet into a new pbuf */
265   p = low_level_input(netif);
266   /* if no packet could be read, silently ignore this */
267   if (p != NULL) {
268     /* pass all packets to ethernet_input, which decides what packets it supports */
269     if (netif->input(p, netif) != ERR_OK) {
270       LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
271       pbuf_free(p);
272       p = NULL;
273     }
274   }
275 }
276 
277 /**
278  * Should be called at the beginning of the program to set up the
279  * network interface. It calls the function low_level_init() to do the
280  * actual setup of the hardware.
281  *
282  * This function should be passed as a parameter to netif_add().
283  *
284  * @param netif the lwip network interface structure for this ethernetif
285  * @return ERR_OK if the loopif is initialized
286  *         ERR_MEM if private data couldn't be allocated
287  *         any other err_t on error
288  */
289 err_t
290 ethernetif_init(struct netif *netif)
291 {
292   struct ethernetif *ethernetif;
293 
294   LWIP_ASSERT("netif != NULL", (netif != NULL));
295 
296   ethernetif = mem_malloc(sizeof(struct ethernetif));
297   if (ethernetif == NULL) {
298     LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
299     return ERR_MEM;
300   }
301 
302 #if LWIP_NETIF_HOSTNAME
303   /* Initialize interface hostname */
304   netif->hostname = "lwip";
305 #endif /* LWIP_NETIF_HOSTNAME */
306 
307   /*
308    * Initialize the snmp variables and counters inside the struct netif.
309    * The last argument should be replaced with your link speed, in units
310    * of bits per second.
311    */
312   MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
313 
314   netif->state = ethernetif;
315   netif->name[0] = IFNAME0;
316   netif->name[1] = IFNAME1;
317   /* We directly use etharp_output() here to save a function call.
318    * You can instead declare your own function an call etharp_output()
319    * from it if you have to do some checks before sending (e.g. if link
320    * is available...) */
321   netif->output = etharp_output;
322 #if LWIP_IPV6
323   netif->output_ip6 = ethip6_output;
324 #endif /* LWIP_IPV6 */
325   netif->linkoutput = low_level_output;
326 
327   ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
328 
329   /* initialize the hardware */
330   low_level_init(netif);
331 
332   return ERR_OK;
333 }
334 
335 #endif /* 0 */

IWIP内核和底层物理层数据交互只需要如下几个函数

low_level_init

负责底层MAC地址的配置,MTU和网卡支持的功能和网卡的注册和使能相关,这个函数有LWIP内核初始化由ethernetif_init调用。

low_level_input

接受以太网数据帧,最后由ethernetif_input函数调用输入IWIP内核处理。

low_level_output

内核由需要发送的数据将最终调用这个函数交给物理层发送。以上三个函数的具体实现ST的demo程序已经完成了,直接拿来就可以是用了。

最终代码:

static void
low_level_init(struct netif *netif)
{
  /* set MAC hardware address length */
  netif->hwaddr_len = ETHARP_HWADDR_LEN;

  /* set MAC hardware address */
  netif->hwaddr[0] =  MACaddr[0];
  netif->hwaddr[1] =  MACaddr[1];
  netif->hwaddr[2] =  MACaddr[2];
  netif->hwaddr[3] =  MACaddr[3];
  netif->hwaddr[4] =  MACaddr[4];
  netif->hwaddr[5] =  MACaddr[5];

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

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

  /* Initialize Tx Descriptors list: Chain Mode */
  ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
  /* Initialize Rx Descriptors list: Chain Mode  */
  ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);

  /* Enable Ethernet Rx interrrupt */
  { int i;
    for(i=0; i)
    {
      ETH_DMARxDescReceiveITConfig(&DMARxDscrTab[i], ENABLE);
    }
  }

#ifdef CHECKSUM_BY_HARDWARE
  /* Enable the checksum insertion for the Tx frames */
  { int i;
    for(i=0; i)
    {
      ETH_DMATxDescChecksumInsertionConfig(&DMATxDscrTab[i], ETH_DMATxDesc_ChecksumTCPUDPICMPFull);
    }
  }
#endif

  /* Enable MAC and DMA transmission and reception */
  ETH_Start();

}

static err_t
low_level_output(struct netif *netif, struct pbuf *p)
{
  struct pbuf *q;
  int l = 0;
  u8 *buffer =  (u8 *)ETH_GetCurrentTxBuffer();
  
  for(q = p; q != NULL; q = q->next) 
  {
    memcpy((u8_t*)&buffer[l], q->payload, q->len);
    l = l + q->len;
  }

  ETH_TxPkt_ChainMode(l);

  return ERR_OK;
}

static struct pbuf *
low_level_input(struct netif *netif)
{
  struct pbuf *p, *q;
  u16_t len;
  int l =0;
  FrameTypeDef frame;
  u8 *buffer;
  
  p = NULL;
  frame = ETH_RxPkt_ChainMode();  
  /* Obtain the size of the packet and put it into the "len"
     variable. */
  len = frame.length;
  
  buffer = (u8 *)frame.buffer;

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

  if (p != NULL)
  {
    for (q = p; q != NULL; q = q->next)
    {
      memcpy((u8_t*)q->payload, (u8_t*)&buffer[l], q->len);
      l = l + q->len;
    }    
  }


  /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  frame.descriptor->Status = ETH_DMARxDesc_OWN; 
 
  /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET)  
  {
    /* Clear RBUS ETHERNET DMA flag */
    ETH->DMASR = ETH_DMASR_RBUS;
    /* Resume DMA reception */
    ETH->DMARPDR = 0;
  }


  return p;
}

这样底层移植算基本完成了,如果配置了以太网中断则需要在中断中放入如下代码

 

/* Handles all the received frames */
  while(ETH_GetRxPktSize() != 0) 
  {        
    LwIP_Pkt_Handle();//TODO FOR COMPILE MXC
  }

  /* Clear the Eth DMA Rx IT pending bits */
  ETH_DMAClearITPendingBit(ETH_DMA_IT_R);
  ETH_DMAClearITPendingBit(ETH_DMA_IT_NIS);

 

这样就可以将底层数据递交到网络层了,其中LWIP_Pkt_Handle()只是针对ethernetif_input函数的一个简单封装,如果为配置为查询模式,只需要将LWIP_Pkt_Handle()放在主循环中查询调用就可以了。完事了接下来。。。此时如果没有问题对MAC地址,IP地址和网关等一系列的内容进行配置后就应该能ping通了。移植好的基础工程模板测试过可以运行Tcpecho demo程序(Raw和Netcomm API 两种接口都可以)连接:https://github.com/Dazzingdusk/IWIP-for-STM32F107

 

转载于:https://www.cnblogs.com/w-smile/p/9955121.html

你可能感兴趣的:(STM32F107移植LWIP)