drv_eth.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * Copyright (c) 2006-2021, 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. */
  13. #include "drv_config.h"
  14. #include "drv_eth.h"
  15. #include <netif/ethernetif.h>
  16. #include <lwipopts.h>
  17. /*
  18. * Emac driver uses CubeMX tool to generate emac and phy's configuration,
  19. * the configuration files can be found in CubeMX_Config folder.
  20. */
  21. /* debug option */
  22. //#define ETH_RX_DUMP
  23. //#define ETH_TX_DUMP
  24. //#define DRV_DEBUG
  25. #define LOG_TAG "drv.emac"
  26. #include <drv_log.h>
  27. #define MAX_ADDR_LEN 6
  28. #undef PHY_FULL_DUPLEX
  29. #define PHY_LINK (1 << 0)
  30. #define PHY_100M (1 << 1)
  31. #define PHY_FULL_DUPLEX (1 << 2)
  32. struct rt_stm32_eth
  33. {
  34. /* inherit from ethernet device */
  35. struct eth_device parent;
  36. #ifndef PHY_USING_INTERRUPT_MODE
  37. rt_timer_t poll_link_timer;
  38. #endif
  39. /* interface address info, hw address */
  40. rt_uint8_t dev_addr[MAX_ADDR_LEN];
  41. /* ETH_Speed */
  42. rt_uint32_t ETH_Speed;
  43. /* ETH_Duplex_Mode */
  44. rt_uint32_t ETH_Mode;
  45. };
  46. static ETH_DMADescTypeDef *DMARxDscrTab, *DMATxDscrTab;
  47. static rt_uint8_t *Rx_Buff, *Tx_Buff;
  48. static ETH_HandleTypeDef EthHandle;
  49. static struct rt_stm32_eth stm32_eth_device;
  50. #if defined(ETH_RX_DUMP) || defined(ETH_TX_DUMP)
  51. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  52. static void dump_hex(const rt_uint8_t *ptr, rt_size_t buflen)
  53. {
  54. unsigned char *buf = (unsigned char *)ptr;
  55. int i, j;
  56. for (i = 0; i < buflen; i += 16)
  57. {
  58. rt_kprintf("%08X: ", i);
  59. for (j = 0; j < 16; j++)
  60. if (i + j < buflen)
  61. rt_kprintf("%02X ", buf[i + j]);
  62. else
  63. rt_kprintf(" ");
  64. rt_kprintf(" ");
  65. for (j = 0; j < 16; j++)
  66. if (i + j < buflen)
  67. rt_kprintf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
  68. rt_kprintf("\n");
  69. }
  70. }
  71. #endif
  72. extern void phy_reset(void);
  73. /* EMAC initialization function */
  74. static rt_err_t rt_stm32_eth_init(rt_device_t dev)
  75. {
  76. __HAL_RCC_ETH_CLK_ENABLE();
  77. phy_reset();
  78. /* ETHERNET Configuration */
  79. EthHandle.Instance = ETH;
  80. EthHandle.Init.MACAddr = (rt_uint8_t *)&stm32_eth_device.dev_addr[0];
  81. EthHandle.Init.AutoNegotiation = ETH_AUTONEGOTIATION_DISABLE;
  82. EthHandle.Init.Speed = ETH_SPEED_100M;
  83. EthHandle.Init.DuplexMode = ETH_MODE_FULLDUPLEX;
  84. EthHandle.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII;
  85. EthHandle.Init.RxMode = ETH_RXINTERRUPT_MODE;
  86. #ifdef RT_LWIP_USING_HW_CHECKSUM
  87. EthHandle.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;
  88. #else
  89. EthHandle.Init.ChecksumMode = ETH_CHECKSUM_BY_SOFTWARE;
  90. #endif
  91. HAL_ETH_DeInit(&EthHandle);
  92. /* configure ethernet peripheral (GPIOs, clocks, MAC, DMA) */
  93. if (HAL_ETH_Init(&EthHandle) != HAL_OK)
  94. {
  95. LOG_E("eth hardware init failed");
  96. }
  97. else
  98. {
  99. LOG_D("eth hardware init success");
  100. }
  101. /* Initialize Tx Descriptors list: Chain Mode */
  102. HAL_ETH_DMATxDescListInit(&EthHandle, DMATxDscrTab, Tx_Buff, ETH_TXBUFNB);
  103. /* Initialize Rx Descriptors list: Chain Mode */
  104. HAL_ETH_DMARxDescListInit(&EthHandle, DMARxDscrTab, Rx_Buff, ETH_RXBUFNB);
  105. /* ETH interrupt Init */
  106. HAL_NVIC_SetPriority(ETH_IRQn, 0x07, 0);
  107. HAL_NVIC_EnableIRQ(ETH_IRQn);
  108. /* Enable MAC and DMA transmission and reception */
  109. if (HAL_ETH_Start(&EthHandle) == HAL_OK)
  110. {
  111. LOG_D("emac hardware start");
  112. }
  113. else
  114. {
  115. LOG_E("emac hardware start faild");
  116. return -RT_ERROR;
  117. }
  118. return RT_EOK;
  119. }
  120. static rt_err_t rt_stm32_eth_open(rt_device_t dev, rt_uint16_t oflag)
  121. {
  122. LOG_D("emac open");
  123. return RT_EOK;
  124. }
  125. static rt_err_t rt_stm32_eth_close(rt_device_t dev)
  126. {
  127. LOG_D("emac close");
  128. return RT_EOK;
  129. }
  130. static rt_size_t rt_stm32_eth_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  131. {
  132. LOG_D("emac read");
  133. rt_set_errno(-RT_ENOSYS);
  134. return 0;
  135. }
  136. static rt_size_t rt_stm32_eth_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  137. {
  138. LOG_D("emac write");
  139. rt_set_errno(-RT_ENOSYS);
  140. return 0;
  141. }
  142. static rt_err_t rt_stm32_eth_control(rt_device_t dev, int cmd, void *args)
  143. {
  144. switch (cmd)
  145. {
  146. case NIOCTL_GADDR:
  147. /* get mac address */
  148. if (args)
  149. {
  150. SMEMCPY(args, stm32_eth_device.dev_addr, 6);
  151. }
  152. else
  153. {
  154. return -RT_ERROR;
  155. }
  156. break;
  157. default :
  158. break;
  159. }
  160. return RT_EOK;
  161. }
  162. /* ethernet device interface */
  163. /* transmit data*/
  164. rt_err_t rt_stm32_eth_tx(rt_device_t dev, struct pbuf *p)
  165. {
  166. rt_err_t ret = RT_ERROR;
  167. HAL_StatusTypeDef state;
  168. struct pbuf *q;
  169. uint8_t *buffer = (uint8_t *)(EthHandle.TxDesc->Buffer1Addr);
  170. __IO ETH_DMADescTypeDef *DmaTxDesc;
  171. uint32_t framelength = 0;
  172. uint32_t bufferoffset = 0;
  173. uint32_t byteslefttocopy = 0;
  174. uint32_t payloadoffset = 0;
  175. DmaTxDesc = EthHandle.TxDesc;
  176. bufferoffset = 0;
  177. /* copy frame from pbufs to driver buffers */
  178. for (q = p; q != NULL; q = q->next)
  179. {
  180. /* Is this buffer available? If not, goto error */
  181. if ((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
  182. {
  183. LOG_D("buffer not valid");
  184. ret = ERR_USE;
  185. goto error;
  186. }
  187. /* Get bytes in current lwIP buffer */
  188. byteslefttocopy = q->len;
  189. payloadoffset = 0;
  190. /* Check if the length of data to copy is bigger than Tx buffer size*/
  191. while ((byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE)
  192. {
  193. /* Copy data to Tx buffer*/
  194. SMEMCPY((uint8_t *)((uint8_t *)buffer + bufferoffset), (uint8_t *)((uint8_t *)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset));
  195. /* Point to next descriptor */
  196. DmaTxDesc = (ETH_DMADescTypeDef *)(DmaTxDesc->Buffer2NextDescAddr);
  197. /* Check if the buffer is available */
  198. if ((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
  199. {
  200. LOG_E("dma tx desc buffer is not valid");
  201. ret = ERR_USE;
  202. goto error;
  203. }
  204. buffer = (uint8_t *)(DmaTxDesc->Buffer1Addr);
  205. byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset);
  206. payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset);
  207. framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset);
  208. bufferoffset = 0;
  209. }
  210. /* Copy the remaining bytes */
  211. SMEMCPY((uint8_t *)((uint8_t *)buffer + bufferoffset), (uint8_t *)((uint8_t *)q->payload + payloadoffset), byteslefttocopy);
  212. bufferoffset = bufferoffset + byteslefttocopy;
  213. framelength = framelength + byteslefttocopy;
  214. }
  215. #ifdef ETH_TX_DUMP
  216. dump_hex(buffer, p->tot_len);
  217. #endif
  218. /* Prepare transmit descriptors to give to DMA */
  219. /* TODO Optimize data send speed*/
  220. LOG_D("transmit frame length :%d", framelength);
  221. /* wait for unlocked */
  222. while (EthHandle.Lock == HAL_LOCKED);
  223. state = HAL_ETH_TransmitFrame(&EthHandle, framelength);
  224. if (state != HAL_OK)
  225. {
  226. LOG_E("eth transmit frame faild: %d", state);
  227. }
  228. ret = ERR_OK;
  229. error:
  230. /* When Transmit Underflow flag is set, clear it and issue a Transmit Poll Demand to resume transmission */
  231. if ((EthHandle.Instance->DMASR & ETH_DMASR_TUS) != (uint32_t)RESET)
  232. {
  233. /* Clear TUS ETHERNET DMA flag */
  234. EthHandle.Instance->DMASR = ETH_DMASR_TUS;
  235. /* Resume DMA transmission*/
  236. EthHandle.Instance->DMATPDR = 0;
  237. }
  238. return ret;
  239. }
  240. /* receive data*/
  241. struct pbuf *rt_stm32_eth_rx(rt_device_t dev)
  242. {
  243. struct pbuf *p = NULL;
  244. struct pbuf *q = NULL;
  245. HAL_StatusTypeDef state;
  246. uint16_t len = 0;
  247. uint8_t *buffer;
  248. __IO ETH_DMADescTypeDef *dmarxdesc;
  249. uint32_t bufferoffset = 0;
  250. uint32_t payloadoffset = 0;
  251. uint32_t byteslefttocopy = 0;
  252. uint32_t i = 0;
  253. /* Get received frame */
  254. state = HAL_ETH_GetReceivedFrame_IT(&EthHandle);
  255. if (state != HAL_OK)
  256. {
  257. LOG_D("receive frame faild");
  258. return NULL;
  259. }
  260. /* Obtain the size of the packet and put it into the "len" variable. */
  261. len = EthHandle.RxFrameInfos.length;
  262. buffer = (uint8_t *)EthHandle.RxFrameInfos.buffer;
  263. LOG_D("receive frame len : %d", len);
  264. if (len > 0)
  265. {
  266. /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
  267. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  268. }
  269. #ifdef ETH_RX_DUMP
  270. dump_hex(buffer, p->tot_len);
  271. #endif
  272. if (p != NULL)
  273. {
  274. dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;
  275. bufferoffset = 0;
  276. for (q = p; q != NULL; q = q->next)
  277. {
  278. byteslefttocopy = q->len;
  279. payloadoffset = 0;
  280. /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
  281. while ((byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE)
  282. {
  283. /* Copy data to pbuf */
  284. SMEMCPY((uint8_t *)((uint8_t *)q->payload + payloadoffset), (uint8_t *)((uint8_t *)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
  285. /* Point to next descriptor */
  286. dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
  287. buffer = (uint8_t *)(dmarxdesc->Buffer1Addr);
  288. byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
  289. payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
  290. bufferoffset = 0;
  291. }
  292. /* Copy remaining data in pbuf */
  293. SMEMCPY((uint8_t *)((uint8_t *)q->payload + payloadoffset), (uint8_t *)((uint8_t *)buffer + bufferoffset), byteslefttocopy);
  294. bufferoffset = bufferoffset + byteslefttocopy;
  295. }
  296. }
  297. /* Release descriptors to DMA */
  298. /* Point to first descriptor */
  299. dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;
  300. /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
  301. for (i = 0; i < EthHandle.RxFrameInfos.SegCount; i++)
  302. {
  303. dmarxdesc->Status |= ETH_DMARXDESC_OWN;
  304. dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
  305. }
  306. /* Clear Segment_Count */
  307. EthHandle.RxFrameInfos.SegCount = 0;
  308. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  309. if ((EthHandle.Instance->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)
  310. {
  311. /* Clear RBUS ETHERNET DMA flag */
  312. EthHandle.Instance->DMASR = ETH_DMASR_RBUS;
  313. /* Resume DMA reception */
  314. EthHandle.Instance->DMARPDR = 0;
  315. }
  316. return p;
  317. }
  318. /* interrupt service routine */
  319. void ETH_IRQHandler(void)
  320. {
  321. /* enter interrupt */
  322. rt_interrupt_enter();
  323. HAL_ETH_IRQHandler(&EthHandle);
  324. /* leave interrupt */
  325. rt_interrupt_leave();
  326. }
  327. void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
  328. {
  329. rt_err_t result;
  330. result = eth_device_ready(&(stm32_eth_device.parent));
  331. if (result != RT_EOK)
  332. {
  333. LOG_I("RxCpltCallback err = %d", result);
  334. }
  335. }
  336. void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *heth)
  337. {
  338. LOG_E("eth err");
  339. }
  340. static void phy_linkchange()
  341. {
  342. static rt_uint8_t phy_speed = 0;
  343. rt_uint8_t phy_speed_new = 0;
  344. rt_uint32_t status;
  345. HAL_ETH_ReadPHYRegister(&EthHandle, PHY_BASIC_STATUS_REG, (uint32_t *)&status);
  346. LOG_D("phy basic status reg is 0x%X", status);
  347. if (status & (PHY_AUTONEGO_COMPLETE_MASK | PHY_LINKED_STATUS_MASK))
  348. {
  349. rt_uint32_t SR = 0;
  350. phy_speed_new |= PHY_LINK;
  351. HAL_ETH_ReadPHYRegister(&EthHandle, PHY_Status_REG, (uint32_t *)&SR);
  352. LOG_D("phy control status reg is 0x%X", SR);
  353. if (PHY_Status_SPEED_100M(SR))
  354. {
  355. phy_speed_new |= PHY_100M;
  356. }
  357. if (PHY_Status_FULL_DUPLEX(SR))
  358. {
  359. phy_speed_new |= PHY_FULL_DUPLEX;
  360. }
  361. }
  362. if (phy_speed != phy_speed_new)
  363. {
  364. phy_speed = phy_speed_new;
  365. if (phy_speed & PHY_LINK)
  366. {
  367. LOG_D("link up");
  368. if (phy_speed & PHY_100M)
  369. {
  370. LOG_D("100Mbps");
  371. stm32_eth_device.ETH_Speed = ETH_SPEED_100M;
  372. }
  373. else
  374. {
  375. stm32_eth_device.ETH_Speed = ETH_SPEED_10M;
  376. LOG_D("10Mbps");
  377. }
  378. if (phy_speed & PHY_FULL_DUPLEX)
  379. {
  380. LOG_D("full-duplex");
  381. stm32_eth_device.ETH_Mode = ETH_MODE_FULLDUPLEX;
  382. }
  383. else
  384. {
  385. LOG_D("half-duplex");
  386. stm32_eth_device.ETH_Mode = ETH_MODE_HALFDUPLEX;
  387. }
  388. /* send link up. */
  389. eth_device_linkchange(&stm32_eth_device.parent, RT_TRUE);
  390. }
  391. else
  392. {
  393. LOG_I("link down");
  394. eth_device_linkchange(&stm32_eth_device.parent, RT_FALSE);
  395. }
  396. }
  397. }
  398. #ifdef PHY_USING_INTERRUPT_MODE
  399. static void eth_phy_isr(void *args)
  400. {
  401. rt_uint32_t status = 0;
  402. HAL_ETH_ReadPHYRegister(&EthHandle, PHY_INTERRUPT_FLAG_REG, (uint32_t *)&status);
  403. LOG_D("phy interrupt status reg is 0x%X", status);
  404. phy_linkchange();
  405. }
  406. #endif /* PHY_USING_INTERRUPT_MODE */
  407. static void phy_monitor_thread_entry(void *parameter)
  408. {
  409. uint8_t phy_addr = 0xFF;
  410. uint8_t detected_count = 0;
  411. while(phy_addr == 0xFF)
  412. {
  413. /* phy search */
  414. rt_uint32_t i, temp;
  415. for (i = 0; i <= 0x1F; i++)
  416. {
  417. EthHandle.Init.PhyAddress = i;
  418. HAL_ETH_ReadPHYRegister(&EthHandle, PHY_ID1_REG, (uint32_t *)&temp);
  419. if (temp != 0xFFFF && temp != 0x00)
  420. {
  421. phy_addr = i;
  422. break;
  423. }
  424. }
  425. detected_count++;
  426. rt_thread_mdelay(1000);
  427. if (detected_count > 10)
  428. {
  429. LOG_E("No PHY device was detected, please check hardware!");
  430. }
  431. }
  432. LOG_D("Found a phy, address:0x%02X", phy_addr);
  433. /* RESET PHY */
  434. LOG_D("RESET PHY!");
  435. HAL_ETH_WritePHYRegister(&EthHandle, PHY_BASIC_CONTROL_REG, PHY_RESET_MASK);
  436. rt_thread_mdelay(2000);
  437. HAL_ETH_WritePHYRegister(&EthHandle, PHY_BASIC_CONTROL_REG, PHY_AUTO_NEGOTIATION_MASK);
  438. phy_linkchange();
  439. #ifdef PHY_USING_INTERRUPT_MODE
  440. /* configuration intterrupt pin */
  441. rt_pin_mode(PHY_INT_PIN, PIN_MODE_INPUT_PULLUP);
  442. rt_pin_attach_irq(PHY_INT_PIN, PIN_IRQ_MODE_FALLING, eth_phy_isr, (void *)"callbackargs");
  443. rt_pin_irq_enable(PHY_INT_PIN, PIN_IRQ_ENABLE);
  444. /* enable phy interrupt */
  445. HAL_ETH_WritePHYRegister(&EthHandle, PHY_INTERRUPT_MASK_REG, PHY_INT_MASK);
  446. #if defined(PHY_INTERRUPT_CTRL_REG)
  447. HAL_ETH_WritePHYRegister(&EthHandle, PHY_INTERRUPT_CTRL_REG, PHY_INTERRUPT_EN);
  448. #endif
  449. #else /* PHY_USING_INTERRUPT_MODE */
  450. stm32_eth_device.poll_link_timer = rt_timer_create("phylnk", (void (*)(void*))phy_linkchange,
  451. NULL, RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC);
  452. if (!stm32_eth_device.poll_link_timer || rt_timer_start(stm32_eth_device.poll_link_timer) != RT_EOK)
  453. {
  454. LOG_E("Start link change detection timer failed");
  455. }
  456. #endif /* PHY_USING_INTERRUPT_MODE */
  457. }
  458. /* Register the EMAC device */
  459. static int rt_hw_stm32_eth_init(void)
  460. {
  461. rt_err_t state = RT_EOK;
  462. /* Prepare receive and send buffers */
  463. Rx_Buff = (rt_uint8_t *)rt_calloc(ETH_RXBUFNB, ETH_MAX_PACKET_SIZE);
  464. if (Rx_Buff == RT_NULL)
  465. {
  466. LOG_E("No memory");
  467. state = -RT_ENOMEM;
  468. goto __exit;
  469. }
  470. Tx_Buff = (rt_uint8_t *)rt_calloc(ETH_TXBUFNB, ETH_MAX_PACKET_SIZE);
  471. if (Tx_Buff == RT_NULL)
  472. {
  473. LOG_E("No memory");
  474. state = -RT_ENOMEM;
  475. goto __exit;
  476. }
  477. DMARxDscrTab = (ETH_DMADescTypeDef *)rt_calloc(ETH_RXBUFNB, sizeof(ETH_DMADescTypeDef));
  478. if (DMARxDscrTab == RT_NULL)
  479. {
  480. LOG_E("No memory");
  481. state = -RT_ENOMEM;
  482. goto __exit;
  483. }
  484. DMATxDscrTab = (ETH_DMADescTypeDef *)rt_calloc(ETH_TXBUFNB, sizeof(ETH_DMADescTypeDef));
  485. if (DMATxDscrTab == RT_NULL)
  486. {
  487. LOG_E("No memory");
  488. state = -RT_ENOMEM;
  489. goto __exit;
  490. }
  491. stm32_eth_device.ETH_Speed = ETH_SPEED_100M;
  492. stm32_eth_device.ETH_Mode = ETH_MODE_FULLDUPLEX;
  493. /* OUI 00-80-E1 STMICROELECTRONICS. */
  494. stm32_eth_device.dev_addr[0] = 0x00;
  495. stm32_eth_device.dev_addr[1] = 0x80;
  496. stm32_eth_device.dev_addr[2] = 0xE1;
  497. /* generate MAC addr from 96bit unique ID (only for test). */
  498. stm32_eth_device.dev_addr[3] = *(rt_uint8_t *)(UID_BASE + 4);
  499. stm32_eth_device.dev_addr[4] = *(rt_uint8_t *)(UID_BASE + 2);
  500. stm32_eth_device.dev_addr[5] = *(rt_uint8_t *)(UID_BASE + 0);
  501. stm32_eth_device.parent.parent.init = rt_stm32_eth_init;
  502. stm32_eth_device.parent.parent.open = rt_stm32_eth_open;
  503. stm32_eth_device.parent.parent.close = rt_stm32_eth_close;
  504. stm32_eth_device.parent.parent.read = rt_stm32_eth_read;
  505. stm32_eth_device.parent.parent.write = rt_stm32_eth_write;
  506. stm32_eth_device.parent.parent.control = rt_stm32_eth_control;
  507. stm32_eth_device.parent.parent.user_data = RT_NULL;
  508. stm32_eth_device.parent.eth_rx = rt_stm32_eth_rx;
  509. stm32_eth_device.parent.eth_tx = rt_stm32_eth_tx;
  510. /* register eth device */
  511. state = eth_device_init(&(stm32_eth_device.parent), "e0");
  512. if (RT_EOK == state)
  513. {
  514. LOG_D("emac device init success");
  515. }
  516. else
  517. {
  518. LOG_E("emac device init faild: %d", state);
  519. state = -RT_ERROR;
  520. goto __exit;
  521. }
  522. /* start phy monitor */
  523. rt_thread_t tid;
  524. tid = rt_thread_create("phy",
  525. phy_monitor_thread_entry,
  526. RT_NULL,
  527. 1024,
  528. RT_THREAD_PRIORITY_MAX - 2,
  529. 2);
  530. if (tid != RT_NULL)
  531. {
  532. rt_thread_startup(tid);
  533. }
  534. else
  535. {
  536. state = -RT_ERROR;
  537. }
  538. __exit:
  539. if (state != RT_EOK)
  540. {
  541. if (Rx_Buff)
  542. {
  543. rt_free(Rx_Buff);
  544. }
  545. if (Tx_Buff)
  546. {
  547. rt_free(Tx_Buff);
  548. }
  549. if (DMARxDscrTab)
  550. {
  551. rt_free(DMARxDscrTab);
  552. }
  553. if (DMATxDscrTab)
  554. {
  555. rt_free(DMATxDscrTab);
  556. }
  557. }
  558. return state;
  559. }
  560. INIT_DEVICE_EXPORT(rt_hw_stm32_eth_init);