mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 2 * RL-ARM - RTX
elessair 0:f269e3021894 3 *----------------------------------------------------------------------------
elessair 0:f269e3021894 4 * Name: rt_CMSIS.c
elessair 0:f269e3021894 5 * Purpose: CMSIS RTOS API
elessair 0:f269e3021894 6 * Rev.: V4.60
elessair 0:f269e3021894 7 *----------------------------------------------------------------------------
elessair 0:f269e3021894 8 *
elessair 0:f269e3021894 9 * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
elessair 0:f269e3021894 10 * All rights reserved.
elessair 0:f269e3021894 11 * Redistribution and use in source and binary forms, with or without
elessair 0:f269e3021894 12 * modification, are permitted provided that the following conditions are met:
elessair 0:f269e3021894 13 * - Redistributions of source code must retain the above copyright
elessair 0:f269e3021894 14 * notice, this list of conditions and the following disclaimer.
elessair 0:f269e3021894 15 * - Redistributions in binary form must reproduce the above copyright
elessair 0:f269e3021894 16 * notice, this list of conditions and the following disclaimer in the
elessair 0:f269e3021894 17 * documentation and/or other materials provided with the distribution.
elessair 0:f269e3021894 18 * - Neither the name of ARM nor the names of its contributors may be used
elessair 0:f269e3021894 19 * to endorse or promote products derived from this software without
elessair 0:f269e3021894 20 * specific prior written permission.
elessair 0:f269e3021894 21 *
elessair 0:f269e3021894 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
elessair 0:f269e3021894 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
elessair 0:f269e3021894 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
elessair 0:f269e3021894 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
elessair 0:f269e3021894 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
elessair 0:f269e3021894 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
elessair 0:f269e3021894 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
elessair 0:f269e3021894 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
elessair 0:f269e3021894 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
elessair 0:f269e3021894 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
elessair 0:f269e3021894 32 * POSSIBILITY OF SUCH DAMAGE.
elessair 0:f269e3021894 33 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 34
elessair 0:f269e3021894 35 #define __CMSIS_GENERIC
elessair 0:f269e3021894 36
elessair 0:f269e3021894 37 #include "core_arm7.h"
elessair 0:f269e3021894 38
elessair 0:f269e3021894 39 #include "rt_TypeDef.h"
elessair 0:f269e3021894 40 #include "RTX_Conf.h"
elessair 0:f269e3021894 41 #include "rt_System.h"
elessair 0:f269e3021894 42 #include "rt_Task.h"
elessair 0:f269e3021894 43 #include "rt_Event.h"
elessair 0:f269e3021894 44 #include "rt_List.h"
elessair 0:f269e3021894 45 #include "rt_Time.h"
elessair 0:f269e3021894 46 #include "rt_Mutex.h"
elessair 0:f269e3021894 47 #include "rt_Semaphore.h"
elessair 0:f269e3021894 48 #include "rt_Mailbox.h"
elessair 0:f269e3021894 49 #include "rt_MemBox.h"
elessair 0:f269e3021894 50 #include "rt_HAL_CM.h"
elessair 0:f269e3021894 51
elessair 0:f269e3021894 52 #define os_thread_cb OS_TCB
elessair 0:f269e3021894 53
elessair 0:f269e3021894 54 #include "cmsis_os.h"
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 #if (osFeature_Signals != 16)
elessair 0:f269e3021894 57 #error Invalid "osFeature_Signals" value!
elessair 0:f269e3021894 58 #endif
elessair 0:f269e3021894 59 #if (osFeature_Semaphore > 65535)
elessair 0:f269e3021894 60 #error Invalid "osFeature_Semaphore" value!
elessair 0:f269e3021894 61 #endif
elessair 0:f269e3021894 62 #if (osFeature_Wait != 0)
elessair 0:f269e3021894 63 #error osWait not supported!
elessair 0:f269e3021894 64 #endif
elessair 0:f269e3021894 65
elessair 0:f269e3021894 66
elessair 0:f269e3021894 67 // ==== Enumeration, structures, defines ====
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 // Service Calls defines
elessair 0:f269e3021894 70
elessair 0:f269e3021894 71 #if defined (__CC_ARM) /* ARM Compiler */
elessair 0:f269e3021894 72
elessair 0:f269e3021894 73 #define __NO_RETURN __declspec(noreturn)
elessair 0:f269e3021894 74
elessair 0:f269e3021894 75 #define osEvent_type osEvent
elessair 0:f269e3021894 76 #define osEvent_ret_status ret
elessair 0:f269e3021894 77 #define osEvent_ret_value ret
elessair 0:f269e3021894 78 #define osEvent_ret_msg ret
elessair 0:f269e3021894 79 #define osEvent_ret_mail ret
elessair 0:f269e3021894 80
elessair 0:f269e3021894 81 #define osCallback_type osCallback
elessair 0:f269e3021894 82 #define osCallback_ret ret
elessair 0:f269e3021894 83
elessair 0:f269e3021894 84 #define SVC_0_1(f,t,...) \
elessair 0:f269e3021894 85 __svc_indirect(0) t _##f (t(*)()); \
elessair 0:f269e3021894 86 t f (void); \
elessair 0:f269e3021894 87 __attribute__((always_inline)) \
elessair 0:f269e3021894 88 static __inline t __##f (void) { \
elessair 0:f269e3021894 89 return _##f(f); \
elessair 0:f269e3021894 90 }
elessair 0:f269e3021894 91
elessair 0:f269e3021894 92 #define SVC_1_1(f,t,t1,...) \
elessair 0:f269e3021894 93 __svc_indirect(0) t _##f (t(*)(t1),t1); \
elessair 0:f269e3021894 94 t f (t1 a1); \
elessair 0:f269e3021894 95 __attribute__((always_inline)) \
elessair 0:f269e3021894 96 static __inline t __##f (t1 a1) { \
elessair 0:f269e3021894 97 return _##f(f,a1); \
elessair 0:f269e3021894 98 }
elessair 0:f269e3021894 99
elessair 0:f269e3021894 100 #define SVC_2_1(f,t,t1,t2,...) \
elessair 0:f269e3021894 101 __svc_indirect(0) t _##f (t(*)(t1,t2),t1,t2); \
elessair 0:f269e3021894 102 t f (t1 a1, t2 a2); \
elessair 0:f269e3021894 103 __attribute__((always_inline)) \
elessair 0:f269e3021894 104 static __inline t __##f (t1 a1, t2 a2) { \
elessair 0:f269e3021894 105 return _##f(f,a1,a2); \
elessair 0:f269e3021894 106 }
elessair 0:f269e3021894 107
elessair 0:f269e3021894 108 #define SVC_3_1(f,t,t1,t2,t3,...) \
elessair 0:f269e3021894 109 __svc_indirect(0) t _##f (t(*)(t1,t2,t3),t1,t2,t3); \
elessair 0:f269e3021894 110 t f (t1 a1, t2 a2, t3 a3); \
elessair 0:f269e3021894 111 __attribute__((always_inline)) \
elessair 0:f269e3021894 112 static __inline t __##f (t1 a1, t2 a2, t3 a3) { \
elessair 0:f269e3021894 113 return _##f(f,a1,a2,a3); \
elessair 0:f269e3021894 114 }
elessair 0:f269e3021894 115
elessair 0:f269e3021894 116 #define SVC_4_1(f,t,t1,t2,t3,t4,...) \
elessair 0:f269e3021894 117 __svc_indirect(0) t _##f (t(*)(t1,t2,t3,t4),t1,t2,t3,t4); \
elessair 0:f269e3021894 118 t f (t1 a1, t2 a2, t3 a3, t4 a4); \
elessair 0:f269e3021894 119 __attribute__((always_inline)) \
elessair 0:f269e3021894 120 static __inline t __##f (t1 a1, t2 a2, t3 a3, t4 a4) { \
elessair 0:f269e3021894 121 return _##f(f,a1,a2,a3,a4); \
elessair 0:f269e3021894 122 }
elessair 0:f269e3021894 123
elessair 0:f269e3021894 124 #define SVC_1_2 SVC_1_1
elessair 0:f269e3021894 125 #define SVC_1_3 SVC_1_1
elessair 0:f269e3021894 126 #define SVC_2_3 SVC_2_1
elessair 0:f269e3021894 127
elessair 0:f269e3021894 128 #elif defined (__GNUC__) /* GNU Compiler */
elessair 0:f269e3021894 129
elessair 0:f269e3021894 130 #define __NO_RETURN __attribute__((noreturn))
elessair 0:f269e3021894 131
elessair 0:f269e3021894 132 typedef uint32_t __attribute__((vector_size(8))) ret64;
elessair 0:f269e3021894 133 typedef uint32_t __attribute__((vector_size(16))) ret128;
elessair 0:f269e3021894 134
elessair 0:f269e3021894 135 #define RET_pointer __r0
elessair 0:f269e3021894 136 #define RET_int32_t __r0
elessair 0:f269e3021894 137 #define RET_osStatus __r0
elessair 0:f269e3021894 138 #define RET_osPriority __r0
elessair 0:f269e3021894 139 #define RET_osEvent {(osStatus)__r0, {(uint32_t)__r1}, {(void *)__r2}}
elessair 0:f269e3021894 140 #define RET_osCallback {(void *)__r0, (void *)__r1}
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 #define osEvent_type ret128
elessair 0:f269e3021894 143 #define osEvent_ret_status (ret128){ret.status}
elessair 0:f269e3021894 144 #define osEvent_ret_value (ret128){ret.status, ret.value.v}
elessair 0:f269e3021894 145 #define osEvent_ret_msg (ret128){ret.status, ret.value.v, (uint32_t)ret.def.message_id}
elessair 0:f269e3021894 146 #define osEvent_ret_mail (ret128){ret.status, ret.value.v, (uint32_t)ret.def.mail_id}
elessair 0:f269e3021894 147
elessair 0:f269e3021894 148 #define osCallback_type ret64
elessair 0:f269e3021894 149 #define osCallback_ret (ret64) {(uint32_t)ret.fp, (uint32_t)ret.arg}
elessair 0:f269e3021894 150
elessair 0:f269e3021894 151 #define SVC_ArgN(n) \
elessair 0:f269e3021894 152 register int __r##n __asm("r"#n);
elessair 0:f269e3021894 153
elessair 0:f269e3021894 154 #define SVC_ArgR(n,t,a) \
elessair 0:f269e3021894 155 register t __r##n __asm("r"#n) = a;
elessair 0:f269e3021894 156
elessair 0:f269e3021894 157 #define SVC_Arg0() \
elessair 0:f269e3021894 158 SVC_ArgN(0) \
elessair 0:f269e3021894 159 SVC_ArgN(1) \
elessair 0:f269e3021894 160 SVC_ArgN(2) \
elessair 0:f269e3021894 161 SVC_ArgN(3)
elessair 0:f269e3021894 162
elessair 0:f269e3021894 163 #define SVC_Arg1(t1) \
elessair 0:f269e3021894 164 SVC_ArgR(0,t1,a1) \
elessair 0:f269e3021894 165 SVC_ArgN(1) \
elessair 0:f269e3021894 166 SVC_ArgN(2) \
elessair 0:f269e3021894 167 SVC_ArgN(3)
elessair 0:f269e3021894 168
elessair 0:f269e3021894 169 #define SVC_Arg2(t1,t2) \
elessair 0:f269e3021894 170 SVC_ArgR(0,t1,a1) \
elessair 0:f269e3021894 171 SVC_ArgR(1,t2,a2) \
elessair 0:f269e3021894 172 SVC_ArgN(2) \
elessair 0:f269e3021894 173 SVC_ArgN(3)
elessair 0:f269e3021894 174
elessair 0:f269e3021894 175 #define SVC_Arg3(t1,t2,t3) \
elessair 0:f269e3021894 176 SVC_ArgR(0,t1,a1) \
elessair 0:f269e3021894 177 SVC_ArgR(1,t2,a2) \
elessair 0:f269e3021894 178 SVC_ArgR(2,t3,a3) \
elessair 0:f269e3021894 179 SVC_ArgN(3)
elessair 0:f269e3021894 180
elessair 0:f269e3021894 181 #define SVC_Arg4(t1,t2,t3,t4) \
elessair 0:f269e3021894 182 SVC_ArgR(0,t1,a1) \
elessair 0:f269e3021894 183 SVC_ArgR(1,t2,a2) \
elessair 0:f269e3021894 184 SVC_ArgR(2,t3,a3) \
elessair 0:f269e3021894 185 SVC_ArgR(3,t4,a4)
elessair 0:f269e3021894 186
elessair 0:f269e3021894 187 #if (defined (__CORTEX_M0)) || defined (__CORTEX_M0PLUS)
elessair 0:f269e3021894 188 #define SVC_Call(f) \
elessair 0:f269e3021894 189 __asm volatile \
elessair 0:f269e3021894 190 ( \
elessair 0:f269e3021894 191 "ldr r7,="#f"\n\t" \
elessair 0:f269e3021894 192 "mov r12,r7\n\t" \
elessair 0:f269e3021894 193 "svc 0" \
elessair 0:f269e3021894 194 : "=r" (__r0), "=r" (__r1), "=r" (__r2), "=r" (__r3) \
elessair 0:f269e3021894 195 : "r" (__r0), "r" (__r1), "r" (__r2), "r" (__r3) \
elessair 0:f269e3021894 196 : "r7", "r12", "lr", "cc" \
elessair 0:f269e3021894 197 );
elessair 0:f269e3021894 198 #else
elessair 0:f269e3021894 199 #define SVC_Call(f) \
elessair 0:f269e3021894 200 __asm volatile \
elessair 0:f269e3021894 201 ( \
elessair 0:f269e3021894 202 "ldr r12,="#f"\n\t" \
elessair 0:f269e3021894 203 "svc 0" \
elessair 0:f269e3021894 204 : "=r" (__r0), "=r" (__r1), "=r" (__r2), "=r" (__r3) \
elessair 0:f269e3021894 205 : "r" (__r0), "r" (__r1), "r" (__r2), "r" (__r3) \
elessair 0:f269e3021894 206 : "r12", "lr", "cc" \
elessair 0:f269e3021894 207 );
elessair 0:f269e3021894 208 #endif
elessair 0:f269e3021894 209
elessair 0:f269e3021894 210 #define SVC_0_1(f,t,rv) \
elessair 0:f269e3021894 211 __attribute__((always_inline)) \
elessair 0:f269e3021894 212 static inline t __##f (void) { \
elessair 0:f269e3021894 213 SVC_Arg0(); \
elessair 0:f269e3021894 214 SVC_Call(f); \
elessair 0:f269e3021894 215 return (t) rv; \
elessair 0:f269e3021894 216 }
elessair 0:f269e3021894 217
elessair 0:f269e3021894 218 #define SVC_1_1(f,t,t1,rv) \
elessair 0:f269e3021894 219 __attribute__((always_inline)) \
elessair 0:f269e3021894 220 static inline t __##f (t1 a1) { \
elessair 0:f269e3021894 221 SVC_Arg1(t1); \
elessair 0:f269e3021894 222 SVC_Call(f); \
elessair 0:f269e3021894 223 return (t) rv; \
elessair 0:f269e3021894 224 }
elessair 0:f269e3021894 225
elessair 0:f269e3021894 226 #define SVC_2_1(f,t,t1,t2,rv) \
elessair 0:f269e3021894 227 __attribute__((always_inline)) \
elessair 0:f269e3021894 228 static inline t __##f (t1 a1, t2 a2) { \
elessair 0:f269e3021894 229 SVC_Arg2(t1,t2); \
elessair 0:f269e3021894 230 SVC_Call(f); \
elessair 0:f269e3021894 231 return (t) rv; \
elessair 0:f269e3021894 232 }
elessair 0:f269e3021894 233
elessair 0:f269e3021894 234 #define SVC_3_1(f,t,t1,t2,t3,rv) \
elessair 0:f269e3021894 235 __attribute__((always_inline)) \
elessair 0:f269e3021894 236 static inline t __##f (t1 a1, t2 a2, t3 a3) { \
elessair 0:f269e3021894 237 SVC_Arg3(t1,t2,t3); \
elessair 0:f269e3021894 238 SVC_Call(f); \
elessair 0:f269e3021894 239 return (t) rv; \
elessair 0:f269e3021894 240 }
elessair 0:f269e3021894 241
elessair 0:f269e3021894 242 #define SVC_4_1(f,t,t1,t2,t3,t4,rv) \
elessair 0:f269e3021894 243 __attribute__((always_inline)) \
elessair 0:f269e3021894 244 static inline t __##f (t1 a1, t2 a2, t3 a3, t4 a4) { \
elessair 0:f269e3021894 245 SVC_Arg4(t1,t2,t3,t4); \
elessair 0:f269e3021894 246 SVC_Call(f); \
elessair 0:f269e3021894 247 return (t) rv; \
elessair 0:f269e3021894 248 }
elessair 0:f269e3021894 249
elessair 0:f269e3021894 250 #define SVC_1_2 SVC_1_1
elessair 0:f269e3021894 251 #define SVC_1_3 SVC_1_1
elessair 0:f269e3021894 252 #define SVC_2_3 SVC_2_1
elessair 0:f269e3021894 253
elessair 0:f269e3021894 254 #elif defined (__ICCARM__) /* IAR Compiler */
elessair 0:f269e3021894 255
elessair 0:f269e3021894 256 #define __NO_RETURN __noreturn
elessair 0:f269e3021894 257
elessair 0:f269e3021894 258 #define osEvent_type osEvent
elessair 0:f269e3021894 259 #define osEvent_ret_status ret
elessair 0:f269e3021894 260 #define osEvent_ret_value ret
elessair 0:f269e3021894 261 #define osEvent_ret_msg ret
elessair 0:f269e3021894 262 #define osEvent_ret_mail ret
elessair 0:f269e3021894 263
elessair 0:f269e3021894 264 #define osCallback_type osCallback
elessair 0:f269e3021894 265 #define osCallback_ret ret
elessair 0:f269e3021894 266
elessair 0:f269e3021894 267 #define RET_osEvent osEvent
elessair 0:f269e3021894 268 #define RET_osCallback osCallback
elessair 0:f269e3021894 269
elessair 0:f269e3021894 270 #define SVC_Setup(f) \
elessair 0:f269e3021894 271 __asm( \
elessair 0:f269e3021894 272 "mov r12,%0\n" \
elessair 0:f269e3021894 273 :: "r"(&f): "r12" \
elessair 0:f269e3021894 274 );
elessair 0:f269e3021894 275
elessair 0:f269e3021894 276
elessair 0:f269e3021894 277 #define SVC_0_1(f,t,...) \
elessair 0:f269e3021894 278 t f (void); \
elessair 0:f269e3021894 279 _Pragma("swi_number=0") __swi t _##f (void); \
elessair 0:f269e3021894 280 static inline t __##f (void) { \
elessair 0:f269e3021894 281 SVC_Setup(f); \
elessair 0:f269e3021894 282 return _##f(); \
elessair 0:f269e3021894 283 }
elessair 0:f269e3021894 284
elessair 0:f269e3021894 285 #define SVC_1_1(f,t,t1,...) \
elessair 0:f269e3021894 286 t f (t1 a1); \
elessair 0:f269e3021894 287 _Pragma("swi_number=0") __swi t _##f (t1 a1); \
elessair 0:f269e3021894 288 static inline t __##f (t1 a1) { \
elessair 0:f269e3021894 289 SVC_Setup(f); \
elessair 0:f269e3021894 290 return _##f(a1); \
elessair 0:f269e3021894 291 }
elessair 0:f269e3021894 292
elessair 0:f269e3021894 293 #define SVC_2_1(f,t,t1,t2,...) \
elessair 0:f269e3021894 294 t f (t1 a1, t2 a2); \
elessair 0:f269e3021894 295 _Pragma("swi_number=0") __swi t _##f (t1 a1, t2 a2); \
elessair 0:f269e3021894 296 static inline t __##f (t1 a1, t2 a2) { \
elessair 0:f269e3021894 297 SVC_Setup(f); \
elessair 0:f269e3021894 298 return _##f(a1,a2); \
elessair 0:f269e3021894 299 }
elessair 0:f269e3021894 300
elessair 0:f269e3021894 301 #define SVC_3_1(f,t,t1,t2,t3,...) \
elessair 0:f269e3021894 302 t f (t1 a1, t2 a2, t3 a3); \
elessair 0:f269e3021894 303 _Pragma("swi_number=0") __swi t _##f (t1 a1, t2 a2, t3 a3); \
elessair 0:f269e3021894 304 static inline t __##f (t1 a1, t2 a2, t3 a3) { \
elessair 0:f269e3021894 305 SVC_Setup(f); \
elessair 0:f269e3021894 306 return _##f(a1,a2,a3); \
elessair 0:f269e3021894 307 }
elessair 0:f269e3021894 308
elessair 0:f269e3021894 309 #define SVC_4_1(f,t,t1,t2,t3,t4,...) \
elessair 0:f269e3021894 310 t f (t1 a1, t2 a2, t3 a3, t4 a4); \
elessair 0:f269e3021894 311 _Pragma("swi_number=0") __swi t _##f (t1 a1, t2 a2, t3 a3, t4 a4); \
elessair 0:f269e3021894 312 static inline t __##f (t1 a1, t2 a2, t3 a3, t4 a4) { \
elessair 0:f269e3021894 313 SVC_Setup(f); \
elessair 0:f269e3021894 314 return _##f(a1,a2,a3,a4); \
elessair 0:f269e3021894 315 }
elessair 0:f269e3021894 316
elessair 0:f269e3021894 317 #define SVC_1_2 SVC_1_1
elessair 0:f269e3021894 318 #define SVC_1_3 SVC_1_1
elessair 0:f269e3021894 319 #define SVC_2_3 SVC_2_1
elessair 0:f269e3021894 320
elessair 0:f269e3021894 321 #endif
elessair 0:f269e3021894 322
elessair 0:f269e3021894 323
elessair 0:f269e3021894 324 // Callback structure
elessair 0:f269e3021894 325 typedef struct {
elessair 0:f269e3021894 326 void *fp; // Function pointer
elessair 0:f269e3021894 327 void *arg; // Function argument
elessair 0:f269e3021894 328 } osCallback;
elessair 0:f269e3021894 329
elessair 0:f269e3021894 330
elessair 0:f269e3021894 331 // OS Section definitions
elessair 0:f269e3021894 332 #ifdef OS_SECTIONS_LINK_INFO
elessair 0:f269e3021894 333 extern const uint32_t os_section_id$$Base;
elessair 0:f269e3021894 334 extern const uint32_t os_section_id$$Limit;
elessair 0:f269e3021894 335 #endif
elessair 0:f269e3021894 336
elessair 0:f269e3021894 337 // OS Timers external resources
elessair 0:f269e3021894 338 extern osThreadDef_t os_thread_def_osTimerThread;
elessair 0:f269e3021894 339 extern osThreadId osThreadId_osTimerThread;
elessair 0:f269e3021894 340 extern osMessageQDef_t os_messageQ_def_osTimerMessageQ;
elessair 0:f269e3021894 341 extern osMessageQId osMessageQId_osTimerMessageQ;
elessair 0:f269e3021894 342
elessair 0:f269e3021894 343
elessair 0:f269e3021894 344 // ==== Helper Functions ====
elessair 0:f269e3021894 345
elessair 0:f269e3021894 346 /// Convert timeout in millisec to system ticks
elessair 0:f269e3021894 347 static uint32_t rt_ms2tick (uint32_t millisec) {
elessair 0:f269e3021894 348 uint32_t tick;
elessair 0:f269e3021894 349
elessair 0:f269e3021894 350 if (millisec == osWaitForever) return 0xFFFF; // Indefinite timeout
elessair 0:f269e3021894 351 if (millisec > 4000000) return 0xFFFE; // Max ticks supported
elessair 0:f269e3021894 352
elessair 0:f269e3021894 353 tick = ((1000 * millisec) + os_clockrate - 1) / os_clockrate;
elessair 0:f269e3021894 354 if (tick > 0xFFFE) return 0xFFFE;
elessair 0:f269e3021894 355
elessair 0:f269e3021894 356 return tick;
elessair 0:f269e3021894 357 }
elessair 0:f269e3021894 358
elessair 0:f269e3021894 359 /// Convert Thread ID to TCB pointer
elessair 0:f269e3021894 360 static P_TCB rt_tid2ptcb (osThreadId thread_id) {
elessair 0:f269e3021894 361 P_TCB ptcb;
elessair 0:f269e3021894 362
elessair 0:f269e3021894 363 if (thread_id == NULL) return NULL;
elessair 0:f269e3021894 364
elessair 0:f269e3021894 365 if ((uint32_t)thread_id & 3) return NULL;
elessair 0:f269e3021894 366
elessair 0:f269e3021894 367 #ifdef OS_SECTIONS_LINK_INFO
elessair 0:f269e3021894 368 if ((os_section_id$$Base != 0) && (os_section_id$$Limit != 0)) {
elessair 0:f269e3021894 369 if (thread_id < (osThreadId)os_section_id$$Base) return NULL;
elessair 0:f269e3021894 370 if (thread_id >= (osThreadId)os_section_id$$Limit) return NULL;
elessair 0:f269e3021894 371 }
elessair 0:f269e3021894 372 #endif
elessair 0:f269e3021894 373
elessair 0:f269e3021894 374 ptcb = thread_id;
elessair 0:f269e3021894 375
elessair 0:f269e3021894 376 if (ptcb->cb_type != TCB) return NULL;
elessair 0:f269e3021894 377
elessair 0:f269e3021894 378 return ptcb;
elessair 0:f269e3021894 379 }
elessair 0:f269e3021894 380
elessair 0:f269e3021894 381 /// Convert ID pointer to Object pointer
elessair 0:f269e3021894 382 static void *rt_id2obj (void *id) {
elessair 0:f269e3021894 383
elessair 0:f269e3021894 384 if ((uint32_t)id & 3) return NULL;
elessair 0:f269e3021894 385
elessair 0:f269e3021894 386 #ifdef OS_SECTIONS_LINK_INFO
elessair 0:f269e3021894 387 if ((os_section_id$$Base != 0) && (os_section_id$$Limit != 0)) {
elessair 0:f269e3021894 388 if (id < (void *)os_section_id$$Base) return NULL;
elessair 0:f269e3021894 389 if (id >= (void *)os_section_id$$Limit) return NULL;
elessair 0:f269e3021894 390 }
elessair 0:f269e3021894 391 #endif
elessair 0:f269e3021894 392
elessair 0:f269e3021894 393 return id;
elessair 0:f269e3021894 394 }
elessair 0:f269e3021894 395
elessair 0:f269e3021894 396
elessair 0:f269e3021894 397 // ==== Kernel Control ====
elessair 0:f269e3021894 398
elessair 0:f269e3021894 399 uint8_t os_initialized; // Kernel Initialized flag
elessair 0:f269e3021894 400 uint8_t os_running; // Kernel Running flag
elessair 0:f269e3021894 401
elessair 0:f269e3021894 402 // Kernel Control Service Calls declarations
elessair 0:f269e3021894 403 SVC_0_1(svcKernelInitialize, osStatus, RET_osStatus)
elessair 0:f269e3021894 404 SVC_0_1(svcKernelStart, osStatus, RET_osStatus)
elessair 0:f269e3021894 405 SVC_0_1(svcKernelRunning, int32_t, RET_int32_t)
elessair 0:f269e3021894 406
elessair 0:f269e3021894 407 extern void sysThreadError (osStatus status);
elessair 0:f269e3021894 408 osThreadId svcThreadCreate (osThreadDef_t *thread_def, void *argument);
elessair 0:f269e3021894 409 osMessageQId svcMessageCreate (osMessageQDef_t *queue_def, osThreadId thread_id);
elessair 0:f269e3021894 410
elessair 0:f269e3021894 411 // Kernel Control Service Calls
elessair 0:f269e3021894 412
elessair 0:f269e3021894 413 /// Initialize the RTOS Kernel for creating objects
elessair 0:f269e3021894 414 osStatus svcKernelInitialize (void) {
elessair 0:f269e3021894 415 if (os_initialized) return osOK;
elessair 0:f269e3021894 416
elessair 0:f269e3021894 417 rt_sys_init(); // RTX System Initialization
elessair 0:f269e3021894 418 os_tsk.run->prio = 255; // Highest priority
elessair 0:f269e3021894 419
elessair 0:f269e3021894 420 sysThreadError(osOK);
elessair 0:f269e3021894 421
elessair 0:f269e3021894 422 os_initialized = 1;
elessair 0:f269e3021894 423
elessair 0:f269e3021894 424 return osOK;
elessair 0:f269e3021894 425 }
elessair 0:f269e3021894 426
elessair 0:f269e3021894 427 /// Start the RTOS Kernel
elessair 0:f269e3021894 428 osStatus svcKernelStart (void) {
elessair 0:f269e3021894 429
elessair 0:f269e3021894 430 if (os_running) return osOK;
elessair 0:f269e3021894 431
elessair 0:f269e3021894 432 // Create OS Timers resources (Message Queue & Thread)
elessair 0:f269e3021894 433 osMessageQId_osTimerMessageQ = svcMessageCreate (&os_messageQ_def_osTimerMessageQ, NULL);
elessair 0:f269e3021894 434 osThreadId_osTimerThread = svcThreadCreate(&os_thread_def_osTimerThread, NULL);
elessair 0:f269e3021894 435
elessair 0:f269e3021894 436 rt_tsk_prio(0, 0); // Lowest priority
elessair 0:f269e3021894 437 // __set_SP(os_tsk.run->tsk_stack + 8*4); // New context
elessair 0:f269e3021894 438 os_tsk.run = NULL; // Force context switch
elessair 0:f269e3021894 439
elessair 0:f269e3021894 440 rt_sys_start();
elessair 0:f269e3021894 441
elessair 0:f269e3021894 442 os_running = 1;
elessair 0:f269e3021894 443
elessair 0:f269e3021894 444 return osOK;
elessair 0:f269e3021894 445 }
elessair 0:f269e3021894 446
elessair 0:f269e3021894 447 /// Check if the RTOS kernel is already started
elessair 0:f269e3021894 448 int32_t svcKernelRunning(void) {
elessair 0:f269e3021894 449 return os_running;
elessair 0:f269e3021894 450 }
elessair 0:f269e3021894 451
elessair 0:f269e3021894 452 // Kernel Control Public API
elessair 0:f269e3021894 453
elessair 0:f269e3021894 454 /// Initialize the RTOS Kernel for creating objects
elessair 0:f269e3021894 455 osStatus osKernelInitialize (void) {
elessair 0:f269e3021894 456 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 457 if (__get_CONTROL() == MODE_SUPERVISOR) { // Privileged mode
elessair 0:f269e3021894 458 return svcKernelInitialize();
elessair 0:f269e3021894 459 } else {
elessair 0:f269e3021894 460 return __svcKernelInitialize();
elessair 0:f269e3021894 461 }
elessair 0:f269e3021894 462 }
elessair 0:f269e3021894 463
elessair 0:f269e3021894 464 /// Start the RTOS Kernel
elessair 0:f269e3021894 465 osStatus osKernelStart (void) {
elessair 0:f269e3021894 466
elessair 0:f269e3021894 467 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 468 switch (__get_CONTROL()) {
elessair 0:f269e3021894 469 case MODE_SUPERVISOR: // Privileged mode
elessair 0:f269e3021894 470 break;
elessair 0:f269e3021894 471 case MODE_USER:
elessair 0:f269e3021894 472 case MODE_SYSTEM: // Unprivileged mode
elessair 0:f269e3021894 473 return osErrorOS;
elessair 0:f269e3021894 474 default: // Other invalid modes
elessair 0:f269e3021894 475 return osErrorOS;
elessair 0:f269e3021894 476 break;
elessair 0:f269e3021894 477 }
elessair 0:f269e3021894 478 return svcKernelStart();
elessair 0:f269e3021894 479 }
elessair 0:f269e3021894 480
elessair 0:f269e3021894 481 /// Check if the RTOS kernel is already started
elessair 0:f269e3021894 482 int32_t osKernelRunning(void) {
elessair 0:f269e3021894 483 if ((__get_CONTROL() == MODE_IRQ) || (__get_CONTROL() == MODE_SUPERVISOR)) {
elessair 0:f269e3021894 484 // in ISR or Privileged
elessair 0:f269e3021894 485 return os_running;
elessair 0:f269e3021894 486 } else {
elessair 0:f269e3021894 487 return __svcKernelRunning();
elessair 0:f269e3021894 488 }
elessair 0:f269e3021894 489 }
elessair 0:f269e3021894 490
elessair 0:f269e3021894 491
elessair 0:f269e3021894 492 // ==== Thread Management ====
elessair 0:f269e3021894 493
elessair 0:f269e3021894 494 __NO_RETURN void osThreadExit (void);
elessair 0:f269e3021894 495
elessair 0:f269e3021894 496 // Thread Service Calls declarations
elessair 0:f269e3021894 497 SVC_2_1(svcThreadCreate, osThreadId, osThreadDef_t *, void *, RET_pointer)
elessair 0:f269e3021894 498 SVC_0_1(svcThreadGetId, osThreadId, RET_pointer)
elessair 0:f269e3021894 499 SVC_1_1(svcThreadTerminate, osStatus, osThreadId, RET_osStatus)
elessair 0:f269e3021894 500 SVC_0_1(svcThreadYield, osStatus, RET_osStatus)
elessair 0:f269e3021894 501 SVC_2_1(svcThreadSetPriority, osStatus, osThreadId, osPriority, RET_osStatus)
elessair 0:f269e3021894 502 SVC_1_1(svcThreadGetPriority, osPriority, osThreadId, RET_osPriority)
elessair 0:f269e3021894 503
elessair 0:f269e3021894 504 // Thread Service Calls
elessair 0:f269e3021894 505 extern OS_TID rt_get_TID (void);
elessair 0:f269e3021894 506 extern void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body);
elessair 0:f269e3021894 507
elessair 0:f269e3021894 508 /// Create a thread and add it to Active Threads and set it to state READY
elessair 0:f269e3021894 509 osThreadId svcThreadCreate (osThreadDef_t *thread_def, void *argument) {
elessair 0:f269e3021894 510 P_TCB ptcb;
elessair 0:f269e3021894 511
elessair 0:f269e3021894 512 if ((thread_def == NULL) ||
elessair 0:f269e3021894 513 (thread_def->pthread == NULL) ||
elessair 0:f269e3021894 514 (thread_def->tpriority < osPriorityIdle) ||
elessair 0:f269e3021894 515 (thread_def->tpriority > osPriorityRealtime) ||
elessair 0:f269e3021894 516 (thread_def->stacksize == 0) ||
elessair 0:f269e3021894 517 (thread_def->stack_pointer == NULL) ) {
elessair 0:f269e3021894 518 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 519 return NULL;
elessair 0:f269e3021894 520 }
elessair 0:f269e3021894 521
elessair 0:f269e3021894 522 U8 priority = thread_def->tpriority - osPriorityIdle + 1;
elessair 0:f269e3021894 523 P_TCB task_context = &thread_def->tcb;
elessair 0:f269e3021894 524
elessair 0:f269e3021894 525 /* Utilize the user provided stack. */
elessair 0:f269e3021894 526 task_context->stack = (U32*)thread_def->stack_pointer;
elessair 0:f269e3021894 527 task_context->priv_stack = thread_def->stacksize;
elessair 0:f269e3021894 528 /* Find a free entry in 'os_active_TCB' table. */
elessair 0:f269e3021894 529 OS_TID tsk = rt_get_TID ();
elessair 0:f269e3021894 530 os_active_TCB[tsk-1] = task_context;
elessair 0:f269e3021894 531 task_context->task_id = tsk;
elessair 0:f269e3021894 532 /* Pass parameter 'argv' to 'rt_init_context' */
elessair 0:f269e3021894 533 task_context->msg = argument;
elessair 0:f269e3021894 534 /* For 'size == 0' system allocates the user stack from the memory pool. */
elessair 0:f269e3021894 535 rt_init_context (task_context, priority, (FUNCP)thread_def->pthread);
elessair 0:f269e3021894 536
elessair 0:f269e3021894 537 /* Dispatch this task to the scheduler for execution. */
elessair 0:f269e3021894 538 DBG_TASK_NOTIFY(task_context, __TRUE);
elessair 0:f269e3021894 539 rt_dispatch (task_context);
elessair 0:f269e3021894 540
elessair 0:f269e3021894 541 ptcb = (P_TCB)os_active_TCB[tsk - 1]; // TCB pointer
elessair 0:f269e3021894 542
elessair 0:f269e3021894 543 *((uint32_t *)ptcb->tsk_stack + TCB_STACK_LR_OFFSET_DWORDS) = (uint32_t)osThreadExit; /* LR = osThreadExit */
elessair 0:f269e3021894 544
elessair 0:f269e3021894 545 return ptcb;
elessair 0:f269e3021894 546 }
elessair 0:f269e3021894 547
elessair 0:f269e3021894 548 /// Return the thread ID of the current running thread
elessair 0:f269e3021894 549 osThreadId svcThreadGetId (void) {
elessair 0:f269e3021894 550 OS_TID tsk;
elessair 0:f269e3021894 551
elessair 0:f269e3021894 552 tsk = rt_tsk_self();
elessair 0:f269e3021894 553 if (tsk == 0) return NULL;
elessair 0:f269e3021894 554 return (P_TCB)os_active_TCB[tsk - 1];
elessair 0:f269e3021894 555 }
elessair 0:f269e3021894 556
elessair 0:f269e3021894 557 /// Terminate execution of a thread and remove it from ActiveThreads
elessair 0:f269e3021894 558 osStatus svcThreadTerminate (osThreadId thread_id) {
elessair 0:f269e3021894 559 OS_RESULT res;
elessair 0:f269e3021894 560 P_TCB ptcb;
elessair 0:f269e3021894 561
elessair 0:f269e3021894 562 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
elessair 0:f269e3021894 563 if (ptcb == NULL) return osErrorParameter;
elessair 0:f269e3021894 564
elessair 0:f269e3021894 565 res = rt_tsk_delete(ptcb->task_id); // Delete task
elessair 0:f269e3021894 566
elessair 0:f269e3021894 567 if (res == OS_R_NOK) return osErrorResource; // Delete task failed
elessair 0:f269e3021894 568
elessair 0:f269e3021894 569 return osOK;
elessair 0:f269e3021894 570 }
elessair 0:f269e3021894 571
elessair 0:f269e3021894 572 /// Pass control to next thread that is in state READY
elessair 0:f269e3021894 573 osStatus svcThreadYield (void) {
elessair 0:f269e3021894 574 rt_tsk_pass(); // Pass control to next task
elessair 0:f269e3021894 575 return osOK;
elessair 0:f269e3021894 576 }
elessair 0:f269e3021894 577
elessair 0:f269e3021894 578 /// Change priority of an active thread
elessair 0:f269e3021894 579 osStatus svcThreadSetPriority (osThreadId thread_id, osPriority priority) {
elessair 0:f269e3021894 580 OS_RESULT res;
elessair 0:f269e3021894 581 P_TCB ptcb;
elessair 0:f269e3021894 582
elessair 0:f269e3021894 583 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
elessair 0:f269e3021894 584 if (ptcb == NULL) return osErrorParameter;
elessair 0:f269e3021894 585
elessair 0:f269e3021894 586 if ((priority < osPriorityIdle) || (priority > osPriorityRealtime)) {
elessair 0:f269e3021894 587 return osErrorValue;
elessair 0:f269e3021894 588 }
elessair 0:f269e3021894 589
elessair 0:f269e3021894 590 res = rt_tsk_prio( // Change task priority
elessair 0:f269e3021894 591 ptcb->task_id, // Task ID
elessair 0:f269e3021894 592 priority - osPriorityIdle + 1 // New task priority
elessair 0:f269e3021894 593 );
elessair 0:f269e3021894 594
elessair 0:f269e3021894 595 if (res == OS_R_NOK) return osErrorResource; // Change task priority failed
elessair 0:f269e3021894 596
elessair 0:f269e3021894 597 return osOK;
elessair 0:f269e3021894 598 }
elessair 0:f269e3021894 599
elessair 0:f269e3021894 600 /// Get current priority of an active thread
elessair 0:f269e3021894 601 osPriority svcThreadGetPriority (osThreadId thread_id) {
elessair 0:f269e3021894 602 P_TCB ptcb;
elessair 0:f269e3021894 603
elessair 0:f269e3021894 604 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
elessair 0:f269e3021894 605 if (ptcb == NULL) return osPriorityError;
elessair 0:f269e3021894 606
elessair 0:f269e3021894 607 return (osPriority)(ptcb->prio - 1 + osPriorityIdle);
elessair 0:f269e3021894 608 }
elessair 0:f269e3021894 609
elessair 0:f269e3021894 610
elessair 0:f269e3021894 611 // Thread Public API
elessair 0:f269e3021894 612
elessair 0:f269e3021894 613 /// Create a thread and add it to Active Threads and set it to state READY
elessair 0:f269e3021894 614 osThreadId osThreadCreate (osThreadDef_t *thread_def, void *argument) {
elessair 0:f269e3021894 615 if (__get_CONTROL() == MODE_IRQ) return NULL; // Not allowed in ISR
elessair 0:f269e3021894 616 if ((__get_CONTROL() == MODE_SUPERVISOR) && (os_running == 0)) {
elessair 0:f269e3021894 617 // Privileged and not running
elessair 0:f269e3021894 618 return svcThreadCreate(thread_def, argument);
elessair 0:f269e3021894 619 } else {
elessair 0:f269e3021894 620 return __svcThreadCreate(thread_def, argument);
elessair 0:f269e3021894 621 }
elessair 0:f269e3021894 622 }
elessair 0:f269e3021894 623
elessair 0:f269e3021894 624 /// Return the thread ID of the current running thread
elessair 0:f269e3021894 625 osThreadId osThreadGetId (void) {
elessair 0:f269e3021894 626 if (__get_CONTROL() == MODE_IRQ) return NULL; // Not allowed in ISR
elessair 0:f269e3021894 627 return __svcThreadGetId();
elessair 0:f269e3021894 628 }
elessair 0:f269e3021894 629
elessair 0:f269e3021894 630 /// Terminate execution of a thread and remove it from ActiveThreads
elessair 0:f269e3021894 631 osStatus osThreadTerminate (osThreadId thread_id) {
elessair 0:f269e3021894 632 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 633 return __svcThreadTerminate(thread_id);
elessair 0:f269e3021894 634 }
elessair 0:f269e3021894 635
elessair 0:f269e3021894 636 /// Pass control to next thread that is in state READY
elessair 0:f269e3021894 637 osStatus osThreadYield (void) {
elessair 0:f269e3021894 638 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 639 return __svcThreadYield();
elessair 0:f269e3021894 640 }
elessair 0:f269e3021894 641
elessair 0:f269e3021894 642 /// Change priority of an active thread
elessair 0:f269e3021894 643 osStatus osThreadSetPriority (osThreadId thread_id, osPriority priority) {
elessair 0:f269e3021894 644 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 645 return __svcThreadSetPriority(thread_id, priority);
elessair 0:f269e3021894 646 }
elessair 0:f269e3021894 647
elessair 0:f269e3021894 648 /// Get current priority of an active thread
elessair 0:f269e3021894 649 osPriority osThreadGetPriority (osThreadId thread_id) {
elessair 0:f269e3021894 650 if (__get_CONTROL() == MODE_IRQ) return osPriorityError;// Not allowed in ISR
elessair 0:f269e3021894 651 return __svcThreadGetPriority(thread_id);
elessair 0:f269e3021894 652 }
elessair 0:f269e3021894 653
elessair 0:f269e3021894 654 /// INTERNAL - Not Public
elessair 0:f269e3021894 655 /// Auto Terminate Thread on exit (used implicitly when thread exists)
elessair 0:f269e3021894 656 __NO_RETURN void osThreadExit (void) {
elessair 0:f269e3021894 657 __svcThreadTerminate(__svcThreadGetId());
elessair 0:f269e3021894 658 for (;;); // Should never come here
elessair 0:f269e3021894 659 }
elessair 0:f269e3021894 660
elessair 0:f269e3021894 661
elessair 0:f269e3021894 662 // ==== Generic Wait Functions ====
elessair 0:f269e3021894 663
elessair 0:f269e3021894 664 // Generic Wait Service Calls declarations
elessair 0:f269e3021894 665 SVC_1_1(svcDelay, osStatus, uint32_t, RET_osStatus)
elessair 0:f269e3021894 666 #if osFeature_Wait != 0
elessair 0:f269e3021894 667 SVC_1_3(svcWait, os_InRegs osEvent, uint32_t, RET_osEvent)
elessair 0:f269e3021894 668 #endif
elessair 0:f269e3021894 669
elessair 0:f269e3021894 670 // Generic Wait Service Calls
elessair 0:f269e3021894 671
elessair 0:f269e3021894 672 /// Wait for Timeout (Time Delay)
elessair 0:f269e3021894 673 osStatus svcDelay (uint32_t millisec) {
elessair 0:f269e3021894 674 if (millisec == 0) return osOK;
elessair 0:f269e3021894 675 rt_dly_wait(rt_ms2tick(millisec));
elessair 0:f269e3021894 676 return osEventTimeout;
elessair 0:f269e3021894 677 }
elessair 0:f269e3021894 678
elessair 0:f269e3021894 679 /// Wait for Signal, Message, Mail, or Timeout
elessair 0:f269e3021894 680 #if osFeature_Wait != 0
elessair 0:f269e3021894 681 os_InRegs osEvent_type svcWait (uint32_t millisec) {
elessair 0:f269e3021894 682 osEvent ret;
elessair 0:f269e3021894 683
elessair 0:f269e3021894 684 if (millisec == 0) {
elessair 0:f269e3021894 685 ret.status = osOK;
elessair 0:f269e3021894 686 return osEvent_ret_status;
elessair 0:f269e3021894 687 }
elessair 0:f269e3021894 688
elessair 0:f269e3021894 689 /* To Do: osEventSignal, osEventMessage, osEventMail */
elessair 0:f269e3021894 690 rt_dly_wait(rt_ms2tick(millisec));
elessair 0:f269e3021894 691 ret.status = osEventTimeout;
elessair 0:f269e3021894 692
elessair 0:f269e3021894 693 return osEvent_ret_status;
elessair 0:f269e3021894 694 }
elessair 0:f269e3021894 695 #endif
elessair 0:f269e3021894 696
elessair 0:f269e3021894 697
elessair 0:f269e3021894 698 // Generic Wait API
elessair 0:f269e3021894 699
elessair 0:f269e3021894 700 /// Wait for Timeout (Time Delay)
elessair 0:f269e3021894 701 osStatus osDelay (uint32_t millisec) {
elessair 0:f269e3021894 702 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 703 return __svcDelay(millisec);
elessair 0:f269e3021894 704 }
elessair 0:f269e3021894 705
elessair 0:f269e3021894 706 /// Wait for Signal, Message, Mail, or Timeout
elessair 0:f269e3021894 707 os_InRegs osEvent osWait (uint32_t millisec) {
elessair 0:f269e3021894 708 osEvent ret;
elessair 0:f269e3021894 709
elessair 0:f269e3021894 710 #if osFeature_Wait == 0
elessair 0:f269e3021894 711 ret.status = osErrorOS;
elessair 0:f269e3021894 712 return ret;
elessair 0:f269e3021894 713 #else
elessair 0:f269e3021894 714 if (__get_CONTROL() == MODE_IRQ) { // Not allowed in ISR
elessair 0:f269e3021894 715 ret.status = osErrorISR;
elessair 0:f269e3021894 716 return ret;
elessair 0:f269e3021894 717 }
elessair 0:f269e3021894 718 return __svcWait(millisec);
elessair 0:f269e3021894 719 #endif
elessair 0:f269e3021894 720 }
elessair 0:f269e3021894 721
elessair 0:f269e3021894 722
elessair 0:f269e3021894 723 // ==== Timer Management ====
elessair 0:f269e3021894 724
elessair 0:f269e3021894 725 // Timer definitions
elessair 0:f269e3021894 726 #define osTimerInvalid 0
elessair 0:f269e3021894 727 #define osTimerStopped 1
elessair 0:f269e3021894 728 #define osTimerRunning 2
elessair 0:f269e3021894 729
elessair 0:f269e3021894 730 // Timer structures
elessair 0:f269e3021894 731
elessair 0:f269e3021894 732 typedef struct os_timer_cb_ { // Timer Control Block
elessair 0:f269e3021894 733 struct os_timer_cb_ *next; // Pointer to next active Timer
elessair 0:f269e3021894 734 uint8_t state; // Timer State
elessair 0:f269e3021894 735 uint8_t type; // Timer Type (Periodic/One-shot)
elessair 0:f269e3021894 736 uint16_t reserved; // Reserved
elessair 0:f269e3021894 737 uint16_t tcnt; // Timer Delay Count
elessair 0:f269e3021894 738 uint16_t icnt; // Timer Initial Count
elessair 0:f269e3021894 739 void *arg; // Timer Function Argument
elessair 0:f269e3021894 740 osTimerDef_t *timer; // Pointer to Timer definition
elessair 0:f269e3021894 741 } os_timer_cb;
elessair 0:f269e3021894 742
elessair 0:f269e3021894 743 // Timer variables
elessair 0:f269e3021894 744 os_timer_cb *os_timer_head; // Pointer to first active Timer
elessair 0:f269e3021894 745
elessair 0:f269e3021894 746
elessair 0:f269e3021894 747 // Timer Helper Functions
elessair 0:f269e3021894 748
elessair 0:f269e3021894 749 // Insert Timer into the list sorted by time
elessair 0:f269e3021894 750 static void rt_timer_insert (os_timer_cb *pt, uint32_t tcnt) {
elessair 0:f269e3021894 751 os_timer_cb *p, *prev;
elessair 0:f269e3021894 752
elessair 0:f269e3021894 753 prev = NULL;
elessair 0:f269e3021894 754 p = os_timer_head;
elessair 0:f269e3021894 755 while (p != NULL) {
elessair 0:f269e3021894 756 if (tcnt < p->tcnt) break;
elessair 0:f269e3021894 757 tcnt -= p->tcnt;
elessair 0:f269e3021894 758 prev = p;
elessair 0:f269e3021894 759 p = p->next;
elessair 0:f269e3021894 760 }
elessair 0:f269e3021894 761 pt->next = p;
elessair 0:f269e3021894 762 pt->tcnt = (uint16_t)tcnt;
elessair 0:f269e3021894 763 if (p != NULL) {
elessair 0:f269e3021894 764 p->tcnt -= pt->tcnt;
elessair 0:f269e3021894 765 }
elessair 0:f269e3021894 766 if (prev != NULL) {
elessair 0:f269e3021894 767 prev->next = pt;
elessair 0:f269e3021894 768 } else {
elessair 0:f269e3021894 769 os_timer_head = pt;
elessair 0:f269e3021894 770 }
elessair 0:f269e3021894 771 }
elessair 0:f269e3021894 772
elessair 0:f269e3021894 773 // Remove Timer from the list
elessair 0:f269e3021894 774 static int rt_timer_remove (os_timer_cb *pt) {
elessair 0:f269e3021894 775 os_timer_cb *p, *prev;
elessair 0:f269e3021894 776
elessair 0:f269e3021894 777 prev = NULL;
elessair 0:f269e3021894 778 p = os_timer_head;
elessair 0:f269e3021894 779 while (p != NULL) {
elessair 0:f269e3021894 780 if (p == pt) break;
elessair 0:f269e3021894 781 prev = p;
elessair 0:f269e3021894 782 p = p->next;
elessair 0:f269e3021894 783 }
elessair 0:f269e3021894 784 if (p == NULL) return -1;
elessair 0:f269e3021894 785 if (prev != NULL) {
elessair 0:f269e3021894 786 prev->next = pt->next;
elessair 0:f269e3021894 787 } else {
elessair 0:f269e3021894 788 os_timer_head = pt->next;
elessair 0:f269e3021894 789 }
elessair 0:f269e3021894 790 if (pt->next != NULL) {
elessair 0:f269e3021894 791 pt->next->tcnt += pt->tcnt;
elessair 0:f269e3021894 792 }
elessair 0:f269e3021894 793
elessair 0:f269e3021894 794 return 0;
elessair 0:f269e3021894 795 }
elessair 0:f269e3021894 796
elessair 0:f269e3021894 797
elessair 0:f269e3021894 798 // Timer Service Calls declarations
elessair 0:f269e3021894 799 SVC_3_1(svcTimerCreate, osTimerId, osTimerDef_t *, os_timer_type, void *, RET_pointer)
elessair 0:f269e3021894 800 SVC_2_1(svcTimerStart, osStatus, osTimerId, uint32_t, RET_osStatus)
elessair 0:f269e3021894 801 SVC_1_1(svcTimerStop, osStatus, osTimerId, RET_osStatus)
elessair 0:f269e3021894 802 SVC_1_1(svcTimerDelete, osStatus, osTimerId, RET_osStatus)
elessair 0:f269e3021894 803 SVC_1_2(svcTimerCall, os_InRegs osCallback, osTimerId, RET_osCallback)
elessair 0:f269e3021894 804
elessair 0:f269e3021894 805 // Timer Management Service Calls
elessair 0:f269e3021894 806
elessair 0:f269e3021894 807 /// Create timer
elessair 0:f269e3021894 808 osTimerId svcTimerCreate (osTimerDef_t *timer_def, os_timer_type type, void *argument) {
elessair 0:f269e3021894 809 os_timer_cb *pt;
elessair 0:f269e3021894 810
elessair 0:f269e3021894 811 if ((timer_def == NULL) || (timer_def->ptimer == NULL)) {
elessair 0:f269e3021894 812 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 813 return NULL;
elessair 0:f269e3021894 814 }
elessair 0:f269e3021894 815
elessair 0:f269e3021894 816 pt = timer_def->timer;
elessair 0:f269e3021894 817 if (pt == NULL) {
elessair 0:f269e3021894 818 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 819 return NULL;
elessair 0:f269e3021894 820 }
elessair 0:f269e3021894 821
elessair 0:f269e3021894 822 if ((type != osTimerOnce) && (type != osTimerPeriodic)) {
elessair 0:f269e3021894 823 sysThreadError(osErrorValue);
elessair 0:f269e3021894 824 return NULL;
elessair 0:f269e3021894 825 }
elessair 0:f269e3021894 826
elessair 0:f269e3021894 827 if (osThreadId_osTimerThread == NULL) {
elessair 0:f269e3021894 828 sysThreadError(osErrorResource);
elessair 0:f269e3021894 829 return NULL;
elessair 0:f269e3021894 830 }
elessair 0:f269e3021894 831
elessair 0:f269e3021894 832 if (pt->state != osTimerInvalid){
elessair 0:f269e3021894 833 sysThreadError(osErrorResource);
elessair 0:f269e3021894 834 return NULL;
elessair 0:f269e3021894 835 }
elessair 0:f269e3021894 836
elessair 0:f269e3021894 837 pt->state = osTimerStopped;
elessair 0:f269e3021894 838 pt->type = (uint8_t)type;
elessair 0:f269e3021894 839 pt->arg = argument;
elessair 0:f269e3021894 840 pt->timer = timer_def;
elessair 0:f269e3021894 841
elessair 0:f269e3021894 842 return (osTimerId)pt;
elessair 0:f269e3021894 843 }
elessair 0:f269e3021894 844
elessair 0:f269e3021894 845 /// Start or restart timer
elessair 0:f269e3021894 846 osStatus svcTimerStart (osTimerId timer_id, uint32_t millisec) {
elessair 0:f269e3021894 847 os_timer_cb *pt;
elessair 0:f269e3021894 848 uint32_t tcnt;
elessair 0:f269e3021894 849
elessair 0:f269e3021894 850 pt = rt_id2obj(timer_id);
elessair 0:f269e3021894 851 if (pt == NULL) return osErrorParameter;
elessair 0:f269e3021894 852
elessair 0:f269e3021894 853 tcnt = rt_ms2tick(millisec);
elessair 0:f269e3021894 854 if (tcnt == 0) return osErrorValue;
elessair 0:f269e3021894 855
elessair 0:f269e3021894 856 switch (pt->state) {
elessair 0:f269e3021894 857 case osTimerRunning:
elessair 0:f269e3021894 858 if (rt_timer_remove(pt) != 0) {
elessair 0:f269e3021894 859 return osErrorResource;
elessair 0:f269e3021894 860 }
elessair 0:f269e3021894 861 break;
elessair 0:f269e3021894 862 case osTimerStopped:
elessair 0:f269e3021894 863 pt->state = osTimerRunning;
elessair 0:f269e3021894 864 pt->icnt = (uint16_t)tcnt;
elessair 0:f269e3021894 865 break;
elessair 0:f269e3021894 866 default:
elessair 0:f269e3021894 867 return osErrorResource;
elessair 0:f269e3021894 868 }
elessair 0:f269e3021894 869
elessair 0:f269e3021894 870 rt_timer_insert(pt, tcnt);
elessair 0:f269e3021894 871
elessair 0:f269e3021894 872 return osOK;
elessair 0:f269e3021894 873 }
elessair 0:f269e3021894 874
elessair 0:f269e3021894 875 /// Stop timer
elessair 0:f269e3021894 876 osStatus svcTimerStop (osTimerId timer_id) {
elessair 0:f269e3021894 877 os_timer_cb *pt;
elessair 0:f269e3021894 878
elessair 0:f269e3021894 879 pt = rt_id2obj(timer_id);
elessair 0:f269e3021894 880 if (pt == NULL) return osErrorParameter;
elessair 0:f269e3021894 881
elessair 0:f269e3021894 882 if (pt->state != osTimerRunning) return osErrorResource;
elessair 0:f269e3021894 883
elessair 0:f269e3021894 884 pt->state = osTimerStopped;
elessair 0:f269e3021894 885
elessair 0:f269e3021894 886 if (rt_timer_remove(pt) != 0) {
elessair 0:f269e3021894 887 return osErrorResource;
elessair 0:f269e3021894 888 }
elessair 0:f269e3021894 889
elessair 0:f269e3021894 890 return osOK;
elessair 0:f269e3021894 891 }
elessair 0:f269e3021894 892
elessair 0:f269e3021894 893 /// Delete timer
elessair 0:f269e3021894 894 osStatus svcTimerDelete (osTimerId timer_id) {
elessair 0:f269e3021894 895 os_timer_cb *pt;
elessair 0:f269e3021894 896
elessair 0:f269e3021894 897 pt = rt_id2obj(timer_id);
elessair 0:f269e3021894 898 if (pt == NULL) return osErrorParameter;
elessair 0:f269e3021894 899
elessair 0:f269e3021894 900 switch (pt->state) {
elessair 0:f269e3021894 901 case osTimerRunning:
elessair 0:f269e3021894 902 rt_timer_remove(pt);
elessair 0:f269e3021894 903 break;
elessair 0:f269e3021894 904 case osTimerStopped:
elessair 0:f269e3021894 905 break;
elessair 0:f269e3021894 906 default:
elessair 0:f269e3021894 907 return osErrorResource;
elessair 0:f269e3021894 908 }
elessair 0:f269e3021894 909
elessair 0:f269e3021894 910 pt->state = osTimerInvalid;
elessair 0:f269e3021894 911
elessair 0:f269e3021894 912 return osOK;
elessair 0:f269e3021894 913 }
elessair 0:f269e3021894 914
elessair 0:f269e3021894 915 /// Get timer callback parameters
elessair 0:f269e3021894 916 os_InRegs osCallback_type svcTimerCall (osTimerId timer_id) {
elessair 0:f269e3021894 917 os_timer_cb *pt;
elessair 0:f269e3021894 918 osCallback ret;
elessair 0:f269e3021894 919
elessair 0:f269e3021894 920 pt = rt_id2obj(timer_id);
elessair 0:f269e3021894 921 if (pt == NULL) {
elessair 0:f269e3021894 922 ret.fp = NULL;
elessair 0:f269e3021894 923 ret.arg = NULL;
elessair 0:f269e3021894 924 return osCallback_ret;
elessair 0:f269e3021894 925 }
elessair 0:f269e3021894 926
elessair 0:f269e3021894 927 ret.fp = (void *)pt->timer->ptimer;
elessair 0:f269e3021894 928 ret.arg = pt->arg;
elessair 0:f269e3021894 929
elessair 0:f269e3021894 930 return osCallback_ret;
elessair 0:f269e3021894 931 }
elessair 0:f269e3021894 932
elessair 0:f269e3021894 933 static __INLINE osStatus isrMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec);
elessair 0:f269e3021894 934
elessair 0:f269e3021894 935 /// Timer Tick (called each SysTick)
elessair 0:f269e3021894 936 void sysTimerTick (void) {
elessair 0:f269e3021894 937 os_timer_cb *pt, *p;
elessair 0:f269e3021894 938
elessair 0:f269e3021894 939 p = os_timer_head;
elessair 0:f269e3021894 940 if (p == NULL) return;
elessair 0:f269e3021894 941
elessair 0:f269e3021894 942 p->tcnt--;
elessair 0:f269e3021894 943 while ((p != NULL) && (p->tcnt == 0)) {
elessair 0:f269e3021894 944 pt = p;
elessair 0:f269e3021894 945 p = p->next;
elessair 0:f269e3021894 946 os_timer_head = p;
elessair 0:f269e3021894 947 isrMessagePut(osMessageQId_osTimerMessageQ, (uint32_t)pt, 0);
elessair 0:f269e3021894 948 if (pt->type == osTimerPeriodic) {
elessair 0:f269e3021894 949 rt_timer_insert(pt, pt->icnt);
elessair 0:f269e3021894 950 } else {
elessair 0:f269e3021894 951 pt->state = osTimerStopped;
elessair 0:f269e3021894 952 }
elessair 0:f269e3021894 953 }
elessair 0:f269e3021894 954 }
elessair 0:f269e3021894 955
elessair 0:f269e3021894 956
elessair 0:f269e3021894 957 // Timer Management Public API
elessair 0:f269e3021894 958
elessair 0:f269e3021894 959 /// Create timer
elessair 0:f269e3021894 960 osTimerId osTimerCreate (osTimerDef_t *timer_def, os_timer_type type, void *argument) {
elessair 0:f269e3021894 961 if (__get_CONTROL() == MODE_IRQ) return NULL; // Not allowed in ISR
elessair 0:f269e3021894 962 if ((__get_CONTROL() == MODE_SUPERVISOR) && (os_running == 0)) {
elessair 0:f269e3021894 963 // Privileged and not running
elessair 0:f269e3021894 964 return svcTimerCreate(timer_def, type, argument);
elessair 0:f269e3021894 965 } else {
elessair 0:f269e3021894 966 return __svcTimerCreate(timer_def, type, argument);
elessair 0:f269e3021894 967 }
elessair 0:f269e3021894 968 }
elessair 0:f269e3021894 969
elessair 0:f269e3021894 970 /// Start or restart timer
elessair 0:f269e3021894 971 osStatus osTimerStart (osTimerId timer_id, uint32_t millisec) {
elessair 0:f269e3021894 972 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 973 return __svcTimerStart(timer_id, millisec);
elessair 0:f269e3021894 974 }
elessair 0:f269e3021894 975
elessair 0:f269e3021894 976 /// Stop timer
elessair 0:f269e3021894 977 osStatus osTimerStop (osTimerId timer_id) {
elessair 0:f269e3021894 978 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 979 return __svcTimerStop(timer_id);
elessair 0:f269e3021894 980 }
elessair 0:f269e3021894 981
elessair 0:f269e3021894 982 /// Delete timer
elessair 0:f269e3021894 983 osStatus osTimerDelete (osTimerId timer_id) {
elessair 0:f269e3021894 984 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 985 return __svcTimerDelete(timer_id);
elessair 0:f269e3021894 986 }
elessair 0:f269e3021894 987
elessair 0:f269e3021894 988 /// INTERNAL - Not Public
elessair 0:f269e3021894 989 /// Get timer callback parameters (used by OS Timer Thread)
elessair 0:f269e3021894 990 os_InRegs osCallback osTimerCall (osTimerId timer_id) {
elessair 0:f269e3021894 991 return __svcTimerCall(timer_id);
elessair 0:f269e3021894 992 }
elessair 0:f269e3021894 993
elessair 0:f269e3021894 994
elessair 0:f269e3021894 995 // Timer Thread
elessair 0:f269e3021894 996 __NO_RETURN void osTimerThread (void const *argument) {
elessair 0:f269e3021894 997 osCallback cb;
elessair 0:f269e3021894 998 osEvent evt;
elessair 0:f269e3021894 999
elessair 0:f269e3021894 1000 for (;;) {
elessair 0:f269e3021894 1001 evt = osMessageGet(osMessageQId_osTimerMessageQ, osWaitForever);
elessair 0:f269e3021894 1002 if (evt.status == osEventMessage) {
elessair 0:f269e3021894 1003 cb = osTimerCall(evt.value.p);
elessair 0:f269e3021894 1004 if (cb.fp != NULL) {
elessair 0:f269e3021894 1005 (*(os_ptimer)cb.fp)(cb.arg);
elessair 0:f269e3021894 1006 }
elessair 0:f269e3021894 1007 }
elessair 0:f269e3021894 1008 }
elessair 0:f269e3021894 1009 }
elessair 0:f269e3021894 1010
elessair 0:f269e3021894 1011
elessair 0:f269e3021894 1012 // ==== Signal Management ====
elessair 0:f269e3021894 1013
elessair 0:f269e3021894 1014 // Signal Service Calls declarations
elessair 0:f269e3021894 1015 SVC_2_1(svcSignalSet, int32_t, osThreadId, int32_t, RET_int32_t)
elessair 0:f269e3021894 1016 SVC_2_1(svcSignalClear, int32_t, osThreadId, int32_t, RET_int32_t)
elessair 0:f269e3021894 1017 SVC_1_1(svcSignalGet, int32_t, osThreadId, RET_int32_t)
elessair 0:f269e3021894 1018 SVC_2_3(svcSignalWait, os_InRegs osEvent, int32_t, uint32_t, RET_osEvent)
elessair 0:f269e3021894 1019
elessair 0:f269e3021894 1020 // Signal Service Calls
elessair 0:f269e3021894 1021
elessair 0:f269e3021894 1022 /// Set the specified Signal Flags of an active thread
elessair 0:f269e3021894 1023 int32_t svcSignalSet (osThreadId thread_id, int32_t signals) {
elessair 0:f269e3021894 1024 P_TCB ptcb;
elessair 0:f269e3021894 1025 int32_t sig;
elessair 0:f269e3021894 1026
elessair 0:f269e3021894 1027 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
elessair 0:f269e3021894 1028 if (ptcb == NULL) return 0x80000000;
elessair 0:f269e3021894 1029
elessair 0:f269e3021894 1030 if (signals & (0xFFFFFFFF << osFeature_Signals)) return 0x80000000;
elessair 0:f269e3021894 1031
elessair 0:f269e3021894 1032 sig = ptcb->events; // Previous signal flags
elessair 0:f269e3021894 1033
elessair 0:f269e3021894 1034 rt_evt_set(signals, ptcb->task_id); // Set event flags
elessair 0:f269e3021894 1035
elessair 0:f269e3021894 1036 return sig;
elessair 0:f269e3021894 1037 }
elessair 0:f269e3021894 1038
elessair 0:f269e3021894 1039 /// Clear the specified Signal Flags of an active thread
elessair 0:f269e3021894 1040 int32_t svcSignalClear (osThreadId thread_id, int32_t signals) {
elessair 0:f269e3021894 1041 P_TCB ptcb;
elessair 0:f269e3021894 1042 int32_t sig;
elessair 0:f269e3021894 1043
elessair 0:f269e3021894 1044 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
elessair 0:f269e3021894 1045 if (ptcb == NULL) return 0x80000000;
elessair 0:f269e3021894 1046
elessair 0:f269e3021894 1047 if (signals & (0xFFFFFFFF << osFeature_Signals)) return 0x80000000;
elessair 0:f269e3021894 1048
elessair 0:f269e3021894 1049 sig = ptcb->events; // Previous signal flags
elessair 0:f269e3021894 1050
elessair 0:f269e3021894 1051 rt_evt_clr(signals, ptcb->task_id); // Clear event flags
elessair 0:f269e3021894 1052
elessair 0:f269e3021894 1053 return sig;
elessair 0:f269e3021894 1054 }
elessair 0:f269e3021894 1055
elessair 0:f269e3021894 1056 /// Get Signal Flags status of an active thread
elessair 0:f269e3021894 1057 int32_t svcSignalGet (osThreadId thread_id) {
elessair 0:f269e3021894 1058 P_TCB ptcb;
elessair 0:f269e3021894 1059
elessair 0:f269e3021894 1060 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
elessair 0:f269e3021894 1061 if (ptcb == NULL) return 0x80000000;
elessair 0:f269e3021894 1062
elessair 0:f269e3021894 1063 return ptcb->events; // Return event flags
elessair 0:f269e3021894 1064 }
elessair 0:f269e3021894 1065
elessair 0:f269e3021894 1066 /// Wait for one or more Signal Flags to become signaled for the current RUNNING thread
elessair 0:f269e3021894 1067 os_InRegs osEvent_type svcSignalWait (int32_t signals, uint32_t millisec) {
elessair 0:f269e3021894 1068 OS_RESULT res;
elessair 0:f269e3021894 1069 osEvent ret;
elessair 0:f269e3021894 1070
elessair 0:f269e3021894 1071 if (signals & (0xFFFFFFFF << osFeature_Signals)) {
elessair 0:f269e3021894 1072 ret.status = osErrorValue;
elessair 0:f269e3021894 1073 return osEvent_ret_status;
elessair 0:f269e3021894 1074 }
elessair 0:f269e3021894 1075
elessair 0:f269e3021894 1076 if (signals != 0) { // Wait for all specified signals
elessair 0:f269e3021894 1077 res = rt_evt_wait(signals, rt_ms2tick(millisec), __TRUE);
elessair 0:f269e3021894 1078 } else { // Wait for any signal
elessair 0:f269e3021894 1079 res = rt_evt_wait(0xFFFF, rt_ms2tick(millisec), __FALSE);
elessair 0:f269e3021894 1080 }
elessair 0:f269e3021894 1081
elessair 0:f269e3021894 1082 if (res == OS_R_EVT) {
elessair 0:f269e3021894 1083 ret.status = osEventSignal;
elessair 0:f269e3021894 1084 ret.value.signals = signals ? signals : os_tsk.run->waits;
elessair 0:f269e3021894 1085 } else {
elessair 0:f269e3021894 1086 ret.status = millisec ? osEventTimeout : osOK;
elessair 0:f269e3021894 1087 ret.value.signals = 0;
elessair 0:f269e3021894 1088 }
elessair 0:f269e3021894 1089
elessair 0:f269e3021894 1090 return osEvent_ret_value;
elessair 0:f269e3021894 1091 }
elessair 0:f269e3021894 1092
elessair 0:f269e3021894 1093
elessair 0:f269e3021894 1094 // Signal ISR Calls
elessair 0:f269e3021894 1095
elessair 0:f269e3021894 1096 /// Set the specified Signal Flags of an active thread
elessair 0:f269e3021894 1097 static __INLINE int32_t isrSignalSet (osThreadId thread_id, int32_t signals) {
elessair 0:f269e3021894 1098 P_TCB ptcb;
elessair 0:f269e3021894 1099 int32_t sig;
elessair 0:f269e3021894 1100
elessair 0:f269e3021894 1101 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
elessair 0:f269e3021894 1102 if (ptcb == NULL) return 0x80000000;
elessair 0:f269e3021894 1103
elessair 0:f269e3021894 1104 if (signals & (0xFFFFFFFF << osFeature_Signals)) return 0x80000000;
elessair 0:f269e3021894 1105
elessair 0:f269e3021894 1106 sig = ptcb->events; // Previous signal flags
elessair 0:f269e3021894 1107
elessair 0:f269e3021894 1108 isr_evt_set(signals, ptcb->task_id); // Set event flags
elessair 0:f269e3021894 1109
elessair 0:f269e3021894 1110 return sig;
elessair 0:f269e3021894 1111 }
elessair 0:f269e3021894 1112
elessair 0:f269e3021894 1113
elessair 0:f269e3021894 1114 // Signal Public API
elessair 0:f269e3021894 1115
elessair 0:f269e3021894 1116 /// Set the specified Signal Flags of an active thread
elessair 0:f269e3021894 1117 int32_t osSignalSet (osThreadId thread_id, int32_t signals) {
elessair 0:f269e3021894 1118 if (__get_CONTROL() == MODE_IRQ) { // in ISR
elessair 0:f269e3021894 1119 return isrSignalSet(thread_id, signals);
elessair 0:f269e3021894 1120 } else { // in Thread
elessair 0:f269e3021894 1121 return __svcSignalSet(thread_id, signals);
elessair 0:f269e3021894 1122 }
elessair 0:f269e3021894 1123 }
elessair 0:f269e3021894 1124
elessair 0:f269e3021894 1125 /// Clear the specified Signal Flags of an active thread
elessair 0:f269e3021894 1126 int32_t osSignalClear (osThreadId thread_id, int32_t signals) {
elessair 0:f269e3021894 1127 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 1128 return __svcSignalClear(thread_id, signals);
elessair 0:f269e3021894 1129 }
elessair 0:f269e3021894 1130
elessair 0:f269e3021894 1131 /// Get Signal Flags status of an active thread
elessair 0:f269e3021894 1132 int32_t osSignalGet (osThreadId thread_id) {
elessair 0:f269e3021894 1133 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 1134 return __svcSignalGet(thread_id);
elessair 0:f269e3021894 1135 }
elessair 0:f269e3021894 1136
elessair 0:f269e3021894 1137 /// Wait for one or more Signal Flags to become signaled for the current RUNNING thread
elessair 0:f269e3021894 1138 os_InRegs osEvent osSignalWait (int32_t signals, uint32_t millisec) {
elessair 0:f269e3021894 1139 osEvent ret;
elessair 0:f269e3021894 1140
elessair 0:f269e3021894 1141 if (__get_CONTROL() == MODE_IRQ) { // Not allowed in ISR
elessair 0:f269e3021894 1142 ret.status = osErrorISR;
elessair 0:f269e3021894 1143 return ret;
elessair 0:f269e3021894 1144 }
elessair 0:f269e3021894 1145 return __svcSignalWait(signals, millisec);
elessair 0:f269e3021894 1146 }
elessair 0:f269e3021894 1147
elessair 0:f269e3021894 1148
elessair 0:f269e3021894 1149 // ==== Mutex Management ====
elessair 0:f269e3021894 1150
elessair 0:f269e3021894 1151 // Mutex Service Calls declarations
elessair 0:f269e3021894 1152 SVC_1_1(svcMutexCreate, osMutexId, osMutexDef_t *, RET_pointer)
elessair 0:f269e3021894 1153 SVC_2_1(svcMutexWait, osStatus, osMutexId, uint32_t, RET_osStatus)
elessair 0:f269e3021894 1154 SVC_1_1(svcMutexRelease, osStatus, osMutexId, RET_osStatus)
elessair 0:f269e3021894 1155 SVC_1_1(svcMutexDelete, osStatus, osMutexId, RET_osStatus)
elessair 0:f269e3021894 1156
elessair 0:f269e3021894 1157 // Mutex Service Calls
elessair 0:f269e3021894 1158
elessair 0:f269e3021894 1159 /// Create and Initialize a Mutex object
elessair 0:f269e3021894 1160 osMutexId svcMutexCreate (osMutexDef_t *mutex_def) {
elessair 0:f269e3021894 1161 OS_ID mut;
elessair 0:f269e3021894 1162
elessair 0:f269e3021894 1163 if (mutex_def == NULL) {
elessair 0:f269e3021894 1164 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 1165 return NULL;
elessair 0:f269e3021894 1166 }
elessair 0:f269e3021894 1167
elessair 0:f269e3021894 1168 mut = mutex_def->mutex;
elessair 0:f269e3021894 1169 if (mut == NULL) {
elessair 0:f269e3021894 1170 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 1171 return NULL;
elessair 0:f269e3021894 1172 }
elessair 0:f269e3021894 1173
elessair 0:f269e3021894 1174 if (((P_MUCB)mut)->cb_type != 0) {
elessair 0:f269e3021894 1175 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 1176 return NULL;
elessair 0:f269e3021894 1177 }
elessair 0:f269e3021894 1178
elessair 0:f269e3021894 1179 rt_mut_init(mut); // Initialize Mutex
elessair 0:f269e3021894 1180
elessair 0:f269e3021894 1181 return mut;
elessair 0:f269e3021894 1182 }
elessair 0:f269e3021894 1183
elessair 0:f269e3021894 1184 /// Wait until a Mutex becomes available
elessair 0:f269e3021894 1185 osStatus svcMutexWait (osMutexId mutex_id, uint32_t millisec) {
elessair 0:f269e3021894 1186 OS_ID mut;
elessair 0:f269e3021894 1187 OS_RESULT res;
elessair 0:f269e3021894 1188
elessair 0:f269e3021894 1189 mut = rt_id2obj(mutex_id);
elessair 0:f269e3021894 1190 if (mut == NULL) return osErrorParameter;
elessair 0:f269e3021894 1191
elessair 0:f269e3021894 1192 if (((P_MUCB)mut)->cb_type != MUCB) return osErrorParameter;
elessair 0:f269e3021894 1193
elessair 0:f269e3021894 1194 res = rt_mut_wait(mut, rt_ms2tick(millisec)); // Wait for Mutex
elessair 0:f269e3021894 1195
elessair 0:f269e3021894 1196 if (res == OS_R_TMO) {
elessair 0:f269e3021894 1197 return (millisec ? osErrorTimeoutResource : osErrorResource);
elessair 0:f269e3021894 1198 }
elessair 0:f269e3021894 1199
elessair 0:f269e3021894 1200 return osOK;
elessair 0:f269e3021894 1201 }
elessair 0:f269e3021894 1202
elessair 0:f269e3021894 1203 /// Release a Mutex that was obtained with osMutexWait
elessair 0:f269e3021894 1204 osStatus svcMutexRelease (osMutexId mutex_id) {
elessair 0:f269e3021894 1205 OS_ID mut;
elessair 0:f269e3021894 1206 OS_RESULT res;
elessair 0:f269e3021894 1207
elessair 0:f269e3021894 1208 mut = rt_id2obj(mutex_id);
elessair 0:f269e3021894 1209 if (mut == NULL) return osErrorParameter;
elessair 0:f269e3021894 1210
elessair 0:f269e3021894 1211 if (((P_MUCB)mut)->cb_type != MUCB) return osErrorParameter;
elessair 0:f269e3021894 1212
elessair 0:f269e3021894 1213 res = rt_mut_release(mut); // Release Mutex
elessair 0:f269e3021894 1214
elessair 0:f269e3021894 1215 if (res == OS_R_NOK) return osErrorResource; // Thread not owner or Zero Counter
elessair 0:f269e3021894 1216
elessair 0:f269e3021894 1217 return osOK;
elessair 0:f269e3021894 1218 }
elessair 0:f269e3021894 1219
elessair 0:f269e3021894 1220 /// Delete a Mutex that was created by osMutexCreate
elessair 0:f269e3021894 1221 osStatus svcMutexDelete (osMutexId mutex_id) {
elessair 0:f269e3021894 1222 OS_ID mut;
elessair 0:f269e3021894 1223
elessair 0:f269e3021894 1224 mut = rt_id2obj(mutex_id);
elessair 0:f269e3021894 1225 if (mut == NULL) return osErrorParameter;
elessair 0:f269e3021894 1226
elessair 0:f269e3021894 1227 if (((P_MUCB)mut)->cb_type != MUCB) return osErrorParameter;
elessair 0:f269e3021894 1228
elessair 0:f269e3021894 1229 rt_mut_delete(mut); // Release Mutex
elessair 0:f269e3021894 1230
elessair 0:f269e3021894 1231 return osOK;
elessair 0:f269e3021894 1232 }
elessair 0:f269e3021894 1233
elessair 0:f269e3021894 1234
elessair 0:f269e3021894 1235 // Mutex Public API
elessair 0:f269e3021894 1236
elessair 0:f269e3021894 1237 /// Create and Initialize a Mutex object
elessair 0:f269e3021894 1238 osMutexId osMutexCreate (osMutexDef_t *mutex_def) {
elessair 0:f269e3021894 1239 if (__get_CONTROL() == MODE_IRQ) return NULL; // Not allowed in ISR
elessair 0:f269e3021894 1240 if ((__get_CONTROL() == MODE_SUPERVISOR) && (os_running == 0)) {
elessair 0:f269e3021894 1241 // Privileged and not running
elessair 0:f269e3021894 1242 return svcMutexCreate(mutex_def);
elessair 0:f269e3021894 1243 } else {
elessair 0:f269e3021894 1244 return __svcMutexCreate(mutex_def);
elessair 0:f269e3021894 1245 }
elessair 0:f269e3021894 1246 }
elessair 0:f269e3021894 1247
elessair 0:f269e3021894 1248 /// Wait until a Mutex becomes available
elessair 0:f269e3021894 1249 osStatus osMutexWait (osMutexId mutex_id, uint32_t millisec) {
elessair 0:f269e3021894 1250 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 1251 return __svcMutexWait(mutex_id, millisec);
elessair 0:f269e3021894 1252 }
elessair 0:f269e3021894 1253
elessair 0:f269e3021894 1254 /// Release a Mutex that was obtained with osMutexWait
elessair 0:f269e3021894 1255 osStatus osMutexRelease (osMutexId mutex_id) {
elessair 0:f269e3021894 1256 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 1257 return __svcMutexRelease(mutex_id);
elessair 0:f269e3021894 1258 }
elessair 0:f269e3021894 1259
elessair 0:f269e3021894 1260 /// Delete a Mutex that was created by osMutexCreate
elessair 0:f269e3021894 1261 osStatus osMutexDelete (osMutexId mutex_id) {
elessair 0:f269e3021894 1262 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 1263 return __svcMutexDelete(mutex_id);
elessair 0:f269e3021894 1264 }
elessair 0:f269e3021894 1265
elessair 0:f269e3021894 1266
elessair 0:f269e3021894 1267 // ==== Semaphore Management ====
elessair 0:f269e3021894 1268
elessair 0:f269e3021894 1269 // Semaphore Service Calls declarations
elessair 0:f269e3021894 1270 SVC_2_1(svcSemaphoreCreate, osSemaphoreId, const osSemaphoreDef_t *, int32_t, RET_pointer)
elessair 0:f269e3021894 1271 SVC_2_1(svcSemaphoreWait, int32_t, osSemaphoreId, uint32_t, RET_int32_t)
elessair 0:f269e3021894 1272 SVC_1_1(svcSemaphoreRelease, osStatus, osSemaphoreId, RET_osStatus)
elessair 0:f269e3021894 1273 SVC_1_1(svcSemaphoreDelete, osStatus, osSemaphoreId, RET_osStatus)
elessair 0:f269e3021894 1274
elessair 0:f269e3021894 1275 // Semaphore Service Calls
elessair 0:f269e3021894 1276
elessair 0:f269e3021894 1277 /// Create and Initialize a Semaphore object
elessair 0:f269e3021894 1278 osSemaphoreId svcSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count) {
elessair 0:f269e3021894 1279 OS_ID sem;
elessair 0:f269e3021894 1280
elessair 0:f269e3021894 1281 if (semaphore_def == NULL) {
elessair 0:f269e3021894 1282 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 1283 return NULL;
elessair 0:f269e3021894 1284 }
elessair 0:f269e3021894 1285
elessair 0:f269e3021894 1286 sem = semaphore_def->semaphore;
elessair 0:f269e3021894 1287 if (sem == NULL) {
elessair 0:f269e3021894 1288 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 1289 return NULL;
elessair 0:f269e3021894 1290 }
elessair 0:f269e3021894 1291
elessair 0:f269e3021894 1292 if (((P_SCB)sem)->cb_type != 0) {
elessair 0:f269e3021894 1293 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 1294 return NULL;
elessair 0:f269e3021894 1295 }
elessair 0:f269e3021894 1296
elessair 0:f269e3021894 1297 if (count > osFeature_Semaphore) {
elessair 0:f269e3021894 1298 sysThreadError(osErrorValue);
elessair 0:f269e3021894 1299 return NULL;
elessair 0:f269e3021894 1300 }
elessair 0:f269e3021894 1301
elessair 0:f269e3021894 1302 rt_sem_init(sem, count); // Initialize Semaphore
elessair 0:f269e3021894 1303
elessair 0:f269e3021894 1304 return sem;
elessair 0:f269e3021894 1305 }
elessair 0:f269e3021894 1306
elessair 0:f269e3021894 1307 /// Wait until a Semaphore becomes available
elessair 0:f269e3021894 1308 int32_t svcSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec) {
elessair 0:f269e3021894 1309 OS_ID sem;
elessair 0:f269e3021894 1310 OS_RESULT res;
elessair 0:f269e3021894 1311
elessair 0:f269e3021894 1312 sem = rt_id2obj(semaphore_id);
elessair 0:f269e3021894 1313 if (sem == NULL) return -1;
elessair 0:f269e3021894 1314
elessair 0:f269e3021894 1315 if (((P_SCB)sem)->cb_type != SCB) return -1;
elessair 0:f269e3021894 1316
elessair 0:f269e3021894 1317 res = rt_sem_wait(sem, rt_ms2tick(millisec)); // Wait for Semaphore
elessair 0:f269e3021894 1318
elessair 0:f269e3021894 1319 if (res == OS_R_TMO) return 0; // Timeout
elessair 0:f269e3021894 1320
elessair 0:f269e3021894 1321 return (((P_SCB)sem)->tokens + 1);
elessair 0:f269e3021894 1322 }
elessair 0:f269e3021894 1323
elessair 0:f269e3021894 1324 /// Release a Semaphore
elessair 0:f269e3021894 1325 osStatus svcSemaphoreRelease (osSemaphoreId semaphore_id) {
elessair 0:f269e3021894 1326 OS_ID sem;
elessair 0:f269e3021894 1327
elessair 0:f269e3021894 1328 sem = rt_id2obj(semaphore_id);
elessair 0:f269e3021894 1329 if (sem == NULL) return osErrorParameter;
elessair 0:f269e3021894 1330
elessair 0:f269e3021894 1331 if (((P_SCB)sem)->cb_type != SCB) return osErrorParameter;
elessair 0:f269e3021894 1332
elessair 0:f269e3021894 1333 if (((P_SCB)sem)->tokens == osFeature_Semaphore) return osErrorResource;
elessair 0:f269e3021894 1334
elessair 0:f269e3021894 1335 rt_sem_send(sem); // Release Semaphore
elessair 0:f269e3021894 1336
elessair 0:f269e3021894 1337 return osOK;
elessair 0:f269e3021894 1338 }
elessair 0:f269e3021894 1339
elessair 0:f269e3021894 1340 /// Delete a Semaphore that was created by osSemaphoreCreate
elessair 0:f269e3021894 1341 osStatus svcSemaphoreDelete (osSemaphoreId semaphore_id) {
elessair 0:f269e3021894 1342 OS_ID sem;
elessair 0:f269e3021894 1343
elessair 0:f269e3021894 1344 sem = rt_id2obj(semaphore_id);
elessair 0:f269e3021894 1345 if (sem == NULL) return osErrorParameter;
elessair 0:f269e3021894 1346
elessair 0:f269e3021894 1347 if (((P_SCB)sem)->cb_type != SCB) return osErrorParameter;
elessair 0:f269e3021894 1348
elessair 0:f269e3021894 1349 rt_sem_delete(sem); // Delete Semaphore
elessair 0:f269e3021894 1350
elessair 0:f269e3021894 1351 return osOK;
elessair 0:f269e3021894 1352 }
elessair 0:f269e3021894 1353
elessair 0:f269e3021894 1354
elessair 0:f269e3021894 1355 // Semaphore ISR Calls
elessair 0:f269e3021894 1356
elessair 0:f269e3021894 1357 /// Release a Semaphore
elessair 0:f269e3021894 1358 static __INLINE osStatus isrSemaphoreRelease (osSemaphoreId semaphore_id) {
elessair 0:f269e3021894 1359 OS_ID sem;
elessair 0:f269e3021894 1360
elessair 0:f269e3021894 1361 sem = rt_id2obj(semaphore_id);
elessair 0:f269e3021894 1362 if (sem == NULL) return osErrorParameter;
elessair 0:f269e3021894 1363
elessair 0:f269e3021894 1364 if (((P_SCB)sem)->cb_type != SCB) return osErrorParameter;
elessair 0:f269e3021894 1365
elessair 0:f269e3021894 1366 if (((P_SCB)sem)->tokens == osFeature_Semaphore) return osErrorResource;
elessair 0:f269e3021894 1367
elessair 0:f269e3021894 1368 isr_sem_send(sem); // Release Semaphore
elessair 0:f269e3021894 1369
elessair 0:f269e3021894 1370 return osOK;
elessair 0:f269e3021894 1371 }
elessair 0:f269e3021894 1372
elessair 0:f269e3021894 1373
elessair 0:f269e3021894 1374 // Semaphore Public API
elessair 0:f269e3021894 1375
elessair 0:f269e3021894 1376 /// Create and Initialize a Semaphore object
elessair 0:f269e3021894 1377 osSemaphoreId osSemaphoreCreate (osSemaphoreDef_t *semaphore_def, int32_t count) {
elessair 0:f269e3021894 1378 if (__get_CONTROL() == MODE_IRQ) return NULL; // Not allowed in ISR
elessair 0:f269e3021894 1379 if ((__get_CONTROL() == MODE_SUPERVISOR) && (os_running == 0)) {
elessair 0:f269e3021894 1380 // Privileged and not running
elessair 0:f269e3021894 1381 return svcSemaphoreCreate(semaphore_def, count);
elessair 0:f269e3021894 1382 } else {
elessair 0:f269e3021894 1383 return __svcSemaphoreCreate(semaphore_def, count);
elessair 0:f269e3021894 1384 }
elessair 0:f269e3021894 1385 }
elessair 0:f269e3021894 1386
elessair 0:f269e3021894 1387 /// Wait until a Semaphore becomes available
elessair 0:f269e3021894 1388 int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec) {
elessair 0:f269e3021894 1389 if (__get_CONTROL() == MODE_IRQ) return -1; // Not allowed in ISR
elessair 0:f269e3021894 1390 return __svcSemaphoreWait(semaphore_id, millisec);
elessair 0:f269e3021894 1391 }
elessair 0:f269e3021894 1392
elessair 0:f269e3021894 1393 /// Release a Semaphore
elessair 0:f269e3021894 1394 osStatus osSemaphoreRelease (osSemaphoreId semaphore_id) {
elessair 0:f269e3021894 1395 if (__get_CONTROL() == MODE_IRQ) { // in ISR
elessair 0:f269e3021894 1396 return isrSemaphoreRelease(semaphore_id);
elessair 0:f269e3021894 1397 } else { // in Thread
elessair 0:f269e3021894 1398 return __svcSemaphoreRelease(semaphore_id);
elessair 0:f269e3021894 1399 }
elessair 0:f269e3021894 1400 }
elessair 0:f269e3021894 1401
elessair 0:f269e3021894 1402 /// Delete a Semaphore that was created by osSemaphoreCreate
elessair 0:f269e3021894 1403 osStatus osSemaphoreDelete (osSemaphoreId semaphore_id) {
elessair 0:f269e3021894 1404 if (__get_CONTROL() == MODE_IRQ) return osErrorISR; // Not allowed in ISR
elessair 0:f269e3021894 1405 return __svcSemaphoreDelete(semaphore_id);
elessair 0:f269e3021894 1406 }
elessair 0:f269e3021894 1407
elessair 0:f269e3021894 1408
elessair 0:f269e3021894 1409 // ==== Memory Management Functions ====
elessair 0:f269e3021894 1410
elessair 0:f269e3021894 1411 // Memory Management Helper Functions
elessair 0:f269e3021894 1412
elessair 0:f269e3021894 1413 // Clear Memory Box (Zero init)
elessair 0:f269e3021894 1414 static void rt_clr_box (void *box_mem, void *box) {
elessair 0:f269e3021894 1415 uint32_t *p, n;
elessair 0:f269e3021894 1416
elessair 0:f269e3021894 1417 if (box) {
elessair 0:f269e3021894 1418 p = box;
elessair 0:f269e3021894 1419 for (n = ((P_BM)box_mem)->blk_size; n; n -= 4) {
elessair 0:f269e3021894 1420 *p++ = 0;
elessair 0:f269e3021894 1421 }
elessair 0:f269e3021894 1422 }
elessair 0:f269e3021894 1423 }
elessair 0:f269e3021894 1424
elessair 0:f269e3021894 1425 // Memory Management Service Calls declarations
elessair 0:f269e3021894 1426 SVC_1_1(svcPoolCreate, osPoolId, const osPoolDef_t *, RET_pointer)
elessair 0:f269e3021894 1427 SVC_2_1(sysPoolAlloc, void *, osPoolId, uint32_t, RET_pointer)
elessair 0:f269e3021894 1428 SVC_2_1(sysPoolFree, osStatus, osPoolId, void *, RET_osStatus)
elessair 0:f269e3021894 1429
elessair 0:f269e3021894 1430 // Memory Management Service & ISR Calls
elessair 0:f269e3021894 1431
elessair 0:f269e3021894 1432 /// Create and Initialize memory pool
elessair 0:f269e3021894 1433 osPoolId svcPoolCreate (const osPoolDef_t *pool_def) {
elessair 0:f269e3021894 1434 uint32_t blk_sz;
elessair 0:f269e3021894 1435
elessair 0:f269e3021894 1436 if ((pool_def == NULL) ||
elessair 0:f269e3021894 1437 (pool_def->pool_sz == 0) ||
elessair 0:f269e3021894 1438 (pool_def->item_sz == 0) ||
elessair 0:f269e3021894 1439 (pool_def->pool == NULL)) {
elessair 0:f269e3021894 1440 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 1441 return NULL;
elessair 0:f269e3021894 1442 }
elessair 0:f269e3021894 1443
elessair 0:f269e3021894 1444 blk_sz = (pool_def->item_sz + 3) & ~3;
elessair 0:f269e3021894 1445
elessair 0:f269e3021894 1446 _init_box(pool_def->pool, sizeof(struct OS_BM) + pool_def->pool_sz * blk_sz, blk_sz);
elessair 0:f269e3021894 1447
elessair 0:f269e3021894 1448 return pool_def->pool;
elessair 0:f269e3021894 1449 }
elessair 0:f269e3021894 1450
elessair 0:f269e3021894 1451 /// Allocate a memory block from a memory pool
elessair 0:f269e3021894 1452 void *sysPoolAlloc (osPoolId pool_id, uint32_t clr) {
elessair 0:f269e3021894 1453 void *ptr;
elessair 0:f269e3021894 1454
elessair 0:f269e3021894 1455 if (pool_id == NULL) return NULL;
elessair 0:f269e3021894 1456
elessair 0:f269e3021894 1457 ptr = rt_alloc_box(pool_id);
elessair 0:f269e3021894 1458 if (clr) {
elessair 0:f269e3021894 1459 rt_clr_box(pool_id, ptr);
elessair 0:f269e3021894 1460 }
elessair 0:f269e3021894 1461
elessair 0:f269e3021894 1462 return ptr;
elessair 0:f269e3021894 1463 }
elessair 0:f269e3021894 1464
elessair 0:f269e3021894 1465 /// Return an allocated memory block back to a specific memory pool
elessair 0:f269e3021894 1466 osStatus sysPoolFree (osPoolId pool_id, void *block) {
elessair 0:f269e3021894 1467 int32_t res;
elessair 0:f269e3021894 1468
elessair 0:f269e3021894 1469 if (pool_id == NULL) return osErrorParameter;
elessair 0:f269e3021894 1470
elessair 0:f269e3021894 1471 res = rt_free_box(pool_id, block);
elessair 0:f269e3021894 1472 if (res != 0) return osErrorValue;
elessair 0:f269e3021894 1473
elessair 0:f269e3021894 1474 return osOK;
elessair 0:f269e3021894 1475 }
elessair 0:f269e3021894 1476
elessair 0:f269e3021894 1477
elessair 0:f269e3021894 1478 // Memory Management Public API
elessair 0:f269e3021894 1479
elessair 0:f269e3021894 1480 /// Create and Initialize memory pool
elessair 0:f269e3021894 1481 osPoolId osPoolCreate (osPoolDef_t *pool_def) {
elessair 0:f269e3021894 1482 if (__get_CONTROL() == MODE_IRQ) return NULL; // Not allowed in ISR
elessair 0:f269e3021894 1483 if ((__get_CONTROL() == MODE_SUPERVISOR) && (os_running == 0)) {
elessair 0:f269e3021894 1484 // Privileged and not running
elessair 0:f269e3021894 1485 return svcPoolCreate(pool_def);
elessair 0:f269e3021894 1486 } else {
elessair 0:f269e3021894 1487 return __svcPoolCreate(pool_def);
elessair 0:f269e3021894 1488 }
elessair 0:f269e3021894 1489 }
elessair 0:f269e3021894 1490
elessair 0:f269e3021894 1491 /// Allocate a memory block from a memory pool
elessair 0:f269e3021894 1492 void *osPoolAlloc (osPoolId pool_id) {
elessair 0:f269e3021894 1493 if ((__get_CONTROL() == MODE_IRQ) || (__get_CONTROL() == MODE_SUPERVISOR)) { // in ISR or Privileged
elessair 0:f269e3021894 1494 return sysPoolAlloc(pool_id, 0);
elessair 0:f269e3021894 1495 } else { // in Thread
elessair 0:f269e3021894 1496 return __sysPoolAlloc(pool_id, 0);
elessair 0:f269e3021894 1497 }
elessair 0:f269e3021894 1498 }
elessair 0:f269e3021894 1499
elessair 0:f269e3021894 1500 /// Allocate a memory block from a memory pool and set memory block to zero
elessair 0:f269e3021894 1501 void *osPoolCAlloc (osPoolId pool_id) {
elessair 0:f269e3021894 1502 if ((__get_CONTROL() == MODE_IRQ) || (__get_CONTROL() == MODE_SUPERVISOR)) { // in ISR or Privileged
elessair 0:f269e3021894 1503 return sysPoolAlloc(pool_id, 1);
elessair 0:f269e3021894 1504 } else { // in Thread
elessair 0:f269e3021894 1505 return __sysPoolAlloc(pool_id, 1);
elessair 0:f269e3021894 1506 }
elessair 0:f269e3021894 1507 }
elessair 0:f269e3021894 1508
elessair 0:f269e3021894 1509 /// Return an allocated memory block back to a specific memory pool
elessair 0:f269e3021894 1510 osStatus osPoolFree (osPoolId pool_id, void *block) {
elessair 0:f269e3021894 1511 if ((__get_CONTROL() == MODE_IRQ) || (__get_CONTROL() == MODE_SUPERVISOR)) { // in ISR or Privileged
elessair 0:f269e3021894 1512 return sysPoolFree(pool_id, block);
elessair 0:f269e3021894 1513 } else { // in Thread
elessair 0:f269e3021894 1514 return __sysPoolFree(pool_id, block);
elessair 0:f269e3021894 1515 }
elessair 0:f269e3021894 1516 }
elessair 0:f269e3021894 1517
elessair 0:f269e3021894 1518
elessair 0:f269e3021894 1519 // ==== Message Queue Management Functions ====
elessair 0:f269e3021894 1520
elessair 0:f269e3021894 1521 // Message Queue Management Service Calls declarations
elessair 0:f269e3021894 1522 SVC_2_1(svcMessageCreate, osMessageQId, osMessageQDef_t *, osThreadId, RET_pointer)
elessair 0:f269e3021894 1523 SVC_3_1(svcMessagePut, osStatus, osMessageQId, uint32_t, uint32_t, RET_osStatus)
elessair 0:f269e3021894 1524 SVC_2_3(svcMessageGet, os_InRegs osEvent, osMessageQId, uint32_t, RET_osEvent)
elessair 0:f269e3021894 1525
elessair 0:f269e3021894 1526 // Message Queue Service Calls
elessair 0:f269e3021894 1527
elessair 0:f269e3021894 1528 /// Create and Initialize Message Queue
elessair 0:f269e3021894 1529 osMessageQId svcMessageCreate (osMessageQDef_t *queue_def, osThreadId thread_id) {
elessair 0:f269e3021894 1530
elessair 0:f269e3021894 1531 if ((queue_def == NULL) ||
elessair 0:f269e3021894 1532 (queue_def->queue_sz == 0) ||
elessair 0:f269e3021894 1533 (queue_def->pool == NULL)) {
elessair 0:f269e3021894 1534 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 1535 return NULL;
elessair 0:f269e3021894 1536 }
elessair 0:f269e3021894 1537
elessair 0:f269e3021894 1538 if (((P_MCB)queue_def->pool)->cb_type != 0) {
elessair 0:f269e3021894 1539 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 1540 return NULL;
elessair 0:f269e3021894 1541 }
elessair 0:f269e3021894 1542
elessair 0:f269e3021894 1543 rt_mbx_init(queue_def->pool, 4*(queue_def->queue_sz + 4));
elessair 0:f269e3021894 1544
elessair 0:f269e3021894 1545 return queue_def->pool;
elessair 0:f269e3021894 1546 }
elessair 0:f269e3021894 1547
elessair 0:f269e3021894 1548 /// Put a Message to a Queue
elessair 0:f269e3021894 1549 osStatus svcMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
elessair 0:f269e3021894 1550 OS_RESULT res;
elessair 0:f269e3021894 1551
elessair 0:f269e3021894 1552 if (queue_id == NULL) return osErrorParameter;
elessair 0:f269e3021894 1553
elessair 0:f269e3021894 1554 if (((P_MCB)queue_id)->cb_type != MCB) return osErrorParameter;
elessair 0:f269e3021894 1555
elessair 0:f269e3021894 1556 res = rt_mbx_send(queue_id, (void *)info, rt_ms2tick(millisec));
elessair 0:f269e3021894 1557
elessair 0:f269e3021894 1558 if (res == OS_R_TMO) {
elessair 0:f269e3021894 1559 return (millisec ? osErrorTimeoutResource : osErrorResource);
elessair 0:f269e3021894 1560 }
elessair 0:f269e3021894 1561
elessair 0:f269e3021894 1562 return osOK;
elessair 0:f269e3021894 1563 }
elessair 0:f269e3021894 1564
elessair 0:f269e3021894 1565 /// Get a Message or Wait for a Message from a Queue
elessair 0:f269e3021894 1566 os_InRegs osEvent_type svcMessageGet (osMessageQId queue_id, uint32_t millisec) {
elessair 0:f269e3021894 1567 OS_RESULT res;
elessair 0:f269e3021894 1568 osEvent ret;
elessair 0:f269e3021894 1569
elessair 0:f269e3021894 1570 if (queue_id == NULL) {
elessair 0:f269e3021894 1571 ret.status = osErrorParameter;
elessair 0:f269e3021894 1572 return osEvent_ret_status;
elessair 0:f269e3021894 1573 }
elessair 0:f269e3021894 1574
elessair 0:f269e3021894 1575 if (((P_MCB)queue_id)->cb_type != MCB) {
elessair 0:f269e3021894 1576 ret.status = osErrorParameter;
elessair 0:f269e3021894 1577 return osEvent_ret_status;
elessair 0:f269e3021894 1578 }
elessair 0:f269e3021894 1579
elessair 0:f269e3021894 1580 res = rt_mbx_wait(queue_id, &ret.value.p, rt_ms2tick(millisec));
elessair 0:f269e3021894 1581
elessair 0:f269e3021894 1582 if (res == OS_R_TMO) {
elessair 0:f269e3021894 1583 ret.status = millisec ? osEventTimeout : osOK;
elessair 0:f269e3021894 1584 return osEvent_ret_value;
elessair 0:f269e3021894 1585 }
elessair 0:f269e3021894 1586
elessair 0:f269e3021894 1587 ret.status = osEventMessage;
elessair 0:f269e3021894 1588
elessair 0:f269e3021894 1589 return osEvent_ret_value;
elessair 0:f269e3021894 1590 }
elessair 0:f269e3021894 1591
elessair 0:f269e3021894 1592
elessair 0:f269e3021894 1593 // Message Queue ISR Calls
elessair 0:f269e3021894 1594
elessair 0:f269e3021894 1595 /// Put a Message to a Queue
elessair 0:f269e3021894 1596 static __INLINE osStatus isrMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
elessair 0:f269e3021894 1597
elessair 0:f269e3021894 1598 if ((queue_id == NULL) || (millisec != 0)) {
elessair 0:f269e3021894 1599 return osErrorParameter;
elessair 0:f269e3021894 1600 }
elessair 0:f269e3021894 1601
elessair 0:f269e3021894 1602 if (((P_MCB)queue_id)->cb_type != MCB) return osErrorParameter;
elessair 0:f269e3021894 1603
elessair 0:f269e3021894 1604 if (rt_mbx_check(queue_id) == 0) { // Check if Queue is full
elessair 0:f269e3021894 1605 return osErrorResource;
elessair 0:f269e3021894 1606 }
elessair 0:f269e3021894 1607
elessair 0:f269e3021894 1608 isr_mbx_send(queue_id, (void *)info);
elessair 0:f269e3021894 1609
elessair 0:f269e3021894 1610 return osOK;
elessair 0:f269e3021894 1611 }
elessair 0:f269e3021894 1612
elessair 0:f269e3021894 1613 /// Get a Message or Wait for a Message from a Queue
elessair 0:f269e3021894 1614 static __INLINE os_InRegs osEvent isrMessageGet (osMessageQId queue_id, uint32_t millisec) {
elessair 0:f269e3021894 1615 OS_RESULT res;
elessair 0:f269e3021894 1616 osEvent ret;
elessair 0:f269e3021894 1617
elessair 0:f269e3021894 1618 if ((queue_id == NULL) || (millisec != 0)) {
elessair 0:f269e3021894 1619 ret.status = osErrorParameter;
elessair 0:f269e3021894 1620 return ret;
elessair 0:f269e3021894 1621 }
elessair 0:f269e3021894 1622
elessair 0:f269e3021894 1623 if (((P_MCB)queue_id)->cb_type != MCB) {
elessair 0:f269e3021894 1624 ret.status = osErrorParameter;
elessair 0:f269e3021894 1625 return ret;
elessair 0:f269e3021894 1626 }
elessair 0:f269e3021894 1627
elessair 0:f269e3021894 1628 res = isr_mbx_receive(queue_id, &ret.value.p);
elessair 0:f269e3021894 1629
elessair 0:f269e3021894 1630 if (res != OS_R_MBX) {
elessair 0:f269e3021894 1631 ret.status = osOK;
elessair 0:f269e3021894 1632 return ret;
elessair 0:f269e3021894 1633 }
elessair 0:f269e3021894 1634
elessair 0:f269e3021894 1635 ret.status = osEventMessage;
elessair 0:f269e3021894 1636
elessair 0:f269e3021894 1637 return ret;
elessair 0:f269e3021894 1638 }
elessair 0:f269e3021894 1639
elessair 0:f269e3021894 1640
elessair 0:f269e3021894 1641 // Message Queue Management Public API
elessair 0:f269e3021894 1642
elessair 0:f269e3021894 1643 /// Create and Initialize Message Queue
elessair 0:f269e3021894 1644 osMessageQId osMessageCreate (osMessageQDef_t *queue_def, osThreadId thread_id) {
elessair 0:f269e3021894 1645 if (__get_CONTROL() == MODE_IRQ) return NULL; // Not allowed in ISR
elessair 0:f269e3021894 1646 if ((__get_CONTROL() == MODE_SUPERVISOR) && (os_running == 0)) {
elessair 0:f269e3021894 1647 // Privileged and not running
elessair 0:f269e3021894 1648 return svcMessageCreate(queue_def, thread_id);
elessair 0:f269e3021894 1649 } else {
elessair 0:f269e3021894 1650 return __svcMessageCreate(queue_def, thread_id);
elessair 0:f269e3021894 1651 }
elessair 0:f269e3021894 1652 }
elessair 0:f269e3021894 1653
elessair 0:f269e3021894 1654 /// Put a Message to a Queue
elessair 0:f269e3021894 1655 osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
elessair 0:f269e3021894 1656 if (__get_CONTROL() == MODE_IRQ) { // in ISR
elessair 0:f269e3021894 1657 return isrMessagePut(queue_id, info, millisec);
elessair 0:f269e3021894 1658 } else { // in Thread
elessair 0:f269e3021894 1659 return __svcMessagePut(queue_id, info, millisec);
elessair 0:f269e3021894 1660 }
elessair 0:f269e3021894 1661 }
elessair 0:f269e3021894 1662
elessair 0:f269e3021894 1663 /// Get a Message or Wait for a Message from a Queue
elessair 0:f269e3021894 1664 os_InRegs osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec) {
elessair 0:f269e3021894 1665 if (__get_CONTROL() == MODE_IRQ) { // in ISR
elessair 0:f269e3021894 1666 return isrMessageGet(queue_id, millisec);
elessair 0:f269e3021894 1667 } else { // in Thread
elessair 0:f269e3021894 1668 return __svcMessageGet(queue_id, millisec);
elessair 0:f269e3021894 1669 }
elessair 0:f269e3021894 1670 }
elessair 0:f269e3021894 1671
elessair 0:f269e3021894 1672
elessair 0:f269e3021894 1673 // ==== Mail Queue Management Functions ====
elessair 0:f269e3021894 1674
elessair 0:f269e3021894 1675 // Mail Queue Management Service Calls declarations
elessair 0:f269e3021894 1676 SVC_2_1(svcMailCreate, osMailQId, osMailQDef_t *, osThreadId, RET_pointer)
elessair 0:f269e3021894 1677 SVC_4_1(sysMailAlloc, void *, osMailQId, uint32_t, uint32_t, uint32_t, RET_pointer)
elessair 0:f269e3021894 1678 SVC_3_1(sysMailFree, osStatus, osMailQId, void *, uint32_t, RET_osStatus)
elessair 0:f269e3021894 1679
elessair 0:f269e3021894 1680 // Mail Queue Management Service & ISR Calls
elessair 0:f269e3021894 1681
elessair 0:f269e3021894 1682 /// Create and Initialize mail queue
elessair 0:f269e3021894 1683 osMailQId svcMailCreate (osMailQDef_t *queue_def, osThreadId thread_id) {
elessair 0:f269e3021894 1684 uint32_t blk_sz;
elessair 0:f269e3021894 1685 P_MCB pmcb;
elessair 0:f269e3021894 1686 void *pool;
elessair 0:f269e3021894 1687
elessair 0:f269e3021894 1688 if ((queue_def == NULL) ||
elessair 0:f269e3021894 1689 (queue_def->queue_sz == 0) ||
elessair 0:f269e3021894 1690 (queue_def->item_sz == 0) ||
elessair 0:f269e3021894 1691 (queue_def->pool == NULL)) {
elessair 0:f269e3021894 1692 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 1693 return NULL;
elessair 0:f269e3021894 1694 }
elessair 0:f269e3021894 1695
elessair 0:f269e3021894 1696 pmcb = *(((void **)queue_def->pool) + 0);
elessair 0:f269e3021894 1697 pool = *(((void **)queue_def->pool) + 1);
elessair 0:f269e3021894 1698
elessair 0:f269e3021894 1699 if ((pool == NULL) || (pmcb == NULL) || (pmcb->cb_type != 0)) {
elessair 0:f269e3021894 1700 sysThreadError(osErrorParameter);
elessair 0:f269e3021894 1701 return NULL;
elessair 0:f269e3021894 1702 }
elessair 0:f269e3021894 1703
elessair 0:f269e3021894 1704 blk_sz = (queue_def->item_sz + 3) & ~3;
elessair 0:f269e3021894 1705
elessair 0:f269e3021894 1706 _init_box(pool, sizeof(struct OS_BM) + queue_def->queue_sz * blk_sz, blk_sz);
elessair 0:f269e3021894 1707
elessair 0:f269e3021894 1708 rt_mbx_init(pmcb, 4*(queue_def->queue_sz + 4));
elessair 0:f269e3021894 1709
elessair 0:f269e3021894 1710
elessair 0:f269e3021894 1711 return queue_def->pool;
elessair 0:f269e3021894 1712 }
elessair 0:f269e3021894 1713
elessair 0:f269e3021894 1714 /// Allocate a memory block from a mail
elessair 0:f269e3021894 1715 void *sysMailAlloc (osMailQId queue_id, uint32_t millisec, uint32_t isr, uint32_t clr) {
elessair 0:f269e3021894 1716 P_MCB pmcb;
elessair 0:f269e3021894 1717 void *pool;
elessair 0:f269e3021894 1718 void *mem;
elessair 0:f269e3021894 1719
elessair 0:f269e3021894 1720 if (queue_id == NULL) return NULL;
elessair 0:f269e3021894 1721
elessair 0:f269e3021894 1722 pmcb = *(((void **)queue_id) + 0);
elessair 0:f269e3021894 1723 pool = *(((void **)queue_id) + 1);
elessair 0:f269e3021894 1724
elessair 0:f269e3021894 1725 if ((pool == NULL) || (pmcb == NULL)) return NULL;
elessair 0:f269e3021894 1726
elessair 0:f269e3021894 1727 if (isr && (millisec != 0)) return NULL;
elessair 0:f269e3021894 1728
elessair 0:f269e3021894 1729 mem = rt_alloc_box(pool);
elessair 0:f269e3021894 1730 if (clr) {
elessair 0:f269e3021894 1731 rt_clr_box(pool, mem);
elessair 0:f269e3021894 1732 }
elessair 0:f269e3021894 1733
elessair 0:f269e3021894 1734 if ((mem == NULL) && (millisec != 0)) {
elessair 0:f269e3021894 1735 // Put Task to sleep when Memory not available
elessair 0:f269e3021894 1736 if (pmcb->p_lnk != NULL) {
elessair 0:f269e3021894 1737 rt_put_prio((P_XCB)pmcb, os_tsk.run);
elessair 0:f269e3021894 1738 } else {
elessair 0:f269e3021894 1739 pmcb->p_lnk = os_tsk.run;
elessair 0:f269e3021894 1740 os_tsk.run->p_lnk = NULL;
elessair 0:f269e3021894 1741 os_tsk.run->p_rlnk = (P_TCB)pmcb;
elessair 0:f269e3021894 1742 // Task is waiting to allocate a message
elessair 0:f269e3021894 1743 pmcb->state = 3;
elessair 0:f269e3021894 1744 }
elessair 0:f269e3021894 1745 rt_block(rt_ms2tick(millisec), WAIT_MBX);
elessair 0:f269e3021894 1746 }
elessair 0:f269e3021894 1747
elessair 0:f269e3021894 1748 return mem;
elessair 0:f269e3021894 1749 }
elessair 0:f269e3021894 1750
elessair 0:f269e3021894 1751 /// Free a memory block from a mail
elessair 0:f269e3021894 1752 osStatus sysMailFree (osMailQId queue_id, void *mail, uint32_t isr) {
elessair 0:f269e3021894 1753 P_MCB pmcb;
elessair 0:f269e3021894 1754 P_TCB ptcb;
elessair 0:f269e3021894 1755 void *pool;
elessair 0:f269e3021894 1756 void *mem;
elessair 0:f269e3021894 1757 int32_t res;
elessair 0:f269e3021894 1758
elessair 0:f269e3021894 1759 if (queue_id == NULL) return osErrorParameter;
elessair 0:f269e3021894 1760
elessair 0:f269e3021894 1761 pmcb = *(((void **)queue_id) + 0);
elessair 0:f269e3021894 1762 pool = *(((void **)queue_id) + 1);
elessair 0:f269e3021894 1763
elessair 0:f269e3021894 1764 if ((pmcb == NULL) || (pool == NULL)) return osErrorParameter;
elessair 0:f269e3021894 1765
elessair 0:f269e3021894 1766 res = rt_free_box(pool, mail);
elessair 0:f269e3021894 1767
elessair 0:f269e3021894 1768 if (res != 0) return osErrorValue;
elessair 0:f269e3021894 1769
elessair 0:f269e3021894 1770 if (pmcb->state == 3) {
elessair 0:f269e3021894 1771 // Task is waiting to allocate a message
elessair 0:f269e3021894 1772 if (isr) {
elessair 0:f269e3021894 1773 rt_psq_enq (pmcb, (U32)pool);
elessair 0:f269e3021894 1774 rt_psh_req ();
elessair 0:f269e3021894 1775 } else {
elessair 0:f269e3021894 1776 mem = rt_alloc_box(pool);
elessair 0:f269e3021894 1777 if (mem != NULL) {
elessair 0:f269e3021894 1778 ptcb = rt_get_first((P_XCB)pmcb);
elessair 0:f269e3021894 1779 if (pmcb->p_lnk == NULL) {
elessair 0:f269e3021894 1780 pmcb->state = 0;
elessair 0:f269e3021894 1781 }
elessair 0:f269e3021894 1782 rt_ret_val(ptcb, (U32)mem);
elessair 0:f269e3021894 1783 rt_rmv_dly(ptcb);
elessair 0:f269e3021894 1784 rt_dispatch(ptcb);
elessair 0:f269e3021894 1785 }
elessair 0:f269e3021894 1786 }
elessair 0:f269e3021894 1787 }
elessair 0:f269e3021894 1788
elessair 0:f269e3021894 1789 return osOK;
elessair 0:f269e3021894 1790 }
elessair 0:f269e3021894 1791
elessair 0:f269e3021894 1792
elessair 0:f269e3021894 1793 // Mail Queue Management Public API
elessair 0:f269e3021894 1794
elessair 0:f269e3021894 1795 /// Create and Initialize mail queue
elessair 0:f269e3021894 1796 osMailQId osMailCreate (osMailQDef_t *queue_def, osThreadId thread_id) {
elessair 0:f269e3021894 1797 if (__get_CONTROL() == MODE_IRQ) return NULL; // Not allowed in ISR
elessair 0:f269e3021894 1798 if ((__get_CONTROL() == MODE_SUPERVISOR) && (os_running == 0)) {
elessair 0:f269e3021894 1799 // Privileged and not running
elessair 0:f269e3021894 1800 return svcMailCreate(queue_def, thread_id);
elessair 0:f269e3021894 1801 } else {
elessair 0:f269e3021894 1802 return __svcMailCreate(queue_def, thread_id);
elessair 0:f269e3021894 1803 }
elessair 0:f269e3021894 1804 }
elessair 0:f269e3021894 1805
elessair 0:f269e3021894 1806 /// Allocate a memory block from a mail
elessair 0:f269e3021894 1807 void *osMailAlloc (osMailQId queue_id, uint32_t millisec) {
elessair 0:f269e3021894 1808 if (__get_CONTROL() == MODE_IRQ) { // in ISR
elessair 0:f269e3021894 1809 return sysMailAlloc(queue_id, millisec, 1, 0);
elessair 0:f269e3021894 1810 } else { // in Thread
elessair 0:f269e3021894 1811 return __sysMailAlloc(queue_id, millisec, 0, 0);
elessair 0:f269e3021894 1812 }
elessair 0:f269e3021894 1813 }
elessair 0:f269e3021894 1814
elessair 0:f269e3021894 1815 /// Allocate a memory block from a mail and set memory block to zero
elessair 0:f269e3021894 1816 void *osMailCAlloc (osMailQId queue_id, uint32_t millisec) {
elessair 0:f269e3021894 1817 if (__get_CONTROL() == MODE_IRQ) { // in ISR
elessair 0:f269e3021894 1818 return sysMailAlloc(queue_id, millisec, 1, 1);
elessair 0:f269e3021894 1819 } else { // in Thread
elessair 0:f269e3021894 1820 return __sysMailAlloc(queue_id, millisec, 0, 1);
elessair 0:f269e3021894 1821 }
elessair 0:f269e3021894 1822 }
elessair 0:f269e3021894 1823
elessair 0:f269e3021894 1824 /// Free a memory block from a mail
elessair 0:f269e3021894 1825 osStatus osMailFree (osMailQId queue_id, void *mail) {
elessair 0:f269e3021894 1826 if (__get_CONTROL() == MODE_IRQ) { // in ISR
elessair 0:f269e3021894 1827 return sysMailFree(queue_id, mail, 1);
elessair 0:f269e3021894 1828 } else { // in Thread
elessair 0:f269e3021894 1829 return __sysMailFree(queue_id, mail, 0);
elessair 0:f269e3021894 1830 }
elessair 0:f269e3021894 1831 }
elessair 0:f269e3021894 1832
elessair 0:f269e3021894 1833 /// Put a mail to a queue
elessair 0:f269e3021894 1834 osStatus osMailPut (osMailQId queue_id, void *mail) {
elessair 0:f269e3021894 1835 if (queue_id == NULL) return osErrorParameter;
elessair 0:f269e3021894 1836 if (mail == NULL) return osErrorValue;
elessair 0:f269e3021894 1837 return osMessagePut(*((void **)queue_id), (uint32_t)mail, 0);
elessair 0:f269e3021894 1838 }
elessair 0:f269e3021894 1839
elessair 0:f269e3021894 1840 /// Get a mail from a queue
elessair 0:f269e3021894 1841 os_InRegs osEvent osMailGet (osMailQId queue_id, uint32_t millisec) {
elessair 0:f269e3021894 1842 osEvent ret;
elessair 0:f269e3021894 1843
elessair 0:f269e3021894 1844 if (queue_id == NULL) {
elessair 0:f269e3021894 1845 ret.status = osErrorParameter;
elessair 0:f269e3021894 1846 return ret;
elessair 0:f269e3021894 1847 }
elessair 0:f269e3021894 1848
elessair 0:f269e3021894 1849 ret = osMessageGet(*((void **)queue_id), millisec);
elessair 0:f269e3021894 1850 if (ret.status == osEventMessage) ret.status = osEventMail;
elessair 0:f269e3021894 1851
elessair 0:f269e3021894 1852 return ret;
elessair 0:f269e3021894 1853 }