bsp_flash.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if ((unsigned int)p_dst + (sizewords * 4) > END_FSTORAGE_ADDR || (unsigned int)p_dst < START_FSTORAGE_ADDR)
  62. {
  63. DEBUG_LOG(" ----------------------------sd_flash_write,0x%x 0x%x %d\r\n", (unsigned int)p_dst, (unsigned int)p_src, sizewords);
  64. return FLASH_ERROR_ADDRESS_FAIL;
  65. }
  66. BUSY_FLAG =NRF_STATE_BUSY;
  67. FLASH_OP_state =NRF_FLASH_OPERATION_IDLE;
  68. sd_flash_write(p_dst, p_src, sizewords);
  69. waitForFlashReady();
  70. if(NRF_FLASH_OPERATION_FINISH != FLASH_OP_state)return FLASH_ERROR_WRITE_FAIL;
  71. return FLASH_OP_SUCCESS;
  72. }
  73. flash_OPER_Result sdflash_page_erase(uint32_t page_addr)
  74. {
  75. if(page_addr % 4 != 0 || page_addr == 0)return FLASH_ERROR_ADDRESS_FAIL;
  76. uint32_t page_number = page_addr / FLASH_PAGE_SIZE;
  77. BUSY_FLAG =NRF_STATE_BUSY;
  78. FLASH_OP_state =NRF_FLASH_OPERATION_IDLE;
  79. sd_flash_page_erase(page_number);
  80. waitForFlashReady();
  81. if(NRF_FLASH_OPERATION_FINISH != FLASH_OP_state)return FLASH_ERROR_ERASE_FAIL;
  82. // DEBUG_LOG(">>>>>>>>>>sdflash_page_erase FLASH_OP_SUCCESS,%02x\r\n",page_addr);
  83. return FLASH_OP_SUCCESS;
  84. }
  85. /****************************************************END OF FILE****************************************************/