-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathat_rtos.h
998 lines (856 loc) · 29.4 KB
/
at_rtos.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
/**
* Copyright (c) Riven Zheng ([email protected]).
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
**/
#ifndef _AT_RTOS_H_
#define _AT_RTOS_H_
#include "k_type.h"
#include "k_struct.h"
#include "k_config.h"
#include "k_trace.h"
#include "postcode.h"
#include "static_init.h"
#ifdef OS_TYPEDEF_ENABLED
typedef _char_t char_t;
typedef _uchar_t uchar_t;
typedef _u8_t u8_t;
typedef _u16_t u16_t;
typedef _u32_t u32_t;
typedef _u64_t u64_t;
typedef _i8_t i8_t;
typedef _i16_t i16_t;
typedef _i32_t i32_t;
typedef _i64_t i64_t;
typedef _b_t b_t;
typedef _i32p_t i32p_t;
#endif
#define OS_PC_OK (PC_OS_OK)
#define OS_PC_TIMEOUT (PC_OS_WAIT_TIMEOUT)
#define OS_PC_AVAILABLE (PC_OS_WAIT_AVAILABLE)
#define OS_PC_UNAVAILABLE (PC_OS_WAIT_UNAVAILABLE)
#define OS_PC_NODATA (PC_OS_WAIT_NODATA)
#define OS_ID_INVALID (OS_INVALID_ID_VAL)
#define OS_SEM_LIMIT_BINARY (1u)
#define OS_TIME_NOWAIT (OS_TIME_NOWAIT_VAL)
#define OS_TIME_WAIT_FOREVER (OS_TIME_FOREVER_VAL)
typedef u32_t os_timeout_t;
typedef enum {
OS_TIMER_CTRL_ONCE = (TIMER_CTRL_ONCE_VAL),
OS_TIMER_CTRL_CYCLE = (TIMER_CTRL_CYCLE_VAL),
OS_TIMER_CTRL_TEMPORARY = (TIMER_CTRL_TEMPORARY_VAL),
} os_timer_ctrl_t;
typedef struct os_id os_thread_id_t;
typedef struct os_id os_timer_id_t;
typedef struct os_id os_sem_id_t;
typedef struct os_id os_mutex_id_t;
typedef struct os_id os_evt_id_t;
typedef struct os_id os_msgq_id_t;
typedef struct os_id os_pool_id_t;
typedef struct os_id os_publish_id_t;
typedef struct os_id os_subscribe_id_t;
typedef struct evt_val os_evt_val_t;
#define OS_ID_SET(p_handle, u32_value) p_handle->u32_val = (u32_value)
#define OS_PRIORITY_INVALID (OS_PRIOTITY_INVALID_LEVEL)
#define OS_PRIORITY_APPLICATION_HIGHEST (OS_PRIORITY_APPLICATION_HIGHEST_LEVEL)
#define OS_PRIORITY_APPLICATION_LOWEST (OS_PRIORITY_APPLICATION_LOWEST_LEVEL)
#define OS_PRIORITY_PREEMPT_SET(p) (p)
#define OS_PRIORITY_COOPERATION_SET(c) (-(OS_PRIOTITY_COOPERATION_NUM - (c)))
#define OS_STACK_INIT(name, size) STACK_STATIC_VALUE_DEFINE(name, size)
#define OS_THREAD_INIT(id_name, priority, stack_size, pEntryFn, pArg) INIT_OS_THREAD_DEFINE(id_name, priority, stack_size, pEntryFn, pArg)
#define OS_TIMER_INIT(id_name, pEntryFunc) INIT_OS_TIMER_DEFINE(id_name, pEntryFunc)
#define OS_SEMAPHORE_INIT(id_name, remain, limit) INIT_OS_SEMAPHORE_DEFINE(id_name, remain, limit)
#define OS_MUTEX_INIT(id_name) INIT_OS_MUTEX_DEFINE(id_name)
#define OS_EVT_INIT(id_name, anyMask, modeMask, dirMask, init) INIT_OS_EVT_DEFINE(id_name, anyMask, modeMask, dirMask, init)
#define OS_MSGQ_INIT(id_name, pBufAddr, len, num) INIT_OS_MSGQ_DEFINE(id_name, pBufAddr, len, num)
#define OS_POOL_INIT(id_name, pMemAddr, len, num) INIT_OS_POOL_DEFINE(id_name, pMemAddr, len, num)
#define OS_SUBSCRIBE_INIT(id_name, pDataAddr, size) INIT_OS_SUBSCRIBE_DEFINE(id_name, pDataAddr, size)
#define OS_PUBLISH_INIT(id_name, pDataAddr, size) INIT_OS_PUBLISH_DEFINE(id_name, pDataAddr, size)
#define OS_FUNC_PRIO_0 (INIT_LEVEL_0)
#define OS_FUNC_PRIO_1 (INIT_LEVEL_1)
#define OS_FUNC_PRIO_2 (INIT_LEVEL_2)
#define OS_FUNC_PRIO_3 (INIT_LEVEL_3)
#define OS_FUNC_PRIO_4 (INIT_LEVEL_4)
#define OS_FUNC_INIT(init_fn, prio) INIT_FUNC_DEFINE(init_fn, prio)
extern _u32_t impl_kernel_irq_disable(void);
extern void impl_kernel_irq_enable(_u32_t val);
struct foreach_item {
u8_t i;
u32_t u32_val;
};
#define OS_CRITICAL_SECTION() \
for (struct foreach_item __item = {.i = 0, .u32_val = impl_kernel_irq_disable()}; !__item.i; \
impl_kernel_irq_enable(__item.u32_val), __item.i++)
#define OS_ENTER_CRITICAL_SECTION() u32_t __val = impl_kernel_irq_disable()
#define OS_EXIT_CRITICAL_SECTION() impl_kernel_irq_enable(__val)
/**
* @brief Initialize a thread, and put it to pending list that are ready to run.
*
* @param pEntryFun The pointer of the thread entry function. Thread function must be designed to never return.
* @param pStackAddr The pointer of the thread stack address. The stack memory must be predefined and allocated in your system.
* @param stackSize The size of the the stack is base on your specified variable.
* @param priority The thread priority specified the thread's priority when the AtOS do kernel schedule.
* @param pArg The thread entry function argument.
* @param pName The thread name.
*
* @return The value of thread unique id.
*/
static inline os_thread_id_t os_thread_init(u32_t *pStackAddr, u32_t size, i16_t priority, pThread_entryFunc_t pEntryFun, void *pArg,
const char_t *pName)
{
extern _u32_t _impl_thread_init(pThread_entryFunc_t pEntryFun, _u32_t * pAddress, _u32_t size, _i16_t priority, void *pArg,
const _char_t *pName);
os_thread_id_t id = {0u};
id.u32_val = _impl_thread_init(pEntryFun, pStackAddr, size, priority, pArg, pName);
id.pName = pName;
return id;
}
/**
* @brief Add a user thread data.
*
* @param id The thread unique id.
*
* @return The result of thread data operation.
*/
static inline i32p_t os_thread_user_data_set(os_thread_id_t id, void *pUserData)
{
extern i32p_t _impl_thread_user_data_register(u32_t ctx, void *pUserData);
return (i32p_t)_impl_thread_user_data_register(id.u32_val, pUserData);
}
/**
* @brief Get a user thread data.
*
* @param id The thread unique id.
*
* @return The result of thread data operation.
*/
static inline void *os_thread_user_data_get(os_thread_id_t id)
{
extern void *_impl_thread_user_data_get(u32_t ctx);
return (void *)_impl_thread_user_data_get(id.u32_val);
}
/**
* @brief Put the current running thread into sleep mode with timeout condition.
*
* @param timeout_ms The time user defined.
*
* @return The result of thread sleep operation.
*/
static inline i32p_t os_thread_sleep(u32_t ms)
{
extern i32p_t _impl_thread_sleep(u32_t ms);
return (i32p_t)_impl_thread_sleep(ms);
}
/**
* @brief Resume a thread to run.
*
* @param id The thread unique id.
*
* @return The result of thread resume operation.
*/
static inline i32p_t os_thread_resume(os_thread_id_t id)
{
extern i32p_t _impl_thread_resume(u32_t ctx);
return (i32p_t)_impl_thread_resume(id.u32_val);
}
/**
* @brief Suspend a thread to permit another to run.
*
* @param id The thread unique id.
*
* @return The result of thread suspend operation.
*/
static inline i32p_t os_thread_suspend(os_thread_id_t id)
{
extern i32p_t _impl_thread_suspend(u32_t ctx);
return (i32p_t)_impl_thread_suspend(id.u32_val);
}
/**
* @brief Yield current thread to allow other thread to run.
*
* @param id The thread unique id.
*
* @return The result of thread yield operation.
*/
static inline i32p_t os_thread_yield(void)
{
extern i32p_t _impl_thread_yield(void);
return (i32p_t)_impl_thread_yield();
}
/**
* @brief Delete a sleep mode thread, and erase the stack data, and that can't be recovered.
*
* @param id The thread unique id.
*
* @return The result of thread delete operation.
*/
static inline i32p_t os_thread_delete(os_thread_id_t id)
{
extern i32p_t _impl_thread_delete(u32_t ctx);
return (i32p_t)_impl_thread_delete(id.u32_val);
}
/**
* @brief Delete current running thread.
*
* @return The result of thread delete operation.
*/
static inline _i32p_t os_thread_delete_self(void)
{
extern i32p_t _impl_thread_delete(u32_t ctx);
extern thread_context_t *kernel_thread_runContextGet(void);
return (i32p_t)_impl_thread_delete((u32_t)kernel_thread_runContextGet());
}
/**
* @brief Get the current running thread id.
*
* @return The running thread context.
*/
static inline os_thread_id_t os_thread_id_self(void)
{
extern thread_context_t *kernel_thread_runContextGet(void);
extern const _char_t *_impl_thread_name_get(_u32_t ctx);
os_thread_id_t id = {0u};
id.u32_val = (u32_t)kernel_thread_runContextGet();
id.pName = _impl_thread_name_get(id.u32_val);
return id;
}
/**
* @brief Idle thread callback function register.
*
* @param fn The invoke function.
*/
static inline void os_thread_idle_callback_register(const pThread_entryFunc_t loop_fn)
{
extern void _impl_kthread_idle_user_callback_register(const pThread_entryFunc_t fn);
_impl_kthread_idle_user_callback_register(loop_fn);
}
/**
* @brief Get the idle thread id.
*
* @return The value of thread unique id.
*/
static inline os_thread_id_t *os_thread_idle_id_probe(void)
{
extern os_thread_id_t *_impl_idle_thread_id_get(void);
return _impl_idle_thread_id_get();
}
/**
* @brief Add a user thread data.
*
* @param id The thread unique id.
*
* @return The result of thread data operation.
*/
static inline u32_t os_thread_stack_free_size_probe(os_thread_id_t id)
{
extern u32_t _impl_thread_stack_free_size_get(u32_t ctx);
return (u32_t)_impl_thread_stack_free_size_get(id.u32_val);
}
/**
* @brief Initialize a new timer, or allocate a temporary timer to run.
*
* @param pCallFun The timer entry function pointer.
* @param pUserData The timer callback user data.
* @param pName The timer's name, it supported NULL pointer.
*
* @return The value of the timer unique id.
*/
static inline os_timer_id_t os_timer_init(pTimer_callbackFunc_t pEntryFun, void *pUserData, const char_t *pName)
{
extern u32_t _impl_timer_init(pTimer_callbackFunc_t pCallFun, void *pUserData, const char_t *pName);
os_timer_id_t id = {0u};
id.u32_val = _impl_timer_init(pEntryFun, pUserData, pName);
id.pName = pName;
return id;
}
/**
* @brief Allocate a temporary timer to run and release it when it stops.
*
* @param pCallFun The timer entry function pointer.
* @param pUserData The timer callback user data.
* @param pName The timer's name, it supported NULL pointer.
*
* @return The value of the timer unique id.
*/
static inline os_timer_id_t os_timer_automatic(pTimer_callbackFunc_t pEntryFun, void *pUserData, const char_t *pName)
{
extern u32_t _impl_timer_automatic(pTimer_callbackFunc_t pCallFun, void *pUserData, const char_t *pName);
os_timer_id_t id = {0u};
id.u32_val = _impl_timer_automatic(pEntryFun, pUserData, pName);
id.pName = pName;
return id;
}
/**
* @brief Timer starts operation, be careful if the timer's last time isn't expired or be handled,
* the new resume will override it.
*
* @param id The timer unique id.
* @param control It defines the timer running mode.
* @param timeout_ms The timer expired time.
*
* @return The result of timer start operation.
*/
static inline i32p_t os_timer_start(os_timer_id_t id, os_timer_ctrl_t control, os_timeout_t timeout_ms)
{
extern i32p_t _impl_timer_start(u32_t ctx, u8_t control, u32_t timeout_ms);
return (i32p_t)_impl_timer_start(id.u32_val, (u8_t)control, (u32_t)timeout_ms);
}
/**
* @brief timer stops operation.
*
* @param id The timer unique id.
*
* @return The result of timer stop operation.
*/
static inline i32p_t os_timer_stop(os_timer_id_t id)
{
extern i32p_t _impl_timer_stop(u32_t ctx);
return (i32p_t)_impl_timer_stop(id.u32_val);
}
/**
* @brief Check the timer to confirm if it's already scheduled in the waiting list.
*
* @param id The timer unique id.
*
* @return The true result indicates time busy, otherwise is free status.
*/
static inline i32p_t os_timer_busy(os_timer_id_t id)
{
extern b_t _impl_timer_busy(u32_t ctx);
return (i32p_t)_impl_timer_busy(id.u32_val);
}
/**
* @brief Get the kernel RTOS system time (ms).
*
* @return The value of the total system time (ms).
*/
static inline u32_t os_timer_system_total_ms(void)
{
extern u32_t _impl_timer_total_system_ms_get(void);
return (u32_t)_impl_timer_total_system_ms_get();
}
/**
* @brief System busy wait (us).
*
* @return The result of system busy wait.
*/
static inline u32_t os_timer_system_busy_wait(u32_t us)
{
extern u32_t _impl_system_busy_wait(u32_t us);
return _impl_system_busy_wait(us);
}
/**
* @brief Initialize a new semaphore.
*
* @param remain The initial count that allows the system take.
* @param limit The maximum count that it's the semaphore's limitation.
* @param pName The semaphore name.
*
* @return The semaphore unique id.
*/
static inline os_sem_id_t os_sem_init(u8_t remain, u8_t limit, const char_t *pName)
{
extern u32_t _impl_semaphore_init(u8_t remainCount, u8_t limitCount, const char_t *pName);
os_sem_id_t id = {0u};
id.u32_val = _impl_semaphore_init(remain, limit, pName);
id.pName = pName;
return id;
}
/**
* @brief Take a semaphore count away with timeout option.
*
* @param id The semaphore unique id.
*
* @return The result of the operation.
*/
static inline i32p_t os_sem_take(os_sem_id_t id, u32_t timeout_ms)
{
extern i32p_t _impl_semaphore_take(u32_t ctx, u32_t timeout_ms);
return (i32p_t)_impl_semaphore_take(id.u32_val, timeout_ms);
}
/**
* @brief Give the semaphore to release the avaliable count.
*
* @param id The semaphore unique id.
*
* @return The result of the operation.
*/
static inline i32p_t os_sem_give(os_sem_id_t id)
{
extern i32p_t _impl_semaphore_give(u32_t ctx);
return (i32p_t)_impl_semaphore_give(id.u32_val);
}
/**
* @brief Flush the semaphore to release all the avaliable count.
*
* @param id The semaphore unique id.
*
* @return The result of the operation.
*/
static inline i32p_t os_sem_flush(os_sem_id_t id)
{
extern i32p_t _impl_semaphore_flush(u32_t ctx);
return (i32p_t)_impl_semaphore_flush(id.u32_val);
}
/**
* @brief Initialize a new mutex.
*
* @param pName The mutex name.
*
* @return The mutex unique id.
*/
static inline os_mutex_id_t os_mutex_init(const char_t *pName)
{
extern u32_t _impl_mutex_init(const char_t *pName);
os_mutex_id_t id = {0u};
id.u32_val = _impl_mutex_init(pName);
id.pName = pName;
return id;
}
/**
* @brief Mutex lock to avoid another thread access this resource.
*
* @param id The mutex unique id.
*
* @return The result of the operation.
*/
static inline i32p_t os_mutex_lock(os_mutex_id_t id)
{
extern i32p_t _impl_mutex_lock(u32_t ctx);
return (i32p_t)_impl_mutex_lock(id.u32_val);
}
/**
* @brief Mutex unlock to allow another access the resource.
*
* @param id The mutex unique id.
*
* @return The result of the operation.
*/
static inline i32p_t os_mutex_unlock(os_mutex_id_t id)
{
extern i32p_t _impl_mutex_unlock(u32_t ctx);
return (i32p_t)_impl_mutex_unlock(id.u32_val);
}
/**
* @brief Initialize a new event.
*
* @param anyMask: Changed bits always trigger = 1. otherwise, see dirMask below = 0.
* @param modeMask: Level trigger = 0, Edge trigger = 1.
* @param dirMask: Fall or Low trigger = 0, Rise or high trigger = 1.
* @param init: The init signal value.
* @param pName: The event name.
*
* @return The event unique id.
* ...
*/
static inline os_evt_id_t os_evt_init(u32_t anyMask, u32_t modeMask, u32_t dirMask, u32_t init, const char_t *pName)
{
extern u32_t _impl_event_init(u32_t anyMask, u32_t modeMask, u32_t dirMask, u32_t init, const char_t *pName);
os_msgq_id_t id = {0u};
id.u32_val = _impl_event_init(anyMask, modeMask, dirMask, init, pName);
id.pName = pName;
return id;
}
/**
* @brief Read or write the event signal value.
*
* @param id: Event unique id.
* @param pValue: The pointer of the private event value.
*
* @return The result of the operation.
*/
static inline i32p_t os_evt_value_get(os_evt_id_t id, u32_t *pValue)
{
extern i32p_t _impl_event_value_get(u32_t ctx, u32_t * pValue);
return (i32p_t)_impl_event_value_get(id.u32_val, pValue);
}
/**
* @brief Set/clear/toggle a event bits.
*
* @param id The event unique id.
* @param set The event value bits set.
* @param clear The event value bits clear.
* @param toggle The event value bits toggle.
*
* @return The result of the operation.
*/
static inline i32p_t os_evt_set(os_evt_id_t id, u32_t set, u32_t clear, u32_t toggle)
{
extern i32p_t _impl_event_set(u32_t ctx, u32_t set, u32_t clear, u32_t toggle);
return (i32p_t)_impl_event_set(id.u32_val, set, clear, toggle);
}
/**
* @brief Wait a trigger event.
*
* @param id The event unique id.
* @param pEvtData The pointer of event value.
* @param listen_mask Current thread listen which bits in the event.
* @param timeout_ms The event wait timeout setting.
*
* @return The result of the operation.
*/
static inline i32p_t os_evt_wait(os_evt_id_t id, os_evt_val_t *pEvtData, u32_t listen_mask, os_timeout_t timeout_ms)
{
extern i32p_t _impl_event_wait(u32_t ctx, struct evt_val * pEvtData, u32_t listen_mask, u32_t timeout_ms);
return (i32p_t)_impl_event_wait(id.u32_val, pEvtData, listen_mask, (u32_t)timeout_ms);
}
/**
* @brief Initialize a new queue.
*
* @param pName The queue name.
* @param pBufferAddr The pointer of the queue buffer.
* @param len The element size.
* @param num The element number.
*
* @return The queue unique id.
*/
static inline os_msgq_id_t os_msgq_init(const void *pBufferAddr, u16_t len, u16_t num, const char_t *pName)
{
extern u32_t _impl_queue_init(const void *pQueueBufferAddr, u16_t elementLen, u16_t elementNum, const char_t *pName);
os_msgq_id_t id = {0u};
id.u32_val = _impl_queue_init(pBufferAddr, len, num, pName);
id.pName = pName;
return id;
}
/**
* @brief Send a queue message.
*
* @param id The queue unique id.
* @param pUserBuffer The pointer of the message buffer address.
* @param size The queue buffer size.
* @param isToFront The direction of the message operation.
* @param timeout_ms The queue send timeout option.
*
* @return The result of the operation.
*/
static inline i32p_t os_msgq_put(os_msgq_id_t id, const u8_t *pUserBuffer, u16_t size, b_t isToFront, os_timeout_t timeout_ms)
{
extern i32p_t _impl_queue_send(u32_t ctx, const u8_t *pUserBuffer, u16_t bufferSize, b_t isToFront, u32_t timeout_ms);
return (i32p_t)_impl_queue_send(id.u32_val, pUserBuffer, size, isToFront, (u32_t)timeout_ms);
}
/**
* @brief Receive a queue message.
*
* @param id The queue unique id.
* @param pUserBuffer The pointer of the message buffer address.
* @param size The queue buffer size.
* @param isFromBack The direction of the message operation.
* @param timeout_ms The queue send timeout option.
*
* @return The result of the operation.
*/
static inline i32p_t os_msgq_get(os_msgq_id_t id, const u8_t *pUserBuffer, u16_t size, b_t isFromBack, os_timeout_t timeout_ms)
{
extern i32p_t _impl_queue_receive(u32_t ctx, const u8_t *pUserBuffer, u16_t bufferSize, b_t isFromBack, u32_t timeout_ms);
return (i32p_t)_impl_queue_receive(id.u32_val, pUserBuffer, size, isFromBack, (u32_t)timeout_ms);
}
/**
* @brief Get the received message number.
*
* @param ctx The queue unique id.
*
* @return The result of the operation.
*/
static inline u32_t os_msgq_num_probe(os_msgq_id_t id)
{
extern u32_t _impl_queue_num_probe(u32_t ctx);
return (u32_t)_impl_queue_num_probe(id.u32_val);
}
/**
* @brief Initialize a new pool.
*
* @param pName The pool name.
* @param pMemAddr The pointer of the pool buffer.
* @param size The element size.
* @param num The element number.
*
* @return The pool unique id.
*/
static inline os_pool_id_t os_pool_init(const void *pMemAddr, u16_t size, u16_t num, const char_t *pName)
{
extern u32_t _impl_pool_init(const void *pMemAddr, u16_t elementLen, u16_t elementNum, const char_t *pName);
os_pool_id_t id = {0u};
id.u32_val = _impl_pool_init(pMemAddr, size, num, pName);
id.pName = pName;
return id;
}
/**
* @brief Take a message pool resource.
*
* @param id The pool unique id.
* @param ppUserMem The dual pointer of the message memory address.
* @param size The pointer of the message memory size.
* @param timeout_ms The pool take timeout option.
*
* @return The result of the operation.
*/
static inline i32p_t os_pool_take(os_pool_id_t id, void **ppUserMem, u16_t size, os_timeout_t timeout_ms)
{
extern i32p_t _impl_pool_take(u32_t ctx, void **ppUserBuffer, u16_t bufferSize, u32_t timeout_ms);
return (i32p_t)_impl_pool_take(id.u32_val, ppUserMem, size, (u32_t)timeout_ms);
}
/**
* @brief Release memory pool.
*
* @param id The pool unique id.
* @param ppUserMem The dual pointer of the message memory address.
*
* @return The result of the operation.
*/
static inline i32p_t os_pool_release(os_pool_id_t id, void **ppUserMem)
{
extern i32p_t _impl_pool_release(u32_t ctx, void **ppUserBuffer);
return (i32p_t)_impl_pool_release(id.u32_val, ppUserMem);
}
/**
* @brief Initialize a new publish.
*
* @param pName The publish name.
*
* @return The publish unique id.
*/
static inline os_publish_id_t os_publish_init(const char_t *pName)
{
extern u32_t _impl_publish_init(const char_t *pName);
os_publish_id_t id = {0u};
id.u32_val = _impl_publish_init(pName);
id.pName = pName;
return id;
}
/**
* @brief Publisher Submits the report data.
*
* @param id The publish unique id.
* @param pData The pointer of the data buffer address.
* @param size The data buffer size.
*
* @return Value The result of the publisher data operation.
*/
static inline i32p_t os_publish_data_submit(os_publish_id_t id, const void *pData, u16_t size)
{
extern i32p_t _impl_publish_data_submit(u32_t pub_ctx, const void *pPublishData, u16_t publishSize);
return _impl_publish_data_submit(id.u32_val, pData, size);
}
/**
* @brief Initialize a new subscribe.
*
* @param pDataAddr The pointer of the data buffer address.
* @param size The data buffer size.
* @param pName The subscribe name.
*
* @return Value The result fo subscribe init operation.
*/
static inline os_subscribe_id_t os_subscribe_init(void *pDataAddr, u16_t size, const char_t *pName)
{
extern u32_t _impl_subscribe_init(void *pDataAddr, u16_t dataSize, const char_t *pName);
os_subscribe_id_t id = {0u};
id.u32_val = _impl_subscribe_init(pDataAddr, size, pName);
id.pName = pName;
return id;
}
/**
* @brief To check if the publisher submits new data and that is not applied by subscriber.
*
* @param subscribe_id The subscribe unique id.
*
* @return Value The result of subscribe data is ready.
*/
static inline b_t os_subscribe_data_is_ready(os_subscribe_id_t id)
{
extern b_t _impl_subscribe_data_is_ready(u32_t sub_ctx);
return _impl_subscribe_data_is_ready(id.u32_val);
}
/**
* @brief The subscribe register the corresponding publish.
*
* @param subscribe_id The subscribe unique id.
* @param publish_id The publish unique id.
* @param isMute The set of notification operation.
* @param pFuncHandler The notification function handler pointer.
*
* @return Value The result fo subscribe init operation.
*/
static inline i32p_t os_subscribe_register(os_subscribe_id_t subscribe_id, os_publish_id_t publish_id, b_t isMute,
pSubscribe_callbackFunc_t pNotificationHandler)
{
extern i32p_t _impl_subscribe_register(u32_t sub_ctx, u32_t pub_ctx, b_t isMute, pSubscribe_callbackFunc_t pNotificationHandler);
return _impl_subscribe_register(subscribe_id.u32_val, publish_id.u32_val, isMute, pNotificationHandler);
}
/**
* @brief The subscriber wait publisher put new data with a timeout option.
*
* @param subscribe_id The subscribe unique id.
* @param pData The pointer of data buffer.
* @param pDataLen The pointer of data buffer len.
*
* @return Value The result of subscribe init operation.
*/
static inline i32p_t os_subscribe_data_apply(os_subscribe_id_t subscribe_id, void *pData, u16_t *pDataLen)
{
extern i32p_t _impl_subscribe_data_apply(u32_t sub_ctx, void *pDataBuffer, u16_t *pDataLen);
return _impl_subscribe_data_apply(subscribe_id.u32_val, pData, pDataLen);
}
/**
* @brief Check if the thread unique id if is's invalid.
*
* @param id The provided unique id.
*
* @return The value of true is invalid, otherwise is valid.
*/
static inline b_t os_id_is_invalid(struct os_id id)
{
return (b_t)kernel_os_id_is_invalid(id);
}
/**
* @brief The kernel OS start to run.
*/
static inline i32p_t os_kernel_run(void)
{
extern i32p_t _impl_kernel_at_rtos_run(void);
return _impl_kernel_at_rtos_run();
}
/**
* @brief To check if the kernel OS is running.
*
* return The true indicates the kernel OS is running.
*/
static inline b_t os_kernel_is_running(void)
{
extern b_t _impl_kernel_rtos_isRun(void);
return (b_t)(_impl_kernel_rtos_isRun() ? (TRUE) : (FALSE));
}
/**
* @brief Force kernel schedule stop.
*/
static inline void os_kernel_lock(void)
{
extern void _impl_kernel_schedule_lock(void);
_impl_kernel_schedule_lock();
}
/**
* @brief Kernel schedule recovery.
*/
static inline void os_kernel_unlock(void)
{
extern void _impl_kernel_schedule_unlock(void);
_impl_kernel_schedule_unlock();
}
/**
* @brief Trace At-RTOS firmware version.
*
* @return The value of firmware version number.
*/
static inline void os_trace_firmware_version(void)
{
u32_t _impl_trace_firmware_version_get();
}
/**
* @brief Trace At-RTOS failed postcode callback function register.
*
* @param fn The invoke function.
*/
static inline void os_trace_postcode_callback_register(const pTrace_postcodeFunc_t fn)
{
_impl_trace_postcode_callback_register(fn);
}
/**
* @brief Trace At-RTOS failed postcode value.
*
* @param fn The invoke function.
*
* @return The value of true is failed, otherwise is pass.
*/
static inline b_t os_trace_failed_postcode(const pTrace_postcodeFunc_t fn)
{
return _impl_trace_postcode_failed_get(fn);
}
/**
* @brief Trace At-RTOS each thread context.
*
* @param fn The invoke function.
*/
static inline void os_trace_foreach_thread(const pTrace_threadFunc_t fn)
{
_impl_trace_thread(fn);
}
/**
* @brief Trace At-RTOS kernel thread time usage.
*
* @param fn The invoke function.
*/
static inline void os_trace_analyze(const pTrace_analyzeFunc_t fn)
{
_impl_trace_analyze(fn);
}
/**
* @brief Force kernel object free, must to confirm no thread blocking in this object
*
* @param id The kernel object unique id.
*/
static inline void os_object_free_force(struct os_id id)
{
extern void _impl_kernel_object_free(u32_t ctx);
_impl_kernel_object_free(id.u32_val);
}
/* It defined the AtOS extern symbol for convenience use, but it has extra memory consumption */
#ifdef OS_API_ENABLED
typedef struct {
os_thread_id_t (*thread_init)(u32_t *, u32_t, i16_t, pThread_entryFunc_t, void *, const char_t *);
i32p_t (*thread_sleep)(u32_t);
i32p_t (*thread_resume)(os_thread_id_t);
i32p_t (*thread_suspend)(os_thread_id_t);
i32p_t (*thread_yield)(void);
i32p_t (*thread_delete)(os_thread_id_t);
i32p_t (*thread_delete_self)(void);
os_thread_id_t (*thread_id_self)(void);
i32p_t (*thread_user_data_set)(os_thread_id_t, void *);
void *(*thread_user_data_get)(os_thread_id_t);
void (*thread_idle_fn_register)(const pThread_entryFunc_t);
os_thread_id_t *(*thread_idle_id_probe)(void);
u32_t (*thread_stack_free_size_probe)(os_thread_id_t);
os_timer_id_t (*timer_init)(pTimer_callbackFunc_t, void *, const char_t *);
os_timer_id_t (*timer_automatic)(pTimer_callbackFunc_t, void *, const char_t *);
i32p_t (*timer_start)(os_timer_id_t, os_timer_ctrl_t, os_timeout_t);
i32p_t (*timer_stop)(os_timer_id_t);
i32p_t (*timer_busy)(os_timer_id_t);
u32_t (*timer_system_total_ms)(void);
u32_t (*timer_system_busy_wait)(u32_t);
os_sem_id_t (*sem_init)(u8_t, u8_t, const char_t *);
i32p_t (*sem_take)(os_sem_id_t, os_timeout_t);
i32p_t (*sem_give)(os_sem_id_t);
i32p_t (*sem_flush)(os_sem_id_t);
os_mutex_id_t (*mutex_init)(const char_t *);
i32p_t (*mutex_lock)(os_mutex_id_t);
i32p_t (*mutex_unlock)(os_mutex_id_t);
os_evt_id_t (*evt_init)(u32_t, u32_t, u32_t, u32_t, const char_t *);
i32p_t (*evt_set)(os_evt_id_t, u32_t, u32_t, u32_t);
i32p_t (*evt_wait)(os_evt_id_t, os_evt_val_t *, u32_t, os_timeout_t);
os_msgq_id_t (*msgq_init)(const void *, u16_t, u16_t, const char_t *);
i32p_t (*msgq_put)(os_msgq_id_t, const u8_t *, u16_t, b_t, os_timeout_t);
i32p_t (*msgq_get)(os_msgq_id_t, const u8_t *, u16_t, b_t, os_timeout_t);
u32_t (*msgq_num_probe)(os_msgq_id_t);
os_pool_id_t (*pool_init)(const void *, u16_t, u16_t, const char_t *);
i32p_t (*pool_take)(os_pool_id_t, void **, u16_t, os_timeout_t);
i32p_t (*pool_release)(os_pool_id_t, void **);
os_publish_id_t (*publish_init)(const char_t *);
i32p_t (*publish_data_submit)(os_publish_id_t, const void *, u16_t);
os_subscribe_id_t (*subscribe_init)(void *, u16_t, const char_t *);
i32p_t (*subscribe_register)(os_subscribe_id_t, os_publish_id_t, b_t, pSubscribe_callbackFunc_t);
i32p_t (*subscribe_data_apply)(os_subscribe_id_t, void *, u16_t *);
b_t (*subscribe_data_is_ready)(os_subscribe_id_t);
b_t (*id_isInvalid)(struct os_id);
i32p_t (*schedule_run)(void);
b_t (*schedule_is_running)(void);
void (*schedule_lock)(void);
void (*schedule_unlock)(void);
void (*trace_versison)(void);
void (*trace_postcode_fn_register)(const pTrace_postcodeFunc_t);
b_t (*trace_postcode)(const pTrace_postcodeFunc_t);
void (*trace_thread)(const pTrace_threadFunc_t);
void (*trace_time)(const pTrace_analyzeFunc_t);
void (*object_free)(struct os_id);
} at_rtos_api_t;
extern const at_rtos_api_t os;
#endif
#endif /* _AT_RTOS_H_ */