nandflash.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. *********************************************************************************************************
  3. *
  4. * 模块名称 : NAND Flash 驱动模块
  5. * 文件名称 : bsp_nand.h
  6. * 版 本 : V1.1
  7. * 说 明 : 头文件
  8. *
  9. * Copyright (C), 2014-2015, 安富莱电子 www.armfly.com
  10. *
  11. *********************************************************************************************************
  12. */
  13. #ifndef __BSP_NAND_H
  14. #define __BSP_NAND_H
  15. #include "main.h"
  16. typedef struct
  17. {
  18. uint16_t Zone;
  19. uint16_t Block;
  20. uint16_t Page;
  21. } NAND_ADDRESS_T;
  22. #define NAND_TYPE S34ML02G100TF
  23. /*
  24. 定义有效的 NAND ID
  25. HY27UF081G2A = 0xAD 0xF1 0x80 0x1D
  26. K9F1G08U0A = 0xEC 0xF1 0x80 0x15
  27. K9F1G08U0B = 0xEC 0xF1 0x00 0x95
  28. H27U4G8F2DTR = 0xAD DC 90 95
  29. */
  30. /* NAND Flash 型号 */
  31. #define HY27UF081G2A 0xADF1801D
  32. #define K9F1G08U0A 0xECF18015
  33. #define K9F1G08U0B 0xECF10095
  34. #define H27U1G8F2BTR 0xADF1001D /* STM32-V4 缺省 */
  35. #define H27U4G8F2DTR 0xADDC9095
  36. #define S34ML02G100TF 0x01DA9095
  37. #define NAND_UNKNOW 0xFFFFFFFF
  38. /* Exported constants --------------------------------------------------------*/
  39. /* NAND Area definition for STM3210E-EVAL Board RevD */
  40. //#define CMD_AREA (uint32_t)(1<<16) /* A16 = CLE high */
  41. //#define ADDR_AREA (uint32_t)(1<<17) /* A17 = ALE high */
  42. #define DATA_AREA ((uint32_t)0x00000000)
  43. /* FSMC NAND memory command */ //在stm32f2xx_hal_nand.h
  44. //#define NAND_CMD_AREA_A ((uint8_t)0x00)
  45. //#define NAND_CMD_AREA_B ((uint8_t)0x01)
  46. //#define NAND_CMD_AREA_C ((uint8_t)0x50)
  47. //#define NAND_CMD_AREA_TRUE1 ((uint8_t)0x30)
  48. //#define NAND_CMD_WRITE0 ((uint8_t)0x80)
  49. //#define NAND_CMD_WRITE_TRUE1 ((uint8_t)0x10)
  50. //#define NAND_CMD_ERASE0 ((uint8_t)0x60)
  51. //#define NAND_CMD_ERASE1 ((uint8_t)0xD0)
  52. //#define NAND_CMD_READID ((uint8_t)0x90)
  53. //#define NAND_CMD_LOCK_STATUS ((uint8_t)0x7A)
  54. //#define NAND_CMD_RESET ((uint8_t)0xFF)
  55. /* NAND memory status */
  56. //#define NAND_BUSY ((uint8_t)0x00)
  57. //#define NAND_ERROR ((uint8_t)0x01)
  58. //#define NAND_READY ((uint8_t)0x40)
  59. //#define NAND_TIMEOUT_ERROR ((uint8_t)0x80)
  60. #if NAND_TYPE==S34ML02G100TF
  61. #define NAND_PAGE_SIZE ((uint16_t)0x0800) /* 2 * 1024 bytes per page w/o Spare Area */
  62. #define NAND_BLOCK_SIZE ((uint16_t)0x0040) /* 64 pages per block */
  63. #define NAND_ZONE_SIZE ((uint16_t)0x0400) /* 1024 Block per zone */
  64. #define NAND_SPARE_AREA_SIZE ((uint16_t)0x0040) /* last 64 bytes as spare area */
  65. #define NAND_MAX_ZONE ((uint16_t)0x0002) /* 2 zones of 1024 block */
  66. #define NAND_ADDR_5 1 /* 0表示只用发送4个字节的地址,1表示5个 */
  67. /* 命令代码定义 */
  68. #define NAND_CMD_COPYBACK_A ((uint8_t)0x00) /* PAGE COPY-BACK 命令序列 */
  69. #define NAND_CMD_COPYBACK_B ((uint8_t)0x35)
  70. #define NAND_CMD_COPYBACK_C ((uint8_t)0x85)
  71. #define NAND_CMD_COPYBACK_D ((uint8_t)0x10)
  72. // #define NAND_CMD_STATUS ((uint8_t)0x70) /* 读NAND Flash的状态字 */
  73. #define MAX_PHY_BLOCKS_PER_ZONE 1024 /* 每个区最大物理块号 */
  74. #define MAX_LOG_BLOCKS_PER_ZONE 1000 /* 每个区最大逻辑块号 */
  75. #define NAND_BLOCK_COUNT 1024 /* 块个数 */
  76. #define NAND_PAGE_TOTAL_SIZE (NAND_PAGE_SIZE + NAND_SPARE_AREA_SIZE) /* 页面总大小 */
  77. #elif NAND_TYPE == HY27UF081G2A
  78. #define NAND_PAGE_SIZE ((uint16_t)0x0800) /* 2 * 1024 bytes per page w/o Spare Area */
  79. #define NAND_BLOCK_SIZE ((uint16_t)0x0040) /* 64 pages per block */
  80. #define NAND_ZONE_SIZE ((uint16_t)0x0400) /* 1024 Block per zone */
  81. #define NAND_SPARE_AREA_SIZE ((uint16_t)0x0040) /* last 64 bytes as spare area */
  82. #define NAND_MAX_ZONE ((uint16_t)0x0001) /* 1 zones of 1024 block */
  83. #define NAND_ADDR_5 0 /* 0表示只用发送4个字节的地址,1表示5个 */
  84. /* 命令代码定义 */
  85. #define NAND_CMD_COPYBACK_A ((uint8_t)0x00) /* PAGE COPY-BACK 命令序列 */
  86. #define NAND_CMD_COPYBACK_B ((uint8_t)0x35)
  87. #define NAND_CMD_COPYBACK_C ((uint8_t)0x85)
  88. #define NAND_CMD_COPYBACK_D ((uint8_t)0x10)
  89. #define NAND_CMD_STATUS ((uint8_t)0x70) /* 读NAND Flash的状态字 */
  90. #define MAX_PHY_BLOCKS_PER_ZONE 1024 /* 每个区最大物理块号 */
  91. #define MAX_LOG_BLOCKS_PER_ZONE 1000 /* 每个区最大逻辑块号 */
  92. #define NAND_BLOCK_COUNT 1024 /* 块个数 */
  93. #define NAND_PAGE_TOTAL_SIZE (NAND_PAGE_SIZE + NAND_SPARE_AREA_SIZE) /* 页面总大小 */
  94. #elif NAND_TYPE == H27U4G8F2DTR
  95. #define NAND_PAGE_SIZE ((uint16_t)0x0800) /* 2 * 1024 bytes per page w/o Spare Area */
  96. #define NAND_BLOCK_SIZE ((uint16_t)0x0040) /* 64 pages per block */
  97. #define NAND_ZONE_SIZE ((uint16_t)0x1000) /* 4096 Block per zone */
  98. #define NAND_SPARE_AREA_SIZE ((uint16_t)0x0040) /* last 64 bytes as spare area */
  99. #define NAND_MAX_ZONE ((uint16_t)0x0001) /* 1 zones of 4096 block */
  100. #define NAND_ADDR_5 1 /* 1表示只发送4个字节的地址,1表示5个 */
  101. /* 命令代码定义 */
  102. #define NAND_CMD_COPYBACK_A ((uint8_t)0x00) /* PAGE COPY-BACK 命令序列 */
  103. #define NAND_CMD_COPYBACK_B ((uint8_t)0x35)
  104. #define NAND_CMD_COPYBACK_C ((uint8_t)0x85)
  105. #define NAND_CMD_COPYBACK_D ((uint8_t)0x10)
  106. #define NAND_CMD_STATUS ((uint8_t)0x70) /* 读NAND Flash的状态字 */
  107. #define MAX_PHY_BLOCKS_PER_ZONE 4096 /* 每个区最大物理块号 */
  108. #define MAX_LOG_BLOCKS_PER_ZONE 4000 /* 每个区最大逻辑块号 */
  109. #define NAND_BLOCK_COUNT 4096 /* 块个数 */
  110. #define NAND_PAGE_TOTAL_SIZE (NAND_PAGE_SIZE + NAND_SPARE_AREA_SIZE) /* 页面总大小 */
  111. #else
  112. #define NAND_PAGE_SIZE ((uint16_t)0x0200) /* 512 bytes per page w/o Spare Area */
  113. #define NAND_BLOCK_SIZE ((uint16_t)0x0020) /* 32x512 bytes pages per block */
  114. #define NAND_ZONE_SIZE ((uint16_t)0x0400) /* 1024 Block per zone */
  115. #define NAND_SPARE_AREA_SIZE ((uint16_t)0x0010) /* last 16 bytes as spare area */
  116. #define NAND_MAX_ZONE ((uint16_t)0x0004) /* 4 zones of 1024 block */
  117. #endif
  118. #define NAND_BAD_BLOCK_FLAG 0x00 /* 块内第1个page备用区的第1个字节写入非0xFF数据表示坏块 */
  119. #define NAND_USED_BLOCK_FLAG 0xF0 /* 块内第1个page备用区的第2个字节写入非0xFF数据表示已使用的块 */
  120. #define BI_OFFSET 0 /* 块内第1个page备用区的第1个字节是坏块标志 */
  121. #define USED_OFFSET 1 /* 块内第1个page备用区的第1个字节是已用标志 */
  122. #define LBN0_OFFSET 2 /* 块内第1个page备用区的第3个字节表示逻辑块号低8bit */
  123. #define LBN1_OFFSET 3 /* 块内第1个page备用区的第4个字节表示逻辑块号高8bit */
  124. #define VALID_SPARE_SIZE 4 /* 实际使用的备用区大小,用于函数内部声明数据缓冲区大小 */
  125. /* FSMC NAND memory address computation */
  126. #define ADDR_1st_CYCLE(ADDR) (uint8_t)((ADDR)& 0xFF) /* 1st addressing cycle */
  127. #define ADDR_2nd_CYCLE(ADDR) (uint8_t)(((ADDR)& 0xFF00) >> 8) /* 2nd addressing cycle */
  128. #define ADDR_3rd_CYCLE(ADDR) (uint8_t)(((ADDR)& 0xFF0000) >> 16) /* 3rd addressing cycle */
  129. #define ADDR_4th_CYCLE(ADDR) (uint8_t)(((ADDR)& 0xFF000000) >> 24) /* 4th addressing cycle */
  130. /* Exported macro ------------------------------------------------------------*/
  131. /* Exported functions ------------------------------------------------------- */
  132. #define NAND_OK 0
  133. #define NAND_FAIL 1
  134. #define FREE_BLOCK (1 << 12 )
  135. #define BAD_BLOCK (1 << 13 )
  136. #define VALID_BLOCK (1 << 14 )
  137. #define USED_BLOCK (1 << 15 )
  138. /*
  139. LUT[]的格式:
  140. uint16_t usGoodBlockFirst; // 第1个好块
  141. uint16_t usDataBlockCount; // 可用于数据存储的块个数, 从第2个好块开始
  142. uint16_t usBakBlockStart; // 备份块起始块号
  143. uint32_t usPhyBlockNo[ulDataBlockCount]; // 物理块号数组。低字节在前,高字节在后。
  144. */
  145. #define DATA_BLOCK_PERCENT 98 /* 数据块占总有效块数的百分比 */
  146. #define LUT_FIRST_GOOD_BLOCK 0 /* LUT[] 第1个单元用于存储第1个有效块号 */
  147. #define LUT_DATA_BLOCK_COUNT 1 /* LUT[] 第2个单元用于存储第有效块号个数 */
  148. #define LUT_BAK_BLOCK_START 2 /* LUT[] 第3个单元用于备份区起始块号 */
  149. #define LUT_GOOD_BLOCK_START 3 /* LUT[] 第4个单元用于数据区起始块号 */
  150. /* Private Structures---------------------------------------------------------*/
  151. typedef struct __SPARE_AREA {
  152. uint16_t LogicalIndex;
  153. uint16_t DataStatus;
  154. uint16_t BlockStatus;
  155. } SPARE_AREA;
  156. typedef enum {
  157. WRITE_IDLE = 0,
  158. POST_WRITE,
  159. PRE_WRITE,
  160. WRITE_CLEANUP,
  161. WRITE_ONGOING
  162. }WRITE_STATE;
  163. typedef enum {
  164. OLD_BLOCK = 0,
  165. UNUSED_BLOCK
  166. }BLOCK_STATE;
  167. /* ONFI 结构 (for H27U4G8F2DTR) page 26 */
  168. /* 必须添加__packed关键字表示结构体成员间紧密排列 */
  169. __packed typedef struct
  170. {
  171. uint8_t Sign[4]; /* = "ONFI" */
  172. uint16_t Revision; /* Bit1 = 1 表示支持 ONFI Ver 1.0 */
  173. uint16_t Features; /* */
  174. uint16_t OptionalCommands;
  175. uint8_t Rsv1[22];
  176. /* Manufacturer information block */
  177. uint8_t Manufacturer[12]; /* 制造商 */
  178. uint8_t Model[20]; /* 型号 */
  179. uint8_t JEDEC_ID; /* AD */
  180. uint16_t DateCode;
  181. uint8_t Rsv2[13];
  182. /* Memory organization block */
  183. uint32_t PageDataSize;
  184. uint16_t PageSpareSize;
  185. uint32_t PartialPageDataSize;
  186. uint16_t PartialPageSpareSize;
  187. uint32_t BlockSize;
  188. uint32_t LogicalUnitSize;
  189. uint8_t LogicalUnitNumber;
  190. uint8_t AddressCycles;
  191. uint8_t CellBits;
  192. uint16_t BadBlockMax;
  193. uint16_t BlockEndurance;
  194. uint8_t ValidBlocksBegin; /* 最前面保证有效的块个数 */
  195. uint16_t BlockEndurance2; /* Block endurance for guaranteed valid blocks */
  196. uint8_t ProgramsPerPage; /* Number of programs per page */
  197. uint8_t PartialProgram;
  198. uint8_t ECCcorrectBits;
  199. uint8_t InterleavedAddrBits; /* 交错的地址位 */
  200. uint8_t InterleavedOperaton;
  201. uint8_t Rsv3[13];
  202. /* Electrical parameters block */
  203. uint8_t PinCapactance;
  204. uint16_t TimingMode;
  205. uint16_t ProgramCacheTimingMode;
  206. uint16_t PageProgTime;
  207. uint16_t BlockEraseTime;
  208. uint16_t PageReadTime;
  209. uint16_t ChangeColumnSetupTime;
  210. uint8_t Rsv4[23];
  211. /* Vendor block */
  212. uint16_t VendorRevision;
  213. uint8_t VendorSpecific[88];
  214. uint16_t IntegritaCRC;
  215. }PARAM_PAGE_T;
  216. /* Private macro --------------------------------------------------------------*/
  217. //#define WEAR_LEVELLING_SUPPORT 磨损平衡支持
  218. #define WEAR_DEPTH 10 /* 磨损深度 */
  219. #define PAGE_TO_WRITE (Transfer_Length/512)
  220. #define BAD_BALOK_TEST_CYCLE 3 /* 判别坏块算法的重复擦写次数 */
  221. /* NAND 块统计 */
  222. typedef struct
  223. {
  224. uint32_t ChipID;
  225. char ChipName[16];
  226. uint32_t Bad;
  227. uint32_t Free;
  228. uint32_t Used;
  229. }NAND_BLOCK_INFO_T;
  230. /* Private variables ----------------------------------------------------------*/
  231. /* Private function prototypes ------------------------------------------------*/
  232. /* exported functions ---------------------------------------------------------*/
  233. uint8_t NAND_Init(void);
  234. uint8_t NAND_Write(uint32_t _ulMemAddr, uint32_t *_pWriteBuf, uint16_t _usSize);
  235. uint8_t NAND_Read(uint32_t _ulMemAddr, uint32_t *_pReadBuf, uint16_t _usSize);
  236. uint8_t NAND_Format(void);
  237. void NAND_DispBadBlockInfo(void);
  238. uint8_t NAND_ScanBlock(uint32_t _ulPageNo);
  239. uint32_t NAND_FormatCapacity(void);
  240. uint32_t NAND_ReadID(void);
  241. void NAND_DispPhyPageData(uint32_t _uiPhyPageNo);
  242. void NAND_DispLogicPageData(uint32_t _uiLogicPageNo);
  243. uint8_t NAND_WriteMultiSectors(uint8_t *_pBuf, uint32_t _SectorNo, uint16_t _SectorSize, uint32_t _SectorCount);
  244. uint8_t NAND_ReadMultiSectors(uint8_t *_pBuf, uint32_t _SectorNo, uint16_t _SectorSize, uint32_t _SectorCount);
  245. void NAND_ReadONFI(uint8_t *_pBuf);
  246. void NAND_ReadParamPage(PARAM_PAGE_T *_pData);
  247. void NAND_DispParamPage(void);
  248. void NAND_ScanAllBadBlock(void);
  249. uint8_t NAND_GetBlockInfo(NAND_BLOCK_INFO_T *_pInfo);
  250. void NAND_MarkBadBlock(uint32_t _ulBlockNo);
  251. #endif /* __FSMC_NAND_H */
  252. /***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/