stm32f1xx_hal_nor.c 33 KB

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