stm32l4xx_hal_nor.c 34 KB

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