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