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