bsp_flash.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //flash 1.0V
  2. /*********************************************************************
  3. * INCLUDES
  4. */
  5. #include "bsp_flash.h"
  6. #include "exception.h"
  7. #include "system.h"
  8. #include "nrf_sdh_soc.h"
  9. #include "nrf_pwr_mgmt.h"
  10. #include "nrf_sdm.h"
  11. typedef enum
  12. {
  13. NRF_STATE_IDLE, //FALSH空闲
  14. NRF_STATE_BUSY, //FLASH忙
  15. } falsh_busy_state;
  16. typedef enum
  17. {
  18. NRF_FLASH_OPERATION_IDLE, //!< No operations requested to the SoftDevice.
  19. NRF_FLASH_OPERATION_FINISH
  20. } nrf_flash_sd_state_t;
  21. static volatile falsh_busy_state BUSY_FLAG =NRF_STATE_IDLE;
  22. static volatile nrf_flash_sd_state_t FLASH_OP_state =NRF_FLASH_OPERATION_IDLE;
  23. //等待flash操作完成
  24. static void waitForFlashReady(void)
  25. {
  26. uint8_t flag =0;
  27. //没有打开协议栈直接退出
  28. sd_softdevice_is_enabled(&flag);
  29. if(0 == flag){
  30. BUSY_FLAG = NRF_STATE_IDLE;
  31. FLASH_OP_state =NRF_FLASH_OPERATION_FINISH;
  32. return;
  33. }
  34. while(BUSY_FLAG != NRF_STATE_IDLE) // While fstorage is busy, sleep and wait for an event.
  35. {
  36. nrf_pwr_mgmt_run();
  37. }
  38. }
  39. void flash_evt_handler(uint32_t sys_evt, void *p_context)
  40. {
  41. //UNUSED_PARAMETER(p_context);
  42. // DEBUG_LOG(" NRF_EVT_FLASH_OPERATION_SUCCESS\r\n");
  43. if ((sys_evt != NRF_EVT_FLASH_OPERATION_SUCCESS) && (sys_evt != NRF_EVT_FLASH_OPERATION_ERROR))
  44. {
  45. return;
  46. }
  47. switch (sys_evt)
  48. {
  49. case NRF_EVT_FLASH_OPERATION_SUCCESS:
  50. BUSY_FLAG =NRF_STATE_IDLE;
  51. FLASH_OP_state =NRF_FLASH_OPERATION_FINISH;
  52. break;
  53. case NRF_EVT_FLASH_OPERATION_ERROR:
  54. BUSY_FLAG =NRF_STATE_IDLE;
  55. break;
  56. }
  57. }
  58. NRF_SDH_SOC_OBSERVER(m_sys_obs, 0, flash_evt_handler, NULL);
  59. flash_OPER_Result sdflash_write(uint32_t *p_dst, uint32_t const *p_src, uint32_t sizewords)
  60. {
  61. uint8_t err_code =0;
  62. char buff[40]={0};
  63. memset(buff,0,sizeof(buff));
  64. if ((unsigned int)p_dst + (sizewords * 4) > END_FSTORAGE_ADDR || (unsigned int)p_dst < START_FSTORAGE_ADDR)
  65. {
  66. DEBUG_LOG(" ----------------------------sd_flash_write,0x%x 0x%x %d\r\n", (unsigned int)p_dst, (unsigned int)p_src, sizewords);
  67. return FLASH_ERROR_ADDRESS_FAIL;
  68. }
  69. BUSY_FLAG =NRF_STATE_BUSY;
  70. FLASH_OP_state =NRF_FLASH_OPERATION_IDLE;
  71. err_code = sd_flash_write(p_dst, p_src, sizewords);
  72. if(NRF_SUCCESS != err_code){
  73. sprintf(buff,"Flash_SaveStep fail:%d",err_code);
  74. Except_TxError(EXCEPT_FLASH,(const char*)buff);
  75. return FLASH_ERROR_WRITE_FAIL;
  76. }
  77. waitForFlashReady();
  78. if(NRF_FLASH_OPERATION_FINISH != FLASH_OP_state)return FLASH_ERROR_WRITE_FAIL;
  79. return FLASH_OP_SUCCESS;
  80. }
  81. flash_OPER_Result sdflash_page_erase(uint32_t page_addr)
  82. {
  83. uint8_t err_code =0;
  84. char buff[40]={0};
  85. memset(buff,0,sizeof(buff));
  86. if(page_addr % 4 != 0 || page_addr == 0)return FLASH_ERROR_ADDRESS_FAIL;
  87. uint32_t page_number = page_addr / FLASH_PAGE_SIZE;
  88. BUSY_FLAG =NRF_STATE_BUSY;
  89. FLASH_OP_state =NRF_FLASH_OPERATION_IDLE;
  90. err_code = sd_flash_page_erase(page_number);
  91. if(NRF_SUCCESS != err_code){
  92. sprintf(buff,"sd_flash_page_erase fail:%d",err_code);
  93. Except_TxError(EXCEPT_FLASH,(const char*)buff);
  94. return FLASH_ERROR_ERASE_FAIL;
  95. }
  96. waitForFlashReady();
  97. if(NRF_FLASH_OPERATION_FINISH != FLASH_OP_state)return FLASH_ERROR_ERASE_FAIL;
  98. // DEBUG_LOG(">>>>>>>>>>sdflash_page_erase FLASH_OP_SUCCESS,%02x\r\n",page_addr);
  99. return FLASH_OP_SUCCESS;
  100. }
  101. /****************************************************END OF FILE****************************************************/