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