1 #include "cmsis_os.h" 2 3 // Kernel Control Public API 4 5 /// Start the RTOS Kernel with executing the specified thread 6 osStatus osKernelStart(void) 7 { 8 rt_system_scheduler_start(); 9 10 return osOK; 11 } 12 13 /// Check if the RTOS kernel is already started 14 int32_t osKernelRunning(void) 15 { 16 return (rt_thread_self() != RT_NULL) ? 1 : 0; 17 } 18 19 // Thread Public API 20 21 /// Create a thread and add it to Active Threads and set it to state READY 22 osThreadId osThreadCreate(osThreadDef_t *thread_def, void *argument) 23 { 24 osThreadId thread; 25 int size; 26 27 size = thread_def->stack_size; 28 if (size == 0) 29 { 30 size = 4096; 31 } 32 33 thread = rt_thread_create(thread_def->name, thread_def->entry, argument, size, thread_def->priority, thread_def->tick); 34 if (thread != RT_NULL) 35 rt_thread_startup(thread); 36 37 return thread; 38 } 39 40 /// Return the thread ID of the current running thread 41 osThreadId osThreadGetId(void) 42 { 43 return rt_thread_self(); 44 } 45 46 /// Terminate execution of a thread and remove it from ActiveThreads 47 osStatus osThreadTerminate(osThreadId thread_id) 48 { 49 rt_err_t result; 50 51 result = rt_thread_delete(thread_id); 52 53 if (result == RT_EOK) 54 return osOK; 55 else 56 return osErrorOS; 57 } 58 59 /// Pass control to next thread that is in state READY 60 osStatus osThreadYield(void) 61 { 62 rt_err_t result; 63 64 result = rt_thread_yield(); 65 66 if (result == RT_EOK) 67 return osOK; 68 else 69 return osErrorOS; 70 } 71 72 /// Change prority of an active thread 73 osStatus osThreadSetPriority(osThreadId thread_id, osPriority priority) 74 { 75 rt_err_t result; 76 77 if (thread_id == RT_NULL) 78 return osErrorOS; 79 80 if (priority < osPriorityIdle || priority > osPriorityRealtime) 81 return osErrorPriority; 82 83 result = rt_thread_control(thread_id, RT_THREAD_CTRL_CHANGE_PRIORITY, &priority); 84 85 if (result == RT_EOK) 86 return osOK; 87 else 88 return osErrorOS; 89 } 90 91 /// Get current prority of an active thread 92 osPriority osThreadGetPriority(osThreadId thread_id) 93 { 94 if (thread_id == RT_NULL) 95 return osPriorityError; 96 97 if ((osPriority)thread_id->current_priority < osPriorityIdle || (osPriority)thread_id->current_priority > osPriorityRealtime) 98 return osPriorityError; 99 100 return thread_id->current_priority; 101 } 102 103 // Generic Wait API 104 105 /// Wait for Timeout (Time Delay) 106 osStatus osDelay(uint32_t millisec) 107 { 108 rt_err_t result; 109 rt_tick_t ticks; 110 111 ticks = rt_tick_from_millisecond(millisec); 112 result = rt_thread_delay(ticks); 113 114 if (result == RT_EOK) 115 return osOK; 116 else 117 return osErrorOS; 118 } 119 120 /// Wait for Signal, Message, Mail, or Timeout 121 osEvent osWait(uint32_t millisec) 122 { 123 rt_err_t result; 124 rt_tick_t ticks; 125 126 ticks = rt_tick_from_millisecond(millisec); 127 result = rt_thread_delay(ticks); 128 /* 129 if (result == RT_EOK) 130 return osOK; 131 else 132 return osErrorOS; 133 */ 134 } 135 136 // Timer Management Public API 137 138 /// Create timer 139 osTimerId osTimerCreate(osTimerDef_t *timer_def, os_timer_type type, void *argument) 140 { 141 uint8_t flag = RT_TIMER_FLAG_SOFT_TIMER; 142 143 if (type == osTimerPeriodic) 144 { 145 flag |= RT_TIMER_FLAG_PERIODIC; 146 } 147 148 return rt_timer_create(timer_def->name, timer_def->timeout, argument, timer_def->time, flag); 149 } 150 151 /// Start or restart timer 152 osStatus osTimerStart(osTimerId timer_id, uint32_t millisec) 153 { 154 rt_err_t result; 155 rt_tick_t ticks; 156 157 ticks = rt_tick_from_millisecond(millisec); 158 rt_timer_control(timer_id, RT_TIMER_CTRL_SET_TIME, &ticks); 159 result = rt_timer_start(timer_id); 160 if (result == RT_EOK) 161 return osOK; 162 else 163 return osErrorOS; 164 } 165 166 /// Stop timer 167 osStatus osTimerStop(osTimerId timer_id) 168 { 169 rt_err_t result; 170 171 result = rt_timer_stop(timer_id); 172 if (result == RT_EOK) 173 return osOK; 174 else 175 return osErrorOS; 176 } 177 178 // Mutex Public API 179 180 /// Create and Initialize a Mutex object 181 osMutexId osMutexCreate(osMutexDef_t *mutex_def) 182 { 183 return rt_mutex_create(mutex_def->name, mutex_def->flag); 184 } 185 186 /// Wait until a Mutex becomes available 187 osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec) 188 { 189 rt_err_t result; 190 rt_tick_t ticks; 191 192 ticks = rt_tick_from_millisecond(millisec); 193 result = rt_mutex_take(mutex_id, ticks); 194 195 if (result == RT_EOK) 196 return osOK; 197 else 198 return osErrorOS; 199 } 200 201 /// Release a Mutex that was obtained with osMutexWait 202 osStatus osMutexRelease(osMutexId mutex_id) 203 { 204 rt_err_t result; 205 206 result = rt_mutex_release(mutex_id); 207 208 if (result == RT_EOK) 209 return osOK; 210 else 211 return osErrorOS; 212 } 213 214 osStatus osMutexDelete (osMutexId mutex_id) 215 { 216 rt_err_t result; 217 218 result = rt_mutex_delete(mutex_id); 219 220 if (result == RT_EOK) 221 return osOK; 222 else 223 return osErrorOS; 224 } 225 226 // Semaphore Public API 227 228 /// Create and Initialize a Semaphore object 229 osSemaphoreId osSemaphoreCreate(osSemaphoreDef_t *semaphore_def, int32_t count) 230 { 231 return rt_sem_create(semaphore_def->name, count, semaphore_def->flag); 232 } 233 234 /// Wait until a Semaphore becomes available 235 int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec) 236 { 237 rt_tick_t ticks; 238 239 if (semaphore_id == RT_NULL) 240 return -1; 241 242 ticks = rt_tick_from_millisecond(millisec); 243 rt_sem_take(semaphore_id, ticks); 244 245 return semaphore_id->value; 246 } 247 248 /// Release a Semaphore 249 osStatus osSemaphoreRelease(osSemaphoreId semaphore_id) 250 { 251 rt_err_t result; 252 253 result = rt_sem_release(semaphore_id); 254 255 if (result == RT_EOK) 256 return osOK; 257 else 258 return osErrorOS; 259 } 260 261 // Memory Management Public API 262 263 /// Create and Initialize memory pool 264 osPoolId osPoolCreate(osPoolDef_t *pool_def) 265 { 266 return rt_mp_create(pool_def->name, pool_def->block_count, pool_def->block_size); 267 } 268 269 /// Allocate a memory block from a memory pool 270 void *osPoolAlloc(osPoolId pool_id) 271 { 272 return rt_mp_alloc(pool_id, 0); 273 } 274 275 /// Allocate a memory block from a memory pool and set memory block to zero 276 void *osPoolCAlloc(osPoolId pool_id) 277 { 278 return RT_NULL; 279 } 280 281 /// Return an allocated memory block back to a specific memory pool 282 osStatus osPoolFree(osPoolId pool_id, void *block) 283 { 284 rt_mp_free(block); 285 286 return osOK; 287 } 288 289 // Message Queue Management Public API 290 291 /// Create and Initialize Message Queue 292 osMessageQId osMessageCreate(osMessageQDef_t *queue_def, osThreadId thread_id) 293 { 294 return rt_mq_create(queue_def->name, queue_def->msg_size, queue_def->max_msgs, queue_def->flag); 295 } 296 297 /// Put a Message to a Queue 298 osStatus osMessagePut(osMessageQId queue_id, uint32_t info, uint32_t millisec) 299 { 300 rt_err_t result; 301 302 result = rt_mq_send(queue_id, &info, 4); 303 304 if (result == RT_EOK) 305 return osOK; 306 else 307 return osErrorOS; 308 } 309 310 /// Get a Message or Wait for a Message from a Queue 311 osEvent osMessageGet(osMessageQId queue_id, uint32_t millisec) 312 { 313 osEvent event; 314 rt_err_t result; 315 rt_tick_t ticks; 316 317 ticks = rt_tick_from_millisecond(millisec); 318 result = rt_mq_recv(queue_id, &event.value, 4, ticks); 319 320 if (result == RT_EOK) 321 { 322 event.status = osEventMessage; 323 } 324 else 325 { 326 event.status = osEventTimeout; 327 } 328 329 return event; 330 } 331 332 // Mail Queue Management Public API 333 334 /// Create and Initialize mail queue 335 osMailQId osMailCreate(osMailQDef_t *queue_def, osThreadId thread_id) 336 { 337 return RT_NULL; 338 } 339 340 /// Allocate a memory block from a mail 341 void *osMailAlloc(osMailQId queue_id, uint32_t millisec) 342 { 343 return RT_NULL; 344 } 345 346 /// Allocate a memory block from a mail and set memory block to zero 347 void *osMailCAlloc(osMailQId queue_id, uint32_t millisec) 348 { 349 return RT_NULL; 350 } 351 352 /// Free a memory block from a mail 353 osStatus osMailFree(osMailQId queue_id, void *mail) 354 { 355 return osErrorOS; 356 } 357 358 /// Put a mail to a queue 359 osStatus osMailPut(osMailQId queue_id, void *mail) 360 { 361 return osErrorOS; 362 } 363 364 /// Get a mail from a queue 365 osEvent osMailGet(osMailQId queue_id, uint32_t millisec) 366 { 367 osEvent ret; 368 369 if (queue_id == NULL) { 370 ret.status = osErrorParameter; 371 return ret; 372 } 373 374 ret = osMessageGet(*((void **)queue_id), millisec); 375 if (ret.status == osEventMessage) ret.status = osEventMail; 376 377 return ret; 378 } 379 380 381