stm32f1xx_hal_nand.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_nand.c
  4. * @author MCD Application Team
  5. * @brief NAND HAL module driver.
  6. * This file provides a generic firmware to drive NAND memories mounted
  7. * as external device.
  8. *
  9. @verbatim
  10. ==============================================================================
  11. ##### How to use this driver #####
  12. ==============================================================================
  13. [..]
  14. This driver is a generic layered driver which contains a set of APIs used to
  15. control NAND flash memories. It uses the FSMC layer functions to interface
  16. with NAND devices. This driver is used as follows:
  17. (+) NAND flash memory configuration sequence using the function HAL_NAND_Init()
  18. with control and timing parameters for both common and attribute spaces.
  19. (+) Read NAND flash memory maker and device IDs using the function
  20. HAL_NAND_Read_ID(). The read information is stored in the NAND_ID_TypeDef
  21. structure declared by the function caller.
  22. (+) Access NAND flash memory by read/write operations using the functions
  23. HAL_NAND_Read_Page_8b()/HAL_NAND_Read_SpareArea_8b(),
  24. HAL_NAND_Write_Page_8b()/HAL_NAND_Write_SpareArea_8b(),
  25. HAL_NAND_Read_Page_16b()/HAL_NAND_Read_SpareArea_16b(),
  26. HAL_NAND_Write_Page_16b()/HAL_NAND_Write_SpareArea_16b()
  27. to read/write page(s)/spare area(s). These functions use specific device
  28. information (Block, page size..) predefined by the user in the NAND_DeviceConfigTypeDef
  29. structure. The read/write address information is contained by the Nand_Address_Typedef
  30. structure passed as parameter.
  31. (+) Perform NAND flash Reset chip operation using the function HAL_NAND_Reset().
  32. (+) Perform NAND flash erase block operation using the function HAL_NAND_Erase_Block().
  33. The erase block address information is contained in the Nand_Address_Typedef
  34. structure passed as parameter.
  35. (+) Read the NAND flash status operation using the function HAL_NAND_Read_Status().
  36. (+) You can also control the NAND device by calling the control APIs HAL_NAND_ECC_Enable()/
  37. HAL_NAND_ECC_Disable() to respectively enable/disable the ECC code correction
  38. feature or the function HAL_NAND_GetECC() to get the ECC correction code.
  39. (+) You can monitor the NAND device HAL state by calling the function
  40. HAL_NAND_GetState()
  41. [..]
  42. (@) This driver is a set of generic APIs which handle standard NAND flash operations.
  43. If a NAND flash device contains different operations and/or implementations,
  44. it should be implemented separately.
  45. @endverbatim
  46. ******************************************************************************
  47. * @attention
  48. *
  49. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  50. *
  51. * Redistribution and use in source and binary forms, with or without modification,
  52. * are permitted provided that the following conditions are met:
  53. * 1. Redistributions of source code must retain the above copyright notice,
  54. * this list of conditions and the following disclaimer.
  55. * 2. Redistributions in binary form must reproduce the above copyright notice,
  56. * this list of conditions and the following disclaimer in the documentation
  57. * and/or other materials provided with the distribution.
  58. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  59. * may be used to endorse or promote products derived from this software
  60. * without specific prior written permission.
  61. *
  62. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  63. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  64. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  65. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  66. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  67. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  68. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  69. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  70. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  71. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  72. *
  73. ******************************************************************************
  74. */
  75. /* Includes ------------------------------------------------------------------*/
  76. #include "stm32f1xx_hal.h"
  77. /** @addtogroup STM32F1xx_HAL_Driver
  78. * @{
  79. */
  80. #ifdef HAL_NAND_MODULE_ENABLED
  81. #if defined (STM32F101xE) || defined(STM32F103xE) || defined(STM32F101xG) || defined(STM32F103xG)
  82. /** @defgroup NAND NAND
  83. * @brief NAND HAL module driver
  84. * @{
  85. */
  86. /* Private typedef -----------------------------------------------------------*/
  87. /* Private define ------------------------------------------------------------*/
  88. /** @defgroup NAND_Private_Constants NAND Private Constants
  89. * @{
  90. */
  91. /**
  92. * @}
  93. */
  94. /* Private macro -------------------------------------------------------------*/
  95. /** @defgroup NAND_Private_Macros NAND Private Macros
  96. * @{
  97. */
  98. /**
  99. * @}
  100. */
  101. /* Private variables ---------------------------------------------------------*/
  102. /* Private function prototypes -----------------------------------------------*/
  103. /* Exported functions --------------------------------------------------------*/
  104. /** @defgroup NAND_Exported_Functions NAND Exported Functions
  105. * @{
  106. */
  107. /** @defgroup NAND_Exported_Functions_Group1 Initialization and de-initialization functions
  108. * @brief Initialization and Configuration functions
  109. *
  110. @verbatim
  111. ==============================================================================
  112. ##### NAND Initialization and de-initialization functions #####
  113. ==============================================================================
  114. [..]
  115. This section provides functions allowing to initialize/de-initialize
  116. the NAND memory
  117. @endverbatim
  118. * @{
  119. */
  120. /**
  121. * @brief Perform NAND memory Initialization sequence
  122. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  123. * the configuration information for NAND module.
  124. * @param ComSpace_Timing: pointer to Common space timing structure
  125. * @param AttSpace_Timing: pointer to Attribute space timing structure
  126. * @retval HAL status
  127. */
  128. HAL_StatusTypeDef HAL_NAND_Init(NAND_HandleTypeDef *hnand, FSMC_NAND_PCC_TimingTypeDef *ComSpace_Timing, FSMC_NAND_PCC_TimingTypeDef *AttSpace_Timing)
  129. {
  130. /* Check the NAND handle state */
  131. if(hnand == NULL)
  132. {
  133. return HAL_ERROR;
  134. }
  135. if(hnand->State == HAL_NAND_STATE_RESET)
  136. {
  137. /* Allocate lock resource and initialize it */
  138. hnand->Lock = HAL_UNLOCKED;
  139. /* Initialize the low level hardware (MSP) */
  140. HAL_NAND_MspInit(hnand);
  141. }
  142. /* Initialize NAND control Interface */
  143. FSMC_NAND_Init(hnand->Instance, &(hnand->Init));
  144. /* Initialize NAND common space timing Interface */
  145. FSMC_NAND_CommonSpace_Timing_Init(hnand->Instance, ComSpace_Timing, hnand->Init.NandBank);
  146. /* Initialize NAND attribute space timing Interface */
  147. FSMC_NAND_AttributeSpace_Timing_Init(hnand->Instance, AttSpace_Timing, hnand->Init.NandBank);
  148. /* Enable the NAND device */
  149. __FSMC_NAND_ENABLE(hnand->Instance, hnand->Init.NandBank);
  150. /* Update the NAND controller state */
  151. hnand->State = HAL_NAND_STATE_READY;
  152. return HAL_OK;
  153. }
  154. /**
  155. * @brief Perform NAND memory De-Initialization sequence
  156. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  157. * the configuration information for NAND module.
  158. * @retval HAL status
  159. */
  160. HAL_StatusTypeDef HAL_NAND_DeInit(NAND_HandleTypeDef *hnand)
  161. {
  162. /* Initialize the low level hardware (MSP) */
  163. HAL_NAND_MspDeInit(hnand);
  164. /* Configure the NAND registers with their reset values */
  165. FSMC_NAND_DeInit(hnand->Instance, hnand->Init.NandBank);
  166. /* Reset the NAND controller state */
  167. hnand->State = HAL_NAND_STATE_RESET;
  168. /* Release Lock */
  169. __HAL_UNLOCK(hnand);
  170. return HAL_OK;
  171. }
  172. /**
  173. * @brief NAND MSP Init
  174. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  175. * the configuration information for NAND module.
  176. * @retval None
  177. */
  178. __weak void HAL_NAND_MspInit(NAND_HandleTypeDef *hnand)
  179. {
  180. /* Prevent unused argument(s) compilation warning */
  181. UNUSED(hnand);
  182. /* NOTE : This function Should not be modified, when the callback is needed,
  183. the HAL_NAND_MspInit could be implemented in the user file
  184. */
  185. }
  186. /**
  187. * @brief NAND MSP DeInit
  188. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  189. * the configuration information for NAND module.
  190. * @retval None
  191. */
  192. __weak void HAL_NAND_MspDeInit(NAND_HandleTypeDef *hnand)
  193. {
  194. /* Prevent unused argument(s) compilation warning */
  195. UNUSED(hnand);
  196. /* NOTE : This function Should not be modified, when the callback is needed,
  197. the HAL_NAND_MspDeInit could be implemented in the user file
  198. */
  199. }
  200. /**
  201. * @brief This function handles NAND device interrupt request.
  202. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  203. * the configuration information for NAND module.
  204. * @retval HAL status
  205. */
  206. void HAL_NAND_IRQHandler(NAND_HandleTypeDef *hnand)
  207. {
  208. /* Check NAND interrupt Rising edge flag */
  209. if(__FSMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_RISING_EDGE))
  210. {
  211. /* NAND interrupt callback*/
  212. HAL_NAND_ITCallback(hnand);
  213. /* Clear NAND interrupt Rising edge pending bit */
  214. __FSMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_RISING_EDGE);
  215. }
  216. /* Check NAND interrupt Level flag */
  217. if(__FSMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_LEVEL))
  218. {
  219. /* NAND interrupt callback*/
  220. HAL_NAND_ITCallback(hnand);
  221. /* Clear NAND interrupt Level pending bit */
  222. __FSMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_LEVEL);
  223. }
  224. /* Check NAND interrupt Falling edge flag */
  225. if(__FSMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_FALLING_EDGE))
  226. {
  227. /* NAND interrupt callback*/
  228. HAL_NAND_ITCallback(hnand);
  229. /* Clear NAND interrupt Falling edge pending bit */
  230. __FSMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_FALLING_EDGE);
  231. }
  232. /* Check NAND interrupt FIFO empty flag */
  233. if(__FSMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_FEMPT))
  234. {
  235. /* NAND interrupt callback*/
  236. HAL_NAND_ITCallback(hnand);
  237. /* Clear NAND interrupt FIFO empty pending bit */
  238. __FSMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_FEMPT);
  239. }
  240. }
  241. /**
  242. * @brief NAND interrupt feature callback
  243. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  244. * the configuration information for NAND module.
  245. * @retval None
  246. */
  247. __weak void HAL_NAND_ITCallback(NAND_HandleTypeDef *hnand)
  248. {
  249. /* Prevent unused argument(s) compilation warning */
  250. UNUSED(hnand);
  251. /* NOTE : This function Should not be modified, when the callback is needed,
  252. the HAL_NAND_ITCallback could be implemented in the user file
  253. */
  254. }
  255. /**
  256. * @}
  257. */
  258. /** @defgroup NAND_Exported_Functions_Group2 Input and Output functions
  259. * @brief Input Output and memory control functions
  260. *
  261. @verbatim
  262. ==============================================================================
  263. ##### NAND Input and Output functions #####
  264. ==============================================================================
  265. [..]
  266. This section provides functions allowing to use and control the NAND
  267. memory
  268. @endverbatim
  269. * @{
  270. */
  271. /**
  272. * @brief Read the NAND memory electronic signature
  273. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  274. * the configuration information for NAND module.
  275. * @param pNAND_ID: NAND ID structure
  276. * @retval HAL status
  277. */
  278. HAL_StatusTypeDef HAL_NAND_Read_ID(NAND_HandleTypeDef *hnand, NAND_IDTypeDef *pNAND_ID)
  279. {
  280. __IO uint32_t data = 0U;
  281. __IO uint32_t data1 = 0U;
  282. uint32_t deviceaddress = 0U;
  283. /* Process Locked */
  284. __HAL_LOCK(hnand);
  285. /* Check the NAND controller state */
  286. if(hnand->State == HAL_NAND_STATE_BUSY)
  287. {
  288. return HAL_BUSY;
  289. }
  290. /* Identify the device address */
  291. if(hnand->Init.NandBank == FSMC_NAND_BANK2)
  292. {
  293. deviceaddress = NAND_DEVICE1;
  294. }
  295. else
  296. {
  297. deviceaddress = NAND_DEVICE2;
  298. }
  299. /* Update the NAND controller state */
  300. hnand->State = HAL_NAND_STATE_BUSY;
  301. /* Send Read ID command sequence */
  302. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_READID;
  303. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  304. /* Read the electronic signature from NAND flash */
  305. if (hnand->Init.MemoryDataWidth == FSMC_NAND_PCC_MEM_BUS_WIDTH_8)
  306. {
  307. data = *(__IO uint32_t *)deviceaddress;
  308. /* Return the data read */
  309. pNAND_ID->Maker_Id = ADDR_1ST_CYCLE(data);
  310. pNAND_ID->Device_Id = ADDR_2ND_CYCLE(data);
  311. pNAND_ID->Third_Id = ADDR_3RD_CYCLE(data);
  312. pNAND_ID->Fourth_Id = ADDR_4TH_CYCLE(data);
  313. }
  314. else
  315. {
  316. data = *(__IO uint32_t *)deviceaddress;
  317. data1 = *((__IO uint32_t *)deviceaddress + 4U);
  318. /* Return the data read */
  319. pNAND_ID->Maker_Id = ADDR_1ST_CYCLE(data);
  320. pNAND_ID->Device_Id = ADDR_3RD_CYCLE(data);
  321. pNAND_ID->Third_Id = ADDR_1ST_CYCLE(data1);
  322. pNAND_ID->Fourth_Id = ADDR_3RD_CYCLE(data1);
  323. }
  324. /* Update the NAND controller state */
  325. hnand->State = HAL_NAND_STATE_READY;
  326. /* Process unlocked */
  327. __HAL_UNLOCK(hnand);
  328. return HAL_OK;
  329. }
  330. /**
  331. * @brief NAND memory reset
  332. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  333. * the configuration information for NAND module.
  334. * @retval HAL status
  335. */
  336. HAL_StatusTypeDef HAL_NAND_Reset(NAND_HandleTypeDef *hnand)
  337. {
  338. uint32_t deviceaddress = 0U;
  339. /* Process Locked */
  340. __HAL_LOCK(hnand);
  341. /* Check the NAND controller state */
  342. if(hnand->State == HAL_NAND_STATE_BUSY)
  343. {
  344. return HAL_BUSY;
  345. }
  346. /* Identify the device address */
  347. if(hnand->Init.NandBank == FSMC_NAND_BANK2)
  348. {
  349. deviceaddress = NAND_DEVICE1;
  350. }
  351. else
  352. {
  353. deviceaddress = NAND_DEVICE2;
  354. }
  355. /* Update the NAND controller state */
  356. hnand->State = HAL_NAND_STATE_BUSY;
  357. /* Send NAND reset command */
  358. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = 0xFF;
  359. /* Update the NAND controller state */
  360. hnand->State = HAL_NAND_STATE_READY;
  361. /* Process unlocked */
  362. __HAL_UNLOCK(hnand);
  363. return HAL_OK;
  364. }
  365. /**
  366. * @brief Configure the device: Enter the physical parameters of the device
  367. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  368. * the configuration information for NAND module.
  369. * @param pDeviceConfig : pointer to NAND_DeviceConfigTypeDef structure
  370. * @retval HAL status
  371. */
  372. HAL_StatusTypeDef HAL_NAND_ConfigDevice(NAND_HandleTypeDef *hnand, NAND_DeviceConfigTypeDef *pDeviceConfig)
  373. {
  374. hnand->Config.PageSize = pDeviceConfig->PageSize;
  375. hnand->Config.SpareAreaSize = pDeviceConfig->SpareAreaSize;
  376. hnand->Config.BlockSize = pDeviceConfig->BlockSize;
  377. hnand->Config.BlockNbr = pDeviceConfig->BlockNbr;
  378. hnand->Config.PlaneSize = pDeviceConfig->PlaneSize;
  379. hnand->Config.PlaneNbr = pDeviceConfig->PlaneNbr;
  380. hnand->Config.ExtraCommandEnable = pDeviceConfig->ExtraCommandEnable;
  381. return HAL_OK;
  382. }
  383. /**
  384. * @brief Read Page(s) from NAND memory block (8-bits addressing)
  385. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  386. * the configuration information for NAND module.
  387. * @param pAddress : pointer to NAND address structure
  388. * @param pBuffer : pointer to destination read buffer
  389. * @param NumPageToRead : number of pages to read from block
  390. * @retval HAL status
  391. */
  392. HAL_StatusTypeDef HAL_NAND_Read_Page_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumPageToRead)
  393. {
  394. __IO uint32_t index = 0U;
  395. uint32_t tickstart = 0U;
  396. uint32_t deviceaddress = 0U, size = 0U, numPagesRead = 0U, nandaddress = 0U;
  397. /* Process Locked */
  398. __HAL_LOCK(hnand);
  399. /* Check the NAND controller state */
  400. if(hnand->State == HAL_NAND_STATE_BUSY)
  401. {
  402. return HAL_BUSY;
  403. }
  404. /* Identify the device address */
  405. if(hnand->Init.NandBank == FSMC_NAND_BANK2)
  406. {
  407. deviceaddress = NAND_DEVICE1;
  408. }
  409. else
  410. {
  411. deviceaddress = NAND_DEVICE2;
  412. }
  413. /* Update the NAND controller state */
  414. hnand->State = HAL_NAND_STATE_BUSY;
  415. /* NAND raw address calculation */
  416. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  417. /* Page(s) read loop */
  418. while((NumPageToRead != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  419. {
  420. /* update the buffer size */
  421. size = (hnand->Config.PageSize) + ((hnand->Config.PageSize) * numPagesRead);
  422. /* Send read page command sequence */
  423. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  424. /* Cards with page size <= 512 bytes */
  425. if((hnand->Config.PageSize) <= 512U)
  426. {
  427. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  428. {
  429. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  430. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  431. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  432. }
  433. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  434. {
  435. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  436. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  437. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  438. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  439. }
  440. }
  441. else /* (hnand->Config.PageSize) > 512 */
  442. {
  443. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  444. {
  445. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  446. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  447. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  448. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  449. }
  450. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  451. {
  452. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  453. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  454. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  455. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  456. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  457. }
  458. }
  459. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  460. /* Check if an extra command is needed for reading pages */
  461. if(hnand->Config.ExtraCommandEnable == ENABLE)
  462. {
  463. /* Get tick */
  464. tickstart = HAL_GetTick();
  465. /* Read status until NAND is ready */
  466. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  467. {
  468. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  469. {
  470. return HAL_TIMEOUT;
  471. }
  472. }
  473. /* Go back to read mode */
  474. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  475. }
  476. /* Get Data into Buffer */
  477. for(; index < size; index++)
  478. {
  479. *(uint8_t *)pBuffer++ = *(uint8_t *)deviceaddress;
  480. }
  481. /* Increment read pages number */
  482. numPagesRead++;
  483. /* Decrement pages to read */
  484. NumPageToRead--;
  485. /* Increment the NAND address */
  486. nandaddress = (uint32_t)(nandaddress + 1U);
  487. }
  488. /* Update the NAND controller state */
  489. hnand->State = HAL_NAND_STATE_READY;
  490. /* Process unlocked */
  491. __HAL_UNLOCK(hnand);
  492. return HAL_OK;
  493. }
  494. /**
  495. * @brief Read Page(s) from NAND memory block (16-bits addressing)
  496. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  497. * the configuration information for NAND module.
  498. * @param pAddress : pointer to NAND address structure
  499. * @param pBuffer : pointer to destination read buffer. pBuffer should be 16bits aligned
  500. * @param NumPageToRead : number of pages to read from block
  501. * @retval HAL status
  502. */
  503. HAL_StatusTypeDef HAL_NAND_Read_Page_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer, uint32_t NumPageToRead)
  504. {
  505. __IO uint32_t index = 0U;
  506. uint32_t tickstart = 0U;
  507. uint32_t deviceaddress = 0U, size = 0U, numPagesRead = 0U, nandaddress = 0U;
  508. /* Process Locked */
  509. __HAL_LOCK(hnand);
  510. /* Check the NAND controller state */
  511. if(hnand->State == HAL_NAND_STATE_BUSY)
  512. {
  513. return HAL_BUSY;
  514. }
  515. /* Identify the device address */
  516. if(hnand->Init.NandBank == FSMC_NAND_BANK2)
  517. {
  518. deviceaddress = NAND_DEVICE1;
  519. }
  520. else
  521. {
  522. deviceaddress = NAND_DEVICE2;
  523. }
  524. /* Update the NAND controller state */
  525. hnand->State = HAL_NAND_STATE_BUSY;
  526. /* NAND raw address calculation */
  527. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  528. /* Page(s) read loop */
  529. while((NumPageToRead != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  530. {
  531. /* update the buffer size */
  532. size = (hnand->Config.PageSize) + ((hnand->Config.PageSize) * numPagesRead);
  533. /* Send read page command sequence */
  534. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  535. /* Cards with page size <= 512 bytes */
  536. if((hnand->Config.PageSize) <= 512U)
  537. {
  538. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  539. {
  540. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  541. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  542. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  543. }
  544. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  545. {
  546. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  547. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  548. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  549. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  550. }
  551. }
  552. else /* (hnand->Config.PageSize) > 512 */
  553. {
  554. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  555. {
  556. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  557. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  558. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  559. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  560. }
  561. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  562. {
  563. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  564. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  565. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  566. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  567. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  568. }
  569. }
  570. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  571. if(hnand->Config.ExtraCommandEnable == ENABLE)
  572. {
  573. /* Get tick */
  574. tickstart = HAL_GetTick();
  575. /* Read status until NAND is ready */
  576. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  577. {
  578. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  579. {
  580. return HAL_TIMEOUT;
  581. }
  582. }
  583. /* Go back to read mode */
  584. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  585. }
  586. /* Get Data into Buffer */
  587. for(; index < size; index++)
  588. {
  589. *(uint16_t *)pBuffer++ = *(uint16_t *)deviceaddress;
  590. }
  591. /* Increment read pages number */
  592. numPagesRead++;
  593. /* Decrement pages to read */
  594. NumPageToRead--;
  595. /* Increment the NAND address */
  596. nandaddress = (uint32_t)(nandaddress + 1U);
  597. }
  598. /* Update the NAND controller state */
  599. hnand->State = HAL_NAND_STATE_READY;
  600. /* Process unlocked */
  601. __HAL_UNLOCK(hnand);
  602. return HAL_OK;
  603. }
  604. /**
  605. * @brief Write Page(s) to NAND memory block (8-bits addressing)
  606. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  607. * the configuration information for NAND module.
  608. * @param pAddress : pointer to NAND address structure
  609. * @param pBuffer : pointer to source buffer to write
  610. * @param NumPageToWrite : number of pages to write to block
  611. * @retval HAL status
  612. */
  613. HAL_StatusTypeDef HAL_NAND_Write_Page_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumPageToWrite)
  614. {
  615. __IO uint32_t index = 0U;
  616. uint32_t tickstart = 0U;
  617. uint32_t deviceaddress = 0U, size = 0U, numPagesWritten = 0U, nandaddress = 0U;
  618. /* Process Locked */
  619. __HAL_LOCK(hnand);
  620. /* Check the NAND controller state */
  621. if(hnand->State == HAL_NAND_STATE_BUSY)
  622. {
  623. return HAL_BUSY;
  624. }
  625. /* Identify the device address */
  626. if(hnand->Init.NandBank == FSMC_NAND_BANK2)
  627. {
  628. deviceaddress = NAND_DEVICE1;
  629. }
  630. else
  631. {
  632. deviceaddress = NAND_DEVICE2;
  633. }
  634. /* Update the NAND controller state */
  635. hnand->State = HAL_NAND_STATE_BUSY;
  636. /* NAND raw address calculation */
  637. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  638. /* Page(s) write loop */
  639. while((NumPageToWrite != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  640. {
  641. /* update the buffer size */
  642. size = hnand->Config.PageSize + ((hnand->Config.PageSize) * numPagesWritten);
  643. /* Send write page command sequence */
  644. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  645. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  646. /* Cards with page size <= 512 bytes */
  647. if((hnand->Config.PageSize) <= 512U)
  648. {
  649. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  650. {
  651. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  652. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  653. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  654. }
  655. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  656. {
  657. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  658. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  659. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  660. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  661. }
  662. }
  663. else /* (hnand->Config.PageSize) > 512 */
  664. {
  665. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  666. {
  667. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  668. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  669. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  670. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  671. }
  672. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  673. {
  674. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  675. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  676. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  677. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  678. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  679. }
  680. }
  681. /* Write data to memory */
  682. for(; index < size; index++)
  683. {
  684. *(__IO uint8_t *)deviceaddress = *(uint8_t *)pBuffer++;
  685. }
  686. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  687. /* Read status until NAND is ready */
  688. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  689. {
  690. /* Get tick */
  691. tickstart = HAL_GetTick();
  692. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  693. {
  694. return HAL_TIMEOUT;
  695. }
  696. }
  697. /* Increment written pages number */
  698. numPagesWritten++;
  699. /* Decrement pages to write */
  700. NumPageToWrite--;
  701. /* Increment the NAND address */
  702. nandaddress = (uint32_t)(nandaddress + 1U);
  703. }
  704. /* Update the NAND controller state */
  705. hnand->State = HAL_NAND_STATE_READY;
  706. /* Process unlocked */
  707. __HAL_UNLOCK(hnand);
  708. return HAL_OK;
  709. }
  710. /**
  711. * @brief Write Page(s) to NAND memory block (16-bits addressing)
  712. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  713. * the configuration information for NAND module.
  714. * @param pAddress : pointer to NAND address structure
  715. * @param pBuffer : pointer to source buffer to write. pBuffer should be 16bits aligned
  716. * @param NumPageToWrite : number of pages to write to block
  717. * @retval HAL status
  718. */
  719. HAL_StatusTypeDef HAL_NAND_Write_Page_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer, uint32_t NumPageToWrite)
  720. {
  721. __IO uint32_t index = 0U;
  722. uint32_t tickstart = 0U;
  723. uint32_t deviceaddress = 0U, size = 0U, numPagesWritten = 0U, nandaddress = 0U;
  724. /* Process Locked */
  725. __HAL_LOCK(hnand);
  726. /* Check the NAND controller state */
  727. if(hnand->State == HAL_NAND_STATE_BUSY)
  728. {
  729. return HAL_BUSY;
  730. }
  731. /* Identify the device address */
  732. if(hnand->Init.NandBank == FSMC_NAND_BANK2)
  733. {
  734. deviceaddress = NAND_DEVICE1;
  735. }
  736. else
  737. {
  738. deviceaddress = NAND_DEVICE2;
  739. }
  740. /* Update the NAND controller state */
  741. hnand->State = HAL_NAND_STATE_BUSY;
  742. /* NAND raw address calculation */
  743. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  744. /* Page(s) write loop */
  745. while((NumPageToWrite != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  746. {
  747. /* update the buffer size */
  748. size = (hnand->Config.PageSize) + ((hnand->Config.PageSize) * numPagesWritten);
  749. /* Send write page command sequence */
  750. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  751. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  752. /* Cards with page size <= 512 bytes */
  753. if((hnand->Config.PageSize) <= 512U)
  754. {
  755. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  756. {
  757. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  758. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  759. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  760. }
  761. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  762. {
  763. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  764. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  765. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  766. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  767. }
  768. }
  769. else /* (hnand->Config.PageSize) > 512 */
  770. {
  771. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  772. {
  773. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  774. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  775. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  776. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  777. }
  778. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  779. {
  780. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  781. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  782. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  783. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  784. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  785. }
  786. }
  787. /* Write data to memory */
  788. for(; index < size; index++)
  789. {
  790. *(__IO uint16_t *)deviceaddress = *(uint16_t *)pBuffer++;
  791. }
  792. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  793. /* Read status until NAND is ready */
  794. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  795. {
  796. /* Get tick */
  797. tickstart = HAL_GetTick();
  798. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  799. {
  800. return HAL_TIMEOUT;
  801. }
  802. }
  803. /* Increment written pages number */
  804. numPagesWritten++;
  805. /* Decrement pages to write */
  806. NumPageToWrite--;
  807. /* Increment the NAND address */
  808. nandaddress = (uint32_t)(nandaddress + 1U);
  809. }
  810. /* Update the NAND controller state */
  811. hnand->State = HAL_NAND_STATE_READY;
  812. /* Process unlocked */
  813. __HAL_UNLOCK(hnand);
  814. return HAL_OK;
  815. }
  816. /**
  817. * @brief Read Spare area(s) from NAND memory (8-bits addressing)
  818. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  819. * the configuration information for NAND module.
  820. * @param pAddress : pointer to NAND address structure
  821. * @param pBuffer: pointer to source buffer to write
  822. * @param NumSpareAreaToRead: Number of spare area to read
  823. * @retval HAL status
  824. */
  825. HAL_StatusTypeDef HAL_NAND_Read_SpareArea_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumSpareAreaToRead)
  826. {
  827. __IO uint32_t index = 0U;
  828. uint32_t tickstart = 0U;
  829. uint32_t deviceaddress = 0U, size = 0U, numSpareAreaRead = 0U, nandaddress = 0U, columnaddress = 0U;
  830. /* Process Locked */
  831. __HAL_LOCK(hnand);
  832. /* Check the NAND controller state */
  833. if(hnand->State == HAL_NAND_STATE_BUSY)
  834. {
  835. return HAL_BUSY;
  836. }
  837. /* Identify the device address */
  838. if(hnand->Init.NandBank == FSMC_NAND_BANK2)
  839. {
  840. deviceaddress = NAND_DEVICE1;
  841. }
  842. else
  843. {
  844. deviceaddress = NAND_DEVICE2;
  845. }
  846. /* Update the NAND controller state */
  847. hnand->State = HAL_NAND_STATE_BUSY;
  848. /* NAND raw address calculation */
  849. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  850. /* Column in page address */
  851. columnaddress = COLUMN_ADDRESS(hnand);
  852. /* Spare area(s) read loop */
  853. while((NumSpareAreaToRead != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  854. {
  855. /* update the buffer size */
  856. size = (hnand->Config.SpareAreaSize) + ((hnand->Config.SpareAreaSize) * numSpareAreaRead);
  857. /* Cards with page size <= 512 bytes */
  858. if((hnand->Config.PageSize) <= 512U)
  859. {
  860. /* Send read spare area command sequence */
  861. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  862. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  863. {
  864. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  865. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  866. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  867. }
  868. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  869. {
  870. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  871. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  872. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  873. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  874. }
  875. }
  876. else /* (hnand->Config.PageSize) > 512 */
  877. {
  878. /* Send read spare area command sequence */
  879. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  880. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  881. {
  882. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  883. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  884. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  885. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  886. }
  887. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  888. {
  889. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  890. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  891. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  892. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  893. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  894. }
  895. }
  896. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  897. if(hnand->Config.ExtraCommandEnable == ENABLE)
  898. {
  899. /* Get tick */
  900. tickstart = HAL_GetTick();
  901. /* Read status until NAND is ready */
  902. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  903. {
  904. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  905. {
  906. return HAL_TIMEOUT;
  907. }
  908. }
  909. /* Go back to read mode */
  910. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  911. }
  912. /* Get Data into Buffer */
  913. for(; index < size; index++)
  914. {
  915. *(uint8_t *)pBuffer++ = *(uint8_t *)deviceaddress;
  916. }
  917. /* Increment read spare areas number */
  918. numSpareAreaRead++;
  919. /* Decrement spare areas to read */
  920. NumSpareAreaToRead--;
  921. /* Increment the NAND address */
  922. nandaddress = (uint32_t)(nandaddress + 1U);
  923. }
  924. /* Update the NAND controller state */
  925. hnand->State = HAL_NAND_STATE_READY;
  926. /* Process unlocked */
  927. __HAL_UNLOCK(hnand);
  928. return HAL_OK;
  929. }
  930. /**
  931. * @brief Read Spare area(s) from NAND memory (16-bits addressing)
  932. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  933. * the configuration information for NAND module.
  934. * @param pAddress : pointer to NAND address structure
  935. * @param pBuffer: pointer to source buffer to write. pBuffer should be 16bits aligned.
  936. * @param NumSpareAreaToRead: Number of spare area to read
  937. * @retval HAL status
  938. */
  939. HAL_StatusTypeDef HAL_NAND_Read_SpareArea_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer, uint32_t NumSpareAreaToRead)
  940. {
  941. __IO uint32_t index = 0U;
  942. uint32_t tickstart = 0U;
  943. uint32_t deviceaddress = 0U, size = 0U, numSpareAreaRead = 0U, nandaddress = 0U, columnaddress = 0U;
  944. /* Process Locked */
  945. __HAL_LOCK(hnand);
  946. /* Check the NAND controller state */
  947. if(hnand->State == HAL_NAND_STATE_BUSY)
  948. {
  949. return HAL_BUSY;
  950. }
  951. /* Identify the device address */
  952. if(hnand->Init.NandBank == FSMC_NAND_BANK2)
  953. {
  954. deviceaddress = NAND_DEVICE1;
  955. }
  956. else
  957. {
  958. deviceaddress = NAND_DEVICE2;
  959. }
  960. /* Update the NAND controller state */
  961. hnand->State = HAL_NAND_STATE_BUSY;
  962. /* NAND raw address calculation */
  963. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  964. /* Column in page address */
  965. columnaddress = (uint32_t)(COLUMN_ADDRESS(hnand) * 2U);
  966. /* Spare area(s) read loop */
  967. while((NumSpareAreaToRead != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  968. {
  969. /* update the buffer size */
  970. size = (hnand->Config.SpareAreaSize) + ((hnand->Config.SpareAreaSize) * numSpareAreaRead);
  971. /* Cards with page size <= 512 bytes */
  972. if((hnand->Config.PageSize) <= 512U)
  973. {
  974. /* Send read spare area command sequence */
  975. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  976. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  977. {
  978. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  979. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  980. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  981. }
  982. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  983. {
  984. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  985. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  986. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  987. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  988. }
  989. }
  990. else /* (hnand->Config.PageSize) > 512 */
  991. {
  992. /* Send read spare area command sequence */
  993. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  994. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  995. {
  996. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  997. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  998. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  999. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1000. }
  1001. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1002. {
  1003. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1004. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1005. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1006. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1007. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1008. }
  1009. }
  1010. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  1011. if(hnand->Config.ExtraCommandEnable == ENABLE)
  1012. {
  1013. /* Get tick */
  1014. tickstart = HAL_GetTick();
  1015. /* Read status until NAND is ready */
  1016. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  1017. {
  1018. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  1019. {
  1020. return HAL_TIMEOUT;
  1021. }
  1022. }
  1023. /* Go back to read mode */
  1024. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  1025. }
  1026. /* Get Data into Buffer */
  1027. for(; index < size; index++)
  1028. {
  1029. *(uint16_t *)pBuffer++ = *(uint16_t *)deviceaddress;
  1030. }
  1031. /* Increment read spare areas number */
  1032. numSpareAreaRead++;
  1033. /* Decrement spare areas to read */
  1034. NumSpareAreaToRead--;
  1035. /* Increment the NAND address */
  1036. nandaddress = (uint32_t)(nandaddress + 1U);
  1037. }
  1038. /* Update the NAND controller state */
  1039. hnand->State = HAL_NAND_STATE_READY;
  1040. /* Process unlocked */
  1041. __HAL_UNLOCK(hnand);
  1042. return HAL_OK;
  1043. }
  1044. /**
  1045. * @brief Write Spare area(s) to NAND memory (8-bits addressing)
  1046. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  1047. * the configuration information for NAND module.
  1048. * @param pAddress : pointer to NAND address structure
  1049. * @param pBuffer : pointer to source buffer to write
  1050. * @param NumSpareAreaTowrite : number of spare areas to write to block
  1051. * @retval HAL status
  1052. */
  1053. HAL_StatusTypeDef HAL_NAND_Write_SpareArea_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumSpareAreaTowrite)
  1054. {
  1055. __IO uint32_t index = 0U;
  1056. uint32_t tickstart = 0U;
  1057. uint32_t deviceaddress = 0U, size = 0U, numSpareAreaWritten = 0U, nandaddress = 0U, columnaddress = 0U;
  1058. /* Process Locked */
  1059. __HAL_LOCK(hnand);
  1060. /* Check the NAND controller state */
  1061. if(hnand->State == HAL_NAND_STATE_BUSY)
  1062. {
  1063. return HAL_BUSY;
  1064. }
  1065. /* Identify the device address */
  1066. if(hnand->Init.NandBank == FSMC_NAND_BANK2)
  1067. {
  1068. deviceaddress = NAND_DEVICE1;
  1069. }
  1070. else
  1071. {
  1072. deviceaddress = NAND_DEVICE2;
  1073. }
  1074. /* Update the FSMC_NAND controller state */
  1075. hnand->State = HAL_NAND_STATE_BUSY;
  1076. /* Page address calculation */
  1077. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1078. /* Column in page address */
  1079. columnaddress = COLUMN_ADDRESS(hnand);
  1080. /* Spare area(s) write loop */
  1081. while((NumSpareAreaTowrite != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1082. {
  1083. /* update the buffer size */
  1084. size = (hnand->Config.SpareAreaSize) + ((hnand->Config.SpareAreaSize) * numSpareAreaWritten);
  1085. /* Cards with page size <= 512 bytes */
  1086. if((hnand->Config.PageSize) <= 512U)
  1087. {
  1088. /* Send write Spare area command sequence */
  1089. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1090. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1091. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1092. {
  1093. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1094. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1095. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1096. }
  1097. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1098. {
  1099. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1100. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1101. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1102. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1103. }
  1104. }
  1105. else /* (hnand->Config.PageSize) > 512 */
  1106. {
  1107. /* Send write Spare area command sequence */
  1108. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1109. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1110. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1111. {
  1112. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1113. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1114. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1115. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1116. }
  1117. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1118. {
  1119. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1120. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1121. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1122. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1123. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1124. }
  1125. }
  1126. /* Write data to memory */
  1127. for(; index < size; index++)
  1128. {
  1129. *(__IO uint8_t *)deviceaddress = *(uint8_t *)pBuffer++;
  1130. }
  1131. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  1132. /* Get tick */
  1133. tickstart = HAL_GetTick();
  1134. /* Read status until NAND is ready */
  1135. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  1136. {
  1137. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  1138. {
  1139. return HAL_TIMEOUT;
  1140. }
  1141. }
  1142. /* Increment written spare areas number */
  1143. numSpareAreaWritten++;
  1144. /* Decrement spare areas to write */
  1145. NumSpareAreaTowrite--;
  1146. /* Increment the NAND address */
  1147. nandaddress = (uint32_t)(nandaddress + 1U);
  1148. }
  1149. /* Update the NAND controller state */
  1150. hnand->State = HAL_NAND_STATE_READY;
  1151. /* Process unlocked */
  1152. __HAL_UNLOCK(hnand);
  1153. return HAL_OK;
  1154. }
  1155. /**
  1156. * @brief Write Spare area(s) to NAND memory (16-bits addressing)
  1157. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  1158. * the configuration information for NAND module.
  1159. * @param pAddress : pointer to NAND address structure
  1160. * @param pBuffer : pointer to source buffer to write. pBuffer should be 16bits aligned.
  1161. * @param NumSpareAreaTowrite : number of spare areas to write to block
  1162. * @retval HAL status
  1163. */
  1164. HAL_StatusTypeDef HAL_NAND_Write_SpareArea_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer, uint32_t NumSpareAreaTowrite)
  1165. {
  1166. __IO uint32_t index = 0U;
  1167. uint32_t tickstart = 0U;
  1168. uint32_t deviceaddress = 0U, size = 0U, numSpareAreaWritten = 0U, nandaddress = 0U, columnaddress = 0U;
  1169. /* Process Locked */
  1170. __HAL_LOCK(hnand);
  1171. /* Check the NAND controller state */
  1172. if(hnand->State == HAL_NAND_STATE_BUSY)
  1173. {
  1174. return HAL_BUSY;
  1175. }
  1176. /* Identify the device address */
  1177. if(hnand->Init.NandBank == FSMC_NAND_BANK2)
  1178. {
  1179. deviceaddress = NAND_DEVICE1;
  1180. }
  1181. else
  1182. {
  1183. deviceaddress = NAND_DEVICE2;
  1184. }
  1185. /* Update the FSMC_NAND controller state */
  1186. hnand->State = HAL_NAND_STATE_BUSY;
  1187. /* NAND raw address calculation */
  1188. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1189. /* Column in page address */
  1190. columnaddress = (uint32_t)(COLUMN_ADDRESS(hnand) * 2U);
  1191. /* Spare area(s) write loop */
  1192. while((NumSpareAreaTowrite != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1193. {
  1194. /* update the buffer size */
  1195. size = (hnand->Config.SpareAreaSize) + ((hnand->Config.SpareAreaSize) * numSpareAreaWritten);
  1196. /* Cards with page size <= 512 bytes */
  1197. if((hnand->Config.PageSize) <= 512U)
  1198. {
  1199. /* Send write Spare area command sequence */
  1200. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1201. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1202. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1203. {
  1204. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1205. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1206. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1207. }
  1208. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1209. {
  1210. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1211. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1212. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1213. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1214. }
  1215. }
  1216. else /* (hnand->Config.PageSize) > 512 */
  1217. {
  1218. /* Send write Spare area command sequence */
  1219. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1220. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1221. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1222. {
  1223. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1224. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1225. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1226. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1227. }
  1228. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1229. {
  1230. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1231. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1232. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1233. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1234. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1235. }
  1236. }
  1237. /* Write data to memory */
  1238. for(; index < size; index++)
  1239. {
  1240. *(__IO uint16_t *)deviceaddress = *(uint16_t *)pBuffer++;
  1241. }
  1242. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  1243. /* Read status until NAND is ready */
  1244. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  1245. {
  1246. /* Get tick */
  1247. tickstart = HAL_GetTick();
  1248. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  1249. {
  1250. return HAL_TIMEOUT;
  1251. }
  1252. }
  1253. /* Increment written spare areas number */
  1254. numSpareAreaWritten++;
  1255. /* Decrement spare areas to write */
  1256. NumSpareAreaTowrite--;
  1257. /* Increment the NAND address */
  1258. nandaddress = (uint32_t)(nandaddress + 1U);
  1259. }
  1260. /* Update the NAND controller state */
  1261. hnand->State = HAL_NAND_STATE_READY;
  1262. /* Process unlocked */
  1263. __HAL_UNLOCK(hnand);
  1264. return HAL_OK;
  1265. }
  1266. /**
  1267. * @brief NAND memory Block erase
  1268. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  1269. * the configuration information for NAND module.
  1270. * @param pAddress : pointer to NAND address structure
  1271. * @retval HAL status
  1272. */
  1273. HAL_StatusTypeDef HAL_NAND_Erase_Block(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress)
  1274. {
  1275. uint32_t deviceaddress = 0U;
  1276. uint32_t tickstart = 0U;
  1277. /* Process Locked */
  1278. __HAL_LOCK(hnand);
  1279. /* Check the NAND controller state */
  1280. if(hnand->State == HAL_NAND_STATE_BUSY)
  1281. {
  1282. return HAL_BUSY;
  1283. }
  1284. /* Identify the device address */
  1285. if(hnand->Init.NandBank == FSMC_NAND_BANK2)
  1286. {
  1287. deviceaddress = NAND_DEVICE1;
  1288. }
  1289. else
  1290. {
  1291. deviceaddress = NAND_DEVICE2;
  1292. }
  1293. /* Update the NAND controller state */
  1294. hnand->State = HAL_NAND_STATE_BUSY;
  1295. /* Send Erase block command sequence */
  1296. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE0;
  1297. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1298. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1299. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1300. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE1;
  1301. /* Update the NAND controller state */
  1302. hnand->State = HAL_NAND_STATE_READY;
  1303. /* Get tick */
  1304. tickstart = HAL_GetTick();
  1305. /* Read status until NAND is ready */
  1306. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  1307. {
  1308. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  1309. {
  1310. /* Process unlocked */
  1311. __HAL_UNLOCK(hnand);
  1312. return HAL_TIMEOUT;
  1313. }
  1314. }
  1315. /* Process unlocked */
  1316. __HAL_UNLOCK(hnand);
  1317. return HAL_OK;
  1318. }
  1319. /**
  1320. * @brief NAND memory read status
  1321. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  1322. * the configuration information for NAND module.
  1323. * @retval NAND status
  1324. */
  1325. uint32_t HAL_NAND_Read_Status(NAND_HandleTypeDef *hnand)
  1326. {
  1327. uint32_t data = 0U;
  1328. uint32_t deviceaddress = 0U;
  1329. /* Identify the device address */
  1330. if(hnand->Init.NandBank == FSMC_NAND_BANK2)
  1331. {
  1332. deviceaddress = NAND_DEVICE1;
  1333. }
  1334. else
  1335. {
  1336. deviceaddress = NAND_DEVICE2;
  1337. }
  1338. /* Send Read status operation command */
  1339. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_STATUS;
  1340. /* Read status register data */
  1341. data = *(__IO uint8_t *)deviceaddress;
  1342. /* Return the status */
  1343. if((data & NAND_ERROR) == NAND_ERROR)
  1344. {
  1345. return NAND_ERROR;
  1346. }
  1347. else if((data & NAND_READY) == NAND_READY)
  1348. {
  1349. return NAND_READY;
  1350. }
  1351. return NAND_BUSY;
  1352. }
  1353. /**
  1354. * @brief Increment the NAND memory address
  1355. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  1356. * the configuration information for NAND module.
  1357. * @param pAddress: pointer to NAND address structure
  1358. * @retval The new status of the increment address operation. It can be:
  1359. * - NAND_VALID_ADDRESS: When the new address is valid address
  1360. * - NAND_INVALID_ADDRESS: When the new address is invalid address
  1361. */
  1362. uint32_t HAL_NAND_Address_Inc(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress)
  1363. {
  1364. uint32_t status = NAND_VALID_ADDRESS;
  1365. /* Increment page address */
  1366. pAddress->Page++;
  1367. /* Check NAND address is valid */
  1368. if(pAddress->Page == hnand->Config.BlockSize)
  1369. {
  1370. pAddress->Page = 0U;
  1371. pAddress->Block++;
  1372. if(pAddress->Block == hnand->Config.PlaneSize)
  1373. {
  1374. pAddress->Block = 0U;
  1375. pAddress->Plane++;
  1376. if(pAddress->Plane == (hnand->Config.PlaneNbr))
  1377. {
  1378. status = NAND_INVALID_ADDRESS;
  1379. }
  1380. }
  1381. }
  1382. return (status);
  1383. }
  1384. /**
  1385. * @}
  1386. */
  1387. /** @defgroup NAND_Exported_Functions_Group3 Peripheral Control functions
  1388. * @brief management functions
  1389. *
  1390. @verbatim
  1391. ==============================================================================
  1392. ##### NAND Control functions #####
  1393. ==============================================================================
  1394. [..]
  1395. This subsection provides a set of functions allowing to control dynamically
  1396. the NAND interface.
  1397. @endverbatim
  1398. * @{
  1399. */
  1400. /**
  1401. * @brief Enables dynamically NAND ECC feature.
  1402. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  1403. * the configuration information for NAND module.
  1404. * @retval HAL status
  1405. */
  1406. HAL_StatusTypeDef HAL_NAND_ECC_Enable(NAND_HandleTypeDef *hnand)
  1407. {
  1408. /* Check the NAND controller state */
  1409. if(hnand->State == HAL_NAND_STATE_BUSY)
  1410. {
  1411. return HAL_BUSY;
  1412. }
  1413. /* Update the NAND state */
  1414. hnand->State = HAL_NAND_STATE_BUSY;
  1415. /* Enable ECC feature */
  1416. FSMC_NAND_ECC_Enable(hnand->Instance, hnand->Init.NandBank);
  1417. /* Update the NAND state */
  1418. hnand->State = HAL_NAND_STATE_READY;
  1419. return HAL_OK;
  1420. }
  1421. /**
  1422. * @brief Disables dynamically FSMC_NAND ECC feature.
  1423. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  1424. * the configuration information for NAND module.
  1425. * @retval HAL status
  1426. */
  1427. HAL_StatusTypeDef HAL_NAND_ECC_Disable(NAND_HandleTypeDef *hnand)
  1428. {
  1429. /* Check the NAND controller state */
  1430. if(hnand->State == HAL_NAND_STATE_BUSY)
  1431. {
  1432. return HAL_BUSY;
  1433. }
  1434. /* Update the NAND state */
  1435. hnand->State = HAL_NAND_STATE_BUSY;
  1436. /* Disable ECC feature */
  1437. FSMC_NAND_ECC_Disable(hnand->Instance, hnand->Init.NandBank);
  1438. /* Update the NAND state */
  1439. hnand->State = HAL_NAND_STATE_READY;
  1440. return HAL_OK;
  1441. }
  1442. /**
  1443. * @brief Disables dynamically NAND ECC feature.
  1444. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  1445. * the configuration information for NAND module.
  1446. * @param ECCval: pointer to ECC value
  1447. * @param Timeout: maximum timeout to wait
  1448. * @retval HAL status
  1449. */
  1450. HAL_StatusTypeDef HAL_NAND_GetECC(NAND_HandleTypeDef *hnand, uint32_t *ECCval, uint32_t Timeout)
  1451. {
  1452. HAL_StatusTypeDef status = HAL_OK;
  1453. /* Check the NAND controller state */
  1454. if(hnand->State == HAL_NAND_STATE_BUSY)
  1455. {
  1456. return HAL_BUSY;
  1457. }
  1458. /* Update the NAND state */
  1459. hnand->State = HAL_NAND_STATE_BUSY;
  1460. /* Get NAND ECC value */
  1461. status = FSMC_NAND_GetECC(hnand->Instance, ECCval, hnand->Init.NandBank, Timeout);
  1462. /* Update the NAND state */
  1463. hnand->State = HAL_NAND_STATE_READY;
  1464. return status;
  1465. }
  1466. /**
  1467. * @}
  1468. */
  1469. /** @defgroup NAND_Exported_Functions_Group4 Peripheral State functions
  1470. * @brief Peripheral State functions
  1471. *
  1472. @verbatim
  1473. ==============================================================================
  1474. ##### NAND State functions #####
  1475. ==============================================================================
  1476. [..]
  1477. This subsection permits to get in run-time the status of the NAND controller
  1478. and the data flow.
  1479. @endverbatim
  1480. * @{
  1481. */
  1482. /**
  1483. * @brief return the NAND state
  1484. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  1485. * the configuration information for NAND module.
  1486. * @retval HAL state
  1487. */
  1488. HAL_NAND_StateTypeDef HAL_NAND_GetState(NAND_HandleTypeDef *hnand)
  1489. {
  1490. return hnand->State;
  1491. }
  1492. /**
  1493. * @}
  1494. */
  1495. /**
  1496. * @}
  1497. */
  1498. /**
  1499. * @}
  1500. */
  1501. #endif /* STM32F101xE || STM32F103xE || STM32F101xG || STM32F103xG */
  1502. #endif /* HAL_NAND_MODULE_ENABLED */
  1503. /**
  1504. * @}
  1505. */
  1506. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/