drv_emac.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-05-16 shelton first version
  9. */
  10. #include "drv_emac.h"
  11. #include <netif/ethernetif.h>
  12. #include <lwipopts.h>
  13. /* debug option */
  14. //#define EMAC_RX_DUMP
  15. //#define EMAC_TX_DUMP
  16. //#define DRV_DEBUG
  17. #define LOG_TAG "drv.emac"
  18. #include <drv_log.h>
  19. #define CRYSTAL_ON_PHY 0
  20. /* emac memory buffer configuration */
  21. #define EMAC_NUM_RX_BUF 4 /* 0x1800 for rx (4 * 1536 = 6k) */
  22. #define EMAC_NUM_TX_BUF 2 /* 0x0600 for tx (2 * 1536 = 3k) */
  23. #define MAX_ADDR_LEN 6
  24. struct rt_at32_emac
  25. {
  26. /* inherit from ethernet device */
  27. struct eth_device parent;
  28. #ifndef PHY_USING_INTERRUPT_MODE
  29. rt_timer_t poll_link_timer;
  30. #endif
  31. /* interface address info, hw address */
  32. rt_uint8_t dev_addr[MAX_ADDR_LEN];
  33. /* emac_speed */
  34. emac_speed_type emac_speed;
  35. /* emac_duplex_mode */
  36. emac_duplex_type emac_mode;
  37. };
  38. static emac_dma_desc_type *dma_rx_dscr_tab, *dma_tx_dscr_tab;
  39. extern emac_dma_desc_type *dma_rx_desc_to_get, *dma_tx_desc_to_set;
  40. static rt_uint8_t *rx_buff, *tx_buff;
  41. static struct rt_at32_emac at32_emac_device;
  42. static uint8_t phy_addr = 0xFF;
  43. static struct rt_semaphore tx_wait;
  44. static rt_bool_t tx_is_waiting = RT_FALSE;
  45. #if defined(EMAC_RX_DUMP) || defined(EMAC_TX_DUMP)
  46. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  47. static void dump_hex(const rt_uint8_t *ptr, rt_size_t buflen)
  48. {
  49. unsigned char *buf = (unsigned char *)ptr;
  50. int i, j;
  51. for (i = 0; i < buflen; i += 16)
  52. {
  53. rt_kprintf("%08X: ", i);
  54. for (j = 0; j < 16; j++)
  55. if (i + j < buflen)
  56. rt_kprintf("%02X ", buf[i + j]);
  57. else
  58. rt_kprintf(" ");
  59. rt_kprintf(" ");
  60. for (j = 0; j < 16; j++)
  61. if (i + j < buflen)
  62. rt_kprintf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
  63. rt_kprintf("\n");
  64. }
  65. }
  66. #endif
  67. /**
  68. * @brief phy reset
  69. */
  70. static void phy_reset(void)
  71. {
  72. gpio_init_type gpio_init_struct;
  73. #if defined (SOC_SERIES_AT32F437)
  74. crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
  75. crm_periph_clock_enable(CRM_GPIOG_PERIPH_CLOCK, TRUE);
  76. gpio_default_para_init(&gpio_init_struct);
  77. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  78. gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  79. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  80. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  81. gpio_init_struct.gpio_pins = GPIO_PINS_15;
  82. gpio_init(GPIOE, &gpio_init_struct);
  83. gpio_init_struct.gpio_pins = GPIO_PINS_15;
  84. gpio_init(GPIOG, &gpio_init_struct);
  85. gpio_bits_reset(GPIOE, GPIO_PINS_15);
  86. gpio_bits_reset(GPIOG, GPIO_PINS_15);
  87. rt_thread_mdelay(2);
  88. gpio_bits_set(GPIOE, GPIO_PINS_15);
  89. #endif
  90. #if defined (SOC_SERIES_AT32F407)
  91. crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
  92. gpio_default_para_init(&gpio_init_struct);
  93. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  94. gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  95. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  96. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  97. gpio_init_struct.gpio_pins = GPIO_PINS_8;
  98. gpio_init(GPIOC, &gpio_init_struct);
  99. gpio_bits_reset(GPIOC, GPIO_PINS_8);
  100. rt_thread_mdelay(2);
  101. gpio_bits_set(GPIOC, GPIO_PINS_8);
  102. #endif
  103. rt_thread_mdelay(2000);
  104. }
  105. /**
  106. * @brief phy clock config
  107. */
  108. static void phy_clock_config(void)
  109. {
  110. #if (CRYSTAL_ON_PHY == 0)
  111. /* if CRYSTAL_NO_PHY, output clock with pa8 of mcu */
  112. gpio_init_type gpio_init_struct;
  113. crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  114. gpio_default_para_init(&gpio_init_struct);
  115. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  116. gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  117. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  118. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  119. gpio_init_struct.gpio_pins = GPIO_PINS_8;
  120. gpio_init(GPIOA, &gpio_init_struct);
  121. /* 9162 clkout output 25 mhz */
  122. /* 83848 clkout output 50 mhz */
  123. #if defined (SOC_SERIES_AT32F407)
  124. crm_clock_out_set(CRM_CLKOUT_SCLK);
  125. #if defined (PHY_USING_DM9162)
  126. crm_clkout_div_set(CRM_CLKOUT_DIV_8);
  127. #elif defined (PHY_USING_DP83848)
  128. crm_clkout_div_set(CRM_CLKOUT_DIV_4);
  129. #endif
  130. #endif
  131. #if defined (SOC_SERIES_AT32F437)
  132. crm_clock_out1_set(CRM_CLKOUT1_PLL);
  133. #if defined (PHY_USING_DM9162)
  134. crm_clkout_div_set(CRM_CLKOUT_INDEX_1, CRM_CLKOUT_DIV1_5, CRM_CLKOUT_DIV2_2);
  135. #elif defined (PHY_USING_DP83848)
  136. crm_clkout_div_set(CRM_CLKOUT_INDEX_1, CRM_CLKOUT_DIV1_5, CRM_CLKOUT_DIV2_1);
  137. #endif
  138. #endif
  139. #endif
  140. }
  141. /**
  142. * @brief reset phy register
  143. */
  144. static error_status emac_phy_register_reset(void)
  145. {
  146. uint16_t data = 0;
  147. uint32_t timeout = 0;
  148. uint32_t i = 0;
  149. if(emac_phy_register_write(phy_addr, PHY_CONTROL_REG, PHY_RESET_BIT) == ERROR)
  150. {
  151. return ERROR;
  152. }
  153. for(i = 0; i < 0x000FFFFF; i++);
  154. do
  155. {
  156. timeout++;
  157. if(emac_phy_register_read(phy_addr, PHY_CONTROL_REG, &data) == ERROR)
  158. {
  159. return ERROR;
  160. }
  161. } while((data & PHY_RESET_BIT) && (timeout < PHY_TIMEOUT));
  162. for(i = 0; i < 0x00FFFFF; i++);
  163. if(timeout == PHY_TIMEOUT)
  164. {
  165. return ERROR;
  166. }
  167. return SUCCESS;
  168. }
  169. /**
  170. * @brief set mac speed related parameters
  171. */
  172. static error_status emac_speed_config(emac_auto_negotiation_type nego, emac_duplex_type mode, emac_speed_type speed)
  173. {
  174. uint16_t data = 0;
  175. uint32_t timeout = 0;
  176. if(nego == EMAC_AUTO_NEGOTIATION_ON)
  177. {
  178. do
  179. {
  180. timeout++;
  181. if(emac_phy_register_read(phy_addr, PHY_STATUS_REG, &data) == ERROR)
  182. {
  183. return ERROR;
  184. }
  185. } while(!(data & PHY_LINKED_STATUS_BIT) && (timeout < PHY_TIMEOUT));
  186. if(timeout == PHY_TIMEOUT)
  187. {
  188. return ERROR;
  189. }
  190. timeout = 0;
  191. if(emac_phy_register_write(phy_addr, PHY_CONTROL_REG, PHY_AUTO_NEGOTIATION_BIT) == ERROR)
  192. {
  193. return ERROR;
  194. }
  195. do
  196. {
  197. timeout++;
  198. if(emac_phy_register_read(phy_addr, PHY_STATUS_REG, &data) == ERROR)
  199. {
  200. return ERROR;
  201. }
  202. } while(!(data & PHY_NEGO_COMPLETE_BIT) && (timeout < PHY_TIMEOUT));
  203. if(timeout == PHY_TIMEOUT)
  204. {
  205. return ERROR;
  206. }
  207. if(emac_phy_register_read(phy_addr, PHY_SPECIFIED_CS_REG, &data) == ERROR)
  208. {
  209. return ERROR;
  210. }
  211. #ifdef PHY_USING_DM9162
  212. if(data & PHY_FULL_DUPLEX_100MBPS_BIT)
  213. {
  214. emac_fast_speed_set(EMAC_SPEED_100MBPS);
  215. emac_duplex_mode_set(EMAC_FULL_DUPLEX);
  216. }
  217. else if(data & PHY_HALF_DUPLEX_100MBPS_BIT)
  218. {
  219. emac_fast_speed_set(EMAC_SPEED_100MBPS);
  220. emac_duplex_mode_set(EMAC_HALF_DUPLEX);
  221. }
  222. else if(data & PHY_FULL_DUPLEX_10MBPS_BIT)
  223. {
  224. emac_fast_speed_set(EMAC_SPEED_10MBPS);
  225. emac_duplex_mode_set(EMAC_FULL_DUPLEX);
  226. }
  227. else if(data & PHY_HALF_DUPLEX_10MBPS_BIT)
  228. {
  229. emac_fast_speed_set(EMAC_SPEED_10MBPS);
  230. emac_duplex_mode_set(EMAC_HALF_DUPLEX);
  231. }
  232. #endif
  233. #ifdef PHY_USING_DP83848
  234. if(data & PHY_DUPLEX_MODE)
  235. {
  236. emac_duplex_mode_set(EMAC_FULL_DUPLEX);
  237. }
  238. else
  239. {
  240. emac_duplex_mode_set(EMAC_HALF_DUPLEX);
  241. }
  242. if(data & PHY_SPEED_MODE)
  243. {
  244. emac_fast_speed_set(EMAC_SPEED_10MBPS);
  245. }
  246. else
  247. {
  248. emac_fast_speed_set(EMAC_SPEED_100MBPS);
  249. }
  250. #endif
  251. }
  252. else
  253. {
  254. if(emac_phy_register_write(phy_addr, PHY_CONTROL_REG, (uint16_t)((mode << 8) | (speed << 13))) == ERROR)
  255. {
  256. return ERROR;
  257. }
  258. if(speed == EMAC_SPEED_100MBPS)
  259. {
  260. emac_fast_speed_set(EMAC_SPEED_100MBPS);
  261. }
  262. else
  263. {
  264. emac_fast_speed_set(EMAC_SPEED_10MBPS);
  265. }
  266. if(mode == EMAC_FULL_DUPLEX)
  267. {
  268. emac_duplex_mode_set(EMAC_FULL_DUPLEX);
  269. }
  270. else
  271. {
  272. emac_duplex_mode_set(EMAC_HALF_DUPLEX);
  273. }
  274. }
  275. return SUCCESS;
  276. }
  277. /**
  278. * @brief initialize emac phy
  279. */
  280. static error_status emac_phy_init(emac_control_config_type *control_para)
  281. {
  282. emac_clock_range_set();
  283. if(emac_phy_register_reset() == ERROR)
  284. {
  285. return ERROR;
  286. }
  287. if(emac_speed_config(control_para->auto_nego, control_para->duplex_mode, control_para->fast_ethernet_speed) == ERROR)
  288. {
  289. return ERROR;
  290. }
  291. emac_control_config(control_para);
  292. return SUCCESS;
  293. }
  294. /**
  295. * @brief emac initialization function
  296. */
  297. static rt_err_t rt_at32_emac_init(rt_device_t dev)
  298. {
  299. emac_control_config_type mac_control_para;
  300. emac_dma_config_type dma_control_para;
  301. /* check till phy detected */
  302. while(phy_addr == 0xFF)
  303. {
  304. rt_thread_mdelay(1000);
  305. }
  306. /* emac reset */
  307. emac_reset();
  308. /* software reset emac dma */
  309. emac_dma_software_reset_set();
  310. while(emac_dma_software_reset_get() == SET);
  311. emac_control_para_init(&mac_control_para);
  312. mac_control_para.auto_nego = EMAC_AUTO_NEGOTIATION_ON;
  313. if(emac_phy_init(&mac_control_para) == ERROR)
  314. {
  315. LOG_E("emac hardware init failed");
  316. return -RT_ERROR;
  317. }
  318. else
  319. {
  320. LOG_D("emac hardware init success");
  321. }
  322. emac_transmit_flow_control_enable(TRUE);
  323. emac_zero_quanta_pause_disable(TRUE);
  324. /* set mac address */
  325. emac_local_address_set(at32_emac_device.dev_addr);
  326. /* set emac dma rx link list */
  327. emac_dma_descriptor_list_address_set(EMAC_DMA_RECEIVE, dma_rx_dscr_tab, rx_buff, EMAC_NUM_RX_BUF);
  328. /* set emac dma tx link list */
  329. emac_dma_descriptor_list_address_set(EMAC_DMA_TRANSMIT, dma_tx_dscr_tab, tx_buff, EMAC_NUM_TX_BUF);
  330. /* emac interrupt init */
  331. emac_dma_config(&dma_control_para);
  332. emac_dma_interrupt_enable(EMAC_DMA_INTERRUPT_NORMAL_SUMMARY, TRUE);
  333. emac_dma_interrupt_enable(EMAC_DMA_INTERRUPT_RX, TRUE);
  334. nvic_irq_enable(EMAC_IRQn, 0x07, 0);
  335. /* enable emac */
  336. emac_start();
  337. return RT_EOK;
  338. }
  339. static rt_err_t rt_at32_emac_open(rt_device_t dev, rt_uint16_t oflag)
  340. {
  341. LOG_D("emac open");
  342. return RT_EOK;
  343. }
  344. static rt_err_t rt_at32_emac_close(rt_device_t dev)
  345. {
  346. LOG_D("emac close");
  347. return RT_EOK;
  348. }
  349. static rt_size_t rt_at32_emac_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  350. {
  351. LOG_D("emac read");
  352. rt_set_errno(-RT_ENOSYS);
  353. return 0;
  354. }
  355. static rt_size_t rt_at32_emac_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  356. {
  357. LOG_D("emac write");
  358. rt_set_errno(-RT_ENOSYS);
  359. return 0;
  360. }
  361. static rt_err_t rt_at32_emac_control(rt_device_t dev, int cmd, void *args)
  362. {
  363. switch (cmd)
  364. {
  365. case NIOCTL_GADDR:
  366. /* get mac address */
  367. if (args)
  368. {
  369. SMEMCPY(args, at32_emac_device.dev_addr, 6);
  370. }
  371. else
  372. {
  373. return -RT_ERROR;
  374. }
  375. break;
  376. default :
  377. break;
  378. }
  379. return RT_EOK;
  380. }
  381. /**
  382. * @brief transmit data
  383. */
  384. rt_err_t rt_at32_emac_tx(rt_device_t dev, struct pbuf *p)
  385. {
  386. struct pbuf *q;
  387. rt_uint32_t offset;
  388. while ((dma_tx_desc_to_set->status & EMAC_DMATXDESC_OWN) != RESET)
  389. {
  390. rt_err_t result;
  391. rt_uint32_t level;
  392. level = rt_hw_interrupt_disable();
  393. tx_is_waiting = RT_TRUE;
  394. rt_hw_interrupt_enable(level);
  395. /* it's own bit set, wait it */
  396. result = rt_sem_take(&tx_wait, RT_WAITING_FOREVER);
  397. if (result == RT_EOK) break;
  398. if (result == -RT_ERROR) return -RT_ERROR;
  399. }
  400. offset = 0;
  401. for (q = p; q != NULL; q = q->next)
  402. {
  403. uint8_t *buffer;
  404. /* copy the frame to be sent into memory pointed by the current ethernet dma tx descriptor */
  405. buffer = (uint8_t*)((dma_tx_desc_to_set->buf1addr) + offset);
  406. SMEMCPY(buffer, q->payload, q->len);
  407. offset += q->len;
  408. }
  409. #ifdef EMAC_TX_DUMP
  410. dump_hex(p->payload, p->tot_len);
  411. #endif
  412. /* prepare transmit descriptors to give to dma */
  413. LOG_D("transmit frame length :%d", p->tot_len);
  414. /* setting the frame length: bits[12:0] */
  415. dma_tx_desc_to_set->controlsize = (p->tot_len & EMAC_DMATXDESC_TBS1);
  416. /* Setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
  417. dma_tx_desc_to_set->status |= EMAC_DMATXDESC_LS | EMAC_DMATXDESC_FS;
  418. /* enable tx completion interrupt */
  419. dma_tx_desc_to_set->status |= EMAC_DMATXDESC_IC;
  420. /* set own bit of the tx descriptor status: gives the buffer back to ethernet dma */
  421. dma_tx_desc_to_set->status |= EMAC_DMATXDESC_OWN;
  422. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  423. if(emac_dma_flag_get(EMAC_DMA_TBU_FLAG) != RESET)
  424. {
  425. emac_dma_flag_clear(EMAC_DMA_TBU_FLAG);
  426. emac_dma_poll_demand_set(EMAC_DMA_TRANSMIT, 0);
  427. }
  428. /* selects the next dma tx descriptor list for next buffer to send */
  429. dma_tx_desc_to_set = (emac_dma_desc_type*) (dma_tx_desc_to_set->buf2nextdescaddr);
  430. return ERR_OK;
  431. }
  432. /**
  433. * @brief receive data
  434. */
  435. struct pbuf *rt_at32_emac_rx(rt_device_t dev)
  436. {
  437. struct pbuf *p = NULL;
  438. struct pbuf *q = NULL;
  439. rt_uint32_t offset = 0;
  440. uint16_t len = 0;
  441. uint8_t *buffer;
  442. /* get received frame */
  443. len = emac_received_packet_size_get();
  444. if(len > 0)
  445. {
  446. LOG_D("receive frame len : %d", len);
  447. /* we allocate a pbuf chain of pbufs from the lwip buffer pool */
  448. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  449. if(p != NULL)
  450. {
  451. for (q = p; q != RT_NULL; q= q->next)
  452. {
  453. /* get rx buffer */
  454. buffer = (uint8_t *)(dma_rx_desc_to_get->buf1addr);
  455. #ifdef EMAC_RX_DUMP
  456. dump_hex(buffer, len);
  457. #endif
  458. /* copy the received frame into buffer from memory pointed by the current ethernet dma rx descriptor */
  459. SMEMCPY(q->payload, (buffer + offset), q->len);
  460. offset += q->len;
  461. }
  462. }
  463. }
  464. else
  465. {
  466. return p;
  467. }
  468. /* release descriptors to dma */
  469. dma_rx_desc_to_get->status |= EMAC_DMARXDESC_OWN;
  470. /* when rx buffer unavailable flag is set: clear it and resume reception */
  471. if(emac_dma_flag_get(EMAC_DMA_RBU_FLAG) != RESET)
  472. {
  473. /* clear rbu ethernet dma flag */
  474. emac_dma_flag_clear(EMAC_DMA_RBU_FLAG);
  475. /* resume dma reception */
  476. emac_dma_poll_demand_set(EMAC_DMA_RECEIVE, 0);
  477. }
  478. /* update the ethernet dma global rx descriptor with next rx decriptor */
  479. /* chained mode */
  480. if((dma_rx_desc_to_get->controlsize & EMAC_DMARXDESC_RCH) != RESET)
  481. {
  482. /* selects the next dma rx descriptor list for next buffer to read */
  483. dma_rx_desc_to_get = (emac_dma_desc_type*) (dma_rx_desc_to_get->buf2nextdescaddr);
  484. }
  485. /* ring mode */
  486. else
  487. {
  488. if((dma_rx_desc_to_get->controlsize & EMAC_DMARXDESC_RER) != RESET)
  489. {
  490. /* selects the first dma rx descriptor for next buffer to read: last rx descriptor was used */
  491. dma_rx_desc_to_get = (emac_dma_desc_type*) (EMAC_DMA->rdladdr);
  492. }
  493. else
  494. {
  495. /* selects the next dma rx descriptor list for next buffer to read */
  496. dma_rx_desc_to_get = (emac_dma_desc_type*) ((uint32_t)dma_rx_desc_to_get + 0x10 + ((EMAC_DMA->bm & 0x0000007C) >> 2));
  497. }
  498. }
  499. return p;
  500. }
  501. void EMAC_IRQHandler(void)
  502. {
  503. /* enter interrupt */
  504. rt_interrupt_enter();
  505. /* clear received it */
  506. if(emac_dma_flag_get(EMAC_DMA_NIS_FLAG) != RESET)
  507. {
  508. emac_dma_flag_clear(EMAC_DMA_NIS_FLAG);
  509. }
  510. if(emac_dma_flag_get(EMAC_DMA_AIS_FLAG) != RESET)
  511. {
  512. emac_dma_flag_clear(EMAC_DMA_AIS_FLAG);
  513. }
  514. if(emac_dma_flag_get(EMAC_DMA_OVF_FLAG) != RESET)
  515. {
  516. emac_dma_flag_clear(EMAC_DMA_OVF_FLAG);
  517. }
  518. if(emac_dma_flag_get(EMAC_DMA_RBU_FLAG) != RESET)
  519. {
  520. emac_dma_flag_clear(EMAC_DMA_RBU_FLAG);
  521. }
  522. /* packet receiption */
  523. if (emac_dma_flag_get(EMAC_DMA_RI_FLAG) == SET)
  524. {
  525. /* a frame has been received */
  526. eth_device_ready(&(at32_emac_device.parent));
  527. emac_dma_flag_clear(EMAC_DMA_RI_FLAG);
  528. }
  529. /* packet transmission */
  530. if (emac_dma_flag_get(EMAC_DMA_TI_FLAG) == SET)
  531. {
  532. if (tx_is_waiting == RT_TRUE)
  533. {
  534. tx_is_waiting = RT_FALSE;
  535. rt_sem_release(&tx_wait);
  536. }
  537. emac_dma_flag_clear(EMAC_DMA_TI_FLAG);
  538. }
  539. /* leave interrupt */
  540. rt_interrupt_leave();
  541. }
  542. enum {
  543. PHY_LINK = (1 << 0),
  544. PHY_10M = (1 << 1),
  545. PHY_FULLDUPLEX = (1 << 2),
  546. };
  547. static void phy_linkchange()
  548. {
  549. static rt_uint8_t phy_speed = 0;
  550. rt_uint8_t phy_speed_new = 0;
  551. rt_uint16_t status;
  552. emac_phy_register_read(phy_addr, PHY_BASIC_STATUS_REG, (uint16_t *)&status);
  553. LOG_D("phy basic status reg is 0x%X", status);
  554. if (status & (PHY_AUTONEGO_COMPLETE_MASK | PHY_LINKED_STATUS_MASK))
  555. {
  556. rt_uint16_t SR = 0;
  557. phy_speed_new |= PHY_LINK;
  558. emac_phy_register_read(phy_addr, PHY_SPECIFIED_CS_REG, (uint16_t *)&SR);
  559. LOG_D("phy control status reg is 0x%X", SR);
  560. if (SR & (PHY_SPEED_MODE))
  561. {
  562. phy_speed_new |= PHY_10M;
  563. }
  564. if (SR & (PHY_DUPLEX_MODE))
  565. {
  566. phy_speed_new |= PHY_FULLDUPLEX;
  567. }
  568. }
  569. if (phy_speed != phy_speed_new)
  570. {
  571. phy_speed = phy_speed_new;
  572. if (phy_speed & PHY_LINK)
  573. {
  574. LOG_D("link up");
  575. if (phy_speed & PHY_10M)
  576. {
  577. LOG_D("10Mbps");
  578. at32_emac_device.emac_speed = EMAC_SPEED_10MBPS;
  579. }
  580. else
  581. {
  582. at32_emac_device.emac_speed = EMAC_SPEED_100MBPS;
  583. LOG_D("100Mbps");
  584. }
  585. if (phy_speed & PHY_FULLDUPLEX)
  586. {
  587. LOG_D("full-duplex");
  588. at32_emac_device.emac_mode = EMAC_FULL_DUPLEX;
  589. }
  590. else
  591. {
  592. LOG_D("half-duplex");
  593. at32_emac_device.emac_mode = EMAC_HALF_DUPLEX;
  594. }
  595. /* send link up. */
  596. eth_device_linkchange(&at32_emac_device.parent, RT_TRUE);
  597. }
  598. else
  599. {
  600. LOG_I("link down");
  601. eth_device_linkchange(&at32_emac_device.parent, RT_FALSE);
  602. }
  603. }
  604. }
  605. #ifdef PHY_USING_INTERRUPT_MODE
  606. static void emac_phy_isr(void *args)
  607. {
  608. rt_uint32_t status = 0;
  609. emac_phy_register_read(phy_addr, PHY_INTERRUPT_FLAG_REG, (uint16_t *)&status);
  610. LOG_D("phy interrupt status reg is 0x%X", status);
  611. phy_linkchange();
  612. }
  613. #endif /* PHY_USING_INTERRUPT_MODE */
  614. static void phy_monitor_thread_entry(void *parameter)
  615. {
  616. uint8_t detected_count = 0;
  617. while(phy_addr == 0xFF)
  618. {
  619. /* phy search */
  620. rt_uint32_t i, temp;
  621. for (i = 0; i <= 0x1F; i++)
  622. {
  623. emac_phy_register_read(i, PHY_BASIC_STATUS_REG, (uint16_t *)&temp);
  624. if (temp != 0xFFFF && temp != 0x00)
  625. {
  626. phy_addr = i;
  627. break;
  628. }
  629. }
  630. detected_count++;
  631. rt_thread_mdelay(1000);
  632. if (detected_count > 10)
  633. {
  634. LOG_E("No PHY device was detected, please check hardware!");
  635. }
  636. }
  637. LOG_D("Found a phy, address:0x%02X", phy_addr);
  638. /* reset phy */
  639. LOG_D("RESET PHY!");
  640. emac_phy_register_write(phy_addr, PHY_BASIC_CONTROL_REG, PHY_RESET_MASK);
  641. rt_thread_mdelay(2000);
  642. emac_phy_register_write(phy_addr, PHY_BASIC_CONTROL_REG, PHY_AUTO_NEGOTIATION_MASK);
  643. phy_linkchange();
  644. #ifdef PHY_USING_INTERRUPT_MODE
  645. /* configuration intterrupt pin */
  646. rt_pin_mode(PHY_INT_PIN, PIN_MODE_INPUT_PULLUP);
  647. rt_pin_attach_irq(PHY_INT_PIN, PIN_IRQ_MODE_FALLING, emac_phy_isr, (void *)"callbackargs");
  648. rt_pin_irq_enable(PHY_INT_PIN, PIN_IRQ_ENABLE);
  649. /* enable phy interrupt */
  650. emac_phy_register_write(phy_addr, PHY_INTERRUPT_MASK_REG, PHY_INT_MASK);
  651. #if defined(PHY_INTERRUPT_CTRL_REG)
  652. emac_phy_register_write(phy_addr, PHY_INTERRUPT_CTRL_REG, PHY_INTERRUPT_EN);
  653. #endif
  654. #else /* PHY_USING_INTERRUPT_MODE */
  655. at32_emac_device.poll_link_timer = rt_timer_create("phylnk", (void (*)(void*))phy_linkchange,
  656. NULL, RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC);
  657. if (!at32_emac_device.poll_link_timer || rt_timer_start(at32_emac_device.poll_link_timer) != RT_EOK)
  658. {
  659. LOG_E("Start link change detection timer failed");
  660. }
  661. #endif /* PHY_USING_INTERRUPT_MODE */
  662. }
  663. /* Register the EMAC device */
  664. static int rt_hw_at32_emac_init(void)
  665. {
  666. rt_err_t state = RT_EOK;
  667. /* Prepare receive and send buffers */
  668. rx_buff = (rt_uint8_t *)rt_calloc(EMAC_NUM_RX_BUF, EMAC_MAX_PACKET_LENGTH);
  669. if (rx_buff == RT_NULL)
  670. {
  671. LOG_E("No memory");
  672. state = -RT_ENOMEM;
  673. goto __exit;
  674. }
  675. tx_buff = (rt_uint8_t *)rt_calloc(EMAC_NUM_TX_BUF, EMAC_MAX_PACKET_LENGTH);
  676. if (tx_buff == RT_NULL)
  677. {
  678. LOG_E("No memory");
  679. state = -RT_ENOMEM;
  680. goto __exit;
  681. }
  682. dma_rx_dscr_tab = (emac_dma_desc_type *)rt_calloc(EMAC_NUM_RX_BUF, sizeof(emac_dma_desc_type));
  683. if (dma_rx_dscr_tab == RT_NULL)
  684. {
  685. LOG_E("No memory");
  686. state = -RT_ENOMEM;
  687. goto __exit;
  688. }
  689. dma_tx_dscr_tab = (emac_dma_desc_type *)rt_calloc(EMAC_NUM_TX_BUF, sizeof(emac_dma_desc_type));
  690. if (dma_tx_dscr_tab == RT_NULL)
  691. {
  692. LOG_E("No memory");
  693. state = -RT_ENOMEM;
  694. goto __exit;
  695. }
  696. /* phy clock */
  697. phy_clock_config();
  698. /* enable periph clock */
  699. crm_periph_clock_enable(CRM_EMAC_PERIPH_CLOCK, TRUE);
  700. crm_periph_clock_enable(CRM_EMACTX_PERIPH_CLOCK, TRUE);
  701. crm_periph_clock_enable(CRM_EMACRX_PERIPH_CLOCK, TRUE);
  702. /* interface mode */
  703. #if defined (SOC_SERIES_AT32F407)
  704. gpio_pin_remap_config(MII_RMII_SEL_GMUX, TRUE);
  705. #endif
  706. #if defined (SOC_SERIES_AT32F437)
  707. scfg_emac_interface_set(SCFG_EMAC_SELECT_RMII);
  708. #endif
  709. /* emac gpio init */
  710. at32_msp_emac_init(NULL);
  711. at32_emac_device.emac_speed = EMAC_SPEED_100MBPS;
  712. at32_emac_device.emac_mode = EMAC_FULL_DUPLEX;
  713. at32_emac_device.dev_addr[0] = 0x00;
  714. at32_emac_device.dev_addr[1] = 0x66;
  715. at32_emac_device.dev_addr[2] = 0x88;
  716. /* generate mac addr from unique id (only for test). */
  717. at32_emac_device.dev_addr[3] = *(rt_uint8_t *)(0x1FFFF7E8 + 4);
  718. at32_emac_device.dev_addr[4] = *(rt_uint8_t *)(0x1FFFF7E8 + 2);
  719. at32_emac_device.dev_addr[5] = *(rt_uint8_t *)(0x1FFFF7E8 + 0);
  720. at32_emac_device.parent.parent.init = rt_at32_emac_init;
  721. at32_emac_device.parent.parent.open = rt_at32_emac_open;
  722. at32_emac_device.parent.parent.close = rt_at32_emac_close;
  723. at32_emac_device.parent.parent.read = rt_at32_emac_read;
  724. at32_emac_device.parent.parent.write = rt_at32_emac_write;
  725. at32_emac_device.parent.parent.control = rt_at32_emac_control;
  726. at32_emac_device.parent.parent.user_data = RT_NULL;
  727. at32_emac_device.parent.eth_rx = rt_at32_emac_rx;
  728. at32_emac_device.parent.eth_tx = rt_at32_emac_tx;
  729. /* reset phy */
  730. phy_reset();
  731. /* start phy monitor */
  732. rt_thread_t tid;
  733. tid = rt_thread_create("phy",
  734. phy_monitor_thread_entry,
  735. RT_NULL,
  736. 1024,
  737. RT_THREAD_PRIORITY_MAX - 2,
  738. 2);
  739. if (tid != RT_NULL)
  740. {
  741. rt_thread_startup(tid);
  742. }
  743. else
  744. {
  745. state = -RT_ERROR;
  746. }
  747. /* register eth device */
  748. state = eth_device_init(&(at32_emac_device.parent), "e0");
  749. if (RT_EOK == state)
  750. {
  751. LOG_D("emac device init success");
  752. }
  753. else
  754. {
  755. LOG_E("emac device init faild: %d", state);
  756. state = -RT_ERROR;
  757. goto __exit;
  758. }
  759. __exit:
  760. if (state != RT_EOK)
  761. {
  762. if (rx_buff)
  763. {
  764. rt_free(rx_buff);
  765. }
  766. if (tx_buff)
  767. {
  768. rt_free(tx_buff);
  769. }
  770. if (dma_rx_dscr_tab)
  771. {
  772. rt_free(dma_rx_dscr_tab);
  773. }
  774. if (dma_tx_dscr_tab)
  775. {
  776. rt_free(dma_tx_dscr_tab);
  777. }
  778. }
  779. return state;
  780. }
  781. INIT_DEVICE_EXPORT(rt_hw_at32_emac_init);