bsp_flash.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef __BSP_FLASH_H__
  2. #define __BSP_FLASH_H__
  3. //flash 1.0V
  4. /*********************************************************************
  5. * INCLUDES
  6. */
  7. #include "nrf_fstorage.h"
  8. #include "nrf_fstorage_sd.h"
  9. #include "nrf_pwr_mgmt.h"
  10. #include "nrf_soc.h"
  11. #include "nrf_log.h"
  12. #include "app_error.h"
  13. #include "SEGGER_RTT.h"
  14. /*********************************************************************
  15. * DEFINITIONS
  16. */
  17. #define FSTORAGE_READ 0x00 //读标志位
  18. #define FSTORAGE_WRITE 0x01 //写标志位
  19. #define START_FSTORAGE_ADDR 0x00074000UL //FLASH起始地址
  20. #define END_FSTORAGE_ADDR 0x00078000UL //FLASH结束地址
  21. #define FLASH_PAGE_SIZE 4096 //页的长度
  22. #define FLASH_PAGE_NUM ((END_FSTORAGE_ADDR - START_FSTORAGE_ADDR)/FLASH_PAGE_SIZE) //页的个数
  23. #define PAGE_INT_SIZE (FLASH_PAGE_SIZE/4) //一页所包含的4字节个数
  24. #define FLASH_ZONE_ADDR_1 START_FSTORAGE_ADDR //页1首地址
  25. #define FLASH_ZONE_ADDR_2 (FLASH_ZONE_ADDR_1 +FLASH_PAGE_SIZE) //页2首地址
  26. #define FLASH_ZONE_ADDR_3 (FLASH_ZONE_ADDR_2 +FLASH_PAGE_SIZE) //页3首地址
  27. #define FLASH_ZONE_ADDR_4 (FLASH_ZONE_ADDR_3 +FLASH_PAGE_SIZE) //页4首地址
  28. #define FLASH_ZONEADDR_CHECK(zone_addr) ((zone_addr==FLASH_ZONE_ADDR_1 || \
  29. zone_addr==FLASH_ZONE_ADDR_2 || \
  30. zone_addr==FLASH_ZONE_ADDR_3 || \
  31. zone_addr==FLASH_ZONE_ADDR_4)?0:1) //验证是否在页x的范围内
  32. //ERROR CODE
  33. #define FLASH_OP_SUCCESS 0x00 //flash操作成功
  34. #define FLASH_ERROR_ZONEADDR (FLASH_OP_SUCCESS+0x01) //区域地址错误
  35. #define FLASH_ERROR_ADDROVERBOUNDS (FLASH_OP_SUCCESS+0x02) //地址越界
  36. #define FLASH_ERROR_ADDRALIGN (FLASH_OP_SUCCESS+0x03) //地址不对齐
  37. #define FLASH_ERROR_DATAALIGN (FLASH_OP_SUCCESS+0x04) //4字节数据不对齐
  38. #define FLASH_ERROR_DATAEXIST (FLASH_OP_SUCCESS+0x05) //数据已存在
  39. #define FLASH_ERROR_PAGENUM (FLASH_OP_SUCCESS+0x06) //页号错误
  40. #define FLASH_ERROR_READ (FLASH_OP_SUCCESS+0x07) //读取错误
  41. #define FLASH_ERROR_WRITE (FLASH_OP_SUCCESS+0x08) //写入错误
  42. #define FLASH_ERROR_ERASE (FLASH_OP_SUCCESS+0x09) //擦除错误
  43. /*********************************************************************
  44. * API FUNCTIONS
  45. */
  46. //FLASH初始化
  47. void Fstorage_FlashInit(void);
  48. //连续读取N个字节
  49. uint32_t Read_N_Byte_flash(uint32_t addr , uint32_t *pData, uint32_t dataLen);
  50. //获取输入地址对应的页号
  51. uint32_t GetPageNum(uint32_t addr, uint16_t *PageNum);
  52. //根据页号擦除一整页
  53. uint32_t Erase_OnePage(uint16_t PageNum);
  54. //连续写入N个4字节(可跨页写,不影响其他数据)
  55. uint32_t Write_N_4Byte_flash(uint32_t addr , uint32_t *pdata, uint32_t len);
  56. //连续写入N个4字节(调用前FLASH预先已擦除)
  57. uint32_t Only_Write_N_4Byte_flash(uint32_t addr , uint32_t *pdata, uint32_t len, bool is_check);
  58. //查找一页中可写的空间
  59. uint32_t Find_FreeSpace_In_Page(uint16_t PageNum, uint32_t *start_addr, uint32_t *len);
  60. #endif