stm32f4xx_hal_nor.c 34 KB

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