Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cmsis_os2_port.c Source File

cmsis_os2_port.c

00001 /**
00002  * @file    cmsis_os2_port.c
00003  * @brief   
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2019, ARM Limited, All Rights Reserved
00007  * SPDX-License-Identifier: Apache-2.0
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  * not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  * http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include "cmsis_os2.h"
00023 #include "RTL.h"
00024 #include "cortex_m.h"
00025 
00026 #define MAIN_TASK_PRIORITY      (10)
00027 #define MAIN_TASK_STACK         (800)
00028 static uint64_t stk_main_task [MAIN_TASK_STACK / sizeof(uint64_t)];
00029 
00030 #define TIMER_TASK_30_PRIORITY  (11)
00031 #define TIMER_TASK_STACK        (136)
00032 static uint64_t stk_timer_task[TIMER_TASK_STACK / sizeof(uint64_t)];
00033 
00034 static uint32_t taskCount = 0; 
00035 static osTimerFunc_t onlyTimerFunction = NULL;
00036 static uint32_t timerTick = 0;
00037 
00038 static OS_MUT onlyMutex;
00039 
00040 osStatus_t osKernelInitialize(void)
00041 {
00042     taskCount = 0;
00043     return osOK;
00044 }
00045 
00046 osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
00047 {
00048     OS_TID tid = 0;
00049     //first task will init the rtx
00050     if (taskCount == 0) {
00051         os_sys_init_user((void (*)(void))func, MAIN_TASK_PRIORITY, stk_main_task, MAIN_TASK_STACK);
00052     }
00053     else {
00054         tid = os_tsk_create((void (*)(void))func, MAIN_TASK_PRIORITY+1);
00055     }
00056     taskCount++;
00057     return (osThreadId_t) tid;
00058 }
00059 
00060 uint32_t osThreadFlagsSet(osThreadId_t thread_id, uint32_t flags)
00061 {
00062     if (cortex_in_isr()){
00063         isr_evt_set(flags, (OS_TID)thread_id);
00064     }else {
00065         os_evt_set(flags, (OS_TID)thread_id);
00066     }
00067     return flags;
00068 }
00069 
00070 osStatus_t osKernelStart(void)
00071 {
00072     //first thread already started the kernel
00073     return osOK;
00074 }
00075 
00076 // Timer task
00077 static void rt_timer_task(void)
00078 {
00079     os_itv_set(timerTick);
00080     while (1) {
00081         os_itv_wait();
00082         if (onlyTimerFunction) {
00083             onlyTimerFunction(NULL);
00084         }
00085     }
00086 }
00087 
00088 osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
00089 {
00090     OS_TID tid = 0;
00091     onlyTimerFunction = func;
00092     return (osTimerId_t)tid;
00093 }
00094 
00095 osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks)
00096 {
00097     timerTick = ticks;
00098     OS_TID tid = os_tsk_create_user(rt_timer_task, TIMER_TASK_30_PRIORITY, (void *)stk_timer_task, TIMER_TASK_STACK);
00099     return osOK;
00100 }
00101 
00102 uint32_t osThreadFlagsWait(uint32_t flags, uint32_t options, uint32_t timeout)
00103 {
00104     os_evt_wait_or(flags, timeout);
00105     return os_evt_get();
00106 }
00107 
00108 osMutexId_t osMutexNew(const osMutexAttr_t *attr)
00109 {
00110     os_mut_init(onlyMutex);
00111     return (osMutexId_t)onlyMutex;
00112 }
00113 
00114 osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout)
00115 {
00116     os_mut_wait((OS_ID)mutex_id, timeout);
00117     return osOK;
00118 }
00119 
00120 osStatus_t osMutexRelease(osMutexId_t mutex_id)
00121 {
00122     os_mut_release((OS_ID)mutex_id);
00123     return osOK;
00124 }
00125 
00126 osThreadId_t osThreadGetId(void)
00127 {
00128     return (osThreadId_t)os_tsk_self();
00129 }
00130 
00131 osStatus_t osDelay(uint32_t ticks)
00132 {
00133     os_dly_wait(ticks);
00134     return osOK;
00135 }
00136 
00137 uint32_t osKernelGetSysTimerCount(void)
00138 {
00139     return os_time_get();
00140 }
00141