• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
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  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef __LEGARTOS_H__
17 #define __LEGARTOS_H__
18 #include <ctype.h>
19 #include <stdint.h>
20 #include "lega_rtos_port.h"
21 #include <stdbool.h>
22 #include "los_interrupt.h"
23 
24 #define LEGA_NEVER_TIMEOUT   (0xFFFFFFFF)
25 #define LEGA_WAIT_FOREVER    (0xFFFFFFFF)
26 #define LEGA_NO_WAIT         (0)
27 
28 #define LEGA_TASK_CONFIG_MAX            32
29 #define LEGA_TASK_CONFIG_WPA3_AUTH      0
30 #define LEGA_TASK_CONFIG_LWIFI          1
31 #define LEGA_TASK_CONFIG_UWIFI_RX       2
32 #define LEGA_TASK_CONFIG_UWIFI          3
33 #define LEGA_TASK_CONFIG_DHCPS          4
34 #define LEGA_TASK_CONFIG_BLE            5
35 
36 #define     LEGA_LWIP_DHCP_TASK_PRIORITY             (29)
37 
38 #ifdef ALIOS_SUPPORT
39 #define     LEGA_WPA3_AUTH_TASK_PRIORITY             (16)
40 #define     LEGA_LWIFI_TASK_PRIORITY                 (17)
41 #define     LEGA_UWIFI_RX_TASK_PRIORITY              (15)
42 #define     LEGA_UWIFI_TASK_PRIORITY                 (20)
43 #define     LEGA_BLE_SCHEDULER_PRIORITY              (13) // for testing
44 
45 #elif defined(HARMONYOS_SUPPORT)
46 #define     LEGA_WPA3_AUTH_TASK_PRIORITY             (36 - 5)
47 #define     LEGA_LWIFI_TASK_PRIORITY                 (35 - 5)
48 #define     LEGA_UWIFI_RX_TASK_PRIORITY              (37 - 5)
49 #define     LEGA_UWIFI_TASK_PRIORITY                 (33 - 5)
50 #define     LEGA_BLE_SCHEDULER_PRIORITY              (38 - 5) // for testing
51 #else
52 #define     LEGA_WPA3_AUTH_TASK_PRIORITY             (28)
53 #define     LEGA_LWIFI_TASK_PRIORITY                 (27)
54 #define     LEGA_UWIFI_RX_TASK_PRIORITY              (28)
55 #define     LEGA_UWIFI_TASK_PRIORITY                 (26)
56 #define     LEGA_BLE_SCHEDULER_PRIORITY              (29) // for testing
57 #endif // ALIOS_SUPPORT
58 
59 #ifdef ALIOS_SUPPORT
60 #define CONFIG_KV_BUFFER_SIZE KV_CONFIG_TOTAL_SIZE
61 #elif defined(HARMONYOS_SUPPORT)
62 #define CONFIG_KV_BUFFER_SIZE 0x8000
63 #else
64 #define CONFIG_KV_BUFFER_SIZE 0
65 #endif // ALIOS_SUPPORT
66 
67 #define lega_cpsr_t UINTPTR
68 #define MS_PER_SECONED  1000
69 typedef enum {
70     kNoErr = 0,
71     kGeneralErr,
72     kTimeoutErr,
73 } OSStatus;
74 
75 #ifndef FALSE
76 #define  FALSE 0
77 #endif
78 
79 #ifndef TRUE
80 #define TRUE 1
81 #endif
82 typedef bool OSBool;
83 
84 typedef void *lega_semaphore_t;
85 typedef void *lega_mutex_t;
86 typedef void *lega_thread_t;
87 typedef void *lega_queue_t;
88 typedef void (*timer_handler_t)(void *arg);
89 
90 typedef struct {
91     void           *handle;
92     timer_handler_t function;
93     void           *arg;
94     uint8_t        *name;
95 } lega_timer_t;
96 
97 typedef struct {
98     uint8_t        task_priority;
99     uint32_t       stack_size;
100 } lega_task_config_t;
101 
102 typedef struct {
103     uint32_t stackTop;
104     uint32_t stackBottom;
105     // add more info here
106 } lega_threadinfo_t;
107 
108 typedef uint32_t lega_thread_arg_t;
109 typedef void (*lega_thread_function_t)(lega_thread_arg_t arg);
110 
111 /** @defgroup LEGA_RTOS_Thread LEGA RTOS Thread Management Functions
112  *  @brief Provide thread creation, delete, suspend, resume, and other RTOS management API
113  *  @verbatim
114  *   LEGA thread priority table
115  *
116  * +----------+-----------------+
117  * | Priority |      Thread     |
118  * |----------|-----------------|
119  * |     0    |      LEGA       |   Highest priority
120  * |     1    |     Network     |
121  * |     2    |                 |
122  * |     3    | Network worker  |
123  * |     4    |                 |
124  * |     5    | Default Library |
125  * |          | Default worker  |
126  * |     6    |                 |
127  * |     7    |   Application   |
128  * |     8    |                 |
129  * |     9    |      Idle       |   Lowest priority
130  * +----------+-----------------+
131  *  @endverbatim
132  * @{
133  */
134 OSBool lega_rtos_is_in_interrupt_context(void);
135 
136 #define lega_rtos_declare_critical() lega_cpsr_t critical_cpsr
137 
138 /** @brief Enter a critical session, all interrupts are disabled
139   *
140   * @return    none
141   */
142 lega_cpsr_t _lega_rtos_enter_critical(void);
143 #define lega_rtos_enter_critical()           \
144     do {                                     \
145         critical_cpsr = _lega_rtos_enter_critical();  \
146     } while (0)
147 /** @brief Exit a critical session, all interrupts are enabled
148   *
149   * @return    none
150   */
151 void _lega_rtos_exit_critical(lega_cpsr_t cpsr_store);
152 #define lega_rtos_exit_critical()        \
153     do {                                 \
154         _lega_rtos_exit_critical(critical_cpsr); \
155     } while (0)
156 /**
157   * @}
158   */
159 
160 /** @brief Creates and starts a new thread
161   *
162   * @param thread     : Pointer to variable that will receive the thread handle (can be null)
163   * @param priority   : A priority number.
164   * @param name       : a text name for the thread (can be null)
165   * @param function   : the main thread function
166   * @param stack_size : stack size for this thread
167   * @param arg        : argument which will be passed to thread function
168   *
169   * @return    kNoErr          : on success.
170   * @return    kGeneralErr     : if an error occurred
171   */
172 OSStatus lega_rtos_create_thread(lega_thread_t *thread, uint8_t priority, const char *name,
173                                  lega_thread_function_t function, uint32_t stack_size, lega_thread_arg_t arg);
174 
175 /** @brief   Deletes a terminated thread
176   *
177   * @param   thread     : the handle of the thread to delete, , NULL is the current thread
178   *
179   * @return  kNoErr        : on success.
180   * @return  kGeneralErr   : if an error occurred
181   */
182 OSStatus lega_rtos_delete_thread(lega_thread_t *thread);
183 
184 /** @defgroup LEGA_RTOS_SEM LEGA RTOS Semaphore Functions
185   * @brief Provide management APIs for semaphore such as init,set,get and dinit.
186   * @{
187   */
188 
189 /** @brief    Initialises a counting semaphore and set count to 0
190   *
191   * @param    semaphore : a pointer to the semaphore handle to be initialised
192   * @param    count     : the max count number of this semaphore
193   *
194   * @return   kNoErr        : on success.
195   * @return   kGeneralErr   : if an error occurred
196   */
197 OSStatus lega_rtos_init_semaphore(lega_semaphore_t *semaphore, int count);
198 
199 /** @brief    Set (post/put/increment) a semaphore
200   *
201   * @param    semaphore : a pointer to the semaphore handle to be set
202   *
203   * @return   kNoErr        : on success.
204   * @return   kGeneralErr   : if an error occurred
205   */
206 OSStatus lega_rtos_set_semaphore(lega_semaphore_t *semaphore);
207 
208 /** @brief    Get (wait/decrement) a semaphore
209   *
210   * @Details  Attempts to get (wait/decrement) a semaphore. If semaphore is at zero already,
211   *           then the calling thread will be suspended until another thread sets the
212   *           semaphore with @ref lega_rtos_set_semaphore
213   *
214   * @param    semaphore : a pointer to the semaphore handle
215   * @param    timeout_ms: the number of milliseconds to wait before returning
216   *
217   * @return   kNoErr        : on success.
218   * @return   kGeneralErr   : if an error occurred
219   */
220 OSStatus lega_rtos_get_semaphore(lega_semaphore_t *semaphore, uint32_t timeout_ms);
221 
222 /** @brief    De-initialise a semaphore
223   *
224   * @Details  Deletes a semaphore created with @ref lega_rtos_init_semaphore
225   *
226   * @param    semaphore : a pointer to the semaphore handle
227   *
228   * @return   kNoErr        : on success.
229   * @return   kGeneralErr   : if an error occurred
230   */
231 OSStatus lega_rtos_deinit_semaphore(lega_semaphore_t *semaphore);
232 
233 /** @brief    if the task num pending by this semaphore is 0
234   *
235   * @Details  if the task num pending by this semaphore is 0
236   *
237   * @param    semaphore : a pointer to the semaphore handle
238   *
239   * @return   TRUE    : pending task num is 0
240   * @return   FALSE   : pending task num is not 0
241   */
242 OSBool lega_rtos_semaphore_pending_task_null(lega_semaphore_t *semaphore);
243 
244 /**
245   * @}
246   */
247 
248 /** @defgroup LEGA_RTOS_MUTEX LEGA RTOS Mutex Functions
249   * @brief Provide management APIs for Mutex such as init,lock,unlock and dinit.
250   * @{
251   */
252 
253 /** @brief    Initialises a mutex
254   *
255   * @Details  A mutex is different to a semaphore in that a thread that already holds
256   *           the lock on the mutex can request the lock again (nested) without causing
257   *           it to be suspended.
258   *
259   * @param    mutex : a pointer to the mutex handle to be initialised
260   *
261   * @return   kNoErr        : on success.
262   * @return   kGeneralErr   : if an error occurred
263   */
264 OSStatus lega_rtos_init_mutex(lega_mutex_t *mutex);
265 
266 /** @brief    Obtains the lock on a mutex
267   *
268   * @Details  Attempts to obtain the lock on a mutex. If the lock is already held
269   *           by another thead, the calling thread will be suspended until the mutex
270   *           lock is released by the other thread.
271   *
272   * @param    mutex : a pointer to the mutex handle to be locked
273   *
274   * @return   kNoErr        : on success.
275   * @return   kGeneralErr   : if an error occurred
276   */
277 OSStatus lega_rtos_lock_mutex(lega_mutex_t *mutex, uint32_t timeout_ms);
278 
279 /** @brief    Releases the lock on a mutex
280   *
281   * @Details  Releases a currently held lock on a mutex. If another thread
282   *           is waiting on the mutex lock, then it will be resumed.
283   *
284   * @param    mutex : a pointer to the mutex handle to be unlocked
285   *
286   * @return   kNoErr        : on success.
287   * @return   kGeneralErr   : if an error occurred
288   */
289 OSStatus lega_rtos_unlock_mutex(lega_mutex_t *mutex);
290 
291 /** @brief    De-initialise a mutex
292   *
293   * @Details  Deletes a mutex created with @ref lega_rtos_init_mutex
294   *
295   * @param    mutex : a pointer to the mutex handle
296   *
297   * @return   kNoErr        : on success.
298   * @return   kGeneralErr   : if an error occurred
299   */
300 OSStatus lega_rtos_deinit_mutex(lega_mutex_t *mutex);
301 /**
302   * @}
303   */
304 
305 /** @defgroup LEGA_RTOS_QUEUE LEGA RTOS FIFO Queue Functions
306   * @brief Provide management APIs for FIFO such as init,push,pop and dinit.
307   * @{
308   */
309 
310 /** @brief    Initialises a FIFO queue
311   *
312   * @param    queue : a pointer to the queue handle to be initialised
313   * @param    name  : a text string name for the queue (NULL is allowed)
314   * @param    message_size : size in bytes of objects that will be held in the queue
315   * @param    number_of_messages : depth of the queue - i.e. max number of objects in the queue
316   *
317   * @return   kNoErr        : on success.
318   * @return   kGeneralErr   : if an error occurred
319   */
320 OSStatus lega_rtos_init_queue(lega_queue_t *queue, const char *name, uint32_t message_size,
321                               uint32_t number_of_messages);
322 
323 /** @brief    Pushes an object onto a queue
324   *
325   * @param    queue : a pointer to the queue handle
326   * @param    message : the object to be added to the queue. Size is assumed to be
327   *                  the size specified in @ref lega_rtos_init_queue
328   * @param    timeout_ms: the number of milliseconds to wait before returning
329   *
330   * @return   kNoErr        : on success.
331   * @return   kGeneralErr   : if an error or timeout occurred
332   */
333 OSStatus lega_rtos_push_to_queue(lega_queue_t *queue, void *message, uint32_t timeout_ms);
334 
335 /** @brief    Pops an object off a queue
336   *
337   * @param    queue : a pointer to the queue handle
338   * @param    message : pointer to a buffer that will receive the object being
339   *                     popped off the queue. Size is assumed to be
340   *                     the size specified in @ref lega_rtos_init_queue , hence
341   *                     you must ensure the buffer is long enough or memory
342   *                     corruption will result
343   * @param    timeout_ms: the number of milliseconds to wait before returning
344   *
345   * @return   kNoErr        : on success.
346   * @return   kGeneralErr   : if an error or timeout occurred
347   */
348 OSStatus lega_rtos_pop_from_queue(lega_queue_t *queue, void *message, uint32_t timeout_ms);
349 
350 /** @brief    De-initialise a queue created with @ref lega_rtos_init_queue
351   *
352   * @param    queue : a pointer to the queue handle
353   *
354   * @return   kNoErr        : on success.
355   * @return   kGeneralErr   : if an error occurred
356   */
357 OSStatus lega_rtos_deinit_queue(lega_queue_t *queue);
358 
359 /** @brief    Check if a queue is empty
360   *
361   * @param    queue : a pointer to the queue handle
362   *
363   * @return   true  : queue is empty.
364   * @return   false : queue is not empty.
365   */
366 OSBool lega_rtos_is_queue_empty(lega_queue_t *queue);
367 
368 /** @brief    Check if a queue is full
369   *
370   * @param    queue : a pointer to the queue handle
371   *
372   * @return   true  : queue is empty.
373   * @return   false : queue is not empty.
374   */
375 OSBool lega_rtos_is_queue_full(lega_queue_t *queue);
376 
377 /**
378   * @}
379   */
380 
381 /** @defgroup LEGA_RTOS_TIMER LEGA RTOS Timer Functions
382   * @brief Provide management APIs for timer such as init,start,stop,reload and dinit.
383   * @{
384   */
385 
386 #if 1
387 /**
388   * @brief     Initialize a RTOS timer
389   *
390   * @note      Timer does not start running until @ref lega_start_timer is called
391   *
392   * @param     timer    : a pointer to the timer handle to be initialised
393   * @param     time_ms  : Timer period in milliseconds
394   * @param     function : the callback handler function that is called each time the
395   *                       timer expires
396   * @param     arg      : an argument that will be passed to the callback function
397   *
398   * @return    kNoErr        : on success.
399   * @return    kGeneralErr   : if an error occurred
400   */
401 #ifdef USE_TIMER_NAME
402 OSStatus lega_rtos_init_timer_name(lega_timer_t *timer, uint32_t time_ms, timer_handler_t function, void *arg,
403                                    uint8_t  *name);
404 #define lega_rtos_init_timer(timer,time_ms,function,arg) lega_rtos_init_timer_name(timer,time_ms,function,arg,__FUNCTION__)
405 #else
406 OSStatus lega_rtos_init_timer(lega_timer_t *timer, uint32_t time_ms, timer_handler_t function, void *arg);
407 #endif
408 
409 /** @brief    Starts a RTOS timer running
410   *
411   * @note     Timer must have been previously initialised with @ref lega_rtos_init_timer
412   *
413   * @param    timer    : a pointer to the timer handle to start
414   *
415   * @return   kNoErr        : on success.
416   * @return   kGeneralErr   : if an error occurred
417   */
418 OSStatus lega_rtos_start_timer(lega_timer_t *timer);
419 
420 /** @brief    Stops a running RTOS timer
421   *
422   * @note     Timer must have been previously started with @ref lega_rtos_init_timer
423   *
424   * @param    timer    : a pointer to the timer handle to stop
425   *
426   * @return   kNoErr        : on success.
427   * @return   kGeneralErr   : if an error occurred
428   */
429 OSStatus lega_rtos_stop_timer(lega_timer_t *timer);
430 
431 /** @brief    Reloads a RTOS timer that has expired
432   *
433   * @note     This is usually called in the timer callback handler, to
434   *           reschedule the timer for the next period.
435   *
436   * @param    timer    : a pointer to the timer handle to reload
437   *
438   * @return   kNoErr        : on success.
439   * @return   kGeneralErr   : if an error occurred
440   */
441 OSStatus lega_rtos_reload_timer(lega_timer_t *timer);
442 
443 /** @brief    De-initialise a RTOS timer
444   *
445   * @note     Deletes a RTOS timer created with @ref lega_rtos_init_timer
446   *
447   * @param    timer : a pointer to the RTOS timer handle
448   *
449   * @return   kNoErr        : on success.
450   * @return   kGeneralErr   : if an error occurred
451   */
452 OSStatus lega_rtos_deinit_timer(lega_timer_t *timer);
453 
454 /** @brief    Check if an RTOS timer is running
455   *
456   * @param    timer : a pointer to the RTOS timer handle
457   *
458   * @return   true        : if running.
459   * @return   false       : if not running
460   */
461 OSBool lega_rtos_is_timer_running(lega_timer_t *timer);
462 
463 /**
464   * @brief    Gets time in miiliseconds since RTOS start
465   *
466   * @note:    Since this is only 32 bits, it will roll over every 49 days, 17 hours.
467   *
468   * @returns  Time in milliseconds since RTOS started.
469   */
470 uint32_t lega_rtos_get_time(void);
471 
472 /**
473   * @}
474   */
475 
476 /** @brief    Suspend current thread for a specific time
477  *
478  * @param     num_ms : A time interval (Unit: millisecond)
479  *
480  * @return    kNoErr.
481  */
482 OSStatus lega_rtos_delay_milliseconds(uint32_t num_ms);
483 #endif
484 
485 #define lega_rtos_malloc(s) _lega_rtos_malloc(s, __FUNCTION__, __LINE__)
486 void *_lega_rtos_malloc(uint32_t xWantedSize, const char *function, uint32_t line);
487 void lega_rtos_free(void *mem);
488 
489 void lega_system_reset(void);
490 /** @brief  return the current system version
491  *  @return    system version
492  */
493 const char *lega_rtos_get_system_version(void);
494 void lega_intrpt_enter(void);
495 void lega_intrpt_exit(void);
496 uint32_t lega_rtos_get_system_ticks(void);
497 uint32_t lega_rtos_int_disable(void);
498 void lega_rtos_int_enable(uint32_t int_mask);
499 void lega_rtos_systick_reconfig(void);
500 int lega_rtos_running(void);
501 OSStatus lega_rtos_get_threadinfo(lega_thread_t *thread, lega_threadinfo_t *info);
502 
503 #endif // __LEGARTOS_H__