drv_eth.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*
  2. * Copyright (c) 2006-2022, 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. */
  12. #include <rtthread.h>
  13. #include "board.h"
  14. #include <rtdevice.h>
  15. #ifdef RT_USING_FINSH
  16. #include <finsh.h>
  17. #endif
  18. #include "fsl_enet.h"
  19. #include "fsl_gpio.h"
  20. #include "fsl_cache.h"
  21. #include "fsl_iomuxc.h"
  22. #include "fsl_common.h"
  23. #ifdef RT_USING_LWIP
  24. #include <netif/ethernetif.h>
  25. #include "lwipopts.h"
  26. #define ENET_RXBD_NUM (4)
  27. #define ENET_TXBD_NUM (4)
  28. #define ENET_RXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
  29. #define ENET_TXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
  30. /* debug option */
  31. #undef ETH_RX_DUMP
  32. #undef ETH_TX_DUMP
  33. #define DBG_ENABLE
  34. #define DBG_SECTION_NAME "[ETH]"
  35. #define DBG_COLOR
  36. #define DBG_LEVEL DBG_INFO
  37. #include <rtdbg.h>
  38. #define MAX_ADDR_LEN 6
  39. struct rt_imxrt_eth
  40. {
  41. /* inherit from ethernet device */
  42. struct eth_device parent;
  43. enet_handle_t enet_handle;
  44. ENET_Type *enet_base;
  45. enet_data_error_stats_t error_statistic;
  46. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  47. rt_bool_t tx_is_waiting;
  48. struct rt_semaphore tx_wait;
  49. enet_mii_speed_t speed;
  50. enet_mii_duplex_t duplex;
  51. };
  52. AT_NONCACHEABLE_SECTION_ALIGN(enet_tx_bd_struct_t g_txBuffDescrip[ENET_TXBD_NUM], ENET_BUFF_ALIGNMENT);
  53. ALIGN(ENET_BUFF_ALIGNMENT) rt_uint8_t g_txDataBuff[ENET_TXBD_NUM][RT_ALIGN(ENET_TXBUFF_SIZE, ENET_BUFF_ALIGNMENT)];
  54. AT_NONCACHEABLE_SECTION_ALIGN(enet_rx_bd_struct_t g_rxBuffDescrip[ENET_RXBD_NUM], ENET_BUFF_ALIGNMENT);
  55. ALIGN(ENET_BUFF_ALIGNMENT) rt_uint8_t g_rxDataBuff[ENET_RXBD_NUM][RT_ALIGN(ENET_RXBUFF_SIZE, ENET_BUFF_ALIGNMENT)];
  56. static struct rt_imxrt_eth imxrt_eth_device;
  57. void _enet_rx_callback(struct rt_imxrt_eth *eth)
  58. {
  59. rt_err_t result;
  60. ENET_DisableInterrupts(eth->enet_base, kENET_RxFrameInterrupt);
  61. result = eth_device_ready(&(eth->parent));
  62. if (result != RT_EOK)
  63. rt_kprintf("RX err =%d\n", result);
  64. }
  65. void _enet_tx_callback(struct rt_imxrt_eth *eth)
  66. {
  67. if (eth->tx_is_waiting == RT_TRUE)
  68. {
  69. eth->tx_is_waiting = RT_FALSE;
  70. rt_sem_release(&eth->tx_wait);
  71. }
  72. }
  73. void _enet_callback(ENET_Type *base, enet_handle_t *handle, enet_event_t event, void *userData)
  74. {
  75. switch (event)
  76. {
  77. case kENET_RxEvent:
  78. _enet_rx_callback((struct rt_imxrt_eth *)userData);
  79. break;
  80. case kENET_TxEvent:
  81. _enet_tx_callback((struct rt_imxrt_eth *)userData);
  82. break;
  83. case kENET_ErrEvent:
  84. dbg_log(DBG_LOG, "kENET_ErrEvent\n");
  85. break;
  86. case kENET_WakeUpEvent:
  87. dbg_log(DBG_LOG, "kENET_WakeUpEvent\n");
  88. break;
  89. case kENET_TimeStampEvent:
  90. dbg_log(DBG_LOG, "kENET_TimeStampEvent\n");
  91. break;
  92. case kENET_TimeStampAvailEvent:
  93. dbg_log(DBG_LOG, "kENET_TimeStampAvailEvent \n");
  94. break;
  95. default:
  96. dbg_log(DBG_LOG, "unknow error\n");
  97. break;
  98. }
  99. }
  100. static void _enet_clk_init(void)
  101. {
  102. #ifdef SOC_IMXRT1170_SERIES
  103. const clock_sys_pll1_config_t sysPll1Config = {
  104. .pllDiv2En = true,
  105. };
  106. CLOCK_InitSysPll1(&sysPll1Config);
  107. clock_root_config_t rootCfg = {.mux = 4, .div = 10}; /* Generate 50M root clock. */
  108. CLOCK_SetRootClock(kCLOCK_Root_Enet1, &rootCfg);
  109. /* Select syspll2pfd3, 528*18/24 = 396M */
  110. CLOCK_InitPfd(kCLOCK_PllSys2, kCLOCK_Pfd3, 24);
  111. rootCfg.mux = 7;
  112. rootCfg.div = 2;
  113. CLOCK_SetRootClock(kCLOCK_Root_Bus, &rootCfg); /* Generate 198M bus clock. */
  114. IOMUXC_GPR->GPR4 |= 0x3;
  115. #else
  116. const clock_enet_pll_config_t config = {.enableClkOutput = true, .enableClkOutput25M = false, .loopDivider = 1};
  117. CLOCK_InitEnetPll(&config);
  118. IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir, true);
  119. IOMUXC_GPR->GPR1|=1<<23;
  120. #endif
  121. }
  122. static void _enet_config(void)
  123. {
  124. enet_config_t config;
  125. uint32_t sysClock;
  126. /* prepare the buffer configuration. */
  127. enet_buffer_config_t buffConfig =
  128. {
  129. ENET_RXBD_NUM,
  130. ENET_TXBD_NUM,
  131. SDK_SIZEALIGN(ENET_RXBUFF_SIZE, ENET_BUFF_ALIGNMENT),
  132. SDK_SIZEALIGN(ENET_TXBUFF_SIZE, ENET_BUFF_ALIGNMENT),
  133. &g_rxBuffDescrip[0],
  134. &g_txBuffDescrip[0],
  135. &g_rxDataBuff[0][0],
  136. &g_txDataBuff[0][0],
  137. };
  138. /* Get default configuration. */
  139. /*
  140. * config.miiMode = kENET_RmiiMode;
  141. * config.miiSpeed = kENET_MiiSpeed100M;
  142. * config.miiDuplex = kENET_MiiFullDuplex;
  143. * config.rxMaxFrameLen = ENET_FRAME_MAX_FRAMELEN;
  144. */
  145. ENET_GetDefaultConfig(&config);
  146. config.interrupt = kENET_TxFrameInterrupt | kENET_RxFrameInterrupt;
  147. config.miiSpeed = imxrt_eth_device.speed;
  148. config.miiDuplex = imxrt_eth_device.duplex;
  149. /* Set SMI to get PHY link status. */
  150. #ifdef SOC_IMXRT1170_SERIES
  151. sysClock = CLOCK_GetRootClockFreq(kCLOCK_Root_Bus);
  152. #else
  153. sysClock = CLOCK_GetFreq(kCLOCK_AhbClk);
  154. #endif
  155. dbg_log(DBG_LOG, "deinit\n");
  156. ENET_Deinit(imxrt_eth_device.enet_base);
  157. dbg_log(DBG_LOG, "init\n");
  158. ENET_Init(imxrt_eth_device.enet_base, &imxrt_eth_device.enet_handle, &config, &buffConfig, &imxrt_eth_device.dev_addr[0], sysClock);
  159. dbg_log(DBG_LOG, "set call back\n");
  160. ENET_SetCallback(&imxrt_eth_device.enet_handle, _enet_callback, &imxrt_eth_device);
  161. dbg_log(DBG_LOG, "active read\n");
  162. ENET_ActiveRead(imxrt_eth_device.enet_base);
  163. }
  164. #if defined(ETH_RX_DUMP) || defined(ETH_TX_DUMP)
  165. static void packet_dump(const char *msg, const struct pbuf *p)
  166. {
  167. const struct pbuf *q;
  168. rt_uint32_t i, j;
  169. rt_uint8_t *ptr;
  170. rt_kprintf("%s %d byte\n", msg, p->tot_len);
  171. i = 0;
  172. for (q = p; q != RT_NULL; q = q->next)
  173. {
  174. ptr = q->payload;
  175. for (j = 0; j < q->len; j++)
  176. {
  177. if ((i % 8) == 0)
  178. {
  179. rt_kprintf(" ");
  180. }
  181. if ((i % 16) == 0)
  182. {
  183. rt_kprintf("\r\n");
  184. }
  185. rt_kprintf("%02x ", *ptr);
  186. i++;
  187. ptr++;
  188. }
  189. }
  190. rt_kprintf("\n\n");
  191. }
  192. #else
  193. #define packet_dump(...)
  194. #endif /* dump */
  195. /* initialize the interface */
  196. static rt_err_t rt_imxrt_eth_init(rt_device_t dev)
  197. {
  198. dbg_log(DBG_LOG, "rt_imxrt_eth_init...\n");
  199. _enet_config();
  200. return RT_EOK;
  201. }
  202. static rt_err_t rt_imxrt_eth_open(rt_device_t dev, rt_uint16_t oflag)
  203. {
  204. dbg_log(DBG_LOG, "rt_imxrt_eth_open...\n");
  205. return RT_EOK;
  206. }
  207. static rt_err_t rt_imxrt_eth_close(rt_device_t dev)
  208. {
  209. dbg_log(DBG_LOG, "rt_imxrt_eth_close...\n");
  210. return RT_EOK;
  211. }
  212. static rt_size_t rt_imxrt_eth_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  213. {
  214. dbg_log(DBG_LOG, "rt_imxrt_eth_read...\n");
  215. rt_set_errno(-RT_ENOSYS);
  216. return 0;
  217. }
  218. static rt_size_t rt_imxrt_eth_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  219. {
  220. dbg_log(DBG_LOG, "rt_imxrt_eth_write...\n");
  221. rt_set_errno(-RT_ENOSYS);
  222. return 0;
  223. }
  224. static rt_err_t rt_imxrt_eth_control(rt_device_t dev, int cmd, void *args)
  225. {
  226. dbg_log(DBG_LOG, "rt_imxrt_eth_control...\n");
  227. switch (cmd)
  228. {
  229. case NIOCTL_GADDR:
  230. /* get mac address */
  231. if (args) rt_memcpy(args, imxrt_eth_device.dev_addr, 6);
  232. else return -RT_ERROR;
  233. break;
  234. default :
  235. break;
  236. }
  237. return RT_EOK;
  238. }
  239. static void _ENET_ActiveSend(ENET_Type *base, uint32_t ringId)
  240. {
  241. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  242. switch (ringId)
  243. {
  244. case 0:
  245. base->TDAR = ENET_TDAR_TDAR_MASK;
  246. break;
  247. #if FSL_FEATURE_ENET_QUEUE > 1
  248. case 1:
  249. base->TDAR1 = ENET_TDAR1_TDAR_MASK;
  250. break;
  251. case 2:
  252. base->TDAR2 = ENET_TDAR2_TDAR_MASK;
  253. break;
  254. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  255. default:
  256. base->TDAR = ENET_TDAR_TDAR_MASK;
  257. break;
  258. }
  259. }
  260. static status_t _ENET_SendFrame(ENET_Type *base, enet_handle_t *handle, const uint8_t *data, uint32_t length)
  261. {
  262. assert(handle);
  263. assert(data);
  264. volatile enet_tx_bd_struct_t *curBuffDescrip;
  265. uint32_t len = 0;
  266. uint32_t sizeleft = 0;
  267. uint32_t address;
  268. /* Check the frame length. */
  269. if (length > ENET_FRAME_MAX_FRAMELEN)
  270. {
  271. return kStatus_ENET_TxFrameOverLen;
  272. }
  273. /* Check if the transmit buffer is ready. */
  274. curBuffDescrip = handle->txBdCurrent[0];
  275. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  276. {
  277. return kStatus_ENET_TxFrameBusy;
  278. }
  279. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  280. bool isPtpEventMessage = false;
  281. /* Check PTP message with the PTP header. */
  282. isPtpEventMessage = ENET_Ptp1588ParseFrame(data, NULL, true);
  283. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  284. /* One transmit buffer is enough for one frame. */
  285. if (handle->txBuffSizeAlign[0] >= length)
  286. {
  287. /* Copy data to the buffer for uDMA transfer. */
  288. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  289. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  290. #else
  291. address = (uint32_t)curBuffDescrip->buffer;
  292. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  293. pbuf_copy_partial((const struct pbuf *)data, (void *)address, length, 0);
  294. /* Set data length. */
  295. curBuffDescrip->length = length;
  296. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  297. /* For enable the timestamp. */
  298. if (isPtpEventMessage)
  299. {
  300. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  301. }
  302. else
  303. {
  304. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  305. }
  306. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  307. curBuffDescrip->control |= (ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK);
  308. /* Increase the buffer descriptor address. */
  309. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  310. {
  311. handle->txBdCurrent[0] = handle->txBdBase[0];
  312. }
  313. else
  314. {
  315. handle->txBdCurrent[0]++;
  316. }
  317. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  318. /* Add the cache clean maintain. */
  319. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  320. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  321. #else
  322. address = (uint32_t)curBuffDescrip->buffer;
  323. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  324. DCACHE_CleanByRange(address, length);
  325. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  326. /* Active the transmit buffer descriptor. */
  327. _ENET_ActiveSend(base, 0);
  328. return kStatus_Success;
  329. }
  330. else
  331. {
  332. /* One frame requires more than one transmit buffers. */
  333. do
  334. {
  335. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  336. /* For enable the timestamp. */
  337. if (isPtpEventMessage)
  338. {
  339. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  340. }
  341. else
  342. {
  343. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  344. }
  345. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  346. /* Increase the buffer descriptor address. */
  347. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  348. {
  349. handle->txBdCurrent[0] = handle->txBdBase[0];
  350. }
  351. else
  352. {
  353. handle->txBdCurrent[0]++;
  354. }
  355. /* update the size left to be transmit. */
  356. sizeleft = length - len;
  357. if (sizeleft > handle->txBuffSizeAlign[0])
  358. {
  359. /* Data copy. */
  360. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  361. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  362. #else
  363. address = (uint32_t)curBuffDescrip->buffer;
  364. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  365. memcpy((void *)address, data + len, handle->txBuffSizeAlign[0]);
  366. /* Data length update. */
  367. curBuffDescrip->length = handle->txBuffSizeAlign[0];
  368. len += handle->txBuffSizeAlign[0];
  369. /* Sets the control flag. */
  370. curBuffDescrip->control &= ~ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  371. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK;
  372. /* Active the transmit buffer descriptor*/
  373. _ENET_ActiveSend(base, 0);
  374. }
  375. else
  376. {
  377. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  378. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  379. #else
  380. address = (uint32_t)curBuffDescrip->buffer;
  381. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  382. memcpy((void *)address, data + len, sizeleft);
  383. curBuffDescrip->length = sizeleft;
  384. /* Set Last buffer wrap flag. */
  385. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  386. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  387. /* Add the cache clean maintain. */
  388. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  389. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  390. #else
  391. address = (uint32_t)curBuffDescrip->buffer;
  392. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  393. DCACHE_CleanByRange(address, handle->txBuffSizeAlign[0]);
  394. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  395. /* Active the transmit buffer descriptor. */
  396. _ENET_ActiveSend(base, 0);
  397. return kStatus_Success;
  398. }
  399. /* Get the current buffer descriptor address. */
  400. curBuffDescrip = handle->txBdCurrent[0];
  401. } while (!(curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK));
  402. return kStatus_ENET_TxFrameBusy;
  403. }
  404. }
  405. /* ethernet device interface */
  406. /* transmit packet. */
  407. rt_err_t rt_imxrt_eth_tx(rt_device_t dev, struct pbuf *p)
  408. {
  409. rt_err_t result = RT_EOK;
  410. enet_handle_t * enet_handle = &imxrt_eth_device.enet_handle;
  411. RT_ASSERT(p != NULL);
  412. RT_ASSERT(enet_handle != RT_NULL);
  413. dbg_log(DBG_LOG, "rt_imxrt_eth_tx: %d\n", p->len);
  414. #ifdef ETH_TX_DUMP
  415. packet_dump("send", p);
  416. #endif
  417. do
  418. {
  419. result = _ENET_SendFrame(imxrt_eth_device.enet_base, enet_handle, (const uint8_t *)p, p->tot_len);
  420. if (result == kStatus_ENET_TxFrameBusy)
  421. {
  422. imxrt_eth_device.tx_is_waiting = RT_TRUE;
  423. rt_sem_take(&imxrt_eth_device.tx_wait, RT_WAITING_FOREVER);
  424. }
  425. }
  426. while (result == kStatus_ENET_TxFrameBusy);
  427. return RT_EOK;
  428. }
  429. /* reception packet. */
  430. struct pbuf *rt_imxrt_eth_rx(rt_device_t dev)
  431. {
  432. uint32_t length = 0;
  433. status_t status;
  434. struct pbuf *p = RT_NULL;
  435. enet_handle_t *enet_handle = &imxrt_eth_device.enet_handle;
  436. ENET_Type *enet_base = imxrt_eth_device.enet_base;
  437. enet_data_error_stats_t *error_statistic = &imxrt_eth_device.error_statistic;
  438. /* Get the Frame size */
  439. status = ENET_GetRxFrameSize(enet_handle, &length);
  440. /* Call ENET_ReadFrame when there is a received frame. */
  441. if (length != 0)
  442. {
  443. /* Received valid frame. Deliver the rx buffer with the size equal to length. */
  444. p = pbuf_alloc(PBUF_RAW, length, PBUF_POOL);
  445. if (p != NULL)
  446. {
  447. status = ENET_ReadFrame(enet_base, enet_handle, p->payload, length);
  448. if (status == kStatus_Success)
  449. {
  450. #ifdef ETH_RX_DUMP
  451. packet_dump("recv", p);
  452. #endif
  453. return p;
  454. }
  455. else
  456. {
  457. dbg_log(DBG_LOG, " A frame read failed\n");
  458. pbuf_free(p);
  459. }
  460. }
  461. else
  462. {
  463. dbg_log(DBG_LOG, " pbuf_alloc faild\n");
  464. }
  465. }
  466. else if (status == kStatus_ENET_RxFrameError)
  467. {
  468. dbg_log(DBG_WARNING, "ENET_GetRxFrameSize: kStatus_ENET_RxFrameError\n");
  469. /* Update the received buffer when error happened. */
  470. /* Get the error information of the received g_frame. */
  471. ENET_GetRxErrBeforeReadFrame(enet_handle, error_statistic);
  472. /* update the receive buffer. */
  473. ENET_ReadFrame(enet_base, enet_handle, NULL, 0);
  474. }
  475. ENET_EnableInterrupts(enet_base, kENET_RxFrameInterrupt);
  476. return NULL;
  477. }
  478. #ifdef BSP_USING_PHY
  479. static struct rt_phy_device *phy_dev = RT_NULL;
  480. static void phy_monitor_thread_entry(void *parameter)
  481. {
  482. rt_uint32_t speed;
  483. rt_uint32_t duplex;
  484. rt_bool_t link = RT_FALSE;
  485. phy_dev = (struct rt_phy_device *)rt_device_find("rtt-phy");
  486. if ((RT_NULL == phy_dev) || (RT_NULL == phy_dev->ops))
  487. {
  488. // TODO print warning information
  489. LOG_E("Can not find phy device called \"rtt-phy\"");
  490. return ;
  491. }
  492. if (RT_NULL == phy_dev->ops->init)
  493. {
  494. LOG_E("phy driver error!");
  495. return ;
  496. }
  497. #ifdef SOC_IMXRT1170_SERIES
  498. rt_phy_status status = phy_dev->ops->init(imxrt_eth_device.enet_base, PHY_DEVICE_ADDRESS, CLOCK_GetRootClockFreq(kCLOCK_Root_Bus));
  499. #else
  500. rt_phy_status status = phy_dev->ops->init(imxrt_eth_device.enet_base, PHY_DEVICE_ADDRESS, CLOCK_GetFreq(kCLOCK_AhbClk));
  501. #endif
  502. if (PHY_STATUS_OK != status)
  503. {
  504. LOG_E("Phy device initialize unsuccessful!\n");
  505. return ;
  506. }
  507. while (1)
  508. {
  509. rt_bool_t new_link = RT_FALSE;
  510. rt_phy_status status = phy_dev->ops->get_link_status(&new_link);
  511. if ((PHY_STATUS_OK == status) && (link != new_link))
  512. {
  513. link = new_link;
  514. if (link) // link up
  515. {
  516. phy_dev->ops->get_link_speed_duplex(&speed, &duplex);
  517. if (PHY_SPEED_10M == speed)
  518. {
  519. dbg_log(DBG_LOG, "10M\n");
  520. }
  521. else
  522. {
  523. dbg_log(DBG_LOG, "100M\n");
  524. }
  525. if (PHY_HALF_DUPLEX == duplex)
  526. {
  527. dbg_log(DBG_LOG, "half dumplex\n");
  528. }
  529. else
  530. {
  531. dbg_log(DBG_LOG, "full dumplex\n");
  532. }
  533. if ((imxrt_eth_device.speed != (enet_mii_speed_t)speed) || (imxrt_eth_device.duplex != (enet_mii_duplex_t)duplex))
  534. {
  535. imxrt_eth_device.speed = (enet_mii_speed_t)speed;
  536. imxrt_eth_device.duplex = (enet_mii_duplex_t)duplex;
  537. dbg_log(DBG_LOG, "link up, and update eth mode.\n");
  538. rt_imxrt_eth_init((rt_device_t)&imxrt_eth_device);
  539. }
  540. else
  541. {
  542. dbg_log(DBG_LOG, "link up, eth not need re-config.\n");
  543. }
  544. dbg_log(DBG_LOG, "link up.\n");
  545. eth_device_linkchange(&imxrt_eth_device.parent, RT_TRUE);
  546. }
  547. else
  548. {
  549. dbg_log(DBG_LOG, "link down.\n");
  550. eth_device_linkchange(&imxrt_eth_device.parent, RT_FALSE);
  551. }
  552. }
  553. rt_thread_delay(RT_TICK_PER_SECOND * 2);
  554. }
  555. }
  556. #endif
  557. static int rt_hw_imxrt_eth_init(void)
  558. {
  559. rt_err_t state;
  560. _enet_clk_init();
  561. /* NXP (Freescale) MAC OUI */
  562. imxrt_eth_device.dev_addr[0] = 0x00;
  563. imxrt_eth_device.dev_addr[1] = 0x04;
  564. imxrt_eth_device.dev_addr[2] = 0x9F;
  565. /* generate MAC addr from 96bit unique ID (only for test). */
  566. imxrt_eth_device.dev_addr[3] = 0x05;
  567. imxrt_eth_device.dev_addr[4] = 0x44;
  568. imxrt_eth_device.dev_addr[5] = 0xE5;
  569. imxrt_eth_device.speed = kENET_MiiSpeed100M;
  570. imxrt_eth_device.duplex = kENET_MiiFullDuplex;
  571. imxrt_eth_device.enet_base = ENET;
  572. imxrt_eth_device.parent.parent.init = rt_imxrt_eth_init;
  573. imxrt_eth_device.parent.parent.open = rt_imxrt_eth_open;
  574. imxrt_eth_device.parent.parent.close = rt_imxrt_eth_close;
  575. imxrt_eth_device.parent.parent.read = rt_imxrt_eth_read;
  576. imxrt_eth_device.parent.parent.write = rt_imxrt_eth_write;
  577. imxrt_eth_device.parent.parent.control = rt_imxrt_eth_control;
  578. imxrt_eth_device.parent.parent.user_data = RT_NULL;
  579. imxrt_eth_device.parent.eth_rx = rt_imxrt_eth_rx;
  580. imxrt_eth_device.parent.eth_tx = rt_imxrt_eth_tx;
  581. dbg_log(DBG_LOG, "sem init: tx_wait\r\n");
  582. /* init tx semaphore */
  583. rt_sem_init(&imxrt_eth_device.tx_wait, "tx_wait", 0, RT_IPC_FLAG_FIFO);
  584. /* register eth device */
  585. dbg_log(DBG_LOG, "eth_device_init start\r\n");
  586. state = eth_device_init(&(imxrt_eth_device.parent), "e0");
  587. if (RT_EOK == state)
  588. {
  589. dbg_log(DBG_LOG, "eth_device_init success\r\n");
  590. }
  591. else
  592. {
  593. dbg_log(DBG_LOG, "eth_device_init faild: %d\r\n", state);
  594. }
  595. eth_device_linkchange(&imxrt_eth_device.parent, RT_FALSE);
  596. /* start phy monitor */
  597. {
  598. #ifdef BSP_USING_PHY
  599. rt_thread_t tid;
  600. tid = rt_thread_create("phy",
  601. phy_monitor_thread_entry,
  602. RT_NULL,
  603. 512,
  604. RT_THREAD_PRIORITY_MAX - 2,
  605. 2);
  606. if (tid != RT_NULL)
  607. rt_thread_startup(tid);
  608. #endif
  609. }
  610. return state;
  611. }
  612. INIT_DEVICE_EXPORT(rt_hw_imxrt_eth_init);
  613. #endif
  614. #if defined(RT_USING_FINSH) && defined(RT_USING_PHY)
  615. #include <finsh.h>
  616. void phy_read(rt_uint32_t phy_reg)
  617. {
  618. rt_uint32_t data;
  619. rt_phy_status status = phy_dev->ops->read(phy_reg, &data);
  620. if (PHY_STATUS_OK == status)
  621. {
  622. rt_kprintf("PHY_Read: %02X --> %08X", phy_reg, data);
  623. }
  624. else
  625. {
  626. rt_kprintf("PHY_Read: %02X --> faild", phy_reg);
  627. }
  628. }
  629. void phy_write(rt_uint32_t phy_reg, rt_uint32_t data)
  630. {
  631. rt_phy_status status = phy_dev->ops->write(phy_reg, data);
  632. if (PHY_STATUS_OK == status)
  633. {
  634. rt_kprintf("PHY_Write: %02X --> %08X\n", phy_reg, data);
  635. }
  636. else
  637. {
  638. rt_kprintf("PHY_Write: %02X --> faild\n", phy_reg);
  639. }
  640. }
  641. void phy_dump(void)
  642. {
  643. rt_uint32_t data;
  644. rt_phy_status status;
  645. int i;
  646. for (i = 0; i < 32; i++)
  647. {
  648. status = phy_dev->ops->read(i, &data);
  649. if (PHY_STATUS_OK != status)
  650. {
  651. rt_kprintf("phy_dump: %02X --> faild", i);
  652. break;
  653. }
  654. if (i % 8 == 7)
  655. {
  656. rt_kprintf("%02X --> %08X ", i, data);
  657. }
  658. else
  659. {
  660. rt_kprintf("%02X --> %08X\n", i, data);
  661. }
  662. }
  663. }
  664. #endif
  665. #if defined(RT_USING_FINSH) && defined(RT_USING_LWIP)
  666. void enet_reg_dump(void)
  667. {
  668. ENET_Type *enet_base = imxrt_eth_device.enet_base;
  669. #define DUMP_REG(__REG) \
  670. rt_kprintf("%s(%08X): %08X\n", #__REG, (uint32_t)&enet_base->__REG, enet_base->__REG)
  671. DUMP_REG(EIR);
  672. DUMP_REG(EIMR);
  673. DUMP_REG(RDAR);
  674. DUMP_REG(TDAR);
  675. DUMP_REG(ECR);
  676. DUMP_REG(MMFR);
  677. DUMP_REG(MSCR);
  678. DUMP_REG(MIBC);
  679. DUMP_REG(RCR);
  680. DUMP_REG(TCR);
  681. DUMP_REG(PALR);
  682. DUMP_REG(PAUR);
  683. DUMP_REG(OPD);
  684. DUMP_REG(TXIC);
  685. DUMP_REG(RXIC);
  686. DUMP_REG(IAUR);
  687. DUMP_REG(IALR);
  688. DUMP_REG(GAUR);
  689. DUMP_REG(GALR);
  690. DUMP_REG(TFWR);
  691. DUMP_REG(RDSR);
  692. DUMP_REG(TDSR);
  693. DUMP_REG(MRBR);
  694. DUMP_REG(RSFL);
  695. DUMP_REG(RSEM);
  696. DUMP_REG(RAEM);
  697. DUMP_REG(RAFL);
  698. DUMP_REG(TSEM);
  699. DUMP_REG(TAEM);
  700. DUMP_REG(TAFL);
  701. DUMP_REG(TIPG);
  702. DUMP_REG(FTRL);
  703. DUMP_REG(TACC);
  704. DUMP_REG(RACC);
  705. // DUMP_REG(RMON_T_DROP);
  706. DUMP_REG(RMON_T_PACKETS);
  707. DUMP_REG(RMON_T_BC_PKT);
  708. DUMP_REG(RMON_T_MC_PKT);
  709. DUMP_REG(RMON_T_CRC_ALIGN);
  710. DUMP_REG(RMON_T_UNDERSIZE);
  711. DUMP_REG(RMON_T_OVERSIZE);
  712. DUMP_REG(RMON_T_FRAG);
  713. DUMP_REG(RMON_T_JAB);
  714. DUMP_REG(RMON_T_COL);
  715. DUMP_REG(RMON_T_P64);
  716. DUMP_REG(RMON_T_P65TO127);
  717. DUMP_REG(RMON_T_P128TO255);
  718. DUMP_REG(RMON_T_P256TO511);
  719. DUMP_REG(RMON_T_P512TO1023);
  720. DUMP_REG(RMON_T_P1024TO2047);
  721. DUMP_REG(RMON_T_P_GTE2048);
  722. DUMP_REG(RMON_T_OCTETS);
  723. DUMP_REG(IEEE_T_DROP);
  724. DUMP_REG(IEEE_T_FRAME_OK);
  725. DUMP_REG(IEEE_T_1COL);
  726. DUMP_REG(IEEE_T_MCOL);
  727. DUMP_REG(IEEE_T_DEF);
  728. DUMP_REG(IEEE_T_LCOL);
  729. DUMP_REG(IEEE_T_EXCOL);
  730. DUMP_REG(IEEE_T_MACERR);
  731. DUMP_REG(IEEE_T_CSERR);
  732. DUMP_REG(IEEE_T_SQE);
  733. DUMP_REG(IEEE_T_FDXFC);
  734. DUMP_REG(IEEE_T_OCTETS_OK);
  735. DUMP_REG(RMON_R_PACKETS);
  736. DUMP_REG(RMON_R_BC_PKT);
  737. DUMP_REG(RMON_R_MC_PKT);
  738. DUMP_REG(RMON_R_CRC_ALIGN);
  739. DUMP_REG(RMON_R_UNDERSIZE);
  740. DUMP_REG(RMON_R_OVERSIZE);
  741. DUMP_REG(RMON_R_FRAG);
  742. DUMP_REG(RMON_R_JAB);
  743. // DUMP_REG(RMON_R_RESVD_0);
  744. DUMP_REG(RMON_R_P64);
  745. DUMP_REG(RMON_R_P65TO127);
  746. DUMP_REG(RMON_R_P128TO255);
  747. DUMP_REG(RMON_R_P256TO511);
  748. DUMP_REG(RMON_R_P512TO1023);
  749. DUMP_REG(RMON_R_P1024TO2047);
  750. DUMP_REG(RMON_R_P_GTE2048);
  751. DUMP_REG(RMON_R_OCTETS);
  752. DUMP_REG(IEEE_R_DROP);
  753. DUMP_REG(IEEE_R_FRAME_OK);
  754. DUMP_REG(IEEE_R_CRC);
  755. DUMP_REG(IEEE_R_ALIGN);
  756. DUMP_REG(IEEE_R_MACERR);
  757. DUMP_REG(IEEE_R_FDXFC);
  758. DUMP_REG(IEEE_R_OCTETS_OK);
  759. DUMP_REG(ATCR);
  760. DUMP_REG(ATVR);
  761. DUMP_REG(ATOFF);
  762. DUMP_REG(ATPER);
  763. DUMP_REG(ATCOR);
  764. DUMP_REG(ATINC);
  765. DUMP_REG(ATSTMP);
  766. DUMP_REG(TGSR);
  767. }
  768. void enet_nvic_tog(void)
  769. {
  770. NVIC_SetPendingIRQ(ENET_IRQn);
  771. }
  772. void enet_rx_stat(void)
  773. {
  774. enet_data_error_stats_t *error_statistic = &imxrt_eth_device.error_statistic;
  775. #define DUMP_STAT(__VAR) \
  776. rt_kprintf("%-25s: %08X\n", #__VAR, error_statistic->__VAR);
  777. DUMP_STAT(statsRxLenGreaterErr);
  778. DUMP_STAT(statsRxAlignErr);
  779. DUMP_STAT(statsRxFcsErr);
  780. DUMP_STAT(statsRxOverRunErr);
  781. DUMP_STAT(statsRxTruncateErr);
  782. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  783. DUMP_STAT(statsRxProtocolChecksumErr);
  784. DUMP_STAT(statsRxIpHeadChecksumErr);
  785. DUMP_STAT(statsRxMacErr);
  786. DUMP_STAT(statsRxPhyErr);
  787. DUMP_STAT(statsRxCollisionErr);
  788. DUMP_STAT(statsTxErr);
  789. DUMP_STAT(statsTxFrameErr);
  790. DUMP_STAT(statsTxOverFlowErr);
  791. DUMP_STAT(statsTxLateCollisionErr);
  792. DUMP_STAT(statsTxExcessCollisionErr);
  793. DUMP_STAT(statsTxUnderFlowErr);
  794. DUMP_STAT(statsTxTsErr);
  795. #endif
  796. }
  797. void enet_buf_info(void)
  798. {
  799. int i = 0;
  800. for (i = 0; i < ENET_RXBD_NUM; i++)
  801. {
  802. rt_kprintf("%d: length: %-8d, control: %04X, buffer:%p\n",
  803. i,
  804. g_rxBuffDescrip[i].length,
  805. g_rxBuffDescrip[i].control,
  806. g_rxBuffDescrip[i].buffer);
  807. }
  808. for (i = 0; i < ENET_TXBD_NUM; i++)
  809. {
  810. rt_kprintf("%d: length: %-8d, control: %04X, buffer:%p\n",
  811. i,
  812. g_txBuffDescrip[i].length,
  813. g_txBuffDescrip[i].control,
  814. g_txBuffDescrip[i].buffer);
  815. }
  816. }
  817. FINSH_FUNCTION_EXPORT(phy_read, read phy register);
  818. FINSH_FUNCTION_EXPORT(phy_write, write phy register);
  819. FINSH_FUNCTION_EXPORT(phy_dump, dump phy registers);
  820. FINSH_FUNCTION_EXPORT(enet_reg_dump, dump enet registers);
  821. FINSH_FUNCTION_EXPORT(enet_nvic_tog, toggle enet nvic pendding bit);
  822. FINSH_FUNCTION_EXPORT(enet_rx_stat, dump enet rx statistic);
  823. FINSH_FUNCTION_EXPORT(enet_buf_info, dump enet tx and tx buffer descripter);
  824. #endif