stm32f1xx_hal_nor.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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 FSMC 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. *** Callback registration ***
  43. =============================================
  44. [..]
  45. The compilation define USE_HAL_NOR_REGISTER_CALLBACKS when set to 1
  46. allows the user to configure dynamically the driver callbacks.
  47. Use Functions @ref HAL_NOR_RegisterCallback() to register a user callback,
  48. it allows to register following callbacks:
  49. (+) MspInitCallback : NOR MspInit.
  50. (+) MspDeInitCallback : NOR MspDeInit.
  51. This function takes as parameters the HAL peripheral handle, the Callback ID
  52. and a pointer to the user callback function.
  53. Use function @ref HAL_NOR_UnRegisterCallback() to reset a callback to the default
  54. weak (surcharged) function. It allows to reset following callbacks:
  55. (+) MspInitCallback : NOR MspInit.
  56. (+) MspDeInitCallback : NOR MspDeInit.
  57. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  58. By default, after the @ref HAL_NOR_Init and if the state is HAL_NOR_STATE_RESET
  59. all callbacks are reset to the corresponding legacy weak (surcharged) functions.
  60. Exception done for MspInit and MspDeInit callbacks that are respectively
  61. reset to the legacy weak (surcharged) functions in the @ref HAL_NOR_Init
  62. and @ref HAL_NOR_DeInit only when these callbacks are null (not registered beforehand).
  63. If not, MspInit or MspDeInit are not null, the @ref HAL_NOR_Init and @ref HAL_NOR_DeInit
  64. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  65. Callbacks can be registered/unregistered in READY state only.
  66. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  67. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  68. during the Init/DeInit.
  69. In that case first register the MspInit/MspDeInit user callbacks
  70. using @ref HAL_NOR_RegisterCallback before calling @ref HAL_NOR_DeInit
  71. or @ref HAL_NOR_Init function.
  72. When The compilation define USE_HAL_NOR_REGISTER_CALLBACKS is set to 0 or
  73. not defined, the callback registering feature is not available
  74. and weak (surcharged) callbacks are used.
  75. @endverbatim
  76. ******************************************************************************
  77. * @attention
  78. *
  79. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  80. * All rights reserved.</center></h2>
  81. *
  82. * This software component is licensed by ST under BSD 3-Clause license,
  83. * the "License"; You may not use this file except in compliance with the
  84. * License. You may obtain a copy of the License at:
  85. * opensource.org/licenses/BSD-3-Clause
  86. *
  87. ******************************************************************************
  88. */
  89. /* Includes ------------------------------------------------------------------*/
  90. #include "stm32f1xx_hal.h"
  91. #if defined FSMC_BANK1
  92. /** @addtogroup STM32F1xx_HAL_Driver
  93. * @{
  94. */
  95. #ifdef HAL_NOR_MODULE_ENABLED
  96. /** @defgroup NOR NOR
  97. * @brief NOR driver modules
  98. * @{
  99. */
  100. /* Private typedef -----------------------------------------------------------*/
  101. /* Private define ------------------------------------------------------------*/
  102. /** @defgroup NOR_Private_Defines NOR Private Defines
  103. * @{
  104. */
  105. /* Constants to define address to set to write a command */
  106. #define NOR_CMD_ADDRESS_FIRST (uint16_t)0x0555
  107. #define NOR_CMD_ADDRESS_FIRST_CFI (uint16_t)0x0055
  108. #define NOR_CMD_ADDRESS_SECOND (uint16_t)0x02AA
  109. #define NOR_CMD_ADDRESS_THIRD (uint16_t)0x0555
  110. #define NOR_CMD_ADDRESS_FOURTH (uint16_t)0x0555
  111. #define NOR_CMD_ADDRESS_FIFTH (uint16_t)0x02AA
  112. #define NOR_CMD_ADDRESS_SIXTH (uint16_t)0x0555
  113. /* Constants to define data to program a command */
  114. #define NOR_CMD_DATA_READ_RESET (uint16_t)0x00F0
  115. #define NOR_CMD_DATA_FIRST (uint16_t)0x00AA
  116. #define NOR_CMD_DATA_SECOND (uint16_t)0x0055
  117. #define NOR_CMD_DATA_AUTO_SELECT (uint16_t)0x0090
  118. #define NOR_CMD_DATA_PROGRAM (uint16_t)0x00A0
  119. #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD (uint16_t)0x0080
  120. #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH (uint16_t)0x00AA
  121. #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH (uint16_t)0x0055
  122. #define NOR_CMD_DATA_CHIP_ERASE (uint16_t)0x0010
  123. #define NOR_CMD_DATA_CFI (uint16_t)0x0098
  124. #define NOR_CMD_DATA_BUFFER_AND_PROG (uint8_t)0x25
  125. #define NOR_CMD_DATA_BUFFER_AND_PROG_CONFIRM (uint8_t)0x29
  126. #define NOR_CMD_DATA_BLOCK_ERASE (uint8_t)0x30
  127. /* Mask on NOR STATUS REGISTER */
  128. #define NOR_MASK_STATUS_DQ5 (uint16_t)0x0020
  129. #define NOR_MASK_STATUS_DQ6 (uint16_t)0x0040
  130. /**
  131. * @}
  132. */
  133. /* Private macro -------------------------------------------------------------*/
  134. /* Private variables ---------------------------------------------------------*/
  135. /** @defgroup NOR_Private_Variables NOR Private Variables
  136. * @{
  137. */
  138. static uint32_t uwNORMemoryDataWidth = NOR_MEMORY_8B;
  139. /**
  140. * @}
  141. */
  142. /* Private functions ---------------------------------------------------------*/
  143. /* Exported functions --------------------------------------------------------*/
  144. /** @defgroup NOR_Exported_Functions NOR Exported Functions
  145. * @{
  146. */
  147. /** @defgroup NOR_Exported_Functions_Group1 Initialization and de-initialization functions
  148. * @brief Initialization and Configuration functions
  149. *
  150. @verbatim
  151. ==============================================================================
  152. ##### NOR Initialization and de_initialization functions #####
  153. ==============================================================================
  154. [..]
  155. This section provides functions allowing to initialize/de-initialize
  156. the NOR memory
  157. @endverbatim
  158. * @{
  159. */
  160. /**
  161. * @brief Perform the NOR memory Initialization sequence
  162. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  163. * the configuration information for NOR module.
  164. * @param Timing pointer to NOR control timing structure
  165. * @param ExtTiming pointer to NOR extended mode timing structure
  166. * @retval HAL status
  167. */
  168. HAL_StatusTypeDef HAL_NOR_Init(NOR_HandleTypeDef *hnor, FSMC_NORSRAM_TimingTypeDef *Timing, FSMC_NORSRAM_TimingTypeDef *ExtTiming)
  169. {
  170. /* Check the NOR handle parameter */
  171. if (hnor == NULL)
  172. {
  173. return HAL_ERROR;
  174. }
  175. if (hnor->State == HAL_NOR_STATE_RESET)
  176. {
  177. /* Allocate lock resource and initialize it */
  178. hnor->Lock = HAL_UNLOCKED;
  179. #if (USE_HAL_NOR_REGISTER_CALLBACKS == 1)
  180. if(hnor->MspInitCallback == NULL)
  181. {
  182. hnor->MspInitCallback = HAL_NOR_MspInit;
  183. }
  184. /* Init the low level hardware */
  185. hnor->MspInitCallback(hnor);
  186. #else
  187. /* Initialize the low level hardware (MSP) */
  188. HAL_NOR_MspInit(hnor);
  189. #endif /* (USE_HAL_NOR_REGISTER_CALLBACKS) */
  190. }
  191. /* Initialize NOR control Interface */
  192. (void)FSMC_NORSRAM_Init(hnor->Instance, &(hnor->Init));
  193. /* Initialize NOR timing Interface */
  194. (void)FSMC_NORSRAM_Timing_Init(hnor->Instance, Timing, hnor->Init.NSBank);
  195. /* Initialize NOR extended mode timing Interface */
  196. (void)FSMC_NORSRAM_Extended_Timing_Init(hnor->Extended, ExtTiming, hnor->Init.NSBank, hnor->Init.ExtendedMode);
  197. /* Enable the NORSRAM device */
  198. __FSMC_NORSRAM_ENABLE(hnor->Instance, hnor->Init.NSBank);
  199. /* Initialize NOR Memory Data Width*/
  200. if (hnor->Init.MemoryDataWidth == FSMC_NORSRAM_MEM_BUS_WIDTH_8)
  201. {
  202. uwNORMemoryDataWidth = NOR_MEMORY_8B;
  203. }
  204. else
  205. {
  206. uwNORMemoryDataWidth = NOR_MEMORY_16B;
  207. }
  208. /* Initialize the NOR controller state */
  209. hnor->State = HAL_NOR_STATE_READY;
  210. return HAL_OK;
  211. }
  212. /**
  213. * @brief Perform NOR memory De-Initialization sequence
  214. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  215. * the configuration information for NOR module.
  216. * @retval HAL status
  217. */
  218. HAL_StatusTypeDef HAL_NOR_DeInit(NOR_HandleTypeDef *hnor)
  219. {
  220. #if (USE_HAL_NOR_REGISTER_CALLBACKS == 1)
  221. if(hnor->MspDeInitCallback == NULL)
  222. {
  223. hnor->MspDeInitCallback = HAL_NOR_MspDeInit;
  224. }
  225. /* DeInit the low level hardware */
  226. hnor->MspDeInitCallback(hnor);
  227. #else
  228. /* De-Initialize the low level hardware (MSP) */
  229. HAL_NOR_MspDeInit(hnor);
  230. #endif /* (USE_HAL_NOR_REGISTER_CALLBACKS) */
  231. /* Configure the NOR registers with their reset values */
  232. (void)FSMC_NORSRAM_DeInit(hnor->Instance, hnor->Extended, hnor->Init.NSBank);
  233. /* Reset the NOR controller state */
  234. hnor->State = HAL_NOR_STATE_RESET;
  235. /* Release Lock */
  236. __HAL_UNLOCK(hnor);
  237. return HAL_OK;
  238. }
  239. /**
  240. * @brief NOR MSP Init
  241. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  242. * the configuration information for NOR module.
  243. * @retval None
  244. */
  245. __weak void HAL_NOR_MspInit(NOR_HandleTypeDef *hnor)
  246. {
  247. /* Prevent unused argument(s) compilation warning */
  248. UNUSED(hnor);
  249. /* NOTE : This function Should not be modified, when the callback is needed,
  250. the HAL_NOR_MspInit could be implemented in the user file
  251. */
  252. }
  253. /**
  254. * @brief NOR MSP DeInit
  255. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  256. * the configuration information for NOR module.
  257. * @retval None
  258. */
  259. __weak void HAL_NOR_MspDeInit(NOR_HandleTypeDef *hnor)
  260. {
  261. /* Prevent unused argument(s) compilation warning */
  262. UNUSED(hnor);
  263. /* NOTE : This function Should not be modified, when the callback is needed,
  264. the HAL_NOR_MspDeInit could be implemented in the user file
  265. */
  266. }
  267. /**
  268. * @brief NOR MSP Wait for Ready/Busy signal
  269. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  270. * the configuration information for NOR module.
  271. * @param Timeout Maximum timeout value
  272. * @retval None
  273. */
  274. __weak void HAL_NOR_MspWait(NOR_HandleTypeDef *hnor, uint32_t Timeout)
  275. {
  276. /* Prevent unused argument(s) compilation warning */
  277. UNUSED(hnor);
  278. UNUSED(Timeout);
  279. /* NOTE : This function Should not be modified, when the callback is needed,
  280. the HAL_NOR_MspWait could be implemented in the user file
  281. */
  282. }
  283. /**
  284. * @}
  285. */
  286. /** @defgroup NOR_Exported_Functions_Group2 Input and Output functions
  287. * @brief Input Output and memory control functions
  288. *
  289. @verbatim
  290. ==============================================================================
  291. ##### NOR Input and Output functions #####
  292. ==============================================================================
  293. [..]
  294. This section provides functions allowing to use and control the NOR memory
  295. @endverbatim
  296. * @{
  297. */
  298. /**
  299. * @brief Read NOR flash IDs
  300. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  301. * the configuration information for NOR module.
  302. * @param pNOR_ID pointer to NOR ID structure
  303. * @retval HAL status
  304. */
  305. HAL_StatusTypeDef HAL_NOR_Read_ID(NOR_HandleTypeDef *hnor, NOR_IDTypeDef *pNOR_ID)
  306. {
  307. uint32_t deviceaddress;
  308. HAL_NOR_StateTypeDef state;
  309. /* Check the NOR controller state */
  310. state = hnor->State;
  311. if (state == HAL_NOR_STATE_BUSY)
  312. {
  313. return HAL_BUSY;
  314. }
  315. else if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_PROTECTED))
  316. {
  317. /* Process Locked */
  318. __HAL_LOCK(hnor);
  319. /* Update the NOR controller state */
  320. hnor->State = HAL_NOR_STATE_BUSY;
  321. /* Select the NOR device address */
  322. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  323. {
  324. deviceaddress = NOR_MEMORY_ADRESS1;
  325. }
  326. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  327. {
  328. deviceaddress = NOR_MEMORY_ADRESS2;
  329. }
  330. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  331. {
  332. deviceaddress = NOR_MEMORY_ADRESS3;
  333. }
  334. else /* FSMC_NORSRAM_BANK4 */
  335. {
  336. deviceaddress = NOR_MEMORY_ADRESS4;
  337. }
  338. /* Send read ID command */
  339. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  340. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  341. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_AUTO_SELECT);
  342. /* Read the NOR IDs */
  343. pNOR_ID->Manufacturer_Code = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, MC_ADDRESS);
  344. pNOR_ID->Device_Code1 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, DEVICE_CODE1_ADDR);
  345. pNOR_ID->Device_Code2 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, DEVICE_CODE2_ADDR);
  346. pNOR_ID->Device_Code3 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, DEVICE_CODE3_ADDR);
  347. /* Check the NOR controller state */
  348. hnor->State = state;
  349. /* Process unlocked */
  350. __HAL_UNLOCK(hnor);
  351. }
  352. else
  353. {
  354. return HAL_ERROR;
  355. }
  356. return HAL_OK;
  357. }
  358. /**
  359. * @brief Returns the NOR memory to Read mode.
  360. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  361. * the configuration information for NOR module.
  362. * @retval HAL status
  363. */
  364. HAL_StatusTypeDef HAL_NOR_ReturnToReadMode(NOR_HandleTypeDef *hnor)
  365. {
  366. uint32_t deviceaddress;
  367. HAL_NOR_StateTypeDef state;
  368. /* Check the NOR controller state */
  369. state = hnor->State;
  370. if (state == HAL_NOR_STATE_BUSY)
  371. {
  372. return HAL_BUSY;
  373. }
  374. else if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_PROTECTED))
  375. {
  376. /* Process Locked */
  377. __HAL_LOCK(hnor);
  378. /* Update the NOR controller state */
  379. hnor->State = HAL_NOR_STATE_BUSY;
  380. /* Select the NOR device address */
  381. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  382. {
  383. deviceaddress = NOR_MEMORY_ADRESS1;
  384. }
  385. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  386. {
  387. deviceaddress = NOR_MEMORY_ADRESS2;
  388. }
  389. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  390. {
  391. deviceaddress = NOR_MEMORY_ADRESS3;
  392. }
  393. else /* FSMC_NORSRAM_BANK4 */
  394. {
  395. deviceaddress = NOR_MEMORY_ADRESS4;
  396. }
  397. NOR_WRITE(deviceaddress, NOR_CMD_DATA_READ_RESET);
  398. /* Check the NOR controller state */
  399. hnor->State = state;
  400. /* Process unlocked */
  401. __HAL_UNLOCK(hnor);
  402. }
  403. else
  404. {
  405. return HAL_ERROR;
  406. }
  407. return HAL_OK;
  408. }
  409. /**
  410. * @brief Read data from NOR memory
  411. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  412. * the configuration information for NOR module.
  413. * @param pAddress pointer to Device address
  414. * @param pData pointer to read data
  415. * @retval HAL status
  416. */
  417. HAL_StatusTypeDef HAL_NOR_Read(NOR_HandleTypeDef *hnor, uint32_t *pAddress, uint16_t *pData)
  418. {
  419. uint32_t deviceaddress;
  420. HAL_NOR_StateTypeDef state;
  421. /* Check the NOR controller state */
  422. state = hnor->State;
  423. if (state == HAL_NOR_STATE_BUSY)
  424. {
  425. return HAL_BUSY;
  426. }
  427. else if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_PROTECTED))
  428. {
  429. /* Process Locked */
  430. __HAL_LOCK(hnor);
  431. /* Update the NOR controller state */
  432. hnor->State = HAL_NOR_STATE_BUSY;
  433. /* Select the NOR device address */
  434. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  435. {
  436. deviceaddress = NOR_MEMORY_ADRESS1;
  437. }
  438. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  439. {
  440. deviceaddress = NOR_MEMORY_ADRESS2;
  441. }
  442. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  443. {
  444. deviceaddress = NOR_MEMORY_ADRESS3;
  445. }
  446. else /* FSMC_NORSRAM_BANK4 */
  447. {
  448. deviceaddress = NOR_MEMORY_ADRESS4;
  449. }
  450. /* Send read data command */
  451. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  452. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  453. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_READ_RESET);
  454. /* Read the data */
  455. *pData = (uint16_t)(*(__IO uint32_t *)pAddress);
  456. /* Check the NOR controller state */
  457. hnor->State = state;
  458. /* Process unlocked */
  459. __HAL_UNLOCK(hnor);
  460. }
  461. else
  462. {
  463. return HAL_ERROR;
  464. }
  465. return HAL_OK;
  466. }
  467. /**
  468. * @brief Program data to NOR memory
  469. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  470. * the configuration information for NOR module.
  471. * @param pAddress Device address
  472. * @param pData pointer to the data to write
  473. * @retval HAL status
  474. */
  475. HAL_StatusTypeDef HAL_NOR_Program(NOR_HandleTypeDef *hnor, uint32_t *pAddress, uint16_t *pData)
  476. {
  477. uint32_t deviceaddress;
  478. /* Check the NOR controller state */
  479. if (hnor->State == HAL_NOR_STATE_BUSY)
  480. {
  481. return HAL_BUSY;
  482. }
  483. else if (hnor->State == HAL_NOR_STATE_READY)
  484. {
  485. /* Process Locked */
  486. __HAL_LOCK(hnor);
  487. /* Update the NOR controller state */
  488. hnor->State = HAL_NOR_STATE_BUSY;
  489. /* Select the NOR device address */
  490. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  491. {
  492. deviceaddress = NOR_MEMORY_ADRESS1;
  493. }
  494. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  495. {
  496. deviceaddress = NOR_MEMORY_ADRESS2;
  497. }
  498. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  499. {
  500. deviceaddress = NOR_MEMORY_ADRESS3;
  501. }
  502. else /* FSMC_NORSRAM_BANK4 */
  503. {
  504. deviceaddress = NOR_MEMORY_ADRESS4;
  505. }
  506. /* Send program data command */
  507. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  508. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  509. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_PROGRAM);
  510. /* Write the data */
  511. NOR_WRITE(pAddress, *pData);
  512. /* Check the NOR controller state */
  513. hnor->State = HAL_NOR_STATE_READY;
  514. /* Process unlocked */
  515. __HAL_UNLOCK(hnor);
  516. }
  517. else
  518. {
  519. return HAL_ERROR;
  520. }
  521. return HAL_OK;
  522. }
  523. /**
  524. * @brief Reads a half-word buffer from the NOR memory.
  525. * @param hnor pointer to the NOR handle
  526. * @param uwAddress NOR memory internal address to read from.
  527. * @param pData pointer to the buffer that receives the data read from the
  528. * NOR memory.
  529. * @param uwBufferSize number of Half word to read.
  530. * @retval HAL status
  531. */
  532. HAL_StatusTypeDef HAL_NOR_ReadBuffer(NOR_HandleTypeDef *hnor, uint32_t uwAddress, uint16_t *pData, uint32_t uwBufferSize)
  533. {
  534. uint32_t deviceaddress, size = uwBufferSize, address = uwAddress;
  535. uint16_t *data = pData;
  536. HAL_NOR_StateTypeDef state;
  537. /* Check the NOR controller state */
  538. state = hnor->State;
  539. if (state == HAL_NOR_STATE_BUSY)
  540. {
  541. return HAL_BUSY;
  542. }
  543. else if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_PROTECTED))
  544. {
  545. /* Process Locked */
  546. __HAL_LOCK(hnor);
  547. /* Update the NOR controller state */
  548. hnor->State = HAL_NOR_STATE_BUSY;
  549. /* Select the NOR device address */
  550. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  551. {
  552. deviceaddress = NOR_MEMORY_ADRESS1;
  553. }
  554. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  555. {
  556. deviceaddress = NOR_MEMORY_ADRESS2;
  557. }
  558. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  559. {
  560. deviceaddress = NOR_MEMORY_ADRESS3;
  561. }
  562. else /* FSMC_NORSRAM_BANK4 */
  563. {
  564. deviceaddress = NOR_MEMORY_ADRESS4;
  565. }
  566. /* Send read data command */
  567. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  568. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  569. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_READ_RESET);
  570. /* Read buffer */
  571. while (size > 0U)
  572. {
  573. *data = *(__IO uint16_t *)address;
  574. data++;
  575. address += 2U;
  576. size--;
  577. }
  578. /* Check the NOR controller state */
  579. hnor->State = state;
  580. /* Process unlocked */
  581. __HAL_UNLOCK(hnor);
  582. }
  583. else
  584. {
  585. return HAL_ERROR;
  586. }
  587. return HAL_OK;
  588. }
  589. /**
  590. * @brief Writes a half-word buffer to the NOR memory. This function must be used
  591. only with S29GL128P NOR memory.
  592. * @param hnor pointer to the NOR handle
  593. * @param uwAddress NOR memory internal start write address
  594. * @param pData pointer to source data buffer.
  595. * @param uwBufferSize Size of the buffer to write
  596. * @retval HAL status
  597. */
  598. HAL_StatusTypeDef HAL_NOR_ProgramBuffer(NOR_HandleTypeDef *hnor, uint32_t uwAddress, uint16_t *pData, uint32_t uwBufferSize)
  599. {
  600. uint16_t *p_currentaddress;
  601. const uint16_t *p_endaddress;
  602. uint16_t *data = pData;
  603. uint32_t lastloadedaddress, deviceaddress;
  604. /* Check the NOR controller state */
  605. if (hnor->State == HAL_NOR_STATE_BUSY)
  606. {
  607. return HAL_BUSY;
  608. }
  609. else if (hnor->State == HAL_NOR_STATE_READY)
  610. {
  611. /* Process Locked */
  612. __HAL_LOCK(hnor);
  613. /* Update the NOR controller state */
  614. hnor->State = HAL_NOR_STATE_BUSY;
  615. /* Select the NOR device address */
  616. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  617. {
  618. deviceaddress = NOR_MEMORY_ADRESS1;
  619. }
  620. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  621. {
  622. deviceaddress = NOR_MEMORY_ADRESS2;
  623. }
  624. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  625. {
  626. deviceaddress = NOR_MEMORY_ADRESS3;
  627. }
  628. else /* FSMC_NORSRAM_BANK4 */
  629. {
  630. deviceaddress = NOR_MEMORY_ADRESS4;
  631. }
  632. /* Initialize variables */
  633. p_currentaddress = (uint16_t *)(uwAddress);
  634. p_endaddress = (const uint16_t *)(uwAddress + (uwBufferSize - 1U));
  635. lastloadedaddress = uwAddress;
  636. /* Issue unlock command sequence */
  637. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  638. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  639. /* Write Buffer Load Command */
  640. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, uwAddress), NOR_CMD_DATA_BUFFER_AND_PROG);
  641. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, uwAddress), (uint16_t)(uwBufferSize - 1U));
  642. /* Load Data into NOR Buffer */
  643. while (p_currentaddress <= p_endaddress)
  644. {
  645. /* Store last loaded address & data value (for polling) */
  646. lastloadedaddress = (uint32_t)p_currentaddress;
  647. NOR_WRITE(p_currentaddress, *data);
  648. data++;
  649. p_currentaddress ++;
  650. }
  651. NOR_WRITE((uint32_t)(lastloadedaddress), NOR_CMD_DATA_BUFFER_AND_PROG_CONFIRM);
  652. /* Check the NOR controller state */
  653. hnor->State = HAL_NOR_STATE_READY;
  654. /* Process unlocked */
  655. __HAL_UNLOCK(hnor);
  656. }
  657. else
  658. {
  659. return HAL_ERROR;
  660. }
  661. return HAL_OK;
  662. }
  663. /**
  664. * @brief Erase the specified block of the NOR memory
  665. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  666. * the configuration information for NOR module.
  667. * @param BlockAddress Block to erase address
  668. * @param Address Device address
  669. * @retval HAL status
  670. */
  671. HAL_StatusTypeDef HAL_NOR_Erase_Block(NOR_HandleTypeDef *hnor, uint32_t BlockAddress, uint32_t Address)
  672. {
  673. uint32_t deviceaddress;
  674. /* Check the NOR controller state */
  675. if (hnor->State == HAL_NOR_STATE_BUSY)
  676. {
  677. return HAL_BUSY;
  678. }
  679. else if (hnor->State == HAL_NOR_STATE_READY)
  680. {
  681. /* Process Locked */
  682. __HAL_LOCK(hnor);
  683. /* Update the NOR controller state */
  684. hnor->State = HAL_NOR_STATE_BUSY;
  685. /* Select the NOR device address */
  686. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  687. {
  688. deviceaddress = NOR_MEMORY_ADRESS1;
  689. }
  690. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  691. {
  692. deviceaddress = NOR_MEMORY_ADRESS2;
  693. }
  694. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  695. {
  696. deviceaddress = NOR_MEMORY_ADRESS3;
  697. }
  698. else /* FSMC_NORSRAM_BANK4 */
  699. {
  700. deviceaddress = NOR_MEMORY_ADRESS4;
  701. }
  702. /* Send block erase command sequence */
  703. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  704. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  705. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
  706. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FOURTH), NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH);
  707. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIFTH), NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH);
  708. NOR_WRITE((uint32_t)(BlockAddress + Address), NOR_CMD_DATA_BLOCK_ERASE);
  709. /* Check the NOR memory status and update the controller state */
  710. hnor->State = HAL_NOR_STATE_READY;
  711. /* Process unlocked */
  712. __HAL_UNLOCK(hnor);
  713. }
  714. else
  715. {
  716. return HAL_ERROR;
  717. }
  718. return HAL_OK;
  719. }
  720. /**
  721. * @brief Erase the entire NOR chip.
  722. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  723. * the configuration information for NOR module.
  724. * @param Address Device address
  725. * @retval HAL status
  726. */
  727. HAL_StatusTypeDef HAL_NOR_Erase_Chip(NOR_HandleTypeDef *hnor, uint32_t Address)
  728. {
  729. uint32_t deviceaddress;
  730. UNUSED(Address);
  731. /* Check the NOR controller state */
  732. if (hnor->State == HAL_NOR_STATE_BUSY)
  733. {
  734. return HAL_BUSY;
  735. }
  736. else if (hnor->State == HAL_NOR_STATE_READY)
  737. {
  738. /* Process Locked */
  739. __HAL_LOCK(hnor);
  740. /* Update the NOR controller state */
  741. hnor->State = HAL_NOR_STATE_BUSY;
  742. /* Select the NOR device address */
  743. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  744. {
  745. deviceaddress = NOR_MEMORY_ADRESS1;
  746. }
  747. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  748. {
  749. deviceaddress = NOR_MEMORY_ADRESS2;
  750. }
  751. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  752. {
  753. deviceaddress = NOR_MEMORY_ADRESS3;
  754. }
  755. else /* FSMC_NORSRAM_BANK4 */
  756. {
  757. deviceaddress = NOR_MEMORY_ADRESS4;
  758. }
  759. /* Send NOR chip erase command sequence */
  760. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  761. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  762. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
  763. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FOURTH), NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH);
  764. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIFTH), NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH);
  765. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SIXTH), NOR_CMD_DATA_CHIP_ERASE);
  766. /* Check the NOR memory status and update the controller state */
  767. hnor->State = HAL_NOR_STATE_READY;
  768. /* Process unlocked */
  769. __HAL_UNLOCK(hnor);
  770. }
  771. else
  772. {
  773. return HAL_ERROR;
  774. }
  775. return HAL_OK;
  776. }
  777. /**
  778. * @brief Read NOR flash CFI IDs
  779. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  780. * the configuration information for NOR module.
  781. * @param pNOR_CFI pointer to NOR CFI IDs structure
  782. * @retval HAL status
  783. */
  784. HAL_StatusTypeDef HAL_NOR_Read_CFI(NOR_HandleTypeDef *hnor, NOR_CFITypeDef *pNOR_CFI)
  785. {
  786. uint32_t deviceaddress;
  787. HAL_NOR_StateTypeDef state;
  788. /* Check the NOR controller state */
  789. state = hnor->State;
  790. if (state == HAL_NOR_STATE_BUSY)
  791. {
  792. return HAL_BUSY;
  793. }
  794. else if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_PROTECTED))
  795. {
  796. /* Process Locked */
  797. __HAL_LOCK(hnor);
  798. /* Update the NOR controller state */
  799. hnor->State = HAL_NOR_STATE_BUSY;
  800. /* Select the NOR device address */
  801. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  802. {
  803. deviceaddress = NOR_MEMORY_ADRESS1;
  804. }
  805. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  806. {
  807. deviceaddress = NOR_MEMORY_ADRESS2;
  808. }
  809. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  810. {
  811. deviceaddress = NOR_MEMORY_ADRESS3;
  812. }
  813. else /* FSMC_NORSRAM_BANK4 */
  814. {
  815. deviceaddress = NOR_MEMORY_ADRESS4;
  816. }
  817. /* Send read CFI query command */
  818. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_CFI), NOR_CMD_DATA_CFI);
  819. /* read the NOR CFI information */
  820. pNOR_CFI->CFI_1 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI1_ADDRESS);
  821. pNOR_CFI->CFI_2 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI2_ADDRESS);
  822. pNOR_CFI->CFI_3 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI3_ADDRESS);
  823. pNOR_CFI->CFI_4 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI4_ADDRESS);
  824. /* Check the NOR controller state */
  825. hnor->State = state;
  826. /* Process unlocked */
  827. __HAL_UNLOCK(hnor);
  828. }
  829. else
  830. {
  831. return HAL_ERROR;
  832. }
  833. return HAL_OK;
  834. }
  835. #if (USE_HAL_NOR_REGISTER_CALLBACKS == 1)
  836. /**
  837. * @brief Register a User NOR Callback
  838. * To be used instead of the weak (surcharged) predefined callback
  839. * @param hnor : NOR handle
  840. * @param CallbackId : ID of the callback to be registered
  841. * This parameter can be one of the following values:
  842. * @arg @ref HAL_NOR_MSP_INIT_CB_ID NOR MspInit callback ID
  843. * @arg @ref HAL_NOR_MSP_DEINIT_CB_ID NOR MspDeInit callback ID
  844. * @param pCallback : pointer to the Callback function
  845. * @retval status
  846. */
  847. HAL_StatusTypeDef HAL_NOR_RegisterCallback (NOR_HandleTypeDef *hnor, HAL_NOR_CallbackIDTypeDef CallbackId, pNOR_CallbackTypeDef pCallback)
  848. {
  849. HAL_StatusTypeDef status = HAL_OK;
  850. HAL_NOR_StateTypeDef state;
  851. if(pCallback == NULL)
  852. {
  853. return HAL_ERROR;
  854. }
  855. /* Process locked */
  856. __HAL_LOCK(hnor);
  857. state = hnor->State;
  858. if((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_RESET) || (state == HAL_NOR_STATE_PROTECTED))
  859. {
  860. switch (CallbackId)
  861. {
  862. case HAL_NOR_MSP_INIT_CB_ID :
  863. hnor->MspInitCallback = pCallback;
  864. break;
  865. case HAL_NOR_MSP_DEINIT_CB_ID :
  866. hnor->MspDeInitCallback = pCallback;
  867. break;
  868. default :
  869. /* update return status */
  870. status = HAL_ERROR;
  871. break;
  872. }
  873. }
  874. else
  875. {
  876. /* update return status */
  877. status = HAL_ERROR;
  878. }
  879. /* Release Lock */
  880. __HAL_UNLOCK(hnor);
  881. return status;
  882. }
  883. /**
  884. * @brief Unregister a User NOR Callback
  885. * NOR Callback is redirected to the weak (surcharged) predefined callback
  886. * @param hnor : NOR handle
  887. * @param CallbackId : ID of the callback to be unregistered
  888. * This parameter can be one of the following values:
  889. * @arg @ref HAL_NOR_MSP_INIT_CB_ID NOR MspInit callback ID
  890. * @arg @ref HAL_NOR_MSP_DEINIT_CB_ID NOR MspDeInit callback ID
  891. * @retval status
  892. */
  893. HAL_StatusTypeDef HAL_NOR_UnRegisterCallback (NOR_HandleTypeDef *hnor, HAL_NOR_CallbackIDTypeDef CallbackId)
  894. {
  895. HAL_StatusTypeDef status = HAL_OK;
  896. HAL_NOR_StateTypeDef state;
  897. /* Process locked */
  898. __HAL_LOCK(hnor);
  899. state = hnor->State;
  900. if((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_RESET) || (state == HAL_NOR_STATE_PROTECTED))
  901. {
  902. switch (CallbackId)
  903. {
  904. case HAL_NOR_MSP_INIT_CB_ID :
  905. hnor->MspInitCallback = HAL_NOR_MspInit;
  906. break;
  907. case HAL_NOR_MSP_DEINIT_CB_ID :
  908. hnor->MspDeInitCallback = HAL_NOR_MspDeInit;
  909. break;
  910. default :
  911. /* update return status */
  912. status = HAL_ERROR;
  913. break;
  914. }
  915. }
  916. else
  917. {
  918. /* update return status */
  919. status = HAL_ERROR;
  920. }
  921. /* Release Lock */
  922. __HAL_UNLOCK(hnor);
  923. return status;
  924. }
  925. #endif /* (USE_HAL_NOR_REGISTER_CALLBACKS) */
  926. /**
  927. * @}
  928. */
  929. /** @defgroup NOR_Exported_Functions_Group3 NOR Control functions
  930. * @brief management functions
  931. *
  932. @verbatim
  933. ==============================================================================
  934. ##### NOR Control functions #####
  935. ==============================================================================
  936. [..]
  937. This subsection provides a set of functions allowing to control dynamically
  938. the NOR interface.
  939. @endverbatim
  940. * @{
  941. */
  942. /**
  943. * @brief Enables dynamically NOR write operation.
  944. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  945. * the configuration information for NOR module.
  946. * @retval HAL status
  947. */
  948. HAL_StatusTypeDef HAL_NOR_WriteOperation_Enable(NOR_HandleTypeDef *hnor)
  949. {
  950. /* Check the NOR controller state */
  951. if(hnor->State == HAL_NOR_STATE_PROTECTED)
  952. {
  953. /* Process Locked */
  954. __HAL_LOCK(hnor);
  955. /* Update the NOR controller state */
  956. hnor->State = HAL_NOR_STATE_BUSY;
  957. /* Enable write operation */
  958. (void)FSMC_NORSRAM_WriteOperation_Enable(hnor->Instance, hnor->Init.NSBank);
  959. /* Update the NOR controller state */
  960. hnor->State = HAL_NOR_STATE_READY;
  961. /* Process unlocked */
  962. __HAL_UNLOCK(hnor);
  963. }
  964. else
  965. {
  966. return HAL_ERROR;
  967. }
  968. return HAL_OK;
  969. }
  970. /**
  971. * @brief Disables dynamically NOR write operation.
  972. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  973. * the configuration information for NOR module.
  974. * @retval HAL status
  975. */
  976. HAL_StatusTypeDef HAL_NOR_WriteOperation_Disable(NOR_HandleTypeDef *hnor)
  977. {
  978. /* Check the NOR controller state */
  979. if(hnor->State == HAL_NOR_STATE_READY)
  980. {
  981. /* Process Locked */
  982. __HAL_LOCK(hnor);
  983. /* Update the NOR controller state */
  984. hnor->State = HAL_NOR_STATE_BUSY;
  985. /* Disable write operation */
  986. (void)FSMC_NORSRAM_WriteOperation_Disable(hnor->Instance, hnor->Init.NSBank);
  987. /* Update the NOR controller state */
  988. hnor->State = HAL_NOR_STATE_PROTECTED;
  989. /* Process unlocked */
  990. __HAL_UNLOCK(hnor);
  991. }
  992. else
  993. {
  994. return HAL_ERROR;
  995. }
  996. return HAL_OK;
  997. }
  998. /**
  999. * @}
  1000. */
  1001. /** @defgroup NOR_Exported_Functions_Group4 NOR State functions
  1002. * @brief Peripheral State functions
  1003. *
  1004. @verbatim
  1005. ==============================================================================
  1006. ##### NOR State functions #####
  1007. ==============================================================================
  1008. [..]
  1009. This subsection permits to get in run-time the status of the NOR controller
  1010. and the data flow.
  1011. @endverbatim
  1012. * @{
  1013. */
  1014. /**
  1015. * @brief return the NOR controller state
  1016. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  1017. * the configuration information for NOR module.
  1018. * @retval NOR controller state
  1019. */
  1020. HAL_NOR_StateTypeDef HAL_NOR_GetState(NOR_HandleTypeDef *hnor)
  1021. {
  1022. return hnor->State;
  1023. }
  1024. /**
  1025. * @brief Returns the NOR operation status.
  1026. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  1027. * the configuration information for NOR module.
  1028. * @param Address Device address
  1029. * @param Timeout NOR programming Timeout
  1030. * @retval NOR_Status The returned value can be: HAL_NOR_STATUS_SUCCESS, HAL_NOR_STATUS_ERROR
  1031. * or HAL_NOR_STATUS_TIMEOUT
  1032. */
  1033. HAL_NOR_StatusTypeDef HAL_NOR_GetStatus(NOR_HandleTypeDef *hnor, uint32_t Address, uint32_t Timeout)
  1034. {
  1035. HAL_NOR_StatusTypeDef status = HAL_NOR_STATUS_ONGOING;
  1036. uint16_t tmpSR1, tmpSR2;
  1037. uint32_t tickstart;
  1038. /* Poll on NOR memory Ready/Busy signal ------------------------------------*/
  1039. HAL_NOR_MspWait(hnor, Timeout);
  1040. /* Get the NOR memory operation status -------------------------------------*/
  1041. /* Get tick */
  1042. tickstart = HAL_GetTick();
  1043. while ((status != HAL_NOR_STATUS_SUCCESS) && (status != HAL_NOR_STATUS_TIMEOUT))
  1044. {
  1045. /* Check for the Timeout */
  1046. if (Timeout != HAL_MAX_DELAY)
  1047. {
  1048. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1049. {
  1050. status = HAL_NOR_STATUS_TIMEOUT;
  1051. }
  1052. }
  1053. /* Read NOR status register (DQ6 and DQ5) */
  1054. tmpSR1 = *(__IO uint16_t *)Address;
  1055. tmpSR2 = *(__IO uint16_t *)Address;
  1056. /* If DQ6 did not toggle between the two reads then return HAL_NOR_STATUS_SUCCESS */
  1057. if ((tmpSR1 & NOR_MASK_STATUS_DQ6) == (tmpSR2 & NOR_MASK_STATUS_DQ6))
  1058. {
  1059. return HAL_NOR_STATUS_SUCCESS ;
  1060. }
  1061. if ((tmpSR1 & NOR_MASK_STATUS_DQ5) == NOR_MASK_STATUS_DQ5)
  1062. {
  1063. status = HAL_NOR_STATUS_ONGOING;
  1064. }
  1065. tmpSR1 = *(__IO uint16_t *)Address;
  1066. tmpSR2 = *(__IO uint16_t *)Address;
  1067. /* If DQ6 did not toggle between the two reads then return HAL_NOR_STATUS_SUCCESS */
  1068. if ((tmpSR1 & NOR_MASK_STATUS_DQ6) == (tmpSR2 & NOR_MASK_STATUS_DQ6))
  1069. {
  1070. return HAL_NOR_STATUS_SUCCESS;
  1071. }
  1072. if ((tmpSR1 & NOR_MASK_STATUS_DQ5) == NOR_MASK_STATUS_DQ5)
  1073. {
  1074. return HAL_NOR_STATUS_ERROR;
  1075. }
  1076. }
  1077. /* Return the operation status */
  1078. return status;
  1079. }
  1080. /**
  1081. * @}
  1082. */
  1083. /**
  1084. * @}
  1085. */
  1086. /**
  1087. * @}
  1088. */
  1089. #endif /* HAL_NOR_MODULE_ENABLED */
  1090. /**
  1091. * @}
  1092. */
  1093. #endif /* FSMC_BANK1 */
  1094. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/