stm32l1xx_hal_nor.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_nor.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 5-September-2014
  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) 2014 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 "stm32l1xx_hal.h"
  76. /** @addtogroup STM32L1xx_HAL_Driver
  77. * @{
  78. */
  79. /** @defgroup NOR NOR
  80. * @brief NOR driver modules
  81. * @{
  82. */
  83. #ifdef HAL_NOR_MODULE_ENABLED
  84. #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD)
  85. /* Private typedef -----------------------------------------------------------*/
  86. /* Private define ------------------------------------------------------------*/
  87. /* Private macro -------------------------------------------------------------*/
  88. /* Private variables ---------------------------------------------------------*/
  89. /** @defgroup NOR_Private_Variables NOR Private Variables
  90. * @{
  91. */
  92. static uint32_t uwNORAddress = NOR_MEMORY_ADRESS1;
  93. static uint32_t uwNORMememoryDataWidth = NOR_MEMORY_8B;
  94. /**
  95. * @}
  96. */
  97. /* Private function prototypes -----------------------------------------------*/
  98. /* Private functions ---------------------------------------------------------*/
  99. /** @defgroup NOR_Exported_Functions NOR Exported Functions
  100. * @{
  101. */
  102. /** @defgroup NOR_Exported_Functions_Group1 Initialization and de-initialization functions
  103. * @brief Initialization and Configuration functions
  104. *
  105. @verbatim
  106. ==============================================================================
  107. ##### NOR Initialization and de_initialization functions #####
  108. ==============================================================================
  109. [..]
  110. This section provides functions allowing to initialize/de-initialize
  111. the NOR memory
  112. @endverbatim
  113. * @{
  114. */
  115. /**
  116. * @brief Perform the NOR memory Initialization sequence
  117. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  118. * the configuration information for NOR module.
  119. * @param Timing: pointer to NOR control timing structure
  120. * @param ExtTiming: pointer to NOR extended mode timing structure
  121. * @retval HAL status
  122. */
  123. HAL_StatusTypeDef HAL_NOR_Init(NOR_HandleTypeDef *hnor, FSMC_NORSRAM_TimingTypeDef *Timing, FSMC_NORSRAM_TimingTypeDef *ExtTiming)
  124. {
  125. /* Check the NOR handle parameter */
  126. if(hnor == NULL)
  127. {
  128. return HAL_ERROR;
  129. }
  130. if(hnor->State == HAL_NOR_STATE_RESET)
  131. {
  132. /* Initialize the low level hardware (MSP) */
  133. HAL_NOR_MspInit(hnor);
  134. }
  135. /* Initialize NOR control Interface */
  136. FSMC_NORSRAM_Init(hnor->Instance, &(hnor->Init));
  137. /* Initialize NOR timing Interface */
  138. FSMC_NORSRAM_Timing_Init(hnor->Instance, Timing, hnor->Init.NSBank);
  139. /* Initialize NOR extended mode timing Interface */
  140. FSMC_NORSRAM_Extended_Timing_Init(hnor->Extended, ExtTiming, hnor->Init.NSBank, hnor->Init.ExtendedMode);
  141. /* Enable the NORSRAM device */
  142. __FSMC_NORSRAM_ENABLE(hnor->Instance, hnor->Init.NSBank);
  143. /* Initialize NOR address mapped by FSMC */
  144. if (hnor->Init.NSBank == FSMC_BANK1_NORSRAM1)
  145. {
  146. uwNORAddress = NOR_MEMORY_ADRESS1;
  147. }
  148. else if (hnor->Init.NSBank == FSMC_BANK1_NORSRAM2)
  149. {
  150. uwNORAddress = NOR_MEMORY_ADRESS2;
  151. }
  152. else if (hnor->Init.NSBank == FSMC_BANK1_NORSRAM3)
  153. {
  154. uwNORAddress = NOR_MEMORY_ADRESS3;
  155. }
  156. else
  157. {
  158. uwNORAddress = NOR_MEMORY_ADRESS4;
  159. }
  160. /* Initialize NOR Memory Data Width*/
  161. if (hnor->Init.MemoryDataWidth == FSMC_NORSRAM_MEM_BUS_WIDTH_8)
  162. {
  163. uwNORMememoryDataWidth = NOR_MEMORY_8B;
  164. }
  165. else
  166. {
  167. uwNORMememoryDataWidth = NOR_MEMORY_16B;
  168. }
  169. /* Check the NOR controller state */
  170. hnor->State = HAL_NOR_STATE_READY;
  171. return HAL_OK;
  172. }
  173. /**
  174. * @brief Perform NOR memory De-Initialization sequence
  175. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  176. * the configuration information for NOR module.
  177. * @retval HAL status
  178. */
  179. HAL_StatusTypeDef HAL_NOR_DeInit(NOR_HandleTypeDef *hnor)
  180. {
  181. /* De-Initialize the low level hardware (MSP) */
  182. HAL_NOR_MspDeInit(hnor);
  183. /* Configure the NOR registers with their reset values */
  184. FSMC_NORSRAM_DeInit(hnor->Instance, hnor->Extended, hnor->Init.NSBank);
  185. /* Update the NOR controller state */
  186. hnor->State = HAL_NOR_STATE_RESET;
  187. /* Release Lock */
  188. __HAL_UNLOCK(hnor);
  189. return HAL_OK;
  190. }
  191. /**
  192. * @brief NOR MSP Init
  193. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  194. * the configuration information for NOR module.
  195. * @retval None
  196. */
  197. __weak void HAL_NOR_MspInit(NOR_HandleTypeDef *hnor)
  198. {
  199. /* NOTE : This function Should not be modified, when the callback is needed,
  200. the HAL_NOR_MspInit could be implemented in the user file
  201. */
  202. }
  203. /**
  204. * @brief NOR MSP DeInit
  205. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  206. * the configuration information for NOR module.
  207. * @retval None
  208. */
  209. __weak void HAL_NOR_MspDeInit(NOR_HandleTypeDef *hnor)
  210. {
  211. /* NOTE : This function Should not be modified, when the callback is needed,
  212. the HAL_NOR_MspDeInit could be implemented in the user file
  213. */
  214. }
  215. /**
  216. * @brief NOR BSP Wait fro Ready/Busy signal
  217. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  218. * the configuration information for NOR module.
  219. * @param Timeout: Maximum timeout value
  220. * @retval None
  221. */
  222. __weak void HAL_NOR_MspWait(NOR_HandleTypeDef *hnor, uint32_t Timeout)
  223. {
  224. /* NOTE : This function Should not be modified, when the callback is needed,
  225. the HAL_NOR_BspWait could be implemented in the user file
  226. */
  227. }
  228. /**
  229. * @}
  230. */
  231. /** @defgroup NOR_Exported_Functions_Group2 Input and Output functions
  232. * @brief Input Output and memory control functions
  233. *
  234. @verbatim
  235. ==============================================================================
  236. ##### NOR Input and Output functions #####
  237. ==============================================================================
  238. [..]
  239. This section provides functions allowing to use and control the NOR memory
  240. @endverbatim
  241. * @{
  242. */
  243. /**
  244. * @brief Read NOR flash IDs
  245. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  246. * the configuration information for NOR module.
  247. * @param pNOR_ID : pointer to NOR ID structure
  248. * @retval HAL status
  249. */
  250. HAL_StatusTypeDef HAL_NOR_Read_ID(NOR_HandleTypeDef *hnor, NOR_IDTypeDef *pNOR_ID)
  251. {
  252. /* Process Locked */
  253. __HAL_LOCK(hnor);
  254. /* Check the NOR controller state */
  255. if(hnor->State == HAL_NOR_STATE_BUSY)
  256. {
  257. return HAL_BUSY;
  258. }
  259. /* Update the NOR controller state */
  260. hnor->State = HAL_NOR_STATE_BUSY;
  261. /* Send read ID command */
  262. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0555), 0x00AA);
  263. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x02AA), 0x0055);
  264. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0555), 0x0090);
  265. /* Read the NOR IDs */
  266. pNOR_ID->ManufacturerCode = *(__IO uint16_t *) __NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, MC_ADDRESS);
  267. pNOR_ID->DeviceCode1 = *(__IO uint16_t *) __NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, DEVICE_CODE1_ADDR);
  268. pNOR_ID->DeviceCode2 = *(__IO uint16_t *) __NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, DEVICE_CODE2_ADDR);
  269. pNOR_ID->DeviceCode3 = *(__IO uint16_t *) __NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, DEVICE_CODE3_ADDR);
  270. /* Check the NOR controller state */
  271. hnor->State = HAL_NOR_STATE_READY;
  272. /* Process unlocked */
  273. __HAL_UNLOCK(hnor);
  274. return HAL_OK;
  275. }
  276. /**
  277. * @brief Returns the NOR memory to Read mode.
  278. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  279. * the configuration information for NOR module.
  280. * @retval HAL status
  281. */
  282. HAL_StatusTypeDef HAL_NOR_ReturnToReadMode(NOR_HandleTypeDef *hnor)
  283. {
  284. /* Process Locked */
  285. __HAL_LOCK(hnor);
  286. /* Check the NOR controller state */
  287. if(hnor->State == HAL_NOR_STATE_BUSY)
  288. {
  289. return HAL_BUSY;
  290. }
  291. __NOR_WRITE(uwNORAddress, 0x00F0);
  292. /* Check the NOR controller state */
  293. hnor->State = HAL_NOR_STATE_READY;
  294. /* Process unlocked */
  295. __HAL_UNLOCK(hnor);
  296. return HAL_OK;
  297. }
  298. /**
  299. * @brief Read data from NOR memory
  300. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  301. * the configuration information for NOR module.
  302. * @param pAddress: pointer to Device address
  303. * @param pData : pointer to read data
  304. * @retval HAL status
  305. */
  306. HAL_StatusTypeDef HAL_NOR_Read(NOR_HandleTypeDef *hnor, uint32_t *pAddress, uint16_t *pData)
  307. {
  308. /* Process Locked */
  309. __HAL_LOCK(hnor);
  310. /* Check the NOR controller state */
  311. if(hnor->State == HAL_NOR_STATE_BUSY)
  312. {
  313. return HAL_BUSY;
  314. }
  315. /* Update the NOR controller state */
  316. hnor->State = HAL_NOR_STATE_BUSY;
  317. /* Send read data command */
  318. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x00555), 0x00AA);
  319. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x002AA), 0x0055);
  320. __NOR_WRITE(*pAddress, 0x00F0);
  321. /* Read the data */
  322. *pData = *(__IO uint32_t *)pAddress;
  323. /* Check the NOR controller state */
  324. hnor->State = HAL_NOR_STATE_READY;
  325. /* Process unlocked */
  326. __HAL_UNLOCK(hnor);
  327. return HAL_OK;
  328. }
  329. /**
  330. * @brief Program data to NOR memory
  331. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  332. * the configuration information for NOR module.
  333. * @param pAddress: Device address
  334. * @param pData : pointer to the data to write
  335. * @retval HAL status
  336. */
  337. HAL_StatusTypeDef HAL_NOR_Program(NOR_HandleTypeDef *hnor, uint32_t *pAddress, uint16_t *pData)
  338. {
  339. /* Process Locked */
  340. __HAL_LOCK(hnor);
  341. /* Check the NOR controller state */
  342. if(hnor->State == HAL_NOR_STATE_BUSY)
  343. {
  344. return HAL_BUSY;
  345. }
  346. /* Update the NOR controller state */
  347. hnor->State = HAL_NOR_STATE_BUSY;
  348. /* Send program data command */
  349. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0555), 0x00AA);
  350. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x02AA), 0x0055);
  351. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0555), 0x00A0);
  352. /* Write the data */
  353. __NOR_WRITE(pAddress, *pData);
  354. /* Check the NOR controller state */
  355. hnor->State = HAL_NOR_STATE_READY;
  356. /* Process unlocked */
  357. __HAL_UNLOCK(hnor);
  358. return HAL_OK;
  359. }
  360. /**
  361. * @brief Reads a block of data from the FSMC NOR memory.
  362. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  363. * the configuration information for NOR module.
  364. * @param uwAddress: NOR memory internal address to read from.
  365. * @param pData: pointer to the buffer that receives the data read from the
  366. * NOR memory.
  367. * @param uwBufferSize : number of Half word to read.
  368. * @retval HAL status
  369. */
  370. HAL_StatusTypeDef HAL_NOR_ReadBuffer(NOR_HandleTypeDef *hnor, uint32_t uwAddress, uint16_t *pData, uint32_t uwBufferSize)
  371. {
  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. /* Update the NOR controller state */
  380. hnor->State = HAL_NOR_STATE_BUSY;
  381. /* Send read data command */
  382. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x00555), 0x00AA);
  383. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x002AA), 0x0055);
  384. __NOR_WRITE(uwAddress, 0x00F0);
  385. /* Read buffer */
  386. while( uwBufferSize > 0)
  387. {
  388. *pData++ = *(__IO uint16_t *)uwAddress;
  389. uwAddress += 2;
  390. uwBufferSize--;
  391. }
  392. /* Check the NOR controller state */
  393. hnor->State = HAL_NOR_STATE_READY;
  394. /* Process unlocked */
  395. __HAL_UNLOCK(hnor);
  396. return HAL_OK;
  397. }
  398. /**
  399. * @brief Writes a half-word buffer to the FSMC NOR memory. This function
  400. * must be used only with S29GL128P NOR memory.
  401. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  402. * the configuration information for NOR module.
  403. * @param uwAddress: NOR memory internal address from which the data
  404. * @param pData: pointer to source data buffer.
  405. * @param uwBufferSize: number of Half words to write. The maximum allowed
  406. * @retval HAL status
  407. */
  408. HAL_StatusTypeDef HAL_NOR_ProgramBuffer(NOR_HandleTypeDef *hnor, uint32_t uwAddress, uint16_t *pData, uint32_t uwBufferSize)
  409. {
  410. uint32_t lastloadedaddress = 0;
  411. uint32_t currentaddress = 0;
  412. uint32_t endaddress = 0;
  413. /* Process Locked */
  414. __HAL_LOCK(hnor);
  415. /* Check the NOR controller state */
  416. if(hnor->State == HAL_NOR_STATE_BUSY)
  417. {
  418. return HAL_BUSY;
  419. }
  420. /* Update the NOR controller state */
  421. hnor->State = HAL_NOR_STATE_BUSY;
  422. /* Initialize variables */
  423. currentaddress = uwAddress;
  424. endaddress = uwAddress + uwBufferSize - 1;
  425. lastloadedaddress = uwAddress;
  426. /* Issue unlock command sequence */
  427. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0555), 0x00AA);
  428. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x02AA), 0x0055);
  429. /* Write Buffer Load Command */
  430. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, uwAddress), 0x25);
  431. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, uwAddress), (uwBufferSize - 1));
  432. /* Load Data into NOR Buffer */
  433. while(currentaddress <= endaddress)
  434. {
  435. /* Store last loaded address & data value (for polling) */
  436. lastloadedaddress = currentaddress;
  437. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, currentaddress), *pData++);
  438. currentaddress += 1;
  439. }
  440. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, lastloadedaddress), 0x29);
  441. /* Check the NOR controller state */
  442. hnor->State = HAL_NOR_STATE_READY;
  443. /* Process unlocked */
  444. __HAL_UNLOCK(hnor);
  445. return HAL_OK;
  446. }
  447. /**
  448. * @brief Erase the specified block of the NOR memory
  449. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  450. * the configuration information for NOR module.
  451. * @param BlockAddress : Block to erase address
  452. * @param Address: Device address
  453. * @retval HAL status
  454. */
  455. HAL_StatusTypeDef HAL_NOR_Erase_Block(NOR_HandleTypeDef *hnor, uint32_t BlockAddress, uint32_t Address)
  456. {
  457. /* Process Locked */
  458. __HAL_LOCK(hnor);
  459. /* Check the NOR controller state */
  460. if(hnor->State == HAL_NOR_STATE_BUSY)
  461. {
  462. return HAL_BUSY;
  463. }
  464. /* Update the NOR controller state */
  465. hnor->State = HAL_NOR_STATE_BUSY;
  466. /* Send block erase command sequence */
  467. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0555), 0x00AA);
  468. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x02AA), 0x0055);
  469. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0555), 0x0080);
  470. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0555), 0x00AA);
  471. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x02AA), 0x0055);
  472. __NOR_WRITE((uint32_t)(BlockAddress + Address), 0x30);
  473. /* Check the NOR memory status and update the controller state */
  474. hnor->State = HAL_NOR_STATE_READY;
  475. /* Process unlocked */
  476. __HAL_UNLOCK(hnor);
  477. return HAL_OK;
  478. }
  479. /**
  480. * @brief Erase the entire NOR chip.
  481. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  482. * the configuration information for NOR module.
  483. * @param Address : Device address
  484. * @retval HAL status
  485. */
  486. HAL_StatusTypeDef HAL_NOR_Erase_Chip(NOR_HandleTypeDef *hnor, uint32_t Address)
  487. {
  488. /* Process Locked */
  489. __HAL_LOCK(hnor);
  490. /* Check the NOR controller state */
  491. if(hnor->State == HAL_NOR_STATE_BUSY)
  492. {
  493. return HAL_BUSY;
  494. }
  495. /* Update the NOR controller state */
  496. hnor->State = HAL_NOR_STATE_BUSY;
  497. /* Send NOR chip erase command sequence */
  498. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0555), 0x00AA);
  499. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x02AA), 0x0055);
  500. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0555), 0x0080);
  501. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0555), 0x00AA);
  502. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x02AA), 0x0055);
  503. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0555), 0x0010);
  504. /* Check the NOR memory status and update the controller state */
  505. hnor->State = HAL_NOR_STATE_READY;
  506. /* Process unlocked */
  507. __HAL_UNLOCK(hnor);
  508. return HAL_OK;
  509. }
  510. /**
  511. * @brief Read NOR flash CFI IDs
  512. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  513. * the configuration information for NOR module.
  514. * @param pNOR_CFI : pointer to NOR CFI IDs structure
  515. * @retval HAL status
  516. */
  517. HAL_StatusTypeDef HAL_NOR_Read_CFI(NOR_HandleTypeDef *hnor, NOR_CFITypeDef *pNOR_CFI)
  518. {
  519. /* Process Locked */
  520. __HAL_LOCK(hnor);
  521. /* Check the NOR controller state */
  522. if(hnor->State == HAL_NOR_STATE_BUSY)
  523. {
  524. return HAL_BUSY;
  525. }
  526. /* Update the NOR controller state */
  527. hnor->State = HAL_NOR_STATE_BUSY;
  528. /* Send read CFI query command */
  529. __NOR_WRITE(__NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, 0x0055), 0x0098);
  530. /* read the NOR CFI information */
  531. pNOR_CFI->CFI1 = *(__IO uint16_t *) __NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, CFI1_ADDRESS);
  532. pNOR_CFI->CFI2 = *(__IO uint16_t *) __NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, CFI2_ADDRESS);
  533. pNOR_CFI->CFI3 = *(__IO uint16_t *) __NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, CFI3_ADDRESS);
  534. pNOR_CFI->CFI4 = *(__IO uint16_t *) __NOR_ADDR_SHIFT(uwNORAddress, uwNORMememoryDataWidth, CFI4_ADDRESS);
  535. /* Check the NOR controller state */
  536. hnor->State = HAL_NOR_STATE_READY;
  537. /* Process unlocked */
  538. __HAL_UNLOCK(hnor);
  539. return HAL_OK;
  540. }
  541. /**
  542. * @}
  543. */
  544. /** @defgroup NOR_Exported_Functions_Group3 Control functions
  545. * @brief management functions
  546. *
  547. @verbatim
  548. ==============================================================================
  549. ##### NOR Control functions #####
  550. ==============================================================================
  551. [..]
  552. This subsection provides a set of functions allowing to control dynamically
  553. the NOR interface.
  554. @endverbatim
  555. * @{
  556. */
  557. /**
  558. * @brief Enables dynamically NOR write operation.
  559. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  560. * the configuration information for NOR module.
  561. * @retval HAL status
  562. */
  563. HAL_StatusTypeDef HAL_NOR_WriteOperation_Enable(NOR_HandleTypeDef *hnor)
  564. {
  565. /* Process Locked */
  566. __HAL_LOCK(hnor);
  567. /* Enable write operation */
  568. FSMC_NORSRAM_WriteOperation_Enable(hnor->Instance, hnor->Init.NSBank);
  569. /* Update 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 Disables dynamically NOR write operation.
  577. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  578. * the configuration information for NOR module.
  579. * @retval HAL status
  580. */
  581. HAL_StatusTypeDef HAL_NOR_WriteOperation_Disable(NOR_HandleTypeDef *hnor)
  582. {
  583. /* Process Locked */
  584. __HAL_LOCK(hnor);
  585. /* Update the SRAM controller state */
  586. hnor->State = HAL_NOR_STATE_BUSY;
  587. /* Disable write operation */
  588. FSMC_NORSRAM_WriteOperation_Disable(hnor->Instance, hnor->Init.NSBank);
  589. /* Update the NOR controller state */
  590. hnor->State = HAL_NOR_STATE_PROTECTED;
  591. /* Process unlocked */
  592. __HAL_UNLOCK(hnor);
  593. return HAL_OK;
  594. }
  595. /**
  596. * @}
  597. */
  598. /** @defgroup NOR_Exported_Functions_Group4 State functions
  599. * @brief Peripheral State functions
  600. *
  601. @verbatim
  602. ==============================================================================
  603. ##### NOR State functions #####
  604. ==============================================================================
  605. [..]
  606. This subsection permits to get in run-time the status of the NOR controller
  607. and the data flow.
  608. @endverbatim
  609. * @{
  610. */
  611. /**
  612. * @brief return the NOR controller state
  613. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  614. * the configuration information for NOR module.
  615. * @retval NOR controller state
  616. */
  617. HAL_NOR_StateTypeDef HAL_NOR_GetState(NOR_HandleTypeDef *hnor)
  618. {
  619. return hnor->State;
  620. }
  621. /**
  622. * @brief Returns the NOR operation status.
  623. * @param hnor: pointer to a NOR_HandleTypeDef structure that contains
  624. * the configuration information for NOR module.
  625. * @param Address: Device address
  626. * @param Timeout: NOR progamming Timeout
  627. * @retval NOR_Status: The returned value can be: NOR_SUCCESS, NOR_ERROR
  628. * or NOR_TIMEOUT
  629. */
  630. NOR_StatusTypedef HAL_NOR_GetStatus(NOR_HandleTypeDef *hnor, uint32_t Address, uint32_t Timeout)
  631. {
  632. NOR_StatusTypedef status = NOR_ONGOING;
  633. uint16_t tmpSR1 = 0, tmpSR2 = 0;
  634. uint32_t tickstart = 0;
  635. /* Poll on NOR memory Ready/Busy signal ------------------------------------*/
  636. HAL_NOR_MspWait(hnor, Timeout);
  637. /* Get the NOR memory operation status -------------------------------------*/
  638. while(status != NOR_TIMEOUT)
  639. {
  640. /* Get tick */
  641. tickstart = HAL_GetTick();
  642. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  643. {
  644. status = NOR_TIMEOUT;
  645. }
  646. /* Read NOR status register (DQ6 and DQ5) */
  647. tmpSR1 = *(__IO uint16_t *)Address;
  648. tmpSR2 = *(__IO uint16_t *)Address;
  649. /* If DQ6 did not toggle between the two reads then return NOR_Success */
  650. if((tmpSR1 & 0x0040) == (tmpSR2 & 0x0040))
  651. {
  652. return NOR_SUCCESS;
  653. }
  654. if((tmpSR1 & 0x0020) == 0x0020)
  655. {
  656. return NOR_ONGOING;
  657. }
  658. tmpSR1 = *(__IO uint16_t *)Address;
  659. tmpSR2 = *(__IO uint16_t *)Address;
  660. /* If DQ6 did not toggle between the two reads then return NOR_Success */
  661. if((tmpSR1 & 0x0040) == (tmpSR2 & 0x0040))
  662. {
  663. return NOR_SUCCESS;
  664. }
  665. if((tmpSR1 & 0x0020) == 0x0020)
  666. {
  667. return NOR_ERROR;
  668. }
  669. }
  670. /* Return the operation status */
  671. return status;
  672. }
  673. /**
  674. * @}
  675. */
  676. /**
  677. * @}
  678. */
  679. #endif /* STM32L151xD || STM32L152xD || STM32L162xD */
  680. #endif /* HAL_NOR_MODULE_ENABLED */
  681. /**
  682. * @}
  683. */
  684. /**
  685. * @}
  686. */
  687. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/