1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/atomic.h>
9 #include <cmsis_os.h>
10 
11 #define TOTAL_CMSIS_THREAD_PRIORITIES (osPriorityRealtime - osPriorityIdle + 1)
12 
_is_thread_cmsis_inactive(struct k_thread * thread)13 static inline int _is_thread_cmsis_inactive(struct k_thread *thread)
14 {
15 	uint8_t state = thread->base.thread_state;
16 
17 	return state & (_THREAD_PRESTART | _THREAD_DEAD);
18 }
19 
zephyr_to_cmsis_priority(uint32_t z_prio)20 static inline int32_t zephyr_to_cmsis_priority(uint32_t z_prio)
21 {
22 	return(osPriorityRealtime - z_prio);
23 }
24 
cmsis_to_zephyr_priority(int32_t c_prio)25 static inline uint32_t cmsis_to_zephyr_priority(int32_t c_prio)
26 {
27 	return(osPriorityRealtime - c_prio);
28 }
29 
zephyr_thread_wrapper(void * arg1,void * arg2,void * arg3)30 static void zephyr_thread_wrapper(void *arg1, void *arg2, void *arg3)
31 {
32 	void * (*fun_ptr)(void *) = arg3;
33 
34 	fun_ptr(arg1);
35 }
36 
37 /* clear related bit in cmsis thread status bitarray
38  * when terminating a thread
39  */
z_thread_cmsis_status_mask_clear(struct k_thread * thread)40 void z_thread_cmsis_status_mask_clear(struct k_thread *thread)
41 {
42 	uint32_t offset, instance;
43 
44 	osThreadDef_t *thread_def = (osThreadDef_t *)(thread->custom_data);
45 
46 	if (thread_def != NULL) {
47 		/* get thread instance index according to stack address */
48 		offset = thread->stack_info.start - (uintptr_t)thread_def->stack_mem;
49 		instance = offset / K_THREAD_STACK_LEN(CONFIG_CMSIS_THREAD_MAX_STACK_SIZE);
50 		sys_bitarray_clear_bit((sys_bitarray_t *)(thread_def->status_mask), instance);
51 	}
52 }
53 
54 /**
55  * @brief Create a new thread.
56  */
osThreadCreate(const osThreadDef_t * thread_def,void * arg)57 osThreadId osThreadCreate(const osThreadDef_t *thread_def, void *arg)
58 {
59 	struct k_thread *cm_thread;
60 	uint32_t prio;
61 	k_tid_t tid;
62 	uint32_t stacksz;
63 	int ret;
64 	size_t instance;
65 
66 	k_thread_stack_t
67 	   (*stk_ptr)[K_THREAD_STACK_LEN(CONFIG_CMSIS_THREAD_MAX_STACK_SIZE)];
68 
69 	if ((thread_def == NULL) || (thread_def->instances == 0)) {
70 		return NULL;
71 	}
72 
73 	BUILD_ASSERT(
74 		CONFIG_NUM_PREEMPT_PRIORITIES >= TOTAL_CMSIS_THREAD_PRIORITIES,
75 		"Configure NUM_PREEMPT_PRIORITIES to at least"
76 		" TOTAL_CMSIS_THREAD_PRIORITIES");
77 
78 	__ASSERT(thread_def->stacksize <= CONFIG_CMSIS_THREAD_MAX_STACK_SIZE,
79 		 "invalid stack size\n");
80 
81 	if (k_is_in_isr()) {
82 		return NULL;
83 	}
84 
85 	__ASSERT((thread_def->tpriority >= osPriorityIdle) &&
86 		 (thread_def->tpriority <= osPriorityRealtime),
87 		 "invalid priority\n");
88 
89 	/* get an available thread instance */
90 	ret = sys_bitarray_alloc((sys_bitarray_t *)(thread_def->status_mask),
91 			1, &instance);
92 	if (ret != 0) {
93 		return NULL;
94 	}
95 
96 	stacksz = thread_def->stacksize;
97 	if (stacksz == 0U) {
98 		stacksz = CONFIG_CMSIS_THREAD_MAX_STACK_SIZE;
99 	}
100 
101 	k_poll_signal_init(thread_def->poll_signal);
102 	k_poll_event_init(thread_def->poll_event, K_POLL_TYPE_SIGNAL,
103 			K_POLL_MODE_NOTIFY_ONLY, thread_def->poll_signal);
104 
105 	cm_thread = thread_def->cm_thread;
106 	stk_ptr = thread_def->stack_mem;
107 	prio = cmsis_to_zephyr_priority(thread_def->tpriority);
108 	k_thread_custom_data_set((void *)thread_def);
109 
110 	tid = k_thread_create(&cm_thread[instance],
111 			stk_ptr[instance], stacksz,
112 			(k_thread_entry_t)zephyr_thread_wrapper,
113 			(void *)arg, NULL, thread_def->pthread,
114 			prio, 0, K_NO_WAIT);
115 
116 	/* make custom_data pointer of thread point to its source thread_def,
117 	 * then we can use it to release thread instances
118 	 * when terminating threads
119 	 */
120 	tid->custom_data = (void *)thread_def;
121 
122 	return ((osThreadId)tid);
123 }
124 
125 /**
126  * @brief Return the thread ID of the current running thread.
127  */
osThreadGetId(void)128 osThreadId osThreadGetId(void)
129 {
130 	if (k_is_in_isr()) {
131 		return NULL;
132 	}
133 
134 	return (osThreadId)k_current_get();
135 }
136 
137 /**
138  * @brief Get current priority of an active thread.
139  */
osThreadGetPriority(osThreadId thread_id)140 osPriority osThreadGetPriority(osThreadId thread_id)
141 {
142 	k_tid_t thread = (k_tid_t)thread_id;
143 	uint32_t priority;
144 
145 	if ((thread_id == NULL) || (k_is_in_isr())) {
146 		return osPriorityError;
147 	}
148 
149 	priority = k_thread_priority_get(thread);
150 	return zephyr_to_cmsis_priority(priority);
151 }
152 
153 /**
154  * @brief Change priority of an active thread.
155  */
osThreadSetPriority(osThreadId thread_id,osPriority priority)156 osStatus osThreadSetPriority(osThreadId thread_id, osPriority priority)
157 {
158 	if (thread_id == NULL) {
159 		return osErrorParameter;
160 	}
161 
162 	if (k_is_in_isr()) {
163 		return osErrorISR;
164 	}
165 
166 	if (priority < osPriorityIdle || priority > osPriorityRealtime) {
167 		return osErrorValue;
168 	}
169 
170 	if (_is_thread_cmsis_inactive((k_tid_t)thread_id)) {
171 		return osErrorResource;
172 	}
173 
174 	k_thread_priority_set((k_tid_t)thread_id,
175 				cmsis_to_zephyr_priority(priority));
176 
177 	return osOK;
178 }
179 
180 /**
181  * @brief Terminate execution of a thread.
182  */
osThreadTerminate(osThreadId thread_id)183 osStatus osThreadTerminate(osThreadId thread_id)
184 {
185 	if (thread_id == NULL) {
186 		return osErrorParameter;
187 	}
188 
189 	if (k_is_in_isr()) {
190 		return osErrorISR;
191 	}
192 
193 	if (_is_thread_cmsis_inactive((k_tid_t)thread_id)) {
194 		return osErrorResource;
195 	}
196 
197 	k_thread_abort((k_tid_t)thread_id);
198 	return osOK;
199 }
200 
201 /**
202  * @brief Pass control to next thread that is in READY state.
203  */
osThreadYield(void)204 osStatus osThreadYield(void)
205 {
206 	if (k_is_in_isr()) {
207 		return osErrorISR;
208 	}
209 
210 	k_yield();
211 	return osOK;
212 }
213