stm32h7xx_hal_flash.c 37 KB

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