-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmsis_rtthread.c
2291 lines (1871 loc) · 61.2 KB
/
cmsis_rtthread.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) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-04-12 misonyo the first version
* 2019-04-04 misonyo fix some bugs
*/
#include <cmsis_os2.h>
#include "cmsis_rtthread.h"
#include <board.h>
#include <os_tick.h>
#include <rthw.h>
#define KERNEL_Id "RT-Thread" ///< Kernel identification string
#define DEFAULT_STACK_SIZE 512
#define DEFAULT_TICK 5
#define WAITING_THREAD_FLAGS 0x08
#define MALLOC_CB 0x10
#define MALLOC_STACK 0x04
#define MALLOC_MEM 0x02
static osKernelState_t kernel_state = osKernelInactive;
static void thread_cleanup(rt_thread_t thread)
{
thread_cb_t *thread_cb;
thread_cb = (thread_cb_t *)(thread->user_data);
/* clear cleanup function */
thread->cleanup = RT_NULL;
if (thread_cb->flags & osThreadJoinable)
{
rt_sem_release(thread_cb->joinable_sem);
}
else
{
if (thread_cb->flags & MALLOC_STACK)
rt_free(thread_cb->thread.stack_addr);
if (thread_cb->flags & MALLOC_CB)
rt_free(thread_cb);
}
}
// ==== Kernel Management Functions ====
// ==== Helper functions ====
/// Block Kernel (disable: thread switching, time tick, post ISR processing).
#ifdef SysTick
static volatile uint8_t blocked; ///< Blocked
static uint8_t pendSV; ///< Pending SV
/// Get Pending SV (Service Call) Flag
/// \return Pending SV Flag
__STATIC_INLINE uint8_t GetPendSV (void)
{
return ((uint8_t)((SCB->ICSR & (SCB_ICSR_PENDSVSET_Msk)) >> 24));
}
/// Clear Pending SV (Service Call) Flag
__STATIC_INLINE void ClrPendSV (void)
{
SCB->ICSR = SCB_ICSR_PENDSVCLR_Msk;
}
/// Set Pending SV (Service Call) Flag
__STATIC_INLINE void SetPendSV (void)
{
SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
}
/// Block Kernel (disable: thread switching, time tick, post ISR processing).
static void KernelBlock (void)
{
OS_Tick_Disable();
blocked = 1U;
__DSB();
if (GetPendSV() != 0U)
{
ClrPendSV();
pendSV = 1U;
}
}
/// Unblock Kernel
static void KernelUnblock (void)
{
blocked = 0U;
__DSB();
if (pendSV != 0U)
{
pendSV = 0U;
SetPendSV();
}
OS_Tick_Enable();
}
#endif
/// Initialize the RTOS Kernel.
/// \return status code that indicates the execution status of the function.
osStatus_t osKernelInitialize(void)
{
kernel_state = osKernelReady;
return osOK;
}
/// Get RTOS Kernel Information.
/// \param[out] version pointer to buffer for retrieving version information.
/// \param[out] id_buf pointer to buffer for retrieving kernel identification string.
/// \param[in] id_size size of buffer for kernel identification string.
/// \return status code that indicates the execution status of the function.
osStatus_t osKernelGetInfo(osVersion_t *version, char *id_buf, uint32_t id_size)
{
if ((RT_NULL == version) || (RT_NULL == id_buf) || id_size < sizeof(KERNEL_Id))
return osErrorParameter;
version->api = KERNEL_VERSION;
version->kernel = KERNEL_VERSION;
id_size = sizeof(KERNEL_Id);
rt_strncpy(id_buf, KERNEL_Id, id_size);
return osOK;
}
/// Get the current RTOS Kernel state.
/// \return current RTOS Kernel state.
osKernelState_t osKernelGetState(void)
{
return kernel_state;
}
/// Start the RTOS Kernel scheduler.
/// \return status code that indicates the execution status of the function.
osStatus_t osKernelStart(void)
{
osStatus_t state;
if (osKernelReady == kernel_state)
{
kernel_state = osKernelRunning;
state = osOK;
}
else
{
state = osError;
}
return state;
}
/// Lock the RTOS Kernel scheduler.
/// \return previous lock state (1 - locked, 0 - not locked, error code if negative).
int32_t osKernelLock(void)
{
rt_uint16_t level;
level = rt_critical_level();
rt_enter_critical();
kernel_state = osKernelLocked;
if (level)
{
return 1;
}
else
{
return 0;
}
}
/// Unlock the RTOS Kernel scheduler.
/// \return previous lock state (1 - locked, 0 - not locked, error code if negative).
int32_t osKernelUnlock(void)
{
rt_uint16_t level;
level = rt_critical_level();
if (0U == level)
{
return 0;
}
else
{
rt_exit_critical();
if (1u == level)
kernel_state = osKernelRunning;
return 1;
}
}
/// Restore the RTOS Kernel scheduler lock state.
/// \param[in] lock lock state obtained by \ref osKernelLock or \ref osKernelUnlock.
/// \return new lock state (1 - locked, 0 - not locked, error code if negative).
int32_t osKernelRestoreLock(int32_t lock)
{
if (1u == lock)
{
osKernelLock();
return 1;
}
else
{
osKernelUnlock();
return 0;
}
}
/// Get the RTOS kernel tick count.
/// \return RTOS kernel current tick count.
uint32_t osKernelGetTickCount(void)
{
return (uint32_t)rt_tick_get();
}
/// Get the RTOS kernel tick frequency.
/// \return frequency of the kernel tick.
uint32_t osKernelGetTickFreq(void)
{
return RT_TICK_PER_SECOND;
}
/// The function returns the current RTOS kernel system timer as a 32-bit value.
/// \return RTOS kernel current system timer count as 32-bit value
#ifdef SysTick
uint32_t osKernelGetSysTimerCount(void)
{
uint32_t irqmask ;
rt_tick_t ticks;
uint32_t val;
irqmask = rt_hw_interrupt_disable();
ticks = rt_tick_get();
val = OS_Tick_GetCount();
if (OS_Tick_GetOverflow() != 0U)
{
val = OS_Tick_GetCount();
ticks++;
}
val += ticks * OS_Tick_GetInterval();
rt_hw_interrupt_enable(irqmask);
return (val);
}
#endif /*SysTick*/
/// Get the RTOS kernel system timer frequency.
/// \return frequency of the system timer.
uint32_t osKernelGetSysTimerFreq(void)
{
return RT_TICK_PER_SECOND;
}
/// Suspend the RTOS Kernel scheduler.
/// \return time in ticks, for how long the system can sleep or power-down.
#ifdef SysTick
uint32_t osKernelSuspend (void)
{
rt_thread_t thread;
rt_timer_t timer;
struct rt_object_information *info_thread;
struct rt_object_information *info_timer;
struct rt_list_node *node;
rt_uint8_t timer_index = 0;
rt_tick_t min_tick = 0;
rt_tick_t cur_tick = 0;
rt_tick_t temp_tick = 0;
if (kernel_state != osKernelRunning)
{
return 0U;
}
info_thread = rt_object_get_information(RT_Object_Class_Thread);
info_timer = rt_object_get_information(RT_Object_Class_Timer);
KernelBlock();
min_tick = osWaitForever;
cur_tick = rt_tick_get();
/*check thread delay list*/
if (info_thread != NULL)
{
for (node = info_thread->object_list.next; node != &(info_thread->object_list); node = node->next)
{
thread = thread_rt_list_entry(node, rt_thread);
if (thread->thread_timer.parent.flag & RT_TIMER_FLAG_ACTIVATED)
{
temp_tick = thread->thread_timer.timeout_tick - cur_tick;
if (temp_tick < min_tick)
{
min_tick = temp_tick;
}
}
}
}
/*check active timer list*/
if (info_timer != NULL)
{
for (node = info_timer->object_list.next; node != &(info_timer->object_list); node = node->next)
{
timer = rt_list_entry(node, struct rt_timer, row[timer_index++]);
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
{
temp_tick = timer->timeout_tick - cur_tick;
if (temp_tick < min_tick)
{
min_tick = temp_tick;
}
}
}
}
if (osWaitForever == min_tick)
min_tick = 0U;
kernel_state = osKernelSuspended;
return (min_tick);
}
#endif /* SysTick */
/// Resume the RTOS Kernel scheduler.
/// \param[in] sleep_ticks time in ticks for how long the system was in sleep or power-down mode.
#ifdef SysTick
void osKernelResume (uint32_t sleep_ticks)
{
rt_tick_t delay_tick = 0;
if (kernel_state != osKernelSuspended)
{
return;
}
delay_tick = (rt_tick_t)sleep_ticks;
rt_enter_critical();
while(delay_tick > 0)
{
rt_tick_increase(); /*Process Thread Delay list and Process Active Timer list*/
delay_tick --;
}
rt_exit_critical();
kernel_state = osKernelRunning;
KernelUnblock();
return;
}
#endif
// ==== Thread Management Functions ====
/// Create a thread and add it to Active Threads.
/// \param[in] func thread function.
/// \param[in] argument pointer that is passed to the thread function as start argument.
/// \param[in] attr thread attributes; NULL: default values.
/// \return thread ID for reference by other functions or NULL in case of error.
osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
{
void *stack;
rt_uint8_t rtt_prio;
rt_uint32_t stack_size;
thread_cb_t *thread_cb;
char name[RT_NAME_MAX];
static rt_uint16_t thread_number = 1U;
/* Check parameters */
if (RT_NULL == func)
{
return RT_NULL;
}
if ((RT_NULL == attr) || (RT_NULL == attr->cb_mem))
{
thread_cb = rt_malloc(sizeof(thread_cb_t));
if (RT_NULL == thread_cb)
return RT_NULL;
rt_memset(thread_cb, 0, sizeof(thread_cb_t));
thread_cb->flags |= MALLOC_CB;
}
else
{
if (attr->cb_size >= sizeof(thread_cb_t))
{
thread_cb = attr->cb_mem;
thread_cb->flags = 0;
}
else
return RT_NULL;
}
if ((RT_NULL == attr) || (RT_NULL == attr->name))
rt_snprintf(name, sizeof(name), "th%02d", thread_number ++);
else
rt_snprintf(name, sizeof(name), "%s", attr->name);
if ((RT_NULL == attr) || (osPriorityNone == attr->priority))
{
thread_cb->prio = osPriorityNormal;
}
else
{
if ((attr->priority < osPriorityIdle) || (attr->priority > osPriorityISR))
return RT_NULL;
thread_cb->prio = attr->priority;
}
if ((RT_NULL == attr) || (0U == attr->stack_size))
stack_size = DEFAULT_STACK_SIZE;
else
stack_size = attr->stack_size;
if ((RT_NULL == attr) || (RT_NULL == attr->stack_mem))
{
stack = rt_malloc(stack_size);
if (RT_NULL == stack)
{
if (thread_cb->flags & MALLOC_CB)
rt_free(thread_cb);
return RT_NULL;
}
thread_cb->flags |= MALLOC_STACK;
}
else
{
stack = (void *)(attr->stack_mem);
}
if ((RT_NULL != attr) && (0 != attr->attr_bits))
thread_cb->flags |= attr->attr_bits;
rtt_prio = (osPriorityISR - thread_cb->prio) * RT_THREAD_PRIORITY_MAX / osPriorityISR;
rt_thread_init(&(thread_cb->thread), name, func, argument, stack, stack_size, rtt_prio, DEFAULT_TICK);
if (thread_cb->flags & osThreadJoinable)
{
thread_cb->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
if (RT_NULL == thread_cb->joinable_sem)
{
if (thread_cb->flags & MALLOC_CB)
rt_free(thread_cb);
if (thread_cb->flags & MALLOC_STACK)
rt_free(stack);
return RT_NULL;
}
}
else
thread_cb->joinable_sem = RT_NULL;
thread_cb->thread.cleanup = thread_cleanup;
thread_cb->thread.user_data = (rt_uint32_t)thread_cb;
rt_thread_startup(&(thread_cb->thread));
return thread_cb;
}
/// Get name of a thread.
/// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
/// \return name as NULL terminated string.
const char *osThreadGetName(osThreadId_t thread_id)
{
thread_cb_t *thread_cb = (thread_cb_t *)thread_id;
/* Check parameters */
if ((RT_NULL == thread_cb) || (rt_object_get_type((rt_object_t)(&thread_cb->thread)) != RT_Object_Class_Thread))
{
return RT_NULL;
}
return THREAD_NAME(thread_cb);
}
/// Return the thread ID of the current running thread.
/// \return thread ID for reference by other functions or NULL in case of error.
osThreadId_t osThreadGetId(void)
{
rt_thread_t thread;
thread = rt_thread_self();
return (osThreadId_t)(thread->user_data);
}
/// Get current thread state of a thread.
/// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
/// \return current thread state of the specified thread.
osThreadState_t osThreadGetState(osThreadId_t thread_id)
{
osThreadState_t state;
thread_cb_t *thread_cb = (thread_cb_t *)thread_id;
/* Check parameters */
if ((RT_NULL == thread_cb) || (rt_object_get_type((rt_object_t)(&thread_cb->thread)) != RT_Object_Class_Thread))
{
return osThreadError;
}
switch (thread_cb->thread.stat)
{
case RT_THREAD_INIT:
state = osThreadInactive;
break;
case RT_THREAD_READY:
state = osThreadReady;
break;
case RT_THREAD_SUSPEND:
state = osThreadBlocked;
break;
case RT_THREAD_RUNNING:
state = osThreadRunning;
break;
case RT_THREAD_CLOSE:
state = osThreadTerminated;
break;
default:
state = osThreadError;
break;
}
return state;
}
/// Get stack size of a thread.
/// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
/// \return stack size in bytes.
uint32_t osThreadGetStackSize(osThreadId_t thread_id)
{
thread_cb_t *thread_cb = (thread_cb_t *)thread_id;
/* Check parameters */
if ((RT_NULL == thread_cb) || (rt_object_get_type((rt_object_t)(&thread_cb->thread)) != RT_Object_Class_Thread))
{
return 0U;
}
return ((uint32_t)thread_cb->thread.stack_size);
}
/// Get available stack space of a thread based on stack watermark recording during execution.
/// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
/// \return remaining stack space in bytes.
uint32_t osThreadGetStackSpace(osThreadId_t thread_id)
{
rt_uint8_t *ptr;
thread_cb_t *thread_cb = (thread_cb_t *)thread_id;
/* Check parameters */
if ((RT_NULL == thread_cb) || (rt_object_get_type((rt_object_t)(&thread_cb->thread)) != RT_Object_Class_Thread))
{
return 0U;
}
ptr = (rt_uint8_t *)thread_cb->thread.stack_addr;
while (*ptr == '#')ptr ++;
return (ptr - (rt_uint8_t *)thread_cb->thread.stack_addr);
}
/// Change priority of a thread.
/// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
/// \param[in] priority new priority value for the thread function.
/// \return status code that indicates the execution status of the function.
osStatus_t osThreadSetPriority(osThreadId_t thread_id, osPriority_t priority)
{
rt_uint8_t rt_priority;
thread_cb_t *thread_cb = (thread_cb_t *)thread_id;
// Check parameters
if ((RT_NULL == thread_cb) || (priority < osPriorityIdle) || (priority > osPriorityISR))
{
return osErrorParameter;
}
thread_cb->prio = priority;
rt_priority = (osPriorityISR - thread_cb->prio) * RT_THREAD_PRIORITY_MAX / osPriorityISR;
rt_thread_control(&(thread_cb->thread), RT_THREAD_CTRL_CHANGE_PRIORITY, &rt_priority);
return osOK;
}
/// Get current priority of a thread.
/// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
/// \return current priority value of the specified thread.
osPriority_t osThreadGetPriority(osThreadId_t thread_id)
{
thread_cb_t *thread_cb = (thread_cb_t *)thread_id;
/* Check parameters */
if ((RT_NULL == thread_cb) || (rt_object_get_type((rt_object_t)(&thread_cb->thread)) != RT_Object_Class_Thread))
{
return osPriorityError;
}
return (osPriority_t)thread_cb->prio;
}
/// Pass control to next thread that is in state \b READY.
/// \return status code that indicates the execution status of the function.
osStatus_t osThreadYield(void)
{
rt_thread_yield();
return osOK;
}
/// Suspend execution of a thread.
/// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
/// \return status code that indicates the execution status of the function.
osStatus_t osThreadSuspend(osThreadId_t thread_id)
{
rt_err_t result;
thread_cb_t *thread_cb = (thread_cb_t *)thread_id;
/* Check parameters */
if (RT_NULL == thread_cb)
{
return osErrorParameter;
}
result = rt_thread_suspend(&(thread_cb->thread));
if (RT_EOK == result)
return osOK;
else
return osError;
}
/// Resume execution of a thread.
/// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
/// \return status code that indicates the execution status of the function.
osStatus_t osThreadResume(osThreadId_t thread_id)
{
rt_err_t result;
thread_cb_t *thread_cb = (thread_cb_t *)thread_id;
/* Check parameters */
if (RT_NULL == thread_cb)
{
return osErrorParameter;
}
result = rt_thread_resume(&(thread_cb->thread));
if (RT_EOK == result)
return osOK;
else
return osError;
}
/// Detach a thread (thread storage can be reclaimed when thread terminates).
/// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
/// \return status code that indicates the execution status of the function.
osStatus_t osThreadDetach(osThreadId_t thread_id)
{
thread_cb_t *thread_cb = (thread_cb_t *)thread_id;
/* Check parameters */
if ((RT_NULL == thread_cb) || (rt_object_get_type((rt_object_t)(&thread_cb->thread)) != RT_Object_Class_Thread))
{
return osErrorParameter;
}
/* Check object attributes */
/*
if ((thread_cb->flags & osThreadJoinable) == 0)
{
return osErrorResource;
}
*/
if ((thread_cb->thread.stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
{
rt_thread_detach(&(thread_cb->thread));
if (thread_cb->flags & osThreadJoinable)
rt_sem_delete(thread_cb->joinable_sem);
if (thread_cb->flags & MALLOC_STACK)
rt_free(thread_cb->thread.stack_addr);
if (thread_cb->flags & MALLOC_CB)
rt_free(thread_cb);
}
else
{
rt_enter_critical();
/* change to detach state */
thread_cb->flags &= ~osThreadJoinable;
/* delete joinable semaphore */
if (RT_NULL != thread_cb->joinable_sem)
{
rt_sem_delete(thread_cb->joinable_sem);
thread_cb->joinable_sem = RT_NULL;
}
/* detach thread object */
rt_thread_detach(&thread_cb->thread);
rt_exit_critical();
}
return osOK;
}
/// Wait for specified thread to terminate.
/// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
/// \return status code that indicates the execution status of the function.
osStatus_t osThreadJoin(osThreadId_t thread_id)
{
rt_err_t result;
thread_cb_t *thread_cb = (thread_cb_t *)thread_id;
/* Check parameters */
if (RT_NULL == thread_cb)
{
return osErrorParameter;
}
if (((&thread_cb->thread) == rt_thread_self()) ||
(0 == (thread_cb->flags & osThreadJoinable)))
{
/* join self or join a detached thread*/
return osErrorResource;
}
result = rt_sem_take(thread_cb->joinable_sem, RT_WAITING_FOREVER);
if (RT_EOK == result)
{
/* release resource */
if (thread_cb->flags & osThreadJoinable)
rt_sem_delete(thread_cb->joinable_sem);
if (thread_cb->flags & MALLOC_STACK)
rt_free(thread_cb->thread.stack_addr);
if (thread_cb->flags & MALLOC_CB)
rt_free(thread_cb);
}
else
return osError;
return osOK;
}
/// Terminate execution of current running thread.
__NO_RETURN void osThreadExit(void)
{
rt_thread_t self = rt_thread_self();
rt_thread_control(self, RT_THREAD_CTRL_CLOSE, RT_NULL);
RT_ASSERT(0);
while(1);
}
/// Terminate execution of a thread.
/// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
/// \return status code that indicates the execution status of the function.
osStatus_t osThreadTerminate(osThreadId_t thread_id)
{
thread_cb_t *thread_cb;
thread_cb = (thread_cb_t *)(rt_thread_self()->user_data);
/* Check parameters */
if (RT_NULL == thread_cb)
{
return osErrorParameter;
}
rt_thread_detach(&(thread_cb->thread));
rt_schedule();
return osOK;
}
/// Get number of active threads.
/// \return number of active threads.
uint32_t osThreadGetCount(void)
{
rt_uint32_t thread_count = 0U;
struct rt_object_information *info;
info = rt_object_get_information(RT_Object_Class_Thread);
rt_enter_critical();
thread_count = rt_list_len(&(info->object_list));
rt_exit_critical();
return thread_count;
}
/// Enumerate active threads.
/// \param[out] thread_array pointer to array for retrieving thread IDs.
/// \param[in] array_items maximum number of items in array for retrieving thread IDs.
/// \return number of enumerated threads.
uint32_t osThreadEnumerate(osThreadId_t *thread_array, uint32_t array_items)
{
rt_uint32_t thread_count = 0U;
rt_thread_t thread;
struct rt_object_information *info;
struct rt_list_node *node;
/* Check parameters */
if ((RT_NULL == thread_array) || (0U == array_items))
{
return 0U;
}
info = rt_object_get_information(RT_Object_Class_Thread);
rt_enter_critical();
for (node = info->object_list.next; node != &(info->object_list); node = node->next)
{
thread = thread_rt_list_entry(node, rt_thread);
thread_array[thread_count] = (osThreadId_t)thread;
thread_count++;
if (thread_count >= array_items)
break;
}
rt_exit_critical();
return thread_count;
}
// ==== Thread Flags Functions ====
/// Set the specified Thread Flags of a thread.
/// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
/// \param[in] flags specifies the flags of the thread that shall be set.
/// \return thread flags after setting or error code if highest bit set.
uint32_t osThreadFlagsSet(osThreadId_t thread_id, uint32_t flags)
{
register rt_base_t status;
register rt_ubase_t level;
rt_bool_t need_schedule = RT_FALSE;
thread_cb_t *thread_cb;
rt_uint32_t return_value;
thread_cb = (thread_cb_t *)(thread_id);
/* Check parameters */
if ((RT_NULL == thread_cb) || (rt_object_get_type((rt_object_t)(&thread_cb->thread)) != RT_Object_Class_Thread))
{
return osFlagsErrorParameter;
}
level = rt_hw_interrupt_disable();
thread_cb->flag_set |= flags;
return_value = thread_cb->flag_set;
/* Check if Thread is waiting for Thread Flags */
if (thread_cb->thread.event_info & WAITING_THREAD_FLAGS)
{
status = -RT_ERROR;
if (thread_cb->thread.event_info & osFlagsWaitAll)
{
if ((thread_cb->thread.event_set & thread_cb->flag_set) == thread_cb->thread.event_set)
{
/* received an AND event */
status = RT_EOK;
}
}
else
{
if (thread_cb->thread.event_set & thread_cb->flag_set)
{
/* save recieved event set */
thread_cb->thread.event_set &= thread_cb->flag_set;
/* received an OR event */
status = RT_EOK;
}
}
/* condition is satisfied, resume thread */
if (RT_EOK == status)
{
thread_cb->thread.event_info &= ~WAITING_THREAD_FLAGS;
/* clear event */
if (!(thread_cb->thread.event_info & osFlagsNoClear))
thread_cb->flag_set &= ~thread_cb->thread.event_set;
/* resume thread, and thread list breaks out */
rt_thread_resume(rt_thread_self());
need_schedule = RT_TRUE;
}
}
rt_hw_interrupt_enable(level);
if (need_schedule == RT_TRUE)
rt_schedule();
return return_value;
}
/// Clear the specified Thread Flags of current running thread.
/// \param[in] flags specifies the flags of the thread that shall be cleared.
/// \return thread flags before clearing or error code if highest bit set.
uint32_t osThreadFlagsClear(uint32_t flags)
{
rt_thread_t thread = rt_thread_self();
thread_cb_t *thread_cb;
rt_uint32_t flag;
/* Check parameters */
if (RT_NULL == thread)
{
return osFlagsErrorParameter;
}
thread_cb = (thread_cb_t *)(thread->user_data);
rt_enter_critical();
flag = thread_cb->flag_set;
thread_cb->flag_set &= ~flags;
rt_exit_critical();
return flag;
}
/// Get the current Thread Flags of current running thread.
/// \return current thread flags.
uint32_t osThreadFlagsGet(void)
{
rt_thread_t thread = rt_thread_self();
thread_cb_t *thread_cb;
/* Check parameters */
if (RT_NULL == thread)
{
return osFlagsErrorParameter;
}
thread_cb = (thread_cb_t *)(thread->user_data);
return thread_cb->flag_set;
}
/// Wait for one or more Thread Flags of the current running thread to become signaled.
/// \param[in] flags specifies the flags to wait for.
/// \param[in] options specifies flags options (osFlagsXxxx).
/// \param[in] timeout \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
/// \return thread flags before clearing or error code if highest bit set.
uint32_t osThreadFlagsWait(uint32_t flags, uint32_t options, uint32_t timeout)
{
rt_uint32_t return_value;
register rt_ubase_t level;