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.
 
 
 

1068 lines
33 KiB

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