To get started with Seeed Tiny BLE, include detecting motion, button and battery level.
Dependencies: BLE_API eMPL_MPU6050 mbed nRF51822
nRF51822/nordic-sdk/components/libraries/bootloader_dfu/bootloader_util_arm.c@1:fc2f9d636751, 2015-04-22 (annotated)
- Committer:
- yihui
- Date:
- Wed Apr 22 07:47:17 2015 +0000
- Revision:
- 1:fc2f9d636751
update libraries; ; delete nRF51822/nordic-sdk/components/gpiote/app_gpiote.c to solve GPIOTE_IRQHandler multiply defined issue. temperarily change nRF51822 library to folder
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 1:fc2f9d636751 | 1 | /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved. |
yihui | 1:fc2f9d636751 | 2 | * |
yihui | 1:fc2f9d636751 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
yihui | 1:fc2f9d636751 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
yihui | 1:fc2f9d636751 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
yihui | 1:fc2f9d636751 | 6 | * |
yihui | 1:fc2f9d636751 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
yihui | 1:fc2f9d636751 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
yihui | 1:fc2f9d636751 | 9 | * the file. |
yihui | 1:fc2f9d636751 | 10 | * |
yihui | 1:fc2f9d636751 | 11 | */ |
yihui | 1:fc2f9d636751 | 12 | |
yihui | 1:fc2f9d636751 | 13 | #include "bootloader_util.h" |
yihui | 1:fc2f9d636751 | 14 | #include <stdint.h> |
yihui | 1:fc2f9d636751 | 15 | #include <string.h> |
yihui | 1:fc2f9d636751 | 16 | |
yihui | 1:fc2f9d636751 | 17 | |
yihui | 1:fc2f9d636751 | 18 | /** |
yihui | 1:fc2f9d636751 | 19 | * @brief Function for aborting current handler mode and jump to to other application/bootloader. |
yihui | 1:fc2f9d636751 | 20 | * |
yihui | 1:fc2f9d636751 | 21 | * @details This functions will use the address provide (reset handler) to be executed after |
yihui | 1:fc2f9d636751 | 22 | * handler mode is exited. It creates an initial stack to ensure correct reset behavior |
yihui | 1:fc2f9d636751 | 23 | * when the reset handler is executed. |
yihui | 1:fc2f9d636751 | 24 | * See http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0552a/Babefdjc.html |
yihui | 1:fc2f9d636751 | 25 | * |
yihui | 1:fc2f9d636751 | 26 | * @param[in] reset_handler Address of the reset handler to be executed when handler mode exits. |
yihui | 1:fc2f9d636751 | 27 | * |
yihui | 1:fc2f9d636751 | 28 | * @note This function must never be called directly from 'C' but is intended only to be used from |
yihui | 1:fc2f9d636751 | 29 | * \ref bootloader_util_reset. This function will never return but issue a reset into |
yihui | 1:fc2f9d636751 | 30 | * provided address. |
yihui | 1:fc2f9d636751 | 31 | */ |
yihui | 1:fc2f9d636751 | 32 | |
yihui | 1:fc2f9d636751 | 33 | |
yihui | 1:fc2f9d636751 | 34 | /** |
yihui | 1:fc2f9d636751 | 35 | * @brief Function for aborting current application/bootloader jump to to other app/bootloader. |
yihui | 1:fc2f9d636751 | 36 | * |
yihui | 1:fc2f9d636751 | 37 | * @details This functions will use the address provide to swap the stack pointer and then load |
yihui | 1:fc2f9d636751 | 38 | * the address of the reset handler to be executed. It will check current system mode |
yihui | 1:fc2f9d636751 | 39 | * (thread/handler) and if in thread mode it will reset into other application. |
yihui | 1:fc2f9d636751 | 40 | * If in handler mode \ref isr_abort will be executed to ensure correct exit of handler |
yihui | 1:fc2f9d636751 | 41 | * mode and jump into reset handler of other application. |
yihui | 1:fc2f9d636751 | 42 | * |
yihui | 1:fc2f9d636751 | 43 | * @param[in] start_addr Start address of other application. This address must point to the |
yihui | 1:fc2f9d636751 | 44 | initial stack pointer of the application. |
yihui | 1:fc2f9d636751 | 45 | * |
yihui | 1:fc2f9d636751 | 46 | * @note This function will never return but issue a reset into provided application. |
yihui | 1:fc2f9d636751 | 47 | */ |
yihui | 1:fc2f9d636751 | 48 | #ifdef TOOLCHAIN_ARM |
yihui | 1:fc2f9d636751 | 49 | __asm static void bootloader_util_reset(uint32_t start_addr) |
yihui | 1:fc2f9d636751 | 50 | { |
yihui | 1:fc2f9d636751 | 51 | MASK_ONES EQU 0xFFFFFFFF ; Ones, to be loaded into register as default value before reset. |
yihui | 1:fc2f9d636751 | 52 | MASK_ZEROS EQU 0x00000000 ; Zeros, to be loaded into register as default value before reset. |
yihui | 1:fc2f9d636751 | 53 | xPSR_RESET EQU 0x21000000 ; Default value of xPSR after System Reset. |
yihui | 1:fc2f9d636751 | 54 | EXC_RETURN_CMD EQU 0xFFFFFFF9 ; EXC_RETURN for ARM Cortex. When loaded to PC the current interrupt service routine (handler mode) willl exit and the stack will be popped. Execution will continue in thread mode. |
yihui | 1:fc2f9d636751 | 55 | LDR R5, [R0] ; Get App initial MSP for bootloader. |
yihui | 1:fc2f9d636751 | 56 | MSR MSP, R5 ; Set the main stack pointer to the applications MSP. |
yihui | 1:fc2f9d636751 | 57 | LDR R6, [R0, #0x04] ; Load Reset handler into register 6. |
yihui | 1:fc2f9d636751 | 58 | |
yihui | 1:fc2f9d636751 | 59 | LDR R2, =MASK_ZEROS ; Load zeros to R2 |
yihui | 1:fc2f9d636751 | 60 | MRS R3, IPSR ; Load IPSR to R3 to check for handler or thread mode |
yihui | 1:fc2f9d636751 | 61 | CMP R2, R3 ; Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader |
yihui | 1:fc2f9d636751 | 62 | MOV R0, R6 |
yihui | 1:fc2f9d636751 | 63 | BNE isr_abort ; If not zero we need to exit current ISR and jump to reset handler of bootloader |
yihui | 1:fc2f9d636751 | 64 | |
yihui | 1:fc2f9d636751 | 65 | LDR R4, =MASK_ONES ; Load ones to R4 to be placed in Link Register. |
yihui | 1:fc2f9d636751 | 66 | MOV LR, R4 ; Clear the link register and set to ones to ensure no return. |
yihui | 1:fc2f9d636751 | 67 | BX R6 ; Branch to reset handler of bootloader |
yihui | 1:fc2f9d636751 | 68 | isr_abort |
yihui | 1:fc2f9d636751 | 69 | LDR R4,=MASK_ONES ; Fill with ones before jumping to reset handling. We be popped as R12 when exiting ISR (Cleaning up the registers). |
yihui | 1:fc2f9d636751 | 70 | LDR R5,=MASK_ONES ; Fill with ones before jumping to reset handling. We be popped as LR when exiting ISR. Ensures no return to application. |
yihui | 1:fc2f9d636751 | 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. |
yihui | 1:fc2f9d636751 | 72 | LDR R7,=xPSR_RESET ; Move reset value of xPSR to R7. Will be popped as xPSR when exiting ISR. |
yihui | 1:fc2f9d636751 | 73 | PUSH {r4-r7} ; Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR. |
yihui | 1:fc2f9d636751 | 74 | |
yihui | 1:fc2f9d636751 | 75 | LDR R4,=MASK_ZEROS ; Fill with zeros before jumping to reset handling. We be popped as R0 when exiting ISR (Cleaning up of the registers). |
yihui | 1:fc2f9d636751 | 76 | LDR R5,=MASK_ZEROS ; Fill with zeros before jumping to reset handling. We be popped as R1 when exiting ISR (Cleaning up of the registers). |
yihui | 1:fc2f9d636751 | 77 | LDR R6,=MASK_ZEROS ; Fill with zeros before jumping to reset handling. We be popped as R2 when exiting ISR (Cleaning up of the registers). |
yihui | 1:fc2f9d636751 | 78 | LDR R7,=MASK_ZEROS ; Fill with zeros before jumping to reset handling. We be popped as R3 when exiting ISR (Cleaning up of the registers). |
yihui | 1:fc2f9d636751 | 79 | PUSH {r4-r7} ; Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine. |
yihui | 1:fc2f9d636751 | 80 | |
yihui | 1:fc2f9d636751 | 81 | LDR R0,=EXC_RETURN_CMD ; Load the execution return command into register. |
yihui | 1:fc2f9d636751 | 82 | BX R0 ; No return - Handler mode will be exited. Stack will be popped and execution will continue in reset handler initializing other application. |
yihui | 1:fc2f9d636751 | 83 | ALIGN |
yihui | 1:fc2f9d636751 | 84 | } |
yihui | 1:fc2f9d636751 | 85 | #elif defined(TOOLCHAIN_GCC) |
yihui | 1:fc2f9d636751 | 86 | static void bootloader_util_reset(uint32_t start_addr) |
yihui | 1:fc2f9d636751 | 87 | { |
yihui | 1:fc2f9d636751 | 88 | asm( |
yihui | 1:fc2f9d636751 | 89 | ".equ MASK_ONES, 0xFFFFFFFF\n\t" /* Ones, to be loaded into register as default value before reset. */ |
yihui | 1:fc2f9d636751 | 90 | ".equ MASK_ZEROS, 0x00000000\n\t" /* Zeros, to be loaded into register as default value before reset. */ |
yihui | 1:fc2f9d636751 | 91 | |
yihui | 1:fc2f9d636751 | 92 | "LDR r5, [r0] \n\t" /* Get App initial MSP for bootloader. */ |
yihui | 1:fc2f9d636751 | 93 | "MSR MSP, r5 \n\t" /* Set the main stack pointer to the applications MSP. */ |
yihui | 1:fc2f9d636751 | 94 | "LDR r6,[r0, #0x04] \n\t" /* Load Reset handler into register 0. */ |
yihui | 1:fc2f9d636751 | 95 | |
yihui | 1:fc2f9d636751 | 96 | "LDR r2, =MASK_ZEROS\n\t" /* Load zeros to R2 */ |
yihui | 1:fc2f9d636751 | 97 | "MRS r3, IPSR \n\t" /* Load IPSR to R3 to check for handler or thread mode */ |
yihui | 1:fc2f9d636751 | 98 | "CMP r2, r3 \n\t" /* Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader */ |
yihui | 1:fc2f9d636751 | 99 | "MOV R0, R6 \n\t" |
yihui | 1:fc2f9d636751 | 100 | "BNE isr_abort \n\t" /* If not zero we need to exit current ISR and jump to reset handler of bootloader */ |
yihui | 1:fc2f9d636751 | 101 | |
yihui | 1:fc2f9d636751 | 102 | "LDR r4, =MASK_ONES \n\t" /* Load ones to R4 to be placed in Link Register. */ |
yihui | 1:fc2f9d636751 | 103 | "MOV LR, r4 \n\t" /* Clear the link register and set to ones to ensure no return. */ |
yihui | 1:fc2f9d636751 | 104 | "BX r6 \n\t" /* Branch to reset handler of bootloader */ |
yihui | 1:fc2f9d636751 | 105 | "isr_abort: \n\t" |
yihui | 1:fc2f9d636751 | 106 | "LDR R4,=MASK_ONES \n\t" /* Fill with ones before jumping to reset handling. We be popped as R12 when exiting ISR (Cleaning up the registers). */ |
yihui | 1:fc2f9d636751 | 107 | "LDR R5,=MASK_ONES \n\t" /* Fill with ones before jumping to reset handling. We be popped as LR when exiting ISR. Ensures no return to application. */ |
yihui | 1:fc2f9d636751 | 108 | "MOV R6, R0 \n\t" /* 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. */ |
yihui | 1:fc2f9d636751 | 109 | "LDR R7,=xPSR_RESET\n\t" /* Move reset value of xPSR to R7. Will be popped as xPSR when exiting ISR. */ |
yihui | 1:fc2f9d636751 | 110 | "PUSH {r4-r7} \n\t" /* Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR. */ |
yihui | 1:fc2f9d636751 | 111 | |
yihui | 1:fc2f9d636751 | 112 | "LDR R4,=MASK_ZEROS\n\t" /* Fill with zeros before jumping to reset handling. We be popped as R0 when exiting ISR (Cleaning up of the registers). */ |
yihui | 1:fc2f9d636751 | 113 | "LDR R5,=MASK_ZEROS\n\t" /* Fill with zeros before jumping to reset handling. We be popped as R1 when exiting ISR (Cleaning up of the registers). */ |
yihui | 1:fc2f9d636751 | 114 | "LDR R6,=MASK_ZEROS\n\t" /* Fill with zeros before jumping to reset handling. We be popped as R2 when exiting ISR (Cleaning up of the registers). */ |
yihui | 1:fc2f9d636751 | 115 | "LDR R7,=MASK_ZEROS\n\t" /* Fill with zeros before jumping to reset handling. We be popped as R3 when exiting ISR (Cleaning up of the registers). */ |
yihui | 1:fc2f9d636751 | 116 | "PUSH {r4-r7} \n\t" /* Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine. */ |
yihui | 1:fc2f9d636751 | 117 | |
yihui | 1:fc2f9d636751 | 118 | "LDR R0,=EXC_RETURN_CMD\n\t" /* Load the execution return command into register. */ |
yihui | 1:fc2f9d636751 | 119 | "BX R0 \n\t" /* No return - Handler mode will be exited. Stack will be popped and execution will continue in reset handler initializing other application. */ |
yihui | 1:fc2f9d636751 | 120 | : /* output operands */ |
yihui | 1:fc2f9d636751 | 121 | : /* input operands */ |
yihui | 1:fc2f9d636751 | 122 | : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" /* clobber list */ |
yihui | 1:fc2f9d636751 | 123 | ); |
yihui | 1:fc2f9d636751 | 124 | } |
yihui | 1:fc2f9d636751 | 125 | #endif /* TOOLCHAIN_ARM */ |
yihui | 1:fc2f9d636751 | 126 | |
yihui | 1:fc2f9d636751 | 127 | void bootloader_util_app_start(uint32_t start_addr) |
yihui | 1:fc2f9d636751 | 128 | { |
yihui | 1:fc2f9d636751 | 129 | bootloader_util_reset(start_addr); |
yihui | 1:fc2f9d636751 | 130 | } |