Maintain legacy RTOS behavior before mbed-5
Fork of mbed-rtos by
rtx/TARGET_CORTEX_M/HAL_CM.c@123:58563e6cba1e, 2016-11-14 (annotated)
- Committer:
- c1728p9
- Date:
- Mon Nov 14 17:14:42 2016 -0600
- Revision:
- 123:58563e6cba1e
- Parent:
- 120:4dc938e301cc
Configure RTOS to behave as it did before 5.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 49:77c8e4604045 | 1 | /*---------------------------------------------------------------------------- |
mbed_official | 112:53ace74b190c | 2 | * CMSIS-RTOS - RTX |
mbed_official | 49:77c8e4604045 | 3 | *---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 4 | * Name: HAL_CM.C |
mbed_official | 49:77c8e4604045 | 5 | * Purpose: Hardware Abstraction Layer for Cortex-M |
mbed_official | 112:53ace74b190c | 6 | * Rev.: V4.79 |
mbed_official | 49:77c8e4604045 | 7 | *---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 8 | * |
mbed_official | 112:53ace74b190c | 9 | * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH |
mbed_official | 49:77c8e4604045 | 10 | * All rights reserved. |
mbed_official | 49:77c8e4604045 | 11 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 49:77c8e4604045 | 12 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 49:77c8e4604045 | 13 | * - Redistributions of source code must retain the above copyright |
mbed_official | 49:77c8e4604045 | 14 | * notice, this list of conditions and the following disclaimer. |
mbed_official | 49:77c8e4604045 | 15 | * - Redistributions in binary form must reproduce the above copyright |
mbed_official | 49:77c8e4604045 | 16 | * notice, this list of conditions and the following disclaimer in the |
mbed_official | 49:77c8e4604045 | 17 | * documentation and/or other materials provided with the distribution. |
mbed_official | 49:77c8e4604045 | 18 | * - Neither the name of ARM nor the names of its contributors may be used |
mbed_official | 49:77c8e4604045 | 19 | * to endorse or promote products derived from this software without |
mbed_official | 49:77c8e4604045 | 20 | * specific prior written permission. |
mbed_official | 49:77c8e4604045 | 21 | * |
mbed_official | 49:77c8e4604045 | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 49:77c8e4604045 | 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 49:77c8e4604045 | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
mbed_official | 49:77c8e4604045 | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE |
mbed_official | 49:77c8e4604045 | 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
mbed_official | 49:77c8e4604045 | 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
mbed_official | 49:77c8e4604045 | 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
mbed_official | 49:77c8e4604045 | 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
mbed_official | 49:77c8e4604045 | 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
mbed_official | 49:77c8e4604045 | 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
mbed_official | 49:77c8e4604045 | 32 | * POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 49:77c8e4604045 | 33 | *---------------------------------------------------------------------------*/ |
mbed_official | 49:77c8e4604045 | 34 | |
mbed_official | 49:77c8e4604045 | 35 | #include "rt_TypeDef.h" |
mbed_official | 112:53ace74b190c | 36 | #include "RTX_Config.h" |
mbed_official | 49:77c8e4604045 | 37 | #include "rt_HAL_CM.h" |
Kojto | 118:6635230e06ba | 38 | #include "cmsis_os.h" |
mbed_official | 49:77c8e4604045 | 39 | |
mbed_official | 49:77c8e4604045 | 40 | /*---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 41 | * Global Variables |
mbed_official | 49:77c8e4604045 | 42 | *---------------------------------------------------------------------------*/ |
mbed_official | 49:77c8e4604045 | 43 | |
mbed_official | 49:77c8e4604045 | 44 | #ifdef DBG_MSG |
mbed_official | 49:77c8e4604045 | 45 | BIT dbg_msg; |
mbed_official | 49:77c8e4604045 | 46 | #endif |
mbed_official | 49:77c8e4604045 | 47 | |
mbed_official | 49:77c8e4604045 | 48 | /*---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 49 | * Functions |
mbed_official | 49:77c8e4604045 | 50 | *---------------------------------------------------------------------------*/ |
mbed_official | 49:77c8e4604045 | 51 | |
mbed_official | 49:77c8e4604045 | 52 | |
mbed_official | 49:77c8e4604045 | 53 | /*--------------------------- rt_init_stack ---------------------------------*/ |
mbed_official | 49:77c8e4604045 | 54 | |
mbed_official | 49:77c8e4604045 | 55 | void rt_init_stack (P_TCB p_TCB, FUNCP task_body) { |
mbed_official | 49:77c8e4604045 | 56 | /* Prepare TCB and saved context for a first time start of a task. */ |
mbed_official | 49:77c8e4604045 | 57 | U32 *stk,i,size; |
mbed_official | 49:77c8e4604045 | 58 | |
mbed_official | 49:77c8e4604045 | 59 | /* Prepare a complete interrupt frame for first task start */ |
mbed_official | 49:77c8e4604045 | 60 | size = p_TCB->priv_stack >> 2; |
mbed_official | 112:53ace74b190c | 61 | if (size == 0U) { |
mbed_official | 112:53ace74b190c | 62 | size = (U16)os_stackinfo >> 2; |
mbed_official | 112:53ace74b190c | 63 | } |
mbed_official | 49:77c8e4604045 | 64 | |
mbed_official | 49:77c8e4604045 | 65 | /* Write to the top of stack. */ |
mbed_official | 49:77c8e4604045 | 66 | stk = &p_TCB->stack[size]; |
mbed_official | 49:77c8e4604045 | 67 | |
mbed_official | 49:77c8e4604045 | 68 | /* Auto correct to 8-byte ARM stack alignment. */ |
mbed_official | 112:53ace74b190c | 69 | if ((U32)stk & 0x04U) { |
mbed_official | 49:77c8e4604045 | 70 | stk--; |
mbed_official | 49:77c8e4604045 | 71 | } |
mbed_official | 49:77c8e4604045 | 72 | |
mbed_official | 49:77c8e4604045 | 73 | stk -= 16; |
mbed_official | 49:77c8e4604045 | 74 | |
mbed_official | 49:77c8e4604045 | 75 | /* Default xPSR and initial PC */ |
mbed_official | 49:77c8e4604045 | 76 | stk[15] = INITIAL_xPSR; |
mbed_official | 49:77c8e4604045 | 77 | stk[14] = (U32)task_body; |
mbed_official | 49:77c8e4604045 | 78 | |
mbed_official | 49:77c8e4604045 | 79 | /* Clear R4-R11,R0-R3,R12,LR registers. */ |
mbed_official | 112:53ace74b190c | 80 | for (i = 0U; i < 14U; i++) { |
mbed_official | 112:53ace74b190c | 81 | stk[i] = 0U; |
mbed_official | 49:77c8e4604045 | 82 | } |
mbed_official | 49:77c8e4604045 | 83 | |
mbed_official | 49:77c8e4604045 | 84 | /* Assign a void pointer to R0. */ |
mbed_official | 49:77c8e4604045 | 85 | stk[8] = (U32)p_TCB->msg; |
mbed_official | 49:77c8e4604045 | 86 | |
mbed_official | 49:77c8e4604045 | 87 | /* Initial Task stack pointer. */ |
mbed_official | 49:77c8e4604045 | 88 | p_TCB->tsk_stack = (U32)stk; |
mbed_official | 49:77c8e4604045 | 89 | |
mbed_official | 49:77c8e4604045 | 90 | /* Task entry point. */ |
mbed_official | 49:77c8e4604045 | 91 | p_TCB->ptask = task_body; |
mbed_official | 49:77c8e4604045 | 92 | |
c1728p9 | 123:58563e6cba1e | 93 | |
c1728p9 | 123:58563e6cba1e | 94 | #ifdef __MBED_CMSIS_RTOS_CM |
c1728p9 | 123:58563e6cba1e | 95 | /* Set a magic word for checking of stack overflow. |
c1728p9 | 123:58563e6cba1e | 96 | For the main thread (ID: MAIN_THREAD_ID) the stack is in a memory area shared with the |
c1728p9 | 123:58563e6cba1e | 97 | heap, therefore the last word of the stack is a moving target. |
c1728p9 | 123:58563e6cba1e | 98 | We want to do stack/heap collision detection instead. |
c1728p9 | 123:58563e6cba1e | 99 | Similar applies to stack filling for the magic pattern. |
c1728p9 | 123:58563e6cba1e | 100 | */ |
c1728p9 | 123:58563e6cba1e | 101 | if (p_TCB->task_id != MAIN_THREAD_ID) { |
c1728p9 | 123:58563e6cba1e | 102 | p_TCB->stack[0] = MAGIC_WORD; |
c1728p9 | 123:58563e6cba1e | 103 | |
c1728p9 | 123:58563e6cba1e | 104 | /* Initialize stack with magic pattern. */ |
c1728p9 | 123:58563e6cba1e | 105 | if (os_stackinfo & 0x10000000U) { |
c1728p9 | 123:58563e6cba1e | 106 | if (size > (16U+1U)) { |
c1728p9 | 123:58563e6cba1e | 107 | for (i = ((size - 16U)/2U) - 1U; i; i--) { |
c1728p9 | 123:58563e6cba1e | 108 | stk -= 2U; |
c1728p9 | 123:58563e6cba1e | 109 | stk[1] = MAGIC_PATTERN; |
c1728p9 | 123:58563e6cba1e | 110 | stk[0] = MAGIC_PATTERN; |
c1728p9 | 123:58563e6cba1e | 111 | } |
c1728p9 | 123:58563e6cba1e | 112 | if (--stk > p_TCB->stack) { |
c1728p9 | 123:58563e6cba1e | 113 | *stk = MAGIC_PATTERN; |
c1728p9 | 123:58563e6cba1e | 114 | } |
c1728p9 | 123:58563e6cba1e | 115 | } |
c1728p9 | 123:58563e6cba1e | 116 | } |
c1728p9 | 123:58563e6cba1e | 117 | } |
c1728p9 | 123:58563e6cba1e | 118 | #else |
mbed_official | 112:53ace74b190c | 119 | /* Initialize stack with magic pattern. */ |
mbed_official | 112:53ace74b190c | 120 | if (os_stackinfo & 0x10000000U) { |
mbed_official | 112:53ace74b190c | 121 | if (size > (16U+1U)) { |
mbed_official | 112:53ace74b190c | 122 | for (i = ((size - 16U)/2U) - 1U; i; i--) { |
mbed_official | 112:53ace74b190c | 123 | stk -= 2U; |
mbed_official | 112:53ace74b190c | 124 | stk[1] = MAGIC_PATTERN; |
mbed_official | 112:53ace74b190c | 125 | stk[0] = MAGIC_PATTERN; |
mbed_official | 112:53ace74b190c | 126 | } |
mbed_official | 112:53ace74b190c | 127 | if (--stk > p_TCB->stack) { |
mbed_official | 112:53ace74b190c | 128 | *stk = MAGIC_PATTERN; |
mbed_official | 112:53ace74b190c | 129 | } |
mbed_official | 112:53ace74b190c | 130 | } |
mbed_official | 112:53ace74b190c | 131 | } |
mbed_official | 112:53ace74b190c | 132 | |
mbed_official | 112:53ace74b190c | 133 | /* Set a magic word for checking of stack overflow. */ |
mbed_official | 112:53ace74b190c | 134 | p_TCB->stack[0] = MAGIC_WORD; |
c1728p9 | 123:58563e6cba1e | 135 | #endif |
mbed_official | 49:77c8e4604045 | 136 | } |
mbed_official | 49:77c8e4604045 | 137 | |
mbed_official | 49:77c8e4604045 | 138 | |
mbed_official | 49:77c8e4604045 | 139 | /*--------------------------- rt_ret_val ----------------------------------*/ |
mbed_official | 49:77c8e4604045 | 140 | |
mbed_official | 49:77c8e4604045 | 141 | static __inline U32 *rt_ret_regs (P_TCB p_TCB) { |
mbed_official | 49:77c8e4604045 | 142 | /* Get pointer to task return value registers (R0..R3) in Stack */ |
mbed_official | 112:53ace74b190c | 143 | #if defined(__TARGET_FPU_VFP) |
mbed_official | 49:77c8e4604045 | 144 | if (p_TCB->stack_frame) { |
mbed_official | 49:77c8e4604045 | 145 | /* Extended Stack Frame: R4-R11,S16-S31,R0-R3,R12,LR,PC,xPSR,S0-S15,FPSCR */ |
mbed_official | 112:53ace74b190c | 146 | return (U32 *)(p_TCB->tsk_stack + (8U*4U) + (16U*4U)); |
mbed_official | 49:77c8e4604045 | 147 | } else { |
mbed_official | 49:77c8e4604045 | 148 | /* Basic Stack Frame: R4-R11,R0-R3,R12,LR,PC,xPSR */ |
mbed_official | 112:53ace74b190c | 149 | return (U32 *)(p_TCB->tsk_stack + (8U*4U)); |
mbed_official | 49:77c8e4604045 | 150 | } |
mbed_official | 49:77c8e4604045 | 151 | #else |
mbed_official | 49:77c8e4604045 | 152 | /* Stack Frame: R4-R11,R0-R3,R12,LR,PC,xPSR */ |
mbed_official | 112:53ace74b190c | 153 | return (U32 *)(p_TCB->tsk_stack + (8U*4U)); |
mbed_official | 49:77c8e4604045 | 154 | #endif |
mbed_official | 49:77c8e4604045 | 155 | } |
mbed_official | 49:77c8e4604045 | 156 | |
mbed_official | 49:77c8e4604045 | 157 | void rt_ret_val (P_TCB p_TCB, U32 v0) { |
mbed_official | 49:77c8e4604045 | 158 | U32 *ret; |
mbed_official | 49:77c8e4604045 | 159 | |
mbed_official | 49:77c8e4604045 | 160 | ret = rt_ret_regs(p_TCB); |
mbed_official | 49:77c8e4604045 | 161 | ret[0] = v0; |
mbed_official | 49:77c8e4604045 | 162 | } |
mbed_official | 49:77c8e4604045 | 163 | |
mbed_official | 49:77c8e4604045 | 164 | void rt_ret_val2(P_TCB p_TCB, U32 v0, U32 v1) { |
mbed_official | 49:77c8e4604045 | 165 | U32 *ret; |
mbed_official | 49:77c8e4604045 | 166 | |
mbed_official | 49:77c8e4604045 | 167 | ret = rt_ret_regs(p_TCB); |
mbed_official | 49:77c8e4604045 | 168 | ret[0] = v0; |
mbed_official | 49:77c8e4604045 | 169 | ret[1] = v1; |
mbed_official | 49:77c8e4604045 | 170 | } |
mbed_official | 49:77c8e4604045 | 171 | |
mbed_official | 49:77c8e4604045 | 172 | |
mbed_official | 49:77c8e4604045 | 173 | /*--------------------------- dbg_init --------------------------------------*/ |
mbed_official | 49:77c8e4604045 | 174 | |
mbed_official | 49:77c8e4604045 | 175 | #ifdef DBG_MSG |
mbed_official | 49:77c8e4604045 | 176 | void dbg_init (void) { |
mbed_official | 112:53ace74b190c | 177 | if (((DEMCR & DEMCR_TRCENA) != 0U) && |
mbed_official | 112:53ace74b190c | 178 | ((ITM_CONTROL & ITM_ITMENA) != 0U) && |
mbed_official | 112:53ace74b190c | 179 | ((ITM_ENABLE & (1UL << 31)) != 0U)) { |
mbed_official | 49:77c8e4604045 | 180 | dbg_msg = __TRUE; |
mbed_official | 49:77c8e4604045 | 181 | } |
mbed_official | 49:77c8e4604045 | 182 | } |
mbed_official | 49:77c8e4604045 | 183 | #endif |
mbed_official | 49:77c8e4604045 | 184 | |
mbed_official | 49:77c8e4604045 | 185 | /*--------------------------- dbg_task_notify -------------------------------*/ |
mbed_official | 49:77c8e4604045 | 186 | |
mbed_official | 49:77c8e4604045 | 187 | #ifdef DBG_MSG |
mbed_official | 49:77c8e4604045 | 188 | void dbg_task_notify (P_TCB p_tcb, BOOL create) { |
mbed_official | 112:53ace74b190c | 189 | while (ITM_PORT31_U32 == 0U); |
mbed_official | 49:77c8e4604045 | 190 | ITM_PORT31_U32 = (U32)p_tcb->ptask; |
mbed_official | 112:53ace74b190c | 191 | while (ITM_PORT31_U32 == 0U); |
mbed_official | 112:53ace74b190c | 192 | ITM_PORT31_U16 = (U16)((create << 8) | p_tcb->task_id); |
mbed_official | 49:77c8e4604045 | 193 | } |
mbed_official | 49:77c8e4604045 | 194 | #endif |
mbed_official | 49:77c8e4604045 | 195 | |
mbed_official | 49:77c8e4604045 | 196 | /*--------------------------- dbg_task_switch -------------------------------*/ |
mbed_official | 49:77c8e4604045 | 197 | |
mbed_official | 49:77c8e4604045 | 198 | #ifdef DBG_MSG |
mbed_official | 49:77c8e4604045 | 199 | void dbg_task_switch (U32 task_id) { |
mbed_official | 112:53ace74b190c | 200 | while (ITM_PORT31_U32 == 0U); |
mbed_official | 112:53ace74b190c | 201 | ITM_PORT31_U8 = (U8)task_id; |
mbed_official | 49:77c8e4604045 | 202 | } |
mbed_official | 49:77c8e4604045 | 203 | #endif |
mbed_official | 49:77c8e4604045 | 204 | |
mbed_official | 49:77c8e4604045 | 205 | /*---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 206 | * end of file |
mbed_official | 49:77c8e4604045 | 207 | *---------------------------------------------------------------------------*/ |