drv_eth.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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. * 2017-06-08 tanek first implementation
  9. * 2019-03-11 JiCheng Adapt RT1020's IO MAP
  10. */
  11. #include <rtthread.h>
  12. #include "board.h"
  13. #include <rtdevice.h>
  14. #ifdef RT_USING_FINSH
  15. #include <finsh.h>
  16. #endif
  17. #include "fsl_enet.h"
  18. #include "fsl_gpio.h"
  19. #include "fsl_iomuxc.h"
  20. #include "fsl_phy.h"
  21. #include "fsl_cache.h"
  22. #ifdef RT_USING_LWIP
  23. #include <netif/ethernetif.h>
  24. #include "lwipopts.h"
  25. #define ENET_RXBD_NUM (4)
  26. #define ENET_TXBD_NUM (4)
  27. #define ENET_RXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
  28. #define ENET_TXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
  29. #if defined(BOARD_RT1021_EVK)
  30. #define PHY_ADDRESS 0x02u
  31. #endif
  32. /* debug option */
  33. //#define ETH_RX_DUMP
  34. //#define ETH_TX_DUMP
  35. #define DBG_ENABLE
  36. #define DBG_SECTION_NAME "ETH"
  37. #define DBG_COLOR
  38. #define DBG_LEVEL DBG_INFO
  39. #include <rtdbg.h>
  40. #define MAX_ADDR_LEN 6
  41. struct rt_imxrt_eth
  42. {
  43. /* inherit from ethernet device */
  44. struct eth_device parent;
  45. enet_handle_t enet_handle;
  46. ENET_Type *enet_base;
  47. enet_data_error_stats_t error_statistic;
  48. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  49. rt_bool_t tx_is_waiting;
  50. struct rt_semaphore tx_wait;
  51. enet_mii_speed_t speed;
  52. enet_mii_duplex_t duplex;
  53. };
  54. ALIGN(ENET_BUFF_ALIGNMENT) enet_tx_bd_struct_t g_txBuffDescrip[ENET_TXBD_NUM] SECTION("NonCacheable");
  55. ALIGN(ENET_BUFF_ALIGNMENT) rt_uint8_t g_txDataBuff[ENET_TXBD_NUM][RT_ALIGN(ENET_TXBUFF_SIZE, ENET_BUFF_ALIGNMENT)];
  56. ALIGN(ENET_BUFF_ALIGNMENT) enet_rx_bd_struct_t g_rxBuffDescrip[ENET_RXBD_NUM] SECTION("NonCacheable");
  57. ALIGN(ENET_BUFF_ALIGNMENT) rt_uint8_t g_rxDataBuff[ENET_RXBD_NUM][RT_ALIGN(ENET_RXBUFF_SIZE, ENET_BUFF_ALIGNMENT)];
  58. static struct rt_imxrt_eth imxrt_eth_device;
  59. void _enet_rx_callback(struct rt_imxrt_eth *eth)
  60. {
  61. rt_err_t result;
  62. ENET_DisableInterrupts(eth->enet_base, kENET_RxFrameInterrupt);
  63. result = eth_device_ready(&(eth->parent));
  64. if (result != RT_EOK)
  65. rt_kprintf("RX err =%d\n", result);
  66. }
  67. void _enet_tx_callback(struct rt_imxrt_eth *eth)
  68. {
  69. if (eth->tx_is_waiting == RT_TRUE)
  70. {
  71. eth->tx_is_waiting = RT_FALSE;
  72. rt_sem_release(&eth->tx_wait);
  73. }
  74. }
  75. void _enet_callback(ENET_Type *base, enet_handle_t *handle, enet_event_t event, void *userData)
  76. {
  77. switch (event)
  78. {
  79. case kENET_RxEvent:
  80. _enet_rx_callback((struct rt_imxrt_eth *)userData);
  81. break;
  82. case kENET_TxEvent:
  83. _enet_tx_callback((struct rt_imxrt_eth *)userData);
  84. break;
  85. case kENET_ErrEvent:
  86. //rt_kprintf("kENET_ErrEvent\n");
  87. break;
  88. case kENET_WakeUpEvent:
  89. //rt_kprintf("kENET_WakeUpEvent\n");
  90. break;
  91. case kENET_TimeStampEvent:
  92. //rt_kprintf("kENET_TimeStampEvent\n");
  93. break;
  94. case kENET_TimeStampAvailEvent:
  95. //rt_kprintf("kENET_TimeStampAvailEvent \n");
  96. break;
  97. default:
  98. //rt_kprintf("unknow error\n");
  99. break;
  100. }
  101. }
  102. #if defined (BOARD_RT1021_EVK)
  103. static void evk_enet_io_init(void)
  104. {
  105. CLOCK_EnableClock(kCLOCK_Iomuxc); /* iomuxc clock (iomuxc_clk_enable): 0x03u */
  106. IOMUXC_SetPinMux(
  107. IOMUXC_GPIO_AD_B0_04_GPIO1_IO04, /* GPIO_AD_B0_04 is configured as GPIO1_IO04 */
  108. 0U); /* Software Input On Field: Input Path is determined by functionality */
  109. IOMUXC_SetPinMux(
  110. IOMUXC_GPIO_AD_B0_06_LPUART1_TX, /* GPIO_AD_B0_06 is configured as LPUART1_TX */
  111. 0U); /* Software Input On Field: Input Path is determined by functionality */
  112. IOMUXC_SetPinMux(
  113. IOMUXC_GPIO_AD_B0_07_LPUART1_RX, /* GPIO_AD_B0_07 is configured as LPUART1_RX */
  114. 0U); /* Software Input On Field: Input Path is determined by functionality */
  115. IOMUXC_SetPinMux(
  116. IOMUXC_GPIO_AD_B0_08_ENET_REF_CLK1, /* GPIO_AD_B0_08 is configured as ENET_REF_CLK1 */
  117. 1U); /* Software Input On Field: Force input path of pad GPIO_AD_B0_08 */
  118. IOMUXC_SetPinMux(
  119. IOMUXC_GPIO_AD_B0_09_ENET_RDATA01, /* GPIO_AD_B0_09 is configured as ENET_RDATA01 */
  120. 0U); /* Software Input On Field: Input Path is determined by functionality */
  121. IOMUXC_SetPinMux(
  122. IOMUXC_GPIO_AD_B0_10_ENET_RDATA00, /* GPIO_AD_B0_10 is configured as ENET_RDATA00 */
  123. 0U); /* Software Input On Field: Input Path is determined by functionality */
  124. IOMUXC_SetPinMux(
  125. IOMUXC_GPIO_AD_B0_11_ENET_RX_EN, /* GPIO_AD_B0_11 is configured as ENET_RX_EN */
  126. 0U); /* Software Input On Field: Input Path is determined by functionality */
  127. IOMUXC_SetPinMux(
  128. IOMUXC_GPIO_AD_B0_12_ENET_RX_ER, /* GPIO_AD_B0_12 is configured as ENET_RX_ER */
  129. 0U); /* Software Input On Field: Input Path is determined by functionality */
  130. IOMUXC_SetPinMux(
  131. IOMUXC_GPIO_AD_B0_13_ENET_TX_EN, /* GPIO_AD_B0_13 is configured as ENET_TX_EN */
  132. 0U); /* Software Input On Field: Input Path is determined by functionality */
  133. IOMUXC_SetPinMux(
  134. IOMUXC_GPIO_AD_B0_14_ENET_TDATA00, /* GPIO_AD_B0_14 is configured as ENET_TDATA00 */
  135. 0U); /* Software Input On Field: Input Path is determined by functionality */
  136. IOMUXC_SetPinMux(
  137. IOMUXC_GPIO_AD_B0_15_ENET_TDATA01, /* GPIO_AD_B0_15 is configured as ENET_TDATA01 */
  138. 0U); /* Software Input On Field: Input Path is determined by functionality */
  139. IOMUXC_SetPinMux(
  140. IOMUXC_GPIO_AD_B1_06_GPIO1_IO22, /* GPIO_AD_B1_06 is configured as GPIO1_IO22 */
  141. 0U); /* Software Input On Field: Input Path is determined by functionality */
  142. IOMUXC_SetPinMux(
  143. IOMUXC_GPIO_EMC_40_ENET_MDIO, /* GPIO_EMC_40 is configured as ENET_MDIO */
  144. 0U); /* Software Input On Field: Input Path is determined by functionality */
  145. IOMUXC_SetPinMux(
  146. IOMUXC_GPIO_EMC_41_ENET_MDC, /* GPIO_EMC_41 is configured as ENET_MDC */
  147. 0U); /* Software Input On Field: Input Path is determined by functionality */
  148. IOMUXC_SetPinConfig(
  149. IOMUXC_GPIO_AD_B0_04_GPIO1_IO04, /* GPIO_AD_B0_04 PAD functional properties : */
  150. 0xB0A9u); /* Slew Rate Field: Fast Slew Rate
  151. Drive Strength Field: R0/5
  152. Speed Field: medium(100MHz)
  153. Open Drain Enable Field: Open Drain Disabled
  154. Pull / Keep Enable Field: Pull/Keeper Enabled
  155. Pull / Keep Select Field: Pull
  156. Pull Up / Down Config. Field: 100K Ohm Pull Up
  157. Hyst. Enable Field: Hysteresis Disabled */
  158. IOMUXC_SetPinConfig(
  159. IOMUXC_GPIO_AD_B0_06_LPUART1_TX, /* GPIO_AD_B0_06 PAD functional properties : */
  160. 0x10B0u); /* Slew Rate Field: Slow Slew Rate
  161. Drive Strength Field: R0/6
  162. Speed Field: medium(100MHz)
  163. Open Drain Enable Field: Open Drain Disabled
  164. Pull / Keep Enable Field: Pull/Keeper Enabled
  165. Pull / Keep Select Field: Keeper
  166. Pull Up / Down Config. Field: 100K Ohm Pull Down
  167. Hyst. Enable Field: Hysteresis Disabled */
  168. IOMUXC_SetPinConfig(
  169. IOMUXC_GPIO_AD_B0_07_LPUART1_RX, /* GPIO_AD_B0_07 PAD functional properties : */
  170. 0x10B0u); /* Slew Rate Field: Slow Slew Rate
  171. Drive Strength Field: R0/6
  172. Speed Field: medium(100MHz)
  173. Open Drain Enable Field: Open Drain Disabled
  174. Pull / Keep Enable Field: Pull/Keeper Enabled
  175. Pull / Keep Select Field: Keeper
  176. Pull Up / Down Config. Field: 100K Ohm Pull Down
  177. Hyst. Enable Field: Hysteresis Disabled */
  178. IOMUXC_SetPinConfig(
  179. IOMUXC_GPIO_AD_B0_08_ENET_REF_CLK1, /* GPIO_AD_B0_08 PAD functional properties : */
  180. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  181. Drive Strength Field: R0/5
  182. Speed Field: max(200MHz)
  183. Open Drain Enable Field: Open Drain Disabled
  184. Pull / Keep Enable Field: Pull/Keeper Enabled
  185. Pull / Keep Select Field: Pull
  186. Pull Up / Down Config. Field: 100K Ohm Pull Up
  187. Hyst. Enable Field: Hysteresis Disabled */
  188. IOMUXC_SetPinConfig(
  189. IOMUXC_GPIO_AD_B0_09_ENET_RDATA01, /* GPIO_AD_B0_09 PAD functional properties : */
  190. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  191. Drive Strength Field: R0/5
  192. Speed Field: max(200MHz)
  193. Open Drain Enable Field: Open Drain Disabled
  194. Pull / Keep Enable Field: Pull/Keeper Enabled
  195. Pull / Keep Select Field: Pull
  196. Pull Up / Down Config. Field: 100K Ohm Pull Up
  197. Hyst. Enable Field: Hysteresis Disabled */
  198. IOMUXC_SetPinConfig(
  199. IOMUXC_GPIO_AD_B0_10_ENET_RDATA00, /* GPIO_AD_B0_10 PAD functional properties : */
  200. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  201. Drive Strength Field: R0/5
  202. Speed Field: max(200MHz)
  203. Open Drain Enable Field: Open Drain Disabled
  204. Pull / Keep Enable Field: Pull/Keeper Enabled
  205. Pull / Keep Select Field: Pull
  206. Pull Up / Down Config. Field: 100K Ohm Pull Up
  207. Hyst. Enable Field: Hysteresis Disabled */
  208. IOMUXC_SetPinConfig(
  209. IOMUXC_GPIO_AD_B0_11_ENET_RX_EN, /* GPIO_AD_B0_11 PAD functional properties : */
  210. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  211. Drive Strength Field: R0/5
  212. Speed Field: max(200MHz)
  213. Open Drain Enable Field: Open Drain Disabled
  214. Pull / Keep Enable Field: Pull/Keeper Enabled
  215. Pull / Keep Select Field: Pull
  216. Pull Up / Down Config. Field: 100K Ohm Pull Up
  217. Hyst. Enable Field: Hysteresis Disabled */
  218. IOMUXC_SetPinConfig(
  219. IOMUXC_GPIO_AD_B0_12_ENET_RX_ER, /* GPIO_AD_B0_12 PAD functional properties : */
  220. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  221. Drive Strength Field: R0/5
  222. Speed Field: max(200MHz)
  223. Open Drain Enable Field: Open Drain Disabled
  224. Pull / Keep Enable Field: Pull/Keeper Enabled
  225. Pull / Keep Select Field: Pull
  226. Pull Up / Down Config. Field: 100K Ohm Pull Up
  227. Hyst. Enable Field: Hysteresis Disabled */
  228. IOMUXC_SetPinConfig(
  229. IOMUXC_GPIO_AD_B0_13_ENET_TX_EN, /* GPIO_AD_B0_13 PAD functional properties : */
  230. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  231. Drive Strength Field: R0/5
  232. Speed Field: max(200MHz)
  233. Open Drain Enable Field: Open Drain Disabled
  234. Pull / Keep Enable Field: Pull/Keeper Enabled
  235. Pull / Keep Select Field: Pull
  236. Pull Up / Down Config. Field: 100K Ohm Pull Up
  237. Hyst. Enable Field: Hysteresis Disabled */
  238. IOMUXC_SetPinConfig(
  239. IOMUXC_GPIO_AD_B0_14_ENET_TDATA00, /* GPIO_AD_B0_14 PAD functional properties : */
  240. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  241. Drive Strength Field: R0/5
  242. Speed Field: max(200MHz)
  243. Open Drain Enable Field: Open Drain Disabled
  244. Pull / Keep Enable Field: Pull/Keeper Enabled
  245. Pull / Keep Select Field: Pull
  246. Pull Up / Down Config. Field: 100K Ohm Pull Up
  247. Hyst. Enable Field: Hysteresis Disabled */
  248. IOMUXC_SetPinConfig(
  249. IOMUXC_GPIO_AD_B0_15_ENET_TDATA01, /* GPIO_AD_B0_15 PAD functional properties : */
  250. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  251. Drive Strength Field: R0/5
  252. Speed Field: max(200MHz)
  253. Open Drain Enable Field: Open Drain Disabled
  254. Pull / Keep Enable Field: Pull/Keeper Enabled
  255. Pull / Keep Select Field: Pull
  256. Pull Up / Down Config. Field: 100K Ohm Pull Up
  257. Hyst. Enable Field: Hysteresis Disabled */
  258. IOMUXC_SetPinConfig(
  259. IOMUXC_GPIO_AD_B1_06_GPIO1_IO22, /* GPIO_AD_B1_06 PAD functional properties : */
  260. 0xB0A9u); /* Slew Rate Field: Fast Slew Rate
  261. Drive Strength Field: R0/5
  262. Speed Field: medium(100MHz)
  263. Open Drain Enable Field: Open Drain Disabled
  264. Pull / Keep Enable Field: Pull/Keeper Enabled
  265. Pull / Keep Select Field: Pull
  266. Pull Up / Down Config. Field: 100K Ohm Pull Up
  267. Hyst. Enable Field: Hysteresis Disabled */
  268. IOMUXC_SetPinConfig(
  269. IOMUXC_GPIO_EMC_40_ENET_MDIO, /* GPIO_EMC_40 PAD functional properties : */
  270. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  271. Drive Strength Field: R0/5
  272. Speed Field: max(200MHz)
  273. Open Drain Enable Field: Open Drain Disabled
  274. Pull / Keep Enable Field: Pull/Keeper Enabled
  275. Pull / Keep Select Field: Pull
  276. Pull Up / Down Config. Field: 100K Ohm Pull Up
  277. Hyst. Enable Field: Hysteresis Disabled */
  278. IOMUXC_SetPinConfig(
  279. IOMUXC_GPIO_EMC_41_ENET_MDC, /* GPIO_EMC_41 PAD functional properties : */
  280. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  281. Drive Strength Field: R0/5
  282. Speed Field: max(200MHz)
  283. Open Drain Enable Field: Open Drain Disabled
  284. Pull / Keep Enable Field: Pull/Keeper Enabled
  285. Pull / Keep Select Field: Pull
  286. Pull Up / Down Config. Field: 100K Ohm Pull Up
  287. Hyst. Enable Field: Hysteresis Disabled */
  288. }
  289. #endif
  290. static void _enet_clk_init(void)
  291. {
  292. const clock_enet_pll_config_t config = {true, false, false, 1};
  293. CLOCK_InitEnetPll(&config);
  294. IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir, true);
  295. }
  296. static void _delay(void)
  297. {
  298. volatile int i = 1000000;
  299. while (i--)
  300. i = i;
  301. }
  302. static void _enet_phy_reset_by_gpio(void)
  303. {
  304. gpio_pin_config_t gpio_config = {kGPIO_DigitalOutput, 0, kGPIO_NoIntmode};
  305. GPIO_PinInit(GPIO1, 4, &gpio_config);
  306. GPIO_PinInit(GPIO1, 22, &gpio_config);
  307. /* pull up the ENET_INT before RESET. */
  308. GPIO_WritePinOutput(GPIO1, 22, 1);
  309. GPIO_WritePinOutput(GPIO1, 4, 0);
  310. _delay();
  311. GPIO_WritePinOutput(GPIO1, 4, 1);
  312. }
  313. static void _enet_config(void)
  314. {
  315. enet_config_t config;
  316. uint32_t sysClock;
  317. /* prepare the buffer configuration. */
  318. enet_buffer_config_t buffConfig =
  319. {
  320. ENET_RXBD_NUM,
  321. ENET_TXBD_NUM,
  322. SDK_SIZEALIGN(ENET_RXBUFF_SIZE, ENET_BUFF_ALIGNMENT),
  323. SDK_SIZEALIGN(ENET_TXBUFF_SIZE, ENET_BUFF_ALIGNMENT),
  324. &g_rxBuffDescrip[0],
  325. &g_txBuffDescrip[0],
  326. &g_rxDataBuff[0][0],
  327. &g_txDataBuff[0][0],
  328. };
  329. /* Get default configuration. */
  330. /*
  331. * config.miiMode = kENET_RmiiMode;
  332. * config.miiSpeed = kENET_MiiSpeed100M;
  333. * config.miiDuplex = kENET_MiiFullDuplex;
  334. * config.rxMaxFrameLen = ENET_FRAME_MAX_FRAMELEN;
  335. */
  336. ENET_GetDefaultConfig(&config);
  337. config.interrupt = kENET_TxFrameInterrupt | kENET_RxFrameInterrupt;
  338. //config.interrupt = 0xFFFFFFFF;
  339. config.miiSpeed = imxrt_eth_device.speed;
  340. config.miiDuplex = imxrt_eth_device.duplex;
  341. /* Set SMI to get PHY link status. */
  342. sysClock = CLOCK_GetFreq(kCLOCK_AhbClk);
  343. LOG_D("deinit\n");
  344. ENET_Deinit(imxrt_eth_device.enet_base);
  345. LOG_D("init\n");
  346. ENET_Init(imxrt_eth_device.enet_base, &imxrt_eth_device.enet_handle, &config, &buffConfig, &imxrt_eth_device.dev_addr[0], sysClock);
  347. LOG_D("set call back\n");
  348. ENET_SetCallback(&imxrt_eth_device.enet_handle, _enet_callback, &imxrt_eth_device);
  349. LOG_D("active read\n");
  350. ENET_ActiveRead(imxrt_eth_device.enet_base);
  351. }
  352. #if defined(ETH_RX_DUMP) || defined(ETH_TX_DUMP)
  353. static void packet_dump(const char *msg, const struct pbuf *p)
  354. {
  355. const struct pbuf *q;
  356. rt_uint32_t i, j;
  357. rt_uint8_t *ptr;
  358. rt_kprintf("%s %d byte\n", msg, p->tot_len);
  359. i = 0;
  360. for (q = p; q != RT_NULL; q = q->next)
  361. {
  362. ptr = q->payload;
  363. for (j = 0; j < q->len; j++)
  364. {
  365. if ((i % 8) == 0)
  366. {
  367. rt_kprintf(" ");
  368. }
  369. if ((i % 16) == 0)
  370. {
  371. rt_kprintf("\r\n");
  372. }
  373. rt_kprintf("%02x ", *ptr);
  374. i++;
  375. ptr++;
  376. }
  377. }
  378. rt_kprintf("\n\n");
  379. }
  380. #else
  381. #define packet_dump(...)
  382. #endif /* dump */
  383. /* initialize the interface */
  384. static rt_err_t rt_imxrt_eth_init(rt_device_t dev)
  385. {
  386. LOG_D("rt_imxrt_eth_init...\n");
  387. _enet_config();
  388. return RT_EOK;
  389. }
  390. static rt_err_t rt_imxrt_eth_open(rt_device_t dev, rt_uint16_t oflag)
  391. {
  392. LOG_D("rt_imxrt_eth_open...\n");
  393. return RT_EOK;
  394. }
  395. static rt_err_t rt_imxrt_eth_close(rt_device_t dev)
  396. {
  397. LOG_D("rt_imxrt_eth_close...\n");
  398. return RT_EOK;
  399. }
  400. static rt_size_t rt_imxrt_eth_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  401. {
  402. LOG_D("rt_imxrt_eth_read...\n");
  403. rt_set_errno(-RT_ENOSYS);
  404. return 0;
  405. }
  406. static rt_size_t rt_imxrt_eth_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  407. {
  408. LOG_D("rt_imxrt_eth_write...\n");
  409. rt_set_errno(-RT_ENOSYS);
  410. return 0;
  411. }
  412. static rt_err_t rt_imxrt_eth_control(rt_device_t dev, int cmd, void *args)
  413. {
  414. LOG_D("rt_imxrt_eth_control...\n");
  415. switch (cmd)
  416. {
  417. case NIOCTL_GADDR:
  418. /* get mac address */
  419. if (args) rt_memcpy(args, imxrt_eth_device.dev_addr, 6);
  420. else return -RT_ERROR;
  421. break;
  422. default :
  423. break;
  424. }
  425. return RT_EOK;
  426. }
  427. static void _ENET_ActiveSend(ENET_Type *base, uint32_t ringId)
  428. {
  429. assert(ringId < FSL_FEATURE_ENET_QUEUE);
  430. switch (ringId)
  431. {
  432. case 0:
  433. base->TDAR = ENET_TDAR_TDAR_MASK;
  434. break;
  435. #if FSL_FEATURE_ENET_QUEUE > 1
  436. case kENET_Ring1:
  437. base->TDAR1 = ENET_TDAR1_TDAR_MASK;
  438. break;
  439. case kENET_Ring2:
  440. base->TDAR2 = ENET_TDAR2_TDAR_MASK;
  441. break;
  442. #endif /* FSL_FEATURE_ENET_QUEUE > 1 */
  443. default:
  444. base->TDAR = ENET_TDAR_TDAR_MASK;
  445. break;
  446. }
  447. }
  448. static status_t _ENET_SendFrame(ENET_Type *base, enet_handle_t *handle, const uint8_t *data, uint32_t length)
  449. {
  450. assert(handle);
  451. assert(data);
  452. volatile enet_tx_bd_struct_t *curBuffDescrip;
  453. uint32_t len = 0;
  454. uint32_t sizeleft = 0;
  455. uint32_t address;
  456. /* Check the frame length. */
  457. if (length > ENET_FRAME_MAX_FRAMELEN)
  458. {
  459. return kStatus_ENET_TxFrameOverLen;
  460. }
  461. /* Check if the transmit buffer is ready. */
  462. curBuffDescrip = handle->txBdCurrent[0];
  463. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK)
  464. {
  465. return kStatus_ENET_TxFrameBusy;
  466. }
  467. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  468. bool isPtpEventMessage = false;
  469. /* Check PTP message with the PTP header. */
  470. isPtpEventMessage = ENET_Ptp1588ParseFrame(data, NULL, true);
  471. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  472. /* One transmit buffer is enough for one frame. */
  473. if (handle->txBuffSizeAlign[0] >= length)
  474. {
  475. /* Copy data to the buffer for uDMA transfer. */
  476. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  477. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  478. #else
  479. address = (uint32_t)curBuffDescrip->buffer;
  480. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  481. pbuf_copy_partial((const struct pbuf *)data, (void *)address, length, 0);
  482. /* Set data length. */
  483. curBuffDescrip->length = length;
  484. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  485. /* For enable the timestamp. */
  486. if (isPtpEventMessage)
  487. {
  488. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  489. }
  490. else
  491. {
  492. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  493. }
  494. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  495. curBuffDescrip->control |= (ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK);
  496. /* Increase the buffer descriptor address. */
  497. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  498. {
  499. handle->txBdCurrent[0] = handle->txBdBase[0];
  500. }
  501. else
  502. {
  503. handle->txBdCurrent[0]++;
  504. }
  505. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  506. /* Add the cache clean maintain. */
  507. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  508. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  509. #else
  510. address = (uint32_t)curBuffDescrip->buffer;
  511. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  512. DCACHE_CleanByRange(address, length);
  513. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  514. /* Active the transmit buffer descriptor. */
  515. _ENET_ActiveSend(base, 0);
  516. return kStatus_Success;
  517. }
  518. else
  519. {
  520. /* One frame requires more than one transmit buffers. */
  521. do
  522. {
  523. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  524. /* For enable the timestamp. */
  525. if (isPtpEventMessage)
  526. {
  527. curBuffDescrip->controlExtend1 |= ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  528. }
  529. else
  530. {
  531. curBuffDescrip->controlExtend1 &= ~ENET_BUFFDESCRIPTOR_TX_TIMESTAMP_MASK;
  532. }
  533. #endif /* ENET_ENHANCEDBUFFERDESCRIPTOR_MODE */
  534. /* Increase the buffer descriptor address. */
  535. if (curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
  536. {
  537. handle->txBdCurrent[0] = handle->txBdBase[0];
  538. }
  539. else
  540. {
  541. handle->txBdCurrent[0]++;
  542. }
  543. /* update the size left to be transmit. */
  544. sizeleft = length - len;
  545. if (sizeleft > handle->txBuffSizeAlign[0])
  546. {
  547. /* Data copy. */
  548. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  549. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  550. #else
  551. address = (uint32_t)curBuffDescrip->buffer;
  552. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  553. memcpy((void *)address, data + len, handle->txBuffSizeAlign[0]);
  554. /* Data length update. */
  555. curBuffDescrip->length = handle->txBuffSizeAlign[0];
  556. len += handle->txBuffSizeAlign[0];
  557. /* Sets the control flag. */
  558. curBuffDescrip->control &= ~ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  559. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK;
  560. /* Active the transmit buffer descriptor*/
  561. _ENET_ActiveSend(base, 0);
  562. }
  563. else
  564. {
  565. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  566. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  567. #else
  568. address = (uint32_t)curBuffDescrip->buffer;
  569. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  570. memcpy((void *)address, data + len, sizeleft);
  571. curBuffDescrip->length = sizeleft;
  572. /* Set Last buffer wrap flag. */
  573. curBuffDescrip->control |= ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK;
  574. #if defined(FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL) && FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL
  575. /* Add the cache clean maintain. */
  576. #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
  577. address = MEMORY_ConvertMemoryMapAddress((uint32_t)curBuffDescrip->buffer,kMEMORY_DMA2Local);
  578. #else
  579. address = (uint32_t)curBuffDescrip->buffer;
  580. #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
  581. DCACHE_CleanByRange(address, handle->txBuffSizeAlign[0]);
  582. #endif /* FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL */
  583. /* Active the transmit buffer descriptor. */
  584. _ENET_ActiveSend(base, 0);
  585. return kStatus_Success;
  586. }
  587. /* Get the current buffer descriptor address. */
  588. curBuffDescrip = handle->txBdCurrent[0];
  589. }
  590. while (!(curBuffDescrip->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK));
  591. return kStatus_ENET_TxFrameBusy;
  592. }
  593. }
  594. /* ethernet device interface */
  595. /* transmit packet. */
  596. rt_err_t rt_imxrt_eth_tx(rt_device_t dev, struct pbuf *p)
  597. {
  598. rt_err_t result = RT_EOK;
  599. enet_handle_t * enet_handle = &imxrt_eth_device.enet_handle;
  600. RT_ASSERT(p != NULL);
  601. RT_ASSERT(enet_handle != RT_NULL);
  602. LOG_D("rt_imxrt_eth_tx: %d\n", p->len);
  603. #ifdef ETH_TX_DUMP
  604. packet_dump("send", p);
  605. #endif
  606. do
  607. {
  608. result = _ENET_SendFrame(imxrt_eth_device.enet_base, enet_handle, (const uint8_t *)p, p->tot_len);
  609. if (result == kStatus_ENET_TxFrameBusy)
  610. {
  611. imxrt_eth_device.tx_is_waiting = RT_TRUE;
  612. rt_sem_take(&imxrt_eth_device.tx_wait, RT_WAITING_FOREVER);
  613. }
  614. }
  615. while (result == kStatus_ENET_TxFrameBusy);
  616. return RT_EOK;
  617. }
  618. /* reception packet. */
  619. struct pbuf *rt_imxrt_eth_rx(rt_device_t dev)
  620. {
  621. uint32_t length = 0;
  622. status_t status;
  623. struct pbuf *p = RT_NULL;
  624. enet_handle_t *enet_handle = &imxrt_eth_device.enet_handle;
  625. ENET_Type *enet_base = imxrt_eth_device.enet_base;
  626. enet_data_error_stats_t *error_statistic = &imxrt_eth_device.error_statistic;
  627. /* Get the Frame size */
  628. status = ENET_GetRxFrameSize(enet_handle, &length);
  629. /* Call ENET_ReadFrame when there is a received frame. */
  630. if (length != 0)
  631. {
  632. /* Received valid frame. Deliver the rx buffer with the size equal to length. */
  633. p = pbuf_alloc(PBUF_RAW, length, PBUF_POOL);
  634. if (p != NULL)
  635. {
  636. status = ENET_ReadFrame(enet_base, enet_handle, p->payload, length);
  637. if (status == kStatus_Success)
  638. {
  639. #ifdef ETH_RX_DUMP
  640. packet_dump("recv", p);
  641. #endif
  642. return p;
  643. }
  644. else
  645. {
  646. LOG_D(" A frame read failed\n");
  647. pbuf_free(p);
  648. }
  649. }
  650. else
  651. {
  652. LOG_D(" pbuf_alloc faild\n");
  653. }
  654. }
  655. else if (status == kStatus_ENET_RxFrameError)
  656. {
  657. LOG_W("ENET_GetRxFrameSize: kStatus_ENET_RxFrameError\n");
  658. /* Update the received buffer when error happened. */
  659. /* Get the error information of the received g_frame. */
  660. ENET_GetRxErrBeforeReadFrame(enet_handle, error_statistic);
  661. /* update the receive buffer. */
  662. ENET_ReadFrame(enet_base, enet_handle, NULL, 0);
  663. }
  664. ENET_EnableInterrupts(enet_base, kENET_RxFrameInterrupt);
  665. return NULL;
  666. }
  667. static void phy_monitor_thread_entry(void *parameter)
  668. {
  669. phy_speed_t speed;
  670. phy_duplex_t duplex;
  671. bool link = false;
  672. _enet_phy_reset_by_gpio();
  673. PHY_Init(imxrt_eth_device.enet_base, PHY_ADDRESS, CLOCK_GetFreq(kCLOCK_AhbClk));
  674. while (1)
  675. {
  676. bool new_link = false;
  677. status_t status = PHY_GetLinkStatus(imxrt_eth_device.enet_base, PHY_ADDRESS, &new_link);
  678. if ((status == kStatus_Success) && (link != new_link))
  679. {
  680. link = new_link;
  681. if (link) // link up
  682. {
  683. PHY_GetLinkSpeedDuplex(imxrt_eth_device.enet_base,
  684. PHY_ADDRESS, &speed, &duplex);
  685. if (kPHY_Speed10M == speed)
  686. {
  687. LOG_D("10M\n");
  688. }
  689. else
  690. {
  691. LOG_D("100M\n");
  692. }
  693. if (kPHY_HalfDuplex == duplex)
  694. {
  695. LOG_D("half dumplex\n");
  696. }
  697. else
  698. {
  699. LOG_D("full dumplex\n");
  700. }
  701. if ((imxrt_eth_device.speed != (enet_mii_speed_t)speed)
  702. || (imxrt_eth_device.duplex != (enet_mii_duplex_t)duplex))
  703. {
  704. imxrt_eth_device.speed = (enet_mii_speed_t)speed;
  705. imxrt_eth_device.duplex = (enet_mii_duplex_t)duplex;
  706. LOG_D("link up, and update eth mode.\n");
  707. rt_imxrt_eth_init((rt_device_t)&imxrt_eth_device);
  708. }
  709. else
  710. {
  711. LOG_D("link up, eth not need re-config.\n");
  712. }
  713. LOG_D("link up.\n");
  714. eth_device_linkchange(&imxrt_eth_device.parent, RT_TRUE);
  715. }
  716. else // link down
  717. {
  718. LOG_D("link down.\n");
  719. eth_device_linkchange(&imxrt_eth_device.parent, RT_FALSE);
  720. }
  721. }
  722. rt_thread_delay(RT_TICK_PER_SECOND * 2);
  723. }
  724. }
  725. static int rt_hw_imxrt_eth_init(void)
  726. {
  727. rt_err_t state;
  728. #if defined (BOARD_RT1021_EVK)
  729. evk_enet_io_init();
  730. #endif
  731. _enet_clk_init();
  732. /* OUI 00-80-E1 STMICROELECTRONICS. */
  733. imxrt_eth_device.dev_addr[0] = 0x00;
  734. imxrt_eth_device.dev_addr[1] = 0x04;
  735. imxrt_eth_device.dev_addr[2] = 0x9F;
  736. /* generate MAC addr from 96bit unique ID (only for test). */
  737. imxrt_eth_device.dev_addr[3] = 0x08;
  738. imxrt_eth_device.dev_addr[4] = 0x43;
  739. imxrt_eth_device.dev_addr[5] = 0x75;
  740. imxrt_eth_device.speed = kENET_MiiSpeed100M;
  741. imxrt_eth_device.duplex = kENET_MiiFullDuplex;
  742. imxrt_eth_device.enet_base = ENET;
  743. imxrt_eth_device.parent.parent.init = rt_imxrt_eth_init;
  744. imxrt_eth_device.parent.parent.open = rt_imxrt_eth_open;
  745. imxrt_eth_device.parent.parent.close = rt_imxrt_eth_close;
  746. imxrt_eth_device.parent.parent.read = rt_imxrt_eth_read;
  747. imxrt_eth_device.parent.parent.write = rt_imxrt_eth_write;
  748. imxrt_eth_device.parent.parent.control = rt_imxrt_eth_control;
  749. imxrt_eth_device.parent.parent.user_data = RT_NULL;
  750. imxrt_eth_device.parent.eth_rx = rt_imxrt_eth_rx;
  751. imxrt_eth_device.parent.eth_tx = rt_imxrt_eth_tx;
  752. LOG_D("sem init: tx_wait\r\n");
  753. /* init tx semaphore */
  754. rt_sem_init(&imxrt_eth_device.tx_wait, "tx_wait", 0, RT_IPC_FLAG_FIFO);
  755. /* register eth device */
  756. LOG_D("eth_device_init start\r\n");
  757. state = eth_device_init(&(imxrt_eth_device.parent), "e0");
  758. if (RT_EOK == state)
  759. {
  760. LOG_D("eth_device_init success\r\n");
  761. }
  762. else
  763. {
  764. LOG_D("eth_device_init faild: %d\r\n", state);
  765. }
  766. eth_device_linkchange(&imxrt_eth_device.parent, RT_FALSE);
  767. /* start phy monitor */
  768. {
  769. rt_thread_t tid;
  770. tid = rt_thread_create("phy",
  771. phy_monitor_thread_entry,
  772. RT_NULL,
  773. 512,
  774. RT_THREAD_PRIORITY_MAX - 2,
  775. 2);
  776. if (tid != RT_NULL)
  777. rt_thread_startup(tid);
  778. }
  779. return state;
  780. }
  781. #ifdef BOARD_rt1021_ATK
  782. INIT_ENV_EXPORT(rt_hw_imxrt_eth_init);
  783. #else
  784. INIT_DEVICE_EXPORT(rt_hw_imxrt_eth_init);
  785. #endif
  786. #endif
  787. #ifdef RT_USING_FINSH
  788. #include <finsh.h>
  789. void phy_read(uint32_t phyReg)
  790. {
  791. uint32_t data;
  792. status_t status;
  793. status = PHY_Read(imxrt_eth_device.enet_base, PHY_ADDRESS, phyReg, &data);
  794. if (kStatus_Success == status)
  795. {
  796. rt_kprintf("PHY_Read: %02X --> %08X", phyReg, data);
  797. }
  798. else
  799. {
  800. rt_kprintf("PHY_Read: %02X --> faild", phyReg);
  801. }
  802. }
  803. void phy_write(uint32_t phyReg, uint32_t data)
  804. {
  805. status_t status;
  806. status = PHY_Write(imxrt_eth_device.enet_base, PHY_ADDRESS, phyReg, data);
  807. if (kStatus_Success == status)
  808. {
  809. rt_kprintf("PHY_Write: %02X --> %08X\n", phyReg, data);
  810. }
  811. else
  812. {
  813. rt_kprintf("PHY_Write: %02X --> faild\n", phyReg);
  814. }
  815. }
  816. void phy_dump(void)
  817. {
  818. uint32_t data;
  819. status_t status;
  820. int i;
  821. for (i = 0; i < 32; i++)
  822. {
  823. status = PHY_Read(imxrt_eth_device.enet_base, PHY_ADDRESS, i, &data);
  824. if (kStatus_Success != status)
  825. {
  826. rt_kprintf("phy_dump: %02X --> faild", i);
  827. break;
  828. }
  829. if (i % 8 == 7)
  830. {
  831. rt_kprintf("%02X --> %08X ", i, data);
  832. }
  833. else
  834. {
  835. rt_kprintf("%02X --> %08X\n", i, data);
  836. }
  837. }
  838. }
  839. void enet_reg_dump(void)
  840. {
  841. ENET_Type *enet_base = imxrt_eth_device.enet_base;
  842. #define DUMP_REG(__REG) \
  843. rt_kprintf("%s(%08X): %08X\n", #__REG, (uint32_t)&enet_base->__REG, enet_base->__REG)
  844. DUMP_REG(EIR);
  845. DUMP_REG(EIMR);
  846. DUMP_REG(RDAR);
  847. DUMP_REG(TDAR);
  848. DUMP_REG(ECR);
  849. DUMP_REG(MMFR);
  850. DUMP_REG(MSCR);
  851. DUMP_REG(MIBC);
  852. DUMP_REG(RCR);
  853. DUMP_REG(TCR);
  854. DUMP_REG(PALR);
  855. DUMP_REG(PAUR);
  856. DUMP_REG(OPD);
  857. DUMP_REG(TXIC);
  858. DUMP_REG(RXIC);
  859. DUMP_REG(IAUR);
  860. DUMP_REG(IALR);
  861. DUMP_REG(GAUR);
  862. DUMP_REG(GALR);
  863. DUMP_REG(TFWR);
  864. DUMP_REG(RDSR);
  865. DUMP_REG(TDSR);
  866. DUMP_REG(MRBR);
  867. DUMP_REG(RSFL);
  868. DUMP_REG(RSEM);
  869. DUMP_REG(RAEM);
  870. DUMP_REG(RAFL);
  871. DUMP_REG(TSEM);
  872. DUMP_REG(TAEM);
  873. DUMP_REG(TAFL);
  874. DUMP_REG(TIPG);
  875. DUMP_REG(FTRL);
  876. DUMP_REG(TACC);
  877. DUMP_REG(RACC);
  878. DUMP_REG(RMON_T_DROP);
  879. DUMP_REG(RMON_T_PACKETS);
  880. DUMP_REG(RMON_T_BC_PKT);
  881. DUMP_REG(RMON_T_MC_PKT);
  882. DUMP_REG(RMON_T_CRC_ALIGN);
  883. DUMP_REG(RMON_T_UNDERSIZE);
  884. DUMP_REG(RMON_T_OVERSIZE);
  885. DUMP_REG(RMON_T_FRAG);
  886. DUMP_REG(RMON_T_JAB);
  887. DUMP_REG(RMON_T_COL);
  888. DUMP_REG(RMON_T_P64);
  889. DUMP_REG(RMON_T_P65TO127);
  890. DUMP_REG(RMON_T_P128TO255);
  891. DUMP_REG(RMON_T_P256TO511);
  892. DUMP_REG(RMON_T_P512TO1023);
  893. DUMP_REG(RMON_T_P1024TO2047);
  894. DUMP_REG(RMON_T_P_GTE2048);
  895. DUMP_REG(RMON_T_OCTETS);
  896. DUMP_REG(IEEE_T_DROP);
  897. DUMP_REG(IEEE_T_FRAME_OK);
  898. DUMP_REG(IEEE_T_1COL);
  899. DUMP_REG(IEEE_T_MCOL);
  900. DUMP_REG(IEEE_T_DEF);
  901. DUMP_REG(IEEE_T_LCOL);
  902. DUMP_REG(IEEE_T_EXCOL);
  903. DUMP_REG(IEEE_T_MACERR);
  904. DUMP_REG(IEEE_T_CSERR);
  905. DUMP_REG(IEEE_T_SQE);
  906. DUMP_REG(IEEE_T_FDXFC);
  907. DUMP_REG(IEEE_T_OCTETS_OK);
  908. DUMP_REG(RMON_R_PACKETS);
  909. DUMP_REG(RMON_R_BC_PKT);
  910. DUMP_REG(RMON_R_MC_PKT);
  911. DUMP_REG(RMON_R_CRC_ALIGN);
  912. DUMP_REG(RMON_R_UNDERSIZE);
  913. DUMP_REG(RMON_R_OVERSIZE);
  914. DUMP_REG(RMON_R_FRAG);
  915. DUMP_REG(RMON_R_JAB);
  916. DUMP_REG(RMON_R_RESVD_0);
  917. DUMP_REG(RMON_R_P64);
  918. DUMP_REG(RMON_R_P65TO127);
  919. DUMP_REG(RMON_R_P128TO255);
  920. DUMP_REG(RMON_R_P256TO511);
  921. DUMP_REG(RMON_R_P512TO1023);
  922. DUMP_REG(RMON_R_P1024TO2047);
  923. DUMP_REG(RMON_R_P_GTE2048);
  924. DUMP_REG(RMON_R_OCTETS);
  925. DUMP_REG(IEEE_R_DROP);
  926. DUMP_REG(IEEE_R_FRAME_OK);
  927. DUMP_REG(IEEE_R_CRC);
  928. DUMP_REG(IEEE_R_ALIGN);
  929. DUMP_REG(IEEE_R_MACERR);
  930. DUMP_REG(IEEE_R_FDXFC);
  931. DUMP_REG(IEEE_R_OCTETS_OK);
  932. DUMP_REG(ATCR);
  933. DUMP_REG(ATVR);
  934. DUMP_REG(ATOFF);
  935. DUMP_REG(ATPER);
  936. DUMP_REG(ATCOR);
  937. DUMP_REG(ATINC);
  938. DUMP_REG(ATSTMP);
  939. DUMP_REG(TGSR);
  940. }
  941. void enet_nvic_tog(void)
  942. {
  943. NVIC_SetPendingIRQ(ENET_IRQn);
  944. }
  945. void enet_rx_stat(void)
  946. {
  947. enet_data_error_stats_t *error_statistic = &imxrt_eth_device.error_statistic;
  948. #define DUMP_STAT(__VAR) \
  949. rt_kprintf("%-25s: %08X\n", #__VAR, error_statistic->__VAR);
  950. DUMP_STAT(statsRxLenGreaterErr);
  951. DUMP_STAT(statsRxAlignErr);
  952. DUMP_STAT(statsRxFcsErr);
  953. DUMP_STAT(statsRxOverRunErr);
  954. DUMP_STAT(statsRxTruncateErr);
  955. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  956. DUMP_STAT(statsRxProtocolChecksumErr);
  957. DUMP_STAT(statsRxIpHeadChecksumErr);
  958. DUMP_STAT(statsRxMacErr);
  959. DUMP_STAT(statsRxPhyErr);
  960. DUMP_STAT(statsRxCollisionErr);
  961. DUMP_STAT(statsTxErr);
  962. DUMP_STAT(statsTxFrameErr);
  963. DUMP_STAT(statsTxOverFlowErr);
  964. DUMP_STAT(statsTxLateCollisionErr);
  965. DUMP_STAT(statsTxExcessCollisionErr);
  966. DUMP_STAT(statsTxUnderFlowErr);
  967. DUMP_STAT(statsTxTsErr);
  968. #endif
  969. }
  970. void enet_buf_info(void)
  971. {
  972. int i = 0;
  973. for (i = 0; i < ENET_RXBD_NUM; i++)
  974. {
  975. rt_kprintf("%d: length: %-8d, control: %04X, buffer:%p\n",
  976. i,
  977. g_rxBuffDescrip[i].length,
  978. g_rxBuffDescrip[i].control,
  979. g_rxBuffDescrip[i].buffer);
  980. }
  981. for (i = 0; i < ENET_TXBD_NUM; i++)
  982. {
  983. rt_kprintf("%d: length: %-8d, control: %04X, buffer:%p\n",
  984. i,
  985. g_txBuffDescrip[i].length,
  986. g_txBuffDescrip[i].control,
  987. g_txBuffDescrip[i].buffer);
  988. }
  989. }
  990. FINSH_FUNCTION_EXPORT(phy_read, read phy register);
  991. FINSH_FUNCTION_EXPORT(phy_write, write phy register);
  992. FINSH_FUNCTION_EXPORT(phy_dump, dump phy registers);
  993. FINSH_FUNCTION_EXPORT(enet_reg_dump, dump enet registers);
  994. FINSH_FUNCTION_EXPORT(enet_nvic_tog, toggle enet nvic pendding bit);
  995. FINSH_FUNCTION_EXPORT(enet_rx_stat, dump enet rx statistic);
  996. FINSH_FUNCTION_EXPORT(enet_buf_info, dump enet tx and tx buffer descripter);
  997. #endif