Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

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