desp koval / mbed-rtos

Dependents:   Mecatro_Filtre

Committer:
mecatro_prod
Date:
Mon Mar 15 14:18:11 2021 +0000
Revision:
0:4a2c9c1f5b9e
Pas de changement

Who changed what in which revision?

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