Control a robot over the internet using UDP and a Ethernet interface.

Dependencies:   EthernetInterface Motor TextLCD mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
apatel336
Date:
Thu Oct 17 13:26:38 2013 +0000
Revision:
0:1496281373a5
Initial Release

Who changed what in which revision?

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