#include "flash.h" #include "nrf_delay.h" #include "nrf_log.h" #include "type.h" #include "md5.h" static bool store_flag=false; static bool update_flag=false; static bool load_flag=false; static bool clear_flag=false; pstorage_handle_t m_storage_handle_app; static pstorage_handle_t m_storage_handle_appdata; static pstorage_handle_t block_hander_appdata; static pstorage_handle_t block_hander_reset_data; static void upgrade_pstorage_cb_handler(pstorage_handle_t * p_handle, uint8_t op_code, uint32_t result, uint8_t * p_data, uint32_t data_len) { switch(op_code) { case PSTORAGE_LOAD_OP_CODE: if (result == NRF_SUCCESS) { load_flag=true; // NRF_LOG_PRINTF("PSTORAGE_LOAD_OP_CODE success \r\n"); } else { // NRF_LOG_PRINTF("PSTORAGE_LOAD_OP_CODE fail \r\n"); } break; case PSTORAGE_STORE_OP_CODE: if (result == NRF_SUCCESS) { store_flag=true; // NRF_LOG_PRINTF("PSTORAGE_STORE_OP_CODE success \r\n"); } else { // NRF_LOG_PRINTF("PSTORAGE_STORE_OP_CODE fail \r\n"); } break; case PSTORAGE_CLEAR_OP_CODE: if (result == NRF_SUCCESS) { clear_flag=true; // NRF_LOG_PRINTF("PSTORAGE_CLEAR_OP_CODE success \r\n"); } else { // NRF_LOG_PRINTF("PSTORAGE_CLEAR_OP_CODE fail \r\n"); } break; case PSTORAGE_UPDATE_OP_CODE: if (result == NRF_SUCCESS) { update_flag=true; // NRF_LOG_PRINTF("PSTORAGE_UPDATE_OP_CODE success \r\n"); } else { // NRF_LOG_PRINTF("PSTORAGE_UPDATE_OP_CODE fail \r\n"); } break; default:break; } } uint16_t flash_upgrade_init(void) { pstorage_module_param_t param; uint16_t err_code; param.cb = upgrade_pstorage_cb_handler; m_storage_handle_app.block_id = DFU_BANK_1_REGION_START; // NRF_LOG_PRINTF("flash bank1 start is[%02X],size is[%d k]\r\n",m_storage_handle_app.block_id,DFU_IMAGE_MAX_SIZE_BANKED/1024); err_code = pstorage_raw_register(¶m,&m_storage_handle_app); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("flash pstorage register success!\r\n"); } else { // NRF_LOG_PRINTF("flash pstorage register fail!err_code is %d\r\n",err_code); return err_code; } clear_flag = false; err_code = pstorage_raw_clear(&m_storage_handle_app,DFU_IMAGE_MAX_SIZE_BANKED); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("flash pstorage clear success!\r\n"); } else { // NRF_LOG_PRINTF("flash pstorage clear fail!err_code is %d\r\n",err_code); return err_code; } while(!clear_flag) { // NRF_LOG_PRINTF("wating clear...\r\n"); //nrf_delay_ms(1); nrf_delay_ms(50); } return err_code; } uint16_t flash_upgrade_store(uint8_t* input,uint16_t input_len,uint16_t ota_bin_offset) { uint16_t err_code; // NRF_LOG_PRINTF("flash pstorage start store,input_len is %d,offset is %d\r\n",input_len,ota_bin_offset); store_flag = false; err_code = pstorage_raw_store(&m_storage_handle_app,input,input_len,ota_bin_offset); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("flash pstorage store success!\r\n"); } else { // NRF_LOG_PRINTF("flash pstorage store fail!err_code is %d\r\n",err_code); return err_code; } while(!store_flag) { // NRF_LOG_PRINTF("wating store...\r\n"); nrf_delay_ms(5); //nrf_delay_ms(1); } return err_code; } uint16_t appdata_flash_init(void) { pstorage_module_param_t param; uint16_t err_code; err_code = pstorage_init(); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("appdata flash pstorage init success!\r\n"); } else { // NRF_LOG_PRINTF("appdata flash pstorage init fail!err_code is %d\r\n",err_code); return err_code; } param.block_size = PSTORAGE_MAX_BLOCK_SIZE; param.block_count = 2; param.cb = upgrade_pstorage_cb_handler; m_storage_handle_appdata.block_id = DFU_APP_DATA_START; // NRF_LOG_PRINTF("appdata flash start is %02X\r\n",m_storage_handle_appdata.block_id); err_code = pstorage_register(¶m,&m_storage_handle_appdata); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("appdata flash pstorage register success!\r\n"); } else { // NRF_LOG_PRINTF("appdata flash pstorage register fail!err_code is %d\r\n",err_code); return err_code; } return err_code; } uint16_t flash_upgrade_clear(void) { uint16_t err_code; clear_flag = false; err_code = pstorage_raw_clear(&m_storage_handle_app,DFU_IMAGE_MAX_SIZE_BANKED); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("flash pstorage clear success!\r\n"); } else { // NRF_LOG_PRINTF("flash pstorage clear fail!err_code is %d\r\n",err_code); return err_code; } while(!clear_flag) { // NRF_LOG_PRINTF("wating clear...\r\n"); //nrf_delay_ms(1); nrf_delay_ms(50); } return err_code; } uint16_t appdata_flash_update(uint8_t *src,uint16_t size,uint16_t offset) { uint16_t err_code; err_code = pstorage_block_identifier_get(&m_storage_handle_appdata,1,&block_hander_appdata); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("appdata flash pstorage block id get success!\r\n"); } else { // NRF_LOG_PRINTF("appdata flash pstorage block id get fail,err_code is %d!\r\n",err_code); return err_code; } update_flag = false; err_code = pstorage_update(&block_hander_appdata,src,size,offset); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("appdata flash pstorage update success!\r\n"); } else { // NRF_LOG_PRINTF("appdata flash pstorage update fail,err_code is %d!\r\n",err_code); return err_code; } while(!update_flag) { // NRF_LOG_PRINTF("appdata wating update...\r\n"); nrf_delay_ms(5); } return err_code; } uint16_t appdata_flash_load(uint8_t *dest,uint16_t size,uint16_t offset) { uint16_t err_code; err_code = pstorage_block_identifier_get(&m_storage_handle_appdata,1,&block_hander_appdata); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("appdata flash pstorage block id get success!\r\n"); } else { // NRF_LOG_PRINTF("appdata flash pstorage block id get fail,err_code is %d!\r\n",err_code); return err_code; } load_flag = false; err_code = pstorage_load(dest,&block_hander_appdata,size,offset); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("appdata flash pstorage load success!\r\n"); } else { // NRF_LOG_PRINTF("appdata flash pstorage load fail,err_code is %d!\r\n",err_code); return err_code; } while(!load_flag) { // NRF_LOG_PRINTF("appdata wating load...\r\n"); nrf_delay_ms(5); } return err_code; } uint16_t reset_data_flash_update(uint8_t *src,uint16_t size,uint16_t offset) { uint16_t err_code; err_code = pstorage_block_identifier_get(&m_storage_handle_appdata,0,&block_hander_reset_data); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("appdata flash pstorage block id get success!\r\n"); } else { // NRF_LOG_PRINTF("appdata flash pstorage block id get fail,err_code is %d!\r\n",err_code); return err_code; } update_flag = false; err_code = pstorage_update(&block_hander_reset_data,src,size,offset); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("appdata flash pstorage update success!\r\n"); } else { // NRF_LOG_PRINTF("appdata flash pstorage update fail,err_code is %d!\r\n",err_code); return err_code; } while(!update_flag) { // NRF_LOG_PRINTF("appdata wating update...\r\n"); nrf_delay_ms(5); } return err_code; } uint16_t reset_data_flash_load(uint8_t *dest,uint16_t size,uint16_t offset) { uint16_t err_code; err_code = pstorage_block_identifier_get(&m_storage_handle_appdata,0,&block_hander_reset_data); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("appdata flash pstorage block id get success!\r\n"); } else { // NRF_LOG_PRINTF("appdata flash pstorage block id get fail,err_code is %d!\r\n",err_code); return err_code; } load_flag = false; err_code = pstorage_load(dest,&block_hander_reset_data,size,offset); if(err_code == NRF_SUCCESS) { // NRF_LOG_PRINTF("appdata flash pstorage load success!\r\n"); } else { // NRF_LOG_PRINTF("appdata flash pstorage load fail,err_code is %d!\r\n",err_code); return err_code; } while(!load_flag) { // NRF_LOG_PRINTF("appdata wating load...\r\n"); nrf_delay_ms(5); } return err_code; }