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.

889 lines
24 KiB

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