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.

900 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
1 year ago
2 years ago
1 year 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. // 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_StatusTypeDef halecode = HAL_ETH_Transmit_IT(&heth, &TxConfig);
  324. if (halecode == HAL_OK) {
  325. if(osSemaphoreWait(TxPktSemaphore, 10) != osOK){
  326. errval = ERR_TIMEOUT;
  327. }else{
  328. }
  329. } else {
  330. errval = ERR_TIMEOUT;
  331. }
  332. HAL_ETH_ReleaseTxPacket(&heth);
  333. // printf("low_level_output: TX.....end\n");
  334. return errval;
  335. }
  336. /**
  337. * @brief Should allocate a pbuf and transfer the bytes of the incoming
  338. * packet from the interface into the pbuf.
  339. *
  340. * @param netif the lwip network interface structure for this ethernetif
  341. * @return a pbuf filled with the received packet (including MAC header)
  342. * NULL on memory error
  343. */
  344. static struct pbuf * low_level_input(struct netif *netif)
  345. {
  346. struct pbuf *p = NULL;
  347. if(RxAllocStatus == RX_ALLOC_OK)
  348. {
  349. HAL_ETH_ReadData(&heth, (void **)&p);
  350. }
  351. return p;
  352. }
  353. /**
  354. * @brief This function should be called when a packet is ready to be read
  355. * from the interface. It uses the function low_level_input() that
  356. * should handle the actual reception of bytes from the network
  357. * interface. Then the type of the received packet is determined and
  358. * the appropriate input function is called.
  359. *
  360. * @param netif the lwip network interface structure for this ethernetif
  361. */
  362. static void ethernetif_input(void const * argument)
  363. {
  364. struct pbuf *p = NULL;
  365. struct netif *netif = (struct netif *) argument;
  366. for( ;; )
  367. {
  368. if (osSemaphoreWait(RxPktSemaphore, TIME_WAITING_FOR_INPUT) == osOK)
  369. {
  370. do
  371. {
  372. p = low_level_input( netif );
  373. if (p != NULL)
  374. {
  375. if (netif->input( p, netif) != ERR_OK )
  376. {
  377. pbuf_free(p);
  378. }
  379. }
  380. } while(p!=NULL);
  381. }
  382. }
  383. }
  384. #if !LWIP_ARP
  385. /**
  386. * This function has to be completed by user in case of ARP OFF.
  387. *
  388. * @param netif the lwip network interface structure for this ethernetif
  389. * @return ERR_OK if ...
  390. */
  391. static err_t low_level_output_arp_off(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr)
  392. {
  393. err_t errval;
  394. errval = ERR_OK;
  395. /* USER CODE BEGIN 5 */
  396. /* USER CODE END 5 */
  397. return errval;
  398. }
  399. #endif /* LWIP_ARP */
  400. /**
  401. * @brief Should be called at the beginning of the program to set up the
  402. * network interface. It calls the function low_level_init() to do the
  403. * actual setup of the hardware.
  404. *
  405. * This function should be passed as a parameter to netif_add().
  406. *
  407. * @param netif the lwip network interface structure for this ethernetif
  408. * @return ERR_OK if the loopif is initialized
  409. * ERR_MEM if private data couldn't be allocated
  410. * any other err_t on error
  411. */
  412. err_t ethernetif_init(struct netif *netif)
  413. {
  414. LWIP_ASSERT("netif != NULL", (netif != NULL));
  415. #if LWIP_NETIF_HOSTNAME
  416. /* Initialize interface hostname */
  417. netif->hostname = "lwip";
  418. #endif /* LWIP_NETIF_HOSTNAME */
  419. /*
  420. * Initialize the snmp variables and counters inside the struct netif.
  421. * The last argument should be replaced with your link speed, in units
  422. * of bits per second.
  423. */
  424. // MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
  425. netif->name[0] = IFNAME0;
  426. netif->name[1] = IFNAME1;
  427. /* We directly use etharp_output() here to save a function call.
  428. * You can instead declare your own function an call etharp_output()
  429. * from it if you have to do some checks before sending (e.g. if link
  430. * is available...) */
  431. #if LWIP_IPV4
  432. #if LWIP_ARP || LWIP_ETHERNET
  433. #if LWIP_ARP
  434. netif->output = etharp_output;
  435. #else
  436. /* The user should write its own code in low_level_output_arp_off function */
  437. netif->output = low_level_output_arp_off;
  438. #endif /* LWIP_ARP */
  439. #endif /* LWIP_ARP || LWIP_ETHERNET */
  440. #endif /* LWIP_IPV4 */
  441. #if LWIP_IPV6
  442. netif->output_ip6 = ethip6_output;
  443. #endif /* LWIP_IPV6 */
  444. netif->linkoutput = low_level_output;
  445. /* initialize the hardware */
  446. low_level_init(netif);
  447. return ERR_OK;
  448. }
  449. /**
  450. * @brief Custom Rx pbuf free callback
  451. * @param pbuf: pbuf to be freed
  452. * @retval None
  453. */
  454. void pbuf_free_custom(struct pbuf *p)
  455. {
  456. struct pbuf_custom* custom_pbuf = (struct pbuf_custom*)p;
  457. LWIP_MEMPOOL_FREE(RX_POOL, custom_pbuf);
  458. /* If the Rx Buffer Pool was exhausted, signal the ethernetif_input task to
  459. * call HAL_ETH_GetRxDataBuffer to rebuild the Rx descriptors. */
  460. if (RxAllocStatus == RX_ALLOC_ERROR)
  461. {
  462. RxAllocStatus = RX_ALLOC_OK;
  463. osSemaphoreRelease(RxPktSemaphore);
  464. }
  465. }
  466. /* USER CODE BEGIN 6 */
  467. /**
  468. * @brief Returns the current time in milliseconds
  469. * when LWIP_TIMERS == 1 and NO_SYS == 1
  470. * @param None
  471. * @retval Time
  472. */
  473. u32_t sys_now(void)
  474. {
  475. return HAL_GetTick();
  476. }
  477. /* USER CODE END 6 */
  478. /**
  479. * @brief Initializes the ETH MSP.
  480. * @param ethHandle: ETH handle
  481. * @retval None
  482. */
  483. void HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle)
  484. {
  485. GPIO_InitTypeDef GPIO_InitStruct = {0};
  486. if(ethHandle->Instance==ETH)
  487. {
  488. /* USER CODE BEGIN ETH_MspInit 0 */
  489. /* USER CODE END ETH_MspInit 0 */
  490. /* Enable Peripheral clock */
  491. __HAL_RCC_ETH_CLK_ENABLE();
  492. __HAL_RCC_GPIOC_CLK_ENABLE();
  493. __HAL_RCC_GPIOA_CLK_ENABLE();
  494. __HAL_RCC_GPIOB_CLK_ENABLE();
  495. /**ETH GPIO Configuration
  496. PC1 ------> ETH_MDC
  497. PA1 ------> ETH_REF_CLK
  498. PA2 ------> ETH_MDIO
  499. PA7 ------> ETH_CRS_DV
  500. PC4 ------> ETH_RXD0
  501. PC5 ------> ETH_RXD1
  502. PB11 ------> ETH_TX_EN
  503. PB12 ------> ETH_TXD0
  504. PB13 ------> ETH_TXD1
  505. */
  506. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
  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(GPIOC, &GPIO_InitStruct);
  512. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7;
  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(GPIOA, &GPIO_InitStruct);
  518. GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13;
  519. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  520. GPIO_InitStruct.Pull = GPIO_NOPULL;
  521. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  522. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  523. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  524. /* Peripheral interrupt init */
  525. HAL_NVIC_SetPriority(ETH_IRQn, 5, 0);
  526. HAL_NVIC_EnableIRQ(ETH_IRQn);
  527. /* USER CODE BEGIN ETH_MspInit 1 */
  528. #define ETH_RST_Pin GPIO_PIN_3
  529. #define ETH_RST_GPIO_Port GPIOD
  530. HAL_GPIO_WritePin(ETH_RST_GPIO_Port,ETH_RST_Pin,GPIO_PIN_RESET);
  531. HAL_Delay(50);
  532. HAL_GPIO_WritePin(ETH_RST_GPIO_Port,ETH_RST_Pin,GPIO_PIN_SET);
  533. /* USER CODE END ETH_MspInit 1 */
  534. }
  535. }
  536. void HAL_ETH_MspDeInit(ETH_HandleTypeDef* ethHandle)
  537. {
  538. if(ethHandle->Instance==ETH)
  539. {
  540. /* USER CODE BEGIN ETH_MspDeInit 0 */
  541. /* USER CODE END ETH_MspDeInit 0 */
  542. /* Peripheral clock disable */
  543. __HAL_RCC_ETH_CLK_DISABLE();
  544. /**ETH GPIO Configuration
  545. PC1 ------> ETH_MDC
  546. PA1 ------> ETH_REF_CLK
  547. PA2 ------> ETH_MDIO
  548. PA7 ------> ETH_CRS_DV
  549. PC4 ------> ETH_RXD0
  550. PC5 ------> ETH_RXD1
  551. PB11 ------> ETH_TX_EN
  552. PB12 ------> ETH_TXD0
  553. PB13 ------> ETH_TXD1
  554. */
  555. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5);
  556. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7);
  557. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13);
  558. /* Peripheral interrupt Deinit*/
  559. HAL_NVIC_DisableIRQ(ETH_IRQn);
  560. /* USER CODE BEGIN ETH_MspDeInit 1 */
  561. /* USER CODE END ETH_MspDeInit 1 */
  562. }
  563. }
  564. /*******************************************************************************
  565. PHI IO Functions
  566. *******************************************************************************/
  567. /**
  568. * @brief Initializes the MDIO interface GPIO and clocks.
  569. * @param None
  570. * @retval 0 if OK, -1 if ERROR
  571. */
  572. int32_t ETH_PHY_IO_Init(void)
  573. {
  574. /* We assume that MDIO GPIO configuration is already done
  575. in the ETH_MspInit() else it should be done here
  576. */
  577. /* Configure the MDIO Clock */
  578. HAL_ETH_SetMDIOClockRange(&heth);
  579. return 0;
  580. }
  581. /**
  582. * @brief De-Initializes the MDIO interface .
  583. * @param None
  584. * @retval 0 if OK, -1 if ERROR
  585. */
  586. int32_t ETH_PHY_IO_DeInit (void)
  587. {
  588. return 0;
  589. }
  590. /**
  591. * @brief Read a PHY register through the MDIO interface.
  592. * @param DevAddr: PHY port address
  593. * @param RegAddr: PHY register address
  594. * @param pRegVal: pointer to hold the register value
  595. * @retval 0 if OK -1 if Error
  596. */
  597. int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal)
  598. {
  599. if(HAL_ETH_ReadPHYRegister(&heth, DevAddr, RegAddr, pRegVal) != HAL_OK)
  600. {
  601. return -1;
  602. }
  603. return 0;
  604. }
  605. /**
  606. * @brief Write a value to a PHY register through the MDIO interface.
  607. * @param DevAddr: PHY port address
  608. * @param RegAddr: PHY register address
  609. * @param RegVal: Value to be written
  610. * @retval 0 if OK -1 if Error
  611. */
  612. int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal)
  613. {
  614. if(HAL_ETH_WritePHYRegister(&heth, DevAddr, RegAddr, RegVal) != HAL_OK)
  615. {
  616. return -1;
  617. }
  618. return 0;
  619. }
  620. /**
  621. * @brief Get the time in millisecons used for internal PHY driver process.
  622. * @retval Time value
  623. */
  624. int32_t ETH_PHY_IO_GetTick(void)
  625. {
  626. return HAL_GetTick();
  627. }
  628. /**
  629. * @brief Check the ETH link state then update ETH driver and netif link accordingly.
  630. * @retval None
  631. */
  632. void ethernet_link_thread(void const * argument)
  633. {
  634. ETH_MACConfigTypeDef MACConf = {0};
  635. int32_t PHYLinkState = 0;
  636. uint32_t linkchanged = 0U, speed = 0U, duplex = 0U;
  637. struct netif *netif = (struct netif *) argument;
  638. /* USER CODE BEGIN ETH link init */
  639. netif->link_callback(netif);
  640. /* USER CODE END ETH link init */
  641. for(;;)
  642. {
  643. PHYLinkState = LAN8742_GetLinkState(&LAN8742);
  644. if(netif_is_link_up(netif) && (PHYLinkState <= LAN8742_STATUS_LINK_DOWN))
  645. {
  646. HAL_ETH_Stop_IT(&heth);
  647. netif_set_down(netif);
  648. netif_set_link_down(netif);
  649. }
  650. else if(!netif_is_link_up(netif) && (PHYLinkState > LAN8742_STATUS_LINK_DOWN))
  651. {
  652. switch (PHYLinkState)
  653. {
  654. case LAN8742_STATUS_100MBITS_FULLDUPLEX:
  655. duplex = ETH_FULLDUPLEX_MODE;
  656. speed = ETH_SPEED_100M;
  657. linkchanged = 1;
  658. break;
  659. case LAN8742_STATUS_100MBITS_HALFDUPLEX:
  660. duplex = ETH_HALFDUPLEX_MODE;
  661. speed = ETH_SPEED_100M;
  662. linkchanged = 1;
  663. break;
  664. case LAN8742_STATUS_10MBITS_FULLDUPLEX:
  665. duplex = ETH_FULLDUPLEX_MODE;
  666. speed = ETH_SPEED_10M;
  667. linkchanged = 1;
  668. break;
  669. case LAN8742_STATUS_10MBITS_HALFDUPLEX:
  670. duplex = ETH_HALFDUPLEX_MODE;
  671. speed = ETH_SPEED_10M;
  672. linkchanged = 1;
  673. break;
  674. default:
  675. break;
  676. }
  677. if(linkchanged)
  678. {
  679. /* Get MAC Config MAC */
  680. HAL_ETH_GetMACConfig(&heth, &MACConf);
  681. MACConf.DuplexMode = duplex;
  682. MACConf.Speed = speed;
  683. HAL_ETH_SetMACConfig(&heth, &MACConf);
  684. HAL_ETH_Start_IT(&heth);
  685. netif_set_up(netif);
  686. netif_set_link_up(netif);
  687. }
  688. }
  689. /* USER CODE BEGIN ETH link Thread core code for User BSP */
  690. /* USER CODE END ETH link Thread core code for User BSP */
  691. osDelay(100);
  692. }
  693. }
  694. void HAL_ETH_RxAllocateCallback(uint8_t **buff)
  695. {
  696. /* USER CODE BEGIN HAL ETH RxAllocateCallback */
  697. struct pbuf_custom *p = LWIP_MEMPOOL_ALLOC(RX_POOL);
  698. if (p)
  699. {
  700. /* Get the buff from the struct pbuf address. */
  701. *buff = (uint8_t *)p + offsetof(RxBuff_t, buff);
  702. p->custom_free_function = pbuf_free_custom;
  703. /* Initialize the struct pbuf.
  704. * This must be performed whenever a buffer's allocated because it may be
  705. * changed by lwIP or the app, e.g., pbuf_free decrements ref. */
  706. pbuf_alloced_custom(PBUF_RAW, 0, PBUF_REF, p, *buff, ETH_RX_BUF_SIZE);
  707. }
  708. else
  709. {
  710. RxAllocStatus = RX_ALLOC_ERROR;
  711. *buff = NULL;
  712. }
  713. /* USER CODE END HAL ETH RxAllocateCallback */
  714. }
  715. void HAL_ETH_RxLinkCallback(void **pStart, void **pEnd, uint8_t *buff, uint16_t Length)
  716. {
  717. /* USER CODE BEGIN HAL ETH RxLinkCallback */
  718. struct pbuf **ppStart = (struct pbuf **)pStart;
  719. struct pbuf **ppEnd = (struct pbuf **)pEnd;
  720. struct pbuf *p = NULL;
  721. /* Get the struct pbuf from the buff address. */
  722. p = (struct pbuf *)(buff - offsetof(RxBuff_t, buff));
  723. p->next = NULL;
  724. p->tot_len = 0;
  725. p->len = Length;
  726. /* Chain the buffer. */
  727. if (!*ppStart)
  728. {
  729. /* The first buffer of the packet. */
  730. *ppStart = p;
  731. }
  732. else
  733. {
  734. /* Chain the buffer to the end of the packet. */
  735. (*ppEnd)->next = p;
  736. }
  737. *ppEnd = p;
  738. /* Update the total length of all the buffers of the chain. Each pbuf in the chain should have its tot_len
  739. * set to its own length, plus the length of all the following pbufs in the chain. */
  740. for (p = *ppStart; p != NULL; p = p->next)
  741. {
  742. p->tot_len += Length;
  743. }
  744. /* USER CODE END HAL ETH RxLinkCallback */
  745. }
  746. void HAL_ETH_TxFreeCallback(uint32_t * buff)
  747. {
  748. /* USER CODE BEGIN HAL ETH TxFreeCallback */
  749. pbuf_free((struct pbuf *)buff);
  750. /* USER CODE END HAL ETH TxFreeCallback */
  751. }
  752. /* USER CODE BEGIN 8 */
  753. /**
  754. * @brief This function notify user about link status changement.
  755. * @param netif: the network interface
  756. * @retval None
  757. */
  758. __weak void ethernetif_notify_conn_changed(struct netif *netif)
  759. {
  760. /* NOTE : This is function could be implemented in user file
  761. when the callback is needed,
  762. */
  763. }
  764. /* USER CODE END 8 */