stm32f1xx_hal_nor.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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. ******************************************************************************
  10. * @attention
  11. *
  12. * Copyright (c) 2016 STMicroelectronics.
  13. * All rights reserved.
  14. *
  15. * This software is licensed under terms that can be found in the LICENSE file
  16. * in the root directory of this software component.
  17. * If no LICENSE file comes with this software, it is provided AS-IS.
  18. *
  19. ******************************************************************************
  20. @verbatim
  21. ==============================================================================
  22. ##### How to use this driver #####
  23. ==============================================================================
  24. [..]
  25. This driver is a generic layered driver which contains a set of APIs used to
  26. control NOR flash memories. It uses the FSMC layer functions to interface
  27. with NOR devices. This driver is used as follows:
  28. (+) NOR flash memory configuration sequence using the function HAL_NOR_Init()
  29. with control and timing parameters for both normal and extended mode.
  30. (+) Read NOR flash memory manufacturer code and device IDs using the function
  31. HAL_NOR_Read_ID(). The read information is stored in the NOR_ID_TypeDef
  32. structure declared by the function caller.
  33. (+) Access NOR flash memory by read/write data unit operations using the functions
  34. HAL_NOR_Read(), HAL_NOR_Program().
  35. (+) Perform NOR flash erase block/chip operations using the functions
  36. HAL_NOR_Erase_Block() and HAL_NOR_Erase_Chip().
  37. (+) Read the NOR flash CFI (common flash interface) IDs using the function
  38. HAL_NOR_Read_CFI(). The read information is stored in the NOR_CFI_TypeDef
  39. structure declared by the function caller.
  40. (+) You can also control the NOR device by calling the control APIs HAL_NOR_WriteOperation_Enable()/
  41. HAL_NOR_WriteOperation_Disable() to respectively enable/disable the NOR write operation
  42. (+) You can monitor the NOR device HAL state by calling the function
  43. HAL_NOR_GetState()
  44. [..]
  45. (@) This driver is a set of generic APIs which handle standard NOR flash operations.
  46. If a NOR flash device contains different operations and/or implementations,
  47. it should be implemented separately.
  48. *** NOR HAL driver macros list ***
  49. =============================================
  50. [..]
  51. Below the list of most used macros in NOR HAL driver.
  52. (+) NOR_WRITE : NOR memory write data to specified address
  53. *** Callback registration ***
  54. =============================================
  55. [..]
  56. The compilation define USE_HAL_NOR_REGISTER_CALLBACKS when set to 1
  57. allows the user to configure dynamically the driver callbacks.
  58. Use Functions HAL_NOR_RegisterCallback() to register a user callback,
  59. it allows to register following callbacks:
  60. (+) MspInitCallback : NOR MspInit.
  61. (+) MspDeInitCallback : NOR MspDeInit.
  62. This function takes as parameters the HAL peripheral handle, the Callback ID
  63. and a pointer to the user callback function.
  64. Use function HAL_NOR_UnRegisterCallback() to reset a callback to the default
  65. weak (overridden) function. It allows to reset following callbacks:
  66. (+) MspInitCallback : NOR MspInit.
  67. (+) MspDeInitCallback : NOR MspDeInit.
  68. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  69. By default, after the HAL_NOR_Init and if the state is HAL_NOR_STATE_RESET
  70. all callbacks are reset to the corresponding legacy weak (overridden) functions.
  71. Exception done for MspInit and MspDeInit callbacks that are respectively
  72. reset to the legacy weak (overridden) functions in the HAL_NOR_Init
  73. and HAL_NOR_DeInit only when these callbacks are null (not registered beforehand).
  74. If not, MspInit or MspDeInit are not null, the HAL_NOR_Init and HAL_NOR_DeInit
  75. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  76. Callbacks can be registered/unregistered in READY state only.
  77. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  78. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  79. during the Init/DeInit.
  80. In that case first register the MspInit/MspDeInit user callbacks
  81. using HAL_NOR_RegisterCallback before calling HAL_NOR_DeInit
  82. or HAL_NOR_Init function.
  83. When The compilation define USE_HAL_NOR_REGISTER_CALLBACKS is set to 0 or
  84. not defined, the callback registering feature is not available
  85. and weak (overridden) callbacks are used.
  86. @endverbatim
  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_BYTE (uint16_t)0x0AAA
  107. #define NOR_CMD_ADDRESS_FIRST_CFI_BYTE (uint16_t)0x00AA
  108. #define NOR_CMD_ADDRESS_SECOND_BYTE (uint16_t)0x0555
  109. #define NOR_CMD_ADDRESS_THIRD_BYTE (uint16_t)0x0AAA
  110. #define NOR_CMD_ADDRESS_FIRST (uint16_t)0x0555
  111. #define NOR_CMD_ADDRESS_FIRST_CFI (uint16_t)0x0055
  112. #define NOR_CMD_ADDRESS_SECOND (uint16_t)0x02AA
  113. #define NOR_CMD_ADDRESS_THIRD (uint16_t)0x0555
  114. #define NOR_CMD_ADDRESS_FOURTH (uint16_t)0x0555
  115. #define NOR_CMD_ADDRESS_FIFTH (uint16_t)0x02AA
  116. #define NOR_CMD_ADDRESS_SIXTH (uint16_t)0x0555
  117. /* Constants to define data to program a command */
  118. #define NOR_CMD_DATA_READ_RESET (uint16_t)0x00F0
  119. #define NOR_CMD_DATA_FIRST (uint16_t)0x00AA
  120. #define NOR_CMD_DATA_SECOND (uint16_t)0x0055
  121. #define NOR_CMD_DATA_AUTO_SELECT (uint16_t)0x0090
  122. #define NOR_CMD_DATA_PROGRAM (uint16_t)0x00A0
  123. #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD (uint16_t)0x0080
  124. #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH (uint16_t)0x00AA
  125. #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH (uint16_t)0x0055
  126. #define NOR_CMD_DATA_CHIP_ERASE (uint16_t)0x0010
  127. #define NOR_CMD_DATA_CFI (uint16_t)0x0098
  128. #define NOR_CMD_DATA_BUFFER_AND_PROG (uint8_t)0x25
  129. #define NOR_CMD_DATA_BUFFER_AND_PROG_CONFIRM (uint8_t)0x29
  130. #define NOR_CMD_DATA_BLOCK_ERASE (uint8_t)0x30
  131. #define NOR_CMD_READ_ARRAY (uint16_t)0x00FF
  132. #define NOR_CMD_WORD_PROGRAM (uint16_t)0x0040
  133. #define NOR_CMD_BUFFERED_PROGRAM (uint16_t)0x00E8
  134. #define NOR_CMD_CONFIRM (uint16_t)0x00D0
  135. #define NOR_CMD_BLOCK_ERASE (uint16_t)0x0020
  136. #define NOR_CMD_BLOCK_UNLOCK (uint16_t)0x0060
  137. #define NOR_CMD_READ_STATUS_REG (uint16_t)0x0070
  138. #define NOR_CMD_CLEAR_STATUS_REG (uint16_t)0x0050
  139. /* Mask on NOR STATUS REGISTER */
  140. #define NOR_MASK_STATUS_DQ4 (uint16_t)0x0010
  141. #define NOR_MASK_STATUS_DQ5 (uint16_t)0x0020
  142. #define NOR_MASK_STATUS_DQ6 (uint16_t)0x0040
  143. #define NOR_MASK_STATUS_DQ7 (uint16_t)0x0080
  144. /* Address of the primary command set */
  145. #define NOR_ADDRESS_COMMAND_SET (uint16_t)0x0013
  146. /* Command set code assignment (defined in JEDEC JEP137B version may 2004) */
  147. #define NOR_INTEL_SHARP_EXT_COMMAND_SET (uint16_t)0x0001 /* Supported in this driver */
  148. #define NOR_AMD_FUJITSU_COMMAND_SET (uint16_t)0x0002 /* Supported in this driver */
  149. #define NOR_INTEL_STANDARD_COMMAND_SET (uint16_t)0x0003 /* Not Supported in this driver */
  150. #define NOR_AMD_FUJITSU_EXT_COMMAND_SET (uint16_t)0x0004 /* Not Supported in this driver */
  151. #define NOR_WINDBOND_STANDARD_COMMAND_SET (uint16_t)0x0006 /* Not Supported in this driver */
  152. #define NOR_MITSUBISHI_STANDARD_COMMAND_SET (uint16_t)0x0100 /* Not Supported in this driver */
  153. #define NOR_MITSUBISHI_EXT_COMMAND_SET (uint16_t)0x0101 /* Not Supported in this driver */
  154. #define NOR_PAGE_WRITE_COMMAND_SET (uint16_t)0x0102 /* Not Supported in this driver */
  155. #define NOR_INTEL_PERFORMANCE_COMMAND_SET (uint16_t)0x0200 /* Not Supported in this driver */
  156. #define NOR_INTEL_DATA_COMMAND_SET (uint16_t)0x0210 /* Not Supported in this driver */
  157. /**
  158. * @}
  159. */
  160. /* Private macro -------------------------------------------------------------*/
  161. /* Private variables ---------------------------------------------------------*/
  162. /** @defgroup NOR_Private_Variables NOR Private Variables
  163. * @{
  164. */
  165. static uint32_t uwNORMemoryDataWidth = NOR_MEMORY_8B;
  166. /**
  167. * @}
  168. */
  169. /* Private functions ---------------------------------------------------------*/
  170. /* Exported functions --------------------------------------------------------*/
  171. /** @defgroup NOR_Exported_Functions NOR Exported Functions
  172. * @{
  173. */
  174. /** @defgroup NOR_Exported_Functions_Group1 Initialization and de-initialization functions
  175. * @brief Initialization and Configuration functions
  176. *
  177. @verbatim
  178. ==============================================================================
  179. ##### NOR Initialization and de_initialization functions #####
  180. ==============================================================================
  181. [..]
  182. This section provides functions allowing to initialize/de-initialize
  183. the NOR memory
  184. @endverbatim
  185. * @{
  186. */
  187. /**
  188. * @brief Perform the NOR memory Initialization sequence
  189. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  190. * the configuration information for NOR module.
  191. * @param Timing pointer to NOR control timing structure
  192. * @param ExtTiming pointer to NOR extended mode timing structure
  193. * @retval HAL status
  194. */
  195. HAL_StatusTypeDef HAL_NOR_Init(NOR_HandleTypeDef *hnor, FSMC_NORSRAM_TimingTypeDef *Timing,
  196. FSMC_NORSRAM_TimingTypeDef *ExtTiming)
  197. {
  198. uint32_t deviceaddress;
  199. HAL_StatusTypeDef status = HAL_OK;
  200. /* Check the NOR handle parameter */
  201. if (hnor == NULL)
  202. {
  203. return HAL_ERROR;
  204. }
  205. if (hnor->State == HAL_NOR_STATE_RESET)
  206. {
  207. /* Allocate lock resource and initialize it */
  208. hnor->Lock = HAL_UNLOCKED;
  209. #if (USE_HAL_NOR_REGISTER_CALLBACKS == 1)
  210. if (hnor->MspInitCallback == NULL)
  211. {
  212. hnor->MspInitCallback = HAL_NOR_MspInit;
  213. }
  214. /* Init the low level hardware */
  215. hnor->MspInitCallback(hnor);
  216. #else
  217. /* Initialize the low level hardware (MSP) */
  218. HAL_NOR_MspInit(hnor);
  219. #endif /* (USE_HAL_NOR_REGISTER_CALLBACKS) */
  220. }
  221. /* Initialize NOR control Interface */
  222. (void)FSMC_NORSRAM_Init(hnor->Instance, &(hnor->Init));
  223. /* Initialize NOR timing Interface */
  224. (void)FSMC_NORSRAM_Timing_Init(hnor->Instance, Timing, hnor->Init.NSBank);
  225. /* Initialize NOR extended mode timing Interface */
  226. (void)FSMC_NORSRAM_Extended_Timing_Init(hnor->Extended, ExtTiming,
  227. hnor->Init.NSBank, hnor->Init.ExtendedMode);
  228. /* Enable the NORSRAM device */
  229. __FSMC_NORSRAM_ENABLE(hnor->Instance, hnor->Init.NSBank);
  230. /* Initialize NOR Memory Data Width*/
  231. if (hnor->Init.MemoryDataWidth == FSMC_NORSRAM_MEM_BUS_WIDTH_8)
  232. {
  233. uwNORMemoryDataWidth = NOR_MEMORY_8B;
  234. }
  235. else
  236. {
  237. uwNORMemoryDataWidth = NOR_MEMORY_16B;
  238. }
  239. /* Initialize the NOR controller state */
  240. hnor->State = HAL_NOR_STATE_READY;
  241. /* Select the NOR device address */
  242. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  243. {
  244. deviceaddress = NOR_MEMORY_ADRESS1;
  245. }
  246. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  247. {
  248. deviceaddress = NOR_MEMORY_ADRESS2;
  249. }
  250. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  251. {
  252. deviceaddress = NOR_MEMORY_ADRESS3;
  253. }
  254. else /* FSMC_NORSRAM_BANK4 */
  255. {
  256. deviceaddress = NOR_MEMORY_ADRESS4;
  257. }
  258. if (hnor->Init.WriteOperation == FSMC_WRITE_OPERATION_DISABLE)
  259. {
  260. (void)FSMC_NORSRAM_WriteOperation_Disable(hnor->Instance, hnor->Init.NSBank);
  261. /* Update the NOR controller state */
  262. hnor->State = HAL_NOR_STATE_PROTECTED;
  263. }
  264. else
  265. {
  266. /* Get the value of the command set */
  267. if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
  268. {
  269. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_CFI_BYTE),
  270. NOR_CMD_DATA_CFI);
  271. }
  272. else
  273. {
  274. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_CFI), NOR_CMD_DATA_CFI);
  275. }
  276. hnor->CommandSet = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_ADDRESS_COMMAND_SET);
  277. status = HAL_NOR_ReturnToReadMode(hnor);
  278. }
  279. return status;
  280. }
  281. /**
  282. * @brief Perform NOR memory De-Initialization sequence
  283. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  284. * the configuration information for NOR module.
  285. * @retval HAL status
  286. */
  287. HAL_StatusTypeDef HAL_NOR_DeInit(NOR_HandleTypeDef *hnor)
  288. {
  289. #if (USE_HAL_NOR_REGISTER_CALLBACKS == 1)
  290. if (hnor->MspDeInitCallback == NULL)
  291. {
  292. hnor->MspDeInitCallback = HAL_NOR_MspDeInit;
  293. }
  294. /* DeInit the low level hardware */
  295. hnor->MspDeInitCallback(hnor);
  296. #else
  297. /* De-Initialize the low level hardware (MSP) */
  298. HAL_NOR_MspDeInit(hnor);
  299. #endif /* (USE_HAL_NOR_REGISTER_CALLBACKS) */
  300. /* Configure the NOR registers with their reset values */
  301. (void)FSMC_NORSRAM_DeInit(hnor->Instance, hnor->Extended, hnor->Init.NSBank);
  302. /* Reset the NOR controller state */
  303. hnor->State = HAL_NOR_STATE_RESET;
  304. /* Release Lock */
  305. __HAL_UNLOCK(hnor);
  306. return HAL_OK;
  307. }
  308. /**
  309. * @brief NOR MSP Init
  310. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  311. * the configuration information for NOR module.
  312. * @retval None
  313. */
  314. __weak void HAL_NOR_MspInit(NOR_HandleTypeDef *hnor)
  315. {
  316. /* Prevent unused argument(s) compilation warning */
  317. UNUSED(hnor);
  318. /* NOTE : This function Should not be modified, when the callback is needed,
  319. the HAL_NOR_MspInit could be implemented in the user file
  320. */
  321. }
  322. /**
  323. * @brief NOR MSP DeInit
  324. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  325. * the configuration information for NOR module.
  326. * @retval None
  327. */
  328. __weak void HAL_NOR_MspDeInit(NOR_HandleTypeDef *hnor)
  329. {
  330. /* Prevent unused argument(s) compilation warning */
  331. UNUSED(hnor);
  332. /* NOTE : This function Should not be modified, when the callback is needed,
  333. the HAL_NOR_MspDeInit could be implemented in the user file
  334. */
  335. }
  336. /**
  337. * @brief NOR MSP Wait for Ready/Busy signal
  338. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  339. * the configuration information for NOR module.
  340. * @param Timeout Maximum timeout value
  341. * @retval None
  342. */
  343. __weak void HAL_NOR_MspWait(NOR_HandleTypeDef *hnor, uint32_t Timeout)
  344. {
  345. /* Prevent unused argument(s) compilation warning */
  346. UNUSED(hnor);
  347. UNUSED(Timeout);
  348. /* NOTE : This function Should not be modified, when the callback is needed,
  349. the HAL_NOR_MspWait could be implemented in the user file
  350. */
  351. }
  352. /**
  353. * @}
  354. */
  355. /** @defgroup NOR_Exported_Functions_Group2 Input and Output functions
  356. * @brief Input Output and memory control functions
  357. *
  358. @verbatim
  359. ==============================================================================
  360. ##### NOR Input and Output functions #####
  361. ==============================================================================
  362. [..]
  363. This section provides functions allowing to use and control the NOR memory
  364. @endverbatim
  365. * @{
  366. */
  367. /**
  368. * @brief Read NOR flash IDs
  369. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  370. * the configuration information for NOR module.
  371. * @param pNOR_ID pointer to NOR ID structure
  372. * @retval HAL status
  373. */
  374. HAL_StatusTypeDef HAL_NOR_Read_ID(NOR_HandleTypeDef *hnor, NOR_IDTypeDef *pNOR_ID)
  375. {
  376. uint32_t deviceaddress;
  377. HAL_NOR_StateTypeDef state;
  378. HAL_StatusTypeDef status = HAL_OK;
  379. /* Check the NOR controller state */
  380. state = hnor->State;
  381. if (state == HAL_NOR_STATE_BUSY)
  382. {
  383. return HAL_BUSY;
  384. }
  385. else if (state == HAL_NOR_STATE_PROTECTED)
  386. {
  387. return HAL_ERROR;
  388. }
  389. else if (state == HAL_NOR_STATE_READY)
  390. {
  391. /* Process Locked */
  392. __HAL_LOCK(hnor);
  393. /* Update the NOR controller state */
  394. hnor->State = HAL_NOR_STATE_BUSY;
  395. /* Select the NOR device address */
  396. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  397. {
  398. deviceaddress = NOR_MEMORY_ADRESS1;
  399. }
  400. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  401. {
  402. deviceaddress = NOR_MEMORY_ADRESS2;
  403. }
  404. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  405. {
  406. deviceaddress = NOR_MEMORY_ADRESS3;
  407. }
  408. else /* FSMC_NORSRAM_BANK4 */
  409. {
  410. deviceaddress = NOR_MEMORY_ADRESS4;
  411. }
  412. /* Send read ID command */
  413. if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
  414. {
  415. if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
  416. {
  417. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
  418. NOR_CMD_DATA_FIRST);
  419. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
  420. NOR_CMD_DATA_SECOND);
  421. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD_BYTE),
  422. NOR_CMD_DATA_AUTO_SELECT);
  423. }
  424. else
  425. {
  426. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  427. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  428. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD),
  429. NOR_CMD_DATA_AUTO_SELECT);
  430. }
  431. }
  432. else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
  433. {
  434. NOR_WRITE(deviceaddress, NOR_CMD_DATA_AUTO_SELECT);
  435. }
  436. else
  437. {
  438. /* Primary command set not supported by the driver */
  439. status = HAL_ERROR;
  440. }
  441. if (status != HAL_ERROR)
  442. {
  443. /* Read the NOR IDs */
  444. pNOR_ID->Manufacturer_Code = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, MC_ADDRESS);
  445. pNOR_ID->Device_Code1 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth,
  446. DEVICE_CODE1_ADDR);
  447. pNOR_ID->Device_Code2 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth,
  448. DEVICE_CODE2_ADDR);
  449. pNOR_ID->Device_Code3 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth,
  450. DEVICE_CODE3_ADDR);
  451. }
  452. /* Check the NOR controller state */
  453. hnor->State = state;
  454. /* Process unlocked */
  455. __HAL_UNLOCK(hnor);
  456. }
  457. else
  458. {
  459. return HAL_ERROR;
  460. }
  461. return status;
  462. }
  463. /**
  464. * @brief Returns the NOR memory to Read mode.
  465. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  466. * the configuration information for NOR module.
  467. * @retval HAL status
  468. */
  469. HAL_StatusTypeDef HAL_NOR_ReturnToReadMode(NOR_HandleTypeDef *hnor)
  470. {
  471. uint32_t deviceaddress;
  472. HAL_NOR_StateTypeDef state;
  473. HAL_StatusTypeDef status = HAL_OK;
  474. /* Check the NOR controller state */
  475. state = hnor->State;
  476. if (state == HAL_NOR_STATE_BUSY)
  477. {
  478. return HAL_BUSY;
  479. }
  480. else if (state == HAL_NOR_STATE_PROTECTED)
  481. {
  482. return HAL_ERROR;
  483. }
  484. else if (state == HAL_NOR_STATE_READY)
  485. {
  486. /* Process Locked */
  487. __HAL_LOCK(hnor);
  488. /* Update the NOR controller state */
  489. hnor->State = HAL_NOR_STATE_BUSY;
  490. /* Select the NOR device address */
  491. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  492. {
  493. deviceaddress = NOR_MEMORY_ADRESS1;
  494. }
  495. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  496. {
  497. deviceaddress = NOR_MEMORY_ADRESS2;
  498. }
  499. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  500. {
  501. deviceaddress = NOR_MEMORY_ADRESS3;
  502. }
  503. else /* FSMC_NORSRAM_BANK4 */
  504. {
  505. deviceaddress = NOR_MEMORY_ADRESS4;
  506. }
  507. if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
  508. {
  509. NOR_WRITE(deviceaddress, NOR_CMD_DATA_READ_RESET);
  510. }
  511. else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
  512. {
  513. NOR_WRITE(deviceaddress, NOR_CMD_READ_ARRAY);
  514. }
  515. else
  516. {
  517. /* Primary command set not supported by the driver */
  518. status = HAL_ERROR;
  519. }
  520. /* Check the NOR controller state */
  521. hnor->State = state;
  522. /* Process unlocked */
  523. __HAL_UNLOCK(hnor);
  524. }
  525. else
  526. {
  527. return HAL_ERROR;
  528. }
  529. return status;
  530. }
  531. /**
  532. * @brief Read data from NOR memory
  533. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  534. * the configuration information for NOR module.
  535. * @param pAddress pointer to Device address
  536. * @param pData pointer to read data
  537. * @retval HAL status
  538. */
  539. HAL_StatusTypeDef HAL_NOR_Read(NOR_HandleTypeDef *hnor, uint32_t *pAddress, uint16_t *pData)
  540. {
  541. uint32_t deviceaddress;
  542. HAL_NOR_StateTypeDef state;
  543. HAL_StatusTypeDef status = HAL_OK;
  544. /* Check the NOR controller state */
  545. state = hnor->State;
  546. if (state == HAL_NOR_STATE_BUSY)
  547. {
  548. return HAL_BUSY;
  549. }
  550. else if (state == HAL_NOR_STATE_PROTECTED)
  551. {
  552. return HAL_ERROR;
  553. }
  554. else if (state == HAL_NOR_STATE_READY)
  555. {
  556. /* Process Locked */
  557. __HAL_LOCK(hnor);
  558. /* Update the NOR controller state */
  559. hnor->State = HAL_NOR_STATE_BUSY;
  560. /* Select the NOR device address */
  561. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  562. {
  563. deviceaddress = NOR_MEMORY_ADRESS1;
  564. }
  565. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  566. {
  567. deviceaddress = NOR_MEMORY_ADRESS2;
  568. }
  569. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  570. {
  571. deviceaddress = NOR_MEMORY_ADRESS3;
  572. }
  573. else /* FSMC_NORSRAM_BANK4 */
  574. {
  575. deviceaddress = NOR_MEMORY_ADRESS4;
  576. }
  577. /* Send read data command */
  578. if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
  579. {
  580. if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
  581. {
  582. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
  583. NOR_CMD_DATA_FIRST);
  584. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
  585. NOR_CMD_DATA_SECOND);
  586. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD_BYTE),
  587. NOR_CMD_DATA_READ_RESET);
  588. }
  589. else
  590. {
  591. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  592. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  593. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD),
  594. NOR_CMD_DATA_READ_RESET);
  595. }
  596. }
  597. else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
  598. {
  599. NOR_WRITE(pAddress, NOR_CMD_READ_ARRAY);
  600. }
  601. else
  602. {
  603. /* Primary command set not supported by the driver */
  604. status = HAL_ERROR;
  605. }
  606. if (status != HAL_ERROR)
  607. {
  608. /* Read the data */
  609. *pData = (uint16_t)(*(__IO uint32_t *)pAddress);
  610. }
  611. /* Check the NOR controller state */
  612. hnor->State = state;
  613. /* Process unlocked */
  614. __HAL_UNLOCK(hnor);
  615. }
  616. else
  617. {
  618. return HAL_ERROR;
  619. }
  620. return status;
  621. }
  622. /**
  623. * @brief Program data to NOR memory
  624. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  625. * the configuration information for NOR module.
  626. * @param pAddress Device address
  627. * @param pData pointer to the data to write
  628. * @retval HAL status
  629. */
  630. HAL_StatusTypeDef HAL_NOR_Program(NOR_HandleTypeDef *hnor, uint32_t *pAddress, uint16_t *pData)
  631. {
  632. uint32_t deviceaddress;
  633. HAL_StatusTypeDef status = HAL_OK;
  634. /* Check the NOR controller state */
  635. if (hnor->State == HAL_NOR_STATE_BUSY)
  636. {
  637. return HAL_BUSY;
  638. }
  639. else if (hnor->State == HAL_NOR_STATE_READY)
  640. {
  641. /* Process Locked */
  642. __HAL_LOCK(hnor);
  643. /* Update the NOR controller state */
  644. hnor->State = HAL_NOR_STATE_BUSY;
  645. /* Select the NOR device address */
  646. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  647. {
  648. deviceaddress = NOR_MEMORY_ADRESS1;
  649. }
  650. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  651. {
  652. deviceaddress = NOR_MEMORY_ADRESS2;
  653. }
  654. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  655. {
  656. deviceaddress = NOR_MEMORY_ADRESS3;
  657. }
  658. else /* FSMC_NORSRAM_BANK4 */
  659. {
  660. deviceaddress = NOR_MEMORY_ADRESS4;
  661. }
  662. /* Send program data command */
  663. if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
  664. {
  665. if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
  666. {
  667. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
  668. NOR_CMD_DATA_FIRST);
  669. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
  670. NOR_CMD_DATA_SECOND);
  671. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD_BYTE),
  672. NOR_CMD_DATA_PROGRAM);
  673. }
  674. else
  675. {
  676. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  677. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  678. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_PROGRAM);
  679. }
  680. }
  681. else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
  682. {
  683. NOR_WRITE(pAddress, NOR_CMD_WORD_PROGRAM);
  684. }
  685. else
  686. {
  687. /* Primary command set not supported by the driver */
  688. status = HAL_ERROR;
  689. }
  690. if (status != HAL_ERROR)
  691. {
  692. /* Write the data */
  693. NOR_WRITE(pAddress, *pData);
  694. }
  695. /* Check the NOR controller state */
  696. hnor->State = HAL_NOR_STATE_READY;
  697. /* Process unlocked */
  698. __HAL_UNLOCK(hnor);
  699. }
  700. else
  701. {
  702. return HAL_ERROR;
  703. }
  704. return status;
  705. }
  706. /**
  707. * @brief Reads a half-word buffer from the NOR memory.
  708. * @param hnor pointer to the NOR handle
  709. * @param uwAddress NOR memory internal address to read from.
  710. * @param pData pointer to the buffer that receives the data read from the
  711. * NOR memory.
  712. * @param uwBufferSize number of Half word to read.
  713. * @retval HAL status
  714. */
  715. HAL_StatusTypeDef HAL_NOR_ReadBuffer(NOR_HandleTypeDef *hnor, uint32_t uwAddress, uint16_t *pData,
  716. uint32_t uwBufferSize)
  717. {
  718. uint32_t deviceaddress;
  719. uint32_t size = uwBufferSize;
  720. uint32_t address = uwAddress;
  721. uint16_t *data = pData;
  722. HAL_NOR_StateTypeDef state;
  723. HAL_StatusTypeDef status = HAL_OK;
  724. /* Check the NOR controller state */
  725. state = hnor->State;
  726. if (state == HAL_NOR_STATE_BUSY)
  727. {
  728. return HAL_BUSY;
  729. }
  730. else if (state == HAL_NOR_STATE_PROTECTED)
  731. {
  732. return HAL_ERROR;
  733. }
  734. else if (state == HAL_NOR_STATE_READY)
  735. {
  736. /* Process Locked */
  737. __HAL_LOCK(hnor);
  738. /* Update the NOR controller state */
  739. hnor->State = HAL_NOR_STATE_BUSY;
  740. /* Select the NOR device address */
  741. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  742. {
  743. deviceaddress = NOR_MEMORY_ADRESS1;
  744. }
  745. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  746. {
  747. deviceaddress = NOR_MEMORY_ADRESS2;
  748. }
  749. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  750. {
  751. deviceaddress = NOR_MEMORY_ADRESS3;
  752. }
  753. else /* FSMC_NORSRAM_BANK4 */
  754. {
  755. deviceaddress = NOR_MEMORY_ADRESS4;
  756. }
  757. /* Send read data command */
  758. if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
  759. {
  760. if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
  761. {
  762. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
  763. NOR_CMD_DATA_FIRST);
  764. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
  765. NOR_CMD_DATA_SECOND);
  766. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD_BYTE),
  767. NOR_CMD_DATA_READ_RESET);
  768. }
  769. else
  770. {
  771. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  772. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  773. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD),
  774. NOR_CMD_DATA_READ_RESET);
  775. }
  776. }
  777. else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
  778. {
  779. NOR_WRITE(deviceaddress, NOR_CMD_READ_ARRAY);
  780. }
  781. else
  782. {
  783. /* Primary command set not supported by the driver */
  784. status = HAL_ERROR;
  785. }
  786. if (status != HAL_ERROR)
  787. {
  788. /* Read buffer */
  789. while (size > 0U)
  790. {
  791. *data = *(__IO uint16_t *)address;
  792. data++;
  793. address += 2U;
  794. size--;
  795. }
  796. }
  797. /* Check the NOR controller state */
  798. hnor->State = state;
  799. /* Process unlocked */
  800. __HAL_UNLOCK(hnor);
  801. }
  802. else
  803. {
  804. return HAL_ERROR;
  805. }
  806. return status;
  807. }
  808. /**
  809. * @brief Writes a half-word buffer to the NOR memory. This function must be used
  810. only with S29GL128P NOR memory.
  811. * @param hnor pointer to the NOR handle
  812. * @param uwAddress NOR memory internal start write address
  813. * @param pData pointer to source data buffer.
  814. * @param uwBufferSize Size of the buffer to write
  815. * @retval HAL status
  816. */
  817. HAL_StatusTypeDef HAL_NOR_ProgramBuffer(NOR_HandleTypeDef *hnor, uint32_t uwAddress, uint16_t *pData,
  818. uint32_t uwBufferSize)
  819. {
  820. uint16_t *p_currentaddress;
  821. const uint16_t *p_endaddress;
  822. uint16_t *data = pData;
  823. uint32_t deviceaddress;
  824. HAL_StatusTypeDef status = HAL_OK;
  825. /* Check the NOR controller state */
  826. if (hnor->State == HAL_NOR_STATE_BUSY)
  827. {
  828. return HAL_BUSY;
  829. }
  830. else if (hnor->State == HAL_NOR_STATE_READY)
  831. {
  832. /* Process Locked */
  833. __HAL_LOCK(hnor);
  834. /* Update the NOR controller state */
  835. hnor->State = HAL_NOR_STATE_BUSY;
  836. /* Select the NOR device address */
  837. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  838. {
  839. deviceaddress = NOR_MEMORY_ADRESS1;
  840. }
  841. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  842. {
  843. deviceaddress = NOR_MEMORY_ADRESS2;
  844. }
  845. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  846. {
  847. deviceaddress = NOR_MEMORY_ADRESS3;
  848. }
  849. else /* FSMC_NORSRAM_BANK4 */
  850. {
  851. deviceaddress = NOR_MEMORY_ADRESS4;
  852. }
  853. /* Initialize variables */
  854. p_currentaddress = (uint16_t *)(deviceaddress + uwAddress);
  855. p_endaddress = (uint16_t *)(deviceaddress + uwAddress + (2U * (uwBufferSize - 1U)));
  856. if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
  857. {
  858. if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
  859. {
  860. /* Issue unlock command sequence */
  861. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
  862. NOR_CMD_DATA_FIRST);
  863. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
  864. NOR_CMD_DATA_SECOND);
  865. }
  866. else
  867. {
  868. /* Issue unlock command sequence */
  869. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  870. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  871. }
  872. /* Write Buffer Load Command */
  873. NOR_WRITE((deviceaddress + uwAddress), NOR_CMD_DATA_BUFFER_AND_PROG);
  874. NOR_WRITE((deviceaddress + uwAddress), (uint16_t)(uwBufferSize - 1U));
  875. }
  876. else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
  877. {
  878. /* Write Buffer Load Command */
  879. NOR_WRITE((deviceaddress + uwAddress), NOR_CMD_BUFFERED_PROGRAM);
  880. NOR_WRITE((deviceaddress + uwAddress), (uint16_t)(uwBufferSize - 1U));
  881. }
  882. else
  883. {
  884. /* Primary command set not supported by the driver */
  885. status = HAL_ERROR;
  886. }
  887. if (status != HAL_ERROR)
  888. {
  889. /* Load Data into NOR Buffer */
  890. while (p_currentaddress <= p_endaddress)
  891. {
  892. NOR_WRITE(p_currentaddress, *data);
  893. data++;
  894. p_currentaddress ++;
  895. }
  896. if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
  897. {
  898. NOR_WRITE((deviceaddress + uwAddress), NOR_CMD_DATA_BUFFER_AND_PROG_CONFIRM);
  899. }
  900. else /* => hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET */
  901. {
  902. NOR_WRITE((deviceaddress + uwAddress), NOR_CMD_CONFIRM);
  903. }
  904. }
  905. /* Check the NOR controller state */
  906. hnor->State = HAL_NOR_STATE_READY;
  907. /* Process unlocked */
  908. __HAL_UNLOCK(hnor);
  909. }
  910. else
  911. {
  912. return HAL_ERROR;
  913. }
  914. return status;
  915. }
  916. /**
  917. * @brief Erase the specified block of the NOR memory
  918. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  919. * the configuration information for NOR module.
  920. * @param BlockAddress Block to erase address
  921. * @param Address Device address
  922. * @retval HAL status
  923. */
  924. HAL_StatusTypeDef HAL_NOR_Erase_Block(NOR_HandleTypeDef *hnor, uint32_t BlockAddress, uint32_t Address)
  925. {
  926. uint32_t deviceaddress;
  927. HAL_StatusTypeDef status = HAL_OK;
  928. /* Check the NOR controller state */
  929. if (hnor->State == HAL_NOR_STATE_BUSY)
  930. {
  931. return HAL_BUSY;
  932. }
  933. else if (hnor->State == HAL_NOR_STATE_READY)
  934. {
  935. /* Process Locked */
  936. __HAL_LOCK(hnor);
  937. /* Update the NOR controller state */
  938. hnor->State = HAL_NOR_STATE_BUSY;
  939. /* Select the NOR device address */
  940. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  941. {
  942. deviceaddress = NOR_MEMORY_ADRESS1;
  943. }
  944. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  945. {
  946. deviceaddress = NOR_MEMORY_ADRESS2;
  947. }
  948. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  949. {
  950. deviceaddress = NOR_MEMORY_ADRESS3;
  951. }
  952. else /* FSMC_NORSRAM_BANK4 */
  953. {
  954. deviceaddress = NOR_MEMORY_ADRESS4;
  955. }
  956. /* Send block erase command sequence */
  957. if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
  958. {
  959. if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
  960. {
  961. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
  962. NOR_CMD_DATA_FIRST);
  963. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
  964. NOR_CMD_DATA_SECOND);
  965. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD_BYTE),
  966. NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
  967. }
  968. else
  969. {
  970. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  971. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  972. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD),
  973. NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
  974. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FOURTH),
  975. NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH);
  976. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIFTH),
  977. NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH);
  978. }
  979. NOR_WRITE((uint32_t)(BlockAddress + Address), NOR_CMD_DATA_BLOCK_ERASE);
  980. }
  981. else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
  982. {
  983. NOR_WRITE((BlockAddress + Address), NOR_CMD_BLOCK_UNLOCK);
  984. NOR_WRITE((BlockAddress + Address), NOR_CMD_CONFIRM);
  985. NOR_WRITE((BlockAddress + Address), NOR_CMD_BLOCK_ERASE);
  986. NOR_WRITE((BlockAddress + Address), NOR_CMD_CONFIRM);
  987. }
  988. else
  989. {
  990. /* Primary command set not supported by the driver */
  991. status = HAL_ERROR;
  992. }
  993. /* Check the NOR memory status and update the controller state */
  994. hnor->State = HAL_NOR_STATE_READY;
  995. /* Process unlocked */
  996. __HAL_UNLOCK(hnor);
  997. }
  998. else
  999. {
  1000. return HAL_ERROR;
  1001. }
  1002. return status;
  1003. }
  1004. /**
  1005. * @brief Erase the entire NOR chip.
  1006. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  1007. * the configuration information for NOR module.
  1008. * @param Address Device address
  1009. * @retval HAL status
  1010. */
  1011. HAL_StatusTypeDef HAL_NOR_Erase_Chip(NOR_HandleTypeDef *hnor, uint32_t Address)
  1012. {
  1013. uint32_t deviceaddress;
  1014. HAL_StatusTypeDef status = HAL_OK;
  1015. UNUSED(Address);
  1016. /* Check the NOR controller state */
  1017. if (hnor->State == HAL_NOR_STATE_BUSY)
  1018. {
  1019. return HAL_BUSY;
  1020. }
  1021. else if (hnor->State == HAL_NOR_STATE_READY)
  1022. {
  1023. /* Process Locked */
  1024. __HAL_LOCK(hnor);
  1025. /* Update the NOR controller state */
  1026. hnor->State = HAL_NOR_STATE_BUSY;
  1027. /* Select the NOR device address */
  1028. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  1029. {
  1030. deviceaddress = NOR_MEMORY_ADRESS1;
  1031. }
  1032. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  1033. {
  1034. deviceaddress = NOR_MEMORY_ADRESS2;
  1035. }
  1036. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  1037. {
  1038. deviceaddress = NOR_MEMORY_ADRESS3;
  1039. }
  1040. else /* FSMC_NORSRAM_BANK4 */
  1041. {
  1042. deviceaddress = NOR_MEMORY_ADRESS4;
  1043. }
  1044. /* Send NOR chip erase command sequence */
  1045. if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
  1046. {
  1047. if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
  1048. {
  1049. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
  1050. NOR_CMD_DATA_FIRST);
  1051. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
  1052. NOR_CMD_DATA_SECOND);
  1053. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD_BYTE),
  1054. NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
  1055. }
  1056. else
  1057. {
  1058. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
  1059. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
  1060. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD),
  1061. NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
  1062. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FOURTH),
  1063. NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH);
  1064. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIFTH),
  1065. NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH);
  1066. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SIXTH),
  1067. NOR_CMD_DATA_CHIP_ERASE);
  1068. }
  1069. }
  1070. else
  1071. {
  1072. /* Primary command set not supported by the driver */
  1073. status = HAL_ERROR;
  1074. }
  1075. /* Check the NOR memory status and update the controller state */
  1076. hnor->State = HAL_NOR_STATE_READY;
  1077. /* Process unlocked */
  1078. __HAL_UNLOCK(hnor);
  1079. }
  1080. else
  1081. {
  1082. return HAL_ERROR;
  1083. }
  1084. return status;
  1085. }
  1086. /**
  1087. * @brief Read NOR flash CFI IDs
  1088. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  1089. * the configuration information for NOR module.
  1090. * @param pNOR_CFI pointer to NOR CFI IDs structure
  1091. * @retval HAL status
  1092. */
  1093. HAL_StatusTypeDef HAL_NOR_Read_CFI(NOR_HandleTypeDef *hnor, NOR_CFITypeDef *pNOR_CFI)
  1094. {
  1095. uint32_t deviceaddress;
  1096. HAL_NOR_StateTypeDef state;
  1097. /* Check the NOR controller state */
  1098. state = hnor->State;
  1099. if (state == HAL_NOR_STATE_BUSY)
  1100. {
  1101. return HAL_BUSY;
  1102. }
  1103. else if (state == HAL_NOR_STATE_PROTECTED)
  1104. {
  1105. return HAL_ERROR;
  1106. }
  1107. else if (state == HAL_NOR_STATE_READY)
  1108. {
  1109. /* Process Locked */
  1110. __HAL_LOCK(hnor);
  1111. /* Update the NOR controller state */
  1112. hnor->State = HAL_NOR_STATE_BUSY;
  1113. /* Select the NOR device address */
  1114. if (hnor->Init.NSBank == FSMC_NORSRAM_BANK1)
  1115. {
  1116. deviceaddress = NOR_MEMORY_ADRESS1;
  1117. }
  1118. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK2)
  1119. {
  1120. deviceaddress = NOR_MEMORY_ADRESS2;
  1121. }
  1122. else if (hnor->Init.NSBank == FSMC_NORSRAM_BANK3)
  1123. {
  1124. deviceaddress = NOR_MEMORY_ADRESS3;
  1125. }
  1126. else /* FSMC_NORSRAM_BANK4 */
  1127. {
  1128. deviceaddress = NOR_MEMORY_ADRESS4;
  1129. }
  1130. /* Send read CFI query command */
  1131. if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
  1132. {
  1133. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_CFI_BYTE),
  1134. NOR_CMD_DATA_CFI);
  1135. }
  1136. else
  1137. {
  1138. NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_CFI), NOR_CMD_DATA_CFI);
  1139. }
  1140. /* read the NOR CFI information */
  1141. pNOR_CFI->CFI_1 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI1_ADDRESS);
  1142. pNOR_CFI->CFI_2 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI2_ADDRESS);
  1143. pNOR_CFI->CFI_3 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI3_ADDRESS);
  1144. pNOR_CFI->CFI_4 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI4_ADDRESS);
  1145. /* Check the NOR controller state */
  1146. hnor->State = state;
  1147. /* Process unlocked */
  1148. __HAL_UNLOCK(hnor);
  1149. }
  1150. else
  1151. {
  1152. return HAL_ERROR;
  1153. }
  1154. return HAL_OK;
  1155. }
  1156. #if (USE_HAL_NOR_REGISTER_CALLBACKS == 1)
  1157. /**
  1158. * @brief Register a User NOR Callback
  1159. * To be used to override the weak predefined callback
  1160. * @param hnor : NOR handle
  1161. * @param CallbackId : ID of the callback to be registered
  1162. * This parameter can be one of the following values:
  1163. * @arg @ref HAL_NOR_MSP_INIT_CB_ID NOR MspInit callback ID
  1164. * @arg @ref HAL_NOR_MSP_DEINIT_CB_ID NOR MspDeInit callback ID
  1165. * @param pCallback : pointer to the Callback function
  1166. * @retval status
  1167. */
  1168. HAL_StatusTypeDef HAL_NOR_RegisterCallback(NOR_HandleTypeDef *hnor, HAL_NOR_CallbackIDTypeDef CallbackId,
  1169. pNOR_CallbackTypeDef pCallback)
  1170. {
  1171. HAL_StatusTypeDef status = HAL_OK;
  1172. HAL_NOR_StateTypeDef state;
  1173. if (pCallback == NULL)
  1174. {
  1175. return HAL_ERROR;
  1176. }
  1177. state = hnor->State;
  1178. if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_RESET) || (state == HAL_NOR_STATE_PROTECTED))
  1179. {
  1180. switch (CallbackId)
  1181. {
  1182. case HAL_NOR_MSP_INIT_CB_ID :
  1183. hnor->MspInitCallback = pCallback;
  1184. break;
  1185. case HAL_NOR_MSP_DEINIT_CB_ID :
  1186. hnor->MspDeInitCallback = pCallback;
  1187. break;
  1188. default :
  1189. /* update return status */
  1190. status = HAL_ERROR;
  1191. break;
  1192. }
  1193. }
  1194. else
  1195. {
  1196. /* update return status */
  1197. status = HAL_ERROR;
  1198. }
  1199. return status;
  1200. }
  1201. /**
  1202. * @brief Unregister a User NOR Callback
  1203. * NOR Callback is redirected to the weak predefined callback
  1204. * @param hnor : NOR handle
  1205. * @param CallbackId : ID of the callback to be unregistered
  1206. * This parameter can be one of the following values:
  1207. * @arg @ref HAL_NOR_MSP_INIT_CB_ID NOR MspInit callback ID
  1208. * @arg @ref HAL_NOR_MSP_DEINIT_CB_ID NOR MspDeInit callback ID
  1209. * @retval status
  1210. */
  1211. HAL_StatusTypeDef HAL_NOR_UnRegisterCallback(NOR_HandleTypeDef *hnor, HAL_NOR_CallbackIDTypeDef CallbackId)
  1212. {
  1213. HAL_StatusTypeDef status = HAL_OK;
  1214. HAL_NOR_StateTypeDef state;
  1215. state = hnor->State;
  1216. if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_RESET) || (state == HAL_NOR_STATE_PROTECTED))
  1217. {
  1218. switch (CallbackId)
  1219. {
  1220. case HAL_NOR_MSP_INIT_CB_ID :
  1221. hnor->MspInitCallback = HAL_NOR_MspInit;
  1222. break;
  1223. case HAL_NOR_MSP_DEINIT_CB_ID :
  1224. hnor->MspDeInitCallback = HAL_NOR_MspDeInit;
  1225. break;
  1226. default :
  1227. /* update return status */
  1228. status = HAL_ERROR;
  1229. break;
  1230. }
  1231. }
  1232. else
  1233. {
  1234. /* update return status */
  1235. status = HAL_ERROR;
  1236. }
  1237. return status;
  1238. }
  1239. #endif /* (USE_HAL_NOR_REGISTER_CALLBACKS) */
  1240. /**
  1241. * @}
  1242. */
  1243. /** @defgroup NOR_Exported_Functions_Group3 NOR Control functions
  1244. * @brief management functions
  1245. *
  1246. @verbatim
  1247. ==============================================================================
  1248. ##### NOR Control functions #####
  1249. ==============================================================================
  1250. [..]
  1251. This subsection provides a set of functions allowing to control dynamically
  1252. the NOR interface.
  1253. @endverbatim
  1254. * @{
  1255. */
  1256. /**
  1257. * @brief Enables dynamically NOR write operation.
  1258. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  1259. * the configuration information for NOR module.
  1260. * @retval HAL status
  1261. */
  1262. HAL_StatusTypeDef HAL_NOR_WriteOperation_Enable(NOR_HandleTypeDef *hnor)
  1263. {
  1264. /* Check the NOR controller state */
  1265. if (hnor->State == HAL_NOR_STATE_PROTECTED)
  1266. {
  1267. /* Process Locked */
  1268. __HAL_LOCK(hnor);
  1269. /* Update the NOR controller state */
  1270. hnor->State = HAL_NOR_STATE_BUSY;
  1271. /* Enable write operation */
  1272. (void)FSMC_NORSRAM_WriteOperation_Enable(hnor->Instance, hnor->Init.NSBank);
  1273. /* Update the NOR controller state */
  1274. hnor->State = HAL_NOR_STATE_READY;
  1275. /* Process unlocked */
  1276. __HAL_UNLOCK(hnor);
  1277. }
  1278. else
  1279. {
  1280. return HAL_ERROR;
  1281. }
  1282. return HAL_OK;
  1283. }
  1284. /**
  1285. * @brief Disables dynamically NOR write operation.
  1286. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  1287. * the configuration information for NOR module.
  1288. * @retval HAL status
  1289. */
  1290. HAL_StatusTypeDef HAL_NOR_WriteOperation_Disable(NOR_HandleTypeDef *hnor)
  1291. {
  1292. /* Check the NOR controller state */
  1293. if (hnor->State == HAL_NOR_STATE_READY)
  1294. {
  1295. /* Process Locked */
  1296. __HAL_LOCK(hnor);
  1297. /* Update the NOR controller state */
  1298. hnor->State = HAL_NOR_STATE_BUSY;
  1299. /* Disable write operation */
  1300. (void)FSMC_NORSRAM_WriteOperation_Disable(hnor->Instance, hnor->Init.NSBank);
  1301. /* Update the NOR controller state */
  1302. hnor->State = HAL_NOR_STATE_PROTECTED;
  1303. /* Process unlocked */
  1304. __HAL_UNLOCK(hnor);
  1305. }
  1306. else
  1307. {
  1308. return HAL_ERROR;
  1309. }
  1310. return HAL_OK;
  1311. }
  1312. /**
  1313. * @}
  1314. */
  1315. /** @defgroup NOR_Exported_Functions_Group4 NOR State functions
  1316. * @brief Peripheral State functions
  1317. *
  1318. @verbatim
  1319. ==============================================================================
  1320. ##### NOR State functions #####
  1321. ==============================================================================
  1322. [..]
  1323. This subsection permits to get in run-time the status of the NOR controller
  1324. and the data flow.
  1325. @endverbatim
  1326. * @{
  1327. */
  1328. /**
  1329. * @brief return the NOR controller state
  1330. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  1331. * the configuration information for NOR module.
  1332. * @retval NOR controller state
  1333. */
  1334. HAL_NOR_StateTypeDef HAL_NOR_GetState(const NOR_HandleTypeDef *hnor)
  1335. {
  1336. return hnor->State;
  1337. }
  1338. /**
  1339. * @brief Returns the NOR operation status.
  1340. * @param hnor pointer to a NOR_HandleTypeDef structure that contains
  1341. * the configuration information for NOR module.
  1342. * @param Address Device address
  1343. * @param Timeout NOR programming Timeout
  1344. * @retval NOR_Status The returned value can be: HAL_NOR_STATUS_SUCCESS, HAL_NOR_STATUS_ERROR
  1345. * or HAL_NOR_STATUS_TIMEOUT
  1346. */
  1347. HAL_NOR_StatusTypeDef HAL_NOR_GetStatus(NOR_HandleTypeDef *hnor, uint32_t Address, uint32_t Timeout)
  1348. {
  1349. HAL_NOR_StatusTypeDef status = HAL_NOR_STATUS_ONGOING;
  1350. uint16_t tmpsr1;
  1351. uint16_t tmpsr2;
  1352. uint32_t tickstart;
  1353. /* Poll on NOR memory Ready/Busy signal ------------------------------------*/
  1354. HAL_NOR_MspWait(hnor, Timeout);
  1355. /* Get the NOR memory operation status -------------------------------------*/
  1356. /* Get tick */
  1357. tickstart = HAL_GetTick();
  1358. if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
  1359. {
  1360. while ((status != HAL_NOR_STATUS_SUCCESS) && (status != HAL_NOR_STATUS_TIMEOUT))
  1361. {
  1362. /* Check for the Timeout */
  1363. if (Timeout != HAL_MAX_DELAY)
  1364. {
  1365. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1366. {
  1367. status = HAL_NOR_STATUS_TIMEOUT;
  1368. }
  1369. }
  1370. /* Read NOR status register (DQ6 and DQ5) */
  1371. tmpsr1 = *(__IO uint16_t *)Address;
  1372. tmpsr2 = *(__IO uint16_t *)Address;
  1373. /* If DQ6 did not toggle between the two reads then return HAL_NOR_STATUS_SUCCESS */
  1374. if ((tmpsr1 & NOR_MASK_STATUS_DQ6) == (tmpsr2 & NOR_MASK_STATUS_DQ6))
  1375. {
  1376. return HAL_NOR_STATUS_SUCCESS ;
  1377. }
  1378. if ((tmpsr1 & NOR_MASK_STATUS_DQ5) == NOR_MASK_STATUS_DQ5)
  1379. {
  1380. status = HAL_NOR_STATUS_ONGOING;
  1381. }
  1382. tmpsr1 = *(__IO uint16_t *)Address;
  1383. tmpsr2 = *(__IO uint16_t *)Address;
  1384. /* If DQ6 did not toggle between the two reads then return HAL_NOR_STATUS_SUCCESS */
  1385. if ((tmpsr1 & NOR_MASK_STATUS_DQ6) == (tmpsr2 & NOR_MASK_STATUS_DQ6))
  1386. {
  1387. return HAL_NOR_STATUS_SUCCESS;
  1388. }
  1389. if ((tmpsr1 & NOR_MASK_STATUS_DQ5) == NOR_MASK_STATUS_DQ5)
  1390. {
  1391. return HAL_NOR_STATUS_ERROR;
  1392. }
  1393. }
  1394. }
  1395. else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
  1396. {
  1397. do
  1398. {
  1399. NOR_WRITE(Address, NOR_CMD_READ_STATUS_REG);
  1400. tmpsr2 = *(__IO uint16_t *)(Address);
  1401. /* Check for the Timeout */
  1402. if (Timeout != HAL_MAX_DELAY)
  1403. {
  1404. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1405. {
  1406. return HAL_NOR_STATUS_TIMEOUT;
  1407. }
  1408. }
  1409. } while ((tmpsr2 & NOR_MASK_STATUS_DQ7) == 0U);
  1410. NOR_WRITE(Address, NOR_CMD_READ_STATUS_REG);
  1411. tmpsr1 = *(__IO uint16_t *)(Address);
  1412. if ((tmpsr1 & (NOR_MASK_STATUS_DQ5 | NOR_MASK_STATUS_DQ4)) != 0U)
  1413. {
  1414. /* Clear the Status Register */
  1415. NOR_WRITE(Address, NOR_CMD_READ_STATUS_REG);
  1416. status = HAL_NOR_STATUS_ERROR;
  1417. }
  1418. else
  1419. {
  1420. status = HAL_NOR_STATUS_SUCCESS;
  1421. }
  1422. }
  1423. else
  1424. {
  1425. /* Primary command set not supported by the driver */
  1426. status = HAL_NOR_STATUS_ERROR;
  1427. }
  1428. /* Return the operation status */
  1429. return status;
  1430. }
  1431. /**
  1432. * @}
  1433. */
  1434. /**
  1435. * @}
  1436. */
  1437. /**
  1438. * @}
  1439. */
  1440. #endif /* HAL_NOR_MODULE_ENABLED */
  1441. /**
  1442. * @}
  1443. */
  1444. #endif /* FSMC_BANK1 */