ets_sys.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2023 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 "soc/soc.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /** \defgroup ets_sys_apis, ets system related apis
  14. * @brief ets system apis
  15. */
  16. /** @addtogroup ets_sys_apis
  17. * @{
  18. */
  19. /************************************************************************
  20. * NOTE
  21. * Many functions in this header files can't be run in FreeRTOS.
  22. * Please see the comment of the Functions.
  23. * There are also some functions that doesn't work on FreeRTOS
  24. * without listed in the header, such as:
  25. * xtos functions start with "_xtos_" in ld file.
  26. *
  27. ***********************************************************************
  28. */
  29. /** \defgroup ets_apis, Espressif Task Scheduler related apis
  30. * @brief ets apis
  31. */
  32. /** @addtogroup ets_apis
  33. * @{
  34. */
  35. typedef enum {
  36. ETS_OK = 0, /**< return successful in ets*/
  37. ETS_FAILED = 1, /**< return failed in ets*/
  38. ETS_PENDING = 2,
  39. ETS_BUSY = 3,
  40. ETS_CANCEL = 4,
  41. } ETS_STATUS;
  42. typedef ETS_STATUS ets_status_t;
  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. * @}
  54. */
  55. /** \defgroup ets_boot_apis, Boot routing related apis
  56. * @brief ets boot apis
  57. */
  58. /** @addtogroup ets_apis
  59. * @{
  60. */
  61. extern const char *const exc_cause_table[40]; ///**< excption cause that defined by the core.*/
  62. /**
  63. * @brief Set Pro cpu Entry code, code can be called in PRO CPU when booting is not completed.
  64. * When Pro CPU booting is completed, Pro CPU will call the Entry code if not NULL.
  65. *
  66. * @param uint32_t start : the PRO Entry code address value in uint32_t
  67. *
  68. * @return None
  69. */
  70. void ets_set_user_start(uint32_t start);
  71. /**
  72. * @brief Set App cpu Entry code, code can be called in PRO CPU.
  73. * When APP booting is completed, APP CPU will call the Entry code if not NULL.
  74. *
  75. * @param uint32_t start : the APP Entry code address value in uint32_t, stored in register APPCPU_CTRL_REG_D.
  76. *
  77. * @return None
  78. */
  79. void ets_set_appcpu_boot_addr(uint32_t start);
  80. /**
  81. * @}
  82. */
  83. /** \defgroup ets_printf_apis, ets_printf related apis used in ets
  84. * @brief ets printf apis
  85. */
  86. /** @addtogroup ets_printf_apis
  87. * @{
  88. */
  89. /**
  90. * @brief Printf the strings to uart or other devices, similar with printf, simple than printf.
  91. * Can not print float point data format, or longlong data format.
  92. * So we maybe only use this in ROM.
  93. *
  94. * @param const char *fmt : See printf.
  95. *
  96. * @param ... : See printf.
  97. *
  98. * @return int : the length printed to the output device.
  99. */
  100. int ets_printf(const char *fmt, ...);
  101. /**
  102. * @brief Get the uart channel of ets_printf(uart_tx_one_char).
  103. *
  104. * @return uint8_t uart channel used by ets_printf(uart_tx_one_char).
  105. */
  106. uint8_t ets_get_printf_channel(void);
  107. /**
  108. * @brief Output a char to uart, which uart to output(which is in uart module in ROM) is not in scope of the function.
  109. * Can not print float point data format, or longlong data format
  110. *
  111. * @param char c : char to output.
  112. *
  113. * @return None
  114. */
  115. void ets_write_char_uart(char c);
  116. /**
  117. * @brief Ets_printf have two output functions: putc1 and putc2, both of which will be called if need ouput.
  118. * To install putc1, which is defaulted installed as ets_write_char_uart in none silent boot mode, as NULL in silent mode.
  119. *
  120. * @param void (*)(char) p: Output function to install.
  121. *
  122. * @return None
  123. */
  124. void ets_install_putc1(void (*p)(char c));
  125. /**
  126. * @brief Ets_printf have two output functions: putc1 and putc2, both of which will be called if need ouput.
  127. * To install putc2, which is defaulted installed as NULL.
  128. *
  129. * @param void (*)(char) p: Output function to install.
  130. *
  131. * @return None
  132. */
  133. void ets_install_putc2(void (*p)(char c));
  134. /**
  135. * @brief Install putc1 as ets_write_char_uart.
  136. * In silent boot mode(to void interfere the UART attached MCU), we can call this function, after booting ok.
  137. *
  138. * @param None
  139. *
  140. * @return None
  141. */
  142. void ets_install_uart_printf(void);
  143. #define ETS_PRINTF(...) ets_printf(...)
  144. #define ETS_ASSERT(v) do { \
  145. if (!(v)) { \
  146. ets_printf("%s %u \n", __FILE__, __LINE__); \
  147. while (1) {}; \
  148. } \
  149. } while (0);
  150. /**
  151. * @}
  152. */
  153. /** \defgroup ets_timer_apis, ets_timer related apis used in ets
  154. * @brief ets timer apis
  155. */
  156. /** @addtogroup ets_timer_apis
  157. * @{
  158. */
  159. typedef void ETSTimerFunc(void *timer_arg);/**< timer handler*/
  160. typedef struct _ETSTIMER_ {
  161. struct _ETSTIMER_ *timer_next; /**< timer linker*/
  162. uint32_t timer_expire; /**< abstruct time when timer expire*/
  163. uint32_t timer_period; /**< timer period, 0 means timer is not periodic repeated*/
  164. ETSTimerFunc *timer_func; /**< timer handler*/
  165. void *timer_arg; /**< timer handler argument*/
  166. } ETSTimer;
  167. /**
  168. * @brief Init ets timer, this timer range is 640 us to 429496 ms
  169. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  170. *
  171. * @param None
  172. *
  173. * @return None
  174. */
  175. void ets_timer_init(void);
  176. /**
  177. * @brief In FreeRTOS, please call FreeRTOS apis, never call this api.
  178. *
  179. * @param None
  180. *
  181. * @return None
  182. */
  183. void ets_timer_deinit(void);
  184. /**
  185. * @brief Arm an ets timer, this timer range is 640 us to 429496 ms.
  186. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  187. *
  188. * @param ETSTimer *timer : Timer struct pointer.
  189. *
  190. * @param uint32_t tmout : Timer value in ms, range is 1 to 429496.
  191. *
  192. * @param bool repeat : Timer is periodic repeated.
  193. *
  194. * @return None
  195. */
  196. void ets_timer_arm(ETSTimer *timer, uint32_t tmout, bool repeat);
  197. /**
  198. * @brief Arm an ets timer, this timer range is 640 us to 429496 ms.
  199. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  200. *
  201. * @param ETSTimer *timer : Timer struct pointer.
  202. *
  203. * @param uint32_t tmout : Timer value in us, range is 1 to 429496729.
  204. *
  205. * @param bool repeat : Timer is periodic repeated.
  206. *
  207. * @return None
  208. */
  209. void ets_timer_arm_us(ETSTimer *ptimer, uint32_t us, bool repeat);
  210. /**
  211. * @brief Disarm an ets timer.
  212. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  213. *
  214. * @param ETSTimer *timer : Timer struct pointer.
  215. *
  216. * @return None
  217. */
  218. void ets_timer_disarm(ETSTimer *timer);
  219. /**
  220. * @brief Set timer callback and argument.
  221. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  222. *
  223. * @param ETSTimer *timer : Timer struct pointer.
  224. *
  225. * @param ETSTimerFunc *pfunction : Timer callback.
  226. *
  227. * @param void *parg : Timer callback argument.
  228. *
  229. * @return None
  230. */
  231. void ets_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg);
  232. /**
  233. * @brief Unset timer callback and argument to NULL.
  234. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  235. *
  236. * @param ETSTimer *timer : Timer struct pointer.
  237. *
  238. * @return None
  239. */
  240. void ets_timer_done(ETSTimer *ptimer);
  241. /**
  242. * @brief CPU do while loop for some time.
  243. * In FreeRTOS task, please call FreeRTOS apis.
  244. *
  245. * @param uint32_t us : Delay time in us.
  246. *
  247. * @return None
  248. */
  249. void ets_delay_us(uint32_t us);
  250. /**
  251. * @brief Set the real CPU ticks per us to the ets, so that ets_delay_us will be accurate.
  252. * Call this function when CPU frequency is changed.
  253. *
  254. * @param uint32_t ticks_per_us : CPU ticks per us.
  255. *
  256. * @return None
  257. */
  258. void ets_update_cpu_frequency(uint32_t ticks_per_us);
  259. /**
  260. * @brief Get the real CPU ticks per us to the ets.
  261. * 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.
  262. *
  263. * @param None
  264. *
  265. * @return uint32_t : CPU ticks per us record in ets.
  266. */
  267. uint32_t ets_get_cpu_frequency(void);
  268. /**
  269. * @brief Get xtal_freq value, If value not stored in RTC_STORE5, than store.
  270. *
  271. * @param None
  272. *
  273. * @return uint32_t : if stored in efuse(not 0)
  274. * clock = ets_efuse_get_xtal_freq() * 1000000;
  275. * else if analog_8M in efuse
  276. * clock = ets_get_xtal_scale() * 625 / 16 * ets_efuse_get_8M_clock();
  277. * else clock = 40M.
  278. */
  279. uint32_t ets_get_xtal_freq(void);
  280. /**
  281. * @brief Get the apb divisor. The xtal frequency gets divided
  282. * by this value to generate the APB clock.
  283. * When any types of reset happens, the default value is 2.
  284. *
  285. * @param None
  286. *
  287. * @return uint32_t : 1 or 2.
  288. */
  289. uint32_t ets_get_xtal_div(void);
  290. /**
  291. * @brief Modifies the apb divisor. The xtal frequency gets divided by this to
  292. * generate the APB clock.
  293. *
  294. * @note The xtal frequency divisor is 2 by default as the glitch detector
  295. * doesn't properly stop glitches when it is 1. Please do not set the
  296. * divisor to 1 before the PLL is active without being aware that you
  297. * may be introducing a security risk.
  298. *
  299. * @param div Divisor. 1 = xtal freq, 2 = 1/2th xtal freq.
  300. */
  301. void ets_set_xtal_div(int div);
  302. /**
  303. * @brief Get apb_freq value, If value not stored in RTC_STORE5, than store.
  304. *
  305. * @param None
  306. *
  307. * @return uint32_t : if rtc store the value (RTC_STORE5 high 16 bits and low 16 bits with same value), read from rtc register.
  308. * clock = (REG_READ(RTC_STORE5) & 0xffff) << 12;
  309. * else store ets_get_detected_xtal_freq() in.
  310. */
  311. uint32_t ets_get_apb_freq(void);
  312. /**
  313. * @}
  314. */
  315. /** \defgroup ets_intr_apis, ets interrupt configure related apis
  316. * @brief ets intr apis
  317. */
  318. /** @addtogroup ets_intr_apis
  319. * @{
  320. */
  321. typedef void (* ets_isr_t)(void *);/**< interrupt handler type*/
  322. /**
  323. * @brief Attach a interrupt handler to a CPU interrupt number.
  324. * This function equals to _xtos_set_interrupt_handler_arg(i, func, arg).
  325. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  326. *
  327. * @param int i : CPU interrupt number.
  328. *
  329. * @param ets_isr_t func : Interrupt handler.
  330. *
  331. * @param void *arg : argument of the handler.
  332. *
  333. * @return None
  334. */
  335. void ets_isr_attach(int i, ets_isr_t func, void *arg);
  336. /**
  337. * @brief Mask the interrupts which show in mask bits.
  338. * This function equals to _xtos_ints_off(mask).
  339. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  340. *
  341. * @param uint32_t mask : BIT(i) means mask CPU interrupt number i.
  342. *
  343. * @return None
  344. */
  345. void ets_isr_mask(uint32_t mask);
  346. /**
  347. * @brief Unmask the interrupts which show in mask bits.
  348. * This function equals to _xtos_ints_on(mask).
  349. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  350. *
  351. * @param uint32_t mask : BIT(i) means mask CPU interrupt number i.
  352. *
  353. * @return None
  354. */
  355. void ets_isr_unmask(uint32_t unmask);
  356. /**
  357. * @brief Lock the interrupt to level 2.
  358. * This function direct set the CPU registers.
  359. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  360. *
  361. * @param None
  362. *
  363. * @return None
  364. */
  365. void ets_intr_lock(void);
  366. /**
  367. * @brief Unlock the interrupt to level 0.
  368. * This function direct set the CPU registers.
  369. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  370. *
  371. * @param None
  372. *
  373. * @return None
  374. */
  375. void ets_intr_unlock(void);
  376. /**
  377. * @brief Attach an CPU interrupt to a hardware source.
  378. * We have 4 steps to use an interrupt:
  379. * 1.Attach hardware interrupt source to CPU. intr_matrix_set(0, ETS_WIFI_MAC_INTR_SOURCE, ETS_WMAC_INUM);
  380. * 2.Set interrupt handler. xt_set_interrupt_handler(ETS_WMAC_INUM, func, NULL);
  381. * 3.Enable interrupt for CPU. xt_ints_on(1 << ETS_WMAC_INUM);
  382. * 4.Enable interrupt in the module.
  383. *
  384. * @param int cpu_no : The CPU which the interrupt number belongs.
  385. *
  386. * @param uint32_t model_num : The interrupt hardware source number, please see the interrupt hardware source table.
  387. *
  388. * @param uint32_t intr_num : The interrupt number CPU, please see the interrupt cpu using table.
  389. *
  390. * @return None
  391. */
  392. void intr_matrix_set(int cpu_no, uint32_t model_num, uint32_t intr_num);
  393. #define _ETSTR(v) # v
  394. #define _ETS_SET_INTLEVEL(intlevel) ({ unsigned __tmp; \
  395. __asm__ __volatile__( "rsil %0, " _ETSTR(intlevel) "\n" \
  396. : "=a" (__tmp) : : "memory" ); \
  397. })
  398. #ifdef CONFIG_NONE_OS
  399. #define ETS_INTR_LOCK() \
  400. ets_intr_lock()
  401. #define ETS_INTR_UNLOCK() \
  402. ets_intr_unlock()
  403. #define ETS_ISR_ATTACH \
  404. ets_isr_attach
  405. #define ETS_INTR_ENABLE(inum) \
  406. ets_isr_unmask((1<<inum))
  407. #define ETS_INTR_DISABLE(inum) \
  408. ets_isr_mask((1<<inum))
  409. #define ETS_WMAC_INTR_ATTACH(func, arg) \
  410. ETS_ISR_ATTACH(ETS_WMAC_INUM, (func), (void *)(arg))
  411. #define ETS_TG0_T0_INTR_ATTACH(func, arg) \
  412. ETS_ISR_ATTACH(ETS_TG0_T0_INUM, (func), (void *)(arg))
  413. #define ETS_GPIO_INTR_ATTACH(func, arg) \
  414. ETS_ISR_ATTACH(ETS_GPIO_INUM, (func), (void *)(arg))
  415. #define ETS_UART0_INTR_ATTACH(func, arg) \
  416. ETS_ISR_ATTACH(ETS_UART0_INUM, (func), (void *)(arg))
  417. #define ETS_WDT_INTR_ATTACH(func, arg) \
  418. ETS_ISR_ATTACH(ETS_WDT_INUM, (func), (void *)(arg))
  419. #define ETS_SLC_INTR_ATTACH(func, arg) \
  420. ETS_ISR_ATTACH(ETS_SLC_INUM, (func), (void *)(arg))
  421. #define ETS_BB_INTR_ENABLE() \
  422. ETS_INTR_ENABLE(ETS_BB_INUM)
  423. #define ETS_BB_INTR_DISABLE() \
  424. ETS_INTR_DISABLE(ETS_BB_INUM)
  425. #define ETS_UART0_INTR_ENABLE() \
  426. ETS_INTR_ENABLE(ETS_UART0_INUM)
  427. #define ETS_UART0_INTR_DISABLE() \
  428. ETS_INTR_DISABLE(ETS_UART0_INUM)
  429. #define ETS_GPIO_INTR_ENABLE() \
  430. ETS_INTR_ENABLE(ETS_GPIO_INUM)
  431. #define ETS_GPIO_INTR_DISABLE() \
  432. ETS_INTR_DISABLE(ETS_GPIO_INUM)
  433. #define ETS_WDT_INTR_ENABLE() \
  434. ETS_INTR_ENABLE(ETS_WDT_INUM)
  435. #define ETS_WDT_INTR_DISABLE() \
  436. ETS_INTR_DISABLE(ETS_WDT_INUM)
  437. #define ETS_TG0_T0_INTR_ENABLE() \
  438. ETS_INTR_ENABLE(ETS_TG0_T0_INUM)
  439. #define ETS_TG0_T0_INTR_DISABLE() \
  440. ETS_INTR_DISABLE(ETS_TG0_T0_INUM)
  441. #define ETS_SLC_INTR_ENABLE() \
  442. ETS_INTR_ENABLE(ETS_SLC_INUM)
  443. #define ETS_SLC_INTR_DISABLE() \
  444. ETS_INTR_DISABLE(ETS_SLC_INUM)
  445. #endif
  446. /**
  447. * @}
  448. */
  449. #ifndef MAC2STR
  450. #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
  451. #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
  452. #endif
  453. #define ETS_MEM_BAR() asm volatile ( "" : : : "memory" )
  454. #ifdef ESP_PLATFORM
  455. // Remove in IDF v6.0 (IDF-7044)
  456. typedef enum {
  457. OK = 0,
  458. FAIL,
  459. PENDING,
  460. BUSY,
  461. CANCEL,
  462. } STATUS __attribute__((deprecated("Use ETS_STATUS instead")));
  463. #endif
  464. /**
  465. * @}
  466. */
  467. #ifdef __cplusplus
  468. }
  469. #endif