drv_eth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-10-10 Tanek the first version
  9. * 2019-5-10 misonyo add DMA TX and RX function
  10. * 2020-10-14 wangqiang use phy device in phy monitor thread
  11. * 2022-08-29 xjy198903 add 1170 rgmii support
  12. */
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #ifdef RT_USING_FINSH
  16. #include <finsh.h>
  17. #endif
  18. #include "fsl_enet.h"
  19. #ifdef RT_USING_LWIP
  20. #include <netif/ethernetif.h>
  21. #include "lwipopts.h"
  22. #define DBG_TAG "drv.eth"
  23. #define DBG_LVL DBG_INFO
  24. #include <rtdbg.h>
  25. /* The PHY ID one register */
  26. #define PHY_ID1_REG 0x02U
  27. /* The PHY ID two register */
  28. #define PHY_ID2_REG 0x03U
  29. /* The PHY auto-negotiate advertise register */
  30. #define PHY_AUTONEG_ADVERTISE_REG 0x04U
  31. /* The PHY basic control register */
  32. #define PHY_BASIC_CONTROL_REG 0x00U
  33. #define PHY_RESET_MASK (1<<15)
  34. #define PHY_AUTO_NEGOTIATION_MASK (1<<12)
  35. #define PHY_LINK (1 << 0)
  36. #define PHY_100M (1 << 1)
  37. #define PHY_FULL_DUPLEX (1 << 2)
  38. /* The PHY basic status register */
  39. #define PHY_BASIC_STATUS_REG 0x01U
  40. #define PHY_LINKED_STATUS_MASK (1<<2)
  41. #define PHY_AUTONEGO_COMPLETE_MASK (1<<5)
  42. /* The PHY status register. */
  43. #define PHY_Status_REG 0x1FU
  44. #define PHY_100M_MASK (1<<3)
  45. #define PHY_FULL_DUPLEX_MASK (1<<4)
  46. #define PHY_Status_SPEED_100M(sr) ((sr) & PHY_100M_MASK)
  47. #define PHY_Status_FULL_DUPLEX(sr) ((sr) & PHY_FULL_DUPLEX_MASK)
  48. //extern phy_lan8741_resource_t g_phy_resource;
  49. #define EXAMPLE_ENET_BASE ENET0
  50. #define EXAMPLE_PHY_ADDRESS 0x00U
  51. #define ENET_EXAMPLE_IRQ ETHERNET_IRQn
  52. #define EXAMPLE_CLOCK_FREQ (50000000U)
  53. #define ENET_RXBD_NUM (4)
  54. #define ENET_TXBD_NUM (4)
  55. #define ENET_RXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
  56. #define ENET_BuffSizeAlign(n) ENET_ALIGN(n, ENET_BUFF_ALIGNMENT)
  57. #define ENET_ALIGN(x, align) ((unsigned int)((x) + ((align)-1)) & (unsigned int)(~(unsigned int)((align)-1)))
  58. rt_align(4) enet_rx_bd_struct_t g_rxBuffDescrip[ENET_RXBD_NUM];
  59. rt_align(4) enet_tx_bd_struct_t g_txBuffDescrip[ENET_RXBD_NUM];
  60. static enet_tx_reclaim_info_t g_txDirty[ENET_TXBD_NUM];
  61. #define MAX_ADDR_LEN 6
  62. struct rt_stm32_eth
  63. {
  64. /* inherit from ethernet device */
  65. struct eth_device parent;
  66. #ifndef PHY_USING_INTERRUPT_MODE
  67. rt_timer_t poll_link_timer;
  68. #endif
  69. /* interface address info, hw address */
  70. rt_uint8_t dev_addr[MAX_ADDR_LEN];
  71. enet_handle_t g_handle;
  72. rt_uint32_t rx_channel;
  73. };
  74. static struct rt_stm32_eth stm32_eth_device;
  75. static void ENET_IntCallback(ENET_Type *base, enet_handle_t *handle, enet_event_t event, uint8_t channel, enet_tx_reclaim_info_t *txReclaimInfo, void *param)
  76. {
  77. rt_err_t result;
  78. switch (event)
  79. {
  80. case kENET_TxIntEvent:
  81. /* Get frame info after whole frame transmits out */
  82. // if (txReclaimInfo != NULL)
  83. // {
  84. // rt_free((*txReclaimInfo).context);
  85. // }
  86. break;
  87. case kENET_RxIntEvent:
  88. stm32_eth_device.rx_channel = channel;
  89. result = eth_device_ready(&(stm32_eth_device.parent));
  90. if (result != RT_EOK)
  91. {
  92. LOG_I("_enet_rx_data err = %d", result);
  93. }
  94. default:
  95. break;
  96. }
  97. }
  98. static rt_err_t rt_stm32_eth_init(rt_device_t dev)
  99. {
  100. uint32_t count = 0;
  101. bool link = false;
  102. bool autonego = false;
  103. status_t status;
  104. enet_config_t config;
  105. int i;
  106. uint32_t rxbuffer[ENET_RXBD_NUM];
  107. struct rt_stm32_eth *eth = (struct rt_stm32_eth*)dev->user_data;
  108. for (i = 0; i < ENET_RXBD_NUM; i++)
  109. {
  110. /* This is for rx buffers, static alloc and dynamic alloc both ok. use as your wish. */
  111. rxbuffer[i] = (uint32_t)rt_malloc(ENET_RXBUFF_SIZE);
  112. }
  113. /* prepare the buffer configuration. */
  114. enet_buffer_config_t buffConfig[1] = {{
  115. ENET_RXBD_NUM,
  116. ENET_TXBD_NUM,
  117. &g_txBuffDescrip[0],
  118. &g_txBuffDescrip[0],
  119. &g_txDirty[0],
  120. &g_rxBuffDescrip[0],
  121. &g_rxBuffDescrip[ENET_RXBD_NUM],
  122. &rxbuffer[0],
  123. ENET_BuffSizeAlign(ENET_RXBUFF_SIZE),
  124. }};
  125. CLOCK_AttachClk(kNONE_to_ENETRMII);
  126. CLOCK_EnableClock(kCLOCK_Enet);
  127. ENET_SetSMI(EXAMPLE_ENET_BASE, CLOCK_GetCoreSysClkFreq());
  128. ENET_GetDefaultConfig(&config);
  129. /* Use the actual speed and duplex when phy success to finish the autonegotiation. */
  130. config.miiSpeed = kENET_MiiSpeed100M;
  131. config.miiDuplex = kENET_MiiFullDuplex;
  132. config.interrupt = (kENET_DmaRx) | (kENET_DmaTx);
  133. ENET_Init(EXAMPLE_ENET_BASE, &config, &stm32_eth_device.dev_addr[0], EXAMPLE_CLOCK_FREQ);
  134. ENET_DescriptorInit(EXAMPLE_ENET_BASE, &config, &buffConfig[0]);
  135. ENET_CreateHandler(EXAMPLE_ENET_BASE, &eth->g_handle, &config, &buffConfig[0], ENET_IntCallback, NULL);
  136. ENET_StartRxTx(EXAMPLE_ENET_BASE, 1, 1);
  137. return RT_EOK;
  138. }
  139. static rt_err_t rt_stm32_eth_control(rt_device_t dev, int cmd, void *args)
  140. {
  141. switch (cmd)
  142. {
  143. case NIOCTL_GADDR:
  144. /* get mac address */
  145. if (args)
  146. {
  147. SMEMCPY(args, stm32_eth_device.dev_addr, 6);
  148. }
  149. else
  150. {
  151. return -RT_ERROR;
  152. }
  153. break;
  154. default :
  155. break;
  156. }
  157. return RT_EOK;
  158. }
  159. rt_err_t rt_stm32_eth_tx(rt_device_t dev, struct pbuf *p)
  160. {
  161. struct pbuf *q;
  162. status_t status;
  163. struct rt_stm32_eth *eth = (struct rt_stm32_eth*)dev->user_data;
  164. // LOG_D("rt_stm32_eth_tx: len: %d, tot_len:%d", p->len, p->tot_len);
  165. enet_buffer_struct_t txBuff[ENET_TXBD_NUM];
  166. enet_tx_frame_struct_t txFrame = {0};
  167. for (q = p; q != NULL; q = q->next)
  168. {
  169. txBuff[txFrame.txBuffNum].buffer = q->payload;
  170. txBuff[txFrame.txBuffNum].length = q->len;
  171. txFrame.txBuffNum++;
  172. }
  173. txFrame.txBuffArray = txBuff;
  174. txFrame.txConfig.intEnable = true;
  175. txFrame.txConfig.tsEnable = true;
  176. txFrame.context = RT_NULL;
  177. status = ENET_SendFrame(EXAMPLE_ENET_BASE, &eth->g_handle, &txFrame, 0);
  178. return (status == kStatus_Success);
  179. }
  180. struct pbuf *rt_stm32_eth_rx(rt_device_t dev)
  181. {
  182. struct pbuf *p = RT_NULL;
  183. uint32_t len = 0;
  184. status_t status;
  185. struct rt_stm32_eth *eth = (struct rt_stm32_eth*)dev->user_data;
  186. status = ENET_GetRxFrameSize(EXAMPLE_ENET_BASE, &eth->g_handle, &len, eth->rx_channel);
  187. // LOG_D("rt_stm32_eth_rx: status:%d, length: %d, channel:%d", status, len, eth->rx_channel);
  188. if (len != 0 && (status == kStatus_Success))
  189. {
  190. /* Received valid frame. Deliver the rx buffer with the size equal to length. */
  191. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  192. if (p != NULL)
  193. {
  194. status = ENET_ReadFrame(EXAMPLE_ENET_BASE, &eth->g_handle, p->payload, len, eth->rx_channel, NULL);
  195. if (status == kStatus_Success)
  196. {
  197. return p;
  198. }
  199. else
  200. {
  201. LOG_D(" A frame read failed\n");
  202. pbuf_free(p);
  203. }
  204. }
  205. else
  206. {
  207. LOG_D(" pbuf_alloc faild\n");
  208. }
  209. }
  210. else if (status == kStatus_ENET_RxFrameError)
  211. {
  212. /* update the receive buffer. */
  213. ENET_ReadFrame(EXAMPLE_ENET_BASE, &eth->g_handle, NULL, 0, eth->rx_channel, NULL);
  214. }
  215. return RT_NULL;
  216. }
  217. static void phy_monitor_thread_entry(void *parameter)
  218. {
  219. uint8_t phy_addr = 0xFF;
  220. uint8_t detected_count = 0;
  221. while(phy_addr == 0xFF)
  222. {
  223. /* phy search */
  224. rt_uint16_t i, temp;
  225. for (i = 0; i <= 0x1F; i++)
  226. {
  227. // EthHandle.Init.PhyAddress = i;
  228. ENET_MDIORead(EXAMPLE_ENET_BASE, i, PHY_ID1_REG, &temp);
  229. if (temp != 0xFFFF && temp != 0x00)
  230. {
  231. phy_addr = i;
  232. break;
  233. }
  234. }
  235. detected_count++;
  236. rt_thread_mdelay(1000);
  237. if (detected_count > 10)
  238. {
  239. LOG_E("No PHY device was detected, please check hardware!");
  240. }
  241. }
  242. LOG_D("Found a phy, address:0x%02X", phy_addr);
  243. /* RESET PHY */
  244. LOG_D("RESET PHY!");
  245. ENET_MDIOWrite(EXAMPLE_ENET_BASE, phy_addr, PHY_BASIC_CONTROL_REG, PHY_RESET_MASK);
  246. rt_thread_mdelay(2000);
  247. ENET_MDIOWrite(EXAMPLE_ENET_BASE, phy_addr, PHY_BASIC_CONTROL_REG, PHY_AUTO_NEGOTIATION_MASK);
  248. while(1)
  249. {
  250. static rt_uint8_t phy_speed = 0;
  251. uint8_t phy_speed_new = 0;
  252. uint16_t status;
  253. ENET_MDIORead(EXAMPLE_ENET_BASE, phy_addr, PHY_BASIC_STATUS_REG, &status);
  254. LOG_D("phy basic status reg is 0x%X", status);
  255. if (status & (PHY_AUTONEGO_COMPLETE_MASK | PHY_LINKED_STATUS_MASK))
  256. {
  257. uint16_t SR = 0;
  258. phy_speed_new |= PHY_LINK;
  259. ENET_MDIORead(EXAMPLE_ENET_BASE, phy_addr, PHY_Status_REG, &SR);
  260. LOG_D("phy control status reg is 0x%X", SR);
  261. if (PHY_Status_SPEED_100M(SR))
  262. {
  263. phy_speed_new |= PHY_100M;
  264. }
  265. if (PHY_Status_FULL_DUPLEX(SR))
  266. {
  267. phy_speed_new |= PHY_FULL_DUPLEX;
  268. }
  269. }
  270. if (phy_speed != phy_speed_new)
  271. {
  272. phy_speed = phy_speed_new;
  273. if (phy_speed & PHY_LINK)
  274. {
  275. LOG_D("link up");
  276. if (phy_speed & PHY_100M)
  277. {
  278. LOG_D("100Mbps");
  279. }
  280. else
  281. {
  282. LOG_D("10Mbps");
  283. }
  284. if (phy_speed & PHY_FULL_DUPLEX)
  285. {
  286. LOG_D("full-duplex");
  287. }
  288. else
  289. {
  290. LOG_D("half-duplex");
  291. }
  292. /* send link up. */
  293. eth_device_linkchange(&stm32_eth_device.parent, RT_TRUE);
  294. }
  295. else
  296. {
  297. LOG_I("link down");
  298. eth_device_linkchange(&stm32_eth_device.parent, RT_FALSE);
  299. }
  300. }
  301. rt_thread_mdelay(1000);
  302. }
  303. }
  304. static int rt_hw_imxrt_eth_init(void)
  305. {
  306. rt_err_t state = RT_EOK;
  307. stm32_eth_device.dev_addr[0] = 0x00;
  308. stm32_eth_device.dev_addr[1] = 0x80;
  309. stm32_eth_device.dev_addr[2] = 0xE1;
  310. stm32_eth_device.dev_addr[3] = 0x01;
  311. stm32_eth_device.dev_addr[4] = 0x02;
  312. stm32_eth_device.dev_addr[5] = 0x03;
  313. stm32_eth_device.parent.parent.init = rt_stm32_eth_init;
  314. stm32_eth_device.parent.parent.open = RT_NULL;
  315. stm32_eth_device.parent.parent.close = RT_NULL;
  316. stm32_eth_device.parent.parent.read = RT_NULL;
  317. stm32_eth_device.parent.parent.write = RT_NULL;
  318. stm32_eth_device.parent.parent.control = rt_stm32_eth_control;
  319. stm32_eth_device.parent.parent.user_data = &stm32_eth_device;
  320. stm32_eth_device.parent.eth_rx = rt_stm32_eth_rx;
  321. stm32_eth_device.parent.eth_tx = rt_stm32_eth_tx;
  322. /* register eth device */
  323. state = eth_device_init(&(stm32_eth_device.parent), "e0");
  324. if (RT_EOK != state)
  325. {
  326. LOG_E("emac device init faild: %d", state);
  327. state = -RT_ERROR;
  328. }
  329. /* start phy monitor */
  330. rt_thread_startup(rt_thread_create("phy", phy_monitor_thread_entry, RT_NULL, 1024, RT_THREAD_PRIORITY_MAX - 2, 2));
  331. return state;
  332. }
  333. INIT_DEVICE_EXPORT(rt_hw_imxrt_eth_init);
  334. #endif