ets_sys.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * SPDX-FileCopyrightText: 2020-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. #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_STATUS;
  39. typedef ETS_STATUS ets_status_t;
  40. typedef uint32_t ETSSignal;
  41. typedef uint32_t ETSParam;
  42. typedef struct ETSEventTag ETSEvent; /**< Event transmit/receive in ets*/
  43. struct ETSEventTag {
  44. ETSSignal sig; /**< Event signal, in same task, different Event with different signal*/
  45. ETSParam par; /**< Event parameter, sometimes without usage, then will be set as 0*/
  46. };
  47. typedef void (*ETSTask)(ETSEvent *e); /**< Type of the Task processer*/
  48. typedef void (* ets_idle_cb_t)(void *arg); /**< Type of the system idle callback*/
  49. /**
  50. * @brief Start the Espressif Task Scheduler, which is an infinit loop. Please do not add code after it.
  51. *
  52. * @param none
  53. *
  54. * @return none
  55. */
  56. void ets_run(void);
  57. /**
  58. * @brief Set the Idle callback, when Tasks are processed, will call the callback before CPU goto sleep.
  59. *
  60. * @param ets_idle_cb_t func : The callback function.
  61. *
  62. * @param void *arg : Argument of the callback.
  63. *
  64. * @return None
  65. */
  66. void ets_set_idle_cb(ets_idle_cb_t func, void *arg);
  67. /**
  68. * @brief Init a task with processer, priority, queue to receive Event, queue length.
  69. *
  70. * @param ETSTask task : The task processer.
  71. *
  72. * @param uint8_t prio : Task priority, 0-31, bigger num with high priority, one priority with one task.
  73. *
  74. * @param ETSEvent *queue : Queue belongs to the task, task always receives Events, Queue is circular used.
  75. *
  76. * @param uint8_t qlen : Queue length.
  77. *
  78. * @return None
  79. */
  80. void ets_task(ETSTask task, uint8_t prio, ETSEvent *queue, uint8_t qlen);
  81. /**
  82. * @brief Post an event to an Task.
  83. *
  84. * @param uint8_t prio : Priority of the Task.
  85. *
  86. * @param ETSSignal sig : Event signal.
  87. *
  88. * @param ETSParam par : Event parameter
  89. *
  90. * @return ETS_OK : post successful
  91. * @return ETS_FAILED : post failed
  92. */
  93. ETS_STATUS ets_post(uint8_t prio, ETSSignal sig, ETSParam par);
  94. /**
  95. * @}
  96. */
  97. /** \defgroup ets_boot_apis, Boot routing related apis
  98. * @brief ets boot apis
  99. */
  100. /** @addtogroup ets_apis
  101. * @{
  102. */
  103. extern const char *const exc_cause_table[40]; ///**< excption cause that defined by the core.*/
  104. /**
  105. * @brief Set Pro cpu Entry code, code can be called in PRO CPU when booting is not completed.
  106. * When Pro CPU booting is completed, Pro CPU will call the Entry code if not NULL.
  107. *
  108. * @param uint32_t start : the PRO Entry code address value in uint32_t
  109. *
  110. * @return None
  111. */
  112. void ets_set_user_start(uint32_t start);
  113. /**
  114. * @brief Set Pro cpu Startup code, code can be called when booting is not completed, or in Entry code.
  115. * When Entry code completed, CPU will call the Startup code if not NULL, else call ets_run.
  116. *
  117. * @param uint32_t callback : the Startup code address value in uint32_t
  118. *
  119. * @return None : post successful
  120. */
  121. void ets_set_startup_callback(uint32_t callback);
  122. /**
  123. * @brief Set App cpu Entry code, code can be called in PRO CPU.
  124. * When APP booting is completed, APP CPU will call the Entry code if not NULL.
  125. *
  126. * @param uint32_t start : the APP Entry code address value in uint32_t, stored in register APPCPU_CTRL_REG_D.
  127. *
  128. * @return None
  129. */
  130. void ets_set_appcpu_boot_addr(uint32_t start);
  131. /**
  132. * @}
  133. */
  134. /** \defgroup ets_printf_apis, ets_printf related apis used in ets
  135. * @brief ets printf apis
  136. */
  137. /** @addtogroup ets_printf_apis
  138. * @{
  139. */
  140. /**
  141. * @brief Printf the strings to uart or other devices, similar with printf, simple than printf.
  142. * Can not print float point data format, or longlong data format.
  143. * So we maybe only use this in ROM.
  144. *
  145. * @param const char *fmt : See printf.
  146. *
  147. * @param ... : See printf.
  148. *
  149. * @return int : the length printed to the output device.
  150. */
  151. int ets_printf(const char *fmt, ...);
  152. /**
  153. * @brief Set the uart channel of ets_printf(uart_tx_one_char).
  154. * ROM will set it base on the efuse and gpio setting, however, this can be changed after booting.
  155. *
  156. * @param uart_no : 0 for UART0, 1 for UART1.
  157. *
  158. * @return None
  159. */
  160. void ets_set_printf_channel(uint8_t uart_no);
  161. /**
  162. * @brief Get the uart channel of ets_printf(uart_tx_one_char).
  163. *
  164. * @return uint8_t uart channel used by ets_printf(uart_tx_one_char).
  165. */
  166. uint8_t ets_get_printf_channel(void);
  167. /**
  168. * @brief Output a char to uart, which uart to output(which is in uart module in ROM) is not in scope of the function.
  169. * Can not print float point data format, or longlong data format
  170. *
  171. * @param char c : char to output.
  172. *
  173. * @return None
  174. */
  175. void ets_write_char_uart(char c);
  176. /**
  177. * @brief Ets_printf have two output functions: putc1 and putc2, both of which will be called if need ouput.
  178. * To install putc1, which is defaulted installed as ets_write_char_uart in none silent boot mode, as NULL in silent mode.
  179. *
  180. * @param void (*)(char) p: Output function to install.
  181. *
  182. * @return None
  183. */
  184. void ets_install_putc1(void (*p)(char c));
  185. /**
  186. * @brief Ets_printf have two output functions: putc1 and putc2, both of which will be called if need ouput.
  187. * To install putc2, which is defaulted installed as NULL.
  188. *
  189. * @param void (*)(char) p: Output function to install.
  190. *
  191. * @return None
  192. */
  193. void ets_install_putc2(void (*p)(char c));
  194. /**
  195. * @brief Install putc1 as ets_write_char_uart.
  196. * In silent boot mode(to void interfere the UART attached MCU), we can call this function, after booting ok.
  197. *
  198. * @param None
  199. *
  200. * @return None
  201. */
  202. void ets_install_uart_printf(void);
  203. #define ETS_PRINTF(...) ets_printf(...)
  204. #define ETS_ASSERT(v) do { \
  205. if (!(v)) { \
  206. ets_printf("%s %u \n", __FILE__, __LINE__); \
  207. while (1) {}; \
  208. } \
  209. } while (0);
  210. /**
  211. * @}
  212. */
  213. /** \defgroup ets_timer_apis, ets_timer related apis used in ets
  214. * @brief ets timer apis
  215. */
  216. /** @addtogroup ets_timer_apis
  217. * @{
  218. */
  219. typedef void ETSTimerFunc(void *timer_arg);/**< timer handler*/
  220. typedef struct _ETSTIMER_ {
  221. struct _ETSTIMER_ *timer_next; /**< timer linker*/
  222. uint32_t timer_expire; /**< abstruct time when timer expire*/
  223. uint32_t timer_period; /**< timer period, 0 means timer is not periodic repeated*/
  224. ETSTimerFunc *timer_func; /**< timer handler*/
  225. void *timer_arg; /**< timer handler argument*/
  226. } ETSTimer;
  227. /**
  228. * @brief Init ets timer, this timer range is 640 us to 429496 ms
  229. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  230. *
  231. * @param None
  232. *
  233. * @return None
  234. */
  235. void ets_timer_init(void);
  236. /**
  237. * @brief In FreeRTOS, please call FreeRTOS apis, never call this api.
  238. *
  239. * @param None
  240. *
  241. * @return None
  242. */
  243. void ets_timer_deinit(void);
  244. /**
  245. * @brief Arm an ets timer, this timer range is 640 us to 429496 ms.
  246. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  247. *
  248. * @param ETSTimer *timer : Timer struct pointer.
  249. *
  250. * @param uint32_t tmout : Timer value in ms, range is 1 to 429496.
  251. *
  252. * @param bool repeat : Timer is periodic repeated.
  253. *
  254. * @return None
  255. */
  256. void ets_timer_arm(ETSTimer *timer, uint32_t tmout, bool repeat);
  257. /**
  258. * @brief Arm an ets timer, this timer range is 640 us to 429496 ms.
  259. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  260. *
  261. * @param ETSTimer *timer : Timer struct pointer.
  262. *
  263. * @param uint32_t tmout : Timer value in us, range is 1 to 429496729.
  264. *
  265. * @param bool repeat : Timer is periodic repeated.
  266. *
  267. * @return None
  268. */
  269. void ets_timer_arm_us(ETSTimer *ptimer, uint32_t us, bool repeat);
  270. /**
  271. * @brief Disarm an ets timer.
  272. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  273. *
  274. * @param ETSTimer *timer : Timer struct pointer.
  275. *
  276. * @return None
  277. */
  278. void ets_timer_disarm(ETSTimer *timer);
  279. /**
  280. * @brief Set timer callback and argument.
  281. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  282. *
  283. * @param ETSTimer *timer : Timer struct pointer.
  284. *
  285. * @param ETSTimerFunc *pfunction : Timer callback.
  286. *
  287. * @param void *parg : Timer callback argument.
  288. *
  289. * @return None
  290. */
  291. void ets_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg);
  292. /**
  293. * @brief Unset timer callback and argument to NULL.
  294. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  295. *
  296. * @param ETSTimer *timer : Timer struct pointer.
  297. *
  298. * @return None
  299. */
  300. void ets_timer_done(ETSTimer *ptimer);
  301. /**
  302. * @brief CPU do while loop for some time.
  303. * In FreeRTOS task, please call FreeRTOS apis.
  304. *
  305. * @param uint32_t us : Delay time in us.
  306. *
  307. * @return None
  308. */
  309. void ets_delay_us(uint32_t us);
  310. /**
  311. * @brief Set the real CPU ticks per us to the ets, so that ets_delay_us will be accurate.
  312. * Call this function when CPU frequency is changed.
  313. *
  314. * @param uint32_t ticks_per_us : CPU ticks per us.
  315. *
  316. * @return None
  317. */
  318. void ets_update_cpu_frequency(uint32_t ticks_per_us);
  319. /**
  320. * @brief Set the real CPU ticks per us to the ets, so that ets_delay_us will be accurate.
  321. *
  322. * @note This function only sets the tick rate for the current CPU. It is located in ROM,
  323. * so the deep sleep stub can use it even if IRAM is not initialized yet.
  324. *
  325. * @param uint32_t ticks_per_us : CPU ticks per us.
  326. *
  327. * @return None
  328. */
  329. void ets_update_cpu_frequency_rom(uint32_t ticks_per_us);
  330. /**
  331. * @brief Get the real CPU ticks per us to the ets.
  332. * 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.
  333. *
  334. * @param None
  335. *
  336. * @return uint32_t : CPU ticks per us record in ets.
  337. */
  338. uint32_t ets_get_cpu_frequency(void);
  339. /**
  340. * @brief Get xtal_freq value, If value not stored in RTC_STORE5, than store.
  341. *
  342. * @param None
  343. *
  344. * @return uint32_t : if stored in efuse(not 0)
  345. * clock = ets_efuse_get_xtal_freq() * 1000000;
  346. * else if analog_8M in efuse
  347. * clock = ets_get_xtal_scale() * 625 / 16 * ets_efuse_get_8M_clock();
  348. * else clock = 40M.
  349. */
  350. uint32_t ets_get_xtal_freq(void);
  351. /**
  352. * @brief Get the apb divior by xtal frequency.
  353. * When any types of reset happen, the default value is 2.
  354. *
  355. * @param None
  356. *
  357. * @return uint32_t : 1 or 2.
  358. */
  359. uint32_t ets_get_xtal_div(void);
  360. /**
  361. * @brief Get apb_freq value, If value not stored in RTC_STORE5, than store.
  362. *
  363. * @param None
  364. *
  365. * @return uint32_t : if rtc store the value (RTC_STORE5 high 16 bits and low 16 bits with same value), read from rtc register.
  366. * clock = (REG_READ(RTC_STORE5) & 0xffff) << 12;
  367. * else store ets_get_detected_xtal_freq() in.
  368. */
  369. uint32_t ets_get_apb_freq(void);
  370. /**
  371. * @}
  372. */
  373. /** \defgroup ets_intr_apis, ets interrupt configure related apis
  374. * @brief ets intr apis
  375. */
  376. /** @addtogroup ets_intr_apis
  377. * @{
  378. */
  379. typedef void (* ets_isr_t)(void *);/**< interrupt handler type*/
  380. /**
  381. * @brief Attach a interrupt handler to a CPU interrupt number.
  382. * This function equals to _xtos_set_interrupt_handler_arg(i, func, arg).
  383. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  384. *
  385. * @param int i : CPU interrupt number.
  386. *
  387. * @param ets_isr_t func : Interrupt handler.
  388. *
  389. * @param void *arg : argument of the handler.
  390. *
  391. * @return None
  392. */
  393. void ets_isr_attach(int i, ets_isr_t func, void *arg);
  394. /**
  395. * @brief Mask the interrupts which show in mask bits.
  396. * This function equals to _xtos_ints_off(mask).
  397. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  398. *
  399. * @param uint32_t mask : BIT(i) means mask CPU interrupt number i.
  400. *
  401. * @return None
  402. */
  403. void ets_isr_mask(uint32_t mask);
  404. /**
  405. * @brief Unmask the interrupts which show in mask bits.
  406. * This function equals to _xtos_ints_on(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_unmask(uint32_t unmask);
  414. /**
  415. * @brief Lock the interrupt to level 2.
  416. * This function direct set the CPU registers.
  417. * In FreeRTOS, please call FreeRTOS apis, never call this api.
  418. *
  419. * @param None
  420. *
  421. * @return None
  422. */
  423. void ets_intr_lock(void);
  424. /**
  425. * @brief Unlock the interrupt to level 0.
  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_unlock(void);
  434. /**
  435. * @brief Unlock the interrupt to level 0, and CPU will go into power save mode(wait interrupt).
  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_waiti0(void);
  444. /**
  445. * @brief Attach an CPU interrupt to a hardware source.
  446. * We have 4 steps to use an interrupt:
  447. * 1.Attach hardware interrupt source to CPU. intr_matrix_set(0, ETS_WIFI_MAC_INTR_SOURCE, ETS_WMAC_INUM);
  448. * 2.Set interrupt handler. xt_set_interrupt_handler(ETS_WMAC_INUM, func, NULL);
  449. * 3.Enable interrupt for CPU. xt_ints_on(1 << ETS_WMAC_INUM);
  450. * 4.Enable interrupt in the module.
  451. *
  452. * @param int cpu_no : The CPU which the interrupt number belongs.
  453. *
  454. * @param uint32_t model_num : The interrupt hardware source number, please see the interrupt hardware source table.
  455. *
  456. * @param uint32_t intr_num : The interrupt number CPU, please see the interrupt cpu using table.
  457. *
  458. * @return None
  459. */
  460. void intr_matrix_set(int cpu_no, uint32_t model_num, uint32_t intr_num);
  461. /**
  462. * @}
  463. */
  464. #ifndef MAC2STR
  465. #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
  466. #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
  467. #endif
  468. #define ETS_MEM_BAR() asm volatile ( "" : : : "memory" )
  469. typedef enum {
  470. OK = 0,
  471. FAIL,
  472. PENDING,
  473. BUSY,
  474. CANCEL,
  475. } STATUS;
  476. /**
  477. * @}
  478. */
  479. #ifdef __cplusplus
  480. }
  481. #endif
  482. #endif /* _ROM_ETS_SYS_H_ */