stm32f4xx_hal_nor.c 31 KB

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