ets_sys.h 17 KB

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