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.
 
 
 

1052 lines
33 KiB

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