ets_sys.h 17 KB

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