stm32h7xx_hal_flash.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_flash.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 21-April-2017
  7. * @brief FLASH HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the internal FLASH memory:
  10. * + Program operations functions
  11. * + Memory Control functions
  12. * + Peripheral Errors functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### FLASH peripheral features #####
  17. ==============================================================================
  18. [..] The Flash memory interface manages CPU AXI I-Code and D-Code accesses
  19. to the Flash memory. It implements the erase and program Flash memory operations
  20. and the read and write protection mechanisms.
  21. [..] The FLASH main features are:
  22. (+) Flash memory read operations
  23. (+) Flash memory program/erase operations
  24. (+) Read / write protections
  25. (+) Option bytes programming
  26. (+) Error code correction (ECC) : Data in flash are 266-bits word
  27. (10 bits added per double word)
  28. ##### How to use this driver #####
  29. ==============================================================================
  30. [..]
  31. This driver provides functions and macros to configure and program the FLASH
  32. memory of all STM32H7xx devices.
  33. (#) FLASH Memory IO Programming functions:
  34. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  35. HAL_FLASH_Lock() functions
  36. (++) Program functions: double word only
  37. (++) There Two modes of programming :
  38. (+++) Polling mode using HAL_FLASH_Program() function
  39. (+++) Interrupt mode using HAL_FLASH_Program_IT() function
  40. (#) Interrupts and flags management functions :
  41. (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
  42. (++) Callback functions are called when the flash operations are finished :
  43. HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise
  44. HAL_FLASH_OperationErrorCallback()
  45. (++) Get error flag status by calling HAL_FLASH_GetError()
  46. (#) Option bytes management functions :
  47. (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and
  48. HAL_FLASH_OB_Lock() functions
  49. (++) Launch the reload of the option bytes using HAL_FLASH_Launch() function.
  50. In this case, a reset is generated
  51. [..]
  52. In addition to these functions, this driver includes a set of macros allowing
  53. to handle the following operations:
  54. (+) Set the latency
  55. (+) Enable/Disable the FLASH interrupts
  56. (+) Monitor the FLASH flags status
  57. [..]
  58. (@) For any Flash memory program operation (erase or program), the CPU clock frequency
  59. (HCLK) must be at least 1MHz.
  60. (@) The contents of the Flash memory are not guaranteed if a device reset occurs during
  61. a Flash memory operation.
  62. (@) Any attempt to read the Flash memory while it is being written or erased, causes the
  63. bus to stall. Read operations are processed correctly once the program operation has
  64. completed. This means that code or data fetches cannot be performed while a write/erase
  65. operation is ongoing
  66. @endverbatim
  67. ******************************************************************************
  68. * @attention
  69. *
  70. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  71. *
  72. * Redistribution and use in source and binary forms, with or without modification,
  73. * are permitted provided that the following conditions are met:
  74. * 1. Redistributions of source code must retain the above copyright notice,
  75. * this list of conditions and the following disclaimer.
  76. * 2. Redistributions in binary form must reproduce the above copyright notice,
  77. * this list of conditions and the following disclaimer in the documentation
  78. * and/or other materials provided with the distribution.
  79. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  80. * may be used to endorse or promote products derived from this software
  81. * without specific prior written permission.
  82. *
  83. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  84. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  85. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  86. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  87. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  88. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  89. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  90. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  91. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  92. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  93. *
  94. ******************************************************************************
  95. */
  96. /* Includes ------------------------------------------------------------------*/
  97. #include "stm32h7xx_hal.h"
  98. /** @addtogroup STM32H7xx_HAL_Driver
  99. * @{
  100. */
  101. /** @defgroup FLASH FLASH
  102. * @brief FLASH HAL module driver
  103. * @{
  104. */
  105. #ifdef HAL_FLASH_MODULE_ENABLED
  106. /* Private typedef -----------------------------------------------------------*/
  107. /* Private define ------------------------------------------------------------*/
  108. /** @addtogroup FLASH_Private_Constants
  109. * @{
  110. */
  111. #define FLASH_TIMEOUT_VALUE ((uint32_t)50000U)/* 50 s */
  112. /**
  113. * @}
  114. */
  115. /* Private macro -------------------------------------------------------------*/
  116. /* Private variables ---------------------------------------------------------*/
  117. FLASH_ProcessTypeDef pFlash;
  118. /* Private function prototypes -----------------------------------------------*/
  119. static void FLASH_SetErrorCode(uint32_t Bank);
  120. /* Private functions ---------------------------------------------------------*/
  121. /** @defgroup FLASH_Private_Functions FLASH Private functions
  122. * @{
  123. */
  124. /** @defgroup FLASH_Group1 Programming operation functions
  125. * @brief Programming operation functions
  126. *
  127. @verbatim
  128. ===============================================================================
  129. ##### Programming operation functions #####
  130. ===============================================================================
  131. [..]
  132. This subsection provides a set of functions allowing to manage the FLASH
  133. program operations.
  134. @endverbatim
  135. * @{
  136. */
  137. /**
  138. * @brief Program flash word of 256 bits at a specified address
  139. * @param TypeProgram Indicate the way to program at a specified address.
  140. * This parameter can be a value of @ref FLASH_Type_Program
  141. * @param FlashAddress specifies the address to be programmed.
  142. * @param DataAddress specifies the address of data (256 bits) to be programmed
  143. *
  144. * @retval HAL_StatusTypeDef HAL Status
  145. */
  146. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t FlashAddress, uint64_t DataAddress)
  147. {
  148. HAL_StatusTypeDef status = HAL_ERROR;
  149. __IO uint64_t *dest_addr = (__IO uint64_t *)FlashAddress;
  150. __IO uint64_t *src_addr = (__IO uint64_t*)((uint32_t)DataAddress);
  151. uint32_t bank;
  152. uint8_t row_index = 4;
  153. /* Process Locked */
  154. __HAL_LOCK(&pFlash);
  155. /* Check the parameters */
  156. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  157. assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress));
  158. if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
  159. {
  160. bank = FLASH_BANK_1;
  161. }
  162. else
  163. {
  164. bank = FLASH_BANK_2;
  165. }
  166. /* Wait for last operation to be completed */
  167. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
  168. if(status == HAL_OK)
  169. {
  170. if(bank == FLASH_BANK_1)
  171. {
  172. /* Clear bank 1 pending flags (if any) */
  173. __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1 | FLASH_FLAG_QW_BANK1 | FLASH_FLAG_WBNE_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK1);
  174. /* Set PG bit */
  175. SET_BIT(FLASH->CR1, FLASH_CR_PG);
  176. }
  177. else
  178. {
  179. /* Clear bank 2 pending flags (if any) */
  180. __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2 | FLASH_FLAG_QW_BANK2 | FLASH_FLAG_WBNE_BANK2 | FLASH_FLAG_ALL_ERRORS_BANK2);
  181. /* Set PG bit */
  182. SET_BIT(FLASH->CR2, FLASH_CR_PG);
  183. }
  184. /* Program the 256 bits flash word */
  185. do
  186. {
  187. *dest_addr++ = *src_addr++;
  188. } while (--row_index != 0);
  189. __DSB();
  190. /* Wait for last operation to be completed */
  191. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
  192. if(bank == FLASH_BANK_1)
  193. {
  194. /* Check FLASH End of Operation flag */
  195. if (__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_EOP_BANK1))
  196. {
  197. /* Clear FLASH End of Operation pending bit */
  198. __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
  199. }
  200. /* If the program operation is completed, disable the PG*/
  201. CLEAR_BIT(FLASH->CR1, FLASH_CR_PG);
  202. }
  203. else
  204. {
  205. /* Check FLASH End of Operation flag */
  206. if (__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_EOP_BANK2))
  207. {
  208. /* Clear FLASH End of Operation pending bit */
  209. __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
  210. }
  211. /* If the program operation is completed, disable the PG */
  212. CLEAR_BIT(FLASH->CR2, FLASH_CR_PG);
  213. }
  214. }
  215. /* Process Unlocked */
  216. __HAL_UNLOCK(&pFlash);
  217. return status;
  218. }
  219. /**
  220. * @brief Program flash words of 256 bits at a specified address with interrupt enabled.
  221. * @param TypeProgram Indicate the way to program at a specified address.
  222. * This parameter can be a value of @ref FLASH_Type_Program
  223. * @param FlashAddress specifies the address to be programmed.
  224. * @param DataAddress specifies the address of data (256 bits) to be programmed
  225. *
  226. * @retval HAL Status
  227. */
  228. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t FlashAddress, uint64_t DataAddress)
  229. {
  230. HAL_StatusTypeDef status = HAL_OK;
  231. __IO uint64_t *dest_addr = (__IO uint64_t*)FlashAddress;
  232. __IO uint64_t *src_addr = (__IO uint64_t*)((uint32_t)DataAddress);
  233. uint32_t bank;
  234. uint8_t row_index = 4;
  235. /* Process Locked */
  236. __HAL_LOCK(&pFlash);
  237. /* Check the parameters */
  238. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  239. assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress));
  240. if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
  241. {
  242. bank = FLASH_BANK_1;
  243. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK1;
  244. }
  245. else
  246. {
  247. bank = FLASH_BANK_2;
  248. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK2;
  249. }
  250. /* Set internal variables used by the IRQ handler */
  251. pFlash.Address = FlashAddress;
  252. if(bank == FLASH_BANK_1)
  253. {
  254. /* Clear bank 1 pending flags (if any) */
  255. __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK1);
  256. /* Set PG bit */
  257. SET_BIT(FLASH->CR1, FLASH_CR_PG);
  258. /* Enable End of Operation and Error interrupts for Bank 1 */
  259. __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
  260. FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
  261. }
  262. else
  263. {
  264. /* Clear bank 2 pending flags (if any) */
  265. __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2 | FLASH_FLAG_ALL_ERRORS_BANK2);
  266. /* Set PG bit */
  267. SET_BIT(FLASH->CR2, FLASH_CR_PG);
  268. /* Enable End of Operation and Error interrupts for Bank2*/
  269. __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
  270. FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2);
  271. }
  272. /* Program the 256 bits flash word */
  273. do
  274. {
  275. *dest_addr++ = *src_addr++;
  276. } while (--row_index != 0);
  277. return status;
  278. }
  279. /**
  280. * @brief This function handles FLASH interrupt request.
  281. * @retval None
  282. */
  283. void HAL_FLASH_IRQHandler(void)
  284. {
  285. uint32_t temp;
  286. /* Check FLASH Bank1 End of Operation flag */
  287. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_SR_EOP) != RESET)
  288. {
  289. if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE_BANK1)
  290. {
  291. /*Nb of sector to erased can be decreased*/
  292. pFlash.NbSectorsToErase--;
  293. /* Check if there are still sectors to erase*/
  294. if(pFlash.NbSectorsToErase != 0)
  295. {
  296. temp = pFlash.Sector;
  297. /*Indicate user which sector has been erased*/
  298. HAL_FLASH_EndOfOperationCallback(temp);
  299. /* Clear pending flags (if any) */
  300. /* Clear bank 1 pending flags (if any) */
  301. __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK1);
  302. /*Increment sector number*/
  303. temp = ++pFlash.Sector;
  304. FLASH_Erase_Sector(temp, FLASH_BANK_1/*pFlash.Bank*/, pFlash.VoltageForErase);
  305. }
  306. else
  307. {
  308. /*No more sectors to Erase, user callback can be called.*/
  309. /*Reset Sector and stop Erase sectors procedure*/
  310. pFlash.Sector = temp = 0xFFFFFFFF;
  311. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  312. /* FLASH EOP interrupt user callback */
  313. HAL_FLASH_EndOfOperationCallback(temp);
  314. /* Clear FLASH End of Operation pending bit */
  315. __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
  316. }
  317. }
  318. else
  319. {
  320. if((pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE_BANK1) || (pFlash.ProcedureOnGoing == FLASH_PROC_ALLBANK_MASSERASE))
  321. {
  322. /*MassErase ended. Return the selected bank*/
  323. /* FLASH EOP interrupt user callback */
  324. HAL_FLASH_EndOfOperationCallback(FLASH_BANK_1);
  325. }
  326. else if(pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_BANK1)
  327. {
  328. /*Program ended. Return the selected address*/
  329. /* FLASH EOP interrupt user callback */
  330. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  331. }
  332. if((pFlash.ProcedureOnGoing != FLASH_PROC_SECTERASE_BANK2) && \
  333. (pFlash.ProcedureOnGoing != FLASH_PROC_MASSERASE_BANK2)&& \
  334. (pFlash.ProcedureOnGoing != FLASH_PROC_PROGRAM_BANK2))
  335. {
  336. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  337. /* Clear FLASH End of Operation pending bit */
  338. __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
  339. }
  340. }
  341. }
  342. /* Check FLASH Bank2 End of Operation flag */
  343. if(__HAL_FLASH_GET_FLAG_BANK2(FLASH_SR_EOP) != RESET)
  344. {
  345. if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE_BANK2)
  346. {
  347. /*Nb of sector to erased can be decreased*/
  348. pFlash.NbSectorsToErase--;
  349. /* Check if there are still sectors to erase*/
  350. if(pFlash.NbSectorsToErase != 0)
  351. {
  352. temp = pFlash.Sector;
  353. /*Indicate user which sector has been erased*/
  354. HAL_FLASH_EndOfOperationCallback(temp);
  355. /* Clear pending flags (if any) */
  356. /* Clear bank 2 pending flags (if any) */
  357. __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2 | FLASH_FLAG_ALL_ERRORS_BANK2);
  358. /*Increment sector number*/
  359. temp = ++pFlash.Sector;
  360. FLASH_Erase_Sector(temp, FLASH_BANK_2 /*pFlash.Bank*/, pFlash.VoltageForErase);
  361. }
  362. else
  363. {
  364. /*No more sectors to Erase, user callback can be called.*/
  365. /*Reset Sector and stop Erase sectors procedure*/
  366. pFlash.Sector = temp = 0xFFFFFFFF;
  367. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  368. /* FLASH EOP interrupt user callback */
  369. HAL_FLASH_EndOfOperationCallback(temp);
  370. /* Clear FLASH End of Operation pending bit */
  371. __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
  372. }
  373. }
  374. else
  375. {
  376. if((pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE_BANK2) || (pFlash.ProcedureOnGoing == FLASH_PROC_ALLBANK_MASSERASE))
  377. {
  378. /*MassErase ended. Return the selected bank*/
  379. /* FLASH EOP interrupt user callback */
  380. HAL_FLASH_EndOfOperationCallback(FLASH_BANK_2);
  381. }
  382. else
  383. {
  384. /*Program ended. Return the selected address*/
  385. /* FLASH EOP interrupt user callback */
  386. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  387. }
  388. if((pFlash.ProcedureOnGoing != FLASH_PROC_SECTERASE_BANK1) && \
  389. (pFlash.ProcedureOnGoing != FLASH_PROC_MASSERASE_BANK1)&& \
  390. (pFlash.ProcedureOnGoing != FLASH_PROC_PROGRAM_BANK1))
  391. {
  392. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  393. /* Clear FLASH End of Operation pending bit */
  394. __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
  395. }
  396. }
  397. }
  398. /* Check FLASH Bank1 operation error flags */
  399. if(__HAL_FLASH_GET_FLAG_BANK1((FLASH_FLAG_EOP_BANK1 | FLASH_FLAG_WRPERR_BANK1 | FLASH_FLAG_PGSERR_BANK1 | \
  400. FLASH_FLAG_STRBER_BANK1R | FLASH_FLAG_INCERR_BANK1 | FLASH_FLAG_OPERR_BANK1)) != RESET)
  401. {
  402. if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE_BANK1)
  403. {
  404. /*return the faulty sector*/
  405. temp = pFlash.Sector;
  406. pFlash.Sector = 0xFFFFFFFF;
  407. }
  408. else if((pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE_BANK1) || (pFlash.ProcedureOnGoing == FLASH_PROC_ALLBANK_MASSERASE))
  409. {
  410. /*return the faulty bank*/
  411. temp = FLASH_BANK_1;
  412. }
  413. else
  414. {
  415. /*return the faulty address*/
  416. temp = pFlash.Address;
  417. }
  418. /*Save the Error code*/
  419. FLASH_SetErrorCode(FLASH_BANK_1);
  420. /* FLASH error interrupt user callback */
  421. HAL_FLASH_OperationErrorCallback(temp);
  422. /* Clear FLASH error pending bits */
  423. __HAL_FLASH_CLEAR_FLAG_BANK1((FLASH_FLAG_EOP_BANK1 | FLASH_FLAG_WRPERR_BANK1 | FLASH_FLAG_PGSERR_BANK1 | \
  424. FLASH_FLAG_STRBER_BANK1R | FLASH_FLAG_INCERR_BANK1 | FLASH_FLAG_OPERR_BANK1));
  425. /*Stop the procedure ongoing*/
  426. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  427. }
  428. /* Check FLASH Bank2 operation error flags */
  429. if(__HAL_FLASH_GET_FLAG_BANK2((FLASH_FLAG_EOP_BANK2 | FLASH_FLAG_WRPERR_BANK2 | FLASH_FLAG_PGSERR_BANK2 | \
  430. FLASH_FLAG_STRBER_BANK2R | FLASH_FLAG_INCERR_BANK2 | FLASH_FLAG_OPERR_BANK2)) != RESET)
  431. {
  432. if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE_BANK2)
  433. {
  434. /*return the faulty sector*/
  435. temp = pFlash.Sector;
  436. pFlash.Sector = 0xFFFFFFFF;
  437. }
  438. else if((pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE_BANK2) || (pFlash.ProcedureOnGoing == FLASH_PROC_ALLBANK_MASSERASE))
  439. {
  440. /*return the faulty bank*/
  441. temp = FLASH_BANK_2;
  442. }
  443. else
  444. {
  445. /*return the faulty address*/
  446. temp = pFlash.Address;
  447. }
  448. /*Save the Error code*/
  449. FLASH_SetErrorCode(FLASH_BANK_2);
  450. /* FLASH error interrupt user callback */
  451. HAL_FLASH_OperationErrorCallback(temp);
  452. /* Clear FLASH error pending bits */
  453. __HAL_FLASH_CLEAR_FLAG_BANK2((FLASH_FLAG_EOP_BANK2 | FLASH_FLAG_WRPERR_BANK2 | FLASH_FLAG_PGSERR_BANK2 | \
  454. FLASH_FLAG_STRBER_BANK2R | FLASH_FLAG_INCERR_BANK2 | FLASH_FLAG_OPERR_BANK2));
  455. /*Stop the procedure ongoing*/
  456. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  457. }
  458. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  459. {
  460. /* Disable Bank1 Operation and Error source interrupt */
  461. __HAL_FLASH_DISABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
  462. FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
  463. /* Disable Bank2 Operation and Error source interrupt */
  464. __HAL_FLASH_DISABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
  465. FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2);
  466. /* Process Unlocked */
  467. __HAL_UNLOCK(&pFlash);
  468. }
  469. }
  470. /**
  471. * @brief FLASH end of operation interrupt callback
  472. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  473. * Mass Erase Bank number which has been requested to erase
  474. * Sectors Erase: Sector which has been erased
  475. * (if 0xFFFFFFFF, it means that all the selected sectors have been erased)
  476. * Program Address which was selected for data program
  477. * @retval None
  478. */
  479. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  480. {
  481. /* NOTE : This function Should not be modified, when the callback is needed,
  482. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  483. */
  484. }
  485. /**
  486. * @brief FLASH operation error interrupt callback
  487. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  488. * Mass Erase: Bank number which has been requested to erase
  489. * Sectors Erase: Sector number which returned an error
  490. * Program: Address which was selected for data program
  491. * @retval None
  492. */
  493. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  494. {
  495. /* NOTE : This function Should not be modified, when the callback is needed,
  496. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  497. */
  498. }
  499. /**
  500. * @}
  501. */
  502. /** @defgroup FLASH_Group2 Peripheral Control functions
  503. * @brief management functions
  504. *
  505. @verbatim
  506. ===============================================================================
  507. ##### Peripheral Control functions #####
  508. ===============================================================================
  509. [..]
  510. This subsection provides a set of functions allowing to control the FLASH
  511. memory operations.
  512. @endverbatim
  513. * @{
  514. */
  515. /**
  516. * @brief Unlock the FLASH control registers access
  517. * @retval HAL Status
  518. */
  519. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  520. {
  521. if((READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != RESET) && (READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != RESET))
  522. {
  523. /* Authorize the FLASH A Registers access */
  524. WRITE_REG(FLASH->KEYR1, FLASH_KEY1);
  525. WRITE_REG(FLASH->KEYR1, FLASH_KEY2);
  526. /* Authorize the FLASH B Registers access */
  527. WRITE_REG(FLASH->KEYR2, FLASH_KEY1);
  528. WRITE_REG(FLASH->KEYR2, FLASH_KEY2);
  529. }
  530. else
  531. {
  532. return HAL_ERROR;
  533. }
  534. return HAL_OK;
  535. }
  536. /**
  537. * @brief Locks the FLASH control registers access
  538. * @retval HAL Status
  539. */
  540. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  541. {
  542. /* Set the LOCK Bit to lock the FLASH A Registers access */
  543. SET_BIT(FLASH->CR1, FLASH_CR_LOCK);
  544. /* Set the LOCK Bit to lock the FLASH B Registers access */
  545. SET_BIT(FLASH->CR2, FLASH_CR_LOCK);
  546. return HAL_OK;
  547. }
  548. /**
  549. * @brief Unlock the FLASH Option Control Registers access.
  550. * @retval HAL Status
  551. */
  552. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  553. {
  554. if(READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) != RESET)
  555. {
  556. /* Authorizes the Option Byte register programming */
  557. WRITE_REG(FLASH->OPTKEYR, FLASH_OPT_KEY1);
  558. WRITE_REG(FLASH->OPTKEYR, FLASH_OPT_KEY2);
  559. }
  560. else
  561. {
  562. return HAL_ERROR;
  563. }
  564. return HAL_OK;
  565. }
  566. /**
  567. * @brief Lock the FLASH Option Control Registers access.
  568. * @retval HAL Status
  569. */
  570. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  571. {
  572. /* Set the OPTLOCK Bit to lock the FLASH A and B Option Byte Registers access */
  573. SET_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK);
  574. return HAL_OK;
  575. }
  576. /**
  577. * @brief Launch the option byte loading.
  578. * @retval HAL Status
  579. */
  580. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  581. {
  582. HAL_StatusTypeDef status = HAL_ERROR;
  583. /* Set OPTSTRT Bit */
  584. SET_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTSTART);
  585. /* Wait for OB change operation to be completed */
  586. status = FLASH_OB_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  587. return status;
  588. }
  589. /**
  590. * @}
  591. */
  592. /** @defgroup FLASH_Group3 Peripheral State and Errors functions
  593. * @brief Peripheral Errors functions
  594. *
  595. @verbatim
  596. ===============================================================================
  597. ##### Peripheral Errors functions #####
  598. ===============================================================================
  599. [..]
  600. This subsection permits to get in run-time Errors of the FLASH peripheral.
  601. @endverbatim
  602. * @{
  603. */
  604. /**
  605. * @brief Get the specific FLASH error flag.
  606. * @retval HAL_FLASH_ERRORCode The returned value can be:
  607. * @arg HAL_FLASH_ERROR_NONE: No error set
  608. *
  609. * @arg HAL_FLASH_ERROR_WRP_BANK1: Write Protection Error on Bank 1
  610. * @arg HAL_FLASH_ERROR_PGS_BANK1 : Program Sequence Error on Bank 1
  611. * @arg HAL_FLASH_ERROR_STRB_BANK1 : Strobe Error on Bank 1
  612. * @arg HAL_FLASH_ERROR_INC_BANK1 : Inconsistency Error on Bank 1
  613. * @arg HAL_FLASH_ERROR_OPE_BANK1 : Operation Error on Bank 1
  614. * @arg HAL_FLASH_ERROR_RDP_BANK1 : Read Protection Error on Bank 1
  615. * @arg HAL_FLASH_ERROR_RDS_BANK1 : Read Secured Error on Bank 1
  616. * @arg HAL_FLASH_ERROR_SNECC_BANK1: SNECC Error on Bank 1
  617. * @arg HAL_FLASH_ERROR_DBECC_BANK1: Double Detection ECC on Bank 1
  618. *
  619. * @arg HAL_FLASH_ERROR_WRP_BANK2 : Write Protection Error on Bank 2
  620. * @arg HAL_FLASH_ERROR_PGS_BANK2 : Program Sequence Error on Bank 2
  621. * @arg HAL_FLASH_ERROR_STRB_BANK2 : Strobe Error on Bank 2
  622. * @arg HAL_FLASH_ERROR_INC_BANK2 : Inconsistency Error on Bank 2
  623. * @arg HAL_FLASH_ERROR_OPE_BANK2 : Operation Error on Bank 2
  624. * @arg HAL_FLASH_ERROR_RDP_BANK2 : Read Protection Error on Bank 2
  625. * @arg HAL_FLASH_ERROR_RDS_BANK2 : Read Secured Error on Bank 2
  626. * @arg HAL_FLASH_ERROR_SNECC_BANK2: SNECC Error on Bank 2
  627. * @arg HAL_FLASH_ERROR_DBECC_BANK2: Double Detection ECC on Bank 2
  628. */
  629. uint32_t HAL_FLASH_GetError(void)
  630. {
  631. return pFlash.ErrorCode;
  632. }
  633. /**
  634. * @}
  635. */
  636. /**
  637. * @}
  638. */
  639. /**
  640. * @brief Wait for a FLASH operation to complete.
  641. * @param Timeout maximum flash operation timeout
  642. * @param Bank flash FLASH_BANK_1 or FLASH_BANK_2
  643. * @retval HAL_StatusTypeDef HAL Status
  644. */
  645. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout, uint32_t Bank)
  646. {
  647. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  648. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  649. flag will be set */
  650. uint32_t bsyflag, errorflag = 0;
  651. uint32_t timeout = HAL_GetTick() + Timeout;
  652. assert_param(IS_FLASH_BANK_EXCLUSIVE(Bank));
  653. if(Bank == FLASH_BANK_1)
  654. {
  655. bsyflag = FLASH_FLAG_BSY_BANK1 | FLASH_FLAG_QW_BANK1;
  656. if((FLASH->OPTCR & FLASH_OPTCR_SWAP_BANK) == 0)
  657. {
  658. bsyflag |= FLASH_FLAG_WBNE_BANK1;
  659. }
  660. else
  661. {
  662. bsyflag |= FLASH_FLAG_WBNE_BANK2;
  663. }
  664. }
  665. else
  666. {
  667. bsyflag = FLASH_FLAG_BSY_BANK2 | FLASH_FLAG_QW_BANK2;
  668. if((FLASH->OPTCR & FLASH_OPTCR_SWAP_BANK) == 0)
  669. {
  670. bsyflag |= FLASH_FLAG_WBNE_BANK2;
  671. }
  672. else
  673. {
  674. bsyflag |= FLASH_FLAG_WBNE_BANK1;
  675. }
  676. }
  677. while(__HAL_FLASH_GET_FLAG(bsyflag))
  678. {
  679. if(Timeout != HAL_MAX_DELAY)
  680. {
  681. if(HAL_GetTick() >= timeout)
  682. {
  683. return HAL_TIMEOUT;
  684. }
  685. }
  686. }
  687. if((Bank == FLASH_BANK_1) && ((FLASH->SR1 & FLASH_FLAG_ALL_ERRORS_BANK1) != RESET))
  688. {
  689. errorflag = FLASH_FLAG_ALL_ERRORS_BANK1;
  690. }
  691. else if((Bank == FLASH_BANK_2) && ((FLASH->SR2 & FLASH_FLAG_ALL_ERRORS_BANK2 & 0x7FFFFFFF) != RESET))
  692. {
  693. errorflag = FLASH_FLAG_ALL_ERRORS_BANK2;
  694. }
  695. if(errorflag != 0)
  696. {
  697. /*Save the error code*/
  698. FLASH_SetErrorCode(Bank);
  699. /* Clear error programming flags */
  700. __HAL_FLASH_CLEAR_FLAG(errorflag);
  701. return HAL_ERROR;
  702. }
  703. /* If there is an error flag set */
  704. return HAL_OK;
  705. }
  706. /**
  707. * @brief Wait for a FLASH Option Bytes change operation to complete.
  708. * @param Timeout maximum flash operation timeout
  709. * @retval HAL_StatusTypeDef HAL Status
  710. */
  711. HAL_StatusTypeDef FLASH_OB_WaitForLastOperation(uint32_t Timeout)
  712. {
  713. uint32_t timeout = HAL_GetTick() + Timeout;
  714. /* Wait for the FLASH Option Bytes change operation to complete by polling on OPT_BUSY flag to be reset.*/
  715. while(FLASH->OPTSR_CUR & FLASH_OPTSR_OPT_BUSY)
  716. {
  717. if(Timeout != HAL_MAX_DELAY)
  718. {
  719. if(HAL_GetTick() >= timeout)
  720. {
  721. return HAL_TIMEOUT;
  722. }
  723. }
  724. }
  725. if(FLASH->OPTSR_CUR & FLASH_OPTSR_OPTCHANGEERR)
  726. {
  727. /*Save the error code*/
  728. pFlash.ErrorCode |= HAL_FLASH_ERROR_OB_CHANGE;
  729. /*Clear the OB error flag*/
  730. FLASH->OPTCCR |= FLASH_OPTCCR_CLR_OPTCHANGEERR;
  731. return HAL_ERROR;
  732. }
  733. /* If there is an error flag set */
  734. return HAL_OK;
  735. }
  736. /**
  737. * @brief Set the specific FLASH error flag.
  738. * @retval None
  739. */
  740. static void FLASH_SetErrorCode(uint32_t Bank)
  741. {
  742. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  743. if(Bank == FLASH_BANK_1)
  744. {
  745. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_WRPERR_BANK1))
  746. {
  747. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP_BANK1;
  748. }
  749. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_PGSERR_BANK1))
  750. {
  751. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS_BANK1;
  752. }
  753. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_STRBER_BANK1R))
  754. {
  755. pFlash.ErrorCode |= HAL_FLASH_ERROR_STRB_BANK1;
  756. }
  757. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_INCERR_BANK1))
  758. {
  759. pFlash.ErrorCode |= HAL_FLASH_ERROR_INC_BANK1;
  760. }
  761. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_OPERR_BANK1))
  762. {
  763. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPE_BANK1;
  764. }
  765. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_RDPERR_BANK1))
  766. {
  767. pFlash.ErrorCode |= HAL_FLASH_ERROR_RDP_BANK1;
  768. }
  769. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_RDSERR_BANK1))
  770. {
  771. pFlash.ErrorCode |= HAL_FLASH_ERROR_RDS_BANK1;
  772. }
  773. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_SNECCE_BANK1RR))
  774. {
  775. pFlash.ErrorCode |= HAL_FLASH_ERROR_SNECC_BANK1;
  776. }
  777. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_DBECCE_BANK1RR))
  778. {
  779. pFlash.ErrorCode |= HAL_FLASH_ERROR_DBECC_BANK1;
  780. }
  781. }
  782. else if(Bank == FLASH_BANK_2)
  783. {
  784. if(__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_WRPERR_BANK2))
  785. {
  786. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP_BANK2;
  787. }
  788. if(__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_PGSERR_BANK2))
  789. {
  790. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS_BANK2;
  791. }
  792. if(__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_STRBER_BANK2R))
  793. {
  794. pFlash.ErrorCode |= HAL_FLASH_ERROR_STRB_BANK2;
  795. }
  796. if(__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_INCERR_BANK2))
  797. {
  798. pFlash.ErrorCode |= HAL_FLASH_ERROR_INC_BANK2;
  799. }
  800. if(__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_OPERR_BANK2))
  801. {
  802. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPE_BANK2;
  803. }
  804. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_RDPERR_BANK2))
  805. {
  806. pFlash.ErrorCode |= HAL_FLASH_ERROR_RDP_BANK2;
  807. }
  808. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_RDSERR_BANK2))
  809. {
  810. pFlash.ErrorCode |= HAL_FLASH_ERROR_RDS_BANK2;
  811. }
  812. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_SNECCE_BANK2RR))
  813. {
  814. pFlash.ErrorCode |= HAL_FLASH_ERROR_SNECC_BANK2;
  815. }
  816. if(__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_DBECCE_BANK2RR))
  817. {
  818. pFlash.ErrorCode |= HAL_FLASH_ERROR_DBECC_BANK2;
  819. }
  820. }
  821. }
  822. #endif /* HAL_FLASH_MODULE_ENABLED */
  823. /**
  824. * @}
  825. */
  826. /**
  827. * @}
  828. */
  829. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/