Clone of mbed rtos 2 for easy accessibility.
Fork of mbed-rtos by
rtx/TARGET_CORTEX_M/rt_CMSIS.c@125:5713cbbdb706, 2017-07-04 (annotated)
- Committer:
- Kojto
- Date:
- Tue Jul 04 13:32:20 2017 +0100
- Revision:
- 125:5713cbbdb706
- Parent:
- 123:58563e6cba1e
replace mbed_rtx by mbed_rtx4
Not causing a conflict with mbed_rtx that is for newer rtos
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 49:77c8e4604045 | 1 | /*---------------------------------------------------------------------------- |
mbed_official | 112:53ace74b190c | 2 | * CMSIS-RTOS - 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 | 112:53ace74b190c | 6 | * Rev.: V4.80 |
mbed_official | 49:77c8e4604045 | 7 | *---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 8 | * |
mbed_official | 112:53ace74b190c | 9 | * Copyright (c) 1999-2009 KEIL, 2009-2015 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 | 114:031a41d65add | 51 | // This affects cmsis_os only, as it's not used anywhere else. This was left by kernel team |
mbed_official | 114:031a41d65add | 52 | // to suppress the warning in rt_tid2ptcb about incompatible pointer assignment. |
mbed_official | 114:031a41d65add | 53 | #define os_thread_cb OS_TCB |
mbed_official | 114:031a41d65add | 54 | |
mbed_official | 49:77c8e4604045 | 55 | #include "rt_TypeDef.h" |
mbed_official | 112:53ace74b190c | 56 | #include "RTX_Config.h" |
mbed_official | 49:77c8e4604045 | 57 | #include "rt_System.h" |
mbed_official | 49:77c8e4604045 | 58 | #include "rt_Task.h" |
mbed_official | 49:77c8e4604045 | 59 | #include "rt_Event.h" |
mbed_official | 49:77c8e4604045 | 60 | #include "rt_List.h" |
mbed_official | 49:77c8e4604045 | 61 | #include "rt_Time.h" |
mbed_official | 49:77c8e4604045 | 62 | #include "rt_Mutex.h" |
mbed_official | 49:77c8e4604045 | 63 | #include "rt_Semaphore.h" |
mbed_official | 49:77c8e4604045 | 64 | #include "rt_Mailbox.h" |
mbed_official | 49:77c8e4604045 | 65 | #include "rt_MemBox.h" |
mbed_official | 112:53ace74b190c | 66 | #include "rt_Memory.h" |
mbed_official | 49:77c8e4604045 | 67 | #include "rt_HAL_CM.h" |
Kojto | 118:6635230e06ba | 68 | #include "rt_OsEventObserver.h" |
mbed_official | 49:77c8e4604045 | 69 | |
mbed_official | 49:77c8e4604045 | 70 | #include "cmsis_os.h" |
mbed_official | 49:77c8e4604045 | 71 | |
mbed_official | 49:77c8e4604045 | 72 | #if (osFeature_Signals != 16) |
mbed_official | 49:77c8e4604045 | 73 | #error Invalid "osFeature_Signals" value! |
mbed_official | 49:77c8e4604045 | 74 | #endif |
mbed_official | 49:77c8e4604045 | 75 | #if (osFeature_Semaphore > 65535) |
mbed_official | 49:77c8e4604045 | 76 | #error Invalid "osFeature_Semaphore" value! |
mbed_official | 49:77c8e4604045 | 77 | #endif |
mbed_official | 49:77c8e4604045 | 78 | #if (osFeature_Wait != 0) |
mbed_official | 49:77c8e4604045 | 79 | #error osWait not supported! |
mbed_official | 49:77c8e4604045 | 80 | #endif |
mbed_official | 49:77c8e4604045 | 81 | |
mbed_official | 49:77c8e4604045 | 82 | |
mbed_official | 49:77c8e4604045 | 83 | // ==== Enumeration, structures, defines ==== |
mbed_official | 49:77c8e4604045 | 84 | |
mbed_official | 49:77c8e4604045 | 85 | // Service Calls defines |
mbed_official | 49:77c8e4604045 | 86 | |
mbed_official | 49:77c8e4604045 | 87 | #if defined (__CC_ARM) /* ARM Compiler */ |
mbed_official | 49:77c8e4604045 | 88 | |
mbed_official | 49:77c8e4604045 | 89 | #define __NO_RETURN __declspec(noreturn) |
mbed_official | 49:77c8e4604045 | 90 | |
mbed_official | 49:77c8e4604045 | 91 | #define osEvent_type osEvent |
mbed_official | 49:77c8e4604045 | 92 | #define osEvent_ret_status ret |
mbed_official | 49:77c8e4604045 | 93 | #define osEvent_ret_value ret |
mbed_official | 49:77c8e4604045 | 94 | #define osEvent_ret_msg ret |
mbed_official | 49:77c8e4604045 | 95 | #define osEvent_ret_mail ret |
mbed_official | 49:77c8e4604045 | 96 | |
mbed_official | 49:77c8e4604045 | 97 | #define osCallback_type osCallback |
mbed_official | 49:77c8e4604045 | 98 | #define osCallback_ret ret |
mbed_official | 49:77c8e4604045 | 99 | |
mbed_official | 49:77c8e4604045 | 100 | #define SVC_0_1(f,t,...) \ |
mbed_official | 49:77c8e4604045 | 101 | __svc_indirect(0) t _##f (t(*)()); \ |
mbed_official | 49:77c8e4604045 | 102 | t f (void); \ |
mbed_official | 49:77c8e4604045 | 103 | __attribute__((always_inline)) \ |
mbed_official | 49:77c8e4604045 | 104 | static __inline t __##f (void) { \ |
mbed_official | 49:77c8e4604045 | 105 | return _##f(f); \ |
mbed_official | 49:77c8e4604045 | 106 | } |
mbed_official | 49:77c8e4604045 | 107 | |
mbed_official | 112:53ace74b190c | 108 | #define SVC_1_0(f,t,t1,...) \ |
mbed_official | 112:53ace74b190c | 109 | __svc_indirect(0) t _##f (t(*)(t1),t1); \ |
mbed_official | 112:53ace74b190c | 110 | t f (t1 a1); \ |
mbed_official | 112:53ace74b190c | 111 | __attribute__((always_inline)) \ |
mbed_official | 112:53ace74b190c | 112 | static __inline t __##f (t1 a1) { \ |
mbed_official | 112:53ace74b190c | 113 | _##f(f,a1); \ |
mbed_official | 112:53ace74b190c | 114 | } |
mbed_official | 112:53ace74b190c | 115 | |
mbed_official | 49:77c8e4604045 | 116 | #define SVC_1_1(f,t,t1,...) \ |
mbed_official | 49:77c8e4604045 | 117 | __svc_indirect(0) t _##f (t(*)(t1),t1); \ |
mbed_official | 49:77c8e4604045 | 118 | t f (t1 a1); \ |
mbed_official | 49:77c8e4604045 | 119 | __attribute__((always_inline)) \ |
mbed_official | 49:77c8e4604045 | 120 | static __inline t __##f (t1 a1) { \ |
mbed_official | 49:77c8e4604045 | 121 | return _##f(f,a1); \ |
mbed_official | 49:77c8e4604045 | 122 | } |
mbed_official | 49:77c8e4604045 | 123 | |
mbed_official | 49:77c8e4604045 | 124 | #define SVC_2_1(f,t,t1,t2,...) \ |
mbed_official | 49:77c8e4604045 | 125 | __svc_indirect(0) t _##f (t(*)(t1,t2),t1,t2); \ |
mbed_official | 49:77c8e4604045 | 126 | t f (t1 a1, t2 a2); \ |
mbed_official | 49:77c8e4604045 | 127 | __attribute__((always_inline)) \ |
mbed_official | 49:77c8e4604045 | 128 | static __inline t __##f (t1 a1, t2 a2) { \ |
mbed_official | 49:77c8e4604045 | 129 | return _##f(f,a1,a2); \ |
mbed_official | 49:77c8e4604045 | 130 | } |
mbed_official | 49:77c8e4604045 | 131 | |
mbed_official | 49:77c8e4604045 | 132 | #define SVC_3_1(f,t,t1,t2,t3,...) \ |
mbed_official | 49:77c8e4604045 | 133 | __svc_indirect(0) t _##f (t(*)(t1,t2,t3),t1,t2,t3); \ |
mbed_official | 49:77c8e4604045 | 134 | t f (t1 a1, t2 a2, t3 a3); \ |
mbed_official | 49:77c8e4604045 | 135 | __attribute__((always_inline)) \ |
mbed_official | 49:77c8e4604045 | 136 | static __inline t __##f (t1 a1, t2 a2, t3 a3) { \ |
mbed_official | 49:77c8e4604045 | 137 | return _##f(f,a1,a2,a3); \ |
mbed_official | 49:77c8e4604045 | 138 | } |
mbed_official | 49:77c8e4604045 | 139 | |
mbed_official | 49:77c8e4604045 | 140 | #define SVC_4_1(f,t,t1,t2,t3,t4,...) \ |
mbed_official | 49:77c8e4604045 | 141 | __svc_indirect(0) t _##f (t(*)(t1,t2,t3,t4),t1,t2,t3,t4); \ |
mbed_official | 49:77c8e4604045 | 142 | t f (t1 a1, t2 a2, t3 a3, t4 a4); \ |
mbed_official | 49:77c8e4604045 | 143 | __attribute__((always_inline)) \ |
mbed_official | 49:77c8e4604045 | 144 | static __inline t __##f (t1 a1, t2 a2, t3 a3, t4 a4) { \ |
mbed_official | 49:77c8e4604045 | 145 | return _##f(f,a1,a2,a3,a4); \ |
mbed_official | 49:77c8e4604045 | 146 | } |
mbed_official | 49:77c8e4604045 | 147 | |
mbed_official | 112:53ace74b190c | 148 | #define SVC_1_2 SVC_1_1 |
mbed_official | 112:53ace74b190c | 149 | #define SVC_1_3 SVC_1_1 |
mbed_official | 112:53ace74b190c | 150 | #define SVC_2_3 SVC_2_1 |
mbed_official | 49:77c8e4604045 | 151 | |
mbed_official | 49:77c8e4604045 | 152 | #elif defined (__GNUC__) /* GNU Compiler */ |
mbed_official | 49:77c8e4604045 | 153 | |
mbed_official | 49:77c8e4604045 | 154 | #define __NO_RETURN __attribute__((noreturn)) |
mbed_official | 49:77c8e4604045 | 155 | |
mbed_official | 49:77c8e4604045 | 156 | typedef uint32_t __attribute__((vector_size(8))) ret64; |
mbed_official | 49:77c8e4604045 | 157 | typedef uint32_t __attribute__((vector_size(16))) ret128; |
mbed_official | 49:77c8e4604045 | 158 | |
mbed_official | 49:77c8e4604045 | 159 | #define RET_pointer __r0 |
mbed_official | 49:77c8e4604045 | 160 | #define RET_int32_t __r0 |
mbed_official | 112:53ace74b190c | 161 | #define RET_uint32_t __r0 |
mbed_official | 49:77c8e4604045 | 162 | #define RET_osStatus __r0 |
mbed_official | 49:77c8e4604045 | 163 | #define RET_osPriority __r0 |
mbed_official | 49:77c8e4604045 | 164 | #define RET_osEvent {(osStatus)__r0, {(uint32_t)__r1}, {(void *)__r2}} |
mbed_official | 49:77c8e4604045 | 165 | #define RET_osCallback {(void *)__r0, (void *)__r1} |
mbed_official | 49:77c8e4604045 | 166 | |
mbed_official | 112:53ace74b190c | 167 | #define osEvent_type __attribute__((pcs("aapcs"))) ret128 |
mbed_official | 49:77c8e4604045 | 168 | #define osEvent_ret_status (ret128){ret.status} |
mbed_official | 49:77c8e4604045 | 169 | #define osEvent_ret_value (ret128){ret.status, ret.value.v} |
mbed_official | 49:77c8e4604045 | 170 | #define osEvent_ret_msg (ret128){ret.status, ret.value.v, (uint32_t)ret.def.message_id} |
mbed_official | 49:77c8e4604045 | 171 | #define osEvent_ret_mail (ret128){ret.status, ret.value.v, (uint32_t)ret.def.mail_id} |
mbed_official | 49:77c8e4604045 | 172 | |
mbed_official | 112:53ace74b190c | 173 | #define osCallback_type __attribute__((pcs("aapcs"))) ret64 |
mbed_official | 49:77c8e4604045 | 174 | #define osCallback_ret (ret64) {(uint32_t)ret.fp, (uint32_t)ret.arg} |
mbed_official | 49:77c8e4604045 | 175 | |
mbed_official | 49:77c8e4604045 | 176 | #define SVC_ArgN(n) \ |
mbed_official | 49:77c8e4604045 | 177 | register int __r##n __asm("r"#n); |
mbed_official | 49:77c8e4604045 | 178 | |
mbed_official | 49:77c8e4604045 | 179 | #define SVC_ArgR(n,t,a) \ |
mbed_official | 49:77c8e4604045 | 180 | register t __r##n __asm("r"#n) = a; |
mbed_official | 49:77c8e4604045 | 181 | |
mbed_official | 49:77c8e4604045 | 182 | #define SVC_Arg0() \ |
mbed_official | 49:77c8e4604045 | 183 | SVC_ArgN(0) \ |
mbed_official | 49:77c8e4604045 | 184 | SVC_ArgN(1) \ |
mbed_official | 49:77c8e4604045 | 185 | SVC_ArgN(2) \ |
mbed_official | 49:77c8e4604045 | 186 | SVC_ArgN(3) |
mbed_official | 49:77c8e4604045 | 187 | |
mbed_official | 49:77c8e4604045 | 188 | #define SVC_Arg1(t1) \ |
mbed_official | 49:77c8e4604045 | 189 | SVC_ArgR(0,t1,a1) \ |
mbed_official | 49:77c8e4604045 | 190 | SVC_ArgN(1) \ |
mbed_official | 49:77c8e4604045 | 191 | SVC_ArgN(2) \ |
mbed_official | 49:77c8e4604045 | 192 | SVC_ArgN(3) |
mbed_official | 49:77c8e4604045 | 193 | |
mbed_official | 49:77c8e4604045 | 194 | #define SVC_Arg2(t1,t2) \ |
mbed_official | 49:77c8e4604045 | 195 | SVC_ArgR(0,t1,a1) \ |
mbed_official | 49:77c8e4604045 | 196 | SVC_ArgR(1,t2,a2) \ |
mbed_official | 49:77c8e4604045 | 197 | SVC_ArgN(2) \ |
mbed_official | 49:77c8e4604045 | 198 | SVC_ArgN(3) |
mbed_official | 49:77c8e4604045 | 199 | |
mbed_official | 49:77c8e4604045 | 200 | #define SVC_Arg3(t1,t2,t3) \ |
mbed_official | 49:77c8e4604045 | 201 | SVC_ArgR(0,t1,a1) \ |
mbed_official | 49:77c8e4604045 | 202 | SVC_ArgR(1,t2,a2) \ |
mbed_official | 49:77c8e4604045 | 203 | SVC_ArgR(2,t3,a3) \ |
mbed_official | 49:77c8e4604045 | 204 | SVC_ArgN(3) |
mbed_official | 49:77c8e4604045 | 205 | |
mbed_official | 49:77c8e4604045 | 206 | #define SVC_Arg4(t1,t2,t3,t4) \ |
mbed_official | 49:77c8e4604045 | 207 | SVC_ArgR(0,t1,a1) \ |
mbed_official | 49:77c8e4604045 | 208 | SVC_ArgR(1,t2,a2) \ |
mbed_official | 49:77c8e4604045 | 209 | SVC_ArgR(2,t3,a3) \ |
mbed_official | 49:77c8e4604045 | 210 | SVC_ArgR(3,t4,a4) |
mbed_official | 49:77c8e4604045 | 211 | |
mbed_official | 49:77c8e4604045 | 212 | #if (defined (__CORTEX_M0)) || defined (__CORTEX_M0PLUS) |
mbed_official | 49:77c8e4604045 | 213 | #define SVC_Call(f) \ |
mbed_official | 112:53ace74b190c | 214 | __asm volatile \ |
mbed_official | 49:77c8e4604045 | 215 | ( \ |
mbed_official | 49:77c8e4604045 | 216 | "ldr r7,="#f"\n\t" \ |
mbed_official | 49:77c8e4604045 | 217 | "mov r12,r7\n\t" \ |
mbed_official | 49:77c8e4604045 | 218 | "svc 0" \ |
mbed_official | 49:77c8e4604045 | 219 | : "=r" (__r0), "=r" (__r1), "=r" (__r2), "=r" (__r3) \ |
mbed_official | 49:77c8e4604045 | 220 | : "r" (__r0), "r" (__r1), "r" (__r2), "r" (__r3) \ |
mbed_official | 49:77c8e4604045 | 221 | : "r7", "r12", "lr", "cc" \ |
mbed_official | 49:77c8e4604045 | 222 | ); |
mbed_official | 49:77c8e4604045 | 223 | #else |
mbed_official | 49:77c8e4604045 | 224 | #define SVC_Call(f) \ |
mbed_official | 112:53ace74b190c | 225 | __asm volatile \ |
mbed_official | 49:77c8e4604045 | 226 | ( \ |
mbed_official | 49:77c8e4604045 | 227 | "ldr r12,="#f"\n\t" \ |
mbed_official | 49:77c8e4604045 | 228 | "svc 0" \ |
mbed_official | 49:77c8e4604045 | 229 | : "=r" (__r0), "=r" (__r1), "=r" (__r2), "=r" (__r3) \ |
mbed_official | 49:77c8e4604045 | 230 | : "r" (__r0), "r" (__r1), "r" (__r2), "r" (__r3) \ |
mbed_official | 49:77c8e4604045 | 231 | : "r12", "lr", "cc" \ |
mbed_official | 49:77c8e4604045 | 232 | ); |
mbed_official | 49:77c8e4604045 | 233 | #endif |
mbed_official | 49:77c8e4604045 | 234 | |
mbed_official | 49:77c8e4604045 | 235 | #define SVC_0_1(f,t,rv) \ |
mbed_official | 49:77c8e4604045 | 236 | __attribute__((always_inline)) \ |
mbed_official | 49:77c8e4604045 | 237 | static inline t __##f (void) { \ |
mbed_official | 49:77c8e4604045 | 238 | SVC_Arg0(); \ |
mbed_official | 49:77c8e4604045 | 239 | SVC_Call(f); \ |
mbed_official | 49:77c8e4604045 | 240 | return (t) rv; \ |
mbed_official | 49:77c8e4604045 | 241 | } |
mbed_official | 49:77c8e4604045 | 242 | |
mbed_official | 112:53ace74b190c | 243 | #define SVC_1_0(f,t,t1) \ |
mbed_official | 112:53ace74b190c | 244 | __attribute__((always_inline)) \ |
mbed_official | 112:53ace74b190c | 245 | static inline t __##f (t1 a1) { \ |
mbed_official | 112:53ace74b190c | 246 | SVC_Arg1(t1); \ |
mbed_official | 112:53ace74b190c | 247 | SVC_Call(f); \ |
mbed_official | 112:53ace74b190c | 248 | } |
mbed_official | 112:53ace74b190c | 249 | |
mbed_official | 49:77c8e4604045 | 250 | #define SVC_1_1(f,t,t1,rv) \ |
mbed_official | 49:77c8e4604045 | 251 | __attribute__((always_inline)) \ |
mbed_official | 49:77c8e4604045 | 252 | static inline t __##f (t1 a1) { \ |
mbed_official | 49:77c8e4604045 | 253 | SVC_Arg1(t1); \ |
mbed_official | 49:77c8e4604045 | 254 | SVC_Call(f); \ |
mbed_official | 49:77c8e4604045 | 255 | return (t) rv; \ |
mbed_official | 49:77c8e4604045 | 256 | } |
mbed_official | 49:77c8e4604045 | 257 | |
mbed_official | 49:77c8e4604045 | 258 | #define SVC_2_1(f,t,t1,t2,rv) \ |
mbed_official | 49:77c8e4604045 | 259 | __attribute__((always_inline)) \ |
mbed_official | 49:77c8e4604045 | 260 | static inline t __##f (t1 a1, t2 a2) { \ |
mbed_official | 49:77c8e4604045 | 261 | SVC_Arg2(t1,t2); \ |
mbed_official | 49:77c8e4604045 | 262 | SVC_Call(f); \ |
mbed_official | 49:77c8e4604045 | 263 | return (t) rv; \ |
mbed_official | 49:77c8e4604045 | 264 | } |
mbed_official | 49:77c8e4604045 | 265 | |
mbed_official | 49:77c8e4604045 | 266 | #define SVC_3_1(f,t,t1,t2,t3,rv) \ |
mbed_official | 49:77c8e4604045 | 267 | __attribute__((always_inline)) \ |
mbed_official | 49:77c8e4604045 | 268 | static inline t __##f (t1 a1, t2 a2, t3 a3) { \ |
mbed_official | 49:77c8e4604045 | 269 | SVC_Arg3(t1,t2,t3); \ |
mbed_official | 49:77c8e4604045 | 270 | SVC_Call(f); \ |
mbed_official | 49:77c8e4604045 | 271 | return (t) rv; \ |
mbed_official | 49:77c8e4604045 | 272 | } |
mbed_official | 49:77c8e4604045 | 273 | |
mbed_official | 49:77c8e4604045 | 274 | #define SVC_4_1(f,t,t1,t2,t3,t4,rv) \ |
mbed_official | 49:77c8e4604045 | 275 | __attribute__((always_inline)) \ |
mbed_official | 49:77c8e4604045 | 276 | static inline t __##f (t1 a1, t2 a2, t3 a3, t4 a4) { \ |
mbed_official | 49:77c8e4604045 | 277 | SVC_Arg4(t1,t2,t3,t4); \ |
mbed_official | 49:77c8e4604045 | 278 | SVC_Call(f); \ |
mbed_official | 49:77c8e4604045 | 279 | return (t) rv; \ |
mbed_official | 49:77c8e4604045 | 280 | } |
mbed_official | 49:77c8e4604045 | 281 | |
mbed_official | 112:53ace74b190c | 282 | #define SVC_1_2 SVC_1_1 |
mbed_official | 112:53ace74b190c | 283 | #define SVC_1_3 SVC_1_1 |
mbed_official | 112:53ace74b190c | 284 | #define SVC_2_3 SVC_2_1 |
mbed_official | 49:77c8e4604045 | 285 | |
mbed_official | 49:77c8e4604045 | 286 | #elif defined (__ICCARM__) /* IAR Compiler */ |
mbed_official | 49:77c8e4604045 | 287 | |
mbed_official | 49:77c8e4604045 | 288 | #define __NO_RETURN __noreturn |
mbed_official | 49:77c8e4604045 | 289 | |
mbed_official | 49:77c8e4604045 | 290 | #define osEvent_type osEvent |
mbed_official | 49:77c8e4604045 | 291 | #define osEvent_ret_status ret |
mbed_official | 49:77c8e4604045 | 292 | #define osEvent_ret_value ret |
mbed_official | 49:77c8e4604045 | 293 | #define osEvent_ret_msg ret |
mbed_official | 49:77c8e4604045 | 294 | #define osEvent_ret_mail ret |
mbed_official | 49:77c8e4604045 | 295 | |
mbed_official | 53:c35dab33c427 | 296 | #define osCallback_type osCallback |
mbed_official | 53:c35dab33c427 | 297 | #define osCallback_ret ret |
mbed_official | 53:c35dab33c427 | 298 | |
mbed_official | 53:c35dab33c427 | 299 | #define RET_osEvent osEvent |
mbed_official | 53:c35dab33c427 | 300 | #define RET_osCallback osCallback |
mbed_official | 49:77c8e4604045 | 301 | |
mbed_official | 49:77c8e4604045 | 302 | #define SVC_Setup(f) \ |
mbed_official | 53:c35dab33c427 | 303 | __asm( \ |
mbed_official | 49:77c8e4604045 | 304 | "mov r12,%0\n" \ |
Kojto | 118:6635230e06ba | 305 | :: "r"(&f): "r0", "r1", "r2", "r3", "r12" \ |
mbed_official | 49:77c8e4604045 | 306 | ); |
mbed_official | 49:77c8e4604045 | 307 | |
mbed_official | 112:53ace74b190c | 308 | #define SVC_Ret3() \ |
mbed_official | 112:53ace74b190c | 309 | __asm( \ |
mbed_official | 112:53ace74b190c | 310 | "ldr r0,[sp,#0]\n" \ |
mbed_official | 112:53ace74b190c | 311 | "ldr r1,[sp,#4]\n" \ |
mbed_official | 112:53ace74b190c | 312 | "ldr r2,[sp,#8]\n" \ |
mbed_official | 112:53ace74b190c | 313 | ); |
mbed_official | 49:77c8e4604045 | 314 | |
mbed_official | 49:77c8e4604045 | 315 | #define SVC_0_1(f,t,...) \ |
mbed_official | 49:77c8e4604045 | 316 | t f (void); \ |
mbed_official | 49:77c8e4604045 | 317 | _Pragma("swi_number=0") __swi t _##f (void); \ |
mbed_official | 49:77c8e4604045 | 318 | static inline t __##f (void) { \ |
mbed_official | 49:77c8e4604045 | 319 | SVC_Setup(f); \ |
mbed_official | 49:77c8e4604045 | 320 | return _##f(); \ |
mbed_official | 49:77c8e4604045 | 321 | } |
mbed_official | 49:77c8e4604045 | 322 | |
mbed_official | 112:53ace74b190c | 323 | #define SVC_1_0(f,t,t1,...) \ |
mbed_official | 112:53ace74b190c | 324 | t f (t1 a1); \ |
mbed_official | 112:53ace74b190c | 325 | _Pragma("swi_number=0") __swi t _##f (t1 a1); \ |
mbed_official | 112:53ace74b190c | 326 | static inline t __##f (t1 a1) { \ |
mbed_official | 112:53ace74b190c | 327 | SVC_Setup(f); \ |
mbed_official | 112:53ace74b190c | 328 | _##f(a1); \ |
mbed_official | 112:53ace74b190c | 329 | } |
mbed_official | 112:53ace74b190c | 330 | |
mbed_official | 49:77c8e4604045 | 331 | #define SVC_1_1(f,t,t1,...) \ |
mbed_official | 49:77c8e4604045 | 332 | t f (t1 a1); \ |
mbed_official | 49:77c8e4604045 | 333 | _Pragma("swi_number=0") __swi t _##f (t1 a1); \ |
mbed_official | 49:77c8e4604045 | 334 | static inline t __##f (t1 a1) { \ |
mbed_official | 49:77c8e4604045 | 335 | SVC_Setup(f); \ |
mbed_official | 49:77c8e4604045 | 336 | return _##f(a1); \ |
mbed_official | 49:77c8e4604045 | 337 | } |
mbed_official | 49:77c8e4604045 | 338 | |
mbed_official | 49:77c8e4604045 | 339 | #define SVC_2_1(f,t,t1,t2,...) \ |
mbed_official | 49:77c8e4604045 | 340 | t f (t1 a1, t2 a2); \ |
mbed_official | 49:77c8e4604045 | 341 | _Pragma("swi_number=0") __swi t _##f (t1 a1, t2 a2); \ |
mbed_official | 49:77c8e4604045 | 342 | static inline t __##f (t1 a1, t2 a2) { \ |
mbed_official | 49:77c8e4604045 | 343 | SVC_Setup(f); \ |
mbed_official | 49:77c8e4604045 | 344 | return _##f(a1,a2); \ |
mbed_official | 49:77c8e4604045 | 345 | } |
mbed_official | 49:77c8e4604045 | 346 | |
mbed_official | 49:77c8e4604045 | 347 | #define SVC_3_1(f,t,t1,t2,t3,...) \ |
mbed_official | 49:77c8e4604045 | 348 | t f (t1 a1, t2 a2, t3 a3); \ |
mbed_official | 49:77c8e4604045 | 349 | _Pragma("swi_number=0") __swi t _##f (t1 a1, t2 a2, t3 a3); \ |
mbed_official | 49:77c8e4604045 | 350 | static inline t __##f (t1 a1, t2 a2, t3 a3) { \ |
mbed_official | 49:77c8e4604045 | 351 | SVC_Setup(f); \ |
mbed_official | 49:77c8e4604045 | 352 | return _##f(a1,a2,a3); \ |
mbed_official | 49:77c8e4604045 | 353 | } |
mbed_official | 49:77c8e4604045 | 354 | |
mbed_official | 49:77c8e4604045 | 355 | #define SVC_4_1(f,t,t1,t2,t3,t4,...) \ |
mbed_official | 49:77c8e4604045 | 356 | t f (t1 a1, t2 a2, t3 a3, t4 a4); \ |
mbed_official | 49:77c8e4604045 | 357 | _Pragma("swi_number=0") __swi t _##f (t1 a1, t2 a2, t3 a3, t4 a4); \ |
mbed_official | 49:77c8e4604045 | 358 | static inline t __##f (t1 a1, t2 a2, t3 a3, t4 a4) { \ |
mbed_official | 49:77c8e4604045 | 359 | SVC_Setup(f); \ |
mbed_official | 49:77c8e4604045 | 360 | return _##f(a1,a2,a3,a4); \ |
mbed_official | 49:77c8e4604045 | 361 | } |
mbed_official | 49:77c8e4604045 | 362 | |
mbed_official | 53:c35dab33c427 | 363 | #define SVC_1_2 SVC_1_1 |
mbed_official | 53:c35dab33c427 | 364 | #define SVC_1_3 SVC_1_1 |
mbed_official | 53:c35dab33c427 | 365 | #define SVC_2_3 SVC_2_1 |
mbed_official | 49:77c8e4604045 | 366 | |
mbed_official | 49:77c8e4604045 | 367 | #endif |
mbed_official | 49:77c8e4604045 | 368 | |
mbed_official | 49:77c8e4604045 | 369 | |
mbed_official | 49:77c8e4604045 | 370 | // Callback structure |
mbed_official | 49:77c8e4604045 | 371 | typedef struct { |
mbed_official | 49:77c8e4604045 | 372 | void *fp; // Function pointer |
mbed_official | 49:77c8e4604045 | 373 | void *arg; // Function argument |
mbed_official | 49:77c8e4604045 | 374 | } osCallback; |
mbed_official | 49:77c8e4604045 | 375 | |
mbed_official | 49:77c8e4604045 | 376 | |
mbed_official | 49:77c8e4604045 | 377 | // OS Section definitions |
mbed_official | 49:77c8e4604045 | 378 | #ifdef OS_SECTIONS_LINK_INFO |
mbed_official | 49:77c8e4604045 | 379 | extern const uint32_t os_section_id$$Base; |
mbed_official | 49:77c8e4604045 | 380 | extern const uint32_t os_section_id$$Limit; |
mbed_official | 49:77c8e4604045 | 381 | #endif |
mbed_official | 49:77c8e4604045 | 382 | |
mbed_official | 112:53ace74b190c | 383 | #ifndef __MBED_CMSIS_RTOS_CM |
mbed_official | 112:53ace74b190c | 384 | // OS Stack Memory for Threads definitions |
mbed_official | 112:53ace74b190c | 385 | extern uint64_t os_stack_mem[]; |
mbed_official | 112:53ace74b190c | 386 | extern const uint32_t os_stack_sz; |
mbed_official | 112:53ace74b190c | 387 | #endif |
mbed_official | 112:53ace74b190c | 388 | |
mbed_official | 49:77c8e4604045 | 389 | // OS Timers external resources |
mbed_official | 112:53ace74b190c | 390 | extern const osThreadDef_t os_thread_def_osTimerThread; |
mbed_official | 112:53ace74b190c | 391 | extern osThreadId osThreadId_osTimerThread; |
mbed_official | 112:53ace74b190c | 392 | extern const osMessageQDef_t os_messageQ_def_osTimerMessageQ; |
mbed_official | 112:53ace74b190c | 393 | extern osMessageQId osMessageQId_osTimerMessageQ; |
mbed_official | 49:77c8e4604045 | 394 | |
c1728p9 | 123:58563e6cba1e | 395 | // Thread creation and destruction |
c1728p9 | 123:58563e6cba1e | 396 | osMutexDef(osThreadMutex); |
c1728p9 | 123:58563e6cba1e | 397 | osMutexId osMutexId_osThreadMutex; |
c1728p9 | 123:58563e6cba1e | 398 | void sysThreadTerminate(osThreadId id); |
mbed_official | 49:77c8e4604045 | 399 | |
mbed_official | 49:77c8e4604045 | 400 | // ==== Helper Functions ==== |
mbed_official | 49:77c8e4604045 | 401 | |
mbed_official | 49:77c8e4604045 | 402 | /// Convert timeout in millisec to system ticks |
mbed_official | 112:53ace74b190c | 403 | static uint16_t rt_ms2tick (uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 404 | uint32_t tick; |
mbed_official | 49:77c8e4604045 | 405 | |
mbed_official | 112:53ace74b190c | 406 | if (millisec == 0U) { return 0x0U; } // No timeout |
mbed_official | 112:53ace74b190c | 407 | if (millisec == osWaitForever) { return 0xFFFFU; } // Indefinite timeout |
mbed_official | 112:53ace74b190c | 408 | if (millisec > 4000000U) { return 0xFFFEU; } // Max ticks supported |
mbed_official | 49:77c8e4604045 | 409 | |
mbed_official | 112:53ace74b190c | 410 | tick = ((1000U * millisec) + os_clockrate - 1U) / os_clockrate; |
mbed_official | 112:53ace74b190c | 411 | if (tick > 0xFFFEU) { return 0xFFFEU; } |
mbed_official | 112:53ace74b190c | 412 | |
mbed_official | 112:53ace74b190c | 413 | return (uint16_t)tick; |
mbed_official | 49:77c8e4604045 | 414 | } |
mbed_official | 49:77c8e4604045 | 415 | |
mbed_official | 49:77c8e4604045 | 416 | /// Convert Thread ID to TCB pointer |
mbed_official | 112:53ace74b190c | 417 | P_TCB rt_tid2ptcb (osThreadId thread_id) { |
mbed_official | 49:77c8e4604045 | 418 | P_TCB ptcb; |
mbed_official | 49:77c8e4604045 | 419 | |
mbed_official | 112:53ace74b190c | 420 | if (thread_id == NULL) { return NULL; } |
mbed_official | 49:77c8e4604045 | 421 | |
mbed_official | 112:53ace74b190c | 422 | if ((uint32_t)thread_id & 3U) { return NULL; } |
mbed_official | 49:77c8e4604045 | 423 | |
mbed_official | 49:77c8e4604045 | 424 | #ifdef OS_SECTIONS_LINK_INFO |
mbed_official | 112:53ace74b190c | 425 | if ((os_section_id$$Base != 0U) && (os_section_id$$Limit != 0U)) { |
mbed_official | 112:53ace74b190c | 426 | if (thread_id < (osThreadId)os_section_id$$Base) { return NULL; } |
mbed_official | 112:53ace74b190c | 427 | if (thread_id >= (osThreadId)os_section_id$$Limit) { return NULL; } |
mbed_official | 49:77c8e4604045 | 428 | } |
mbed_official | 49:77c8e4604045 | 429 | #endif |
mbed_official | 49:77c8e4604045 | 430 | |
mbed_official | 49:77c8e4604045 | 431 | ptcb = thread_id; |
mbed_official | 49:77c8e4604045 | 432 | |
mbed_official | 112:53ace74b190c | 433 | if (ptcb->cb_type != TCB) { return NULL; } |
mbed_official | 49:77c8e4604045 | 434 | |
mbed_official | 49:77c8e4604045 | 435 | return ptcb; |
mbed_official | 49:77c8e4604045 | 436 | } |
mbed_official | 49:77c8e4604045 | 437 | |
mbed_official | 49:77c8e4604045 | 438 | /// Convert ID pointer to Object pointer |
mbed_official | 49:77c8e4604045 | 439 | static void *rt_id2obj (void *id) { |
mbed_official | 49:77c8e4604045 | 440 | |
mbed_official | 112:53ace74b190c | 441 | if ((uint32_t)id & 3U) { return NULL; } |
mbed_official | 49:77c8e4604045 | 442 | |
mbed_official | 49:77c8e4604045 | 443 | #ifdef OS_SECTIONS_LINK_INFO |
mbed_official | 112:53ace74b190c | 444 | if ((os_section_id$$Base != 0U) && (os_section_id$$Limit != 0U)) { |
mbed_official | 112:53ace74b190c | 445 | if (id < (void *)os_section_id$$Base) { return NULL; } |
mbed_official | 112:53ace74b190c | 446 | if (id >= (void *)os_section_id$$Limit) { return NULL; } |
mbed_official | 49:77c8e4604045 | 447 | } |
mbed_official | 49:77c8e4604045 | 448 | #endif |
mbed_official | 49:77c8e4604045 | 449 | |
mbed_official | 49:77c8e4604045 | 450 | return id; |
mbed_official | 49:77c8e4604045 | 451 | } |
mbed_official | 49:77c8e4604045 | 452 | |
mbed_official | 49:77c8e4604045 | 453 | |
mbed_official | 49:77c8e4604045 | 454 | // ==== Kernel Control ==== |
mbed_official | 49:77c8e4604045 | 455 | |
mbed_official | 49:77c8e4604045 | 456 | uint8_t os_initialized; // Kernel Initialized flag |
mbed_official | 49:77c8e4604045 | 457 | uint8_t os_running; // Kernel Running flag |
mbed_official | 49:77c8e4604045 | 458 | |
mbed_official | 49:77c8e4604045 | 459 | // Kernel Control Service Calls declarations |
mbed_official | 49:77c8e4604045 | 460 | SVC_0_1(svcKernelInitialize, osStatus, RET_osStatus) |
mbed_official | 49:77c8e4604045 | 461 | SVC_0_1(svcKernelStart, osStatus, RET_osStatus) |
mbed_official | 49:77c8e4604045 | 462 | SVC_0_1(svcKernelRunning, int32_t, RET_int32_t) |
mbed_official | 112:53ace74b190c | 463 | SVC_0_1(svcKernelSysTick, uint32_t, RET_uint32_t) |
mbed_official | 49:77c8e4604045 | 464 | |
mbed_official | 112:53ace74b190c | 465 | static void sysThreadError (osStatus status); |
Kojto | 118:6635230e06ba | 466 | osThreadId svcThreadCreate (const osThreadDef_t *thread_def, void *argument, void *context); |
mbed_official | 112:53ace74b190c | 467 | osMessageQId svcMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id); |
mbed_official | 49:77c8e4604045 | 468 | |
mbed_official | 49:77c8e4604045 | 469 | // Kernel Control Service Calls |
mbed_official | 49:77c8e4604045 | 470 | |
mbed_official | 49:77c8e4604045 | 471 | /// Initialize the RTOS Kernel for creating objects |
mbed_official | 49:77c8e4604045 | 472 | osStatus svcKernelInitialize (void) { |
mbed_official | 112:53ace74b190c | 473 | #ifdef __MBED_CMSIS_RTOS_CM |
mbed_official | 112:53ace74b190c | 474 | if (!os_initialized) { |
mbed_official | 112:53ace74b190c | 475 | rt_sys_init(); // RTX System Initialization |
mbed_official | 112:53ace74b190c | 476 | } |
mbed_official | 112:53ace74b190c | 477 | #else |
mbed_official | 112:53ace74b190c | 478 | uint32_t ret; |
mbed_official | 112:53ace74b190c | 479 | |
mbed_official | 112:53ace74b190c | 480 | if (os_initialized == 0U) { |
mbed_official | 49:77c8e4604045 | 481 | |
mbed_official | 112:53ace74b190c | 482 | // Init Thread Stack Memory (must be 8-byte aligned) |
mbed_official | 112:53ace74b190c | 483 | if (((uint32_t)os_stack_mem & 7U) != 0U) { return osErrorNoMemory; } |
mbed_official | 112:53ace74b190c | 484 | ret = rt_init_mem(os_stack_mem, os_stack_sz); |
mbed_official | 112:53ace74b190c | 485 | if (ret != 0U) { return osErrorNoMemory; } |
mbed_official | 112:53ace74b190c | 486 | |
mbed_official | 112:53ace74b190c | 487 | rt_sys_init(); // RTX System Initialization |
mbed_official | 112:53ace74b190c | 488 | } |
mbed_official | 112:53ace74b190c | 489 | #endif |
mbed_official | 112:53ace74b190c | 490 | |
mbed_official | 112:53ace74b190c | 491 | os_tsk.run->prio = 255U; // Highest priority |
mbed_official | 112:53ace74b190c | 492 | |
mbed_official | 112:53ace74b190c | 493 | if (os_initialized == 0U) { |
mbed_official | 112:53ace74b190c | 494 | // Create OS Timers resources (Message Queue & Thread) |
mbed_official | 112:53ace74b190c | 495 | osMessageQId_osTimerMessageQ = svcMessageCreate (&os_messageQ_def_osTimerMessageQ, NULL); |
Kojto | 118:6635230e06ba | 496 | osThreadId_osTimerThread = svcThreadCreate(&os_thread_def_osTimerThread, NULL, NULL); |
c1728p9 | 123:58563e6cba1e | 497 | // Initialize thread mutex |
c1728p9 | 123:58563e6cba1e | 498 | osMutexId_osThreadMutex = osMutexCreate(osMutex(osThreadMutex)); |
mbed_official | 112:53ace74b190c | 499 | } |
mbed_official | 49:77c8e4604045 | 500 | |
mbed_official | 49:77c8e4604045 | 501 | sysThreadError(osOK); |
mbed_official | 49:77c8e4604045 | 502 | |
mbed_official | 112:53ace74b190c | 503 | os_initialized = 1U; |
mbed_official | 112:53ace74b190c | 504 | os_running = 0U; |
mbed_official | 49:77c8e4604045 | 505 | |
mbed_official | 49:77c8e4604045 | 506 | return osOK; |
mbed_official | 49:77c8e4604045 | 507 | } |
mbed_official | 49:77c8e4604045 | 508 | |
mbed_official | 49:77c8e4604045 | 509 | /// Start the RTOS Kernel |
mbed_official | 49:77c8e4604045 | 510 | osStatus svcKernelStart (void) { |
mbed_official | 49:77c8e4604045 | 511 | |
mbed_official | 112:53ace74b190c | 512 | if (os_running) { return osOK; } |
mbed_official | 49:77c8e4604045 | 513 | |
mbed_official | 112:53ace74b190c | 514 | rt_tsk_prio(0U, os_tsk.run->prio_base); // Restore priority |
mbed_official | 112:53ace74b190c | 515 | if (os_tsk.run->task_id == 0xFFU) { // Idle Thread |
mbed_official | 112:53ace74b190c | 516 | __set_PSP(os_tsk.run->tsk_stack + (8U*4U)); // Setup PSP |
mbed_official | 112:53ace74b190c | 517 | } |
mbed_official | 112:53ace74b190c | 518 | if (os_tsk.new_tsk == NULL) { // Force context switch |
mbed_official | 112:53ace74b190c | 519 | os_tsk.new_tsk = os_tsk.run; |
mbed_official | 112:53ace74b190c | 520 | os_tsk.run = NULL; |
mbed_official | 112:53ace74b190c | 521 | } |
mbed_official | 49:77c8e4604045 | 522 | |
mbed_official | 49:77c8e4604045 | 523 | rt_sys_start(); |
mbed_official | 49:77c8e4604045 | 524 | |
mbed_official | 112:53ace74b190c | 525 | os_running = 1U; |
mbed_official | 49:77c8e4604045 | 526 | |
mbed_official | 49:77c8e4604045 | 527 | return osOK; |
mbed_official | 49:77c8e4604045 | 528 | } |
mbed_official | 49:77c8e4604045 | 529 | |
mbed_official | 49:77c8e4604045 | 530 | /// Check if the RTOS kernel is already started |
mbed_official | 112:53ace74b190c | 531 | int32_t svcKernelRunning (void) { |
mbed_official | 112:53ace74b190c | 532 | return (int32_t)os_running; |
mbed_official | 112:53ace74b190c | 533 | } |
mbed_official | 112:53ace74b190c | 534 | |
mbed_official | 112:53ace74b190c | 535 | /// Get the RTOS kernel system timer counter |
mbed_official | 112:53ace74b190c | 536 | uint32_t svcKernelSysTick (void) { |
mbed_official | 112:53ace74b190c | 537 | uint32_t tick, tick0; |
mbed_official | 112:53ace74b190c | 538 | |
mbed_official | 112:53ace74b190c | 539 | tick = os_tick_val(); |
mbed_official | 112:53ace74b190c | 540 | if (os_tick_ovf()) { |
mbed_official | 112:53ace74b190c | 541 | tick0 = os_tick_val(); |
mbed_official | 112:53ace74b190c | 542 | if (tick0 < tick) { tick = tick0; } |
mbed_official | 112:53ace74b190c | 543 | tick += (os_trv + 1U) * (os_time + 1U); |
mbed_official | 112:53ace74b190c | 544 | } else { |
mbed_official | 112:53ace74b190c | 545 | tick += (os_trv + 1U) * os_time; |
mbed_official | 112:53ace74b190c | 546 | } |
mbed_official | 112:53ace74b190c | 547 | |
mbed_official | 112:53ace74b190c | 548 | return tick; |
mbed_official | 49:77c8e4604045 | 549 | } |
mbed_official | 49:77c8e4604045 | 550 | |
mbed_official | 49:77c8e4604045 | 551 | // Kernel Control Public API |
mbed_official | 49:77c8e4604045 | 552 | |
mbed_official | 49:77c8e4604045 | 553 | /// Initialize the RTOS Kernel for creating objects |
mbed_official | 49:77c8e4604045 | 554 | osStatus osKernelInitialize (void) { |
c1728p9 | 123:58563e6cba1e | 555 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 556 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 557 | } |
mbed_official | 112:53ace74b190c | 558 | if ((__get_CONTROL() & 1U) == 0U) { // Privileged mode |
mbed_official | 49:77c8e4604045 | 559 | return svcKernelInitialize(); |
mbed_official | 49:77c8e4604045 | 560 | } else { |
mbed_official | 49:77c8e4604045 | 561 | return __svcKernelInitialize(); |
mbed_official | 49:77c8e4604045 | 562 | } |
mbed_official | 49:77c8e4604045 | 563 | } |
mbed_official | 49:77c8e4604045 | 564 | |
mbed_official | 49:77c8e4604045 | 565 | /// Start the RTOS Kernel |
mbed_official | 49:77c8e4604045 | 566 | osStatus osKernelStart (void) { |
mbed_official | 49:77c8e4604045 | 567 | uint32_t stack[8]; |
mbed_official | 49:77c8e4604045 | 568 | |
c1728p9 | 123:58563e6cba1e | 569 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 570 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 571 | } |
Kojto | 118:6635230e06ba | 572 | |
Kojto | 118:6635230e06ba | 573 | /* Call the pre-start event (from unprivileged mode) if the handler exists |
Kojto | 118:6635230e06ba | 574 | * and the kernel is not running. */ |
Kojto | 118:6635230e06ba | 575 | /* FIXME osEventObs needs to be readable but not writable from unprivileged |
Kojto | 118:6635230e06ba | 576 | * code. */ |
Kojto | 118:6635230e06ba | 577 | if (!osKernelRunning() && osEventObs && osEventObs->pre_start) { |
Kojto | 118:6635230e06ba | 578 | osEventObs->pre_start(); |
Kojto | 118:6635230e06ba | 579 | } |
Kojto | 118:6635230e06ba | 580 | |
mbed_official | 112:53ace74b190c | 581 | switch (__get_CONTROL() & 0x03U) { |
mbed_official | 112:53ace74b190c | 582 | case 0x00U: // Privileged Thread mode & MSP |
mbed_official | 49:77c8e4604045 | 583 | __set_PSP((uint32_t)(stack + 8)); // Initial PSP |
mbed_official | 112:53ace74b190c | 584 | if (os_flags & 1U) { |
mbed_official | 112:53ace74b190c | 585 | __set_CONTROL(0x02U); // Set Privileged Thread mode & PSP |
mbed_official | 49:77c8e4604045 | 586 | } else { |
mbed_official | 112:53ace74b190c | 587 | __set_CONTROL(0x03U); // Set Unprivileged Thread mode & PSP |
mbed_official | 49:77c8e4604045 | 588 | } |
mbed_official | 49:77c8e4604045 | 589 | __DSB(); |
mbed_official | 49:77c8e4604045 | 590 | __ISB(); |
mbed_official | 49:77c8e4604045 | 591 | break; |
mbed_official | 112:53ace74b190c | 592 | case 0x01U: // Unprivileged Thread mode & MSP |
mbed_official | 49:77c8e4604045 | 593 | return osErrorOS; |
mbed_official | 112:53ace74b190c | 594 | case 0x02U: // Privileged Thread mode & PSP |
mbed_official | 112:53ace74b190c | 595 | if ((os_flags & 1U) == 0U) { // Unprivileged Thread mode requested |
mbed_official | 112:53ace74b190c | 596 | __set_CONTROL(0x03U); // Set Unprivileged Thread mode & PSP |
mbed_official | 49:77c8e4604045 | 597 | __DSB(); |
mbed_official | 49:77c8e4604045 | 598 | __ISB(); |
mbed_official | 49:77c8e4604045 | 599 | } |
mbed_official | 49:77c8e4604045 | 600 | break; |
mbed_official | 112:53ace74b190c | 601 | case 0x03U: // Unprivileged Thread mode & PSP |
mbed_official | 112:53ace74b190c | 602 | if (os_flags & 1U) { return osErrorOS; } // Privileged Thread mode requested |
mbed_official | 49:77c8e4604045 | 603 | break; |
mbed_official | 49:77c8e4604045 | 604 | } |
mbed_official | 49:77c8e4604045 | 605 | return __svcKernelStart(); |
mbed_official | 49:77c8e4604045 | 606 | } |
mbed_official | 49:77c8e4604045 | 607 | |
mbed_official | 49:77c8e4604045 | 608 | /// Check if the RTOS kernel is already started |
mbed_official | 112:53ace74b190c | 609 | int32_t osKernelRunning (void) { |
c1728p9 | 123:58563e6cba1e | 610 | if ((__get_PRIMASK() != 0U || __get_IPSR() != 0U) || ((__get_CONTROL() & 1U) == 0U)) { |
mbed_official | 49:77c8e4604045 | 611 | // in ISR or Privileged |
mbed_official | 112:53ace74b190c | 612 | return (int32_t)os_running; |
mbed_official | 49:77c8e4604045 | 613 | } else { |
mbed_official | 49:77c8e4604045 | 614 | return __svcKernelRunning(); |
mbed_official | 49:77c8e4604045 | 615 | } |
mbed_official | 49:77c8e4604045 | 616 | } |
mbed_official | 49:77c8e4604045 | 617 | |
mbed_official | 112:53ace74b190c | 618 | /// Get the RTOS kernel system timer counter |
mbed_official | 112:53ace74b190c | 619 | uint32_t osKernelSysTick (void) { |
c1728p9 | 123:58563e6cba1e | 620 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { return 0U; } // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 621 | return __svcKernelSysTick(); |
mbed_official | 112:53ace74b190c | 622 | } |
mbed_official | 112:53ace74b190c | 623 | |
mbed_official | 49:77c8e4604045 | 624 | |
mbed_official | 49:77c8e4604045 | 625 | // ==== Thread Management ==== |
mbed_official | 49:77c8e4604045 | 626 | |
mbed_official | 112:53ace74b190c | 627 | /// Set Thread Error (for Create functions which return IDs) |
mbed_official | 112:53ace74b190c | 628 | static void sysThreadError (osStatus status) { |
mbed_official | 112:53ace74b190c | 629 | // To Do |
mbed_official | 112:53ace74b190c | 630 | } |
mbed_official | 112:53ace74b190c | 631 | |
mbed_official | 49:77c8e4604045 | 632 | __NO_RETURN void osThreadExit (void); |
mbed_official | 49:77c8e4604045 | 633 | |
mbed_official | 49:77c8e4604045 | 634 | // Thread Service Calls declarations |
Kojto | 118:6635230e06ba | 635 | SVC_3_1(svcThreadCreate, osThreadId, const osThreadDef_t *, void *, void *, RET_pointer) |
mbed_official | 112:53ace74b190c | 636 | SVC_0_1(svcThreadGetId, osThreadId, RET_pointer) |
mbed_official | 112:53ace74b190c | 637 | SVC_1_1(svcThreadTerminate, osStatus, osThreadId, RET_osStatus) |
mbed_official | 112:53ace74b190c | 638 | SVC_0_1(svcThreadYield, osStatus, RET_osStatus) |
mbed_official | 112:53ace74b190c | 639 | SVC_2_1(svcThreadSetPriority, osStatus, osThreadId, osPriority, RET_osStatus) |
mbed_official | 112:53ace74b190c | 640 | SVC_1_1(svcThreadGetPriority, osPriority, osThreadId, RET_osPriority) |
c1728p9 | 123:58563e6cba1e | 641 | SVC_2_3(svcThreadGetInfo, os_InRegs osEvent, osThreadId, osThreadInfo, RET_osEvent) |
mbed_official | 49:77c8e4604045 | 642 | |
mbed_official | 49:77c8e4604045 | 643 | // Thread Service Calls |
mbed_official | 49:77c8e4604045 | 644 | |
mbed_official | 49:77c8e4604045 | 645 | /// Create a thread and add it to Active Threads and set it to state READY |
Kojto | 118:6635230e06ba | 646 | osThreadId svcThreadCreate (const osThreadDef_t *thread_def, void *argument, void *context) { |
mbed_official | 49:77c8e4604045 | 647 | P_TCB ptcb; |
mbed_official | 112:53ace74b190c | 648 | OS_TID tsk; |
mbed_official | 112:53ace74b190c | 649 | void *stk; |
mbed_official | 49:77c8e4604045 | 650 | |
mbed_official | 49:77c8e4604045 | 651 | if ((thread_def == NULL) || |
mbed_official | 49:77c8e4604045 | 652 | (thread_def->pthread == NULL) || |
mbed_official | 49:77c8e4604045 | 653 | (thread_def->tpriority < osPriorityIdle) || |
mbed_official | 112:53ace74b190c | 654 | (thread_def->tpriority > osPriorityRealtime)) { |
mbed_official | 112:53ace74b190c | 655 | sysThreadError(osErrorParameter); |
mbed_official | 112:53ace74b190c | 656 | return NULL; |
mbed_official | 112:53ace74b190c | 657 | } |
mbed_official | 112:53ace74b190c | 658 | |
mbed_official | 112:53ace74b190c | 659 | #ifdef __MBED_CMSIS_RTOS_CM |
mbed_official | 112:53ace74b190c | 660 | if (thread_def->stacksize != 0) { // Custom stack size |
mbed_official | 112:53ace74b190c | 661 | stk = (void *)thread_def->stack_pointer; |
mbed_official | 112:53ace74b190c | 662 | } else { // Default stack size |
mbed_official | 112:53ace74b190c | 663 | stk = NULL; |
mbed_official | 112:53ace74b190c | 664 | } |
mbed_official | 112:53ace74b190c | 665 | #else |
mbed_official | 112:53ace74b190c | 666 | if (thread_def->stacksize != 0) { // Custom stack size |
mbed_official | 112:53ace74b190c | 667 | stk = rt_alloc_mem( // Allocate stack |
mbed_official | 112:53ace74b190c | 668 | os_stack_mem, |
mbed_official | 112:53ace74b190c | 669 | thread_def->stacksize |
mbed_official | 112:53ace74b190c | 670 | ); |
mbed_official | 112:53ace74b190c | 671 | if (stk == NULL) { |
mbed_official | 112:53ace74b190c | 672 | sysThreadError(osErrorNoMemory); // Out of memory |
mbed_official | 112:53ace74b190c | 673 | return NULL; |
mbed_official | 112:53ace74b190c | 674 | } |
mbed_official | 112:53ace74b190c | 675 | } else { // Default stack size |
mbed_official | 112:53ace74b190c | 676 | stk = NULL; |
mbed_official | 112:53ace74b190c | 677 | } |
mbed_official | 112:53ace74b190c | 678 | #endif |
mbed_official | 112:53ace74b190c | 679 | |
mbed_official | 112:53ace74b190c | 680 | tsk = rt_tsk_create( // Create task |
mbed_official | 112:53ace74b190c | 681 | (FUNCP)thread_def->pthread, // Task function pointer |
mbed_official | 112:53ace74b190c | 682 | (uint32_t) |
mbed_official | 112:53ace74b190c | 683 | (thread_def->tpriority-osPriorityIdle+1) | // Task priority |
mbed_official | 112:53ace74b190c | 684 | (thread_def->stacksize << 8), // Task stack size in bytes |
mbed_official | 112:53ace74b190c | 685 | stk, // Pointer to task's stack |
mbed_official | 112:53ace74b190c | 686 | argument // Argument to the task |
mbed_official | 112:53ace74b190c | 687 | ); |
mbed_official | 112:53ace74b190c | 688 | |
mbed_official | 112:53ace74b190c | 689 | if (tsk == 0U) { // Invalid task ID |
mbed_official | 112:53ace74b190c | 690 | #ifndef __MBED_CMSIS_RTOS_CM |
mbed_official | 112:53ace74b190c | 691 | if (stk != NULL) { |
mbed_official | 112:53ace74b190c | 692 | rt_free_mem(os_stack_mem, stk); // Free allocated stack |
mbed_official | 112:53ace74b190c | 693 | } |
mbed_official | 112:53ace74b190c | 694 | #endif |
mbed_official | 112:53ace74b190c | 695 | sysThreadError(osErrorNoMemory); // Create task failed (Out of memory) |
mbed_official | 49:77c8e4604045 | 696 | return NULL; |
mbed_official | 49:77c8e4604045 | 697 | } |
mbed_official | 49:77c8e4604045 | 698 | |
mbed_official | 112:53ace74b190c | 699 | ptcb = (P_TCB)os_active_TCB[tsk - 1U]; // TCB pointer |
mbed_official | 49:77c8e4604045 | 700 | |
mbed_official | 49:77c8e4604045 | 701 | *((uint32_t *)ptcb->tsk_stack + 13) = (uint32_t)osThreadExit; |
mbed_official | 49:77c8e4604045 | 702 | |
Kojto | 118:6635230e06ba | 703 | if (osEventObs && osEventObs->thread_create) { |
Kojto | 118:6635230e06ba | 704 | ptcb->context = osEventObs->thread_create(ptcb->task_id, context); |
Kojto | 118:6635230e06ba | 705 | } else { |
Kojto | 118:6635230e06ba | 706 | ptcb->context = context; |
Kojto | 118:6635230e06ba | 707 | } |
Kojto | 118:6635230e06ba | 708 | |
mbed_official | 49:77c8e4604045 | 709 | return ptcb; |
mbed_official | 49:77c8e4604045 | 710 | } |
mbed_official | 49:77c8e4604045 | 711 | |
mbed_official | 49:77c8e4604045 | 712 | /// Return the thread ID of the current running thread |
mbed_official | 49:77c8e4604045 | 713 | osThreadId svcThreadGetId (void) { |
mbed_official | 49:77c8e4604045 | 714 | OS_TID tsk; |
mbed_official | 49:77c8e4604045 | 715 | |
mbed_official | 49:77c8e4604045 | 716 | tsk = rt_tsk_self(); |
mbed_official | 112:53ace74b190c | 717 | if (tsk == 0U) { return NULL; } |
mbed_official | 112:53ace74b190c | 718 | return (P_TCB)os_active_TCB[tsk - 1U]; |
mbed_official | 49:77c8e4604045 | 719 | } |
mbed_official | 49:77c8e4604045 | 720 | |
mbed_official | 49:77c8e4604045 | 721 | /// Terminate execution of a thread and remove it from ActiveThreads |
mbed_official | 49:77c8e4604045 | 722 | osStatus svcThreadTerminate (osThreadId thread_id) { |
mbed_official | 49:77c8e4604045 | 723 | OS_RESULT res; |
mbed_official | 49:77c8e4604045 | 724 | P_TCB ptcb; |
mbed_official | 112:53ace74b190c | 725 | #ifndef __MBED_CMSIS_RTOS_CM |
mbed_official | 112:53ace74b190c | 726 | void *stk; |
mbed_official | 112:53ace74b190c | 727 | #endif |
mbed_official | 49:77c8e4604045 | 728 | |
mbed_official | 49:77c8e4604045 | 729 | ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer |
mbed_official | 112:53ace74b190c | 730 | if (ptcb == NULL) { |
mbed_official | 112:53ace74b190c | 731 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 732 | } |
mbed_official | 112:53ace74b190c | 733 | |
mbed_official | 112:53ace74b190c | 734 | #ifndef __MBED_CMSIS_RTOS_CM |
mbed_official | 112:53ace74b190c | 735 | stk = ptcb->priv_stack ? ptcb->stack : NULL; // Private stack |
mbed_official | 112:53ace74b190c | 736 | #endif |
mbed_official | 49:77c8e4604045 | 737 | |
Kojto | 118:6635230e06ba | 738 | if (osEventObs && osEventObs->thread_destroy) { |
Kojto | 118:6635230e06ba | 739 | osEventObs->thread_destroy(ptcb->context); |
Kojto | 118:6635230e06ba | 740 | } |
Kojto | 118:6635230e06ba | 741 | |
mbed_official | 49:77c8e4604045 | 742 | res = rt_tsk_delete(ptcb->task_id); // Delete task |
mbed_official | 49:77c8e4604045 | 743 | |
mbed_official | 112:53ace74b190c | 744 | if (res == OS_R_NOK) { |
mbed_official | 112:53ace74b190c | 745 | return osErrorResource; // Delete task failed |
mbed_official | 112:53ace74b190c | 746 | } |
mbed_official | 112:53ace74b190c | 747 | |
mbed_official | 112:53ace74b190c | 748 | #ifndef __MBED_CMSIS_RTOS_CM |
mbed_official | 112:53ace74b190c | 749 | if (stk != NULL) { |
mbed_official | 112:53ace74b190c | 750 | rt_free_mem(os_stack_mem, stk); // Free private stack |
mbed_official | 112:53ace74b190c | 751 | } |
mbed_official | 112:53ace74b190c | 752 | #endif |
mbed_official | 49:77c8e4604045 | 753 | |
mbed_official | 49:77c8e4604045 | 754 | return osOK; |
mbed_official | 49:77c8e4604045 | 755 | } |
mbed_official | 49:77c8e4604045 | 756 | |
mbed_official | 49:77c8e4604045 | 757 | /// Pass control to next thread that is in state READY |
mbed_official | 49:77c8e4604045 | 758 | osStatus svcThreadYield (void) { |
mbed_official | 49:77c8e4604045 | 759 | rt_tsk_pass(); // Pass control to next task |
mbed_official | 49:77c8e4604045 | 760 | return osOK; |
mbed_official | 49:77c8e4604045 | 761 | } |
mbed_official | 49:77c8e4604045 | 762 | |
mbed_official | 49:77c8e4604045 | 763 | /// Change priority of an active thread |
mbed_official | 49:77c8e4604045 | 764 | osStatus svcThreadSetPriority (osThreadId thread_id, osPriority priority) { |
mbed_official | 49:77c8e4604045 | 765 | OS_RESULT res; |
mbed_official | 49:77c8e4604045 | 766 | P_TCB ptcb; |
mbed_official | 49:77c8e4604045 | 767 | |
mbed_official | 49:77c8e4604045 | 768 | ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer |
mbed_official | 112:53ace74b190c | 769 | if (ptcb == NULL) { |
mbed_official | 112:53ace74b190c | 770 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 771 | } |
mbed_official | 49:77c8e4604045 | 772 | |
mbed_official | 49:77c8e4604045 | 773 | if ((priority < osPriorityIdle) || (priority > osPriorityRealtime)) { |
mbed_official | 49:77c8e4604045 | 774 | return osErrorValue; |
mbed_official | 49:77c8e4604045 | 775 | } |
mbed_official | 49:77c8e4604045 | 776 | |
mbed_official | 49:77c8e4604045 | 777 | res = rt_tsk_prio( // Change task priority |
mbed_official | 49:77c8e4604045 | 778 | ptcb->task_id, // Task ID |
mbed_official | 112:53ace74b190c | 779 | (uint8_t)(priority - osPriorityIdle + 1) // New task priority |
mbed_official | 49:77c8e4604045 | 780 | ); |
mbed_official | 49:77c8e4604045 | 781 | |
mbed_official | 112:53ace74b190c | 782 | if (res == OS_R_NOK) { |
mbed_official | 112:53ace74b190c | 783 | return osErrorResource; // Change task priority failed |
mbed_official | 112:53ace74b190c | 784 | } |
mbed_official | 49:77c8e4604045 | 785 | |
mbed_official | 49:77c8e4604045 | 786 | return osOK; |
mbed_official | 49:77c8e4604045 | 787 | } |
mbed_official | 49:77c8e4604045 | 788 | |
mbed_official | 49:77c8e4604045 | 789 | /// Get current priority of an active thread |
mbed_official | 49:77c8e4604045 | 790 | osPriority svcThreadGetPriority (osThreadId thread_id) { |
mbed_official | 49:77c8e4604045 | 791 | P_TCB ptcb; |
mbed_official | 49:77c8e4604045 | 792 | |
mbed_official | 49:77c8e4604045 | 793 | ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer |
mbed_official | 112:53ace74b190c | 794 | if (ptcb == NULL) { |
mbed_official | 112:53ace74b190c | 795 | return osPriorityError; |
mbed_official | 112:53ace74b190c | 796 | } |
mbed_official | 49:77c8e4604045 | 797 | |
mbed_official | 112:53ace74b190c | 798 | return (osPriority)(ptcb->prio - 1 + osPriorityIdle); |
mbed_official | 49:77c8e4604045 | 799 | } |
mbed_official | 49:77c8e4604045 | 800 | |
c1728p9 | 123:58563e6cba1e | 801 | /// Get info from an active thread |
c1728p9 | 123:58563e6cba1e | 802 | os_InRegs osEvent_type svcThreadGetInfo (osThreadId thread_id, osThreadInfo info) { |
c1728p9 | 123:58563e6cba1e | 803 | P_TCB ptcb; |
c1728p9 | 123:58563e6cba1e | 804 | osEvent ret; |
c1728p9 | 123:58563e6cba1e | 805 | ret.status = osOK; |
c1728p9 | 123:58563e6cba1e | 806 | |
c1728p9 | 123:58563e6cba1e | 807 | ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer |
c1728p9 | 123:58563e6cba1e | 808 | if (ptcb == NULL) { |
c1728p9 | 123:58563e6cba1e | 809 | ret.status = osErrorValue; |
c1728p9 | 123:58563e6cba1e | 810 | return osEvent_ret_status; |
c1728p9 | 123:58563e6cba1e | 811 | } |
c1728p9 | 123:58563e6cba1e | 812 | |
c1728p9 | 123:58563e6cba1e | 813 | if (osThreadInfoStackSize == info) { |
c1728p9 | 123:58563e6cba1e | 814 | uint32_t size; |
c1728p9 | 123:58563e6cba1e | 815 | size = ptcb->priv_stack; |
c1728p9 | 123:58563e6cba1e | 816 | if (0 == size) { |
c1728p9 | 123:58563e6cba1e | 817 | // This is an OS task - always a fixed size |
c1728p9 | 123:58563e6cba1e | 818 | size = os_stackinfo & 0x3FFFF; |
c1728p9 | 123:58563e6cba1e | 819 | } |
c1728p9 | 123:58563e6cba1e | 820 | ret.value.v = size; |
c1728p9 | 123:58563e6cba1e | 821 | return osEvent_ret_value; |
c1728p9 | 123:58563e6cba1e | 822 | } |
c1728p9 | 123:58563e6cba1e | 823 | |
c1728p9 | 123:58563e6cba1e | 824 | if (osThreadInfoStackMax == info) { |
c1728p9 | 123:58563e6cba1e | 825 | uint32_t i; |
c1728p9 | 123:58563e6cba1e | 826 | uint32_t *stack_ptr; |
c1728p9 | 123:58563e6cba1e | 827 | uint32_t stack_size; |
c1728p9 | 123:58563e6cba1e | 828 | if (!(os_stackinfo & (1 << 28))) { |
c1728p9 | 123:58563e6cba1e | 829 | // Stack init must be turned on for max stack usage |
c1728p9 | 123:58563e6cba1e | 830 | ret.status = osErrorResource; |
c1728p9 | 123:58563e6cba1e | 831 | return osEvent_ret_status; |
c1728p9 | 123:58563e6cba1e | 832 | } |
c1728p9 | 123:58563e6cba1e | 833 | stack_ptr = (uint32_t*)ptcb->stack; |
c1728p9 | 123:58563e6cba1e | 834 | stack_size = ptcb->priv_stack; |
c1728p9 | 123:58563e6cba1e | 835 | if (0 == stack_size) { |
c1728p9 | 123:58563e6cba1e | 836 | // This is an OS task - always a fixed size |
c1728p9 | 123:58563e6cba1e | 837 | stack_size = os_stackinfo & 0x3FFFF; |
c1728p9 | 123:58563e6cba1e | 838 | } |
c1728p9 | 123:58563e6cba1e | 839 | for (i = 1; i <stack_size / 4; i++) { |
c1728p9 | 123:58563e6cba1e | 840 | if (stack_ptr[i] != MAGIC_PATTERN) { |
c1728p9 | 123:58563e6cba1e | 841 | break; |
c1728p9 | 123:58563e6cba1e | 842 | } |
c1728p9 | 123:58563e6cba1e | 843 | } |
c1728p9 | 123:58563e6cba1e | 844 | ret.value.v = stack_size - i * 4; |
c1728p9 | 123:58563e6cba1e | 845 | return osEvent_ret_value; |
c1728p9 | 123:58563e6cba1e | 846 | } |
c1728p9 | 123:58563e6cba1e | 847 | |
c1728p9 | 123:58563e6cba1e | 848 | if (osThreadInfoEntry == info) { |
c1728p9 | 123:58563e6cba1e | 849 | ret.value.p = (void*)ptcb->ptask; |
c1728p9 | 123:58563e6cba1e | 850 | return osEvent_ret_value; |
c1728p9 | 123:58563e6cba1e | 851 | } |
c1728p9 | 123:58563e6cba1e | 852 | |
c1728p9 | 123:58563e6cba1e | 853 | if (osThreadInfoArg == info) { |
c1728p9 | 123:58563e6cba1e | 854 | ret.value.p = (void*)ptcb->argv; |
c1728p9 | 123:58563e6cba1e | 855 | return osEvent_ret_value; |
c1728p9 | 123:58563e6cba1e | 856 | } |
c1728p9 | 123:58563e6cba1e | 857 | |
c1728p9 | 123:58563e6cba1e | 858 | // Unsupported option so return error |
c1728p9 | 123:58563e6cba1e | 859 | ret.status = osErrorParameter; |
c1728p9 | 123:58563e6cba1e | 860 | return osEvent_ret_status; |
c1728p9 | 123:58563e6cba1e | 861 | } |
mbed_official | 49:77c8e4604045 | 862 | |
mbed_official | 49:77c8e4604045 | 863 | // Thread Public API |
mbed_official | 49:77c8e4604045 | 864 | |
mbed_official | 49:77c8e4604045 | 865 | /// Create a thread and add it to Active Threads and set it to state READY |
mbed_official | 112:53ace74b190c | 866 | osThreadId osThreadCreate (const osThreadDef_t *thread_def, void *argument) { |
Kojto | 118:6635230e06ba | 867 | return osThreadContextCreate(thread_def, argument, NULL); |
Kojto | 118:6635230e06ba | 868 | } |
Kojto | 118:6635230e06ba | 869 | osThreadId osThreadContextCreate (const osThreadDef_t *thread_def, void *argument, void *context) { |
c1728p9 | 123:58563e6cba1e | 870 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 871 | return NULL; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 872 | } |
mbed_official | 112:53ace74b190c | 873 | if (((__get_CONTROL() & 1U) == 0U) && (os_running == 0U)) { |
mbed_official | 49:77c8e4604045 | 874 | // Privileged and not running |
Kojto | 118:6635230e06ba | 875 | return svcThreadCreate(thread_def, argument, context); |
mbed_official | 49:77c8e4604045 | 876 | } else { |
c1728p9 | 123:58563e6cba1e | 877 | osThreadId id; |
c1728p9 | 123:58563e6cba1e | 878 | osMutexWait(osMutexId_osThreadMutex, osWaitForever); |
c1728p9 | 123:58563e6cba1e | 879 | // Thread mutex must be held when a thread is created or terminated |
c1728p9 | 123:58563e6cba1e | 880 | id = __svcThreadCreate(thread_def, argument, context); |
c1728p9 | 123:58563e6cba1e | 881 | osMutexRelease(osMutexId_osThreadMutex); |
c1728p9 | 123:58563e6cba1e | 882 | return id; |
mbed_official | 49:77c8e4604045 | 883 | } |
mbed_official | 49:77c8e4604045 | 884 | } |
mbed_official | 49:77c8e4604045 | 885 | |
mbed_official | 49:77c8e4604045 | 886 | /// Return the thread ID of the current running thread |
mbed_official | 49:77c8e4604045 | 887 | osThreadId osThreadGetId (void) { |
c1728p9 | 123:58563e6cba1e | 888 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 889 | return NULL; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 890 | } |
mbed_official | 49:77c8e4604045 | 891 | return __svcThreadGetId(); |
mbed_official | 49:77c8e4604045 | 892 | } |
mbed_official | 49:77c8e4604045 | 893 | |
mbed_official | 49:77c8e4604045 | 894 | /// Terminate execution of a thread and remove it from ActiveThreads |
mbed_official | 49:77c8e4604045 | 895 | osStatus osThreadTerminate (osThreadId thread_id) { |
c1728p9 | 123:58563e6cba1e | 896 | osStatus status; |
c1728p9 | 123:58563e6cba1e | 897 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 898 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 899 | } |
c1728p9 | 123:58563e6cba1e | 900 | osMutexWait(osMutexId_osThreadMutex, osWaitForever); |
c1728p9 | 123:58563e6cba1e | 901 | sysThreadTerminate(thread_id); |
c1728p9 | 123:58563e6cba1e | 902 | // Thread mutex must be held when a thread is created or terminated |
c1728p9 | 123:58563e6cba1e | 903 | status = __svcThreadTerminate(thread_id); |
c1728p9 | 123:58563e6cba1e | 904 | osMutexRelease(osMutexId_osThreadMutex); |
c1728p9 | 123:58563e6cba1e | 905 | return status; |
mbed_official | 49:77c8e4604045 | 906 | } |
mbed_official | 49:77c8e4604045 | 907 | |
mbed_official | 49:77c8e4604045 | 908 | /// Pass control to next thread that is in state READY |
mbed_official | 49:77c8e4604045 | 909 | osStatus osThreadYield (void) { |
c1728p9 | 123:58563e6cba1e | 910 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 911 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 912 | } |
mbed_official | 49:77c8e4604045 | 913 | return __svcThreadYield(); |
mbed_official | 49:77c8e4604045 | 914 | } |
mbed_official | 49:77c8e4604045 | 915 | |
mbed_official | 49:77c8e4604045 | 916 | /// Change priority of an active thread |
mbed_official | 49:77c8e4604045 | 917 | osStatus osThreadSetPriority (osThreadId thread_id, osPriority priority) { |
c1728p9 | 123:58563e6cba1e | 918 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 919 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 920 | } |
mbed_official | 49:77c8e4604045 | 921 | return __svcThreadSetPriority(thread_id, priority); |
mbed_official | 49:77c8e4604045 | 922 | } |
mbed_official | 49:77c8e4604045 | 923 | |
mbed_official | 49:77c8e4604045 | 924 | /// Get current priority of an active thread |
mbed_official | 49:77c8e4604045 | 925 | osPriority osThreadGetPriority (osThreadId thread_id) { |
c1728p9 | 123:58563e6cba1e | 926 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 927 | return osPriorityError; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 928 | } |
mbed_official | 49:77c8e4604045 | 929 | return __svcThreadGetPriority(thread_id); |
mbed_official | 49:77c8e4604045 | 930 | } |
mbed_official | 49:77c8e4604045 | 931 | |
mbed_official | 49:77c8e4604045 | 932 | /// INTERNAL - Not Public |
mbed_official | 49:77c8e4604045 | 933 | /// Auto Terminate Thread on exit (used implicitly when thread exists) |
c1728p9 | 123:58563e6cba1e | 934 | __NO_RETURN void osThreadExit (void) { |
c1728p9 | 123:58563e6cba1e | 935 | osThreadId id; |
c1728p9 | 123:58563e6cba1e | 936 | // Thread mutex must be held when a thread is created or terminated |
c1728p9 | 123:58563e6cba1e | 937 | // Note - the mutex will be released automatically by the os when |
c1728p9 | 123:58563e6cba1e | 938 | // the thread is terminated |
c1728p9 | 123:58563e6cba1e | 939 | osMutexWait(osMutexId_osThreadMutex, osWaitForever); |
c1728p9 | 123:58563e6cba1e | 940 | id = __svcThreadGetId(); |
c1728p9 | 123:58563e6cba1e | 941 | sysThreadTerminate(id); |
c1728p9 | 123:58563e6cba1e | 942 | __svcThreadTerminate(id); |
mbed_official | 49:77c8e4604045 | 943 | for (;;); // Should never come here |
mbed_official | 49:77c8e4604045 | 944 | } |
mbed_official | 49:77c8e4604045 | 945 | |
mbed_official | 112:53ace74b190c | 946 | #ifdef __MBED_CMSIS_RTOS_CM |
mbed_official | 112:53ace74b190c | 947 | /// Get current thread state |
mbed_official | 112:53ace74b190c | 948 | uint8_t osThreadGetState (osThreadId thread_id) { |
mbed_official | 112:53ace74b190c | 949 | P_TCB ptcb; |
mbed_official | 112:53ace74b190c | 950 | |
c1728p9 | 123:58563e6cba1e | 951 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 952 | |
mbed_official | 112:53ace74b190c | 953 | ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer |
mbed_official | 117:4c105b8d7cae | 954 | if (ptcb == NULL) return INACTIVE; |
mbed_official | 112:53ace74b190c | 955 | |
mbed_official | 112:53ace74b190c | 956 | return ptcb->state; |
mbed_official | 112:53ace74b190c | 957 | } |
mbed_official | 112:53ace74b190c | 958 | #endif |
mbed_official | 49:77c8e4604045 | 959 | |
c1728p9 | 123:58563e6cba1e | 960 | /// Get the requested info from the specified active thread |
c1728p9 | 123:58563e6cba1e | 961 | os_InRegs osEvent _osThreadGetInfo(osThreadId thread_id, osThreadInfo info) { |
c1728p9 | 123:58563e6cba1e | 962 | osEvent ret; |
c1728p9 | 123:58563e6cba1e | 963 | |
c1728p9 | 123:58563e6cba1e | 964 | if (__get_IPSR() != 0U) { // Not allowed in ISR |
c1728p9 | 123:58563e6cba1e | 965 | ret.status = osErrorISR; |
c1728p9 | 123:58563e6cba1e | 966 | return ret; |
c1728p9 | 123:58563e6cba1e | 967 | } |
c1728p9 | 123:58563e6cba1e | 968 | return __svcThreadGetInfo(thread_id, info); |
c1728p9 | 123:58563e6cba1e | 969 | } |
c1728p9 | 123:58563e6cba1e | 970 | |
c1728p9 | 123:58563e6cba1e | 971 | osThreadEnumId _osThreadsEnumStart() { |
c1728p9 | 123:58563e6cba1e | 972 | static uint32_t thread_enum_index; |
c1728p9 | 123:58563e6cba1e | 973 | osMutexWait(osMutexId_osThreadMutex, osWaitForever); |
c1728p9 | 123:58563e6cba1e | 974 | thread_enum_index = 0; |
c1728p9 | 123:58563e6cba1e | 975 | return &thread_enum_index; |
c1728p9 | 123:58563e6cba1e | 976 | } |
c1728p9 | 123:58563e6cba1e | 977 | |
c1728p9 | 123:58563e6cba1e | 978 | osThreadId _osThreadEnumNext(osThreadEnumId enum_id) { |
c1728p9 | 123:58563e6cba1e | 979 | uint32_t i; |
c1728p9 | 123:58563e6cba1e | 980 | osThreadId id = NULL; |
c1728p9 | 123:58563e6cba1e | 981 | uint32_t *index = (uint32_t*)enum_id; |
c1728p9 | 123:58563e6cba1e | 982 | for (i = *index; i < os_maxtaskrun; i++) { |
c1728p9 | 123:58563e6cba1e | 983 | if (os_active_TCB[i] != NULL) { |
c1728p9 | 123:58563e6cba1e | 984 | id = (osThreadId)os_active_TCB[i]; |
c1728p9 | 123:58563e6cba1e | 985 | break; |
c1728p9 | 123:58563e6cba1e | 986 | } |
c1728p9 | 123:58563e6cba1e | 987 | } |
c1728p9 | 123:58563e6cba1e | 988 | if (i == os_maxtaskrun) { |
c1728p9 | 123:58563e6cba1e | 989 | // Include the idle task at the end of the enumeration |
c1728p9 | 123:58563e6cba1e | 990 | id = &os_idle_TCB; |
c1728p9 | 123:58563e6cba1e | 991 | } |
c1728p9 | 123:58563e6cba1e | 992 | *index = i + 1; |
c1728p9 | 123:58563e6cba1e | 993 | return id; |
c1728p9 | 123:58563e6cba1e | 994 | } |
c1728p9 | 123:58563e6cba1e | 995 | |
c1728p9 | 123:58563e6cba1e | 996 | osStatus _osThreadEnumFree(osThreadEnumId enum_id) { |
c1728p9 | 123:58563e6cba1e | 997 | uint32_t *index = (uint32_t*)enum_id; |
c1728p9 | 123:58563e6cba1e | 998 | *index = 0; |
c1728p9 | 123:58563e6cba1e | 999 | osMutexRelease(osMutexId_osThreadMutex); |
c1728p9 | 123:58563e6cba1e | 1000 | return osOK; |
c1728p9 | 123:58563e6cba1e | 1001 | } |
c1728p9 | 123:58563e6cba1e | 1002 | |
mbed_official | 49:77c8e4604045 | 1003 | // ==== Generic Wait Functions ==== |
mbed_official | 49:77c8e4604045 | 1004 | |
mbed_official | 49:77c8e4604045 | 1005 | // Generic Wait Service Calls declarations |
mbed_official | 49:77c8e4604045 | 1006 | SVC_1_1(svcDelay, osStatus, uint32_t, RET_osStatus) |
mbed_official | 49:77c8e4604045 | 1007 | #if osFeature_Wait != 0 |
mbed_official | 49:77c8e4604045 | 1008 | SVC_1_3(svcWait, os_InRegs osEvent, uint32_t, RET_osEvent) |
mbed_official | 49:77c8e4604045 | 1009 | #endif |
mbed_official | 49:77c8e4604045 | 1010 | |
mbed_official | 49:77c8e4604045 | 1011 | // Generic Wait Service Calls |
mbed_official | 49:77c8e4604045 | 1012 | |
mbed_official | 49:77c8e4604045 | 1013 | /// Wait for Timeout (Time Delay) |
mbed_official | 49:77c8e4604045 | 1014 | osStatus svcDelay (uint32_t millisec) { |
mbed_official | 112:53ace74b190c | 1015 | if (millisec == 0U) { return osOK; } |
mbed_official | 49:77c8e4604045 | 1016 | rt_dly_wait(rt_ms2tick(millisec)); |
mbed_official | 49:77c8e4604045 | 1017 | return osEventTimeout; |
mbed_official | 49:77c8e4604045 | 1018 | } |
mbed_official | 49:77c8e4604045 | 1019 | |
mbed_official | 49:77c8e4604045 | 1020 | /// Wait for Signal, Message, Mail, or Timeout |
mbed_official | 49:77c8e4604045 | 1021 | #if osFeature_Wait != 0 |
mbed_official | 49:77c8e4604045 | 1022 | os_InRegs osEvent_type svcWait (uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 1023 | osEvent ret; |
mbed_official | 49:77c8e4604045 | 1024 | |
mbed_official | 112:53ace74b190c | 1025 | if (millisec == 0U) { |
mbed_official | 49:77c8e4604045 | 1026 | ret.status = osOK; |
mbed_official | 49:77c8e4604045 | 1027 | return osEvent_ret_status; |
mbed_official | 49:77c8e4604045 | 1028 | } |
mbed_official | 49:77c8e4604045 | 1029 | |
mbed_official | 49:77c8e4604045 | 1030 | /* To Do: osEventSignal, osEventMessage, osEventMail */ |
mbed_official | 49:77c8e4604045 | 1031 | rt_dly_wait(rt_ms2tick(millisec)); |
mbed_official | 49:77c8e4604045 | 1032 | ret.status = osEventTimeout; |
mbed_official | 49:77c8e4604045 | 1033 | |
mbed_official | 49:77c8e4604045 | 1034 | return osEvent_ret_status; |
mbed_official | 49:77c8e4604045 | 1035 | } |
mbed_official | 49:77c8e4604045 | 1036 | #endif |
mbed_official | 49:77c8e4604045 | 1037 | |
mbed_official | 49:77c8e4604045 | 1038 | |
mbed_official | 49:77c8e4604045 | 1039 | // Generic Wait API |
mbed_official | 49:77c8e4604045 | 1040 | |
mbed_official | 49:77c8e4604045 | 1041 | /// Wait for Timeout (Time Delay) |
mbed_official | 49:77c8e4604045 | 1042 | osStatus osDelay (uint32_t millisec) { |
c1728p9 | 123:58563e6cba1e | 1043 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1044 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1045 | } |
mbed_official | 49:77c8e4604045 | 1046 | return __svcDelay(millisec); |
mbed_official | 49:77c8e4604045 | 1047 | } |
mbed_official | 49:77c8e4604045 | 1048 | |
mbed_official | 49:77c8e4604045 | 1049 | /// Wait for Signal, Message, Mail, or Timeout |
mbed_official | 49:77c8e4604045 | 1050 | os_InRegs osEvent osWait (uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 1051 | osEvent ret; |
mbed_official | 49:77c8e4604045 | 1052 | |
mbed_official | 49:77c8e4604045 | 1053 | #if osFeature_Wait == 0 |
mbed_official | 49:77c8e4604045 | 1054 | ret.status = osErrorOS; |
mbed_official | 49:77c8e4604045 | 1055 | return ret; |
mbed_official | 49:77c8e4604045 | 1056 | #else |
c1728p9 | 123:58563e6cba1e | 1057 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { // Not allowed in ISR |
mbed_official | 49:77c8e4604045 | 1058 | ret.status = osErrorISR; |
mbed_official | 49:77c8e4604045 | 1059 | return ret; |
mbed_official | 49:77c8e4604045 | 1060 | } |
mbed_official | 49:77c8e4604045 | 1061 | return __svcWait(millisec); |
mbed_official | 49:77c8e4604045 | 1062 | #endif |
mbed_official | 49:77c8e4604045 | 1063 | } |
mbed_official | 49:77c8e4604045 | 1064 | |
mbed_official | 49:77c8e4604045 | 1065 | |
mbed_official | 49:77c8e4604045 | 1066 | // ==== Timer Management ==== |
mbed_official | 49:77c8e4604045 | 1067 | |
mbed_official | 49:77c8e4604045 | 1068 | // Timer definitions |
mbed_official | 112:53ace74b190c | 1069 | #define osTimerInvalid 0U |
mbed_official | 112:53ace74b190c | 1070 | #define osTimerStopped 1U |
mbed_official | 112:53ace74b190c | 1071 | #define osTimerRunning 2U |
mbed_official | 49:77c8e4604045 | 1072 | |
mbed_official | 112:53ace74b190c | 1073 | // Timer structures |
mbed_official | 49:77c8e4604045 | 1074 | |
mbed_official | 49:77c8e4604045 | 1075 | typedef struct os_timer_cb_ { // Timer Control Block |
mbed_official | 49:77c8e4604045 | 1076 | struct os_timer_cb_ *next; // Pointer to next active Timer |
mbed_official | 49:77c8e4604045 | 1077 | uint8_t state; // Timer State |
mbed_official | 49:77c8e4604045 | 1078 | uint8_t type; // Timer Type (Periodic/One-shot) |
mbed_official | 49:77c8e4604045 | 1079 | uint16_t reserved; // Reserved |
mbed_official | 112:53ace74b190c | 1080 | uint32_t tcnt; // Timer Delay Count |
mbed_official | 112:53ace74b190c | 1081 | uint32_t icnt; // Timer Initial Count |
mbed_official | 49:77c8e4604045 | 1082 | void *arg; // Timer Function Argument |
mbed_official | 112:53ace74b190c | 1083 | const osTimerDef_t *timer; // Pointer to Timer definition |
mbed_official | 49:77c8e4604045 | 1084 | } os_timer_cb; |
mbed_official | 49:77c8e4604045 | 1085 | |
mbed_official | 49:77c8e4604045 | 1086 | // Timer variables |
mbed_official | 49:77c8e4604045 | 1087 | os_timer_cb *os_timer_head; // Pointer to first active Timer |
mbed_official | 49:77c8e4604045 | 1088 | |
mbed_official | 49:77c8e4604045 | 1089 | |
mbed_official | 49:77c8e4604045 | 1090 | // Timer Helper Functions |
mbed_official | 49:77c8e4604045 | 1091 | |
mbed_official | 49:77c8e4604045 | 1092 | // Insert Timer into the list sorted by time |
mbed_official | 49:77c8e4604045 | 1093 | static void rt_timer_insert (os_timer_cb *pt, uint32_t tcnt) { |
mbed_official | 49:77c8e4604045 | 1094 | os_timer_cb *p, *prev; |
mbed_official | 49:77c8e4604045 | 1095 | |
mbed_official | 49:77c8e4604045 | 1096 | prev = NULL; |
mbed_official | 49:77c8e4604045 | 1097 | p = os_timer_head; |
mbed_official | 49:77c8e4604045 | 1098 | while (p != NULL) { |
mbed_official | 112:53ace74b190c | 1099 | if (tcnt < p->tcnt) { break; } |
mbed_official | 49:77c8e4604045 | 1100 | tcnt -= p->tcnt; |
mbed_official | 49:77c8e4604045 | 1101 | prev = p; |
mbed_official | 49:77c8e4604045 | 1102 | p = p->next; |
mbed_official | 49:77c8e4604045 | 1103 | } |
mbed_official | 49:77c8e4604045 | 1104 | pt->next = p; |
mbed_official | 112:53ace74b190c | 1105 | pt->tcnt = tcnt; |
mbed_official | 49:77c8e4604045 | 1106 | if (p != NULL) { |
mbed_official | 49:77c8e4604045 | 1107 | p->tcnt -= pt->tcnt; |
mbed_official | 49:77c8e4604045 | 1108 | } |
mbed_official | 49:77c8e4604045 | 1109 | if (prev != NULL) { |
mbed_official | 49:77c8e4604045 | 1110 | prev->next = pt; |
mbed_official | 49:77c8e4604045 | 1111 | } else { |
mbed_official | 49:77c8e4604045 | 1112 | os_timer_head = pt; |
mbed_official | 49:77c8e4604045 | 1113 | } |
mbed_official | 49:77c8e4604045 | 1114 | } |
mbed_official | 49:77c8e4604045 | 1115 | |
mbed_official | 49:77c8e4604045 | 1116 | // Remove Timer from the list |
mbed_official | 112:53ace74b190c | 1117 | static int32_t rt_timer_remove (os_timer_cb *pt) { |
mbed_official | 49:77c8e4604045 | 1118 | os_timer_cb *p, *prev; |
mbed_official | 49:77c8e4604045 | 1119 | |
mbed_official | 49:77c8e4604045 | 1120 | prev = NULL; |
mbed_official | 49:77c8e4604045 | 1121 | p = os_timer_head; |
mbed_official | 49:77c8e4604045 | 1122 | while (p != NULL) { |
mbed_official | 112:53ace74b190c | 1123 | if (p == pt) { break; } |
mbed_official | 49:77c8e4604045 | 1124 | prev = p; |
mbed_official | 49:77c8e4604045 | 1125 | p = p->next; |
mbed_official | 49:77c8e4604045 | 1126 | } |
mbed_official | 112:53ace74b190c | 1127 | if (p == NULL) { return -1; } |
mbed_official | 49:77c8e4604045 | 1128 | if (prev != NULL) { |
mbed_official | 49:77c8e4604045 | 1129 | prev->next = pt->next; |
mbed_official | 49:77c8e4604045 | 1130 | } else { |
mbed_official | 49:77c8e4604045 | 1131 | os_timer_head = pt->next; |
mbed_official | 49:77c8e4604045 | 1132 | } |
mbed_official | 49:77c8e4604045 | 1133 | if (pt->next != NULL) { |
mbed_official | 49:77c8e4604045 | 1134 | pt->next->tcnt += pt->tcnt; |
mbed_official | 49:77c8e4604045 | 1135 | } |
mbed_official | 49:77c8e4604045 | 1136 | |
mbed_official | 49:77c8e4604045 | 1137 | return 0; |
mbed_official | 49:77c8e4604045 | 1138 | } |
mbed_official | 49:77c8e4604045 | 1139 | |
mbed_official | 49:77c8e4604045 | 1140 | |
mbed_official | 49:77c8e4604045 | 1141 | // Timer Service Calls declarations |
mbed_official | 112:53ace74b190c | 1142 | SVC_3_1(svcTimerCreate, osTimerId, const osTimerDef_t *, os_timer_type, void *, RET_pointer) |
mbed_official | 112:53ace74b190c | 1143 | SVC_2_1(svcTimerStart, osStatus, osTimerId, uint32_t, RET_osStatus) |
mbed_official | 112:53ace74b190c | 1144 | SVC_1_1(svcTimerStop, osStatus, osTimerId, RET_osStatus) |
mbed_official | 112:53ace74b190c | 1145 | SVC_1_1(svcTimerDelete, osStatus, osTimerId, RET_osStatus) |
mbed_official | 112:53ace74b190c | 1146 | SVC_1_2(svcTimerCall, os_InRegs osCallback, osTimerId, RET_osCallback) |
mbed_official | 49:77c8e4604045 | 1147 | |
mbed_official | 49:77c8e4604045 | 1148 | // Timer Management Service Calls |
mbed_official | 49:77c8e4604045 | 1149 | |
mbed_official | 49:77c8e4604045 | 1150 | /// Create timer |
mbed_official | 112:53ace74b190c | 1151 | osTimerId svcTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument) { |
mbed_official | 49:77c8e4604045 | 1152 | os_timer_cb *pt; |
mbed_official | 49:77c8e4604045 | 1153 | |
mbed_official | 49:77c8e4604045 | 1154 | if ((timer_def == NULL) || (timer_def->ptimer == NULL)) { |
mbed_official | 49:77c8e4604045 | 1155 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 1156 | return NULL; |
mbed_official | 49:77c8e4604045 | 1157 | } |
mbed_official | 49:77c8e4604045 | 1158 | |
mbed_official | 49:77c8e4604045 | 1159 | pt = timer_def->timer; |
mbed_official | 49:77c8e4604045 | 1160 | if (pt == NULL) { |
mbed_official | 49:77c8e4604045 | 1161 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 1162 | return NULL; |
mbed_official | 49:77c8e4604045 | 1163 | } |
mbed_official | 49:77c8e4604045 | 1164 | |
mbed_official | 49:77c8e4604045 | 1165 | if ((type != osTimerOnce) && (type != osTimerPeriodic)) { |
mbed_official | 49:77c8e4604045 | 1166 | sysThreadError(osErrorValue); |
mbed_official | 49:77c8e4604045 | 1167 | return NULL; |
mbed_official | 49:77c8e4604045 | 1168 | } |
mbed_official | 49:77c8e4604045 | 1169 | |
mbed_official | 49:77c8e4604045 | 1170 | if (osThreadId_osTimerThread == NULL) { |
mbed_official | 49:77c8e4604045 | 1171 | sysThreadError(osErrorResource); |
mbed_official | 49:77c8e4604045 | 1172 | return NULL; |
mbed_official | 49:77c8e4604045 | 1173 | } |
mbed_official | 49:77c8e4604045 | 1174 | |
mbed_official | 49:77c8e4604045 | 1175 | if (pt->state != osTimerInvalid){ |
mbed_official | 49:77c8e4604045 | 1176 | sysThreadError(osErrorResource); |
mbed_official | 49:77c8e4604045 | 1177 | return NULL; |
mbed_official | 49:77c8e4604045 | 1178 | } |
mbed_official | 49:77c8e4604045 | 1179 | |
mbed_official | 112:53ace74b190c | 1180 | pt->next = NULL; |
mbed_official | 49:77c8e4604045 | 1181 | pt->state = osTimerStopped; |
mbed_official | 49:77c8e4604045 | 1182 | pt->type = (uint8_t)type; |
mbed_official | 49:77c8e4604045 | 1183 | pt->arg = argument; |
mbed_official | 49:77c8e4604045 | 1184 | pt->timer = timer_def; |
mbed_official | 49:77c8e4604045 | 1185 | |
mbed_official | 49:77c8e4604045 | 1186 | return (osTimerId)pt; |
mbed_official | 49:77c8e4604045 | 1187 | } |
mbed_official | 49:77c8e4604045 | 1188 | |
mbed_official | 49:77c8e4604045 | 1189 | /// Start or restart timer |
mbed_official | 49:77c8e4604045 | 1190 | osStatus svcTimerStart (osTimerId timer_id, uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 1191 | os_timer_cb *pt; |
mbed_official | 49:77c8e4604045 | 1192 | uint32_t tcnt; |
mbed_official | 49:77c8e4604045 | 1193 | |
mbed_official | 49:77c8e4604045 | 1194 | pt = rt_id2obj(timer_id); |
mbed_official | 112:53ace74b190c | 1195 | if (pt == NULL) { |
mbed_official | 112:53ace74b190c | 1196 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1197 | } |
mbed_official | 49:77c8e4604045 | 1198 | |
mbed_official | 112:53ace74b190c | 1199 | if (millisec == 0U) { return osErrorValue; } |
mbed_official | 112:53ace74b190c | 1200 | |
mbed_official | 112:53ace74b190c | 1201 | tcnt = (uint32_t)(((1000U * (uint64_t)millisec) + os_clockrate - 1U) / os_clockrate); |
mbed_official | 49:77c8e4604045 | 1202 | |
mbed_official | 49:77c8e4604045 | 1203 | switch (pt->state) { |
mbed_official | 49:77c8e4604045 | 1204 | case osTimerRunning: |
mbed_official | 49:77c8e4604045 | 1205 | if (rt_timer_remove(pt) != 0) { |
mbed_official | 49:77c8e4604045 | 1206 | return osErrorResource; |
mbed_official | 49:77c8e4604045 | 1207 | } |
mbed_official | 49:77c8e4604045 | 1208 | break; |
mbed_official | 49:77c8e4604045 | 1209 | case osTimerStopped: |
mbed_official | 49:77c8e4604045 | 1210 | pt->state = osTimerRunning; |
mbed_official | 112:53ace74b190c | 1211 | pt->icnt = tcnt; |
mbed_official | 49:77c8e4604045 | 1212 | break; |
mbed_official | 49:77c8e4604045 | 1213 | default: |
mbed_official | 49:77c8e4604045 | 1214 | return osErrorResource; |
mbed_official | 49:77c8e4604045 | 1215 | } |
mbed_official | 112:53ace74b190c | 1216 | |
mbed_official | 49:77c8e4604045 | 1217 | rt_timer_insert(pt, tcnt); |
mbed_official | 49:77c8e4604045 | 1218 | |
mbed_official | 49:77c8e4604045 | 1219 | return osOK; |
mbed_official | 49:77c8e4604045 | 1220 | } |
mbed_official | 49:77c8e4604045 | 1221 | |
mbed_official | 49:77c8e4604045 | 1222 | /// Stop timer |
mbed_official | 49:77c8e4604045 | 1223 | osStatus svcTimerStop (osTimerId timer_id) { |
mbed_official | 49:77c8e4604045 | 1224 | os_timer_cb *pt; |
mbed_official | 49:77c8e4604045 | 1225 | |
mbed_official | 49:77c8e4604045 | 1226 | pt = rt_id2obj(timer_id); |
mbed_official | 112:53ace74b190c | 1227 | if (pt == NULL) { |
mbed_official | 112:53ace74b190c | 1228 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1229 | } |
mbed_official | 49:77c8e4604045 | 1230 | |
mbed_official | 112:53ace74b190c | 1231 | if (pt->state != osTimerRunning) { return osErrorResource; } |
mbed_official | 49:77c8e4604045 | 1232 | |
mbed_official | 49:77c8e4604045 | 1233 | pt->state = osTimerStopped; |
mbed_official | 49:77c8e4604045 | 1234 | |
mbed_official | 49:77c8e4604045 | 1235 | if (rt_timer_remove(pt) != 0) { |
mbed_official | 49:77c8e4604045 | 1236 | return osErrorResource; |
mbed_official | 49:77c8e4604045 | 1237 | } |
mbed_official | 49:77c8e4604045 | 1238 | |
mbed_official | 49:77c8e4604045 | 1239 | return osOK; |
mbed_official | 49:77c8e4604045 | 1240 | } |
mbed_official | 49:77c8e4604045 | 1241 | |
mbed_official | 49:77c8e4604045 | 1242 | /// Delete timer |
mbed_official | 49:77c8e4604045 | 1243 | osStatus svcTimerDelete (osTimerId timer_id) { |
mbed_official | 49:77c8e4604045 | 1244 | os_timer_cb *pt; |
mbed_official | 49:77c8e4604045 | 1245 | |
mbed_official | 49:77c8e4604045 | 1246 | pt = rt_id2obj(timer_id); |
mbed_official | 112:53ace74b190c | 1247 | if (pt == NULL) { |
mbed_official | 112:53ace74b190c | 1248 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1249 | } |
mbed_official | 49:77c8e4604045 | 1250 | |
mbed_official | 49:77c8e4604045 | 1251 | switch (pt->state) { |
mbed_official | 49:77c8e4604045 | 1252 | case osTimerRunning: |
mbed_official | 49:77c8e4604045 | 1253 | rt_timer_remove(pt); |
mbed_official | 49:77c8e4604045 | 1254 | break; |
mbed_official | 49:77c8e4604045 | 1255 | case osTimerStopped: |
mbed_official | 49:77c8e4604045 | 1256 | break; |
mbed_official | 49:77c8e4604045 | 1257 | default: |
mbed_official | 49:77c8e4604045 | 1258 | return osErrorResource; |
mbed_official | 49:77c8e4604045 | 1259 | } |
mbed_official | 49:77c8e4604045 | 1260 | |
mbed_official | 49:77c8e4604045 | 1261 | pt->state = osTimerInvalid; |
mbed_official | 49:77c8e4604045 | 1262 | |
mbed_official | 49:77c8e4604045 | 1263 | return osOK; |
mbed_official | 49:77c8e4604045 | 1264 | } |
mbed_official | 49:77c8e4604045 | 1265 | |
mbed_official | 49:77c8e4604045 | 1266 | /// Get timer callback parameters |
mbed_official | 49:77c8e4604045 | 1267 | os_InRegs osCallback_type svcTimerCall (osTimerId timer_id) { |
mbed_official | 49:77c8e4604045 | 1268 | os_timer_cb *pt; |
mbed_official | 49:77c8e4604045 | 1269 | osCallback ret; |
mbed_official | 49:77c8e4604045 | 1270 | |
mbed_official | 49:77c8e4604045 | 1271 | pt = rt_id2obj(timer_id); |
mbed_official | 49:77c8e4604045 | 1272 | if (pt == NULL) { |
mbed_official | 49:77c8e4604045 | 1273 | ret.fp = NULL; |
mbed_official | 49:77c8e4604045 | 1274 | ret.arg = NULL; |
mbed_official | 49:77c8e4604045 | 1275 | return osCallback_ret; |
mbed_official | 49:77c8e4604045 | 1276 | } |
mbed_official | 49:77c8e4604045 | 1277 | |
mbed_official | 49:77c8e4604045 | 1278 | ret.fp = (void *)pt->timer->ptimer; |
mbed_official | 49:77c8e4604045 | 1279 | ret.arg = pt->arg; |
mbed_official | 49:77c8e4604045 | 1280 | |
mbed_official | 49:77c8e4604045 | 1281 | return osCallback_ret; |
mbed_official | 49:77c8e4604045 | 1282 | } |
mbed_official | 49:77c8e4604045 | 1283 | |
mbed_official | 112:53ace74b190c | 1284 | osStatus isrMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec); |
mbed_official | 49:77c8e4604045 | 1285 | |
mbed_official | 49:77c8e4604045 | 1286 | /// Timer Tick (called each SysTick) |
mbed_official | 49:77c8e4604045 | 1287 | void sysTimerTick (void) { |
mbed_official | 49:77c8e4604045 | 1288 | os_timer_cb *pt, *p; |
mbed_official | 112:53ace74b190c | 1289 | osStatus status; |
mbed_official | 49:77c8e4604045 | 1290 | |
mbed_official | 49:77c8e4604045 | 1291 | p = os_timer_head; |
mbed_official | 112:53ace74b190c | 1292 | if (p == NULL) { return; } |
mbed_official | 49:77c8e4604045 | 1293 | |
mbed_official | 49:77c8e4604045 | 1294 | p->tcnt--; |
mbed_official | 112:53ace74b190c | 1295 | while ((p != NULL) && (p->tcnt == 0U)) { |
mbed_official | 49:77c8e4604045 | 1296 | pt = p; |
mbed_official | 49:77c8e4604045 | 1297 | p = p->next; |
mbed_official | 49:77c8e4604045 | 1298 | os_timer_head = p; |
mbed_official | 112:53ace74b190c | 1299 | status = isrMessagePut(osMessageQId_osTimerMessageQ, (uint32_t)pt, 0U); |
mbed_official | 112:53ace74b190c | 1300 | if (status != osOK) { |
mbed_official | 112:53ace74b190c | 1301 | os_error(OS_ERR_TIMER_OVF); |
mbed_official | 112:53ace74b190c | 1302 | } |
mbed_official | 112:53ace74b190c | 1303 | if (pt->type == (uint8_t)osTimerPeriodic) { |
mbed_official | 49:77c8e4604045 | 1304 | rt_timer_insert(pt, pt->icnt); |
mbed_official | 49:77c8e4604045 | 1305 | } else { |
mbed_official | 49:77c8e4604045 | 1306 | pt->state = osTimerStopped; |
mbed_official | 49:77c8e4604045 | 1307 | } |
mbed_official | 49:77c8e4604045 | 1308 | } |
mbed_official | 49:77c8e4604045 | 1309 | } |
mbed_official | 49:77c8e4604045 | 1310 | |
mbed_official | 112:53ace74b190c | 1311 | /// Get user timers wake-up time |
mbed_official | 112:53ace74b190c | 1312 | uint32_t sysUserTimerWakeupTime (void) { |
mbed_official | 112:53ace74b190c | 1313 | |
mbed_official | 112:53ace74b190c | 1314 | if (os_timer_head) { |
mbed_official | 112:53ace74b190c | 1315 | return os_timer_head->tcnt; |
mbed_official | 112:53ace74b190c | 1316 | } |
mbed_official | 112:53ace74b190c | 1317 | return 0xFFFFFFFFU; |
mbed_official | 112:53ace74b190c | 1318 | } |
mbed_official | 112:53ace74b190c | 1319 | |
mbed_official | 112:53ace74b190c | 1320 | /// Update user timers on resume |
mbed_official | 112:53ace74b190c | 1321 | void sysUserTimerUpdate (uint32_t sleep_time) { |
mbed_official | 112:53ace74b190c | 1322 | |
mbed_official | 112:53ace74b190c | 1323 | while ((os_timer_head != NULL) && (sleep_time != 0U)) { |
mbed_official | 112:53ace74b190c | 1324 | if (sleep_time >= os_timer_head->tcnt) { |
mbed_official | 112:53ace74b190c | 1325 | sleep_time -= os_timer_head->tcnt; |
mbed_official | 112:53ace74b190c | 1326 | os_timer_head->tcnt = 1U; |
mbed_official | 112:53ace74b190c | 1327 | sysTimerTick(); |
mbed_official | 112:53ace74b190c | 1328 | } else { |
mbed_official | 112:53ace74b190c | 1329 | os_timer_head->tcnt -= sleep_time; |
mbed_official | 112:53ace74b190c | 1330 | break; |
mbed_official | 112:53ace74b190c | 1331 | } |
mbed_official | 112:53ace74b190c | 1332 | } |
mbed_official | 112:53ace74b190c | 1333 | } |
mbed_official | 112:53ace74b190c | 1334 | |
mbed_official | 49:77c8e4604045 | 1335 | |
mbed_official | 49:77c8e4604045 | 1336 | // Timer Management Public API |
mbed_official | 49:77c8e4604045 | 1337 | |
mbed_official | 49:77c8e4604045 | 1338 | /// Create timer |
mbed_official | 112:53ace74b190c | 1339 | osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument) { |
c1728p9 | 123:58563e6cba1e | 1340 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1341 | return NULL; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1342 | } |
mbed_official | 112:53ace74b190c | 1343 | if (((__get_CONTROL() & 1U) == 0U) && (os_running == 0U)) { |
mbed_official | 49:77c8e4604045 | 1344 | // Privileged and not running |
mbed_official | 49:77c8e4604045 | 1345 | return svcTimerCreate(timer_def, type, argument); |
mbed_official | 49:77c8e4604045 | 1346 | } else { |
mbed_official | 49:77c8e4604045 | 1347 | return __svcTimerCreate(timer_def, type, argument); |
mbed_official | 49:77c8e4604045 | 1348 | } |
mbed_official | 49:77c8e4604045 | 1349 | } |
mbed_official | 49:77c8e4604045 | 1350 | |
mbed_official | 49:77c8e4604045 | 1351 | /// Start or restart timer |
mbed_official | 49:77c8e4604045 | 1352 | osStatus osTimerStart (osTimerId timer_id, uint32_t millisec) { |
c1728p9 | 123:58563e6cba1e | 1353 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1354 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1355 | } |
mbed_official | 49:77c8e4604045 | 1356 | return __svcTimerStart(timer_id, millisec); |
mbed_official | 49:77c8e4604045 | 1357 | } |
mbed_official | 49:77c8e4604045 | 1358 | |
mbed_official | 49:77c8e4604045 | 1359 | /// Stop timer |
mbed_official | 49:77c8e4604045 | 1360 | osStatus osTimerStop (osTimerId timer_id) { |
c1728p9 | 123:58563e6cba1e | 1361 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1362 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1363 | } |
mbed_official | 49:77c8e4604045 | 1364 | return __svcTimerStop(timer_id); |
mbed_official | 49:77c8e4604045 | 1365 | } |
mbed_official | 49:77c8e4604045 | 1366 | |
mbed_official | 49:77c8e4604045 | 1367 | /// Delete timer |
mbed_official | 49:77c8e4604045 | 1368 | osStatus osTimerDelete (osTimerId timer_id) { |
c1728p9 | 123:58563e6cba1e | 1369 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1370 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1371 | } |
mbed_official | 49:77c8e4604045 | 1372 | return __svcTimerDelete(timer_id); |
mbed_official | 49:77c8e4604045 | 1373 | } |
mbed_official | 49:77c8e4604045 | 1374 | |
mbed_official | 49:77c8e4604045 | 1375 | /// INTERNAL - Not Public |
mbed_official | 49:77c8e4604045 | 1376 | /// Get timer callback parameters (used by OS Timer Thread) |
mbed_official | 112:53ace74b190c | 1377 | os_InRegs osCallback osTimerCall (osTimerId timer_id) { |
mbed_official | 112:53ace74b190c | 1378 | return __svcTimerCall(timer_id); |
mbed_official | 49:77c8e4604045 | 1379 | } |
mbed_official | 49:77c8e4604045 | 1380 | |
mbed_official | 49:77c8e4604045 | 1381 | |
mbed_official | 49:77c8e4604045 | 1382 | // Timer Thread |
mbed_official | 49:77c8e4604045 | 1383 | __NO_RETURN void osTimerThread (void const *argument) { |
mbed_official | 49:77c8e4604045 | 1384 | osCallback cb; |
mbed_official | 49:77c8e4604045 | 1385 | osEvent evt; |
mbed_official | 49:77c8e4604045 | 1386 | |
mbed_official | 49:77c8e4604045 | 1387 | for (;;) { |
mbed_official | 49:77c8e4604045 | 1388 | evt = osMessageGet(osMessageQId_osTimerMessageQ, osWaitForever); |
mbed_official | 49:77c8e4604045 | 1389 | if (evt.status == osEventMessage) { |
mbed_official | 49:77c8e4604045 | 1390 | cb = osTimerCall(evt.value.p); |
mbed_official | 49:77c8e4604045 | 1391 | if (cb.fp != NULL) { |
mbed_official | 49:77c8e4604045 | 1392 | (*(os_ptimer)cb.fp)(cb.arg); |
mbed_official | 49:77c8e4604045 | 1393 | } |
mbed_official | 49:77c8e4604045 | 1394 | } |
mbed_official | 49:77c8e4604045 | 1395 | } |
mbed_official | 49:77c8e4604045 | 1396 | } |
mbed_official | 49:77c8e4604045 | 1397 | |
mbed_official | 49:77c8e4604045 | 1398 | |
mbed_official | 49:77c8e4604045 | 1399 | // ==== Signal Management ==== |
mbed_official | 49:77c8e4604045 | 1400 | |
mbed_official | 49:77c8e4604045 | 1401 | // Signal Service Calls declarations |
mbed_official | 49:77c8e4604045 | 1402 | SVC_2_1(svcSignalSet, int32_t, osThreadId, int32_t, RET_int32_t) |
mbed_official | 49:77c8e4604045 | 1403 | SVC_2_1(svcSignalClear, int32_t, osThreadId, int32_t, RET_int32_t) |
mbed_official | 49:77c8e4604045 | 1404 | SVC_2_3(svcSignalWait, os_InRegs osEvent, int32_t, uint32_t, RET_osEvent) |
mbed_official | 49:77c8e4604045 | 1405 | |
mbed_official | 49:77c8e4604045 | 1406 | // Signal Service Calls |
mbed_official | 49:77c8e4604045 | 1407 | |
mbed_official | 49:77c8e4604045 | 1408 | /// Set the specified Signal Flags of an active thread |
mbed_official | 49:77c8e4604045 | 1409 | int32_t svcSignalSet (osThreadId thread_id, int32_t signals) { |
mbed_official | 49:77c8e4604045 | 1410 | P_TCB ptcb; |
mbed_official | 49:77c8e4604045 | 1411 | int32_t sig; |
mbed_official | 49:77c8e4604045 | 1412 | |
mbed_official | 49:77c8e4604045 | 1413 | ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer |
mbed_official | 112:53ace74b190c | 1414 | if (ptcb == NULL) { |
mbed_official | 112:53ace74b190c | 1415 | return (int32_t)0x80000000U; |
mbed_official | 112:53ace74b190c | 1416 | } |
mbed_official | 49:77c8e4604045 | 1417 | |
mbed_official | 112:53ace74b190c | 1418 | if ((uint32_t)signals & (0xFFFFFFFFU << osFeature_Signals)) { |
mbed_official | 112:53ace74b190c | 1419 | return (int32_t)0x80000000U; |
mbed_official | 112:53ace74b190c | 1420 | } |
mbed_official | 49:77c8e4604045 | 1421 | |
mbed_official | 112:53ace74b190c | 1422 | sig = (int32_t)ptcb->events; // Previous signal flags |
mbed_official | 112:53ace74b190c | 1423 | |
mbed_official | 112:53ace74b190c | 1424 | rt_evt_set((uint16_t)signals, ptcb->task_id); // Set event flags |
mbed_official | 49:77c8e4604045 | 1425 | |
mbed_official | 49:77c8e4604045 | 1426 | return sig; |
mbed_official | 49:77c8e4604045 | 1427 | } |
mbed_official | 49:77c8e4604045 | 1428 | |
mbed_official | 49:77c8e4604045 | 1429 | /// Clear the specified Signal Flags of an active thread |
mbed_official | 49:77c8e4604045 | 1430 | int32_t svcSignalClear (osThreadId thread_id, int32_t signals) { |
mbed_official | 49:77c8e4604045 | 1431 | P_TCB ptcb; |
mbed_official | 49:77c8e4604045 | 1432 | int32_t sig; |
mbed_official | 49:77c8e4604045 | 1433 | |
mbed_official | 49:77c8e4604045 | 1434 | ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer |
mbed_official | 112:53ace74b190c | 1435 | if (ptcb == NULL) { |
mbed_official | 112:53ace74b190c | 1436 | return (int32_t)0x80000000U; |
mbed_official | 112:53ace74b190c | 1437 | } |
mbed_official | 49:77c8e4604045 | 1438 | |
mbed_official | 112:53ace74b190c | 1439 | if ((uint32_t)signals & (0xFFFFFFFFU << osFeature_Signals)) { |
mbed_official | 112:53ace74b190c | 1440 | return (int32_t)0x80000000U; |
mbed_official | 112:53ace74b190c | 1441 | } |
mbed_official | 49:77c8e4604045 | 1442 | |
mbed_official | 112:53ace74b190c | 1443 | sig = (int32_t)ptcb->events; // Previous signal flags |
mbed_official | 112:53ace74b190c | 1444 | |
mbed_official | 112:53ace74b190c | 1445 | rt_evt_clr((uint16_t)signals, ptcb->task_id); // Clear event flags |
mbed_official | 49:77c8e4604045 | 1446 | |
mbed_official | 49:77c8e4604045 | 1447 | return sig; |
mbed_official | 49:77c8e4604045 | 1448 | } |
mbed_official | 49:77c8e4604045 | 1449 | |
mbed_official | 49:77c8e4604045 | 1450 | /// Wait for one or more Signal Flags to become signaled for the current RUNNING thread |
mbed_official | 49:77c8e4604045 | 1451 | os_InRegs osEvent_type svcSignalWait (int32_t signals, uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 1452 | OS_RESULT res; |
mbed_official | 49:77c8e4604045 | 1453 | osEvent ret; |
mbed_official | 49:77c8e4604045 | 1454 | |
mbed_official | 112:53ace74b190c | 1455 | if ((uint32_t)signals & (0xFFFFFFFFU << osFeature_Signals)) { |
mbed_official | 49:77c8e4604045 | 1456 | ret.status = osErrorValue; |
mbed_official | 49:77c8e4604045 | 1457 | return osEvent_ret_status; |
mbed_official | 49:77c8e4604045 | 1458 | } |
mbed_official | 49:77c8e4604045 | 1459 | |
mbed_official | 49:77c8e4604045 | 1460 | if (signals != 0) { // Wait for all specified signals |
mbed_official | 112:53ace74b190c | 1461 | res = rt_evt_wait((uint16_t)signals, rt_ms2tick(millisec), __TRUE); |
mbed_official | 49:77c8e4604045 | 1462 | } else { // Wait for any signal |
mbed_official | 112:53ace74b190c | 1463 | res = rt_evt_wait(0xFFFFU, rt_ms2tick(millisec), __FALSE); |
mbed_official | 49:77c8e4604045 | 1464 | } |
mbed_official | 49:77c8e4604045 | 1465 | |
mbed_official | 49:77c8e4604045 | 1466 | if (res == OS_R_EVT) { |
mbed_official | 49:77c8e4604045 | 1467 | ret.status = osEventSignal; |
mbed_official | 112:53ace74b190c | 1468 | ret.value.signals = (signals != 0) ? signals : (int32_t)os_tsk.run->waits; |
mbed_official | 49:77c8e4604045 | 1469 | } else { |
mbed_official | 112:53ace74b190c | 1470 | ret.status = (millisec != 0U) ? osEventTimeout : osOK; |
mbed_official | 49:77c8e4604045 | 1471 | ret.value.signals = 0; |
mbed_official | 49:77c8e4604045 | 1472 | } |
mbed_official | 49:77c8e4604045 | 1473 | |
mbed_official | 49:77c8e4604045 | 1474 | return osEvent_ret_value; |
mbed_official | 49:77c8e4604045 | 1475 | } |
mbed_official | 49:77c8e4604045 | 1476 | |
mbed_official | 49:77c8e4604045 | 1477 | |
mbed_official | 49:77c8e4604045 | 1478 | // Signal ISR Calls |
mbed_official | 49:77c8e4604045 | 1479 | |
mbed_official | 49:77c8e4604045 | 1480 | /// Set the specified Signal Flags of an active thread |
mbed_official | 112:53ace74b190c | 1481 | int32_t isrSignalSet (osThreadId thread_id, int32_t signals) { |
mbed_official | 49:77c8e4604045 | 1482 | P_TCB ptcb; |
mbed_official | 49:77c8e4604045 | 1483 | int32_t sig; |
mbed_official | 49:77c8e4604045 | 1484 | |
mbed_official | 49:77c8e4604045 | 1485 | ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer |
mbed_official | 112:53ace74b190c | 1486 | if (ptcb == NULL) { |
mbed_official | 112:53ace74b190c | 1487 | return (int32_t)0x80000000U; |
mbed_official | 112:53ace74b190c | 1488 | } |
mbed_official | 49:77c8e4604045 | 1489 | |
mbed_official | 112:53ace74b190c | 1490 | if ((uint32_t)signals & (0xFFFFFFFFU << osFeature_Signals)) { |
mbed_official | 112:53ace74b190c | 1491 | return (int32_t)0x80000000U; |
mbed_official | 112:53ace74b190c | 1492 | } |
mbed_official | 49:77c8e4604045 | 1493 | |
mbed_official | 112:53ace74b190c | 1494 | sig = (int32_t)ptcb->events; // Previous signal flags |
mbed_official | 112:53ace74b190c | 1495 | |
mbed_official | 112:53ace74b190c | 1496 | isr_evt_set((uint16_t)signals, ptcb->task_id);// Set event flags |
mbed_official | 49:77c8e4604045 | 1497 | |
mbed_official | 49:77c8e4604045 | 1498 | return sig; |
mbed_official | 49:77c8e4604045 | 1499 | } |
mbed_official | 49:77c8e4604045 | 1500 | |
mbed_official | 49:77c8e4604045 | 1501 | |
mbed_official | 49:77c8e4604045 | 1502 | // Signal Public API |
mbed_official | 49:77c8e4604045 | 1503 | |
mbed_official | 49:77c8e4604045 | 1504 | /// Set the specified Signal Flags of an active thread |
mbed_official | 49:77c8e4604045 | 1505 | int32_t osSignalSet (osThreadId thread_id, int32_t signals) { |
c1728p9 | 123:58563e6cba1e | 1506 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { // in ISR |
mbed_official | 112:53ace74b190c | 1507 | return isrSignalSet(thread_id, signals); |
mbed_official | 49:77c8e4604045 | 1508 | } else { // in Thread |
mbed_official | 49:77c8e4604045 | 1509 | return __svcSignalSet(thread_id, signals); |
mbed_official | 49:77c8e4604045 | 1510 | } |
mbed_official | 49:77c8e4604045 | 1511 | } |
mbed_official | 49:77c8e4604045 | 1512 | |
mbed_official | 49:77c8e4604045 | 1513 | /// Clear the specified Signal Flags of an active thread |
mbed_official | 49:77c8e4604045 | 1514 | int32_t osSignalClear (osThreadId thread_id, int32_t signals) { |
c1728p9 | 123:58563e6cba1e | 1515 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1516 | return (int32_t)0x80000000U; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1517 | } |
mbed_official | 49:77c8e4604045 | 1518 | return __svcSignalClear(thread_id, signals); |
mbed_official | 49:77c8e4604045 | 1519 | } |
mbed_official | 49:77c8e4604045 | 1520 | |
mbed_official | 49:77c8e4604045 | 1521 | /// Wait for one or more Signal Flags to become signaled for the current RUNNING thread |
mbed_official | 49:77c8e4604045 | 1522 | os_InRegs osEvent osSignalWait (int32_t signals, uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 1523 | osEvent ret; |
mbed_official | 49:77c8e4604045 | 1524 | |
c1728p9 | 123:58563e6cba1e | 1525 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { // Not allowed in ISR |
mbed_official | 49:77c8e4604045 | 1526 | ret.status = osErrorISR; |
mbed_official | 49:77c8e4604045 | 1527 | return ret; |
mbed_official | 49:77c8e4604045 | 1528 | } |
mbed_official | 49:77c8e4604045 | 1529 | return __svcSignalWait(signals, millisec); |
mbed_official | 49:77c8e4604045 | 1530 | } |
mbed_official | 49:77c8e4604045 | 1531 | |
mbed_official | 49:77c8e4604045 | 1532 | |
mbed_official | 49:77c8e4604045 | 1533 | // ==== Mutex Management ==== |
mbed_official | 49:77c8e4604045 | 1534 | |
mbed_official | 49:77c8e4604045 | 1535 | // Mutex Service Calls declarations |
mbed_official | 112:53ace74b190c | 1536 | SVC_1_1(svcMutexCreate, osMutexId, const osMutexDef_t *, RET_pointer) |
mbed_official | 112:53ace74b190c | 1537 | SVC_2_1(svcMutexWait, osStatus, osMutexId, uint32_t, RET_osStatus) |
mbed_official | 112:53ace74b190c | 1538 | SVC_1_1(svcMutexRelease, osStatus, osMutexId, RET_osStatus) |
mbed_official | 112:53ace74b190c | 1539 | SVC_1_1(svcMutexDelete, osStatus, osMutexId, RET_osStatus) |
mbed_official | 49:77c8e4604045 | 1540 | |
mbed_official | 49:77c8e4604045 | 1541 | // Mutex Service Calls |
mbed_official | 49:77c8e4604045 | 1542 | |
mbed_official | 49:77c8e4604045 | 1543 | /// Create and Initialize a Mutex object |
mbed_official | 112:53ace74b190c | 1544 | osMutexId svcMutexCreate (const osMutexDef_t *mutex_def) { |
mbed_official | 49:77c8e4604045 | 1545 | OS_ID mut; |
mbed_official | 49:77c8e4604045 | 1546 | |
mbed_official | 49:77c8e4604045 | 1547 | if (mutex_def == NULL) { |
mbed_official | 49:77c8e4604045 | 1548 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 1549 | return NULL; |
mbed_official | 49:77c8e4604045 | 1550 | } |
mbed_official | 49:77c8e4604045 | 1551 | |
mbed_official | 49:77c8e4604045 | 1552 | mut = mutex_def->mutex; |
mbed_official | 49:77c8e4604045 | 1553 | if (mut == NULL) { |
mbed_official | 49:77c8e4604045 | 1554 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 1555 | return NULL; |
mbed_official | 49:77c8e4604045 | 1556 | } |
mbed_official | 49:77c8e4604045 | 1557 | |
mbed_official | 112:53ace74b190c | 1558 | if (((P_MUCB)mut)->cb_type != 0U) { |
mbed_official | 49:77c8e4604045 | 1559 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 1560 | return NULL; |
mbed_official | 49:77c8e4604045 | 1561 | } |
mbed_official | 49:77c8e4604045 | 1562 | |
mbed_official | 49:77c8e4604045 | 1563 | rt_mut_init(mut); // Initialize Mutex |
mbed_official | 49:77c8e4604045 | 1564 | |
mbed_official | 49:77c8e4604045 | 1565 | return mut; |
mbed_official | 49:77c8e4604045 | 1566 | } |
mbed_official | 49:77c8e4604045 | 1567 | |
mbed_official | 49:77c8e4604045 | 1568 | /// Wait until a Mutex becomes available |
mbed_official | 49:77c8e4604045 | 1569 | osStatus svcMutexWait (osMutexId mutex_id, uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 1570 | OS_ID mut; |
mbed_official | 49:77c8e4604045 | 1571 | OS_RESULT res; |
mbed_official | 49:77c8e4604045 | 1572 | |
mbed_official | 49:77c8e4604045 | 1573 | mut = rt_id2obj(mutex_id); |
mbed_official | 112:53ace74b190c | 1574 | if (mut == NULL) { |
mbed_official | 112:53ace74b190c | 1575 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1576 | } |
mbed_official | 49:77c8e4604045 | 1577 | |
mbed_official | 112:53ace74b190c | 1578 | if (((P_MUCB)mut)->cb_type != MUCB) { |
mbed_official | 112:53ace74b190c | 1579 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1580 | } |
mbed_official | 49:77c8e4604045 | 1581 | |
mbed_official | 49:77c8e4604045 | 1582 | res = rt_mut_wait(mut, rt_ms2tick(millisec)); // Wait for Mutex |
mbed_official | 49:77c8e4604045 | 1583 | |
mbed_official | 49:77c8e4604045 | 1584 | if (res == OS_R_TMO) { |
mbed_official | 112:53ace74b190c | 1585 | return ((millisec != 0U) ? osErrorTimeoutResource : osErrorResource); |
mbed_official | 49:77c8e4604045 | 1586 | } |
mbed_official | 49:77c8e4604045 | 1587 | |
mbed_official | 49:77c8e4604045 | 1588 | return osOK; |
mbed_official | 49:77c8e4604045 | 1589 | } |
mbed_official | 49:77c8e4604045 | 1590 | |
mbed_official | 49:77c8e4604045 | 1591 | /// Release a Mutex that was obtained with osMutexWait |
mbed_official | 49:77c8e4604045 | 1592 | osStatus svcMutexRelease (osMutexId mutex_id) { |
mbed_official | 49:77c8e4604045 | 1593 | OS_ID mut; |
mbed_official | 49:77c8e4604045 | 1594 | OS_RESULT res; |
mbed_official | 49:77c8e4604045 | 1595 | |
mbed_official | 49:77c8e4604045 | 1596 | mut = rt_id2obj(mutex_id); |
mbed_official | 112:53ace74b190c | 1597 | if (mut == NULL) { |
mbed_official | 112:53ace74b190c | 1598 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1599 | } |
mbed_official | 49:77c8e4604045 | 1600 | |
mbed_official | 112:53ace74b190c | 1601 | if (((P_MUCB)mut)->cb_type != MUCB) { |
mbed_official | 112:53ace74b190c | 1602 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1603 | } |
mbed_official | 49:77c8e4604045 | 1604 | |
mbed_official | 49:77c8e4604045 | 1605 | res = rt_mut_release(mut); // Release Mutex |
mbed_official | 49:77c8e4604045 | 1606 | |
mbed_official | 112:53ace74b190c | 1607 | if (res == OS_R_NOK) { |
mbed_official | 112:53ace74b190c | 1608 | return osErrorResource; // Thread not owner or Zero Counter |
mbed_official | 112:53ace74b190c | 1609 | } |
mbed_official | 49:77c8e4604045 | 1610 | |
mbed_official | 49:77c8e4604045 | 1611 | return osOK; |
mbed_official | 49:77c8e4604045 | 1612 | } |
mbed_official | 49:77c8e4604045 | 1613 | |
mbed_official | 49:77c8e4604045 | 1614 | /// Delete a Mutex that was created by osMutexCreate |
mbed_official | 49:77c8e4604045 | 1615 | osStatus svcMutexDelete (osMutexId mutex_id) { |
mbed_official | 49:77c8e4604045 | 1616 | OS_ID mut; |
mbed_official | 49:77c8e4604045 | 1617 | |
mbed_official | 49:77c8e4604045 | 1618 | mut = rt_id2obj(mutex_id); |
mbed_official | 112:53ace74b190c | 1619 | if (mut == NULL) { |
mbed_official | 112:53ace74b190c | 1620 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1621 | } |
mbed_official | 49:77c8e4604045 | 1622 | |
mbed_official | 112:53ace74b190c | 1623 | if (((P_MUCB)mut)->cb_type != MUCB) { |
mbed_official | 112:53ace74b190c | 1624 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1625 | } |
mbed_official | 49:77c8e4604045 | 1626 | |
mbed_official | 49:77c8e4604045 | 1627 | rt_mut_delete(mut); // Release Mutex |
mbed_official | 49:77c8e4604045 | 1628 | |
mbed_official | 49:77c8e4604045 | 1629 | return osOK; |
mbed_official | 49:77c8e4604045 | 1630 | } |
mbed_official | 49:77c8e4604045 | 1631 | |
mbed_official | 49:77c8e4604045 | 1632 | |
mbed_official | 49:77c8e4604045 | 1633 | // Mutex Public API |
mbed_official | 49:77c8e4604045 | 1634 | |
mbed_official | 49:77c8e4604045 | 1635 | /// Create and Initialize a Mutex object |
mbed_official | 112:53ace74b190c | 1636 | osMutexId osMutexCreate (const osMutexDef_t *mutex_def) { |
c1728p9 | 123:58563e6cba1e | 1637 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1638 | return NULL; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1639 | } |
mbed_official | 112:53ace74b190c | 1640 | if (((__get_CONTROL() & 1U) == 0U) && (os_running == 0U)) { |
mbed_official | 49:77c8e4604045 | 1641 | // Privileged and not running |
mbed_official | 49:77c8e4604045 | 1642 | return svcMutexCreate(mutex_def); |
mbed_official | 49:77c8e4604045 | 1643 | } else { |
mbed_official | 49:77c8e4604045 | 1644 | return __svcMutexCreate(mutex_def); |
mbed_official | 49:77c8e4604045 | 1645 | } |
mbed_official | 49:77c8e4604045 | 1646 | } |
mbed_official | 49:77c8e4604045 | 1647 | |
mbed_official | 49:77c8e4604045 | 1648 | /// Wait until a Mutex becomes available |
mbed_official | 49:77c8e4604045 | 1649 | osStatus osMutexWait (osMutexId mutex_id, uint32_t millisec) { |
c1728p9 | 123:58563e6cba1e | 1650 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1651 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1652 | } |
mbed_official | 49:77c8e4604045 | 1653 | return __svcMutexWait(mutex_id, millisec); |
mbed_official | 49:77c8e4604045 | 1654 | } |
mbed_official | 49:77c8e4604045 | 1655 | |
mbed_official | 49:77c8e4604045 | 1656 | /// Release a Mutex that was obtained with osMutexWait |
mbed_official | 49:77c8e4604045 | 1657 | osStatus osMutexRelease (osMutexId mutex_id) { |
c1728p9 | 123:58563e6cba1e | 1658 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1659 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1660 | } |
mbed_official | 49:77c8e4604045 | 1661 | return __svcMutexRelease(mutex_id); |
mbed_official | 49:77c8e4604045 | 1662 | } |
mbed_official | 49:77c8e4604045 | 1663 | |
mbed_official | 49:77c8e4604045 | 1664 | /// Delete a Mutex that was created by osMutexCreate |
mbed_official | 49:77c8e4604045 | 1665 | osStatus osMutexDelete (osMutexId mutex_id) { |
c1728p9 | 123:58563e6cba1e | 1666 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1667 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1668 | } |
mbed_official | 49:77c8e4604045 | 1669 | return __svcMutexDelete(mutex_id); |
mbed_official | 49:77c8e4604045 | 1670 | } |
mbed_official | 49:77c8e4604045 | 1671 | |
mbed_official | 49:77c8e4604045 | 1672 | |
mbed_official | 49:77c8e4604045 | 1673 | // ==== Semaphore Management ==== |
mbed_official | 49:77c8e4604045 | 1674 | |
mbed_official | 49:77c8e4604045 | 1675 | // Semaphore Service Calls declarations |
mbed_official | 49:77c8e4604045 | 1676 | SVC_2_1(svcSemaphoreCreate, osSemaphoreId, const osSemaphoreDef_t *, int32_t, RET_pointer) |
mbed_official | 112:53ace74b190c | 1677 | SVC_2_1(svcSemaphoreWait, int32_t, osSemaphoreId, uint32_t, RET_int32_t) |
mbed_official | 112:53ace74b190c | 1678 | SVC_1_1(svcSemaphoreRelease, osStatus, osSemaphoreId, RET_osStatus) |
mbed_official | 49:77c8e4604045 | 1679 | SVC_1_1(svcSemaphoreDelete, osStatus, osSemaphoreId, RET_osStatus) |
mbed_official | 49:77c8e4604045 | 1680 | |
mbed_official | 49:77c8e4604045 | 1681 | // Semaphore Service Calls |
mbed_official | 49:77c8e4604045 | 1682 | |
mbed_official | 49:77c8e4604045 | 1683 | /// Create and Initialize a Semaphore object |
mbed_official | 49:77c8e4604045 | 1684 | osSemaphoreId svcSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count) { |
mbed_official | 49:77c8e4604045 | 1685 | OS_ID sem; |
mbed_official | 49:77c8e4604045 | 1686 | |
mbed_official | 49:77c8e4604045 | 1687 | if (semaphore_def == NULL) { |
mbed_official | 49:77c8e4604045 | 1688 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 1689 | return NULL; |
mbed_official | 49:77c8e4604045 | 1690 | } |
mbed_official | 49:77c8e4604045 | 1691 | |
mbed_official | 49:77c8e4604045 | 1692 | sem = semaphore_def->semaphore; |
mbed_official | 49:77c8e4604045 | 1693 | if (sem == NULL) { |
mbed_official | 49:77c8e4604045 | 1694 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 1695 | return NULL; |
mbed_official | 49:77c8e4604045 | 1696 | } |
mbed_official | 49:77c8e4604045 | 1697 | |
mbed_official | 112:53ace74b190c | 1698 | if (((P_SCB)sem)->cb_type != 0U) { |
mbed_official | 49:77c8e4604045 | 1699 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 1700 | return NULL; |
mbed_official | 49:77c8e4604045 | 1701 | } |
mbed_official | 49:77c8e4604045 | 1702 | |
mbed_official | 49:77c8e4604045 | 1703 | if (count > osFeature_Semaphore) { |
mbed_official | 49:77c8e4604045 | 1704 | sysThreadError(osErrorValue); |
mbed_official | 49:77c8e4604045 | 1705 | return NULL; |
mbed_official | 49:77c8e4604045 | 1706 | } |
mbed_official | 49:77c8e4604045 | 1707 | |
mbed_official | 112:53ace74b190c | 1708 | rt_sem_init(sem, (uint16_t)count); // Initialize Semaphore |
mbed_official | 112:53ace74b190c | 1709 | |
mbed_official | 49:77c8e4604045 | 1710 | return sem; |
mbed_official | 49:77c8e4604045 | 1711 | } |
mbed_official | 49:77c8e4604045 | 1712 | |
mbed_official | 49:77c8e4604045 | 1713 | /// Wait until a Semaphore becomes available |
mbed_official | 49:77c8e4604045 | 1714 | int32_t svcSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 1715 | OS_ID sem; |
mbed_official | 49:77c8e4604045 | 1716 | OS_RESULT res; |
mbed_official | 49:77c8e4604045 | 1717 | |
mbed_official | 49:77c8e4604045 | 1718 | sem = rt_id2obj(semaphore_id); |
mbed_official | 112:53ace74b190c | 1719 | if (sem == NULL) { |
mbed_official | 112:53ace74b190c | 1720 | return -1; |
mbed_official | 112:53ace74b190c | 1721 | } |
mbed_official | 49:77c8e4604045 | 1722 | |
mbed_official | 112:53ace74b190c | 1723 | if (((P_SCB)sem)->cb_type != SCB) { |
mbed_official | 112:53ace74b190c | 1724 | return -1; |
mbed_official | 112:53ace74b190c | 1725 | } |
mbed_official | 49:77c8e4604045 | 1726 | |
mbed_official | 49:77c8e4604045 | 1727 | res = rt_sem_wait(sem, rt_ms2tick(millisec)); // Wait for Semaphore |
mbed_official | 49:77c8e4604045 | 1728 | |
mbed_official | 112:53ace74b190c | 1729 | if (res == OS_R_TMO) { return 0; } // Timeout |
mbed_official | 49:77c8e4604045 | 1730 | |
mbed_official | 112:53ace74b190c | 1731 | return (int32_t)(((P_SCB)sem)->tokens + 1U); |
mbed_official | 49:77c8e4604045 | 1732 | } |
mbed_official | 49:77c8e4604045 | 1733 | |
mbed_official | 49:77c8e4604045 | 1734 | /// Release a Semaphore |
mbed_official | 49:77c8e4604045 | 1735 | osStatus svcSemaphoreRelease (osSemaphoreId semaphore_id) { |
mbed_official | 49:77c8e4604045 | 1736 | OS_ID sem; |
mbed_official | 49:77c8e4604045 | 1737 | |
mbed_official | 49:77c8e4604045 | 1738 | sem = rt_id2obj(semaphore_id); |
mbed_official | 112:53ace74b190c | 1739 | if (sem == NULL) { |
mbed_official | 112:53ace74b190c | 1740 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1741 | } |
mbed_official | 49:77c8e4604045 | 1742 | |
mbed_official | 112:53ace74b190c | 1743 | if (((P_SCB)sem)->cb_type != SCB) { |
mbed_official | 112:53ace74b190c | 1744 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1745 | } |
mbed_official | 49:77c8e4604045 | 1746 | |
mbed_official | 112:53ace74b190c | 1747 | if ((int32_t)((P_SCB)sem)->tokens == osFeature_Semaphore) { |
mbed_official | 112:53ace74b190c | 1748 | return osErrorResource; |
mbed_official | 112:53ace74b190c | 1749 | } |
mbed_official | 112:53ace74b190c | 1750 | |
mbed_official | 49:77c8e4604045 | 1751 | rt_sem_send(sem); // Release Semaphore |
mbed_official | 49:77c8e4604045 | 1752 | |
mbed_official | 49:77c8e4604045 | 1753 | return osOK; |
mbed_official | 49:77c8e4604045 | 1754 | } |
mbed_official | 49:77c8e4604045 | 1755 | |
mbed_official | 49:77c8e4604045 | 1756 | /// Delete a Semaphore that was created by osSemaphoreCreate |
mbed_official | 49:77c8e4604045 | 1757 | osStatus svcSemaphoreDelete (osSemaphoreId semaphore_id) { |
mbed_official | 49:77c8e4604045 | 1758 | OS_ID sem; |
mbed_official | 49:77c8e4604045 | 1759 | |
mbed_official | 49:77c8e4604045 | 1760 | sem = rt_id2obj(semaphore_id); |
mbed_official | 112:53ace74b190c | 1761 | if (sem == NULL) { |
mbed_official | 112:53ace74b190c | 1762 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1763 | } |
mbed_official | 49:77c8e4604045 | 1764 | |
mbed_official | 112:53ace74b190c | 1765 | if (((P_SCB)sem)->cb_type != SCB) { |
mbed_official | 112:53ace74b190c | 1766 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1767 | } |
mbed_official | 49:77c8e4604045 | 1768 | |
mbed_official | 49:77c8e4604045 | 1769 | rt_sem_delete(sem); // Delete Semaphore |
mbed_official | 49:77c8e4604045 | 1770 | |
mbed_official | 49:77c8e4604045 | 1771 | return osOK; |
mbed_official | 49:77c8e4604045 | 1772 | } |
mbed_official | 49:77c8e4604045 | 1773 | |
mbed_official | 49:77c8e4604045 | 1774 | |
mbed_official | 49:77c8e4604045 | 1775 | // Semaphore ISR Calls |
mbed_official | 49:77c8e4604045 | 1776 | |
mbed_official | 49:77c8e4604045 | 1777 | /// Release a Semaphore |
mbed_official | 112:53ace74b190c | 1778 | osStatus isrSemaphoreRelease (osSemaphoreId semaphore_id) { |
mbed_official | 49:77c8e4604045 | 1779 | OS_ID sem; |
mbed_official | 49:77c8e4604045 | 1780 | |
mbed_official | 49:77c8e4604045 | 1781 | sem = rt_id2obj(semaphore_id); |
mbed_official | 112:53ace74b190c | 1782 | if (sem == NULL) { |
mbed_official | 112:53ace74b190c | 1783 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1784 | } |
mbed_official | 49:77c8e4604045 | 1785 | |
mbed_official | 112:53ace74b190c | 1786 | if (((P_SCB)sem)->cb_type != SCB) { |
mbed_official | 112:53ace74b190c | 1787 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1788 | } |
mbed_official | 49:77c8e4604045 | 1789 | |
mbed_official | 112:53ace74b190c | 1790 | if ((int32_t)((P_SCB)sem)->tokens == osFeature_Semaphore) { |
mbed_official | 112:53ace74b190c | 1791 | return osErrorResource; |
mbed_official | 112:53ace74b190c | 1792 | } |
mbed_official | 49:77c8e4604045 | 1793 | |
mbed_official | 49:77c8e4604045 | 1794 | isr_sem_send(sem); // Release Semaphore |
mbed_official | 49:77c8e4604045 | 1795 | |
mbed_official | 49:77c8e4604045 | 1796 | return osOK; |
mbed_official | 49:77c8e4604045 | 1797 | } |
mbed_official | 49:77c8e4604045 | 1798 | |
mbed_official | 49:77c8e4604045 | 1799 | |
mbed_official | 49:77c8e4604045 | 1800 | // Semaphore Public API |
mbed_official | 49:77c8e4604045 | 1801 | |
mbed_official | 49:77c8e4604045 | 1802 | /// Create and Initialize a Semaphore object |
mbed_official | 112:53ace74b190c | 1803 | osSemaphoreId osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count) { |
c1728p9 | 123:58563e6cba1e | 1804 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1805 | return NULL; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1806 | } |
mbed_official | 112:53ace74b190c | 1807 | if (((__get_CONTROL() & 1U) == 0U) && (os_running == 0U)) { |
mbed_official | 49:77c8e4604045 | 1808 | // Privileged and not running |
mbed_official | 49:77c8e4604045 | 1809 | return svcSemaphoreCreate(semaphore_def, count); |
mbed_official | 49:77c8e4604045 | 1810 | } else { |
mbed_official | 49:77c8e4604045 | 1811 | return __svcSemaphoreCreate(semaphore_def, count); |
mbed_official | 49:77c8e4604045 | 1812 | } |
mbed_official | 49:77c8e4604045 | 1813 | } |
mbed_official | 49:77c8e4604045 | 1814 | |
mbed_official | 49:77c8e4604045 | 1815 | /// Wait until a Semaphore becomes available |
mbed_official | 49:77c8e4604045 | 1816 | int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec) { |
c1728p9 | 123:58563e6cba1e | 1817 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1818 | return -1; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1819 | } |
mbed_official | 49:77c8e4604045 | 1820 | return __svcSemaphoreWait(semaphore_id, millisec); |
mbed_official | 49:77c8e4604045 | 1821 | } |
mbed_official | 49:77c8e4604045 | 1822 | |
mbed_official | 49:77c8e4604045 | 1823 | /// Release a Semaphore |
mbed_official | 49:77c8e4604045 | 1824 | osStatus osSemaphoreRelease (osSemaphoreId semaphore_id) { |
c1728p9 | 123:58563e6cba1e | 1825 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { // in ISR |
mbed_official | 49:77c8e4604045 | 1826 | return isrSemaphoreRelease(semaphore_id); |
c1728p9 | 123:58563e6cba1e | 1827 | } else { // in Thread |
mbed_official | 49:77c8e4604045 | 1828 | return __svcSemaphoreRelease(semaphore_id); |
mbed_official | 49:77c8e4604045 | 1829 | } |
mbed_official | 49:77c8e4604045 | 1830 | } |
mbed_official | 49:77c8e4604045 | 1831 | |
mbed_official | 49:77c8e4604045 | 1832 | /// Delete a Semaphore that was created by osSemaphoreCreate |
mbed_official | 49:77c8e4604045 | 1833 | osStatus osSemaphoreDelete (osSemaphoreId semaphore_id) { |
c1728p9 | 123:58563e6cba1e | 1834 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1835 | return osErrorISR; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1836 | } |
mbed_official | 49:77c8e4604045 | 1837 | return __svcSemaphoreDelete(semaphore_id); |
mbed_official | 49:77c8e4604045 | 1838 | } |
mbed_official | 49:77c8e4604045 | 1839 | |
mbed_official | 49:77c8e4604045 | 1840 | |
mbed_official | 49:77c8e4604045 | 1841 | // ==== Memory Management Functions ==== |
mbed_official | 49:77c8e4604045 | 1842 | |
mbed_official | 49:77c8e4604045 | 1843 | // Memory Management Helper Functions |
mbed_official | 49:77c8e4604045 | 1844 | |
mbed_official | 49:77c8e4604045 | 1845 | // Clear Memory Box (Zero init) |
mbed_official | 49:77c8e4604045 | 1846 | static void rt_clr_box (void *box_mem, void *box) { |
mbed_official | 49:77c8e4604045 | 1847 | uint32_t *p, n; |
mbed_official | 49:77c8e4604045 | 1848 | |
mbed_official | 112:53ace74b190c | 1849 | if ((box_mem != NULL) && (box != NULL)) { |
mbed_official | 49:77c8e4604045 | 1850 | p = box; |
mbed_official | 112:53ace74b190c | 1851 | for (n = ((P_BM)box_mem)->blk_size; n; n -= 4U) { |
mbed_official | 112:53ace74b190c | 1852 | *p++ = 0U; |
mbed_official | 49:77c8e4604045 | 1853 | } |
mbed_official | 49:77c8e4604045 | 1854 | } |
mbed_official | 49:77c8e4604045 | 1855 | } |
mbed_official | 49:77c8e4604045 | 1856 | |
mbed_official | 49:77c8e4604045 | 1857 | // Memory Management Service Calls declarations |
mbed_official | 112:53ace74b190c | 1858 | SVC_1_1(svcPoolCreate, osPoolId, const osPoolDef_t *, RET_pointer) |
mbed_official | 112:53ace74b190c | 1859 | SVC_1_1(sysPoolAlloc, void *, osPoolId, RET_pointer) |
mbed_official | 112:53ace74b190c | 1860 | SVC_2_1(sysPoolFree, osStatus, osPoolId, void *, RET_osStatus) |
mbed_official | 49:77c8e4604045 | 1861 | |
mbed_official | 49:77c8e4604045 | 1862 | // Memory Management Service & ISR Calls |
mbed_official | 49:77c8e4604045 | 1863 | |
mbed_official | 49:77c8e4604045 | 1864 | /// Create and Initialize memory pool |
mbed_official | 49:77c8e4604045 | 1865 | osPoolId svcPoolCreate (const osPoolDef_t *pool_def) { |
mbed_official | 49:77c8e4604045 | 1866 | uint32_t blk_sz; |
mbed_official | 49:77c8e4604045 | 1867 | |
mbed_official | 49:77c8e4604045 | 1868 | if ((pool_def == NULL) || |
mbed_official | 112:53ace74b190c | 1869 | (pool_def->pool_sz == 0U) || |
mbed_official | 112:53ace74b190c | 1870 | (pool_def->item_sz == 0U) || |
mbed_official | 49:77c8e4604045 | 1871 | (pool_def->pool == NULL)) { |
mbed_official | 49:77c8e4604045 | 1872 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 1873 | return NULL; |
mbed_official | 49:77c8e4604045 | 1874 | } |
mbed_official | 49:77c8e4604045 | 1875 | |
mbed_official | 112:53ace74b190c | 1876 | blk_sz = (pool_def->item_sz + 3U) & (uint32_t)~3U; |
mbed_official | 49:77c8e4604045 | 1877 | |
mbed_official | 112:53ace74b190c | 1878 | _init_box(pool_def->pool, sizeof(struct OS_BM) + (pool_def->pool_sz * blk_sz), blk_sz); |
mbed_official | 49:77c8e4604045 | 1879 | |
mbed_official | 49:77c8e4604045 | 1880 | return pool_def->pool; |
mbed_official | 49:77c8e4604045 | 1881 | } |
mbed_official | 49:77c8e4604045 | 1882 | |
mbed_official | 49:77c8e4604045 | 1883 | /// Allocate a memory block from a memory pool |
mbed_official | 112:53ace74b190c | 1884 | void *sysPoolAlloc (osPoolId pool_id) { |
mbed_official | 112:53ace74b190c | 1885 | void *mem; |
mbed_official | 49:77c8e4604045 | 1886 | |
mbed_official | 112:53ace74b190c | 1887 | if (pool_id == NULL) { |
mbed_official | 112:53ace74b190c | 1888 | return NULL; |
mbed_official | 49:77c8e4604045 | 1889 | } |
mbed_official | 49:77c8e4604045 | 1890 | |
mbed_official | 112:53ace74b190c | 1891 | mem = rt_alloc_box(pool_id); |
mbed_official | 112:53ace74b190c | 1892 | |
mbed_official | 112:53ace74b190c | 1893 | return mem; |
mbed_official | 49:77c8e4604045 | 1894 | } |
mbed_official | 49:77c8e4604045 | 1895 | |
mbed_official | 49:77c8e4604045 | 1896 | /// Return an allocated memory block back to a specific memory pool |
mbed_official | 49:77c8e4604045 | 1897 | osStatus sysPoolFree (osPoolId pool_id, void *block) { |
mbed_official | 112:53ace74b190c | 1898 | uint32_t res; |
mbed_official | 112:53ace74b190c | 1899 | |
mbed_official | 112:53ace74b190c | 1900 | if (pool_id == NULL) { |
mbed_official | 112:53ace74b190c | 1901 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1902 | } |
mbed_official | 49:77c8e4604045 | 1903 | |
mbed_official | 49:77c8e4604045 | 1904 | res = rt_free_box(pool_id, block); |
mbed_official | 112:53ace74b190c | 1905 | if (res != 0) { |
mbed_official | 112:53ace74b190c | 1906 | return osErrorValue; |
mbed_official | 112:53ace74b190c | 1907 | } |
mbed_official | 49:77c8e4604045 | 1908 | |
mbed_official | 49:77c8e4604045 | 1909 | return osOK; |
mbed_official | 49:77c8e4604045 | 1910 | } |
mbed_official | 49:77c8e4604045 | 1911 | |
mbed_official | 49:77c8e4604045 | 1912 | |
mbed_official | 49:77c8e4604045 | 1913 | // Memory Management Public API |
mbed_official | 49:77c8e4604045 | 1914 | |
mbed_official | 49:77c8e4604045 | 1915 | /// Create and Initialize memory pool |
mbed_official | 112:53ace74b190c | 1916 | osPoolId osPoolCreate (const osPoolDef_t *pool_def) { |
c1728p9 | 123:58563e6cba1e | 1917 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 1918 | return NULL; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 1919 | } |
mbed_official | 112:53ace74b190c | 1920 | if (((__get_CONTROL() & 1U) == 0U) && (os_running == 0U)) { |
mbed_official | 49:77c8e4604045 | 1921 | // Privileged and not running |
mbed_official | 49:77c8e4604045 | 1922 | return svcPoolCreate(pool_def); |
mbed_official | 49:77c8e4604045 | 1923 | } else { |
mbed_official | 49:77c8e4604045 | 1924 | return __svcPoolCreate(pool_def); |
mbed_official | 49:77c8e4604045 | 1925 | } |
mbed_official | 49:77c8e4604045 | 1926 | } |
mbed_official | 49:77c8e4604045 | 1927 | |
mbed_official | 49:77c8e4604045 | 1928 | /// Allocate a memory block from a memory pool |
mbed_official | 49:77c8e4604045 | 1929 | void *osPoolAlloc (osPoolId pool_id) { |
c1728p9 | 123:58563e6cba1e | 1930 | if ((__get_PRIMASK() != 0U || __get_IPSR() != 0U) || ((__get_CONTROL() & 1U) == 0U)) { // in ISR or Privileged |
mbed_official | 112:53ace74b190c | 1931 | return sysPoolAlloc(pool_id); |
mbed_official | 49:77c8e4604045 | 1932 | } else { // in Thread |
mbed_official | 112:53ace74b190c | 1933 | return __sysPoolAlloc(pool_id); |
mbed_official | 49:77c8e4604045 | 1934 | } |
mbed_official | 49:77c8e4604045 | 1935 | } |
mbed_official | 49:77c8e4604045 | 1936 | |
mbed_official | 49:77c8e4604045 | 1937 | /// Allocate a memory block from a memory pool and set memory block to zero |
mbed_official | 49:77c8e4604045 | 1938 | void *osPoolCAlloc (osPoolId pool_id) { |
mbed_official | 112:53ace74b190c | 1939 | void *mem; |
mbed_official | 112:53ace74b190c | 1940 | |
c1728p9 | 123:58563e6cba1e | 1941 | if ((__get_PRIMASK() != 0U || __get_IPSR() != 0U) || ((__get_CONTROL() & 1U) == 0U)) { // in ISR or Privileged |
mbed_official | 112:53ace74b190c | 1942 | mem = sysPoolAlloc(pool_id); |
mbed_official | 49:77c8e4604045 | 1943 | } else { // in Thread |
mbed_official | 112:53ace74b190c | 1944 | mem = __sysPoolAlloc(pool_id); |
mbed_official | 49:77c8e4604045 | 1945 | } |
mbed_official | 112:53ace74b190c | 1946 | |
mbed_official | 112:53ace74b190c | 1947 | rt_clr_box(pool_id, mem); |
mbed_official | 112:53ace74b190c | 1948 | |
mbed_official | 112:53ace74b190c | 1949 | return mem; |
mbed_official | 49:77c8e4604045 | 1950 | } |
mbed_official | 49:77c8e4604045 | 1951 | |
mbed_official | 49:77c8e4604045 | 1952 | /// Return an allocated memory block back to a specific memory pool |
mbed_official | 49:77c8e4604045 | 1953 | osStatus osPoolFree (osPoolId pool_id, void *block) { |
c1728p9 | 123:58563e6cba1e | 1954 | if ((__get_PRIMASK() != 0U || __get_IPSR() != 0U) || ((__get_CONTROL() & 1U) == 0U)) { // in ISR or Privileged |
mbed_official | 49:77c8e4604045 | 1955 | return sysPoolFree(pool_id, block); |
mbed_official | 49:77c8e4604045 | 1956 | } else { // in Thread |
mbed_official | 49:77c8e4604045 | 1957 | return __sysPoolFree(pool_id, block); |
mbed_official | 49:77c8e4604045 | 1958 | } |
mbed_official | 49:77c8e4604045 | 1959 | } |
mbed_official | 49:77c8e4604045 | 1960 | |
mbed_official | 49:77c8e4604045 | 1961 | |
mbed_official | 49:77c8e4604045 | 1962 | // ==== Message Queue Management Functions ==== |
mbed_official | 49:77c8e4604045 | 1963 | |
mbed_official | 49:77c8e4604045 | 1964 | // Message Queue Management Service Calls declarations |
mbed_official | 112:53ace74b190c | 1965 | SVC_2_1(svcMessageCreate, osMessageQId, const osMessageQDef_t *, osThreadId, RET_pointer) |
mbed_official | 112:53ace74b190c | 1966 | SVC_3_1(svcMessagePut, osStatus, osMessageQId, uint32_t, uint32_t, RET_osStatus) |
mbed_official | 112:53ace74b190c | 1967 | SVC_2_3(svcMessageGet, os_InRegs osEvent, osMessageQId, uint32_t, RET_osEvent) |
mbed_official | 49:77c8e4604045 | 1968 | |
mbed_official | 49:77c8e4604045 | 1969 | // Message Queue Service Calls |
mbed_official | 49:77c8e4604045 | 1970 | |
mbed_official | 49:77c8e4604045 | 1971 | /// Create and Initialize Message Queue |
mbed_official | 112:53ace74b190c | 1972 | osMessageQId svcMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id) { |
mbed_official | 49:77c8e4604045 | 1973 | |
mbed_official | 49:77c8e4604045 | 1974 | if ((queue_def == NULL) || |
mbed_official | 112:53ace74b190c | 1975 | (queue_def->queue_sz == 0U) || |
mbed_official | 49:77c8e4604045 | 1976 | (queue_def->pool == NULL)) { |
mbed_official | 49:77c8e4604045 | 1977 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 1978 | return NULL; |
mbed_official | 49:77c8e4604045 | 1979 | } |
mbed_official | 112:53ace74b190c | 1980 | |
mbed_official | 112:53ace74b190c | 1981 | if (((P_MCB)queue_def->pool)->cb_type != 0U) { |
mbed_official | 49:77c8e4604045 | 1982 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 1983 | return NULL; |
mbed_official | 49:77c8e4604045 | 1984 | } |
mbed_official | 49:77c8e4604045 | 1985 | |
mbed_official | 112:53ace74b190c | 1986 | rt_mbx_init(queue_def->pool, (uint16_t)(4U*(queue_def->queue_sz + 4U))); |
mbed_official | 49:77c8e4604045 | 1987 | |
mbed_official | 49:77c8e4604045 | 1988 | return queue_def->pool; |
mbed_official | 49:77c8e4604045 | 1989 | } |
mbed_official | 49:77c8e4604045 | 1990 | |
mbed_official | 49:77c8e4604045 | 1991 | /// Put a Message to a Queue |
mbed_official | 49:77c8e4604045 | 1992 | osStatus svcMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 1993 | OS_RESULT res; |
mbed_official | 49:77c8e4604045 | 1994 | |
mbed_official | 112:53ace74b190c | 1995 | if (queue_id == NULL) { |
mbed_official | 112:53ace74b190c | 1996 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 1997 | } |
mbed_official | 49:77c8e4604045 | 1998 | |
mbed_official | 112:53ace74b190c | 1999 | if (((P_MCB)queue_id)->cb_type != MCB) { |
mbed_official | 112:53ace74b190c | 2000 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 2001 | } |
mbed_official | 49:77c8e4604045 | 2002 | |
mbed_official | 49:77c8e4604045 | 2003 | res = rt_mbx_send(queue_id, (void *)info, rt_ms2tick(millisec)); |
mbed_official | 49:77c8e4604045 | 2004 | |
mbed_official | 49:77c8e4604045 | 2005 | if (res == OS_R_TMO) { |
mbed_official | 112:53ace74b190c | 2006 | return ((millisec != 0U) ? osErrorTimeoutResource : osErrorResource); |
mbed_official | 49:77c8e4604045 | 2007 | } |
mbed_official | 49:77c8e4604045 | 2008 | |
mbed_official | 49:77c8e4604045 | 2009 | return osOK; |
mbed_official | 49:77c8e4604045 | 2010 | } |
mbed_official | 49:77c8e4604045 | 2011 | |
mbed_official | 49:77c8e4604045 | 2012 | /// Get a Message or Wait for a Message from a Queue |
mbed_official | 49:77c8e4604045 | 2013 | os_InRegs osEvent_type svcMessageGet (osMessageQId queue_id, uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 2014 | OS_RESULT res; |
mbed_official | 49:77c8e4604045 | 2015 | osEvent ret; |
mbed_official | 49:77c8e4604045 | 2016 | |
mbed_official | 49:77c8e4604045 | 2017 | if (queue_id == NULL) { |
mbed_official | 49:77c8e4604045 | 2018 | ret.status = osErrorParameter; |
mbed_official | 49:77c8e4604045 | 2019 | return osEvent_ret_status; |
mbed_official | 49:77c8e4604045 | 2020 | } |
mbed_official | 49:77c8e4604045 | 2021 | |
mbed_official | 49:77c8e4604045 | 2022 | if (((P_MCB)queue_id)->cb_type != MCB) { |
mbed_official | 49:77c8e4604045 | 2023 | ret.status = osErrorParameter; |
mbed_official | 49:77c8e4604045 | 2024 | return osEvent_ret_status; |
mbed_official | 49:77c8e4604045 | 2025 | } |
mbed_official | 49:77c8e4604045 | 2026 | |
mbed_official | 49:77c8e4604045 | 2027 | res = rt_mbx_wait(queue_id, &ret.value.p, rt_ms2tick(millisec)); |
mbed_official | 112:53ace74b190c | 2028 | |
mbed_official | 49:77c8e4604045 | 2029 | if (res == OS_R_TMO) { |
mbed_official | 112:53ace74b190c | 2030 | ret.status = (millisec != 0U) ? osEventTimeout : osOK; |
mbed_official | 49:77c8e4604045 | 2031 | return osEvent_ret_value; |
mbed_official | 49:77c8e4604045 | 2032 | } |
mbed_official | 49:77c8e4604045 | 2033 | |
mbed_official | 49:77c8e4604045 | 2034 | ret.status = osEventMessage; |
mbed_official | 49:77c8e4604045 | 2035 | |
mbed_official | 49:77c8e4604045 | 2036 | return osEvent_ret_value; |
mbed_official | 49:77c8e4604045 | 2037 | } |
mbed_official | 49:77c8e4604045 | 2038 | |
mbed_official | 49:77c8e4604045 | 2039 | |
mbed_official | 49:77c8e4604045 | 2040 | // Message Queue ISR Calls |
mbed_official | 49:77c8e4604045 | 2041 | |
mbed_official | 49:77c8e4604045 | 2042 | /// Put a Message to a Queue |
mbed_official | 112:53ace74b190c | 2043 | osStatus isrMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 2044 | |
mbed_official | 112:53ace74b190c | 2045 | if ((queue_id == NULL) || (millisec != 0U)) { |
mbed_official | 49:77c8e4604045 | 2046 | return osErrorParameter; |
mbed_official | 49:77c8e4604045 | 2047 | } |
mbed_official | 49:77c8e4604045 | 2048 | |
mbed_official | 112:53ace74b190c | 2049 | if (((P_MCB)queue_id)->cb_type != MCB) { |
mbed_official | 112:53ace74b190c | 2050 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 2051 | } |
mbed_official | 49:77c8e4604045 | 2052 | |
mbed_official | 112:53ace74b190c | 2053 | if (rt_mbx_check(queue_id) == 0U) { // Check if Queue is full |
mbed_official | 49:77c8e4604045 | 2054 | return osErrorResource; |
mbed_official | 49:77c8e4604045 | 2055 | } |
mbed_official | 49:77c8e4604045 | 2056 | |
mbed_official | 49:77c8e4604045 | 2057 | isr_mbx_send(queue_id, (void *)info); |
mbed_official | 49:77c8e4604045 | 2058 | |
mbed_official | 49:77c8e4604045 | 2059 | return osOK; |
mbed_official | 49:77c8e4604045 | 2060 | } |
mbed_official | 49:77c8e4604045 | 2061 | |
mbed_official | 49:77c8e4604045 | 2062 | /// Get a Message or Wait for a Message from a Queue |
mbed_official | 112:53ace74b190c | 2063 | os_InRegs osEvent isrMessageGet (osMessageQId queue_id, uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 2064 | OS_RESULT res; |
mbed_official | 49:77c8e4604045 | 2065 | osEvent ret; |
mbed_official | 49:77c8e4604045 | 2066 | |
mbed_official | 112:53ace74b190c | 2067 | if ((queue_id == NULL) || (millisec != 0U)) { |
mbed_official | 49:77c8e4604045 | 2068 | ret.status = osErrorParameter; |
mbed_official | 49:77c8e4604045 | 2069 | return ret; |
mbed_official | 49:77c8e4604045 | 2070 | } |
mbed_official | 49:77c8e4604045 | 2071 | |
mbed_official | 49:77c8e4604045 | 2072 | if (((P_MCB)queue_id)->cb_type != MCB) { |
mbed_official | 49:77c8e4604045 | 2073 | ret.status = osErrorParameter; |
mbed_official | 49:77c8e4604045 | 2074 | return ret; |
mbed_official | 49:77c8e4604045 | 2075 | } |
mbed_official | 49:77c8e4604045 | 2076 | |
mbed_official | 49:77c8e4604045 | 2077 | res = isr_mbx_receive(queue_id, &ret.value.p); |
mbed_official | 112:53ace74b190c | 2078 | |
mbed_official | 49:77c8e4604045 | 2079 | if (res != OS_R_MBX) { |
mbed_official | 49:77c8e4604045 | 2080 | ret.status = osOK; |
mbed_official | 49:77c8e4604045 | 2081 | return ret; |
mbed_official | 49:77c8e4604045 | 2082 | } |
mbed_official | 49:77c8e4604045 | 2083 | |
mbed_official | 112:53ace74b190c | 2084 | ret.status = osEventMessage; |
mbed_official | 49:77c8e4604045 | 2085 | |
mbed_official | 49:77c8e4604045 | 2086 | return ret; |
mbed_official | 49:77c8e4604045 | 2087 | } |
mbed_official | 49:77c8e4604045 | 2088 | |
mbed_official | 49:77c8e4604045 | 2089 | |
mbed_official | 49:77c8e4604045 | 2090 | // Message Queue Management Public API |
mbed_official | 49:77c8e4604045 | 2091 | |
mbed_official | 49:77c8e4604045 | 2092 | /// Create and Initialize Message Queue |
mbed_official | 112:53ace74b190c | 2093 | osMessageQId osMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id) { |
c1728p9 | 123:58563e6cba1e | 2094 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 2095 | return NULL; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 2096 | } |
mbed_official | 112:53ace74b190c | 2097 | if (((__get_CONTROL() & 1U) == 0U) && (os_running == 0U)) { |
mbed_official | 49:77c8e4604045 | 2098 | // Privileged and not running |
mbed_official | 49:77c8e4604045 | 2099 | return svcMessageCreate(queue_def, thread_id); |
mbed_official | 49:77c8e4604045 | 2100 | } else { |
mbed_official | 49:77c8e4604045 | 2101 | return __svcMessageCreate(queue_def, thread_id); |
mbed_official | 49:77c8e4604045 | 2102 | } |
mbed_official | 49:77c8e4604045 | 2103 | } |
mbed_official | 49:77c8e4604045 | 2104 | |
mbed_official | 49:77c8e4604045 | 2105 | /// Put a Message to a Queue |
mbed_official | 49:77c8e4604045 | 2106 | osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) { |
c1728p9 | 123:58563e6cba1e | 2107 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { // in ISR |
mbed_official | 49:77c8e4604045 | 2108 | return isrMessagePut(queue_id, info, millisec); |
mbed_official | 49:77c8e4604045 | 2109 | } else { // in Thread |
mbed_official | 49:77c8e4604045 | 2110 | return __svcMessagePut(queue_id, info, millisec); |
mbed_official | 49:77c8e4604045 | 2111 | } |
mbed_official | 49:77c8e4604045 | 2112 | } |
mbed_official | 49:77c8e4604045 | 2113 | |
mbed_official | 49:77c8e4604045 | 2114 | /// Get a Message or Wait for a Message from a Queue |
mbed_official | 49:77c8e4604045 | 2115 | os_InRegs osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec) { |
c1728p9 | 123:58563e6cba1e | 2116 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { // in ISR |
mbed_official | 49:77c8e4604045 | 2117 | return isrMessageGet(queue_id, millisec); |
mbed_official | 49:77c8e4604045 | 2118 | } else { // in Thread |
mbed_official | 49:77c8e4604045 | 2119 | return __svcMessageGet(queue_id, millisec); |
mbed_official | 49:77c8e4604045 | 2120 | } |
mbed_official | 49:77c8e4604045 | 2121 | } |
mbed_official | 49:77c8e4604045 | 2122 | |
mbed_official | 49:77c8e4604045 | 2123 | |
mbed_official | 49:77c8e4604045 | 2124 | // ==== Mail Queue Management Functions ==== |
mbed_official | 49:77c8e4604045 | 2125 | |
mbed_official | 49:77c8e4604045 | 2126 | // Mail Queue Management Service Calls declarations |
mbed_official | 112:53ace74b190c | 2127 | SVC_2_1(svcMailCreate, osMailQId, const osMailQDef_t *, osThreadId, RET_pointer) |
mbed_official | 112:53ace74b190c | 2128 | SVC_3_1(sysMailAlloc, void *, osMailQId, uint32_t, uint32_t, RET_pointer) |
mbed_official | 112:53ace74b190c | 2129 | SVC_3_1(sysMailFree, osStatus, osMailQId, void *, uint32_t, RET_osStatus) |
mbed_official | 49:77c8e4604045 | 2130 | |
mbed_official | 49:77c8e4604045 | 2131 | // Mail Queue Management Service & ISR Calls |
mbed_official | 49:77c8e4604045 | 2132 | |
mbed_official | 49:77c8e4604045 | 2133 | /// Create and Initialize mail queue |
mbed_official | 112:53ace74b190c | 2134 | osMailQId svcMailCreate (const osMailQDef_t *queue_def, osThreadId thread_id) { |
mbed_official | 49:77c8e4604045 | 2135 | uint32_t blk_sz; |
mbed_official | 49:77c8e4604045 | 2136 | P_MCB pmcb; |
mbed_official | 49:77c8e4604045 | 2137 | void *pool; |
mbed_official | 49:77c8e4604045 | 2138 | |
mbed_official | 49:77c8e4604045 | 2139 | if ((queue_def == NULL) || |
mbed_official | 112:53ace74b190c | 2140 | (queue_def->queue_sz == 0U) || |
mbed_official | 112:53ace74b190c | 2141 | (queue_def->item_sz == 0U) || |
mbed_official | 49:77c8e4604045 | 2142 | (queue_def->pool == NULL)) { |
mbed_official | 49:77c8e4604045 | 2143 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 2144 | return NULL; |
mbed_official | 49:77c8e4604045 | 2145 | } |
mbed_official | 49:77c8e4604045 | 2146 | |
mbed_official | 49:77c8e4604045 | 2147 | pmcb = *(((void **)queue_def->pool) + 0); |
mbed_official | 49:77c8e4604045 | 2148 | pool = *(((void **)queue_def->pool) + 1); |
mbed_official | 49:77c8e4604045 | 2149 | |
mbed_official | 112:53ace74b190c | 2150 | if ((pool == NULL) || (pmcb == NULL) || (pmcb->cb_type != 0U)) { |
mbed_official | 49:77c8e4604045 | 2151 | sysThreadError(osErrorParameter); |
mbed_official | 49:77c8e4604045 | 2152 | return NULL; |
mbed_official | 49:77c8e4604045 | 2153 | } |
mbed_official | 49:77c8e4604045 | 2154 | |
mbed_official | 112:53ace74b190c | 2155 | blk_sz = (queue_def->item_sz + 3U) & (uint32_t)~3U; |
mbed_official | 49:77c8e4604045 | 2156 | |
mbed_official | 112:53ace74b190c | 2157 | _init_box(pool, sizeof(struct OS_BM) + (queue_def->queue_sz * blk_sz), blk_sz); |
mbed_official | 49:77c8e4604045 | 2158 | |
mbed_official | 112:53ace74b190c | 2159 | rt_mbx_init(pmcb, (uint16_t)(4U*(queue_def->queue_sz + 4U))); |
mbed_official | 49:77c8e4604045 | 2160 | |
mbed_official | 49:77c8e4604045 | 2161 | return queue_def->pool; |
mbed_official | 49:77c8e4604045 | 2162 | } |
mbed_official | 49:77c8e4604045 | 2163 | |
mbed_official | 49:77c8e4604045 | 2164 | /// Allocate a memory block from a mail |
mbed_official | 112:53ace74b190c | 2165 | void *sysMailAlloc (osMailQId queue_id, uint32_t millisec, uint32_t isr) { |
mbed_official | 49:77c8e4604045 | 2166 | P_MCB pmcb; |
mbed_official | 49:77c8e4604045 | 2167 | void *pool; |
mbed_official | 49:77c8e4604045 | 2168 | void *mem; |
mbed_official | 49:77c8e4604045 | 2169 | |
mbed_official | 112:53ace74b190c | 2170 | if (queue_id == NULL) { |
mbed_official | 112:53ace74b190c | 2171 | return NULL; |
mbed_official | 112:53ace74b190c | 2172 | } |
mbed_official | 49:77c8e4604045 | 2173 | |
mbed_official | 49:77c8e4604045 | 2174 | pmcb = *(((void **)queue_id) + 0); |
mbed_official | 49:77c8e4604045 | 2175 | pool = *(((void **)queue_id) + 1); |
mbed_official | 49:77c8e4604045 | 2176 | |
mbed_official | 112:53ace74b190c | 2177 | if ((pool == NULL) || (pmcb == NULL)) { |
mbed_official | 112:53ace74b190c | 2178 | return NULL; |
mbed_official | 112:53ace74b190c | 2179 | } |
mbed_official | 49:77c8e4604045 | 2180 | |
mbed_official | 112:53ace74b190c | 2181 | if ((isr != 0U) && (millisec != 0U)) { |
mbed_official | 112:53ace74b190c | 2182 | return NULL; |
mbed_official | 112:53ace74b190c | 2183 | } |
mbed_official | 49:77c8e4604045 | 2184 | |
mbed_official | 49:77c8e4604045 | 2185 | mem = rt_alloc_box(pool); |
mbed_official | 49:77c8e4604045 | 2186 | |
mbed_official | 112:53ace74b190c | 2187 | if ((mem == NULL) && (millisec != 0U)) { |
mbed_official | 49:77c8e4604045 | 2188 | // Put Task to sleep when Memory not available |
mbed_official | 49:77c8e4604045 | 2189 | if (pmcb->p_lnk != NULL) { |
mbed_official | 49:77c8e4604045 | 2190 | rt_put_prio((P_XCB)pmcb, os_tsk.run); |
mbed_official | 49:77c8e4604045 | 2191 | } else { |
mbed_official | 49:77c8e4604045 | 2192 | pmcb->p_lnk = os_tsk.run; |
mbed_official | 49:77c8e4604045 | 2193 | os_tsk.run->p_lnk = NULL; |
mbed_official | 49:77c8e4604045 | 2194 | os_tsk.run->p_rlnk = (P_TCB)pmcb; |
mbed_official | 49:77c8e4604045 | 2195 | // Task is waiting to allocate a message |
mbed_official | 112:53ace74b190c | 2196 | pmcb->state = 3U; |
mbed_official | 49:77c8e4604045 | 2197 | } |
mbed_official | 49:77c8e4604045 | 2198 | rt_block(rt_ms2tick(millisec), WAIT_MBX); |
mbed_official | 49:77c8e4604045 | 2199 | } |
mbed_official | 49:77c8e4604045 | 2200 | |
mbed_official | 112:53ace74b190c | 2201 | return mem; |
mbed_official | 49:77c8e4604045 | 2202 | } |
mbed_official | 49:77c8e4604045 | 2203 | |
mbed_official | 49:77c8e4604045 | 2204 | /// Free a memory block from a mail |
mbed_official | 49:77c8e4604045 | 2205 | osStatus sysMailFree (osMailQId queue_id, void *mail, uint32_t isr) { |
mbed_official | 112:53ace74b190c | 2206 | P_MCB pmcb; |
mbed_official | 112:53ace74b190c | 2207 | P_TCB ptcb; |
mbed_official | 112:53ace74b190c | 2208 | void *pool; |
mbed_official | 112:53ace74b190c | 2209 | void *mem; |
mbed_official | 112:53ace74b190c | 2210 | uint32_t res; |
mbed_official | 49:77c8e4604045 | 2211 | |
mbed_official | 112:53ace74b190c | 2212 | if (queue_id == NULL) { |
mbed_official | 112:53ace74b190c | 2213 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 2214 | } |
mbed_official | 49:77c8e4604045 | 2215 | |
mbed_official | 49:77c8e4604045 | 2216 | pmcb = *(((void **)queue_id) + 0); |
mbed_official | 49:77c8e4604045 | 2217 | pool = *(((void **)queue_id) + 1); |
mbed_official | 49:77c8e4604045 | 2218 | |
mbed_official | 112:53ace74b190c | 2219 | if ((pmcb == NULL) || (pool == NULL)) { |
mbed_official | 112:53ace74b190c | 2220 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 2221 | } |
mbed_official | 49:77c8e4604045 | 2222 | |
mbed_official | 49:77c8e4604045 | 2223 | res = rt_free_box(pool, mail); |
mbed_official | 49:77c8e4604045 | 2224 | |
mbed_official | 112:53ace74b190c | 2225 | if (res != 0U) { |
mbed_official | 112:53ace74b190c | 2226 | return osErrorValue; |
mbed_official | 112:53ace74b190c | 2227 | } |
mbed_official | 49:77c8e4604045 | 2228 | |
mbed_official | 112:53ace74b190c | 2229 | if ((pmcb->p_lnk != NULL) && (pmcb->state == 3U)) { |
mbed_official | 49:77c8e4604045 | 2230 | // Task is waiting to allocate a message |
mbed_official | 112:53ace74b190c | 2231 | if (isr != 0U) { |
mbed_official | 49:77c8e4604045 | 2232 | rt_psq_enq (pmcb, (U32)pool); |
mbed_official | 49:77c8e4604045 | 2233 | rt_psh_req (); |
mbed_official | 49:77c8e4604045 | 2234 | } else { |
mbed_official | 49:77c8e4604045 | 2235 | mem = rt_alloc_box(pool); |
mbed_official | 49:77c8e4604045 | 2236 | if (mem != NULL) { |
mbed_official | 49:77c8e4604045 | 2237 | ptcb = rt_get_first((P_XCB)pmcb); |
mbed_official | 49:77c8e4604045 | 2238 | rt_ret_val(ptcb, (U32)mem); |
mbed_official | 49:77c8e4604045 | 2239 | rt_rmv_dly(ptcb); |
mbed_official | 49:77c8e4604045 | 2240 | rt_dispatch(ptcb); |
mbed_official | 49:77c8e4604045 | 2241 | } |
mbed_official | 49:77c8e4604045 | 2242 | } |
mbed_official | 49:77c8e4604045 | 2243 | } |
mbed_official | 49:77c8e4604045 | 2244 | |
mbed_official | 49:77c8e4604045 | 2245 | return osOK; |
mbed_official | 49:77c8e4604045 | 2246 | } |
mbed_official | 49:77c8e4604045 | 2247 | |
mbed_official | 49:77c8e4604045 | 2248 | |
mbed_official | 49:77c8e4604045 | 2249 | // Mail Queue Management Public API |
mbed_official | 49:77c8e4604045 | 2250 | |
mbed_official | 49:77c8e4604045 | 2251 | /// Create and Initialize mail queue |
mbed_official | 112:53ace74b190c | 2252 | osMailQId osMailCreate (const osMailQDef_t *queue_def, osThreadId thread_id) { |
c1728p9 | 123:58563e6cba1e | 2253 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { |
mbed_official | 112:53ace74b190c | 2254 | return NULL; // Not allowed in ISR |
mbed_official | 112:53ace74b190c | 2255 | } |
mbed_official | 112:53ace74b190c | 2256 | if (((__get_CONTROL() & 1U) == 0U) && (os_running == 0U)) { |
mbed_official | 49:77c8e4604045 | 2257 | // Privileged and not running |
mbed_official | 49:77c8e4604045 | 2258 | return svcMailCreate(queue_def, thread_id); |
mbed_official | 49:77c8e4604045 | 2259 | } else { |
mbed_official | 49:77c8e4604045 | 2260 | return __svcMailCreate(queue_def, thread_id); |
mbed_official | 49:77c8e4604045 | 2261 | } |
mbed_official | 49:77c8e4604045 | 2262 | } |
mbed_official | 49:77c8e4604045 | 2263 | |
mbed_official | 49:77c8e4604045 | 2264 | /// Allocate a memory block from a mail |
mbed_official | 49:77c8e4604045 | 2265 | void *osMailAlloc (osMailQId queue_id, uint32_t millisec) { |
c1728p9 | 123:58563e6cba1e | 2266 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { // in ISR |
mbed_official | 112:53ace74b190c | 2267 | return sysMailAlloc(queue_id, millisec, 1U); |
mbed_official | 49:77c8e4604045 | 2268 | } else { // in Thread |
mbed_official | 112:53ace74b190c | 2269 | return __sysMailAlloc(queue_id, millisec, 0U); |
mbed_official | 49:77c8e4604045 | 2270 | } |
mbed_official | 49:77c8e4604045 | 2271 | } |
mbed_official | 49:77c8e4604045 | 2272 | |
mbed_official | 49:77c8e4604045 | 2273 | /// Allocate a memory block from a mail and set memory block to zero |
mbed_official | 49:77c8e4604045 | 2274 | void *osMailCAlloc (osMailQId queue_id, uint32_t millisec) { |
mbed_official | 112:53ace74b190c | 2275 | void *pool; |
mbed_official | 112:53ace74b190c | 2276 | void *mem; |
mbed_official | 112:53ace74b190c | 2277 | |
c1728p9 | 123:58563e6cba1e | 2278 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { // in ISR |
mbed_official | 112:53ace74b190c | 2279 | mem = sysMailAlloc(queue_id, millisec, 1U); |
mbed_official | 49:77c8e4604045 | 2280 | } else { // in Thread |
mbed_official | 112:53ace74b190c | 2281 | mem = __sysMailAlloc(queue_id, millisec, 0U); |
mbed_official | 49:77c8e4604045 | 2282 | } |
mbed_official | 112:53ace74b190c | 2283 | |
mbed_official | 112:53ace74b190c | 2284 | pool = *(((void **)queue_id) + 1); |
mbed_official | 112:53ace74b190c | 2285 | |
mbed_official | 112:53ace74b190c | 2286 | rt_clr_box(pool, mem); |
mbed_official | 112:53ace74b190c | 2287 | |
mbed_official | 112:53ace74b190c | 2288 | return mem; |
mbed_official | 49:77c8e4604045 | 2289 | } |
mbed_official | 49:77c8e4604045 | 2290 | |
mbed_official | 49:77c8e4604045 | 2291 | /// Free a memory block from a mail |
mbed_official | 49:77c8e4604045 | 2292 | osStatus osMailFree (osMailQId queue_id, void *mail) { |
c1728p9 | 123:58563e6cba1e | 2293 | if (__get_PRIMASK() != 0U || __get_IPSR() != 0U) { // in ISR |
mbed_official | 112:53ace74b190c | 2294 | return sysMailFree(queue_id, mail, 1U); |
mbed_official | 49:77c8e4604045 | 2295 | } else { // in Thread |
mbed_official | 112:53ace74b190c | 2296 | return __sysMailFree(queue_id, mail, 0U); |
mbed_official | 49:77c8e4604045 | 2297 | } |
mbed_official | 49:77c8e4604045 | 2298 | } |
mbed_official | 49:77c8e4604045 | 2299 | |
mbed_official | 49:77c8e4604045 | 2300 | /// Put a mail to a queue |
mbed_official | 49:77c8e4604045 | 2301 | osStatus osMailPut (osMailQId queue_id, void *mail) { |
mbed_official | 112:53ace74b190c | 2302 | if (queue_id == NULL) { |
mbed_official | 112:53ace74b190c | 2303 | return osErrorParameter; |
mbed_official | 112:53ace74b190c | 2304 | } |
mbed_official | 112:53ace74b190c | 2305 | if (mail == NULL) { |
mbed_official | 112:53ace74b190c | 2306 | return osErrorValue; |
mbed_official | 112:53ace74b190c | 2307 | } |
mbed_official | 112:53ace74b190c | 2308 | return osMessagePut(*((void **)queue_id), (uint32_t)mail, 0U); |
mbed_official | 49:77c8e4604045 | 2309 | } |
mbed_official | 49:77c8e4604045 | 2310 | |
mbed_official | 49:77c8e4604045 | 2311 | /// Get a mail from a queue |
mbed_official | 49:77c8e4604045 | 2312 | os_InRegs osEvent osMailGet (osMailQId queue_id, uint32_t millisec) { |
mbed_official | 49:77c8e4604045 | 2313 | osEvent ret; |
mbed_official | 49:77c8e4604045 | 2314 | |
mbed_official | 49:77c8e4604045 | 2315 | if (queue_id == NULL) { |
mbed_official | 49:77c8e4604045 | 2316 | ret.status = osErrorParameter; |
mbed_official | 49:77c8e4604045 | 2317 | return ret; |
mbed_official | 49:77c8e4604045 | 2318 | } |
mbed_official | 49:77c8e4604045 | 2319 | |
mbed_official | 49:77c8e4604045 | 2320 | ret = osMessageGet(*((void **)queue_id), millisec); |
mbed_official | 49:77c8e4604045 | 2321 | if (ret.status == osEventMessage) ret.status = osEventMail; |
mbed_official | 49:77c8e4604045 | 2322 | |
mbed_official | 49:77c8e4604045 | 2323 | return ret; |
mbed_official | 49:77c8e4604045 | 2324 | } |
mbed_official | 112:53ace74b190c | 2325 | |
mbed_official | 112:53ace74b190c | 2326 | |
mbed_official | 112:53ace74b190c | 2327 | // ==== RTX Extensions ==== |
mbed_official | 112:53ace74b190c | 2328 | |
mbed_official | 112:53ace74b190c | 2329 | // Service Calls declarations |
mbed_official | 112:53ace74b190c | 2330 | SVC_0_1(rt_suspend, uint32_t, RET_uint32_t) |
mbed_official | 112:53ace74b190c | 2331 | SVC_1_0(rt_resume, void, uint32_t) |
mbed_official | 112:53ace74b190c | 2332 | |
mbed_official | 112:53ace74b190c | 2333 | |
mbed_official | 112:53ace74b190c | 2334 | // Public API |
mbed_official | 112:53ace74b190c | 2335 | |
mbed_official | 112:53ace74b190c | 2336 | /// Suspends the OS task scheduler |
mbed_official | 112:53ace74b190c | 2337 | uint32_t os_suspend (void) { |
mbed_official | 112:53ace74b190c | 2338 | return __rt_suspend(); |
mbed_official | 112:53ace74b190c | 2339 | } |
mbed_official | 112:53ace74b190c | 2340 | |
mbed_official | 112:53ace74b190c | 2341 | /// Resumes the OS task scheduler |
mbed_official | 112:53ace74b190c | 2342 | void os_resume (uint32_t sleep_time) { |
mbed_official | 112:53ace74b190c | 2343 | __rt_resume(sleep_time); |
mbed_official | 112:53ace74b190c | 2344 | } |