ets_sys.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. // Copyright 2010-2020 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "soc/soc.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /** \defgroup ets_sys_apis, ets system related apis
  22. * @brief ets system apis
  23. */
  24. /** @addtogroup ets_sys_apis
  25. * @{
  26. */
  27. /************************************************************************
  28. * NOTE
  29. * Many functions in this header files can't be run in FreeRTOS.
  30. * Please see the comment of the Functions.
  31. * There are also some functions that doesn't work on FreeRTOS
  32. * without listed in the header, such as:
  33. * xtos functions start with "_xtos_" in ld file.
  34. *
  35. ***********************************************************************
  36. */
  37. /** \defgroup ets_apis, Espressif Task Scheduler related apis
  38. * @brief ets apis
  39. */
  40. /** @addtogroup ets_apis
  41. * @{
  42. */
  43. typedef enum {
  44. ETS_OK = 0, /**< return successful in ets*/
  45. ETS_FAILED = 1 /**< return failed in ets*/
  46. } ETS_STATUS;
  47. typedef ETS_STATUS ets_status_t;
  48. typedef uint32_t ETSSignal;
  49. typedef uint32_t ETSParam;
  50. typedef struct ETSEventTag ETSEvent; /**< Event transmit/receive in ets*/
  51. struct ETSEventTag {
  52. ETSSignal sig; /**< Event signal, in same task, different Event with different signal*/
  53. ETSParam par; /**< Event parameter, sometimes without usage, then will be set as 0*/
  54. };
  55. typedef void (*ETSTask)(ETSEvent *e); /**< Type of the Task processer*/
  56. typedef void (* ets_idle_cb_t)(void *arg); /**< Type of the system idle callback*/
  57. /**
  58. * @brief Start the Espressif Task Scheduler, which is an infinit loop. Please do not add code after it.
  59. *
  60. * @param none
  61. *
  62. * @return none
  63. */
  64. void ets_run(void);
  65. /**
  66. * @brief Set the Idle callback, when Tasks are processed, will call the callback before CPU goto sleep.
  67. *
  68. * @param ets_idle_cb_t func : The callback function.
  69. *
  70. * @param void *arg : Argument of the callback.
  71. *
  72. * @return None
  73. */
  74. void ets_set_idle_cb(ets_idle_cb_t func, void *arg);
  75. /**
  76. * @brief Init a task with processer, priority, queue to receive Event, queue length.
  77. *
  78. * @param ETSTask task : The task processer.
  79. *
  80. * @param uint8_t prio : Task priority, 0-31, bigger num with high priority, one priority with one task.
  81. *
  82. * @param ETSEvent *queue : Queue belongs to the task, task always receives Events, Queue is circular used.
  83. *
  84. * @param uint8_t qlen : Queue length.
  85. *
  86. * @return None
  87. */
  88. void ets_task(ETSTask task, uint8_t prio, ETSEvent *queue, uint8_t qlen);
  89. /**
  90. * @brief Post an event to an Task.
  91. *
  92. * @param uint8_t prio : Priority of the Task.
  93. *
  94. * @param ETSSignal sig : Event signal.
  95. *
  96. * @param ETSParam par : Event parameter
  97. *
  98. * @return ETS_OK : post successful
  99. * @return ETS_FAILED : post failed
  100. */
  101. ETS_STATUS ets_post(uint8_t prio, ETSSignal sig, ETSParam par);
  102. /**
  103. * @}
  104. */
  105. /** \defgroup ets_boot_apis, Boot routing related apis
  106. * @brief ets boot apis
  107. */
  108. /** @addtogroup ets_apis
  109. * @{
  110. */
  111. extern const char *const exc_cause_table[40]; ///**< excption cause that defined by the core.*/
  112. /**
  113. * @brief Set Pro cpu Entry code, code can be called in PRO CPU when booting is not completed.
  114. * When Pro CPU booting is completed, Pro CPU will call the Entry code if not NULL.
  115. *
  116. * @param uint32_t start : the PRO Entry code address value in uint32_t
  117. *
  118. * @return None
  119. */
  120. void ets_set_user_start(uint32_t start);
  121. /**
  122. * @brief Set Pro cpu Startup code, code can be called when booting is not completed, or in Entry code.
  123. * When Entry code completed, CPU will call the Startup code if not NULL, else call ets_run.
  124. *
  125. * @param uint32_t callback : the Startup code address value in uint32_t
  126. *
  127. * @return None : post successful
  128. */
  129. void ets_set_startup_callback(uint32_t callback);
  130. /**
  131. * @brief Set App cpu Entry code, code can be called in PRO CPU.
  132. * When APP booting is completed, APP CPU will call the Entry code if not NULL.
  133. *
  134. * @param uint32_t start : the APP Entry code address value in uint32_t, stored in register APPCPU_CTRL_REG_D.
  135. *
  136. * @return None
  137. */
  138. void ets_set_appcpu_boot_addr(uint32_t start);
  139. /**
  140. * @}
  141. */
  142. /** \defgroup ets_printf_apis, ets_printf related apis used in ets
  143. * @brief ets printf apis
  144. */
  145. /** @addtogroup ets_printf_apis
  146. * @{
  147. */
  148. /**
  149. * @brief Printf the strings to uart or other devices, similar with printf, simple than printf.
  150. * Can not print float point data format, or longlong data format.
  151. * So we maybe only use this in ROM.
  152. *
  153. * @param const char *fmt : See printf.
  154. *
  155. * @param ... : See printf.
  156. *
  157. * @return int : the length printed to the output device.
  158. */
  159. int ets_printf(const char *fmt, ...);
  160. /**
  161. * @brief Set the uart channel of ets_printf(uart_tx_one_char).
  162. * ROM will set it base on the efuse and gpio setting, however, this can be changed after booting.
  163. *
  164. * @param uart_no : 0 for UART0, 1 for UART1, 2 for UART2.
  165. *
  166. * @return None
  167. */
  168. void ets_set_printf_channel(uint8_t uart_no);
  169. /**
  170. * @brief Get the uart channel of ets_printf(uart_tx_one_char).
  171. *
  172. * @return uint8_t uart channel used by ets_printf(uart_tx_one_char).
  173. */
  174. uint8_t ets_get_printf_channel(void);
  175. /**
  176. * @brief Output a char to uart, which uart to output(which is in uart module in ROM) is not in scope of the function.
  177. * Can not print float point data format, or longlong data format
  178. *
  179. * @param char c : char to output.
  180. *
  181. * @return None
  182. */
  183. void ets_write_char_uart(char c);
  184. /**
  185. * @brief Ets_printf have two output functions: putc1 and putc2, both of which will be called if need ouput.
  186. * To install putc1, which is defaulted installed as ets_write_char_uart in none silent boot mode, as NULL in silent mode.
  187. *
  188. * @param void (*)(char) p: Output function to install.
  189. *
  190. * @return None
  191. */
  192. void ets_install_putc1(void (*p)(char c));
  193. /**
  194. * @brief Ets_printf have two output functions: putc1 and putc2, both of which will be called if need ouput.
  195. * To install putc2, which is defaulted installed as NULL.
  196. *
  197. * @param void (*)(char) p: Output function to install.
  198. *
  199. * @return None
  200. */
  201. void ets_install_putc2(void (*p)(char c));
  202. /**
  203. * @brief Install putc1 as ets_write_char_uart.
  204. * In silent boot mode(to void interfere the UART attached MCU), we can call this function, after booting ok.
  205. *
  206. * @param None
  207. *
  208. * @return None
  209. */
  210. void ets_install_uart_printf(void);
  211. #define ETS_PRINTF(...) ets_printf(...)
  212. #define ETS_ASSERT(v) do { \
  213. if (!(v)) { \
  214. ets_printf("%s %u \n", __FILE__, __LINE__); \
  215. while (1) {}; \
  216. } \
  217. } while (0);
  218. /**
  219. * @}
  220. */
  221. /** \defgroup ets_timer_apis, ets_timer related apis used in ets
  222. * @brief ets timer apis
  223. */
  224. /** @addtogroup ets_timer_apis
  225. * @{
  226. */
  227. typedef void ETSTimerFunc(void *timer_arg);/**< timer handler*/
  228. typedef struct _ETSTIMER_ {
  229. struct _ETSTIMER_ *timer_next; /**< timer linker*/
  230. uint32_t timer_expire; /**< abstruct time when timer expire*/
  231. uint32_t timer_period; /**< timer period, 0 means timer is not periodic repeated*/
  232. ETSTimerFunc *timer_func; /**< timer handler*/
  233. void *timer_arg; /**< timer handler argument*/
  234. } ETSTimer;
  235. /**
  236. * @brief Init ets timer, this timer range is 640 us to 429496 ms
  237. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  238. *
  239. * @param None
  240. *
  241. * @return None
  242. */
  243. void ets_timer_init(void);
  244. /**
  245. * @brief In FreeRTOS, please call FreeRTOS apis, never call this api.
  246. *
  247. * @param None
  248. *
  249. * @return None
  250. */
  251. void ets_timer_deinit(void);
  252. /**
  253. * @brief Arm an ets timer, this timer range is 640 us to 429496 ms.
  254. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  255. *
  256. * @param ETSTimer *timer : Timer struct pointer.
  257. *
  258. * @param uint32_t tmout : Timer value in ms, range is 1 to 429496.
  259. *
  260. * @param bool repeat : Timer is periodic repeated.
  261. *
  262. * @return None
  263. */
  264. void ets_timer_arm(ETSTimer *timer, uint32_t tmout, bool repeat);
  265. /**
  266. * @brief Arm an ets timer, this timer range is 640 us to 429496 ms.
  267. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  268. *
  269. * @param ETSTimer *timer : Timer struct pointer.
  270. *
  271. * @param uint32_t tmout : Timer value in us, range is 1 to 429496729.
  272. *
  273. * @param bool repeat : Timer is periodic repeated.
  274. *
  275. * @return None
  276. */
  277. void ets_timer_arm_us(ETSTimer *ptimer, uint32_t us, bool repeat);
  278. /**
  279. * @brief Disarm an ets timer.
  280. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  281. *
  282. * @param ETSTimer *timer : Timer struct pointer.
  283. *
  284. * @return None
  285. */
  286. void ets_timer_disarm(ETSTimer *timer);
  287. /**
  288. * @brief Set timer callback and argument.
  289. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  290. *
  291. * @param ETSTimer *timer : Timer struct pointer.
  292. *
  293. * @param ETSTimerFunc *pfunction : Timer callback.
  294. *
  295. * @param void *parg : Timer callback argument.
  296. *
  297. * @return None
  298. */
  299. void ets_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg);
  300. /**
  301. * @brief Unset timer callback and argument to NULL.
  302. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  303. *
  304. * @param ETSTimer *timer : Timer struct pointer.
  305. *
  306. * @return None
  307. */
  308. void ets_timer_done(ETSTimer *ptimer);
  309. /**
  310. * @brief CPU do while loop for some time.
  311. * In FreeRTOS task, please call FreeRTOS apis.
  312. *
  313. * @param uint32_t us : Delay time in us.
  314. *
  315. * @return None
  316. */
  317. void ets_delay_us(uint32_t us);
  318. /**
  319. * @brief Set the real CPU ticks per us to the ets, so that ets_delay_us will be accurate.
  320. * Call this function when CPU frequency is changed.
  321. *
  322. * @param uint32_t ticks_per_us : CPU ticks per us.
  323. *
  324. * @return None
  325. */
  326. void ets_update_cpu_frequency(uint32_t ticks_per_us);
  327. /**
  328. * @brief Set the real CPU ticks per us to the ets, so that ets_delay_us will be accurate.
  329. *
  330. * @note This function only sets the tick rate for the current CPU. It is located in ROM,
  331. * so the deep sleep stub can use it even if IRAM is not initialized yet.
  332. *
  333. * @param uint32_t ticks_per_us : CPU ticks per us.
  334. *
  335. * @return None
  336. */
  337. void ets_update_cpu_frequency_rom(uint32_t ticks_per_us);
  338. /**
  339. * @brief Get the real CPU ticks per us to the ets.
  340. * This function do not return real CPU ticks per us, just the record in ets. It can be used to check with the real CPU frequency.
  341. *
  342. * @param None
  343. *
  344. * @return uint32_t : CPU ticks per us record in ets.
  345. */
  346. uint32_t ets_get_cpu_frequency(void);
  347. /**
  348. * @brief Get xtal_freq value, If value not stored in RTC_STORE5, than store.
  349. *
  350. * @param None
  351. *
  352. * @return uint32_t : if stored in efuse(not 0)
  353. * clock = ets_efuse_get_xtal_freq() * 1000000;
  354. * else if analog_8M in efuse
  355. * clock = ets_get_xtal_scale() * 625 / 16 * ets_efuse_get_8M_clock();
  356. * else clock = 40M.
  357. */
  358. uint32_t ets_get_xtal_freq(void);
  359. /**
  360. * @brief Get the apb divisor. The xtal frequency gets divided
  361. * by this value to generate the APB clock.
  362. * When any types of reset happens, the default value is 2.
  363. *
  364. * @param None
  365. *
  366. * @return uint32_t : 1 or 2.
  367. */
  368. uint32_t ets_get_xtal_div(void);
  369. /**
  370. * @brief Modifies the apb divisor. The xtal frequency gets divided by this to
  371. * generate the APB clock.
  372. *
  373. * @note The xtal frequency divisor is 2 by default as the glitch detector
  374. * doesn't properly stop glitches when it is 1. Please do not set the
  375. * divisor to 1 before the PLL is active without being aware that you
  376. * may be introducing a security risk.
  377. *
  378. * @param div Divisor. 1 = xtal freq, 2 = 1/2th xtal freq.
  379. */
  380. void ets_set_xtal_div(int div);
  381. /**
  382. * @brief Get apb_freq value, If value not stored in RTC_STORE5, than store.
  383. *
  384. * @param None
  385. *
  386. * @return uint32_t : if rtc store the value (RTC_STORE5 high 16 bits and low 16 bits with same value), read from rtc register.
  387. * clock = (REG_READ(RTC_STORE5) & 0xffff) << 12;
  388. * else store ets_get_detected_xtal_freq() in.
  389. */
  390. uint32_t ets_get_apb_freq(void);
  391. /**
  392. * @}
  393. */
  394. /** \defgroup ets_intr_apis, ets interrupt configure related apis
  395. * @brief ets intr apis
  396. */
  397. /** @addtogroup ets_intr_apis
  398. * @{
  399. */
  400. typedef void (* ets_isr_t)(void *);/**< interrupt handler type*/
  401. /**
  402. * @brief Attach a interrupt handler to a CPU interrupt number.
  403. * This function equals to _xtos_set_interrupt_handler_arg(i, func, arg).
  404. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  405. *
  406. * @param int i : CPU interrupt number.
  407. *
  408. * @param ets_isr_t func : Interrupt handler.
  409. *
  410. * @param void *arg : argument of the handler.
  411. *
  412. * @return None
  413. */
  414. void ets_isr_attach(int i, ets_isr_t func, void *arg);
  415. /**
  416. * @brief Mask the interrupts which show in mask bits.
  417. * This function equals to _xtos_ints_off(mask).
  418. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  419. *
  420. * @param uint32_t mask : BIT(i) means mask CPU interrupt number i.
  421. *
  422. * @return None
  423. */
  424. void ets_isr_mask(uint32_t mask);
  425. /**
  426. * @brief Unmask the interrupts which show in mask bits.
  427. * This function equals to _xtos_ints_on(mask).
  428. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  429. *
  430. * @param uint32_t mask : BIT(i) means mask CPU interrupt number i.
  431. *
  432. * @return None
  433. */
  434. void ets_isr_unmask(uint32_t unmask);
  435. /**
  436. * @brief Lock the interrupt to level 2.
  437. * This function direct set the CPU registers.
  438. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  439. *
  440. * @param None
  441. *
  442. * @return None
  443. */
  444. void ets_intr_lock(void);
  445. /**
  446. * @brief Unlock the interrupt to level 0.
  447. * This function direct set the CPU registers.
  448. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  449. *
  450. * @param None
  451. *
  452. * @return None
  453. */
  454. void ets_intr_unlock(void);
  455. /**
  456. * @brief Unlock the interrupt to level 0, and CPU will go into power save mode(wait interrupt).
  457. * This function direct set the CPU registers.
  458. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  459. *
  460. * @param None
  461. *
  462. * @return None
  463. */
  464. void ets_waiti0(void);
  465. /**
  466. * @brief Attach an CPU interrupt to a hardware source.
  467. * We have 4 steps to use an interrupt:
  468. * 1.Attach hardware interrupt source to CPU. intr_matrix_set(0, ETS_WIFI_MAC_INTR_SOURCE, ETS_WMAC_INUM);
  469. * 2.Set interrupt handler. xt_set_interrupt_handler(ETS_WMAC_INUM, func, NULL);
  470. * 3.Enable interrupt for CPU. xt_ints_on(1 << ETS_WMAC_INUM);
  471. * 4.Enable interrupt in the module.
  472. *
  473. * @param int cpu_no : The CPU which the interrupt number belongs.
  474. *
  475. * @param uint32_t model_num : The interrupt hardware source number, please see the interrupt hardware source table.
  476. *
  477. * @param uint32_t intr_num : The interrupt number CPU, please see the interrupt cpu using table.
  478. *
  479. * @return None
  480. */
  481. void intr_matrix_set(int cpu_no, uint32_t model_num, uint32_t intr_num);
  482. #define _ETSTR(v) # v
  483. #define _ETS_SET_INTLEVEL(intlevel) ({ unsigned __tmp; \
  484. __asm__ __volatile__( "rsil %0, " _ETSTR(intlevel) "\n" \
  485. : "=a" (__tmp) : : "memory" ); \
  486. })
  487. #ifdef CONFIG_NONE_OS
  488. #define ETS_INTR_LOCK() \
  489. ets_intr_lock()
  490. #define ETS_INTR_UNLOCK() \
  491. ets_intr_unlock()
  492. #define ETS_ISR_ATTACH \
  493. ets_isr_attach
  494. #define ETS_INTR_ENABLE(inum) \
  495. ets_isr_unmask((1<<inum))
  496. #define ETS_INTR_DISABLE(inum) \
  497. ets_isr_mask((1<<inum))
  498. #define ETS_WMAC_INTR_ATTACH(func, arg) \
  499. ETS_ISR_ATTACH(ETS_WMAC_INUM, (func), (void *)(arg))
  500. #define ETS_TG0_T0_INTR_ATTACH(func, arg) \
  501. ETS_ISR_ATTACH(ETS_TG0_T0_INUM, (func), (void *)(arg))
  502. #define ETS_GPIO_INTR_ATTACH(func, arg) \
  503. ETS_ISR_ATTACH(ETS_GPIO_INUM, (func), (void *)(arg))
  504. #define ETS_UART0_INTR_ATTACH(func, arg) \
  505. ETS_ISR_ATTACH(ETS_UART0_INUM, (func), (void *)(arg))
  506. #define ETS_WDT_INTR_ATTACH(func, arg) \
  507. ETS_ISR_ATTACH(ETS_WDT_INUM, (func), (void *)(arg))
  508. #define ETS_SLC_INTR_ATTACH(func, arg) \
  509. ETS_ISR_ATTACH(ETS_SLC_INUM, (func), (void *)(arg))
  510. #define ETS_BB_INTR_ENABLE() \
  511. ETS_INTR_ENABLE(ETS_BB_INUM)
  512. #define ETS_BB_INTR_DISABLE() \
  513. ETS_INTR_DISABLE(ETS_BB_INUM)
  514. #define ETS_UART0_INTR_ENABLE() \
  515. ETS_INTR_ENABLE(ETS_UART0_INUM)
  516. #define ETS_UART0_INTR_DISABLE() \
  517. ETS_INTR_DISABLE(ETS_UART0_INUM)
  518. #define ETS_GPIO_INTR_ENABLE() \
  519. ETS_INTR_ENABLE(ETS_GPIO_INUM)
  520. #define ETS_GPIO_INTR_DISABLE() \
  521. ETS_INTR_DISABLE(ETS_GPIO_INUM)
  522. #define ETS_WDT_INTR_ENABLE() \
  523. ETS_INTR_ENABLE(ETS_WDT_INUM)
  524. #define ETS_WDT_INTR_DISABLE() \
  525. ETS_INTR_DISABLE(ETS_WDT_INUM)
  526. #define ETS_TG0_T0_INTR_ENABLE() \
  527. ETS_INTR_ENABLE(ETS_TG0_T0_INUM)
  528. #define ETS_TG0_T0_INTR_DISABLE() \
  529. ETS_INTR_DISABLE(ETS_TG0_T0_INUM)
  530. #define ETS_SLC_INTR_ENABLE() \
  531. ETS_INTR_ENABLE(ETS_SLC_INUM)
  532. #define ETS_SLC_INTR_DISABLE() \
  533. ETS_INTR_DISABLE(ETS_SLC_INUM)
  534. #endif
  535. /**
  536. * @}
  537. */
  538. #ifndef MAC2STR
  539. #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
  540. #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
  541. #endif
  542. #define ETS_MEM_BAR() asm volatile ( "" : : : "memory" )
  543. typedef enum {
  544. OK = 0,
  545. FAIL,
  546. PENDING,
  547. BUSY,
  548. CANCEL,
  549. } STATUS;
  550. /**
  551. * @}
  552. */
  553. #ifdef __cplusplus
  554. }
  555. #endif