Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <string.h>
- #include <stddef.h>
- #include "ble.h"
- #include "nrf_sdm.h"
- #include "nrf_mbr.h"
- #include "nrf_log.h"
- #include "nrf_delay.h"
- #include "nrf_drv_uart.h"
- #include "app_error.h"
- #include "app_uart.h"
- #include "softdevice_handler_appsh.h"
- #define IS_SRVC_CHANGED_CHARACT_PRESENT 1
- #define NRF_UICR_BASE 0x10001000UL
- #define NRF_UICR_BOOT_START_ADDRESS (NRF_UICR_BASE + 0x14)
- #define BOOTLOADER_REGION_START 0x0003C000
- #define IRQ_ENABLED 0x01 /**< Field identifying if an interrupt is enabled. */
- #define MAX_NUMBER_INTERRUPTS 32 /**< Maximum number of interrupts available. */
- #define MBR_SIZE (0x1000)
- #define SOFTDEVICE_INFO_STRUCT_OFFSET (0x2000)
- #define SD_SIZE_OFFSET (SOFTDEVICE_INFO_STRUCT_OFFSET + 0x08)
- #define SD_SIZE_GET(baseaddr) (*((uint32_t *) ((baseaddr) + SD_SIZE_OFFSET)))
- #define CODE_REGION_1_START SD_SIZE_GET(MBR_SIZE)
- #define DFU_BANK_0_REGION_START CODE_REGION_1_START
- uint32_t m_uicr_bootloader_start_address __attribute__((at(NRF_UICR_BOOT_START_ADDRESS))) = BOOTLOADER_REGION_START;
- void uart_puts(char* s);
- static void interrupts_disable(void)
- {
- uint32_t interrupt_setting_mask;
- uint32_t irq = 0; // We start from first interrupt, i.e. interrupt 0.
- // Fetch the current interrupt settings.
- interrupt_setting_mask = NVIC->ISER[0];
- for (; irq < MAX_NUMBER_INTERRUPTS; irq++)
- {
- if (interrupt_setting_mask & (IRQ_ENABLED << irq))
- {
- // The interrupt was enabled, and hence disable it.
- NVIC_DisableIRQ((IRQn_Type)irq);
- }
- }
- }
- /**
- * @brief Function for aborting current application/bootloader jump to to other app/bootloader.
- *
- * @details This functions will use the address provide to swap the stack pointer and then load
- * the address of the reset handler to be executed. It will check current system mode
- * (thread/handler) and if in thread mode it will reset into other application.
- * If in handler mode \ref isr_abort will be executed to ensure correct exit of handler
- * mode and jump into reset handler of other application.
- *
- * @param[in] start_addr Start address of other application. This address must point to the
- initial stack pointer of the application.
- *
- * @note This function will never return but issue a reset into provided application.
- */
- #if defined ( __CC_ARM )
- __asm static void bootloader_util_reset(uint32_t start_addr)
- {
- LDR R5, [R0] ; Get App initial MSP for bootloader.
- MSR MSP, R5 ; Set the main stack pointer to the applications MSP.
- LDR R0, [R0, #0x04] ; Load Reset handler into R0. This will be first argument to branch instruction (BX).
- MOVS R4, #0xFF ; Load ones to R4.
- SXTB R4, R4 ; Sign extend R4 to obtain 0xFFFFFFFF instead of 0xFF.
- MRS R5, IPSR ; Load IPSR to R5 to check for handler or thread mode.
- CMP R5, #0x00 ; Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader.
- BNE isr_abort ; If not zero we need to exit current ISR and jump to reset handler of bootloader.
- MOV LR, R4 ; Clear the link register and set to ones to ensure no return, R4 = 0xFFFFFFFF.
- BX R0 ; Branch to reset handler of bootloader.
- isr_abort
- ; R4 contains ones from line above. Will be popped as R12 when exiting ISR (Cleaning up the registers).
- MOV R5, R4 ; Fill with ones before jumping to reset handling. We be popped as LR when exiting ISR. Ensures no return to application.
- 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.
- 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.
- REV r7, r7 ; Reverse byte order to put 0x21 as MSB.
- PUSH {r4-r7} ; Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
- MOVS R4, #0x00 ; Fill with zeros before jumping to reset handling. We be popped as R0 when exiting ISR (Cleaning up of the registers).
- MOVS R5, #0x00 ; Fill with zeros before jumping to reset handling. We be popped as R1 when exiting ISR (Cleaning up of the registers).
- MOVS R6, #0x00 ; Fill with zeros before jumping to reset handling. We be popped as R2 when exiting ISR (Cleaning up of the registers).
- MOVS R7, #0x00 ; Fill with zeros before jumping to reset handling. We be popped as R3 when exiting ISR (Cleaning up of the registers).
- PUSH {r4-r7} ; Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
- MOVS R0, #0xF9 ; Move the execution return command into register, 0xFFFFFFF9.
- SXTB R0, R0 ; Sign extend R0 to obtain 0xFFFFFFF9 instead of 0xF9.
- BX R0 ; No return - Handler mode will be exited. Stack will be popped and execution will continue in reset handler initializing other application.
- ALIGN
- }
- #elif defined ( __GNUC__ )
- static inline void bootloader_util_reset(uint32_t start_addr)
- {
- __asm volatile(
- "ldr r0, [%0]\t\n" // Get App initial MSP for bootloader.
- "msr msp, r0\t\n" // Set the main stack pointer to the applications MSP.
- "ldr r0, [%0, #0x04]\t\n" // Load Reset handler into R0.
- "movs r4, #0xFF\t\n" // Move ones to R4.
- "sxtb r4, r4\t\n" // Sign extend R4 to obtain 0xFFFFFFFF instead of 0xFF.
- "mrs r5, IPSR\t\n" // Load IPSR to R5 to check for handler or thread mode.
- "cmp r5, #0x00\t\n" // Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader.
- "bne isr_abort\t\n" // If not zero we need to exit current ISR and jump to reset handler of bootloader.
- "mov lr, r4\t\n" // Clear the link register and set to ones to ensure no return.
- "bx r0\t\n" // Branch to reset handler of bootloader.
- "isr_abort: \t\n"
- "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.
- "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.
- "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.
- "rev r7, r7\t\n" // Reverse byte order to put 0x21 as MSB.
- "push {r4-r7}\t\n" // Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
- "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).
- "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).
- "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).
- "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).
- "push {r4-r7}\t\n" // Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
- "movs r0, #0xF9\t\n" // Move the execution return command into register, 0xFFFFFFF9.
- "sxtb r0, r0\t\n" // Sign extend R0 to obtain 0xFFFFFFF9 instead of 0xF9.
- "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.
- ".align\t\n"
- :: "r" (start_addr) // Argument list for the gcc assembly. start_addr is %0.
- : "r0", "r4", "r5", "r6", "r7" // List of register maintained manually.
- );
- }
- #elif defined ( __ICCARM__ )
- static inline void bootloader_util_reset(uint32_t start_addr)
- {
- asm("ldr r5, [%0]\n" // Get App initial MSP for bootloader.
- "msr msp, r5\n" // Set the main stack pointer to the applications MSP.
- "ldr r0, [%0, #0x04]\n" // Load Reset handler into R0.
- "movs r4, #0x00\n" // Load zero into R4.
- "mvns r4, r4\n" // Invert R4 to ensure it contain ones.
- "mrs r5, IPSR\n" // Load IPSR to R5 to check for handler or thread mode
- "cmp r5, #0x00\n" // Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader.
- "bne.n isr_abort\n" // If not zero we need to exit current ISR and jump to reset handler of bootloader.
- "mov lr, r4\n" // Clear the link register and set to ones to ensure no return.
- "bx r0\n" // Branch to reset handler of bootloader.
- "isr_abort: \n"
- // R4 contains ones from line above. We be popped as R12 when exiting ISR (Cleaning up the registers).
- "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.
- "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.
- "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.
- "rev r7, r7\n" // Reverse byte order to put 0x21 as MSB.
- "push {r4-r7}\n" // Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
- "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).
- "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).
- "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).
- "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).
- "push {r4-r7}\n" // Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
- "movs r0, #0x06\n" // Load 0x06 into R6 to prepare for exec return command.
- "mvns r0, r0\n" // Invert 0x06 to obtain EXEC_RETURN, 0xFFFFFFF9.
- "bx r0\n" // No return - Handler mode will be exited. Stack will be popped and execution will continue in reset handler initializing other application.
- :: "r" (start_addr) // Argument list for the IAR assembly. start_addr is %0.
- : "r0", "r4", "r5", "r6", "r7"); // List of register maintained manually.
- }
- #else
- #error Compiler not supported.
- #endif
- void bootloader_util_app_start(uint32_t start_addr)
- {
- bootloader_util_reset(start_addr);
- }
- void bootloader_app_start(uint32_t app_addr)
- {
- // If the applications CRC has been checked and passed, the magic number will be written and we
- // can start the application safely.
- uint32_t err_code = sd_softdevice_disable();
- APP_ERROR_CHECK(err_code);
- interrupts_disable();
- err_code = sd_softdevice_vector_table_base_set(CODE_REGION_1_START);
- APP_ERROR_CHECK(err_code);
- bootloader_util_app_start(CODE_REGION_1_START);
- }
- void uart_evt_callback(app_uart_evt_t * uart_evt)
- {
- uint8_t ch;
- switch (uart_evt->evt_type)
- {
- case APP_UART_DATA:
- //Data is ready on the UART
- break;
- case APP_UART_DATA_READY:
- //Data is ready on the UART FIFO
- app_uart_get(&ch);
- //app_uart_put(ch); // echo
- if(ch == 'h')
- {
- uart_puts("\
- \n\
- help:\n\
- h - this help.\n\
- s - start app at bank0.\n");
- } else if(ch == 's') {
- bootloader_app_start(DFU_BANK_0_REGION_START);
- NVIC_SystemReset();
- }
- break;
- case APP_UART_TX_EMPTY:
- //Data has been successfully transmitted on the UART
- break;
- default:
- break;
- }
- }
- void uart_init(void)
- {
- uint32_t err_code;
- const app_uart_comm_params_t comm_params =
- {
- 11, // RX_PIN_NUMBER,
- 9, //TX_PIN_NUMBER,
- 10, //RTS_PIN_NUMBER,
- 8, //CTS_PIN_NUMBER,
- APP_UART_FLOW_CONTROL_DISABLED,
- false,
- UART_BAUDRATE_BAUDRATE_Baud115200
- };
- APP_UART_FIFO_INIT(&comm_params,
- 1, // RX buf
- 256, // TX buf
- uart_evt_callback,
- APP_IRQ_PRIORITY_HIGH,
- err_code);
- APP_ERROR_CHECK(err_code);
- }
- void uart_puts(char* s)
- {
- while(*s){
- app_uart_put(*s++);
- nrf_delay_ms(1);
- }
- }
- static void sys_evt_dispatch(uint32_t event)
- {
- //pstorage_sys_event_handler(event);
- }
- /**@brief Function for initializing the BLE stack.
- *
- * @details Initializes the SoftDevice and the BLE event interrupt.
- *
- * @param[in] init_softdevice true if SoftDevice should be initialized. The SoftDevice must only
- * be initialized if a chip reset has occured. Soft reset from
- * application must not reinitialize the SoftDevice.
- */
- static void ble_stack_init(bool init_softdevice)
- {
- uint32_t err_code;
- sd_mbr_command_t com = {SD_MBR_COMMAND_INIT_SD, };
- if (init_softdevice)
- {
- err_code = sd_mbr_command(&com);
- APP_ERROR_CHECK(err_code);
- }
- err_code = sd_softdevice_vector_table_base_set(BOOTLOADER_REGION_START);
- APP_ERROR_CHECK(err_code);
- SOFTDEVICE_HANDLER_APPSH_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, true);
- // Enable BLE stack.
- ble_enable_params_t ble_enable_params;
- // Only one connection as a central is used when performing dfu.
- err_code = softdevice_enable_get_default_config(1, 1, &ble_enable_params);
- APP_ERROR_CHECK(err_code);
- ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
- err_code = softdevice_enable(&ble_enable_params);
- APP_ERROR_CHECK(err_code);
- err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
- APP_ERROR_CHECK(err_code);
- }
- int main(void)
- {
- uart_init();
- ble_stack_init(true);
- while(1)
- {
- __WFI();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement