drv_eth.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-19 SummerGift first version
  9. * 2018-12-25 zylx fix some bugs
  10. * 2019-06-10 SummerGift optimize PHY state detection process
  11. * 2019-09-03 xiaofan optimize link change detection process
  12. * 2020-07-17 wanghaijing support h7
  13. * 2020-11-30 wanghaijing add phy reset
  14. */
  15. #include<rtthread.h>
  16. #include<rtdevice.h>
  17. #include "board.h"
  18. #include "drv_config.h"
  19. #ifdef BSP_USING_ETH_ARTPI
  20. #include <netif/ethernetif.h>
  21. #include "lwipopts.h"
  22. #include "drv_eth.h"
  23. /*
  24. * Emac driver uses CubeMX tool to generate emac and phy's configuration,
  25. * the configuration files can be found in CubeMX_Config folder.
  26. */
  27. /* debug option */
  28. #define LOG_TAG "drv.emac"
  29. #include <drv_log.h>
  30. #define MAX_ADDR_LEN 6
  31. struct rt_stm32_eth
  32. {
  33. /* inherit from ethernet device */
  34. struct eth_device parent;
  35. #ifndef PHY_USING_INTERRUPT_MODE
  36. rt_timer_t poll_link_timer;
  37. #endif
  38. /* interface address info, hw address */
  39. rt_uint8_t dev_addr[MAX_ADDR_LEN];
  40. /* ETH_Speed */
  41. uint32_t ETH_Speed;
  42. /* ETH_Duplex_Mode */
  43. uint32_t ETH_Mode;
  44. };
  45. static ETH_HandleTypeDef EthHandle;
  46. static ETH_TxPacketConfig TxConfig;
  47. static struct rt_stm32_eth stm32_eth_device;
  48. static uint8_t PHY_ADDR = 0x1F;
  49. static rt_uint32_t reset_pin = 0;
  50. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  51. #pragma location=0x30040000
  52. ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
  53. #pragma location=0x30040060
  54. ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
  55. #pragma location=0x30040200
  56. uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_MAX_PACKET_SIZE]; /* Ethernet Receive Buffers */
  57. #elif defined ( __CC_ARM ) /* MDK ARM Compiler */
  58. __attribute__((at(0x30040000))) ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
  59. __attribute__((at(0x30040060))) ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
  60. __attribute__((at(0x30040200))) uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_MAX_PACKET_SIZE]; /* Ethernet Receive Buffer */
  61. #elif defined ( __GNUC__ ) /* GNU Compiler */
  62. ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT] __attribute__((section(".RxDecripSection"))); /* Ethernet Rx DMA Descriptors */
  63. ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] __attribute__((section(".TxDecripSection"))); /* Ethernet Tx DMA Descriptors */
  64. uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_MAX_PACKET_SIZE] __attribute__((section(".RxArraySection"))); /* Ethernet Receive Buffers */
  65. #endif
  66. #if defined(ETH_RX_DUMP) || defined(ETH_TX_DUMP)
  67. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  68. static void dump_hex(const rt_uint8_t *ptr, rt_size_t buflen)
  69. {
  70. unsigned char *buf = (unsigned char *)ptr;
  71. int i, j;
  72. for (i = 0; i < buflen; i += 16)
  73. {
  74. rt_kprintf("%08X: ", i);
  75. for (j = 0; j < 16; j++)
  76. if (i + j < buflen)
  77. rt_kprintf("%02X ", buf[i + j]);
  78. else
  79. rt_kprintf(" ");
  80. rt_kprintf(" ");
  81. for (j = 0; j < 16; j++)
  82. if (i + j < buflen)
  83. rt_kprintf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
  84. rt_kprintf("\n");
  85. }
  86. }
  87. #endif
  88. static void phy_reset(void)
  89. {
  90. rt_pin_write(reset_pin, PIN_LOW);
  91. rt_thread_mdelay(50);
  92. rt_pin_write(reset_pin, PIN_HIGH);
  93. }
  94. /* EMAC initialization function */
  95. static rt_err_t rt_stm32_eth_init(rt_device_t dev)
  96. {
  97. ETH_MACConfigTypeDef MACConf;
  98. uint32_t regvalue = 0;
  99. uint8_t status = RT_EOK;
  100. __HAL_RCC_D2SRAM3_CLK_ENABLE();
  101. phy_reset();
  102. /* ETHERNET Configuration */
  103. EthHandle.Instance = ETH;
  104. EthHandle.Init.MACAddr = (rt_uint8_t *)&stm32_eth_device.dev_addr[0];
  105. EthHandle.Init.MediaInterface = HAL_ETH_RMII_MODE;
  106. EthHandle.Init.TxDesc = DMATxDscrTab;
  107. EthHandle.Init.RxDesc = DMARxDscrTab;
  108. EthHandle.Init.RxBuffLen = ETH_MAX_PACKET_SIZE;
  109. SCB_InvalidateDCache();
  110. HAL_ETH_DeInit(&EthHandle);
  111. /* configure ethernet peripheral (GPIOs, clocks, MAC, DMA) */
  112. if (HAL_ETH_Init(&EthHandle) != HAL_OK)
  113. {
  114. LOG_E("eth hardware init failed");
  115. }
  116. else
  117. {
  118. LOG_D("eth hardware init success");
  119. }
  120. rt_memset(&TxConfig, 0, sizeof(ETH_TxPacketConfig));
  121. TxConfig.Attributes = ETH_TX_PACKETS_FEATURES_CSUM | ETH_TX_PACKETS_FEATURES_CRCPAD;
  122. TxConfig.ChecksumCtrl = ETH_CHECKSUM_IPHDR_PAYLOAD_INSERT_PHDR_CALC;
  123. TxConfig.CRCPadCtrl = ETH_CRC_PAD_INSERT;
  124. for (int idx = 0; idx < ETH_RX_DESC_CNT; idx++)
  125. {
  126. HAL_ETH_DescAssignMemory(&EthHandle, idx, &Rx_Buff[idx][0], NULL);
  127. }
  128. HAL_ETH_SetMDIOClockRange(&EthHandle);
  129. for(int i = 0; i <= PHY_ADDR; i ++)
  130. {
  131. if(HAL_ETH_ReadPHYRegister(&EthHandle, i, PHY_SPECIAL_MODES_REG, &regvalue) != HAL_OK)
  132. {
  133. status = RT_ERROR;
  134. /* Can't read from this device address continue with next address */
  135. continue;
  136. }
  137. if((regvalue & PHY_BASIC_STATUS_REG) == i)
  138. {
  139. PHY_ADDR = i;
  140. status = RT_EOK;
  141. LOG_D("Found a phy, address:0x%02X", PHY_ADDR);
  142. break;
  143. }
  144. }
  145. if(HAL_ETH_WritePHYRegister(&EthHandle, PHY_ADDR, PHY_BASIC_CONTROL_REG, PHY_RESET_MASK) == HAL_OK)
  146. {
  147. HAL_ETH_ReadPHYRegister(&EthHandle, PHY_ADDR, PHY_SPECIAL_MODES_REG, &regvalue);
  148. uint32_t tickstart = rt_tick_get();
  149. /* wait until software reset is done or timeout occured */
  150. while(regvalue & PHY_RESET_MASK)
  151. {
  152. if((rt_tick_get() - tickstart) <= 500)
  153. {
  154. if(HAL_ETH_ReadPHYRegister(&EthHandle, PHY_ADDR, PHY_BASIC_CONTROL_REG, &regvalue) != HAL_OK)
  155. {
  156. status = RT_ERROR;
  157. break;
  158. }
  159. }
  160. else
  161. {
  162. status = RT_ETIMEOUT;
  163. }
  164. }
  165. }
  166. rt_thread_delay(2000);
  167. if(HAL_ETH_ReadPHYRegister(&EthHandle, PHY_ADDR, PHY_BASIC_CONTROL_REG, &regvalue) == HAL_OK)
  168. {
  169. regvalue |= PHY_AUTO_NEGOTIATION_MASK;
  170. HAL_ETH_WritePHYRegister(&EthHandle, PHY_ADDR, PHY_BASIC_CONTROL_REG, regvalue);
  171. eth_device_linkchange(&stm32_eth_device.parent, RT_TRUE);
  172. HAL_ETH_GetMACConfig(&EthHandle, &MACConf);
  173. MACConf.DuplexMode = ETH_FULLDUPLEX_MODE;
  174. MACConf.Speed = ETH_SPEED_100M;
  175. HAL_ETH_SetMACConfig(&EthHandle, &MACConf);
  176. HAL_ETH_Start_IT(&EthHandle);
  177. }
  178. else
  179. {
  180. status = RT_ERROR;
  181. }
  182. return status;
  183. }
  184. static rt_err_t rt_stm32_eth_open(rt_device_t dev, rt_uint16_t oflag)
  185. {
  186. LOG_D("emac open");
  187. return RT_EOK;
  188. }
  189. static rt_err_t rt_stm32_eth_close(rt_device_t dev)
  190. {
  191. LOG_D("emac close");
  192. return RT_EOK;
  193. }
  194. static rt_size_t rt_stm32_eth_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  195. {
  196. LOG_D("emac read");
  197. rt_set_errno(-RT_ENOSYS);
  198. return 0;
  199. }
  200. static rt_size_t rt_stm32_eth_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  201. {
  202. LOG_D("emac write");
  203. rt_set_errno(-RT_ENOSYS);
  204. return 0;
  205. }
  206. static rt_err_t rt_stm32_eth_control(rt_device_t dev, int cmd, void *args)
  207. {
  208. switch (cmd)
  209. {
  210. case NIOCTL_GADDR:
  211. /* get mac address */
  212. if (args) rt_memcpy(args, stm32_eth_device.dev_addr, 6);
  213. else return -RT_ERROR;
  214. break;
  215. default :
  216. break;
  217. }
  218. return RT_EOK;
  219. }
  220. /* ethernet device interface */
  221. /* transmit data*/
  222. rt_err_t rt_stm32_eth_tx(rt_device_t dev, struct pbuf *p)
  223. {
  224. rt_err_t ret = RT_ERROR;
  225. HAL_StatusTypeDef state;
  226. uint32_t i = 0, framelen = 0;
  227. struct pbuf *q;
  228. ETH_BufferTypeDef Txbuffer[ETH_TX_DESC_CNT];
  229. rt_memset(Txbuffer, 0, ETH_TX_DESC_CNT * sizeof(ETH_BufferTypeDef));
  230. for (q = p; q != NULL; q = q->next)
  231. {
  232. if (i >= ETH_TX_DESC_CNT)
  233. return ERR_IF;
  234. Txbuffer[i].buffer = q->payload;
  235. Txbuffer[i].len = q->len;
  236. framelen += q->len;
  237. if (i > 0)
  238. {
  239. Txbuffer[i - 1].next = &Txbuffer[i];
  240. }
  241. if (q->next == NULL)
  242. {
  243. Txbuffer[i].next = NULL;
  244. }
  245. i++;
  246. }
  247. TxConfig.Length = framelen;
  248. TxConfig.TxBuffer = Txbuffer;
  249. #ifdef ETH_TX_DUMP
  250. rt_kprintf("Tx dump, len= %d\r\n", framelen);
  251. dump_hex(&Txbuffer[0]);
  252. #endif
  253. if (stm32_eth_device.parent.link_status)
  254. {
  255. SCB_CleanInvalidateDCache();
  256. state = HAL_ETH_Transmit(&EthHandle, &TxConfig, 1000);
  257. if (state != HAL_OK)
  258. {
  259. LOG_W("eth transmit frame faild: %d", EthHandle.ErrorCode);
  260. EthHandle.ErrorCode = HAL_ETH_STATE_READY;
  261. EthHandle.gState = HAL_ETH_STATE_READY;
  262. }
  263. }
  264. else
  265. {
  266. LOG_E("eth transmit frame faild, netif not up");
  267. }
  268. ret = ERR_OK;
  269. return ret;
  270. }
  271. /* receive data*/
  272. struct pbuf *rt_stm32_eth_rx(rt_device_t dev)
  273. {
  274. uint32_t framelength = 0;
  275. rt_uint16_t l;
  276. struct pbuf *p = RT_NULL, *q;
  277. ETH_BufferTypeDef RxBuff;
  278. uint32_t alignedAddr;
  279. if(HAL_ETH_GetRxDataBuffer(&EthHandle, &RxBuff) == HAL_OK)
  280. {
  281. HAL_ETH_GetRxDataLength(&EthHandle, &framelength);
  282. /* Build Rx descriptor to be ready for next data reception */
  283. HAL_ETH_BuildRxDescriptors(&EthHandle);
  284. /* Invalidate data cache for ETH Rx Buffers */
  285. alignedAddr = (uint32_t)RxBuff.buffer & ~0x1F;
  286. SCB_InvalidateDCache_by_Addr((uint32_t *)alignedAddr, (uint32_t)RxBuff.buffer - alignedAddr + framelength);
  287. p = pbuf_alloc(PBUF_RAW, framelength, PBUF_RAM);
  288. if (p != NULL)
  289. {
  290. for (q = p, l = 0; q != NULL; q = q->next)
  291. {
  292. memcpy((rt_uint8_t *)q->payload, (rt_uint8_t *)&RxBuff.buffer[l], q->len);
  293. l = l + q->len;
  294. }
  295. }
  296. }
  297. return p;
  298. }
  299. /* interrupt service routine */
  300. void ETH_IRQHandler(void)
  301. {
  302. /* enter interrupt */
  303. rt_interrupt_enter();
  304. HAL_ETH_IRQHandler(&EthHandle);
  305. /* leave interrupt */
  306. rt_interrupt_leave();
  307. }
  308. void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
  309. {
  310. rt_err_t result;
  311. result = eth_device_ready(&(stm32_eth_device.parent));
  312. if (result != RT_EOK)
  313. LOG_I("RxCpltCallback err = %d", result);
  314. }
  315. void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *heth)
  316. {
  317. LOG_E("eth err");
  318. }
  319. enum
  320. {
  321. PHY_LINK = (1 << 0),
  322. PHY_100M = (1 << 1),
  323. PHY_FULL_DUPLEX = (1 << 2),
  324. };
  325. static void phy_linkchange()
  326. {
  327. static rt_uint8_t phy_speed = 0;
  328. rt_uint8_t phy_speed_new = 0;
  329. rt_uint32_t status;
  330. HAL_ETH_ReadPHYRegister(&EthHandle, PHY_ADDR, PHY_BASIC_STATUS_REG, (uint32_t *)&status);
  331. LOG_D("phy basic status reg is 0x%X", status);
  332. if (status & (PHY_AUTONEGO_COMPLETE_MASK | PHY_LINKED_STATUS_MASK))
  333. {
  334. rt_uint32_t SR = 0;
  335. phy_speed_new |= PHY_LINK;
  336. HAL_ETH_ReadPHYRegister(&EthHandle, PHY_ADDR, PHY_Status_REG, (uint32_t *)&SR);
  337. LOG_D("phy control status reg is 0x%X", SR);
  338. if (PHY_Status_SPEED_100M(SR))
  339. {
  340. phy_speed_new |= PHY_100M;
  341. }
  342. if (PHY_Status_FULL_DUPLEX(SR))
  343. {
  344. phy_speed_new |= PHY_FULL_DUPLEX;
  345. }
  346. }
  347. if (phy_speed != phy_speed_new)
  348. {
  349. phy_speed = phy_speed_new;
  350. if (phy_speed & PHY_LINK)
  351. {
  352. LOG_D("link up");
  353. if (phy_speed & PHY_100M)
  354. {
  355. LOG_D("100Mbps");
  356. stm32_eth_device.ETH_Speed = ETH_SPEED_100M;
  357. }
  358. else
  359. {
  360. stm32_eth_device.ETH_Speed = ETH_SPEED_10M;
  361. LOG_D("10Mbps");
  362. }
  363. if (phy_speed & PHY_FULL_DUPLEX)
  364. {
  365. LOG_D("full-duplex");
  366. stm32_eth_device.ETH_Mode = ETH_FULLDUPLEX_MODE;
  367. }
  368. else
  369. {
  370. LOG_D("half-duplex");
  371. stm32_eth_device.ETH_Mode = ETH_HALFDUPLEX_MODE;
  372. }
  373. /* send link up. */
  374. eth_device_linkchange(&stm32_eth_device.parent, RT_TRUE);
  375. }
  376. else
  377. {
  378. LOG_I("link down");
  379. eth_device_linkchange(&stm32_eth_device.parent, RT_FALSE);
  380. }
  381. }
  382. }
  383. #ifdef PHY_USING_INTERRUPT_MODE
  384. static void eth_phy_isr(void *args)
  385. {
  386. rt_uint32_t status = 0;
  387. HAL_ETH_ReadPHYRegister(&EthHandle, PHY_ADDR, PHY_INTERRUPT_FLAG_REG, (uint32_t *)&status);
  388. LOG_D("phy interrupt status reg is 0x%X", status);
  389. phy_linkchange();
  390. }
  391. #endif /* PHY_USING_INTERRUPT_MODE */
  392. static void phy_monitor_thread_entry(void *parameter)
  393. {
  394. phy_linkchange();
  395. #ifdef PHY_USING_INTERRUPT_MODE
  396. /* configuration intterrupt pin */
  397. rt_pin_mode(PHY_INT_PIN, PIN_MODE_INPUT_PULLUP);
  398. rt_pin_attach_irq(PHY_INT_PIN, PIN_IRQ_MODE_FALLING, eth_phy_isr, (void *)"callbackargs");
  399. rt_pin_irq_enable(PHY_INT_PIN, PIN_IRQ_ENABLE);
  400. /* enable phy interrupt */
  401. HAL_ETH_WritePHYRegister(&EthHandle, PHY_ADDR, PHY_INTERRUPT_MASK_REG, PHY_INT_MASK);
  402. #if defined(PHY_INTERRUPT_CTRL_REG)
  403. HAL_ETH_WritePHYRegister(&EthHandle, PHY_ADDR, PHY_INTERRUPT_CTRL_REG, PHY_INTERRUPT_EN);
  404. #endif
  405. #else /* PHY_USING_INTERRUPT_MODE */
  406. stm32_eth_device.poll_link_timer = rt_timer_create("phylnk", (void (*)(void*))phy_linkchange,
  407. NULL, RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC);
  408. if (!stm32_eth_device.poll_link_timer || rt_timer_start(stm32_eth_device.poll_link_timer) != RT_EOK)
  409. {
  410. LOG_E("Start link change detection timer failed");
  411. }
  412. #endif /* PHY_USING_INTERRUPT_MODE */
  413. }
  414. /* Register the EMAC device */
  415. static int rt_hw_stm32_eth_init(void)
  416. {
  417. rt_err_t state = RT_EOK;
  418. reset_pin = rt_pin_get(ETH_RESET_PIN);
  419. rt_pin_mode(reset_pin, PIN_MODE_OUTPUT);
  420. rt_pin_write(reset_pin, PIN_HIGH);
  421. stm32_eth_device.ETH_Speed = ETH_SPEED_100M;
  422. stm32_eth_device.ETH_Mode = ETH_FULLDUPLEX_MODE;
  423. /* OUI 00-80-E1 STMICROELECTRONICS. */
  424. stm32_eth_device.dev_addr[0] = 0x00;
  425. stm32_eth_device.dev_addr[1] = 0x80;
  426. stm32_eth_device.dev_addr[2] = 0xE1;
  427. /* generate MAC addr from 96bit unique ID (only for test). */
  428. stm32_eth_device.dev_addr[3] = *(rt_uint8_t *)(UID_BASE + 4);
  429. stm32_eth_device.dev_addr[4] = *(rt_uint8_t *)(UID_BASE + 2);
  430. stm32_eth_device.dev_addr[5] = *(rt_uint8_t *)(UID_BASE + 0);
  431. stm32_eth_device.parent.parent.init = rt_stm32_eth_init;
  432. stm32_eth_device.parent.parent.open = rt_stm32_eth_open;
  433. stm32_eth_device.parent.parent.close = rt_stm32_eth_close;
  434. stm32_eth_device.parent.parent.read = rt_stm32_eth_read;
  435. stm32_eth_device.parent.parent.write = rt_stm32_eth_write;
  436. stm32_eth_device.parent.parent.control = rt_stm32_eth_control;
  437. stm32_eth_device.parent.parent.user_data = RT_NULL;
  438. stm32_eth_device.parent.eth_rx = rt_stm32_eth_rx;
  439. stm32_eth_device.parent.eth_tx = rt_stm32_eth_tx;
  440. /* register eth device */
  441. state = eth_device_init(&(stm32_eth_device.parent), "e0");
  442. if (RT_EOK == state)
  443. {
  444. LOG_D("emac device init success");
  445. }
  446. else
  447. {
  448. LOG_E("emac device init faild: %d", state);
  449. state = -RT_ERROR;
  450. }
  451. /* start phy monitor */
  452. rt_thread_t tid;
  453. tid = rt_thread_create("phy",
  454. phy_monitor_thread_entry,
  455. RT_NULL,
  456. 1024,
  457. RT_THREAD_PRIORITY_MAX - 2,
  458. 2);
  459. if (tid != RT_NULL)
  460. {
  461. rt_thread_startup(tid);
  462. }
  463. else
  464. {
  465. state = -RT_ERROR;
  466. }
  467. return state;
  468. }
  469. INIT_DEVICE_EXPORT(rt_hw_stm32_eth_init);
  470. #endif /* BSP_USING_ETH_ARTPI */