硬汉嵌入式论坛

 找回密码
 立即注册
查看: 1970|回复: 5
收起左侧

[CMSIS-Driver] CMSIS-Driver升级lwip的底层驱动,增加多线程信号量保护(2020-04-25)

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106649
QQ
发表于 2020-4-25 09:06:42 | 显示全部楼层 |阅读模式
新版lwip软件包镜像下载:
http://www.armbbs.cn/forum.php?mod=viewthread&tid=96992

就是这个ethernetif.c:

后面升级lwip教程,同步升级下这个驱动文件:

  1. /*
  2. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. *    this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. *    this list of conditions and the following disclaimer in the documentation
  12. *    and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. *    derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Adam Dunkels <adam@sics.se>
  30. *
  31. */

  32. #include "lwip/opt.h"

  33. #include "lwip/def.h"
  34. #include "lwip/mem.h"
  35. #include "lwip/pbuf.h"
  36. #include "lwip/stats.h"
  37. #include "lwip/snmp.h"
  38. #include "lwip/ethip6.h"
  39. #include "lwip/etharp.h"
  40. #include "netif/ppp/pppoe.h"

  41. #include "arch/sys_arch.h"
  42. #include "lwip/sys.h"

  43. #include "ethif_config.h"
  44. #include "ethernetif.h"
  45. #include "Driver_ETH_MAC.h"
  46. #include "Driver_ETH_PHY.h"

  47. /* Ethernet MAC & PHY Driver */
  48. extern ARM_DRIVER_ETH_MAC ARM_Driver_ETH_MAC_(ETH_DRV_NUM);
  49. extern ARM_DRIVER_ETH_PHY ARM_Driver_ETH_PHY_(ETH_DRV_NUM);

  50. /* Define those to better describe your network interface. */
  51. #define IFNAME0 'e'
  52. #define IFNAME1 'n'

  53. /**
  54. * Helper struct to hold private data used to operate your ethernet interface.
  55. * Keeping the ethernet address of the MAC in this struct is not necessary
  56. * as it is already kept in the struct netif.
  57. */
  58. struct ethernetif {
  59.   ARM_DRIVER_ETH_MAC *mac;              // Registered MAC driver
  60.   ARM_DRIVER_ETH_PHY *phy;              // Registered PHY driver
  61.   ARM_ETH_LINK_STATE link;              // Ethernet Link State
  62.   bool               event_rx_frame;    // Callback RX event generated
  63.   bool               phy_ok;            // PHY initialized successfully
  64.   bool               rx_event;          // Frame received
  65.   sys_sem_t          sem;
  66. };

  67. static struct ethernetif eth0_status;

  68. /* Forward declarations. */
  69. static void  ethernetif_input(struct netif *netif);
  70. static void  eth0_notify (uint32_t event);

  71. /**
  72. * In this function, the hardware should be initialized.
  73. * Called from ethernetif_init().
  74. *
  75. * @param netif the already initialized lwip network interface structure
  76. *        for this ethernetif
  77. */
  78. static void
  79. low_level_init(struct netif *netif)
  80. {
  81.   struct ethernetif *eth = netif->state;
  82.   ARM_ETH_MAC_CAPABILITIES cap;

  83.   /* set MAC hardware address length */
  84.   netif->hwaddr_len = ETH_HWADDR_LEN;

  85.   /* set MAC hardware address */
  86.   netif->hwaddr[0] =  MAC_ADDR0;
  87.   netif->hwaddr[1] =  MAC_ADDR1;
  88.   netif->hwaddr[2] =  MAC_ADDR2;
  89.   netif->hwaddr[3] =  MAC_ADDR3;
  90.   netif->hwaddr[4] =  MAC_ADDR4;
  91.   netif->hwaddr[5] =  MAC_ADDR5;

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

  94.   /* device capabilities */
  95.   /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  96.   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET;
  97. #if LWIP_IPV4 && LWIP_IGMP
  98.   netif->flags |= NETIF_FLAG_IGMP;
  99. #endif
  100. #if LWIP_IPV6 && LWIP_IPV6_MLD
  101.   netif->flags |= NETIF_FLAG_MLD6;
  102.   /*
  103.    * For hardware/netifs that implement MAC filtering.
  104.    * All-nodes link-local is handled by default, so we must let the hardware know
  105.    * to allow multicast packets in.
  106.    * Should set mld_mac_filter previously. */
  107.   if (netif->mld_mac_filter != NULL) {
  108.     ip6_addr_t ip6_allnodes_ll;
  109.     ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
  110.     netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
  111.   }
  112. #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */

  113.   /* Do whatever else is needed to initialize interface. */
  114.   eth->phy = &ARM_Driver_ETH_PHY_(ETH_DRV_NUM);
  115.   eth->mac = &ARM_Driver_ETH_MAC_(ETH_DRV_NUM);

  116.   eth->link   = ARM_ETH_LINK_DOWN;
  117.   eth->phy_ok = false;

  118.   sys_sem_new (e->sem, 0);
  119.   /* Get MAC capabilities */
  120.   cap = eth->mac->GetCapabilities ();
  121.   eth->event_rx_frame = (cap.event_rx_frame) ? true : false;

  122.   eth->mac->Initialize (eth0_notify);
  123.   eth->mac->PowerControl (ARM_POWER_FULL);
  124.   eth->mac->SetMacAddress ((ARM_ETH_MAC_ADDR *)netif->hwaddr);
  125.   eth->mac->Control (ARM_ETH_MAC_CONTROL_TX, 0);
  126.   eth->mac->Control (ARM_ETH_MAC_CONTROL_RX, 0);

  127.   /* Initialize Physical Media Interface */
  128.   if (eth->phy->Initialize (eth->mac->PHY_Read, eth->mac->PHY_Write) == ARM_DRIVER_OK) {
  129.     eth->phy->PowerControl (ARM_POWER_FULL);
  130.     eth->phy->SetInterface (cap.media_interface);
  131.     eth->phy->SetMode (ARM_ETH_PHY_AUTO_NEGOTIATE);
  132.     eth->phy_ok = true;
  133.   }
  134.   sys_sem_signal (e->sem);
  135. }

  136. static void eth0_notify (uint32_t event) {
  137.   /* Send notification on RX event */
  138.   if (event == ARM_ETH_MAC_EVENT_RX_FRAME) {
  139.     eth0_status.rx_event = true;
  140.   }
  141. }

  142. /**
  143. * This function should do the actual transmission of the packet. The packet is
  144. * contained in the pbuf that is passed to the function. This pbuf
  145. * might be chained.
  146. *
  147. * @param netif the lwip network interface structure for this ethernetif
  148. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  149. * @return ERR_OK if the packet could be sent
  150. *         an err_t value if the packet couldn't be sent
  151. *
  152. * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
  153. *       strange results. You might consider waiting for space in the DMA queue
  154. *       to become available since the stack doesn't retry to send a packet
  155. *       dropped because of memory failure (except for the TCP timers).
  156. */

  157. static err_t
  158. low_level_output(struct netif *netif, struct pbuf *p)
  159. {
  160.   struct ethernetif *eth = netif->state;
  161.   struct pbuf *q;

  162. #if ETH_PAD_SIZE
  163.   pbuf_remove_header(p, ETH_PAD_SIZE); /* drop the padding word */
  164. #endif

  165.   sys_sem_wait (e->sem);
  166.   for (q = p; q != NULL; q = q->next) {
  167.     /* Send the data from the pbuf to the interface, one pbuf at a
  168.        time. The size of the data in each pbuf is kept in the ->len
  169.        variable. */
  170.     u32_t flags = (q->next) ? ARM_ETH_MAC_TX_FRAME_FRAGMENT : 0;
  171.     eth->mac->SendFrame (q->payload, q->len, flags);
  172.   }
  173.   sys_sem_signal (e->sem);

  174.   MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
  175.   if (((u8_t *)p->payload)[0] & 1) {
  176.     /* broadcast or multicast packet*/
  177.     MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
  178.   } else {
  179.     /* unicast packet */
  180.     MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
  181.   }
  182.   /* increase ifoutdiscards or ifouterrors on error */

  183. #if ETH_PAD_SIZE
  184.   pbuf_add_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
  185. #endif

  186.   LINK_STATS_INC(link.xmit);

  187.   return ERR_OK;
  188. }

  189. /**
  190. * Should allocate a pbuf and transfer the bytes of the incoming
  191. * packet from the interface into the pbuf.
  192. *
  193. * @param netif the lwip network interface structure for this ethernetif
  194. * @return a pbuf filled with the received packet (including MAC header)
  195. *         NULL on memory error
  196. */
  197. static struct pbuf *
  198. low_level_input(struct netif *netif)
  199. {
  200.   struct ethernetif *eth = netif->state;
  201.   struct pbuf *p;
  202.   u16_t len;

  203.   /* Obtain the size of the packet and put it into the "len"
  204.      variable. */
  205.   len = eth->mac->GetRxFrameSize ();
  206.   if (len == 0) {
  207.     /* No packet available */
  208.     return NULL;
  209.   }
  210.   if (len > 1514) {
  211.     /* Drop oversized packet */
  212.     eth->mac->ReadFrame (NULL, 0);
  213.     return NULL;
  214.   }

  215. #if ETH_PAD_SIZE
  216.   len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
  217. #endif

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

  220.   if (p != NULL) {

  221. #if ETH_PAD_SIZE
  222.     pbuf_remove_header(p, ETH_PAD_SIZE); /* drop the padding word */
  223. #endif

  224.     /* pbuf payload should be allocated in one piece of contiguous memory. */
  225.     if (p->len < len) {
  226.       len = p->len;
  227.     }
  228.     /* we are assuming the data will fit into this payload.
  229.      * this is documented in pbuf.h
  230.      */
  231.     eth->mac->ReadFrame (p->payload, len);

  232.     MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
  233.     if (((u8_t *)p->payload)[0] & 1) {
  234.       /* broadcast or multicast packet*/
  235.       MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
  236.     } else {
  237.       /* unicast packet*/
  238.       MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
  239.     }
  240. #if ETH_PAD_SIZE
  241.     pbuf_add_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
  242. #endif

  243.     LINK_STATS_INC(link.recv);
  244.   } else {
  245.     /* Drop packet */
  246.     eth->mac->ReadFrame (NULL, 0);
  247.     LINK_STATS_INC(link.memerr);
  248.     LINK_STATS_INC(link.drop);
  249.     MIB2_STATS_NETIF_INC(netif, ifindiscards);
  250.   }

  251.   return p;
  252. }

  253. /**
  254. * This function should be called when a packet is ready to be read
  255. * from the interface. It uses the function low_level_input() that
  256. * should handle the actual reception of bytes from the network
  257. * interface. Then the type of the received packet is determined and
  258. * the appropriate input function is called.
  259. *
  260. * @param netif the lwip network interface structure for this ethernetif
  261. */
  262. static void
  263. ethernetif_input(struct netif *netif)
  264. {
  265.   struct ethernetif *eth = netif->state;
  266.   struct pbuf *p;

  267.   /* move received packet into a new pbuf */
  268.   sys_sem_wait (e->sem);
  269.   p = low_level_input(netif);
  270.   sys_sem_signal (e->sem);
  271.   /* if no packet could be read, silently ignore this */
  272.   if (p != NULL) {
  273.     /* pass all packets to ethernet_input, which decides what packets it supports */
  274.     if (netif->input(p, netif) != ERR_OK) {
  275.       LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
  276.       pbuf_free(p);
  277.       p = NULL;
  278.     }
  279.     return;
  280.   }
  281.   eth->rx_event = false;
  282. }

  283. /**
  284. * Should be called at the beginning of the program to set up the
  285. * network interface. It calls the function low_level_init() to do the
  286. * actual setup of the hardware.
  287. *
  288. * This function should be passed as a parameter to netif_add().
  289. *
  290. * @param netif the lwip network interface structure for this ethernetif
  291. * @return ERR_OK if the loopif is initialized
  292. *         ERR_MEM if private data couldn't be allocated
  293. *         any other err_t on error
  294. */
  295. err_t
  296. ethernetif_init(struct netif *netif)
  297. {
  298.   LWIP_ASSERT("netif != NULL", (netif != NULL));

  299. #if LWIP_NETIF_HOSTNAME
  300.   /* Initialize interface hostname */
  301.   netif->hostname = "lwip";
  302. #endif /* LWIP_NETIF_HOSTNAME */

  303.   /*
  304.    * Initialize the snmp variables and counters inside the struct netif.
  305.    * The last argument should be replaced with your link speed, in units
  306.    * of bits per second.
  307.    */
  308.   MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, 100000000);

  309.   netif->state = e0_status;
  310.   netif->name[0] = IFNAME0;
  311.   netif->name[1] = IFNAME1;
  312.   /* We directly use etharp_output() here to save a function call.
  313.    * You can instead declare your own function an call etharp_output()
  314.    * from it if you have to do some checks before sending (e.g. if link
  315.    * is available...) */
  316. #if LWIP_IPV4
  317.   netif->output = etharp_output;
  318. #endif /* LWIP_IPV4 */
  319. #if LWIP_IPV6
  320.   netif->output_ip6 = ethip6_output;
  321. #endif /* LWIP_IPV6 */
  322.   netif->linkoutput = low_level_output;

  323.   /* initialize the hardware */
  324.   low_level_init(netif);

  325.   return ERR_OK;
  326. }

  327. void ethernetif_check_link (struct netif *netif) {
  328.   struct ethernetif *eth = netif->state;
  329.   ARM_ETH_LINK_STATE link;

  330.   if (!eth->phy_ok) {
  331.     return;
  332.   }
  333.   /* Check link status */
  334.   sys_sem_wait (e->sem);
  335.   link = eth->phy->GetLinkState ();
  336.   if (link == eth->link) {
  337.     /* No change */
  338.     sys_sem_signal (e->sem);
  339.     return;
  340.   }
  341.   eth->link = link;
  342.   if (eth->link == ARM_ETH_LINK_UP) {
  343.     /* Start EMAC DMA */
  344.     ARM_ETH_LINK_INFO info = eth->phy->GetLinkInfo ();
  345.     eth->mac->Control (ARM_ETH_MAC_CONFIGURE,
  346.                        info.speed  << ARM_ETH_MAC_SPEED_Pos  |
  347.                        info.duplex << ARM_ETH_MAC_DUPLEX_Pos |
  348.                        ARM_ETH_MAC_ADDRESS_BROADCAST         );
  349.     eth->mac->Control(ARM_ETH_MAC_CONTROL_TX,1);
  350.     eth->mac->Control(ARM_ETH_MAC_CONTROL_RX,1);
  351.     sys_sem_signal (e->sem);
  352.     netif_set_link_up (netif);
  353.   }
  354.   else {
  355.     /* Stop EMAC DMA */
  356.     eth->mac->Control(ARM_ETH_MAC_CONTROL_TX,0);
  357.     eth->mac->Control(ARM_ETH_MAC_CONTROL_RX,0);
  358.     sys_sem_signal (e->sem);
  359.     netif_set_link_down (netif);
  360.   }
  361. }

  362. void ethernetif_poll (struct netif *netif) {
  363.   struct ethernetif *eth = netif->state;

  364.   if (!eth->phy_ok || eth->link == ARM_ETH_LINK_DOWN) {
  365.     return;
  366.   }
  367.   if ((eth->rx_event) || (!eth->event_rx_frame)) {
  368.     /* process received ethernet packet */
  369.     ethernetif_input (netif);
  370.   }
  371. }
