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: HAL_CM0.C
mbedAustin 11:cada08fc8a70 5 * Purpose: Hardware Abstraction Layer for Cortex-M0
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-2012 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 "rt_TypeDef.h"
mbedAustin 11:cada08fc8a70 36 #include "RTX_Conf.h"
mbedAustin 11:cada08fc8a70 37 #include "rt_System.h"
mbedAustin 11:cada08fc8a70 38 #include "rt_HAL_CM.h"
mbedAustin 11:cada08fc8a70 39 #include "rt_Task.h"
mbedAustin 11:cada08fc8a70 40 #include "rt_MemBox.h"
mbedAustin 11:cada08fc8a70 41
mbedAustin 11:cada08fc8a70 42
mbedAustin 11:cada08fc8a70 43 /*----------------------------------------------------------------------------
mbedAustin 11:cada08fc8a70 44 * Functions
mbedAustin 11:cada08fc8a70 45 *---------------------------------------------------------------------------*/
mbedAustin 11:cada08fc8a70 46
mbedAustin 11:cada08fc8a70 47
mbedAustin 11:cada08fc8a70 48 /*--------------------------- rt_set_PSP ------------------------------------*/
mbedAustin 11:cada08fc8a70 49
mbedAustin 11:cada08fc8a70 50 __asm void rt_set_PSP (U32 stack) {
mbedAustin 11:cada08fc8a70 51 MSR PSP,R0
mbedAustin 11:cada08fc8a70 52 BX LR
mbedAustin 11:cada08fc8a70 53 }
mbedAustin 11:cada08fc8a70 54
mbedAustin 11:cada08fc8a70 55
mbedAustin 11:cada08fc8a70 56 /*--------------------------- rt_get_PSP ------------------------------------*/
mbedAustin 11:cada08fc8a70 57
mbedAustin 11:cada08fc8a70 58 __asm U32 rt_get_PSP (void) {
mbedAustin 11:cada08fc8a70 59 MRS R0,PSP
mbedAustin 11:cada08fc8a70 60 BX LR
mbedAustin 11:cada08fc8a70 61 }
mbedAustin 11:cada08fc8a70 62
mbedAustin 11:cada08fc8a70 63
mbedAustin 11:cada08fc8a70 64 /*--------------------------- os_set_env ------------------------------------*/
mbedAustin 11:cada08fc8a70 65
mbedAustin 11:cada08fc8a70 66 __asm void os_set_env (void) {
mbedAustin 11:cada08fc8a70 67 /* Switch to Unprivileged/Privileged Thread mode, use PSP. */
mbedAustin 11:cada08fc8a70 68 MOV R0,SP ; PSP = MSP
mbedAustin 11:cada08fc8a70 69 MSR PSP,R0
mbedAustin 11:cada08fc8a70 70 LDR R0,=__cpp(&os_flags)
mbedAustin 11:cada08fc8a70 71 LDRB R0,[R0]
mbedAustin 11:cada08fc8a70 72 LSLS R0,#31
mbedAustin 11:cada08fc8a70 73 BNE PrivilegedE
mbedAustin 11:cada08fc8a70 74 MOVS R0,#0x03 ; Unprivileged Thread mode, use PSP
mbedAustin 11:cada08fc8a70 75 MSR CONTROL,R0
mbedAustin 11:cada08fc8a70 76 BX LR
mbedAustin 11:cada08fc8a70 77 PrivilegedE
mbedAustin 11:cada08fc8a70 78 MOVS R0,#0x02 ; Privileged Thread mode, use PSP
mbedAustin 11:cada08fc8a70 79 MSR CONTROL,R0
mbedAustin 11:cada08fc8a70 80 BX LR
mbedAustin 11:cada08fc8a70 81
mbedAustin 11:cada08fc8a70 82 ALIGN
mbedAustin 11:cada08fc8a70 83 }
mbedAustin 11:cada08fc8a70 84
mbedAustin 11:cada08fc8a70 85
mbedAustin 11:cada08fc8a70 86 /*--------------------------- _alloc_box ------------------------------------*/
mbedAustin 11:cada08fc8a70 87
mbedAustin 11:cada08fc8a70 88 __asm void *_alloc_box (void *box_mem) {
mbedAustin 11:cada08fc8a70 89 /* Function wrapper for Unprivileged/Privileged mode. */
mbedAustin 11:cada08fc8a70 90 LDR R3,=__cpp(rt_alloc_box)
mbedAustin 11:cada08fc8a70 91 MOV R12,R3
mbedAustin 11:cada08fc8a70 92 MRS R3,IPSR
mbedAustin 11:cada08fc8a70 93 LSLS R3,#24
mbedAustin 11:cada08fc8a70 94 BNE PrivilegedA
mbedAustin 11:cada08fc8a70 95 MRS R3,CONTROL
mbedAustin 11:cada08fc8a70 96 LSLS R3,#31
mbedAustin 11:cada08fc8a70 97 BEQ PrivilegedA
mbedAustin 11:cada08fc8a70 98 SVC 0
mbedAustin 11:cada08fc8a70 99 BX LR
mbedAustin 11:cada08fc8a70 100 PrivilegedA
mbedAustin 11:cada08fc8a70 101 BX R12
mbedAustin 11:cada08fc8a70 102
mbedAustin 11:cada08fc8a70 103 ALIGN
mbedAustin 11:cada08fc8a70 104 }
mbedAustin 11:cada08fc8a70 105
mbedAustin 11:cada08fc8a70 106
mbedAustin 11:cada08fc8a70 107 /*--------------------------- _free_box -------------------------------------*/
mbedAustin 11:cada08fc8a70 108
mbedAustin 11:cada08fc8a70 109 __asm int _free_box (void *box_mem, void *box) {
mbedAustin 11:cada08fc8a70 110 /* Function wrapper for Unprivileged/Privileged mode. */
mbedAustin 11:cada08fc8a70 111 LDR R3,=__cpp(rt_free_box)
mbedAustin 11:cada08fc8a70 112 MOV R12,R3
mbedAustin 11:cada08fc8a70 113 MRS R3,IPSR
mbedAustin 11:cada08fc8a70 114 LSLS R3,#24
mbedAustin 11:cada08fc8a70 115 BNE PrivilegedF
mbedAustin 11:cada08fc8a70 116 MRS R3,CONTROL
mbedAustin 11:cada08fc8a70 117 LSLS R3,#31
mbedAustin 11:cada08fc8a70 118 BEQ PrivilegedF
mbedAustin 11:cada08fc8a70 119 SVC 0
mbedAustin 11:cada08fc8a70 120 BX LR
mbedAustin 11:cada08fc8a70 121 PrivilegedF
mbedAustin 11:cada08fc8a70 122 BX R12
mbedAustin 11:cada08fc8a70 123
mbedAustin 11:cada08fc8a70 124 ALIGN
mbedAustin 11:cada08fc8a70 125 }
mbedAustin 11:cada08fc8a70 126
mbedAustin 11:cada08fc8a70 127
mbedAustin 11:cada08fc8a70 128 /*-------------------------- SVC_Handler ------------------------------------*/
mbedAustin 11:cada08fc8a70 129
mbedAustin 11:cada08fc8a70 130 __asm void SVC_Handler (void) {
mbedAustin 11:cada08fc8a70 131 PRESERVE8
mbedAustin 11:cada08fc8a70 132
mbedAustin 11:cada08fc8a70 133 IMPORT SVC_Count
mbedAustin 11:cada08fc8a70 134 IMPORT SVC_Table
mbedAustin 11:cada08fc8a70 135 IMPORT rt_stk_check
mbedAustin 11:cada08fc8a70 136
mbedAustin 11:cada08fc8a70 137 MRS R0,PSP ; Read PSP
mbedAustin 11:cada08fc8a70 138 LDR R1,[R0,#24] ; Read Saved PC from Stack
mbedAustin 11:cada08fc8a70 139 SUBS R1,R1,#2 ; Point to SVC Instruction
mbedAustin 11:cada08fc8a70 140 LDRB R1,[R1] ; Load SVC Number
mbedAustin 11:cada08fc8a70 141 CMP R1,#0
mbedAustin 11:cada08fc8a70 142 BNE SVC_User ; User SVC Number > 0
mbedAustin 11:cada08fc8a70 143
mbedAustin 11:cada08fc8a70 144 MOV LR,R4
mbedAustin 11:cada08fc8a70 145 LDMIA R0,{R0-R3,R4} ; Read R0-R3,R12 from stack
mbedAustin 11:cada08fc8a70 146 MOV R12,R4
mbedAustin 11:cada08fc8a70 147 MOV R4,LR
mbedAustin 11:cada08fc8a70 148 BLX R12 ; Call SVC Function
mbedAustin 11:cada08fc8a70 149
mbedAustin 11:cada08fc8a70 150 MRS R3,PSP ; Read PSP
mbedAustin 11:cada08fc8a70 151 STMIA R3!,{R0-R2} ; Store return values
mbedAustin 11:cada08fc8a70 152
mbedAustin 11:cada08fc8a70 153 LDR R3,=__cpp(&os_tsk)
mbedAustin 11:cada08fc8a70 154 LDMIA R3!,{R1,R2} ; os_tsk.run, os_tsk.new
mbedAustin 11:cada08fc8a70 155 CMP R1,R2
mbedAustin 11:cada08fc8a70 156 BEQ SVC_Exit ; no task switch
mbedAustin 11:cada08fc8a70 157
mbedAustin 11:cada08fc8a70 158 SUBS R3,#8
mbedAustin 11:cada08fc8a70 159 CMP R1,#0 ; Runtask deleted?
mbedAustin 11:cada08fc8a70 160 BEQ SVC_Next
mbedAustin 11:cada08fc8a70 161
mbedAustin 11:cada08fc8a70 162 MRS R0,PSP ; Read PSP
mbedAustin 11:cada08fc8a70 163 SUBS R0,R0,#32 ; Adjust Start Address
mbedAustin 11:cada08fc8a70 164 STR R0,[R1,#TCB_TSTACK] ; Update os_tsk.run->tsk_stack
mbedAustin 11:cada08fc8a70 165 STMIA R0!,{R4-R7} ; Save old context (R4-R7)
mbedAustin 11:cada08fc8a70 166 MOV R4,R8
mbedAustin 11:cada08fc8a70 167 MOV R5,R9
mbedAustin 11:cada08fc8a70 168 MOV R6,R10
mbedAustin 11:cada08fc8a70 169 MOV R7,R11
mbedAustin 11:cada08fc8a70 170 STMIA R0!,{R4-R7} ; Save old context (R8-R11)
mbedAustin 11:cada08fc8a70 171
mbedAustin 11:cada08fc8a70 172 PUSH {R2,R3}
mbedAustin 11:cada08fc8a70 173 BL rt_stk_check ; Check for Stack overflow
mbedAustin 11:cada08fc8a70 174 POP {R2,R3}
mbedAustin 11:cada08fc8a70 175
mbedAustin 11:cada08fc8a70 176 SVC_Next
mbedAustin 11:cada08fc8a70 177 STR R2,[R3] ; os_tsk.run = os_tsk.new
mbedAustin 11:cada08fc8a70 178
mbedAustin 11:cada08fc8a70 179 LDR R0,[R2,#TCB_TSTACK] ; os_tsk.new->tsk_stack
mbedAustin 11:cada08fc8a70 180 ADDS R0,R0,#16 ; Adjust Start Address
mbedAustin 11:cada08fc8a70 181 LDMIA R0!,{R4-R7} ; Restore new Context (R8-R11)
mbedAustin 11:cada08fc8a70 182 MOV R8,R4
mbedAustin 11:cada08fc8a70 183 MOV R9,R5
mbedAustin 11:cada08fc8a70 184 MOV R10,R6
mbedAustin 11:cada08fc8a70 185 MOV R11,R7
mbedAustin 11:cada08fc8a70 186 MSR PSP,R0 ; Write PSP
mbedAustin 11:cada08fc8a70 187 SUBS R0,R0,#32 ; Adjust Start Address
mbedAustin 11:cada08fc8a70 188 LDMIA R0!,{R4-R7} ; Restore new Context (R4-R7)
mbedAustin 11:cada08fc8a70 189
mbedAustin 11:cada08fc8a70 190 SVC_Exit
mbedAustin 11:cada08fc8a70 191 MOVS R0,#:NOT:0xFFFFFFFD ; Set EXC_RETURN value
mbedAustin 11:cada08fc8a70 192 MVNS R0,R0
mbedAustin 11:cada08fc8a70 193 BX R0 ; RETI to Thread Mode, use PSP
mbedAustin 11:cada08fc8a70 194
mbedAustin 11:cada08fc8a70 195 /*------------------- User SVC ------------------------------*/
mbedAustin 11:cada08fc8a70 196
mbedAustin 11:cada08fc8a70 197 SVC_User
mbedAustin 11:cada08fc8a70 198 PUSH {R4,LR} ; Save Registers
mbedAustin 11:cada08fc8a70 199 LDR R2,=SVC_Count
mbedAustin 11:cada08fc8a70 200 LDR R2,[R2]
mbedAustin 11:cada08fc8a70 201 CMP R1,R2
mbedAustin 11:cada08fc8a70 202 BHI SVC_Done ; Overflow
mbedAustin 11:cada08fc8a70 203
mbedAustin 11:cada08fc8a70 204 LDR R4,=SVC_Table-4
mbedAustin 11:cada08fc8a70 205 LSLS R1,R1,#2
mbedAustin 11:cada08fc8a70 206 LDR R4,[R4,R1] ; Load SVC Function Address
mbedAustin 11:cada08fc8a70 207 MOV LR,R4
mbedAustin 11:cada08fc8a70 208
mbedAustin 11:cada08fc8a70 209 LDMIA R0,{R0-R3,R4} ; Read R0-R3,R12 from stack
mbedAustin 11:cada08fc8a70 210 MOV R12,R4
mbedAustin 11:cada08fc8a70 211 BLX LR ; Call SVC Function
mbedAustin 11:cada08fc8a70 212
mbedAustin 11:cada08fc8a70 213 MRS R4,PSP ; Read PSP
mbedAustin 11:cada08fc8a70 214 STMIA R4!,{R0-R3} ; Function return values
mbedAustin 11:cada08fc8a70 215 SVC_Done
mbedAustin 11:cada08fc8a70 216 POP {R4,PC} ; RETI
mbedAustin 11:cada08fc8a70 217
mbedAustin 11:cada08fc8a70 218 ALIGN
mbedAustin 11:cada08fc8a70 219 }
mbedAustin 11:cada08fc8a70 220
mbedAustin 11:cada08fc8a70 221
mbedAustin 11:cada08fc8a70 222 /*-------------------------- PendSV_Handler ---------------------------------*/
mbedAustin 11:cada08fc8a70 223
mbedAustin 11:cada08fc8a70 224 __asm void PendSV_Handler (void) {
mbedAustin 11:cada08fc8a70 225 PRESERVE8
mbedAustin 11:cada08fc8a70 226
mbedAustin 11:cada08fc8a70 227 BL __cpp(rt_pop_req)
mbedAustin 11:cada08fc8a70 228
mbedAustin 11:cada08fc8a70 229 Sys_Switch
mbedAustin 11:cada08fc8a70 230 LDR R3,=__cpp(&os_tsk)
mbedAustin 11:cada08fc8a70 231 LDMIA R3!,{R1,R2} ; os_tsk.run, os_tsk.new
mbedAustin 11:cada08fc8a70 232 CMP R1,R2
mbedAustin 11:cada08fc8a70 233 BEQ Sys_Exit ; no task switch
mbedAustin 11:cada08fc8a70 234
mbedAustin 11:cada08fc8a70 235 SUBS R3,#8
mbedAustin 11:cada08fc8a70 236
mbedAustin 11:cada08fc8a70 237 MRS R0,PSP ; Read PSP
mbedAustin 11:cada08fc8a70 238 SUBS R0,R0,#32 ; Adjust Start Address
mbedAustin 11:cada08fc8a70 239 STR R0,[R1,#TCB_TSTACK] ; Update os_tsk.run->tsk_stack
mbedAustin 11:cada08fc8a70 240 STMIA R0!,{R4-R7} ; Save old context (R4-R7)
mbedAustin 11:cada08fc8a70 241 MOV R4,R8
mbedAustin 11:cada08fc8a70 242 MOV R5,R9
mbedAustin 11:cada08fc8a70 243 MOV R6,R10
mbedAustin 11:cada08fc8a70 244 MOV R7,R11
mbedAustin 11:cada08fc8a70 245 STMIA R0!,{R4-R7} ; Save old context (R8-R11)
mbedAustin 11:cada08fc8a70 246
mbedAustin 11:cada08fc8a70 247 PUSH {R2,R3}
mbedAustin 11:cada08fc8a70 248 BL rt_stk_check ; Check for Stack overflow
mbedAustin 11:cada08fc8a70 249 POP {R2,R3}
mbedAustin 11:cada08fc8a70 250
mbedAustin 11:cada08fc8a70 251 STR R2,[R3] ; os_tsk.run = os_tsk.new
mbedAustin 11:cada08fc8a70 252
mbedAustin 11:cada08fc8a70 253 LDR R0,[R2,#TCB_TSTACK] ; os_tsk.new->tsk_stack
mbedAustin 11:cada08fc8a70 254 ADDS R0,R0,#16 ; Adjust Start Address
mbedAustin 11:cada08fc8a70 255 LDMIA R0!,{R4-R7} ; Restore new Context (R8-R11)
mbedAustin 11:cada08fc8a70 256 MOV R8,R4
mbedAustin 11:cada08fc8a70 257 MOV R9,R5
mbedAustin 11:cada08fc8a70 258 MOV R10,R6
mbedAustin 11:cada08fc8a70 259 MOV R11,R7
mbedAustin 11:cada08fc8a70 260 MSR PSP,R0 ; Write PSP
mbedAustin 11:cada08fc8a70 261 SUBS R0,R0,#32 ; Adjust Start Address
mbedAustin 11:cada08fc8a70 262 LDMIA R0!,{R4-R7} ; Restore new Context (R4-R7)
mbedAustin 11:cada08fc8a70 263
mbedAustin 11:cada08fc8a70 264 Sys_Exit
mbedAustin 11:cada08fc8a70 265 MOVS R0,#:NOT:0xFFFFFFFD ; Set EXC_RETURN value
mbedAustin 11:cada08fc8a70 266 MVNS R0,R0
mbedAustin 11:cada08fc8a70 267 BX R0 ; RETI to Thread Mode, use PSP
mbedAustin 11:cada08fc8a70 268
mbedAustin 11:cada08fc8a70 269 ALIGN
mbedAustin 11:cada08fc8a70 270 }
mbedAustin 11:cada08fc8a70 271
mbedAustin 11:cada08fc8a70 272
mbedAustin 11:cada08fc8a70 273 /*-------------------------- SysTick_Handler --------------------------------*/
mbedAustin 11:cada08fc8a70 274
mbedAustin 11:cada08fc8a70 275 __asm void SysTick_Handler (void) {
mbedAustin 11:cada08fc8a70 276 PRESERVE8
mbedAustin 11:cada08fc8a70 277
mbedAustin 11:cada08fc8a70 278 BL __cpp(rt_systick)
mbedAustin 11:cada08fc8a70 279 B Sys_Switch
mbedAustin 11:cada08fc8a70 280
mbedAustin 11:cada08fc8a70 281 ALIGN
mbedAustin 11:cada08fc8a70 282 }
mbedAustin 11:cada08fc8a70 283
mbedAustin 11:cada08fc8a70 284
mbedAustin 11:cada08fc8a70 285 /*-------------------------- OS_Tick_Handler --------------------------------*/
mbedAustin 11:cada08fc8a70 286
mbedAustin 11:cada08fc8a70 287 __asm void OS_Tick_Handler (void) {
mbedAustin 11:cada08fc8a70 288 PRESERVE8
mbedAustin 11:cada08fc8a70 289
mbedAustin 11:cada08fc8a70 290 BL __cpp(os_tick_irqack)
mbedAustin 11:cada08fc8a70 291 BL __cpp(rt_systick)
mbedAustin 11:cada08fc8a70 292 B Sys_Switch
mbedAustin 11:cada08fc8a70 293
mbedAustin 11:cada08fc8a70 294 ALIGN
mbedAustin 11:cada08fc8a70 295 }
mbedAustin 11:cada08fc8a70 296
mbedAustin 11:cada08fc8a70 297
mbedAustin 11:cada08fc8a70 298 /*----------------------------------------------------------------------------
mbedAustin 11:cada08fc8a70 299 * end of file
mbedAustin 11:cada08fc8a70 300 *---------------------------------------------------------------------------*/
mbedAustin 11:cada08fc8a70 301