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
|
#include "cmsis_os.h"
// Kernel Control Public API
/// Start the RTOS Kernel with executing the specified thread
osStatus osKernelStart(void)
{
rt_system_scheduler_start();
return osOK;
}
/// Check if the RTOS kernel is already started
int32_t osKernelRunning(void)
{
return (rt_thread_self() != RT_NULL) ? 1 : 0;
}
// Thread Public API
/// Create a thread and add it to Active Threads and set it to state READY
osThreadId osThreadCreate(osThreadDef_t *thread_def, void *argument)
{
osThreadId thread;
int size;
size = thread_def->stack_size;
if (size == 0)
{
size = 4096;
}
thread = rt_thread_create(thread_def->name, thread_def->entry, argument, size, thread_def->priority, thread_def->tick);
if (thread != RT_NULL)
rt_thread_startup(thread);
return thread;
}
/// Return the thread ID of the current running thread
osThreadId osThreadGetId(void)
{
return rt_thread_self();
}
/// Terminate execution of a thread and remove it from ActiveThreads
osStatus osThreadTerminate(osThreadId thread_id)
{
rt_err_t result;
result = rt_thread_delete(thread_id);
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
}
/// Pass control to next thread that is in state READY
osStatus osThreadYield(void)
{
rt_err_t result;
result = rt_thread_yield();
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
}
/// Change prority of an active thread
osStatus osThreadSetPriority(osThreadId thread_id, osPriority priority)
{
rt_err_t result;
if (thread_id == RT_NULL)
return osErrorOS;
if (priority < osPriorityIdle || priority > osPriorityRealtime)
return osErrorPriority;
result = rt_thread_control(thread_id, RT_THREAD_CTRL_CHANGE_PRIORITY, &priority);
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
}
/// Get current prority of an active thread
osPriority osThreadGetPriority(osThreadId thread_id)
{
if (thread_id == RT_NULL)
return osPriorityError;
if ((osPriority)thread_id->current_priority < osPriorityIdle || (osPriority)thread_id->current_priority > osPriorityRealtime)
return osPriorityError;
return thread_id->current_priority;
}
// Generic Wait API
/// Wait for Timeout (Time Delay)
osStatus osDelay(uint32_t millisec)
{
rt_err_t result;
rt_tick_t ticks;
ticks = rt_tick_from_millisecond(millisec);
result = rt_thread_delay(ticks);
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
}
/// Wait for Signal, Message, Mail, or Timeout
osEvent osWait(uint32_t millisec)
{
rt_err_t result;
rt_tick_t ticks;
ticks = rt_tick_from_millisecond(millisec);
result = rt_thread_delay(ticks);
/*
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
*/
}
// Timer Management Public API
/// Create timer
osTimerId osTimerCreate(osTimerDef_t *timer_def, os_timer_type type, void *argument)
{
uint8_t flag = RT_TIMER_FLAG_SOFT_TIMER;
if (type == osTimerPeriodic)
{
flag |= RT_TIMER_FLAG_PERIODIC;
}
return rt_timer_create(timer_def->name, timer_def->timeout, argument, timer_def->time, flag);
}
/// Start or restart timer
osStatus osTimerStart(osTimerId timer_id, uint32_t millisec)
{
rt_err_t result;
rt_tick_t ticks;
ticks = rt_tick_from_millisecond(millisec);
rt_timer_control(timer_id, RT_TIMER_CTRL_SET_TIME, &ticks);
result = rt_timer_start(timer_id);
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
}
/// Stop timer
osStatus osTimerStop(osTimerId timer_id)
{
rt_err_t result;
result = rt_timer_stop(timer_id);
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
}
// Mutex Public API
/// Create and Initialize a Mutex object
osMutexId osMutexCreate(osMutexDef_t *mutex_def)
{
return rt_mutex_create(mutex_def->name, mutex_def->flag);
}
/// Wait until a Mutex becomes available
osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec)
{
rt_err_t result;
rt_tick_t ticks;
ticks = rt_tick_from_millisecond(millisec);
result = rt_mutex_take(mutex_id, ticks);
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
}
/// Release a Mutex that was obtained with osMutexWait
osStatus osMutexRelease(osMutexId mutex_id)
{
rt_err_t result;
result = rt_mutex_release(mutex_id);
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
}
osStatus osMutexDelete (osMutexId mutex_id)
{
rt_err_t result;
result = rt_mutex_delete(mutex_id);
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
}
// Semaphore Public API
/// Create and Initialize a Semaphore object
osSemaphoreId osSemaphoreCreate(osSemaphoreDef_t *semaphore_def, int32_t count)
{
return rt_sem_create(semaphore_def->name, count, semaphore_def->flag);
}
/// Wait until a Semaphore becomes available
int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec)
{
rt_tick_t ticks;
if (semaphore_id == RT_NULL)
return -1;
ticks = rt_tick_from_millisecond(millisec);
rt_sem_take(semaphore_id, ticks);
return semaphore_id->value;
}
/// Release a Semaphore
osStatus osSemaphoreRelease(osSemaphoreId semaphore_id)
{
rt_err_t result;
result = rt_sem_release(semaphore_id);
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
}
// Memory Management Public API
/// Create and Initialize memory pool
osPoolId osPoolCreate(osPoolDef_t *pool_def)
{
return rt_mp_create(pool_def->name, pool_def->block_count, pool_def->block_size);
}
/// Allocate a memory block from a memory pool
void *osPoolAlloc(osPoolId pool_id)
{
return rt_mp_alloc(pool_id, 0);
}
/// Allocate a memory block from a memory pool and set memory block to zero
void *osPoolCAlloc(osPoolId pool_id)
{
return RT_NULL;
}
/// Return an allocated memory block back to a specific memory pool
osStatus osPoolFree(osPoolId pool_id, void *block)
{
rt_mp_free(block);
return osOK;
}
// Message Queue Management Public API
/// Create and Initialize Message Queue
osMessageQId osMessageCreate(osMessageQDef_t *queue_def, osThreadId thread_id)
{
return rt_mq_create(queue_def->name, queue_def->msg_size, queue_def->max_msgs, queue_def->flag);
}
/// Put a Message to a Queue
osStatus osMessagePut(osMessageQId queue_id, uint32_t info, uint32_t millisec)
{
rt_err_t result;
result = rt_mq_send(queue_id, &info, 4);
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
}
/// Get a Message or Wait for a Message from a Queue
osEvent osMessageGet(osMessageQId queue_id, uint32_t millisec)
{
osEvent event;
rt_err_t result;
rt_tick_t ticks;
ticks = rt_tick_from_millisecond(millisec);
result = rt_mq_recv(queue_id, &event.value, 4, ticks);
if (result == RT_EOK)
{
event.status = osEventMessage;
}
else
{
event.status = osEventTimeout;
}
return event;
}
// Mail Queue Management Public API
/// Create and Initialize mail queue
osMailQId osMailCreate(osMailQDef_t *queue_def, osThreadId thread_id)
{
return RT_NULL;
}
/// Allocate a memory block from a mail
void *osMailAlloc(osMailQId queue_id, uint32_t millisec)
{
return RT_NULL;
}
/// Allocate a memory block from a mail and set memory block to zero
void *osMailCAlloc(osMailQId queue_id, uint32_t millisec)
{
return RT_NULL;
}
/// Free a memory block from a mail
osStatus osMailFree(osMailQId queue_id, void *mail)
{
return osErrorOS;
}
/// Put a mail to a queue
osStatus osMailPut(osMailQId queue_id, void *mail)
{
return osErrorOS;
}
/// Get a mail from a queue
osEvent osMailGet(osMailQId queue_id, uint32_t millisec)
{
osEvent ret;
if (queue_id == NULL) {
ret.status = osErrorParameter;
return ret;
}
ret = osMessageGet(*((void **)queue_id), millisec);
if (ret.status == osEventMessage) ret.status = osEventMail;
return ret;
}
|