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
62
63
74
75
76
77
78
79
80
81
82
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
110
111
112
113
114
115
116
117
118
119
120
123
124
125
126
127
128
129
130
134
135
136
141
142
143
150
156
157
158
159
160
161
162
163
164
165
166
167
168
172
173
174
175
176
177
178
179
180
181
182
183
184
193
194
195
202
203
204
205
206
207
220
224
225
230
231
232
233
234
235
240
241
242
243
244
245
246
247
248
249
255
256
257
258
260
261
262
263
264
265
270
276
277
284
285
286
287
289
290
291
292
293
294
300
315
316
317
322
323
324
325
330
331
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
377
378
384
385
390
391
392
393
394
395
396
397
404
405
406
407
408
409
410
411
412
413
414
425
430
431
432
433
434
435
436
437
443
444
445
446
447
461
462
463
464
465
466
472
473
474
475
476
477
487
488
489
490
491
492
493
494
495
502
503
504
505
506
507
508
515
516
517
518
519
520
524
525
526
527
534
535
536
543
544
545
546
547
548
549
550
551
552
553
556
562
563
567
580
581
585
586
587
588
589
590
591
597
598
599
600
614
615
616
617
618
619
626
627
628
629
630
631
632
635
636
637
640
646
647
653
656
657
658
659
660
666
667
668
669
670
671
677
681
682
683
684
690
691
692
695
696
697
698
699
700
701
702
703
704
705
712
713
714
715
744
745
746
747
748
749
750
751
752
753
754
755
756
770
771
772
779
780
781
782
783
784
785
788
789
790
793
799
800
806
809
810
811
812
813
819
820
821
822
823
824
825
831
836
837
838
839
840
846
847
848
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
875
876
877
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
917
921
922
923
924
925
926
927
928
929
930
936
937
938
939
940
941
942
943
946
949
950
960
961
964
967
968
969
970
971
977
978
979
980
981
985
986
987
988
989
996
997
998
999
1000
1003
1004
1007
1008
1011
1012
1013
1016
1017
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1076
1077
1083
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1112
1113
1114
1115
1116
1119
1125
1126
1136
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1182
1183
1184
1185
1186
1187
1188
1192
1193
1194
1195
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1265
1266
1267
1268
1274
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1302
1303
1304
1305
1306
1309
1315
1316
1326
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1354
1355
1356
1357
1362
1363
1364
1365
1366
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1393
1396
1399
1402
1405
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1432
1433
1434
1435
1438
1439
1440
1441
1442
1443
1448
1449
1450
1451
1463
1464
1465
1466
1467
1468
1472
1478
1479
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1507
1508
1512
1513
1514
1515
1520
1521
1522
1523
1527
1528
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1555
1556
1557
1558
1559
1560
1561
1562
1566
1567
1568
1571
1577
1578
1583
1587
1588
1589
1590
1591
1596
1606
1607
1612
1616
1617
1622
1623
1624
1627
1628
1629
1630
1631
1632
1633
1638
1639
1640
1641
1654
1655
1656
1657
1658
1659
1664
1665
1666
1667
1674
1675
1676
1677
1678
1679
1685
1686
1687
1688
1689
1690
1694
1695
1696
1700
1708
1709
1713
1714
1715
1716
1717
1718
1719
1724
/* ... */
#include <string.h>
#include "cmsis_os.h"
/* ... */
#if defined ( __CC_ARM )
#define __ASM __asm
#define __INLINE __inline
#define __STATIC_INLINE static __inline
#include "cmsis_armcc.h"
/* ... */
/* ... */#elif defined ( __GNUC__ )
#define __ASM __asm
#define __INLINE inline
#define __STATIC_INLINE static inline
#include "cmsis_gcc.h"
/* ... */
/* ... */#elif defined ( __ICCARM__ )
#ifndef __ASM
#define __ASM __asm
#endif
#ifndef __INLINE
#define __INLINE inline
#endif
#ifndef __STATIC_INLINE
#define __STATIC_INLINE static inline
#endif
#include <cmsis_iar.h>
/* ... */#endif
extern void xPortSysTickHandler(void);
static unsigned portBASE_TYPE makeFreeRtosPriority (osPriority priority)
{
unsigned portBASE_TYPE fpriority = tskIDLE_PRIORITY;
if (priority != osPriorityError) {
fpriority += (priority - osPriorityIdle);
}if (priority != osPriorityError) { ... }
return fpriority;
}{ ... }
#if (INCLUDE_uxTaskPriorityGet == 1)
static osPriority makeCmsisPriority (unsigned portBASE_TYPE fpriority)
{
osPriority priority = osPriorityError;
if ((fpriority - tskIDLE_PRIORITY) <= (osPriorityRealtime - osPriorityIdle)) {
priority = (osPriority)((int)osPriorityIdle + (int)(fpriority - tskIDLE_PRIORITY));
}if ((fpriority - tskIDLE_PRIORITY) <= (osPriorityRealtime - osPriorityIdle)) { ... }
return priority;
}{ ... }
#endif/* ... */
static int inHandlerMode (void)
{
return __get_IPSR() != 0;
}{ ... }
/* ... */
osStatus osKernelInitialize (void);
/* ... */
osStatus osKernelStart (void)
{
vTaskStartScheduler();
return osOK;
}{ ... }
/* ... */
int32_t osKernelRunning(void)
{
#if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)
return 0;
else
return 1;/* ... */
#else
return (-1);
#endif
}{ ... }
#if (defined (osFeature_SysTick) && (osFeature_SysTick != 0))
/* ... */
uint32_t osKernelSysTick(void)
{
if (inHandlerMode()) {
return xTaskGetTickCountFromISR();
}if (inHandlerMode()) { ... }
else {
return xTaskGetTickCount();
}else { ... }
}{ ... }
#endif/* ... */ Kernel Control Functions
/* ... */
osThreadId osThreadCreate (const osThreadDef_t *thread_def, void *argument)
{
TaskHandle_t handle;
#if( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
if((thread_def->buffer != NULL) && (thread_def->controlblock != NULL)) {
handle = xTaskCreateStatic((TaskFunction_t)thread_def->pthread,(const portCHAR *)thread_def->name,
thread_def->stacksize, argument, makeFreeRtosPriority(thread_def->tpriority),
thread_def->buffer, thread_def->controlblock);
}if ((thread_def->buffer != NULL) && (thread_def->controlblock != NULL)) { ... }
else {
if (xTaskCreate((TaskFunction_t)thread_def->pthread,(const portCHAR *)thread_def->name,
thread_def->stacksize, argument, makeFreeRtosPriority(thread_def->tpriority),
&handle) != pdPASS) {
return NULL;
}if (xTaskCreate((TaskFunction_t)thread_def->pthread,(const portCHAR *)thread_def->name, thread_def->stacksize, argument, makeFreeRtosPriority(thread_def->tpriority), &handle) != pdPASS) { ... }
}else { ... }
/* ... */#elif( configSUPPORT_STATIC_ALLOCATION == 1 )
handle = xTaskCreateStatic((TaskFunction_t)thread_def->pthread,(const portCHAR *)thread_def->name,
thread_def->stacksize, argument, makeFreeRtosPriority(thread_def->tpriority),
thread_def->buffer, thread_def->controlblock);/* ... */
#else
if (xTaskCreate((TaskFunction_t)thread_def->pthread,(const portCHAR *)thread_def->name,
thread_def->stacksize, argument, makeFreeRtosPriority(thread_def->tpriority),
&handle) != pdPASS) {
return NULL;
}if (xTaskCreate((TaskFunction_t)thread_def->pthread,(const portCHAR *)thread_def->name, thread_def->stacksize, argument, makeFreeRtosPriority(thread_def->tpriority), &handle) != pdPASS) { ... } /* ... */
#endif
return handle;
}{ ... }
/* ... */
osThreadId osThreadGetId (void)
{
#if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
return xTaskGetCurrentTaskHandle();
#else
return NULL;
#endif
}{ ... }
/* ... */
osStatus osThreadTerminate (osThreadId thread_id)
{
#if (INCLUDE_vTaskDelete == 1)
vTaskDelete(thread_id);
return osOK;/* ... */
#else
return osErrorOS;
#endif
}{ ... }
/* ... */
osStatus osThreadYield (void)
{
taskYIELD();
return osOK;
}{ ... }
/* ... */
osStatus osThreadSetPriority (osThreadId thread_id, osPriority priority)
{
#if (INCLUDE_vTaskPrioritySet == 1)
vTaskPrioritySet(thread_id, makeFreeRtosPriority(priority));
return osOK;/* ... */
#else
return osErrorOS;
#endif
}{ ... }
/* ... */
osPriority osThreadGetPriority (osThreadId thread_id)
{
#if (INCLUDE_uxTaskPriorityGet == 1)
if (inHandlerMode())
{
return makeCmsisPriority(uxTaskPriorityGetFromISR(thread_id));
}if (inHandlerMode()) { ... }
else
{
return makeCmsisPriority(uxTaskPriorityGet(thread_id));
}else { ... }
/* ... */#else
return osPriorityError;
#endif
}{ ... }
Thread Management
/* ... */
osStatus osDelay (uint32_t millisec)
{
#if INCLUDE_vTaskDelay
TickType_t ticks = millisec / portTICK_PERIOD_MS;
vTaskDelay(ticks ? ticks : 1);
return osOK;/* ... */
#else
(void) millisec;
return osErrorResource;/* ... */
#endif
}{ ... }
#if (defined (osFeature_Wait) && (osFeature_Wait != 0))
/* ... */
osEvent osWait (uint32_t millisec);
/* ... */
#endif
Generic Wait Functions
/* ... */
osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument)
{
#if (configUSE_TIMERS == 1)
#if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
if(timer_def->controlblock != NULL) {
return xTimerCreateStatic((const char *)"",
1,
(type == osTimerPeriodic) ? pdTRUE : pdFALSE,
(void *) argument,
(TimerCallbackFunction_t)timer_def->ptimer,
(StaticTimer_t *)timer_def->controlblock);
}if (timer_def->controlblock != NULL) { ... }
else {
return xTimerCreate((const char *)"",
1,
(type == osTimerPeriodic) ? pdTRUE : pdFALSE,
(void *) argument,
(TimerCallbackFunction_t)timer_def->ptimer);
}else { ... }
/* ... */#elif( configSUPPORT_STATIC_ALLOCATION == 1 )
return xTimerCreateStatic((const char *)"",
1,
(type == osTimerPeriodic) ? pdTRUE : pdFALSE,
(void *) argument,
(TimerCallbackFunction_t)timer_def->ptimer,
(StaticTimer_t *)timer_def->controlblock); /* ... */
#else
return xTimerCreate((const char *)"",
1,
(type == osTimerPeriodic) ? pdTRUE : pdFALSE,
(void *) argument,
(TimerCallbackFunction_t)timer_def->ptimer);/* ... */
#endif
/* ... */
#else
return NULL;
#endif
}{ ... }
/* ... */
osStatus osTimerStart (osTimerId timer_id, uint32_t millisec)
{
osStatus result = osOK;
#if (configUSE_TIMERS == 1)
portBASE_TYPE taskWoken = pdFALSE;
TickType_t ticks = millisec / portTICK_PERIOD_MS;
if (ticks == 0)
ticks = 1;
if (inHandlerMode())
{
if (xTimerChangePeriodFromISR(timer_id, ticks, &taskWoken) != pdPASS)
{
result = osErrorOS;
}if (xTimerChangePeriodFromISR(timer_id, ticks, &taskWoken) != pdPASS) { ... }
else
{
portEND_SWITCHING_ISR(taskWoken);
}else { ... }
}if (inHandlerMode()) { ... }
else
{
if (xTimerChangePeriod(timer_id, ticks, 0) != pdPASS)
result = osErrorOS;
}else { ... }
/* ... */
#else
result = osErrorOS;
#endif
return result;
}{ ... }
/* ... */
osStatus osTimerStop (osTimerId timer_id)
{
osStatus result = osOK;
#if (configUSE_TIMERS == 1)
portBASE_TYPE taskWoken = pdFALSE;
if (inHandlerMode()) {
if (xTimerStopFromISR(timer_id, &taskWoken) != pdPASS) {
return osErrorOS;
}if (xTimerStopFromISR(timer_id, &taskWoken) != pdPASS) { ... }
portEND_SWITCHING_ISR(taskWoken);
}if (inHandlerMode()) { ... }
else {
if (xTimerStop(timer_id, 0) != pdPASS) {
result = osErrorOS;
}if (xTimerStop(timer_id, 0) != pdPASS) { ... }
}else { ... }
/* ... */#else
result = osErrorOS;
#endif
return result;
}{ ... }
/* ... */
osStatus osTimerDelete (osTimerId timer_id)
{
osStatus result = osOK;
#if (configUSE_TIMERS == 1)
if (inHandlerMode()) {
return osErrorISR;
}if (inHandlerMode()) { ... }
else {
if ((xTimerDelete(timer_id, osWaitForever )) != pdPASS) {
result = osErrorOS;
}if ((xTimerDelete(timer_id, osWaitForever )) != pdPASS) { ... }
}else { ... }
/* ... */
#else
result = osErrorOS;
#endif
return result;
}{ ... }
Timer Management Functions
/* ... */
int32_t osSignalSet (osThreadId thread_id, int32_t signal)
{
#if( configUSE_TASK_NOTIFICATIONS == 1 )
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
uint32_t ulPreviousNotificationValue = 0;
if (inHandlerMode())
{
if(xTaskGenericNotifyFromISR( thread_id , (uint32_t)signal, eSetBits, &ulPreviousNotificationValue, &xHigherPriorityTaskWoken ) != pdPASS )
return 0x80000000;
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}if (inHandlerMode()) { ... }
else if(xTaskGenericNotify( thread_id , (uint32_t)signal, eSetBits, &ulPreviousNotificationValue) != pdPASS )
return 0x80000000;
return ulPreviousNotificationValue;/* ... */
#else
(void) thread_id;
(void) signal;
return 0x80000000; /* ... */
#endif
}{ ... }
/* ... */
int32_t osSignalClear (osThreadId thread_id, int32_t signal);
/* ... */
osEvent osSignalWait (int32_t signals, uint32_t millisec)
{
osEvent ret;
#if( configUSE_TASK_NOTIFICATIONS == 1 )
TickType_t ticks;
ret.value.signals = 0;
ticks = 0;
if (millisec == osWaitForever) {
ticks = portMAX_DELAY;
}if (millisec == osWaitForever) { ... }
else if (millisec != 0) {
ticks = millisec / portTICK_PERIOD_MS;
if (ticks == 0) {
ticks = 1;
}if (ticks == 0) { ... }
}else if (millisec != 0) { ... }
if (inHandlerMode())
{
ret.status = osErrorISR;
}if (inHandlerMode()) { ... }
else
{
if(xTaskNotifyWait( 0,(uint32_t) signals, (uint32_t *)&ret.value.signals, ticks) != pdTRUE)
{
if(ticks == 0) ret.status = osOK;
else ret.status = osEventTimeout;
}if (xTaskNotifyWait( 0,(uint32_t) signals, (uint32_t *)&ret.value.signals, ticks) != pdTRUE) { ... }
else if(ret.value.signals < 0)
{
ret.status = osErrorValue;
}else if (ret.value.signals < 0) { ... }
else ret.status = osEventSignal;
}else { ... }
/* ... */#else
(void) signals;
(void) millisec;
ret.status = osErrorOS; /* ... */
#endif
return ret;
}{ ... }
Signal Management
/* ... */
osMutexId osMutexCreate (const osMutexDef_t *mutex_def)
{
#if ( configUSE_MUTEXES == 1)
#if( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
if (mutex_def->controlblock != NULL) {
return xSemaphoreCreateMutexStatic( mutex_def->controlblock );
}if (mutex_def->controlblock != NULL) { ... }
else {
return xSemaphoreCreateMutex();
}else { ... }
/* ... */#elif ( configSUPPORT_STATIC_ALLOCATION == 1 )
return xSemaphoreCreateMutexStatic( mutex_def->controlblock );
#else
return xSemaphoreCreateMutex();
#endif/* ... */
#else
return NULL;
#endif
}{ ... }
/* ... */
osStatus osMutexWait (osMutexId mutex_id, uint32_t millisec)
{
TickType_t ticks;
portBASE_TYPE taskWoken = pdFALSE;
if (mutex_id == NULL) {
return osErrorParameter;
}if (mutex_id == NULL) { ... }
ticks = 0;
if (millisec == osWaitForever) {
ticks = portMAX_DELAY;
}if (millisec == osWaitForever) { ... }
else if (millisec != 0) {
ticks = millisec / portTICK_PERIOD_MS;
if (ticks == 0) {
ticks = 1;
}if (ticks == 0) { ... }
}else if (millisec != 0) { ... }
if (inHandlerMode()) {
if (xSemaphoreTakeFromISR(mutex_id, &taskWoken) != pdTRUE) {
return osErrorOS;
}if (xSemaphoreTakeFromISR(mutex_id, &taskWoken) != pdTRUE) { ... }
portEND_SWITCHING_ISR(taskWoken);
}if (inHandlerMode()) { ... }
else if (xSemaphoreTake(mutex_id, ticks) != pdTRUE) {
return osErrorOS;
}else if (xSemaphoreTake(mutex_id, ticks) != pdTRUE) { ... }
return osOK;
}{ ... }
/* ... */
osStatus osMutexRelease (osMutexId mutex_id)
{
osStatus result = osOK;
portBASE_TYPE taskWoken = pdFALSE;
if (inHandlerMode()) {
if (xSemaphoreGiveFromISR(mutex_id, &taskWoken) != pdTRUE) {
return osErrorOS;
}if (xSemaphoreGiveFromISR(mutex_id, &taskWoken) != pdTRUE) { ... }
portEND_SWITCHING_ISR(taskWoken);
}if (inHandlerMode()) { ... }
else if (xSemaphoreGive(mutex_id) != pdTRUE)
{
result = osErrorOS;
}else if (xSemaphoreGive(mutex_id) != pdTRUE) { ... }
return result;
}{ ... }
/* ... */
osStatus osMutexDelete (osMutexId mutex_id)
{
if (inHandlerMode()) {
return osErrorISR;
}if (inHandlerMode()) { ... }
vQueueDelete(mutex_id);
return osOK;
}{ ... }
Mutex Management
#if (defined (osFeature_Semaphore) && (osFeature_Semaphore != 0))
/* ... */
osSemaphoreId osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count)
{
#if( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
osSemaphoreId sema;
if (semaphore_def->controlblock != NULL){
if (count == 1) {
return xSemaphoreCreateBinaryStatic( semaphore_def->controlblock );
}if (count == 1) { ... }
else {
#if (configUSE_COUNTING_SEMAPHORES == 1 )
return xSemaphoreCreateCountingStatic( count, count, semaphore_def->controlblock );
#else
return NULL;
#endif
}else { ... }
}if (semaphore_def->controlblock != NULL) { ... }
else {
if (count == 1) {
vSemaphoreCreateBinary(sema);
return sema;
}if (count == 1) { ... }
else {
#if (configUSE_COUNTING_SEMAPHORES == 1 )
return xSemaphoreCreateCounting(count, count);
#else
return NULL;
#endif
}else { ... }
}else { ... }
/* ... */#elif ( configSUPPORT_STATIC_ALLOCATION == 1 )
if(count == 1) {
return xSemaphoreCreateBinaryStatic( semaphore_def->controlblock );
}if (count == 1) { ... }
else
{
#if (configUSE_COUNTING_SEMAPHORES == 1 )
return xSemaphoreCreateCountingStatic( count, count, semaphore_def->controlblock );
#else
return NULL;
#endif
}else { ... }
/* ... */#else
osSemaphoreId sema;
if (count == 1) {
vSemaphoreCreateBinary(sema);
return sema;
}if (count == 1) { ... }
else {
#if (configUSE_COUNTING_SEMAPHORES == 1 )
return xSemaphoreCreateCounting(count, count);
#else
return NULL;
#endif
}else { ... }
/* ... */#endif
}{ ... }
/* ... */
int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec)
{
TickType_t ticks;
portBASE_TYPE taskWoken = pdFALSE;
if (semaphore_id == NULL) {
return osErrorParameter;
}if (semaphore_id == NULL) { ... }
ticks = 0;
if (millisec == osWaitForever) {
ticks = portMAX_DELAY;
}if (millisec == osWaitForever) { ... }
else if (millisec != 0) {
ticks = millisec / portTICK_PERIOD_MS;
if (ticks == 0) {
ticks = 1;
}if (ticks == 0) { ... }
}else if (millisec != 0) { ... }
if (inHandlerMode()) {
if (xSemaphoreTakeFromISR(semaphore_id, &taskWoken) != pdTRUE) {
return osErrorOS;
}if (xSemaphoreTakeFromISR(semaphore_id, &taskWoken) != pdTRUE) { ... }
portEND_SWITCHING_ISR(taskWoken);
}if (inHandlerMode()) { ... }
else if (xSemaphoreTake(semaphore_id, ticks) != pdTRUE) {
return osErrorOS;
}else if (xSemaphoreTake(semaphore_id, ticks) != pdTRUE) { ... }
return osOK;
}{ ... }
/* ... */
osStatus osSemaphoreRelease (osSemaphoreId semaphore_id)
{
osStatus result = osOK;
portBASE_TYPE taskWoken = pdFALSE;
if (inHandlerMode()) {
if (xSemaphoreGiveFromISR(semaphore_id, &taskWoken) != pdTRUE) {
return osErrorOS;
}if (xSemaphoreGiveFromISR(semaphore_id, &taskWoken) != pdTRUE) { ... }
portEND_SWITCHING_ISR(taskWoken);
}if (inHandlerMode()) { ... }
else {
if (xSemaphoreGive(semaphore_id) != pdTRUE) {
result = osErrorOS;
}if (xSemaphoreGive(semaphore_id) != pdTRUE) { ... }
}else { ... }
return result;
}{ ... }
/* ... */
osStatus osSemaphoreDelete (osSemaphoreId semaphore_id)
{
if (inHandlerMode()) {
return osErrorISR;
}if (inHandlerMode()) { ... }
vSemaphoreDelete(semaphore_id);
return osOK;
}{ ... }
/* ... */#endif
Semaphore Management Functions
#if (defined (osFeature_Pool) && (osFeature_Pool != 0))
typedef struct os_pool_cb {
void *pool;
uint8_t *markers;
uint32_t pool_sz;
uint32_t item_sz;
uint32_t currentIndex;
...} os_pool_cb_t;
/* ... */
osPoolId osPoolCreate (const osPoolDef_t *pool_def)
{
#if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
osPoolId thePool;
int itemSize = 4 * ((pool_def->item_sz + 3) / 4);
uint32_t i;
thePool = pvPortMalloc(sizeof(os_pool_cb_t));
if (thePool) {
thePool->pool_sz = pool_def->pool_sz;
thePool->item_sz = itemSize;
thePool->currentIndex = 0;
thePool->markers = pvPortMalloc(pool_def->pool_sz);
if (thePool->markers) {
thePool->pool = pvPortMalloc(pool_def->pool_sz * itemSize);
if (thePool->pool) {
for (i = 0; i < pool_def->pool_sz; i++) {
thePool->markers[i] = 0;
}for (i = 0; i < pool_def->pool_sz; i++) { ... }
}if (thePool->pool) { ... }
else {
vPortFree(thePool->markers);
vPortFree(thePool);
thePool = NULL;
}else { ... }
}if (thePool->markers) { ... }
else {
vPortFree(thePool);
thePool = NULL;
}else { ... }
}if (thePool) { ... }
return thePool;
/* ... */
#else
return NULL;
#endif
}{ ... }
/* ... */
void *osPoolAlloc (osPoolId pool_id)
{
int dummy = 0;
void *p = NULL;
uint32_t i;
uint32_t index;
if (inHandlerMode()) {
dummy = portSET_INTERRUPT_MASK_FROM_ISR();
}if (inHandlerMode()) { ... }
else {
vPortEnterCritical();
}else { ... }
for (i = 0; i < pool_id->pool_sz; i++) {
index = (pool_id->currentIndex + i) % pool_id->pool_sz;
if (pool_id->markers[index] == 0) {
pool_id->markers[index] = 1;
p = (void *)((uint32_t)(pool_id->pool) + (index * pool_id->item_sz));
pool_id->currentIndex = index;
break;
}if (pool_id->markers[index] == 0) { ... }
}for (i = 0; i < pool_id->pool_sz; i++) { ... }
if (inHandlerMode()) {
portCLEAR_INTERRUPT_MASK_FROM_ISR(dummy);
}if (inHandlerMode()) { ... }
else {
vPortExitCritical();
}else { ... }
return p;
}{ ... }
/* ... */
void *osPoolCAlloc (osPoolId pool_id)
{
void *p = osPoolAlloc(pool_id);
if (p != NULL)
{
memset(p, 0, sizeof(pool_id->pool_sz));
}if (p != NULL) { ... }
return p;
}{ ... }
/* ... */
osStatus osPoolFree (osPoolId pool_id, void *block)
{
uint32_t index;
if (pool_id == NULL) {
return osErrorParameter;
}if (pool_id == NULL) { ... }
if (block == NULL) {
return osErrorParameter;
}if (block == NULL) { ... }
if (block < pool_id->pool) {
return osErrorParameter;
}if (block < pool_id->pool) { ... }
index = (uint32_t)block - (uint32_t)(pool_id->pool);
if (index % pool_id->item_sz) {
return osErrorParameter;
}if (index % pool_id->item_sz) { ... }
index = index / pool_id->item_sz;
if (index >= pool_id->pool_sz) {
return osErrorParameter;
}if (index >= pool_id->pool_sz) { ... }
pool_id->markers[index] = 0;
return osOK;
}{ ... }
/* ... */
#endif
Memory Pool Management Functions
#if (defined (osFeature_MessageQ) && (osFeature_MessageQ != 0))
/* ... */
osMessageQId osMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id)
{
(void) thread_id;
#if( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
if ((queue_def->buffer != NULL) && (queue_def->controlblock != NULL)) {
return xQueueCreateStatic(queue_def->queue_sz, queue_def->item_sz, queue_def->buffer, queue_def->controlblock);
}if ((queue_def->buffer != NULL) && (queue_def->controlblock != NULL)) { ... }
else {
return xQueueCreate(queue_def->queue_sz, queue_def->item_sz);
}else { ... }
/* ... */#elif ( configSUPPORT_STATIC_ALLOCATION == 1 )
return xQueueCreateStatic(queue_def->queue_sz, queue_def->item_sz, queue_def->buffer, queue_def->controlblock);
#else
return xQueueCreate(queue_def->queue_sz, queue_def->item_sz);
#endif
}{ ... }
/* ... */
osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec)
{
portBASE_TYPE taskWoken = pdFALSE;
TickType_t ticks;
ticks = millisec / portTICK_PERIOD_MS;
if (ticks == 0) {
ticks = 1;
}if (ticks == 0) { ... }
if (inHandlerMode()) {
if (xQueueSendFromISR(queue_id, &info, &taskWoken) != pdTRUE) {
return osErrorOS;
}if (xQueueSendFromISR(queue_id, &info, &taskWoken) != pdTRUE) { ... }
portEND_SWITCHING_ISR(taskWoken);
}if (inHandlerMode()) { ... }
else {
if (xQueueSend(queue_id, &info, ticks) != pdTRUE) {
return osErrorOS;
}if (xQueueSend(queue_id, &info, ticks) != pdTRUE) { ... }
}else { ... }
return osOK;
}{ ... }
/* ... */
osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec)
{
portBASE_TYPE taskWoken;
TickType_t ticks;
osEvent event;
event.def.message_id = queue_id;
event.value.v = 0;
if (queue_id == NULL) {
event.status = osErrorParameter;
return event;
}if (queue_id == NULL) { ... }
taskWoken = pdFALSE;
ticks = 0;
if (millisec == osWaitForever) {
ticks = portMAX_DELAY;
}if (millisec == osWaitForever) { ... }
else if (millisec != 0) {
ticks = millisec / portTICK_PERIOD_MS;
if (ticks == 0) {
ticks = 1;
}if (ticks == 0) { ... }
}else if (millisec != 0) { ... }
if (inHandlerMode()) {
if (xQueueReceiveFromISR(queue_id, &event.value.v, &taskWoken) == pdTRUE) {
event.status = osEventMessage;
}if (xQueueReceiveFromISR(queue_id, &event.value.v, &taskWoken) == pdTRUE) { ... }
else {
event.status = osOK;
}else { ... }
portEND_SWITCHING_ISR(taskWoken);
}if (inHandlerMode()) { ... }
else {
if (xQueueReceive(queue_id, &event.value.v, ticks) == pdTRUE) {
event.status = osEventMessage;
}if (xQueueReceive(queue_id, &event.value.v, ticks) == pdTRUE) { ... }
else {
event.status = (ticks == 0) ? osOK : osEventTimeout;
}else { ... }
}else { ... }
return event;
}{ ... }
/* ... */#endif
Message Queue Management Functions
#if (defined (osFeature_MailQ) && (osFeature_MailQ != 0))
typedef struct os_mailQ_cb {
const osMailQDef_t *queue_def;
QueueHandle_t handle;
osPoolId pool;
...} os_mailQ_cb_t;
/* ... */
osMailQId osMailCreate (const osMailQDef_t *queue_def, osThreadId thread_id)
{
#if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
(void) thread_id;
osPoolDef_t pool_def = {queue_def->queue_sz, queue_def->item_sz, NULL};
*(queue_def->cb) = pvPortMalloc(sizeof(struct os_mailQ_cb));
if (*(queue_def->cb) == NULL) {
return NULL;
}if (*(queue_def->cb) == NULL) { ... }
(*(queue_def->cb))->queue_def = queue_def;
(*(queue_def->cb))->handle = xQueueCreate(queue_def->queue_sz, sizeof(void *));
if ((*(queue_def->cb))->handle == NULL) {
vPortFree(*(queue_def->cb));
return NULL;
}if ((*(queue_def->cb))->handle == NULL) { ... }
(*(queue_def->cb))->pool = osPoolCreate(&pool_def);
if ((*(queue_def->cb))->pool == NULL) {
vPortFree(*(queue_def->cb));
return NULL;
}if ((*(queue_def->cb))->pool == NULL) { ... }
return *(queue_def->cb);/* ... */
#else
return NULL;
#endif
}{ ... }
/* ... */
void *osMailAlloc (osMailQId queue_id, uint32_t millisec)
{
(void) millisec;
void *p;
if (queue_id == NULL) {
return NULL;
}if (queue_id == NULL) { ... }
p = osPoolAlloc(queue_id->pool);
return p;
}{ ... }
/* ... */
void *osMailCAlloc (osMailQId queue_id, uint32_t millisec)
{
uint32_t i;
void *p = osMailAlloc(queue_id, millisec);
if (p) {
for (i = 0; i < queue_id->queue_def->item_sz; i++) {
((uint8_t *)p)[i] = 0;
}for (i = 0; i < queue_id->queue_def->item_sz; i++) { ... }
}if (p) { ... }
return p;
}{ ... }
/* ... */
osStatus osMailPut (osMailQId queue_id, void *mail)
{
portBASE_TYPE taskWoken;
if (queue_id == NULL) {
return osErrorParameter;
}if (queue_id == NULL) { ... }
taskWoken = pdFALSE;
if (inHandlerMode()) {
if (xQueueSendFromISR(queue_id->handle, &mail, &taskWoken) != pdTRUE) {
return osErrorOS;
}if (xQueueSendFromISR(queue_id->handle, &mail, &taskWoken) != pdTRUE) { ... }
portEND_SWITCHING_ISR(taskWoken);
}if (inHandlerMode()) { ... }
else {
if (xQueueSend(queue_id->handle, &mail, 0) != pdTRUE) {
return osErrorOS;
}if (xQueueSend(queue_id->handle, &mail, 0) != pdTRUE) { ... }
}else { ... }
return osOK;
}{ ... }
/* ... */
osEvent osMailGet (osMailQId queue_id, uint32_t millisec)
{
portBASE_TYPE taskWoken;
TickType_t ticks;
osEvent event;
event.def.mail_id = queue_id;
if (queue_id == NULL) {
event.status = osErrorParameter;
return event;
}if (queue_id == NULL) { ... }
taskWoken = pdFALSE;
ticks = 0;
if (millisec == osWaitForever) {
ticks = portMAX_DELAY;
}if (millisec == osWaitForever) { ... }
else if (millisec != 0) {
ticks = millisec / portTICK_PERIOD_MS;
if (ticks == 0) {
ticks = 1;
}if (ticks == 0) { ... }
}else if (millisec != 0) { ... }
if (inHandlerMode()) {
if (xQueueReceiveFromISR(queue_id->handle, &event.value.p, &taskWoken) == pdTRUE) {
event.status = osEventMail;
}if (xQueueReceiveFromISR(queue_id->handle, &event.value.p, &taskWoken) == pdTRUE) { ... }
else {
event.status = osOK;
}else { ... }
portEND_SWITCHING_ISR(taskWoken);
}if (inHandlerMode()) { ... }
else {
if (xQueueReceive(queue_id->handle, &event.value.p, ticks) == pdTRUE) {
event.status = osEventMail;
}if (xQueueReceive(queue_id->handle, &event.value.p, ticks) == pdTRUE) { ... }
else {
event.status = (ticks == 0) ? osOK : osEventTimeout;
}else { ... }
}else { ... }
return event;
}{ ... }
/* ... */
osStatus osMailFree (osMailQId queue_id, void *mail)
{
if (queue_id == NULL) {
return osErrorParameter;
}if (queue_id == NULL) { ... }
return osPoolFree(queue_id->pool, mail);
}{ ... }
#endif/* ... */
Mail Queue Management Functions
/* ... */
void osSystickHandler(void)
{
#if (INCLUDE_xTaskGetSchedulerState == 1 )
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
{
#endif
xPortSysTickHandler();
#if (INCLUDE_xTaskGetSchedulerState == 1 )
}if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) { ... }
#endif
}{ ... }
#if ( INCLUDE_eTaskGetState == 1 )
/* ... */
osThreadState osThreadGetState(osThreadId thread_id)
{
eTaskState ThreadState;
osThreadState result;
ThreadState = eTaskGetState(thread_id);
switch (ThreadState)
{
case eRunning :
result = osThreadRunning;
break;case eRunning :
case eReady :
result = osThreadReady;
break;case eReady :
case eBlocked :
result = osThreadBlocked;
break;case eBlocked :
case eSuspended :
result = osThreadSuspended;
break;case eSuspended :
case eDeleted :
result = osThreadDeleted;
break;case eDeleted :
default:
result = osThreadError;default
}switch (ThreadState) { ... }
return result;
}osThreadGetState (osThreadId thread_id) { ... }
/* ... */#endif
#if (INCLUDE_eTaskGetState == 1)
/* ... */
osStatus osThreadIsSuspended(osThreadId thread_id)
{
if (eTaskGetState(thread_id) == eSuspended)
return osOK;
else
return osErrorOS;
}osThreadIsSuspended (osThreadId thread_id) { ... }
/* ... */#endif
/* ... */
osStatus osThreadSuspend (osThreadId thread_id)
{
#if (INCLUDE_vTaskSuspend == 1)
vTaskSuspend(thread_id);
return osOK;/* ... */
#else
return osErrorResource;
#endif
}{ ... }
/* ... */
osStatus osThreadResume (osThreadId thread_id)
{
#if (INCLUDE_vTaskSuspend == 1)
if(inHandlerMode())
{
if (xTaskResumeFromISR(thread_id) == pdTRUE)
{
portYIELD_FROM_ISR(pdTRUE);
}if (xTaskResumeFromISR(thread_id) == pdTRUE) { ... }
}if (inHandlerMode()) { ... }
else
{
vTaskResume(thread_id);
}else { ... }
return osOK;/* ... */
#else
return osErrorResource;
#endif
}{ ... }
/* ... */
osStatus osThreadSuspendAll (void)
{
vTaskSuspendAll();
return osOK;
}{ ... }
/* ... */
osStatus osThreadResumeAll (void)
{
if (xTaskResumeAll() == pdTRUE)
return osOK;
else
return osErrorOS;
}{ ... }
/* ... */
osStatus osDelayUntil (uint32_t *PreviousWakeTime, uint32_t millisec)
{
#if INCLUDE_vTaskDelayUntil
TickType_t ticks = (millisec / portTICK_PERIOD_MS);
vTaskDelayUntil((TickType_t *) PreviousWakeTime, ticks ? ticks : 1);
return osOK;/* ... */
#else
(void) millisec;
(void) PreviousWakeTime;
return osErrorResource;/* ... */
#endif
}{ ... }
/* ... */
osStatus osAbortDelay(osThreadId thread_id)
{
#if INCLUDE_xTaskAbortDelay
xTaskAbortDelay(thread_id);
return osOK;/* ... */
#else
(void) thread_id;
return osErrorResource;/* ... */
#endif
}{ ... }
/* ... */
osStatus osThreadList (uint8_t *buffer)
{
#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) )
vTaskList((char *)buffer);
#endif
return osOK;
}{ ... }
/* ... */
osEvent osMessagePeek (osMessageQId queue_id, uint32_t millisec)
{
TickType_t ticks;
osEvent event;
event.def.message_id = queue_id;
if (queue_id == NULL) {
event.status = osErrorParameter;
return event;
}if (queue_id == NULL) { ... }
ticks = 0;
if (millisec == osWaitForever) {
ticks = portMAX_DELAY;
}if (millisec == osWaitForever) { ... }
else if (millisec != 0) {
ticks = millisec / portTICK_PERIOD_MS;
if (ticks == 0) {
ticks = 1;
}if (ticks == 0) { ... }
}else if (millisec != 0) { ... }
if (xQueuePeek(queue_id, &event.value.v, ticks) == pdTRUE)
{
event.status = osEventMessage;
}if (xQueuePeek(queue_id, &event.value.v, ticks) == pdTRUE) { ... }
else
{
event.status = (ticks == 0) ? osOK : osEventTimeout;
}else { ... }
return event;
}{ ... }
/* ... */
uint32_t osMessageWaiting(osMessageQId queue_id)
{
if (inHandlerMode()) {
return uxQueueMessagesWaitingFromISR(queue_id);
}if (inHandlerMode()) { ... }
else
{
return uxQueueMessagesWaiting(queue_id);
}else { ... }
}{ ... }
/* ... */
uint32_t osMessageAvailableSpace(osMessageQId queue_id)
{
return uxQueueSpacesAvailable(queue_id);
}{ ... }
/* ... */
osStatus osMessageDelete (osMessageQId queue_id)
{
if (inHandlerMode()) {
return osErrorISR;
}if (inHandlerMode()) { ... }
vQueueDelete(queue_id);
return osOK;
}{ ... }
/* ... */
osMutexId osRecursiveMutexCreate (const osMutexDef_t *mutex_def)
{
#if (configUSE_RECURSIVE_MUTEXES == 1)
#if( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
if (mutex_def->controlblock != NULL){
return xSemaphoreCreateRecursiveMutexStatic( mutex_def->controlblock );
}if (mutex_def->controlblock != NULL) { ... }
else {
return xSemaphoreCreateRecursiveMutex();
}else { ... }
/* ... */#elif ( configSUPPORT_STATIC_ALLOCATION == 1 )
return xSemaphoreCreateRecursiveMutexStatic( mutex_def->controlblock );
#else
return xSemaphoreCreateRecursiveMutex();
#endif/* ... */
#else
return NULL;
#endif
}{ ... }
/* ... */
osStatus osRecursiveMutexRelease (osMutexId mutex_id)
{
#if (configUSE_RECURSIVE_MUTEXES == 1)
osStatus result = osOK;
if (xSemaphoreGiveRecursive(mutex_id) != pdTRUE)
{
result = osErrorOS;
}if (xSemaphoreGiveRecursive(mutex_id) != pdTRUE) { ... }
return result;/* ... */
#else
return osErrorResource;
#endif
}{ ... }
/* ... */
osStatus osRecursiveMutexWait (osMutexId mutex_id, uint32_t millisec)
{
#if (configUSE_RECURSIVE_MUTEXES == 1)
TickType_t ticks;
if (mutex_id == NULL)
{
return osErrorParameter;
}if (mutex_id == NULL) { ... }
ticks = 0;
if (millisec == osWaitForever)
{
ticks = portMAX_DELAY;
}if (millisec == osWaitForever) { ... }
else if (millisec != 0)
{
ticks = millisec / portTICK_PERIOD_MS;
if (ticks == 0)
{
ticks = 1;
}if (ticks == 0) { ... }
}else if (millisec != 0) { ... }
if (xSemaphoreTakeRecursive(mutex_id, ticks) != pdTRUE)
{
return osErrorOS;
}if (xSemaphoreTakeRecursive(mutex_id, ticks) != pdTRUE) { ... }
return osOK;/* ... */
#else
return osErrorResource;
#endif
}{ ... }
/* ... */
uint32_t osSemaphoreGetCount(osSemaphoreId semaphore_id)
{
return uxSemaphoreGetCount(semaphore_id);
}{ ... }