-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmsis_os2.c
2957 lines (2518 loc) · 69.5 KB
/
cmsis_os2.c
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
999
1000
/* --------------------------------------------------------------------------
* Copyright (c) 2013-2022 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Name: cmsis_os2.c
* Purpose: CMSIS RTOS2 wrapper for FreeRTOS
*
*---------------------------------------------------------------------------*/
#include <string.h>
#include "FreeRTOS.h" // ARM.FreeRTOS::RTOS:Core
#include "task.h" // ARM.FreeRTOS::RTOS:Core
#include "event_groups.h" // ARM.FreeRTOS::RTOS:Event Groups
#include "semphr.h" // ARM.FreeRTOS::RTOS:Core
#include "timers.h" // ARM.FreeRTOS::RTOS:Timers
#include "freertos_mpool.h" // osMemoryPool definitions
#include "freertos_os2.h" // Configuration check and setup
#include "cmsis_os2.h" // ::CMSIS:RTOS2
#include "cmsis_compiler.h" // Compiler agnostic definitions
/*---------------------------------------------------------------------------*/
#ifndef __ARM_ARCH_6M__
#define __ARM_ARCH_6M__ 0
#endif
#ifndef __ARM_ARCH_7M__
#define __ARM_ARCH_7M__ 0
#endif
#ifndef __ARM_ARCH_7EM__
#define __ARM_ARCH_7EM__ 0
#endif
#ifndef __ARM_ARCH_8M_MAIN__
#define __ARM_ARCH_8M_MAIN__ 0
#endif
#ifndef __ARM_ARCH_7A__
#define __ARM_ARCH_7A__ 0
#endif
#if ((__ARM_ARCH_7M__ == 1U) || \
(__ARM_ARCH_7EM__ == 1U) || \
(__ARM_ARCH_8M_MAIN__ == 1U))
#define IS_IRQ_MASKED() ((__get_PRIMASK() != 0U) || (__get_BASEPRI() != 0U))
#elif (__ARM_ARCH_6M__ == 1U)
#define IS_IRQ_MASKED() (__get_PRIMASK() != 0U)
#elif (__ARM_ARCH_7A__ == 1U)
/* CPSR mask bits */
#define CPSR_MASKBIT_I 0x80U
#define IS_IRQ_MASKED() ((__get_CPSR() & CPSR_MASKBIT_I) != 0U)
#else
#define IS_IRQ_MASKED() (__get_PRIMASK() != 0U)
#endif
#if (__ARM_ARCH_7A__ == 1U)
/* CPSR mode bitmasks */
#define CPSR_MODE_USER 0x10U
#define CPSR_MODE_SYSTEM 0x1FU
#define IS_IRQ_MODE() ((__get_mode() != CPSR_MODE_USER) && (__get_mode() != CPSR_MODE_SYSTEM))
#else
#define IS_IRQ_MODE() (__get_IPSR() != 0U)
#endif
/* Limits */
#define MAX_BITS_TASK_NOTIFY 31U
#define MAX_BITS_EVENT_GROUPS 24U
#define THREAD_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_TASK_NOTIFY) - 1U))
#define EVENT_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_EVENT_GROUPS) - 1U))
/* Kernel version and identification string definition (major.minor.rev: mmnnnrrrr dec) */
#define KERNEL_VERSION (((uint32_t)tskKERNEL_VERSION_MAJOR * 10000000UL) | \
((uint32_t)tskKERNEL_VERSION_MINOR * 10000UL) | \
((uint32_t)tskKERNEL_VERSION_BUILD * 1UL))
#define KERNEL_ID ("FreeRTOS " tskKERNEL_VERSION_NUMBER)
/* Timer callback information structure definition */
typedef struct {
osTimerFunc_t func;
void *arg;
} TimerCallback_t;
/* Kernel initialization state */
static osKernelState_t KernelState = osKernelInactive;
/* Get OS Tick count value */
static uint32_t OS_Tick_GetCount (void);
#if (configUSE_TICKLESS_IDLE == 0)
/* Get OS Tick overflow status */
static uint32_t OS_Tick_GetOverflow (void);
#endif
/* Get OS Tick interval */
static uint32_t OS_Tick_GetInterval (void);
/*
Heap region definition used by heap_5 variant
Define configAPPLICATION_ALLOCATED_HEAP as nonzero value in FreeRTOSConfig.h if
heap regions are already defined and vPortDefineHeapRegions is called in application.
Otherwise vPortDefineHeapRegions will be called by osKernelInitialize using
definition configHEAP_5_REGIONS as parameter. Overriding configHEAP_5_REGIONS
is possible by defining it globally or in FreeRTOSConfig.h.
*/
#if defined(USE_FreeRTOS_HEAP_5)
#if (configAPPLICATION_ALLOCATED_HEAP == 0)
/*
FreeRTOS heap is not defined by the application.
Single region of size configTOTAL_HEAP_SIZE (defined in FreeRTOSConfig.h)
is provided by default. Define configHEAP_5_REGIONS to provide custom
HeapRegion_t array.
*/
#define HEAP_5_REGION_SETUP 1
#ifndef configHEAP_5_REGIONS
#define configHEAP_5_REGIONS xHeapRegions
static uint8_t ucHeap[configTOTAL_HEAP_SIZE];
static HeapRegion_t xHeapRegions[] = {
{ ucHeap, configTOTAL_HEAP_SIZE },
{ NULL, 0 }
};
#else
/* Global definition is provided to override default heap array */
extern HeapRegion_t configHEAP_5_REGIONS[];
#endif
#else
/*
The application already defined the array used for the FreeRTOS heap and
called vPortDefineHeapRegions to initialize heap.
*/
#define HEAP_5_REGION_SETUP 0
#endif /* configAPPLICATION_ALLOCATED_HEAP */
#endif /* USE_FreeRTOS_HEAP_5 */
#if defined(SysTick)
#undef SysTick_Handler
/* CMSIS SysTick interrupt handler prototype */
extern void SysTick_Handler (void);
/* FreeRTOS tick timer interrupt handler prototype */
extern void xPortSysTickHandler (void);
/*
SysTick handler implementation that also clears overflow flag.
*/
#if (USE_CUSTOM_SYSTICK_HANDLER_IMPLEMENTATION == 0)
void SysTick_Handler (void) {
#if (configUSE_TICKLESS_IDLE == 0)
/* Clear overflow flag */
SysTick->CTRL;
#endif
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
/* Call tick handler */
xPortSysTickHandler();
}
}
#endif
#endif /* SysTick */
/*
Setup SVC to reset value.
*/
__STATIC_INLINE void SVC_Setup (void) {
#if (__ARM_ARCH_7A__ == 0U)
/* Service Call interrupt might be configured before kernel start */
/* and when its priority is lower or equal to BASEPRI, svc instruction */
/* causes a Hard Fault. */
NVIC_SetPriority (SVCall_IRQn, 0U);
#endif
}
/*
Function macro used to retrieve semaphore count from ISR
*/
#ifndef uxSemaphoreGetCountFromISR
#define uxSemaphoreGetCountFromISR( xSemaphore ) uxQueueMessagesWaitingFromISR( ( QueueHandle_t ) ( xSemaphore ) )
#endif
/*
Determine if CPU executes from interrupt context or if interrupts are masked.
*/
__STATIC_INLINE uint32_t IRQ_Context (void) {
uint32_t irq;
BaseType_t state;
irq = 0U;
if (IS_IRQ_MODE()) {
/* Called from interrupt context */
irq = 1U;
}
else {
/* Get FreeRTOS scheduler state */
state = xTaskGetSchedulerState();
if (state != taskSCHEDULER_NOT_STARTED) {
/* Scheduler was started */
if (IS_IRQ_MASKED()) {
/* Interrupts are masked */
irq = 1U;
}
}
}
/* Return context, 0: thread context, 1: IRQ context */
return (irq);
}
/* Get OS Tick count value */
static uint32_t OS_Tick_GetCount (void) {
#if (__ARM_ARCH_7A__ == 1U)
return (__get_CNTFRQ() - PL1_GetCurrentValue());
#else
uint32_t load = SysTick->LOAD;
return (load - SysTick->VAL);
#endif /* __ARM_ARCH_7A__ */
}
#if (configUSE_TICKLESS_IDLE == 0)
/* Get OS Tick overflow status */
static uint32_t OS_Tick_GetOverflow (void) {
#if (__ARM_ARCH_7A__ == 1U)
CNTP_CTL_Type cntp_ctl;
cntp_ctl.w = PL1_GetControl();
return (cntp_ctl.b.ISTATUS);
#else
return ((SysTick->CTRL >> 16) & 1U);
#endif /* __ARM_ARCH_7A__ */
}
#endif
/* Get OS Tick interval */
static uint32_t OS_Tick_GetInterval (void) {
#if (__ARM_ARCH_7A__ == 1U)
return (__get_CNTFRQ() + 1U);
#else
return (SysTick->LOAD + 1U);
#endif /* __ARM_ARCH_7A__ */
}
/* ==== Kernel Management Functions ==== */
/*
Initialize the RTOS Kernel.
*/
osStatus_t osKernelInitialize (void) {
osStatus_t stat;
BaseType_t state;
if (IRQ_Context() != 0U) {
stat = osErrorISR;
}
else {
state = xTaskGetSchedulerState();
/* Initialize if scheduler not started and not initialized before */
if ((state == taskSCHEDULER_NOT_STARTED) && (KernelState == osKernelInactive)) {
#if defined(USE_TRACE_EVENT_RECORDER)
/* Initialize the trace macro debugging output channel */
EvrFreeRTOSSetup(0U);
#endif
#if defined(USE_FreeRTOS_HEAP_5) && (HEAP_5_REGION_SETUP == 1)
/* Initialize the memory regions when using heap_5 variant */
vPortDefineHeapRegions (configHEAP_5_REGIONS);
#endif
KernelState = osKernelReady;
stat = osOK;
} else {
stat = osError;
}
}
/* Return execution status */
return (stat);
}
/*
Get RTOS Kernel Information.
*/
osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size) {
if (version != NULL) {
/* Version encoding is major.minor.rev: mmnnnrrrr dec */
version->api = KERNEL_VERSION;
version->kernel = KERNEL_VERSION;
}
if ((id_buf != NULL) && (id_size != 0U)) {
/* Buffer for retrieving identification string is provided */
if (id_size > sizeof(KERNEL_ID)) {
id_size = sizeof(KERNEL_ID);
}
/* Copy kernel identification string into provided buffer */
memcpy(id_buf, KERNEL_ID, id_size);
}
/* Return execution status */
return (osOK);
}
/*
Get the current RTOS Kernel state.
*/
osKernelState_t osKernelGetState (void) {
osKernelState_t state;
switch (xTaskGetSchedulerState()) {
case taskSCHEDULER_RUNNING:
state = osKernelRunning;
break;
case taskSCHEDULER_SUSPENDED:
state = osKernelLocked;
break;
case taskSCHEDULER_NOT_STARTED:
default:
if (KernelState == osKernelReady) {
/* Ready, osKernelInitialize was already called */
state = osKernelReady;
} else {
/* Not initialized */
state = osKernelInactive;
}
break;
}
/* Return current state */
return (state);
}
/*
Start the RTOS Kernel scheduler.
*/
osStatus_t osKernelStart (void) {
osStatus_t stat;
BaseType_t state;
if (IRQ_Context() != 0U) {
stat = osErrorISR;
}
else {
state = xTaskGetSchedulerState();
/* Start scheduler if initialized and not started before */
if ((state == taskSCHEDULER_NOT_STARTED) && (KernelState == osKernelReady)) {
/* Ensure SVC priority is at the reset value */
SVC_Setup();
/* Change state to ensure correct API flow */
KernelState = osKernelRunning;
/* Start the kernel scheduler */
vTaskStartScheduler();
stat = osOK;
} else {
stat = osError;
}
}
/* Return execution status */
return (stat);
}
/*
Lock the RTOS Kernel scheduler.
*/
int32_t osKernelLock (void) {
int32_t lock;
if (IRQ_Context() != 0U) {
lock = (int32_t)osErrorISR;
}
else {
switch (xTaskGetSchedulerState()) {
case taskSCHEDULER_SUSPENDED:
lock = 1;
break;
case taskSCHEDULER_RUNNING:
vTaskSuspendAll();
lock = 0;
break;
case taskSCHEDULER_NOT_STARTED:
default:
lock = (int32_t)osError;
break;
}
}
/* Return previous lock state */
return (lock);
}
/*
Unlock the RTOS Kernel scheduler.
*/
int32_t osKernelUnlock (void) {
int32_t lock;
if (IRQ_Context() != 0U) {
lock = (int32_t)osErrorISR;
}
else {
switch (xTaskGetSchedulerState()) {
case taskSCHEDULER_SUSPENDED:
lock = 1;
if (xTaskResumeAll() != pdTRUE) {
if (xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED) {
lock = (int32_t)osError;
}
}
break;
case taskSCHEDULER_RUNNING:
lock = 0;
break;
case taskSCHEDULER_NOT_STARTED:
default:
lock = (int32_t)osError;
break;
}
}
/* Return previous lock state */
return (lock);
}
/*
Restore the RTOS Kernel scheduler lock state.
*/
int32_t osKernelRestoreLock (int32_t lock) {
if (IRQ_Context() != 0U) {
lock = (int32_t)osErrorISR;
}
else {
switch (xTaskGetSchedulerState()) {
case taskSCHEDULER_SUSPENDED:
case taskSCHEDULER_RUNNING:
if (lock == 1) {
vTaskSuspendAll();
}
else {
if (lock != 0) {
lock = (int32_t)osError;
}
else {
if (xTaskResumeAll() != pdTRUE) {
if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
lock = (int32_t)osError;
}
}
}
}
break;
case taskSCHEDULER_NOT_STARTED:
default:
lock = (int32_t)osError;
break;
}
}
/* Return new lock state */
return (lock);
}
/*
Get the RTOS kernel tick count.
*/
uint32_t osKernelGetTickCount (void) {
TickType_t ticks;
if (IRQ_Context() != 0U) {
ticks = xTaskGetTickCountFromISR();
} else {
ticks = xTaskGetTickCount();
}
/* Return kernel tick count */
return (ticks);
}
/*
Get the RTOS kernel tick frequency.
*/
uint32_t osKernelGetTickFreq (void) {
/* Return frequency in hertz */
return (configTICK_RATE_HZ);
}
/*
Get the RTOS kernel system timer count.
*/
uint32_t osKernelGetSysTimerCount (void) {
uint32_t irqmask = IS_IRQ_MASKED();
TickType_t ticks;
uint32_t val;
#if (configUSE_TICKLESS_IDLE != 0)
uint32_t val0;
/* Low Power Tickless Idle controls timer overflow flag and therefore */
/* OS_Tick_GetOverflow may be non-functional. As a workaround a reference */
/* time is measured here before disabling interrupts. Timer value overflow */
/* is then checked by comparing reference against latest time measurement. */
/* Timer count value returned by this method is less accurate but if an */
/* overflow is missed, an invalid timer count would be returned. */
val0 = OS_Tick_GetCount();
#endif
__disable_irq();
ticks = xTaskGetTickCount();
val = OS_Tick_GetCount();
/* Update tick count and timer value when timer overflows */
#if (configUSE_TICKLESS_IDLE != 0)
if (val < val0) {
ticks++;
}
#else
if (OS_Tick_GetOverflow() != 0U) {
val = OS_Tick_GetCount();
ticks++;
}
#endif
val += ticks * OS_Tick_GetInterval();
if (irqmask == 0U) {
__enable_irq();
}
/* Return system timer count */
return (val);
}
/*
Get the RTOS kernel system timer frequency.
*/
uint32_t osKernelGetSysTimerFreq (void) {
/* Return frequency in hertz */
return (configCPU_CLOCK_HZ);
}
/* ==== Thread Management Functions ==== */
/*
Create a thread and add it to Active Threads.
Limitations:
- The memory for control block and stack must be provided in the osThreadAttr_t
structure in order to allocate object statically.
- Attribute osThreadJoinable is not supported, NULL is returned if used.
*/
osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
const char *name;
uint32_t stack;
TaskHandle_t hTask;
UBaseType_t prio;
int32_t mem;
hTask = NULL;
if ((IRQ_Context() == 0U) && (func != NULL)) {
stack = configMINIMAL_STACK_SIZE;
prio = (UBaseType_t)osPriorityNormal;
name = NULL;
mem = -1;
if (attr != NULL) {
if (attr->name != NULL) {
name = attr->name;
}
if (attr->priority != osPriorityNone) {
prio = (UBaseType_t)attr->priority;
}
if ((prio < osPriorityIdle) || (prio > osPriorityISR) || ((attr->attr_bits & osThreadJoinable) == osThreadJoinable)) {
/* Invalid priority or unsupported osThreadJoinable attribute used */
return (NULL);
}
if (attr->stack_size > 0U) {
/* In FreeRTOS stack is not in bytes, but in sizeof(StackType_t) which is 4 on ARM ports. */
/* Stack size should be therefore 4 byte aligned in order to avoid division caused side effects */
stack = attr->stack_size / sizeof(StackType_t);
}
if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticTask_t)) &&
(attr->stack_mem != NULL) && (attr->stack_size > 0U)) {
/* The memory for control block and stack is provided, use static object */
mem = 1;
}
else {
if ((attr->cb_mem == NULL) && (attr->cb_size == 0U) && (attr->stack_mem == NULL)) {
/* Control block and stack memory will be allocated from the dynamic pool */
mem = 0;
}
}
}
else {
mem = 0;
}
if (mem == 1) {
#if (configSUPPORT_STATIC_ALLOCATION == 1)
hTask = xTaskCreateStatic ((TaskFunction_t)func, name, stack, argument, prio, (StackType_t *)attr->stack_mem,
(StaticTask_t *)attr->cb_mem);
#endif
}
else {
if (mem == 0) {
#if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
if (xTaskCreate ((TaskFunction_t)func, name, (configSTACK_DEPTH_TYPE)stack, argument, prio, &hTask) != pdPASS) {
hTask = NULL;
}
#endif
}
}
}
/* Return thread ID */
return ((osThreadId_t)hTask);
}
/*
Get name of a thread.
*/
const char *osThreadGetName (osThreadId_t thread_id) {
TaskHandle_t hTask = (TaskHandle_t)thread_id;
const char *name;
if ((IRQ_Context() != 0U) || (hTask == NULL)) {
name = NULL;
} else {
name = pcTaskGetName (hTask);
}
/* Return name as null-terminated string */
return (name);
}
/*
Return the thread ID of the current running thread.
*/
osThreadId_t osThreadGetId (void) {
osThreadId_t id;
id = (osThreadId_t)xTaskGetCurrentTaskHandle();
/* Return thread ID */
return (id);
}
/*
Get current thread state of a thread.
*/
osThreadState_t osThreadGetState (osThreadId_t thread_id) {
TaskHandle_t hTask = (TaskHandle_t)thread_id;
osThreadState_t state;
if ((IRQ_Context() != 0U) || (hTask == NULL)) {
state = osThreadError;
}
else {
switch (eTaskGetState (hTask)) {
case eRunning: state = osThreadRunning; break;
case eReady: state = osThreadReady; break;
case eBlocked:
case eSuspended: state = osThreadBlocked; break;
case eDeleted:
case eInvalid:
default: state = osThreadError; break;
}
}
/* Return current thread state */
return (state);
}
/*
Get available stack space of a thread based on stack watermark recording during execution.
*/
uint32_t osThreadGetStackSpace (osThreadId_t thread_id) {
TaskHandle_t hTask = (TaskHandle_t)thread_id;
uint32_t sz;
if ((IRQ_Context() != 0U) || (hTask == NULL)) {
sz = 0U;
} else {
sz = (uint32_t)(uxTaskGetStackHighWaterMark(hTask) * sizeof(StackType_t));
}
/* Return remaining stack space in bytes */
return (sz);
}
/*
Change priority of a thread.
*/
osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
TaskHandle_t hTask = (TaskHandle_t)thread_id;
osStatus_t stat;
if (IRQ_Context() != 0U) {
stat = osErrorISR;
}
else if ((hTask == NULL) || (priority < osPriorityIdle) || (priority > osPriorityISR)) {
stat = osErrorParameter;
}
else {
stat = osOK;
vTaskPrioritySet (hTask, (UBaseType_t)priority);
}
/* Return execution status */
return (stat);
}
/*
Get current priority of a thread.
*/
osPriority_t osThreadGetPriority (osThreadId_t thread_id) {
TaskHandle_t hTask = (TaskHandle_t)thread_id;
osPriority_t prio;
if ((IRQ_Context() != 0U) || (hTask == NULL)) {
prio = osPriorityError;
} else {
prio = (osPriority_t)((int32_t)uxTaskPriorityGet (hTask));
}
/* Return current thread priority */
return (prio);
}
/*
Pass control to next thread that is in state READY.
*/
osStatus_t osThreadYield (void) {
osStatus_t stat;
if (IRQ_Context() != 0U) {
stat = osErrorISR;
} else {
stat = osOK;
taskYIELD();
}
/* Return execution status */
return (stat);
}
#if (configUSE_OS2_THREAD_SUSPEND_RESUME == 1)
/*
Suspend execution of a thread.
*/
osStatus_t osThreadSuspend (osThreadId_t thread_id) {
TaskHandle_t hTask = (TaskHandle_t)thread_id;
osStatus_t stat;
if (IRQ_Context() != 0U) {
stat = osErrorISR;
}
else if (hTask == NULL) {
stat = osErrorParameter;
}
else {
stat = osOK;
vTaskSuspend (hTask);
}
/* Return execution status */
return (stat);
}
/*
Resume execution of a thread.
*/
osStatus_t osThreadResume (osThreadId_t thread_id) {
TaskHandle_t hTask = (TaskHandle_t)thread_id;
osStatus_t stat;
if (IRQ_Context() != 0U) {
stat = osErrorISR;
}
else if (hTask == NULL) {
stat = osErrorParameter;
}
else {
stat = osOK;
vTaskResume (hTask);
}
/* Return execution status */
return (stat);
}
#endif /* (configUSE_OS2_THREAD_SUSPEND_RESUME == 1) */
/*
Terminate execution of current running thread.
*/
__NO_RETURN void osThreadExit (void) {
#ifndef USE_FreeRTOS_HEAP_1
vTaskDelete (NULL);
#endif
for (;;);
}
/*
Terminate execution of a thread.
*/
osStatus_t osThreadTerminate (osThreadId_t thread_id) {
TaskHandle_t hTask = (TaskHandle_t)thread_id;
osStatus_t stat;
#ifndef USE_FreeRTOS_HEAP_1
eTaskState tstate;
if (IRQ_Context() != 0U) {
stat = osErrorISR;
}
else if (hTask == NULL) {
stat = osErrorParameter;
}
else {
tstate = eTaskGetState (hTask);
if (tstate != eDeleted) {
stat = osOK;
vTaskDelete (hTask);
} else {
stat = osErrorResource;
}
}
#else
stat = osError;
#endif
/* Return execution status */
return (stat);
}
/*
Get number of active threads.
*/
uint32_t osThreadGetCount (void) {
uint32_t count;
if (IRQ_Context() != 0U) {
count = 0U;
} else {
count = uxTaskGetNumberOfTasks();
}
/* Return number of active threads */
return (count);
}
#if (configUSE_OS2_THREAD_ENUMERATE == 1)
/*
Enumerate active threads.
*/
uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
uint32_t i, count;
TaskStatus_t *task;
if ((IRQ_Context() != 0U) || (thread_array == NULL) || (array_items == 0U)) {
count = 0U;
} else {
vTaskSuspendAll();
/* Allocate memory on heap to temporarily store TaskStatus_t information */
count = uxTaskGetNumberOfTasks();
task = pvPortMalloc (count * sizeof(TaskStatus_t));
if (task != NULL) {
/* Retrieve task status information */
count = uxTaskGetSystemState (task, count, NULL);
/* Copy handles from task status array into provided thread array */
for (i = 0U; (i < count) && (i < array_items); i++) {
thread_array[i] = (osThreadId_t)task[i].xHandle;
}
count = i;
}
(void)xTaskResumeAll();
vPortFree (task);
}
/* Return number of enumerated threads */
return (count);
}
#endif /* (configUSE_OS2_THREAD_ENUMERATE == 1) */
/* ==== Thread Flags Functions ==== */
#if (configUSE_OS2_THREAD_FLAGS == 1)
/*
Set the specified Thread Flags of a thread.
*/
uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
TaskHandle_t hTask = (TaskHandle_t)thread_id;
uint32_t rflags;
BaseType_t yield;
if ((hTask == NULL) || ((flags & THREAD_FLAGS_INVALID_BITS) != 0U)) {
rflags = (uint32_t)osErrorParameter;
}
else {
rflags = (uint32_t)osError;
if (IRQ_Context() != 0U) {
yield = pdFALSE;
(void)xTaskNotifyFromISR (hTask, flags, eSetBits, &yield);
(void)xTaskNotifyAndQueryFromISR (hTask, 0, eNoAction, &rflags, NULL);
portYIELD_FROM_ISR (yield);
}
else {
(void)xTaskNotify (hTask, flags, eSetBits);
(void)xTaskNotifyAndQuery (hTask, 0, eNoAction, &rflags);
}
}
/* Return flags after setting */
return (rflags);
}
/*
Clear the specified Thread Flags of current running thread.
*/
uint32_t osThreadFlagsClear (uint32_t flags) {
TaskHandle_t hTask;
uint32_t rflags, cflags;
if (IRQ_Context() != 0U) {
rflags = (uint32_t)osErrorISR;
}
else if ((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
rflags = (uint32_t)osErrorParameter;
}
else {
hTask = xTaskGetCurrentTaskHandle();
if (xTaskNotifyAndQuery (hTask, 0, eNoAction, &cflags) == pdPASS) {
rflags = cflags;
cflags &= ~flags;
if (xTaskNotify (hTask, cflags, eSetValueWithOverwrite) != pdPASS) {
rflags = (uint32_t)osError;
}
}
else {
rflags = (uint32_t)osError;
}
}
/* Return flags before clearing */
return (rflags);
}
/*
Get the current Thread Flags of current running thread.
*/
uint32_t osThreadFlagsGet (void) {
TaskHandle_t hTask;
uint32_t rflags;
if (IRQ_Context() != 0U) {
rflags = (uint32_t)osErrorISR;
}
else {
hTask = xTaskGetCurrentTaskHandle();
if (xTaskNotifyAndQuery (hTask, 0, eNoAction, &rflags) != pdPASS) {