ethernetif.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /**
  2. ******************************************************************************
  3. * File Name : ethernetif.c
  4. * Description : This file provides code for the configuration
  5. * of the ethernetif.c MiddleWare.
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2018 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* Includes ------------------------------------------------------------------*/
  50. #include "stm32f4xx_hal.h"
  51. #include "lwip/opt.h"
  52. #include "lwip/mem.h"
  53. #include "lwip/memp.h"
  54. #include "lwip/timeouts.h"
  55. #include "netif/ethernet.h"
  56. #include "netif/etharp.h"
  57. #include "lwip/ethip6.h"
  58. #include "ethernetif.h"
  59. #include <string.h>
  60. /* Within 'USER CODE' section, code will be kept by default at each generation */
  61. /* USER CODE BEGIN 0 */
  62. /* USER CODE END 0 */
  63. /* Private define ------------------------------------------------------------*/
  64. /* Network interface name */
  65. #define IFNAME0 's'
  66. #define IFNAME1 't'
  67. /* USER CODE BEGIN 1 */
  68. /* USER CODE END 1 */
  69. /* Private variables ---------------------------------------------------------*/
  70. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  71. #pragma data_alignment=4
  72. #endif
  73. __ALIGN_BEGIN ETH_DMADescTypeDef DMARxDscrTab[ETH_RXBUFNB] __ALIGN_END;/* Ethernet Rx MA Descriptor */
  74. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  75. #pragma data_alignment=4
  76. #endif
  77. __ALIGN_BEGIN ETH_DMADescTypeDef DMATxDscrTab[ETH_TXBUFNB] __ALIGN_END;/* Ethernet Tx DMA Descriptor */
  78. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  79. #pragma data_alignment=4
  80. #endif
  81. __ALIGN_BEGIN uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE] __ALIGN_END; /* Ethernet Receive Buffer */
  82. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  83. #pragma data_alignment=4
  84. #endif
  85. __ALIGN_BEGIN uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE] __ALIGN_END; /* Ethernet Transmit Buffer */
  86. /* USER CODE BEGIN 2 */
  87. /* USER CODE END 2 */
  88. /* Global Ethernet handle */
  89. ETH_HandleTypeDef heth;
  90. /* USER CODE BEGIN 3 */
  91. /* USER CODE END 3 */
  92. /* Private functions ---------------------------------------------------------*/
  93. void HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle)
  94. {
  95. GPIO_InitTypeDef GPIO_InitStruct;
  96. if(ethHandle->Instance==ETH)
  97. {
  98. /* USER CODE BEGIN ETH_MspInit 0 */
  99. /* USER CODE END ETH_MspInit 0 */
  100. /* Enable Peripheral clock */
  101. __HAL_RCC_ETH_CLK_ENABLE();
  102. /**ETH GPIO Configuration
  103. PC1 ------> ETH_MDC
  104. PA1 ------> ETH_REF_CLK
  105. PA2 ------> ETH_MDIO
  106. PA7 ------> ETH_CRS_DV
  107. PC4 ------> ETH_RXD0
  108. PC5 ------> ETH_RXD1
  109. PB13 ------> ETH_TXD1
  110. PG11 ------> ETH_TX_EN
  111. PG13 ------> ETH_TXD0
  112. */
  113. GPIO_InitStruct.Pin = RMII_MDC_Pin|RMII_RXD0_Pin|RMII_RXD1_Pin;
  114. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  115. GPIO_InitStruct.Pull = GPIO_NOPULL;
  116. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  117. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  118. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  119. GPIO_InitStruct.Pin = GPIO_PIN_1|RMII_MDIO_Pin|GPIO_PIN_7;
  120. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  121. GPIO_InitStruct.Pull = GPIO_NOPULL;
  122. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  123. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  124. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  125. GPIO_InitStruct.Pin = RMII_TXD1_Pin;
  126. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  127. GPIO_InitStruct.Pull = GPIO_NOPULL;
  128. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  129. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  130. HAL_GPIO_Init(RMII_TXD1_GPIO_Port, &GPIO_InitStruct);
  131. GPIO_InitStruct.Pin = RMII_TX_EN_Pin|RMII_TXD0_Pin;
  132. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  133. GPIO_InitStruct.Pull = GPIO_NOPULL;
  134. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  135. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  136. HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
  137. /* Peripheral interrupt init */
  138. HAL_NVIC_SetPriority(ETH_IRQn, 0, 0);
  139. HAL_NVIC_EnableIRQ(ETH_IRQn);
  140. /* USER CODE BEGIN ETH_MspInit 1 */
  141. /* USER CODE END ETH_MspInit 1 */
  142. }
  143. }
  144. void HAL_ETH_MspDeInit(ETH_HandleTypeDef* ethHandle)
  145. {
  146. if(ethHandle->Instance==ETH)
  147. {
  148. /* USER CODE BEGIN ETH_MspDeInit 0 */
  149. /* USER CODE END ETH_MspDeInit 0 */
  150. /* Peripheral clock disable */
  151. __HAL_RCC_ETH_CLK_DISABLE();
  152. /**ETH GPIO Configuration
  153. PC1 ------> ETH_MDC
  154. PA1 ------> ETH_REF_CLK
  155. PA2 ------> ETH_MDIO
  156. PA7 ------> ETH_CRS_DV
  157. PC4 ------> ETH_RXD0
  158. PC5 ------> ETH_RXD1
  159. PB13 ------> ETH_TXD1
  160. PG11 ------> ETH_TX_EN
  161. PG13 ------> ETH_TXD0
  162. */
  163. HAL_GPIO_DeInit(GPIOC, RMII_MDC_Pin|RMII_RXD0_Pin|RMII_RXD1_Pin);
  164. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1|RMII_MDIO_Pin|GPIO_PIN_7);
  165. HAL_GPIO_DeInit(RMII_TXD1_GPIO_Port, RMII_TXD1_Pin);
  166. HAL_GPIO_DeInit(GPIOG, RMII_TX_EN_Pin|RMII_TXD0_Pin);
  167. /* Peripheral interrupt Deinit*/
  168. HAL_NVIC_DisableIRQ(ETH_IRQn);
  169. /* USER CODE BEGIN ETH_MspDeInit 1 */
  170. /* USER CODE END ETH_MspDeInit 1 */
  171. }
  172. }
  173. /* USER CODE BEGIN 4 */
  174. /* USER CODE END 4 */
  175. /*******************************************************************************
  176. LL Driver Interface ( LwIP stack --> ETH)
  177. *******************************************************************************/
  178. /**
  179. * In this function, the hardware should be initialized.
  180. * Called from ethernetif_init().
  181. *
  182. * @param netif the already initialized lwip network interface structure
  183. * for this ethernetif
  184. */
  185. static void low_level_init(struct netif *netif)
  186. {
  187. uint32_t regvalue = 0;
  188. HAL_StatusTypeDef hal_eth_init_status;
  189. /* Init ETH */
  190. uint8_t MACAddr[6] ;
  191. heth.Instance = ETH;
  192. heth.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE;
  193. heth.Init.PhyAddress = LAN8742A_PHY_ADDRESS;
  194. MACAddr[0] = 0x00;
  195. MACAddr[1] = 0x80;
  196. MACAddr[2] = 0xE1;
  197. MACAddr[3] = 0x12;
  198. MACAddr[4] = 0x34;
  199. MACAddr[5] = 0x56;
  200. heth.Init.MACAddr = &MACAddr[0];
  201. heth.Init.RxMode = ETH_RXPOLLING_MODE;
  202. heth.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;
  203. heth.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII;
  204. /* USER CODE BEGIN MACADDRESS */
  205. /* USER CODE END MACADDRESS */
  206. hal_eth_init_status = HAL_ETH_Init(&heth);
  207. if (hal_eth_init_status == HAL_OK)
  208. {
  209. /* Set netif link flag */
  210. netif->flags |= NETIF_FLAG_LINK_UP;
  211. }
  212. /* Initialize Tx Descriptors list: Chain Mode */
  213. HAL_ETH_DMATxDescListInit(&heth, DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
  214. /* Initialize Rx Descriptors list: Chain Mode */
  215. HAL_ETH_DMARxDescListInit(&heth, DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
  216. #if LWIP_ARP || LWIP_ETHERNET
  217. /* set MAC hardware address length */
  218. netif->hwaddr_len = ETH_HWADDR_LEN;
  219. /* set MAC hardware address */
  220. netif->hwaddr[0] = heth.Init.MACAddr[0];
  221. netif->hwaddr[1] = heth.Init.MACAddr[1];
  222. netif->hwaddr[2] = heth.Init.MACAddr[2];
  223. netif->hwaddr[3] = heth.Init.MACAddr[3];
  224. netif->hwaddr[4] = heth.Init.MACAddr[4];
  225. netif->hwaddr[5] = heth.Init.MACAddr[5];
  226. /* maximum transfer unit */
  227. netif->mtu = 1500;
  228. /* Accept broadcast address and ARP traffic */
  229. /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  230. #if LWIP_ARP
  231. netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  232. #else
  233. netif->flags |= NETIF_FLAG_BROADCAST;
  234. #endif /* LWIP_ARP */
  235. /* Enable MAC and DMA transmission and reception */
  236. HAL_ETH_Start(&heth);
  237. /* USER CODE BEGIN PHY_PRE_CONFIG */
  238. /* USER CODE END PHY_PRE_CONFIG */
  239. /* Read Register Configuration */
  240. HAL_ETH_ReadPHYRegister(&heth, PHY_ISFR, &regvalue);
  241. regvalue |= (PHY_ISFR_INT4);
  242. /* Enable Interrupt on change of link status */
  243. HAL_ETH_WritePHYRegister(&heth, PHY_ISFR , regvalue );
  244. /* Read Register Configuration */
  245. HAL_ETH_ReadPHYRegister(&heth, PHY_ISFR , &regvalue);
  246. /* USER CODE BEGIN PHY_POST_CONFIG */
  247. /* USER CODE END PHY_POST_CONFIG */
  248. #endif /* LWIP_ARP || LWIP_ETHERNET */
  249. /* USER CODE BEGIN LOW_LEVEL_INIT */
  250. /* USER CODE END LOW_LEVEL_INIT */
  251. }
  252. /**
  253. * This function should do the actual transmission of the packet. The packet is
  254. * contained in the pbuf that is passed to the function. This pbuf
  255. * might be chained.
  256. *
  257. * @param netif the lwip network interface structure for this ethernetif
  258. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  259. * @return ERR_OK if the packet could be sent
  260. * an err_t value if the packet couldn't be sent
  261. *
  262. * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
  263. * strange results. You might consider waiting for space in the DMA queue
  264. * to become availale since the stack doesn't retry to send a packet
  265. * dropped because of memory failure (except for the TCP timers).
  266. */
  267. static err_t low_level_output(struct netif *netif, struct pbuf *p)
  268. {
  269. err_t errval;
  270. struct pbuf *q;
  271. uint8_t *buffer = (uint8_t *)(heth.TxDesc->Buffer1Addr);
  272. __IO ETH_DMADescTypeDef *DmaTxDesc;
  273. uint32_t framelength = 0;
  274. uint32_t bufferoffset = 0;
  275. uint32_t byteslefttocopy = 0;
  276. uint32_t payloadoffset = 0;
  277. DmaTxDesc = heth.TxDesc;
  278. bufferoffset = 0;
  279. /* copy frame from pbufs to driver buffers */
  280. for(q = p; q != NULL; q = q->next)
  281. {
  282. /* Is this buffer available? If not, goto error */
  283. if((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
  284. {
  285. errval = ERR_USE;
  286. goto error;
  287. }
  288. /* Get bytes in current lwIP buffer */
  289. byteslefttocopy = q->len;
  290. payloadoffset = 0;
  291. /* Check if the length of data to copy is bigger than Tx buffer size*/
  292. while( (byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE )
  293. {
  294. /* Copy data to Tx buffer*/
  295. memcpy( (uint8_t*)((uint8_t*)buffer + bufferoffset), (uint8_t*)((uint8_t*)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset) );
  296. /* Point to next descriptor */
  297. DmaTxDesc = (ETH_DMADescTypeDef *)(DmaTxDesc->Buffer2NextDescAddr);
  298. /* Check if the buffer is available */
  299. if((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
  300. {
  301. errval = ERR_USE;
  302. goto error;
  303. }
  304. buffer = (uint8_t *)(DmaTxDesc->Buffer1Addr);
  305. byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset);
  306. payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset);
  307. framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset);
  308. bufferoffset = 0;
  309. }
  310. /* Copy the remaining bytes */
  311. memcpy( (uint8_t*)((uint8_t*)buffer + bufferoffset), (uint8_t*)((uint8_t*)q->payload + payloadoffset), byteslefttocopy );
  312. bufferoffset = bufferoffset + byteslefttocopy;
  313. framelength = framelength + byteslefttocopy;
  314. }
  315. /* Prepare transmit descriptors to give to DMA */
  316. HAL_ETH_TransmitFrame(&heth, framelength);
  317. errval = ERR_OK;
  318. error:
  319. /* When Transmit Underflow flag is set, clear it and issue a Transmit Poll Demand to resume transmission */
  320. if ((heth.Instance->DMASR & ETH_DMASR_TUS) != (uint32_t)RESET)
  321. {
  322. /* Clear TUS ETHERNET DMA flag */
  323. heth.Instance->DMASR = ETH_DMASR_TUS;
  324. /* Resume DMA transmission*/
  325. heth.Instance->DMATPDR = 0;
  326. }
  327. return errval;
  328. }
  329. /**
  330. * Should allocate a pbuf and transfer the bytes of the incoming
  331. * packet from the interface into the pbuf.
  332. *
  333. * @param netif the lwip network interface structure for this ethernetif
  334. * @return a pbuf filled with the received packet (including MAC header)
  335. * NULL on memory error
  336. */
  337. static struct pbuf * low_level_input(struct netif *netif)
  338. {
  339. struct pbuf *p = NULL;
  340. struct pbuf *q = NULL;
  341. uint16_t len = 0;
  342. uint8_t *buffer;
  343. __IO ETH_DMADescTypeDef *dmarxdesc;
  344. uint32_t bufferoffset = 0;
  345. uint32_t payloadoffset = 0;
  346. uint32_t byteslefttocopy = 0;
  347. uint32_t i=0;
  348. /* get received frame */
  349. if (HAL_ETH_GetReceivedFrame(&heth) != HAL_OK)
  350. return NULL;
  351. /* Obtain the size of the packet and put it into the "len" variable. */
  352. len = heth.RxFrameInfos.length;
  353. buffer = (uint8_t *)heth.RxFrameInfos.buffer;
  354. if (len > 0)
  355. {
  356. /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
  357. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  358. }
  359. if (p != NULL)
  360. {
  361. dmarxdesc = heth.RxFrameInfos.FSRxDesc;
  362. bufferoffset = 0;
  363. for(q = p; q != NULL; q = q->next)
  364. {
  365. byteslefttocopy = q->len;
  366. payloadoffset = 0;
  367. /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
  368. while( (byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE )
  369. {
  370. /* Copy data to pbuf */
  371. memcpy( (uint8_t*)((uint8_t*)q->payload + payloadoffset), (uint8_t*)((uint8_t*)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
  372. /* Point to next descriptor */
  373. dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
  374. buffer = (uint8_t *)(dmarxdesc->Buffer1Addr);
  375. byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
  376. payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
  377. bufferoffset = 0;
  378. }
  379. /* Copy remaining data in pbuf */
  380. memcpy( (uint8_t*)((uint8_t*)q->payload + payloadoffset), (uint8_t*)((uint8_t*)buffer + bufferoffset), byteslefttocopy);
  381. bufferoffset = bufferoffset + byteslefttocopy;
  382. }
  383. }
  384. /* Release descriptors to DMA */
  385. /* Point to first descriptor */
  386. dmarxdesc = heth.RxFrameInfos.FSRxDesc;
  387. /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
  388. for (i=0; i< heth.RxFrameInfos.SegCount; i++)
  389. {
  390. dmarxdesc->Status |= ETH_DMARXDESC_OWN;
  391. dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
  392. }
  393. /* Clear Segment_Count */
  394. heth.RxFrameInfos.SegCount =0;
  395. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  396. if ((heth.Instance->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)
  397. {
  398. /* Clear RBUS ETHERNET DMA flag */
  399. heth.Instance->DMASR = ETH_DMASR_RBUS;
  400. /* Resume DMA reception */
  401. heth.Instance->DMARPDR = 0;
  402. }
  403. return p;
  404. }
  405. /**
  406. * This function should be called when a packet is ready to be read
  407. * from the interface. It uses the function low_level_input() that
  408. * should handle the actual reception of bytes from the network
  409. * interface. Then the type of the received packet is determined and
  410. * the appropriate input function is called.
  411. *
  412. * @param netif the lwip network interface structure for this ethernetif
  413. */
  414. void ethernetif_input(struct netif *netif)
  415. {
  416. err_t err;
  417. struct pbuf *p;
  418. /* move received packet into a new pbuf */
  419. p = low_level_input(netif);
  420. /* no packet could be read, silently ignore this */
  421. if (p == NULL) return;
  422. /* entry point to the LwIP stack */
  423. err = netif->input(p, netif);
  424. if (err != ERR_OK)
  425. {
  426. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
  427. pbuf_free(p);
  428. p = NULL;
  429. }
  430. }
  431. #if !LWIP_ARP
  432. /**
  433. * This function has to be completed by user in case of ARP OFF.
  434. *
  435. * @param netif the lwip network interface structure for this ethernetif
  436. * @return ERR_OK if ...
  437. */
  438. static err_t low_level_output_arp_off(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr)
  439. {
  440. err_t errval;
  441. errval = ERR_OK;
  442. /* USER CODE BEGIN 5 */
  443. /* USER CODE END 5 */
  444. return errval;
  445. }
  446. #endif /* LWIP_ARP */
  447. /**
  448. * Should be called at the beginning of the program to set up the
  449. * network interface. It calls the function low_level_init() to do the
  450. * actual setup of the hardware.
  451. *
  452. * This function should be passed as a parameter to netif_add().
  453. *
  454. * @param netif the lwip network interface structure for this ethernetif
  455. * @return ERR_OK if the loopif is initialized
  456. * ERR_MEM if private data couldn't be allocated
  457. * any other err_t on error
  458. */
  459. err_t ethernetif_init(struct netif *netif)
  460. {
  461. LWIP_ASSERT("netif != NULL", (netif != NULL));
  462. #if LWIP_NETIF_HOSTNAME
  463. /* Initialize interface hostname */
  464. netif->hostname = "lwip";
  465. #endif /* LWIP_NETIF_HOSTNAME */
  466. netif->name[0] = IFNAME0;
  467. netif->name[1] = IFNAME1;
  468. /* We directly use etharp_output() here to save a function call.
  469. * You can instead declare your own function an call etharp_output()
  470. * from it if you have to do some checks before sending (e.g. if link
  471. * is available...) */
  472. #if LWIP_IPV4
  473. #if LWIP_ARP || LWIP_ETHERNET
  474. #if LWIP_ARP
  475. netif->output = etharp_output;
  476. #else
  477. /* The user should write ist own code in low_level_output_arp_off function */
  478. netif->output = low_level_output_arp_off;
  479. #endif /* LWIP_ARP */
  480. #endif /* LWIP_ARP || LWIP_ETHERNET */
  481. #endif /* LWIP_IPV4 */
  482. #if LWIP_IPV6
  483. netif->output_ip6 = ethip6_output;
  484. #endif /* LWIP_IPV6 */
  485. netif->linkoutput = low_level_output;
  486. /* initialize the hardware */
  487. low_level_init(netif);
  488. return ERR_OK;
  489. }
  490. /* USER CODE BEGIN 6 */
  491. /**
  492. * @brief Returns the current time in milliseconds
  493. * when LWIP_TIMERS == 1 and NO_SYS == 1
  494. * @param None
  495. * @retval Time
  496. */
  497. u32_t sys_jiffies(void)
  498. {
  499. return HAL_GetTick();
  500. }
  501. /**
  502. * @brief Returns the current time in milliseconds
  503. * when LWIP_TIMERS == 1 and NO_SYS == 1
  504. * @param None
  505. * @retval Time
  506. */
  507. u32_t sys_now(void)
  508. {
  509. return HAL_GetTick();
  510. }
  511. /* USER CODE END 6 */
  512. /* USER CODE BEGIN 7 */
  513. /* USER CODE END 7 */
  514. #if LWIP_NETIF_LINK_CALLBACK
  515. /**
  516. * @brief Link callback function, this function is called on change of link status
  517. * to update low level driver configuration.
  518. * @param netif: The network interface
  519. * @retval None
  520. */
  521. void ethernetif_update_config(struct netif *netif)
  522. {
  523. __IO uint32_t tickstart = 0;
  524. uint32_t regvalue = 0;
  525. if(netif_is_link_up(netif))
  526. {
  527. /* Restart the auto-negotiation */
  528. if(heth.Init.AutoNegotiation != ETH_AUTONEGOTIATION_DISABLE)
  529. {
  530. /* Enable Auto-Negotiation */
  531. HAL_ETH_WritePHYRegister(&heth, PHY_BCR, PHY_AUTONEGOTIATION);
  532. /* Get tick */
  533. tickstart = HAL_GetTick();
  534. /* Wait until the auto-negotiation will be completed */
  535. do
  536. {
  537. HAL_ETH_ReadPHYRegister(&heth, PHY_BSR, &regvalue);
  538. /* Check for the Timeout ( 1s ) */
  539. if((HAL_GetTick() - tickstart ) > 1000)
  540. {
  541. /* In case of timeout */
  542. goto error;
  543. }
  544. } while (((regvalue & PHY_AUTONEGO_COMPLETE) != PHY_AUTONEGO_COMPLETE));
  545. /* Read the result of the auto-negotiation */
  546. HAL_ETH_ReadPHYRegister(&heth, PHY_SR, &regvalue);
  547. /* Configure the MAC with the Duplex Mode fixed by the auto-negotiation process */
  548. if((regvalue & PHY_DUPLEX_STATUS) != (uint32_t)RESET)
  549. {
  550. /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
  551. heth.Init.DuplexMode = ETH_MODE_FULLDUPLEX;
  552. }
  553. else
  554. {
  555. /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
  556. heth.Init.DuplexMode = ETH_MODE_HALFDUPLEX;
  557. }
  558. /* Configure the MAC with the speed fixed by the auto-negotiation process */
  559. if(regvalue & PHY_SPEED_STATUS)
  560. {
  561. /* Set Ethernet speed to 10M following the auto-negotiation */
  562. heth.Init.Speed = ETH_SPEED_10M;
  563. }
  564. else
  565. {
  566. /* Set Ethernet speed to 100M following the auto-negotiation */
  567. heth.Init.Speed = ETH_SPEED_100M;
  568. }
  569. }
  570. else /* AutoNegotiation Disable */
  571. {
  572. error :
  573. /* Check parameters */
  574. assert_param(IS_ETH_SPEED(heth.Init.Speed));
  575. assert_param(IS_ETH_DUPLEX_MODE(heth.Init.DuplexMode));
  576. /* Set MAC Speed and Duplex Mode to PHY */
  577. HAL_ETH_WritePHYRegister(&heth, PHY_BCR, ((uint16_t)(heth.Init.DuplexMode >> 3) |
  578. (uint16_t)(heth.Init.Speed >> 1)));
  579. }
  580. /* ETHERNET MAC Re-Configuration */
  581. HAL_ETH_ConfigMAC(&heth, (ETH_MACInitTypeDef *) NULL);
  582. /* Restart MAC interface */
  583. HAL_ETH_Start(&heth);
  584. }
  585. else
  586. {
  587. /* Stop MAC interface */
  588. HAL_ETH_Stop(&heth);
  589. }
  590. ethernetif_notify_conn_changed(netif);
  591. }
  592. /* USER CODE BEGIN 8 */
  593. /**
  594. * @brief This function notify user about link status changement.
  595. * @param netif: the network interface
  596. * @retval None
  597. */
  598. __weak void ethernetif_notify_conn_changed(struct netif *netif)
  599. {
  600. /* NOTE : This is function could be implemented in user file
  601. when the callback is needed,
  602. */
  603. }
  604. /* USER CODE END 8 */
  605. #endif /* LWIP_NETIF_LINK_CALLBACK */
  606. /* USER CODE BEGIN 9 */
  607. /* USER CODE END 9 */
  608. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/