Maiko Matsumoto / Mbed 2 deprecated BLE_WallbotBLE_Challenge_byYUTAKA3

Dependencies:   mbed

Fork of BLE_WallbotBLE_Challenge_byYUTAKA by Yutaka Yoshida

Committer:
mmmmmmmmma
Date:
Mon Jun 18 08:43:33 2018 +0000
Revision:
12:252acb9c1201
Parent:
0:76dfa9657d9d
6/18 ?????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:76dfa9657d9d 1 /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
jksoft 0:76dfa9657d9d 2 *
jksoft 0:76dfa9657d9d 3 * The information contained herein is property of Nordic Semiconductor ASA.
jksoft 0:76dfa9657d9d 4 * Terms and conditions of usage are described in detail in NORDIC
jksoft 0:76dfa9657d9d 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
jksoft 0:76dfa9657d9d 6 *
jksoft 0:76dfa9657d9d 7 * Licensees are granted free, non-transferable use of the information. NO
jksoft 0:76dfa9657d9d 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
jksoft 0:76dfa9657d9d 9 * the file.
jksoft 0:76dfa9657d9d 10 *
jksoft 0:76dfa9657d9d 11 */
jksoft 0:76dfa9657d9d 12
jksoft 0:76dfa9657d9d 13 #include "bootloader_util.h"
jksoft 0:76dfa9657d9d 14 #include <stdint.h>
jksoft 0:76dfa9657d9d 15
jksoft 0:76dfa9657d9d 16
jksoft 0:76dfa9657d9d 17 /**
jksoft 0:76dfa9657d9d 18 * @brief Function for aborting current handler mode and jump to to other application/bootloader.
jksoft 0:76dfa9657d9d 19 *
jksoft 0:76dfa9657d9d 20 * @details This functions will use the address provide (reset handler) to be executed after
jksoft 0:76dfa9657d9d 21 * handler mode is exited. It creates an initial stack to ensure correct reset behavior
jksoft 0:76dfa9657d9d 22 * when the reset handler is executed.
jksoft 0:76dfa9657d9d 23 *
jksoft 0:76dfa9657d9d 24 * @param[in] reset_handler Address of the reset handler to be executed when handler mode exits.
jksoft 0:76dfa9657d9d 25 *
jksoft 0:76dfa9657d9d 26 * @note This function must never be called directly from 'C' but is intended only to be used from
jksoft 0:76dfa9657d9d 27 * \ref bootloader_util_reset. This function will never return but issue a reset into
jksoft 0:76dfa9657d9d 28 * provided address.
jksoft 0:76dfa9657d9d 29 */
jksoft 0:76dfa9657d9d 30 __asm void isr_abort(uint32_t reset_handler)
jksoft 0:76dfa9657d9d 31 {
jksoft 0:76dfa9657d9d 32 xPSR_RESET EQU 0x21000000 ; Default value of xPSR after System Reset.
jksoft 0:76dfa9657d9d 33 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.
jksoft 0:76dfa9657d9d 34
jksoft 0:76dfa9657d9d 35 LDR R4,=MASK_ONES ; Fill with ones before jumping to reset handling. We be popped as R12 when exiting ISR (Cleaning up the registers).
jksoft 0:76dfa9657d9d 36 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.
jksoft 0:76dfa9657d9d 37 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.
jksoft 0:76dfa9657d9d 38 LDR R7,=xPSR_RESET ; Move reset value of xPSR to R7. Will be popped as xPSR when exiting ISR.
jksoft 0:76dfa9657d9d 39 PUSH {r4-r7} ; Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
jksoft 0:76dfa9657d9d 40
jksoft 0:76dfa9657d9d 41 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).
jksoft 0:76dfa9657d9d 42 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).
jksoft 0:76dfa9657d9d 43 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).
jksoft 0:76dfa9657d9d 44 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).
jksoft 0:76dfa9657d9d 45 PUSH {r4-r7} ; Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
jksoft 0:76dfa9657d9d 46
jksoft 0:76dfa9657d9d 47 LDR R0,=EXC_RETURN_CMD ; Load the execution return command into register.
jksoft 0:76dfa9657d9d 48 BX R0 ; No return - Handler mode will be exited. Stack will be popped and execution will continue in reset handler initializing other application.
jksoft 0:76dfa9657d9d 49 ALIGN
jksoft 0:76dfa9657d9d 50 }
jksoft 0:76dfa9657d9d 51
jksoft 0:76dfa9657d9d 52
jksoft 0:76dfa9657d9d 53 /**
jksoft 0:76dfa9657d9d 54 * @brief Function for aborting current application/bootloader jump to to other app/bootloader.
jksoft 0:76dfa9657d9d 55 *
jksoft 0:76dfa9657d9d 56 * @details This functions will use the address provide to swap the stack pointer and then load
jksoft 0:76dfa9657d9d 57 * the address of the reset handler to be executed. It will check current system mode
jksoft 0:76dfa9657d9d 58 * (thread/handler) and if in thread mode it will reset into other application.
jksoft 0:76dfa9657d9d 59 * If in handler mode \ref isr_abort will be executed to ensure correct exit of handler
jksoft 0:76dfa9657d9d 60 * mode and jump into reset handler of other application.
jksoft 0:76dfa9657d9d 61 *
jksoft 0:76dfa9657d9d 62 * @param[in] start_addr Start address of other application. This address must point to the
jksoft 0:76dfa9657d9d 63 initial stack pointer of the application.
jksoft 0:76dfa9657d9d 64 *
jksoft 0:76dfa9657d9d 65 * @note This function will never return but issue a reset into provided application.
jksoft 0:76dfa9657d9d 66 */
jksoft 0:76dfa9657d9d 67 __asm static void bootloader_util_reset(uint32_t start_addr)
jksoft 0:76dfa9657d9d 68 {
jksoft 0:76dfa9657d9d 69 MASK_ONES EQU 0xFFFFFFFF ; Ones, to be loaded into register as default value before reset.
jksoft 0:76dfa9657d9d 70 MASK_ZEROS EQU 0x00000000 ; Zeros, to be loaded into register as default value before reset.
jksoft 0:76dfa9657d9d 71
jksoft 0:76dfa9657d9d 72 LDR R1, [R0] ; Get App initial MSP for bootloader.
jksoft 0:76dfa9657d9d 73 MSR MSP, R1 ; Set the main stack pointer to the applications MSP.
jksoft 0:76dfa9657d9d 74 LDR R0,[R0, #0x04] ; Load Reset handler into register 0.
jksoft 0:76dfa9657d9d 75
jksoft 0:76dfa9657d9d 76 LDR R2, =MASK_ZEROS ; Load zeros to R2
jksoft 0:76dfa9657d9d 77 MRS R3, IPSR ; Load IPSR to R3 to check for handler or thread mode
jksoft 0:76dfa9657d9d 78 CMP R2, R3 ; Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader
jksoft 0:76dfa9657d9d 79 BNE isr_abort ; If not zero we need to exit current ISR and jump to reset handler of bootloader
jksoft 0:76dfa9657d9d 80
jksoft 0:76dfa9657d9d 81 LDR R4, =MASK_ONES ; Load ones to R4 to be placed in Link Register.
jksoft 0:76dfa9657d9d 82 MOV LR, R4 ; Clear the link register and set to ones to ensure no return.
jksoft 0:76dfa9657d9d 83 BX R0 ; Branch to reset handler of bootloader
jksoft 0:76dfa9657d9d 84 ALIGN
jksoft 0:76dfa9657d9d 85 }
jksoft 0:76dfa9657d9d 86
jksoft 0:76dfa9657d9d 87
jksoft 0:76dfa9657d9d 88 void bootloader_util_app_start(uint32_t start_addr)
jksoft 0:76dfa9657d9d 89 {
jksoft 0:76dfa9657d9d 90 bootloader_util_reset(start_addr);
jksoft 0:76dfa9657d9d 91 }