Maintain legacy RTOS behavior before mbed-5

Fork of mbed-rtos by mbed official

Committer:
Kojto
Date:
Thu Aug 18 14:38:56 2016 +0100
Revision:
120:4dc938e301cc
Parent:
118:6635230e06ba
Child:
123:58563e6cba1e
RTOS rev120

Compatible with the mbed library v124

Changes:
- Fix heap limit checking
- Fix set initial stack pointer K64F + stack pointer size
- Fix stack size for NRF51
- Turn on main thread stack check
- Use new deprecate macros

Who changed what in which revision?

UserRevisionLine numberNew 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
mbed_official 112:53ace74b190c 93 /* Initialize stack with magic pattern. */
mbed_official 112:53ace74b190c 94 if (os_stackinfo & 0x10000000U) {
mbed_official 112:53ace74b190c 95 if (size > (16U+1U)) {
mbed_official 112:53ace74b190c 96 for (i = ((size - 16U)/2U) - 1U; i; i--) {
mbed_official 112:53ace74b190c 97 stk -= 2U;
mbed_official 112:53ace74b190c 98 stk[1] = MAGIC_PATTERN;
mbed_official 112:53ace74b190c 99 stk[0] = MAGIC_PATTERN;
mbed_official 112:53ace74b190c 100 }
mbed_official 112:53ace74b190c 101 if (--stk > p_TCB->stack) {
mbed_official 112:53ace74b190c 102 *stk = MAGIC_PATTERN;
mbed_official 112:53ace74b190c 103 }
mbed_official 112:53ace74b190c 104 }
mbed_official 112:53ace74b190c 105 }
mbed_official 112:53ace74b190c 106
mbed_official 112:53ace74b190c 107 /* Set a magic word for checking of stack overflow. */
mbed_official 112:53ace74b190c 108 p_TCB->stack[0] = MAGIC_WORD;
mbed_official 49:77c8e4604045 109 }
mbed_official 49:77c8e4604045 110
mbed_official 49:77c8e4604045 111
mbed_official 49:77c8e4604045 112 /*--------------------------- rt_ret_val ----------------------------------*/
mbed_official 49:77c8e4604045 113
mbed_official 49:77c8e4604045 114 static __inline U32 *rt_ret_regs (P_TCB p_TCB) {
mbed_official 49:77c8e4604045 115 /* Get pointer to task return value registers (R0..R3) in Stack */
mbed_official 112:53ace74b190c 116 #if defined(__TARGET_FPU_VFP)
mbed_official 49:77c8e4604045 117 if (p_TCB->stack_frame) {
mbed_official 49:77c8e4604045 118 /* Extended Stack Frame: R4-R11,S16-S31,R0-R3,R12,LR,PC,xPSR,S0-S15,FPSCR */
mbed_official 112:53ace74b190c 119 return (U32 *)(p_TCB->tsk_stack + (8U*4U) + (16U*4U));
mbed_official 49:77c8e4604045 120 } else {
mbed_official 49:77c8e4604045 121 /* Basic Stack Frame: R4-R11,R0-R3,R12,LR,PC,xPSR */
mbed_official 112:53ace74b190c 122 return (U32 *)(p_TCB->tsk_stack + (8U*4U));
mbed_official 49:77c8e4604045 123 }
mbed_official 49:77c8e4604045 124 #else
mbed_official 49:77c8e4604045 125 /* Stack Frame: R4-R11,R0-R3,R12,LR,PC,xPSR */
mbed_official 112:53ace74b190c 126 return (U32 *)(p_TCB->tsk_stack + (8U*4U));
mbed_official 49:77c8e4604045 127 #endif
mbed_official 49:77c8e4604045 128 }
mbed_official 49:77c8e4604045 129
mbed_official 49:77c8e4604045 130 void rt_ret_val (P_TCB p_TCB, U32 v0) {
mbed_official 49:77c8e4604045 131 U32 *ret;
mbed_official 49:77c8e4604045 132
mbed_official 49:77c8e4604045 133 ret = rt_ret_regs(p_TCB);
mbed_official 49:77c8e4604045 134 ret[0] = v0;
mbed_official 49:77c8e4604045 135 }
mbed_official 49:77c8e4604045 136
mbed_official 49:77c8e4604045 137 void rt_ret_val2(P_TCB p_TCB, U32 v0, U32 v1) {
mbed_official 49:77c8e4604045 138 U32 *ret;
mbed_official 49:77c8e4604045 139
mbed_official 49:77c8e4604045 140 ret = rt_ret_regs(p_TCB);
mbed_official 49:77c8e4604045 141 ret[0] = v0;
mbed_official 49:77c8e4604045 142 ret[1] = v1;
mbed_official 49:77c8e4604045 143 }
mbed_official 49:77c8e4604045 144
mbed_official 49:77c8e4604045 145
mbed_official 49:77c8e4604045 146 /*--------------------------- dbg_init --------------------------------------*/
mbed_official 49:77c8e4604045 147
mbed_official 49:77c8e4604045 148 #ifdef DBG_MSG
mbed_official 49:77c8e4604045 149 void dbg_init (void) {
mbed_official 112:53ace74b190c 150 if (((DEMCR & DEMCR_TRCENA) != 0U) &&
mbed_official 112:53ace74b190c 151 ((ITM_CONTROL & ITM_ITMENA) != 0U) &&
mbed_official 112:53ace74b190c 152 ((ITM_ENABLE & (1UL << 31)) != 0U)) {
mbed_official 49:77c8e4604045 153 dbg_msg = __TRUE;
mbed_official 49:77c8e4604045 154 }
mbed_official 49:77c8e4604045 155 }
mbed_official 49:77c8e4604045 156 #endif
mbed_official 49:77c8e4604045 157
mbed_official 49:77c8e4604045 158 /*--------------------------- dbg_task_notify -------------------------------*/
mbed_official 49:77c8e4604045 159
mbed_official 49:77c8e4604045 160 #ifdef DBG_MSG
mbed_official 49:77c8e4604045 161 void dbg_task_notify (P_TCB p_tcb, BOOL create) {
mbed_official 112:53ace74b190c 162 while (ITM_PORT31_U32 == 0U);
mbed_official 49:77c8e4604045 163 ITM_PORT31_U32 = (U32)p_tcb->ptask;
mbed_official 112:53ace74b190c 164 while (ITM_PORT31_U32 == 0U);
mbed_official 112:53ace74b190c 165 ITM_PORT31_U16 = (U16)((create << 8) | p_tcb->task_id);
mbed_official 49:77c8e4604045 166 }
mbed_official 49:77c8e4604045 167 #endif
mbed_official 49:77c8e4604045 168
mbed_official 49:77c8e4604045 169 /*--------------------------- dbg_task_switch -------------------------------*/
mbed_official 49:77c8e4604045 170
mbed_official 49:77c8e4604045 171 #ifdef DBG_MSG
mbed_official 49:77c8e4604045 172 void dbg_task_switch (U32 task_id) {
mbed_official 112:53ace74b190c 173 while (ITM_PORT31_U32 == 0U);
mbed_official 112:53ace74b190c 174 ITM_PORT31_U8 = (U8)task_id;
mbed_official 49:77c8e4604045 175 }
mbed_official 49:77c8e4604045 176 #endif
mbed_official 49:77c8e4604045 177
mbed_official 49:77c8e4604045 178 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 179 * end of file
mbed_official 49:77c8e4604045 180 *---------------------------------------------------------------------------*/