ets_sys.h 15 KB

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