drv_eth.c 19 KB

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