Control a robot over the internet using UDP and a Wifly module (WiFi).

Dependencies:   Motor TextLCD WiflyInterface mbed-rtos mbed

Committer:
apatel336
Date:
Thu Oct 17 13:27:56 2013 +0000
Revision:
0:c0dc3a76f3d4
Initial Release

Who changed what in which revision?

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