nandflash.h 11 KB

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