Nordic nrf51 sdk sources. Mirrored from https://github.com/ARMmbed/nrf51-sdk.

Dependents:   nRF51822 nRF51822

Committer:
vcoubard
Date:
Thu Apr 07 17:37:56 2016 +0100
Revision:
28:041dac1366b2
Parent:
20:a90c48eb1d30
Child:
29:286940b7ee5a
Synchronized with git rev 012b8118
Author: Liyou Zhou
Pull in files from sdk 10.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 28:041dac1366b2 1 /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
vcoubard 28:041dac1366b2 2 *
vcoubard 28:041dac1366b2 3 * The information contained herein is property of Nordic Semiconductor ASA.
vcoubard 28:041dac1366b2 4 * Terms and conditions of usage are described in detail in NORDIC
vcoubard 28:041dac1366b2 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
vcoubard 28:041dac1366b2 6 *
vcoubard 28:041dac1366b2 7 * Licensees are granted free, non-transferable use of the information. NO
vcoubard 28:041dac1366b2 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
vcoubard 28:041dac1366b2 9 * the file.
vcoubard 28:041dac1366b2 10 *
vcoubard 1:ebc0e0ef0a11 11 */
vcoubard 1:ebc0e0ef0a11 12
vcoubard 1:ebc0e0ef0a11 13 #include "bootloader_util.h"
vcoubard 1:ebc0e0ef0a11 14 #include <stdint.h>
vcoubard 1:ebc0e0ef0a11 15 #include <string.h>
vcoubard 1:ebc0e0ef0a11 16
vcoubard 1:ebc0e0ef0a11 17
vcoubard 1:ebc0e0ef0a11 18 /**
vcoubard 1:ebc0e0ef0a11 19 * @brief Function for aborting current application/bootloader jump to to other app/bootloader.
vcoubard 1:ebc0e0ef0a11 20 *
vcoubard 1:ebc0e0ef0a11 21 * @details This functions will use the address provide to swap the stack pointer and then load
vcoubard 1:ebc0e0ef0a11 22 * the address of the reset handler to be executed. It will check current system mode
vcoubard 1:ebc0e0ef0a11 23 * (thread/handler) and if in thread mode it will reset into other application.
vcoubard 1:ebc0e0ef0a11 24 * If in handler mode \ref isr_abort will be executed to ensure correct exit of handler
vcoubard 1:ebc0e0ef0a11 25 * mode and jump into reset handler of other application.
vcoubard 1:ebc0e0ef0a11 26 *
vcoubard 1:ebc0e0ef0a11 27 * @param[in] start_addr Start address of other application. This address must point to the
vcoubard 1:ebc0e0ef0a11 28 initial stack pointer of the application.
vcoubard 1:ebc0e0ef0a11 29 *
vcoubard 1:ebc0e0ef0a11 30 * @note This function will never return but issue a reset into provided application.
vcoubard 1:ebc0e0ef0a11 31 */
vcoubard 1:ebc0e0ef0a11 32 #if defined ( __CC_ARM )
vcoubard 1:ebc0e0ef0a11 33 __asm static void bootloader_util_reset(uint32_t start_addr)
vcoubard 1:ebc0e0ef0a11 34 {
vcoubard 1:ebc0e0ef0a11 35 LDR R5, [R0] ; Get App initial MSP for bootloader.
vcoubard 1:ebc0e0ef0a11 36 MSR MSP, R5 ; Set the main stack pointer to the applications MSP.
vcoubard 1:ebc0e0ef0a11 37 LDR R0, [R0, #0x04] ; Load Reset handler into R0. This will be first argument to branch instruction (BX).
vcoubard 1:ebc0e0ef0a11 38
vcoubard 1:ebc0e0ef0a11 39 MOVS R4, #0xFF ; Load ones to R4.
vcoubard 1:ebc0e0ef0a11 40 SXTB R4, R4 ; Sign extend R4 to obtain 0xFFFFFFFF instead of 0xFF.
vcoubard 1:ebc0e0ef0a11 41 MRS R5, IPSR ; Load IPSR to R5 to check for handler or thread mode.
vcoubard 1:ebc0e0ef0a11 42 CMP R5, #0x00 ; Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 43 BNE isr_abort ; If not zero we need to exit current ISR and jump to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 44
vcoubard 1:ebc0e0ef0a11 45 MOV LR, R4 ; Clear the link register and set to ones to ensure no return, R4 = 0xFFFFFFFF.
vcoubard 1:ebc0e0ef0a11 46 BX R0 ; Branch to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 47
vcoubard 1:ebc0e0ef0a11 48 isr_abort
vcoubard 1:ebc0e0ef0a11 49 ; R4 contains ones from line above. Will be popped as R12 when exiting ISR (Cleaning up the registers).
vcoubard 1:ebc0e0ef0a11 50 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 51 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 52 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 53 REV r7, r7 ; Reverse byte order to put 0x21 as MSB.
vcoubard 1:ebc0e0ef0a11 54 PUSH {r4-r7} ; Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
vcoubard 1:ebc0e0ef0a11 55
vcoubard 1:ebc0e0ef0a11 56 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 57 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 58 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 59 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 60 PUSH {r4-r7} ; Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
vcoubard 1:ebc0e0ef0a11 61
vcoubard 1:ebc0e0ef0a11 62 MOVS R0, #0xF9 ; Move the execution return command into register, 0xFFFFFFF9.
vcoubard 1:ebc0e0ef0a11 63 SXTB R0, R0 ; Sign extend R0 to obtain 0xFFFFFFF9 instead of 0xF9.
vcoubard 1:ebc0e0ef0a11 64 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 65 ALIGN
vcoubard 1:ebc0e0ef0a11 66 }
vcoubard 1:ebc0e0ef0a11 67 #elif defined ( __GNUC__ )
vcoubard 1:ebc0e0ef0a11 68 static inline void bootloader_util_reset(uint32_t start_addr)
vcoubard 1:ebc0e0ef0a11 69 {
vcoubard 1:ebc0e0ef0a11 70 __asm volatile(
vcoubard 1:ebc0e0ef0a11 71 "ldr r0, [%0]\t\n" // Get App initial MSP for bootloader.
vcoubard 1:ebc0e0ef0a11 72 "msr msp, r0\t\n" // Set the main stack pointer to the applications MSP.
vcoubard 1:ebc0e0ef0a11 73 "ldr r0, [%0, #0x04]\t\n" // Load Reset handler into R0.
vcoubard 1:ebc0e0ef0a11 74
vcoubard 1:ebc0e0ef0a11 75 "movs r4, #0xFF\t\n" // Move ones to R4.
vcoubard 1:ebc0e0ef0a11 76 "sxtb r4, r4\t\n" // Sign extend R4 to obtain 0xFFFFFFFF instead of 0xFF.
vcoubard 1:ebc0e0ef0a11 77
vcoubard 1:ebc0e0ef0a11 78 "mrs r5, IPSR\t\n" // Load IPSR to R5 to check for handler or thread mode.
vcoubard 1:ebc0e0ef0a11 79 "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 80 "bne isr_abort\t\n" // If not zero we need to exit current ISR and jump to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 81
vcoubard 1:ebc0e0ef0a11 82 "mov lr, r4\t\n" // Clear the link register and set to ones to ensure no return.
vcoubard 1:ebc0e0ef0a11 83 "bx r0\t\n" // Branch to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 84
vcoubard 1:ebc0e0ef0a11 85 "isr_abort: \t\n"
vcoubard 1:ebc0e0ef0a11 86
vcoubard 1:ebc0e0ef0a11 87 "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 88 "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 89 "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 90 "rev r7, r7\t\n" // Reverse byte order to put 0x21 as MSB.
vcoubard 1:ebc0e0ef0a11 91 "push {r4-r7}\t\n" // Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
vcoubard 1:ebc0e0ef0a11 92
vcoubard 1:ebc0e0ef0a11 93 "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 94 "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 95 "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 96 "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 97 "push {r4-r7}\t\n" // Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
vcoubard 1:ebc0e0ef0a11 98
vcoubard 1:ebc0e0ef0a11 99 "movs r0, #0xF9\t\n" // Move the execution return command into register, 0xFFFFFFF9.
vcoubard 1:ebc0e0ef0a11 100 "sxtb r0, r0\t\n" // Sign extend R0 to obtain 0xFFFFFFF9 instead of 0xF9.
vcoubard 1:ebc0e0ef0a11 101 "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 102 ".align\t\n"
vcoubard 1:ebc0e0ef0a11 103 :: "r" (start_addr) // Argument list for the gcc assembly. start_addr is %0.
vcoubard 1:ebc0e0ef0a11 104 : "r0", "r4", "r5", "r6", "r7" // List of register maintained manually.
vcoubard 1:ebc0e0ef0a11 105 );
vcoubard 1:ebc0e0ef0a11 106 }
vcoubard 1:ebc0e0ef0a11 107 #elif defined ( __ICCARM__ )
vcoubard 1:ebc0e0ef0a11 108 static inline void bootloader_util_reset(uint32_t start_addr)
vcoubard 1:ebc0e0ef0a11 109 {
vcoubard 1:ebc0e0ef0a11 110 asm("ldr r5, [%0]\n" // Get App initial MSP for bootloader.
vcoubard 1:ebc0e0ef0a11 111 "msr msp, r5\n" // Set the main stack pointer to the applications MSP.
vcoubard 1:ebc0e0ef0a11 112 "ldr r0, [%0, #0x04]\n" // Load Reset handler into R0.
vcoubard 1:ebc0e0ef0a11 113
vcoubard 1:ebc0e0ef0a11 114 "movs r4, #0x00\n" // Load zero into R4.
vcoubard 1:ebc0e0ef0a11 115 "mvns r4, r4\n" // Invert R4 to ensure it contain ones.
vcoubard 1:ebc0e0ef0a11 116
vcoubard 1:ebc0e0ef0a11 117 "mrs r5, IPSR\n" // Load IPSR to R5 to check for handler or thread mode
vcoubard 1:ebc0e0ef0a11 118 "cmp r5, #0x00\n" // Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 119 "bne isr_abort\n" // If not zero we need to exit current ISR and jump to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 120
vcoubard 1:ebc0e0ef0a11 121 "mov lr, r4\n" // Clear the link register and set to ones to ensure no return.
vcoubard 1:ebc0e0ef0a11 122 "bx r0\n" // Branch to reset handler of bootloader.
vcoubard 1:ebc0e0ef0a11 123
vcoubard 1:ebc0e0ef0a11 124 "isr_abort: \n"
vcoubard 1:ebc0e0ef0a11 125 // R4 contains ones from line above. We be popped as R12 when exiting ISR (Cleaning up the registers).
vcoubard 1:ebc0e0ef0a11 126 "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 127 "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 128 "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 129 "rev r7, r7\n" // Reverse byte order to put 0x21 as MSB.
vcoubard 1:ebc0e0ef0a11 130 "push {r4-r7}\n" // Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
vcoubard 1:ebc0e0ef0a11 131
vcoubard 1:ebc0e0ef0a11 132 "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 133 "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 134 "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 135 "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 136 "push {r4-r7}\n" // Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
vcoubard 1:ebc0e0ef0a11 137
vcoubard 1:ebc0e0ef0a11 138 "movs r0, #0x06\n" // Load 0x06 into R6 to prepare for exec return command.
vcoubard 1:ebc0e0ef0a11 139 "mvns r0, r0\n" // Invert 0x06 to obtain EXEC_RETURN, 0xFFFFFFF9.
vcoubard 1:ebc0e0ef0a11 140 "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 141 :: "r" (start_addr) // Argument list for the IAR assembly. start_addr is %0.
vcoubard 1:ebc0e0ef0a11 142 : "r0", "r4", "r5", "r6", "r7"); // List of register maintained manually.
vcoubard 1:ebc0e0ef0a11 143 }
vcoubard 1:ebc0e0ef0a11 144 #else
vcoubard 1:ebc0e0ef0a11 145 #error Compiler not supported.
vcoubard 1:ebc0e0ef0a11 146 #endif
vcoubard 1:ebc0e0ef0a11 147
vcoubard 1:ebc0e0ef0a11 148
vcoubard 1:ebc0e0ef0a11 149 void bootloader_util_app_start(uint32_t start_addr)
vcoubard 1:ebc0e0ef0a11 150 {
vcoubard 1:ebc0e0ef0a11 151 bootloader_util_reset(start_addr);
vcoubard 1:ebc0e0ef0a11 152 }