flash.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @file flash.c
  3. * @author chipsea
  4. * @brief
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. /*******************************************************************************
  11. * @file flash.h
  12. * @brief Contains all functions support for flash driver
  13. * @version 0.0
  14. * @date 27. Nov. 2017
  15. * @author qing.han
  16. *
  17. *
  18. *******************************************************************************/
  19. #ifndef _FLASH_H_
  20. #define _FLASH_H_
  21. #include "rom_sym_def.h"
  22. #include "clock.h"
  23. #include "types.h"
  24. #include "gpio.h"
  25. #include "version.h"
  26. #define FLASH_SIZE_256KB (0)
  27. #define FLASH_SIZE_512KB (1)
  28. #define FLASH_SIZE_1MB (2)
  29. #define SPIF_FLASH_SIZE FLASH_SIZE_512KB
  30. #define SPIF_TIMEOUT (0x7ffffff)//1000000
  31. #define SFLG_WIP 1
  32. #define SFLG_WEL 2
  33. #define SFLG_WELWIP 3
  34. //define flash ucds
  35. #define FLASH_BASE_ADDR (0x11000000)
  36. #define FLASH_UCDS_ADDR_BASE 0x11005000
  37. #define CHIP_ID_LENGTH 64
  38. #define CHIP_ID_PID_LEN 16
  39. #define CHIP_ID_LID_LEN 10
  40. #define CHIP_ID_MID_LEN 16
  41. #define CHIP_ID_TID_LEN 14
  42. #define CHIP_ID_SID_LEN 8
  43. #define CHIP_MADDR_LEN 6
  44. //xip flash read instrcution
  45. #define XFRD_FCMD_READ 0x0000003
  46. #define XFRD_FCMD_READ_DUAL 0x801003B
  47. #define XFRD_FCMD_READ_QUAD 0x801006B
  48. #define FCMD_RESET 0x99 //reset
  49. #define FCMD_ENRST 0x66 //enable reset
  50. #define FCMD_WREN 0x06 //write enable
  51. #define FCMD_WRDIS 0x04 //write disable
  52. #define FCMD_VSRWREN 0x50 //Volatile SR Write Enable
  53. #define FCMD_CERASE 0x60 //(or 0xC7)chip erase
  54. #define FCMD_SERASE 0x20 //sector erase
  55. #define FCMD_BERASE32 0x52 //block erease 32k
  56. #define FCMD_BERASE64 0xD8
  57. #define FCMD_DPWRDN 0xB9 //deep power down
  58. #define FCMD_RLSDPD 0xAB //release from powerdown(and read device id)
  59. #define FCMD_WRST 0x01 //write status
  60. #define FCMD_RDID 0x9F //read ID
  61. #define FCMD_RDST 0x05 //read status
  62. #define FCMD_RDST_H 0x35 //read status high byte
  63. #define FCMD_PPROG 0x02 //page program
  64. #define FCMD_READ 0x03 //read
  65. #define FCMD_READF 0x0B //fast read
  66. #define FCMD_READDO 0x3B //dual output fast read
  67. #define FCMD_READDIO 0xBB //dual I/O fast read
  68. #define FCMD_READQO 0x6B //quad output fast read
  69. #define FCMD_READQIO 0xeB //quad I/O fast read
  70. #define FCMD_READQIOW 0xe7 //quad I/O fast read word
  71. //Flash address definition
  72. #define FLASH_CHIP_INFO_ADDR 0x0000
  73. #define FLASH_1ST_BOOT_INFO_ADDR 0x2000
  74. #define FLASH_APP_BOOT_INFO_ADDR 0x3000
  75. #define FLASH_PROGRAMMER_INFO_ADDR 0x4000
  76. #define FLASH_APP_ADDR 0x5000
  77. #define FLASH_CODE_BACKUP_ADDR 0x38000
  78. #define FLASH_BOOTLOADER_ADDR 0x6b000 //64KB
  79. #define FLASH_BOOTLOADER_UPDATE_INFO_ADDR 0x7b000 //0x7b000~0x7bfff 4KB
  80. typedef enum{
  81. FLASH_LOCK_504K = 0x04,//0x00-0x7DFFF
  82. FLASH_LOCK_496K = 0x08,//0x00-0x7BFFF
  83. FLASH_LOCK_480K = 0x0C,//0x00-0x77FFF
  84. FLASH_LOCK_448K = 0x10,//0x00-0x6FFFF
  85. FLASH_LOCK_384K = 0x14,//0x00-0x5FFFF
  86. FLASH_LOCK_HALF = 0x18,//0x00-0x3FFFF
  87. FLASH_LOCK_ALL = 0x1C,//0x00-0x7FFFF
  88. FLASH_UNLOCK = 0x00,
  89. } flash_lock_t;
  90. ErrCode_t HalFlashInit(void);
  91. void HalFlashRead(uint32_t addr, uint8_t *buf, uint32_t len);
  92. ErrCode_t HalFlashWrite(uint32_t addr, uint8_t* buf, uint32_t len);
  93. ErrCode_t HalFlashWriteByDma(uint32_t addr, uint8_t* buf, uint32_t len);
  94. ErrCode_t HalFlashErase(uint32_t sectorNum);
  95. ErrCode_t HalFlashWriteWord(uint32_t addr, uint32_t value);
  96. void HalFlashLock(flash_lock_t lock);
  97. void HalFlashUnlock(void);
  98. #endif