You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_nor.c 32KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_nor.c
  4. * @author MCD Application Team
  5. * @brief NOR HAL module driver.
  6. * This file provides a generic firmware to drive NOR 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 NOR flash memories. It uses the FSMC layer functions to interface
  16. with NOR devices. This driver is used as follows:
  17. (+) NOR flash memory configuration sequence using the function HAL_NOR_Init()
  18. with control and timing parameters for both normal and extended mode.
  19. (+) Read NOR flash memory manufacturer code and device IDs using the function
  20. HAL_NOR_Read_ID(). The read information is stored in the NOR_ID_TypeDef
  21. structure declared by the function caller.
  22. (+) Access NOR flash memory by read/write data unit operations using the functions
  23. HAL_NOR_Read(), HAL_NOR_Program().
  24. (+) Perform NOR flash erase block/chip operations using the functions
  25. HAL_NOR_Erase_Block() and HAL_NOR_Erase_Chip().
  26. (+) Read the NOR flash CFI (common flash interface) IDs using the function
  27. HAL_NOR_Read_CFI(). The read information is stored in the NOR_CFI_TypeDef
  28. structure declared by the function caller.
  29. (+) You can also control the NOR device by calling the control APIs HAL_NOR_WriteOperation_Enable()/
  30. HAL_NOR_WriteOperation_Disable() to respectively enable/disable the NOR write operation
  31. (+) You can monitor the NOR device HAL state by calling the function
  32. HAL_NOR_GetState()
  33. [..]
  34. (@) This driver is a set of generic APIs which handle standard NOR flash operations.
  35. If a NOR flash device contains different operations and/or implementations,
  36. it should be implemented separately.
  37. *** NOR HAL driver macros list ***
  38. =============================================
  39. [..]
  40. Below the list of most used macros in NOR HAL driver.
  41. (+) NOR_WRITE : NOR memory write data to specified address
  42. @endverbatim
  43. ******************************************************************************
  44. * @attention
  45. *
  46. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  47. * All rights reserved.</center></h2>
  48. *
  49. * This software component is licensed by ST under BSD 3-Clause license,
  50. * the "License"; You may not use this file except in compliance with the
  51. * License. You may obtain a copy of the License at:
  52. * opensource.org/licenses/BSD-3-Clause
  53. *
  54. ******************************************************************************
  55. */
  56. /* Includes ------------------------------------------------------------------*/
  57. #include "stm32l1xx_hal.h"
  58. /** @addtogroup STM32L1xx_HAL_Driver
  59. * @{
  60. */
  61. #ifdef HAL_NOR_MODULE_ENABLED
  62. #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD)
  63. /** @defgroup NOR NOR
  64. * @brief NOR driver modules
  65. * @{
  66. */
  67. /* Private typedef -----------------------------------------------------------*/
  68. /* Private define ------------------------------------------------------------*/
  69. /** @defgroup NOR_Private_Constants NOR Private Constants
  70. * @{
  71. */
  72. /* Constants to define address to set to write a command */
  73. #define NOR_CMD_ADDRESS_FIRST (uint16_t)0x0555
  74. #define NOR_CMD_ADDRESS_FIRST_CFI (uint16_t)0x0055
  75. #define NOR_CMD_ADDRESS_SECOND (uint16_t)0x02AA
  76. #define NOR_CMD_ADDRESS_THIRD (uint16_t)0x0555
  77. #define NOR_CMD_ADDRESS_FOURTH (uint16_t)0x0555
  78. #define NOR_CMD_ADDRESS_FIFTH (uint16_t)0x02AA
  79. #define NOR_CMD_ADDRESS_SIXTH (uint16_t)0x0555
  80. /* Constants to define data to program a command */
  81. #define NOR_CMD_DATA_READ_RESET (uint16_t)0x00F0
  82. #define NOR_CMD_DATA_FIRST (uint16_t)0x00AA
  83. #define NOR_CMD_DATA_SECOND (uint16_t)0x0055
  84. #define NOR_CMD_DATA_AUTO_SELECT (uint16_t)0x0090
  85. #define NOR_CMD_DATA_PROGRAM (uint16_t)0x00A0
  86. #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD (uint16_t)0x0080
  87. #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH (uint16_t)0x00AA
  88. #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH (uint16_t)0x0055
  89. #define NOR_CMD_DATA_CHIP_ERASE (uint16_t)0x0010
  90. #define NOR_CMD_DATA_CFI (uint16_t)0x0098
  91. #define NOR_CMD_DATA_BUFFER_AND_PROG (uint8_t)0x25
  92. #define NOR_CMD_DATA_BUFFER_AND_PROG_CONFIRM (uint8_t)0x29
  93. #define NOR_CMD_DATA_BLOCK_ERASE (uint8_t)0x30
  94. /* Mask on NOR STATUS REGISTER */
  95. #define NOR_MASK_STATUS_DQ5 (uint16_t)0x0020
  96. #define NOR_MASK_STATUS_DQ6 (uint16_t)0x0040
  97. /**
  98. * @}
  99. */
  100. /* Private macro -------------------------------------------------------------*/
  101. /** @defgroup NOR_Private_Macros NOR Private Macros
  102. * @{
  103. */
  104. /**
  105. * @}
  106. */
  107. /* Private variables ---------------------------------------------------------*/
  108. /** @defgroup NOR_Private_Variables NOR Private Variables
  109. * @{
  110. */
  111. static uint32_t uwNORMemoryDataWidth = NOR_MEMORY_8B;
  112. /**
  113. * @}
  114. */
  115. /* Private function prototypes -----------------------------------------------*/
  116. /* Private functions ---------------------------------------------------------*/
  117. /** @defgroup NOR_Exported_Functions NOR Exported Functions
  118. * @{
  119. */
  120. /** @defgroup NOR_Exported_Functions_Group1 Initialization and de-initialization functions
  121. * @brief Initialization and Configuration functions
  122. *
  123. @verbatim
  124. ==============================================================================
  125. ##### NOR Initialization and de_initialization functions #####
  126. ==============================================================================
  127. [..]
  128. This section provides functions allowing to initialize/de-initialize
  129. the NOR memory
  130. @endverbatim
  131. * @{
  132. */
  133. /**
  134. * @brief Perform the NOR memory Initialization sequence
  135. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  136. * the configuration information for NOR module.
  137. * @param Timing pointer to NOR control timing structure
  138. * @param ExtTiming pointer to NOR extended mode timing structure
  139. * @retval HAL status
  140. */
  141. HAL_StatusTypeDef HAL_NOR_Init(NOR_HandleTypeDef *hnor, FSMC_NORSRAM_TimingTypeDef *Timing, FSMC_NORSRAM_TimingTypeDef *ExtTiming)
  142. {
  143. /* Check the NOR handle parameter */
  144. if(hnor == NULL)
  145. {
  146. return HAL_ERROR;
  147. }
  148. if(hnor->State == HAL_NOR_STATE_RESET)
  149. {
  150. /* Allocate lock resource and initialize it */
  151. hnor->Lock = HAL_UNLOCKED;
  152. /* Initialize the low level hardware (MSP) */
  153. HAL_NOR_MspInit(hnor);
  154. }
  155. /* Initialize NOR control Interface */
  156. FSMC_NORSRAM_Init(hnor->Instance, &(hnor->Init));
  157. /* Initialize NOR timing Interface */
  158. FSMC_NORSRAM_Timing_Init(hnor->Instance, Timing, hnor->Init.NSBank);
  159. /* Initialize NOR extended mode timing Interface */
  160. FSMC_NORSRAM_Extended_Timing_Init(hnor->Extended, ExtTiming, hnor->Init.NSBank, hnor->Init.ExtendedMode);
  161. /* Enable the NORSRAM device */
  162. __FSMC_NORSRAM_ENABLE(hnor->Instance, hnor->Init.NSBank);
  163. /* Initialize NOR Memory Data Width*/
  164. if (hnor->Init.MemoryDataWidth == FSMC_NORSRAM_MEM_BUS_WIDTH_8)
  165. {
  166. uwNORMemoryDataWidth = NOR_MEMORY_8B;
  167. }
  168. else
  169. {
  170. uwNORMemoryDataWidth = NOR_MEMORY_16B;
  171. }
  172. /* Check the NOR controller state */
  173. hnor->State = HAL_NOR_STATE_READY;
  174. return HAL_OK;
  175. }
  176. /**
  177. * @brief Perform NOR memory De-Initialization sequence
  178. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  179. * the configuration information for NOR module.
  180. * @retval HAL status
  181. */
  182. HAL_StatusTypeDef HAL_NOR_DeInit(NOR_HandleTypeDef *hnor)
  183. {
  184. /* De-Initialize the low level hardware (MSP) */
  185. HAL_NOR_MspDeInit(hnor);
  186. /* Configure the NOR registers with their reset values */
  187. FSMC_NORSRAM_DeInit(hnor->Instance, hnor->Extended, hnor->Init.NSBank);
  188. /* Update the NOR controller state */
  189. hnor->State = HAL_NOR_STATE_RESET;
  190. /* Release Lock */
  191. __HAL_UNLOCK(hnor);
  192. return HAL_OK;
  193. }
  194. /**
  195. * @brief NOR MSP Init
  196. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  197. * the configuration information for NOR module.
  198. * @retval None
  199. */
  200. __weak void HAL_NOR_MspInit(NOR_HandleTypeDef *hnor)
  201. {
  202. /* Prevent unused argument(s) compilation warning */
  203. UNUSED(hnor);
  204. /* NOTE : This function Should not be modified, when the callback is needed,
  205. the HAL_NOR_MspInit could be implemented in the user file
  206. */
  207. }
  208. /**
  209. * @brief NOR MSP DeInit
  210. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  211. * the configuration information for NOR module.
  212. * @retval None
  213. */
  214. __weak void HAL_NOR_MspDeInit(NOR_HandleTypeDef *hnor)
  215. {
  216. /* Prevent unused argument(s) compilation warning */
  217. UNUSED(hnor);
  218. /* NOTE : This function Should not be modified, when the callback is needed,
  219. the HAL_NOR_MspDeInit could be implemented in the user file
  220. */
  221. }
  222. /**
  223. * @brief NOR MSP Wait fro Ready/Busy signal
  224. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  225. * the configuration information for NOR module.
  226. * @param Timeout Maximum timeout value
  227. * @retval None
  228. */
  229. __weak void HAL_NOR_MspWait(NOR_HandleTypeDef *hnor, uint32_t Timeout)
  230. {
  231. /* Prevent unused argument(s) compilation warning */
  232. UNUSED(hnor);
  233. UNUSED(Timeout);
  234. /* NOTE : This function Should not be modified, when the callback is needed,
  235. the HAL_NOR_MspWait could be implemented in the user file
  236. */
  237. }
  238. /**
  239. * @}
  240. */
  241. /** @defgroup NOR_Exported_Functions_Group2 Input and Output functions
  242. * @brief Input Output and memory control functions
  243. *
  244. @verbatim
  245. ==============================================================================
  246. ##### NOR Input and Output functions #####
  247. ==============================================================================
  248. [..]
  249. This section provides functions allowing to use and control the NOR memory
  250. @endverbatim
  251. * @{
  252. */
  253. /**
  254. * @brief Read NOR flash IDs
  255. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  256. * the configuration information for NOR module.
  257. * @param pNOR_ID pointer to NOR ID structure
  258. * @retval HAL status
  259. */
  260. HAL_StatusTypeDef HAL_NOR_Read_ID(NOR_HandleTypeDef *hnor, NOR_IDTypeDef *pNOR_ID)
  261. {
  262. uint32_t deviceaddress = 0;
  263. /* Process Locked */
  264. __HAL_LOCK(hnor);
  265. /* Check the NOR controller state */
  266. if(hnor->State == HAL_NOR_STATE_BUSY)
  267. {
  268. return HAL_BUSY;
  269. }
  270. /* Select the NOR device address */
  271. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  272. {
  273. deviceaddress = NOR_MEMORY_ADRESS1;
  274. }
  275. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  276. {
  277. deviceaddress = NOR_MEMORY_ADRESS2;
  278. }
  279. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  280. {
  281. deviceaddress = NOR_MEMORY_ADRESS3;
  282. }
  283. else /* FSMC_NORSRAM_BANK4 */
  284. {
  285. deviceaddress = NOR_MEMORY_ADRESS4;
  286. }
  287. /* Update the NOR controller state */
  288. hnor->State = HAL_NOR_STATE_BUSY;
  289. /* Send read ID command */
  290. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  291. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  292. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_AUTO_SELECT);
  293. /* Read the NOR IDs */
  294. pNOR_ID->Manufacturer_Code = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, MC_ADDRESS);
  295. pNOR_ID->Device_Code1 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, DEVICE_CODE1_ADDR);
  296. pNOR_ID->Device_Code2 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, DEVICE_CODE2_ADDR);
  297. pNOR_ID->Device_Code3 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, DEVICE_CODE3_ADDR);
  298. /* Check the NOR controller state */
  299. hnor->State = HAL_NOR_STATE_READY;
  300. /* Process unlocked */
  301. __HAL_UNLOCK(hnor);
  302. return HAL_OK;
  303. }
  304. /**
  305. * @brief Returns the NOR memory to Read mode.
  306. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  307. * the configuration information for NOR module.
  308. * @retval HAL status
  309. */
  310. HAL_StatusTypeDef HAL_NOR_ReturnToReadMode(NOR_HandleTypeDef *hnor)
  311. {
  312. uint32_t deviceaddress = 0;
  313. /* Process Locked */
  314. __HAL_LOCK(hnor);
  315. /* Check the NOR controller state */
  316. if(hnor->State == HAL_NOR_STATE_BUSY)
  317. {
  318. return HAL_BUSY;
  319. }
  320. /* Select the NOR device address */
  321. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  322. {
  323. deviceaddress = NOR_MEMORY_ADRESS1;
  324. }
  325. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  326. {
  327. deviceaddress = NOR_MEMORY_ADRESS2;
  328. }
  329. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  330. {
  331. deviceaddress = NOR_MEMORY_ADRESS3;
  332. }
  333. else /* FSMC_NORSRAM_BANK4 */
  334. {
  335. deviceaddress = NOR_MEMORY_ADRESS4;
  336. }
  337. NOR_WRITE(deviceaddress, NOR_CMD_DATA_READ_RESET);
  338. /* Check the NOR controller state */
  339. hnor->State = HAL_NOR_STATE_READY;
  340. /* Process unlocked */
  341. __HAL_UNLOCK(hnor);
  342. return HAL_OK;
  343. }
  344. /**
  345. * @brief Read data from NOR memory
  346. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  347. * the configuration information for NOR module.
  348. * @param pAddress pointer to Device address
  349. * @param pData pointer to read data
  350. * @retval HAL status
  351. */
  352. HAL_StatusTypeDef HAL_NOR_Read(NOR_HandleTypeDef *hnor, uint32_t *pAddress, uint16_t *pData)
  353. {
  354. uint32_t deviceaddress = 0;
  355. /* Process Locked */
  356. __HAL_LOCK(hnor);
  357. /* Check the NOR controller state */
  358. if(hnor->State == HAL_NOR_STATE_BUSY)
  359. {
  360. return HAL_BUSY;
  361. }
  362. /* Select the NOR device address */
  363. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  364. {
  365. deviceaddress = NOR_MEMORY_ADRESS1;
  366. }
  367. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  368. {
  369. deviceaddress = NOR_MEMORY_ADRESS2;
  370. }
  371. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  372. {
  373. deviceaddress = NOR_MEMORY_ADRESS3;
  374. }
  375. else /* FSMC_NORSRAM_BANK4 */
  376. {
  377. deviceaddress = NOR_MEMORY_ADRESS4;
  378. }
  379. /* Update the NOR controller state */
  380. hnor->State = HAL_NOR_STATE_BUSY;
  381. /* Send read data command */
  382. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  383. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  384. NOR_WRITE((uint32_t)pAddress, NOR_CMD_DATA_READ_RESET);
  385. /* Read the data */
  386. *pData = *(__IO uint32_t *)(uint32_t)pAddress;
  387. /* Check the NOR controller state */
  388. hnor->State = HAL_NOR_STATE_READY;
  389. /* Process unlocked */
  390. __HAL_UNLOCK(hnor);
  391. return HAL_OK;
  392. }
  393. /**
  394. * @brief Program data to NOR memory
  395. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  396. * the configuration information for NOR module.
  397. * @param pAddress Device address
  398. * @param pData pointer to the data to write
  399. * @retval HAL status
  400. */
  401. HAL_StatusTypeDef HAL_NOR_Program(NOR_HandleTypeDef *hnor, uint32_t *pAddress, uint16_t *pData)
  402. {
  403. uint32_t deviceaddress = 0;
  404. /* Process Locked */
  405. __HAL_LOCK(hnor);
  406. /* Check the NOR controller state */
  407. if(hnor->State == HAL_NOR_STATE_BUSY)
  408. {
  409. return HAL_BUSY;
  410. }
  411. /* Select the NOR device address */
  412. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  413. {
  414. deviceaddress = NOR_MEMORY_ADRESS1;
  415. }
  416. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  417. {
  418. deviceaddress = NOR_MEMORY_ADRESS2;
  419. }
  420. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  421. {
  422. deviceaddress = NOR_MEMORY_ADRESS3;
  423. }
  424. else /* FSMC_NORSRAM_BANK4 */
  425. {
  426. deviceaddress = NOR_MEMORY_ADRESS4;
  427. }
  428. /* Update the NOR controller state */
  429. hnor->State = HAL_NOR_STATE_BUSY;
  430. /* Send program data command */
  431. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  432. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  433. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_PROGRAM);
  434. /* Write the data */
  435. NOR_WRITE(pAddress, *pData);
  436. /* Check the NOR controller state */
  437. hnor->State = HAL_NOR_STATE_READY;
  438. /* Process unlocked */
  439. __HAL_UNLOCK(hnor);
  440. return HAL_OK;
  441. }
  442. /**
  443. * @brief Reads a block of data from the FSMC NOR memory.
  444. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  445. * the configuration information for NOR module.
  446. * @param uwAddress NOR memory internal address to read from.
  447. * @param pData pointer to the buffer that receives the data read from the
  448. * NOR memory.
  449. * @param uwBufferSize number of Half word to read.
  450. * @retval HAL status
  451. */
  452. HAL_StatusTypeDef HAL_NOR_ReadBuffer(NOR_HandleTypeDef *hnor, uint32_t uwAddress, uint16_t *pData, uint32_t uwBufferSize)
  453. {
  454. uint32_t deviceaddress = 0;
  455. /* Process Locked */
  456. __HAL_LOCK(hnor);
  457. /* Check the NOR controller state */
  458. if(hnor->State == HAL_NOR_STATE_BUSY)
  459. {
  460. return HAL_BUSY;
  461. }
  462. /* Select the NOR device address */
  463. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  464. {
  465. deviceaddress = NOR_MEMORY_ADRESS1;
  466. }
  467. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  468. {
  469. deviceaddress = NOR_MEMORY_ADRESS2;
  470. }
  471. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  472. {
  473. deviceaddress = NOR_MEMORY_ADRESS3;
  474. }
  475. else /* FSMC_NORSRAM_BANK4 */
  476. {
  477. deviceaddress = NOR_MEMORY_ADRESS4;
  478. }
  479. /* Update the NOR controller state */
  480. hnor->State = HAL_NOR_STATE_BUSY;
  481. /* Send read data command */
  482. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  483. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  484. NOR_WRITE(uwAddress, NOR_CMD_DATA_READ_RESET);
  485. /* Read buffer */
  486. while( uwBufferSize > 0)
  487. {
  488. *pData++ = *(__IO uint16_t *)uwAddress;
  489. uwAddress += 2;
  490. uwBufferSize--;
  491. }
  492. /* Check the NOR controller state */
  493. hnor->State = HAL_NOR_STATE_READY;
  494. /* Process unlocked */
  495. __HAL_UNLOCK(hnor);
  496. return HAL_OK;
  497. }
  498. /**
  499. * @brief Writes a half-word buffer to the FSMC NOR memory. This function
  500. * must be used only with S29GL128P NOR memory.
  501. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  502. * the configuration information for NOR module.
  503. * @param uwAddress NOR memory internal address from which the data
  504. * @note Some NOR memory need Address aligned to xx bytes (can be aligned to
  505. * 64 bytes boundary for example).
  506. * @param pData pointer to source data buffer.
  507. * @param uwBufferSize number of Half words to write.
  508. * @note The maximum buffer size allowed is NOR memory dependent
  509. * (can be 64 Bytes max for example).
  510. * @retval HAL status
  511. */
  512. HAL_StatusTypeDef HAL_NOR_ProgramBuffer(NOR_HandleTypeDef *hnor, uint32_t uwAddress, uint16_t *pData, uint32_t uwBufferSize)
  513. {
  514. uint16_t * p_currentaddress = (uint16_t *)NULL;
  515. uint16_t * p_endaddress = (uint16_t *)NULL;
  516. uint32_t lastloadedaddress = 0, deviceaddress = 0;
  517. /* Process Locked */
  518. __HAL_LOCK(hnor);
  519. /* Check the NOR controller state */
  520. if(hnor->State == HAL_NOR_STATE_BUSY)
  521. {
  522. return HAL_BUSY;
  523. }
  524. /* Select the NOR device address */
  525. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  526. {
  527. deviceaddress = NOR_MEMORY_ADRESS1;
  528. }
  529. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  530. {
  531. deviceaddress = NOR_MEMORY_ADRESS2;
  532. }
  533. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  534. {
  535. deviceaddress = NOR_MEMORY_ADRESS3;
  536. }
  537. else /* FSMC_NORSRAM_BANK4 */
  538. {
  539. deviceaddress = NOR_MEMORY_ADRESS4;
  540. }
  541. /* Update the NOR controller state */
  542. hnor->State = HAL_NOR_STATE_BUSY;
  543. /* Initialize variables */
  544. p_currentaddress = (uint16_t*)((uint32_t)(uwAddress));
  545. p_endaddress = p_currentaddress + (uwBufferSize-1);
  546. lastloadedaddress = (uint32_t)(uwAddress);
  547. /* Issue unlock command sequence */
  548. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  549. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  550. /* Write Buffer Load Command */
  551. NOR_WRITE((uint32_t)(p_currentaddress), NOR_CMD_DATA_BUFFER_AND_PROG);
  552. NOR_WRITE((uint32_t)(p_currentaddress), (uwBufferSize-1));
  553. /* Load Data into NOR Buffer */
  554. while(p_currentaddress <= p_endaddress)
  555. {
  556. /* Store last loaded address & data value (for polling) */
  557. lastloadedaddress = (uint32_t)p_currentaddress;
  558. NOR_WRITE(p_currentaddress, *pData++);
  559. p_currentaddress++;
  560. }
  561. NOR_WRITE((uint32_t)(lastloadedaddress), NOR_CMD_DATA_BUFFER_AND_PROG_CONFIRM);
  562. /* Check the NOR controller state */
  563. hnor->State = HAL_NOR_STATE_READY;
  564. /* Process unlocked */
  565. __HAL_UNLOCK(hnor);
  566. return HAL_OK;
  567. }
  568. /**
  569. * @brief Erase the specified block of the NOR memory
  570. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  571. * the configuration information for NOR module.
  572. * @param BlockAddress Block to erase address
  573. * @param Address Device address
  574. * @retval HAL status
  575. */
  576. HAL_StatusTypeDef HAL_NOR_Erase_Block(NOR_HandleTypeDef *hnor, uint32_t BlockAddress, uint32_t Address)
  577. {
  578. uint32_t deviceaddress = 0;
  579. /* Process Locked */
  580. __HAL_LOCK(hnor);
  581. /* Check the NOR controller state */
  582. if(hnor->State == HAL_NOR_STATE_BUSY)
  583. {
  584. return HAL_BUSY;
  585. }
  586. /* Select the NOR device address */
  587. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  588. {
  589. deviceaddress = NOR_MEMORY_ADRESS1;
  590. }
  591. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  592. {
  593. deviceaddress = NOR_MEMORY_ADRESS2;
  594. }
  595. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  596. {
  597. deviceaddress = NOR_MEMORY_ADRESS3;
  598. }
  599. else /* FSMC_NORSRAM_BANK4 */
  600. {
  601. deviceaddress = NOR_MEMORY_ADRESS4;
  602. }
  603. /* Update the NOR controller state */
  604. hnor->State = HAL_NOR_STATE_BUSY;
  605. /* Send block erase command sequence */
  606. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  607. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  608. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
  609. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FOURTH), NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH);
  610. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIFTH), NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH);
  611. NOR_WRITE((uint32_t)(BlockAddress + Address), NOR_CMD_DATA_BLOCK_ERASE);
  612. /* Check the NOR memory status and update the controller state */
  613. hnor->State = HAL_NOR_STATE_READY;
  614. /* Process unlocked */
  615. __HAL_UNLOCK(hnor);
  616. return HAL_OK;
  617. }
  618. /**
  619. * @brief Erase the entire NOR chip.
  620. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  621. * the configuration information for NOR module.
  622. * @param Address Device address
  623. * @retval HAL status
  624. */
  625. HAL_StatusTypeDef HAL_NOR_Erase_Chip(NOR_HandleTypeDef *hnor, uint32_t Address)
  626. {
  627. uint32_t deviceaddress = 0;
  628. /* Process Locked */
  629. __HAL_LOCK(hnor);
  630. /* Check the NOR controller state */
  631. if(hnor->State == HAL_NOR_STATE_BUSY)
  632. {
  633. return HAL_BUSY;
  634. }
  635. /* Select the NOR device address */
  636. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  637. {
  638. deviceaddress = NOR_MEMORY_ADRESS1;
  639. }
  640. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  641. {
  642. deviceaddress = NOR_MEMORY_ADRESS2;
  643. }
  644. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  645. {
  646. deviceaddress = NOR_MEMORY_ADRESS3;
  647. }
  648. else /* FSMC_NORSRAM_BANK4 */
  649. {
  650. deviceaddress = NOR_MEMORY_ADRESS4;
  651. }
  652. /* Update the NOR controller state */
  653. hnor->State = HAL_NOR_STATE_BUSY;
  654. /* Send NOR chip erase command sequence */
  655. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  656. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  657. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
  658. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FOURTH), NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH);
  659. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIFTH), NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH);
  660. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SIXTH), NOR_CMD_DATA_CHIP_ERASE);
  661. /* Check the NOR memory status and update the controller state */
  662. hnor->State = HAL_NOR_STATE_READY;
  663. /* Process unlocked */
  664. __HAL_UNLOCK(hnor);
  665. return HAL_OK;
  666. }
  667. /**
  668. * @brief Read NOR flash CFI IDs
  669. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  670. * the configuration information for NOR module.
  671. * @param pNOR_CFI pointer to NOR CFI IDs structure
  672. * @retval HAL status
  673. */
  674. HAL_StatusTypeDef HAL_NOR_Read_CFI(NOR_HandleTypeDef *hnor, NOR_CFITypeDef *pNOR_CFI)
  675. {
  676. uint32_t deviceaddress = 0;
  677. /* Process Locked */
  678. __HAL_LOCK(hnor);
  679. /* Check the NOR controller state */
  680. if(hnor->State == HAL_NOR_STATE_BUSY)
  681. {
  682. return HAL_BUSY;
  683. }
  684. /* Select the NOR device address */
  685. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  686. {
  687. deviceaddress = NOR_MEMORY_ADRESS1;
  688. }
  689. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  690. {
  691. deviceaddress = NOR_MEMORY_ADRESS2;
  692. }
  693. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  694. {
  695. deviceaddress = NOR_MEMORY_ADRESS3;
  696. }
  697. else /* FSMC_NORSRAM_BANK4 */
  698. {
  699. deviceaddress = NOR_MEMORY_ADRESS4;
  700. }
  701. /* Update the NOR controller state */
  702. hnor->State = HAL_NOR_STATE_BUSY;
  703. /* Send read CFI query command */
  704. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_CFI), NOR_CMD_DATA_CFI);
  705. /* read the NOR CFI information */
  706. pNOR_CFI->CFI_1 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI1_ADDRESS);
  707. pNOR_CFI->CFI_2 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI2_ADDRESS);
  708. pNOR_CFI->CFI_3 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI3_ADDRESS);
  709. pNOR_CFI->CFI_4 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI4_ADDRESS);
  710. /* Check the NOR controller state */
  711. hnor->State = HAL_NOR_STATE_READY;
  712. /* Process unlocked */
  713. __HAL_UNLOCK(hnor);
  714. return HAL_OK;
  715. }
  716. /**
  717. * @}
  718. */
  719. /** @defgroup NOR_Exported_Functions_Group3 Control functions
  720. * @brief management functions
  721. *
  722. @verbatim
  723. ==============================================================================
  724. ##### NOR Control functions #####
  725. ==============================================================================
  726. [..]
  727. This subsection provides a set of functions allowing to control dynamically
  728. the NOR interface.
  729. @endverbatim
  730. * @{
  731. */
  732. /**
  733. * @brief Enables dynamically NOR write operation.
  734. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  735. * the configuration information for NOR module.
  736. * @retval HAL status
  737. */
  738. HAL_StatusTypeDef HAL_NOR_WriteOperation_Enable(NOR_HandleTypeDef *hnor)
  739. {
  740. /* Process Locked */
  741. __HAL_LOCK(hnor);
  742. /* Enable write operation */
  743. FSMC_NORSRAM_WriteOperation_Enable(hnor->Instance, hnor->Init.NSBank);
  744. /* Update the NOR controller state */
  745. hnor->State = HAL_NOR_STATE_READY;
  746. /* Process unlocked */
  747. __HAL_UNLOCK(hnor);
  748. return HAL_OK;
  749. }
  750. /**
  751. * @brief Disables dynamically NOR write operation.
  752. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  753. * the configuration information for NOR module.
  754. * @retval HAL status
  755. */
  756. HAL_StatusTypeDef HAL_NOR_WriteOperation_Disable(NOR_HandleTypeDef *hnor)
  757. {
  758. /* Process Locked */
  759. __HAL_LOCK(hnor);
  760. /* Update the SRAM controller state */
  761. hnor->State = HAL_NOR_STATE_BUSY;
  762. /* Disable write operation */
  763. FSMC_NORSRAM_WriteOperation_Disable(hnor->Instance, hnor->Init.NSBank);
  764. /* Update the NOR controller state */
  765. hnor->State = HAL_NOR_STATE_PROTECTED;
  766. /* Process unlocked */
  767. __HAL_UNLOCK(hnor);
  768. return HAL_OK;
  769. }
  770. /**
  771. * @}
  772. */
  773. /** @defgroup NOR_Exported_Functions_Group4 State functions
  774. * @brief Peripheral State functions
  775. *
  776. @verbatim
  777. ==============================================================================
  778. ##### NOR State functions #####
  779. ==============================================================================
  780. [..]
  781. This subsection permits to get in run-time the status of the NOR controller
  782. and the data flow.
  783. @endverbatim
  784. * @{
  785. */
  786. /**
  787. * @brief return the NOR controller state
  788. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  789. * the configuration information for NOR module.
  790. * @retval NOR controller state
  791. */
  792. HAL_NOR_StateTypeDef HAL_NOR_GetState(NOR_HandleTypeDef *hnor)
  793. {
  794. return hnor->State;
  795. }
  796. /**
  797. * @brief Returns the NOR operation status.
  798. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  799. * the configuration information for NOR module.
  800. * @param Address Device address
  801. * @param Timeout NOR progamming Timeout
  802. * @retval NOR_Status: The returned value can be: HAL_NOR_STATUS_SUCCESS, HAL_NOR_STATUS_ERROR
  803. * or HAL_NOR_STATUS_TIMEOUT
  804. */
  805. HAL_NOR_StatusTypeDef HAL_NOR_GetStatus(NOR_HandleTypeDef *hnor, uint32_t Address, uint32_t Timeout)
  806. {
  807. HAL_NOR_StatusTypeDef status = HAL_NOR_STATUS_ONGOING;
  808. uint16_t tmp_sr1 = 0, tmp_sr2 = 0;
  809. uint32_t tickstart = 0;
  810. /* Poll on NOR memory Ready/Busy signal ------------------------------------*/
  811. HAL_NOR_MspWait(hnor, Timeout);
  812. /* Get tick */
  813. tickstart = HAL_GetTick();
  814. while((status != HAL_NOR_STATUS_SUCCESS) && (status != HAL_NOR_STATUS_TIMEOUT))
  815. {
  816. /* Check for the Timeout */
  817. if(Timeout != HAL_MAX_DELAY)
  818. {
  819. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  820. {
  821. status = HAL_NOR_STATUS_TIMEOUT;
  822. }
  823. }
  824. /* Read NOR status register (DQ6 and DQ5) */
  825. tmp_sr1 = *(__IO uint16_t *)Address;
  826. tmp_sr2 = *(__IO uint16_t *)Address;
  827. /* If DQ6 did not toggle between the two reads then return NOR_Success */
  828. if((tmp_sr1 & NOR_MASK_STATUS_DQ6) == (tmp_sr2 & NOR_MASK_STATUS_DQ6))
  829. {
  830. return HAL_NOR_STATUS_SUCCESS;
  831. }
  832. if((tmp_sr1 & NOR_MASK_STATUS_DQ5) != NOR_MASK_STATUS_DQ5)
  833. {
  834. status = HAL_NOR_STATUS_ONGOING;
  835. }
  836. tmp_sr1 = *(__IO uint16_t *)Address;
  837. tmp_sr2 = *(__IO uint16_t *)Address;
  838. /* If DQ6 did not toggle between the two reads then return NOR_Success */
  839. if((tmp_sr1 & NOR_MASK_STATUS_DQ6) == (tmp_sr2 & NOR_MASK_STATUS_DQ6))
  840. {
  841. return HAL_NOR_STATUS_SUCCESS;
  842. }
  843. else if((tmp_sr1 & NOR_MASK_STATUS_DQ5) == NOR_MASK_STATUS_DQ5)
  844. {
  845. return HAL_NOR_STATUS_ERROR;
  846. }
  847. }
  848. /* Return the operation status */
  849. return status;
  850. }
  851. /**
  852. * @}
  853. */
  854. /**
  855. * @}
  856. */
  857. /**
  858. * @}
  859. */
  860. #endif /* STM32L151xD || STM32L152xD || STM32L162xD */
  861. #endif /* HAL_NOR_MODULE_ENABLED */
  862. /**
  863. * @}
  864. */
  865. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/