1 | /*
|
---|
2 | * Copyright (c) 2013-2017 ARM Limited. All rights reserved.
|
---|
3 | *
|
---|
4 | * SPDX-License-Identifier: Apache-2.0
|
---|
5 | *
|
---|
6 | * Licensed under the Apache License, Version 2.0 (the License); you may
|
---|
7 | * not use this file except in compliance with the License.
|
---|
8 | * You may obtain a copy of the License at
|
---|
9 | *
|
---|
10 | * www.apache.org/licenses/LICENSE-2.0
|
---|
11 | *
|
---|
12 | * Unless required by applicable law or agreed to in writing, software
|
---|
13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT
|
---|
14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
15 | * See the License for the specific language governing permissions and
|
---|
16 | * limitations under the License.
|
---|
17 | *
|
---|
18 | * ----------------------------------------------------------------------
|
---|
19 | *
|
---|
20 | * $Date: 10. January 2017
|
---|
21 | * $Revision: V1.2
|
---|
22 | *
|
---|
23 | * Project: CMSIS-RTOS API V1
|
---|
24 | * Title: cmsis_os_v1.c V1 module file
|
---|
25 | *---------------------------------------------------------------------------*/
|
---|
26 |
|
---|
27 | #include <string.h>
|
---|
28 | #include "cmsis_os.h"
|
---|
29 |
|
---|
30 | #if (osCMSIS >= 0x20000U)
|
---|
31 |
|
---|
32 |
|
---|
33 | // Thread
|
---|
34 | osThreadId osThreadCreate (const osThreadDef_t *thread_def, void *argument) {
|
---|
35 |
|
---|
36 | if (thread_def == NULL) {
|
---|
37 | return (osThreadId)NULL;
|
---|
38 | }
|
---|
39 | return osThreadNew((osThreadFunc_t)thread_def->pthread, argument, &thread_def->attr);
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | // Signals
|
---|
44 |
|
---|
45 | #define SignalMask ((1U<<osFeature_Signals)-1U)
|
---|
46 |
|
---|
47 | int32_t osSignalSet (osThreadId thread_id, int32_t signals) {
|
---|
48 | uint32_t flags;
|
---|
49 |
|
---|
50 | flags = osThreadFlagsSet(thread_id, (uint32_t)signals);
|
---|
51 | if ((flags & 0x80000000U) != 0U) {
|
---|
52 | return ((int32_t)0x80000000U);
|
---|
53 | }
|
---|
54 | return ((int32_t)(flags & ~((uint32_t)signals)));
|
---|
55 | }
|
---|
56 |
|
---|
57 | int32_t osSignalClear (osThreadId thread_id, int32_t signals) {
|
---|
58 | uint32_t flags;
|
---|
59 |
|
---|
60 | if (thread_id != osThreadGetId()) {
|
---|
61 | return ((int32_t)0x80000000U);
|
---|
62 | }
|
---|
63 | flags = osThreadFlagsClear((uint32_t)signals);
|
---|
64 | if ((flags & 0x80000000U) != 0U) {
|
---|
65 | return ((int32_t)0x80000000U);
|
---|
66 | }
|
---|
67 | return ((int32_t)flags);
|
---|
68 | }
|
---|
69 |
|
---|
70 | osEvent osSignalWait (int32_t signals, uint32_t millisec) {
|
---|
71 | osEvent event;
|
---|
72 | uint32_t flags;
|
---|
73 |
|
---|
74 | if (signals != 0) {
|
---|
75 | flags = osThreadFlagsWait((uint32_t)signals, osFlagsWaitAll, millisec);
|
---|
76 | } else {
|
---|
77 | flags = osThreadFlagsWait(SignalMask, osFlagsWaitAny, millisec);
|
---|
78 | }
|
---|
79 | if ((flags > 0U) && (flags < 0x80000000U)) {
|
---|
80 | event.status = osEventSignal;
|
---|
81 | event.value.signals = (int32_t)flags;
|
---|
82 | } else {
|
---|
83 | switch ((int32_t)flags) {
|
---|
84 | case osErrorResource:
|
---|
85 | event.status = osOK;
|
---|
86 | break;
|
---|
87 | case osErrorTimeout:
|
---|
88 | event.status = osEventTimeout;
|
---|
89 | break;
|
---|
90 | case osErrorParameter:
|
---|
91 | event.status = osErrorValue;
|
---|
92 | break;
|
---|
93 | default:
|
---|
94 | event.status = (osStatus)flags;
|
---|
95 | break;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | return event;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | // Timer
|
---|
103 | osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument) {
|
---|
104 |
|
---|
105 | if (timer_def == NULL) {
|
---|
106 | return (osTimerId)NULL;
|
---|
107 | }
|
---|
108 | return osTimerNew((osTimerFunc_t)timer_def->ptimer, type, argument, &timer_def->attr);
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | // Mutex
|
---|
113 | osMutexId osMutexCreate (const osMutexDef_t *mutex_def) {
|
---|
114 |
|
---|
115 | if (mutex_def == NULL) {
|
---|
116 | return (osMutexId)NULL;
|
---|
117 | }
|
---|
118 | return osMutexNew(mutex_def);
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | // Semaphore
|
---|
123 |
|
---|
124 | #if (defined (osFeature_Semaphore) && (osFeature_Semaphore != 0U))
|
---|
125 |
|
---|
126 | osSemaphoreId osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count) {
|
---|
127 |
|
---|
128 | if (semaphore_def == NULL) {
|
---|
129 | return (osSemaphoreId)NULL;
|
---|
130 | }
|
---|
131 | return osSemaphoreNew((uint32_t)count, (uint32_t)count, semaphore_def);
|
---|
132 | }
|
---|
133 |
|
---|
134 | int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec) {
|
---|
135 | osStatus_t status;
|
---|
136 | uint32_t count;
|
---|
137 |
|
---|
138 | status = osSemaphoreAcquire(semaphore_id, millisec);
|
---|
139 | switch (status) {
|
---|
140 | case osOK:
|
---|
141 | count = osSemaphoreGetCount(semaphore_id);
|
---|
142 | return ((int32_t)count + 1);
|
---|
143 | case osErrorResource:
|
---|
144 | case osErrorTimeout:
|
---|
145 | return 0;
|
---|
146 | default:
|
---|
147 | break;
|
---|
148 | }
|
---|
149 | return -1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | #endif // Semaphore
|
---|
153 |
|
---|
154 |
|
---|
155 | // Memory Pool
|
---|
156 |
|
---|
157 | #if (defined(osFeature_Pool) && (osFeature_Pool != 0))
|
---|
158 |
|
---|
159 | osPoolId osPoolCreate (const osPoolDef_t *pool_def) {
|
---|
160 |
|
---|
161 | if (pool_def == NULL) {
|
---|
162 | return (osPoolId)NULL;
|
---|
163 | }
|
---|
164 | return ((osPoolId)(osMemoryPoolNew(pool_def->pool_sz, pool_def->item_sz, &pool_def->attr)));
|
---|
165 | }
|
---|
166 |
|
---|
167 | void *osPoolAlloc (osPoolId pool_id) {
|
---|
168 | return osMemoryPoolAlloc((osMemoryPoolId_t)pool_id, 0U);
|
---|
169 | }
|
---|
170 |
|
---|
171 | void *osPoolCAlloc (osPoolId pool_id) {
|
---|
172 | void *block;
|
---|
173 | uint32_t block_size;
|
---|
174 |
|
---|
175 | block_size = osMemoryPoolGetBlockSize((osMemoryPoolId_t)pool_id);
|
---|
176 | if (block_size == 0U) {
|
---|
177 | return NULL;
|
---|
178 | }
|
---|
179 | block = osMemoryPoolAlloc((osMemoryPoolId_t)pool_id, 0U);
|
---|
180 | if (block != NULL) {
|
---|
181 | memset(block, 0, block_size);
|
---|
182 | }
|
---|
183 | return block;
|
---|
184 | }
|
---|
185 |
|
---|
186 | osStatus osPoolFree (osPoolId pool_id, void *block) {
|
---|
187 | return osMemoryPoolFree((osMemoryPoolId_t)pool_id, block);
|
---|
188 | }
|
---|
189 |
|
---|
190 | #endif // Memory Pool
|
---|
191 |
|
---|
192 |
|
---|
193 | // Message Queue
|
---|
194 |
|
---|
195 | #if (defined(osFeature_MessageQ) && (osFeature_MessageQ != 0))
|
---|
196 |
|
---|
197 | osMessageQId osMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id) {
|
---|
198 | (void)thread_id;
|
---|
199 |
|
---|
200 | if (queue_def == NULL) {
|
---|
201 | return (osMessageQId)NULL;
|
---|
202 | }
|
---|
203 | return ((osMessageQId)(osMessageQueueNew(queue_def->queue_sz, sizeof(uint32_t), &queue_def->attr)));
|
---|
204 | }
|
---|
205 |
|
---|
206 | osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
|
---|
207 | return osMessageQueuePut((osMessageQueueId_t)queue_id, &info, 0U, millisec);
|
---|
208 | }
|
---|
209 |
|
---|
210 | osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec) {
|
---|
211 | osStatus_t status;
|
---|
212 | osEvent event;
|
---|
213 | uint32_t message;
|
---|
214 |
|
---|
215 | status = osMessageQueueGet((osMessageQueueId_t)queue_id, &message, NULL, millisec);
|
---|
216 | switch (status) {
|
---|
217 | case osOK:
|
---|
218 | event.status = osEventMessage;
|
---|
219 | event.value.v = message;
|
---|
220 | break;
|
---|
221 | case osErrorResource:
|
---|
222 | event.status = osOK;
|
---|
223 | break;
|
---|
224 | case osErrorTimeout:
|
---|
225 | event.status = osEventTimeout;
|
---|
226 | break;
|
---|
227 | default:
|
---|
228 | event.status = status;
|
---|
229 | break;
|
---|
230 | }
|
---|
231 | return event;
|
---|
232 | }
|
---|
233 |
|
---|
234 | #endif // Message Queue
|
---|
235 |
|
---|
236 |
|
---|
237 | // Mail Queue
|
---|
238 |
|
---|
239 | #if (defined(osFeature_MailQ) && (osFeature_MailQ != 0))
|
---|
240 |
|
---|
241 | typedef struct os_mail_queue_s {
|
---|
242 | osMemoryPoolId_t mp_id;
|
---|
243 | osMessageQueueId_t mq_id;
|
---|
244 | } os_mail_queue_t;
|
---|
245 |
|
---|
246 | osMailQId osMailCreate (const osMailQDef_t *queue_def, osThreadId thread_id) {
|
---|
247 | os_mail_queue_t *ptr;
|
---|
248 | (void)thread_id;
|
---|
249 |
|
---|
250 | if (queue_def == NULL) {
|
---|
251 | return (osMailQId)NULL;
|
---|
252 | }
|
---|
253 |
|
---|
254 | ptr = queue_def->mail;
|
---|
255 | if (ptr == NULL) {
|
---|
256 | return (osMailQId)NULL;
|
---|
257 | }
|
---|
258 |
|
---|
259 | ptr->mp_id = osMemoryPoolNew (queue_def->queue_sz, queue_def->item_sz, &queue_def->mp_attr);
|
---|
260 | ptr->mq_id = osMessageQueueNew(queue_def->queue_sz, sizeof(void *), &queue_def->mq_attr);
|
---|
261 | if ((ptr->mp_id == (osMemoryPoolId_t)NULL) || (ptr->mq_id == (osMessageQueueId_t)NULL)) {
|
---|
262 | if (ptr->mp_id != (osMemoryPoolId_t)NULL) {
|
---|
263 | osMemoryPoolDelete(ptr->mp_id);
|
---|
264 | }
|
---|
265 | if (ptr->mq_id != (osMessageQueueId_t)NULL) {
|
---|
266 | osMessageQueueDelete(ptr->mq_id);
|
---|
267 | }
|
---|
268 | return (osMailQId)NULL;
|
---|
269 | }
|
---|
270 |
|
---|
271 | return (osMailQId)ptr;
|
---|
272 | }
|
---|
273 |
|
---|
274 | void *osMailAlloc (osMailQId queue_id, uint32_t millisec) {
|
---|
275 | os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
|
---|
276 |
|
---|
277 | if (ptr == NULL) {
|
---|
278 | return NULL;
|
---|
279 | }
|
---|
280 | return osMemoryPoolAlloc(ptr->mp_id, millisec);
|
---|
281 | }
|
---|
282 |
|
---|
283 | void *osMailCAlloc (osMailQId queue_id, uint32_t millisec) {
|
---|
284 | os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
|
---|
285 | void *block;
|
---|
286 | uint32_t block_size;
|
---|
287 |
|
---|
288 | if (ptr == NULL) {
|
---|
289 | return NULL;
|
---|
290 | }
|
---|
291 | block_size = osMemoryPoolGetBlockSize(ptr->mp_id);
|
---|
292 | if (block_size == 0U) {
|
---|
293 | return NULL;
|
---|
294 | }
|
---|
295 | block = osMemoryPoolAlloc(ptr->mp_id, millisec);
|
---|
296 | if (block != NULL) {
|
---|
297 | memset(block, 0, block_size);
|
---|
298 | }
|
---|
299 |
|
---|
300 | return block;
|
---|
301 |
|
---|
302 | }
|
---|
303 |
|
---|
304 | osStatus osMailPut (osMailQId queue_id, const void *mail) {
|
---|
305 | os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
|
---|
306 |
|
---|
307 | if (ptr == NULL) {
|
---|
308 | return osErrorParameter;
|
---|
309 | }
|
---|
310 | if (mail == NULL) {
|
---|
311 | return osErrorValue;
|
---|
312 | }
|
---|
313 | return osMessageQueuePut(ptr->mq_id, &mail, 0U, 0U);
|
---|
314 | }
|
---|
315 |
|
---|
316 | osEvent osMailGet (osMailQId queue_id, uint32_t millisec) {
|
---|
317 | os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
|
---|
318 | osStatus_t status;
|
---|
319 | osEvent event;
|
---|
320 | void *mail;
|
---|
321 |
|
---|
322 | if (ptr == NULL) {
|
---|
323 | event.status = osErrorParameter;
|
---|
324 | return event;
|
---|
325 | }
|
---|
326 |
|
---|
327 | status = osMessageQueueGet(ptr->mq_id, &mail, NULL, millisec);
|
---|
328 | switch (status) {
|
---|
329 | case osOK:
|
---|
330 | event.status = osEventMail;
|
---|
331 | event.value.p = mail;
|
---|
332 | break;
|
---|
333 | case osErrorResource:
|
---|
334 | event.status = osOK;
|
---|
335 | break;
|
---|
336 | case osErrorTimeout:
|
---|
337 | event.status = osEventTimeout;
|
---|
338 | break;
|
---|
339 | default:
|
---|
340 | event.status = status;
|
---|
341 | break;
|
---|
342 | }
|
---|
343 | return event;
|
---|
344 | }
|
---|
345 |
|
---|
346 | osStatus osMailFree (osMailQId queue_id, void *mail) {
|
---|
347 | os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
|
---|
348 |
|
---|
349 | if (ptr == NULL) {
|
---|
350 | return osErrorParameter;
|
---|
351 | }
|
---|
352 | if (mail == NULL) {
|
---|
353 | return osErrorValue;
|
---|
354 | }
|
---|
355 | return osMemoryPoolFree(ptr->mp_id, mail);
|
---|
356 | }
|
---|
357 |
|
---|
358 | #endif // Mail Queue
|
---|
359 |
|
---|
360 |
|
---|
361 | #endif // osCMSIS
|
---|