emac_main.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "rom/ets_sys.h"
  17. #include "rom/gpio.h"
  18. #include "soc/dport_reg.h"
  19. #include "soc/io_mux_reg.h"
  20. #include "soc/rtc.h"
  21. #include "soc/rtc_cntl_reg.h"
  22. #include "soc/gpio_reg.h"
  23. #include "soc/dport_reg.h"
  24. #include "soc/emac_ex_reg.h"
  25. #include "soc/emac_reg_v2.h"
  26. #include "soc/soc.h"
  27. #include "tcpip_adapter.h"
  28. #include "sdkconfig.h"
  29. #include "esp_task_wdt.h"
  30. #include "esp_event.h"
  31. #include "esp_system.h"
  32. #include "esp_err.h"
  33. #include "esp_log.h"
  34. #include "esp_eth.h"
  35. #include "esp_intr_alloc.h"
  36. #include "esp_pm.h"
  37. #include "esp_spiram.h"
  38. #include "driver/periph_ctrl.h"
  39. #include "emac_common.h"
  40. #include "emac_desc.h"
  41. #include "freertos/xtensa_api.h"
  42. #include "freertos/FreeRTOS.h"
  43. #include "freertos/task.h"
  44. #include "freertos/queue.h"
  45. #include "freertos/semphr.h"
  46. #include "freertos/timers.h"
  47. #include "lwip/err.h"
  48. #define EMAC_EVT_QNUM 200
  49. #define EMAC_SIG_MAX 50
  50. static struct emac_config_data emac_config;
  51. static dma_extended_desc_t *emac_dma_rx_chain_buf;
  52. static dma_extended_desc_t *emac_dma_tx_chain_buf;
  53. static uint8_t *emac_dma_rx_buf[DMA_RX_BUF_NUM];
  54. static uint8_t *emac_dma_tx_buf[DMA_TX_BUF_NUM];
  55. static SemaphoreHandle_t emac_g_sem = NULL;
  56. static portMUX_TYPE g_emac_mux = portMUX_INITIALIZER_UNLOCKED;
  57. static xTaskHandle emac_task_hdl = NULL;
  58. static xQueueHandle emac_xqueue = NULL;
  59. static uint8_t emac_sig_cnt[EMAC_SIG_MAX] = {0};
  60. static TimerHandle_t emac_timer = NULL;
  61. static SemaphoreHandle_t emac_rx_xMutex = NULL;
  62. static SemaphoreHandle_t emac_tx_xMutex = NULL;
  63. static intr_handle_t eth_intr_handle = NULL;
  64. static const char *TAG = "emac";
  65. static bool pause_send = false;
  66. #ifdef CONFIG_PM_ENABLE
  67. static esp_pm_lock_handle_t s_pm_lock;
  68. #endif
  69. static esp_err_t emac_ioctl(emac_sig_t sig, emac_par_t par);
  70. esp_err_t emac_post(emac_sig_t sig, emac_par_t par);
  71. static void emac_macaddr_init(void)
  72. {
  73. esp_read_mac(&(emac_config.macaddr[0]), ESP_MAC_ETH);
  74. }
  75. void esp_eth_get_mac(uint8_t mac[6])
  76. {
  77. memcpy(mac, &(emac_config.macaddr[0]), 6);
  78. }
  79. esp_err_t esp_eth_set_mac(const uint8_t mac[6])
  80. {
  81. if (!(mac[0] & 0x01)) {
  82. memcpy(&(emac_config.macaddr[0]), mac, 6);
  83. return ESP_OK;
  84. } else {
  85. return ESP_ERR_INVALID_MAC;
  86. }
  87. }
  88. eth_speed_mode_t esp_eth_get_speed(void)
  89. {
  90. return emac_config.emac_phy_get_speed_mode();
  91. }
  92. static void emac_setup_tx_desc(dma_extended_desc_t *tx_desc, uint32_t size)
  93. {
  94. tx_desc->basic.desc1 = size & 0xfff;
  95. tx_desc->basic.desc0 = EMAC_DESC_TX_OWN | EMAC_DESC_INT_COMPL | EMAC_DESC_LAST_SEGMENT |
  96. EMAC_DESC_FIRST_SEGMENT | EMAC_DESC_SECOND_ADDR_CHAIN;
  97. }
  98. static void emac_clean_tx_desc(dma_extended_desc_t *tx_desc)
  99. {
  100. tx_desc->basic.desc1 = 0;
  101. tx_desc->basic.desc0 = 0;
  102. }
  103. static void emac_clean_rx_desc(dma_extended_desc_t *rx_desc, uint32_t buf_ptr)
  104. {
  105. if (buf_ptr != 0) {
  106. rx_desc->basic.desc2 = buf_ptr;
  107. }
  108. rx_desc->basic.desc1 = EMAC_DESC_RX_SECOND_ADDR_CHAIN | DMA_RX_BUF_SIZE;
  109. rx_desc->basic.desc0 = EMAC_DESC_RX_OWN;
  110. }
  111. static void emac_set_tx_base_reg(void)
  112. {
  113. REG_WRITE(EMAC_DMATXBASEADDR_REG, (uint32_t)(emac_config.dma_etx));
  114. }
  115. static void emac_set_rx_base_reg(void)
  116. {
  117. REG_WRITE(EMAC_DMARXBASEADDR_REG, (uint32_t)(emac_config.dma_erx));
  118. }
  119. /*
  120. * dirty_rx indicates the hardware has been fed with data packets and is the
  121. * first node software needs to handle;
  122. *
  123. * cur_rx indicates the completion of software handling and is the last node
  124. * hardware could use;
  125. *
  126. * cnt_rx is to count the numbers of packets handled by software, passed to
  127. * protocol stack and not been freed.
  128. *
  129. * (1) Initializing the Linked List. Connect the numerable nodes to a circular
  130. * linked list, appoint one of the nodes as the head node, mark* the dirty_rx
  131. * and cur_rx into the node, and mount the node on the hardware base address.
  132. * Initialize cnt_rx into 0.
  133. *
  134. * (2) When hardware receives packets, nodes of linked lists will be fed with
  135. * data packets from the base address by turns, marks the node
  136. * of linked lists as “HARDWARE UNUSABLE” and reports interrupts.
  137. *
  138. * (3) When the software receives the interrupts, it will handle the linked
  139. * lists by turns from dirty_rx, send data packets to protocol
  140. * stack. dirty_rx will deviate backwards by turns and cnt_rx will by turns ++.
  141. *
  142. * (4) After the protocol stack handles all the data and calls the free function,
  143. * it will deviate backwards by turns from cur_rx, mark the * node of linked
  144. * lists as “HARDWARE USABLE” and cnt_rx will by turns --.
  145. *
  146. * (5) Cycle from Step 2 to Step 4 without break and build up circular linked
  147. * list handling.
  148. */
  149. static void emac_reset_dma_chain(void)
  150. {
  151. emac_config.cnt_tx = 0;
  152. emac_config.cur_tx = 0;
  153. emac_config.dirty_tx = 0;
  154. emac_config.cnt_rx = 0;
  155. emac_config.cur_rx = 0;
  156. emac_config.dirty_rx = 0;
  157. }
  158. static void emac_init_dma_chain(void)
  159. {
  160. int i;
  161. uint32_t dma_phy;
  162. dma_extended_desc_t *p = NULL;
  163. //init tx chain
  164. emac_config.dma_etx = emac_dma_tx_chain_buf;
  165. emac_config.cnt_tx = 0;
  166. emac_config.cur_tx = 0;
  167. emac_config.dirty_tx = 0;
  168. dma_phy = (uint32_t)(emac_config.dma_etx);
  169. p = emac_config.dma_etx;
  170. for (i = 0; i < (DMA_TX_BUF_NUM - 1); i++) {
  171. dma_phy += sizeof(dma_extended_desc_t);
  172. emac_clean_tx_desc(p);
  173. p->basic.desc2 = (uint32_t)(emac_dma_tx_buf[i]);
  174. p->basic.desc3 = dma_phy;
  175. p++;
  176. }
  177. emac_clean_tx_desc(p);
  178. p->basic.desc2 = (uint32_t)(emac_dma_tx_buf[i]);
  179. p->basic.desc3 = (uint32_t)(emac_config.dma_etx);
  180. //init rx chain
  181. emac_config.dma_erx = emac_dma_rx_chain_buf;
  182. emac_config.cnt_rx = 0;
  183. emac_config.cur_rx = 0;
  184. emac_config.dirty_rx = 0;
  185. dma_phy = (uint32_t)(emac_config.dma_erx);
  186. p = emac_config.dma_erx;
  187. for (i = 0; i < (DMA_RX_BUF_NUM - 1); i++) {
  188. dma_phy += sizeof(dma_extended_desc_t);
  189. emac_clean_rx_desc(p, (uint32_t)(emac_dma_rx_buf[i]));
  190. p->basic.desc3 = dma_phy;
  191. p++;
  192. }
  193. emac_clean_rx_desc(p, (uint32_t)(emac_dma_rx_buf[i]));
  194. p->basic.desc3 = (uint32_t)(emac_config.dma_erx);
  195. }
  196. void esp_eth_smi_write(uint32_t reg_num, uint16_t value)
  197. {
  198. uint32_t phy_num = emac_config.phy_addr;
  199. while (REG_GET_BIT(EMAC_GMIIADDR_REG, EMAC_MIIBUSY) == 1) {
  200. }
  201. REG_WRITE(EMAC_MIIDATA_REG, value);
  202. REG_WRITE(EMAC_GMIIADDR_REG, 0x3 | ((reg_num & 0x1f) << 6) | ((phy_num & 0x1f) << 11) | ((0x3) << 2));
  203. while (REG_GET_BIT(EMAC_GMIIADDR_REG, EMAC_MIIBUSY) == 1) {
  204. }
  205. }
  206. uint16_t esp_eth_smi_read(uint32_t reg_num)
  207. {
  208. uint32_t phy_num = emac_config.phy_addr;
  209. uint16_t value = 0;
  210. while (REG_GET_BIT(EMAC_GMIIADDR_REG, EMAC_MIIBUSY) == 1) {
  211. }
  212. REG_WRITE(EMAC_GMIIADDR_REG, 0x1 | ((reg_num & 0x1f) << 6) | ((phy_num & 0x1f) << 11) | (0x3 << 2));
  213. while (REG_GET_BIT(EMAC_GMIIADDR_REG, EMAC_MIIBUSY) == 1) {
  214. }
  215. value = (REG_READ(EMAC_MIIDATA_REG) & 0xffff);
  216. return value;
  217. }
  218. esp_err_t esp_eth_smi_wait_value(uint32_t reg_num, uint16_t value, uint16_t value_mask, int timeout_ms)
  219. {
  220. unsigned start = xTaskGetTickCount();
  221. unsigned timeout_ticks = (timeout_ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
  222. uint16_t current_value = 0;
  223. while (timeout_ticks == 0 || (xTaskGetTickCount() - start < timeout_ticks)) {
  224. current_value = esp_eth_smi_read(reg_num);
  225. if ((current_value & value_mask) == (value & value_mask)) {
  226. return ESP_OK;
  227. }
  228. vTaskDelay(1);
  229. }
  230. ESP_LOGE(TAG, "Timed out waiting for PHY register 0x%x to have value 0x%04x(mask 0x%04x). Current value 0x%04x",
  231. reg_num, value, value_mask, current_value);
  232. return ESP_ERR_TIMEOUT;
  233. }
  234. esp_err_t emac_reset(void)
  235. {
  236. REG_SET_BIT(EMAC_DMABUSMODE_REG, EMAC_SW_RST);
  237. if (emac_config.reset_timeout_ms) {
  238. int start = xTaskGetTickCount();
  239. uint32_t timeout_ticks = (emac_config.reset_timeout_ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
  240. while (timeout_ticks == 0 || (xTaskGetTickCount() - start < timeout_ticks)) {
  241. if (REG_GET_BIT(EMAC_DMABUSMODE_REG, EMAC_SW_RST) != EMAC_SW_RST) {
  242. goto reset_ok;
  243. }
  244. vTaskDelay(1);
  245. }
  246. ESP_LOGE(TAG, "Reset EMAC Timeout");
  247. return ESP_ERR_TIMEOUT;
  248. }
  249. /* infinite wait loop */
  250. else {
  251. while (REG_GET_BIT(EMAC_DMABUSMODE_REG, EMAC_SW_RST) == EMAC_SW_RST) {
  252. //nothing to do ,if stop here,maybe emac have not clk input.
  253. ESP_LOGI(TAG, "emac resetting ....");
  254. }
  255. }
  256. reset_ok:
  257. ESP_LOGI(TAG, "emac reset done");
  258. return ESP_OK;
  259. }
  260. static void emac_set_user_config_data(eth_config_t *config)
  261. {
  262. emac_config.phy_addr = config->phy_addr;
  263. emac_config.mac_mode = config->mac_mode;
  264. emac_config.clock_mode = config->clock_mode;
  265. emac_config.phy_init = config->phy_init;
  266. emac_config.emac_tcpip_input = config->tcpip_input;
  267. emac_config.emac_gpio_config = config->gpio_config;
  268. emac_config.emac_phy_check_link = config->phy_check_link;
  269. emac_config.emac_phy_check_init = config->phy_check_init;
  270. emac_config.emac_phy_get_speed_mode = config->phy_get_speed_mode;
  271. emac_config.emac_phy_get_duplex_mode = config->phy_get_duplex_mode;
  272. emac_config.reset_timeout_ms = config->reset_timeout_ms;
  273. #if DMA_RX_BUF_NUM > 9
  274. emac_config.emac_flow_ctrl_enable = config->flow_ctrl_enable;
  275. #else
  276. if (config->flow_ctrl_enable == true) {
  277. ESP_LOGE(TAG, "Can only configure flow_ctrl_enable==true if DMA_RX_BUF_NUM in menuconfig is >9. Disabling flow control.");
  278. }
  279. emac_config.emac_flow_ctrl_enable = false;
  280. #endif
  281. emac_config.emac_phy_get_partner_pause_enable = config->phy_get_partner_pause_enable;
  282. emac_config.emac_phy_power_enable = config->phy_power_enable;
  283. emac_config.promiscuous_enable = config->promiscuous_enable;
  284. }
  285. static void emac_enable_intr()
  286. {
  287. REG_WRITE(EMAC_DMAIN_EN_REG, EMAC_INTR_ENABLE_BIT);
  288. }
  289. static void emac_disable_intr()
  290. {
  291. REG_WRITE(EMAC_DMAIN_EN_REG, 0);
  292. }
  293. static esp_err_t emac_verify_args(void)
  294. {
  295. esp_err_t ret = ESP_OK;
  296. if (emac_config.phy_addr > PHY31) {
  297. ESP_LOGE(TAG, "phy addr err");
  298. ret = ESP_FAIL;
  299. }
  300. if (emac_config.mac_mode != ETH_MODE_RMII) {
  301. ESP_LOGE(TAG, "mac mode err, currently only support for RMII");
  302. ret = ESP_FAIL;
  303. }
  304. if (emac_config.clock_mode > ETH_CLOCK_GPIO17_OUT) {
  305. ESP_LOGE(TAG, "emac clock mode err");
  306. ret = ESP_FAIL;
  307. }
  308. if (emac_config.phy_init == NULL) {
  309. ESP_LOGE(TAG, "phy_init func is null");
  310. ret = ESP_FAIL;
  311. }
  312. if (emac_config.emac_tcpip_input == NULL) {
  313. ESP_LOGE(TAG, "tcpip_input func is null");
  314. ret = ESP_FAIL;
  315. }
  316. if (emac_config.emac_gpio_config == NULL) {
  317. ESP_LOGE(TAG, "gpio config func is null");
  318. ret = ESP_FAIL;
  319. }
  320. if (emac_config.emac_phy_check_link == NULL) {
  321. ESP_LOGE(TAG, "phy check link func is null");
  322. ret = ESP_FAIL;
  323. }
  324. if (emac_config.emac_phy_check_init == NULL) {
  325. ESP_LOGE(TAG, "phy check init func is null");
  326. ret = ESP_FAIL;
  327. }
  328. if (emac_config.emac_phy_get_speed_mode == NULL) {
  329. ESP_LOGE(TAG, "phy get speed mode func is null");
  330. ret = ESP_FAIL;
  331. }
  332. if (emac_config.emac_phy_get_duplex_mode == NULL) {
  333. ESP_LOGE(TAG, "phy get duplex mode func is null");
  334. ret = ESP_FAIL;
  335. }
  336. if (emac_config.emac_flow_ctrl_enable && !emac_config.emac_phy_get_partner_pause_enable) {
  337. ESP_LOGE(TAG, "phy get partner pause enable func is null");
  338. ret = ESP_FAIL;
  339. }
  340. if (emac_config.emac_phy_power_enable == NULL) {
  341. ESP_LOGE(TAG, "phy power enable func is null");
  342. ret = ESP_FAIL;
  343. }
  344. return ret;
  345. }
  346. static void emac_process_tx(void)
  347. {
  348. uint32_t cur_tx_desc = emac_read_tx_cur_reg();
  349. if (emac_config.emac_status == EMAC_RUNTIME_STOP) {
  350. return;
  351. }
  352. xSemaphoreTakeRecursive(emac_tx_xMutex, portMAX_DELAY);
  353. while (((uint32_t) & (emac_config.dma_etx[emac_config.dirty_tx])) != cur_tx_desc) {
  354. emac_clean_tx_desc(&(emac_config.dma_etx[emac_config.dirty_tx]));
  355. emac_config.dirty_tx = (emac_config.dirty_tx + 1) % DMA_TX_BUF_NUM;
  356. emac_config.cnt_tx--;
  357. if (emac_config.cnt_tx < 0) {
  358. ESP_LOGE(TAG, "emac tx chain err");
  359. }
  360. cur_tx_desc = emac_read_tx_cur_reg();
  361. }
  362. xSemaphoreGiveRecursive(emac_tx_xMutex);
  363. }
  364. void esp_eth_free_rx_buf(void *buf)
  365. {
  366. xSemaphoreTakeRecursive(emac_rx_xMutex, portMAX_DELAY);
  367. emac_clean_rx_desc(&(emac_config.dma_erx[emac_config.cur_rx]), (uint32_t)buf);
  368. emac_config.cur_rx = (emac_config.cur_rx + 1) % DMA_RX_BUF_NUM;
  369. emac_config.cnt_rx--;
  370. if (emac_config.cnt_rx < 0) {
  371. ESP_LOGE(TAG, "emac rx buf err");
  372. }
  373. emac_poll_rx_cmd();
  374. xSemaphoreGiveRecursive(emac_rx_xMutex);
  375. if (emac_config.emac_flow_ctrl_partner_support) {
  376. portENTER_CRITICAL(&g_emac_mux);
  377. if (pause_send && emac_config.cnt_rx < FLOW_CONTROL_LOW_WATERMARK) {
  378. emac_send_pause_zero_frame_enable();
  379. pause_send = false;
  380. }
  381. portEXIT_CRITICAL(&g_emac_mux);
  382. }
  383. }
  384. static uint32_t IRAM_ATTR emac_get_rxbuf_count_in_intr(void)
  385. {
  386. uint32_t cnt = 0;
  387. uint32_t cur_rx_desc = emac_read_rx_cur_reg();
  388. dma_extended_desc_t *cur_desc = (dma_extended_desc_t *)cur_rx_desc;
  389. while (cur_desc->basic.desc0 == EMAC_DESC_RX_OWN && cnt < DMA_RX_BUF_NUM) {
  390. cnt++;
  391. cur_desc = (dma_extended_desc_t *)cur_desc->basic.desc3;
  392. }
  393. return cnt;
  394. }
  395. #if CONFIG_EMAC_L2_TO_L3_RX_BUF_MODE
  396. static void emac_process_rx(void)
  397. {
  398. if (emac_config.emac_status == EMAC_RUNTIME_STOP) {
  399. return;
  400. }
  401. uint32_t cur_rx_desc = emac_read_rx_cur_reg();
  402. while (((uint32_t) & (emac_config.dma_erx[emac_config.dirty_rx])) != cur_rx_desc) {
  403. //copy data to lwip
  404. emac_config.emac_tcpip_input((void *)(emac_config.dma_erx[emac_config.dirty_rx].basic.desc2),
  405. (((emac_config.dma_erx[emac_config.dirty_rx].basic.desc0) >> EMAC_DESC_FRAME_LENGTH_S) &
  406. EMAC_DESC_FRAME_LENGTH), NULL);
  407. emac_clean_rx_desc(&(emac_config.dma_erx[emac_config.dirty_rx]), (emac_config.dma_erx[emac_config.dirty_rx].basic.desc2));
  408. emac_config.dirty_rx = (emac_config.dirty_rx + 1) % DMA_RX_BUF_NUM;
  409. cur_rx_desc = emac_read_rx_cur_reg();
  410. }
  411. emac_enable_rx_intr();
  412. }
  413. static void emac_process_rx_unavail(void)
  414. {
  415. if (emac_config.emac_status == EMAC_RUNTIME_STOP) {
  416. return;
  417. }
  418. uint32_t dirty_cnt = 0;
  419. while (dirty_cnt < DMA_RX_BUF_NUM) {
  420. if (emac_config.dma_erx[emac_config.dirty_rx].basic.desc0 & EMAC_DESC_RX_OWN) {
  421. break;
  422. }
  423. dirty_cnt++;
  424. //copy data to lwip
  425. emac_config.emac_tcpip_input((void *)(emac_config.dma_erx[emac_config.dirty_rx].basic.desc2),
  426. (((emac_config.dma_erx[emac_config.dirty_rx].basic.desc0) >> EMAC_DESC_FRAME_LENGTH_S) &
  427. EMAC_DESC_FRAME_LENGTH), NULL);
  428. emac_clean_rx_desc(&(emac_config.dma_erx[emac_config.dirty_rx]), (emac_config.dma_erx[emac_config.dirty_rx].basic.desc2));
  429. emac_config.dirty_rx = (emac_config.dirty_rx + 1) % DMA_RX_BUF_NUM;
  430. }
  431. emac_enable_rx_intr();
  432. emac_enable_rx_unavail_intr();
  433. emac_poll_rx_cmd();
  434. }
  435. #else
  436. static void emac_process_rx_unavail(void)
  437. {
  438. if (emac_config.emac_status == EMAC_RUNTIME_STOP) {
  439. return;
  440. }
  441. xSemaphoreTakeRecursive(emac_rx_xMutex, portMAX_DELAY);
  442. while (emac_config.cnt_rx < DMA_RX_BUF_NUM) {
  443. if (emac_config.dma_erx[emac_config.dirty_rx].basic.desc0 & EMAC_DESC_RX_OWN) {
  444. break;
  445. }
  446. emac_config.cnt_rx++;
  447. if (emac_config.cnt_rx > DMA_RX_BUF_NUM) {
  448. ESP_LOGE(TAG, "emac rx buf full");
  449. }
  450. uint32_t tmp_dirty = emac_config.dirty_rx;
  451. emac_config.dirty_rx = (emac_config.dirty_rx + 1) % DMA_RX_BUF_NUM;
  452. //copy data to lwip
  453. emac_config.emac_tcpip_input((void *)(emac_config.dma_erx[tmp_dirty].basic.desc2),
  454. (((emac_config.dma_erx[tmp_dirty].basic.desc0) >> EMAC_DESC_FRAME_LENGTH_S) &
  455. EMAC_DESC_FRAME_LENGTH), NULL);
  456. }
  457. emac_enable_rx_intr();
  458. emac_enable_rx_unavail_intr();
  459. xSemaphoreGiveRecursive(emac_rx_xMutex);
  460. }
  461. static void emac_process_rx(void)
  462. {
  463. if (emac_config.emac_status == EMAC_RUNTIME_STOP) {
  464. return;
  465. }
  466. uint32_t cur_rx_desc = emac_read_rx_cur_reg();
  467. xSemaphoreTakeRecursive(emac_rx_xMutex, portMAX_DELAY);
  468. if ((((uint32_t) & (emac_config.dma_erx[emac_config.dirty_rx])) != cur_rx_desc)) {
  469. while ((((uint32_t) & (emac_config.dma_erx[emac_config.dirty_rx])) != cur_rx_desc) &&
  470. emac_config.cnt_rx < DMA_RX_BUF_NUM) {
  471. emac_config.cnt_rx++;
  472. if (emac_config.cnt_rx > DMA_RX_BUF_NUM) {
  473. ESP_LOGE(TAG, "emac rx buf full");
  474. }
  475. uint32_t tmp_dirty = emac_config.dirty_rx;
  476. emac_config.dirty_rx = (emac_config.dirty_rx + 1) % DMA_RX_BUF_NUM;
  477. //copy data to lwip
  478. emac_config.emac_tcpip_input((void *)(emac_config.dma_erx[tmp_dirty].basic.desc2),
  479. (((emac_config.dma_erx[tmp_dirty].basic.desc0) >> EMAC_DESC_FRAME_LENGTH_S) &
  480. EMAC_DESC_FRAME_LENGTH), NULL);
  481. cur_rx_desc = emac_read_rx_cur_reg();
  482. }
  483. } else {
  484. if (emac_config.cnt_rx < DMA_RX_BUF_NUM) {
  485. if (!(emac_config.dma_erx[emac_config.dirty_rx].basic.desc0 & EMAC_DESC_RX_OWN)) {
  486. while (emac_config.cnt_rx < DMA_RX_BUF_NUM) {
  487. if (emac_config.dma_erx[emac_config.dirty_rx].basic.desc0 & EMAC_DESC_RX_OWN) {
  488. break;
  489. }
  490. emac_config.cnt_rx++;
  491. if (emac_config.cnt_rx > DMA_RX_BUF_NUM) {
  492. ESP_LOGE(TAG, "emac rx buf full");
  493. }
  494. uint32_t tmp_dirty = emac_config.dirty_rx;
  495. emac_config.dirty_rx = (emac_config.dirty_rx + 1) % DMA_RX_BUF_NUM;
  496. //copy data to lwip
  497. emac_config.emac_tcpip_input((void *)(emac_config.dma_erx[tmp_dirty].basic.desc2),
  498. (((emac_config.dma_erx[tmp_dirty].basic.desc0) >> EMAC_DESC_FRAME_LENGTH_S) &
  499. EMAC_DESC_FRAME_LENGTH), NULL);
  500. }
  501. }
  502. }
  503. }
  504. emac_enable_rx_intr();
  505. xSemaphoreGiveRecursive(emac_rx_xMutex);
  506. }
  507. #endif
  508. //TODO other events need to do something
  509. static void IRAM_ATTR emac_process_intr(void *arg)
  510. {
  511. uint32_t event;
  512. event = REG_READ(EMAC_DMASTATUS_REG);
  513. //clr intrs
  514. REG_WRITE(EMAC_DMASTATUS_REG, event);
  515. if (event & EMAC_RECV_INT) {
  516. emac_disable_rx_intr();
  517. if (emac_config.emac_flow_ctrl_partner_support) {
  518. if (emac_get_rxbuf_count_in_intr() < FLOW_CONTROL_HIGH_WATERMARK && !pause_send) {
  519. pause_send = true;
  520. emac_send_pause_frame_enable();
  521. }
  522. }
  523. emac_post(SIG_EMAC_RX_DONE, 0);
  524. }
  525. if (event & EMAC_RECV_BUF_UNAVAIL) {
  526. emac_disable_rx_unavail_intr();
  527. emac_post(SIG_EMAC_RX_UNAVAIL, 0);
  528. }
  529. if (event & EMAC_TRANS_INT) {
  530. emac_post(SIG_EMAC_TX_DONE, 0);
  531. }
  532. }
  533. static void emac_set_macaddr_reg(void)
  534. {
  535. REG_SET_FIELD(EMAC_ADDR0HIGH_REG, EMAC_ADDRESS0_HI, (emac_config.macaddr[5] << 8) | (emac_config.macaddr[4]));
  536. REG_WRITE(EMAC_ADDR0LOW_REG, (emac_config.macaddr[3] << 24) | (emac_config.macaddr[2] << 16) |
  537. (emac_config.macaddr[1] << 8) | (emac_config.macaddr[0]));
  538. }
  539. static void emac_check_phy_init(void)
  540. {
  541. emac_config.emac_phy_check_init();
  542. if (emac_config.emac_phy_get_duplex_mode() == ETH_MODE_FULLDUPLEX) {
  543. REG_SET_BIT(EMAC_GMACCONFIG_REG, EMAC_EMACDUPLEX);
  544. } else {
  545. REG_CLR_BIT(EMAC_GMACCONFIG_REG, EMAC_EMACDUPLEX);
  546. }
  547. if (emac_config.emac_phy_get_speed_mode() == ETH_SPEED_MODE_100M) {
  548. REG_SET_BIT(EMAC_GMACCONFIG_REG, EMAC_EMACFESPEED);
  549. } else {
  550. REG_CLR_BIT(EMAC_GMACCONFIG_REG, EMAC_EMACFESPEED);
  551. }
  552. #if CONFIG_EMAC_L2_TO_L3_RX_BUF_MODE
  553. emac_disable_flowctrl();
  554. emac_config.emac_flow_ctrl_partner_support = false;
  555. #else
  556. if (emac_config.emac_flow_ctrl_enable) {
  557. if (emac_config.emac_phy_get_partner_pause_enable() &&
  558. emac_config.emac_phy_get_duplex_mode() == ETH_MODE_FULLDUPLEX) {
  559. emac_enable_flowctrl();
  560. emac_config.emac_flow_ctrl_partner_support = true;
  561. } else {
  562. emac_disable_flowctrl();
  563. emac_config.emac_flow_ctrl_partner_support = false;
  564. }
  565. } else {
  566. emac_disable_flowctrl();
  567. emac_config.emac_flow_ctrl_partner_support = false;
  568. }
  569. #endif
  570. emac_mac_enable_txrx();
  571. }
  572. static void emac_process_link_updown(bool link_status)
  573. {
  574. system_event_t evt;
  575. uint8_t i = 0;
  576. emac_config.phy_link_up = link_status;
  577. if (link_status) {
  578. emac_check_phy_init();
  579. ESP_LOGD(TAG, "eth link_up");
  580. emac_enable_dma_tx();
  581. emac_enable_dma_rx();
  582. for (i = 0; i < PHY_LINK_CHECK_NUM; i++) {
  583. emac_check_phy_init();
  584. }
  585. evt.event_id = SYSTEM_EVENT_ETH_CONNECTED;
  586. } else {
  587. ESP_LOGD(TAG, "eth link_down");
  588. emac_disable_dma_tx();
  589. emac_disable_dma_rx();
  590. evt.event_id = SYSTEM_EVENT_ETH_DISCONNECTED;
  591. }
  592. esp_event_send(&evt);
  593. }
  594. static void emac_hw_init(void)
  595. {
  596. //init chain
  597. emac_init_dma_chain();
  598. //get hw features TODO
  599. //ipc TODO
  600. }
  601. esp_err_t esp_eth_tx(uint8_t *buf, uint16_t size)
  602. {
  603. esp_err_t ret = ESP_OK;
  604. if (emac_config.emac_status != EMAC_RUNTIME_START) {
  605. ESP_LOGE(TAG, "tx netif is not ready, emac_status=%d", emac_config.emac_status);
  606. ret = ESP_ERR_INVALID_STATE;
  607. return ret;
  608. }
  609. xSemaphoreTakeRecursive(emac_tx_xMutex, portMAX_DELAY);
  610. if (emac_config.cnt_tx == DMA_TX_BUF_NUM - 1) {
  611. ESP_LOGD(TAG, "tx buf full");
  612. ret = ESP_ERR_NO_MEM;
  613. goto _exit;
  614. }
  615. memcpy((void *)(emac_config.dma_etx[emac_config.cur_tx].basic.desc2), buf, size);
  616. emac_setup_tx_desc(&(emac_config.dma_etx[emac_config.cur_tx]), size);
  617. emac_config.cnt_tx++;
  618. emac_config.cur_tx = (emac_config.cur_tx + 1) % DMA_TX_BUF_NUM;
  619. emac_poll_tx_cmd();
  620. _exit:
  621. xSemaphoreGiveRecursive(emac_tx_xMutex);
  622. return ret;
  623. }
  624. static void emac_init_default_data(void)
  625. {
  626. memset((void *)&emac_config, 0, sizeof(struct emac_config_data));
  627. }
  628. void emac_process_link_check(void)
  629. {
  630. if (emac_config.emac_status != EMAC_RUNTIME_START) {
  631. return;
  632. }
  633. if (emac_config.emac_phy_check_link()) {
  634. if (!emac_config.phy_link_up) {
  635. emac_process_link_updown(true);
  636. }
  637. } else {
  638. if (emac_config.phy_link_up) {
  639. emac_process_link_updown(false);
  640. }
  641. }
  642. }
  643. void emac_link_check_func(void *pv_parameters)
  644. {
  645. emac_post(SIG_EMAC_CHECK_LINK, 0);
  646. }
  647. static bool emac_link_check_timer_init(void)
  648. {
  649. emac_timer = xTimerCreate("emac_timer",
  650. (CONFIG_EMAC_CHECK_LINK_PERIOD_MS / portTICK_PERIOD_MS),
  651. pdTRUE,
  652. NULL,
  653. emac_link_check_func);
  654. if (emac_timer == NULL) {
  655. return false;
  656. } else {
  657. return true;
  658. }
  659. }
  660. static bool emac_link_check_timer_start(void)
  661. {
  662. if (xTimerStart(emac_timer, portMAX_DELAY) != pdPASS) {
  663. return false;
  664. } else {
  665. return true;
  666. }
  667. }
  668. static bool emac_link_check_timer_stop(void)
  669. {
  670. if (xTimerStop(emac_timer, portMAX_DELAY) != pdPASS) {
  671. return false;
  672. } else {
  673. return true;
  674. }
  675. }
  676. static bool emac_link_check_timer_delete(void)
  677. {
  678. xTimerDelete(emac_timer, portMAX_DELAY);
  679. emac_timer = NULL;
  680. return true;
  681. }
  682. static void emac_start(void *param)
  683. {
  684. struct emac_post_cmd *post_cmd = (struct emac_post_cmd *)param;
  685. struct emac_open_cmd *cmd = (struct emac_open_cmd *)(post_cmd->cmd);
  686. ESP_LOGD(TAG, "emac start");
  687. cmd->err = EMAC_CMD_OK;
  688. if (emac_reset() != ESP_OK) {
  689. return;
  690. }
  691. emac_reset_dma_chain();
  692. emac_dma_init();
  693. emac_set_macaddr_reg();
  694. emac_set_tx_base_reg();
  695. emac_set_rx_base_reg();
  696. emac_mac_init();
  697. /* check if enable promiscuous mode */
  698. if (emac_config.promiscuous_enable) {
  699. emac_enable_promiscuous();
  700. } else {
  701. emac_disable_promiscuous();
  702. }
  703. emac_enable_intr();
  704. emac_config.emac_status = EMAC_RUNTIME_START;
  705. system_event_t evt;
  706. evt.event_id = SYSTEM_EVENT_ETH_START;
  707. esp_event_send(&evt);
  708. //set a timer to check link up status
  709. if (emac_link_check_timer_init()) {
  710. if (!emac_link_check_timer_start()) {
  711. cmd->err = EMAC_CMD_FAIL;
  712. emac_link_check_timer_delete();
  713. }
  714. } else {
  715. cmd->err = EMAC_CMD_FAIL;
  716. }
  717. if (post_cmd->post_type == EMAC_POST_SYNC) {
  718. xSemaphoreGive(emac_g_sem);
  719. }
  720. ESP_LOGD(TAG, "emac start success");
  721. }
  722. esp_err_t esp_eth_enable(void)
  723. {
  724. struct emac_post_cmd post_cmd;
  725. struct emac_open_cmd open_cmd;
  726. post_cmd.cmd = (void *)(&open_cmd);
  727. open_cmd.err = EMAC_CMD_OK;
  728. if (emac_config.emac_status == EMAC_RUNTIME_START) {
  729. open_cmd.err = EMAC_CMD_OK;
  730. return open_cmd.err;
  731. }
  732. #ifdef CONFIG_PM_ENABLE
  733. esp_err_t err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "ethernet", &s_pm_lock);
  734. if (err != ESP_OK) {
  735. return err;
  736. }
  737. esp_pm_lock_acquire(s_pm_lock);
  738. #endif //CONFIG_PM_ENABLE
  739. emac_enable_clk(true);
  740. /* init phy device */
  741. if (emac_config.phy_init() != ESP_OK) {
  742. ESP_LOGE(TAG, "Initialise PHY device Timeout");
  743. return ESP_FAIL;
  744. }
  745. if (emac_config.emac_status != EMAC_RUNTIME_NOT_INIT) {
  746. if (emac_ioctl(SIG_EMAC_START, (emac_par_t)(&post_cmd))) {
  747. open_cmd.err = EMAC_CMD_FAIL;
  748. goto cleanup;
  749. }
  750. } else {
  751. open_cmd.err = EMAC_CMD_FAIL;
  752. goto cleanup;
  753. }
  754. return EMAC_CMD_OK;
  755. cleanup:
  756. #ifdef CONFIG_PM_ENABLE
  757. esp_pm_lock_release(s_pm_lock);
  758. esp_pm_lock_delete(s_pm_lock);
  759. s_pm_lock = NULL;
  760. #endif //CONFIG_PM_ENABLE
  761. return open_cmd.err;
  762. }
  763. static void emac_stop(void *param)
  764. {
  765. struct emac_post_cmd *post_cmd = (struct emac_post_cmd *)param;
  766. ESP_LOGD(TAG, "emac stop");
  767. emac_link_check_timer_stop();
  768. emac_link_check_timer_delete();
  769. emac_process_link_updown(false);
  770. emac_disable_intr();
  771. emac_enable_clk(false);
  772. emac_config.emac_status = EMAC_RUNTIME_STOP;
  773. system_event_t evt;
  774. evt.event_id = SYSTEM_EVENT_ETH_STOP;
  775. esp_event_send(&evt);
  776. if (post_cmd->post_type == EMAC_POST_SYNC) {
  777. xSemaphoreGive(emac_g_sem);
  778. }
  779. ESP_LOGD(TAG, "emac stop success");
  780. }
  781. esp_err_t esp_eth_disable(void)
  782. {
  783. struct emac_post_cmd post_cmd;
  784. struct emac_close_cmd close_cmd;
  785. post_cmd.cmd = (void *)(&close_cmd);
  786. close_cmd.err = EMAC_CMD_OK;
  787. if (emac_config.emac_status == EMAC_RUNTIME_STOP) {
  788. close_cmd.err = EMAC_CMD_OK;
  789. return close_cmd.err;
  790. }
  791. #ifdef CONFIG_PM_ENABLE
  792. esp_pm_lock_release(s_pm_lock);
  793. esp_pm_lock_delete(s_pm_lock);
  794. s_pm_lock = NULL;
  795. #endif // CONFIG_PM_ENABLE
  796. if (emac_config.emac_status == EMAC_RUNTIME_START) {
  797. if (emac_ioctl(SIG_EMAC_STOP, (emac_par_t)(&post_cmd)) != 0) {
  798. close_cmd.err = EMAC_CMD_FAIL;
  799. }
  800. } else {
  801. close_cmd.err = EMAC_CMD_FAIL;
  802. }
  803. return close_cmd.err;
  804. }
  805. static esp_err_t emac_ioctl(emac_sig_t sig, emac_par_t par)
  806. {
  807. esp_err_t ret = ESP_OK;
  808. struct emac_post_cmd *post_cmd = (struct emac_post_cmd *)par;
  809. xTaskHandle task_hdl = xTaskGetCurrentTaskHandle();
  810. if (emac_task_hdl != task_hdl) {
  811. post_cmd->post_type = EMAC_POST_SYNC;
  812. if (emac_post(sig, par) != ESP_OK) {
  813. ret = ESP_FAIL;
  814. return ret;
  815. };
  816. if (xSemaphoreTake(emac_g_sem, portMAX_DELAY) == pdTRUE) {
  817. return ret;
  818. }
  819. } else {
  820. post_cmd->post_type = EMAC_POST_ASYNC;
  821. switch (sig) {
  822. case SIG_EMAC_RX_DONE:
  823. emac_process_rx();
  824. break;
  825. case SIG_EMAC_TX_DONE:
  826. emac_process_tx();
  827. break;
  828. case SIG_EMAC_START:
  829. emac_start((void *)par);
  830. break;
  831. case SIG_EMAC_STOP:
  832. emac_stop((void *)par);
  833. break;
  834. default:
  835. ESP_LOGE(TAG, "unexpect sig %d", sig);
  836. break;
  837. }
  838. }
  839. return ret;
  840. }
  841. void emac_task(void *pv)
  842. {
  843. emac_event_t e;
  844. for (;;) {
  845. if (xQueueReceive(emac_xqueue, &e, portMAX_DELAY) == pdTRUE) {
  846. portENTER_CRITICAL(&g_emac_mux);
  847. emac_sig_cnt[e.sig]--;
  848. portEXIT_CRITICAL(&g_emac_mux);
  849. switch (e.sig) {
  850. case SIG_EMAC_RX_DONE:
  851. emac_process_rx();
  852. break;
  853. case SIG_EMAC_RX_UNAVAIL:
  854. emac_process_rx_unavail();
  855. break;
  856. case SIG_EMAC_TX_DONE:
  857. emac_process_tx();
  858. break;
  859. case SIG_EMAC_START:
  860. emac_start((void *)e.par);
  861. break;
  862. case SIG_EMAC_STOP:
  863. emac_stop((void *)e.par);
  864. break;
  865. case SIG_EMAC_CHECK_LINK:
  866. emac_process_link_check();
  867. break;
  868. default:
  869. ESP_LOGE(TAG, "unexpect sig %d", e.sig);
  870. break;
  871. }
  872. }
  873. }
  874. }
  875. esp_err_t IRAM_ATTR emac_post(emac_sig_t sig, emac_par_t par)
  876. {
  877. if (sig <= SIG_EMAC_RX_DONE) {
  878. if (emac_sig_cnt[sig]) {
  879. return ESP_OK;
  880. } else {
  881. emac_sig_cnt[sig]++;
  882. emac_event_t evt;
  883. signed portBASE_TYPE ret;
  884. evt.sig = sig;
  885. evt.par = par;
  886. portBASE_TYPE tmp;
  887. ret = xQueueSendFromISR(emac_xqueue, &evt, &tmp);
  888. if (tmp != pdFALSE) {
  889. portYIELD_FROM_ISR();
  890. }
  891. if (ret != pdPASS) {
  892. return ESP_FAIL;
  893. }
  894. }
  895. } else {
  896. portENTER_CRITICAL(&g_emac_mux);
  897. emac_sig_cnt[sig]++;
  898. portEXIT_CRITICAL(&g_emac_mux);
  899. emac_event_t evt;
  900. evt.sig = sig;
  901. evt.par = par;
  902. if (xQueueSend(emac_xqueue, &evt, 10 / portTICK_PERIOD_MS) != pdTRUE) {
  903. return ESP_FAIL;
  904. }
  905. }
  906. return ESP_OK;
  907. }
  908. esp_err_t esp_eth_init(eth_config_t *config)
  909. {
  910. esp_event_set_default_eth_handlers();
  911. return esp_eth_init_internal(config);
  912. }
  913. esp_err_t esp_eth_init_internal(eth_config_t *config)
  914. {
  915. int i = 0;
  916. esp_err_t ret = ESP_OK;
  917. if (emac_config.emac_status != EMAC_RUNTIME_NOT_INIT) {
  918. goto _initialised;
  919. }
  920. /* dynamically alloc memory for ethernet dma */
  921. emac_dma_rx_chain_buf = (dma_extended_desc_t *)heap_caps_malloc(sizeof(dma_extended_desc_t) * DMA_RX_BUF_NUM, MALLOC_CAP_DMA);
  922. emac_dma_tx_chain_buf = (dma_extended_desc_t *)heap_caps_malloc(sizeof(dma_extended_desc_t) * DMA_TX_BUF_NUM, MALLOC_CAP_DMA);
  923. for (i = 0; i < DMA_RX_BUF_NUM; i++) {
  924. emac_dma_rx_buf[i] = (uint8_t *)heap_caps_malloc(DMA_RX_BUF_SIZE, MALLOC_CAP_DMA);
  925. }
  926. for (i = 0; i < DMA_TX_BUF_NUM; i++) {
  927. emac_dma_tx_buf[i] = (uint8_t *)heap_caps_malloc(DMA_TX_BUF_SIZE, MALLOC_CAP_DMA);
  928. }
  929. emac_init_default_data();
  930. if (config) {
  931. emac_set_user_config_data(config);
  932. }
  933. ret = emac_verify_args();
  934. if (ret != ESP_OK) {
  935. goto _verify_err;
  936. }
  937. //before set emac reg must enable clk
  938. periph_module_enable(PERIPH_EMAC_MODULE);
  939. if (emac_config.clock_mode != ETH_CLOCK_GPIO0_IN) {
  940. #if CONFIG_SPIRAM_SUPPORT
  941. // make sure Ethernet won't have conflict with PSRAM
  942. if (emac_config.clock_mode >= ETH_CLOCK_GPIO16_OUT) {
  943. if (esp_spiram_is_initialized()) {
  944. ESP_LOGE(TAG, "GPIO16 and GPIO17 are occupied by PSRAM, please switch to ETH_CLOCK_GPIO_IN or ETH_CLOCK_GPIO_OUT mode");
  945. ret = ESP_FAIL;
  946. goto _verify_err;
  947. } else {
  948. ESP_LOGW(TAG, "Using GPIO16/17 to output Ethernet RMII clock, make sure you don't have PSRAM on board");
  949. }
  950. }
  951. #endif
  952. // 50 MHz = 40MHz * (6 + 4) / (2 * (2 + 2) = 400MHz / 8
  953. rtc_clk_apll_enable(1, 0, 0, 6, 2);
  954. REG_SET_FIELD(EMAC_EX_CLKOUT_CONF_REG, EMAC_EX_CLK_OUT_H_DIV_NUM, 0);
  955. REG_SET_FIELD(EMAC_EX_CLKOUT_CONF_REG, EMAC_EX_CLK_OUT_DIV_NUM, 0);
  956. if (emac_config.clock_mode == ETH_CLOCK_GPIO0_OUT) {
  957. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
  958. REG_WRITE(PIN_CTRL, 6);
  959. ESP_LOGD(TAG, "EMAC 50MHz clock output on GPIO0");
  960. } else if (emac_config.clock_mode == ETH_CLOCK_GPIO16_OUT) {
  961. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO16_U, FUNC_GPIO16_EMAC_CLK_OUT);
  962. ESP_LOGD(TAG, "EMAC 50MHz clock output on GPIO16");
  963. } else if (emac_config.clock_mode == ETH_CLOCK_GPIO17_OUT) {
  964. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO17_U, FUNC_GPIO17_EMAC_CLK_OUT_180);
  965. ESP_LOGD(TAG, "EMAC 50MHz inverted clock output on GPIO17");
  966. }
  967. }
  968. if (emac_config.clock_mode == ETH_CLOCK_GPIO0_IN) {
  969. // external clock on GPIO0
  970. REG_SET_BIT(EMAC_EX_CLK_CTRL_REG, EMAC_EX_EXT_OSC_EN);
  971. REG_CLR_BIT(EMAC_EX_CLK_CTRL_REG, EMAC_EX_INT_OSC_EN);
  972. REG_SET_BIT(EMAC_EX_OSCCLK_CONF_REG, EMAC_EX_OSC_CLK_SEL);
  973. ESP_LOGD(TAG, "External clock input 50MHz on GPIO0");
  974. if (emac_config.mac_mode == ETH_MODE_MII) {
  975. REG_SET_BIT(EMAC_EX_CLK_CTRL_REG, EMAC_EX_MII_CLK_RX_EN);
  976. REG_SET_BIT(EMAC_EX_CLK_CTRL_REG, EMAC_EX_MII_CLK_TX_EN);
  977. }
  978. } else {
  979. // internal clock by APLL
  980. REG_CLR_BIT(EMAC_EX_CLK_CTRL_REG, EMAC_EX_EXT_OSC_EN);
  981. REG_SET_BIT(EMAC_EX_CLK_CTRL_REG, EMAC_EX_INT_OSC_EN);
  982. REG_CLR_BIT(EMAC_EX_OSCCLK_CONF_REG, EMAC_EX_OSC_CLK_SEL);
  983. }
  984. emac_config.emac_phy_power_enable(true);
  985. emac_enable_clk(true);
  986. REG_SET_FIELD(EMAC_EX_PHYINF_CONF_REG, EMAC_EX_PHY_INTF_SEL, EMAC_EX_PHY_INTF_RMII);
  987. emac_dma_init();
  988. emac_config.emac_gpio_config();
  989. emac_hw_init();
  990. emac_macaddr_init();
  991. //watchdog TODO
  992. //init task for emac
  993. emac_g_sem = xSemaphoreCreateBinary();
  994. emac_rx_xMutex = xSemaphoreCreateRecursiveMutex();
  995. emac_tx_xMutex = xSemaphoreCreateRecursiveMutex();
  996. emac_xqueue = xQueueCreate(EMAC_EVT_QNUM, sizeof(emac_event_t));
  997. xTaskCreate(emac_task,
  998. "emacT",
  999. EMAC_TASK_STACK_SIZE,
  1000. NULL,
  1001. EMAC_TASK_PRIORITY,
  1002. &emac_task_hdl);
  1003. esp_intr_alloc(ETS_ETH_MAC_INTR_SOURCE, 0, emac_process_intr, NULL, &eth_intr_handle);
  1004. emac_config.emac_status = EMAC_RUNTIME_INIT;
  1005. return ESP_OK;
  1006. _verify_err:
  1007. free(emac_dma_rx_chain_buf);
  1008. free(emac_dma_tx_chain_buf);
  1009. emac_dma_rx_chain_buf = NULL;
  1010. emac_dma_tx_chain_buf = NULL;
  1011. for (i = 0; i < DMA_RX_BUF_NUM; i++) {
  1012. free(emac_dma_rx_buf[i]);
  1013. emac_dma_rx_buf[i] = NULL;
  1014. }
  1015. for (i = 0; i < DMA_TX_BUF_NUM; i++) {
  1016. free(emac_dma_tx_buf[i]);
  1017. emac_dma_tx_buf[i] = NULL;
  1018. }
  1019. _initialised:
  1020. return ret;
  1021. }
  1022. esp_err_t esp_eth_deinit(void)
  1023. {
  1024. esp_err_t ret = ESP_OK;
  1025. int i = 0;
  1026. if (emac_config.emac_status == EMAC_RUNTIME_NOT_INIT) {
  1027. goto _exit;
  1028. }
  1029. if (emac_config.emac_status == EMAC_RUNTIME_START) {
  1030. esp_eth_disable();
  1031. }
  1032. if (!emac_task_hdl) {
  1033. ret = ESP_ERR_INVALID_STATE;
  1034. goto _exit;
  1035. }
  1036. vTaskDelete(emac_task_hdl);
  1037. emac_task_hdl = NULL;
  1038. vQueueDelete(emac_xqueue);
  1039. vSemaphoreDelete(emac_tx_xMutex);
  1040. vSemaphoreDelete(emac_rx_xMutex);
  1041. vSemaphoreDelete(emac_g_sem);
  1042. emac_reset_dma_chain();
  1043. emac_config.emac_phy_power_enable(false);
  1044. periph_module_disable(PERIPH_EMAC_MODULE);
  1045. emac_config.emac_status = EMAC_RUNTIME_NOT_INIT;
  1046. /* free memory that dynamically allocted */
  1047. free(emac_dma_rx_chain_buf);
  1048. free(emac_dma_tx_chain_buf);
  1049. emac_dma_rx_chain_buf = NULL;
  1050. emac_dma_tx_chain_buf = NULL;
  1051. for (i = 0; i < DMA_RX_BUF_NUM; i++) {
  1052. free(emac_dma_rx_buf[i]);
  1053. emac_dma_rx_buf[i] = NULL;
  1054. }
  1055. for (i = 0; i < DMA_TX_BUF_NUM; i++) {
  1056. free(emac_dma_tx_buf[i]);
  1057. emac_dma_tx_buf[i] = NULL;
  1058. }
  1059. esp_intr_free(eth_intr_handle);
  1060. _exit:
  1061. return ret;
  1062. }