mbed client lightswitch demo

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by Austin Blackstone

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 11:cada08fc8a70 1 /*----------------------------------------------------------------------------
mbedAustin 11:cada08fc8a70 2 * RL-ARM - RTX
mbedAustin 11:cada08fc8a70 3 *----------------------------------------------------------------------------
mbedAustin 11:cada08fc8a70 4 * Name: RT_HAL_CM.H
mbedAustin 11:cada08fc8a70 5 * Purpose: Hardware Abstraction Layer for Cortex-M definitions
mbedAustin 11:cada08fc8a70 6 * Rev.: V4.60
mbedAustin 11:cada08fc8a70 7 *----------------------------------------------------------------------------
mbedAustin 11:cada08fc8a70 8 *
mbedAustin 11:cada08fc8a70 9 * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
mbedAustin 11:cada08fc8a70 10 * All rights reserved.
mbedAustin 11:cada08fc8a70 11 * Redistribution and use in source and binary forms, with or without
mbedAustin 11:cada08fc8a70 12 * modification, are permitted provided that the following conditions are met:
mbedAustin 11:cada08fc8a70 13 * - Redistributions of source code must retain the above copyright
mbedAustin 11:cada08fc8a70 14 * notice, this list of conditions and the following disclaimer.
mbedAustin 11:cada08fc8a70 15 * - Redistributions in binary form must reproduce the above copyright
mbedAustin 11:cada08fc8a70 16 * notice, this list of conditions and the following disclaimer in the
mbedAustin 11:cada08fc8a70 17 * documentation and/or other materials provided with the distribution.
mbedAustin 11:cada08fc8a70 18 * - Neither the name of ARM nor the names of its contributors may be used
mbedAustin 11:cada08fc8a70 19 * to endorse or promote products derived from this software without
mbedAustin 11:cada08fc8a70 20 * specific prior written permission.
mbedAustin 11:cada08fc8a70 21 *
mbedAustin 11:cada08fc8a70 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbedAustin 11:cada08fc8a70 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbedAustin 11:cada08fc8a70 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
mbedAustin 11:cada08fc8a70 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
mbedAustin 11:cada08fc8a70 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
mbedAustin 11:cada08fc8a70 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
mbedAustin 11:cada08fc8a70 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbedAustin 11:cada08fc8a70 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbedAustin 11:cada08fc8a70 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
mbedAustin 11:cada08fc8a70 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
mbedAustin 11:cada08fc8a70 32 * POSSIBILITY OF SUCH DAMAGE.
mbedAustin 11:cada08fc8a70 33 *---------------------------------------------------------------------------*/
mbedAustin 11:cada08fc8a70 34
mbedAustin 11:cada08fc8a70 35 #include "cmsis.h"
mbedAustin 11:cada08fc8a70 36 /* Definitions */
mbedAustin 11:cada08fc8a70 37 #define INITIAL_xPSR 0x10000000
mbedAustin 11:cada08fc8a70 38 #define DEMCR_TRCENA 0x01000000
mbedAustin 11:cada08fc8a70 39 #define ITM_ITMENA 0x00000001
mbedAustin 11:cada08fc8a70 40 #define MAGIC_WORD 0xE25A2EA5
mbedAustin 11:cada08fc8a70 41
mbedAustin 11:cada08fc8a70 42 #define SYS_TICK_IRQn TIMER0_IRQn
mbedAustin 11:cada08fc8a70 43
mbedAustin 11:cada08fc8a70 44 extern void rt_set_PSP (U32 stack);
mbedAustin 11:cada08fc8a70 45 extern U32 rt_get_PSP (void);
mbedAustin 11:cada08fc8a70 46 extern void os_set_env (void);
mbedAustin 11:cada08fc8a70 47 extern void SysTick_Handler (void);
mbedAustin 11:cada08fc8a70 48 extern void *_alloc_box (void *box_mem);
mbedAustin 11:cada08fc8a70 49 extern int _free_box (void *box_mem, void *box);
mbedAustin 11:cada08fc8a70 50
mbedAustin 11:cada08fc8a70 51 extern void rt_init_stack (P_TCB p_TCB, FUNCP task_body);
mbedAustin 11:cada08fc8a70 52 extern void rt_ret_val (P_TCB p_TCB, U32 v0);
mbedAustin 11:cada08fc8a70 53 extern void rt_ret_val2 (P_TCB p_TCB, U32 v0, U32 v1);
mbedAustin 11:cada08fc8a70 54
mbedAustin 11:cada08fc8a70 55 extern void dbg_init (void);
mbedAustin 11:cada08fc8a70 56 extern void dbg_task_notify (P_TCB p_tcb, BOOL create);
mbedAustin 11:cada08fc8a70 57 extern void dbg_task_switch (U32 task_id);
mbedAustin 11:cada08fc8a70 58
mbedAustin 11:cada08fc8a70 59
mbedAustin 11:cada08fc8a70 60 #if defined (__CC_ARM) /* ARM Compiler */
mbedAustin 11:cada08fc8a70 61
mbedAustin 11:cada08fc8a70 62 #if ((__TARGET_ARCH_7_M || __TARGET_ARCH_7E_M) && !NO_EXCLUSIVE_ACCESS)
mbedAustin 11:cada08fc8a70 63 #define __USE_EXCLUSIVE_ACCESS
mbedAustin 11:cada08fc8a70 64 #else
mbedAustin 11:cada08fc8a70 65 #undef __USE_EXCLUSIVE_ACCESS
mbedAustin 11:cada08fc8a70 66 #endif
mbedAustin 11:cada08fc8a70 67
mbedAustin 11:cada08fc8a70 68 #elif defined (__GNUC__) /* GNU Compiler */
mbedAustin 11:cada08fc8a70 69
mbedAustin 11:cada08fc8a70 70 #undef __USE_EXCLUSIVE_ACCESS
mbedAustin 11:cada08fc8a70 71
mbedAustin 11:cada08fc8a70 72 #if defined (__CORTEX_M0) || defined (__CORTEX_M0PLUS)
mbedAustin 11:cada08fc8a70 73 #define __TARGET_ARCH_6S_M 1
mbedAustin 11:cada08fc8a70 74 #else
mbedAustin 11:cada08fc8a70 75 #define __TARGET_ARCH_6S_M 0
mbedAustin 11:cada08fc8a70 76 #endif
mbedAustin 11:cada08fc8a70 77
mbedAustin 11:cada08fc8a70 78 #if defined (__VFP_FP__) && !defined(__SOFTFP__)
mbedAustin 11:cada08fc8a70 79 #define __TARGET_FPU_VFP 1
mbedAustin 11:cada08fc8a70 80 #else
mbedAustin 11:cada08fc8a70 81 #define __TARGET_FPU_VFP 0
mbedAustin 11:cada08fc8a70 82 #endif
mbedAustin 11:cada08fc8a70 83
mbedAustin 11:cada08fc8a70 84 #define __inline inline
mbedAustin 11:cada08fc8a70 85 #define __weak __attribute__((weak))
mbedAustin 11:cada08fc8a70 86
mbedAustin 11:cada08fc8a70 87
mbedAustin 11:cada08fc8a70 88 #elif defined (__ICCARM__) /* IAR Compiler */
mbedAustin 11:cada08fc8a70 89
mbedAustin 11:cada08fc8a70 90 #undef __USE_EXCLUSIVE_ACCESS
mbedAustin 11:cada08fc8a70 91
mbedAustin 11:cada08fc8a70 92 #if (__CORE__ == __ARM6M__)
mbedAustin 11:cada08fc8a70 93 #define __TARGET_ARCH_6S_M 1
mbedAustin 11:cada08fc8a70 94 #else
mbedAustin 11:cada08fc8a70 95 #define __TARGET_ARCH_6S_M 0
mbedAustin 11:cada08fc8a70 96 #endif
mbedAustin 11:cada08fc8a70 97
mbedAustin 11:cada08fc8a70 98 #if defined __ARMVFP__
mbedAustin 11:cada08fc8a70 99 #define __TARGET_FPU_VFP 1
mbedAustin 11:cada08fc8a70 100 #else
mbedAustin 11:cada08fc8a70 101 #define __TARGET_FPU_VFP 0
mbedAustin 11:cada08fc8a70 102 #endif
mbedAustin 11:cada08fc8a70 103
mbedAustin 11:cada08fc8a70 104 #define __inline inline
mbedAustin 11:cada08fc8a70 105
mbedAustin 11:cada08fc8a70 106 #endif
mbedAustin 11:cada08fc8a70 107
mbedAustin 11:cada08fc8a70 108
mbedAustin 11:cada08fc8a70 109 /* NVIC registers */
mbedAustin 11:cada08fc8a70 110
mbedAustin 11:cada08fc8a70 111 #define OS_PEND_IRQ() NVIC_PendIRQ(SYS_TICK_IRQn)
mbedAustin 11:cada08fc8a70 112 #define OS_PENDING NVIC_PendingIRQ(SYS_TICK_IRQn)
mbedAustin 11:cada08fc8a70 113 #define OS_UNPEND(fl) NVIC_UnpendIRQ(SYS_TICK_IRQn)
mbedAustin 11:cada08fc8a70 114 #define OS_PEND(fl,p) NVIC_PendIRQ(SYS_TICK_IRQn)
mbedAustin 11:cada08fc8a70 115 #define OS_LOCK() NVIC_DisableIRQ(SYS_TICK_IRQn)
mbedAustin 11:cada08fc8a70 116 #define OS_UNLOCK() NVIC_EnableIRQ(SYS_TICK_IRQn)
mbedAustin 11:cada08fc8a70 117
mbedAustin 11:cada08fc8a70 118 #define OS_X_PENDING NVIC_PendingIRQ(SYS_TICK_IRQn)
mbedAustin 11:cada08fc8a70 119 #define OS_X_UNPEND(fl) NVIC_UnpendIRQ(SYS_TICK_IRQn)
mbedAustin 11:cada08fc8a70 120 #define OS_X_PEND(fl,p) NVIC_PendIRQ(SYS_TICK_IRQn)
mbedAustin 11:cada08fc8a70 121
mbedAustin 11:cada08fc8a70 122 #define OS_X_INIT(n) NVIC_EnableIRQ(n)
mbedAustin 11:cada08fc8a70 123 #define OS_X_LOCK(n) NVIC_DisableIRQ(n)
mbedAustin 11:cada08fc8a70 124 #define OS_X_UNLOCK(n) NVIC_EnableIRQ(n)
mbedAustin 11:cada08fc8a70 125
mbedAustin 11:cada08fc8a70 126 /* Variables */
mbedAustin 11:cada08fc8a70 127 extern BIT dbg_msg;
mbedAustin 11:cada08fc8a70 128
mbedAustin 11:cada08fc8a70 129 /* Functions */
mbedAustin 11:cada08fc8a70 130 #ifdef __USE_EXCLUSIVE_ACCESS
mbedAustin 11:cada08fc8a70 131 #define rt_inc(p) while(__strex((__ldrex(p)+1),p))
mbedAustin 11:cada08fc8a70 132 #define rt_dec(p) while(__strex((__ldrex(p)-1),p))
mbedAustin 11:cada08fc8a70 133 #else
mbedAustin 11:cada08fc8a70 134 #define rt_inc(p) __disable_irq();(*p)++;__enable_irq();
mbedAustin 11:cada08fc8a70 135 #define rt_dec(p) __disable_irq();(*p)--;__enable_irq();
mbedAustin 11:cada08fc8a70 136 #endif
mbedAustin 11:cada08fc8a70 137
mbedAustin 11:cada08fc8a70 138 __inline static U32 rt_inc_qi (U32 size, U8 *count, U8 *first) {
mbedAustin 11:cada08fc8a70 139 U32 cnt,c2;
mbedAustin 11:cada08fc8a70 140 #ifdef __USE_EXCLUSIVE_ACCESS
mbedAustin 11:cada08fc8a70 141 do {
mbedAustin 11:cada08fc8a70 142 if ((cnt = __ldrex(count)) == size) {
mbedAustin 11:cada08fc8a70 143 __clrex();
mbedAustin 11:cada08fc8a70 144 return (cnt); }
mbedAustin 11:cada08fc8a70 145 } while (__strex(cnt+1, count));
mbedAustin 11:cada08fc8a70 146 do {
mbedAustin 11:cada08fc8a70 147 c2 = (cnt = __ldrex(first)) + 1;
mbedAustin 11:cada08fc8a70 148 if (c2 == size) c2 = 0;
mbedAustin 11:cada08fc8a70 149 } while (__strex(c2, first));
mbedAustin 11:cada08fc8a70 150 #else
mbedAustin 11:cada08fc8a70 151 __disable_irq();
mbedAustin 11:cada08fc8a70 152 if ((cnt = *count) < size) {
mbedAustin 11:cada08fc8a70 153 *count = cnt+1;
mbedAustin 11:cada08fc8a70 154 c2 = (cnt = *first) + 1;
mbedAustin 11:cada08fc8a70 155 if (c2 == size) c2 = 0;
mbedAustin 11:cada08fc8a70 156 *first = c2;
mbedAustin 11:cada08fc8a70 157 }
mbedAustin 11:cada08fc8a70 158 __enable_irq ();
mbedAustin 11:cada08fc8a70 159 #endif
mbedAustin 11:cada08fc8a70 160 return (cnt);
mbedAustin 11:cada08fc8a70 161 }
mbedAustin 11:cada08fc8a70 162
mbedAustin 11:cada08fc8a70 163 __inline static void rt_systick_init (void) {
mbedAustin 11:cada08fc8a70 164 #if SYS_TICK_IRQn == TIMER0_IRQn
mbedAustin 11:cada08fc8a70 165 #define SYS_TICK_TIMER LPC_TIM0
mbedAustin 11:cada08fc8a70 166 LPC_SC->PCONP |= (1 << PCTIM0);
mbedAustin 11:cada08fc8a70 167 LPC_SC->PCLKSEL0 = (LPC_SC->PCLKSEL0 & (~(1<<3))) | (1<<2); //PCLK == CPUCLK
mbedAustin 11:cada08fc8a70 168 #elif SYS_TICK_IRQn == TIMER1_IRQn
mbedAustin 11:cada08fc8a70 169 #define SYS_TICK_TIMER LPC_TIM1
mbedAustin 11:cada08fc8a70 170 LPC_SC->PCONP |= (1 << PCTIM1);
mbedAustin 11:cada08fc8a70 171 LPC_SC->PCLKSEL0 = (LPC_SC->PCLKSEL0 & (~(1<<5))) | (1<<4); //PCLK == CPUCLK
mbedAustin 11:cada08fc8a70 172 #elif SYS_TICK_IRQn == TIMER2_IRQn
mbedAustin 11:cada08fc8a70 173 #define SYS_TICK_TIMER LPC_TIM2
mbedAustin 11:cada08fc8a70 174 LPC_SC->PCONP |= (1 << PCTIM2);
mbedAustin 11:cada08fc8a70 175 LPC_SC->PCLKSEL1 = (LPC_SC->PCLKSEL1 & (~(1<<13))) | (1<<12); //PCLK == CPUCLK
mbedAustin 11:cada08fc8a70 176 #else
mbedAustin 11:cada08fc8a70 177 #define SYS_TICK_TIMER LPC_TIM3
mbedAustin 11:cada08fc8a70 178 LPC_SC->PCONP |= (1 << PCTIM3);
mbedAustin 11:cada08fc8a70 179 LPC_SC->PCLKSEL1 = (LPC_SC->PCLKSEL1 & (~(1<<15))) | (1<<14); //PCLK == CPUCLK
mbedAustin 11:cada08fc8a70 180 #endif
mbedAustin 11:cada08fc8a70 181
mbedAustin 11:cada08fc8a70 182 // setup Timer to count forever
mbedAustin 11:cada08fc8a70 183 //interrupt_reg
mbedAustin 11:cada08fc8a70 184 SYS_TICK_TIMER->TCR = 2; // reset & disable timer 0
mbedAustin 11:cada08fc8a70 185 SYS_TICK_TIMER->TC = os_trv;
mbedAustin 11:cada08fc8a70 186 SYS_TICK_TIMER->PR = 0; // set the prescale divider
mbedAustin 11:cada08fc8a70 187 //Reset of TC and Interrupt when MR3 MR2 matches TC
mbedAustin 11:cada08fc8a70 188 SYS_TICK_TIMER->MCR = (1 << 9) |(1 << 10); //TMCR_MR3_R_Msk | TMCR_MR3_I_Msk
mbedAustin 11:cada08fc8a70 189 SYS_TICK_TIMER->MR3 = os_trv; // match registers
mbedAustin 11:cada08fc8a70 190 SYS_TICK_TIMER->CCR = 0; // disable compare registers
mbedAustin 11:cada08fc8a70 191 SYS_TICK_TIMER->EMR = 0; // disable external match register
mbedAustin 11:cada08fc8a70 192 // initialize the interrupt vector
mbedAustin 11:cada08fc8a70 193 NVIC_SetVector(SYS_TICK_IRQn, (uint32_t)&SysTick_Handler);
mbedAustin 11:cada08fc8a70 194 SYS_TICK_TIMER->TCR = 1; // enable timer 0
mbedAustin 11:cada08fc8a70 195 }
mbedAustin 11:cada08fc8a70 196
mbedAustin 11:cada08fc8a70 197 __inline static void rt_svc_init (void) {
mbedAustin 11:cada08fc8a70 198 // TODO: add svcInit
mbedAustin 11:cada08fc8a70 199
mbedAustin 11:cada08fc8a70 200 }
mbedAustin 11:cada08fc8a70 201
mbedAustin 11:cada08fc8a70 202 #ifdef DBG_MSG
mbedAustin 11:cada08fc8a70 203 #define DBG_INIT() dbg_init()
mbedAustin 11:cada08fc8a70 204 #define DBG_TASK_NOTIFY(p_tcb,create) if (dbg_msg) dbg_task_notify(p_tcb,create)
mbedAustin 11:cada08fc8a70 205 #define DBG_TASK_SWITCH(task_id) if (dbg_msg && (os_tsk.new_tsk != os_tsk.run)) \
mbedAustin 11:cada08fc8a70 206 dbg_task_switch(task_id)
mbedAustin 11:cada08fc8a70 207 #else
mbedAustin 11:cada08fc8a70 208 #define DBG_INIT()
mbedAustin 11:cada08fc8a70 209 #define DBG_TASK_NOTIFY(p_tcb,create)
mbedAustin 11:cada08fc8a70 210 #define DBG_TASK_SWITCH(task_id)
mbedAustin 11:cada08fc8a70 211 #endif
mbedAustin 11:cada08fc8a70 212
mbedAustin 11:cada08fc8a70 213 /*----------------------------------------------------------------------------
mbedAustin 11:cada08fc8a70 214 * end of file
mbedAustin 11:cada08fc8a70 215 *---------------------------------------------------------------------------*/
mbedAustin 11:cada08fc8a70 216