RTC auf true
rtos/TARGET_CORTEX/TOOLCHAIN_ARM_STD/mbed_boot_arm_std.c@0:38ceb79fef03, 2018-11-28 (annotated)
- Committer:
- kevman
- Date:
- Wed Nov 28 15:10:15 2018 +0000
- Revision:
- 0:38ceb79fef03
RTC modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevman | 0:38ceb79fef03 | 1 | /* mbed Microcontroller Library |
kevman | 0:38ceb79fef03 | 2 | * Copyright (c) 2018-2018 ARM Limited |
kevman | 0:38ceb79fef03 | 3 | * |
kevman | 0:38ceb79fef03 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kevman | 0:38ceb79fef03 | 5 | * you may not use this file except in compliance with the License. |
kevman | 0:38ceb79fef03 | 6 | * You may obtain a copy of the License at |
kevman | 0:38ceb79fef03 | 7 | * |
kevman | 0:38ceb79fef03 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kevman | 0:38ceb79fef03 | 9 | * |
kevman | 0:38ceb79fef03 | 10 | * Unless required by applicable law or agreed to in writing, software |
kevman | 0:38ceb79fef03 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kevman | 0:38ceb79fef03 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kevman | 0:38ceb79fef03 | 13 | * See the License for the specific language governing permissions and |
kevman | 0:38ceb79fef03 | 14 | * limitations under the License. |
kevman | 0:38ceb79fef03 | 15 | */ |
kevman | 0:38ceb79fef03 | 16 | #include <stdlib.h> |
kevman | 0:38ceb79fef03 | 17 | #include <string.h> |
kevman | 0:38ceb79fef03 | 18 | |
kevman | 0:38ceb79fef03 | 19 | #include "cmsis.h" |
kevman | 0:38ceb79fef03 | 20 | #include "mbed_critical.h" |
kevman | 0:38ceb79fef03 | 21 | #include "mbed_boot.h" |
kevman | 0:38ceb79fef03 | 22 | #include <rt_misc.h> |
kevman | 0:38ceb79fef03 | 23 | #include "mbed_rtos_storage.h" |
kevman | 0:38ceb79fef03 | 24 | #include "cmsis_os2.h" |
kevman | 0:38ceb79fef03 | 25 | |
kevman | 0:38ceb79fef03 | 26 | __value_in_regs struct __argc_argv __rt_lib_init(unsigned heapbase, unsigned heaptop); |
kevman | 0:38ceb79fef03 | 27 | void _platform_post_stackheap_init(void); |
kevman | 0:38ceb79fef03 | 28 | |
kevman | 0:38ceb79fef03 | 29 | #if !defined(HEAP_START) |
kevman | 0:38ceb79fef03 | 30 | /* Defined by linker script */ |
kevman | 0:38ceb79fef03 | 31 | extern uint32_t Image$$RW_IRAM1$$ZI$$Limit[]; |
kevman | 0:38ceb79fef03 | 32 | #define HEAP_START ((unsigned char*)Image$$RW_IRAM1$$ZI$$Limit) |
kevman | 0:38ceb79fef03 | 33 | #define HEAP_SIZE ((uint32_t)((uint32_t)INITIAL_SP - (uint32_t)HEAP_START)) |
kevman | 0:38ceb79fef03 | 34 | #endif |
kevman | 0:38ceb79fef03 | 35 | |
kevman | 0:38ceb79fef03 | 36 | /* |
kevman | 0:38ceb79fef03 | 37 | * Note - Overriding the function __cpp_initialize__aeabi_ for the Arm |
kevman | 0:38ceb79fef03 | 38 | * standard library causes a crash when there are no global constructors. |
kevman | 0:38ceb79fef03 | 39 | * To avoid this do not override __cpp_initialize__aeabi_ with the super$$ |
kevman | 0:38ceb79fef03 | 40 | * sub$$ mechanism for the Arm standard library. The uARM library is not |
kevman | 0:38ceb79fef03 | 41 | * effected by this problem. |
kevman | 0:38ceb79fef03 | 42 | */ |
kevman | 0:38ceb79fef03 | 43 | |
kevman | 0:38ceb79fef03 | 44 | /* |
kevman | 0:38ceb79fef03 | 45 | * mbed entry point for the ARM standard toolchain |
kevman | 0:38ceb79fef03 | 46 | * |
kevman | 0:38ceb79fef03 | 47 | * Override the ARM standard library function __rt_entry to run code earlier in |
kevman | 0:38ceb79fef03 | 48 | * the boot sequence. This is after scatter loading has taken place but before |
kevman | 0:38ceb79fef03 | 49 | * the C library has been initialized. |
kevman | 0:38ceb79fef03 | 50 | */ |
kevman | 0:38ceb79fef03 | 51 | void __rt_entry(void) |
kevman | 0:38ceb79fef03 | 52 | { |
kevman | 0:38ceb79fef03 | 53 | unsigned char *free_start = HEAP_START; |
kevman | 0:38ceb79fef03 | 54 | uint32_t free_size = HEAP_SIZE; |
kevman | 0:38ceb79fef03 | 55 | |
kevman | 0:38ceb79fef03 | 56 | #ifdef ISR_STACK_START |
kevman | 0:38ceb79fef03 | 57 | /* Interrupt stack explicitly specified */ |
kevman | 0:38ceb79fef03 | 58 | mbed_stack_isr_size = ISR_STACK_SIZE; |
kevman | 0:38ceb79fef03 | 59 | mbed_stack_isr_start = ISR_STACK_START; |
kevman | 0:38ceb79fef03 | 60 | #else |
kevman | 0:38ceb79fef03 | 61 | /* Interrupt stack - reserve space at the end of the free block */ |
kevman | 0:38ceb79fef03 | 62 | mbed_stack_isr_size = ISR_STACK_SIZE < free_size ? ISR_STACK_SIZE : free_size; |
kevman | 0:38ceb79fef03 | 63 | mbed_stack_isr_start = free_start + free_size - mbed_stack_isr_size; |
kevman | 0:38ceb79fef03 | 64 | free_size -= mbed_stack_isr_size; |
kevman | 0:38ceb79fef03 | 65 | #endif |
kevman | 0:38ceb79fef03 | 66 | |
kevman | 0:38ceb79fef03 | 67 | /* Heap - everything else */ |
kevman | 0:38ceb79fef03 | 68 | mbed_heap_size = free_size; |
kevman | 0:38ceb79fef03 | 69 | mbed_heap_start = free_start; |
kevman | 0:38ceb79fef03 | 70 | mbed_init(); |
kevman | 0:38ceb79fef03 | 71 | |
kevman | 0:38ceb79fef03 | 72 | _platform_post_stackheap_init(); |
kevman | 0:38ceb79fef03 | 73 | mbed_rtos_start(); |
kevman | 0:38ceb79fef03 | 74 | } |
kevman | 0:38ceb79fef03 | 75 | |
kevman | 0:38ceb79fef03 | 76 | void mbed_toolchain_init() |
kevman | 0:38ceb79fef03 | 77 | { |
kevman | 0:38ceb79fef03 | 78 | /* Run the C++ global object constructors */ |
kevman | 0:38ceb79fef03 | 79 | __rt_lib_init((unsigned)mbed_heap_start, (unsigned)(mbed_heap_start + mbed_heap_size)); |
kevman | 0:38ceb79fef03 | 80 | } |
kevman | 0:38ceb79fef03 | 81 | |
kevman | 0:38ceb79fef03 | 82 | /* Move all code here from RTX code base (rtx_lib.c) and do some modifications: |
kevman | 0:38ceb79fef03 | 83 | * |
kevman | 0:38ceb79fef03 | 84 | * 1. _mutex_initialize/_mutex_free are re-implemented to meet Mbed. |
kevman | 0:38ceb79fef03 | 85 | * 2. All _mutex_* functions are declared with '__USED' to avoid excluded by linker. |
kevman | 0:38ceb79fef03 | 86 | */ |
kevman | 0:38ceb79fef03 | 87 | #if defined(RTX_NO_MULTITHREAD_CLIB) |
kevman | 0:38ceb79fef03 | 88 | |
kevman | 0:38ceb79fef03 | 89 | /* The single memory model is checking for stack collision at run time, verifying |
kevman | 0:38ceb79fef03 | 90 | that the heap pointer is underneath the stack pointer. |
kevman | 0:38ceb79fef03 | 91 | With the RTOS there is not only one stack above the heap, there are multiple |
kevman | 0:38ceb79fef03 | 92 | stacks and some of them are underneath the heap pointer. |
kevman | 0:38ceb79fef03 | 93 | */ |
kevman | 0:38ceb79fef03 | 94 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
kevman | 0:38ceb79fef03 | 95 | __asm(".global __use_two_region_memory\n\t"); |
kevman | 0:38ceb79fef03 | 96 | __asm(".global __use_no_semihosting\n\t"); |
kevman | 0:38ceb79fef03 | 97 | #else |
kevman | 0:38ceb79fef03 | 98 | #pragma import(__use_two_region_memory) |
kevman | 0:38ceb79fef03 | 99 | #endif |
kevman | 0:38ceb79fef03 | 100 | |
kevman | 0:38ceb79fef03 | 101 | #define LIBSPACE_SIZE 96 |
kevman | 0:38ceb79fef03 | 102 | |
kevman | 0:38ceb79fef03 | 103 | //lint -esym(714,__user_perthread_libspace,_mutex_*) "Referenced by C library" |
kevman | 0:38ceb79fef03 | 104 | //lint -esym(765,__user_perthread_libspace,_mutex_*) "Global scope" |
kevman | 0:38ceb79fef03 | 105 | //lint -esym(9003, os_libspace*) "variables 'os_libspace*' defined at module scope" |
kevman | 0:38ceb79fef03 | 106 | |
kevman | 0:38ceb79fef03 | 107 | // Memory for libspace |
kevman | 0:38ceb79fef03 | 108 | static uint32_t os_libspace[OS_THREAD_LIBSPACE_NUM+1][LIBSPACE_SIZE/4] \ |
kevman | 0:38ceb79fef03 | 109 | __attribute__((section(".bss.os.libspace"))); |
kevman | 0:38ceb79fef03 | 110 | |
kevman | 0:38ceb79fef03 | 111 | // Thread IDs for libspace |
kevman | 0:38ceb79fef03 | 112 | static osThreadId_t os_libspace_id[OS_THREAD_LIBSPACE_NUM] \ |
kevman | 0:38ceb79fef03 | 113 | __attribute__((section(".bss.os.libspace"))); |
kevman | 0:38ceb79fef03 | 114 | |
kevman | 0:38ceb79fef03 | 115 | // Check if Kernel has been started |
kevman | 0:38ceb79fef03 | 116 | static uint32_t os_kernel_is_active (void) { |
kevman | 0:38ceb79fef03 | 117 | static uint8_t os_kernel_active = 0U; |
kevman | 0:38ceb79fef03 | 118 | |
kevman | 0:38ceb79fef03 | 119 | if (os_kernel_active == 0U) { |
kevman | 0:38ceb79fef03 | 120 | if (osKernelGetState() > osKernelReady) { |
kevman | 0:38ceb79fef03 | 121 | os_kernel_active = 1U; |
kevman | 0:38ceb79fef03 | 122 | } |
kevman | 0:38ceb79fef03 | 123 | } |
kevman | 0:38ceb79fef03 | 124 | return (uint32_t)os_kernel_active; |
kevman | 0:38ceb79fef03 | 125 | } |
kevman | 0:38ceb79fef03 | 126 | |
kevman | 0:38ceb79fef03 | 127 | // Provide libspace for current thread |
kevman | 0:38ceb79fef03 | 128 | void *__user_perthread_libspace (void) { |
kevman | 0:38ceb79fef03 | 129 | osThreadId_t id; |
kevman | 0:38ceb79fef03 | 130 | uint32_t n; |
kevman | 0:38ceb79fef03 | 131 | |
kevman | 0:38ceb79fef03 | 132 | if (os_kernel_is_active() != 0U) { |
kevman | 0:38ceb79fef03 | 133 | id = osThreadGetId(); |
kevman | 0:38ceb79fef03 | 134 | for (n = 0U; n < (uint32_t)OS_THREAD_LIBSPACE_NUM; n++) { |
kevman | 0:38ceb79fef03 | 135 | if (os_libspace_id[n] == NULL) { |
kevman | 0:38ceb79fef03 | 136 | os_libspace_id[n] = id; |
kevman | 0:38ceb79fef03 | 137 | } |
kevman | 0:38ceb79fef03 | 138 | if (os_libspace_id[n] == id) { |
kevman | 0:38ceb79fef03 | 139 | break; |
kevman | 0:38ceb79fef03 | 140 | } |
kevman | 0:38ceb79fef03 | 141 | } |
kevman | 0:38ceb79fef03 | 142 | if (n == (uint32_t)OS_THREAD_LIBSPACE_NUM) { |
kevman | 0:38ceb79fef03 | 143 | (void)osRtxErrorNotify(osRtxErrorClibSpace, id); |
kevman | 0:38ceb79fef03 | 144 | } |
kevman | 0:38ceb79fef03 | 145 | } else { |
kevman | 0:38ceb79fef03 | 146 | n = OS_THREAD_LIBSPACE_NUM; |
kevman | 0:38ceb79fef03 | 147 | } |
kevman | 0:38ceb79fef03 | 148 | |
kevman | 0:38ceb79fef03 | 149 | //lint -e{9087} "cast between pointers to different object types" |
kevman | 0:38ceb79fef03 | 150 | return (void *)&os_libspace[n][0]; |
kevman | 0:38ceb79fef03 | 151 | } |
kevman | 0:38ceb79fef03 | 152 | |
kevman | 0:38ceb79fef03 | 153 | /* ARM toolchain requires dynamically created mutexes to enforce thread safety. There's |
kevman | 0:38ceb79fef03 | 154 | up to 8 static mutexes, protecting atexit, signalinit, stdin, stdout, stderr, stream_list, |
kevman | 0:38ceb79fef03 | 155 | fp_trap_init and the heap. Additionally for each call to fopen one extra mutex will be |
kevman | 0:38ceb79fef03 | 156 | created. |
kevman | 0:38ceb79fef03 | 157 | mbed OS provides a RTX pool for 8 mutexes, to satisfy the static requirements. All |
kevman | 0:38ceb79fef03 | 158 | additional mutexes will be allocated on the heap. We can't use the heap allocation for |
kevman | 0:38ceb79fef03 | 159 | all the required mutexes, as the heap operations also require a mutex. We don't need to |
kevman | 0:38ceb79fef03 | 160 | worry about freeing the allocated memory as library mutexes are only freed when the |
kevman | 0:38ceb79fef03 | 161 | application finishes executing. |
kevman | 0:38ceb79fef03 | 162 | */ |
kevman | 0:38ceb79fef03 | 163 | |
kevman | 0:38ceb79fef03 | 164 | typedef void *mutex; |
kevman | 0:38ceb79fef03 | 165 | #define OS_MUTEX_STATIC_NUM 8 |
kevman | 0:38ceb79fef03 | 166 | mutex _static_mutexes[OS_MUTEX_STATIC_NUM] = {NULL}; |
kevman | 0:38ceb79fef03 | 167 | mbed_rtos_storage_mutex_t _static_mutexes_mem[OS_MUTEX_STATIC_NUM] = {NULL}; |
kevman | 0:38ceb79fef03 | 168 | |
kevman | 0:38ceb79fef03 | 169 | //lint -save "Function prototypes defined in C library" |
kevman | 0:38ceb79fef03 | 170 | //lint -e970 "Use of 'int' outside of a typedef" |
kevman | 0:38ceb79fef03 | 171 | //lint -e818 "Pointer 'm' could be declared as pointing to const" |
kevman | 0:38ceb79fef03 | 172 | |
kevman | 0:38ceb79fef03 | 173 | /* Initialize mutex */ |
kevman | 0:38ceb79fef03 | 174 | __USED int _mutex_initialize(mutex *m) |
kevman | 0:38ceb79fef03 | 175 | { |
kevman | 0:38ceb79fef03 | 176 | osMutexAttr_t attr; |
kevman | 0:38ceb79fef03 | 177 | memset(&attr, 0, sizeof(attr)); |
kevman | 0:38ceb79fef03 | 178 | attr.name = "ARM toolchain mutex"; |
kevman | 0:38ceb79fef03 | 179 | attr.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust; |
kevman | 0:38ceb79fef03 | 180 | |
kevman | 0:38ceb79fef03 | 181 | mutex *slot = NULL; |
kevman | 0:38ceb79fef03 | 182 | core_util_critical_section_enter(); |
kevman | 0:38ceb79fef03 | 183 | for (int i = 0; i < OS_MUTEX_STATIC_NUM; i++) { |
kevman | 0:38ceb79fef03 | 184 | if (_static_mutexes[i] == NULL) { |
kevman | 0:38ceb79fef03 | 185 | _static_mutexes[i] = (mutex)-1; // dummy value to reserve slot |
kevman | 0:38ceb79fef03 | 186 | slot = &_static_mutexes[i]; |
kevman | 0:38ceb79fef03 | 187 | //Use the static attrs |
kevman | 0:38ceb79fef03 | 188 | attr.cb_size = sizeof(mbed_rtos_storage_mutex_t); |
kevman | 0:38ceb79fef03 | 189 | attr.cb_mem = &_static_mutexes_mem[i]; |
kevman | 0:38ceb79fef03 | 190 | break; |
kevman | 0:38ceb79fef03 | 191 | } |
kevman | 0:38ceb79fef03 | 192 | } |
kevman | 0:38ceb79fef03 | 193 | core_util_critical_section_exit(); |
kevman | 0:38ceb79fef03 | 194 | |
kevman | 0:38ceb79fef03 | 195 | if (slot != NULL) { |
kevman | 0:38ceb79fef03 | 196 | *m = osMutexNew(&attr); |
kevman | 0:38ceb79fef03 | 197 | *slot = *m; |
kevman | 0:38ceb79fef03 | 198 | if (*m != NULL) { |
kevman | 0:38ceb79fef03 | 199 | return 1; |
kevman | 0:38ceb79fef03 | 200 | } |
kevman | 0:38ceb79fef03 | 201 | } |
kevman | 0:38ceb79fef03 | 202 | |
kevman | 0:38ceb79fef03 | 203 | /* Mutex pool exhausted, try using HEAP */ |
kevman | 0:38ceb79fef03 | 204 | attr.cb_size = sizeof(mbed_rtos_storage_mutex_t); |
kevman | 0:38ceb79fef03 | 205 | attr.cb_mem = (void*)malloc(attr.cb_size); |
kevman | 0:38ceb79fef03 | 206 | if (attr.cb_mem == NULL) { |
kevman | 0:38ceb79fef03 | 207 | osRtxErrorNotify(osRtxErrorClibSpace, m); |
kevman | 0:38ceb79fef03 | 208 | return 0; |
kevman | 0:38ceb79fef03 | 209 | } |
kevman | 0:38ceb79fef03 | 210 | |
kevman | 0:38ceb79fef03 | 211 | *m = osMutexNew(&attr); |
kevman | 0:38ceb79fef03 | 212 | if (*m == NULL) { |
kevman | 0:38ceb79fef03 | 213 | osRtxErrorNotify(osRtxErrorClibMutex, m); |
kevman | 0:38ceb79fef03 | 214 | return 0; |
kevman | 0:38ceb79fef03 | 215 | } |
kevman | 0:38ceb79fef03 | 216 | |
kevman | 0:38ceb79fef03 | 217 | return 1; |
kevman | 0:38ceb79fef03 | 218 | } |
kevman | 0:38ceb79fef03 | 219 | |
kevman | 0:38ceb79fef03 | 220 | /* Acquire mutex */ |
kevman | 0:38ceb79fef03 | 221 | __USED void _mutex_acquire(mutex *m) { |
kevman | 0:38ceb79fef03 | 222 | if (os_kernel_is_active() != 0U) { |
kevman | 0:38ceb79fef03 | 223 | (void)osMutexAcquire(*m, osWaitForever); |
kevman | 0:38ceb79fef03 | 224 | } |
kevman | 0:38ceb79fef03 | 225 | } |
kevman | 0:38ceb79fef03 | 226 | |
kevman | 0:38ceb79fef03 | 227 | /* Release mutex */ |
kevman | 0:38ceb79fef03 | 228 | __USED void _mutex_release(mutex *m) { |
kevman | 0:38ceb79fef03 | 229 | if (os_kernel_is_active() != 0U) { |
kevman | 0:38ceb79fef03 | 230 | (void)osMutexRelease(*m); |
kevman | 0:38ceb79fef03 | 231 | } |
kevman | 0:38ceb79fef03 | 232 | } |
kevman | 0:38ceb79fef03 | 233 | |
kevman | 0:38ceb79fef03 | 234 | /* Free mutex */ |
kevman | 0:38ceb79fef03 | 235 | __USED void _mutex_free(mutex *m) { |
kevman | 0:38ceb79fef03 | 236 | mutex *slot = NULL; |
kevman | 0:38ceb79fef03 | 237 | core_util_critical_section_enter(); |
kevman | 0:38ceb79fef03 | 238 | for (int i = 0; i < OS_MUTEX_STATIC_NUM; i++) { |
kevman | 0:38ceb79fef03 | 239 | if (_static_mutexes[i] == *m) { |
kevman | 0:38ceb79fef03 | 240 | slot = &_static_mutexes[i]; |
kevman | 0:38ceb79fef03 | 241 | break; |
kevman | 0:38ceb79fef03 | 242 | } |
kevman | 0:38ceb79fef03 | 243 | } |
kevman | 0:38ceb79fef03 | 244 | core_util_critical_section_exit(); |
kevman | 0:38ceb79fef03 | 245 | |
kevman | 0:38ceb79fef03 | 246 | osMutexDelete(*m); |
kevman | 0:38ceb79fef03 | 247 | |
kevman | 0:38ceb79fef03 | 248 | // if no slot reserved for mutex, must have been dynamically allocated |
kevman | 0:38ceb79fef03 | 249 | if (!slot) { |
kevman | 0:38ceb79fef03 | 250 | free(m); |
kevman | 0:38ceb79fef03 | 251 | } else { |
kevman | 0:38ceb79fef03 | 252 | *slot = NULL; |
kevman | 0:38ceb79fef03 | 253 | } |
kevman | 0:38ceb79fef03 | 254 | |
kevman | 0:38ceb79fef03 | 255 | } |
kevman | 0:38ceb79fef03 | 256 | |
kevman | 0:38ceb79fef03 | 257 | #endif /* RTX_NO_MULTITHREAD_CLIB */ |