1 /*
2  * SPDX-FileCopyrightText: 2022-2023 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 
300 /**
301   * @brief  Get the real CPU ticks per us to the ets.
302   *         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.
303   *
304   * @param  None
305   *
306   * @return uint32_t : CPU ticks per us record in ets.
307   */
308 uint32_t ets_get_cpu_frequency(void);
309 
310 /**
311   * @}
312   */
313 
314 /** \defgroup ets_intr_apis, ets interrupt configure related apis
315   * @brief ets intr apis
316   */
317 
318 /** @addtogroup ets_intr_apis
319   * @{
320   */
321 
322 typedef void (* ets_isr_t)(void *);/**< interrupt handler type*/
323 
324 /**
325   * @brief  Attach a interrupt handler to a CPU interrupt number.
326   *         This function equals to _xtos_set_interrupt_handler_arg(i, func, arg).
327   *         In FreeRTOS, please call FreeRTOS apis, never call this api.
328   *
329   * @param  int i : CPU interrupt number.
330   *
331   * @param  ets_isr_t func : Interrupt handler.
332   *
333   * @param  void *arg : argument of the handler.
334   *
335   * @return None
336   */
337 void ets_isr_attach(int i, ets_isr_t func, void *arg);
338 
339 /**
340   * @brief  Mask the interrupts which show in mask bits.
341   *         This function equals to _xtos_ints_off(mask).
342   *         In FreeRTOS, please call FreeRTOS apis, never call this api.
343   *
344   * @param  uint32_t mask : BIT(i) means mask CPU interrupt number i.
345   *
346   * @return None
347   */
348 void ets_isr_mask(uint32_t mask);
349 
350 /**
351   * @brief  Unmask the interrupts which show in mask bits.
352   *         This function equals to _xtos_ints_on(mask).
353   *         In FreeRTOS, please call FreeRTOS apis, never call this api.
354   *
355   * @param  uint32_t mask : BIT(i) means mask CPU interrupt number i.
356   *
357   * @return None
358   */
359 void ets_isr_unmask(uint32_t unmask);
360 
361 /**
362   * @brief  Lock the interrupt to level 2.
363   *         This function direct set the CPU registers.
364   *         In FreeRTOS, please call FreeRTOS apis, never call this api.
365   *
366   * @param  None
367   *
368   * @return None
369   */
370 void ets_intr_lock(void);
371 
372 /**
373   * @brief  Unlock the interrupt to level 0.
374   *         This function direct set the CPU registers.
375   *         In FreeRTOS, please call FreeRTOS apis, never call this api.
376   *
377   * @param  None
378   *
379   * @return None
380   */
381 void ets_intr_unlock(void);
382 
383 /**
384   * @brief  Attach an CPU interrupt to a hardware source.
385   *         We have 4 steps to use an interrupt:
386   *         1.Attach hardware interrupt source to CPU.  intr_matrix_set(0, ETS_WIFI_MAC_INTR_SOURCE, ETS_WMAC_INUM);
387   *         2.Set interrupt handler.                    xt_set_interrupt_handler(ETS_WMAC_INUM, func, NULL);
388   *         3.Enable interrupt for CPU.                 xt_ints_on(1 << ETS_WMAC_INUM);
389   *         4.Enable interrupt in the module.
390   *
391   * @param  int cpu_no : The CPU which the interrupt number belongs.
392   *
393   * @param  uint32_t model_num : The interrupt hardware source number, please see the interrupt hardware source table.
394   *
395   * @param  uint32_t intr_num : The interrupt number CPU, please see the interrupt cpu using table.
396   *
397   * @return None
398   */
399 void intr_matrix_set(int cpu_no, uint32_t model_num, uint32_t intr_num);
400 
401 /**
402   * @}
403   */
404 
405 #ifndef MAC2STR
406 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
407 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
408 #endif
409 
410 #define ETS_MEM_BAR() asm volatile ( "" : : : "memory" )
411 
412 #ifdef ESP_PLATFORM
413 // Remove in IDF v6.0 (IDF-7044)
414 typedef enum {
415     OK = 0,
416     FAIL,
417     PENDING,
418     BUSY,
419     CANCEL,
420 } STATUS __attribute__((deprecated("Use ETS_STATUS instead")));
421 #endif
422 
423 /**
424   * @}
425   */
426 
427 #ifdef __cplusplus
428 }
429 #endif
430 
431 #endif /* _ROM_ETS_SYS_H_ */
432