Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Dependents:   denki-yohou_b TestY201 Network-RTOS NTPClient_HelloWorld ... more

Deprecated

This is the mbed 2 rtos library. mbed OS 5 integrates the mbed library with mbed-rtos. With this, we have provided thread safety for all mbed APIs. If you'd like to learn about using mbed OS 5, please see the docs.

Committer:
mbed_official
Date:
Wed Apr 08 07:30:42 2015 +0100
Revision:
70:3295e347fd88
Parent:
59:28712e303960
Child:
72:83895f30f8f2
Synchronized with git revision 78c43352e9fed0d91de9d5b070cfef639dc95dc1

Full URL: https://github.com/mbedmicro/mbed/commit/78c43352e9fed0d91de9d5b070cfef639dc95dc1/

RZ_A1H - Fix a bug that there is a case that can not be normal communication in I2C.

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 59:28712e303960 550 /* Utilize the 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 /* Find a free entry in 'os_active_TCB' table. */
mbed_official 49:77c8e4604045 554 OS_TID tsk = rt_get_TID ();
mbed_official 49:77c8e4604045 555 os_active_TCB[tsk-1] = task_context;
mbed_official 49:77c8e4604045 556 task_context->task_id = tsk;
mbed_official 59:28712e303960 557 /* Pass parameter 'argv' to 'rt_init_context' */
mbed_official 59:28712e303960 558 task_context->msg = argument;
mbed_official 59:28712e303960 559 /* Initialize thread context structure, including the thread's stack. */
mbed_official 59:28712e303960 560 rt_init_context (task_context, priority, (FUNCP)thread_def->pthread);
mbed_official 59:28712e303960 561
mbed_official 59:28712e303960 562 /* Dispatch this task to the scheduler for execution. */
mbed_official 49:77c8e4604045 563 DBG_TASK_NOTIFY(task_context, __TRUE);
mbed_official 49:77c8e4604045 564 rt_dispatch (task_context);
mbed_official 49:77c8e4604045 565
mbed_official 49:77c8e4604045 566 ptcb = (P_TCB)os_active_TCB[tsk - 1]; // TCB pointer
mbed_official 49:77c8e4604045 567
mbed_official 49:77c8e4604045 568 *((uint32_t *)ptcb->tsk_stack + 13) = (uint32_t)osThreadExit;
mbed_official 49:77c8e4604045 569
mbed_official 49:77c8e4604045 570 return ptcb;
mbed_official 49:77c8e4604045 571 }
mbed_official 49:77c8e4604045 572
mbed_official 49:77c8e4604045 573 /// Return the thread ID of the current running thread
mbed_official 49:77c8e4604045 574 osThreadId svcThreadGetId (void) {
mbed_official 49:77c8e4604045 575 OS_TID tsk;
mbed_official 49:77c8e4604045 576
mbed_official 49:77c8e4604045 577 tsk = rt_tsk_self();
mbed_official 49:77c8e4604045 578 if (tsk == 0) return NULL;
mbed_official 49:77c8e4604045 579 return (P_TCB)os_active_TCB[tsk - 1];
mbed_official 49:77c8e4604045 580 }
mbed_official 49:77c8e4604045 581
mbed_official 49:77c8e4604045 582 /// Terminate execution of a thread and remove it from ActiveThreads
mbed_official 49:77c8e4604045 583 osStatus svcThreadTerminate (osThreadId thread_id) {
mbed_official 49:77c8e4604045 584 OS_RESULT res;
mbed_official 49:77c8e4604045 585 P_TCB ptcb;
mbed_official 49:77c8e4604045 586
mbed_official 49:77c8e4604045 587 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
mbed_official 49:77c8e4604045 588 if (ptcb == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 589
mbed_official 49:77c8e4604045 590 res = rt_tsk_delete(ptcb->task_id); // Delete task
mbed_official 49:77c8e4604045 591
mbed_official 49:77c8e4604045 592 if (res == OS_R_NOK) return osErrorResource; // Delete task failed
mbed_official 49:77c8e4604045 593
mbed_official 49:77c8e4604045 594 return osOK;
mbed_official 49:77c8e4604045 595 }
mbed_official 49:77c8e4604045 596
mbed_official 49:77c8e4604045 597 /// Pass control to next thread that is in state READY
mbed_official 49:77c8e4604045 598 osStatus svcThreadYield (void) {
mbed_official 49:77c8e4604045 599 rt_tsk_pass(); // Pass control to next task
mbed_official 49:77c8e4604045 600 return osOK;
mbed_official 49:77c8e4604045 601 }
mbed_official 49:77c8e4604045 602
mbed_official 49:77c8e4604045 603 /// Change priority of an active thread
mbed_official 49:77c8e4604045 604 osStatus svcThreadSetPriority (osThreadId thread_id, osPriority priority) {
mbed_official 49:77c8e4604045 605 OS_RESULT res;
mbed_official 49:77c8e4604045 606 P_TCB ptcb;
mbed_official 49:77c8e4604045 607
mbed_official 49:77c8e4604045 608 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
mbed_official 49:77c8e4604045 609 if (ptcb == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 610
mbed_official 49:77c8e4604045 611 if ((priority < osPriorityIdle) || (priority > osPriorityRealtime)) {
mbed_official 49:77c8e4604045 612 return osErrorValue;
mbed_official 49:77c8e4604045 613 }
mbed_official 49:77c8e4604045 614
mbed_official 49:77c8e4604045 615 res = rt_tsk_prio( // Change task priority
mbed_official 49:77c8e4604045 616 ptcb->task_id, // Task ID
mbed_official 49:77c8e4604045 617 priority - osPriorityIdle + 1 // New task priority
mbed_official 49:77c8e4604045 618 );
mbed_official 49:77c8e4604045 619
mbed_official 49:77c8e4604045 620 if (res == OS_R_NOK) return osErrorResource; // Change task priority failed
mbed_official 49:77c8e4604045 621
mbed_official 49:77c8e4604045 622 return osOK;
mbed_official 49:77c8e4604045 623 }
mbed_official 49:77c8e4604045 624
mbed_official 49:77c8e4604045 625 /// Get current priority of an active thread
mbed_official 49:77c8e4604045 626 osPriority svcThreadGetPriority (osThreadId thread_id) {
mbed_official 49:77c8e4604045 627 P_TCB ptcb;
mbed_official 49:77c8e4604045 628
mbed_official 49:77c8e4604045 629 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
mbed_official 49:77c8e4604045 630 if (ptcb == NULL) return osPriorityError;
mbed_official 49:77c8e4604045 631
mbed_official 49:77c8e4604045 632 return (osPriority)(ptcb->prio - 1 + osPriorityIdle);
mbed_official 49:77c8e4604045 633 }
mbed_official 49:77c8e4604045 634
mbed_official 49:77c8e4604045 635
mbed_official 49:77c8e4604045 636 // Thread Public API
mbed_official 49:77c8e4604045 637
mbed_official 49:77c8e4604045 638 /// Create a thread and add it to Active Threads and set it to state READY
mbed_official 49:77c8e4604045 639 osThreadId osThreadCreate (osThreadDef_t *thread_def, void *argument) {
mbed_official 49:77c8e4604045 640 if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
mbed_official 49:77c8e4604045 641 if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
mbed_official 49:77c8e4604045 642 // Privileged and not running
mbed_official 49:77c8e4604045 643 return svcThreadCreate(thread_def, argument);
mbed_official 49:77c8e4604045 644 } else {
mbed_official 49:77c8e4604045 645 return __svcThreadCreate(thread_def, argument);
mbed_official 49:77c8e4604045 646 }
mbed_official 49:77c8e4604045 647 }
mbed_official 49:77c8e4604045 648
mbed_official 49:77c8e4604045 649 /// Return the thread ID of the current running thread
mbed_official 49:77c8e4604045 650 osThreadId osThreadGetId (void) {
mbed_official 49:77c8e4604045 651 if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
mbed_official 49:77c8e4604045 652 return __svcThreadGetId();
mbed_official 49:77c8e4604045 653 }
mbed_official 49:77c8e4604045 654
mbed_official 49:77c8e4604045 655 /// Terminate execution of a thread and remove it from ActiveThreads
mbed_official 49:77c8e4604045 656 osStatus osThreadTerminate (osThreadId thread_id) {
mbed_official 49:77c8e4604045 657 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 658 return __svcThreadTerminate(thread_id);
mbed_official 49:77c8e4604045 659 }
mbed_official 49:77c8e4604045 660
mbed_official 49:77c8e4604045 661 /// Pass control to next thread that is in state READY
mbed_official 49:77c8e4604045 662 osStatus osThreadYield (void) {
mbed_official 49:77c8e4604045 663 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 664 return __svcThreadYield();
mbed_official 49:77c8e4604045 665 }
mbed_official 49:77c8e4604045 666
mbed_official 49:77c8e4604045 667 /// Change priority of an active thread
mbed_official 49:77c8e4604045 668 osStatus osThreadSetPriority (osThreadId thread_id, osPriority priority) {
mbed_official 49:77c8e4604045 669 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 670 return __svcThreadSetPriority(thread_id, priority);
mbed_official 49:77c8e4604045 671 }
mbed_official 49:77c8e4604045 672
mbed_official 49:77c8e4604045 673 /// Get current priority of an active thread
mbed_official 49:77c8e4604045 674 osPriority osThreadGetPriority (osThreadId thread_id) {
mbed_official 49:77c8e4604045 675 if (__get_IPSR() != 0) return osPriorityError;// Not allowed in ISR
mbed_official 49:77c8e4604045 676 return __svcThreadGetPriority(thread_id);
mbed_official 49:77c8e4604045 677 }
mbed_official 49:77c8e4604045 678
mbed_official 49:77c8e4604045 679 /// INTERNAL - Not Public
mbed_official 49:77c8e4604045 680 /// Auto Terminate Thread on exit (used implicitly when thread exists)
mbed_official 49:77c8e4604045 681 __NO_RETURN void osThreadExit (void) {
mbed_official 49:77c8e4604045 682 __svcThreadTerminate(__svcThreadGetId());
mbed_official 49:77c8e4604045 683 for (;;); // Should never come here
mbed_official 49:77c8e4604045 684 }
mbed_official 49:77c8e4604045 685
mbed_official 49:77c8e4604045 686
mbed_official 49:77c8e4604045 687 // ==== Generic Wait Functions ====
mbed_official 49:77c8e4604045 688
mbed_official 49:77c8e4604045 689 // Generic Wait Service Calls declarations
mbed_official 49:77c8e4604045 690 SVC_1_1(svcDelay, osStatus, uint32_t, RET_osStatus)
mbed_official 49:77c8e4604045 691 #if osFeature_Wait != 0
mbed_official 49:77c8e4604045 692 SVC_1_3(svcWait, os_InRegs osEvent, uint32_t, RET_osEvent)
mbed_official 49:77c8e4604045 693 #endif
mbed_official 49:77c8e4604045 694
mbed_official 49:77c8e4604045 695 // Generic Wait Service Calls
mbed_official 49:77c8e4604045 696
mbed_official 49:77c8e4604045 697 /// Wait for Timeout (Time Delay)
mbed_official 49:77c8e4604045 698 osStatus svcDelay (uint32_t millisec) {
mbed_official 49:77c8e4604045 699 if (millisec == 0) return osOK;
mbed_official 49:77c8e4604045 700 rt_dly_wait(rt_ms2tick(millisec));
mbed_official 49:77c8e4604045 701 return osEventTimeout;
mbed_official 49:77c8e4604045 702 }
mbed_official 49:77c8e4604045 703
mbed_official 49:77c8e4604045 704 /// Wait for Signal, Message, Mail, or Timeout
mbed_official 49:77c8e4604045 705 #if osFeature_Wait != 0
mbed_official 49:77c8e4604045 706 os_InRegs osEvent_type svcWait (uint32_t millisec) {
mbed_official 49:77c8e4604045 707 osEvent ret;
mbed_official 49:77c8e4604045 708
mbed_official 49:77c8e4604045 709 if (millisec == 0) {
mbed_official 49:77c8e4604045 710 ret.status = osOK;
mbed_official 49:77c8e4604045 711 return osEvent_ret_status;
mbed_official 49:77c8e4604045 712 }
mbed_official 49:77c8e4604045 713
mbed_official 49:77c8e4604045 714 /* To Do: osEventSignal, osEventMessage, osEventMail */
mbed_official 49:77c8e4604045 715 rt_dly_wait(rt_ms2tick(millisec));
mbed_official 49:77c8e4604045 716 ret.status = osEventTimeout;
mbed_official 49:77c8e4604045 717
mbed_official 49:77c8e4604045 718 return osEvent_ret_status;
mbed_official 49:77c8e4604045 719 }
mbed_official 49:77c8e4604045 720 #endif
mbed_official 49:77c8e4604045 721
mbed_official 49:77c8e4604045 722
mbed_official 49:77c8e4604045 723 // Generic Wait API
mbed_official 49:77c8e4604045 724
mbed_official 49:77c8e4604045 725 /// Wait for Timeout (Time Delay)
mbed_official 49:77c8e4604045 726 osStatus osDelay (uint32_t millisec) {
mbed_official 49:77c8e4604045 727 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 728 return __svcDelay(millisec);
mbed_official 49:77c8e4604045 729 }
mbed_official 49:77c8e4604045 730
mbed_official 49:77c8e4604045 731 /// Wait for Signal, Message, Mail, or Timeout
mbed_official 49:77c8e4604045 732 os_InRegs osEvent osWait (uint32_t millisec) {
mbed_official 49:77c8e4604045 733 osEvent ret;
mbed_official 49:77c8e4604045 734
mbed_official 49:77c8e4604045 735 #if osFeature_Wait == 0
mbed_official 49:77c8e4604045 736 ret.status = osErrorOS;
mbed_official 49:77c8e4604045 737 return ret;
mbed_official 49:77c8e4604045 738 #else
mbed_official 49:77c8e4604045 739 if (__get_IPSR() != 0) { // Not allowed in ISR
mbed_official 49:77c8e4604045 740 ret.status = osErrorISR;
mbed_official 49:77c8e4604045 741 return ret;
mbed_official 49:77c8e4604045 742 }
mbed_official 49:77c8e4604045 743 return __svcWait(millisec);
mbed_official 49:77c8e4604045 744 #endif
mbed_official 49:77c8e4604045 745 }
mbed_official 49:77c8e4604045 746
mbed_official 49:77c8e4604045 747
mbed_official 49:77c8e4604045 748 // ==== Timer Management ====
mbed_official 49:77c8e4604045 749
mbed_official 49:77c8e4604045 750 // Timer definitions
mbed_official 49:77c8e4604045 751 #define osTimerInvalid 0
mbed_official 49:77c8e4604045 752 #define osTimerStopped 1
mbed_official 49:77c8e4604045 753 #define osTimerRunning 2
mbed_official 49:77c8e4604045 754
mbed_official 49:77c8e4604045 755 // Timer structures
mbed_official 49:77c8e4604045 756
mbed_official 49:77c8e4604045 757 typedef struct os_timer_cb_ { // Timer Control Block
mbed_official 49:77c8e4604045 758 struct os_timer_cb_ *next; // Pointer to next active Timer
mbed_official 49:77c8e4604045 759 uint8_t state; // Timer State
mbed_official 49:77c8e4604045 760 uint8_t type; // Timer Type (Periodic/One-shot)
mbed_official 49:77c8e4604045 761 uint16_t reserved; // Reserved
mbed_official 49:77c8e4604045 762 uint16_t tcnt; // Timer Delay Count
mbed_official 49:77c8e4604045 763 uint16_t icnt; // Timer Initial Count
mbed_official 49:77c8e4604045 764 void *arg; // Timer Function Argument
mbed_official 49:77c8e4604045 765 osTimerDef_t *timer; // Pointer to Timer definition
mbed_official 49:77c8e4604045 766 } os_timer_cb;
mbed_official 49:77c8e4604045 767
mbed_official 49:77c8e4604045 768 // Timer variables
mbed_official 49:77c8e4604045 769 os_timer_cb *os_timer_head; // Pointer to first active Timer
mbed_official 49:77c8e4604045 770
mbed_official 49:77c8e4604045 771
mbed_official 49:77c8e4604045 772 // Timer Helper Functions
mbed_official 49:77c8e4604045 773
mbed_official 49:77c8e4604045 774 // Insert Timer into the list sorted by time
mbed_official 49:77c8e4604045 775 static void rt_timer_insert (os_timer_cb *pt, uint32_t tcnt) {
mbed_official 49:77c8e4604045 776 os_timer_cb *p, *prev;
mbed_official 49:77c8e4604045 777
mbed_official 49:77c8e4604045 778 prev = NULL;
mbed_official 49:77c8e4604045 779 p = os_timer_head;
mbed_official 49:77c8e4604045 780 while (p != NULL) {
mbed_official 49:77c8e4604045 781 if (tcnt < p->tcnt) break;
mbed_official 49:77c8e4604045 782 tcnt -= p->tcnt;
mbed_official 49:77c8e4604045 783 prev = p;
mbed_official 49:77c8e4604045 784 p = p->next;
mbed_official 49:77c8e4604045 785 }
mbed_official 49:77c8e4604045 786 pt->next = p;
mbed_official 49:77c8e4604045 787 pt->tcnt = (uint16_t)tcnt;
mbed_official 49:77c8e4604045 788 if (p != NULL) {
mbed_official 49:77c8e4604045 789 p->tcnt -= pt->tcnt;
mbed_official 49:77c8e4604045 790 }
mbed_official 49:77c8e4604045 791 if (prev != NULL) {
mbed_official 49:77c8e4604045 792 prev->next = pt;
mbed_official 49:77c8e4604045 793 } else {
mbed_official 49:77c8e4604045 794 os_timer_head = pt;
mbed_official 49:77c8e4604045 795 }
mbed_official 49:77c8e4604045 796 }
mbed_official 49:77c8e4604045 797
mbed_official 49:77c8e4604045 798 // Remove Timer from the list
mbed_official 49:77c8e4604045 799 static int rt_timer_remove (os_timer_cb *pt) {
mbed_official 49:77c8e4604045 800 os_timer_cb *p, *prev;
mbed_official 49:77c8e4604045 801
mbed_official 49:77c8e4604045 802 prev = NULL;
mbed_official 49:77c8e4604045 803 p = os_timer_head;
mbed_official 49:77c8e4604045 804 while (p != NULL) {
mbed_official 49:77c8e4604045 805 if (p == pt) break;
mbed_official 49:77c8e4604045 806 prev = p;
mbed_official 49:77c8e4604045 807 p = p->next;
mbed_official 49:77c8e4604045 808 }
mbed_official 49:77c8e4604045 809 if (p == NULL) return -1;
mbed_official 49:77c8e4604045 810 if (prev != NULL) {
mbed_official 49:77c8e4604045 811 prev->next = pt->next;
mbed_official 49:77c8e4604045 812 } else {
mbed_official 49:77c8e4604045 813 os_timer_head = pt->next;
mbed_official 49:77c8e4604045 814 }
mbed_official 49:77c8e4604045 815 if (pt->next != NULL) {
mbed_official 49:77c8e4604045 816 pt->next->tcnt += pt->tcnt;
mbed_official 49:77c8e4604045 817 }
mbed_official 49:77c8e4604045 818
mbed_official 49:77c8e4604045 819 return 0;
mbed_official 49:77c8e4604045 820 }
mbed_official 49:77c8e4604045 821
mbed_official 49:77c8e4604045 822
mbed_official 49:77c8e4604045 823 // Timer Service Calls declarations
mbed_official 49:77c8e4604045 824 SVC_3_1(svcTimerCreate, osTimerId, osTimerDef_t *, os_timer_type, void *, RET_pointer)
mbed_official 49:77c8e4604045 825 SVC_2_1(svcTimerStart, osStatus, osTimerId, uint32_t, RET_osStatus)
mbed_official 49:77c8e4604045 826 SVC_1_1(svcTimerStop, osStatus, osTimerId, RET_osStatus)
mbed_official 49:77c8e4604045 827 SVC_1_1(svcTimerDelete, osStatus, osTimerId, RET_osStatus)
mbed_official 49:77c8e4604045 828 SVC_1_2(svcTimerCall, os_InRegs osCallback, osTimerId, RET_osCallback)
mbed_official 49:77c8e4604045 829
mbed_official 49:77c8e4604045 830 // Timer Management Service Calls
mbed_official 49:77c8e4604045 831
mbed_official 49:77c8e4604045 832 /// Create timer
mbed_official 49:77c8e4604045 833 osTimerId svcTimerCreate (osTimerDef_t *timer_def, os_timer_type type, void *argument) {
mbed_official 49:77c8e4604045 834 os_timer_cb *pt;
mbed_official 49:77c8e4604045 835
mbed_official 49:77c8e4604045 836 if ((timer_def == NULL) || (timer_def->ptimer == NULL)) {
mbed_official 49:77c8e4604045 837 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 838 return NULL;
mbed_official 49:77c8e4604045 839 }
mbed_official 49:77c8e4604045 840
mbed_official 49:77c8e4604045 841 pt = timer_def->timer;
mbed_official 49:77c8e4604045 842 if (pt == NULL) {
mbed_official 49:77c8e4604045 843 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 844 return NULL;
mbed_official 49:77c8e4604045 845 }
mbed_official 49:77c8e4604045 846
mbed_official 49:77c8e4604045 847 if ((type != osTimerOnce) && (type != osTimerPeriodic)) {
mbed_official 49:77c8e4604045 848 sysThreadError(osErrorValue);
mbed_official 49:77c8e4604045 849 return NULL;
mbed_official 49:77c8e4604045 850 }
mbed_official 49:77c8e4604045 851
mbed_official 49:77c8e4604045 852 if (osThreadId_osTimerThread == NULL) {
mbed_official 49:77c8e4604045 853 sysThreadError(osErrorResource);
mbed_official 49:77c8e4604045 854 return NULL;
mbed_official 49:77c8e4604045 855 }
mbed_official 49:77c8e4604045 856
mbed_official 49:77c8e4604045 857 if (pt->state != osTimerInvalid){
mbed_official 49:77c8e4604045 858 sysThreadError(osErrorResource);
mbed_official 49:77c8e4604045 859 return NULL;
mbed_official 49:77c8e4604045 860 }
mbed_official 49:77c8e4604045 861
mbed_official 49:77c8e4604045 862 pt->state = osTimerStopped;
mbed_official 49:77c8e4604045 863 pt->type = (uint8_t)type;
mbed_official 49:77c8e4604045 864 pt->arg = argument;
mbed_official 49:77c8e4604045 865 pt->timer = timer_def;
mbed_official 49:77c8e4604045 866
mbed_official 49:77c8e4604045 867 return (osTimerId)pt;
mbed_official 49:77c8e4604045 868 }
mbed_official 49:77c8e4604045 869
mbed_official 49:77c8e4604045 870 /// Start or restart timer
mbed_official 49:77c8e4604045 871 osStatus svcTimerStart (osTimerId timer_id, uint32_t millisec) {
mbed_official 49:77c8e4604045 872 os_timer_cb *pt;
mbed_official 49:77c8e4604045 873 uint32_t tcnt;
mbed_official 49:77c8e4604045 874
mbed_official 49:77c8e4604045 875 pt = rt_id2obj(timer_id);
mbed_official 49:77c8e4604045 876 if (pt == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 877
mbed_official 49:77c8e4604045 878 tcnt = rt_ms2tick(millisec);
mbed_official 49:77c8e4604045 879 if (tcnt == 0) return osErrorValue;
mbed_official 49:77c8e4604045 880
mbed_official 49:77c8e4604045 881 switch (pt->state) {
mbed_official 49:77c8e4604045 882 case osTimerRunning:
mbed_official 49:77c8e4604045 883 if (rt_timer_remove(pt) != 0) {
mbed_official 49:77c8e4604045 884 return osErrorResource;
mbed_official 49:77c8e4604045 885 }
mbed_official 49:77c8e4604045 886 break;
mbed_official 49:77c8e4604045 887 case osTimerStopped:
mbed_official 49:77c8e4604045 888 pt->state = osTimerRunning;
mbed_official 49:77c8e4604045 889 pt->icnt = (uint16_t)tcnt;
mbed_official 49:77c8e4604045 890 break;
mbed_official 49:77c8e4604045 891 default:
mbed_official 49:77c8e4604045 892 return osErrorResource;
mbed_official 49:77c8e4604045 893 }
mbed_official 49:77c8e4604045 894
mbed_official 49:77c8e4604045 895 rt_timer_insert(pt, tcnt);
mbed_official 49:77c8e4604045 896
mbed_official 49:77c8e4604045 897 return osOK;
mbed_official 49:77c8e4604045 898 }
mbed_official 49:77c8e4604045 899
mbed_official 49:77c8e4604045 900 /// Stop timer
mbed_official 49:77c8e4604045 901 osStatus svcTimerStop (osTimerId timer_id) {
mbed_official 49:77c8e4604045 902 os_timer_cb *pt;
mbed_official 49:77c8e4604045 903
mbed_official 49:77c8e4604045 904 pt = rt_id2obj(timer_id);
mbed_official 49:77c8e4604045 905 if (pt == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 906
mbed_official 49:77c8e4604045 907 if (pt->state != osTimerRunning) return osErrorResource;
mbed_official 49:77c8e4604045 908
mbed_official 49:77c8e4604045 909 pt->state = osTimerStopped;
mbed_official 49:77c8e4604045 910
mbed_official 49:77c8e4604045 911 if (rt_timer_remove(pt) != 0) {
mbed_official 49:77c8e4604045 912 return osErrorResource;
mbed_official 49:77c8e4604045 913 }
mbed_official 49:77c8e4604045 914
mbed_official 49:77c8e4604045 915 return osOK;
mbed_official 49:77c8e4604045 916 }
mbed_official 49:77c8e4604045 917
mbed_official 49:77c8e4604045 918 /// Delete timer
mbed_official 49:77c8e4604045 919 osStatus svcTimerDelete (osTimerId timer_id) {
mbed_official 49:77c8e4604045 920 os_timer_cb *pt;
mbed_official 49:77c8e4604045 921
mbed_official 49:77c8e4604045 922 pt = rt_id2obj(timer_id);
mbed_official 49:77c8e4604045 923 if (pt == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 924
mbed_official 49:77c8e4604045 925 switch (pt->state) {
mbed_official 49:77c8e4604045 926 case osTimerRunning:
mbed_official 49:77c8e4604045 927 rt_timer_remove(pt);
mbed_official 49:77c8e4604045 928 break;
mbed_official 49:77c8e4604045 929 case osTimerStopped:
mbed_official 49:77c8e4604045 930 break;
mbed_official 49:77c8e4604045 931 default:
mbed_official 49:77c8e4604045 932 return osErrorResource;
mbed_official 49:77c8e4604045 933 }
mbed_official 49:77c8e4604045 934
mbed_official 49:77c8e4604045 935 pt->state = osTimerInvalid;
mbed_official 49:77c8e4604045 936
mbed_official 49:77c8e4604045 937 return osOK;
mbed_official 49:77c8e4604045 938 }
mbed_official 49:77c8e4604045 939
mbed_official 49:77c8e4604045 940 /// Get timer callback parameters
mbed_official 49:77c8e4604045 941 os_InRegs osCallback_type svcTimerCall (osTimerId timer_id) {
mbed_official 49:77c8e4604045 942 os_timer_cb *pt;
mbed_official 49:77c8e4604045 943 osCallback ret;
mbed_official 49:77c8e4604045 944
mbed_official 49:77c8e4604045 945 pt = rt_id2obj(timer_id);
mbed_official 49:77c8e4604045 946 if (pt == NULL) {
mbed_official 49:77c8e4604045 947 ret.fp = NULL;
mbed_official 49:77c8e4604045 948 ret.arg = NULL;
mbed_official 49:77c8e4604045 949 return osCallback_ret;
mbed_official 49:77c8e4604045 950 }
mbed_official 49:77c8e4604045 951
mbed_official 49:77c8e4604045 952 ret.fp = (void *)pt->timer->ptimer;
mbed_official 49:77c8e4604045 953 ret.arg = pt->arg;
mbed_official 49:77c8e4604045 954
mbed_official 49:77c8e4604045 955 return osCallback_ret;
mbed_official 49:77c8e4604045 956 }
mbed_official 49:77c8e4604045 957
mbed_official 49:77c8e4604045 958 static __INLINE osStatus isrMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec);
mbed_official 49:77c8e4604045 959
mbed_official 49:77c8e4604045 960 /// Timer Tick (called each SysTick)
mbed_official 49:77c8e4604045 961 void sysTimerTick (void) {
mbed_official 49:77c8e4604045 962 os_timer_cb *pt, *p;
mbed_official 49:77c8e4604045 963
mbed_official 49:77c8e4604045 964 p = os_timer_head;
mbed_official 49:77c8e4604045 965 if (p == NULL) return;
mbed_official 49:77c8e4604045 966
mbed_official 49:77c8e4604045 967 p->tcnt--;
mbed_official 49:77c8e4604045 968 while ((p != NULL) && (p->tcnt == 0)) {
mbed_official 49:77c8e4604045 969 pt = p;
mbed_official 49:77c8e4604045 970 p = p->next;
mbed_official 49:77c8e4604045 971 os_timer_head = p;
mbed_official 49:77c8e4604045 972 isrMessagePut(osMessageQId_osTimerMessageQ, (uint32_t)pt, 0);
mbed_official 49:77c8e4604045 973 if (pt->type == osTimerPeriodic) {
mbed_official 49:77c8e4604045 974 rt_timer_insert(pt, pt->icnt);
mbed_official 49:77c8e4604045 975 } else {
mbed_official 49:77c8e4604045 976 pt->state = osTimerStopped;
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
mbed_official 49:77c8e4604045 982 // Timer Management Public API
mbed_official 49:77c8e4604045 983
mbed_official 49:77c8e4604045 984 /// Create timer
mbed_official 49:77c8e4604045 985 osTimerId osTimerCreate (osTimerDef_t *timer_def, os_timer_type type, void *argument) {
mbed_official 49:77c8e4604045 986 if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
mbed_official 49:77c8e4604045 987 if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
mbed_official 49:77c8e4604045 988 // Privileged and not running
mbed_official 49:77c8e4604045 989 return svcTimerCreate(timer_def, type, argument);
mbed_official 49:77c8e4604045 990 } else {
mbed_official 49:77c8e4604045 991 return __svcTimerCreate(timer_def, type, argument);
mbed_official 49:77c8e4604045 992 }
mbed_official 49:77c8e4604045 993 }
mbed_official 49:77c8e4604045 994
mbed_official 49:77c8e4604045 995 /// Start or restart timer
mbed_official 49:77c8e4604045 996 osStatus osTimerStart (osTimerId timer_id, uint32_t millisec) {
mbed_official 49:77c8e4604045 997 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 998 return __svcTimerStart(timer_id, millisec);
mbed_official 49:77c8e4604045 999 }
mbed_official 49:77c8e4604045 1000
mbed_official 49:77c8e4604045 1001 /// Stop timer
mbed_official 49:77c8e4604045 1002 osStatus osTimerStop (osTimerId timer_id) {
mbed_official 49:77c8e4604045 1003 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 1004 return __svcTimerStop(timer_id);
mbed_official 49:77c8e4604045 1005 }
mbed_official 49:77c8e4604045 1006
mbed_official 49:77c8e4604045 1007 /// Delete timer
mbed_official 49:77c8e4604045 1008 osStatus osTimerDelete (osTimerId timer_id) {
mbed_official 49:77c8e4604045 1009 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 1010 return __svcTimerDelete(timer_id);
mbed_official 49:77c8e4604045 1011 }
mbed_official 49:77c8e4604045 1012
mbed_official 49:77c8e4604045 1013 /// INTERNAL - Not Public
mbed_official 49:77c8e4604045 1014 /// Get timer callback parameters (used by OS Timer Thread)
mbed_official 49:77c8e4604045 1015 os_InRegs osCallback osTimerCall (osTimerId timer_id) {
mbed_official 49:77c8e4604045 1016 return __svcTimerCall(timer_id);
mbed_official 49:77c8e4604045 1017 }
mbed_official 49:77c8e4604045 1018
mbed_official 49:77c8e4604045 1019
mbed_official 49:77c8e4604045 1020 // Timer Thread
mbed_official 49:77c8e4604045 1021 __NO_RETURN void osTimerThread (void const *argument) {
mbed_official 49:77c8e4604045 1022 osCallback cb;
mbed_official 49:77c8e4604045 1023 osEvent evt;
mbed_official 49:77c8e4604045 1024
mbed_official 49:77c8e4604045 1025 for (;;) {
mbed_official 49:77c8e4604045 1026 evt = osMessageGet(osMessageQId_osTimerMessageQ, osWaitForever);
mbed_official 49:77c8e4604045 1027 if (evt.status == osEventMessage) {
mbed_official 49:77c8e4604045 1028 cb = osTimerCall(evt.value.p);
mbed_official 49:77c8e4604045 1029 if (cb.fp != NULL) {
mbed_official 49:77c8e4604045 1030 (*(os_ptimer)cb.fp)(cb.arg);
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
mbed_official 49:77c8e4604045 1037 // ==== Signal Management ====
mbed_official 49:77c8e4604045 1038
mbed_official 49:77c8e4604045 1039 // Signal Service Calls declarations
mbed_official 49:77c8e4604045 1040 SVC_2_1(svcSignalSet, int32_t, osThreadId, int32_t, RET_int32_t)
mbed_official 49:77c8e4604045 1041 SVC_2_1(svcSignalClear, int32_t, osThreadId, int32_t, RET_int32_t)
mbed_official 49:77c8e4604045 1042 SVC_1_1(svcSignalGet, int32_t, osThreadId, RET_int32_t)
mbed_official 49:77c8e4604045 1043 SVC_2_3(svcSignalWait, os_InRegs osEvent, int32_t, uint32_t, RET_osEvent)
mbed_official 49:77c8e4604045 1044
mbed_official 49:77c8e4604045 1045 // Signal Service Calls
mbed_official 49:77c8e4604045 1046
mbed_official 49:77c8e4604045 1047 /// Set the specified Signal Flags of an active thread
mbed_official 49:77c8e4604045 1048 int32_t svcSignalSet (osThreadId thread_id, int32_t signals) {
mbed_official 49:77c8e4604045 1049 P_TCB ptcb;
mbed_official 49:77c8e4604045 1050 int32_t sig;
mbed_official 49:77c8e4604045 1051
mbed_official 49:77c8e4604045 1052 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
mbed_official 49:77c8e4604045 1053 if (ptcb == NULL) return 0x80000000;
mbed_official 49:77c8e4604045 1054
mbed_official 49:77c8e4604045 1055 if (signals & (0xFFFFFFFF << osFeature_Signals)) return 0x80000000;
mbed_official 49:77c8e4604045 1056
mbed_official 49:77c8e4604045 1057 sig = ptcb->events; // Previous signal flags
mbed_official 49:77c8e4604045 1058
mbed_official 49:77c8e4604045 1059 rt_evt_set(signals, ptcb->task_id); // Set event flags
mbed_official 49:77c8e4604045 1060
mbed_official 49:77c8e4604045 1061 return sig;
mbed_official 49:77c8e4604045 1062 }
mbed_official 49:77c8e4604045 1063
mbed_official 49:77c8e4604045 1064 /// Clear the specified Signal Flags of an active thread
mbed_official 49:77c8e4604045 1065 int32_t svcSignalClear (osThreadId thread_id, int32_t signals) {
mbed_official 49:77c8e4604045 1066 P_TCB ptcb;
mbed_official 49:77c8e4604045 1067 int32_t sig;
mbed_official 49:77c8e4604045 1068
mbed_official 49:77c8e4604045 1069 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
mbed_official 49:77c8e4604045 1070 if (ptcb == NULL) return 0x80000000;
mbed_official 49:77c8e4604045 1071
mbed_official 49:77c8e4604045 1072 if (signals & (0xFFFFFFFF << osFeature_Signals)) return 0x80000000;
mbed_official 49:77c8e4604045 1073
mbed_official 49:77c8e4604045 1074 sig = ptcb->events; // Previous signal flags
mbed_official 49:77c8e4604045 1075
mbed_official 49:77c8e4604045 1076 rt_evt_clr(signals, ptcb->task_id); // Clear event flags
mbed_official 49:77c8e4604045 1077
mbed_official 49:77c8e4604045 1078 return sig;
mbed_official 49:77c8e4604045 1079 }
mbed_official 49:77c8e4604045 1080
mbed_official 49:77c8e4604045 1081 /// Get Signal Flags status of an active thread
mbed_official 49:77c8e4604045 1082 int32_t svcSignalGet (osThreadId thread_id) {
mbed_official 49:77c8e4604045 1083 P_TCB ptcb;
mbed_official 49:77c8e4604045 1084
mbed_official 49:77c8e4604045 1085 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
mbed_official 49:77c8e4604045 1086 if (ptcb == NULL) return 0x80000000;
mbed_official 49:77c8e4604045 1087
mbed_official 49:77c8e4604045 1088 return ptcb->events; // Return event flags
mbed_official 49:77c8e4604045 1089 }
mbed_official 49:77c8e4604045 1090
mbed_official 49:77c8e4604045 1091 /// Wait for one or more Signal Flags to become signaled for the current RUNNING thread
mbed_official 49:77c8e4604045 1092 os_InRegs osEvent_type svcSignalWait (int32_t signals, uint32_t millisec) {
mbed_official 49:77c8e4604045 1093 OS_RESULT res;
mbed_official 49:77c8e4604045 1094 osEvent ret;
mbed_official 49:77c8e4604045 1095
mbed_official 49:77c8e4604045 1096 if (signals & (0xFFFFFFFF << osFeature_Signals)) {
mbed_official 49:77c8e4604045 1097 ret.status = osErrorValue;
mbed_official 49:77c8e4604045 1098 return osEvent_ret_status;
mbed_official 49:77c8e4604045 1099 }
mbed_official 49:77c8e4604045 1100
mbed_official 49:77c8e4604045 1101 if (signals != 0) { // Wait for all specified signals
mbed_official 49:77c8e4604045 1102 res = rt_evt_wait(signals, rt_ms2tick(millisec), __TRUE);
mbed_official 49:77c8e4604045 1103 } else { // Wait for any signal
mbed_official 49:77c8e4604045 1104 res = rt_evt_wait(0xFFFF, rt_ms2tick(millisec), __FALSE);
mbed_official 49:77c8e4604045 1105 }
mbed_official 49:77c8e4604045 1106
mbed_official 49:77c8e4604045 1107 if (res == OS_R_EVT) {
mbed_official 49:77c8e4604045 1108 ret.status = osEventSignal;
mbed_official 49:77c8e4604045 1109 ret.value.signals = signals ? signals : os_tsk.run->waits;
mbed_official 49:77c8e4604045 1110 } else {
mbed_official 49:77c8e4604045 1111 ret.status = millisec ? osEventTimeout : osOK;
mbed_official 49:77c8e4604045 1112 ret.value.signals = 0;
mbed_official 49:77c8e4604045 1113 }
mbed_official 49:77c8e4604045 1114
mbed_official 49:77c8e4604045 1115 return osEvent_ret_value;
mbed_official 49:77c8e4604045 1116 }
mbed_official 49:77c8e4604045 1117
mbed_official 49:77c8e4604045 1118
mbed_official 49:77c8e4604045 1119 // Signal ISR Calls
mbed_official 49:77c8e4604045 1120
mbed_official 49:77c8e4604045 1121 /// Set the specified Signal Flags of an active thread
mbed_official 49:77c8e4604045 1122 static __INLINE int32_t isrSignalSet (osThreadId thread_id, int32_t signals) {
mbed_official 49:77c8e4604045 1123 P_TCB ptcb;
mbed_official 49:77c8e4604045 1124 int32_t sig;
mbed_official 49:77c8e4604045 1125
mbed_official 49:77c8e4604045 1126 ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
mbed_official 49:77c8e4604045 1127 if (ptcb == NULL) return 0x80000000;
mbed_official 49:77c8e4604045 1128
mbed_official 49:77c8e4604045 1129 if (signals & (0xFFFFFFFF << osFeature_Signals)) return 0x80000000;
mbed_official 49:77c8e4604045 1130
mbed_official 49:77c8e4604045 1131 sig = ptcb->events; // Previous signal flags
mbed_official 49:77c8e4604045 1132
mbed_official 49:77c8e4604045 1133 isr_evt_set(signals, ptcb->task_id); // Set event flags
mbed_official 49:77c8e4604045 1134
mbed_official 49:77c8e4604045 1135 return sig;
mbed_official 49:77c8e4604045 1136 }
mbed_official 49:77c8e4604045 1137
mbed_official 49:77c8e4604045 1138
mbed_official 49:77c8e4604045 1139 // Signal Public API
mbed_official 49:77c8e4604045 1140
mbed_official 49:77c8e4604045 1141 /// Set the specified Signal Flags of an active thread
mbed_official 49:77c8e4604045 1142 int32_t osSignalSet (osThreadId thread_id, int32_t signals) {
mbed_official 49:77c8e4604045 1143 if (__get_IPSR() != 0) { // in ISR
mbed_official 49:77c8e4604045 1144 return isrSignalSet(thread_id, signals);
mbed_official 49:77c8e4604045 1145 } else { // in Thread
mbed_official 49:77c8e4604045 1146 return __svcSignalSet(thread_id, signals);
mbed_official 49:77c8e4604045 1147 }
mbed_official 49:77c8e4604045 1148 }
mbed_official 49:77c8e4604045 1149
mbed_official 49:77c8e4604045 1150 /// Clear the specified Signal Flags of an active thread
mbed_official 49:77c8e4604045 1151 int32_t osSignalClear (osThreadId thread_id, int32_t signals) {
mbed_official 49:77c8e4604045 1152 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 1153 return __svcSignalClear(thread_id, signals);
mbed_official 49:77c8e4604045 1154 }
mbed_official 49:77c8e4604045 1155
mbed_official 49:77c8e4604045 1156 /// Get Signal Flags status of an active thread
mbed_official 49:77c8e4604045 1157 int32_t osSignalGet (osThreadId thread_id) {
mbed_official 49:77c8e4604045 1158 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 1159 return __svcSignalGet(thread_id);
mbed_official 49:77c8e4604045 1160 }
mbed_official 49:77c8e4604045 1161
mbed_official 49:77c8e4604045 1162 /// Wait for one or more Signal Flags to become signaled for the current RUNNING thread
mbed_official 49:77c8e4604045 1163 os_InRegs osEvent osSignalWait (int32_t signals, uint32_t millisec) {
mbed_official 49:77c8e4604045 1164 osEvent ret;
mbed_official 49:77c8e4604045 1165
mbed_official 49:77c8e4604045 1166 if (__get_IPSR() != 0) { // Not allowed in ISR
mbed_official 49:77c8e4604045 1167 ret.status = osErrorISR;
mbed_official 49:77c8e4604045 1168 return ret;
mbed_official 49:77c8e4604045 1169 }
mbed_official 49:77c8e4604045 1170 return __svcSignalWait(signals, millisec);
mbed_official 49:77c8e4604045 1171 }
mbed_official 49:77c8e4604045 1172
mbed_official 49:77c8e4604045 1173
mbed_official 49:77c8e4604045 1174 // ==== Mutex Management ====
mbed_official 49:77c8e4604045 1175
mbed_official 49:77c8e4604045 1176 // Mutex Service Calls declarations
mbed_official 49:77c8e4604045 1177 SVC_1_1(svcMutexCreate, osMutexId, osMutexDef_t *, RET_pointer)
mbed_official 49:77c8e4604045 1178 SVC_2_1(svcMutexWait, osStatus, osMutexId, uint32_t, RET_osStatus)
mbed_official 49:77c8e4604045 1179 SVC_1_1(svcMutexRelease, osStatus, osMutexId, RET_osStatus)
mbed_official 49:77c8e4604045 1180 SVC_1_1(svcMutexDelete, osStatus, osMutexId, RET_osStatus)
mbed_official 49:77c8e4604045 1181
mbed_official 49:77c8e4604045 1182 // Mutex Service Calls
mbed_official 49:77c8e4604045 1183
mbed_official 49:77c8e4604045 1184 /// Create and Initialize a Mutex object
mbed_official 49:77c8e4604045 1185 osMutexId svcMutexCreate (osMutexDef_t *mutex_def) {
mbed_official 49:77c8e4604045 1186 OS_ID mut;
mbed_official 49:77c8e4604045 1187
mbed_official 49:77c8e4604045 1188 if (mutex_def == NULL) {
mbed_official 49:77c8e4604045 1189 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 1190 return NULL;
mbed_official 49:77c8e4604045 1191 }
mbed_official 49:77c8e4604045 1192
mbed_official 49:77c8e4604045 1193 mut = mutex_def->mutex;
mbed_official 49:77c8e4604045 1194 if (mut == NULL) {
mbed_official 49:77c8e4604045 1195 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 1196 return NULL;
mbed_official 49:77c8e4604045 1197 }
mbed_official 49:77c8e4604045 1198
mbed_official 49:77c8e4604045 1199 if (((P_MUCB)mut)->cb_type != 0) {
mbed_official 49:77c8e4604045 1200 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 1201 return NULL;
mbed_official 49:77c8e4604045 1202 }
mbed_official 49:77c8e4604045 1203
mbed_official 49:77c8e4604045 1204 rt_mut_init(mut); // Initialize Mutex
mbed_official 49:77c8e4604045 1205
mbed_official 49:77c8e4604045 1206 return mut;
mbed_official 49:77c8e4604045 1207 }
mbed_official 49:77c8e4604045 1208
mbed_official 49:77c8e4604045 1209 /// Wait until a Mutex becomes available
mbed_official 49:77c8e4604045 1210 osStatus svcMutexWait (osMutexId mutex_id, uint32_t millisec) {
mbed_official 49:77c8e4604045 1211 OS_ID mut;
mbed_official 49:77c8e4604045 1212 OS_RESULT res;
mbed_official 49:77c8e4604045 1213
mbed_official 49:77c8e4604045 1214 mut = rt_id2obj(mutex_id);
mbed_official 49:77c8e4604045 1215 if (mut == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 1216
mbed_official 49:77c8e4604045 1217 if (((P_MUCB)mut)->cb_type != MUCB) return osErrorParameter;
mbed_official 49:77c8e4604045 1218
mbed_official 49:77c8e4604045 1219 res = rt_mut_wait(mut, rt_ms2tick(millisec)); // Wait for Mutex
mbed_official 49:77c8e4604045 1220
mbed_official 49:77c8e4604045 1221 if (res == OS_R_TMO) {
mbed_official 49:77c8e4604045 1222 return (millisec ? osErrorTimeoutResource : osErrorResource);
mbed_official 49:77c8e4604045 1223 }
mbed_official 49:77c8e4604045 1224
mbed_official 49:77c8e4604045 1225 return osOK;
mbed_official 49:77c8e4604045 1226 }
mbed_official 49:77c8e4604045 1227
mbed_official 49:77c8e4604045 1228 /// Release a Mutex that was obtained with osMutexWait
mbed_official 49:77c8e4604045 1229 osStatus svcMutexRelease (osMutexId mutex_id) {
mbed_official 49:77c8e4604045 1230 OS_ID mut;
mbed_official 49:77c8e4604045 1231 OS_RESULT res;
mbed_official 49:77c8e4604045 1232
mbed_official 49:77c8e4604045 1233 mut = rt_id2obj(mutex_id);
mbed_official 49:77c8e4604045 1234 if (mut == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 1235
mbed_official 49:77c8e4604045 1236 if (((P_MUCB)mut)->cb_type != MUCB) return osErrorParameter;
mbed_official 49:77c8e4604045 1237
mbed_official 49:77c8e4604045 1238 res = rt_mut_release(mut); // Release Mutex
mbed_official 49:77c8e4604045 1239
mbed_official 49:77c8e4604045 1240 if (res == OS_R_NOK) return osErrorResource; // Thread not owner or Zero Counter
mbed_official 49:77c8e4604045 1241
mbed_official 49:77c8e4604045 1242 return osOK;
mbed_official 49:77c8e4604045 1243 }
mbed_official 49:77c8e4604045 1244
mbed_official 49:77c8e4604045 1245 /// Delete a Mutex that was created by osMutexCreate
mbed_official 49:77c8e4604045 1246 osStatus svcMutexDelete (osMutexId mutex_id) {
mbed_official 49:77c8e4604045 1247 OS_ID mut;
mbed_official 49:77c8e4604045 1248
mbed_official 49:77c8e4604045 1249 mut = rt_id2obj(mutex_id);
mbed_official 49:77c8e4604045 1250 if (mut == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 1251
mbed_official 49:77c8e4604045 1252 if (((P_MUCB)mut)->cb_type != MUCB) return osErrorParameter;
mbed_official 49:77c8e4604045 1253
mbed_official 49:77c8e4604045 1254 rt_mut_delete(mut); // Release Mutex
mbed_official 49:77c8e4604045 1255
mbed_official 49:77c8e4604045 1256 return osOK;
mbed_official 49:77c8e4604045 1257 }
mbed_official 49:77c8e4604045 1258
mbed_official 49:77c8e4604045 1259
mbed_official 49:77c8e4604045 1260 // Mutex Public API
mbed_official 49:77c8e4604045 1261
mbed_official 49:77c8e4604045 1262 /// Create and Initialize a Mutex object
mbed_official 49:77c8e4604045 1263 osMutexId osMutexCreate (osMutexDef_t *mutex_def) {
mbed_official 49:77c8e4604045 1264 if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
mbed_official 49:77c8e4604045 1265 if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
mbed_official 49:77c8e4604045 1266 // Privileged and not running
mbed_official 49:77c8e4604045 1267 return svcMutexCreate(mutex_def);
mbed_official 49:77c8e4604045 1268 } else {
mbed_official 49:77c8e4604045 1269 return __svcMutexCreate(mutex_def);
mbed_official 49:77c8e4604045 1270 }
mbed_official 49:77c8e4604045 1271 }
mbed_official 49:77c8e4604045 1272
mbed_official 49:77c8e4604045 1273 /// Wait until a Mutex becomes available
mbed_official 49:77c8e4604045 1274 osStatus osMutexWait (osMutexId mutex_id, uint32_t millisec) {
mbed_official 49:77c8e4604045 1275 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 1276 return __svcMutexWait(mutex_id, millisec);
mbed_official 49:77c8e4604045 1277 }
mbed_official 49:77c8e4604045 1278
mbed_official 49:77c8e4604045 1279 /// Release a Mutex that was obtained with osMutexWait
mbed_official 49:77c8e4604045 1280 osStatus osMutexRelease (osMutexId mutex_id) {
mbed_official 49:77c8e4604045 1281 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 1282 return __svcMutexRelease(mutex_id);
mbed_official 49:77c8e4604045 1283 }
mbed_official 49:77c8e4604045 1284
mbed_official 49:77c8e4604045 1285 /// Delete a Mutex that was created by osMutexCreate
mbed_official 49:77c8e4604045 1286 osStatus osMutexDelete (osMutexId mutex_id) {
mbed_official 49:77c8e4604045 1287 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 1288 return __svcMutexDelete(mutex_id);
mbed_official 49:77c8e4604045 1289 }
mbed_official 49:77c8e4604045 1290
mbed_official 49:77c8e4604045 1291
mbed_official 49:77c8e4604045 1292 // ==== Semaphore Management ====
mbed_official 49:77c8e4604045 1293
mbed_official 49:77c8e4604045 1294 // Semaphore Service Calls declarations
mbed_official 49:77c8e4604045 1295 SVC_2_1(svcSemaphoreCreate, osSemaphoreId, const osSemaphoreDef_t *, int32_t, RET_pointer)
mbed_official 49:77c8e4604045 1296 SVC_2_1(svcSemaphoreWait, int32_t, osSemaphoreId, uint32_t, RET_int32_t)
mbed_official 49:77c8e4604045 1297 SVC_1_1(svcSemaphoreRelease, osStatus, osSemaphoreId, RET_osStatus)
mbed_official 49:77c8e4604045 1298 SVC_1_1(svcSemaphoreDelete, osStatus, osSemaphoreId, RET_osStatus)
mbed_official 49:77c8e4604045 1299
mbed_official 49:77c8e4604045 1300 // Semaphore Service Calls
mbed_official 49:77c8e4604045 1301
mbed_official 49:77c8e4604045 1302 /// Create and Initialize a Semaphore object
mbed_official 49:77c8e4604045 1303 osSemaphoreId svcSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count) {
mbed_official 49:77c8e4604045 1304 OS_ID sem;
mbed_official 49:77c8e4604045 1305
mbed_official 49:77c8e4604045 1306 if (semaphore_def == NULL) {
mbed_official 49:77c8e4604045 1307 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 1308 return NULL;
mbed_official 49:77c8e4604045 1309 }
mbed_official 49:77c8e4604045 1310
mbed_official 49:77c8e4604045 1311 sem = semaphore_def->semaphore;
mbed_official 49:77c8e4604045 1312 if (sem == NULL) {
mbed_official 49:77c8e4604045 1313 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 1314 return NULL;
mbed_official 49:77c8e4604045 1315 }
mbed_official 49:77c8e4604045 1316
mbed_official 49:77c8e4604045 1317 if (((P_SCB)sem)->cb_type != 0) {
mbed_official 49:77c8e4604045 1318 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 1319 return NULL;
mbed_official 49:77c8e4604045 1320 }
mbed_official 49:77c8e4604045 1321
mbed_official 49:77c8e4604045 1322 if (count > osFeature_Semaphore) {
mbed_official 49:77c8e4604045 1323 sysThreadError(osErrorValue);
mbed_official 49:77c8e4604045 1324 return NULL;
mbed_official 49:77c8e4604045 1325 }
mbed_official 49:77c8e4604045 1326
mbed_official 49:77c8e4604045 1327 rt_sem_init(sem, count); // Initialize Semaphore
mbed_official 49:77c8e4604045 1328
mbed_official 49:77c8e4604045 1329 return sem;
mbed_official 49:77c8e4604045 1330 }
mbed_official 49:77c8e4604045 1331
mbed_official 49:77c8e4604045 1332 /// Wait until a Semaphore becomes available
mbed_official 49:77c8e4604045 1333 int32_t svcSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec) {
mbed_official 49:77c8e4604045 1334 OS_ID sem;
mbed_official 49:77c8e4604045 1335 OS_RESULT res;
mbed_official 49:77c8e4604045 1336
mbed_official 49:77c8e4604045 1337 sem = rt_id2obj(semaphore_id);
mbed_official 49:77c8e4604045 1338 if (sem == NULL) return -1;
mbed_official 49:77c8e4604045 1339
mbed_official 49:77c8e4604045 1340 if (((P_SCB)sem)->cb_type != SCB) return -1;
mbed_official 49:77c8e4604045 1341
mbed_official 49:77c8e4604045 1342 res = rt_sem_wait(sem, rt_ms2tick(millisec)); // Wait for Semaphore
mbed_official 49:77c8e4604045 1343
mbed_official 49:77c8e4604045 1344 if (res == OS_R_TMO) return 0; // Timeout
mbed_official 49:77c8e4604045 1345
mbed_official 49:77c8e4604045 1346 return (((P_SCB)sem)->tokens + 1);
mbed_official 49:77c8e4604045 1347 }
mbed_official 49:77c8e4604045 1348
mbed_official 49:77c8e4604045 1349 /// Release a Semaphore
mbed_official 49:77c8e4604045 1350 osStatus svcSemaphoreRelease (osSemaphoreId semaphore_id) {
mbed_official 49:77c8e4604045 1351 OS_ID sem;
mbed_official 49:77c8e4604045 1352
mbed_official 49:77c8e4604045 1353 sem = rt_id2obj(semaphore_id);
mbed_official 49:77c8e4604045 1354 if (sem == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 1355
mbed_official 49:77c8e4604045 1356 if (((P_SCB)sem)->cb_type != SCB) return osErrorParameter;
mbed_official 49:77c8e4604045 1357
mbed_official 49:77c8e4604045 1358 if (((P_SCB)sem)->tokens == osFeature_Semaphore) return osErrorResource;
mbed_official 49:77c8e4604045 1359
mbed_official 49:77c8e4604045 1360 rt_sem_send(sem); // Release Semaphore
mbed_official 49:77c8e4604045 1361
mbed_official 49:77c8e4604045 1362 return osOK;
mbed_official 49:77c8e4604045 1363 }
mbed_official 49:77c8e4604045 1364
mbed_official 49:77c8e4604045 1365 /// Delete a Semaphore that was created by osSemaphoreCreate
mbed_official 49:77c8e4604045 1366 osStatus svcSemaphoreDelete (osSemaphoreId semaphore_id) {
mbed_official 49:77c8e4604045 1367 OS_ID sem;
mbed_official 49:77c8e4604045 1368
mbed_official 49:77c8e4604045 1369 sem = rt_id2obj(semaphore_id);
mbed_official 49:77c8e4604045 1370 if (sem == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 1371
mbed_official 49:77c8e4604045 1372 if (((P_SCB)sem)->cb_type != SCB) return osErrorParameter;
mbed_official 49:77c8e4604045 1373
mbed_official 49:77c8e4604045 1374 rt_sem_delete(sem); // Delete Semaphore
mbed_official 49:77c8e4604045 1375
mbed_official 49:77c8e4604045 1376 return osOK;
mbed_official 49:77c8e4604045 1377 }
mbed_official 49:77c8e4604045 1378
mbed_official 49:77c8e4604045 1379
mbed_official 49:77c8e4604045 1380 // Semaphore ISR Calls
mbed_official 49:77c8e4604045 1381
mbed_official 49:77c8e4604045 1382 /// Release a Semaphore
mbed_official 49:77c8e4604045 1383 static __INLINE osStatus isrSemaphoreRelease (osSemaphoreId semaphore_id) {
mbed_official 49:77c8e4604045 1384 OS_ID sem;
mbed_official 49:77c8e4604045 1385
mbed_official 49:77c8e4604045 1386 sem = rt_id2obj(semaphore_id);
mbed_official 49:77c8e4604045 1387 if (sem == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 1388
mbed_official 49:77c8e4604045 1389 if (((P_SCB)sem)->cb_type != SCB) return osErrorParameter;
mbed_official 49:77c8e4604045 1390
mbed_official 49:77c8e4604045 1391 if (((P_SCB)sem)->tokens == osFeature_Semaphore) return osErrorResource;
mbed_official 49:77c8e4604045 1392
mbed_official 49:77c8e4604045 1393 isr_sem_send(sem); // Release Semaphore
mbed_official 49:77c8e4604045 1394
mbed_official 49:77c8e4604045 1395 return osOK;
mbed_official 49:77c8e4604045 1396 }
mbed_official 49:77c8e4604045 1397
mbed_official 49:77c8e4604045 1398
mbed_official 49:77c8e4604045 1399 // Semaphore Public API
mbed_official 49:77c8e4604045 1400
mbed_official 49:77c8e4604045 1401 /// Create and Initialize a Semaphore object
mbed_official 49:77c8e4604045 1402 osSemaphoreId osSemaphoreCreate (osSemaphoreDef_t *semaphore_def, int32_t count) {
mbed_official 49:77c8e4604045 1403 if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
mbed_official 49:77c8e4604045 1404 if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
mbed_official 49:77c8e4604045 1405 // Privileged and not running
mbed_official 49:77c8e4604045 1406 return svcSemaphoreCreate(semaphore_def, count);
mbed_official 49:77c8e4604045 1407 } else {
mbed_official 49:77c8e4604045 1408 return __svcSemaphoreCreate(semaphore_def, count);
mbed_official 49:77c8e4604045 1409 }
mbed_official 49:77c8e4604045 1410 }
mbed_official 49:77c8e4604045 1411
mbed_official 49:77c8e4604045 1412 /// Wait until a Semaphore becomes available
mbed_official 49:77c8e4604045 1413 int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec) {
mbed_official 49:77c8e4604045 1414 if (__get_IPSR() != 0) return -1; // Not allowed in ISR
mbed_official 49:77c8e4604045 1415 return __svcSemaphoreWait(semaphore_id, millisec);
mbed_official 49:77c8e4604045 1416 }
mbed_official 49:77c8e4604045 1417
mbed_official 49:77c8e4604045 1418 /// Release a Semaphore
mbed_official 49:77c8e4604045 1419 osStatus osSemaphoreRelease (osSemaphoreId semaphore_id) {
mbed_official 49:77c8e4604045 1420 if (__get_IPSR() != 0) { // in ISR
mbed_official 49:77c8e4604045 1421 return isrSemaphoreRelease(semaphore_id);
mbed_official 49:77c8e4604045 1422 } else { // in Thread
mbed_official 49:77c8e4604045 1423 return __svcSemaphoreRelease(semaphore_id);
mbed_official 49:77c8e4604045 1424 }
mbed_official 49:77c8e4604045 1425 }
mbed_official 49:77c8e4604045 1426
mbed_official 49:77c8e4604045 1427 /// Delete a Semaphore that was created by osSemaphoreCreate
mbed_official 49:77c8e4604045 1428 osStatus osSemaphoreDelete (osSemaphoreId semaphore_id) {
mbed_official 49:77c8e4604045 1429 if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
mbed_official 49:77c8e4604045 1430 return __svcSemaphoreDelete(semaphore_id);
mbed_official 49:77c8e4604045 1431 }
mbed_official 49:77c8e4604045 1432
mbed_official 49:77c8e4604045 1433
mbed_official 49:77c8e4604045 1434 // ==== Memory Management Functions ====
mbed_official 49:77c8e4604045 1435
mbed_official 49:77c8e4604045 1436 // Memory Management Helper Functions
mbed_official 49:77c8e4604045 1437
mbed_official 49:77c8e4604045 1438 // Clear Memory Box (Zero init)
mbed_official 49:77c8e4604045 1439 static void rt_clr_box (void *box_mem, void *box) {
mbed_official 49:77c8e4604045 1440 uint32_t *p, n;
mbed_official 49:77c8e4604045 1441
mbed_official 49:77c8e4604045 1442 if (box) {
mbed_official 49:77c8e4604045 1443 p = box;
mbed_official 49:77c8e4604045 1444 for (n = ((P_BM)box_mem)->blk_size; n; n -= 4) {
mbed_official 49:77c8e4604045 1445 *p++ = 0;
mbed_official 49:77c8e4604045 1446 }
mbed_official 49:77c8e4604045 1447 }
mbed_official 49:77c8e4604045 1448 }
mbed_official 49:77c8e4604045 1449
mbed_official 49:77c8e4604045 1450 // Memory Management Service Calls declarations
mbed_official 49:77c8e4604045 1451 SVC_1_1(svcPoolCreate, osPoolId, const osPoolDef_t *, RET_pointer)
mbed_official 49:77c8e4604045 1452 SVC_2_1(sysPoolAlloc, void *, osPoolId, uint32_t, RET_pointer)
mbed_official 49:77c8e4604045 1453 SVC_2_1(sysPoolFree, osStatus, osPoolId, void *, RET_osStatus)
mbed_official 49:77c8e4604045 1454
mbed_official 49:77c8e4604045 1455 // Memory Management Service & ISR Calls
mbed_official 49:77c8e4604045 1456
mbed_official 49:77c8e4604045 1457 /// Create and Initialize memory pool
mbed_official 49:77c8e4604045 1458 osPoolId svcPoolCreate (const osPoolDef_t *pool_def) {
mbed_official 49:77c8e4604045 1459 uint32_t blk_sz;
mbed_official 49:77c8e4604045 1460
mbed_official 49:77c8e4604045 1461 if ((pool_def == NULL) ||
mbed_official 49:77c8e4604045 1462 (pool_def->pool_sz == 0) ||
mbed_official 49:77c8e4604045 1463 (pool_def->item_sz == 0) ||
mbed_official 49:77c8e4604045 1464 (pool_def->pool == NULL)) {
mbed_official 49:77c8e4604045 1465 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 1466 return NULL;
mbed_official 49:77c8e4604045 1467 }
mbed_official 49:77c8e4604045 1468
mbed_official 49:77c8e4604045 1469 blk_sz = (pool_def->item_sz + 3) & ~3;
mbed_official 49:77c8e4604045 1470
mbed_official 49:77c8e4604045 1471 _init_box(pool_def->pool, sizeof(struct OS_BM) + pool_def->pool_sz * blk_sz, blk_sz);
mbed_official 49:77c8e4604045 1472
mbed_official 49:77c8e4604045 1473 return pool_def->pool;
mbed_official 49:77c8e4604045 1474 }
mbed_official 49:77c8e4604045 1475
mbed_official 49:77c8e4604045 1476 /// Allocate a memory block from a memory pool
mbed_official 49:77c8e4604045 1477 void *sysPoolAlloc (osPoolId pool_id, uint32_t clr) {
mbed_official 49:77c8e4604045 1478 void *ptr;
mbed_official 49:77c8e4604045 1479
mbed_official 49:77c8e4604045 1480 if (pool_id == NULL) return NULL;
mbed_official 49:77c8e4604045 1481
mbed_official 49:77c8e4604045 1482 ptr = rt_alloc_box(pool_id);
mbed_official 49:77c8e4604045 1483 if (clr) {
mbed_official 49:77c8e4604045 1484 rt_clr_box(pool_id, ptr);
mbed_official 49:77c8e4604045 1485 }
mbed_official 49:77c8e4604045 1486
mbed_official 49:77c8e4604045 1487 return ptr;
mbed_official 49:77c8e4604045 1488 }
mbed_official 49:77c8e4604045 1489
mbed_official 49:77c8e4604045 1490 /// Return an allocated memory block back to a specific memory pool
mbed_official 49:77c8e4604045 1491 osStatus sysPoolFree (osPoolId pool_id, void *block) {
mbed_official 49:77c8e4604045 1492 int32_t res;
mbed_official 49:77c8e4604045 1493
mbed_official 49:77c8e4604045 1494 if (pool_id == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 1495
mbed_official 49:77c8e4604045 1496 res = rt_free_box(pool_id, block);
mbed_official 49:77c8e4604045 1497 if (res != 0) return osErrorValue;
mbed_official 49:77c8e4604045 1498
mbed_official 49:77c8e4604045 1499 return osOK;
mbed_official 49:77c8e4604045 1500 }
mbed_official 49:77c8e4604045 1501
mbed_official 49:77c8e4604045 1502
mbed_official 49:77c8e4604045 1503 // Memory Management Public API
mbed_official 49:77c8e4604045 1504
mbed_official 49:77c8e4604045 1505 /// Create and Initialize memory pool
mbed_official 49:77c8e4604045 1506 osPoolId osPoolCreate (osPoolDef_t *pool_def) {
mbed_official 49:77c8e4604045 1507 if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
mbed_official 49:77c8e4604045 1508 if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
mbed_official 49:77c8e4604045 1509 // Privileged and not running
mbed_official 49:77c8e4604045 1510 return svcPoolCreate(pool_def);
mbed_official 49:77c8e4604045 1511 } else {
mbed_official 49:77c8e4604045 1512 return __svcPoolCreate(pool_def);
mbed_official 49:77c8e4604045 1513 }
mbed_official 49:77c8e4604045 1514 }
mbed_official 49:77c8e4604045 1515
mbed_official 49:77c8e4604045 1516 /// Allocate a memory block from a memory pool
mbed_official 49:77c8e4604045 1517 void *osPoolAlloc (osPoolId pool_id) {
mbed_official 49:77c8e4604045 1518 if ((__get_IPSR() != 0) || ((__get_CONTROL() & 1) == 0)) { // in ISR or Privileged
mbed_official 49:77c8e4604045 1519 return sysPoolAlloc(pool_id, 0);
mbed_official 49:77c8e4604045 1520 } else { // in Thread
mbed_official 49:77c8e4604045 1521 return __sysPoolAlloc(pool_id, 0);
mbed_official 49:77c8e4604045 1522 }
mbed_official 49:77c8e4604045 1523 }
mbed_official 49:77c8e4604045 1524
mbed_official 49:77c8e4604045 1525 /// Allocate a memory block from a memory pool and set memory block to zero
mbed_official 49:77c8e4604045 1526 void *osPoolCAlloc (osPoolId pool_id) {
mbed_official 49:77c8e4604045 1527 if ((__get_IPSR() != 0) || ((__get_CONTROL() & 1) == 0)) { // in ISR or Privileged
mbed_official 49:77c8e4604045 1528 return sysPoolAlloc(pool_id, 1);
mbed_official 49:77c8e4604045 1529 } else { // in Thread
mbed_official 49:77c8e4604045 1530 return __sysPoolAlloc(pool_id, 1);
mbed_official 49:77c8e4604045 1531 }
mbed_official 49:77c8e4604045 1532 }
mbed_official 49:77c8e4604045 1533
mbed_official 49:77c8e4604045 1534 /// Return an allocated memory block back to a specific memory pool
mbed_official 49:77c8e4604045 1535 osStatus osPoolFree (osPoolId pool_id, void *block) {
mbed_official 49:77c8e4604045 1536 if ((__get_IPSR() != 0) || ((__get_CONTROL() & 1) == 0)) { // in ISR or Privileged
mbed_official 49:77c8e4604045 1537 return sysPoolFree(pool_id, block);
mbed_official 49:77c8e4604045 1538 } else { // in Thread
mbed_official 49:77c8e4604045 1539 return __sysPoolFree(pool_id, block);
mbed_official 49:77c8e4604045 1540 }
mbed_official 49:77c8e4604045 1541 }
mbed_official 49:77c8e4604045 1542
mbed_official 49:77c8e4604045 1543
mbed_official 49:77c8e4604045 1544 // ==== Message Queue Management Functions ====
mbed_official 49:77c8e4604045 1545
mbed_official 49:77c8e4604045 1546 // Message Queue Management Service Calls declarations
mbed_official 49:77c8e4604045 1547 SVC_2_1(svcMessageCreate, osMessageQId, osMessageQDef_t *, osThreadId, RET_pointer)
mbed_official 49:77c8e4604045 1548 SVC_3_1(svcMessagePut, osStatus, osMessageQId, uint32_t, uint32_t, RET_osStatus)
mbed_official 49:77c8e4604045 1549 SVC_2_3(svcMessageGet, os_InRegs osEvent, osMessageQId, uint32_t, RET_osEvent)
mbed_official 49:77c8e4604045 1550
mbed_official 49:77c8e4604045 1551 // Message Queue Service Calls
mbed_official 49:77c8e4604045 1552
mbed_official 49:77c8e4604045 1553 /// Create and Initialize Message Queue
mbed_official 49:77c8e4604045 1554 osMessageQId svcMessageCreate (osMessageQDef_t *queue_def, osThreadId thread_id) {
mbed_official 49:77c8e4604045 1555
mbed_official 49:77c8e4604045 1556 if ((queue_def == NULL) ||
mbed_official 49:77c8e4604045 1557 (queue_def->queue_sz == 0) ||
mbed_official 49:77c8e4604045 1558 (queue_def->pool == NULL)) {
mbed_official 49:77c8e4604045 1559 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 1560 return NULL;
mbed_official 49:77c8e4604045 1561 }
mbed_official 49:77c8e4604045 1562
mbed_official 49:77c8e4604045 1563 if (((P_MCB)queue_def->pool)->cb_type != 0) {
mbed_official 49:77c8e4604045 1564 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 1565 return NULL;
mbed_official 49:77c8e4604045 1566 }
mbed_official 49:77c8e4604045 1567
mbed_official 49:77c8e4604045 1568 rt_mbx_init(queue_def->pool, 4*(queue_def->queue_sz + 4));
mbed_official 49:77c8e4604045 1569
mbed_official 49:77c8e4604045 1570 return queue_def->pool;
mbed_official 49:77c8e4604045 1571 }
mbed_official 49:77c8e4604045 1572
mbed_official 49:77c8e4604045 1573 /// Put a Message to a Queue
mbed_official 49:77c8e4604045 1574 osStatus svcMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
mbed_official 49:77c8e4604045 1575 OS_RESULT res;
mbed_official 49:77c8e4604045 1576
mbed_official 49:77c8e4604045 1577 if (queue_id == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 1578
mbed_official 49:77c8e4604045 1579 if (((P_MCB)queue_id)->cb_type != MCB) return osErrorParameter;
mbed_official 49:77c8e4604045 1580
mbed_official 49:77c8e4604045 1581 res = rt_mbx_send(queue_id, (void *)info, rt_ms2tick(millisec));
mbed_official 49:77c8e4604045 1582
mbed_official 49:77c8e4604045 1583 if (res == OS_R_TMO) {
mbed_official 49:77c8e4604045 1584 return (millisec ? osErrorTimeoutResource : osErrorResource);
mbed_official 49:77c8e4604045 1585 }
mbed_official 49:77c8e4604045 1586
mbed_official 49:77c8e4604045 1587 return osOK;
mbed_official 49:77c8e4604045 1588 }
mbed_official 49:77c8e4604045 1589
mbed_official 49:77c8e4604045 1590 /// Get a Message or Wait for a Message from a Queue
mbed_official 49:77c8e4604045 1591 os_InRegs osEvent_type svcMessageGet (osMessageQId queue_id, uint32_t millisec) {
mbed_official 49:77c8e4604045 1592 OS_RESULT res;
mbed_official 49:77c8e4604045 1593 osEvent ret;
mbed_official 49:77c8e4604045 1594
mbed_official 49:77c8e4604045 1595 if (queue_id == NULL) {
mbed_official 49:77c8e4604045 1596 ret.status = osErrorParameter;
mbed_official 49:77c8e4604045 1597 return osEvent_ret_status;
mbed_official 49:77c8e4604045 1598 }
mbed_official 49:77c8e4604045 1599
mbed_official 49:77c8e4604045 1600 if (((P_MCB)queue_id)->cb_type != MCB) {
mbed_official 49:77c8e4604045 1601 ret.status = osErrorParameter;
mbed_official 49:77c8e4604045 1602 return osEvent_ret_status;
mbed_official 49:77c8e4604045 1603 }
mbed_official 49:77c8e4604045 1604
mbed_official 49:77c8e4604045 1605 res = rt_mbx_wait(queue_id, &ret.value.p, rt_ms2tick(millisec));
mbed_official 49:77c8e4604045 1606
mbed_official 49:77c8e4604045 1607 if (res == OS_R_TMO) {
mbed_official 49:77c8e4604045 1608 ret.status = millisec ? osEventTimeout : osOK;
mbed_official 49:77c8e4604045 1609 return osEvent_ret_value;
mbed_official 49:77c8e4604045 1610 }
mbed_official 49:77c8e4604045 1611
mbed_official 49:77c8e4604045 1612 ret.status = osEventMessage;
mbed_official 49:77c8e4604045 1613
mbed_official 49:77c8e4604045 1614 return osEvent_ret_value;
mbed_official 49:77c8e4604045 1615 }
mbed_official 49:77c8e4604045 1616
mbed_official 49:77c8e4604045 1617
mbed_official 49:77c8e4604045 1618 // Message Queue ISR Calls
mbed_official 49:77c8e4604045 1619
mbed_official 49:77c8e4604045 1620 /// Put a Message to a Queue
mbed_official 49:77c8e4604045 1621 static __INLINE osStatus isrMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
mbed_official 49:77c8e4604045 1622
mbed_official 49:77c8e4604045 1623 if ((queue_id == NULL) || (millisec != 0)) {
mbed_official 49:77c8e4604045 1624 return osErrorParameter;
mbed_official 49:77c8e4604045 1625 }
mbed_official 49:77c8e4604045 1626
mbed_official 49:77c8e4604045 1627 if (((P_MCB)queue_id)->cb_type != MCB) return osErrorParameter;
mbed_official 49:77c8e4604045 1628
mbed_official 49:77c8e4604045 1629 if (rt_mbx_check(queue_id) == 0) { // Check if Queue is full
mbed_official 49:77c8e4604045 1630 return osErrorResource;
mbed_official 49:77c8e4604045 1631 }
mbed_official 49:77c8e4604045 1632
mbed_official 49:77c8e4604045 1633 isr_mbx_send(queue_id, (void *)info);
mbed_official 49:77c8e4604045 1634
mbed_official 49:77c8e4604045 1635 return osOK;
mbed_official 49:77c8e4604045 1636 }
mbed_official 49:77c8e4604045 1637
mbed_official 49:77c8e4604045 1638 /// Get a Message or Wait for a Message from a Queue
mbed_official 49:77c8e4604045 1639 static __INLINE os_InRegs osEvent isrMessageGet (osMessageQId queue_id, uint32_t millisec) {
mbed_official 49:77c8e4604045 1640 OS_RESULT res;
mbed_official 49:77c8e4604045 1641 osEvent ret;
mbed_official 49:77c8e4604045 1642
mbed_official 49:77c8e4604045 1643 if ((queue_id == NULL) || (millisec != 0)) {
mbed_official 49:77c8e4604045 1644 ret.status = osErrorParameter;
mbed_official 49:77c8e4604045 1645 return ret;
mbed_official 49:77c8e4604045 1646 }
mbed_official 49:77c8e4604045 1647
mbed_official 49:77c8e4604045 1648 if (((P_MCB)queue_id)->cb_type != MCB) {
mbed_official 49:77c8e4604045 1649 ret.status = osErrorParameter;
mbed_official 49:77c8e4604045 1650 return ret;
mbed_official 49:77c8e4604045 1651 }
mbed_official 49:77c8e4604045 1652
mbed_official 49:77c8e4604045 1653 res = isr_mbx_receive(queue_id, &ret.value.p);
mbed_official 49:77c8e4604045 1654
mbed_official 49:77c8e4604045 1655 if (res != OS_R_MBX) {
mbed_official 49:77c8e4604045 1656 ret.status = osOK;
mbed_official 49:77c8e4604045 1657 return ret;
mbed_official 49:77c8e4604045 1658 }
mbed_official 49:77c8e4604045 1659
mbed_official 49:77c8e4604045 1660 ret.status = osEventMessage;
mbed_official 49:77c8e4604045 1661
mbed_official 49:77c8e4604045 1662 return ret;
mbed_official 49:77c8e4604045 1663 }
mbed_official 49:77c8e4604045 1664
mbed_official 49:77c8e4604045 1665
mbed_official 49:77c8e4604045 1666 // Message Queue Management Public API
mbed_official 49:77c8e4604045 1667
mbed_official 49:77c8e4604045 1668 /// Create and Initialize Message Queue
mbed_official 49:77c8e4604045 1669 osMessageQId osMessageCreate (osMessageQDef_t *queue_def, osThreadId thread_id) {
mbed_official 49:77c8e4604045 1670 if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
mbed_official 49:77c8e4604045 1671 if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
mbed_official 49:77c8e4604045 1672 // Privileged and not running
mbed_official 49:77c8e4604045 1673 return svcMessageCreate(queue_def, thread_id);
mbed_official 49:77c8e4604045 1674 } else {
mbed_official 49:77c8e4604045 1675 return __svcMessageCreate(queue_def, thread_id);
mbed_official 49:77c8e4604045 1676 }
mbed_official 49:77c8e4604045 1677 }
mbed_official 49:77c8e4604045 1678
mbed_official 49:77c8e4604045 1679 /// Put a Message to a Queue
mbed_official 49:77c8e4604045 1680 osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
mbed_official 49:77c8e4604045 1681 if (__get_IPSR() != 0) { // in ISR
mbed_official 49:77c8e4604045 1682 return isrMessagePut(queue_id, info, millisec);
mbed_official 49:77c8e4604045 1683 } else { // in Thread
mbed_official 49:77c8e4604045 1684 return __svcMessagePut(queue_id, info, millisec);
mbed_official 49:77c8e4604045 1685 }
mbed_official 49:77c8e4604045 1686 }
mbed_official 49:77c8e4604045 1687
mbed_official 49:77c8e4604045 1688 /// Get a Message or Wait for a Message from a Queue
mbed_official 49:77c8e4604045 1689 os_InRegs osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec) {
mbed_official 49:77c8e4604045 1690 if (__get_IPSR() != 0) { // in ISR
mbed_official 49:77c8e4604045 1691 return isrMessageGet(queue_id, millisec);
mbed_official 49:77c8e4604045 1692 } else { // in Thread
mbed_official 49:77c8e4604045 1693 return __svcMessageGet(queue_id, millisec);
mbed_official 49:77c8e4604045 1694 }
mbed_official 49:77c8e4604045 1695 }
mbed_official 49:77c8e4604045 1696
mbed_official 49:77c8e4604045 1697
mbed_official 49:77c8e4604045 1698 // ==== Mail Queue Management Functions ====
mbed_official 49:77c8e4604045 1699
mbed_official 49:77c8e4604045 1700 // Mail Queue Management Service Calls declarations
mbed_official 49:77c8e4604045 1701 SVC_2_1(svcMailCreate, osMailQId, osMailQDef_t *, osThreadId, RET_pointer)
mbed_official 49:77c8e4604045 1702 SVC_4_1(sysMailAlloc, void *, osMailQId, uint32_t, uint32_t, uint32_t, RET_pointer)
mbed_official 49:77c8e4604045 1703 SVC_3_1(sysMailFree, osStatus, osMailQId, void *, uint32_t, RET_osStatus)
mbed_official 49:77c8e4604045 1704
mbed_official 49:77c8e4604045 1705 // Mail Queue Management Service & ISR Calls
mbed_official 49:77c8e4604045 1706
mbed_official 49:77c8e4604045 1707 /// Create and Initialize mail queue
mbed_official 49:77c8e4604045 1708 osMailQId svcMailCreate (osMailQDef_t *queue_def, osThreadId thread_id) {
mbed_official 49:77c8e4604045 1709 uint32_t blk_sz;
mbed_official 49:77c8e4604045 1710 P_MCB pmcb;
mbed_official 49:77c8e4604045 1711 void *pool;
mbed_official 49:77c8e4604045 1712
mbed_official 49:77c8e4604045 1713 if ((queue_def == NULL) ||
mbed_official 49:77c8e4604045 1714 (queue_def->queue_sz == 0) ||
mbed_official 49:77c8e4604045 1715 (queue_def->item_sz == 0) ||
mbed_official 49:77c8e4604045 1716 (queue_def->pool == NULL)) {
mbed_official 49:77c8e4604045 1717 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 1718 return NULL;
mbed_official 49:77c8e4604045 1719 }
mbed_official 49:77c8e4604045 1720
mbed_official 49:77c8e4604045 1721 pmcb = *(((void **)queue_def->pool) + 0);
mbed_official 49:77c8e4604045 1722 pool = *(((void **)queue_def->pool) + 1);
mbed_official 49:77c8e4604045 1723
mbed_official 49:77c8e4604045 1724 if ((pool == NULL) || (pmcb == NULL) || (pmcb->cb_type != 0)) {
mbed_official 49:77c8e4604045 1725 sysThreadError(osErrorParameter);
mbed_official 49:77c8e4604045 1726 return NULL;
mbed_official 49:77c8e4604045 1727 }
mbed_official 49:77c8e4604045 1728
mbed_official 49:77c8e4604045 1729 blk_sz = (queue_def->item_sz + 3) & ~3;
mbed_official 49:77c8e4604045 1730
mbed_official 49:77c8e4604045 1731 _init_box(pool, sizeof(struct OS_BM) + queue_def->queue_sz * blk_sz, blk_sz);
mbed_official 49:77c8e4604045 1732
mbed_official 49:77c8e4604045 1733 rt_mbx_init(pmcb, 4*(queue_def->queue_sz + 4));
mbed_official 49:77c8e4604045 1734
mbed_official 49:77c8e4604045 1735
mbed_official 49:77c8e4604045 1736 return queue_def->pool;
mbed_official 49:77c8e4604045 1737 }
mbed_official 49:77c8e4604045 1738
mbed_official 49:77c8e4604045 1739 /// Allocate a memory block from a mail
mbed_official 49:77c8e4604045 1740 void *sysMailAlloc (osMailQId queue_id, uint32_t millisec, uint32_t isr, uint32_t clr) {
mbed_official 49:77c8e4604045 1741 P_MCB pmcb;
mbed_official 49:77c8e4604045 1742 void *pool;
mbed_official 49:77c8e4604045 1743 void *mem;
mbed_official 49:77c8e4604045 1744
mbed_official 49:77c8e4604045 1745 if (queue_id == NULL) return NULL;
mbed_official 49:77c8e4604045 1746
mbed_official 49:77c8e4604045 1747 pmcb = *(((void **)queue_id) + 0);
mbed_official 49:77c8e4604045 1748 pool = *(((void **)queue_id) + 1);
mbed_official 49:77c8e4604045 1749
mbed_official 49:77c8e4604045 1750 if ((pool == NULL) || (pmcb == NULL)) return NULL;
mbed_official 49:77c8e4604045 1751
mbed_official 49:77c8e4604045 1752 if (isr && (millisec != 0)) return NULL;
mbed_official 49:77c8e4604045 1753
mbed_official 49:77c8e4604045 1754 mem = rt_alloc_box(pool);
mbed_official 49:77c8e4604045 1755 if (clr) {
mbed_official 49:77c8e4604045 1756 rt_clr_box(pool, mem);
mbed_official 49:77c8e4604045 1757 }
mbed_official 49:77c8e4604045 1758
mbed_official 49:77c8e4604045 1759 if ((mem == NULL) && (millisec != 0)) {
mbed_official 49:77c8e4604045 1760 // Put Task to sleep when Memory not available
mbed_official 49:77c8e4604045 1761 if (pmcb->p_lnk != NULL) {
mbed_official 49:77c8e4604045 1762 rt_put_prio((P_XCB)pmcb, os_tsk.run);
mbed_official 49:77c8e4604045 1763 } else {
mbed_official 49:77c8e4604045 1764 pmcb->p_lnk = os_tsk.run;
mbed_official 49:77c8e4604045 1765 os_tsk.run->p_lnk = NULL;
mbed_official 49:77c8e4604045 1766 os_tsk.run->p_rlnk = (P_TCB)pmcb;
mbed_official 49:77c8e4604045 1767 // Task is waiting to allocate a message
mbed_official 49:77c8e4604045 1768 pmcb->state = 3;
mbed_official 49:77c8e4604045 1769 }
mbed_official 49:77c8e4604045 1770 rt_block(rt_ms2tick(millisec), WAIT_MBX);
mbed_official 49:77c8e4604045 1771 }
mbed_official 49:77c8e4604045 1772
mbed_official 49:77c8e4604045 1773 return mem;
mbed_official 49:77c8e4604045 1774 }
mbed_official 49:77c8e4604045 1775
mbed_official 49:77c8e4604045 1776 /// Free a memory block from a mail
mbed_official 49:77c8e4604045 1777 osStatus sysMailFree (osMailQId queue_id, void *mail, uint32_t isr) {
mbed_official 49:77c8e4604045 1778 P_MCB pmcb;
mbed_official 49:77c8e4604045 1779 P_TCB ptcb;
mbed_official 49:77c8e4604045 1780 void *pool;
mbed_official 49:77c8e4604045 1781 void *mem;
mbed_official 49:77c8e4604045 1782 int32_t res;
mbed_official 49:77c8e4604045 1783
mbed_official 49:77c8e4604045 1784 if (queue_id == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 1785
mbed_official 49:77c8e4604045 1786 pmcb = *(((void **)queue_id) + 0);
mbed_official 49:77c8e4604045 1787 pool = *(((void **)queue_id) + 1);
mbed_official 49:77c8e4604045 1788
mbed_official 49:77c8e4604045 1789 if ((pmcb == NULL) || (pool == NULL)) return osErrorParameter;
mbed_official 49:77c8e4604045 1790
mbed_official 49:77c8e4604045 1791 res = rt_free_box(pool, mail);
mbed_official 49:77c8e4604045 1792
mbed_official 49:77c8e4604045 1793 if (res != 0) return osErrorValue;
mbed_official 49:77c8e4604045 1794
mbed_official 49:77c8e4604045 1795 if (pmcb->state == 3) {
mbed_official 49:77c8e4604045 1796 // Task is waiting to allocate a message
mbed_official 49:77c8e4604045 1797 if (isr) {
mbed_official 49:77c8e4604045 1798 rt_psq_enq (pmcb, (U32)pool);
mbed_official 49:77c8e4604045 1799 rt_psh_req ();
mbed_official 49:77c8e4604045 1800 } else {
mbed_official 49:77c8e4604045 1801 mem = rt_alloc_box(pool);
mbed_official 49:77c8e4604045 1802 if (mem != NULL) {
mbed_official 49:77c8e4604045 1803 ptcb = rt_get_first((P_XCB)pmcb);
mbed_official 49:77c8e4604045 1804 if (pmcb->p_lnk == NULL) {
mbed_official 49:77c8e4604045 1805 pmcb->state = 0;
mbed_official 49:77c8e4604045 1806 }
mbed_official 49:77c8e4604045 1807 rt_ret_val(ptcb, (U32)mem);
mbed_official 49:77c8e4604045 1808 rt_rmv_dly(ptcb);
mbed_official 49:77c8e4604045 1809 rt_dispatch(ptcb);
mbed_official 49:77c8e4604045 1810 }
mbed_official 49:77c8e4604045 1811 }
mbed_official 49:77c8e4604045 1812 }
mbed_official 49:77c8e4604045 1813
mbed_official 49:77c8e4604045 1814 return osOK;
mbed_official 49:77c8e4604045 1815 }
mbed_official 49:77c8e4604045 1816
mbed_official 49:77c8e4604045 1817
mbed_official 49:77c8e4604045 1818 // Mail Queue Management Public API
mbed_official 49:77c8e4604045 1819
mbed_official 49:77c8e4604045 1820 /// Create and Initialize mail queue
mbed_official 49:77c8e4604045 1821 osMailQId osMailCreate (osMailQDef_t *queue_def, osThreadId thread_id) {
mbed_official 49:77c8e4604045 1822 if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
mbed_official 49:77c8e4604045 1823 if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
mbed_official 49:77c8e4604045 1824 // Privileged and not running
mbed_official 49:77c8e4604045 1825 return svcMailCreate(queue_def, thread_id);
mbed_official 49:77c8e4604045 1826 } else {
mbed_official 49:77c8e4604045 1827 return __svcMailCreate(queue_def, thread_id);
mbed_official 49:77c8e4604045 1828 }
mbed_official 49:77c8e4604045 1829 }
mbed_official 49:77c8e4604045 1830
mbed_official 49:77c8e4604045 1831 /// Allocate a memory block from a mail
mbed_official 49:77c8e4604045 1832 void *osMailAlloc (osMailQId queue_id, uint32_t millisec) {
mbed_official 49:77c8e4604045 1833 if (__get_IPSR() != 0) { // in ISR
mbed_official 49:77c8e4604045 1834 return sysMailAlloc(queue_id, millisec, 1, 0);
mbed_official 49:77c8e4604045 1835 } else { // in Thread
mbed_official 49:77c8e4604045 1836 return __sysMailAlloc(queue_id, millisec, 0, 0);
mbed_official 49:77c8e4604045 1837 }
mbed_official 49:77c8e4604045 1838 }
mbed_official 49:77c8e4604045 1839
mbed_official 49:77c8e4604045 1840 /// Allocate a memory block from a mail and set memory block to zero
mbed_official 49:77c8e4604045 1841 void *osMailCAlloc (osMailQId queue_id, uint32_t millisec) {
mbed_official 49:77c8e4604045 1842 if (__get_IPSR() != 0) { // in ISR
mbed_official 49:77c8e4604045 1843 return sysMailAlloc(queue_id, millisec, 1, 1);
mbed_official 49:77c8e4604045 1844 } else { // in Thread
mbed_official 49:77c8e4604045 1845 return __sysMailAlloc(queue_id, millisec, 0, 1);
mbed_official 49:77c8e4604045 1846 }
mbed_official 49:77c8e4604045 1847 }
mbed_official 49:77c8e4604045 1848
mbed_official 49:77c8e4604045 1849 /// Free a memory block from a mail
mbed_official 49:77c8e4604045 1850 osStatus osMailFree (osMailQId queue_id, void *mail) {
mbed_official 49:77c8e4604045 1851 if (__get_IPSR() != 0) { // in ISR
mbed_official 49:77c8e4604045 1852 return sysMailFree(queue_id, mail, 1);
mbed_official 49:77c8e4604045 1853 } else { // in Thread
mbed_official 49:77c8e4604045 1854 return __sysMailFree(queue_id, mail, 0);
mbed_official 49:77c8e4604045 1855 }
mbed_official 49:77c8e4604045 1856 }
mbed_official 49:77c8e4604045 1857
mbed_official 49:77c8e4604045 1858 /// Put a mail to a queue
mbed_official 49:77c8e4604045 1859 osStatus osMailPut (osMailQId queue_id, void *mail) {
mbed_official 49:77c8e4604045 1860 if (queue_id == NULL) return osErrorParameter;
mbed_official 49:77c8e4604045 1861 if (mail == NULL) return osErrorValue;
mbed_official 49:77c8e4604045 1862 return osMessagePut(*((void **)queue_id), (uint32_t)mail, 0);
mbed_official 49:77c8e4604045 1863 }
mbed_official 49:77c8e4604045 1864
mbed_official 70:3295e347fd88 1865 #ifdef __arm__
mbed_official 70:3295e347fd88 1866 #pragma push
mbed_official 70:3295e347fd88 1867 #pragma Ospace
mbed_official 70:3295e347fd88 1868 #endif // __arm__
mbed_official 49:77c8e4604045 1869 /// Get a mail from a queue
mbed_official 49:77c8e4604045 1870 os_InRegs osEvent osMailGet (osMailQId queue_id, uint32_t millisec) {
mbed_official 49:77c8e4604045 1871 osEvent ret;
mbed_official 49:77c8e4604045 1872
mbed_official 49:77c8e4604045 1873 if (queue_id == NULL) {
mbed_official 49:77c8e4604045 1874 ret.status = osErrorParameter;
mbed_official 49:77c8e4604045 1875 return ret;
mbed_official 49:77c8e4604045 1876 }
mbed_official 49:77c8e4604045 1877
mbed_official 49:77c8e4604045 1878 ret = osMessageGet(*((void **)queue_id), millisec);
mbed_official 49:77c8e4604045 1879 if (ret.status == osEventMessage) ret.status = osEventMail;
mbed_official 49:77c8e4604045 1880
mbed_official 49:77c8e4604045 1881 return ret;
mbed_official 49:77c8e4604045 1882 }
mbed_official 70:3295e347fd88 1883 #ifdef __arm__
mbed_official 70:3295e347fd88 1884 #pragma pop
mbed_official 70:3295e347fd88 1885 #endif // __arm__