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.
 
 
 

1045 lines
32 KiB

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