Ejemplo n.º 1
0
/**@brief      This function stores in flash the System Attributes related to a specified master.
 *
 * @param[in]  p_sys_attr   System Attributes to be stored.
 *
 * @return     NRF_SUCCESS on success, an error_code otherwise.
 */
static uint32_t sys_attr_store(master_sys_attr_t * p_sys_attr)
{
    uint32_t err_code;

    // Check if flash is full.
    if (m_sys_attr_in_flash_count >= MAX_BONDS_IN_FLASH)
    {
        return NRF_ERROR_NO_MEM;
    }

    // Check if this is the first time any System Attributes is stored.
    if (m_sys_attr_in_flash_count == 0)
    {
        // Initialize CRC
        m_crc_sys_attr = ble_flash_crc16_compute(NULL, 0, NULL);
    
        // Find pointer to start of System Attributes flash block.
        err_code = ble_flash_page_addr(m_bondmngr_config.flash_page_num_sys_attr, 
                                       &mp_flash_sys_attr);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    }
    
    // Write System Attributes in flash.
    err_code = ble_flash_block_write(mp_flash_sys_attr + 1,
                                    (uint32_t *)p_sys_attr,
                                    sizeof(master_sys_attr_t) / sizeof(uint32_t));
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
    m_crc_sys_attr = ble_flash_crc16_compute((uint8_t *)p_sys_attr, 
                                             sizeof(master_sys_attr_t), 
                                             &m_crc_sys_attr);
    
    // Write header
    err_code = ble_flash_word_write(mp_flash_sys_attr, BLE_FLASH_MAGIC_NUMBER | m_crc_sys_attr);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
    
    // Update flash pointer
    mp_flash_sys_attr += ((sizeof(master_sys_attr_t) / sizeof(uint32_t)) + 1);
    
    m_sys_attr_in_flash_count++;
    return NRF_SUCCESS;
}
Ejemplo n.º 2
0
/**@brief      This function stores the Bonding Information of the specified master to the flash.
 *
 * @param[in]  p_bond   Bonding information to be stored.
 *
 * @return     NRF_SUCCESS on success, an error_code otherwise.
 */
static uint32_t bond_info_store(master_bond_t * p_bond)
{
    uint32_t err_code;

    // Check if flash is full
    if (m_bond_info_in_flash_count >= MAX_BONDS_IN_FLASH)
    {
        return NRF_ERROR_NO_MEM;
    }

    // Check if this is the first bond to be stored
    if (m_bond_info_in_flash_count == 0)
    {
        // Initialize CRC
        m_crc_bond_info = ble_flash_crc16_compute(NULL, 0, NULL);
    
        // Find pointer to start of bond information flash block
        err_code = ble_flash_page_addr(m_bondmngr_config.flash_page_num_bond, &mp_flash_bond_info);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    }
    
    // Write Bonding Information
    err_code = ble_flash_block_write(mp_flash_bond_info + 1,
                                     (uint32_t *)p_bond,
                                     sizeof(master_bond_t) / sizeof(uint32_t));
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
    m_crc_bond_info = ble_flash_crc16_compute((uint8_t *)p_bond, 
                                              sizeof(master_bond_t), 
                                              &m_crc_bond_info);
    
    // Write header
    err_code = ble_flash_word_write(mp_flash_bond_info, BLE_FLASH_MAGIC_NUMBER | m_crc_bond_info);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
    
    // Update flash pointer
    mp_flash_bond_info += (sizeof(master_bond_t) / sizeof(uint32_t)) + 1;
    
    m_bond_info_in_flash_count++;
    return NRF_SUCCESS;
}
Ejemplo n.º 3
0
/**@brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;
    bool     bootloader_is_pushed = false;
		static uint32_t TestAdr;
	
		TestAdr = (uint32_t)*(&(BluetoothDFU));
	
    if(TestAdr == 100) {
			bootloader_is_pushed = true;
			F_flash_page_erase(&BluetoothDFU);
			ble_flash_word_write(&BluetoothDFU , 0);
		}
	
    // This check ensures that the defined fields in the bootloader corresponds with actual
    // setting in the nRF51 chip.
    APP_ERROR_CHECK_BOOL(NRF_UICR->CLENR0 == CODE_REGION_1_START);

    APP_ERROR_CHECK_BOOL(*((uint32_t *)NRF_UICR_BOOT_START_ADDRESS) == BOOTLOADER_REGION_START);
    APP_ERROR_CHECK_BOOL(NRF_FICR->CODEPAGESIZE == CODE_PAGE_SIZE);

    // Initialize.
    timers_init();
    gpiote_init();
    buttons_init();
    ble_stack_init();
    scheduler_init();

    //bootloader_is_pushed = ((nrf_gpio_pin_read(BOOTLOADER_BUTTON_PIN) == 0)? true: false);

    if (bootloader_is_pushed || (!bootloader_app_is_valid(DFU_BANK_0_REGION_START)))
    {
        // Initiate an update of the firmware.
        err_code = bootloader_dfu_start();
        APP_ERROR_CHECK(err_code);
    }

    if (bootloader_app_is_valid(DFU_BANK_0_REGION_START))
    {
        // Select a bank region to use as application region.
        // @note: Only applications running from DFU_BANK_0_REGION_START is supported.
        bootloader_app_start(DFU_BANK_0_REGION_START);
        
    }
    
    NVIC_SystemReset();
}
Ejemplo n.º 4
0
uint32_t bond_store_user_id(void)
{
    uint32_t  err_code;
    uint16_t  m_crc_bond_info;
    uint32_t  *mp_flash_bond_info;

    /* inialize crc check */
    m_crc_bond_info = ble_flash_crc16_compute(NULL, 0, NULL);

    // Find pointer to start of bond information flash block
    err_code = ble_flash_page_addr(FLASH_PAGE_USER_ID, &mp_flash_bond_info);
    if (err_code != NRF_SUCCESS) {
        return err_code;
    }

    /*FIXME: erase a page while bluetooth connected may cause disconnect, so this maybe stored when bluetooth disconnected*/
    /* Erase the whole page */
    ble_flash_page_erase(FLASH_PAGE_USER_ID);

    // Write private bonding Information
    err_code = ble_flash_block_write(mp_flash_bond_info + 1, //the first word is used to store crc
                                     (uint32_t *)user_id,
                                     ((USER_ID_LENGTH - 1) / (4) + 1));
    if (err_code != NRF_SUCCESS) {
        return err_code;
    }

    m_crc_bond_info = ble_flash_crc16_compute((uint8_t *)user_id,
                      USER_ID_LENGTH,
                      &m_crc_bond_info);

    // Write header
    err_code = ble_flash_word_write(mp_flash_bond_info, BLE_FLASH_MAGIC_NUMBER | m_crc_bond_info);
    if (err_code != NRF_SUCCESS) {
        return err_code;
    }

    return NRF_SUCCESS;
}