Versie 0.2 Het versturen via de NRF werkt nog niet helemaal omdat er per 4 bytes verstuurd moet worden. Wordt gefixt d.m.v. dynamic stuff!

Dependencies:   BufferedSerial

Dependents:   rtos_basic rtos_basic

Fork of mbed-rtos by mbed official

Committer:
mbed_official
Date:
Wed Nov 26 08:00:25 2014 +0000
Revision:
53:c35dab33c427
Parent:
49:77c8e4604045
Child:
59:28712e303960
Synchronized with git revision c3208fe6f480055643b5911ca19a9028da3054a9

Full URL: https://github.com/mbedmicro/mbed/commit/c3208fe6f480055643b5911ca19a9028da3054a9/

RTOS: IAR Export for CORTEX M3

Who changed what in which revision?

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