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.

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