|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <kernel_structs.h> |
| 8 | +#include <atomic.h> |
| 9 | +#include <cmsis_os.h> |
| 10 | + |
| 11 | +static inline int _is_thread_cmsis_inactive(struct k_thread *thread) |
| 12 | +{ |
| 13 | + u8_t state = thread->base.thread_state; |
| 14 | + |
| 15 | + return state & (_THREAD_PRESTART | _THREAD_DEAD); |
| 16 | +} |
| 17 | + |
| 18 | +static inline s32_t zephyr_to_cmsis_priority(u32_t z_prio) |
| 19 | +{ |
| 20 | + return(osPriorityRealtime - z_prio); |
| 21 | +} |
| 22 | + |
| 23 | +static inline u32_t cmsis_to_zephyr_priority(s32_t c_prio) |
| 24 | +{ |
| 25 | + return(osPriorityRealtime - c_prio); |
| 26 | +} |
| 27 | + |
| 28 | +static void zephyr_thread_wrapper(void *arg1, void *arg2, void *arg3) |
| 29 | +{ |
| 30 | + void * (*fun_ptr)(void *) = arg3; |
| 31 | + |
| 32 | + fun_ptr(arg1); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * @brief Create a new thread. |
| 37 | + */ |
| 38 | +osThreadId osThreadCreate(const osThreadDef_t *thread_def, void *arg) |
| 39 | +{ |
| 40 | + struct k_thread *cm_thread; |
| 41 | + u32_t prio; |
| 42 | + k_tid_t tid; |
| 43 | + u32_t stacksz; |
| 44 | + |
| 45 | + k_thread_stack_t |
| 46 | + (*stk_ptr)[K_THREAD_STACK_LEN(CONFIG_CMSIS_THREAD_MAX_STACK_SIZE)]; |
| 47 | + |
| 48 | + __ASSERT(thread_def->stacksize >= 0 && |
| 49 | + thread_def->stacksize <= CONFIG_CMSIS_THREAD_MAX_STACK_SIZE, |
| 50 | + "invalid stack size\n"); |
| 51 | + |
| 52 | + if (thread_def->instances == 0) { |
| 53 | + return NULL; |
| 54 | + } |
| 55 | + |
| 56 | + if (_is_in_isr()) { |
| 57 | + return NULL; |
| 58 | + } |
| 59 | + |
| 60 | + stacksz = thread_def->stacksize; |
| 61 | + if (stacksz == 0) { |
| 62 | + stacksz = CONFIG_CMSIS_THREAD_MAX_STACK_SIZE; |
| 63 | + } |
| 64 | + |
| 65 | + cm_thread = thread_def->cm_thread; |
| 66 | + atomic_dec((atomic_t *)&thread_def->instances); |
| 67 | + stk_ptr = thread_def->stack_mem; |
| 68 | + prio = cmsis_to_zephyr_priority(thread_def->tpriority); |
| 69 | + |
| 70 | + tid = k_thread_create(&cm_thread[thread_def->instances], |
| 71 | + stk_ptr[thread_def->instances], stacksz, |
| 72 | + (k_thread_entry_t)zephyr_thread_wrapper, |
| 73 | + (void *)arg, NULL, thread_def->pthread, |
| 74 | + prio, 0, K_NO_WAIT); |
| 75 | + |
| 76 | + return ((osThreadId)tid); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * @brief Return the thread ID of the current running thread. |
| 81 | + */ |
| 82 | +osThreadId osThreadGetId(void) |
| 83 | +{ |
| 84 | + if (_is_in_isr()) { |
| 85 | + return NULL; |
| 86 | + } |
| 87 | + |
| 88 | + return (osThreadId)k_current_get(); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * @brief Get current priority of an active thread. |
| 93 | + */ |
| 94 | +osPriority osThreadGetPriority(osThreadId thread_id) |
| 95 | +{ |
| 96 | + k_tid_t thread = (k_tid_t)thread_id; |
| 97 | + u32_t priority; |
| 98 | + |
| 99 | + if (_is_in_isr()) { |
| 100 | + return osPriorityError; |
| 101 | + } |
| 102 | + |
| 103 | + priority = k_thread_priority_get(thread); |
| 104 | + return zephyr_to_cmsis_priority(priority); |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * @brief Change priority of an active thread. |
| 109 | + */ |
| 110 | +osStatus osThreadSetPriority(osThreadId thread_id, osPriority priority) |
| 111 | +{ |
| 112 | + if (thread_id == NULL) { |
| 113 | + return osErrorParameter; |
| 114 | + } |
| 115 | + |
| 116 | + if (_is_in_isr()) { |
| 117 | + return osErrorISR; |
| 118 | + } |
| 119 | + |
| 120 | + if (priority < osPriorityIdle || priority > osPriorityRealtime) { |
| 121 | + return osErrorValue; |
| 122 | + } |
| 123 | + |
| 124 | + if (_is_thread_cmsis_inactive((k_tid_t)thread_id)) { |
| 125 | + return osErrorResource; |
| 126 | + } |
| 127 | + |
| 128 | + k_thread_priority_set((k_tid_t)thread_id, |
| 129 | + cmsis_to_zephyr_priority(priority)); |
| 130 | + |
| 131 | + return osOK; |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * @brief Terminate execution of a thread. |
| 136 | + */ |
| 137 | +osStatus osThreadTerminate(osThreadId thread_id) |
| 138 | +{ |
| 139 | + if (thread_id == NULL) { |
| 140 | + return osErrorParameter; |
| 141 | + } |
| 142 | + |
| 143 | + if (_is_in_isr()) { |
| 144 | + return osErrorISR; |
| 145 | + } |
| 146 | + |
| 147 | + if (_is_thread_cmsis_inactive((k_tid_t)thread_id)) { |
| 148 | + return osErrorResource; |
| 149 | + } |
| 150 | + |
| 151 | + k_thread_abort((k_tid_t)thread_id); |
| 152 | + return osOK; |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * @brief Pass control to next thread that is in READY state. |
| 157 | + */ |
| 158 | +osStatus osThreadYield(void) |
| 159 | +{ |
| 160 | + if (_is_in_isr()) { |
| 161 | + return osErrorISR; |
| 162 | + } |
| 163 | + |
| 164 | + k_yield(); |
| 165 | + return osOK; |
| 166 | +} |
0 commit comments