Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HAL_CM.c@0:5f46ebd8588e, 2012-09-02 (annotated)
- Committer:
- jonathonfletcher
- Date:
- Sun Sep 02 03:24:20 2012 +0000
- Revision:
- 0:5f46ebd8588e
in OS_TSK, rename run as tsk_run and new as tsk_new.
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| jonathonfletcher | 0:5f46ebd8588e | 1 | /*---------------------------------------------------------------------------- | 
| jonathonfletcher | 0:5f46ebd8588e | 2 | * RL-ARM - RTX | 
| jonathonfletcher | 0:5f46ebd8588e | 3 | *---------------------------------------------------------------------------- | 
| jonathonfletcher | 0:5f46ebd8588e | 4 | * Name: HAL_CM.C | 
| jonathonfletcher | 0:5f46ebd8588e | 5 | * Purpose: Hardware Abstraction Layer for Cortex-M | 
| jonathonfletcher | 0:5f46ebd8588e | 6 | * Rev.: V4.50 | 
| jonathonfletcher | 0:5f46ebd8588e | 7 | *---------------------------------------------------------------------------- | 
| jonathonfletcher | 0:5f46ebd8588e | 8 | * | 
| jonathonfletcher | 0:5f46ebd8588e | 9 | * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH | 
| jonathonfletcher | 0:5f46ebd8588e | 10 | * All rights reserved. | 
| jonathonfletcher | 0:5f46ebd8588e | 11 | * Redistribution and use in source and binary forms, with or without | 
| jonathonfletcher | 0:5f46ebd8588e | 12 | * modification, are permitted provided that the following conditions are met: | 
| jonathonfletcher | 0:5f46ebd8588e | 13 | * - Redistributions of source code must retain the above copyright | 
| jonathonfletcher | 0:5f46ebd8588e | 14 | * notice, this list of conditions and the following disclaimer. | 
| jonathonfletcher | 0:5f46ebd8588e | 15 | * - Redistributions in binary form must reproduce the above copyright | 
| jonathonfletcher | 0:5f46ebd8588e | 16 | * notice, this list of conditions and the following disclaimer in the | 
| jonathonfletcher | 0:5f46ebd8588e | 17 | * documentation and/or other materials provided with the distribution. | 
| jonathonfletcher | 0:5f46ebd8588e | 18 | * - Neither the name of ARM nor the names of its contributors may be used | 
| jonathonfletcher | 0:5f46ebd8588e | 19 | * to endorse or promote products derived from this software without | 
| jonathonfletcher | 0:5f46ebd8588e | 20 | * specific prior written permission. | 
| jonathonfletcher | 0:5f46ebd8588e | 21 | * | 
| jonathonfletcher | 0:5f46ebd8588e | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
| jonathonfletcher | 0:5f46ebd8588e | 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
| jonathonfletcher | 0:5f46ebd8588e | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
| jonathonfletcher | 0:5f46ebd8588e | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE | 
| jonathonfletcher | 0:5f46ebd8588e | 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 
| jonathonfletcher | 0:5f46ebd8588e | 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 
| jonathonfletcher | 0:5f46ebd8588e | 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 
| jonathonfletcher | 0:5f46ebd8588e | 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 
| jonathonfletcher | 0:5f46ebd8588e | 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 
| jonathonfletcher | 0:5f46ebd8588e | 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 
| jonathonfletcher | 0:5f46ebd8588e | 32 | * POSSIBILITY OF SUCH DAMAGE. | 
| jonathonfletcher | 0:5f46ebd8588e | 33 | *---------------------------------------------------------------------------*/ | 
| jonathonfletcher | 0:5f46ebd8588e | 34 | |
| jonathonfletcher | 0:5f46ebd8588e | 35 | #include "rt_TypeDef.h" | 
| jonathonfletcher | 0:5f46ebd8588e | 36 | #include "RTX_Config.h" | 
| jonathonfletcher | 0:5f46ebd8588e | 37 | #include "rt_HAL_CM.h" | 
| jonathonfletcher | 0:5f46ebd8588e | 38 | |
| jonathonfletcher | 0:5f46ebd8588e | 39 | |
| jonathonfletcher | 0:5f46ebd8588e | 40 | /*---------------------------------------------------------------------------- | 
| jonathonfletcher | 0:5f46ebd8588e | 41 | * Global Variables | 
| jonathonfletcher | 0:5f46ebd8588e | 42 | *---------------------------------------------------------------------------*/ | 
| jonathonfletcher | 0:5f46ebd8588e | 43 | |
| jonathonfletcher | 0:5f46ebd8588e | 44 | #ifdef DBG_MSG | 
| jonathonfletcher | 0:5f46ebd8588e | 45 | BIT dbg_msg; | 
| jonathonfletcher | 0:5f46ebd8588e | 46 | #endif | 
| jonathonfletcher | 0:5f46ebd8588e | 47 | |
| jonathonfletcher | 0:5f46ebd8588e | 48 | /*---------------------------------------------------------------------------- | 
| jonathonfletcher | 0:5f46ebd8588e | 49 | * Functions | 
| jonathonfletcher | 0:5f46ebd8588e | 50 | *---------------------------------------------------------------------------*/ | 
| jonathonfletcher | 0:5f46ebd8588e | 51 | |
| jonathonfletcher | 0:5f46ebd8588e | 52 | |
| jonathonfletcher | 0:5f46ebd8588e | 53 | /*--------------------------- rt_init_stack ---------------------------------*/ | 
| jonathonfletcher | 0:5f46ebd8588e | 54 | |
| jonathonfletcher | 0:5f46ebd8588e | 55 | void rt_init_stack (P_TCB p_TCB, FUNCP task_body) { | 
| jonathonfletcher | 0:5f46ebd8588e | 56 | /* Prepare TCB and saved context for a first time start of a task. */ | 
| jonathonfletcher | 0:5f46ebd8588e | 57 | U32 *stk,i,size; | 
| jonathonfletcher | 0:5f46ebd8588e | 58 | |
| jonathonfletcher | 0:5f46ebd8588e | 59 | /* Prepare a complete interrupt frame for first task start */ | 
| jonathonfletcher | 0:5f46ebd8588e | 60 | size = p_TCB->priv_stack >> 2; | 
| jonathonfletcher | 0:5f46ebd8588e | 61 | |
| jonathonfletcher | 0:5f46ebd8588e | 62 | /* Write to the top of stack. */ | 
| jonathonfletcher | 0:5f46ebd8588e | 63 | stk = &p_TCB->stack[size]; | 
| jonathonfletcher | 0:5f46ebd8588e | 64 | |
| jonathonfletcher | 0:5f46ebd8588e | 65 | /* Auto correct to 8-byte ARM stack alignment. */ | 
| jonathonfletcher | 0:5f46ebd8588e | 66 | if ((U32)stk & 0x04) { | 
| jonathonfletcher | 0:5f46ebd8588e | 67 | stk--; | 
| jonathonfletcher | 0:5f46ebd8588e | 68 | } | 
| jonathonfletcher | 0:5f46ebd8588e | 69 | |
| jonathonfletcher | 0:5f46ebd8588e | 70 | stk -= 16; | 
| jonathonfletcher | 0:5f46ebd8588e | 71 | |
| jonathonfletcher | 0:5f46ebd8588e | 72 | /* Default xPSR and initial PC */ | 
| jonathonfletcher | 0:5f46ebd8588e | 73 | stk[15] = INITIAL_xPSR; | 
| jonathonfletcher | 0:5f46ebd8588e | 74 | stk[14] = (U32)task_body; | 
| jonathonfletcher | 0:5f46ebd8588e | 75 | |
| jonathonfletcher | 0:5f46ebd8588e | 76 | /* Clear R4-R11,R0-R3,R12,LR registers. */ | 
| jonathonfletcher | 0:5f46ebd8588e | 77 | for (i = 0; i < 14; i++) { | 
| jonathonfletcher | 0:5f46ebd8588e | 78 | stk[i] = 0; | 
| jonathonfletcher | 0:5f46ebd8588e | 79 | } | 
| jonathonfletcher | 0:5f46ebd8588e | 80 | |
| jonathonfletcher | 0:5f46ebd8588e | 81 | /* Assign a void pointer to R0. */ | 
| jonathonfletcher | 0:5f46ebd8588e | 82 | stk[8] = (U32)p_TCB->msg; | 
| jonathonfletcher | 0:5f46ebd8588e | 83 | |
| jonathonfletcher | 0:5f46ebd8588e | 84 | /* Initial Task stack pointer. */ | 
| jonathonfletcher | 0:5f46ebd8588e | 85 | p_TCB->tsk_stack = (U32)stk; | 
| jonathonfletcher | 0:5f46ebd8588e | 86 | |
| jonathonfletcher | 0:5f46ebd8588e | 87 | /* Task entry point. */ | 
| jonathonfletcher | 0:5f46ebd8588e | 88 | p_TCB->ptask = task_body; | 
| jonathonfletcher | 0:5f46ebd8588e | 89 | |
| jonathonfletcher | 0:5f46ebd8588e | 90 | /* Set a magic word for checking of stack overflow. | 
| jonathonfletcher | 0:5f46ebd8588e | 91 | For the main thread (ID: 0x01) the stack is in a memory area shared with the | 
| jonathonfletcher | 0:5f46ebd8588e | 92 | heap, therefore the last word of the stack is a moving target. | 
| jonathonfletcher | 0:5f46ebd8588e | 93 | We want to do stack/heap collision detection instead. | 
| jonathonfletcher | 0:5f46ebd8588e | 94 | */ | 
| jonathonfletcher | 0:5f46ebd8588e | 95 | if (p_TCB->task_id != 0x01) | 
| jonathonfletcher | 0:5f46ebd8588e | 96 | p_TCB->stack[0] = MAGIC_WORD; | 
| jonathonfletcher | 0:5f46ebd8588e | 97 | } | 
| jonathonfletcher | 0:5f46ebd8588e | 98 | |
| jonathonfletcher | 0:5f46ebd8588e | 99 | |
| jonathonfletcher | 0:5f46ebd8588e | 100 | /*--------------------------- rt_ret_val ----------------------------------*/ | 
| jonathonfletcher | 0:5f46ebd8588e | 101 | |
| jonathonfletcher | 0:5f46ebd8588e | 102 | static __inline U32 *rt_ret_regs (P_TCB p_TCB) { | 
| jonathonfletcher | 0:5f46ebd8588e | 103 | /* Get pointer to task return value registers (R0..R3) in Stack */ | 
| jonathonfletcher | 0:5f46ebd8588e | 104 | #if (__TARGET_FPU_VFP) | 
| jonathonfletcher | 0:5f46ebd8588e | 105 | if (p_TCB->stack_frame) { | 
| jonathonfletcher | 0:5f46ebd8588e | 106 | /* Extended Stack Frame: R4-R11,S16-S31,R0-R3,R12,LR,PC,xPSR,S0-S15,FPSCR */ | 
| jonathonfletcher | 0:5f46ebd8588e | 107 | return (U32 *)(p_TCB->tsk_stack + 8*4 + 16*4); | 
| jonathonfletcher | 0:5f46ebd8588e | 108 | } else { | 
| jonathonfletcher | 0:5f46ebd8588e | 109 | /* Basic Stack Frame: R4-R11,R0-R3,R12,LR,PC,xPSR */ | 
| jonathonfletcher | 0:5f46ebd8588e | 110 | return (U32 *)(p_TCB->tsk_stack + 8*4); | 
| jonathonfletcher | 0:5f46ebd8588e | 111 | } | 
| jonathonfletcher | 0:5f46ebd8588e | 112 | #else | 
| jonathonfletcher | 0:5f46ebd8588e | 113 | /* Stack Frame: R4-R11,R0-R3,R12,LR,PC,xPSR */ | 
| jonathonfletcher | 0:5f46ebd8588e | 114 | return (U32 *)(p_TCB->tsk_stack + 8*4); | 
| jonathonfletcher | 0:5f46ebd8588e | 115 | #endif | 
| jonathonfletcher | 0:5f46ebd8588e | 116 | } | 
| jonathonfletcher | 0:5f46ebd8588e | 117 | |
| jonathonfletcher | 0:5f46ebd8588e | 118 | void rt_ret_val (P_TCB p_TCB, U32 v0) { | 
| jonathonfletcher | 0:5f46ebd8588e | 119 | U32 *ret; | 
| jonathonfletcher | 0:5f46ebd8588e | 120 | |
| jonathonfletcher | 0:5f46ebd8588e | 121 | ret = rt_ret_regs(p_TCB); | 
| jonathonfletcher | 0:5f46ebd8588e | 122 | ret[0] = v0; | 
| jonathonfletcher | 0:5f46ebd8588e | 123 | } | 
| jonathonfletcher | 0:5f46ebd8588e | 124 | |
| jonathonfletcher | 0:5f46ebd8588e | 125 | void rt_ret_val2(P_TCB p_TCB, U32 v0, U32 v1) { | 
| jonathonfletcher | 0:5f46ebd8588e | 126 | U32 *ret; | 
| jonathonfletcher | 0:5f46ebd8588e | 127 | |
| jonathonfletcher | 0:5f46ebd8588e | 128 | ret = rt_ret_regs(p_TCB); | 
| jonathonfletcher | 0:5f46ebd8588e | 129 | ret[0] = v0; | 
| jonathonfletcher | 0:5f46ebd8588e | 130 | ret[1] = v1; | 
| jonathonfletcher | 0:5f46ebd8588e | 131 | } | 
| jonathonfletcher | 0:5f46ebd8588e | 132 | |
| jonathonfletcher | 0:5f46ebd8588e | 133 | |
| jonathonfletcher | 0:5f46ebd8588e | 134 | /*--------------------------- dbg_init --------------------------------------*/ | 
| jonathonfletcher | 0:5f46ebd8588e | 135 | |
| jonathonfletcher | 0:5f46ebd8588e | 136 | #ifdef DBG_MSG | 
| jonathonfletcher | 0:5f46ebd8588e | 137 | void dbg_init (void) { | 
| jonathonfletcher | 0:5f46ebd8588e | 138 | if ((DEMCR & DEMCR_TRCENA) && | 
| jonathonfletcher | 0:5f46ebd8588e | 139 | (ITM_CONTROL & ITM_ITMENA) && | 
| jonathonfletcher | 0:5f46ebd8588e | 140 | (ITM_ENABLE & (1UL << 31))) { | 
| jonathonfletcher | 0:5f46ebd8588e | 141 | dbg_msg = __TRUE; | 
| jonathonfletcher | 0:5f46ebd8588e | 142 | } | 
| jonathonfletcher | 0:5f46ebd8588e | 143 | } | 
| jonathonfletcher | 0:5f46ebd8588e | 144 | #endif | 
| jonathonfletcher | 0:5f46ebd8588e | 145 | |
| jonathonfletcher | 0:5f46ebd8588e | 146 | /*--------------------------- dbg_task_notify -------------------------------*/ | 
| jonathonfletcher | 0:5f46ebd8588e | 147 | |
| jonathonfletcher | 0:5f46ebd8588e | 148 | #ifdef DBG_MSG | 
| jonathonfletcher | 0:5f46ebd8588e | 149 | void dbg_task_notify (P_TCB p_tcb, BOOL create) { | 
| jonathonfletcher | 0:5f46ebd8588e | 150 | while (ITM_PORT31_U32 == 0); | 
| jonathonfletcher | 0:5f46ebd8588e | 151 | ITM_PORT31_U32 = (U32)p_tcb->ptask; | 
| jonathonfletcher | 0:5f46ebd8588e | 152 | while (ITM_PORT31_U32 == 0); | 
| jonathonfletcher | 0:5f46ebd8588e | 153 | ITM_PORT31_U16 = (create << 8) | p_tcb->task_id; | 
| jonathonfletcher | 0:5f46ebd8588e | 154 | } | 
| jonathonfletcher | 0:5f46ebd8588e | 155 | #endif | 
| jonathonfletcher | 0:5f46ebd8588e | 156 | |
| jonathonfletcher | 0:5f46ebd8588e | 157 | /*--------------------------- dbg_task_switch -------------------------------*/ | 
| jonathonfletcher | 0:5f46ebd8588e | 158 | |
| jonathonfletcher | 0:5f46ebd8588e | 159 | #ifdef DBG_MSG | 
| jonathonfletcher | 0:5f46ebd8588e | 160 | void dbg_task_switch (U32 task_id) { | 
| jonathonfletcher | 0:5f46ebd8588e | 161 | while (ITM_PORT31_U32 == 0); | 
| jonathonfletcher | 0:5f46ebd8588e | 162 | ITM_PORT31_U8 = task_id; | 
| jonathonfletcher | 0:5f46ebd8588e | 163 | } | 
| jonathonfletcher | 0:5f46ebd8588e | 164 | #endif | 
| jonathonfletcher | 0:5f46ebd8588e | 165 | |
| jonathonfletcher | 0:5f46ebd8588e | 166 | |
| jonathonfletcher | 0:5f46ebd8588e | 167 | /*---------------------------------------------------------------------------- | 
| jonathonfletcher | 0:5f46ebd8588e | 168 | * end of file | 
| jonathonfletcher | 0:5f46ebd8588e | 169 | *---------------------------------------------------------------------------*/ | 
| jonathonfletcher | 0:5f46ebd8588e | 170 |