BLE FOTA APP

Dependencies:   BLE_API mbed

It doesn't work with the default FOTA bootloader. It use NVIC_SystemReset() to enter a bootloader.

Committer:
yihui
Date:
Fri Oct 10 03:36:28 2014 +0000
Revision:
1:a607cd9655d7
use NVIC_SystemReset() to run bootloader

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 1:a607cd9655d7 1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
yihui 1:a607cd9655d7 2 *
yihui 1:a607cd9655d7 3 * The information contained herein is property of Nordic Semiconductor ASA.
yihui 1:a607cd9655d7 4 * Terms and conditions of usage are described in detail in NORDIC
yihui 1:a607cd9655d7 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
yihui 1:a607cd9655d7 6 *
yihui 1:a607cd9655d7 7 * Licensees are granted free, non-transferable use of the information. NO
yihui 1:a607cd9655d7 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
yihui 1:a607cd9655d7 9 * the file.
yihui 1:a607cd9655d7 10 *
yihui 1:a607cd9655d7 11 */
yihui 1:a607cd9655d7 12
yihui 1:a607cd9655d7 13 /** @file
yihui 1:a607cd9655d7 14 *
yihui 1:a607cd9655d7 15 * @defgroup ble_flash_module Flash Manager
yihui 1:a607cd9655d7 16 * @{
yihui 1:a607cd9655d7 17 * @ingroup ble_sdk_lib
yihui 1:a607cd9655d7 18 * @brief Module for accessing flash memory.
yihui 1:a607cd9655d7 19 *
yihui 1:a607cd9655d7 20 * @details It contains functions for reading, writing and erasing one page in flash.
yihui 1:a607cd9655d7 21 *
yihui 1:a607cd9655d7 22 * The module uses the first 32 bits of the flash page to write a magic number in order to
yihui 1:a607cd9655d7 23 * determine if the page has been written or not.
yihui 1:a607cd9655d7 24 *
yihui 1:a607cd9655d7 25 * @note Be careful not to use a page number in the SoftDevice area (which currently occupies the
yihui 1:a607cd9655d7 26 * range 0 to 127), or in your application space! In both cases, this would end up
yihui 1:a607cd9655d7 27 * with a hard fault.
yihui 1:a607cd9655d7 28 */
yihui 1:a607cd9655d7 29
yihui 1:a607cd9655d7 30 #ifndef BLE_FLASH_H__
yihui 1:a607cd9655d7 31 #define BLE_FLASH_H__
yihui 1:a607cd9655d7 32
yihui 1:a607cd9655d7 33 #include <stdint.h>
yihui 1:a607cd9655d7 34 #include <stdbool.h>
yihui 1:a607cd9655d7 35 #include <nrf51.h>
yihui 1:a607cd9655d7 36
yihui 1:a607cd9655d7 37 #define BLE_FLASH_PAGE_SIZE ((uint16_t)NRF_FICR->CODEPAGESIZE) /**< Size of one flash page. */
yihui 1:a607cd9655d7 38 #define BLE_FLASH_MAGIC_NUMBER 0x45DE0000 /**< Magic value to identify if flash contains valid data. */
yihui 1:a607cd9655d7 39 #define BLE_FLASH_EMPTY_MASK 0xFFFFFFFF /**< Bit mask that defines an empty address in flash. */
yihui 1:a607cd9655d7 40
yihui 1:a607cd9655d7 41
yihui 1:a607cd9655d7 42 /**@brief Macro for getting the end of the flash available for application.
yihui 1:a607cd9655d7 43 *
yihui 1:a607cd9655d7 44 * @details The result flash page number indicates the end boundary of the flash available
yihui 1:a607cd9655d7 45 * to the application. If a bootloader is used, the end will be the start of the
yihui 1:a607cd9655d7 46 * bootloader region. Otherwise, the end will be the size of the flash.
yihui 1:a607cd9655d7 47 */
yihui 1:a607cd9655d7 48 #define BLE_FLASH_PAGE_END \
yihui 1:a607cd9655d7 49 ((NRF_UICR->BOOTLOADERADDR != BLE_FLASH_EMPTY_MASK) \
yihui 1:a607cd9655d7 50 ? (NRF_UICR->BOOTLOADERADDR / BLE_FLASH_PAGE_SIZE) \
yihui 1:a607cd9655d7 51 : NRF_FICR->CODESIZE)
yihui 1:a607cd9655d7 52
yihui 1:a607cd9655d7 53 /**@brief Function for erasing the specified flash page, and then writes the given data to this page.
yihui 1:a607cd9655d7 54 *
yihui 1:a607cd9655d7 55 * @warning This operation blocks the CPU. DO NOT use while in a connection!
yihui 1:a607cd9655d7 56 *
yihui 1:a607cd9655d7 57 * @param[in] page_num Page number to update.
yihui 1:a607cd9655d7 58 * @param[in] p_in_array Pointer to a RAM area containing the elements to write in flash.
yihui 1:a607cd9655d7 59 * This area has to be 32 bits aligned.
yihui 1:a607cd9655d7 60 * @param[in] word_count Number of 32 bits words to write in flash.
yihui 1:a607cd9655d7 61 *
yihui 1:a607cd9655d7 62 * @return NRF_SUCCESS on successful flash write, otherwise an error code.
yihui 1:a607cd9655d7 63 */
yihui 1:a607cd9655d7 64 uint32_t ble_flash_page_write(uint8_t page_num, uint32_t * p_in_array, uint8_t word_count);
yihui 1:a607cd9655d7 65
yihui 1:a607cd9655d7 66 /**@brief Function for reading data from flash to RAM.
yihui 1:a607cd9655d7 67 *
yihui 1:a607cd9655d7 68 * @param[in] page_num Page number to read.
yihui 1:a607cd9655d7 69 * @param[out] p_out_array Pointer to a RAM area where the found data will be written.
yihui 1:a607cd9655d7 70 * This area has to be 32 bits aligned.
yihui 1:a607cd9655d7 71 * @param[out] p_word_count Number of 32 bits words read.
yihui 1:a607cd9655d7 72 *
yihui 1:a607cd9655d7 73 * @return NRF_SUCCESS on successful upload, NRF_ERROR_NOT_FOUND if no valid data has been found
yihui 1:a607cd9655d7 74 * in flash (first 32 bits not equal to the MAGIC_NUMBER+CRC).
yihui 1:a607cd9655d7 75 */
yihui 1:a607cd9655d7 76 uint32_t ble_flash_page_read(uint8_t page_num, uint32_t * p_out_array, uint8_t * p_word_count);
yihui 1:a607cd9655d7 77
yihui 1:a607cd9655d7 78 /**@brief Function for erasing a flash page.
yihui 1:a607cd9655d7 79 *
yihui 1:a607cd9655d7 80 * @note This operation blocks the CPU, so it should not be done while the radio is running!
yihui 1:a607cd9655d7 81 *
yihui 1:a607cd9655d7 82 * @param[in] page_num Page number to erase.
yihui 1:a607cd9655d7 83 *
yihui 1:a607cd9655d7 84 * @return NRF_SUCCESS on success, an error_code otherwise.
yihui 1:a607cd9655d7 85 */
yihui 1:a607cd9655d7 86 uint32_t ble_flash_page_erase(uint8_t page_num);
yihui 1:a607cd9655d7 87
yihui 1:a607cd9655d7 88 /**@brief Function for writing one word to flash.
yihui 1:a607cd9655d7 89 *
yihui 1:a607cd9655d7 90 * @note Flash location to be written must have been erased previously.
yihui 1:a607cd9655d7 91 *
yihui 1:a607cd9655d7 92 * @param[in] p_address Pointer to flash location to be written.
yihui 1:a607cd9655d7 93 * @param[in] value Value to write to flash.
yihui 1:a607cd9655d7 94 *
yihui 1:a607cd9655d7 95 * @return NRF_SUCCESS.
yihui 1:a607cd9655d7 96 */
yihui 1:a607cd9655d7 97 uint32_t ble_flash_word_write(uint32_t * p_address, uint32_t value);
yihui 1:a607cd9655d7 98
yihui 1:a607cd9655d7 99 /**@brief Function for writing a data block to flash.
yihui 1:a607cd9655d7 100 *
yihui 1:a607cd9655d7 101 * @note Flash locations to be written must have been erased previously.
yihui 1:a607cd9655d7 102 *
yihui 1:a607cd9655d7 103 * @param[in] p_address Pointer to start of flash location to be written.
yihui 1:a607cd9655d7 104 * @param[in] p_in_array Pointer to start of flash block to be written.
yihui 1:a607cd9655d7 105 * @param[in] word_count Number of words to be written.
yihui 1:a607cd9655d7 106 *
yihui 1:a607cd9655d7 107 * @return NRF_SUCCESS.
yihui 1:a607cd9655d7 108 */
yihui 1:a607cd9655d7 109 uint32_t ble_flash_block_write(uint32_t * p_address, uint32_t * p_in_array, uint16_t word_count);
yihui 1:a607cd9655d7 110
yihui 1:a607cd9655d7 111 /**@brief Function for computing pointer to start of specified flash page.
yihui 1:a607cd9655d7 112 *
yihui 1:a607cd9655d7 113 * @param[in] page_num Page number.
yihui 1:a607cd9655d7 114 * @param[out] pp_page_addr Pointer to start of flash page.
yihui 1:a607cd9655d7 115 *
yihui 1:a607cd9655d7 116 * @return NRF_SUCCESS.
yihui 1:a607cd9655d7 117 */
yihui 1:a607cd9655d7 118 uint32_t ble_flash_page_addr(uint8_t page_num, uint32_t ** pp_page_addr);
yihui 1:a607cd9655d7 119
yihui 1:a607cd9655d7 120 /**@brief Function for calculating a 16 bit CRC using the CRC-16-CCITT scheme.
yihui 1:a607cd9655d7 121 *
yihui 1:a607cd9655d7 122 * @param[in] p_data Pointer to data on which the CRC is to be calulated.
yihui 1:a607cd9655d7 123 * @param[in] size Number of bytes on which the CRC is to be calulated.
yihui 1:a607cd9655d7 124 * @param[in] p_crc Initial CRC value (if NULL, a preset value is used as the initial value).
yihui 1:a607cd9655d7 125 *
yihui 1:a607cd9655d7 126 * @return Calculated CRC.
yihui 1:a607cd9655d7 127 */
yihui 1:a607cd9655d7 128 uint16_t ble_flash_crc16_compute(uint8_t * p_data, uint16_t size, uint16_t * p_crc);
yihui 1:a607cd9655d7 129
yihui 1:a607cd9655d7 130 /**@brief Function for handling flashing module Radio Notification event.
yihui 1:a607cd9655d7 131 *
yihui 1:a607cd9655d7 132 * @note For flash writing to work safely while in a connection or while advertising, this function
yihui 1:a607cd9655d7 133 * MUST be called from the Radio Notification module's event handler (see
yihui 1:a607cd9655d7 134 * @ref ble_radio_notification for details).
yihui 1:a607cd9655d7 135 *
yihui 1:a607cd9655d7 136 * @param[in] radio_active TRUE if radio is active (or about to become active), FALSE otherwise.
yihui 1:a607cd9655d7 137 */
yihui 1:a607cd9655d7 138 void ble_flash_on_radio_active_evt(bool radio_active);
yihui 1:a607cd9655d7 139
yihui 1:a607cd9655d7 140 #endif // BLE_FLASH_H__
yihui 1:a607cd9655d7 141
yihui 1:a607cd9655d7 142 /** @} */