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