Toyomasa Watarai / nrf51-sdk

Fork of nrf51-sdk by Nordic Semiconductor

Committer:
vcoubard
Date:
Thu Apr 07 17:38:01 2016 +0100
Revision:
29:286940b7ee5a
Parent:
28:041dac1366b2
Synchronized with git rev 22c7454f
Author: Liyou Zhou
Add copyright license headers.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 29:286940b7ee5a 1 /*
vcoubard 29:286940b7ee5a 2 * Copyright (c) Nordic Semiconductor ASA
vcoubard 29:286940b7ee5a 3 * All rights reserved.
vcoubard 29:286940b7ee5a 4 *
vcoubard 29:286940b7ee5a 5 * Redistribution and use in source and binary forms, with or without modification,
vcoubard 29:286940b7ee5a 6 * are permitted provided that the following conditions are met:
vcoubard 29:286940b7ee5a 7 *
vcoubard 29:286940b7ee5a 8 * 1. Redistributions of source code must retain the above copyright notice, this
vcoubard 29:286940b7ee5a 9 * list of conditions and the following disclaimer.
vcoubard 29:286940b7ee5a 10 *
vcoubard 29:286940b7ee5a 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this
vcoubard 29:286940b7ee5a 12 * list of conditions and the following disclaimer in the documentation and/or
vcoubard 29:286940b7ee5a 13 * other materials provided with the distribution.
vcoubard 29:286940b7ee5a 14 *
vcoubard 29:286940b7ee5a 15 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
vcoubard 29:286940b7ee5a 16 * contributors to this software may be used to endorse or promote products
vcoubard 29:286940b7ee5a 17 * derived from this software without specific prior written permission.
vcoubard 29:286940b7ee5a 18 *
vcoubard 29:286940b7ee5a 19 *
vcoubard 29:286940b7ee5a 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
vcoubard 29:286940b7ee5a 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
vcoubard 29:286940b7ee5a 22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
vcoubard 29:286940b7ee5a 23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
vcoubard 29:286940b7ee5a 24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
vcoubard 29:286940b7ee5a 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
vcoubard 29:286940b7ee5a 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
vcoubard 29:286940b7ee5a 27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
vcoubard 29:286940b7ee5a 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
vcoubard 29:286940b7ee5a 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
vcoubard 29:286940b7ee5a 30 *
vcoubard 1:ebc0e0ef0a11 31 */
vcoubard 1:ebc0e0ef0a11 32
vcoubard 1:ebc0e0ef0a11 33 #include "bootloader_util.h"
vcoubard 1:ebc0e0ef0a11 34 #include <stdint.h>
vcoubard 1:ebc0e0ef0a11 35 #include <string.h>
vcoubard 1:ebc0e0ef0a11 36
vcoubard 1:ebc0e0ef0a11 37
vcoubard 1:ebc0e0ef0a11 38 /**
vcoubard 1:ebc0e0ef0a11 39 * @brief Function for aborting current application/bootloader jump to to other app/bootloader.
vcoubard 1:ebc0e0ef0a11 40 *
vcoubard 1:ebc0e0ef0a11 41 * @details This functions will use the address provide to swap the stack pointer and then load
vcoubard 1:ebc0e0ef0a11 42 * the address of the reset handler to be executed. It will check current system mode
vcoubard 1:ebc0e0ef0a11 43 * (thread/handler) and if in thread mode it will reset into other application.
vcoubard 1:ebc0e0ef0a11 44 * If in handler mode \ref isr_abort will be executed to ensure correct exit of handler
vcoubard 1:ebc0e0ef0a11 45 * mode and jump into reset handler of other application.
vcoubard 1:ebc0e0ef0a11 46 *
vcoubard 1:ebc0e0ef0a11 47 * @param[in] start_addr Start address of other application. This address must point to the
vcoubard 1:ebc0e0ef0a11 48 initial stack pointer of the application.
vcoubard 1:ebc0e0ef0a11 49 *
vcoubard 1:ebc0e0ef0a11 50 * @note This function will never return but issue a reset into provided application.
vcoubard 1:ebc0e0ef0a11 51 */
vcoubard 1:ebc0e0ef0a11 52 #if defined ( __CC_ARM )
vcoubard 1:ebc0e0ef0a11 53 __asm static void bootloader_util_reset(uint32_t start_addr)
vcoubard 1:ebc0e0ef0a11 54 {
vcoubard 1:ebc0e0ef0a11 55 LDR R5, [R0] ; Get App initial MSP for bootloader.
vcoubard 1:ebc0e0ef0a11 56 MSR MSP, R5 ; Set the main stack pointer to the applications MSP.
vcoubard 1:ebc0e0ef0a11 57 LDR R0, [R0, #0x04] ; Load Reset handler into R0. This will be first argument to branch instruction (BX).
vcoubard 1:ebc0e0ef0a11 58
vcoubard 1:ebc0e0ef0a11 59 MOVS R4, #0xFF ; Load ones to R4.
vcoubard 1:ebc0e0ef0a11 60 SXTB R4, R4 ; Sign extend R4 to obtain 0xFFFFFFFF instead of 0xFF.
vcoubard 1:ebc0e0ef0a11 61 MRS R5, IPSR ; Load IPSR to R5 to check for handler or thread mode.
vcoubard 1:ebc0e0ef0a11 62 CMP R5, #0x00 ; Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 63 BNE isr_abort ; If not zero we need to exit current ISR and jump to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 64
vcoubard 1:ebc0e0ef0a11 65 MOV LR, R4 ; Clear the link register and set to ones to ensure no return, R4 = 0xFFFFFFFF.
vcoubard 1:ebc0e0ef0a11 66 BX R0 ; Branch to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 67
vcoubard 1:ebc0e0ef0a11 68 isr_abort
vcoubard 1:ebc0e0ef0a11 69 ; R4 contains ones from line above. Will be popped as R12 when exiting ISR (Cleaning up the registers).
vcoubard 1:ebc0e0ef0a11 70 MOV R5, R4 ; Fill with ones before jumping to reset handling. We be popped as LR when exiting ISR. Ensures no return to application.
vcoubard 1:ebc0e0ef0a11 71 MOV R6, R0 ; Move address of reset handler to R6. Will be popped as PC when exiting ISR. Ensures the reset handler will be executed when exist ISR.
vcoubard 1:ebc0e0ef0a11 72 MOVS r7, #0x21 ; Move MSB reset value of xPSR to R7. Will be popped as xPSR when exiting ISR. xPSR is 0x21000000 thus MSB is 0x21.
vcoubard 1:ebc0e0ef0a11 73 REV r7, r7 ; Reverse byte order to put 0x21 as MSB.
vcoubard 1:ebc0e0ef0a11 74 PUSH {r4-r7} ; Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
vcoubard 1:ebc0e0ef0a11 75
vcoubard 1:ebc0e0ef0a11 76 MOVS R4, #0x00 ; Fill with zeros before jumping to reset handling. We be popped as R0 when exiting ISR (Cleaning up of the registers).
vcoubard 1:ebc0e0ef0a11 77 MOVS R5, #0x00 ; Fill with zeros before jumping to reset handling. We be popped as R1 when exiting ISR (Cleaning up of the registers).
vcoubard 1:ebc0e0ef0a11 78 MOVS R6, #0x00 ; Fill with zeros before jumping to reset handling. We be popped as R2 when exiting ISR (Cleaning up of the registers).
vcoubard 1:ebc0e0ef0a11 79 MOVS R7, #0x00 ; Fill with zeros before jumping to reset handling. We be popped as R3 when exiting ISR (Cleaning up of the registers).
vcoubard 1:ebc0e0ef0a11 80 PUSH {r4-r7} ; Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
vcoubard 1:ebc0e0ef0a11 81
vcoubard 1:ebc0e0ef0a11 82 MOVS R0, #0xF9 ; Move the execution return command into register, 0xFFFFFFF9.
vcoubard 1:ebc0e0ef0a11 83 SXTB R0, R0 ; Sign extend R0 to obtain 0xFFFFFFF9 instead of 0xF9.
vcoubard 1:ebc0e0ef0a11 84 BX R0 ; No return - Handler mode will be exited. Stack will be popped and execution will continue in reset handler initializing other application.
vcoubard 1:ebc0e0ef0a11 85 ALIGN
vcoubard 1:ebc0e0ef0a11 86 }
vcoubard 1:ebc0e0ef0a11 87 #elif defined ( __GNUC__ )
vcoubard 1:ebc0e0ef0a11 88 static inline void bootloader_util_reset(uint32_t start_addr)
vcoubard 1:ebc0e0ef0a11 89 {
vcoubard 1:ebc0e0ef0a11 90 __asm volatile(
vcoubard 1:ebc0e0ef0a11 91 "ldr r0, [%0]\t\n" // Get App initial MSP for bootloader.
vcoubard 1:ebc0e0ef0a11 92 "msr msp, r0\t\n" // Set the main stack pointer to the applications MSP.
vcoubard 1:ebc0e0ef0a11 93 "ldr r0, [%0, #0x04]\t\n" // Load Reset handler into R0.
vcoubard 1:ebc0e0ef0a11 94
vcoubard 1:ebc0e0ef0a11 95 "movs r4, #0xFF\t\n" // Move ones to R4.
vcoubard 1:ebc0e0ef0a11 96 "sxtb r4, r4\t\n" // Sign extend R4 to obtain 0xFFFFFFFF instead of 0xFF.
vcoubard 1:ebc0e0ef0a11 97
vcoubard 1:ebc0e0ef0a11 98 "mrs r5, IPSR\t\n" // Load IPSR to R5 to check for handler or thread mode.
vcoubard 1:ebc0e0ef0a11 99 "cmp r5, #0x00\t\n" // Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 100 "bne isr_abort\t\n" // If not zero we need to exit current ISR and jump to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 101
vcoubard 1:ebc0e0ef0a11 102 "mov lr, r4\t\n" // Clear the link register and set to ones to ensure no return.
vcoubard 1:ebc0e0ef0a11 103 "bx r0\t\n" // Branch to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 104
vcoubard 1:ebc0e0ef0a11 105 "isr_abort: \t\n"
vcoubard 1:ebc0e0ef0a11 106
vcoubard 1:ebc0e0ef0a11 107 "mov r5, r4\t\n" // Fill with ones before jumping to reset handling. Will be popped as LR when exiting ISR. Ensures no return to application.
vcoubard 1:ebc0e0ef0a11 108 "mov r6, r0\t\n" // Move address of reset handler to R6. Will be popped as PC when exiting ISR. Ensures the reset handler will be executed when exist ISR.
vcoubard 1:ebc0e0ef0a11 109 "movs r7, #0x21\t\n" // Move MSB reset value of xPSR to R7. Will be popped as xPSR when exiting ISR. xPSR is 0x21000000 thus MSB is 0x21.
vcoubard 1:ebc0e0ef0a11 110 "rev r7, r7\t\n" // Reverse byte order to put 0x21 as MSB.
vcoubard 1:ebc0e0ef0a11 111 "push {r4-r7}\t\n" // Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
vcoubard 1:ebc0e0ef0a11 112
vcoubard 1:ebc0e0ef0a11 113 "movs r4, #0x00\t\n" // Fill with zeros before jumping to reset handling. We be popped as R0 when exiting ISR (Cleaning up of the registers).
vcoubard 1:ebc0e0ef0a11 114 "movs r5, #0x00\t\n" // Fill with zeros before jumping to reset handling. We be popped as R1 when exiting ISR (Cleaning up of the registers).
vcoubard 1:ebc0e0ef0a11 115 "movs r6, #0x00\t\n" // Fill with zeros before jumping to reset handling. We be popped as R2 when exiting ISR (Cleaning up of the registers).
vcoubard 1:ebc0e0ef0a11 116 "movs r7, #0x00\t\n" // Fill with zeros before jumping to reset handling. We be popped as R3 when exiting ISR (Cleaning up of the registers).
vcoubard 1:ebc0e0ef0a11 117 "push {r4-r7}\t\n" // Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
vcoubard 1:ebc0e0ef0a11 118
vcoubard 1:ebc0e0ef0a11 119 "movs r0, #0xF9\t\n" // Move the execution return command into register, 0xFFFFFFF9.
vcoubard 1:ebc0e0ef0a11 120 "sxtb r0, r0\t\n" // Sign extend R0 to obtain 0xFFFFFFF9 instead of 0xF9.
vcoubard 1:ebc0e0ef0a11 121 "bx r0\t\n" // No return - Handler mode will be exited. Stack will be popped and execution will continue in reset handler initializing other application.
vcoubard 1:ebc0e0ef0a11 122 ".align\t\n"
vcoubard 1:ebc0e0ef0a11 123 :: "r" (start_addr) // Argument list for the gcc assembly. start_addr is %0.
vcoubard 1:ebc0e0ef0a11 124 : "r0", "r4", "r5", "r6", "r7" // List of register maintained manually.
vcoubard 1:ebc0e0ef0a11 125 );
vcoubard 1:ebc0e0ef0a11 126 }
vcoubard 1:ebc0e0ef0a11 127 #elif defined ( __ICCARM__ )
vcoubard 1:ebc0e0ef0a11 128 static inline void bootloader_util_reset(uint32_t start_addr)
vcoubard 1:ebc0e0ef0a11 129 {
vcoubard 1:ebc0e0ef0a11 130 asm("ldr r5, [%0]\n" // Get App initial MSP for bootloader.
vcoubard 1:ebc0e0ef0a11 131 "msr msp, r5\n" // Set the main stack pointer to the applications MSP.
vcoubard 1:ebc0e0ef0a11 132 "ldr r0, [%0, #0x04]\n" // Load Reset handler into R0.
vcoubard 1:ebc0e0ef0a11 133
vcoubard 1:ebc0e0ef0a11 134 "movs r4, #0x00\n" // Load zero into R4.
vcoubard 1:ebc0e0ef0a11 135 "mvns r4, r4\n" // Invert R4 to ensure it contain ones.
vcoubard 1:ebc0e0ef0a11 136
vcoubard 1:ebc0e0ef0a11 137 "mrs r5, IPSR\n" // Load IPSR to R5 to check for handler or thread mode
vcoubard 1:ebc0e0ef0a11 138 "cmp r5, #0x00\n" // Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 139 "bne isr_abort\n" // If not zero we need to exit current ISR and jump to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 140
vcoubard 1:ebc0e0ef0a11 141 "mov lr, r4\n" // Clear the link register and set to ones to ensure no return.
vcoubard 1:ebc0e0ef0a11 142 "bx r0\n" // Branch to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 143
vcoubard 1:ebc0e0ef0a11 144 "isr_abort: \n"
vcoubard 1:ebc0e0ef0a11 145 // R4 contains ones from line above. We be popped as R12 when exiting ISR (Cleaning up the registers).
vcoubard 1:ebc0e0ef0a11 146 "mov r5, r4\n" // Fill with ones before jumping to reset handling. Will be popped as LR when exiting ISR. Ensures no return to application.
vcoubard 1:ebc0e0ef0a11 147 "mov r6, r0\n" // Move address of reset handler to R6. Will be popped as PC when exiting ISR. Ensures the reset handler will be executed when exist ISR.
vcoubard 1:ebc0e0ef0a11 148 "movs r7, #0x21\n" // Move MSB reset value of xPSR to R7. Will be popped as xPSR when exiting ISR. xPSR is 0x21000000 thus MSB is 0x21.
vcoubard 1:ebc0e0ef0a11 149 "rev r7, r7\n" // Reverse byte order to put 0x21 as MSB.
vcoubard 1:ebc0e0ef0a11 150 "push {r4-r7}\n" // Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
vcoubard 1:ebc0e0ef0a11 151
vcoubard 1:ebc0e0ef0a11 152 "movs r4, #0x00\n" // Fill with zeros before jumping to reset handling. We be popped as R0 when exiting ISR (Cleaning up of the registers).
vcoubard 1:ebc0e0ef0a11 153 "movs r5, #0x00\n" // Fill with zeros before jumping to reset handling. We be popped as R1 when exiting ISR (Cleaning up of the registers).
vcoubard 1:ebc0e0ef0a11 154 "movs r6, #0x00\n" // Fill with zeros before jumping to reset handling. We be popped as R2 when exiting ISR (Cleaning up of the registers).
vcoubard 1:ebc0e0ef0a11 155 "movs r7, #0x00\n" // Fill with zeros before jumping to reset handling. We be popped as R3 when exiting ISR (Cleaning up of the registers).
vcoubard 1:ebc0e0ef0a11 156 "push {r4-r7}\n" // Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
vcoubard 1:ebc0e0ef0a11 157
vcoubard 1:ebc0e0ef0a11 158 "movs r0, #0x06\n" // Load 0x06 into R6 to prepare for exec return command.
vcoubard 1:ebc0e0ef0a11 159 "mvns r0, r0\n" // Invert 0x06 to obtain EXEC_RETURN, 0xFFFFFFF9.
vcoubard 1:ebc0e0ef0a11 160 "bx r0\n" // No return - Handler mode will be exited. Stack will be popped and execution will continue in reset handler initializing other application.
vcoubard 1:ebc0e0ef0a11 161 :: "r" (start_addr) // Argument list for the IAR assembly. start_addr is %0.
vcoubard 1:ebc0e0ef0a11 162 : "r0", "r4", "r5", "r6", "r7"); // List of register maintained manually.
vcoubard 1:ebc0e0ef0a11 163 }
vcoubard 1:ebc0e0ef0a11 164 #else
vcoubard 1:ebc0e0ef0a11 165 #error Compiler not supported.
vcoubard 1:ebc0e0ef0a11 166 #endif
vcoubard 1:ebc0e0ef0a11 167
vcoubard 1:ebc0e0ef0a11 168
vcoubard 1:ebc0e0ef0a11 169 void bootloader_util_app_start(uint32_t start_addr)
vcoubard 1:ebc0e0ef0a11 170 {
vcoubard 1:ebc0e0ef0a11 171 bootloader_util_reset(start_addr);
vcoubard 1:ebc0e0ef0a11 172 }