drv_eth.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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 rt_bool_t tx_is_waiting = RT_FALSE;
  41. static ETH_HandleTypeDef EthHandle;
  42. static struct rt_stm32_eth stm32_eth_device;
  43. static struct rt_semaphore tx_wait;
  44. #if defined(ETH_RX_DUMP) || defined(ETH_TX_DUMP)
  45. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  46. static void dump_hex(const rt_uint8_t *ptr, rt_size_t buflen)
  47. {
  48. unsigned char *buf = (unsigned char *)ptr;
  49. int i, j;
  50. for (i = 0; i < buflen; i += 16)
  51. {
  52. rt_kprintf("%08X: ", i);
  53. for (j = 0; j < 16; j++)
  54. if (i + j < buflen)
  55. rt_kprintf("%02X ", buf[i + j]);
  56. else
  57. rt_kprintf(" ");
  58. rt_kprintf(" ");
  59. for (j = 0; j < 16; j++)
  60. if (i + j < buflen)
  61. rt_kprintf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
  62. rt_kprintf("\n");
  63. }
  64. }
  65. #endif
  66. extern void phy_reset(void);
  67. /* EMAC initialization function */
  68. static rt_err_t rt_stm32_eth_init(rt_device_t dev)
  69. {
  70. __HAL_RCC_ETH_CLK_ENABLE();
  71. phy_reset();
  72. /* ETHERNET Configuration */
  73. EthHandle.Instance = ETH;
  74. EthHandle.Init.MACAddr = (rt_uint8_t *)&stm32_eth_device.dev_addr[0];
  75. EthHandle.Init.AutoNegotiation = ETH_AUTONEGOTIATION_DISABLE;
  76. EthHandle.Init.Speed = ETH_SPEED_100M;
  77. EthHandle.Init.DuplexMode = ETH_MODE_FULLDUPLEX;
  78. EthHandle.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII;
  79. EthHandle.Init.RxMode = ETH_RXINTERRUPT_MODE;
  80. EthHandle.Init.ChecksumMode = ETH_CHECKSUM_BY_SOFTWARE;
  81. HAL_ETH_DeInit(&EthHandle);
  82. /* configure ethernet peripheral (GPIOs, clocks, MAC, DMA) */
  83. if (HAL_ETH_Init(&EthHandle) != HAL_OK)
  84. {
  85. LOG_E("eth hardware init failed");
  86. return -RT_ERROR;
  87. }
  88. else
  89. {
  90. LOG_D("eth hardware init success");
  91. }
  92. /* Initialize Tx Descriptors list: Chain Mode */
  93. HAL_ETH_DMATxDescListInit(&EthHandle, DMATxDscrTab, Tx_Buff, ETH_TXBUFNB);
  94. /* Initialize Rx Descriptors list: Chain Mode */
  95. HAL_ETH_DMARxDescListInit(&EthHandle, DMARxDscrTab, Rx_Buff, ETH_RXBUFNB);
  96. /* ETH interrupt Init */
  97. HAL_NVIC_SetPriority(ETH_IRQn, 0x07, 0);
  98. HAL_NVIC_EnableIRQ(ETH_IRQn);
  99. /* Enable MAC and DMA transmission and reception */
  100. if (HAL_ETH_Start(&EthHandle) == HAL_OK)
  101. {
  102. LOG_D("emac hardware start");
  103. }
  104. else
  105. {
  106. LOG_E("emac hardware start faild");
  107. return -RT_ERROR;
  108. }
  109. return RT_EOK;
  110. }
  111. static rt_err_t rt_stm32_eth_open(rt_device_t dev, rt_uint16_t oflag)
  112. {
  113. LOG_D("emac open");
  114. return RT_EOK;
  115. }
  116. static rt_err_t rt_stm32_eth_close(rt_device_t dev)
  117. {
  118. LOG_D("emac close");
  119. return RT_EOK;
  120. }
  121. static rt_size_t rt_stm32_eth_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  122. {
  123. LOG_D("emac read");
  124. rt_set_errno(-RT_ENOSYS);
  125. return 0;
  126. }
  127. static rt_size_t rt_stm32_eth_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  128. {
  129. LOG_D("emac write");
  130. rt_set_errno(-RT_ENOSYS);
  131. return 0;
  132. }
  133. static rt_err_t rt_stm32_eth_control(rt_device_t dev, int cmd, void *args)
  134. {
  135. switch (cmd)
  136. {
  137. case NIOCTL_GADDR:
  138. /* get mac address */
  139. if (args) rt_memcpy(args, stm32_eth_device.dev_addr, 6);
  140. else return -RT_ERROR;
  141. break;
  142. default :
  143. break;
  144. }
  145. return RT_EOK;
  146. }
  147. /* ethernet device interface */
  148. /* transmit data*/
  149. rt_err_t rt_stm32_eth_tx(rt_device_t dev, struct pbuf *p)
  150. {
  151. rt_err_t ret = RT_ERROR;
  152. HAL_StatusTypeDef state;
  153. struct pbuf *q;
  154. uint8_t *buffer = (uint8_t *)(EthHandle.TxDesc->Buffer1Addr);
  155. __IO ETH_DMADescTypeDef *DmaTxDesc;
  156. uint32_t framelength = 0;
  157. uint32_t bufferoffset = 0;
  158. uint32_t byteslefttocopy = 0;
  159. uint32_t payloadoffset = 0;
  160. DmaTxDesc = EthHandle.TxDesc;
  161. bufferoffset = 0;
  162. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  163. while ((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
  164. {
  165. rt_err_t result;
  166. rt_uint32_t level;
  167. level = rt_hw_interrupt_disable();
  168. tx_is_waiting = RT_TRUE;
  169. rt_hw_interrupt_enable(level);
  170. /* it's own bit set, wait it */
  171. result = rt_sem_take(&tx_wait, RT_WAITING_FOREVER);
  172. if (result == RT_EOK) break;
  173. if (result == -RT_ERROR) return -RT_ERROR;
  174. }
  175. /* copy frame from pbufs to driver buffers */
  176. for (q = p; q != NULL; q = q->next)
  177. {
  178. /* Is this buffer available? If not, goto error */
  179. if ((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
  180. {
  181. LOG_E("buffer not valid");
  182. ret = ERR_USE;
  183. goto error;
  184. }
  185. /* Get bytes in current lwIP buffer */
  186. byteslefttocopy = q->len;
  187. payloadoffset = 0;
  188. /* Check if the length of data to copy is bigger than Tx buffer size*/
  189. while ((byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE)
  190. {
  191. /* Copy data to Tx buffer*/
  192. memcpy((uint8_t *)((uint8_t *)buffer + bufferoffset), (uint8_t *)((uint8_t *)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset));
  193. /* Point to next descriptor */
  194. DmaTxDesc = (ETH_DMADescTypeDef *)(DmaTxDesc->Buffer2NextDescAddr);
  195. /* Check if the buffer is available */
  196. if ((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
  197. {
  198. LOG_E("dma tx desc buffer is not valid");
  199. ret = ERR_USE;
  200. goto error;
  201. }
  202. buffer = (uint8_t *)(DmaTxDesc->Buffer1Addr);
  203. byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset);
  204. payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset);
  205. framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset);
  206. bufferoffset = 0;
  207. }
  208. /* Copy the remaining bytes */
  209. memcpy((uint8_t *)((uint8_t *)buffer + bufferoffset), (uint8_t *)((uint8_t *)q->payload + payloadoffset), byteslefttocopy);
  210. bufferoffset = bufferoffset + byteslefttocopy;
  211. framelength = framelength + byteslefttocopy;
  212. }
  213. #ifdef ETH_TX_DUMP
  214. dump_hex(buffer, p->tot_len);
  215. #endif
  216. /* Prepare transmit descriptors to give to DMA */
  217. /* TODO Optimize data send speed*/
  218. LOG_D("transmit frame lenth :%d", framelength);
  219. /* wait for unlocked */
  220. while (EthHandle.Lock == HAL_LOCKED);
  221. state = HAL_ETH_TransmitFrame(&EthHandle, framelength);
  222. if (state != HAL_OK)
  223. {
  224. LOG_E("eth transmit frame faild: %d", state);
  225. }
  226. ret = ERR_OK;
  227. error:
  228. /* When Transmit Underflow flag is set, clear it and issue a Transmit Poll Demand to resume transmission */
  229. if ((EthHandle.Instance->DMASR & ETH_DMASR_TUS) != (uint32_t)RESET)
  230. {
  231. /* Clear TUS ETHERNET DMA flag */
  232. EthHandle.Instance->DMASR = ETH_DMASR_TUS;
  233. /* Resume DMA transmission*/
  234. EthHandle.Instance->DMATPDR = 0;
  235. }
  236. return ret;
  237. }
  238. /* receive data*/
  239. struct pbuf *rt_stm32_eth_rx(rt_device_t dev)
  240. {
  241. struct pbuf *p = NULL;
  242. struct pbuf *q = NULL;
  243. HAL_StatusTypeDef state;
  244. uint16_t len = 0;
  245. uint8_t *buffer;
  246. __IO ETH_DMADescTypeDef *dmarxdesc;
  247. uint32_t bufferoffset = 0;
  248. uint32_t payloadoffset = 0;
  249. uint32_t byteslefttocopy = 0;
  250. uint32_t i = 0;
  251. /* Get received frame */
  252. state = HAL_ETH_GetReceivedFrame_IT(&EthHandle);
  253. if (state != HAL_OK)
  254. {
  255. LOG_D("receive frame faild");
  256. return NULL;
  257. }
  258. /* Obtain the size of the packet and put it into the "len" variable. */
  259. len = EthHandle.RxFrameInfos.length;
  260. buffer = (uint8_t *)EthHandle.RxFrameInfos.buffer;
  261. LOG_D("receive frame len : %d", len);
  262. if (len > 0)
  263. {
  264. /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
  265. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  266. }
  267. #ifdef ETH_RX_DUMP
  268. dump_hex(buffer, p->tot_len);
  269. #endif
  270. if (p != NULL)
  271. {
  272. dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;
  273. bufferoffset = 0;
  274. for (q = p; q != NULL; q = q->next)
  275. {
  276. byteslefttocopy = q->len;
  277. payloadoffset = 0;
  278. /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
  279. while ((byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE)
  280. {
  281. /* Copy data to pbuf */
  282. memcpy((uint8_t *)((uint8_t *)q->payload + payloadoffset), (uint8_t *)((uint8_t *)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
  283. /* Point to next descriptor */
  284. dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
  285. buffer = (uint8_t *)(dmarxdesc->Buffer1Addr);
  286. byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
  287. payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
  288. bufferoffset = 0;
  289. }
  290. /* Copy remaining data in pbuf */
  291. memcpy((uint8_t *)((uint8_t *)q->payload + payloadoffset), (uint8_t *)((uint8_t *)buffer + bufferoffset), byteslefttocopy);
  292. bufferoffset = bufferoffset + byteslefttocopy;
  293. }
  294. }
  295. /* Release descriptors to DMA */
  296. /* Point to first descriptor */
  297. dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;
  298. /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
  299. for (i = 0; i < EthHandle.RxFrameInfos.SegCount; i++)
  300. {
  301. dmarxdesc->Status |= ETH_DMARXDESC_OWN;
  302. dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
  303. }
  304. /* Clear Segment_Count */
  305. EthHandle.RxFrameInfos.SegCount = 0;
  306. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  307. if ((EthHandle.Instance->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)
  308. {
  309. /* Clear RBUS ETHERNET DMA flag */
  310. EthHandle.Instance->DMASR = ETH_DMASR_RBUS;
  311. /* Resume DMA reception */
  312. EthHandle.Instance->DMARPDR = 0;
  313. }
  314. return p;
  315. }
  316. /* interrupt service routine */
  317. void ETH_IRQHandler(void)
  318. {
  319. /* enter interrupt */
  320. rt_interrupt_enter();
  321. HAL_ETH_IRQHandler(&EthHandle);
  322. /* leave interrupt */
  323. rt_interrupt_leave();
  324. }
  325. void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *heth)
  326. {
  327. if (tx_is_waiting == RT_TRUE)
  328. {
  329. tx_is_waiting = RT_FALSE;
  330. rt_sem_release(&tx_wait);
  331. }
  332. }
  333. void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
  334. {
  335. rt_err_t result;
  336. result = eth_device_ready(&(stm32_eth_device.parent));
  337. if (result != RT_EOK)
  338. LOG_E("RX err = %d", result);
  339. }
  340. void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *heth)
  341. {
  342. LOG_E("eth err");
  343. }
  344. #ifdef PHY_USING_INTERRUPT_MODE
  345. static void eth_phy_isr(void *args)
  346. {
  347. rt_uint32_t status = 0;
  348. static rt_uint8_t link_status = 1;
  349. HAL_ETH_ReadPHYRegister(&EthHandle, PHY_INTERRUPT_FLAG_REG, (uint32_t *)&status);
  350. LOG_D("phy interrupt status reg is 0x%X", status);
  351. HAL_ETH_ReadPHYRegister(&EthHandle, PHY_BASIC_STATUS_REG, (uint32_t *)&status);
  352. LOG_D("phy basic status reg is 0x%X", status);
  353. if (status & PHY_LINKED_STATUS_MASK)
  354. {
  355. if (link_status == 0)
  356. {
  357. link_status = 1;
  358. LOG_D("link up");
  359. /* send link up. */
  360. eth_device_linkchange(&stm32_eth_device.parent, RT_TRUE);
  361. }
  362. }
  363. else
  364. {
  365. if (link_status == 1)
  366. {
  367. link_status = 0;
  368. LOG_I("link down");
  369. /* send link down. */
  370. eth_device_linkchange(&stm32_eth_device.parent, RT_FALSE);
  371. }
  372. }
  373. }
  374. #endif /* PHY_USING_INTERRUPT_MODE */
  375. static uint8_t phy_speed = 0;
  376. #define PHY_LINK_MASK (1<<0)
  377. static void phy_monitor_thread_entry(void *parameter)
  378. {
  379. uint8_t phy_addr = 0xFF;
  380. uint8_t phy_speed_new = 0;
  381. rt_uint32_t status = 0;
  382. /* phy search */
  383. rt_uint32_t i, temp;
  384. for (i = 0; i <= 0x1F; i++)
  385. {
  386. EthHandle.Init.PhyAddress = i;
  387. HAL_ETH_ReadPHYRegister(&EthHandle, PHY_ID1_REG, (uint32_t *)&temp);
  388. if (temp != 0xFFFF && temp != 0x00)
  389. {
  390. phy_addr = i;
  391. break;
  392. }
  393. }
  394. if (phy_addr == 0xFF)
  395. {
  396. LOG_E("phy not probe!");
  397. return;
  398. }
  399. else
  400. {
  401. LOG_D("found a phy, address:0x%02X", phy_addr);
  402. }
  403. /* RESET PHY */
  404. LOG_D("RESET PHY!");
  405. HAL_ETH_WritePHYRegister(&EthHandle, PHY_BASIC_CONTROL_REG, PHY_RESET_MASK);
  406. rt_thread_mdelay(2000);
  407. HAL_ETH_WritePHYRegister(&EthHandle, PHY_BASIC_CONTROL_REG, PHY_AUTO_NEGOTIATION_MASK);
  408. while (1)
  409. {
  410. HAL_ETH_ReadPHYRegister(&EthHandle, PHY_BASIC_STATUS_REG, (uint32_t *)&status);
  411. LOG_D("PHY BASIC STATUS REG:0x%04X", status);
  412. phy_speed_new = 0;
  413. if (status & (PHY_AUTONEGO_COMPLETE_MASK | PHY_LINKED_STATUS_MASK))
  414. {
  415. rt_uint32_t SR;
  416. phy_speed_new = PHY_LINK_MASK;
  417. SR = HAL_ETH_ReadPHYRegister(&EthHandle, PHY_Status_REG, (uint32_t *)&SR);
  418. LOG_D("PHY Control/Status REG:0x%04X ", SR);
  419. if (SR & PHY_100M_MASK)
  420. {
  421. phy_speed_new |= PHY_100M_MASK;
  422. }
  423. else if (SR & PHY_10M_MASK)
  424. {
  425. phy_speed_new |= PHY_10M_MASK;
  426. }
  427. if (SR & PHY_FULL_DUPLEX_MASK)
  428. {
  429. phy_speed_new |= PHY_FULL_DUPLEX_MASK;
  430. }
  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 (Rx_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. /* init tx semaphore */
  534. rt_sem_init(&tx_wait, "tx_wait", 0, RT_IPC_FLAG_FIFO);
  535. LOG_D("initialize tx wait semaphore");
  536. /* register eth device */
  537. state = eth_device_init(&(stm32_eth_device.parent), "e0");
  538. if (RT_EOK == state)
  539. {
  540. LOG_D("emac device init success");
  541. }
  542. else
  543. {
  544. LOG_E("emac device init faild: %d", state);
  545. state = -RT_ERROR;
  546. goto __exit;
  547. }
  548. /* start phy monitor */
  549. rt_thread_t tid;
  550. tid = rt_thread_create("phy",
  551. phy_monitor_thread_entry,
  552. RT_NULL,
  553. 1024,
  554. RT_THREAD_PRIORITY_MAX - 2,
  555. 2);
  556. if (tid != RT_NULL)
  557. {
  558. rt_thread_startup(tid);
  559. }
  560. else
  561. {
  562. state = -RT_ERROR;
  563. }
  564. __exit:
  565. if (state != RT_EOK)
  566. {
  567. if (Rx_Buff)
  568. {
  569. rt_free(Rx_Buff);
  570. }
  571. if (Tx_Buff)
  572. {
  573. rt_free(Tx_Buff);
  574. }
  575. if (DMARxDscrTab)
  576. {
  577. rt_free(DMARxDscrTab);
  578. }
  579. if (DMATxDscrTab)
  580. {
  581. rt_free(DMATxDscrTab);
  582. }
  583. }
  584. return state;
  585. }
  586. INIT_APP_EXPORT(rt_hw_stm32_eth_init);