复制代码


回复

使用道具 举报

0

主题

23

回帖

23

积分

新手上路

积分
23
发表于 2020-4-26 12:58:35 | 显示全部楼层
用在哪里,有啥区别呢?不是很明白
回复

使用道具 举报

16

主题

148

回帖

196

积分

初级会员

积分
196
发表于 2020-4-26 14:44:24 | 显示全部楼层
硬汉兄用信号量来保护不会造成优先级反转吗?
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106649
QQ
 楼主| 发表于 2020-4-26 17:07:12 | 显示全部楼层
dyhfaily 发表于 2020-4-26 14:44
硬汉兄用信号量来保护不会造成优先级反转吗?

翻转了也没有关系的。

相比互斥,使用信号好量效率还高点。

想避免的话,可以在lwip这几个任务优先级之间不分配其它优先级可,简单省事些。
回复

使用道具 举报

10

主题

19

回帖

49

积分

初级会员

积分
49
发表于 2020-4-28 20:23:38 | 显示全部楼层
void ethernetif_poll (struct netif *netif) {
  struct ethernetif *eth = netif->state;

  if (!eth->phy_ok || eth->link == ARM_ETH_LINK_DOWN) {
    return;
  }
  if ((eth->rx_event) || (!eth->event_rx_frame)) {
    /* process received ethernet packet */
    ethernetif_input (netif);
  }
}
如果没有eth中断,这块岂不是要无限循环了,CPU利用率立马就飙上去了,您觉得呢?我感觉需要定义两个信号量,一个触发中断,一个用来保护输入输出。
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106649
QQ
 楼主| 发表于 2020-4-28 20:39:59 | 显示全部楼层
nextstation 发表于 2020-4-28 20:23
void ethernetif_poll (struct netif *netif) {
  struct ethernetif *eth = netif->state;

那必须的,去年发布的时候就支持了。

LwIP网络教程开始更新,使用MDK的RTE环境开发,配套RTX5和FreeRTOS两个版本,更新至第7章(2019-12-12)
http://www.armbbs.cn/forum.php?m ... id=95874&fromuid=58
(出处: 硬汉嵌入式论坛)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-27 18:13 , Processed in 0.273277 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表