| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- #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;
- }
|