stm32f1xx_hal_nor.c 33 KB

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