drv_eth.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * File : application.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-06-08 tanek first implementation
  13. */
  14. #include <rtthread.h>
  15. #include "board.h"
  16. #include <rtdevice.h>
  17. #ifdef RT_USING_FINSH
  18. #include <finsh.h>
  19. #endif
  20. #include "fsl_enet.h"
  21. #include "fsl_gpio.h"
  22. #include "fsl_iomuxc.h"
  23. #include "fsl_phy.h"
  24. #ifdef RT_USING_LWIP
  25. #include <netif/ethernetif.h>
  26. #include "lwipopts.h"
  27. #define ENET_RXBD_NUM (4)
  28. #define ENET_TXBD_NUM (4)
  29. #define ENET_RXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
  30. #define ENET_TXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
  31. #define PHY_ADDRESS 0x02u
  32. /* debug option */
  33. //#define DEBUG
  34. //#define ETH_RX_DUMP
  35. //#define ETH_TX_DUMP
  36. #ifdef DEBUG
  37. #define ETH_PRINTF rt_kprintf
  38. #else
  39. #define ETH_PRINTF(...)
  40. #endif
  41. #define MAX_ADDR_LEN 6
  42. struct rt_imxrt_eth
  43. {
  44. /* inherit from ethernet device */
  45. struct eth_device parent;
  46. enet_handle_t enet_handle;
  47. ENET_Type *enet_base;
  48. enet_data_error_stats_t error_statistic;
  49. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  50. rt_bool_t tx_is_waiting;
  51. struct rt_semaphore tx_wait;
  52. };
  53. ALIGN(ENET_BUFF_ALIGNMENT) enet_tx_bd_struct_t g_txBuffDescrip[ENET_TXBD_NUM] SECTION("NonCacheable");
  54. ALIGN(ENET_BUFF_ALIGNMENT) rt_uint8_t g_txDataBuff[ENET_TXBD_NUM][RT_ALIGN(ENET_TXBUFF_SIZE, ENET_BUFF_ALIGNMENT)];
  55. ALIGN(ENET_BUFF_ALIGNMENT) enet_rx_bd_struct_t g_rxBuffDescrip[ENET_RXBD_NUM] SECTION("NonCacheable");
  56. ALIGN(ENET_BUFF_ALIGNMENT) rt_uint8_t g_rxDataBuff[ENET_RXBD_NUM][RT_ALIGN(ENET_RXBUFF_SIZE, ENET_BUFF_ALIGNMENT)];
  57. static struct rt_imxrt_eth imxrt_eth_device;
  58. void _enet_rx_callback(struct rt_imxrt_eth * eth)
  59. {
  60. rt_err_t result;
  61. ENET_DisableInterrupts(eth->enet_base, kENET_RxFrameInterrupt);
  62. result = eth_device_ready(&(eth->parent));
  63. if( result != RT_EOK )
  64. rt_kprintf("RX err =%d\n", result );
  65. }
  66. void _enet_tx_callback(struct rt_imxrt_eth * eth)
  67. {
  68. if (eth->tx_is_waiting == RT_TRUE)
  69. {
  70. eth->tx_is_waiting = RT_FALSE;
  71. rt_sem_release(&eth->tx_wait);
  72. }
  73. }
  74. void _enet_callback(ENET_Type *base, enet_handle_t *handle, enet_event_t event, void *userData)
  75. {
  76. switch(event)
  77. {
  78. case kENET_RxEvent:
  79. _enet_rx_callback((struct rt_imxrt_eth *)userData);
  80. break;
  81. case kENET_TxEvent:
  82. _enet_tx_callback((struct rt_imxrt_eth *)userData);
  83. break;
  84. case kENET_ErrEvent:
  85. //rt_kprintf("kENET_ErrEvent\n");
  86. break;
  87. case kENET_WakeUpEvent:
  88. //rt_kprintf("kENET_WakeUpEvent\n");
  89. break;
  90. case kENET_TimeStampEvent:
  91. //rt_kprintf("kENET_TimeStampEvent\n");
  92. break;
  93. case kENET_TimeStampAvailEvent:
  94. //rt_kprintf("kENET_TimeStampAvailEvent \n");
  95. break;
  96. default:
  97. //rt_kprintf("unknow error\n");
  98. break;
  99. }
  100. }
  101. static void _enet_io_init(void)
  102. {
  103. CLOCK_EnableClock(kCLOCK_Iomuxc); /* iomuxc clock (iomuxc_clk_enable): 0x03u */
  104. IOMUXC_SetPinMux(
  105. IOMUXC_GPIO_AD_B0_09_GPIO1_IO09, /* GPIO_AD_B0_09 is configured as GPIO1_IO09 */
  106. 0U); /* Software Input On Field: Input Path is determined by functionality */
  107. IOMUXC_SetPinMux(
  108. IOMUXC_GPIO_AD_B0_10_GPIO1_IO10, /* GPIO_AD_B0_10 is configured as GPIO1_IO10 */
  109. 0U); /* Software Input On Field: Input Path is determined by functionality */
  110. IOMUXC_SetPinMux(
  111. IOMUXC_GPIO_AD_B0_12_LPUART1_TX, /* GPIO_AD_B0_12 is configured as LPUART1_TX */
  112. 0U); /* Software Input On Field: Input Path is determined by functionality */
  113. IOMUXC_SetPinMux(
  114. IOMUXC_GPIO_AD_B0_13_LPUART1_RX, /* GPIO_AD_B0_13 is configured as LPUART1_RX */
  115. 0U); /* Software Input On Field: Input Path is determined by functionality */
  116. IOMUXC_SetPinMux(
  117. IOMUXC_GPIO_B1_04_ENET_RX_DATA00, /* GPIO_B1_04 is configured as ENET_RX_DATA00 */
  118. 0U); /* Software Input On Field: Input Path is determined by functionality */
  119. IOMUXC_SetPinMux(
  120. IOMUXC_GPIO_B1_05_ENET_RX_DATA01, /* GPIO_B1_05 is configured as ENET_RX_DATA01 */
  121. 0U); /* Software Input On Field: Input Path is determined by functionality */
  122. IOMUXC_SetPinMux(
  123. IOMUXC_GPIO_B1_06_ENET_RX_EN, /* GPIO_B1_06 is configured as ENET_RX_EN */
  124. 0U); /* Software Input On Field: Input Path is determined by functionality */
  125. IOMUXC_SetPinMux(
  126. IOMUXC_GPIO_B1_07_ENET_TX_DATA00, /* GPIO_B1_07 is configured as ENET_TX_DATA00 */
  127. 0U); /* Software Input On Field: Input Path is determined by functionality */
  128. IOMUXC_SetPinMux(
  129. IOMUXC_GPIO_B1_08_ENET_TX_DATA01, /* GPIO_B1_08 is configured as ENET_TX_DATA01 */
  130. 0U); /* Software Input On Field: Input Path is determined by functionality */
  131. IOMUXC_SetPinMux(
  132. IOMUXC_GPIO_B1_09_ENET_TX_EN, /* GPIO_B1_09 is configured as ENET_TX_EN */
  133. 0U); /* Software Input On Field: Input Path is determined by functionality */
  134. IOMUXC_SetPinMux(
  135. IOMUXC_GPIO_B1_10_ENET_REF_CLK, /* GPIO_B1_10 is configured as ENET_REF_CLK */
  136. 1U); /* Software Input On Field: Force input path of pad GPIO_B1_10 */
  137. IOMUXC_SetPinMux(
  138. IOMUXC_GPIO_B1_11_ENET_RX_ER, /* GPIO_B1_11 is configured as ENET_RX_ER */
  139. 0U); /* Software Input On Field: Input Path is determined by functionality */
  140. IOMUXC_SetPinMux(
  141. IOMUXC_GPIO_EMC_40_ENET_MDC, /* GPIO_EMC_40 is configured as ENET_MDC */
  142. 0U); /* Software Input On Field: Input Path is determined by functionality */
  143. IOMUXC_SetPinMux(
  144. IOMUXC_GPIO_EMC_41_ENET_MDIO, /* GPIO_EMC_41 is configured as ENET_MDIO */
  145. 0U); /* Software Input On Field: Input Path is determined by functionality */
  146. IOMUXC_SetPinConfig(
  147. IOMUXC_GPIO_AD_B0_09_GPIO1_IO09, /* GPIO_AD_B0_09 PAD functional properties : */
  148. 0xB0A9u); /* Slew Rate Field: Fast Slew Rate
  149. Drive Strength Field: R0/5
  150. Speed Field: medium(100MHz)
  151. Open Drain Enable Field: Open Drain Disabled
  152. Pull / Keep Enable Field: Pull/Keeper Enabled
  153. Pull / Keep Select Field: Pull
  154. Pull Up / Down Config. Field: 100K Ohm Pull Up
  155. Hyst. Enable Field: Hysteresis Disabled */
  156. IOMUXC_SetPinConfig(
  157. IOMUXC_GPIO_AD_B0_10_GPIO1_IO10, /* GPIO_AD_B0_10 PAD functional properties : */
  158. 0xB0A9u); /* Slew Rate Field: Fast Slew Rate
  159. Drive Strength Field: R0/5
  160. Speed Field: medium(100MHz)
  161. Open Drain Enable Field: Open Drain Disabled
  162. Pull / Keep Enable Field: Pull/Keeper Enabled
  163. Pull / Keep Select Field: Pull
  164. Pull Up / Down Config. Field: 100K Ohm Pull Up
  165. Hyst. Enable Field: Hysteresis Disabled */
  166. IOMUXC_SetPinConfig(
  167. IOMUXC_GPIO_AD_B0_12_LPUART1_TX, /* GPIO_AD_B0_12 PAD functional properties : */
  168. 0x10B0u); /* Slew Rate Field: Slow Slew Rate
  169. Drive Strength Field: R0/6
  170. Speed Field: medium(100MHz)
  171. Open Drain Enable Field: Open Drain Disabled
  172. Pull / Keep Enable Field: Pull/Keeper Enabled
  173. Pull / Keep Select Field: Keeper
  174. Pull Up / Down Config. Field: 100K Ohm Pull Down
  175. Hyst. Enable Field: Hysteresis Disabled */
  176. IOMUXC_SetPinConfig(
  177. IOMUXC_GPIO_AD_B0_13_LPUART1_RX, /* GPIO_AD_B0_13 PAD functional properties : */
  178. 0x10B0u); /* Slew Rate Field: Slow Slew Rate
  179. Drive Strength Field: R0/6
  180. Speed Field: medium(100MHz)
  181. Open Drain Enable Field: Open Drain Disabled
  182. Pull / Keep Enable Field: Pull/Keeper Enabled
  183. Pull / Keep Select Field: Keeper
  184. Pull Up / Down Config. Field: 100K Ohm Pull Down
  185. Hyst. Enable Field: Hysteresis Disabled */
  186. IOMUXC_SetPinConfig(
  187. IOMUXC_GPIO_B1_04_ENET_RX_DATA00, /* GPIO_B1_04 PAD functional properties : */
  188. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  189. Drive Strength Field: R0/5
  190. Speed Field: max(200MHz)
  191. Open Drain Enable Field: Open Drain Disabled
  192. Pull / Keep Enable Field: Pull/Keeper Enabled
  193. Pull / Keep Select Field: Pull
  194. Pull Up / Down Config. Field: 100K Ohm Pull Up
  195. Hyst. Enable Field: Hysteresis Disabled */
  196. IOMUXC_SetPinConfig(
  197. IOMUXC_GPIO_B1_05_ENET_RX_DATA01, /* GPIO_B1_05 PAD functional properties : */
  198. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  199. Drive Strength Field: R0/5
  200. Speed Field: max(200MHz)
  201. Open Drain Enable Field: Open Drain Disabled
  202. Pull / Keep Enable Field: Pull/Keeper Enabled
  203. Pull / Keep Select Field: Pull
  204. Pull Up / Down Config. Field: 100K Ohm Pull Up
  205. Hyst. Enable Field: Hysteresis Disabled */
  206. IOMUXC_SetPinConfig(
  207. IOMUXC_GPIO_B1_06_ENET_RX_EN, /* GPIO_B1_06 PAD functional properties : */
  208. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  209. Drive Strength Field: R0/5
  210. Speed Field: max(200MHz)
  211. Open Drain Enable Field: Open Drain Disabled
  212. Pull / Keep Enable Field: Pull/Keeper Enabled
  213. Pull / Keep Select Field: Pull
  214. Pull Up / Down Config. Field: 100K Ohm Pull Up
  215. Hyst. Enable Field: Hysteresis Disabled */
  216. IOMUXC_SetPinConfig(
  217. IOMUXC_GPIO_B1_07_ENET_TX_DATA00, /* GPIO_B1_07 PAD functional properties : */
  218. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  219. Drive Strength Field: R0/5
  220. Speed Field: max(200MHz)
  221. Open Drain Enable Field: Open Drain Disabled
  222. Pull / Keep Enable Field: Pull/Keeper Enabled
  223. Pull / Keep Select Field: Pull
  224. Pull Up / Down Config. Field: 100K Ohm Pull Up
  225. Hyst. Enable Field: Hysteresis Disabled */
  226. IOMUXC_SetPinConfig(
  227. IOMUXC_GPIO_B1_08_ENET_TX_DATA01, /* GPIO_B1_08 PAD functional properties : */
  228. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  229. Drive Strength Field: R0/5
  230. Speed Field: max(200MHz)
  231. Open Drain Enable Field: Open Drain Disabled
  232. Pull / Keep Enable Field: Pull/Keeper Enabled
  233. Pull / Keep Select Field: Pull
  234. Pull Up / Down Config. Field: 100K Ohm Pull Up
  235. Hyst. Enable Field: Hysteresis Disabled */
  236. IOMUXC_SetPinConfig(
  237. IOMUXC_GPIO_B1_09_ENET_TX_EN, /* GPIO_B1_09 PAD functional properties : */
  238. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  239. Drive Strength Field: R0/5
  240. Speed Field: max(200MHz)
  241. Open Drain Enable Field: Open Drain Disabled
  242. Pull / Keep Enable Field: Pull/Keeper Enabled
  243. Pull / Keep Select Field: Pull
  244. Pull Up / Down Config. Field: 100K Ohm Pull Up
  245. Hyst. Enable Field: Hysteresis Disabled */
  246. IOMUXC_SetPinConfig(
  247. IOMUXC_GPIO_B1_10_ENET_REF_CLK, /* GPIO_B1_10 PAD functional properties : */
  248. 0x31u); /* Slew Rate Field: Fast Slew Rate
  249. Drive Strength Field: R0/6
  250. Speed Field: low(50MHz)
  251. Open Drain Enable Field: Open Drain Disabled
  252. Pull / Keep Enable Field: Pull/Keeper Disabled
  253. Pull / Keep Select Field: Keeper
  254. Pull Up / Down Config. Field: 100K Ohm Pull Down
  255. Hyst. Enable Field: Hysteresis Disabled */
  256. IOMUXC_SetPinConfig(
  257. IOMUXC_GPIO_B1_11_ENET_RX_ER, /* GPIO_B1_11 PAD functional properties : */
  258. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  259. Drive Strength Field: R0/5
  260. Speed Field: max(200MHz)
  261. Open Drain Enable Field: Open Drain Disabled
  262. Pull / Keep Enable Field: Pull/Keeper Enabled
  263. Pull / Keep Select Field: Pull
  264. Pull Up / Down Config. Field: 100K Ohm Pull Up
  265. Hyst. Enable Field: Hysteresis Disabled */
  266. IOMUXC_SetPinConfig(
  267. IOMUXC_GPIO_EMC_40_ENET_MDC, /* GPIO_EMC_40 PAD functional properties : */
  268. 0xB0E9u); /* Slew Rate Field: Fast Slew Rate
  269. Drive Strength Field: R0/5
  270. Speed Field: max(200MHz)
  271. Open Drain Enable Field: Open Drain Disabled
  272. Pull / Keep Enable Field: Pull/Keeper Enabled
  273. Pull / Keep Select Field: Pull
  274. Pull Up / Down Config. Field: 100K Ohm Pull Up
  275. Hyst. Enable Field: Hysteresis Disabled */
  276. IOMUXC_SetPinConfig(
  277. IOMUXC_GPIO_EMC_41_ENET_MDIO, /* GPIO_EMC_41 PAD functional properties : */
  278. 0xB829u); /* Slew Rate Field: Fast Slew Rate
  279. Drive Strength Field: R0/5
  280. Speed Field: low(50MHz)
  281. Open Drain Enable Field: Open Drain Enabled
  282. Pull / Keep Enable Field: Pull/Keeper Enabled
  283. Pull / Keep Select Field: Pull
  284. Pull Up / Down Config. Field: 100K Ohm Pull Up
  285. Hyst. Enable Field: Hysteresis Disabled */
  286. }
  287. static void _enet_clk_init(void)
  288. {
  289. const clock_enet_pll_config_t config = {true, false, false, 1, 1};
  290. CLOCK_InitEnetPll(&config);
  291. IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir, true);
  292. }
  293. static void _delay(void)
  294. {
  295. volatile int i = 1000000;
  296. while (i--)
  297. i = i;
  298. }
  299. static void _enet_phy_reset_by_gpio(void)
  300. {
  301. gpio_pin_config_t gpio_config = {kGPIO_DigitalOutput, 0, kGPIO_NoIntmode};
  302. GPIO_PinInit(GPIO1, 9, &gpio_config);
  303. GPIO_PinInit(GPIO1, 10, &gpio_config);
  304. /* pull up the ENET_INT before RESET. */
  305. GPIO_WritePinOutput(GPIO1, 10, 1);
  306. GPIO_WritePinOutput(GPIO1, 9, 0);
  307. _delay();
  308. GPIO_WritePinOutput(GPIO1, 9, 1);
  309. }
  310. static void _enet_config(void)
  311. {
  312. enet_config_t config;
  313. uint32_t sysClock;
  314. status_t status;
  315. phy_speed_t speed;
  316. phy_duplex_t duplex;
  317. bool link = false;
  318. /* prepare the buffer configuration. */
  319. enet_buffer_config_t buffConfig = {
  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. /* Set SMI to get PHY link status. */
  340. sysClock = CLOCK_GetFreq(kCLOCK_AhbClk);
  341. status = PHY_Init(imxrt_eth_device.enet_base, PHY_ADDRESS, sysClock);
  342. while (status != kStatus_Success)
  343. {
  344. ETH_PRINTF("\r\nPHY Auto-negotiation failed. Please check the cable connection and link partner setting.\r\n");
  345. status = PHY_Init(imxrt_eth_device.enet_base, PHY_ADDRESS, sysClock);
  346. }
  347. PHY_GetLinkStatus(imxrt_eth_device.enet_base, PHY_ADDRESS, &link);
  348. if (link)
  349. {
  350. /* Get the actual PHY link speed. */
  351. PHY_GetLinkSpeedDuplex(imxrt_eth_device.enet_base, PHY_ADDRESS, &speed, &duplex);
  352. /* Change the MII speed and duplex for actual link status. */
  353. config.miiSpeed = (enet_mii_speed_t)speed;
  354. config.miiDuplex = (enet_mii_duplex_t)duplex;
  355. }
  356. ENET_Init(imxrt_eth_device.enet_base, &imxrt_eth_device.enet_handle, &config, &buffConfig, &imxrt_eth_device.dev_addr[0], sysClock);
  357. ENET_SetCallback(&imxrt_eth_device.enet_handle, _enet_callback, &imxrt_eth_device);
  358. ENET_ActiveRead(imxrt_eth_device.enet_base);
  359. }
  360. /* initialize the interface */
  361. static rt_err_t rt_imxrt_eth_init(rt_device_t dev)
  362. {
  363. _enet_io_init();
  364. _enet_clk_init();
  365. _enet_phy_reset_by_gpio();
  366. _enet_config();
  367. return RT_EOK;
  368. }
  369. static rt_err_t rt_imxrt_eth_open(rt_device_t dev, rt_uint16_t oflag)
  370. {
  371. ETH_PRINTF("rt_imxrt_eth_open...\n");
  372. return RT_EOK;
  373. }
  374. static rt_err_t rt_imxrt_eth_close(rt_device_t dev)
  375. {
  376. ETH_PRINTF("rt_imxrt_eth_close...\n");
  377. return RT_EOK;
  378. }
  379. static rt_size_t rt_imxrt_eth_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  380. {
  381. ETH_PRINTF("rt_imxrt_eth_read...\n");
  382. rt_set_errno(-RT_ENOSYS);
  383. return 0;
  384. }
  385. static rt_size_t rt_imxrt_eth_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  386. {
  387. ETH_PRINTF("rt_imxrt_eth_write...\n");
  388. rt_set_errno(-RT_ENOSYS);
  389. return 0;
  390. }
  391. static rt_err_t rt_imxrt_eth_control(rt_device_t dev, int cmd, void *args)
  392. {
  393. ETH_PRINTF("rt_imxrt_eth_control...\n");
  394. switch(cmd)
  395. {
  396. case NIOCTL_GADDR:
  397. /* get mac address */
  398. if(args) rt_memcpy(args, imxrt_eth_device.dev_addr, 6);
  399. else return -RT_ERROR;
  400. break;
  401. default :
  402. break;
  403. }
  404. return RT_EOK;
  405. }
  406. /* ethernet device interface */
  407. /* transmit packet. */
  408. rt_err_t rt_imxrt_eth_tx( rt_device_t dev, struct pbuf* p)
  409. {
  410. rt_err_t result = RT_EOK;
  411. enet_handle_t * enet_handle = &imxrt_eth_device.enet_handle;
  412. RT_ASSERT(p != NULL);
  413. RT_ASSERT(enet_handle != RT_NULL);
  414. ETH_PRINTF("rt_imxrt_eth_tx: %d\n", p->len);
  415. #ifdef ETH_TX_DUMP
  416. {
  417. int i;
  418. uint8_t * buf;
  419. buf = (uint8_t *)p->payload;
  420. for (i = 0; i < p->len; i++)
  421. {
  422. ETH_PRINTF("%02X ", buf[i]);
  423. if (i % 16 == 15)
  424. ETH_PRINTF("\n");
  425. }
  426. ETH_PRINTF("\n");
  427. }
  428. #endif
  429. do
  430. {
  431. result = ENET_SendFrame(imxrt_eth_device.enet_base, enet_handle, p->payload, p->len);
  432. if (result == kStatus_ENET_TxFrameBusy)
  433. {
  434. rt_sem_take(&imxrt_eth_device.tx_wait, RT_WAITING_FOREVER);
  435. }
  436. } while (result == kStatus_ENET_TxFrameBusy);
  437. return RT_EOK;
  438. }
  439. /* reception packet. */
  440. struct pbuf *rt_imxrt_eth_rx(rt_device_t dev)
  441. {
  442. uint32_t length = 0;
  443. status_t status;
  444. struct pbuf* p = RT_NULL;
  445. enet_handle_t * enet_handle = &imxrt_eth_device.enet_handle;
  446. ENET_Type *enet_base = imxrt_eth_device.enet_base;
  447. enet_data_error_stats_t *error_statistic = &imxrt_eth_device.error_statistic;
  448. /* Get the Frame size */
  449. status = ENET_GetRxFrameSize(enet_handle, &length);
  450. /* Call ENET_ReadFrame when there is a received frame. */
  451. if (length != 0)
  452. {
  453. /* Received valid frame. Deliver the rx buffer with the size equal to length. */
  454. p = pbuf_alloc(PBUF_RAW, length, PBUF_POOL);
  455. if (p != NULL)
  456. {
  457. status = ENET_ReadFrame(enet_base, enet_handle, p->payload, length);
  458. if (status == kStatus_Success)
  459. {
  460. #ifdef ETH_RX_DUMP
  461. uint8_t *buf;
  462. int i;
  463. ETH_PRINTF("A frame received. the length:%d\n", p->len);
  464. buf = (uint8_t *)p->payload;
  465. for (i = 0; i < p->len; i++)
  466. {
  467. ETH_PRINTF("%02X ", buf[i]);
  468. if (i % 16 == 15)
  469. ETH_PRINTF("\n");
  470. }
  471. ETH_PRINTF("\n");
  472. #endif
  473. return p;
  474. }
  475. else
  476. {
  477. ETH_PRINTF(" A frame read failed\n");
  478. pbuf_free(p);
  479. }
  480. }
  481. else
  482. {
  483. ETH_PRINTF(" pbuf_alloc faild\n");
  484. }
  485. }
  486. else if (status == kStatus_ENET_RxFrameError)
  487. {
  488. ETH_PRINTF("ENET_GetRxFrameSize: kStatus_ENET_RxFrameError\n");
  489. /* Update the received buffer when error happened. */
  490. /* Get the error information of the received g_frame. */
  491. ENET_GetRxErrBeforeReadFrame(enet_handle, error_statistic);
  492. /* update the receive buffer. */
  493. ENET_ReadFrame(enet_base, enet_handle, NULL, 0);
  494. }
  495. ENET_EnableInterrupts(enet_base, kENET_RxFrameInterrupt);
  496. return NULL;
  497. }
  498. static int rt_hw_imxrt_eth_init(void)
  499. {
  500. rt_err_t state;
  501. /* OUI 00-80-E1 STMICROELECTRONICS. */
  502. imxrt_eth_device.dev_addr[0] = 0x00;
  503. imxrt_eth_device.dev_addr[1] = 0x80;
  504. imxrt_eth_device.dev_addr[2] = 0xE1;
  505. /* generate MAC addr from 96bit unique ID (only for test). */
  506. imxrt_eth_device.dev_addr[3] = 0x12;
  507. imxrt_eth_device.dev_addr[4] = 0x34;
  508. imxrt_eth_device.dev_addr[5] = 0x56;
  509. imxrt_eth_device.enet_base = ENET;
  510. imxrt_eth_device.parent.parent.init = rt_imxrt_eth_init;
  511. imxrt_eth_device.parent.parent.open = rt_imxrt_eth_open;
  512. imxrt_eth_device.parent.parent.close = rt_imxrt_eth_close;
  513. imxrt_eth_device.parent.parent.read = rt_imxrt_eth_read;
  514. imxrt_eth_device.parent.parent.write = rt_imxrt_eth_write;
  515. imxrt_eth_device.parent.parent.control = rt_imxrt_eth_control;
  516. imxrt_eth_device.parent.parent.user_data = RT_NULL;
  517. imxrt_eth_device.parent.eth_rx = rt_imxrt_eth_rx;
  518. imxrt_eth_device.parent.eth_tx = rt_imxrt_eth_tx;
  519. ETH_PRINTF("sem init: tx_wait\r\n");
  520. /* init tx semaphore */
  521. rt_sem_init(&imxrt_eth_device.tx_wait, "tx_wait", 0, RT_IPC_FLAG_FIFO);
  522. /* register eth device */
  523. ETH_PRINTF("eth_device_init start\r\n");
  524. state = eth_device_init(&(imxrt_eth_device.parent), "e0");
  525. if (RT_EOK == state)
  526. {
  527. ETH_PRINTF("eth_device_init success\r\n");
  528. }
  529. else
  530. {
  531. ETH_PRINTF("eth_device_init faild: %d\r\n", state);
  532. }
  533. return state;
  534. }
  535. INIT_DEVICE_EXPORT(rt_hw_imxrt_eth_init);
  536. #endif
  537. #ifdef RT_USING_FINSH
  538. #include <finsh.h>
  539. void phy_read(uint32_t phyReg)
  540. {
  541. uint32_t data;
  542. status_t status;
  543. status = PHY_Read(imxrt_eth_device.enet_base, PHY_ADDRESS, phyReg, &data);
  544. if (kStatus_Success == status)
  545. {
  546. rt_kprintf("PHY_Read: %02X --> %08X", phyReg, data);
  547. }
  548. else
  549. {
  550. rt_kprintf("PHY_Read: %02X --> faild", phyReg);
  551. }
  552. }
  553. void phy_write(uint32_t phyReg, uint32_t data)
  554. {
  555. status_t status;
  556. status = PHY_Write(imxrt_eth_device.enet_base, PHY_ADDRESS, phyReg, data);
  557. if (kStatus_Success == status)
  558. {
  559. rt_kprintf("PHY_Write: %02X --> %08X\n", phyReg, data);
  560. }
  561. else
  562. {
  563. rt_kprintf("PHY_Write: %02X --> faild\n", phyReg);
  564. }
  565. }
  566. void phy_dump(void)
  567. {
  568. uint32_t data;
  569. status_t status;
  570. int i;
  571. for (i = 0; i < 32; i++)
  572. {
  573. status = PHY_Read(imxrt_eth_device.enet_base, PHY_ADDRESS, i, &data);
  574. if (kStatus_Success != status)
  575. {
  576. rt_kprintf("phy_dump: %02X --> faild", i);
  577. break;
  578. }
  579. if (i % 8 == 7)
  580. {
  581. rt_kprintf("%02X --> %08X ", i, data);
  582. }
  583. else
  584. {
  585. rt_kprintf("%02X --> %08X\n", i, data);
  586. }
  587. }
  588. }
  589. void enet_reg_dump(void)
  590. {
  591. ENET_Type *enet_base = imxrt_eth_device.enet_base;
  592. #define DUMP_REG(__REG) \
  593. rt_kprintf("%s(%08X): %08X\n", #__REG, (uint32_t)&enet_base->__REG, enet_base->__REG)
  594. DUMP_REG(EIR);
  595. DUMP_REG(EIMR);
  596. DUMP_REG(RDAR);
  597. DUMP_REG(TDAR);
  598. DUMP_REG(ECR);
  599. DUMP_REG(MMFR);
  600. DUMP_REG(MSCR);
  601. DUMP_REG(MIBC);
  602. DUMP_REG(RCR);
  603. DUMP_REG(TCR);
  604. DUMP_REG(PALR);
  605. DUMP_REG(PAUR);
  606. DUMP_REG(OPD);
  607. DUMP_REG(TXIC);
  608. DUMP_REG(RXIC);
  609. DUMP_REG(IAUR);
  610. DUMP_REG(IALR);
  611. DUMP_REG(GAUR);
  612. DUMP_REG(GALR);
  613. DUMP_REG(TFWR);
  614. DUMP_REG(RDSR);
  615. DUMP_REG(TDSR);
  616. DUMP_REG(MRBR);
  617. DUMP_REG(RSFL);
  618. DUMP_REG(RSEM);
  619. DUMP_REG(RAEM);
  620. DUMP_REG(RAFL);
  621. DUMP_REG(TSEM);
  622. DUMP_REG(TAEM);
  623. DUMP_REG(TAFL);
  624. DUMP_REG(TIPG);
  625. DUMP_REG(FTRL);
  626. DUMP_REG(TACC);
  627. DUMP_REG(RACC);
  628. DUMP_REG(RMON_T_DROP);
  629. DUMP_REG(RMON_T_PACKETS);
  630. DUMP_REG(RMON_T_BC_PKT);
  631. DUMP_REG(RMON_T_MC_PKT);
  632. DUMP_REG(RMON_T_CRC_ALIGN);
  633. DUMP_REG(RMON_T_UNDERSIZE);
  634. DUMP_REG(RMON_T_OVERSIZE);
  635. DUMP_REG(RMON_T_FRAG);
  636. DUMP_REG(RMON_T_JAB);
  637. DUMP_REG(RMON_T_COL);
  638. DUMP_REG(RMON_T_P64);
  639. DUMP_REG(RMON_T_P65TO127);
  640. DUMP_REG(RMON_T_P128TO255);
  641. DUMP_REG(RMON_T_P256TO511);
  642. DUMP_REG(RMON_T_P512TO1023);
  643. DUMP_REG(RMON_T_P1024TO2047);
  644. DUMP_REG(RMON_T_P_GTE2048);
  645. DUMP_REG(RMON_T_OCTETS);
  646. DUMP_REG(IEEE_T_DROP);
  647. DUMP_REG(IEEE_T_FRAME_OK);
  648. DUMP_REG(IEEE_T_1COL);
  649. DUMP_REG(IEEE_T_MCOL);
  650. DUMP_REG(IEEE_T_DEF);
  651. DUMP_REG(IEEE_T_LCOL);
  652. DUMP_REG(IEEE_T_EXCOL);
  653. DUMP_REG(IEEE_T_MACERR);
  654. DUMP_REG(IEEE_T_CSERR);
  655. DUMP_REG(IEEE_T_SQE);
  656. DUMP_REG(IEEE_T_FDXFC);
  657. DUMP_REG(IEEE_T_OCTETS_OK);
  658. DUMP_REG(RMON_R_PACKETS);
  659. DUMP_REG(RMON_R_BC_PKT);
  660. DUMP_REG(RMON_R_MC_PKT);
  661. DUMP_REG(RMON_R_CRC_ALIGN);
  662. DUMP_REG(RMON_R_UNDERSIZE);
  663. DUMP_REG(RMON_R_OVERSIZE);
  664. DUMP_REG(RMON_R_FRAG);
  665. DUMP_REG(RMON_R_JAB);
  666. DUMP_REG(RMON_R_RESVD_0);
  667. DUMP_REG(RMON_R_P64);
  668. DUMP_REG(RMON_R_P65TO127);
  669. DUMP_REG(RMON_R_P128TO255);
  670. DUMP_REG(RMON_R_P256TO511);
  671. DUMP_REG(RMON_R_P512TO1023);
  672. DUMP_REG(RMON_R_P1024TO2047);
  673. DUMP_REG(RMON_R_P_GTE2048);
  674. DUMP_REG(RMON_R_OCTETS);
  675. DUMP_REG(IEEE_R_DROP);
  676. DUMP_REG(IEEE_R_FRAME_OK);
  677. DUMP_REG(IEEE_R_CRC);
  678. DUMP_REG(IEEE_R_ALIGN);
  679. DUMP_REG(IEEE_R_MACERR);
  680. DUMP_REG(IEEE_R_FDXFC);
  681. DUMP_REG(IEEE_R_OCTETS_OK);
  682. DUMP_REG(ATCR);
  683. DUMP_REG(ATVR);
  684. DUMP_REG(ATOFF);
  685. DUMP_REG(ATPER);
  686. DUMP_REG(ATCOR);
  687. DUMP_REG(ATINC);
  688. DUMP_REG(ATSTMP);
  689. DUMP_REG(TGSR);
  690. }
  691. void enet_nvic_tog(void)
  692. {
  693. NVIC_SetPendingIRQ(ENET_IRQn);
  694. }
  695. void enet_rx_stat(void)
  696. {
  697. enet_data_error_stats_t *error_statistic = &imxrt_eth_device.error_statistic;
  698. #define DUMP_STAT(__VAR) \
  699. rt_kprintf("%-25s: %08X\n", #__VAR, error_statistic->__VAR);
  700. DUMP_STAT(statsRxLenGreaterErr);
  701. DUMP_STAT(statsRxAlignErr);
  702. DUMP_STAT(statsRxFcsErr);
  703. DUMP_STAT(statsRxOverRunErr);
  704. DUMP_STAT(statsRxTruncateErr);
  705. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  706. DUMP_STAT(statsRxProtocolChecksumErr);
  707. DUMP_STAT(statsRxIpHeadChecksumErr);
  708. DUMP_STAT(statsRxMacErr);
  709. DUMP_STAT(statsRxPhyErr);
  710. DUMP_STAT(statsRxCollisionErr);
  711. DUMP_STAT(statsTxErr);
  712. DUMP_STAT(statsTxFrameErr);
  713. DUMP_STAT(statsTxOverFlowErr);
  714. DUMP_STAT(statsTxLateCollisionErr);
  715. DUMP_STAT(statsTxExcessCollisionErr);
  716. DUMP_STAT(statsTxUnderFlowErr);
  717. DUMP_STAT(statsTxTsErr);
  718. #endif
  719. }
  720. void enet_buf_info(void)
  721. {
  722. int i = 0;
  723. for (i = 0; i < ENET_RXBD_NUM; i++)
  724. {
  725. rt_kprintf("%d: length: %-8d, control: %04X, buffer:%p\n",
  726. i,
  727. g_rxBuffDescrip[i].length,
  728. g_rxBuffDescrip[i].control,
  729. g_rxBuffDescrip[i].buffer);
  730. }
  731. for (i = 0; i < ENET_TXBD_NUM; i++)
  732. {
  733. rt_kprintf("%d: length: %-8d, control: %04X, buffer:%p\n",
  734. i,
  735. g_txBuffDescrip[i].length,
  736. g_txBuffDescrip[i].control,
  737. g_txBuffDescrip[i].buffer);
  738. }
  739. }
  740. FINSH_FUNCTION_EXPORT(phy_read, read phy register);
  741. FINSH_FUNCTION_EXPORT(phy_write, write phy register);
  742. FINSH_FUNCTION_EXPORT(phy_dump, dump phy registers);
  743. FINSH_FUNCTION_EXPORT(enet_reg_dump, dump enet registers);
  744. FINSH_FUNCTION_EXPORT(enet_nvic_tog, toggle enet nvic pendding bit);
  745. FINSH_FUNCTION_EXPORT(enet_rx_stat, dump enet rx statistic);
  746. FINSH_FUNCTION_EXPORT(enet_buf_info, dump enet tx and tx buffer descripter);
  747. #endif