stm32h7xx_hal_flash.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_flash.c
  4. * @author MCD Application Team
  5. * @brief FLASH HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the internal FLASH memory:
  8. * + Program operations functions
  9. * + Memory Control functions
  10. * + Peripheral Errors functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### FLASH peripheral features #####
  15. ==============================================================================
  16. [..] The Flash memory interface manages CPU AXI I-Code and D-Code accesses
  17. to the Flash memory. It implements the erase and program Flash memory operations
  18. and the read and write protection mechanisms.
  19. [..] The FLASH main features are:
  20. (+) Flash memory read operations
  21. (+) Flash memory program/erase operations
  22. (+) Read / write protections
  23. (+) Option bytes programming
  24. (+) Error code correction (ECC) : Data in flash are 266-bits word
  25. (10 bits added per flash word)
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. This driver provides functions and macros to configure and program the FLASH
  30. memory of all STM32H7xx devices.
  31. (#) FLASH Memory IO Programming functions:
  32. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  33. HAL_FLASH_Lock() functions
  34. (++) Program functions: 256-bit word only
  35. (++) There Two modes of programming :
  36. (+++) Polling mode using HAL_FLASH_Program() function
  37. (+++) Interrupt mode using HAL_FLASH_Program_IT() function
  38. (#) Interrupts and flags management functions :
  39. (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
  40. (++) Callback functions are called when the flash operations are finished :
  41. HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise
  42. HAL_FLASH_OperationErrorCallback()
  43. (++) Get error flag status by calling HAL_FLASH_GetError()
  44. (#) Option bytes management functions :
  45. (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and
  46. HAL_FLASH_OB_Lock() functions
  47. (++) Launch the reload of the option bytes using HAL_FLASH_OB_Launch() function.
  48. In this case, a reset is generated
  49. [..]
  50. In addition to these functions, this driver includes a set of macros allowing
  51. to handle the following operations:
  52. (+) Set the latency
  53. (+) Enable/Disable the FLASH interrupts
  54. (+) Monitor the FLASH flags status
  55. [..]
  56. (@) For any Flash memory program operation (erase or program), the CPU clock frequency
  57. (HCLK) must be at least 1MHz.
  58. (@) The contents of the Flash memory are not guaranteed if a device reset occurs during
  59. a Flash memory operation.
  60. (@) The application can simultaneously request a read and a write operation through each AXI
  61. interface.
  62. As the Flash memory is divided into two independent banks, the embedded Flash
  63. memory interface can drive different operations at the same time on each bank. For
  64. example a read, write or erase operation can be executed on bank 1 while another read,
  65. write or erase operation is executed on bank 2.
  66. @endverbatim
  67. ******************************************************************************
  68. * @attention
  69. *
  70. * Copyright (c) 2017 STMicroelectronics.
  71. * All rights reserved.
  72. *
  73. * This software is licensed under terms that can be found in the LICENSE file in
  74. * the root directory of this software component.
  75. * If no LICENSE file comes with this software, it is provided AS-IS.
  76. ******************************************************************************
  77. */
  78. /* Includes ------------------------------------------------------------------*/
  79. #include "stm32h7xx_hal.h"
  80. /** @addtogroup STM32H7xx_HAL_Driver
  81. * @{
  82. */
  83. /** @defgroup FLASH FLASH
  84. * @brief FLASH HAL module driver
  85. * @{
  86. */
  87. #ifdef HAL_FLASH_MODULE_ENABLED
  88. /* Private typedef -----------------------------------------------------------*/
  89. /* Private define ------------------------------------------------------------*/
  90. /** @addtogroup FLASH_Private_Constants
  91. * @{
  92. */
  93. #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
  94. /**
  95. * @}
  96. */
  97. /* Private macro -------------------------------------------------------------*/
  98. /* Private variables ---------------------------------------------------------*/
  99. /** @addtogroup FLASH_Private_Variables
  100. * @{
  101. */
  102. FLASH_ProcessTypeDef pFlash;
  103. /**
  104. * @}
  105. */
  106. /* Private function prototypes -----------------------------------------------*/
  107. /* Exported functions ---------------------------------------------------------*/
  108. /** @defgroup FLASH_Exported_Functions FLASH Exported functions
  109. * @{
  110. */
  111. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  112. * @brief Programming operation functions
  113. *
  114. @verbatim
  115. ===============================================================================
  116. ##### Programming operation functions #####
  117. ===============================================================================
  118. [..]
  119. This subsection provides a set of functions allowing to manage the FLASH
  120. program operations.
  121. @endverbatim
  122. * @{
  123. */
  124. /**
  125. * @brief Program a flash word at a specified address
  126. * @param TypeProgram Indicate the way to program at a specified address.
  127. * This parameter can be a value of @ref FLASH_Type_Program
  128. * @param FlashAddress specifies the address to be programmed.
  129. * This parameter shall be aligned to the Flash word:
  130. * - 256 bits for STM32H74x/5X devices (8x 32bits words)
  131. * - 128 bits for STM32H7Ax/BX devices (4x 32bits words)
  132. * - 256 bits for STM32H72x/3X devices (8x 32bits words)
  133. * @param DataAddress specifies the address of data to be programmed.
  134. * This parameter shall be 32-bit aligned
  135. *
  136. * @retval HAL_StatusTypeDef HAL Status
  137. */
  138. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)
  139. {
  140. HAL_StatusTypeDef status;
  141. __IO uint32_t *dest_addr = (__IO uint32_t *)FlashAddress;
  142. __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
  143. uint32_t bank;
  144. uint8_t row_index = FLASH_NB_32BITWORD_IN_FLASHWORD;
  145. /* Check the parameters */
  146. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  147. assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress));
  148. /* Process Locked */
  149. __HAL_LOCK(&pFlash);
  150. #if defined (FLASH_OPTCR_PG_OTP)
  151. if((IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress)) || (IS_FLASH_PROGRAM_ADDRESS_OTP(FlashAddress)))
  152. #else
  153. if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
  154. #endif /* FLASH_OPTCR_PG_OTP */
  155. {
  156. bank = FLASH_BANK_1;
  157. /* Prevent unused argument(s) compilation warning */
  158. UNUSED(TypeProgram);
  159. }
  160. #if defined (DUAL_BANK)
  161. else if(IS_FLASH_PROGRAM_ADDRESS_BANK2(FlashAddress))
  162. {
  163. bank = FLASH_BANK_2;
  164. }
  165. #endif /* DUAL_BANK */
  166. else
  167. {
  168. return HAL_ERROR;
  169. }
  170. /* Reset error code */
  171. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  172. /* Wait for last operation to be completed */
  173. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
  174. if(status == HAL_OK)
  175. {
  176. #if defined (DUAL_BANK)
  177. if(bank == FLASH_BANK_1)
  178. {
  179. #if defined (FLASH_OPTCR_PG_OTP)
  180. if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
  181. {
  182. /* Set OTP_PG bit */
  183. SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
  184. }
  185. else
  186. #endif /* FLASH_OPTCR_PG_OTP */
  187. {
  188. /* Set PG bit */
  189. SET_BIT(FLASH->CR1, FLASH_CR_PG);
  190. }
  191. }
  192. else
  193. {
  194. /* Set PG bit */
  195. SET_BIT(FLASH->CR2, FLASH_CR_PG);
  196. }
  197. #else /* Single Bank */
  198. #if defined (FLASH_OPTCR_PG_OTP)
  199. if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
  200. {
  201. /* Set OTP_PG bit */
  202. SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
  203. }
  204. else
  205. #endif /* FLASH_OPTCR_PG_OTP */
  206. {
  207. /* Set PG bit */
  208. SET_BIT(FLASH->CR1, FLASH_CR_PG);
  209. }
  210. #endif /* DUAL_BANK */
  211. __ISB();
  212. __DSB();
  213. #if defined (FLASH_OPTCR_PG_OTP)
  214. if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
  215. {
  216. /* Program an OTP word (16 bits) */
  217. *(__IO uint16_t *)FlashAddress = *(__IO uint16_t*)DataAddress;
  218. }
  219. else
  220. #endif /* FLASH_OPTCR_PG_OTP */
  221. {
  222. /* Program the flash word */
  223. do
  224. {
  225. *dest_addr = *src_addr;
  226. dest_addr++;
  227. src_addr++;
  228. row_index--;
  229. } while (row_index != 0U);
  230. }
  231. __ISB();
  232. __DSB();
  233. /* Wait for last operation to be completed */
  234. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
  235. #if defined (DUAL_BANK)
  236. #if defined (FLASH_OPTCR_PG_OTP)
  237. if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
  238. {
  239. /* If the program operation is completed, disable the OTP_PG */
  240. CLEAR_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
  241. }
  242. else
  243. #endif /* FLASH_OPTCR_PG_OTP */
  244. {
  245. if(bank == FLASH_BANK_1)
  246. {
  247. /* If the program operation is completed, disable the PG */
  248. CLEAR_BIT(FLASH->CR1, FLASH_CR_PG);
  249. }
  250. else
  251. {
  252. /* If the program operation is completed, disable the PG */
  253. CLEAR_BIT(FLASH->CR2, FLASH_CR_PG);
  254. }
  255. }
  256. #else /* Single Bank */
  257. #if defined (FLASH_OPTCR_PG_OTP)
  258. if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
  259. {
  260. /* If the program operation is completed, disable the OTP_PG */
  261. CLEAR_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
  262. }
  263. else
  264. #endif /* FLASH_OPTCR_PG_OTP */
  265. {
  266. /* If the program operation is completed, disable the PG */
  267. CLEAR_BIT(FLASH->CR1, FLASH_CR_PG);
  268. }
  269. #endif /* DUAL_BANK */
  270. }
  271. /* Process Unlocked */
  272. __HAL_UNLOCK(&pFlash);
  273. return status;
  274. }
  275. /**
  276. * @brief Program a flash word at a specified address with interrupt enabled.
  277. * @param TypeProgram Indicate the way to program at a specified address.
  278. * This parameter can be a value of @ref FLASH_Type_Program
  279. * @param FlashAddress specifies the address to be programmed.
  280. * This parameter shall be aligned to the Flash word:
  281. * - 256 bits for STM32H74x/5X devices (8x 32bits words)
  282. * - 128 bits for STM32H7Ax/BX devices (4x 32bits words)
  283. * - 256 bits for STM32H72x/3X devices (8x 32bits words)
  284. * @param DataAddress specifies the address of data to be programmed.
  285. * This parameter shall be 32-bit aligned
  286. *
  287. * @retval HAL Status
  288. */
  289. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)
  290. {
  291. HAL_StatusTypeDef status;
  292. __IO uint32_t *dest_addr = (__IO uint32_t*)FlashAddress;
  293. __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
  294. uint32_t bank;
  295. uint8_t row_index = FLASH_NB_32BITWORD_IN_FLASHWORD;
  296. /* Check the parameters */
  297. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  298. assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress));
  299. /* Process Locked */
  300. __HAL_LOCK(&pFlash);
  301. /* Reset error code */
  302. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  303. #if defined (FLASH_OPTCR_PG_OTP)
  304. if((IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress)) || (IS_FLASH_PROGRAM_ADDRESS_OTP(FlashAddress)))
  305. #else
  306. if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
  307. #endif /* FLASH_OPTCR_PG_OTP */
  308. {
  309. bank = FLASH_BANK_1;
  310. /* Prevent unused argument(s) compilation warning */
  311. UNUSED(TypeProgram);
  312. }
  313. #if defined (DUAL_BANK)
  314. else if(IS_FLASH_PROGRAM_ADDRESS_BANK2(FlashAddress))
  315. {
  316. bank = FLASH_BANK_2;
  317. }
  318. #endif /* DUAL_BANK */
  319. else
  320. {
  321. return HAL_ERROR;
  322. }
  323. /* Wait for last operation to be completed */
  324. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
  325. if (status != HAL_OK)
  326. {
  327. /* Process Unlocked */
  328. __HAL_UNLOCK(&pFlash);
  329. }
  330. else
  331. {
  332. pFlash.Address = FlashAddress;
  333. #if defined (DUAL_BANK)
  334. if(bank == FLASH_BANK_1)
  335. {
  336. /* Set internal variables used by the IRQ handler */
  337. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK1;
  338. #if defined (FLASH_OPTCR_PG_OTP)
  339. if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
  340. {
  341. /* Set OTP_PG bit */
  342. SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
  343. }
  344. else
  345. #endif /* FLASH_OPTCR_PG_OTP */
  346. {
  347. /* Set PG bit */
  348. SET_BIT(FLASH->CR1, FLASH_CR_PG);
  349. }
  350. /* Enable End of Operation and Error interrupts for Bank 1 */
  351. #if defined (FLASH_CR_OPERRIE)
  352. __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
  353. FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
  354. #else
  355. __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
  356. FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
  357. #endif /* FLASH_CR_OPERRIE */
  358. }
  359. else
  360. {
  361. /* Set internal variables used by the IRQ handler */
  362. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK2;
  363. /* Set PG bit */
  364. SET_BIT(FLASH->CR2, FLASH_CR_PG);
  365. /* Enable End of Operation and Error interrupts for Bank2 */
  366. #if defined (FLASH_CR_OPERRIE)
  367. __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
  368. FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2);
  369. #else
  370. __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
  371. FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2);
  372. #endif /* FLASH_CR_OPERRIE */
  373. }
  374. #else /* Single Bank */
  375. /* Set internal variables used by the IRQ handler */
  376. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK1;
  377. #if defined (FLASH_OPTCR_PG_OTP)
  378. if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
  379. {
  380. /* Set OTP_PG bit */
  381. SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
  382. }
  383. else
  384. #endif /* FLASH_OPTCR_PG_OTP */
  385. {
  386. /* Set PG bit */
  387. SET_BIT(FLASH->CR1, FLASH_CR_PG);
  388. }
  389. /* Enable End of Operation and Error interrupts for Bank 1 */
  390. #if defined (FLASH_CR_OPERRIE)
  391. __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
  392. FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
  393. #else
  394. __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
  395. FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
  396. #endif /* FLASH_CR_OPERRIE */
  397. #endif /* DUAL_BANK */
  398. __ISB();
  399. __DSB();
  400. #if defined (FLASH_OPTCR_PG_OTP)
  401. if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
  402. {
  403. /* Program an OTP word (16 bits) */
  404. *(__IO uint16_t *)FlashAddress = *(__IO uint16_t*)DataAddress;
  405. }
  406. else
  407. #endif /* FLASH_OPTCR_PG_OTP */
  408. {
  409. /* Program the flash word */
  410. do
  411. {
  412. *dest_addr = *src_addr;
  413. dest_addr++;
  414. src_addr++;
  415. row_index--;
  416. } while (row_index != 0U);
  417. }
  418. __ISB();
  419. __DSB();
  420. }
  421. return status;
  422. }
  423. /**
  424. * @brief This function handles FLASH interrupt request.
  425. * @retval None
  426. */
  427. void HAL_FLASH_IRQHandler(void)
  428. {
  429. uint32_t temp;
  430. uint32_t errorflag;
  431. FLASH_ProcedureTypeDef procedure;
  432. /* Check FLASH Bank1 End of Operation flag */
  433. if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_SR_EOP) != RESET)
  434. {
  435. if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE_BANK1)
  436. {
  437. /* Nb of sector to erased can be decreased */
  438. pFlash.NbSectorsToErase--;
  439. /* Check if there are still sectors to erase */
  440. if(pFlash.NbSectorsToErase != 0U)
  441. {
  442. /* Indicate user which sector has been erased */
  443. HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
  444. /* Clear bank 1 End of Operation pending bit */
  445. __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
  446. /* Increment sector number */
  447. pFlash.Sector++;
  448. temp = pFlash.Sector;
  449. FLASH_Erase_Sector(temp, FLASH_BANK_1, pFlash.VoltageForErase);
  450. }
  451. else
  452. {
  453. /* No more sectors to Erase, user callback can be called */
  454. /* Reset Sector and stop Erase sectors procedure */
  455. pFlash.Sector = 0xFFFFFFFFU;
  456. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  457. /* FLASH EOP interrupt user callback */
  458. HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
  459. /* Clear FLASH End of Operation pending bit */
  460. __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
  461. }
  462. }
  463. else
  464. {
  465. procedure = pFlash.ProcedureOnGoing;
  466. if((procedure == FLASH_PROC_MASSERASE_BANK1) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
  467. {
  468. /* MassErase ended. Return the selected bank */
  469. /* FLASH EOP interrupt user callback */
  470. HAL_FLASH_EndOfOperationCallback(FLASH_BANK_1);
  471. }
  472. else if(procedure == FLASH_PROC_PROGRAM_BANK1)
  473. {
  474. /* Program ended. Return the selected address */
  475. /* FLASH EOP interrupt user callback */
  476. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  477. }
  478. else
  479. {
  480. /* Nothing to do */
  481. }
  482. if((procedure != FLASH_PROC_SECTERASE_BANK2) && \
  483. (procedure != FLASH_PROC_MASSERASE_BANK2) && \
  484. (procedure != FLASH_PROC_PROGRAM_BANK2))
  485. {
  486. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  487. /* Clear FLASH End of Operation pending bit */
  488. __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
  489. }
  490. }
  491. }
  492. #if defined (DUAL_BANK)
  493. /* Check FLASH Bank2 End of Operation flag */
  494. if(__HAL_FLASH_GET_FLAG_BANK2(FLASH_SR_EOP) != RESET)
  495. {
  496. if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE_BANK2)
  497. {
  498. /*Nb of sector to erased can be decreased*/
  499. pFlash.NbSectorsToErase--;
  500. /* Check if there are still sectors to erase*/
  501. if(pFlash.NbSectorsToErase != 0U)
  502. {
  503. /*Indicate user which sector has been erased*/
  504. HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
  505. /* Clear bank 2 End of Operation pending bit */
  506. __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
  507. /*Increment sector number*/
  508. pFlash.Sector++;
  509. temp = pFlash.Sector;
  510. FLASH_Erase_Sector(temp, FLASH_BANK_2, pFlash.VoltageForErase);
  511. }
  512. else
  513. {
  514. /* No more sectors to Erase, user callback can be called */
  515. /* Reset Sector and stop Erase sectors procedure */
  516. pFlash.Sector = 0xFFFFFFFFU;
  517. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  518. /* FLASH EOP interrupt user callback */
  519. HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
  520. /* Clear FLASH End of Operation pending bit */
  521. __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
  522. }
  523. }
  524. else
  525. {
  526. procedure = pFlash.ProcedureOnGoing;
  527. if((procedure == FLASH_PROC_MASSERASE_BANK2) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
  528. {
  529. /*MassErase ended. Return the selected bank*/
  530. /* FLASH EOP interrupt user callback */
  531. HAL_FLASH_EndOfOperationCallback(FLASH_BANK_2);
  532. }
  533. else if(procedure == FLASH_PROC_PROGRAM_BANK2)
  534. {
  535. /* Program ended. Return the selected address */
  536. /* FLASH EOP interrupt user callback */
  537. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  538. }
  539. else
  540. {
  541. /* Nothing to do */
  542. }
  543. if((procedure != FLASH_PROC_SECTERASE_BANK1) && \
  544. (procedure != FLASH_PROC_MASSERASE_BANK1) && \
  545. (procedure != FLASH_PROC_PROGRAM_BANK1))
  546. {
  547. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  548. /* Clear FLASH End of Operation pending bit */
  549. __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
  550. }
  551. }
  552. }
  553. #endif /* DUAL_BANK */
  554. /* Check FLASH Bank1 operation error flags */
  555. #if defined (FLASH_SR_OPERR)
  556. errorflag = FLASH->SR1 & (FLASH_FLAG_WRPERR_BANK1 | FLASH_FLAG_PGSERR_BANK1 | FLASH_FLAG_STRBERR_BANK1 | \
  557. FLASH_FLAG_INCERR_BANK1 | FLASH_FLAG_OPERR_BANK1);
  558. #else
  559. errorflag = FLASH->SR1 & (FLASH_FLAG_WRPERR_BANK1 | FLASH_FLAG_PGSERR_BANK1 | FLASH_FLAG_STRBERR_BANK1 | \
  560. FLASH_FLAG_INCERR_BANK1);
  561. #endif /* FLASH_SR_OPERR */
  562. if(errorflag != 0U)
  563. {
  564. /* Save the error code */
  565. pFlash.ErrorCode |= errorflag;
  566. /* Clear error programming flags */
  567. __HAL_FLASH_CLEAR_FLAG_BANK1(errorflag);
  568. procedure = pFlash.ProcedureOnGoing;
  569. if(procedure == FLASH_PROC_SECTERASE_BANK1)
  570. {
  571. /* Return the faulty sector */
  572. temp = pFlash.Sector;
  573. pFlash.Sector = 0xFFFFFFFFU;
  574. }
  575. else if((procedure == FLASH_PROC_MASSERASE_BANK1) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
  576. {
  577. /* Return the faulty bank */
  578. temp = FLASH_BANK_1;
  579. }
  580. else
  581. {
  582. /* Return the faulty address */
  583. temp = pFlash.Address;
  584. }
  585. /* Stop the procedure ongoing*/
  586. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  587. /* FLASH error interrupt user callback */
  588. HAL_FLASH_OperationErrorCallback(temp);
  589. }
  590. #if (USE_FLASH_ECC == 1U)
  591. /* Check FLASH Bank1 ECC single correction error flag */
  592. errorflag = FLASH->SR1 & FLASH_FLAG_SNECCERR_BANK1;
  593. if(errorflag != 0U)
  594. {
  595. /* Save the error code */
  596. pFlash.ErrorCode |= errorflag;
  597. /* Call User callback */
  598. HAL_FLASHEx_EccCorrectionCallback();
  599. /* Clear FLASH Bank1 ECC single correction error flag in order to allow new ECC error record */
  600. __HAL_FLASH_CLEAR_FLAG_BANK1(errorflag);
  601. }
  602. /* Check FLASH Bank1 ECC double detection error flag */
  603. errorflag = FLASH->SR1 & FLASH_FLAG_DBECCERR_BANK1;
  604. if(errorflag != 0U)
  605. {
  606. /* Save the error code */
  607. pFlash.ErrorCode |= errorflag;
  608. /* Call User callback */
  609. HAL_FLASHEx_EccDetectionCallback();
  610. /* Clear FLASH Bank1 ECC double detection error flag in order to allow new ECC error record */
  611. __HAL_FLASH_CLEAR_FLAG_BANK1(errorflag);
  612. }
  613. #endif /* USE_FLASH_ECC */
  614. #if defined (DUAL_BANK)
  615. /* Check FLASH Bank2 operation error flags */
  616. #if defined (FLASH_SR_OPERR)
  617. errorflag = FLASH->SR2 & ((FLASH_FLAG_WRPERR_BANK2 | FLASH_FLAG_PGSERR_BANK2 | FLASH_FLAG_STRBERR_BANK2 | \
  618. FLASH_FLAG_INCERR_BANK2 | FLASH_FLAG_OPERR_BANK2) & 0x7FFFFFFFU);
  619. #else
  620. errorflag = FLASH->SR2 & ((FLASH_FLAG_WRPERR_BANK2 | FLASH_FLAG_PGSERR_BANK2 | FLASH_FLAG_STRBERR_BANK2 | \
  621. FLASH_FLAG_INCERR_BANK2) & 0x7FFFFFFFU);
  622. #endif /* FLASH_SR_OPERR */
  623. if(errorflag != 0U)
  624. {
  625. /* Save the error code */
  626. pFlash.ErrorCode |= (errorflag | 0x80000000U);
  627. /* Clear error programming flags */
  628. __HAL_FLASH_CLEAR_FLAG_BANK2(errorflag);
  629. procedure = pFlash.ProcedureOnGoing;
  630. if(procedure== FLASH_PROC_SECTERASE_BANK2)
  631. {
  632. /*return the faulty sector*/
  633. temp = pFlash.Sector;
  634. pFlash.Sector = 0xFFFFFFFFU;
  635. }
  636. else if((procedure == FLASH_PROC_MASSERASE_BANK2) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
  637. {
  638. /*return the faulty bank*/
  639. temp = FLASH_BANK_2;
  640. }
  641. else
  642. {
  643. /*return the faulty address*/
  644. temp = pFlash.Address;
  645. }
  646. /*Stop the procedure ongoing*/
  647. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  648. /* FLASH error interrupt user callback */
  649. HAL_FLASH_OperationErrorCallback(temp);
  650. }
  651. #if (USE_FLASH_ECC == 1U)
  652. /* Check FLASH Bank2 ECC single correction error flag */
  653. errorflag = FLASH->SR2 & FLASH_FLAG_SNECCERR_BANK2;
  654. if(errorflag != 0U)
  655. {
  656. /* Save the error code */
  657. pFlash.ErrorCode |= (errorflag | 0x80000000U);
  658. /* Call User callback */
  659. HAL_FLASHEx_EccCorrectionCallback();
  660. /* Clear FLASH Bank2 ECC single correction error flag in order to allow new ECC error record */
  661. __HAL_FLASH_CLEAR_FLAG_BANK2(errorflag);
  662. }
  663. /* Check FLASH Bank2 ECC double detection error flag */
  664. errorflag = FLASH->SR2 & FLASH_FLAG_DBECCERR_BANK2;
  665. if(errorflag != 0U)
  666. {
  667. /* Save the error code */
  668. pFlash.ErrorCode |= (errorflag | 0x80000000U);
  669. /* Call User callback */
  670. HAL_FLASHEx_EccDetectionCallback();
  671. /* Clear FLASH Bank2 ECC double detection error flag in order to allow new ECC error record */
  672. __HAL_FLASH_CLEAR_FLAG_BANK2(errorflag);
  673. }
  674. #endif /* USE_FLASH_ECC */
  675. #endif /* DUAL_BANK */
  676. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  677. {
  678. #if defined (FLASH_CR_OPERRIE)
  679. /* Disable Bank1 Operation and Error source interrupt */
  680. __HAL_FLASH_DISABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
  681. FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
  682. #if defined (DUAL_BANK)
  683. /* Disable Bank2 Operation and Error source interrupt */
  684. __HAL_FLASH_DISABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
  685. FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2);
  686. #endif /* DUAL_BANK */
  687. #else
  688. /* Disable Bank1 Operation and Error source interrupt */
  689. __HAL_FLASH_DISABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
  690. FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
  691. #if defined (DUAL_BANK)
  692. /* Disable Bank2 Operation and Error source interrupt */
  693. __HAL_FLASH_DISABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
  694. FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2);
  695. #endif /* DUAL_BANK */
  696. #endif /* FLASH_CR_OPERRIE */
  697. /* Process Unlocked */
  698. __HAL_UNLOCK(&pFlash);
  699. }
  700. }
  701. /**
  702. * @brief FLASH end of operation interrupt callback
  703. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  704. * Mass Erase: Bank number which has been requested to erase
  705. * Sectors Erase: Sector which has been erased
  706. * (if 0xFFFFFFFF, it means that all the selected sectors have been erased)
  707. * Program: Address which was selected for data program
  708. * @retval None
  709. */
  710. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  711. {
  712. /* Prevent unused argument(s) compilation warning */
  713. UNUSED(ReturnValue);
  714. /* NOTE : This function Should not be modified, when the callback is needed,
  715. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  716. */
  717. }
  718. /**
  719. * @brief FLASH operation error interrupt callback
  720. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  721. * Mass Erase: Bank number which has been requested to erase
  722. * Sectors Erase: Sector number which returned an error
  723. * Program: Address which was selected for data program
  724. * @retval None
  725. */
  726. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  727. {
  728. /* Prevent unused argument(s) compilation warning */
  729. UNUSED(ReturnValue);
  730. /* NOTE : This function Should not be modified, when the callback is needed,
  731. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  732. */
  733. }
  734. /**
  735. * @}
  736. */
  737. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  738. * @brief Management functions
  739. *
  740. @verbatim
  741. ===============================================================================
  742. ##### Peripheral Control functions #####
  743. ===============================================================================
  744. [..]
  745. This subsection provides a set of functions allowing to control the FLASH
  746. memory operations.
  747. @endverbatim
  748. * @{
  749. */
  750. /**
  751. * @brief Unlock the FLASH control registers access
  752. * @retval HAL Status
  753. */
  754. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  755. {
  756. if(READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != 0U)
  757. {
  758. /* Authorize the FLASH Bank1 Registers access */
  759. WRITE_REG(FLASH->KEYR1, FLASH_KEY1);
  760. WRITE_REG(FLASH->KEYR1, FLASH_KEY2);
  761. /* Verify Flash Bank1 is unlocked */
  762. if (READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != 0U)
  763. {
  764. return HAL_ERROR;
  765. }
  766. }
  767. #if defined (DUAL_BANK)
  768. if(READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != 0U)
  769. {
  770. /* Authorize the FLASH Bank2 Registers access */
  771. WRITE_REG(FLASH->KEYR2, FLASH_KEY1);
  772. WRITE_REG(FLASH->KEYR2, FLASH_KEY2);
  773. /* Verify Flash Bank2 is unlocked */
  774. if (READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != 0U)
  775. {
  776. return HAL_ERROR;
  777. }
  778. }
  779. #endif /* DUAL_BANK */
  780. return HAL_OK;
  781. }
  782. /**
  783. * @brief Locks the FLASH control registers access
  784. * @retval HAL Status
  785. */
  786. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  787. {
  788. /* Set the LOCK Bit to lock the FLASH Bank1 Control Register access */
  789. SET_BIT(FLASH->CR1, FLASH_CR_LOCK);
  790. /* Verify Flash Bank1 is locked */
  791. if (READ_BIT(FLASH->CR1, FLASH_CR_LOCK) == 0U)
  792. {
  793. return HAL_ERROR;
  794. }
  795. #if defined (DUAL_BANK)
  796. /* Set the LOCK Bit to lock the FLASH Bank2 Control Register access */
  797. SET_BIT(FLASH->CR2, FLASH_CR_LOCK);
  798. /* Verify Flash Bank2 is locked */
  799. if (READ_BIT(FLASH->CR2, FLASH_CR_LOCK) == 0U)
  800. {
  801. return HAL_ERROR;
  802. }
  803. #endif /* DUAL_BANK */
  804. return HAL_OK;
  805. }
  806. /**
  807. * @brief Unlock the FLASH Option Control Registers access.
  808. * @retval HAL Status
  809. */
  810. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  811. {
  812. if(READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) != 0U)
  813. {
  814. /* Authorizes the Option Byte registers programming */
  815. WRITE_REG(FLASH->OPTKEYR, FLASH_OPT_KEY1);
  816. WRITE_REG(FLASH->OPTKEYR, FLASH_OPT_KEY2);
  817. /* Verify that the Option Bytes are unlocked */
  818. if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) != 0U)
  819. {
  820. return HAL_ERROR;
  821. }
  822. }
  823. return HAL_OK;
  824. }
  825. /**
  826. * @brief Lock the FLASH Option Control Registers access.
  827. * @retval HAL Status
  828. */
  829. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  830. {
  831. /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
  832. SET_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK);
  833. /* Verify that the Option Bytes are locked */
  834. if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) == 0U)
  835. {
  836. return HAL_ERROR;
  837. }
  838. return HAL_OK;
  839. }
  840. /**
  841. * @brief Launch the option bytes loading.
  842. * @retval HAL Status
  843. */
  844. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  845. {
  846. HAL_StatusTypeDef status;
  847. /* Wait for CRC computation to be completed */
  848. if (FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1) != HAL_OK)
  849. {
  850. status = HAL_ERROR;
  851. }
  852. #if defined (DUAL_BANK)
  853. else if (FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
  854. {
  855. status = HAL_ERROR;
  856. }
  857. #endif /* DUAL_BANK */
  858. else
  859. {
  860. status = HAL_OK;
  861. }
  862. if (status == HAL_OK)
  863. {
  864. /* Set OPTSTRT Bit */
  865. SET_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTSTART);
  866. /* Wait for OB change operation to be completed */
  867. status = FLASH_OB_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  868. }
  869. return status;
  870. }
  871. /**
  872. * @}
  873. */
  874. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
  875. * @brief Peripheral Errors functions
  876. *
  877. @verbatim
  878. ===============================================================================
  879. ##### Peripheral Errors functions #####
  880. ===============================================================================
  881. [..]
  882. This subsection permits to get in run-time Errors of the FLASH peripheral.
  883. @endverbatim
  884. * @{
  885. */
  886. /**
  887. * @brief Get the specific FLASH error flag.
  888. * @retval HAL_FLASH_ERRORCode The returned value can be:
  889. * @arg HAL_FLASH_ERROR_NONE : No error set
  890. *
  891. * @arg HAL_FLASH_ERROR_WRP_BANK1 : Write Protection Error on Bank 1
  892. * @arg HAL_FLASH_ERROR_PGS_BANK1 : Program Sequence Error on Bank 1
  893. * @arg HAL_FLASH_ERROR_STRB_BANK1 : Strobe Error on Bank 1
  894. * @arg HAL_FLASH_ERROR_INC_BANK1 : Inconsistency Error on Bank 1
  895. * @arg HAL_FLASH_ERROR_OPE_BANK1 : Operation Error on Bank 1
  896. * @arg HAL_FLASH_ERROR_RDP_BANK1 : Read Protection Error on Bank 1
  897. * @arg HAL_FLASH_ERROR_RDS_BANK1 : Read Secured Error on Bank 1
  898. * @arg HAL_FLASH_ERROR_SNECC_BANK1: ECC Single Correction Error on Bank 1
  899. * @arg HAL_FLASH_ERROR_DBECC_BANK1: ECC Double Detection Error on Bank 1
  900. * @arg HAL_FLASH_ERROR_CRCRD_BANK1: CRC Read Error on Bank 1
  901. *
  902. * @arg HAL_FLASH_ERROR_WRP_BANK2 : Write Protection Error on Bank 2
  903. * @arg HAL_FLASH_ERROR_PGS_BANK2 : Program Sequence Error on Bank 2
  904. * @arg HAL_FLASH_ERROR_STRB_BANK2 : Strobe Error on Bank 2
  905. * @arg HAL_FLASH_ERROR_INC_BANK2 : Inconsistency Error on Bank 2
  906. * @arg HAL_FLASH_ERROR_OPE_BANK2 : Operation Error on Bank 2
  907. * @arg HAL_FLASH_ERROR_RDP_BANK2 : Read Protection Error on Bank 2
  908. * @arg HAL_FLASH_ERROR_RDS_BANK2 : Read Secured Error on Bank 2
  909. * @arg HAL_FLASH_ERROR_SNECC_BANK2: SNECC Error on Bank 2
  910. * @arg HAL_FLASH_ERROR_DBECC_BANK2: Double Detection ECC on Bank 2
  911. * @arg HAL_FLASH_ERROR_CRCRD_BANK2: CRC Read Error on Bank 2
  912. */
  913. uint32_t HAL_FLASH_GetError(void)
  914. {
  915. return pFlash.ErrorCode;
  916. }
  917. /**
  918. * @}
  919. */
  920. /**
  921. * @}
  922. */
  923. /* Private functions ---------------------------------------------------------*/
  924. /** @addtogroup FLASH_Private_Functions
  925. * @{
  926. */
  927. /**
  928. * @brief Wait for a FLASH operation to complete.
  929. * @param Timeout maximum flash operation timeout
  930. * @param Bank flash FLASH_BANK_1 or FLASH_BANK_2
  931. * @retval HAL_StatusTypeDef HAL Status
  932. */
  933. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout, uint32_t Bank)
  934. {
  935. /* Wait for the FLASH operation to complete by polling on QW flag to be reset.
  936. Even if the FLASH operation fails, the QW flag will be reset and an error
  937. flag will be set */
  938. uint32_t bsyflag = FLASH_FLAG_QW_BANK1;
  939. uint32_t errorflag = 0;
  940. uint32_t tickstart = HAL_GetTick();
  941. assert_param(IS_FLASH_BANK_EXCLUSIVE(Bank));
  942. #if defined (DUAL_BANK)
  943. if (Bank == FLASH_BANK_2)
  944. {
  945. /* Select bsyflag depending on Bank */
  946. bsyflag = FLASH_FLAG_QW_BANK2;
  947. }
  948. #endif /* DUAL_BANK */
  949. while(__HAL_FLASH_GET_FLAG(bsyflag))
  950. {
  951. if(Timeout != HAL_MAX_DELAY)
  952. {
  953. if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  954. {
  955. return HAL_TIMEOUT;
  956. }
  957. }
  958. }
  959. /* Get Error Flags */
  960. if (Bank == FLASH_BANK_1)
  961. {
  962. errorflag = FLASH->SR1 & FLASH_FLAG_ALL_ERRORS_BANK1;
  963. }
  964. #if defined (DUAL_BANK)
  965. else
  966. {
  967. errorflag = (FLASH->SR2 & FLASH_FLAG_ALL_ERRORS_BANK2) | 0x80000000U;
  968. }
  969. #endif /* DUAL_BANK */
  970. /* In case of error reported in Flash SR1 or SR2 register */
  971. if((errorflag & 0x7FFFFFFFU) != 0U)
  972. {
  973. /*Save the error code*/
  974. pFlash.ErrorCode |= errorflag;
  975. /* Clear error programming flags */
  976. __HAL_FLASH_CLEAR_FLAG(errorflag);
  977. return HAL_ERROR;
  978. }
  979. /* Check FLASH End of Operation flag */
  980. if(Bank == FLASH_BANK_1)
  981. {
  982. if (__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_EOP_BANK1))
  983. {
  984. /* Clear FLASH End of Operation pending bit */
  985. __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
  986. }
  987. }
  988. #if defined (DUAL_BANK)
  989. else
  990. {
  991. if (__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_EOP_BANK2))
  992. {
  993. /* Clear FLASH End of Operation pending bit */
  994. __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
  995. }
  996. }
  997. #endif /* DUAL_BANK */
  998. return HAL_OK;
  999. }
  1000. /**
  1001. * @brief Wait for a FLASH Option Bytes change operation to complete.
  1002. * @param Timeout maximum flash operation timeout
  1003. * @retval HAL_StatusTypeDef HAL Status
  1004. */
  1005. HAL_StatusTypeDef FLASH_OB_WaitForLastOperation(uint32_t Timeout)
  1006. {
  1007. /* Get timeout */
  1008. uint32_t tickstart = HAL_GetTick();
  1009. /* Wait for the FLASH Option Bytes change operation to complete by polling on OPT_BUSY flag to be reset */
  1010. while(READ_BIT(FLASH->OPTSR_CUR, FLASH_OPTSR_OPT_BUSY) != 0U)
  1011. {
  1012. if(Timeout != HAL_MAX_DELAY)
  1013. {
  1014. if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1015. {
  1016. return HAL_TIMEOUT;
  1017. }
  1018. }
  1019. }
  1020. /* Check option byte change error */
  1021. if(READ_BIT(FLASH->OPTSR_CUR, FLASH_OPTSR_OPTCHANGEERR) != 0U)
  1022. {
  1023. /* Save the error code */
  1024. pFlash.ErrorCode |= HAL_FLASH_ERROR_OB_CHANGE;
  1025. /* Clear the OB error flag */
  1026. FLASH->OPTCCR |= FLASH_OPTCCR_CLR_OPTCHANGEERR;
  1027. return HAL_ERROR;
  1028. }
  1029. /* If there is no error flag set */
  1030. return HAL_OK;
  1031. }
  1032. /**
  1033. * @brief Wait for a FLASH CRC computation to complete.
  1034. * @param Timeout maximum flash operation timeout
  1035. * @param Bank flash FLASH_BANK_1 or FLASH_BANK_2
  1036. * @retval HAL_StatusTypeDef HAL Status
  1037. */
  1038. HAL_StatusTypeDef FLASH_CRC_WaitForLastOperation(uint32_t Timeout, uint32_t Bank)
  1039. {
  1040. uint32_t bsyflag;
  1041. uint32_t tickstart = HAL_GetTick();
  1042. assert_param(IS_FLASH_BANK_EXCLUSIVE(Bank));
  1043. /* Select bsyflag depending on Bank */
  1044. if(Bank == FLASH_BANK_1)
  1045. {
  1046. bsyflag = FLASH_FLAG_CRC_BUSY_BANK1;
  1047. }
  1048. else
  1049. {
  1050. bsyflag = FLASH_FLAG_CRC_BUSY_BANK2;
  1051. }
  1052. /* Wait for the FLASH CRC computation to complete by polling on CRC_BUSY flag to be reset */
  1053. while(__HAL_FLASH_GET_FLAG(bsyflag))
  1054. {
  1055. if(Timeout != HAL_MAX_DELAY)
  1056. {
  1057. if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1058. {
  1059. return HAL_TIMEOUT;
  1060. }
  1061. }
  1062. }
  1063. /* Check FLASH CRC read error flag */
  1064. if(Bank == FLASH_BANK_1)
  1065. {
  1066. if (__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_CRCRDERR_BANK1))
  1067. {
  1068. /* Save the error code */
  1069. pFlash.ErrorCode |= HAL_FLASH_ERROR_CRCRD_BANK1;
  1070. /* Clear FLASH CRC read error pending bit */
  1071. __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_CRCRDERR_BANK1);
  1072. return HAL_ERROR;
  1073. }
  1074. }
  1075. #if defined (DUAL_BANK)
  1076. else
  1077. {
  1078. if (__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_CRCRDERR_BANK2))
  1079. {
  1080. /* Save the error code */
  1081. pFlash.ErrorCode |= HAL_FLASH_ERROR_CRCRD_BANK2;
  1082. /* Clear FLASH CRC read error pending bit */
  1083. __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_CRCRDERR_BANK2);
  1084. return HAL_ERROR;
  1085. }
  1086. }
  1087. #endif /* DUAL_BANK */
  1088. /* If there is no error flag set */
  1089. return HAL_OK;
  1090. }
  1091. /**
  1092. * @}
  1093. */
  1094. #endif /* HAL_FLASH_MODULE_ENABLED */
  1095. /**
  1096. * @}
  1097. */
  1098. /**
  1099. * @}
  1100. */