nand_flash.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. *********************************************************************************************************
  3. *
  4. * 模块名称 : NAND Flash 驱动模块
  5. * 文件名称 : bsp_nand.h
  6. * 版 本 : V1.0
  7. * 说 明 : 头文件
  8. *
  9. * Copyright (C), 2013-2014, 安富莱电子 www.armfly.com
  10. *
  11. *********************************************************************************************************
  12. */
  13. #ifndef __NAND_H
  14. #define __NAND_H
  15. #include "interface.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. /* NAND Flash 型号 */
  30. typedef enum
  31. {
  32. HY27UF081G2A = 0,
  33. K9F1G08U0A,
  34. K9F1G08U0B,
  35. NAND_UNKNOW
  36. } NAND_TYPE_E;
  37. #define NAND_TYPE HY27UF081G2A
  38. /*
  39. 定义有效的 NAND ID
  40. HY27UF081G2A = 0xAD 0xF1 0x80 0x1D
  41. K9F1G08U0A = 0xEC 0xF1 0x80 0x15
  42. K9F1G08U0B = 0xEC 0xF1 0x00 0x95
  43. */
  44. #define NAND_MAKER_ID 0xAD
  45. #define NAND_DEVICE_ID 0xF1
  46. #define NAND_THIRD_ID 0x80
  47. #define NAND_FOURTH_ID 0x1D
  48. #define HY27UF081G2A 0xADF1801D
  49. #define K9F1G08U0A 0xECF18015
  50. #define K9F1G08U0B 0xECF10095
  51. /* Exported constants --------------------------------------------------------*/
  52. /* NAND Area definition for STM3210E-EVAL Board RevD */
  53. #define CMD_AREA (uint32_t)(1 << 16) /* A16 = CLE high */
  54. #define ADDR_AREA (uint32_t)(1 << 17) /* A17 = ALE high */
  55. #define DATA_AREA ((uint32_t)0x00000000)
  56. /* FSMC NAND memory command */
  57. #define NAND_CMD_AREA_A ((uint8_t)0x00)
  58. #define NAND_CMD_AREA_B ((uint8_t)0x01)
  59. #define NAND_CMD_AREA_C ((uint8_t)0x50)
  60. #define NAND_CMD_AREA_TRUE1 ((uint8_t)0x30)
  61. #define NAND_CMD_WRITE0 ((uint8_t)0x80)
  62. #define NAND_CMD_WRITE_TRUE1 ((uint8_t)0x10)
  63. #define NAND_CMD_ERASE0 ((uint8_t)0x60)
  64. #define NAND_CMD_ERASE1 ((uint8_t)0xD0)
  65. #define NAND_CMD_READID ((uint8_t)0x90)
  66. #define NAND_CMD_LOCK_STATUS ((uint8_t)0x7A)
  67. #define NAND_CMD_RESET ((uint8_t)0xFF)
  68. /* NAND memory status */
  69. #define NAND_BUSY ((uint8_t)0x00)
  70. #define NAND_ERROR ((uint8_t)0x01)
  71. #define NAND_READY ((uint8_t)0x40)
  72. #define NAND_TIMEOUT_ERROR ((uint8_t)0x80)
  73. /* FSMC NAND memory parameters */
  74. /* 用于HY27UF081G2A K9F1G08 */
  75. #if NAND_TYPE == HY27UF081G2A
  76. #define NAND_PAGE_SIZE ((uint16_t)0x0800) /* 2 * 1024 bytes per page w/o Spare Area */
  77. #define NAND_BLOCK_SIZE ((uint16_t)0x0040) /* 64 pages per block */
  78. #define NAND_ZONE_SIZE ((uint16_t)0x0400) /* 1024 Block per zone */
  79. #define NAND_SPARE_AREA_SIZE ((uint16_t)0x0040) /* last 64 bytes as spare area */
  80. #define NAND_MAX_ZONE ((uint16_t)0x0001) /* 1 zones of 1024 block */
  81. /* 命令代码定义 */
  82. #define NAND_CMD_COPYBACK_A ((uint8_t)0x00) /* PAGE COPY-BACK 命令序列 */
  83. #define NAND_CMD_COPYBACK_B ((uint8_t)0x35)
  84. #define NAND_CMD_COPYBACK_C ((uint8_t)0x85)
  85. #define NAND_CMD_COPYBACK_D ((uint8_t)0x10)
  86. #define NAND_CMD_STATUS ((uint8_t)0x70) /* 读NAND Flash的状态字 */
  87. #define MAX_PHY_BLOCKS_PER_ZONE 1024 /* 每个区最大物理块号 */
  88. #define MAX_LOG_BLOCKS_PER_ZONE 1000 /* 每个区最大逻辑块号 */
  89. #define NAND_BLOCK_COUNT 1024 /* 块个数 */
  90. #define NAND_PAGE_TOTAL_SIZE (NAND_PAGE_SIZE + NAND_SPARE_AREA_SIZE) /* 页面总大小 */
  91. #else
  92. #define NAND_PAGE_SIZE ((uint16_t)0x0200) /* 512 bytes per page w/o Spare Area */
  93. #define NAND_BLOCK_SIZE ((uint16_t)0x0020) /* 32x512 bytes pages per block */
  94. #define NAND_ZONE_SIZE ((uint16_t)0x0400) /* 1024 Block per zone */
  95. #define NAND_SPARE_AREA_SIZE ((uint16_t)0x0010) /* last 16 bytes as spare area */
  96. #define NAND_MAX_ZONE ((uint16_t)0x0004) /* 4 zones of 1024 block */
  97. #endif
  98. #define NAND_BAD_BLOCK_FLAG 0x00 /* 块内第1个page备用区的第1个字节写入非0xFF数据表示坏块 */
  99. #define NAND_USED_BLOCK_FLAG 0xF0 /* 块内第1个page备用区的第2个字节写入非0xFF数据表示已使用的块 */
  100. #define BI_OFFSET 0 /* 块内第1个page备用区的第1个字节是坏块标志 */
  101. #define USED_OFFSET 1 /* 块内第1个page备用区的第1个字节是已用标志 */
  102. #define LBN0_OFFSET 2 /* 块内第1个page备用区的第3个字节表示逻辑块号低8bit */
  103. #define LBN1_OFFSET 3 /* 块内第1个page备用区的第4个字节表示逻辑块号高8bit */
  104. #define VALID_SPARE_SIZE 4 /* 实际使用的备用区大小,用于函数内部声明数据缓冲区大小 */
  105. /* FSMC NAND memory address computation */
  106. #define ADDR_1st_CYCLE(ADDR) (uint8_t)((ADDR)&0xFF) /* 1st addressing cycle */
  107. #define ADDR_2nd_CYCLE(ADDR) (uint8_t)(((ADDR)&0xFF00) >> 8) /* 2nd addressing cycle */
  108. #define ADDR_3rd_CYCLE(ADDR) (uint8_t)(((ADDR)&0xFF0000) >> 16) /* 3rd addressing cycle */
  109. #define ADDR_4th_CYCLE(ADDR) (uint8_t)(((ADDR)&0xFF000000) >> 24) /* 4th addressing cycle */
  110. /* Exported macro ------------------------------------------------------------*/
  111. /* Exported functions ------------------------------------------------------- */
  112. #define NAND_OK 0
  113. #define NAND_FAIL 1
  114. #define FREE_BLOCK (1 << 12)
  115. #define BAD_BLOCK (1 << 13)
  116. #define VALID_BLOCK (1 << 14)
  117. #define USED_BLOCK (1 << 15)
  118. /*
  119. LUT[]的格式:
  120. uint16_t usGoodBlockFirst; // 第1个好块
  121. uint16_t usDataBlockCount; // 可用于数据存储的块个数, 从第2个好块开始
  122. uint16_t usBakBlockStart; // 备份块起始块号
  123. uint32_t usPhyBlockNo[ulDataBlockCount]; // 物理块号数组。低字节在前,高字节在后。
  124. */
  125. #define DATA_BLOCK_PERCENT 98 /* 数据块占总有效块数的百分比 */
  126. #define LUT_FIRST_GOOD_BLOCK 0 /* LUT[] 第1个单元用于存储第1个有效块号 */
  127. #define LUT_DATA_BLOCK_COUNT 1 /* LUT[] 第2个单元用于存储第有效块号个数 */
  128. #define LUT_BAK_BLOCK_START 2 /* LUT[] 第3个单元用于备份区起始块号 */
  129. #define LUT_GOOD_BLOCK_START 3 /* LUT[] 第4个单元用于数据区起始块号 */
  130. /* Private Structures---------------------------------------------------------*/
  131. typedef struct __SPARE_AREA
  132. {
  133. uint16_t LogicalIndex;
  134. uint16_t DataStatus;
  135. uint16_t BlockStatus;
  136. } SPARE_AREA;
  137. typedef enum
  138. {
  139. WRITE_IDLE = 0,
  140. POST_WRITE,
  141. PRE_WRITE,
  142. WRITE_CLEANUP,
  143. WRITE_ONGOING
  144. } WRITE_STATE;
  145. typedef enum
  146. {
  147. OLD_BLOCK = 0,
  148. UNUSED_BLOCK
  149. } BLOCK_STATE;
  150. /* Private macro --------------------------------------------------------------*/
  151. //#define WEAR_LEVELLING_SUPPORT 磨损平衡支持
  152. #define WEAR_DEPTH 10 /* 磨损深度 */
  153. #define PAGE_TO_WRITE (Transfer_Length / 512)
  154. #define BAD_BALOK_TEST_CYCLE 5 /* 判别坏块算法的重复擦写次数 */
  155. /* Private variables ----------------------------------------------------------*/
  156. /* Private function prototypes ------------------------------------------------*/
  157. /* exported functions ---------------------------------------------------------*/
  158. uint8_t nand_flash_init(void);
  159. uint8_t NAND_Write(uint32_t _ulMemAddr, uint32_t *_pWriteBuf, uint16_t _usSize);
  160. uint8_t NAND_Read(uint32_t _ulMemAddr, uint32_t *_pReadBuf, uint16_t _usSize);
  161. uint8_t NAND_Format(void);
  162. void NAND_DispBadBlockInfo(void);
  163. uint8_t NAND_ScanBlock(uint32_t _ulPageNo);
  164. uint32_t NAND_FormatCapacity(void);
  165. uint32_t NAND_ReadID(void);
  166. void NAND_DispPhyPageData(uint32_t _uiPhyPageNo);
  167. void NAND_DispLogicPageData(uint32_t _uiLogicPageNo);
  168. uint8_t NAND_WriteMultiSectors(uint8_t *_pBuf, uint32_t _SectorNo, uint16_t _SectorSize, uint32_t _SectorCount);
  169. uint8_t NAND_ReadMultiSectors(uint8_t *_pBuf, uint32_t _SectorNo, uint16_t _SectorSize, uint32_t _SectorCount);
  170. #endif /* __FSMC_NAND_H */
  171. /***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/