You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

897 lines
24 KiB

12 months ago
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * File Name : ethernetif.c
  5. * Description : This file provides code for the configuration
  6. * of the ethernetif.c MiddleWare.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "lwip/opt.h"
  23. #include "lwip/timeouts.h"
  24. #include "netif/ethernet.h"
  25. #include "netif/etharp.h"
  26. #include "lwip/ethip6.h"
  27. #include "ethernetif.h"
  28. #include "lan8742.h"
  29. #include <string.h>
  30. #include "cmsis_os.h"
  31. #include "lwip/tcpip.h"
  32. /* Within 'USER CODE' section, code will be kept by default at each generation */
  33. /* USER CODE BEGIN 0 */
  34. /* USER CODE END 0 */
  35. /* Private define ------------------------------------------------------------*/
  36. /* The time to block waiting for input. */
  37. #define TIME_WAITING_FOR_INPUT ( portMAX_DELAY )
  38. /* USER CODE BEGIN OS_THREAD_STACK_SIZE_WITH_RTOS */
  39. /* Stack size of the interface thread */
  40. #define INTERFACE_THREAD_STACK_SIZE ( 350 )
  41. /* USER CODE END OS_THREAD_STACK_SIZE_WITH_RTOS */
  42. /* Network interface name */
  43. #define IFNAME0 's'
  44. #define IFNAME1 't'
  45. /* ETH Setting */
  46. #define ETH_DMA_TRANSMIT_TIMEOUT ( 20U )
  47. #define ETH_TX_BUFFER_MAX ((ETH_TX_DESC_CNT) * 2U)
  48. /* USER CODE BEGIN 1 */
  49. /* USER CODE END 1 */
  50. /* Private variables ---------------------------------------------------------*/
  51. /*
  52. @Note: This interface is implemented to operate in zero-copy mode only:
  53. - Rx buffers will be allocated from LwIP stack memory heap,
  54. then passed to ETH HAL driver.
  55. - Tx buffers will be allocated from LwIP stack memory heap,
  56. then passed to ETH HAL driver.
  57. @Notes:
  58. 1.a. ETH DMA Rx descriptors must be contiguous, the default count is 4,
  59. to customize it please redefine ETH_RX_DESC_CNT in ETH GUI (Rx Descriptor Length)
  60. so that updated value will be generated in stm32xxxx_hal_conf.h
  61. 1.b. ETH DMA Tx descriptors must be contiguous, the default count is 4,
  62. to customize it please redefine ETH_TX_DESC_CNT in ETH GUI (Tx Descriptor Length)
  63. so that updated value will be generated in stm32xxxx_hal_conf.h
  64. 2.a. Rx Buffers number must be between ETH_RX_DESC_CNT and 2*ETH_RX_DESC_CNT
  65. 2.b. Rx Buffers must have the same size: ETH_RX_BUF_SIZE, this value must
  66. passed to ETH DMA in the init field (heth.Init.RxBuffLen)
  67. 2.c The RX Ruffers addresses and sizes must be properly defined to be aligned
  68. to L1-CACHE line size (32 bytes).
  69. */
  70. /* Data Type Definitions */
  71. typedef enum
  72. {
  73. RX_ALLOC_OK = 0x00,
  74. RX_ALLOC_ERROR = 0x01
  75. } RxAllocStatusTypeDef;
  76. typedef struct
  77. {
  78. struct pbuf_custom pbuf_custom;
  79. uint8_t buff[(ETH_RX_BUF_SIZE + 31) & ~31] __ALIGNED(32);
  80. } RxBuff_t;
  81. /* Memory Pool Declaration */
  82. #define ETH_RX_BUFFER_CNT 12U
  83. LWIP_MEMPOOL_DECLARE(RX_POOL, ETH_RX_BUFFER_CNT, sizeof(RxBuff_t), "Zero-copy RX PBUF pool");
  84. /* Variable Definitions */
  85. static uint8_t RxAllocStatus;
  86. ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
  87. ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
  88. /* USER CODE BEGIN 2 */
  89. /* USER CODE END 2 */
  90. osSemaphoreId RxPktSemaphore = NULL; /* Semaphore to signal incoming packets */
  91. osSemaphoreId TxPktSemaphore = NULL; /* Semaphore to signal transmit packet complete */
  92. /* Global Ethernet handle */
  93. ETH_HandleTypeDef heth;
  94. ETH_TxPacketConfig TxConfig;
  95. /* Private function prototypes -----------------------------------------------*/
  96. static void ethernetif_input(void const * argument);
  97. int32_t ETH_PHY_IO_Init(void);
  98. int32_t ETH_PHY_IO_DeInit (void);
  99. int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal);
  100. int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal);
  101. int32_t ETH_PHY_IO_GetTick(void);
  102. lan8742_Object_t LAN8742;
  103. lan8742_IOCtx_t LAN8742_IOCtx = {ETH_PHY_IO_Init,
  104. ETH_PHY_IO_DeInit,
  105. ETH_PHY_IO_WriteReg,
  106. ETH_PHY_IO_ReadReg,
  107. ETH_PHY_IO_GetTick};
  108. /* USER CODE BEGIN 3 */
  109. /* USER CODE END 3 */
  110. /* Private functions ---------------------------------------------------------*/
  111. void pbuf_free_custom(struct pbuf *p);
  112. /**
  113. * @brief Ethernet Rx Transfer completed callback
  114. * @param handlerEth: ETH handler
  115. * @retval None
  116. */
  117. void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *handlerEth)
  118. {
  119. osSemaphoreRelease(RxPktSemaphore);
  120. }
  121. /**
  122. * @brief Ethernet Tx Transfer completed callback
  123. * @param handlerEth: ETH handler
  124. * @retval None
  125. */
  126. void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *handlerEth)
  127. {
  128. osSemaphoreRelease(TxPktSemaphore);
  129. }
  130. /**
  131. * @brief Ethernet DMA transfer error callback
  132. * @param handlerEth: ETH handler
  133. * @retval None
  134. */
  135. void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *handlerEth)
  136. {
  137. if((HAL_ETH_GetDMAError(handlerEth) & ETH_DMASR_RBUS) == ETH_DMASR_RBUS)
  138. {
  139. osSemaphoreRelease(RxPktSemaphore);
  140. }
  141. }
  142. /* USER CODE BEGIN 4 */
  143. // add by zhaohe
  144. extern uint8_t g_mac[];
  145. /* USER CODE END 4 */
  146. /*******************************************************************************
  147. LL Driver Interface ( LwIP stack --> ETH)
  148. *******************************************************************************/
  149. /**
  150. * @brief In this function, the hardware should be initialized.
  151. * Called from ethernetif_init().
  152. *
  153. * @param netif the already initialized lwip network interface structure
  154. * for this ethernetif
  155. */
  156. static void low_level_init(struct netif *netif)
  157. {
  158. HAL_StatusTypeDef hal_eth_init_status = HAL_OK;
  159. uint32_t duplex, speed = 0;
  160. int32_t PHYLinkState = 0;
  161. ETH_MACConfigTypeDef MACConf = {0};
  162. /* Start ETH HAL Init */
  163. uint8_t MACAddr[6] ;
  164. heth.Instance = ETH;
  165. MACAddr[0] = 0x00;
  166. MACAddr[1] = 0x80;
  167. MACAddr[2] = 0xE1;
  168. MACAddr[3] = 0x00;
  169. MACAddr[4] = 0x00;
  170. MACAddr[5] = 0x01;
  171. heth.Init.MACAddr = &MACAddr[0];
  172. heth.Init.MediaInterface = HAL_ETH_RMII_MODE;
  173. heth.Init.TxDesc = DMATxDscrTab;
  174. heth.Init.RxDesc = DMARxDscrTab;
  175. heth.Init.RxBuffLen = 1536;
  176. /* USER CODE BEGIN MACADDRESS */
  177. // add by zhaohe
  178. MACAddr[0] = g_mac[0];
  179. MACAddr[1] = g_mac[1];
  180. MACAddr[2] = g_mac[2];
  181. MACAddr[3] = g_mac[3];
  182. MACAddr[4] = g_mac[4];
  183. MACAddr[5] = g_mac[5];
  184. /* USER CODE END MACADDRESS */
  185. hal_eth_init_status = HAL_ETH_Init(&heth);
  186. memset(&TxConfig, 0 , sizeof(ETH_TxPacketConfig));
  187. TxConfig.Attributes = ETH_TX_PACKETS_FEATURES_CSUM | ETH_TX_PACKETS_FEATURES_CRCPAD;
  188. TxConfig.ChecksumCtrl = ETH_CHECKSUM_IPHDR_PAYLOAD_INSERT_PHDR_CALC;
  189. TxConfig.CRCPadCtrl = ETH_CRC_PAD_INSERT;
  190. /* End ETH HAL Init */
  191. /* Initialize the RX POOL */
  192. LWIP_MEMPOOL_INIT(RX_POOL);
  193. #if LWIP_ARP || LWIP_ETHERNET
  194. /* set MAC hardware address length */
  195. netif->hwaddr_len = ETH_HWADDR_LEN;
  196. /* set MAC hardware address */
  197. netif->hwaddr[0] = heth.Init.MACAddr[0];
  198. netif->hwaddr[1] = heth.Init.MACAddr[1];
  199. netif->hwaddr[2] = heth.Init.MACAddr[2];
  200. netif->hwaddr[3] = heth.Init.MACAddr[3];
  201. netif->hwaddr[4] = heth.Init.MACAddr[4];
  202. netif->hwaddr[5] = heth.Init.MACAddr[5];
  203. /* maximum transfer unit */
  204. netif->mtu = ETH_MAX_PAYLOAD;
  205. /* Accept broadcast address and ARP traffic */
  206. /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  207. #if LWIP_ARP
  208. netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  209. #else
  210. netif->flags |= NETIF_FLAG_BROADCAST;
  211. #endif /* LWIP_ARP */
  212. /* create a binary semaphore used for informing ethernetif of frame reception */
  213. RxPktSemaphore = xSemaphoreCreateBinary();
  214. /* create a binary semaphore used for informing ethernetif of frame transmission */
  215. TxPktSemaphore = xSemaphoreCreateBinary();
  216. /* create the task that handles the ETH_MAC */
  217. /* USER CODE BEGIN OS_THREAD_DEF_CREATE_CMSIS_RTOS_V1 */
  218. osThreadDef(EthIf, ethernetif_input, osPriorityRealtime, 0, INTERFACE_THREAD_STACK_SIZE);
  219. osThreadCreate (osThread(EthIf), netif);
  220. /* USER CODE END OS_THREAD_DEF_CREATE_CMSIS_RTOS_V1 */
  221. /* USER CODE BEGIN PHY_PRE_CONFIG */
  222. /* USER CODE END PHY_PRE_CONFIG */
  223. /* Set PHY IO functions */
  224. LAN8742_RegisterBusIO(&LAN8742, &LAN8742_IOCtx);
  225. /* Initialize the LAN8742 ETH PHY */
  226. LAN8742_Init(&LAN8742);
  227. if (hal_eth_init_status == HAL_OK)
  228. {
  229. PHYLinkState = LAN8742_GetLinkState(&LAN8742);
  230. /* Get link state */
  231. if(PHYLinkState <= LAN8742_STATUS_LINK_DOWN)
  232. {
  233. netif_set_link_down(netif);
  234. netif_set_down(netif);
  235. }
  236. else
  237. {
  238. switch (PHYLinkState)
  239. {
  240. case LAN8742_STATUS_100MBITS_FULLDUPLEX:
  241. duplex = ETH_FULLDUPLEX_MODE;
  242. speed = ETH_SPEED_100M;
  243. break;
  244. case LAN8742_STATUS_100MBITS_HALFDUPLEX:
  245. duplex = ETH_HALFDUPLEX_MODE;
  246. speed = ETH_SPEED_100M;
  247. break;
  248. case LAN8742_STATUS_10MBITS_FULLDUPLEX:
  249. duplex = ETH_FULLDUPLEX_MODE;
  250. speed = ETH_SPEED_10M;
  251. break;
  252. case LAN8742_STATUS_10MBITS_HALFDUPLEX:
  253. duplex = ETH_HALFDUPLEX_MODE;
  254. speed = ETH_SPEED_10M;
  255. break;
  256. default:
  257. duplex = ETH_FULLDUPLEX_MODE;
  258. speed = ETH_SPEED_100M;
  259. break;
  260. }
  261. /* Get MAC Config MAC */
  262. HAL_ETH_GetMACConfig(&heth, &MACConf);
  263. MACConf.DuplexMode = duplex;
  264. MACConf.Speed = speed;
  265. HAL_ETH_SetMACConfig(&heth, &MACConf);
  266. HAL_ETH_Start_IT(&heth);
  267. netif_set_up(netif);
  268. netif_set_link_up(netif);
  269. /* USER CODE BEGIN PHY_POST_CONFIG */
  270. /* USER CODE END PHY_POST_CONFIG */
  271. }
  272. }
  273. else
  274. {
  275. Error_Handler();
  276. }
  277. #endif /* LWIP_ARP || LWIP_ETHERNET */
  278. /* USER CODE BEGIN LOW_LEVEL_INIT */
  279. /* USER CODE END LOW_LEVEL_INIT */
  280. }
  281. /**
  282. * @brief This function should do the actual transmission of the packet. The packet is
  283. * contained in the pbuf that is passed to the function. This pbuf
  284. * might be chained.
  285. *
  286. * @param netif the lwip network interface structure for this ethernetif
  287. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  288. * @return ERR_OK if the packet could be sent
  289. * an err_t value if the packet couldn't be sent
  290. *
  291. * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
  292. * strange results. You might consider waiting for space in the DMA queue
  293. * to become available since the stack doesn't retry to send a packet
  294. * dropped because of memory failure (except for the TCP timers).
  295. */
  296. static err_t low_level_output(struct netif *netif, struct pbuf *p)
  297. {
  298. uint32_t i = 0U;
  299. struct pbuf *q = NULL;
  300. err_t errval = ERR_OK;
  301. ETH_BufferTypeDef Txbuffer[ETH_TX_DESC_CNT] = {0};
  302. memset(Txbuffer, 0 , ETH_TX_DESC_CNT*sizeof(ETH_BufferTypeDef));
  303. for(q = p; q != NULL; q = q->next)
  304. {
  305. if(i >= ETH_TX_DESC_CNT)
  306. return ERR_IF;
  307. Txbuffer[i].buffer = q->payload;
  308. Txbuffer[i].len = q->len;
  309. if(i>0)
  310. {
  311. Txbuffer[i-1].next = &Txbuffer[i];
  312. }
  313. if(q->next == NULL)
  314. {
  315. Txbuffer[i].next = NULL;
  316. }
  317. i++;
  318. }
  319. TxConfig.Length = p->tot_len;
  320. TxConfig.TxBuffer = Txbuffer;
  321. TxConfig.pData = p;
  322. pbuf_ref(p);
  323. HAL_ETH_Transmit_IT(&heth, &TxConfig);
  324. while(osSemaphoreWait(TxPktSemaphore, TIME_WAITING_FOR_INPUT)!=osOK)
  325. {
  326. }
  327. HAL_ETH_ReleaseTxPacket(&heth);
  328. return errval;
  329. }
  330. /**
  331. * @brief Should allocate a pbuf and transfer the bytes of the incoming
  332. * packet from the interface into the pbuf.
  333. *
  334. * @param netif the lwip network interface structure for this ethernetif
  335. * @return a pbuf filled with the received packet (including MAC header)
  336. * NULL on memory error
  337. */
  338. static struct pbuf * low_level_input(struct netif *netif)
  339. {
  340. struct pbuf *p = NULL;
  341. if(RxAllocStatus == RX_ALLOC_OK)
  342. {
  343. HAL_ETH_ReadData(&heth, (void **)&p);
  344. }
  345. return p;
  346. }
  347. /**
  348. * @brief This function should be called when a packet is ready to be read
  349. * from the interface. It uses the function low_level_input() that
  350. * should handle the actual reception of bytes from the network
  351. * interface. Then the type of the received packet is determined and
  352. * the appropriate input function is called.
  353. *
  354. * @param netif the lwip network interface structure for this ethernetif
  355. */
  356. static void ethernetif_input(void const * argument)
  357. {
  358. struct pbuf *p = NULL;
  359. struct netif *netif = (struct netif *) argument;
  360. for( ;; )
  361. {
  362. if (osSemaphoreWait(RxPktSemaphore, TIME_WAITING_FOR_INPUT) == osOK)
  363. {
  364. do
  365. {
  366. p = low_level_input( netif );
  367. if (p != NULL)
  368. {
  369. if (netif->input( p, netif) != ERR_OK )
  370. {
  371. pbuf_free(p);
  372. }
  373. }
  374. } while(p!=NULL);
  375. }
  376. }
  377. }
  378. #if !LWIP_ARP
  379. /**
  380. * This function has to be completed by user in case of ARP OFF.
  381. *
  382. * @param netif the lwip network interface structure for this ethernetif
  383. * @return ERR_OK if ...
  384. */
  385. static err_t low_level_output_arp_off(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr)
  386. {
  387. err_t errval;
  388. errval = ERR_OK;
  389. /* USER CODE BEGIN 5 */
  390. /* USER CODE END 5 */
  391. return errval;
  392. }
  393. #endif /* LWIP_ARP */
  394. /**
  395. * @brief Should be called at the beginning of the program to set up the
  396. * network interface. It calls the function low_level_init() to do the
  397. * actual setup of the hardware.
  398. *
  399. * This function should be passed as a parameter to netif_add().
  400. *
  401. * @param netif the lwip network interface structure for this ethernetif
  402. * @return ERR_OK if the loopif is initialized
  403. * ERR_MEM if private data couldn't be allocated
  404. * any other err_t on error
  405. */
  406. err_t ethernetif_init(struct netif *netif)
  407. {
  408. LWIP_ASSERT("netif != NULL", (netif != NULL));
  409. #if LWIP_NETIF_HOSTNAME
  410. /* Initialize interface hostname */
  411. netif->hostname = "lwip";
  412. #endif /* LWIP_NETIF_HOSTNAME */
  413. /*
  414. * Initialize the snmp variables and counters inside the struct netif.
  415. * The last argument should be replaced with your link speed, in units
  416. * of bits per second.
  417. */
  418. // MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
  419. netif->name[0] = IFNAME0;
  420. netif->name[1] = IFNAME1;
  421. /* We directly use etharp_output() here to save a function call.
  422. * You can instead declare your own function an call etharp_output()
  423. * from it if you have to do some checks before sending (e.g. if link
  424. * is available...) */
  425. #if LWIP_IPV4
  426. #if LWIP_ARP || LWIP_ETHERNET
  427. #if LWIP_ARP
  428. netif->output = etharp_output;
  429. #else
  430. /* The user should write its own code in low_level_output_arp_off function */
  431. netif->output = low_level_output_arp_off;
  432. #endif /* LWIP_ARP */
  433. #endif /* LWIP_ARP || LWIP_ETHERNET */
  434. #endif /* LWIP_IPV4 */
  435. #if LWIP_IPV6
  436. netif->output_ip6 = ethip6_output;
  437. #endif /* LWIP_IPV6 */
  438. netif->linkoutput = low_level_output;
  439. /* initialize the hardware */
  440. low_level_init(netif);
  441. return ERR_OK;
  442. }
  443. /**
  444. * @brief Custom Rx pbuf free callback
  445. * @param pbuf: pbuf to be freed
  446. * @retval None
  447. */
  448. void pbuf_free_custom(struct pbuf *p)
  449. {
  450. struct pbuf_custom* custom_pbuf = (struct pbuf_custom*)p;
  451. LWIP_MEMPOOL_FREE(RX_POOL, custom_pbuf);
  452. /* If the Rx Buffer Pool was exhausted, signal the ethernetif_input task to
  453. * call HAL_ETH_GetRxDataBuffer to rebuild the Rx descriptors. */
  454. if (RxAllocStatus == RX_ALLOC_ERROR)
  455. {
  456. RxAllocStatus = RX_ALLOC_OK;
  457. osSemaphoreRelease(RxPktSemaphore);
  458. }
  459. }
  460. /* USER CODE BEGIN 6 */
  461. /**
  462. * @brief Returns the current time in milliseconds
  463. * when LWIP_TIMERS == 1 and NO_SYS == 1
  464. * @param None
  465. * @retval Time
  466. */
  467. u32_t sys_now(void)
  468. {
  469. return HAL_GetTick();
  470. }
  471. /* USER CODE END 6 */
  472. /**
  473. * @brief Initializes the ETH MSP.
  474. * @param ethHandle: ETH handle
  475. * @retval None
  476. */
  477. void HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle)
  478. {
  479. GPIO_InitTypeDef GPIO_InitStruct = {0};
  480. if(ethHandle->Instance==ETH)
  481. {
  482. /* USER CODE BEGIN ETH_MspInit 0 */
  483. /* USER CODE END ETH_MspInit 0 */
  484. /* Enable Peripheral clock */
  485. __HAL_RCC_ETH_CLK_ENABLE();
  486. __HAL_RCC_GPIOC_CLK_ENABLE();
  487. __HAL_RCC_GPIOA_CLK_ENABLE();
  488. __HAL_RCC_GPIOB_CLK_ENABLE();
  489. /**ETH GPIO Configuration
  490. PC1 ------> ETH_MDC
  491. PA1 ------> ETH_REF_CLK
  492. PA2 ------> ETH_MDIO
  493. PA7 ------> ETH_CRS_DV
  494. PC4 ------> ETH_RXD0
  495. PC5 ------> ETH_RXD1
  496. PB11 ------> ETH_TX_EN
  497. PB12 ------> ETH_TXD0
  498. PB13 ------> ETH_TXD1
  499. */
  500. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
  501. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  502. GPIO_InitStruct.Pull = GPIO_NOPULL;
  503. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  504. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  505. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  506. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7;
  507. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  508. GPIO_InitStruct.Pull = GPIO_NOPULL;
  509. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  510. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  511. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  512. GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13;
  513. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  514. GPIO_InitStruct.Pull = GPIO_NOPULL;
  515. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  516. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  517. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  518. /* Peripheral interrupt init */
  519. HAL_NVIC_SetPriority(ETH_IRQn, 5, 0);
  520. HAL_NVIC_EnableIRQ(ETH_IRQn);
  521. /* USER CODE BEGIN ETH_MspInit 1 */
  522. #define ETH_RST_Pin GPIO_PIN_3
  523. #define ETH_RST_GPIO_Port GPIOD
  524. HAL_GPIO_WritePin(ETH_RST_GPIO_Port,ETH_RST_Pin,GPIO_PIN_RESET);
  525. HAL_Delay(50);
  526. HAL_GPIO_WritePin(ETH_RST_GPIO_Port,ETH_RST_Pin,GPIO_PIN_SET);
  527. /* USER CODE END ETH_MspInit 1 */
  528. }
  529. }
  530. void HAL_ETH_MspDeInit(ETH_HandleTypeDef* ethHandle)
  531. {
  532. if(ethHandle->Instance==ETH)
  533. {
  534. /* USER CODE BEGIN ETH_MspDeInit 0 */
  535. /* USER CODE END ETH_MspDeInit 0 */
  536. /* Peripheral clock disable */
  537. __HAL_RCC_ETH_CLK_DISABLE();
  538. /**ETH GPIO Configuration
  539. PC1 ------> ETH_MDC
  540. PA1 ------> ETH_REF_CLK
  541. PA2 ------> ETH_MDIO
  542. PA7 ------> ETH_CRS_DV
  543. PC4 ------> ETH_RXD0
  544. PC5 ------> ETH_RXD1
  545. PB11 ------> ETH_TX_EN
  546. PB12 ------> ETH_TXD0
  547. PB13 ------> ETH_TXD1
  548. */
  549. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5);
  550. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7);
  551. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13);
  552. /* Peripheral interrupt Deinit*/
  553. HAL_NVIC_DisableIRQ(ETH_IRQn);
  554. /* USER CODE BEGIN ETH_MspDeInit 1 */
  555. /* USER CODE END ETH_MspDeInit 1 */
  556. }
  557. }
  558. /*******************************************************************************
  559. PHI IO Functions
  560. *******************************************************************************/
  561. /**
  562. * @brief Initializes the MDIO interface GPIO and clocks.
  563. * @param None
  564. * @retval 0 if OK, -1 if ERROR
  565. */
  566. int32_t ETH_PHY_IO_Init(void)
  567. {
  568. /* We assume that MDIO GPIO configuration is already done
  569. in the ETH_MspInit() else it should be done here
  570. */
  571. /* Configure the MDIO Clock */
  572. HAL_ETH_SetMDIOClockRange(&heth);
  573. return 0;
  574. }
  575. /**
  576. * @brief De-Initializes the MDIO interface .
  577. * @param None
  578. * @retval 0 if OK, -1 if ERROR
  579. */
  580. int32_t ETH_PHY_IO_DeInit (void)
  581. {
  582. return 0;
  583. }
  584. /**
  585. * @brief Read a PHY register through the MDIO interface.
  586. * @param DevAddr: PHY port address
  587. * @param RegAddr: PHY register address
  588. * @param pRegVal: pointer to hold the register value
  589. * @retval 0 if OK -1 if Error
  590. */
  591. int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal)
  592. {
  593. if(HAL_ETH_ReadPHYRegister(&heth, DevAddr, RegAddr, pRegVal) != HAL_OK)
  594. {
  595. return -1;
  596. }
  597. return 0;
  598. }
  599. /**
  600. * @brief Write a value to a PHY register through the MDIO interface.
  601. * @param DevAddr: PHY port address
  602. * @param RegAddr: PHY register address
  603. * @param RegVal: Value to be written
  604. * @retval 0 if OK -1 if Error
  605. */
  606. int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal)
  607. {
  608. if(HAL_ETH_WritePHYRegister(&heth, DevAddr, RegAddr, RegVal) != HAL_OK)
  609. {
  610. return -1;
  611. }
  612. return 0;
  613. }
  614. /**
  615. * @brief Get the time in millisecons used for internal PHY driver process.
  616. * @retval Time value
  617. */
  618. int32_t ETH_PHY_IO_GetTick(void)
  619. {
  620. return HAL_GetTick();
  621. }
  622. /**
  623. * @brief Check the ETH link state then update ETH driver and netif link accordingly.
  624. * @retval None
  625. */
  626. void ethernet_link_thread(void const * argument)
  627. {
  628. ETH_MACConfigTypeDef MACConf = {0};
  629. int32_t PHYLinkState = 0;
  630. uint32_t linkchanged = 0U, speed = 0U, duplex = 0U;
  631. struct netif *netif = (struct netif *) argument;
  632. /* USER CODE BEGIN ETH link init */
  633. netif->link_callback(netif);
  634. /* USER CODE END ETH link init */
  635. for(;;)
  636. {
  637. PHYLinkState = LAN8742_GetLinkState(&LAN8742);
  638. if(netif_is_link_up(netif) && (PHYLinkState <= LAN8742_STATUS_LINK_DOWN))
  639. {
  640. HAL_ETH_Stop_IT(&heth);
  641. netif_set_down(netif);
  642. netif_set_link_down(netif);
  643. }
  644. else if(!netif_is_link_up(netif) && (PHYLinkState > LAN8742_STATUS_LINK_DOWN))
  645. {
  646. switch (PHYLinkState)
  647. {
  648. case LAN8742_STATUS_100MBITS_FULLDUPLEX:
  649. duplex = ETH_FULLDUPLEX_MODE;
  650. speed = ETH_SPEED_100M;
  651. linkchanged = 1;
  652. break;
  653. case LAN8742_STATUS_100MBITS_HALFDUPLEX:
  654. duplex = ETH_HALFDUPLEX_MODE;
  655. speed = ETH_SPEED_100M;
  656. linkchanged = 1;
  657. break;
  658. case LAN8742_STATUS_10MBITS_FULLDUPLEX:
  659. duplex = ETH_FULLDUPLEX_MODE;
  660. speed = ETH_SPEED_10M;
  661. linkchanged = 1;
  662. break;
  663. case LAN8742_STATUS_10MBITS_HALFDUPLEX:
  664. duplex = ETH_HALFDUPLEX_MODE;
  665. speed = ETH_SPEED_10M;
  666. linkchanged = 1;
  667. break;
  668. default:
  669. break;
  670. }
  671. if(linkchanged)
  672. {
  673. /* Get MAC Config MAC */
  674. HAL_ETH_GetMACConfig(&heth, &MACConf);
  675. MACConf.DuplexMode = duplex;
  676. MACConf.Speed = speed;
  677. HAL_ETH_SetMACConfig(&heth, &MACConf);
  678. HAL_ETH_Start_IT(&heth);
  679. netif_set_up(netif);
  680. netif_set_link_up(netif);
  681. }
  682. }
  683. /* USER CODE BEGIN ETH link Thread core code for User BSP */
  684. /* USER CODE END ETH link Thread core code for User BSP */
  685. osDelay(100);
  686. }
  687. }
  688. void HAL_ETH_RxAllocateCallback(uint8_t **buff)
  689. {
  690. /* USER CODE BEGIN HAL ETH RxAllocateCallback */
  691. struct pbuf_custom *p = LWIP_MEMPOOL_ALLOC(RX_POOL);
  692. if (p)
  693. {
  694. /* Get the buff from the struct pbuf address. */
  695. *buff = (uint8_t *)p + offsetof(RxBuff_t, buff);
  696. p->custom_free_function = pbuf_free_custom;
  697. /* Initialize the struct pbuf.
  698. * This must be performed whenever a buffer's allocated because it may be
  699. * changed by lwIP or the app, e.g., pbuf_free decrements ref. */
  700. pbuf_alloced_custom(PBUF_RAW, 0, PBUF_REF, p, *buff, ETH_RX_BUF_SIZE);
  701. }
  702. else
  703. {
  704. RxAllocStatus = RX_ALLOC_ERROR;
  705. *buff = NULL;
  706. }
  707. /* USER CODE END HAL ETH RxAllocateCallback */
  708. }
  709. void HAL_ETH_RxLinkCallback(void **pStart, void **pEnd, uint8_t *buff, uint16_t Length)
  710. {
  711. /* USER CODE BEGIN HAL ETH RxLinkCallback */
  712. struct pbuf **ppStart = (struct pbuf **)pStart;
  713. struct pbuf **ppEnd = (struct pbuf **)pEnd;
  714. struct pbuf *p = NULL;
  715. /* Get the struct pbuf from the buff address. */
  716. p = (struct pbuf *)(buff - offsetof(RxBuff_t, buff));
  717. p->next = NULL;
  718. p->tot_len = 0;
  719. p->len = Length;
  720. /* Chain the buffer. */
  721. if (!*ppStart)
  722. {
  723. /* The first buffer of the packet. */
  724. *ppStart = p;
  725. }
  726. else
  727. {
  728. /* Chain the buffer to the end of the packet. */
  729. (*ppEnd)->next = p;
  730. }
  731. *ppEnd = p;
  732. /* Update the total length of all the buffers of the chain. Each pbuf in the chain should have its tot_len
  733. * set to its own length, plus the length of all the following pbufs in the chain. */
  734. for (p = *ppStart; p != NULL; p = p->next)
  735. {
  736. p->tot_len += Length;
  737. }
  738. /* USER CODE END HAL ETH RxLinkCallback */
  739. }
  740. void HAL_ETH_TxFreeCallback(uint32_t * buff)
  741. {
  742. /* USER CODE BEGIN HAL ETH TxFreeCallback */
  743. pbuf_free((struct pbuf *)buff);
  744. /* USER CODE END HAL ETH TxFreeCallback */
  745. }
  746. /* USER CODE BEGIN 8 */
  747. /**
  748. * @brief This function notify user about link status changement.
  749. * @param netif: the network interface
  750. * @retval None
  751. */
  752. __weak void ethernetif_notify_conn_changed(struct netif *netif)
  753. {
  754. /* NOTE : This is function could be implemented in user file
  755. when the callback is needed,
  756. */
  757. }
  758. /* USER CODE END 8 */