little_flash_define.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #ifndef _LITTLE_FLASH_DEFINE_H_
  2. #define _LITTLE_FLASH_DEFINE_H_
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "little_flash_config.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #define LF_SW_VERSION "0.0.1"
  12. struct little_flash;
  13. typedef struct little_flash little_flash_t;
  14. #ifndef LF_PRINTF
  15. #define LF_PRINTF printf
  16. #endif
  17. #ifndef LF_INFO
  18. #define LF_INFO(...) LF_PRINTF(__VA_ARGS__)
  19. #endif
  20. #ifndef LF_WARNING
  21. #define LF_WARNING(...) LF_PRINTF(__VA_ARGS__)
  22. #endif
  23. #ifndef LF_ERROR
  24. #define LF_ERROR(...) LF_PRINTF(__VA_ARGS__)
  25. #endif
  26. /* assert for developer. */
  27. #ifdef LF_DEBUG_MODE
  28. #ifndef LF_DEBUG
  29. #define LF_DEBUG(...) LF_PRINTF(__VA_ARGS__)
  30. #endif
  31. #define LF_ASSERT(EXPR) \
  32. if (!(EXPR)) \
  33. { \
  34. LF_PRINTF("(%s) has assert failed at %s.", #EXPR, __FUNCTION__); \
  35. while (1); \
  36. }
  37. #else
  38. #ifndef LF_DEBUG
  39. #define LF_DEBUG(...)
  40. #endif
  41. #define LF_ASSERT(EXPR)
  42. #endif
  43. #ifndef LF_NULL
  44. #define LF_NULL NULL
  45. #endif
  46. #define LF_ENABLE 1
  47. #define LF_DISABLE 0
  48. /**
  49. * status register bits
  50. */
  51. enum {
  52. LF_STATUS_REGISTER_BUSY = (1 << 0), /**< busing */
  53. LF_STATUS_REGISTER_WEL = (1 << 1), /**< write enable latch */
  54. };
  55. /**
  56. * @brief flash type
  57. *
  58. */
  59. #define LF_DRIVER_NOR_FLASH (0)
  60. #define LF_DRIVER_NAND_FLASH (1)
  61. /** Address Bytes */
  62. #define LF_ADDR_BYTES_3 (1<<0)
  63. #define LF_ADDR_BYTES_4 (1<<1)
  64. typedef enum {
  65. LF_ERR_OK = 0,
  66. LF_ERR_NO_FLASH,
  67. LF_ERR_TIMEOUT,
  68. LF_ERR_TRANSFER,
  69. LF_ERR_ERASE,
  70. LF_ERR_WRITE,
  71. LF_ERR_READ,
  72. LF_ERR_BUSY,
  73. LF_ERR_BAD_ADDRESS,
  74. LF_ERR_NO_MEM,
  75. LF_ERR_SFDP_HEADER,
  76. LF_ERR_SFDP_PARAMETER,
  77. }lf_err_t;
  78. #ifndef LF_FLASH_NAME_LEN
  79. #define LF_FLASH_NAME_LEN 16
  80. #endif
  81. /* JEDEC Basic Flash Parameter Table */
  82. typedef struct {
  83. union{ /**< 1st DWORD */
  84. struct {
  85. uint32_t Erase_Sizes : 2; /**< Erase Sizes */
  86. uint32_t Write_Granularity:1; /**< Write Granularity */
  87. uint32_t Volatile_Block_Protect:1; /**< Volatile Status Register Block Protect bits */
  88. uint32_t Write_Enable_Select:1; /**< Write Enable Instruction Select for Writing to Volatile Status Register */
  89. uint32_t :3; /**< Contains 111b and can never be changed. */
  90. uint32_t Erase_4k:8; /**< 4 Kilobyte Erase Instruction */
  91. uint32_t Fast_Read_1S1S2S:1; /**< Supports (1S-1S-2S) Fast Read */
  92. uint32_t Address_Bytes:2; /**< Address Bytes */
  93. uint32_t DTR_Clocking:1; /**< Supports Double Transfer Rate (DTR) Clocking */
  94. uint32_t Fast_Read_1S2S2S:1; /**< Supports (1S-2S-2S) Fast Read */
  95. uint32_t Fast_Read_1S4S4S:1; /**< Supports (1S-4S-4S) Fast Read */
  96. uint32_t Fast_Read_1S1S4S:1; /**< Supports (1S-1S-4S) Fast Read */
  97. uint32_t :8; /**< Contains FFh and can never be changed. */
  98. };
  99. uint32_t pt1;
  100. };
  101. uint32_t Flash_Memory_Density; /**< Flash Memory Density */
  102. union{
  103. struct {
  104. uint32_t Fast_Read_1S4S4S_Wait:5; /**< (1S-4S-4S) Fast Read Number of Wait states (dummy clocks) needed before valid output */
  105. uint32_t Fast_Read_1S4S4S_Clocks:3; /**< Quad Input Address Quad Output (1S-4S-4S) Fast Read Number of Mode Clocks */
  106. uint32_t Fast_Read_1S4S4S_Instruction:8;/**< (1S-4S-4S) Fast Read Instruction */
  107. uint32_t Fast_Read_1S1S4S_Wait:5; /**< (1S-1S-4S) Fast Read Number of Wait states (dummy clocks) needed before valid output */
  108. uint32_t Fast_Read_1S1S4S_Clocks:3; /**< (1S-1S-4S) Fast Read Number of Mode Clocks */
  109. uint32_t Fast_Read_1S1S4S_Instruction:8;/**< (1S-1S-4S) Fast Read Instruction */
  110. };
  111. uint32_t pt3;
  112. };
  113. union{
  114. struct {
  115. uint32_t Fast_Read_1S1S2S_Wait:5; /**< (1S-1S-2S) Fast Read Number of Wait states (dummy clocks) needed before valid output */
  116. uint32_t Fast_Read_1S1S2S_Clocks:3; /**< Quad Input Address Quad Output (1S-1S-2S) Fast Read Number of Mode Clocks */
  117. uint32_t Fast_Read_1S1S2S_Instruction:8;/**< (1S-1S-2S) Fast Read Instruction */
  118. uint32_t Fast_Read_1S2S2S_Wait:5; /**< (1S-2S-2S) Fast Read Number of Wait states (dummy clocks) needed before valid output */
  119. uint32_t Fast_Read_1S2S2S_Clocks:3; /**< (1S-2S-2S) Fast Read Number of Mode Clocks */
  120. uint32_t Fast_Read_1S2S2S_Instruction:8;/**< (1S-2S-2S) Fast Read Instruction */
  121. };
  122. uint32_t pt4;
  123. };
  124. union{
  125. struct {
  126. uint32_t Fast_Read_2S2S2S:1; /**< Supports (2S-2S-2S) Fast Read */
  127. uint32_t :3; /**< Reserved. These bits default to all 1’s */
  128. uint32_t Fast_Read_4S4S4S:1; /**< Supports (4S-4S-4S) Fast Read */
  129. uint32_t :27; /**< Reserved. These bits default to all 1’s */
  130. };
  131. uint32_t pt5;
  132. };
  133. union{
  134. struct {
  135. uint32_t :16; /**< Reserved. These bits default to all 1’s */
  136. uint32_t Fast_Read_2S2S2S_Wait:5; /**< (2S-2S-2S) Fast Read Number of Wait states (dummy clocks) needed before valid output */
  137. uint32_t Fast_Read_2S2S2S_Clocks:3; /**< (2S-2S-2S) Fast Read Number of Mode Clocks */
  138. uint32_t Fast_Read_2S2S2S_Instruction:8;/**< (2S-2S-2S) Fast Read Instruction */
  139. };
  140. uint32_t pt6;
  141. };
  142. union{
  143. struct {
  144. uint32_t :16; /**< Reserved. These bits default to all 1’s */
  145. uint32_t Fast_Read_4S4S4S_Wait:5; /**< (4S-4S-4S) Fast Read Number of Wait states (dummy clocks) needed before valid output */
  146. uint32_t Fast_Read_4S4S4S_Clocks:3; /**< (4S-4S-4S) Fast Read Number of Mode Clocks */
  147. uint32_t Fast_Read_4S4S4S_Instruction:8;/**< (4S-4S-4S) Fast Read Instruction */
  148. };
  149. uint32_t pt7;
  150. };
  151. union{
  152. struct {
  153. uint32_t Erase_Type_1_Size:8; /**< Erase Type 1 Size */
  154. uint32_t Erase_Type_1_Instruction:8; /**< Erase Type 1 Instruction */
  155. uint32_t Erase_Type_2_Size:8; /**< Erase Type 2 Size */
  156. uint32_t Erase_Type_2_Instruction:8; /**< Erase Type 2 Instruction */
  157. };
  158. uint32_t pt8;
  159. };
  160. union{
  161. struct {
  162. uint32_t Erase_Type_3_Size:8; /**< Erase Type 3 Size */
  163. uint32_t Erase_Type_3_Instruction:8; /**< Erase Type 3 Instruction */
  164. uint32_t Erase_Type_4_Size:8; /**< Erase Type 4 Size */
  165. uint32_t Erase_Type_4_Instruction:8; /**< Erase Type 4 Instruction */
  166. };
  167. uint32_t pt9;
  168. };
  169. // union{
  170. // struct {
  171. // uint32_t Erase_Time_Multiplier:4; /**< Multiplier from typical erase time to maximum erase time */
  172. // uint32_t Erase_Type_1_Time:7; /**< Erase Type 1 Erase, Typical time */
  173. // uint32_t Erase_Type_2_Time:7; /**< Erase Type 2 Erase, Typical time */
  174. // uint32_t Erase_Type_3_Time:7; /**< Erase Type 3 Erase, Typical time */
  175. // uint32_t Erase_Type_4_Time:7; /**< Erase Type 4 Erase, Typical time */
  176. // };
  177. // uint32_t pt10;
  178. // };
  179. // union{
  180. // struct {
  181. // uint32_t Page_Program_Time_Multiplier:4; /**< Multiplier from typical time to max time for Page or byte program*/
  182. // uint32_t Page_Size:4; /**< Page Size */
  183. // uint32_t Page_Program_Type_1_Time:6; /**< Page Program Typical time */
  184. // uint32_t Page_Program_Type_2_Time:5; /**< Byte Program Typical time, first byte */
  185. // uint32_t Page_Program_Type_3_Time:5; /**< Byte Program Typical time, additional byte */
  186. // uint32_t Erase_Chip_Type_Time:7; /**< Chip Erase, Typical time */
  187. // uint32_t :1;
  188. // };
  189. // uint32_t pt11;
  190. // };
  191. // // ...
  192. // uint32_t pt12;
  193. // uint32_t pt13;
  194. // uint32_t pt14;
  195. // uint32_t pt15;
  196. // uint32_t pt16;
  197. // uint32_t pt17;
  198. // uint32_t pt18;
  199. // uint32_t pt19;
  200. // uint32_t pt20;
  201. // uint32_t pt21;
  202. // uint32_t pt22;
  203. // uint32_t pt23;
  204. // // ...
  205. }little_flash_sfdp_pt_t;
  206. typedef struct {
  207. uint8_t minor_rev; /**< sfdp minor revision */
  208. uint8_t major_rev; /**< sfdp major revision */
  209. uint8_t nph; /**< Number of Parameter Headers (NPH) */
  210. uint8_t access_protocol; /**< SFDP Access Protocol */
  211. uint16_t parameter_id; /**< Parameter ID */
  212. uint8_t parameter_minor_rev; /**< Parameter Minor Revision */
  213. uint8_t parameter_major_rev; /**< Parameter Major Revision */
  214. uint8_t parameter_length; /**< Parameter Length */
  215. uint32_t parameter_pointer; /**< Parameter Table Pointer */
  216. little_flash_sfdp_pt_t pt; /**< Parameter Table */
  217. }little_flash_sfdp_t;
  218. typedef struct {
  219. char name[LF_FLASH_NAME_LEN]; /**< flash chip name */
  220. uint8_t manufacturer_id; /**< MANUFACTURER ID */
  221. uint16_t device_id; /**< DEVICE ID (1byte or 2bytes) */
  222. union{ /**< driver type */
  223. struct {
  224. uint16_t type : 2; /**< flash type */
  225. uint16_t :14; /**< reserved */
  226. };
  227. uint16_t driver_type;
  228. };
  229. uint32_t capacity; /**< flash capacity (bytes) */
  230. uint8_t erase_cmd; /**< erase granularity size block command */
  231. uint32_t erase_size; /**< erase granularity (bytes) */
  232. /* 以下基本可以代码自动推断无需指定 */
  233. uint8_t addr_bytes; /**< address bytes */
  234. uint32_t prog_size; /**< page size (bytes) */
  235. uint32_t read_size; /**< read size (bytes) */
  236. uint32_t retry_times; /**< retry times */
  237. uint32_t erase_times; /**< erase time (ms) */
  238. } little_flash_chipinfo_t;
  239. typedef struct{
  240. lf_err_t (*transfer)(little_flash_t *lf,uint8_t *tx_buf, uint32_t tx_len, uint8_t *rx_buf, uint32_t rx_len);
  241. void* user_data;
  242. } little_flash_spi_t;
  243. struct little_flash{
  244. little_flash_chipinfo_t chip_info;
  245. little_flash_spi_t spi;
  246. /* lock */
  247. void (*lock)(little_flash_t *lf);
  248. /* unlock */
  249. void (*unlock)(little_flash_t *lf);
  250. /* wait 10us */
  251. void (*wait_10us)(uint32_t count);
  252. /* wait ms */
  253. void (*wait_ms)(uint32_t ms);
  254. #ifdef LF_USE_HEAP
  255. /* malloc */
  256. void* (*malloc)(size_t size);
  257. /* free */
  258. void (*free)(void* ptr);
  259. #endif /* LF_USE_HEAP */
  260. /* user data */
  261. void* user_data;
  262. };
  263. /* SFDP JESD216F revision */
  264. #define LF_SFDP_MAJOR_REV (0x01)
  265. #define LF_SFDP_MINOR_REV (0x0A)
  266. #define LF_RETRY_TIMES (10)
  267. #define LF_NORFLASH_ERASE_TIMES (50)
  268. #define LF_NANDFLASH_ERASE_TIMES (2)
  269. #define LF_NORFLASH_PAGE_ZISE (256) /**< NOR flash page size (bytes) */
  270. #define LF_NORFLASH_SECTOR_ZISE (4096) /**< NOR flash sector size (bytes) */
  271. #define LF_NANDFLASH_PAGE_ZISE (2048) /**< NAND flash page size (bytes) */
  272. #define LF_CMD_WRITE_STATUS_REGISTER (0x01)
  273. #define LF_CMD_WRITE_DISABLE (0x04)
  274. #define LF_CMD_READ_STATUS_REGISTER (0x05)
  275. #define LF_CMD_WRITE_ENABLE (0x06)
  276. #define LF_CMD_SFDP_REGISTER (0x5A)
  277. #define LF_CMD_SFDP_HEADER (0x00)
  278. #define LF_CMD_SFDP_PARAMETER_HEADER1 (0x08)
  279. #define LF_CMD_SFDP_PARAMETER_HEADER2 (0x10)
  280. #define LF_CMD_JEDEC_ID (0x9F)
  281. #define LF_CMD_ERASE_CHIP (0xC7)
  282. #define LF_CMD_ENABLE_RESET (0x66)
  283. #define LF_CMD_NORFLASH_RESET (0x99)
  284. #define LF_CMD_BLOCK_ERASE (0xD8)
  285. #define LF_CMD_NANDFLASH_RESET (0xFF)
  286. #define LF_CMD_PROG_DATA (0x02)
  287. #define LF_CMD_READ_DATA (0x03)
  288. #define LF_NANDFLASH_PAGE_DATA_READ (0x13)
  289. #define LF_NANDFLASH_PAGE_PROG_EXEC (0x10)
  290. /* NAND flash status registers */
  291. #define LF_NANDFLASH_STATUS_REGISTER1 (0xA0)
  292. #define LF_NANDFLASH_STATUS_REGISTER2 (0xB0)
  293. #define LF_NANDFLASH_STATUS_REGISTER3 (0xC0)
  294. #define LF_NANDFLASH_STATUS_REGISTER4 (0xD0)
  295. #ifdef __cplusplus
  296. }
  297. #endif
  298. #endif /* _LITTLE_FLASH_DEFINE_H_ */