stm32f7xx_hal_nor.c 32 KB

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