ethernetif.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. #include "lwip/opt.h"
  2. #include "lwip/def.h"
  3. #include "lwip/mem.h"
  4. #include "lwip/pbuf.h"
  5. #include "lwip/sys.h"
  6. #include <lwip/stats.h>
  7. #include <lwip/snmp.h>
  8. #include "netif/etharp.h"
  9. #include "netif/ppp/pppoe.h"
  10. #include "err.h"
  11. #include "ethernetif.h"
  12. //#include "settings_api.h"
  13. #include "at32f403a_407_emac.h"
  14. #include <string.h>
  15. SemaphoreHandle_t PHY_RX_xSemaphore;
  16. SemaphoreHandle_t PHY_TX_xSemaphore;
  17. #define PHY_PRIO (configMAX_PRIORITIES - 1)
  18. #define PHY_STK_SIZE 1024
  19. TaskHandle_t PHY_Handler;
  20. void NETWORK_Task(void *pvParameters);
  21. extern struct netif xnetif;
  22. struct netif *lwip_netif= &xnetif;
  23. /* TCP and ARP timeouts */
  24. volatile int tcp_end_time, arp_end_time;
  25. /* Define those to better describe your network interface. */
  26. #define IFNAME0 'a'
  27. #define IFNAME1 't'
  28. #define EMAC_DMARxDesc_FrameLengthShift 16
  29. /**
  30. * Helper struct to hold private data used to operate your ethernet interface.
  31. * Keeping the ethernet address of the MAC in this struct is not necessary
  32. * as it is already kept in the struct netif.
  33. * But this is only an example, anyway...
  34. */
  35. struct ethernetif
  36. {
  37. struct eth_addr *ethaddr;
  38. /* Add whatever per-interface state that is needed here. */
  39. int unused;
  40. };
  41. /* Forward declarations. */
  42. err_t ethernetif_input(struct netif *netif);
  43. #define EMAC_RXBUFNB 4
  44. #define EMAC_TXBUFNB 2
  45. uint8_t MACaddr[6];
  46. // Ethernet Rx & Tx DMA Descriptors
  47. #pragma data_alignment=4
  48. emac_dma_desc_type DMARxDscrTab[EMAC_RXBUFNB];
  49. #pragma data_alignment=4
  50. emac_dma_desc_type DMATxDscrTab[EMAC_TXBUFNB];
  51. #pragma data_alignment=4
  52. uint8_t Rx_Buff[EMAC_RXBUFNB][EMAC_MAX_PACKET_LENGTH];
  53. #pragma data_alignment=4
  54. uint8_t Tx_Buff[EMAC_TXBUFNB][EMAC_MAX_PACKET_LENGTH];
  55. extern emac_dma_desc_type *dma_tx_desc_to_set;
  56. extern emac_dma_desc_type *dma_rx_desc_to_get;
  57. typedef struct{
  58. u32 length;
  59. u32 buffer;
  60. emac_dma_desc_type *descriptor;
  61. }FrameTypeDef;
  62. FrameTypeDef emac_rxpkt_chainmode(void);
  63. u32 emac_getcurrenttxbuffer(void);
  64. error_status emac_txpkt_chainmode(u16 FrameLength);
  65. /**
  66. * In this function, the hardware should be initialized.
  67. * Called from ethernetif_init().
  68. *
  69. * @param netif the already initialized lwip network interface structure
  70. * for this ethernetif
  71. */
  72. static void
  73. low_level_init(struct netif *netif)
  74. {
  75. #if 0
  76. uint8_t mac[6];
  77. static bool task_eth_flag = false;
  78. /* set MAC hardware address length */
  79. netif->hwaddr_len = ETHARP_HWADDR_LEN;
  80. SETTINGS_GetMac(mac);
  81. /* set MAC hardware address */
  82. emac_local_address_set(mac);
  83. netif->hwaddr[0] = mac[0];
  84. netif->hwaddr[1] = mac[1];
  85. netif->hwaddr[2] = mac[2];
  86. netif->hwaddr[3] = mac[3];
  87. netif->hwaddr[4] = mac[4];
  88. netif->hwaddr[5] = mac[5];
  89. /* maximum transfer unit */
  90. netif->mtu = 1500;
  91. /* device capabilities */
  92. /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  93. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
  94. /* Initialize Tx Descriptors list: Chain Mode */
  95. emac_dma_descriptor_list_address_set(EMAC_DMA_TRANSMIT, DMATxDscrTab, &Tx_Buff[0][0], EMAC_TXBUFNB);
  96. /* Initialize Rx Descriptors list: Chain Mode */
  97. emac_dma_descriptor_list_address_set(EMAC_DMA_RECEIVE, DMARxDscrTab, &Rx_Buff[0][0], EMAC_RXBUFNB);
  98. if(PHY_RX_xSemaphore == NULL)
  99. {
  100. PHY_RX_xSemaphore = xSemaphoreCreateCounting(20,0);
  101. }
  102. if(PHY_TX_xSemaphore == NULL)
  103. {
  104. PHY_TX_xSemaphore = xSemaphoreCreateBinary();
  105. }
  106. /* Enable Ethernet Rx interrrupt */
  107. { int i;
  108. for(i=0; i < EMAC_RXBUFNB; i++)
  109. {
  110. emac_dma_rx_desc_interrupt_config(&DMARxDscrTab[i], TRUE);
  111. }
  112. }
  113. if (!task_eth_flag)
  114. {
  115. xTaskCreate((TaskFunction_t)NETWORK_Task, (const char*)"PHY",(uint16_t)PHY_STK_SIZE,
  116. (void*)NULL, (UBaseType_t)PHY_PRIO, (TaskHandle_t*)&PHY_Handler);
  117. task_eth_flag = true;
  118. }
  119. /* Enable MAC and DMA transmission and reception */
  120. emac_start();
  121. #endif
  122. }
  123. /**
  124. * This function should do the actual transmission of the packet. The packet is
  125. * contained in the pbuf that is passed to the function. This pbuf
  126. * might be chained.
  127. *
  128. * @param netif the lwip network interface structure for this ethernetif
  129. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  130. * @return ERR_OK if the packet could be sent
  131. * an err_t value if the packet couldn't be sent
  132. *
  133. * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
  134. * strange results. You might consider waiting for space in the DMA queue
  135. * to become availale since the stack doesn't retry to send a packet
  136. * dropped because of memory failure (except for the TCP timers).
  137. */
  138. static err_t
  139. low_level_output(struct netif *netif, struct pbuf *p)
  140. {
  141. struct pbuf *q;
  142. int l = 0;
  143. if(xSemaphoreTake(PHY_TX_xSemaphore,250))
  144. {
  145. u8 *buffer = (u8 *)emac_getcurrenttxbuffer();
  146. for(q = p; q != NULL; q = q->next)
  147. {
  148. memcpy((u8_t*)&buffer[l], q->payload, q->len);
  149. l = l + q->len;
  150. }
  151. }
  152. if(emac_txpkt_chainmode(l) == ERROR)
  153. {
  154. return ERR_MEM;
  155. }
  156. xSemaphoreGive(PHY_TX_xSemaphore);
  157. return ERR_OK;
  158. }
  159. /**
  160. * Should allocate a pbuf and transfer the bytes of the incoming
  161. * packet from the interface into the pbuf.
  162. *
  163. * @param netif the lwip network interface structure for this ethernetif
  164. * @return a pbuf filled with the received packet (including MAC header)
  165. * NULL on memory error
  166. */
  167. static struct pbuf *
  168. low_level_input(struct netif *netif)
  169. {
  170. #if 0
  171. struct pbuf *p, *q;
  172. u16_t len;
  173. int l =0;
  174. FrameTypeDef frame;
  175. u8 *buffer;
  176. p = NULL;
  177. frame = emac_rxpkt_chainmode();
  178. /* Obtain the size of the packet and put it into the "len"
  179. variable. */
  180. len = frame.length;
  181. buffer = (u8 *)frame.buffer;
  182. /* We allocate a pbuf chain of pbufs from the pool. */
  183. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  184. if (p != NULL)
  185. {
  186. for (q = p; q != NULL; q = q->next)
  187. {
  188. memcpy((u8_t*)q->payload, (u8_t*)&buffer[l], q->len);
  189. l = l + q->len;
  190. }
  191. }
  192. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  193. frame.descriptor->status = EMAC_DMARXDESC_OWN;
  194. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  195. if(emac_dma_flag_get(EMAC_DMA_RBU_FLAG))
  196. {
  197. /* Clear RBUS ETHERNET DMA flag */
  198. emac_dma_flag_clear(EMAC_DMA_RBU_FLAG);
  199. /* Resume DMA reception */
  200. EMAC_DMA->rpd_bit.rpd = FALSE;
  201. }
  202. return p;
  203. #endif
  204. }
  205. /**
  206. * This function should be called when a packet is ready to be read
  207. * from the interface. It uses the function low_level_input() that
  208. * should handle the actual reception of bytes from the network
  209. * interface. Then the type of the received packet is determined and
  210. * the appropriate input function is called.
  211. *
  212. * @param netif the lwip network interface structure for this ethernetif
  213. */
  214. err_t
  215. ethernetif_input(struct netif *netif)
  216. {
  217. err_t err;
  218. struct pbuf *p;
  219. /* move received packet into a new pbuf */
  220. p = low_level_input(netif);
  221. /* no packet could be read, silently ignore this */
  222. if (p == NULL) return ERR_MEM;
  223. err = netif->input(p, netif);
  224. if (err != ERR_OK)
  225. {
  226. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
  227. pbuf_free(p);
  228. p = NULL;
  229. }
  230. return err;
  231. }
  232. /**
  233. * Should be called at the beginning of the program to set up the
  234. * network interface. It calls the function low_level_init() to do the
  235. * actual setup of the hardware.
  236. *
  237. * This function should be passed as a parameter to netif_add().
  238. *
  239. * @param netif the lwip network interface structure for this ethernetif
  240. * @return ERR_OK if the loopif is initialized
  241. * ERR_MEM if private data couldn't be allocated
  242. * any other err_t on error
  243. */
  244. err_t
  245. ethernetif_init(struct netif *netif)
  246. {
  247. struct ethernetif *ethernetif;
  248. LWIP_ASSERT("netif != NULL", (netif != NULL));
  249. ethernetif = mem_malloc(sizeof(struct ethernetif));
  250. if (ethernetif == NULL)
  251. {
  252. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
  253. return ERR_MEM;
  254. }
  255. #if LWIP_NETIF_HOSTNAME
  256. /* Initialize interface hostname */
  257. netif->hostname = "lwip";
  258. #endif /* LWIP_NETIF_HOSTNAME */
  259. /*
  260. * Initialize the snmp variables and counters inside the struct netif.
  261. * The last argument should be replaced with your link speed, in units
  262. * of bits per second.
  263. */
  264. NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
  265. netif->state = ethernetif;
  266. netif->name[0] = IFNAME0;
  267. netif->name[1] = IFNAME1;
  268. /* We directly use etharp_output() here to save a function call.
  269. * You can instead declare your own function an call etharp_output()
  270. * from it if you have to do some checks before sending (e.g. if link
  271. * is available...) */
  272. netif->output = etharp_output;
  273. netif->linkoutput = low_level_output;
  274. //ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
  275. /* initialize the hardware */
  276. low_level_init(netif);
  277. ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
  278. return ERR_OK;
  279. }
  280. /*******************************************************************************
  281. * Function Name : emac_rxpkt_chainmode
  282. * Description : Receives a packet.
  283. * Input : None
  284. * Output : None
  285. * Return : frame: farme size and location
  286. *******************************************************************************/
  287. FrameTypeDef emac_rxpkt_chainmode(void)
  288. {
  289. #if 0
  290. u32 framelength = 0;
  291. FrameTypeDef frame = {0,0};
  292. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  293. if((dma_rx_desc_to_get->status & EMAC_DMARXDESC_OWN) != (u32)RESET)
  294. {
  295. frame.length = FALSE;
  296. if(emac_dma_flag_get(EMAC_DMA_RBU_FLAG))
  297. {
  298. /* Clear RBUS ETHERNET DMA flag */
  299. emac_dma_flag_clear(EMAC_DMA_RBU_FLAG);
  300. /* Resume DMA reception */
  301. EMAC_DMA->rpd_bit.rpd = FALSE;
  302. }
  303. /* Return error: OWN bit set */
  304. return frame;
  305. }
  306. if(((dma_rx_desc_to_get->status & EMAC_DMATXDESC_ES) == (u32)RESET) &&
  307. ((dma_rx_desc_to_get->status & EMAC_DMARXDESC_LS) != (u32)RESET) &&
  308. ((dma_rx_desc_to_get->status & EMAC_DMARXDESC_FS) != (u32)RESET))
  309. {
  310. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  311. framelength = ((dma_rx_desc_to_get->status & EMAC_DMARXDESC_FL) >> EMAC_DMARxDesc_FrameLengthShift) - 4;
  312. /* Get the addrees of the actual buffer */
  313. frame.buffer = dma_rx_desc_to_get->buf1addr;
  314. }
  315. else
  316. {
  317. /* Return ERROR */
  318. framelength = FALSE;
  319. }
  320. frame.length = framelength;
  321. frame.descriptor = dma_rx_desc_to_get;
  322. /* Update the ETHERNET DMA global Rx descriptor with next Rx decriptor */
  323. /* Chained Mode */
  324. /* Selects the next DMA Rx descriptor list for next buffer to read */
  325. dma_rx_desc_to_get = (emac_dma_desc_type*) (dma_rx_desc_to_get->buf2nextdescaddr);
  326. /* Return Frame */
  327. return (frame);
  328. #endif
  329. }
  330. /*******************************************************************************
  331. * Function Name : emac_txpkt_chainmode
  332. * Description : Transmits a packet, from application buffer, pointed by ppkt.
  333. * Input : - FrameLength: Tx Packet size.
  334. * Output : None
  335. * Return : ERROR: in case of Tx desc owned by DMA
  336. * SUCCESS: for correct transmission
  337. *******************************************************************************/
  338. error_status emac_txpkt_chainmode(u16 FrameLength)
  339. {
  340. #if 0
  341. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  342. if((dma_tx_desc_to_set->status & EMAC_DMATXDESC_OWN) != (u32)RESET)
  343. {
  344. /* Return ERROR: OWN bit set */
  345. return ERROR;
  346. }
  347. /* Setting the Frame Length: bits[12:0] */
  348. dma_tx_desc_to_set->controlsize = (FrameLength & EMAC_DMATXDESC_TBS1);
  349. /* Setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
  350. dma_tx_desc_to_set->status |= EMAC_DMATXDESC_LS | EMAC_DMATXDESC_FS;
  351. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  352. dma_tx_desc_to_set->status |= EMAC_DMATXDESC_OWN;
  353. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  354. if(emac_dma_flag_get(EMAC_DMA_TBU_FLAG))
  355. {
  356. /* Clear TBUS ETHERNET DMA flag */
  357. emac_dma_flag_clear(EMAC_DMA_TBU_FLAG);
  358. /* Resume DMA transmission*/
  359. EMAC_DMA->tpd_bit.tpd = 0;
  360. }
  361. /* Update the ETHERNET DMA global Tx descriptor with next Tx decriptor */
  362. /* Chained Mode */
  363. /* Selects the next DMA Tx descriptor list for next buffer to send */
  364. dma_tx_desc_to_set = (emac_dma_desc_type*) (dma_tx_desc_to_set->buf2nextdescaddr);
  365. /* Return SUCCESS */
  366. return SUCCESS;
  367. #endif
  368. }
  369. /*******************************************************************************
  370. * Function Name : emac_getcurrenttxbuffer
  371. * Description : Return the address of the buffer pointed by the current descritor.
  372. * Input : None
  373. * Output : None
  374. * Return : Buffer address
  375. *******************************************************************************/
  376. u32 emac_getcurrenttxbuffer(void)
  377. {
  378. /* Return Buffer address */
  379. return (dma_tx_desc_to_set->buf1addr);
  380. }
  381. void NETWORK_Task(void *pvParameters)
  382. {
  383. struct pbuf *p;
  384. err_t err;
  385. for(;;)
  386. {
  387. if(xSemaphoreTake(PHY_RX_xSemaphore, portMAX_DELAY) == pdTRUE)
  388. {
  389. p = low_level_input(lwip_netif);
  390. }
  391. if(p != NULL)
  392. {
  393. err=lwip_netif->input(p, lwip_netif);
  394. if(err!=ERR_OK)
  395. {
  396. pbuf_free(p);
  397. p = NULL;
  398. }
  399. }
  400. }
  401. }