nandflash.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  1. /*
  2. *********************************************************************************************************
  3. *
  4. * 模块名称 : NAND Flash驱动模块
  5. * 文件名称 : bsp_nand.c
  6. * 版 本 : V1.3
  7. * 说 明 : 提供NAND Flash (HY27UF081G2A, 8bit 128K字节 大页)的底层接口函数。【安富莱原创】
  8. *
  9. * 修改记录 :
  10. * 版本号 日期 作者 说明
  11. * V1.0 2013-02-01 armfly 正式发布
  12. * V1.1 2014-05-02 armfly 增加512MB型号: H27U4G8F2DTR,
  13. * (1) 增加 NAND_ReadONFI() 函数;
  14. * (2) 增加 NAND_ReadParamPage() 函数;
  15. * (3) 解决 NAND_IsFreeBlock(uint32_t _ulBlockNo) 函数的BUG。
  16. * (4) 解决 FSMC_NAND_PageCopyBack() 和 FSMC_NAND_PageCopyBackEx(), A18必须相同的BUG
  17. * (5) 修改 NAND_FindFreeBlock(), 增加形参,查找奇数或偶数块号
  18. * (6) 修改 打印坏块的函数,增加标识 1 表示已用块 0 表示空闲块
  19. * (7) NAND_MarkUsedBlock() 调用的地方给的形参不对。 ulPhyPageNo / NAND_BLOCK_SIZE
  20. * V1.2 2015-07-23 armfly 增加函数 uint8_t NAND_GetBlockInfo(void); 方便LCD显示。
  21. * V1.3 2015-08-04 armfly 增加对 H27U1G8F2BTR 芯片的支持。
  22. *
  23. * Copyright (C), 2015-2016, 安富莱电子 www.armfly.com
  24. *
  25. *********************************************************************************************************
  26. */
  27. #include "nandflash.h"
  28. #include "string.h"
  29. #include "stdio.h"
  30. /* 定义调试打印语句,用于排错 */
  31. #define printf_err printf
  32. //#define printf_err(...)
  33. //#define printf_ok printf
  34. #define printf_ok(...)
  35. #define flag
  36. #define WRITE_PAGE_VERIFY_EN /* 写 page 数据时校验 */
  37. /*
  38. 如果在IAR或KEIL的编辑器中阅读,请将编辑器的字体设置为新宋体(9号/五号),缩进的TAB设置为4。
  39. 否则,方框处出现不对齐的问题。
  40. 【待完善的地方】
  41. (1)在操作NAND Flash时,如下语句是一个死循环。如果硬件出现异常,将导致软件死机
  42. while( GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6) == 0 )
  43. (2)没有增加ECC校验功能。ECC可以检查1个或2个bit错误,如果只有1个bit错误,则可以修复这个bit。如果
  44. 多余2个bit错误,则可能检测不到。
  45. (3)正常写文件操作时,会导致重建LUT。目前,重建LUT的代码执行效率还不够高,有待完善。
  46. 【硬件说明】
  47. 安富莱开发板配置的NAND Flahs为海力士的HY27UF081G2A, 439板子配的为 H27U4G8F2DTR
  48. (1)NAND Flash的片选信号连接到CPU的FSMC_NCE2,这决定了NAND Flash的地址空间为 0x70000000(见CPU的数据
  49. 手册的FSMC章节)
  50. (2)有FSMC总线上有多个总线设备(如TFT、SRAM、CH374T、NOR),因此必须确保其他总线设备的片选处于禁止
  51. 状态,否则将出现总线冲突问题 (参见本文件初始化FSMC GPIO的函数)
  52. 【NAND Flash 结构定义】
  53. 备用区有16x4字节,每page 2048字节,每512字节一个扇区,每个扇区对应16自己的备用区:
  54. 每个PAGE的逻辑结构,前面512Bx4是主数据区,后面16Bx4是备用区
  55. ┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐
  56. │ Main area ││ Main area ││ Main area ││Main area ││ Spare area ││ Spare area ││ Spare area ││Spare area │
  57. │ ││ ││ ││ ││ ││ ││ ││ │
  58. │ 512B ││ 512B ││ 512B ││ 512B ││ 16B ││ 16B ││ 16B ││ 16B │
  59. └──────┘└──────┘└──────┘└──────┘└──────┘└──────┘└──────┘└──────┘
  60. 每16B的备用区的逻辑结构如下:(三星推荐标准)
  61. ┌───┐┌───┐┌──┐┌──┐┌──┐┌───┐┌───┐┌───┐┌──┐┌──┐┌──┐┌──┐┌───┐┌───┐┌───┐┌───┐┌───┐
  62. │ BI ││RESER ││LSN0││LSN1││LSN2││RESER ││RESER ││RESER ││ECC0││ECC1││ECC2││ECC0││S-ECC1││S-ECC0││RESER ││RESER ││RESER │
  63. │ ││ VED ││ ││ ││ ││ VED ││ VED ││ VED ││ ││ ││ ││ ││ ││ ││ VED ││ VED ││ VED │
  64. └───┘└───┘└──┘└──┘└──┘└───┘└───┘└───┘└──┘└──┘└──┘└──┘└───┘└───┘└───┘└───┘└───┘
  65. K9F1G08U0A 和 HY27UF081G2A 是兼容的。芯片出厂时,厂商保证芯片的第1个块是好块。如果是坏块,则在该块的第1个PAGE的第1个字节
  66. 或者第2个PAGE(当第1个PAGE坏了无法标记为0xFF时)的第1个字节写入非0xFF值。坏块标记值是随机的,软件直接判断是否等于0xFF即可。
  67. 注意:网上有些资料说NAND Flash厂商的默认做法是将坏块标记定在第1个PAGE的第6个字节处。这个说法是错误。坏块标记在第6个字节仅针对部分小扇区(512字节)的NAND Flash
  68. 并不是所有的NAND Flash都是这个标准。大家在更换NAND Flash时,请仔细阅读芯片的数据手册。
  69. 为了便于在NAND Flash 上移植Fat文件系统,我们对16B的备用区采用以下分配方案:
  70. ┌──┐┌──┐┌──┐┌──┐┌──┐┌──┐┌──┐┌──┐┌──┐┌──┐┌───┐┌───┐┌──┐┌──┐┌──┐┌──┐
  71. │ BI ││USED││LBN0││LBN1││ECC0││ECC1││ECC2││ECC3││ECC4││ECC5││S-ECC1││S-ECC0││RSVD││RSVD││RSVD││RSVD│
  72. │ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ │
  73. └──┘└──┘└──┘└──┘└──┘└──┘└──┘└──┘└──┘└──┘└───┘└───┘└──┘└──┘└──┘└──┘
  74. - BI : 坏块标志(Bad Block Identifier)。每个BLOCK的第1个PAGE或者第2个PAGE的第1个字节指示该块是否坏块。0xFF表示好块,不是0xFF表示坏块。
  75. - USED : 该块使用标志。0xFF表示空闲块;0xF0表示已用块。
  76. - LBN0 LBN1 : 逻辑块号(Logic Block No) 。从0开始编码。只在每个BLOCK的第1个PAGE有效,其它PAGE该字段固定为0xFF FF
  77. - ECC0 ~ ECC6 : 512B主数据区的ECC校验 (按照三星提供ECC算法,256字节对应3个字节的ECC)
  78. - S-ECC1 S-ECC0 : LSN0和LSN2的ECC校验
  79. - RSVD : 保留字节,Reserved
  80. 【坏块管理 & 磨损平衡】
  81. (1) 内部全局数组s_usLUT[]按次序保存物理块号。用于物理块和逻辑块的地址映射。
  82. (2) 格式化时,将98%的好块用于主数据存储。剩余的2%用于备用区(坏块替换)。
  83. (3) 写扇区(512B)时,如果扇区内容为空,则直接写入,减少不必要的块擦除操作。有效提高NAND Flash的寿命和读写性能。
  84. (4) 写扇区时,如果扇区内容不为空,则从末尾开始查找一个空闲块替换掉旧块,替换并改写数据完成后,将旧块擦除,并标注为空闲,之后重建LUT。
  85. (5) 块复制时,充分利用NAND Flash硬件的Copy-Back功能,无需读源页到内存再写入目标页。这样可显著提高读写效率。
  86. (6) 磨损平衡还存在缺陷,效果不好。ECC校验暂未实现。
  87. */
  88. /* 定义NAND Flash的物理地址。这个是由硬件决定的 */
  89. #define Bank2_NAND_ADDR ((uint32_t)0x70000000)
  90. #define Bank_NAND_ADDR Bank2_NAND_ADDR
  91. /* 定义操作NAND Flash用到3个宏 */
  92. #define NAND_CMD_AREA *(__IO uint8_t *)(Bank_NAND_ADDR | CMD_AREA)
  93. #define NAND_ADDR_AREA *(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA)
  94. #define NAND_DATA_AREA *(__IO uint8_t *)(Bank_NAND_ADDR | DATA_AREA)
  95. /* 逻辑块号映射表。好块总数的2%用于备份区,因此数组维数低于1024。 LUT = Look Up Table */
  96. static uint16_t s_usLUT[NAND_BLOCK_COUNT];
  97. static uint16_t s_usValidDataBlockCount; /* 有效的数据块个数 */
  98. static uint8_t s_ucTempBuf[NAND_PAGE_TOTAL_SIZE]; /* 大缓冲区,2112字节. 用于读出比较 */
  99. static uint8_t NAND_BuildLUT(void);
  100. static uint8_t FSMC_NAND_GetStatus(void);
  101. static uint16_t NAND_FindFreeBlock (uint32_t _ulSrcPageNo);
  102. static uint8_t NAND_MarkUsedBlock(uint32_t _ulBlockNo);
  103. static uint16_t NAND_AddrToPhyBlockNo(uint32_t _ulMemAddr);
  104. static uint8_t NAND_IsBufOk(uint8_t *_pBuf, uint32_t _ulLen, uint8_t _ucValue);
  105. uint8_t NAND_WriteToNewBlock(uint32_t _ulPhyPageNo, uint8_t *_pWriteBuf, uint16_t _usOffset, uint16_t _usSize);
  106. static uint8_t NAND_IsFreeBlock(uint32_t _ulBlockNo);
  107. static uint16_t NAND_LBNtoPBN(uint32_t _uiLBN);
  108. static uint8_t FSMC_NAND_ReadPage(uint8_t *_pBuffer, uint32_t _ulPageNo, uint16_t _usAddrInPage, uint16_t _usByteCount);
  109. static uint8_t FSMC_NAND_WritePage(uint8_t *_pBuffer, uint32_t _ulPageNo, uint16_t _usAddrInPage, uint16_t _usByteCount);
  110. static uint8_t FSMC_NAND_CompPage(uint8_t *_pBuffer, uint32_t _ulPageNo, uint16_t _usAddrInPage, uint16_t _usByteCount);
  111. static uint8_t FSMC_NAND_EraseBlock(uint32_t _ulBlockNo);
  112. /*
  113. *********************************************************************************************************
  114. * 函 数 名: FSMC_NAND_Init
  115. * 功能说明: 配置FSMC和GPIO用于NAND Flash接口。这个函数必须在读写nand flash前被调用一次。
  116. * 形 参: 无
  117. * 返 回 值: 无
  118. *********************************************************************************************************
  119. */
  120. /*
  121. FSMC_NWAIT PD6
  122. FSMC_NOE PD4
  123. FSMC_NCE2 PD7
  124. FSMC_A16 PD11
  125. FSMC_A17 PD12
  126. FSMC_NWE PD5
  127. FSMC_D0 PD14
  128. FSMC_D1 PD15
  129. FSMC_D2 PD0
  130. FSMC_D3 PD1
  131. FSMC_D4 PE7
  132. FSMC_D5 PE8
  133. FSMC_D6 PE9
  134. FSMC_D7 PE10
  135. */
  136. static void FSMC_NAND_Init(void)
  137. {
  138. GPIO_InitTypeDef GPIO_InitStructure;
  139. FSMC_NANDInitTypeDef FSMC_NANDInitStructure;
  140. FSMC_NAND_PCCARDTimingInitTypeDef FSMC_NANDTimingInitStructure;
  141. // 使能FSMC时钟
  142. RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
  143. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE, ENABLE);
  144. // 配置PD和PE的复用功能
  145. GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FSMC);
  146. GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FSMC);
  147. GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_FSMC);
  148. GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_FSMC);
  149. GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_FSMC);
  150. GPIO_PinAFConfig(GPIOD, GPIO_PinSource7, GPIO_AF_FSMC);
  151. GPIO_PinAFConfig(GPIOD, GPIO_PinSource11, GPIO_AF_FSMC);
  152. GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_FSMC);
  153. GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_FSMC);
  154. GPIO_PinAFConfig(GPIOE, GPIO_PinSource8, GPIO_AF_FSMC);
  155. GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_FSMC);
  156. GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_FSMC);
  157. /* GPIO 配置 */
  158. /* 控制线CLE, ALE, D0-D3, NOE, NWE 和 NCE2 NAND 引脚配置为复用功能(即用于FSMC) */
  159. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_14 | GPIO_Pin_15 |
  160. GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
  161. GPIO_Pin_7;
  162. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  163. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  164. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  165. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  166. GPIO_Init(GPIOD, &GPIO_InitStructure);
  167. /* 数据线 D4-D7 引脚配置为复用功能 */
  168. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
  169. GPIO_Init(GPIOE, &GPIO_InitStructure);
  170. /* NWAIT 引脚配置*/
  171. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  172. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  173. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  174. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  175. GPIO_Init(GPIOD, &GPIO_InitStructure);
  176. //0、1、1、1
  177. // 配置FSMC NAND Flash的时序参数
  178. FSMC_NANDTimingInitStructure.FSMC_SetupTime = 0x1;
  179. FSMC_NANDTimingInitStructure.FSMC_WaitSetupTime = 0x2;
  180. FSMC_NANDTimingInitStructure.FSMC_HoldSetupTime = 0x2;
  181. FSMC_NANDTimingInitStructure.FSMC_HiZSetupTime = 0x2;
  182. // 配置FSMC NAND Flash的控制参数
  183. FSMC_NANDInitStructure.FSMC_Bank = FSMC_Bank2_NAND;
  184. FSMC_NANDInitStructure.FSMC_Waitfeature = FSMC_Waitfeature_Disable;
  185. FSMC_NANDInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
  186. FSMC_NANDInitStructure.FSMC_ECC = FSMC_ECC_Enable;
  187. FSMC_NANDInitStructure.FSMC_ECCPageSize = FSMC_ECCPageSize_1024Bytes;
  188. FSMC_NANDInitStructure.FSMC_TCLRSetupTime = 0x0;
  189. FSMC_NANDInitStructure.FSMC_TARSetupTime = 0x0;
  190. FSMC_NANDInitStructure.FSMC_CommonSpaceTimingStruct = &FSMC_NANDTimingInitStructure;
  191. FSMC_NANDInitStructure.FSMC_AttributeSpaceTimingStruct = &FSMC_NANDTimingInitStructure;
  192. // 初始化FSMC NAND Flash
  193. FSMC_NANDInit(&FSMC_NANDInitStructure);
  194. // 使能FSMC NAND Flash
  195. FSMC_NANDCmd(FSMC_Bank2_NAND, ENABLE);
  196. }
  197. /*
  198. *********************************************************************************************************
  199. * 函 数 名: NAND_ReadID
  200. * 功能说明: 读NAND Flash的ID。ID存储到形参指定的结构体变量中。
  201. * 形 参: 无
  202. * 返 回 值: 32bit的NAND Flash ID
  203. *********************************************************************************************************
  204. */
  205. uint32_t NAND_ReadID(void)
  206. {
  207. uint32_t data = 0;
  208. /* 发送命令 Command to the command area */
  209. NAND_CMD_AREA = 0x90;
  210. NAND_ADDR_AREA = 0x00;
  211. /* 顺序读取NAND Flash的ID */
  212. data = *(__IO uint32_t *)(Bank_NAND_ADDR | DATA_AREA);
  213. data = ((data << 24) & 0xFF000000) |
  214. ((data << 8 ) & 0x00FF0000) |
  215. ((data >> 8 ) & 0x0000FF00) |
  216. ((data >> 24) & 0x000000FF) ;
  217. return data;
  218. }
  219. /*
  220. *********************************************************************************************************
  221. * 函 数 名: FSMC_NAND_PageCopyBack
  222. * 功能说明: 将一页数据复制到另外一个页。源页和目标页所在的block必须同为偶数或同为奇数。即A18必须相同。
  223. * 形 参: - _ulSrcPageNo: 源页号
  224. * - _ulTarPageNo: 目标页号
  225. * 返 回 值: 执行结果:
  226. * - NAND_FAIL 表示失败
  227. * - NAND_OK 表示成功
  228. *
  229. * 说 明:数据手册推荐:在页复制之前,先校验源页的位校验,否则可能会积累位错误。本函数未实现。
  230. *
  231. *********************************************************************************************************
  232. */
  233. static uint8_t FSMC_NAND_PageCopyBack(uint32_t _ulSrcPageNo, uint32_t _ulTarPageNo)
  234. {
  235. uint8_t i;
  236. if ((_ulSrcPageNo & 0x40) != (_ulTarPageNo & 0x40))
  237. {
  238. printf_err("Error : FSMC_NAND_PageCopyBackEx(src=%d, tar=%d) \r\n", _ulSrcPageNo, _ulTarPageNo);
  239. return NAND_FAIL;
  240. }
  241. NAND_CMD_AREA = NAND_CMD_COPYBACK_A;
  242. /* 发送源页地址 , 对于 HY27UF081G2A
  243. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  244. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  245. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  246. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  247. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  248. H27U4G8F2DTR (512MB)
  249. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  250. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  251. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  252. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  253. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  254. 第5字节: A28 A29 A30 A31 0 0 0 0
  255. */
  256. NAND_ADDR_AREA = 0;
  257. NAND_ADDR_AREA = 0;
  258. NAND_ADDR_AREA = _ulSrcPageNo;
  259. NAND_ADDR_AREA = (_ulSrcPageNo & 0xFF00) >> 8;
  260. #if NAND_ADDR_5 == 1
  261. NAND_ADDR_AREA = (_ulSrcPageNo & 0xFF0000) >> 16;
  262. #endif
  263. NAND_CMD_AREA = NAND_CMD_COPYBACK_B;
  264. /* 必须等待,否则读出数据异常, 此处应该判断超时 */
  265. for (i = 0; i < 20; i++);
  266. while( GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6) == 0 );
  267. NAND_CMD_AREA = NAND_CMD_COPYBACK_C;
  268. /* 发送目标页地址 , 对于 HY27UF081G2A
  269. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  270. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  271. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  272. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  273. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  274. H27U4G8F2DTR (512MB)
  275. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  276. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  277. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  278. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12 --- A18 是plane地址
  279. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  280. 第5字节: A28 A29 A30 A31 0 0 0 0
  281. Source and Destination page in the copy back program sequence must belong to the same device plane (A18)
  282. 源地址和目标地址的 A18必须相同
  283. */
  284. NAND_ADDR_AREA = 0;
  285. NAND_ADDR_AREA = 0;
  286. NAND_ADDR_AREA = _ulTarPageNo;
  287. NAND_ADDR_AREA = (_ulTarPageNo & 0xFF00) >> 8;
  288. #if NAND_ADDR_5 == 1
  289. NAND_ADDR_AREA = (_ulTarPageNo & 0xFF0000) >> 16;
  290. #endif
  291. NAND_CMD_AREA = NAND_CMD_COPYBACK_D;
  292. /* 检查操作状态 */
  293. if (FSMC_NAND_GetStatus() == NAND_READY)
  294. {
  295. return NAND_OK;
  296. }
  297. printf_err("Error: FSMC_NAND_PageCopyBack(%d, %d)\r\n", _ulSrcPageNo, _ulTarPageNo);
  298. return NAND_FAIL;
  299. }
  300. /*
  301. *********************************************************************************************************
  302. * 函 数 名: FSMC_NAND_PageCopyBackEx
  303. * 功能说明: 将一页数据复制到另外一个页,并更新目标页中的部分数据。源页和目标页所在的BLOCK必须同为偶数或同为奇数。
  304. * 形 参: - _ulSrcPageNo: 源页号
  305. * - _ulTarPageNo: 目标页号
  306. * - _usOffset: 页内偏移地址,pBuf的内容将写入这个地址开始单元
  307. * - _pBuf: 数据缓冲区
  308. * - _usSize: 数据大小
  309. * 返 回 值: 执行结果:
  310. * - NAND_FAIL 表示失败
  311. * - NAND_OK 表示成功
  312. *
  313. * 说 明:数据手册推荐:在页复制之前,先校验源页的位校验,否则可能会积累位错误。本函数未实现。
  314. *
  315. *********************************************************************************************************
  316. */
  317. static uint8_t FSMC_NAND_PageCopyBackEx(uint32_t _ulSrcPageNo, uint32_t _ulTarPageNo, uint8_t *_pBuf,
  318. uint16_t _usOffset, uint16_t _usSize)
  319. {
  320. uint16_t i;
  321. if ((_ulSrcPageNo & 0x40) != (_ulTarPageNo & 0x40))
  322. {
  323. printf_err("Error A18 not same: FSMC_NAND_PageCopyBackEx(src=%d, tar=%d) \r\n", _ulSrcPageNo, _ulTarPageNo);
  324. return NAND_FAIL;
  325. }
  326. NAND_CMD_AREA = NAND_CMD_COPYBACK_A;
  327. /* 发送源页地址 , 对于 HY27UF081G2A
  328. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  329. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  330. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  331. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  332. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  333. H27U4G8F2DTR (512MB)
  334. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  335. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  336. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  337. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  338. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  339. 第5字节: A28 A29 A30 A31 0 0 0 0
  340. */
  341. NAND_ADDR_AREA = 0;
  342. NAND_ADDR_AREA = 0;
  343. NAND_ADDR_AREA = _ulSrcPageNo;
  344. NAND_ADDR_AREA = (_ulSrcPageNo & 0xFF00) >> 8;
  345. #if NAND_ADDR_5 == 1
  346. NAND_ADDR_AREA = (_ulSrcPageNo & 0xFF0000) >> 16;
  347. #endif
  348. NAND_CMD_AREA = NAND_CMD_COPYBACK_B;
  349. /* 必须等待,否则读出数据异常, 此处应该判断超时 */
  350. for (i = 0; i < 20; i++);
  351. while( GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6) == 0 );
  352. NAND_CMD_AREA = NAND_CMD_COPYBACK_C;
  353. /* 发送目标页地址 , 对于 HY27UF081G2A
  354. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  355. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  356. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  357. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  358. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  359. H27U4G8F2DTR (512MB)
  360. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  361. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  362. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  363. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  364. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  365. 第5字节: A28 A29 A30 A31 0 0 0 0
  366. */
  367. NAND_ADDR_AREA = 0;
  368. NAND_ADDR_AREA = 0;
  369. NAND_ADDR_AREA = _ulTarPageNo;
  370. NAND_ADDR_AREA = (_ulTarPageNo & 0xFF00) >> 8;
  371. #if NAND_ADDR_5 == 1
  372. NAND_ADDR_AREA = (_ulTarPageNo & 0xFF0000) >> 16;
  373. #endif
  374. /* 中间无需带数据, 也无需等待 */
  375. NAND_CMD_AREA = NAND_CMD_COPYBACK_C;
  376. NAND_ADDR_AREA = _usOffset;
  377. NAND_ADDR_AREA = _usOffset >> 8;
  378. /* 发送数据 */
  379. for(i = 0; i < _usSize; i++)
  380. {
  381. NAND_DATA_AREA = _pBuf[i];
  382. }
  383. NAND_CMD_AREA = NAND_CMD_COPYBACK_D;
  384. /* 检查操作状态 */
  385. if (FSMC_NAND_GetStatus() == NAND_READY)
  386. {
  387. return NAND_OK;
  388. }
  389. printf_err("Error: FSMC_NAND_PageCopyBackEx(src=%d, tar=%d, offset=%d, size=%d)\r\n",
  390. _ulSrcPageNo, _ulTarPageNo, _usOffset, _usSize);
  391. return NAND_FAIL;
  392. }
  393. /*
  394. *********************************************************************************************************
  395. * 函 数 名: FSMC_NAND_WritePage
  396. * 功能说明: 写一组数据至NandFlash指定页面的指定位置,写入的数据长度不大于一页的大小。
  397. * 形 参: - _pBuffer: 指向包含待写数据的缓冲区
  398. * - _ulPageNo: 页号,所有的页统一编码,范围为:0 - 65535
  399. * - _usAddrInPage : 页内地址,范围为:0-2111
  400. * - _usByteCount: 写入的字节个数
  401. * 返 回 值: 执行结果:
  402. * - NAND_FAIL 表示失败
  403. * - NAND_OK 表示成功
  404. *********************************************************************************************************
  405. */
  406. static uint8_t FSMC_NAND_WritePage(uint8_t *_pBuffer, uint32_t _ulPageNo, uint16_t _usAddrInPage, uint16_t _usByteCount)
  407. {
  408. uint16_t i;
  409. /* 发送页写命令 */
  410. NAND_CMD_AREA = NAND_CMD_WRITE0;
  411. /* 发送页内地址 , 对于 HY27UF081G2A
  412. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  413. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  414. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  415. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  416. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  417. H27U4G8F2DTR (512MB)
  418. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  419. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  420. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  421. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  422. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  423. 第5字节: A28 A29 A30 A31 0 0 0 0
  424. */
  425. NAND_ADDR_AREA = _usAddrInPage;
  426. NAND_ADDR_AREA = _usAddrInPage >> 8;
  427. NAND_ADDR_AREA = _ulPageNo;
  428. NAND_ADDR_AREA = (_ulPageNo & 0xFF00) >> 8;
  429. #if NAND_ADDR_5 == 1
  430. NAND_ADDR_AREA = (_ulPageNo & 0xFF0000) >> 16;
  431. #endif
  432. /* tADL = 100ns, Address to Data Loading */
  433. for (i = 0; i < 20; i++); /* 需要大于 100ns */
  434. /* 写数据 */
  435. for(i = 0; i < _usByteCount; i++)
  436. {
  437. NAND_DATA_AREA = _pBuffer[i];
  438. }
  439. NAND_CMD_AREA = NAND_CMD_WRITE_TRUE1;
  440. /* WE High to Busy , 100ns */
  441. for (i = 0; i < 20; i++); /* 需要大于 100ns */
  442. while( GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6) == 0 );
  443. /* 检查操作状态 */
  444. if (FSMC_NAND_GetStatus() == NAND_READY)
  445. {
  446. /* 读出数据进行校验 */
  447. #ifdef WRITE_PAGE_VERIFY_EN
  448. FSMC_NAND_ReadPage (s_ucTempBuf, _ulPageNo, _usAddrInPage, _usByteCount);
  449. if (memcmp(s_ucTempBuf, _pBuffer, _usByteCount) != 0)
  450. {
  451. printf_err("Error1: FSMC_NAND_WritePage(page=%d, addr=%d, count=%d)\r\n",
  452. _ulPageNo, _usAddrInPage, _usByteCount);
  453. return NAND_FAIL;
  454. }
  455. #endif
  456. return NAND_OK;
  457. }
  458. printf_err("Error2: FSMC_NAND_WritePage(page=%d, addr=%d, count=%d)\r\n",
  459. _ulPageNo, _usAddrInPage, _usByteCount);
  460. return NAND_FAIL;
  461. }
  462. /*
  463. *********************************************************************************************************
  464. * 函 数 名: FSMC_NAND_ReadPage
  465. * 功能说明: 从NandFlash指定页面的指定位置读一组数据,读出的数据长度不大于一页的大小。
  466. * 形 参: - _pBuffer: 指向包含待写数据的缓冲区
  467. * - _ulPageNo: 页号,所有的页统一编码,范围为:0 - 65535
  468. * - _usAddrInPage : 页内地址,范围为:0-2111
  469. * - _usByteCount: 字节个数, (最大 2048 + 64)
  470. * 返 回 值: 执行结果:
  471. * - NAND_FAIL 表示失败
  472. * - NAND_OK 表示成功
  473. *********************************************************************************************************
  474. */
  475. static uint8_t FSMC_NAND_ReadPage(uint8_t *_pBuffer, uint32_t _ulPageNo, uint16_t _usAddrInPage, uint16_t _usByteCount)
  476. {
  477. uint16_t i;
  478. /* 发送页面读命令 */
  479. NAND_CMD_AREA = NAND_CMD_AREA_A;
  480. /* 发送页内地址 , 对于 HY27UF081G2A (128MB)
  481. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  482. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  483. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  484. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  485. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  486. H27U4G8F2DTR (512MB)
  487. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  488. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  489. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  490. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  491. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  492. 第5字节: A28 A29 A30 A31 0 0 0 0
  493. */
  494. NAND_ADDR_AREA = _usAddrInPage;
  495. NAND_ADDR_AREA = _usAddrInPage >> 8;
  496. NAND_ADDR_AREA = _ulPageNo;
  497. NAND_ADDR_AREA = (_ulPageNo & 0xFF00) >> 8;
  498. #if NAND_ADDR_5 == 1
  499. NAND_ADDR_AREA = (_ulPageNo & 0xFF0000) >> 16;
  500. #endif
  501. NAND_CMD_AREA = NAND_CMD_AREA_TRUE1;
  502. /* 必须等待,否则读出数据异常, 此处应该判断超时 */
  503. for (i = 0; i < 50; i++);
  504. while( GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6) == 0);
  505. /* 读数据到缓冲区pBuffer */
  506. for(i = 0; i < _usByteCount; i++)
  507. {
  508. _pBuffer[i] = NAND_DATA_AREA;
  509. }
  510. return NAND_OK;
  511. }
  512. /*
  513. *********************************************************************************************************
  514. * 函 数 名: FSMC_NAND_CompPage
  515. * 功能说明: 比较数据
  516. * 形 参: - _pBuffer: 指向包含待比较的数据缓冲区
  517. * - _ulPageNo: 页号,所有的页统一编码,范围为:0 - 65535
  518. * - _usAddrInPage : 页内地址,范围为:0-2111
  519. * - _usByteCount: 字节个数, (最大 2048 + 64)
  520. * 返 回 值: 执行结果:
  521. * - NAND_FAIL 表示失败
  522. * - NAND_OK 表示成功, 相等
  523. *********************************************************************************************************
  524. */
  525. static uint8_t FSMC_NAND_CompPage(uint8_t *_pBuffer, uint32_t _ulPageNo, uint16_t _usAddrInPage, uint16_t _usByteCount)
  526. {
  527. uint16_t i;
  528. /* 发送页面读命令 */
  529. NAND_CMD_AREA = NAND_CMD_AREA_A;
  530. /* 发送页内地址 , 对于 HY27UF081G2A (128MB)
  531. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  532. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  533. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  534. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  535. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  536. H27U4G8F2DTR (512MB)
  537. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  538. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  539. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  540. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12
  541. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  542. 第5字节: A28 A29 A30 A31 0 0 0 0
  543. */
  544. NAND_ADDR_AREA = _usAddrInPage;
  545. NAND_ADDR_AREA = _usAddrInPage >> 8;
  546. NAND_ADDR_AREA = _ulPageNo;
  547. NAND_ADDR_AREA = (_ulPageNo & 0xFF00) >> 8;
  548. #if NAND_ADDR_5 == 1
  549. NAND_ADDR_AREA = (_ulPageNo & 0xFF0000) >> 16;
  550. #endif
  551. NAND_CMD_AREA = NAND_CMD_AREA_TRUE1;
  552. /* 必须等待,否则读出数据异常, 此处应该判断超时 */
  553. for (i = 0; i < 20; i++);
  554. while( GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6) == 0);
  555. /* 读数据到缓冲区pBuffer */
  556. for(i = 0; i < _usByteCount; i++)
  557. {
  558. if (_pBuffer[i] != NAND_DATA_AREA)
  559. {
  560. return NAND_FAIL;
  561. }
  562. }
  563. return NAND_OK;
  564. }
  565. /*
  566. *********************************************************************************************************
  567. * 函 数 名: FSMC_NAND_WriteSpare
  568. * 功能说明: 向1个PAGE的Spare区写入数据
  569. * 形 参: - _pBuffer: 指向包含待写数据的缓冲区
  570. * - _ulPageNo: 页号,所有的页统一编码,范围为:0 - 65535
  571. * - _usAddrInSpare : 页内备用区的偏移地址,范围为:0-63
  572. * - _usByteCount: 写入的字节个数
  573. * 返 回 值: 执行结果:
  574. * - NAND_FAIL 表示失败
  575. * - NAND_OK 表示成功
  576. *********************************************************************************************************
  577. */
  578. static uint8_t FSMC_NAND_WriteSpare(uint8_t *_pBuffer, uint32_t _ulPageNo, uint16_t _usAddrInSpare, uint16_t _usByteCount)
  579. {
  580. if (_usByteCount > NAND_SPARE_AREA_SIZE)
  581. {
  582. printf_err("Error: FSMC_NAND_WriteSpare() %d\r\n",_usByteCount);
  583. return NAND_FAIL;
  584. }
  585. return FSMC_NAND_WritePage(_pBuffer, _ulPageNo, NAND_PAGE_SIZE + _usAddrInSpare, _usByteCount);
  586. }
  587. /*
  588. *********************************************************************************************************
  589. * 函 数 名: FSMC_NAND_ReadSpare
  590. * 功能说明: 读1个PAGE的Spare区的数据
  591. * 形 参: - _pBuffer: 指向包含待写数据的缓冲区
  592. * - _ulPageNo: 页号,所有的页统一编码,范围为:0 - 65535
  593. * - _usAddrInSpare : 页内备用区的偏移地址,范围为:0-63
  594. * - _usByteCount: 写入的字节个数
  595. * 返 回 值: 执行结果:
  596. * - NAND_FAIL 表示失败
  597. * - NAND_OK 表示成功
  598. *********************************************************************************************************
  599. */
  600. static uint8_t FSMC_NAND_ReadSpare(uint8_t *_pBuffer, uint32_t _ulPageNo, uint16_t _usAddrInSpare, uint16_t _usByteCount)
  601. {
  602. if (_usByteCount > NAND_SPARE_AREA_SIZE)
  603. {
  604. printf_err("Error: FSMC_NAND_ReadSpare() %d\r\n",_usByteCount);
  605. return NAND_FAIL;
  606. }
  607. return FSMC_NAND_ReadPage(_pBuffer, _ulPageNo, NAND_PAGE_SIZE + _usAddrInSpare, _usByteCount);
  608. }
  609. /*
  610. *********************************************************************************************************
  611. * 函 数 名: FSMC_NAND_WriteData
  612. * 功能说明: 向1个PAGE的主数据区写入数据
  613. * 形 参: - _pBuffer: 指向包含待写数据的缓冲区
  614. * - _ulPageNo: 页号,所有的页统一编码,范围为:0 - 65535
  615. * - _usAddrInPage : 页内数据区的偏移地址,范围为:0-2047
  616. * - _usByteCount: 写入的字节个数
  617. * 返 回 值: 执行结果:
  618. * - NAND_FAIL 表示失败
  619. * - NAND_OK 表示成功
  620. *********************************************************************************************************
  621. */
  622. static uint8_t FSMC_NAND_WriteData(uint8_t *_pBuffer, uint32_t _ulPageNo, uint16_t _usAddrInPage, uint16_t _usByteCount)
  623. {
  624. if (_usByteCount > NAND_PAGE_SIZE)
  625. {
  626. printf_err("Error: FSMC_NAND_WriteData() %d\r\n",_usByteCount);
  627. return NAND_FAIL;
  628. }
  629. return FSMC_NAND_WritePage(_pBuffer, _ulPageNo, _usAddrInPage, _usByteCount);
  630. }
  631. /*
  632. *********************************************************************************************************
  633. * 函 数 名: FSMC_NAND_ReadData
  634. * 功能说明: 读1个PAGE的主数据的数据
  635. * 形 参: - _pBuffer: 指向包含待写数据的缓冲区
  636. * - _ulPageNo: 页号,所有的页统一编码,范围为:0 - 65535
  637. * - _usAddrInPage : 页内数据区的偏移地址,范围为:0-2047
  638. * - _usByteCount: 写入的字节个数
  639. * 返 回 值: 执行结果:
  640. * - NAND_FAIL 表示失败
  641. * - NAND_OK 表示成功
  642. *********************************************************************************************************
  643. */
  644. static uint8_t FSMC_NAND_ReadData(uint8_t *_pBuffer, uint32_t _ulPageNo, uint16_t _usAddrInPage, uint16_t _usByteCount)
  645. {
  646. if (_usByteCount > NAND_PAGE_SIZE)
  647. {
  648. printf_err("Error: FSMC_NAND_ReadData() %d\r\n",_usByteCount);
  649. return NAND_FAIL;
  650. }
  651. return FSMC_NAND_ReadPage(_pBuffer, _ulPageNo, _usAddrInPage, _usByteCount);
  652. }
  653. /*
  654. *********************************************************************************************************
  655. * 函 数 名: FSMC_NAND_EraseBlock
  656. * 功能说明: 擦除NAND Flash一个块(block)
  657. * 形 参: - _ulBlockNo: 块号,范围为:0 - 1023, 0-4095
  658. * 返 回 值: NAND操作状态,有如下几种值:
  659. * - NAND_TIMEOUT_ERROR : 超时错误
  660. * - NAND_READY : 操作成功
  661. *********************************************************************************************************
  662. */
  663. static uint8_t FSMC_NAND_EraseBlock(uint32_t _ulBlockNo)
  664. {
  665. /* HY27UF081G2A (128MB)
  666. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  667. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  668. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  669. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12 A18以上是块号
  670. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  671. H27U4G8F2DTR (512MB)
  672. Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
  673. 第1字节: A7 A6 A5 A4 A3 A2 A1 A0 (_usPageAddr 的bit7 - bit0)
  674. 第2字节: 0 0 0 0 A11 A10 A9 A8 (_usPageAddr 的bit11 - bit8, 高4bit必须是0)
  675. 第3字节: A19 A18 A17 A16 A15 A14 A13 A12 A18以上是块号
  676. 第4字节: A27 A26 A25 A24 A23 A22 A21 A20
  677. 第5字节: A28 A29 A30 A31 0 0 0 0
  678. */
  679. /* 发送擦除命令 */
  680. NAND_CMD_AREA = NAND_CMD_ERASE0;
  681. _ulBlockNo <<= 6; /* 块号转换为页编号 */
  682. #if NAND_ADDR_5 == 0 /* 128MB的 */
  683. NAND_ADDR_AREA = _ulBlockNo;
  684. NAND_ADDR_AREA = _ulBlockNo >> 8;
  685. #else /* 512MB的 */
  686. NAND_ADDR_AREA = _ulBlockNo;
  687. NAND_ADDR_AREA = _ulBlockNo >> 8;
  688. NAND_ADDR_AREA = _ulBlockNo >> 16;
  689. #endif
  690. NAND_CMD_AREA = NAND_CMD_ERASE1;
  691. return (FSMC_NAND_GetStatus());
  692. }
  693. /*
  694. *********************************************************************************************************
  695. * 函 数 名: FSMC_NAND_Reset
  696. * 功能说明: 复位NAND Flash
  697. * 形 参: 无
  698. * 返 回 值: 无
  699. *********************************************************************************************************
  700. */
  701. static uint8_t FSMC_NAND_Reset(void)
  702. {
  703. NAND_CMD_AREA = NAND_CMD_RESET;
  704. /* 检查操作状态 */
  705. if (FSMC_NAND_GetStatus() == NAND_READY)
  706. {
  707. return NAND_OK;
  708. }
  709. return NAND_FAIL;
  710. }
  711. /*
  712. *********************************************************************************************************
  713. * 函 数 名: FSMC_NAND_ReadStatus
  714. * 功能说明: 使用Read statuc 命令读NAND Flash内部状态
  715. * 形 参: - Address: 被擦除的快内任意地址
  716. * 返 回 值: NAND操作状态,有如下几种值:
  717. * - NAND_BUSY: 内部正忙
  718. * - NAND_READY: 内部空闲,可以进行下步操作
  719. * - NAND_ERROR: 先前的命令执行失败
  720. *********************************************************************************************************
  721. */
  722. static uint8_t FSMC_NAND_ReadStatus(void)
  723. {
  724. uint8_t ucData;
  725. uint8_t ucStatus = NAND_BUSY;
  726. /* 读状态操作 */
  727. NAND_CMD_AREA = NAND_CMD_STATUS;
  728. ucData = *(__IO uint8_t *)(Bank_NAND_ADDR);
  729. if((ucData & NAND_ERROR) == NAND_ERROR)
  730. {
  731. ucStatus = NAND_ERROR;
  732. }
  733. else if((ucData & NAND_READY) == NAND_READY)
  734. {
  735. ucStatus = NAND_READY;
  736. }
  737. else
  738. {
  739. ucStatus = NAND_BUSY;
  740. }
  741. return (ucStatus);
  742. }
  743. /*
  744. *********************************************************************************************************
  745. * 函 数 名: FSMC_NAND_GetStatus
  746. * 功能说明: 获取NAND Flash操作状态
  747. * 形 参: - Address: 被擦除的快内任意地址
  748. * 返 回 值: NAND操作状态,有如下几种值:
  749. * - NAND_TIMEOUT_ERROR : 超时错误
  750. * - NAND_READY : 操作成功
  751. *********************************************************************************************************
  752. */
  753. static uint8_t FSMC_NAND_GetStatus(void)
  754. {
  755. uint32_t ulTimeout = 0x10000;
  756. uint8_t ucStatus = NAND_READY;
  757. ucStatus = FSMC_NAND_ReadStatus();
  758. /* 等待NAND操作结束,超时后会退出 */
  759. while ((ucStatus != NAND_READY) &&( ulTimeout != 0x00))
  760. {
  761. ucStatus = FSMC_NAND_ReadStatus();
  762. ulTimeout--;
  763. }
  764. if(ulTimeout == 0x00)
  765. {
  766. ucStatus = NAND_TIMEOUT_ERROR;
  767. }
  768. /* 返回操作状态 */
  769. return (ucStatus);
  770. }
  771. /*
  772. *********************************************************************************************************
  773. * 函 数 名: NAND_Init
  774. * 功能说明: 初始化NAND Flash接口
  775. * 形 参: 无
  776. * 返 回 值: 执行结果:
  777. * - NAND_FAIL 表示失败
  778. * - NAND_OK 表示成功
  779. *********************************************************************************************************
  780. */
  781. uint8_t NAND_Init(void)
  782. {
  783. uint8_t Status;
  784. FSMC_NAND_Init(); /* 配置FSMC和GPIO用于NAND Flash接口 */
  785. FSMC_NAND_Reset(); /* 通过复位命令复位NAND Flash到读状态 */
  786. Status = NAND_BuildLUT(); /* 建立块管理表 LUT = Look up table */
  787. if(Status==NAND_FAIL) //其可能导致的原因是nandflash没有初始化进行格式化
  788. {
  789. NAND_Format();
  790. NAND_ReadID();
  791. }
  792. return Status;
  793. }
  794. /*
  795. *********************************************************************************************************
  796. * 函 数 名: NAND_WriteToNewBlock
  797. * 功能说明: 将旧块的数据复制到新块,并将新的数据段写入这个新块.
  798. * 形 参: _ulPhyPageNo : 源页号
  799. * _pWriteBuf : 数据缓冲区
  800. * _usOffset : 页内偏移地址
  801. * _usSize :数据长度,必须是4字节的整数倍
  802. * 返 回 值: 执行结果:
  803. * - NAND_FAIL 表示失败
  804. * - NAND_OK 表示成功
  805. *********************************************************************************************************
  806. */
  807. uint8_t NAND_WriteToNewBlock(uint32_t _ulPhyPageNo, uint8_t *_pWriteBuf, uint16_t _usOffset, uint16_t _usSize)
  808. {
  809. uint16_t n, i;
  810. uint16_t usNewBlock;
  811. uint16_t ulSrcBlock;
  812. uint16_t usOffsetPageNo;
  813. ulSrcBlock = _ulPhyPageNo / NAND_BLOCK_SIZE; /* 根据物理页号反推块号 */
  814. usOffsetPageNo = _ulPhyPageNo % NAND_BLOCK_SIZE; /* 根据物理页号计算物理页号在块内偏移页号 */
  815. /* 增加循环的目的是处理目标块为坏块的情况 */
  816. for (n = 0; n < 10; n++)
  817. {
  818. /* 如果不是全0xFF, 则需要寻找一个空闲可用块,并将页内的数据全部移到新块中,然后擦除这个块 */
  819. usNewBlock = NAND_FindFreeBlock(_ulPhyPageNo); /* 从最后一个Block开始,搜寻一个可用块. */
  820. if (usNewBlock >= NAND_BLOCK_COUNT)
  821. {
  822. printf_err("Error1: NAND_WriteToNewBlock() %d\r\n", usNewBlock);
  823. return NAND_FAIL; /* 查找空闲块失败 */
  824. }
  825. printf_ok("NAND_WriteToNewBlock(%d -> %d)\r\n", ulSrcBlock, usNewBlock);
  826. /* 使用page-copy功能,将当前块(usPBN)的数据全部搬移到新块(usNewBlock) */
  827. for (i = 0; i < NAND_BLOCK_SIZE; i++)
  828. {
  829. if (i == usOffsetPageNo)
  830. {
  831. /* 如果写入的数据在当前页,则需要使用带随机数据的Copy-Back命令 */
  832. if (FSMC_NAND_PageCopyBackEx(ulSrcBlock * NAND_BLOCK_SIZE + i, usNewBlock * NAND_BLOCK_SIZE + i,
  833. _pWriteBuf, _usOffset, _usSize) == NAND_FAIL)
  834. {
  835. printf_err("Error2: NAND_WriteToNewBlock() %d\r\n", ulSrcBlock);
  836. NAND_MarkBadBlock(usNewBlock); /* 将新块标记为坏块 */
  837. NAND_BuildLUT(); /* 重建LUT表 */
  838. break;
  839. }
  840. }
  841. else
  842. {
  843. /* 使用NAND Flash 提供的整页Copy-Back功能,可以显著提高操作效率 */
  844. if (FSMC_NAND_PageCopyBack(ulSrcBlock * NAND_BLOCK_SIZE + i,
  845. usNewBlock * NAND_BLOCK_SIZE + i) == NAND_FAIL)
  846. {
  847. printf_err("Error3: NAND_WriteToNewBlock() %d\r\n", ulSrcBlock);
  848. NAND_MarkBadBlock(usNewBlock); /* 将新块标记为坏块 */
  849. NAND_BuildLUT(); /* 重建LUT表 */
  850. break;
  851. }
  852. }
  853. }
  854. /* 目标块更新成功 */
  855. if (i == NAND_BLOCK_SIZE)
  856. {
  857. /* 标记新块为已用块 */
  858. if (NAND_MarkUsedBlock(usNewBlock) == NAND_FAIL)
  859. {
  860. NAND_MarkBadBlock(usNewBlock); /* 将新块标记为坏块 */
  861. NAND_BuildLUT(); /* 重建LUT表 */
  862. continue;
  863. }
  864. /* 擦除源BLOCK (如果源块写失败,则会擦除坏块标记) */
  865. if (FSMC_NAND_EraseBlock(ulSrcBlock) != NAND_READY)
  866. {
  867. printf_err("Error4: FSMC_NAND_EraseBlock(), %d\r\n", ulSrcBlock);
  868. NAND_MarkBadBlock(ulSrcBlock); /* 将源块标记为坏块 */
  869. NAND_BuildLUT(); /* 重建LUT表 */
  870. continue;
  871. }
  872. NAND_BuildLUT(); /* 重建LUT表 */
  873. break;
  874. }
  875. }
  876. if (n == 10)
  877. {
  878. printf_err("Error5: FSMC_NAND_EraseBlock() n=%d\r\n", n);
  879. return NAND_FAIL;
  880. }
  881. return NAND_OK; /* 写入成功 */
  882. }
  883. /*
  884. *********************************************************************************************************
  885. * 函 数 名: NAND_Write
  886. * 功能说明: 写一个扇区
  887. * 形 参: _MemAddr : 内存单元偏移地址
  888. * _pReadbuff :存放待写数据的缓冲区的指针
  889. * _usSize :数据长度,必须是4字节的整数倍
  890. * 返 回 值: 执行结果:
  891. * - NAND_FAIL 表示失败
  892. * - NAND_OK 表示成功
  893. ******************************************s***************************************************************
  894. */
  895. uint8_t NAND_Write(uint32_t _ulMemAddr, uint32_t *_pWriteBuf, uint16_t _usSize)
  896. {
  897. uint16_t usPBN; /* 物理块号 */
  898. uint32_t ulPhyPageNo; /* 物理页号 */
  899. uint16_t usAddrInPage; /* 页内偏移地址 */
  900. uint32_t ulTemp;
  901. /* 数据长度必须是4字节整数倍 */
  902. if ((_usSize % 4) != 0)
  903. {
  904. printf_err("Error:1 NAND_Write() %d\r\n",_usSize);
  905. return NAND_FAIL;
  906. }
  907. /* 数据长度不能超过512字节(遵循 Fat格式) */
  908. if (_usSize > 512)
  909. {
  910. printf_err("Error:2 NAND_Write() %d\r\n",_usSize);
  911. //return NAND_FAIL;
  912. }
  913. usPBN = NAND_AddrToPhyBlockNo(_ulMemAddr); /* 查询LUT表获得物理块号 */
  914. ulTemp = _ulMemAddr % (NAND_BLOCK_SIZE * NAND_PAGE_SIZE);
  915. ulPhyPageNo = usPBN * NAND_BLOCK_SIZE + ulTemp / NAND_PAGE_SIZE; /* 计算物理页号 */
  916. usAddrInPage = ulTemp % NAND_PAGE_SIZE; /* 计算页内偏移地址 */
  917. /* 读出扇区的内容,判断是否全FF */
  918. if (FSMC_NAND_ReadData(s_ucTempBuf, ulPhyPageNo, usAddrInPage, _usSize) == NAND_FAIL)
  919. {
  920. return NAND_FAIL; /* 读NAND Flash失败 */
  921. }
  922. /* 如果是全0xFF, 则可以直接写入,无需擦除 */
  923. if (NAND_IsBufOk(s_ucTempBuf, _usSize, 0xFF) == 1)
  924. {
  925. if (FSMC_NAND_WriteData((uint8_t *)_pWriteBuf, ulPhyPageNo, usAddrInPage, _usSize) == NAND_FAIL)
  926. {
  927. /* 将数据写入到另外一个块(空闲块) */
  928. return NAND_WriteToNewBlock(ulPhyPageNo, (uint8_t *)_pWriteBuf, usAddrInPage, _usSize);
  929. }
  930. /* 标记该块已用 */
  931. if (NAND_MarkUsedBlock(ulPhyPageNo / NAND_BLOCK_SIZE) == NAND_FAIL)
  932. {
  933. /* 标记失败,将数据写入到另外一个块(空闲块) */
  934. return NAND_WriteToNewBlock(ulPhyPageNo, (uint8_t *)_pWriteBuf, usAddrInPage, _usSize);
  935. }
  936. return NAND_OK; /* 写入成功 */
  937. }
  938. /* 将数据写入到另外一个块(空闲块) */
  939. return NAND_WriteToNewBlock(ulPhyPageNo, (uint8_t *)_pWriteBuf, usAddrInPage, _usSize);
  940. }
  941. /*
  942. *********************************************************************************************************
  943. * 函 数 名: NAND_Read
  944. * 功能说明: 读一个扇区
  945. * 形 参: _MemAddr : 内存单元偏移地址
  946. * _pReadbuff :存放读出数据的缓冲区的指针
  947. * _usSize :数据长度,必须是4字节的整数倍
  948. * 返 回 值: 执行结果:
  949. * - NAND_FAIL 表示失败
  950. * - NAND_OK 表示成功
  951. *********************************************************************************************************
  952. */
  953. uint8_t NAND_Read(uint32_t _ulMemAddr, uint32_t *_pReadBuf, uint16_t _usSize)
  954. {
  955. uint16_t usPBN; /* 物理块号 */
  956. uint32_t ulPhyPageNo; /* 物理页号 */
  957. uint16_t usAddrInPage; /* 页内偏移地址 */
  958. uint32_t ulTemp;
  959. /* 数据长度必须是4字节整数倍 */
  960. if ((_usSize % 4) != 0)
  961. {
  962. printf_err("Error:1 NAND_Read(_usSize) %d\r\n",_usSize);
  963. return NAND_FAIL;
  964. }
  965. usPBN = NAND_AddrToPhyBlockNo(_ulMemAddr); /* 查询LUT表获得物理块号 */
  966. if (usPBN >= NAND_BLOCK_COUNT)
  967. {
  968. /* 没有格式化,usPBN = 0xFFFF */
  969. printf_err("Error:1 NAND_Write() usPBN %d\r\n",usPBN);
  970. return NAND_FAIL;
  971. }
  972. ulTemp = _ulMemAddr % (NAND_BLOCK_SIZE * NAND_PAGE_SIZE);
  973. ulPhyPageNo = usPBN * NAND_BLOCK_SIZE + ulTemp / NAND_PAGE_SIZE; /* 计算物理页号 */
  974. usAddrInPage = ulTemp % NAND_PAGE_SIZE; /* 计算页内偏移地址 */
  975. if (FSMC_NAND_ReadData((uint8_t *)_pReadBuf, ulPhyPageNo, usAddrInPage, _usSize) == NAND_FAIL)
  976. {
  977. return NAND_FAIL; /* 读NAND Flash失败 */
  978. }
  979. /* 成功 */
  980. return NAND_OK;
  981. }
  982. /*
  983. *********************************************************************************************************
  984. * 函 数 名: NAND_WriteMultiSectors
  985. * 功能说明: 该函数用于文件系统,连续写多个扇区数据。扇区大小可以是512字节或2048字节
  986. * 形 参: _pBuf : 存放数据的缓冲区的指针
  987. * _SectorNo :扇区号
  988. * _SectorSize :每个扇区的大小 (一般是 512字节)
  989. * _SectorCount : 扇区个数
  990. * 返 回 值: 执行结果:
  991. * - NAND_FAIL 表示失败
  992. * - NAND_OK 表示成功
  993. *********************************************************************************************************
  994. */
  995. uint8_t NAND_WriteMultiSectors(uint8_t *_pBuf, uint32_t _SectorNo, uint16_t _SectorSize, uint32_t _SectorCount)
  996. {
  997. uint32_t i;
  998. uint32_t usLBN; /* 逻辑块号 */
  999. uint32_t usPBN; /* 物理块号 */
  1000. uint32_t uiPhyPageNo; /* 物理页号 */
  1001. uint16_t usAddrInPage; /* 页内偏移地址 */
  1002. uint32_t ulTemp;
  1003. uint8_t ucReturn;
  1004. /*
  1005. HY27UF081G2A = 128M Flash. 有 1024个BLOCK, 每个BLOCK包含64个PAGE, 每个PAGE包含2048+64字节,
  1006. 擦除最小单位是BLOCK, 编程最小单位是字节。
  1007. 每个PAGE在逻辑上可以分为4个512字节扇区。
  1008. */
  1009. for (i = 0; i < _SectorCount; i++)
  1010. {
  1011. /* 根据逻辑扇区号和扇区大小计算逻辑块号 */
  1012. //usLBN = (_SectorNo * _SectorSize) / (NAND_BLOCK_SIZE * NAND_PAGE_SIZE);
  1013. /* (_SectorNo * _SectorSize) 乘积可能大于32位,因此换下面这种写法 */
  1014. usLBN = (_SectorNo + i) / (NAND_BLOCK_SIZE * (NAND_PAGE_SIZE / _SectorSize));
  1015. usPBN = NAND_LBNtoPBN(usLBN); /* 查询LUT表获得物理块号 */
  1016. if (usPBN >= NAND_BLOCK_COUNT)
  1017. {
  1018. printf_err("Error1: NAND_WriteMultiSectors(), no format. usLBN=%d, usPBN=%d\r\n", usLBN, usPBN);
  1019. /* 没有格式化,usPBN = 0xFFFF */
  1020. return NAND_FAIL;
  1021. }
  1022. //ulTemp = ((uint64_t)(_SectorNo + i) * _SectorSize) % (NAND_BLOCK_SIZE * NAND_PAGE_SIZE);
  1023. ulTemp = ((_SectorNo + i) % (NAND_BLOCK_SIZE * (NAND_PAGE_SIZE / _SectorSize))) * _SectorSize;
  1024. uiPhyPageNo = usPBN * NAND_BLOCK_SIZE + ulTemp / NAND_PAGE_SIZE; /* 计算物理页号 */
  1025. usAddrInPage = ulTemp % NAND_PAGE_SIZE; /* 计算页内偏移地址 */
  1026. /* 如果 _SectorCount > 0, 并且是页面首地址,则可以进行优化 */
  1027. if (usAddrInPage == 0)
  1028. {
  1029. /* 暂未处理 */
  1030. }
  1031. memset(s_ucTempBuf, 0xFF, _SectorSize);
  1032. /* 如果是全0xFF, 则可以直接写入,无需擦除 */
  1033. //if (NAND_IsBufOk(s_ucTempBuf, _SectorSize, 0xFF) == 1)
  1034. if (FSMC_NAND_CompPage(s_ucTempBuf, uiPhyPageNo, usAddrInPage, _SectorSize) == NAND_OK)
  1035. {
  1036. if (FSMC_NAND_WriteData(&_pBuf[i * _SectorSize], uiPhyPageNo, usAddrInPage, _SectorSize) == NAND_FAIL)
  1037. {
  1038. printf_err("Error3: NAND_WriteMultiSectors(), Write Faile\r\n");
  1039. /* 将数据写入到另外一个块(空闲块) */
  1040. ucReturn = NAND_WriteToNewBlock(uiPhyPageNo, &_pBuf[i * _SectorSize], usAddrInPage, _SectorSize);
  1041. if (ucReturn != NAND_OK)
  1042. {
  1043. printf_err("Error4: NAND_WriteMultiSectors(), Write Faile\r\n");
  1044. return NAND_FAIL; /* 失败 */
  1045. }
  1046. /* 标记源块为坏块 */
  1047. NAND_MarkBadBlock(uiPhyPageNo / NAND_BLOCK_SIZE); /* 将源块标记为坏块 */
  1048. continue;
  1049. }
  1050. /* 标记该块已用 */
  1051. if (NAND_MarkUsedBlock(uiPhyPageNo / NAND_BLOCK_SIZE) == NAND_FAIL)
  1052. {
  1053. /* 标记失败,将数据写入到另外一个块(空闲块) */
  1054. ucReturn = NAND_WriteToNewBlock(uiPhyPageNo, &_pBuf[i * _SectorSize], usAddrInPage, _SectorSize);
  1055. if (ucReturn != NAND_OK)
  1056. {
  1057. return NAND_FAIL; /* 失败 */
  1058. }
  1059. continue;
  1060. }
  1061. }
  1062. else /* 目标区域已经有数据,不是全FF, 则直接将数据写入另外一个空闲块 */
  1063. {
  1064. /* 将数据写入到另外一个块(空闲块) */
  1065. ucReturn = NAND_WriteToNewBlock(uiPhyPageNo, &_pBuf[i * _SectorSize], usAddrInPage, _SectorSize);
  1066. if (ucReturn != NAND_OK)
  1067. {
  1068. printf_err("Error5: NAND_WriteMultiSectors(), Write Faile\r\n");
  1069. return NAND_FAIL; /* 失败 */
  1070. }
  1071. continue;
  1072. }
  1073. }
  1074. return NAND_OK; /* 成功 */
  1075. }
  1076. /*
  1077. *********************************************************************************************************
  1078. * 函 数 名: NAND_ReadMultiSectors
  1079. * 功能说明: 该函数用于文件系统,按扇区读数据。读1个或多个扇区,扇区大小可以是512字节或2048字节
  1080. * 形 参: _pBuf : 存放读出数据的缓冲区的指针
  1081. * _SectorNo :扇区号
  1082. * _SectorSize :每个扇区的大小
  1083. * _SectorCount : 扇区个数
  1084. * 返 回 值: 执行结果:
  1085. * - NAND_FAIL 表示失败
  1086. * - NAND_OK 表示成功
  1087. *********************************************************************************************************
  1088. */
  1089. uint8_t NAND_ReadMultiSectors(uint8_t *_pBuf, uint32_t _SectorNo, uint16_t _SectorSize, uint32_t _SectorCount)
  1090. {
  1091. uint32_t i;
  1092. uint32_t usLBN; /* 逻辑块号 */
  1093. uint32_t usPBN; /* 物理块号 */
  1094. uint32_t uiPhyPageNo; /* 物理页号 */
  1095. uint16_t usAddrInPage; /* 页内偏移地址 */
  1096. uint32_t ulTemp;
  1097. /*
  1098. HY27UF081G2A = 128M Flash. 有 1024个BLOCK, 每个BLOCK包含64个PAGE, 每个PAGE包含2048+64字节,
  1099. 擦除最小单位是BLOCK, 编程最小单位是字节。
  1100. 每个PAGE在逻辑上可以分为4个512字节扇区。
  1101. */
  1102. for (i = 0; i < _SectorCount; i++)
  1103. {
  1104. /* 根据逻辑扇区号和扇区大小计算逻辑块号 */
  1105. //usLBN = (_SectorNo * _SectorSize) / (NAND_BLOCK_SIZE * NAND_PAGE_SIZE);
  1106. /* (_SectorNo * _SectorSize) 乘积可能大于32位,因此换下面这种写法 */
  1107. usLBN = (_SectorNo + i) / (NAND_BLOCK_SIZE * (NAND_PAGE_SIZE / _SectorSize));
  1108. usPBN = NAND_LBNtoPBN(usLBN); /* 查询LUT表获得物理块号 */
  1109. if (usPBN >= NAND_BLOCK_COUNT)
  1110. {
  1111. printf_err("Error: NAND_ReadMultiSectors(), not format, usPBN = %d\r\n", usPBN);
  1112. /* 没有格式化,usPBN = 0xFFFF */
  1113. return NAND_FAIL;
  1114. }
  1115. ulTemp = ((uint64_t)(_SectorNo + i) * _SectorSize) % (NAND_BLOCK_SIZE * NAND_PAGE_SIZE);
  1116. uiPhyPageNo = usPBN * NAND_BLOCK_SIZE + ulTemp / NAND_PAGE_SIZE; /* 计算物理页号 */
  1117. usAddrInPage = ulTemp % NAND_PAGE_SIZE; /* 计算页内偏移地址 */
  1118. if (FSMC_NAND_ReadData((uint8_t *)&_pBuf[i * _SectorSize], uiPhyPageNo, usAddrInPage, _SectorSize) == NAND_FAIL)
  1119. {
  1120. printf_err("Error: NAND_ReadMultiSectors(), ReadData(page = %d, addr = %d)\r\n", uiPhyPageNo, usAddrInPage);
  1121. return NAND_FAIL; /* 读NAND Flash失败 */
  1122. }
  1123. }
  1124. /* 成功 */
  1125. return NAND_OK;
  1126. }
  1127. /*
  1128. *********************************************************************************************************
  1129. * 函 数 名: NAND_BuildLUT
  1130. * 功能说明: 在内存中创建坏块管理表
  1131. * 形 参: ZoneNbr :区号
  1132. * 返 回 值: NAND_OK: 成功; NAND_FAIL:失败
  1133. *********************************************************************************************************
  1134. */
  1135. static uint8_t NAND_BuildLUT(void)
  1136. {
  1137. uint16_t i;
  1138. uint8_t buf[VALID_SPARE_SIZE];
  1139. uint16_t usLBN; /* 逻辑块号 */
  1140. /* */
  1141. for (i = 0; i < NAND_BLOCK_COUNT; i++)
  1142. {
  1143. s_usLUT[i] = 0xFFFF; /* 填充无效值,用于重建LUT后,判断LUT是否合理 */
  1144. }
  1145. for (i = 0; i < NAND_BLOCK_COUNT; i++)
  1146. {
  1147. /* 读每个块的第1个PAGE,偏移地址为LBN0_OFFSET的数据 */
  1148. FSMC_NAND_ReadSpare(buf, i * NAND_BLOCK_SIZE, 0, VALID_SPARE_SIZE);
  1149. /* 如果是好块,则记录LBN0 LBN1 */
  1150. if (buf[BI_OFFSET] == 0xFF)
  1151. {
  1152. usLBN = buf[LBN0_OFFSET] + buf[LBN1_OFFSET] * 256; /* 计算读出的逻辑块号 */
  1153. if (usLBN < NAND_BLOCK_COUNT)
  1154. {
  1155. /* 如果已经登记过了,则判定为异常 */
  1156. if (s_usLUT[usLBN] != 0xFFFF)
  1157. {
  1158. return NAND_FAIL;
  1159. }
  1160. s_usLUT[usLBN] = i; /* 更新LUT表 */
  1161. }
  1162. }
  1163. }
  1164. /* LUT建立完毕,检查是否合理 */
  1165. for (i = 0; i < NAND_BLOCK_COUNT; i++)
  1166. {
  1167. if (s_usLUT[i] >= NAND_BLOCK_COUNT)
  1168. {
  1169. s_usValidDataBlockCount = i;
  1170. break;
  1171. }
  1172. }
  1173. if (s_usValidDataBlockCount < 100)
  1174. {
  1175. /* 错误: 最大的有效逻辑块号小于100。可能是没有格式化 */
  1176. return NAND_FAIL;
  1177. }
  1178. for (; i < s_usValidDataBlockCount; i++)
  1179. {
  1180. if (s_usLUT[i] != 0xFFFF)
  1181. {
  1182. return NAND_FAIL; /* 错误:LUT表逻辑块号存在跳跃现象,可能是没有格式化 */
  1183. }
  1184. }
  1185. /* 重建LUT正常 */
  1186. return NAND_OK;
  1187. }
  1188. /*
  1189. *********************************************************************************************************
  1190. * 函 数 名: NAND_AddrToPhyBlockNo
  1191. * 功能说明: 内存逻辑地址转换为物理块号
  1192. * 形 参: _ulMemAddr:逻辑内存地址
  1193. * 返 回 值: 物理页号, 如果是 0xFFFFFFFF 则表示错误
  1194. *********************************************************************************************************
  1195. */
  1196. static uint16_t NAND_AddrToPhyBlockNo(uint32_t _ulMemAddr)
  1197. {
  1198. uint16_t usLBN; /* 逻辑块号 */
  1199. uint16_t usPBN; /* 物理块号 */
  1200. usLBN = _ulMemAddr / (NAND_BLOCK_SIZE * NAND_PAGE_SIZE); /* 计算逻辑块号 */
  1201. /* 如果逻辑块号大于有效的数据块个数则固定返回0xFFFF, 调用该函数的代码应该检查出这种错误 */
  1202. if (usLBN >= s_usValidDataBlockCount)
  1203. {
  1204. return 0xFFFF;
  1205. }
  1206. /* 查询LUT表,获得物理块号 */
  1207. usPBN = s_usLUT[usLBN];
  1208. return usPBN;
  1209. }
  1210. /*
  1211. *********************************************************************************************************
  1212. * 函 数 名: NAND_LBNtoPBN
  1213. * 功能说明: 逻辑块号转换为物理块号
  1214. * 形 参: _uiLBN : 逻辑块号 Logic Block No
  1215. * 返 回 值: 物理块号, 如果是 0xFFFFFFFF 则表示错误
  1216. *********************************************************************************************************
  1217. */
  1218. static uint16_t NAND_LBNtoPBN(uint32_t _uiLBN)
  1219. {
  1220. uint16_t usPBN; /* 物理块号 */
  1221. /* 如果逻辑块号大于有效的数据块个数则固定返回0xFFFF, 调用该函数的代码应该检查出这种错误 */
  1222. if (_uiLBN >= s_usValidDataBlockCount)
  1223. {
  1224. return 0xFFFF;
  1225. }
  1226. /* 查询LUT表,获得物理块号 */
  1227. usPBN = s_usLUT[_uiLBN];
  1228. return usPBN;
  1229. }
  1230. /*
  1231. *********************************************************************************************************
  1232. * 函 数 名: NAND_FindFreeBlock
  1233. * 功能说明: 从最后一个块开始,查找一个可用的块。A18必须相同
  1234. * 形 参: _ulSrcPageNo : 源页号
  1235. * 返 回 值: 块号,如果是0xFFFF表示失败
  1236. *********************************************************************************************************
  1237. */
  1238. static uint16_t NAND_FindFreeBlock (uint32_t _ulSrcPageNo)
  1239. {
  1240. uint16_t n;
  1241. n = NAND_BLOCK_COUNT - 1;
  1242. if (_ulSrcPageNo & 0x40) /* 需要奇数块 */
  1243. {
  1244. if ((n & 0x01) == 0)
  1245. {
  1246. n--;
  1247. }
  1248. }
  1249. else /* 需要偶数块 */
  1250. {
  1251. if (n & 0x01)
  1252. {
  1253. n--;
  1254. }
  1255. }
  1256. while (1)
  1257. {
  1258. if (NAND_IsFreeBlock(n)) /* 是空闲块 */
  1259. {
  1260. return n;
  1261. }
  1262. if (n < 2)
  1263. {
  1264. return 0xFFFF; /* 没有找到空闲的块 */
  1265. }
  1266. n -= 2;
  1267. }
  1268. }
  1269. /*
  1270. *********************************************************************************************************
  1271. * 函 数 名: NAND_IsBufOk
  1272. * 功能说明: 判断内存缓冲区的数据是否全部为指定值
  1273. * 形 参: - _pBuf : 输入缓冲区
  1274. * - _ulLen : 缓冲区长度
  1275. * - __ucValue : 缓冲区每个单元的正确数值
  1276. * 返 回 值: 1 :全部正确; 0 :不正确
  1277. *********************************************************************************************************
  1278. */
  1279. static uint8_t NAND_IsBufOk(uint8_t *_pBuf, uint32_t _ulLen, uint8_t _ucValue)
  1280. {
  1281. uint32_t i;
  1282. for (i = 0; i < _ulLen; i++)
  1283. {
  1284. if (_pBuf[i] != _ucValue)
  1285. {
  1286. return 0;
  1287. }
  1288. }
  1289. return 1;
  1290. }
  1291. /*
  1292. *********************************************************************************************************
  1293. * 函 数 名: NAND_IsBadBlock
  1294. * 功能说明: 根据坏块标记检测NAND Flash指定的块是否坏块
  1295. * 形 参: _ulBlockNo :块号 0 - 1023 (对于128M字节,2K Page的NAND Flash,有1024个块)
  1296. * 返 回 值: 0 :该块可用; 1 :该块是坏块
  1297. *********************************************************************************************************
  1298. */
  1299. uint8_t NAND_IsBadBlock(uint32_t _ulBlockNo)
  1300. {
  1301. uint8_t ucFlag;
  1302. /* 如果NAND Flash出厂前已经标注为坏块了,则就认为是坏块 */
  1303. FSMC_NAND_ReadSpare(&ucFlag, _ulBlockNo * NAND_BLOCK_SIZE, BI_OFFSET, 1);
  1304. if (ucFlag != 0xFF)
  1305. {
  1306. return 1;
  1307. }
  1308. FSMC_NAND_ReadSpare(&ucFlag, _ulBlockNo * NAND_BLOCK_SIZE + 1, BI_OFFSET, 1);
  1309. if (ucFlag != 0xFF)
  1310. {
  1311. return 1;
  1312. }
  1313. return 0; /* 是好块 */
  1314. }
  1315. /*
  1316. *********************************************************************************************************
  1317. * 函 数 名: NAND_IsFreeBlock
  1318. * 功能说明: 根据坏块标记和USED标志检测是否可用块
  1319. * 形 参: _ulBlockNo :块号 0 - 1023 (对于128M字节,2K Page的NAND Flash,有1024个块)
  1320. * 返 回 值: 1 :该块可用; 0 :该块是坏块或者已占用
  1321. *********************************************************************************************************
  1322. */
  1323. static uint8_t NAND_IsFreeBlock(uint32_t _ulBlockNo)
  1324. {
  1325. uint8_t ucFlag;
  1326. /* 如果NAND Flash出厂前已经标注为坏块了,则就认为是坏块 */
  1327. if (NAND_IsBadBlock(_ulBlockNo))
  1328. {
  1329. return 0;
  1330. }
  1331. //FSMC_NAND_ReadPage(&ucFlag, _ulBlockNo * NAND_BLOCK_SIZE, USED_OFFSET, 1); 2014-05-03 bug
  1332. FSMC_NAND_ReadSpare(&ucFlag, _ulBlockNo * NAND_BLOCK_SIZE, USED_OFFSET, 1);
  1333. if (ucFlag == 0xFF)
  1334. {
  1335. return 1;
  1336. }
  1337. return 0;
  1338. }
  1339. /*
  1340. *********************************************************************************************************
  1341. * 函 数 名: NAND_ScanBlock
  1342. * 功能说明: 扫描测试NAND Flash指定的块
  1343. * 【扫描测试算法】
  1344. * 1) 第1个块(包括主数据区和备用数据区),擦除后检测是否全0xFF, 正确的话继续测试改块,否则该块
  1345. 是坏块,函数返回
  1346. * 2) 当前块写入全 0x00,然后读取检测,正确的话继续测试改块,否则退出
  1347. * 3) 重复第(2)步;如果循环次数达50次都没有发生错误,那么该块正常,函数返回,否则该块是坏块,
  1348. * 函数返回
  1349. * 【注意】
  1350. * 1) 该函数测试完毕后,会删除块内所有数据,即变为全0xFF;
  1351. * 2) 该函数除了测试主数据区外,也对备用数据区进行测试。
  1352. * 3) 擦写测试循环次数可以宏指定。#define BAD_BALOK_TEST_CYCLE 50
  1353. * 形 参: _ulPageNo :页号 0 - 65535 (对于128M字节,2K Page的NAND Flash,有1024个块)
  1354. * 返 回 值: NAND_OK :该块可用; NAND_FAIL :该块是坏块
  1355. *********************************************************************************************************
  1356. */
  1357. uint8_t NAND_ScanBlock(uint32_t _ulBlockNo)
  1358. {
  1359. uint32_t i, k;
  1360. uint32_t ulPageNo;
  1361. #if 0
  1362. /* 如果NAND Flash出厂前已经标注为坏块了,则就认为是坏块 */
  1363. if (NAND_IsBadBlock(_ulBlockNo))
  1364. {
  1365. return NAND_FAIL;
  1366. }
  1367. #endif
  1368. /* 下面的代码将通过反复擦除、编程的方式来测试NAND Flash每个块的可靠性 */
  1369. memset(s_ucTempBuf, 0x00, NAND_PAGE_TOTAL_SIZE);
  1370. for (i = 0; i < BAD_BALOK_TEST_CYCLE; i++)
  1371. {
  1372. /* 第1步:擦除这个块 */
  1373. if (FSMC_NAND_EraseBlock(_ulBlockNo) != NAND_READY)
  1374. {
  1375. return NAND_FAIL;
  1376. }
  1377. /* 第2步:读出块内每个page的数据,并判断是否全0xFF */
  1378. ulPageNo = _ulBlockNo * NAND_BLOCK_SIZE; /* 计算该块第1个页的页号 */
  1379. for (k = 0; k < NAND_BLOCK_SIZE; k++)
  1380. {
  1381. /* 读出整页数据 */
  1382. FSMC_NAND_ReadPage(s_ucTempBuf, ulPageNo, 0, NAND_PAGE_TOTAL_SIZE);
  1383. /* 判断存储单元是不是全0xFF */
  1384. if (NAND_IsBufOk(s_ucTempBuf, NAND_PAGE_TOTAL_SIZE, 0xFF) == 0)
  1385. {
  1386. return NAND_FAIL;
  1387. }
  1388. ulPageNo++; /* 继续写下一个页 */
  1389. }
  1390. /* 第2步:写全0,并读回判断是否全0 */
  1391. ulPageNo = _ulBlockNo * NAND_BLOCK_SIZE; /* 计算该块第1个页的页号 */
  1392. for (k = 0; k < NAND_BLOCK_SIZE; k++)
  1393. {
  1394. /* 填充buf[]缓冲区为全0,并写入NAND Flash */
  1395. memset(s_ucTempBuf, 0x00, NAND_PAGE_TOTAL_SIZE);
  1396. if (FSMC_NAND_WritePage(s_ucTempBuf, ulPageNo, 0, NAND_PAGE_TOTAL_SIZE) != NAND_OK)
  1397. {
  1398. return NAND_FAIL;
  1399. }
  1400. /* 读出整页数据, 判断存储单元是不是全0x00 */
  1401. FSMC_NAND_ReadPage(s_ucTempBuf, ulPageNo, 0, NAND_PAGE_TOTAL_SIZE);
  1402. if (NAND_IsBufOk(s_ucTempBuf, NAND_PAGE_TOTAL_SIZE, 0x00) == 0)
  1403. {
  1404. return NAND_FAIL;
  1405. }
  1406. ulPageNo++; /* 继续一个页 */
  1407. }
  1408. }
  1409. /* 最后一步:擦除整个块 */
  1410. if (FSMC_NAND_EraseBlock(_ulBlockNo) != NAND_READY)
  1411. {
  1412. return NAND_FAIL;
  1413. }
  1414. ulPageNo = _ulBlockNo * NAND_BLOCK_SIZE; /* 计算该块第1个页的页号 */
  1415. for (k = 0; k < NAND_BLOCK_SIZE; k++)
  1416. {
  1417. /* 读出整页数据 */
  1418. FSMC_NAND_ReadPage(s_ucTempBuf, ulPageNo, 0, NAND_PAGE_TOTAL_SIZE);
  1419. /* 判断存储单元是不是全0xFF */
  1420. if (NAND_IsBufOk(s_ucTempBuf, NAND_PAGE_TOTAL_SIZE, 0xFF) == 0)
  1421. {
  1422. return NAND_FAIL;
  1423. }
  1424. ulPageNo++; /* 继续写下一个页 */
  1425. }
  1426. return NAND_OK;
  1427. }
  1428. /*
  1429. *********************************************************************************************************
  1430. * 函 数 名: NAND_MarkUsedBlock
  1431. * 功能说明: 标记NAND Flash指定的块为已用块
  1432. * 形 参: _ulBlockNo :块号 0 - 1023 (对于128M字节,2K Page的NAND Flash,有1024个块)
  1433. * 返 回 值: NAND_OK:标记成功; NAND_FAIL:标记失败,上级软件应该进行坏块处理。
  1434. *********************************************************************************************************
  1435. */
  1436. static uint8_t NAND_MarkUsedBlock(uint32_t _ulBlockNo)
  1437. {
  1438. uint32_t ulPageNo;
  1439. uint8_t ucFlag;
  1440. /* 计算块的第1个页号 */
  1441. ulPageNo = _ulBlockNo * NAND_BLOCK_SIZE; /* 计算该块第1个页的页号 */
  1442. /* 块内第1个page备用区的第USED_OFFSET个字节写入非0xFF数据表示已用块 */
  1443. ucFlag = NAND_USED_BLOCK_FLAG;
  1444. if (FSMC_NAND_WriteSpare(&ucFlag, ulPageNo, USED_OFFSET, 1) == NAND_FAIL)
  1445. {
  1446. /* 如果标记失败,则需要标注这个块为坏块 */
  1447. return NAND_FAIL;
  1448. }
  1449. return NAND_OK;
  1450. }
  1451. /*
  1452. *********************************************************************************************************
  1453. * 函 数 名: NAND_MarkBadBlock
  1454. * 功能说明: 标记NAND Flash指定的块为坏块
  1455. * 形 参: _ulBlockNo :块号 0 - 1023 (对于128M字节,2K Page的NAND Flash,有1024个块)
  1456. * 返 回 值: 固定NAND_OK
  1457. *********************************************************************************************************
  1458. */
  1459. void NAND_MarkBadBlock(uint32_t _ulBlockNo)
  1460. {
  1461. uint32_t ulPageNo;
  1462. uint8_t ucFlag;
  1463. printf_err("NAND_MarkBadBlock(%d)\r\n", _ulBlockNo);
  1464. /* 计算块的第1个页号 */
  1465. ulPageNo = _ulBlockNo * NAND_BLOCK_SIZE; /* 计算该块第1个页的页号 */
  1466. /* 块内第1个page备用区的第BI_OFFSET个字节写入非0xFF数据表示坏块 */
  1467. ucFlag = NAND_BAD_BLOCK_FLAG;
  1468. if (FSMC_NAND_WriteSpare(&ucFlag, ulPageNo, BI_OFFSET, 1) == NAND_FAIL)
  1469. {
  1470. /* 如果第1个页标记失败,则在第2个页标记 */
  1471. FSMC_NAND_WriteSpare(&ucFlag, ulPageNo + 1, BI_OFFSET, 1);
  1472. }
  1473. }
  1474. /*
  1475. *********************************************************************************************************
  1476. * 函 数 名: NAND_ScanAllBadBlock
  1477. * 功能说明: 扫描测试所有的BLOCK, 如果是坏块,则做坏块标记
  1478. * 形 参: _ulBlockNo :块号 0 - 1023 (对于128M字节,2K Page的NAND Flash,有1024个块)
  1479. * 返 回 值: 固定NAND_OK
  1480. *********************************************************************************************************
  1481. */
  1482. void NAND_ScanAllBadBlock(void)
  1483. {
  1484. uint32_t i;
  1485. for (i = 0; i < NAND_BLOCK_COUNT; i++)
  1486. {
  1487. if (NAND_ScanBlock(i) == NAND_OK)
  1488. {
  1489. printf("Scan Block %d (%d%%), Ok\r\n", i, i * 100 / NAND_BLOCK_COUNT);
  1490. }
  1491. else
  1492. {
  1493. printf("Scan Block %d (%d%%), Err\r\n", i, i * 100 / NAND_BLOCK_COUNT);
  1494. NAND_MarkBadBlock(i);
  1495. }
  1496. }
  1497. }
  1498. /*
  1499. *********************************************************************************************************
  1500. * 函 数 名: NAND_Format
  1501. * 功能说明: NAND Flash格式化,擦除所有的数据,重建LUT
  1502. * 形 参: 无
  1503. * 返 回 值: NAND_OK : 成功; NAND_Fail :失败(一般是坏块数量过多导致)
  1504. *********************************************************************************************************
  1505. */
  1506. uint8_t NAND_Format(void)
  1507. {
  1508. uint16_t i, n;
  1509. uint16_t usGoodBlockCount;
  1510. /* 擦除每个块 */
  1511. usGoodBlockCount = 0;
  1512. for (i = 0; i < NAND_BLOCK_COUNT; i++)
  1513. {
  1514. FSMC_NAND_EraseBlock(i);
  1515. /* 如果是好块,则擦除 */
  1516. if (!NAND_IsBadBlock(i))
  1517. {
  1518. FSMC_NAND_EraseBlock(i);
  1519. usGoodBlockCount++;
  1520. }
  1521. }
  1522. /* 如果好块的数量少于100,则NAND Flash报废 */
  1523. if (usGoodBlockCount < 100)
  1524. {
  1525. return NAND_FAIL;
  1526. }
  1527. usGoodBlockCount = (usGoodBlockCount * 98) / 100; /* 98%的好块用于存储数据 */
  1528. /* 重新搜索一次 */
  1529. n = 0; /* 统计已标注的好块 */
  1530. for (i = 0; i < NAND_BLOCK_COUNT; i++)
  1531. {
  1532. if (!NAND_IsBadBlock(i))
  1533. {
  1534. /* 如果是好块,则在该块的第1个PAGE的LBN0 LBN1处写入n值 (前面已经执行了块擦除) */
  1535. if (FSMC_NAND_WriteSpare((uint8_t *)&n, i * NAND_BLOCK_SIZE, LBN0_OFFSET, 2) != NAND_OK)
  1536. {
  1537. return NAND_FAIL;
  1538. }
  1539. n++;
  1540. /* 计算并写入每个扇区的ECC值 (暂时未作)*/
  1541. if (n == usGoodBlockCount)
  1542. {
  1543. break;
  1544. }
  1545. }
  1546. }
  1547. NAND_BuildLUT(); /* 初始化LUT表 */
  1548. return NAND_OK;
  1549. }
  1550. /*
  1551. *********************************************************************************************************
  1552. * 函 数 名: NAND_FormatCapacity
  1553. * 功能说明: NAND Flash格式化后的有效容量. (最大支持4G)
  1554. * 形 参: 无
  1555. * 返 回 值: NAND_OK : 成功; NAND_Fail :失败(一般是坏块数量过多导致)
  1556. *********************************************************************************************************
  1557. */
  1558. uint32_t NAND_FormatCapacity(void)
  1559. {
  1560. uint16_t usCount;
  1561. /* 计算用于存储数据的数据块个数,按照总有效块数的98%来计算 */
  1562. usCount = (s_usValidDataBlockCount * DATA_BLOCK_PERCENT) / 100;
  1563. return (usCount * NAND_BLOCK_SIZE * NAND_PAGE_SIZE);
  1564. }
  1565. /*
  1566. *********************************************************************************************************
  1567. * 函 数 名: NAND_DispBadBlockInfo
  1568. * 功能说明: 通过串口打印出NAND Flash的坏块信息
  1569. * 形 参: 无
  1570. * 返 回 值: 无
  1571. *********************************************************************************************************
  1572. */
  1573. void NAND_DispBadBlockInfo(void)
  1574. {
  1575. uint32_t id;
  1576. uint32_t i;
  1577. uint32_t n;
  1578. uint32_t used = 0;
  1579. uint16_t bad_no[128];
  1580. FSMC_NAND_Init(); /* 初始化FSMC */
  1581. id = NAND_ReadID();
  1582. printf("NAND Flash ID = 0x%04X, Type = ", id);
  1583. if (id == HY27UF081G2A)
  1584. {
  1585. printf("HY27UF081G2A\r\n 1024 Blocks, 64 pages per block, 2048 + 64 bytes per page\r\n");
  1586. }
  1587. else if (id == K9F1G08U0A)
  1588. {
  1589. printf("K9F1G08U0A\r\n 1024 Blocks, 64 pages per block, 2048 + 64 bytes per page\r\n");
  1590. }
  1591. else if (id == K9F1G08U0B)
  1592. {
  1593. printf("K9F1G08U0B\r\n 1024 Blocks, 64 pages per block, 2048 + 64 bytes per page\r\n");
  1594. }
  1595. else if (id == H27U4G8F2DTR)
  1596. {
  1597. printf("H27U4G8F2DTR\r\n 4096 Blocks, 64 pages per block, 2048 + 64 bytes per page\r\n");
  1598. }
  1599. else if (id == H27U1G8F2BTR)
  1600. {
  1601. printf("H27U1G8F2BTR\r\n 1024 Blocks, 64 pages per block, 2048 + 64 bytes per page\r\n");
  1602. }
  1603. else
  1604. {
  1605. printf("unkonow\r\n");
  1606. return;
  1607. }
  1608. FSMC_NAND_Reset();
  1609. printf("Block Info : 0 is OK, * is Bad, - is Used\r\n");
  1610. n = 0; /* 坏块统计 */
  1611. used = 0; /* 已用块统计 */
  1612. for (i = 0; i < NAND_BLOCK_COUNT; i++)
  1613. {
  1614. if (NAND_IsBadBlock(i))
  1615. {
  1616. if (n < sizeof(bad_no) / 2)
  1617. {
  1618. bad_no[n] = i; /* 记录坏块号 */
  1619. }
  1620. printf("*");
  1621. n++;
  1622. }
  1623. else
  1624. {
  1625. if (NAND_IsFreeBlock(i))
  1626. {
  1627. printf("0");
  1628. }
  1629. else
  1630. {
  1631. printf("-"); /* 已用块 */
  1632. used++;
  1633. }
  1634. }
  1635. if (((i + 1) % 8) == 0)
  1636. {
  1637. printf(" ");
  1638. }
  1639. if (((i + 1) % 64) == 0)
  1640. {
  1641. printf("\r\n");
  1642. }
  1643. }
  1644. if (id == H27U4G8F2DTR)
  1645. {
  1646. printf("Bad Block Count = %d ( < 80 is OK), Used = %d \r\n", n, used);
  1647. }
  1648. else
  1649. {
  1650. printf("Bad Block Count = %d\r\n", n);
  1651. }
  1652. /* 打印坏块序号 */
  1653. if (n > 0)
  1654. {
  1655. for (i = 0; i < n; i++)
  1656. {
  1657. printf("%4d ", bad_no[i]);
  1658. }
  1659. printf("\r\n\r\n");
  1660. }
  1661. }
  1662. /*
  1663. *********************************************************************************************************
  1664. * 函 数 名: NAND_GetBlockInfo
  1665. * 功能说明: 获得NAND 坏块、已用块信息
  1666. * 形 参: 无
  1667. * 返 回 值: 1 表示成功识别, 0 表示识别失败
  1668. *********************************************************************************************************
  1669. */
  1670. uint8_t NAND_GetBlockInfo(NAND_BLOCK_INFO_T *_pInfo)
  1671. {
  1672. uint32_t id;
  1673. uint32_t i;
  1674. uint32_t n;
  1675. uint32_t used = 0;
  1676. uint32_t free = 0;
  1677. FSMC_NAND_Init(); /* 初始化FSMC */
  1678. id = NAND_ReadID();
  1679. _pInfo->ChipID = id;
  1680. if (id == HY27UF081G2A)
  1681. {
  1682. _pInfo->ChipID = HY27UF081G2A;
  1683. strcpy(_pInfo->ChipName, "HY27UF081G2A");
  1684. }
  1685. else if (id == K9F1G08U0A)
  1686. {
  1687. _pInfo->ChipID = K9F1G08U0A;
  1688. strcpy(_pInfo->ChipName, "K9F1G08U0A");
  1689. }
  1690. else if (id == K9F1G08U0B)
  1691. {
  1692. _pInfo->ChipID = K9F1G08U0B;
  1693. strcpy(_pInfo->ChipName, "K9F1G08U0B");
  1694. }
  1695. else if (id == H27U4G8F2DTR)
  1696. {
  1697. _pInfo->ChipID = H27U4G8F2DTR;
  1698. strcpy(_pInfo->ChipName, "H27U4G8F2DTR");
  1699. }
  1700. else if (id == H27U1G8F2BTR)
  1701. {
  1702. _pInfo->ChipID = H27U1G8F2BTR;
  1703. strcpy(_pInfo->ChipName, "H27U1G8F2BTR");
  1704. }
  1705. else
  1706. {
  1707. return 0;
  1708. }
  1709. FSMC_NAND_Reset();
  1710. n = 0; /* 坏块统计 */
  1711. used = 0; /* 已用块统计 */
  1712. free = 0; /* 未用的好块 */
  1713. for (i = 0; i < NAND_BLOCK_COUNT; i++)
  1714. {
  1715. if (NAND_IsBadBlock(i))
  1716. {
  1717. n++; /* 记录坏块 */
  1718. }
  1719. else
  1720. {
  1721. if (NAND_IsFreeBlock(i))
  1722. {
  1723. free++;
  1724. }
  1725. else
  1726. {
  1727. used++; /* 已用块 */
  1728. }
  1729. }
  1730. }
  1731. _pInfo->Bad = n;
  1732. _pInfo->Used = used;
  1733. _pInfo->Free = free;
  1734. return 1;
  1735. }
  1736. /*
  1737. *********************************************************************************************************
  1738. * 函 数 名: NAND_DispPhyPageData
  1739. * 功能说明: 通过串口打印出指定页的数据(2048+64)
  1740. * 形 参: _uiPhyPageNo : 物理页号
  1741. * 返 回 值: 无
  1742. *********************************************************************************************************
  1743. */
  1744. void NAND_DispPhyPageData(uint32_t _uiPhyPageNo)
  1745. {
  1746. uint32_t i, n;
  1747. uint32_t ulBlockNo;
  1748. uint16_t usOffsetPageNo;
  1749. ulBlockNo = _uiPhyPageNo / NAND_BLOCK_SIZE; /* 根据物理页号反推块号 */
  1750. usOffsetPageNo = _uiPhyPageNo % NAND_BLOCK_SIZE; /* 根据物理页号计算物理页号在块内偏移页号 */
  1751. if (NAND_OK != FSMC_NAND_ReadPage(s_ucTempBuf, _uiPhyPageNo, 0, NAND_PAGE_TOTAL_SIZE))
  1752. {
  1753. printf("FSMC_NAND_ReadPage Failed() \r\n");
  1754. return;
  1755. }
  1756. printf("Block = %d, Page = %d\r\n", ulBlockNo, usOffsetPageNo);
  1757. /* 打印前面 2048字节数据,每512字节空一行 */
  1758. for (n = 0; n < 4; n++)
  1759. {
  1760. for (i = 0; i < 512; i++)
  1761. {
  1762. printf(" %02X", s_ucTempBuf[i + n * 512]);
  1763. if ((i & 31) == 31)
  1764. {
  1765. printf("\r\n"); /* 每行显示32字节数据 */
  1766. }
  1767. else if ((i & 31) == 15)
  1768. {
  1769. printf(" - ");
  1770. }
  1771. }
  1772. printf("\r\n");
  1773. }
  1774. /* 打印前面 2048字节数据,每512字节空一行 */
  1775. for (i = 0; i < 64; i++)
  1776. {
  1777. printf(" %02X", s_ucTempBuf[i + 2048]);
  1778. if ((i & 15) == 15)
  1779. {
  1780. printf("\r\n"); /* 每行显示32字节数据 */
  1781. }
  1782. }
  1783. }
  1784. /*
  1785. *********************************************************************************************************
  1786. * 函 数 名: NAND_DispLogicPageData
  1787. * 功能说明: 通过串口打印出指定页的数据(2048+64)
  1788. * 形 参: _uiLogicPageNo : 逻辑页号
  1789. * 返 回 值: 无
  1790. *********************************************************************************************************
  1791. */
  1792. void NAND_DispLogicPageData(uint32_t _uiLogicPageNo)
  1793. {
  1794. uint32_t uiPhyPageNo;
  1795. uint16_t usLBN; /* 逻辑块号 */
  1796. uint16_t usPBN; /* 物理块号 */
  1797. usLBN = _uiLogicPageNo / NAND_BLOCK_SIZE;
  1798. usPBN = NAND_LBNtoPBN(usLBN); /* 查询LUT表获得物理块号 */
  1799. if (usPBN >= NAND_BLOCK_COUNT)
  1800. {
  1801. /* 没有格式化,usPBN = 0xFFFF */
  1802. return;
  1803. }
  1804. printf("LogicBlock = %d, PhyBlock = %d\r\n", _uiLogicPageNo, usPBN);
  1805. /* 计算物理页号 */
  1806. uiPhyPageNo = usPBN * NAND_BLOCK_SIZE + _uiLogicPageNo % NAND_BLOCK_SIZE;
  1807. NAND_DispPhyPageData(uiPhyPageNo); /* 显示指定页数据 */
  1808. }
  1809. /*
  1810. *********************************************************************************************************
  1811. * 函 数 名: NAND_ReadONFI
  1812. * 功能说明: 读NAND Flash的ONFI信息, 针对 H27U4G8F2DTR
  1813. * 形 参: _pBuf 存放结果的缓冲区, 大小4字节。应该固定返回 "ONFI" 字符串
  1814. * 返 回 值: 无
  1815. *********************************************************************************************************
  1816. */
  1817. void NAND_ReadONFI(uint8_t *_pBuf)
  1818. {
  1819. uint16_t i;
  1820. /* 发送命令 Command to the command area */
  1821. NAND_CMD_AREA = 0x90;
  1822. NAND_ADDR_AREA = 0x20;
  1823. /* 读数据到缓冲区pBuffer */
  1824. for(i = 0; i < 256; i++)
  1825. {
  1826. _pBuf[i] = NAND_DATA_AREA;
  1827. }
  1828. }
  1829. /*
  1830. *********************************************************************************************************
  1831. * 函 数 名: NAND_ReadParamPage
  1832. * 功能说明: Read Parameter Page, 针对 H27U4G8F2DTR
  1833. * 形 参: _pData 存放结果的缓冲区。
  1834. * 返 回 值: 无
  1835. *********************************************************************************************************
  1836. */
  1837. void NAND_ReadParamPage(PARAM_PAGE_T *_pData)
  1838. {
  1839. uint16_t i;
  1840. uint8_t *_pBuf = (uint8_t *)_pData;
  1841. /* 发送命令 Command to the command area */
  1842. NAND_CMD_AREA = 0xEC;
  1843. NAND_ADDR_AREA = 0x00;
  1844. /* 必须等待,否则读出数据异常, 此处应该判断超时 */
  1845. for (i = 0; i < 20; i++);
  1846. while( GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6) == 0);
  1847. /* 读数据到缓冲区pBuffer */
  1848. for(i = 0; i < 256; i++)
  1849. {
  1850. _pBuf[i] = NAND_DATA_AREA;
  1851. }
  1852. }
  1853. /*
  1854. *********************************************************************************************************
  1855. * 函 数 名: NAND_DispParamPage
  1856. * 功能说明: 通过串口打印 Parameter Page Data Structure Definition (参数页数据结构) 针对 H27U4G8F2DTR
  1857. * 形 参: 无
  1858. * 返 回 值: 无
  1859. *********************************************************************************************************
  1860. */
  1861. void NAND_DispParamPage(void)
  1862. {
  1863. PARAM_PAGE_T tPage;
  1864. char buf[32];
  1865. FSMC_NAND_Reset();
  1866. NAND_ReadParamPage(&tPage);
  1867. #if 0
  1868. uint8_t Sign[4]; /* = "ONFI" */
  1869. uint16_t Revision; /* Bit1 = 1 表示支持 ONFI Ver 1.0 */
  1870. uint16_t Features; /* */
  1871. uint16_t OptionalCommands;
  1872. #endif
  1873. printf("\r\n");
  1874. printf("Read Parameter Page Data :\r\n");
  1875. printf("Sign = %c%c%c%c\r\n", tPage.Sign[0], tPage.Sign[1], tPage.Sign[2], tPage.Sign[3]);
  1876. printf("Revision = %04X\r\n", tPage.Revision);
  1877. printf("Features = %04X\r\n", tPage.Features);
  1878. printf("OptionalCommands = %04X\r\n", tPage.OptionalCommands);
  1879. /* Manufacturer information block */
  1880. memcpy(buf, tPage.Manufacturer, 12);
  1881. buf[12] = 0;
  1882. printf("Manufacturer = %s\r\n", buf); /* 制造商 */
  1883. memcpy(buf, tPage.Model, 20);
  1884. buf[20] = 0;
  1885. printf("Model = %s\r\n", buf); /* 型号 */
  1886. printf("JEDEC_ID = %02X\r\n", tPage.JEDEC_ID); /* AD */
  1887. printf("DateCode = %04X\r\n", tPage.DateCode);
  1888. #if 0
  1889. /* Memory organization block */
  1890. uint32_t PageDataSize;
  1891. uint16_t PageSpareSize;
  1892. uint32_t PartialPageDataSize;
  1893. uint16_t PartialPageSpareSize;
  1894. uint32_t BlockSize;
  1895. uint32_t LogicalUnitSize;
  1896. uint8_t LogicalUnitNumber;
  1897. uint8_t AddressCycles;
  1898. uint8_t CellBits;
  1899. uint16_t BadBlockMax;
  1900. uint16_t BlockEndurance;
  1901. uint8_t ValidBlocksBegin; /* 最前面保证有效的块个数 */
  1902. uint16_t BlockEndurance2; /* Block endurance for guaranteed valid blocks */
  1903. uint8_t ProgramsPerPage; /* Number of programs per page */
  1904. uint8_t PartialProgram;
  1905. uint8_t ECCcorrectBits;
  1906. uint8_t InterleavedAddrBits; /* 交错的地址位 */
  1907. uint8_t InterleavedOperaton;
  1908. uint8_t Rsv3[13];
  1909. #endif
  1910. printf("\r\n");
  1911. }
  1912. /***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/