drv_eth.c 18 KB

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