ets_sys.h 18 KB

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