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