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/experimental/dfu_app_handler.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) 2014 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 <string.h> |
yihui | 1:fc2f9d636751 | 14 | #include "dfu_app_handler.h" |
yihui | 1:fc2f9d636751 | 15 | #include "bootloader_util.h" |
yihui | 1:fc2f9d636751 | 16 | #include "nrf.h" |
yihui | 1:fc2f9d636751 | 17 | #include "nrf_sdm.h" |
yihui | 1:fc2f9d636751 | 18 | #include "ble_gatts.h" |
yihui | 1:fc2f9d636751 | 19 | #include "app_error.h" |
yihui | 1:fc2f9d636751 | 20 | #include "dfu_ble_svc.h" |
yihui | 1:fc2f9d636751 | 21 | #include "device_manager.h" |
yihui | 1:fc2f9d636751 | 22 | |
yihui | 1:fc2f9d636751 | 23 | #define IRQ_ENABLED 0x01 /**< Field identifying if an interrupt is enabled. */ |
yihui | 1:fc2f9d636751 | 24 | #define MAX_NUMBER_INTERRUPTS 32 /**< Maximum number of interrupts available. */ |
yihui | 1:fc2f9d636751 | 25 | |
yihui | 1:fc2f9d636751 | 26 | static void dfu_app_reset_prepare(void); /**< Forward declare of default reset handler. */ |
yihui | 1:fc2f9d636751 | 27 | static dfu_app_reset_prepare_t m_reset_prepare = dfu_app_reset_prepare; /**< Callback function to application to prepare for system reset. Allows application to cleanup of service and memory prior to reset. */ |
yihui | 1:fc2f9d636751 | 28 | static dfu_ble_peer_data_t m_peer_data; /**< Peer data to be used for data exchange when reseting into DFU mode. */ |
yihui | 1:fc2f9d636751 | 29 | static dm_handle_t m_dm_handle; /**< Device Manager handle with instance id's of current BLE connection. */ |
yihui | 1:fc2f9d636751 | 30 | static bool m_dm_handle_valid = false; /**< Variable indicating if the Device Manager handle is valid. */ |
yihui | 1:fc2f9d636751 | 31 | |
yihui | 1:fc2f9d636751 | 32 | |
yihui | 1:fc2f9d636751 | 33 | /**@brief Default reset prepare handler if application hasn't registered a handler. |
yihui | 1:fc2f9d636751 | 34 | */ |
yihui | 1:fc2f9d636751 | 35 | static void dfu_app_reset_prepare(void) |
yihui | 1:fc2f9d636751 | 36 | { |
yihui | 1:fc2f9d636751 | 37 | // Reset prepare should be handled by application. |
yihui | 1:fc2f9d636751 | 38 | // This function can be extended to include default handling if application does not implement |
yihui | 1:fc2f9d636751 | 39 | // own handler. |
yihui | 1:fc2f9d636751 | 40 | } |
yihui | 1:fc2f9d636751 | 41 | |
yihui | 1:fc2f9d636751 | 42 | |
yihui | 1:fc2f9d636751 | 43 | /**@brief Function for disabling all interrupts before jumping from bootloader to application. |
yihui | 1:fc2f9d636751 | 44 | */ |
yihui | 1:fc2f9d636751 | 45 | static void interrupts_disable(void) |
yihui | 1:fc2f9d636751 | 46 | { |
yihui | 1:fc2f9d636751 | 47 | uint32_t interrupt_setting_mask; |
yihui | 1:fc2f9d636751 | 48 | uint32_t irq = 0; // We start from first interrupt, i.e. interrupt 0. |
yihui | 1:fc2f9d636751 | 49 | |
yihui | 1:fc2f9d636751 | 50 | // Fetch the current interrupt settings. |
yihui | 1:fc2f9d636751 | 51 | interrupt_setting_mask = NVIC->ISER[0]; |
yihui | 1:fc2f9d636751 | 52 | |
yihui | 1:fc2f9d636751 | 53 | for (; irq < MAX_NUMBER_INTERRUPTS; irq++) |
yihui | 1:fc2f9d636751 | 54 | { |
yihui | 1:fc2f9d636751 | 55 | if (interrupt_setting_mask & (IRQ_ENABLED << irq)) |
yihui | 1:fc2f9d636751 | 56 | { |
yihui | 1:fc2f9d636751 | 57 | // The interrupt was enabled, and hence disable it. |
yihui | 1:fc2f9d636751 | 58 | NVIC_DisableIRQ((IRQn_Type)irq); |
yihui | 1:fc2f9d636751 | 59 | } |
yihui | 1:fc2f9d636751 | 60 | } |
yihui | 1:fc2f9d636751 | 61 | } |
yihui | 1:fc2f9d636751 | 62 | |
yihui | 1:fc2f9d636751 | 63 | |
yihui | 1:fc2f9d636751 | 64 | /**@brief Function for providing peer information to DFU for re-establishing bonded connection in |
yihui | 1:fc2f9d636751 | 65 | * DFU mode. |
yihui | 1:fc2f9d636751 | 66 | */ |
yihui | 1:fc2f9d636751 | 67 | static void dfu_app_set_peer_data(void) |
yihui | 1:fc2f9d636751 | 68 | { |
yihui | 1:fc2f9d636751 | 69 | uint32_t err_code; |
yihui | 1:fc2f9d636751 | 70 | dm_sec_keyset_t key_set; |
yihui | 1:fc2f9d636751 | 71 | |
yihui | 1:fc2f9d636751 | 72 | /** [DFU bond sharing] */ |
yihui | 1:fc2f9d636751 | 73 | err_code = dm_distributed_keys_get(&m_dm_handle, &key_set); |
yihui | 1:fc2f9d636751 | 74 | APP_ERROR_CHECK(err_code); |
yihui | 1:fc2f9d636751 | 75 | |
yihui | 1:fc2f9d636751 | 76 | m_peer_data.addr = key_set.keys_central.p_id_key->id_addr_info; |
yihui | 1:fc2f9d636751 | 77 | m_peer_data.enc_info = *key_set.keys_central.enc_key.p_enc_info; |
yihui | 1:fc2f9d636751 | 78 | m_peer_data.irk = key_set.keys_central.p_id_key->id_info; |
yihui | 1:fc2f9d636751 | 79 | |
yihui | 1:fc2f9d636751 | 80 | err_code = dfu_ble_svc_set_peer_data(&m_peer_data); |
yihui | 1:fc2f9d636751 | 81 | APP_ERROR_CHECK(err_code); |
yihui | 1:fc2f9d636751 | 82 | /** [DFU bond sharing] */ |
yihui | 1:fc2f9d636751 | 83 | } |
yihui | 1:fc2f9d636751 | 84 | |
yihui | 1:fc2f9d636751 | 85 | |
yihui | 1:fc2f9d636751 | 86 | /**@brief Function for preparing the reset, disabling SoftDevice and jump to the bootloader. |
yihui | 1:fc2f9d636751 | 87 | */ |
yihui | 1:fc2f9d636751 | 88 | void bootloader_start(void) |
yihui | 1:fc2f9d636751 | 89 | { |
yihui | 1:fc2f9d636751 | 90 | m_reset_prepare(); |
yihui | 1:fc2f9d636751 | 91 | |
yihui | 1:fc2f9d636751 | 92 | uint32_t err_code = sd_power_gpregret_set(BOOTLOADER_DFU_START); |
yihui | 1:fc2f9d636751 | 93 | APP_ERROR_CHECK(err_code); |
yihui | 1:fc2f9d636751 | 94 | |
yihui | 1:fc2f9d636751 | 95 | err_code = sd_softdevice_disable(); |
yihui | 1:fc2f9d636751 | 96 | APP_ERROR_CHECK(err_code); |
yihui | 1:fc2f9d636751 | 97 | |
yihui | 1:fc2f9d636751 | 98 | err_code = sd_softdevice_vector_table_base_set(NRF_UICR->BOOTLOADERADDR); |
yihui | 1:fc2f9d636751 | 99 | APP_ERROR_CHECK(err_code); |
yihui | 1:fc2f9d636751 | 100 | |
yihui | 1:fc2f9d636751 | 101 | // Commenting out the following block because it brings in unwanted dependencies from bonding. |
yihui | 1:fc2f9d636751 | 102 | // TODO: discuss this with Nordic. |
yihui | 1:fc2f9d636751 | 103 | // if (m_dm_handle_valid) |
yihui | 1:fc2f9d636751 | 104 | // { |
yihui | 1:fc2f9d636751 | 105 | // dfu_app_set_peer_data(); |
yihui | 1:fc2f9d636751 | 106 | // } |
yihui | 1:fc2f9d636751 | 107 | |
yihui | 1:fc2f9d636751 | 108 | NVIC_ClearPendingIRQ(SWI2_IRQn); |
yihui | 1:fc2f9d636751 | 109 | interrupts_disable(); |
yihui | 1:fc2f9d636751 | 110 | bootloader_util_app_start(NRF_UICR->BOOTLOADERADDR); |
yihui | 1:fc2f9d636751 | 111 | } |
yihui | 1:fc2f9d636751 | 112 | |
yihui | 1:fc2f9d636751 | 113 | |
yihui | 1:fc2f9d636751 | 114 | void dfu_app_on_dfu_evt(ble_dfu_t * p_dfu, ble_dfu_evt_t * p_evt) |
yihui | 1:fc2f9d636751 | 115 | { |
yihui | 1:fc2f9d636751 | 116 | switch (p_evt->ble_dfu_evt_type) |
yihui | 1:fc2f9d636751 | 117 | { |
yihui | 1:fc2f9d636751 | 118 | case BLE_DFU_START: |
yihui | 1:fc2f9d636751 | 119 | // Starting the bootloader - will cause reset. |
yihui | 1:fc2f9d636751 | 120 | bootloader_start(); |
yihui | 1:fc2f9d636751 | 121 | break; |
yihui | 1:fc2f9d636751 | 122 | |
yihui | 1:fc2f9d636751 | 123 | case BLE_DFU_VALIDATE: |
yihui | 1:fc2f9d636751 | 124 | case BLE_DFU_ACTIVATE_N_RESET: |
yihui | 1:fc2f9d636751 | 125 | case BLE_DFU_SYS_RESET: |
yihui | 1:fc2f9d636751 | 126 | case BLE_DFU_RECEIVE_INIT_DATA: |
yihui | 1:fc2f9d636751 | 127 | case BLE_DFU_RECEIVE_APP_DATA: |
yihui | 1:fc2f9d636751 | 128 | case BLE_DFU_PACKET_WRITE: |
yihui | 1:fc2f9d636751 | 129 | case BLE_DFU_PKT_RCPT_NOTIF_ENABLED: |
yihui | 1:fc2f9d636751 | 130 | case BLE_DFU_PKT_RCPT_NOTIF_DISABLED: |
yihui | 1:fc2f9d636751 | 131 | case BLE_DFU_BYTES_RECEIVED_SEND: |
yihui | 1:fc2f9d636751 | 132 | default: |
yihui | 1:fc2f9d636751 | 133 | { |
yihui | 1:fc2f9d636751 | 134 | // Unsupported event received from DFU Service. |
yihui | 1:fc2f9d636751 | 135 | // Send back BLE_DFU_RESP_VAL_NOT_SUPPORTED message to peer. |
yihui | 1:fc2f9d636751 | 136 | uint32_t err_code = ble_dfu_response_send(p_dfu, |
yihui | 1:fc2f9d636751 | 137 | BLE_DFU_START_PROCEDURE, |
yihui | 1:fc2f9d636751 | 138 | BLE_DFU_RESP_VAL_NOT_SUPPORTED); |
yihui | 1:fc2f9d636751 | 139 | APP_ERROR_CHECK(err_code); |
yihui | 1:fc2f9d636751 | 140 | } |
yihui | 1:fc2f9d636751 | 141 | break; |
yihui | 1:fc2f9d636751 | 142 | } |
yihui | 1:fc2f9d636751 | 143 | } |
yihui | 1:fc2f9d636751 | 144 | |
yihui | 1:fc2f9d636751 | 145 | |
yihui | 1:fc2f9d636751 | 146 | void dfu_app_reset_prepare_set(dfu_app_reset_prepare_t reset_prepare_func) |
yihui | 1:fc2f9d636751 | 147 | { |
yihui | 1:fc2f9d636751 | 148 | m_reset_prepare = reset_prepare_func; |
yihui | 1:fc2f9d636751 | 149 | } |
yihui | 1:fc2f9d636751 | 150 | |
yihui | 1:fc2f9d636751 | 151 | |
yihui | 1:fc2f9d636751 | 152 | void dfu_app_set_dm_handle(dm_handle_t const * p_dm_handle) |
yihui | 1:fc2f9d636751 | 153 | { |
yihui | 1:fc2f9d636751 | 154 | m_dm_handle_valid = false; |
yihui | 1:fc2f9d636751 | 155 | |
yihui | 1:fc2f9d636751 | 156 | if (p_dm_handle != NULL) |
yihui | 1:fc2f9d636751 | 157 | { |
yihui | 1:fc2f9d636751 | 158 | m_dm_handle = *p_dm_handle; |
yihui | 1:fc2f9d636751 | 159 | m_dm_handle_valid = true; |
yihui | 1:fc2f9d636751 | 160 | } |
yihui | 1:fc2f9d636751 | 161 | } |
yihui | 1:fc2f9d636751 | 162 |