123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- #include "bsp_flash.h"
- #include "exception.h"
- #include "system.h"
- #include "nrf_sdh_soc.h"
- #include "nrf_pwr_mgmt.h"
- #include "nrf_sdm.h"
- typedef enum
- {
- NRF_STATE_IDLE,
- NRF_STATE_BUSY,
- } falsh_busy_state;
- typedef enum
- {
- NRF_FLASH_OPERATION_IDLE,
- NRF_FLASH_OPERATION_FINISH
- } nrf_flash_sd_state_t;
- static volatile falsh_busy_state BUSY_FLAG =NRF_STATE_IDLE;
- static volatile nrf_flash_sd_state_t FLASH_OP_state =NRF_FLASH_OPERATION_IDLE;
- static void waitForFlashReady(void)
- {
- uint8_t flag =0;
-
-
- sd_softdevice_is_enabled(&flag);
- if(0 == flag){
- BUSY_FLAG = NRF_STATE_IDLE;
- FLASH_OP_state =NRF_FLASH_OPERATION_FINISH;
- return;
- }
-
- while(BUSY_FLAG != NRF_STATE_IDLE)
- {
- nrf_pwr_mgmt_run();
- }
- }
- void flash_evt_handler(uint32_t sys_evt, void *p_context)
- {
-
-
- if ((sys_evt != NRF_EVT_FLASH_OPERATION_SUCCESS) && (sys_evt != NRF_EVT_FLASH_OPERATION_ERROR))
- {
- return;
- }
- switch (sys_evt)
- {
- case NRF_EVT_FLASH_OPERATION_SUCCESS:
- BUSY_FLAG =NRF_STATE_IDLE;
- FLASH_OP_state =NRF_FLASH_OPERATION_FINISH;
- break;
- case NRF_EVT_FLASH_OPERATION_ERROR:
- BUSY_FLAG =NRF_STATE_IDLE;
- break;
- }
- }
- NRF_SDH_SOC_OBSERVER(m_sys_obs, 0, flash_evt_handler, NULL);
- flash_OPER_Result sdflash_write(uint32_t *p_dst, uint32_t const *p_src, uint32_t sizewords)
- {
- uint8_t err_code =0;
- char buff[40]={0};
- memset(buff,0,sizeof(buff));
-
- if ((unsigned int)p_dst + (sizewords * 4) > END_FSTORAGE_ADDR || (unsigned int)p_dst < START_FSTORAGE_ADDR)
- {
- DEBUG_LOG(" ----------------------------sd_flash_write,0x%x 0x%x %d\r\n", (unsigned int)p_dst, (unsigned int)p_src, sizewords);
- return FLASH_ERROR_ADDRESS_FAIL;
- }
-
- BUSY_FLAG =NRF_STATE_BUSY;
- FLASH_OP_state =NRF_FLASH_OPERATION_IDLE;
-
- err_code = sd_flash_write(p_dst, p_src, sizewords);
- if(NRF_SUCCESS != err_code){
- sprintf(buff,"Flash_SaveStep fail:%d",err_code);
- Except_TxError(EXCEPT_FLASH,(const char*)buff);
- return FLASH_ERROR_WRITE_FAIL;
- }
-
- waitForFlashReady();
-
- if(NRF_FLASH_OPERATION_FINISH != FLASH_OP_state)return FLASH_ERROR_WRITE_FAIL;
- return FLASH_OP_SUCCESS;
- }
- flash_OPER_Result sdflash_page_erase(uint32_t page_addr)
- {
- uint8_t err_code =0;
- char buff[40]={0};
- memset(buff,0,sizeof(buff));
-
- if(page_addr % 4 != 0 || page_addr == 0)return FLASH_ERROR_ADDRESS_FAIL;
-
- uint32_t page_number = page_addr / FLASH_PAGE_SIZE;
- BUSY_FLAG =NRF_STATE_BUSY;
- FLASH_OP_state =NRF_FLASH_OPERATION_IDLE;
-
- err_code = sd_flash_page_erase(page_number);
-
- if(NRF_SUCCESS != err_code){
- sprintf(buff,"sd_flash_page_erase fail:%d",err_code);
- Except_TxError(EXCEPT_FLASH,(const char*)buff);
- return FLASH_ERROR_ERASE_FAIL;
- }
-
-
- waitForFlashReady();
-
- if(NRF_FLASH_OPERATION_FINISH != FLASH_OP_state)return FLASH_ERROR_ERASE_FAIL;
- return FLASH_OP_SUCCESS;
- }
|