stm32l4xx_hal_nand.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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 FMC 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()/HAL_NAND_Read_SpareArea(), HAL_NAND_Write_Page()/HAL_NAND_Write_SpareArea()
  24. to read/write page(s)/spare area(s). These functions use specific device
  25. information (Block, page size..) predefined by the user in the HAL_NAND_Info_TypeDef
  26. structure. The read/write address information is contained by the Nand_Address_Typedef
  27. structure passed as parameter.
  28. (+) Perform NAND flash Reset chip operation using the function HAL_NAND_Reset().
  29. (+) Perform NAND flash erase block operation using the function HAL_NAND_Erase_Block().
  30. The erase block address information is contained in the Nand_Address_Typedef
  31. structure passed as parameter.
  32. (+) Read the NAND flash status operation using the function HAL_NAND_Read_Status().
  33. (+) You can also control the NAND device by calling the control APIs HAL_NAND_ECC_Enable()/
  34. HAL_NAND_ECC_Disable() to respectively enable/disable the ECC code correction
  35. feature or the function HAL_NAND_GetECC() to get the ECC correction code.
  36. (+) You can monitor the NAND device HAL state by calling the function
  37. HAL_NAND_GetState()
  38. [..]
  39. (@) This driver is a set of generic APIs which handle standard NAND flash operations.
  40. If a NAND flash device contains different operations and/or implementations,
  41. it should be implemented separately.
  42. @endverbatim
  43. ******************************************************************************
  44. * @attention
  45. *
  46. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  47. *
  48. * Redistribution and use in source and binary forms, with or without modification,
  49. * are permitted provided that the following conditions are met:
  50. * 1. Redistributions of source code must retain the above copyright notice,
  51. * this list of conditions and the following disclaimer.
  52. * 2. Redistributions in binary form must reproduce the above copyright notice,
  53. * this list of conditions and the following disclaimer in the documentation
  54. * and/or other materials provided with the distribution.
  55. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  56. * may be used to endorse or promote products derived from this software
  57. * without specific prior written permission.
  58. *
  59. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  60. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  61. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  62. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  63. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  64. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  65. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  66. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  67. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  68. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  69. *
  70. ******************************************************************************
  71. */
  72. /* Includes ------------------------------------------------------------------*/
  73. #include "stm32l4xx_hal.h"
  74. #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || \
  75. defined(STM32L496xx) || defined(STM32L4A6xx) || \
  76. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  77. /** @addtogroup STM32L4xx_HAL_Driver
  78. * @{
  79. */
  80. #ifdef HAL_NAND_MODULE_ENABLED
  81. /** @defgroup NAND NAND
  82. * @brief NAND HAL module driver
  83. * @{
  84. */
  85. /* Private typedef -----------------------------------------------------------*/
  86. /* Private define ------------------------------------------------------------*/
  87. /** @defgroup NAND_Private_Constants NAND Private Constants
  88. * @{
  89. */
  90. /**
  91. * @}
  92. */
  93. /* Private macro -------------------------------------------------------------*/
  94. /** @defgroup NAND_Private_Macros NAND Private Macros
  95. * @{
  96. */
  97. /**
  98. * @}
  99. */
  100. /* Private variables ---------------------------------------------------------*/
  101. /* Private function prototypes -----------------------------------------------*/
  102. /** @defgroup NAND_Private_Functions NAND Private Functions
  103. * @{
  104. */
  105. static uint32_t NAND_AddressIncrement(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef* Address);
  106. /**
  107. * @}
  108. */
  109. /* Exported functions ---------------------------------------------------------*/
  110. /** @defgroup NAND_Exported_Functions NAND Exported Functions
  111. * @{
  112. */
  113. /** @defgroup NAND_Exported_Functions_Group1 Initialization and de-initialization functions
  114. * @brief Initialization and Configuration functions
  115. *
  116. @verbatim
  117. ==============================================================================
  118. ##### NAND Initialization and de-initialization functions #####
  119. ==============================================================================
  120. [..]
  121. This section provides functions allowing to initialize/de-initialize
  122. the NAND memory
  123. @endverbatim
  124. * @{
  125. */
  126. /**
  127. * @brief Perform NAND memory Initialization sequence.
  128. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  129. * the configuration information for NAND module.
  130. * @param ComSpace_Timing: pointer to Common space timing structure
  131. * @param AttSpace_Timing: pointer to Attribute space timing structure
  132. * @retval HAL status
  133. */
  134. HAL_StatusTypeDef HAL_NAND_Init(NAND_HandleTypeDef *hnand, FMC_NAND_PCC_TimingTypeDef *ComSpace_Timing, FMC_NAND_PCC_TimingTypeDef *AttSpace_Timing)
  135. {
  136. /* Check the NAND handle state */
  137. if(hnand == NULL)
  138. {
  139. return HAL_ERROR;
  140. }
  141. if(hnand->State == HAL_NAND_STATE_RESET)
  142. {
  143. /* Allocate lock resource and initialize it */
  144. hnand->Lock = HAL_UNLOCKED;
  145. /* Initialize the low level hardware (MSP) */
  146. HAL_NAND_MspInit(hnand);
  147. }
  148. /* Initialize NAND control Interface */
  149. FMC_NAND_Init(hnand->Instance, &(hnand->Init));
  150. /* Initialize NAND common space timing Interface */
  151. FMC_NAND_CommonSpace_Timing_Init(hnand->Instance, ComSpace_Timing, hnand->Init.NandBank);
  152. /* Initialize NAND attribute space timing Interface */
  153. FMC_NAND_AttributeSpace_Timing_Init(hnand->Instance, AttSpace_Timing, hnand->Init.NandBank);
  154. /* Enable the NAND device */
  155. __FMC_NAND_ENABLE(hnand->Instance, hnand->Init.NandBank);
  156. /* Update the NAND controller state */
  157. hnand->State = HAL_NAND_STATE_READY;
  158. return HAL_OK;
  159. }
  160. /**
  161. * @brief Perform NAND memory De-Initialization sequence.
  162. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  163. * the configuration information for NAND module.
  164. * @retval HAL status
  165. */
  166. HAL_StatusTypeDef HAL_NAND_DeInit(NAND_HandleTypeDef *hnand)
  167. {
  168. /* Initialize the low level hardware (MSP) */
  169. HAL_NAND_MspDeInit(hnand);
  170. /* Configure the NAND registers with their reset values */
  171. FMC_NAND_DeInit(hnand->Instance, hnand->Init.NandBank);
  172. /* Reset the NAND controller state */
  173. hnand->State = HAL_NAND_STATE_RESET;
  174. /* Release Lock */
  175. __HAL_UNLOCK(hnand);
  176. return HAL_OK;
  177. }
  178. /**
  179. * @brief Initialize the NAND MSP.
  180. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  181. * the configuration information for NAND module.
  182. * @retval None
  183. */
  184. __weak void HAL_NAND_MspInit(NAND_HandleTypeDef *hnand)
  185. {
  186. /* Prevent unused argument(s) compilation warning */
  187. UNUSED(hnand);
  188. /* NOTE : This function should not be modified, when the callback is needed,
  189. the HAL_NAND_MspInit could be implemented in the user file
  190. */
  191. }
  192. /**
  193. * @brief DeInitialize the NAND MSP.
  194. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  195. * the configuration information for NAND module.
  196. * @retval None
  197. */
  198. __weak void HAL_NAND_MspDeInit(NAND_HandleTypeDef *hnand)
  199. {
  200. /* Prevent unused argument(s) compilation warning */
  201. UNUSED(hnand);
  202. /* NOTE : This function should not be modified, when the callback is needed,
  203. the HAL_NAND_MspDeInit could be implemented in the user file
  204. */
  205. }
  206. /**
  207. * @brief This function handles NAND device interrupt request.
  208. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  209. * the configuration information for NAND module.
  210. * @retval HAL status
  211. */
  212. void HAL_NAND_IRQHandler(NAND_HandleTypeDef *hnand)
  213. {
  214. /* Check NAND interrupt Rising edge flag */
  215. if(__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_RISING_EDGE))
  216. {
  217. /* NAND interrupt callback*/
  218. HAL_NAND_ITCallback(hnand);
  219. /* Clear NAND interrupt Rising edge pending bit */
  220. __FMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_RISING_EDGE);
  221. }
  222. /* Check NAND interrupt Level flag */
  223. if(__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_LEVEL))
  224. {
  225. /* NAND interrupt callback*/
  226. HAL_NAND_ITCallback(hnand);
  227. /* Clear NAND interrupt Level pending bit */
  228. __FMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_LEVEL);
  229. }
  230. /* Check NAND interrupt Falling edge flag */
  231. if(__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_FALLING_EDGE))
  232. {
  233. /* NAND interrupt callback*/
  234. HAL_NAND_ITCallback(hnand);
  235. /* Clear NAND interrupt Falling edge pending bit */
  236. __FMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_FALLING_EDGE);
  237. }
  238. /* Check NAND interrupt FIFO empty flag */
  239. if(__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_FEMPT))
  240. {
  241. /* NAND interrupt callback*/
  242. HAL_NAND_ITCallback(hnand);
  243. /* Clear NAND interrupt FIFO empty pending bit */
  244. __FMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_FEMPT);
  245. }
  246. }
  247. /**
  248. * @brief NAND interrupt feature callback.
  249. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  250. * the configuration information for NAND module.
  251. * @retval None
  252. */
  253. __weak void HAL_NAND_ITCallback(NAND_HandleTypeDef *hnand)
  254. {
  255. /* Prevent unused argument(s) compilation warning */
  256. UNUSED(hnand);
  257. /* NOTE : This function should not be modified, when the callback is needed,
  258. the HAL_NAND_ITCallback could be implemented in the user file
  259. */
  260. }
  261. /**
  262. * @}
  263. */
  264. /** @defgroup NAND_Exported_Functions_Group2 Input and Output functions
  265. * @brief Input Output and memory control functions
  266. *
  267. @verbatim
  268. ==============================================================================
  269. ##### NAND Input and Output functions #####
  270. ==============================================================================
  271. [..]
  272. This section provides functions allowing to use and control the NAND
  273. memory
  274. @endverbatim
  275. * @{
  276. */
  277. /**
  278. * @brief Read the NAND memory electronic signature.
  279. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  280. * the configuration information for NAND module.
  281. * @param pNAND_ID: NAND ID structure
  282. * @retval HAL status
  283. */
  284. HAL_StatusTypeDef HAL_NAND_Read_ID(NAND_HandleTypeDef *hnand, NAND_IDTypeDef *pNAND_ID)
  285. {
  286. __IO uint32_t data = 0;
  287. uint32_t deviceaddress = 0;
  288. /* Process Locked */
  289. __HAL_LOCK(hnand);
  290. /* Check the NAND controller state */
  291. if(hnand->State == HAL_NAND_STATE_BUSY)
  292. {
  293. return HAL_BUSY;
  294. }
  295. /* Identify the device address */
  296. deviceaddress = NAND_DEVICE;
  297. /* Update the NAND controller state */
  298. hnand->State = HAL_NAND_STATE_BUSY;
  299. /* Send Read ID command sequence */
  300. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_READID;
  301. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  302. /* Read the electronic signature from NAND flash */
  303. data = *(__IO uint32_t *)deviceaddress;
  304. /* Return the data read */
  305. pNAND_ID->Maker_Id = ADDR_1ST_CYCLE(data);
  306. pNAND_ID->Device_Id = ADDR_2ND_CYCLE(data);
  307. pNAND_ID->Third_Id = ADDR_3RD_CYCLE(data);
  308. pNAND_ID->Fourth_Id = ADDR_4TH_CYCLE(data);
  309. /* Update the NAND controller state */
  310. hnand->State = HAL_NAND_STATE_READY;
  311. /* Process unlocked */
  312. __HAL_UNLOCK(hnand);
  313. return HAL_OK;
  314. }
  315. /**
  316. * @brief NAND memory reset.
  317. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  318. * the configuration information for NAND module.
  319. * @retval HAL status
  320. */
  321. HAL_StatusTypeDef HAL_NAND_Reset(NAND_HandleTypeDef *hnand)
  322. {
  323. uint32_t deviceaddress = 0;
  324. /* Process Locked */
  325. __HAL_LOCK(hnand);
  326. /* Check the NAND controller state */
  327. if(hnand->State == HAL_NAND_STATE_BUSY)
  328. {
  329. return HAL_BUSY;
  330. }
  331. /* Identify the device address */
  332. deviceaddress = NAND_DEVICE;
  333. /* Update the NAND controller state */
  334. hnand->State = HAL_NAND_STATE_BUSY;
  335. /* Send NAND reset command */
  336. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = 0xFF;
  337. /* Update the NAND controller state */
  338. hnand->State = HAL_NAND_STATE_READY;
  339. /* Process unlocked */
  340. __HAL_UNLOCK(hnand);
  341. return HAL_OK;
  342. }
  343. /**
  344. * @brief Read Page(s) from NAND memory block.
  345. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  346. * the configuration information for NAND module.
  347. * @param pAddress: pointer to NAND address structure
  348. * @param pBuffer: pointer to destination read buffer
  349. * @param NumPageToRead: number of pages to read from block
  350. * @retval HAL status
  351. */
  352. HAL_StatusTypeDef HAL_NAND_Read_Page(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumPageToRead)
  353. {
  354. __IO uint32_t index = 0;
  355. uint32_t deviceaddress = 0, size = 0, numpagesread = 0, addressstatus = NAND_VALID_ADDRESS;
  356. NAND_AddressTypeDef nandaddress;
  357. uint32_t addressoffset = 0;
  358. /* Process Locked */
  359. __HAL_LOCK(hnand);
  360. /* Check the NAND controller state */
  361. if(hnand->State == HAL_NAND_STATE_BUSY)
  362. {
  363. return HAL_BUSY;
  364. }
  365. /* Identify the device address */
  366. deviceaddress = NAND_DEVICE;
  367. /* Update the NAND controller state */
  368. hnand->State = HAL_NAND_STATE_BUSY;
  369. /* Save the content of pAddress as it will be modified */
  370. nandaddress.Block = pAddress->Block;
  371. nandaddress.Page = pAddress->Page;
  372. nandaddress.Zone = pAddress->Zone;
  373. /* Page(s) read loop */
  374. while((NumPageToRead != 0) && (addressstatus == NAND_VALID_ADDRESS))
  375. {
  376. /* update the buffer size */
  377. size = hnand->Info.PageSize + ((hnand->Info.PageSize) * numpagesread);
  378. /* Get the address offset */
  379. addressoffset = ARRAY_ADDRESS(&nandaddress, hnand);
  380. /* Send read page command sequence */
  381. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  382. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  383. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(addressoffset);
  384. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(addressoffset);
  385. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(addressoffset);
  386. /* for 512 and 1 GB devices, 4th cycle is required */
  387. if(hnand->Info.BlockNbr >= 1024)
  388. {
  389. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_4TH_CYCLE(addressoffset);
  390. }
  391. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  392. /* Get Data into Buffer */
  393. for(; index < size; index++)
  394. {
  395. *(uint8_t *)pBuffer++ = *(uint8_t *)deviceaddress;
  396. }
  397. /* Increment read pages number */
  398. numpagesread++;
  399. /* Decrement pages to read */
  400. NumPageToRead--;
  401. /* Increment the NAND address */
  402. addressstatus = NAND_AddressIncrement(hnand, &nandaddress);
  403. }
  404. /* Update the NAND controller state */
  405. hnand->State = HAL_NAND_STATE_READY;
  406. /* Process unlocked */
  407. __HAL_UNLOCK(hnand);
  408. return HAL_OK;
  409. }
  410. /**
  411. * @brief Write Page(s) to NAND memory block.
  412. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  413. * the configuration information for NAND module.
  414. * @param pAddress: pointer to NAND address structure
  415. * @param pBuffer: pointer to source buffer to write
  416. * @param NumPageToWrite: number of pages to write to block
  417. * @retval HAL status
  418. */
  419. HAL_StatusTypeDef HAL_NAND_Write_Page(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumPageToWrite)
  420. {
  421. __IO uint32_t index = 0;
  422. uint32_t tickstart = 0;
  423. uint32_t deviceaddress = 0 , size = 0, numpageswritten = 0, addressstatus = NAND_VALID_ADDRESS;
  424. NAND_AddressTypeDef nandaddress;
  425. uint32_t addressoffset = 0;
  426. /* Process Locked */
  427. __HAL_LOCK(hnand);
  428. /* Check the NAND controller state */
  429. if(hnand->State == HAL_NAND_STATE_BUSY)
  430. {
  431. return HAL_BUSY;
  432. }
  433. /* Identify the device address */
  434. deviceaddress = NAND_DEVICE;
  435. /* Update the NAND controller state */
  436. hnand->State = HAL_NAND_STATE_BUSY;
  437. /* Save the content of pAddress as it will be modified */
  438. nandaddress.Block = pAddress->Block;
  439. nandaddress.Page = pAddress->Page;
  440. nandaddress.Zone = pAddress->Zone;
  441. /* Page(s) write loop */
  442. while((NumPageToWrite != 0) && (addressstatus == NAND_VALID_ADDRESS))
  443. {
  444. /* update the buffer size */
  445. size = hnand->Info.PageSize + ((hnand->Info.PageSize) * numpageswritten);
  446. /* Get the address offset */
  447. addressoffset = ARRAY_ADDRESS(&nandaddress, hnand);
  448. /* Send write page command sequence */
  449. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  450. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  451. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  452. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(addressoffset);
  453. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(addressoffset);
  454. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(addressoffset);
  455. /* for 512 and 1 GB devices, 4th cycle is required */
  456. if(hnand->Info.BlockNbr >= 1024)
  457. {
  458. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_4TH_CYCLE(addressoffset);
  459. }
  460. /* Write data to memory */
  461. for(; index < size; index++)
  462. {
  463. *(__IO uint8_t *)deviceaddress = *(uint8_t *)pBuffer++;
  464. }
  465. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  466. /* Get tick */
  467. tickstart = HAL_GetTick();
  468. /* Read status until NAND is ready */
  469. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  470. {
  471. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  472. {
  473. return HAL_TIMEOUT;
  474. }
  475. }
  476. /* Increment written pages number */
  477. numpageswritten++;
  478. /* Decrement pages to write */
  479. NumPageToWrite--;
  480. /* Increment the NAND address */
  481. addressstatus = NAND_AddressIncrement(hnand, &nandaddress);
  482. }
  483. /* Update the NAND controller state */
  484. hnand->State = HAL_NAND_STATE_READY;
  485. /* Process unlocked */
  486. __HAL_UNLOCK(hnand);
  487. return HAL_OK;
  488. }
  489. /**
  490. * @brief Read Spare area(s) from NAND memory.
  491. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  492. * the configuration information for NAND module.
  493. * @param pAddress: pointer to NAND address structure
  494. * @param pBuffer: pointer to source buffer to write
  495. * @param NumSpareAreaToRead: Number of spare area to read
  496. * @retval HAL status
  497. */
  498. HAL_StatusTypeDef HAL_NAND_Read_SpareArea(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumSpareAreaToRead)
  499. {
  500. __IO uint32_t index = 0;
  501. uint32_t deviceaddress = 0, size = 0, num_spare_area_read = 0, addressstatus = NAND_VALID_ADDRESS;
  502. NAND_AddressTypeDef nandaddress;
  503. uint32_t addressoffset = 0;
  504. /* Process Locked */
  505. __HAL_LOCK(hnand);
  506. /* Check the NAND controller state */
  507. if(hnand->State == HAL_NAND_STATE_BUSY)
  508. {
  509. return HAL_BUSY;
  510. }
  511. /* Identify the device address */
  512. deviceaddress = NAND_DEVICE;
  513. /* Update the NAND controller state */
  514. hnand->State = HAL_NAND_STATE_BUSY;
  515. /* Save the content of pAddress as it will be modified */
  516. nandaddress.Block = pAddress->Block;
  517. nandaddress.Page = pAddress->Page;
  518. nandaddress.Zone = pAddress->Zone;
  519. /* Spare area(s) read loop */
  520. while((NumSpareAreaToRead != 0) && (addressstatus == NAND_VALID_ADDRESS))
  521. {
  522. /* update the buffer size */
  523. size = (hnand->Info.SpareAreaSize) + ((hnand->Info.SpareAreaSize) * num_spare_area_read);
  524. /* Get the address offset */
  525. addressoffset = ARRAY_ADDRESS(&nandaddress, hnand);
  526. /* Send read spare area command sequence */
  527. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  528. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  529. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(addressoffset);
  530. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(addressoffset);
  531. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(addressoffset);
  532. /* for 512 and 1 GB devices, 4th cycle is required */
  533. if(hnand->Info.BlockNbr >= 1024)
  534. {
  535. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_4TH_CYCLE(addressoffset);
  536. }
  537. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  538. /* Get Data into Buffer */
  539. for ( ;index < size; index++)
  540. {
  541. *(uint8_t *)pBuffer++ = *(uint8_t *)deviceaddress;
  542. }
  543. /* Increment read spare areas number */
  544. num_spare_area_read++;
  545. /* Decrement spare areas to read */
  546. NumSpareAreaToRead--;
  547. /* Increment the NAND address */
  548. addressstatus = NAND_AddressIncrement(hnand, &nandaddress);
  549. }
  550. /* Update the NAND controller state */
  551. hnand->State = HAL_NAND_STATE_READY;
  552. /* Process unlocked */
  553. __HAL_UNLOCK(hnand);
  554. return HAL_OK;
  555. }
  556. /**
  557. * @brief Write Spare area(s) to NAND memory.
  558. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  559. * the configuration information for NAND module.
  560. * @param pAddress: pointer to NAND address structure
  561. * @param pBuffer: pointer to source buffer to write
  562. * @param NumSpareAreaTowrite: number of spare areas to write to block
  563. * @retval HAL status
  564. */
  565. HAL_StatusTypeDef HAL_NAND_Write_SpareArea(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumSpareAreaTowrite)
  566. {
  567. __IO uint32_t index = 0;
  568. uint32_t tickstart = 0;
  569. uint32_t deviceaddress = 0, size = 0, num_spare_area_written = 0, addressstatus = NAND_VALID_ADDRESS;
  570. NAND_AddressTypeDef nandaddress;
  571. uint32_t addressoffset = 0;
  572. /* Process Locked */
  573. __HAL_LOCK(hnand);
  574. /* Check the NAND controller state */
  575. if(hnand->State == HAL_NAND_STATE_BUSY)
  576. {
  577. return HAL_BUSY;
  578. }
  579. /* Identify the device address */
  580. deviceaddress = NAND_DEVICE;
  581. /* Update the FMC_NAND controller state */
  582. hnand->State = HAL_NAND_STATE_BUSY;
  583. /* Save the content of pAddress as it will be modified */
  584. nandaddress.Block = pAddress->Block;
  585. nandaddress.Page = pAddress->Page;
  586. nandaddress.Zone = pAddress->Zone;
  587. /* Spare area(s) write loop */
  588. while((NumSpareAreaTowrite != 0) && (addressstatus == NAND_VALID_ADDRESS))
  589. {
  590. /* update the buffer size */
  591. size = (hnand->Info.SpareAreaSize) + ((hnand->Info.SpareAreaSize) * num_spare_area_written);
  592. /* Get the address offset */
  593. addressoffset = ARRAY_ADDRESS(&nandaddress, hnand);
  594. /* Send write Spare area command sequence */
  595. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  596. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  597. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  598. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(addressoffset);
  599. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(addressoffset);
  600. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(addressoffset);
  601. /* for 512 and 1 GB devices, 4th cycle is required */
  602. if(hnand->Info.BlockNbr >= 1024)
  603. {
  604. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_4TH_CYCLE(addressoffset);
  605. }
  606. /* Write data to memory */
  607. for(; index < size; index++)
  608. {
  609. *(__IO uint8_t *)deviceaddress = *(uint8_t *)pBuffer++;
  610. }
  611. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  612. /* Get tick */
  613. tickstart = HAL_GetTick();
  614. /* Read status until NAND is ready */
  615. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  616. {
  617. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  618. {
  619. return HAL_TIMEOUT;
  620. }
  621. }
  622. /* Increment written spare areas number */
  623. num_spare_area_written++;
  624. /* Decrement spare areas to write */
  625. NumSpareAreaTowrite--;
  626. /* Increment the NAND address */
  627. addressstatus = NAND_AddressIncrement(hnand, &nandaddress);
  628. }
  629. /* Update the NAND controller state */
  630. hnand->State = HAL_NAND_STATE_READY;
  631. /* Process unlocked */
  632. __HAL_UNLOCK(hnand);
  633. return HAL_OK;
  634. }
  635. /**
  636. * @brief NAND memory Block erase.
  637. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  638. * the configuration information for NAND module.
  639. * @param pAddress: pointer to NAND address structure
  640. * @retval HAL status
  641. */
  642. HAL_StatusTypeDef HAL_NAND_Erase_Block(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress)
  643. {
  644. uint32_t deviceaddress = 0;
  645. uint32_t tickstart = 0;
  646. /* Process Locked */
  647. __HAL_LOCK(hnand);
  648. /* Check the NAND controller state */
  649. if(hnand->State == HAL_NAND_STATE_BUSY)
  650. {
  651. return HAL_BUSY;
  652. }
  653. /* Identify the device address */
  654. deviceaddress = NAND_DEVICE;
  655. /* Update the NAND controller state */
  656. hnand->State = HAL_NAND_STATE_BUSY;
  657. /* Send Erase block command sequence */
  658. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE0;
  659. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  660. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  661. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  662. /* for 512 and 1 GB devices, 4th cycle is required */
  663. if(hnand->Info.BlockNbr >= 1024)
  664. {
  665. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_4TH_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  666. }
  667. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE1;
  668. /* Update the NAND controller state */
  669. hnand->State = HAL_NAND_STATE_READY;
  670. /* Get tick */
  671. tickstart = HAL_GetTick();
  672. /* Read status until NAND is ready */
  673. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  674. {
  675. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  676. {
  677. /* Process unlocked */
  678. __HAL_UNLOCK(hnand);
  679. return HAL_TIMEOUT;
  680. }
  681. }
  682. /* Process unlocked */
  683. __HAL_UNLOCK(hnand);
  684. return HAL_OK;
  685. }
  686. /**
  687. * @brief NAND memory read status.
  688. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  689. * the configuration information for NAND module.
  690. * @retval NAND status
  691. */
  692. uint32_t HAL_NAND_Read_Status(NAND_HandleTypeDef *hnand)
  693. {
  694. uint32_t data = 0;
  695. uint32_t deviceaddress = 0;
  696. /* Prevent unused argument(s) compilation warning */
  697. UNUSED(hnand);
  698. /* Identify the device address */
  699. deviceaddress = NAND_DEVICE;
  700. /* Send Read status operation command */
  701. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_STATUS;
  702. /* Read status register data */
  703. data = *(__IO uint8_t *)deviceaddress;
  704. /* Return the status */
  705. if((data & NAND_ERROR) == NAND_ERROR)
  706. {
  707. return NAND_ERROR;
  708. }
  709. else if((data & NAND_READY) == NAND_READY)
  710. {
  711. return NAND_READY;
  712. }
  713. return NAND_BUSY;
  714. }
  715. /**
  716. * @brief Increment the NAND memory address.
  717. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  718. * the configuration information for NAND module.
  719. * @param pAddress: pointer to NAND address structure
  720. * @retval The new status of the increment address operation. It can be:
  721. * - NAND_VALID_ADDRESS: When the new address is valid address
  722. * - NAND_INVALID_ADDRESS: When the new address is invalid address
  723. */
  724. uint32_t HAL_NAND_Address_Inc(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress)
  725. {
  726. uint32_t status = NAND_VALID_ADDRESS;
  727. /* Increment page address */
  728. pAddress->Page++;
  729. /* Check NAND address is valid */
  730. if(pAddress->Page == hnand->Info.BlockSize)
  731. {
  732. pAddress->Page = 0;
  733. pAddress->Block++;
  734. if(pAddress->Block == hnand->Info.ZoneSize)
  735. {
  736. pAddress->Block = 0;
  737. pAddress->Zone++;
  738. if(pAddress->Zone == (hnand->Info.ZoneSize/ hnand->Info.BlockNbr))
  739. {
  740. status = NAND_INVALID_ADDRESS;
  741. }
  742. }
  743. }
  744. return (status);
  745. }
  746. /**
  747. * @}
  748. */
  749. /** @defgroup NAND_Exported_Functions_Group3 Peripheral Control functions
  750. * @brief management functions
  751. *
  752. @verbatim
  753. ==============================================================================
  754. ##### NAND Control functions #####
  755. ==============================================================================
  756. [..]
  757. This subsection provides a set of functions allowing to control dynamically
  758. the NAND interface.
  759. @endverbatim
  760. * @{
  761. */
  762. /**
  763. * @brief Enable dynamically NAND ECC feature.
  764. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  765. * the configuration information for NAND module.
  766. * @retval HAL status
  767. */
  768. HAL_StatusTypeDef HAL_NAND_ECC_Enable(NAND_HandleTypeDef *hnand)
  769. {
  770. /* Check the NAND controller state */
  771. if(hnand->State == HAL_NAND_STATE_BUSY)
  772. {
  773. return HAL_BUSY;
  774. }
  775. /* Update the NAND state */
  776. hnand->State = HAL_NAND_STATE_BUSY;
  777. /* Enable ECC feature */
  778. FMC_NAND_ECC_Enable(hnand->Instance, hnand->Init.NandBank);
  779. /* Update the NAND state */
  780. hnand->State = HAL_NAND_STATE_READY;
  781. return HAL_OK;
  782. }
  783. /**
  784. * @brief Disable dynamically NAND ECC feature.
  785. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  786. * the configuration information for NAND module.
  787. * @retval HAL status
  788. */
  789. HAL_StatusTypeDef HAL_NAND_ECC_Disable(NAND_HandleTypeDef *hnand)
  790. {
  791. /* Check the NAND controller state */
  792. if(hnand->State == HAL_NAND_STATE_BUSY)
  793. {
  794. return HAL_BUSY;
  795. }
  796. /* Update the NAND state */
  797. hnand->State = HAL_NAND_STATE_BUSY;
  798. /* Disable ECC feature */
  799. FMC_NAND_ECC_Disable(hnand->Instance, hnand->Init.NandBank);
  800. /* Update the NAND state */
  801. hnand->State = HAL_NAND_STATE_READY;
  802. return HAL_OK;
  803. }
  804. /**
  805. * @brief Disable dynamically NAND ECC feature.
  806. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  807. * the configuration information for NAND module.
  808. * @param ECCval: pointer to ECC value
  809. * @param Timeout: maximum timeout to wait
  810. * @retval HAL status
  811. */
  812. HAL_StatusTypeDef HAL_NAND_GetECC(NAND_HandleTypeDef *hnand, uint32_t *ECCval, uint32_t Timeout)
  813. {
  814. HAL_StatusTypeDef status = HAL_OK;
  815. /* Check the NAND controller state */
  816. if(hnand->State == HAL_NAND_STATE_BUSY)
  817. {
  818. return HAL_BUSY;
  819. }
  820. /* Update the NAND state */
  821. hnand->State = HAL_NAND_STATE_BUSY;
  822. /* Get NAND ECC value */
  823. status = FMC_NAND_GetECC(hnand->Instance, ECCval, hnand->Init.NandBank, Timeout);
  824. /* Update the NAND state */
  825. hnand->State = HAL_NAND_STATE_READY;
  826. return status;
  827. }
  828. /**
  829. * @}
  830. */
  831. /** @defgroup NAND_Exported_Functions_Group4 Peripheral State functions
  832. * @brief Peripheral State functions
  833. *
  834. @verbatim
  835. ==============================================================================
  836. ##### NAND State functions #####
  837. ==============================================================================
  838. [..]
  839. This subsection permits to get in run-time the status of the NAND controller
  840. and the data flow.
  841. @endverbatim
  842. * @{
  843. */
  844. /**
  845. * @brief Return the NAND handle state.
  846. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  847. * the configuration information for NAND module.
  848. * @retval HAL state
  849. */
  850. HAL_NAND_StateTypeDef HAL_NAND_GetState(NAND_HandleTypeDef *hnand)
  851. {
  852. /* Return NAND handle state */
  853. return hnand->State;
  854. }
  855. /**
  856. * @}
  857. */
  858. /**
  859. * @}
  860. */
  861. /** @addtogroup NAND_Private_Functions
  862. * @{
  863. */
  864. /**
  865. * @brief Increment the NAND memory address.
  866. * @param hnand: pointer to a NAND_HandleTypeDef structure that contains
  867. * the configuration information for NAND module.
  868. * @param Address: address to be incremented.
  869. * @retval The new status of the increment address operation. It can be:
  870. * - NAND_VALID_ADDRESS: When the new address is valid address
  871. * - NAND_INVALID_ADDRESS: When the new address is invalid address
  872. */
  873. static uint32_t NAND_AddressIncrement(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef* Address)
  874. {
  875. uint32_t status = NAND_VALID_ADDRESS;
  876. Address->Page++;
  877. if(Address->Page == hnand->Info.BlockSize)
  878. {
  879. Address->Page = 0;
  880. Address->Block++;
  881. if(Address->Block == hnand->Info.ZoneSize)
  882. {
  883. Address->Block = 0;
  884. Address->Zone++;
  885. if(Address->Zone == hnand->Info.BlockNbr)
  886. {
  887. status = NAND_INVALID_ADDRESS;
  888. }
  889. }
  890. }
  891. return (status);
  892. }
  893. /**
  894. * @}
  895. */
  896. /**
  897. * @}
  898. */
  899. #endif /* HAL_NAND_MODULE_ENABLED */
  900. /**
  901. * @}
  902. */
  903. #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || */
  904. /* STM32L496xx || STM32L4A6xx || */
  905. /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  906. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/