stm32h5xx_hal_flash.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /**
  2. ******************************************************************************
  3. * @file stm32h5xx_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. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2023 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ==============================================================================
  25. ##### FLASH peripheral features #####
  26. ==============================================================================
  27. [..] The Flash memory interface manages CPU AHB C-Bus accesses to the Flash memory.
  28. It implements the erase and program Flash memory operations and the read
  29. and write protection mechanisms.
  30. [..] The FLASH main features are:
  31. (+) Flash memory read operations
  32. (+) Flash memory program/erase operations
  33. (+) Read / write protections
  34. (+) Option bytes programming
  35. (+) TrustZone aware
  36. (+) Watermark-based area protection
  37. (+) Block-based sector protection
  38. (+) Error code correction (ECC)
  39. ##### How to use this driver #####
  40. ==============================================================================
  41. [..]
  42. This driver provides functions and macros to configure and program the FLASH
  43. memory of all STM32H5xx devices.
  44. (#) FLASH Memory IO Programming functions:
  45. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  46. HAL_FLASH_Lock() functions
  47. (++) Flash memory programming by 128 bits (user area, OBKeys) and 16 bits (OTP and Flash high-cycle
  48. data area)
  49. (++) There Two modes of programming :
  50. (+++) Polling mode using HAL_FLASH_Program() function
  51. (+++) Interrupt mode using HAL_FLASH_Program_IT() function
  52. (#) Interrupts and flags management functions :
  53. (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
  54. (++) Callback functions are called when the flash operations are finished :
  55. HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise
  56. HAL_FLASH_OperationErrorCallback()
  57. (++) Get error flag status by calling HAL_FLASH_GetError()
  58. (#) Option bytes management functions :
  59. (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and
  60. HAL_FLASH_OB_Lock() functions
  61. (++) Launch the reload of the option bytes using HAL_FLASH_OB_Launch() function.
  62. In this case, a reset is generated
  63. [..]
  64. In addition to these functions, this driver includes a set of macros allowing
  65. to handle the following operations:
  66. (+) Set the latency
  67. (+) Enable/Disable the FLASH interrupts
  68. (+) Monitor the FLASH flags status
  69. [..]
  70. (@) The contents of the Flash memory are not guaranteed if a device reset occurs during
  71. a Flash memory operation.
  72. @endverbatim
  73. */
  74. /* Includes ------------------------------------------------------------------*/
  75. #include "stm32h5xx_hal.h"
  76. /** @addtogroup STM32H5xx_HAL_Driver
  77. * @{
  78. */
  79. /** @defgroup FLASH FLASH
  80. * @brief FLASH HAL module driver
  81. * @{
  82. */
  83. #ifdef HAL_FLASH_MODULE_ENABLED
  84. /* Private typedef -----------------------------------------------------------*/
  85. /* Private define ------------------------------------------------------------*/
  86. /* Private macro -------------------------------------------------------------*/
  87. /* Private variables ---------------------------------------------------------*/
  88. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  89. * @{
  90. */
  91. /**
  92. * @brief Variable used for Program/Erase sectors under interruption
  93. */
  94. FLASH_ProcessTypeDef pFlash = {.Lock = HAL_UNLOCKED, \
  95. .ErrorCode = HAL_FLASH_ERROR_NONE, \
  96. .ProcedureOnGoing = 0U, \
  97. .Address = 0U, \
  98. .Bank = FLASH_BANK_1, \
  99. .Sector = 0U, \
  100. .NbSectorsToErase = 0U
  101. };
  102. /**
  103. * @}
  104. */
  105. /* Private function prototypes -----------------------------------------------*/
  106. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  107. * @{
  108. */
  109. static void FLASH_Program_QuadWord(uint32_t FlashAddress, uint32_t DataAddress);
  110. #if defined (FLASH_SR_OBKERR)
  111. static void FLASH_Program_QuadWord_OBK(uint32_t FlashAddress, uint32_t DataAddress);
  112. #endif /* FLASH_SR_OBKERR */
  113. static void FLASH_Program_HalfWord(uint32_t FlashAddress, uint32_t DataAddress);
  114. #if defined(FLASH_EDATAR_EDATA_EN)
  115. static void FLASH_Program_Word(uint32_t FlashAddress, uint32_t DataAddress);
  116. #endif /* FLASH_EDATAR_EDATA_EN */
  117. /**
  118. * @}
  119. */
  120. /* Exported functions ---------------------------------------------------------*/
  121. /** @defgroup FLASH_Exported_Functions FLASH Exported functions
  122. * @{
  123. */
  124. /** @defgroup FLASH_Exported_Functions_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 a quad-word 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. * This parameter shall be aligned to the Flash word (128-bit)
  143. * @param DataAddress specifies the address of data to be programmed
  144. * This parameter shall be 32-bit aligned
  145. * @retval HAL_StatusTypeDef HAL Status
  146. */
  147. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)
  148. {
  149. HAL_StatusTypeDef status;
  150. __IO uint32_t *reg_cr;
  151. #if defined (FLASH_SR_OBKERR)
  152. __IO uint32_t *reg_obkcfgr;
  153. #endif /* FLASH_SR_OBKERR */
  154. /* Check the parameters */
  155. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  156. /* Reset error code */
  157. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  158. /* Wait for last operation to be completed */
  159. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  160. if (status == HAL_OK)
  161. {
  162. /* Set current operation type */
  163. pFlash.ProcedureOnGoing = TypeProgram;
  164. /* Access to SECCR or NSCR depends on operation type */
  165. #if defined (FLASH_OPTSR2_TZEN)
  166. reg_cr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  167. #else
  168. reg_cr = &(FLASH_NS->NSCR);
  169. #endif /* FLASH_OPTSR2_TZEN */
  170. if ((TypeProgram & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEPROGRAM_QUADWORD)
  171. {
  172. /* Check the parameters */
  173. assert_param(IS_FLASH_USER_MEM_ADDRESS(FlashAddress));
  174. /* Program a quad-word (128-bit) at a specified address */
  175. FLASH_Program_QuadWord(FlashAddress, DataAddress);
  176. }
  177. #if defined (FLASH_SR_OBKERR)
  178. else if ((TypeProgram == FLASH_TYPEPROGRAM_QUADWORD_OBK) || (TypeProgram == FLASH_TYPEPROGRAM_QUADWORD_OBK_ALT))
  179. {
  180. /* Check the parameters */
  181. assert_param(IS_FLASH_OBK_ADDRESS(FlashAddress));
  182. /* Program a quad-word (128-bit) of OBK at a specified address */
  183. FLASH_Program_QuadWord_OBK(FlashAddress, DataAddress);
  184. }
  185. #endif /* FLASH_SR_OBKERR */
  186. #if defined (FLASH_EDATAR_EDATA_EN)
  187. else if ((TypeProgram & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEPROGRAM_HALFWORD_EDATA)
  188. {
  189. /* Check the parameters */
  190. assert_param(IS_FLASH_EDATA_ADDRESS(FlashAddress));
  191. /* Program a Flash high-cycle data half-word at a specified address */
  192. FLASH_Program_HalfWord(FlashAddress, DataAddress);
  193. }
  194. else if ((TypeProgram & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEPROGRAM_WORD_EDATA)
  195. {
  196. /* Check the parameters */
  197. assert_param(IS_FLASH_EDATA_ADDRESS(FlashAddress));
  198. /* Program a Flash high-cycle data half-word at a specified address */
  199. FLASH_Program_Word(FlashAddress, DataAddress);
  200. }
  201. #endif /* FLASH_EDATAR_EDATA_EN */
  202. else
  203. {
  204. /* Check the parameters */
  205. assert_param(IS_FLASH_OTP_ADDRESS(FlashAddress));
  206. /* Program an OTP half-word at a specified address */
  207. FLASH_Program_HalfWord(FlashAddress, DataAddress);
  208. }
  209. /* Wait for last operation to be completed */
  210. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  211. #if defined (FLASH_SR_OBKERR)
  212. /* If the program operation is completed, disable the PG */
  213. CLEAR_BIT((*reg_cr), (TypeProgram & ~(FLASH_NON_SECURE_MASK | FLASH_OBK | FLASH_OTP | FLASH_OBKCFGR_ALT_SECT)));
  214. /* Clear alternate sector bit */
  215. if (TypeProgram == FLASH_TYPEPROGRAM_QUADWORD_OBK_ALT)
  216. {
  217. reg_obkcfgr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECOBKCFGR) : &(FLASH_NS->NSOBKCFGR);
  218. CLEAR_BIT((*reg_obkcfgr), FLASH_OBKCFGR_ALT_SECT);
  219. }
  220. #else
  221. /* If the program operation is completed, disable the PG */
  222. CLEAR_BIT((*reg_cr), (TypeProgram & ~(FLASH_NON_SECURE_MASK | FLASH_OTP)));
  223. #endif /* FLASH_SR_OBKERR */
  224. }
  225. /* return status */
  226. return status;
  227. }
  228. /**
  229. * @brief Program a quad-word at a specified address with interrupt enabled.
  230. * @param TypeProgram Indicate the way to program at a specified address.
  231. * This parameter can be a value of @ref FLASH_Type_Program
  232. * @param FlashAddress specifies the address to be programmed.
  233. * This parameter shall be aligned to the Flash word (128-bit)
  234. * @param DataAddress specifies the address of data to be programmed
  235. * This parameter shall be 32-bit aligned
  236. * @retval HAL Status
  237. */
  238. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)
  239. {
  240. HAL_StatusTypeDef status;
  241. __IO uint32_t *reg_cr;
  242. /* Check the parameters */
  243. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  244. /* Reset error code */
  245. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  246. /* Wait for last operation to be completed */
  247. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  248. if (status != HAL_OK)
  249. {
  250. /* Process Unlocked */
  251. __HAL_UNLOCK(&pFlash);
  252. }
  253. else
  254. {
  255. /* Set internal variables used by the IRQ handler */
  256. pFlash.ProcedureOnGoing = TypeProgram;
  257. pFlash.Address = FlashAddress;
  258. /* Access to SECCR or NSCR depends on operation type */
  259. #if defined (FLASH_OPTSR2_TZEN)
  260. reg_cr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  261. #else
  262. reg_cr = &(FLASH_NS->NSCR);
  263. #endif /* FLASH_OPTSR2_TZEN */
  264. /* Enable End of Operation and Error interrupts */
  265. #if defined (FLASH_SR_OBKERR)
  266. (*reg_cr) |= (FLASH_IT_EOP | FLASH_IT_WRPERR | FLASH_IT_PGSERR | \
  267. FLASH_IT_STRBERR | FLASH_IT_INCERR | FLASH_IT_OBKERR | \
  268. FLASH_IT_OBKWERR);
  269. #else
  270. (*reg_cr) |= (FLASH_IT_EOP | FLASH_IT_WRPERR | FLASH_IT_PGSERR | \
  271. FLASH_IT_STRBERR | FLASH_IT_INCERR);
  272. #endif /* FLASH_SR_OBKERR */
  273. if ((TypeProgram & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEPROGRAM_QUADWORD)
  274. {
  275. /* Check the parameters */
  276. assert_param(IS_FLASH_USER_MEM_ADDRESS(FlashAddress));
  277. /* Program a quad-word (128-bit) at a specified address */
  278. FLASH_Program_QuadWord(FlashAddress, DataAddress);
  279. }
  280. #if defined (FLASH_SR_OBKERR)
  281. else if (((TypeProgram & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEPROGRAM_QUADWORD_OBK) || \
  282. ((TypeProgram & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEPROGRAM_QUADWORD_OBK_ALT))
  283. {
  284. /* Check the parameters */
  285. assert_param(IS_FLASH_OBK_ADDRESS(FlashAddress));
  286. /* Program a quad-word (128-bit) of OBK at a specified address */
  287. FLASH_Program_QuadWord_OBK(FlashAddress, DataAddress);
  288. }
  289. #endif /* FLASH_SR_OBKERR */
  290. #if defined (FLASH_EDATAR_EDATA_EN)
  291. else if ((TypeProgram & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEPROGRAM_HALFWORD_EDATA)
  292. {
  293. /* Check the parameters */
  294. assert_param(IS_FLASH_EDATA_ADDRESS(FlashAddress));
  295. /* Program a Flash high-cycle data half-word at a specified address */
  296. FLASH_Program_HalfWord(FlashAddress, DataAddress);
  297. }
  298. else if ((TypeProgram & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEPROGRAM_WORD_EDATA)
  299. {
  300. /* Check the parameters */
  301. assert_param(IS_FLASH_EDATA_ADDRESS(FlashAddress));
  302. /* Program a Flash high-cycle data word at a specified address */
  303. FLASH_Program_Word(FlashAddress, DataAddress);
  304. }
  305. #endif /* FLASH_EDATAR_EDATA_EN */
  306. else
  307. {
  308. /* Check the parameters */
  309. assert_param(IS_FLASH_OTP_ADDRESS(FlashAddress));
  310. /* Program an OTP word at a specified address */
  311. FLASH_Program_HalfWord(FlashAddress, DataAddress);
  312. }
  313. }
  314. /* return status */
  315. return status;
  316. }
  317. /**
  318. * @brief This function handles FLASH interrupt request.
  319. * @retval None
  320. */
  321. void HAL_FLASH_IRQHandler(void)
  322. {
  323. uint32_t param = 0U;
  324. uint32_t errorflag;
  325. __IO uint32_t *reg_cr;
  326. __IO uint32_t *reg_ccr;
  327. const __IO uint32_t *reg_sr;
  328. const __IO uint32_t *reg_ecccorr;
  329. /* Access to CR, CCR and SR registers depends on operation type */
  330. #if defined (FLASH_OPTSR2_TZEN)
  331. reg_cr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  332. reg_ccr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCCR) : &(FLASH_NS->NSCCR);
  333. reg_sr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECSR) : &(FLASH_NS->NSSR);
  334. #else
  335. reg_cr = &(FLASH_NS->NSCR);
  336. reg_ccr = &(FLASH_NS->NSCCR);
  337. reg_sr = &(FLASH_NS->NSSR);
  338. #endif /* FLASH_OPTSR2_TZEN */
  339. reg_ecccorr = &(FLASH->ECCCORR);
  340. /* Save Flash errors */
  341. errorflag = (*reg_sr) & FLASH_FLAG_SR_ERRORS;
  342. /* Add option byte error flag, if any */
  343. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  344. errorflag |= (FLASH->NSSR & FLASH_FLAG_OPTCHANGEERR);
  345. #endif /* __ARM_FEATURE_CMSE */
  346. /* Set parameter of the callback */
  347. if ((pFlash.ProcedureOnGoing & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEERASE_SECTORS)
  348. {
  349. param = pFlash.Sector;
  350. }
  351. else if ((pFlash.ProcedureOnGoing & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEERASE_MASSERASE)
  352. {
  353. param = pFlash.Bank;
  354. }
  355. else if ((pFlash.ProcedureOnGoing & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEPROGRAM_QUADWORD)
  356. {
  357. param = pFlash.Address;
  358. }
  359. else
  360. {
  361. /* Empty statement (to be compliant MISRA 15.7) */
  362. }
  363. /* Clear operation bit on the on-going procedure */
  364. CLEAR_BIT((*reg_cr), (pFlash.ProcedureOnGoing & ~(FLASH_NON_SECURE_MASK)));
  365. /* Check FLASH operation error flags */
  366. if (errorflag != 0U)
  367. {
  368. /* Save the error code */
  369. pFlash.ErrorCode |= errorflag;
  370. /* Clear error programming flags */
  371. (*reg_ccr) = errorflag & FLASH_FLAG_SR_ERRORS;
  372. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  373. if ((errorflag & FLASH_FLAG_OPTCHANGEERR) != 0U)
  374. {
  375. FLASH->NSCCR = FLASH_FLAG_OPTCHANGEERR;
  376. }
  377. #endif /* __ARM_FEATURE_CMSE */
  378. /* Stop the procedure ongoing */
  379. pFlash.ProcedureOnGoing = 0U;
  380. /* FLASH error interrupt user callback */
  381. HAL_FLASH_OperationErrorCallback(param);
  382. }
  383. /* Check FLASH End of Operation flag */
  384. if (((*reg_sr) & FLASH_FLAG_EOP) != 0U)
  385. {
  386. /* Clear FLASH End of Operation pending bit */
  387. (*reg_ccr) = FLASH_FLAG_EOP;
  388. if ((pFlash.ProcedureOnGoing & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEERASE_SECTORS)
  389. {
  390. /* Nb of sector to erased can be decreased */
  391. pFlash.NbSectorsToErase--;
  392. /* Check if there are still sectors to erase */
  393. if (pFlash.NbSectorsToErase != 0U)
  394. {
  395. /* Increment sector number */
  396. pFlash.Sector++;
  397. FLASH_Erase_Sector(pFlash.Sector, pFlash.Bank);
  398. }
  399. else
  400. {
  401. /* No more sectors to erase */
  402. /* Reset sector parameter and stop erase sectors procedure */
  403. param = 0xFFFFFFFFU;
  404. pFlash.ProcedureOnGoing = 0U;
  405. }
  406. }
  407. else
  408. {
  409. /* Clear the procedure ongoing */
  410. pFlash.ProcedureOnGoing = 0U;
  411. }
  412. /* FLASH EOP interrupt user callback */
  413. HAL_FLASH_EndOfOperationCallback(param);
  414. }
  415. /* Check FLASH ECC correction flag */
  416. if ((*reg_ecccorr & FLASH_ECCR_ECCC) != 0U)
  417. {
  418. /* Call User callback */
  419. HAL_FLASHEx_EccCorrectionCallback();
  420. /* Clear ECC correction flag in order to allow new ECC error record */
  421. FLASH->ECCCORR |= FLASH_ECCR_ECCC;
  422. }
  423. if (pFlash.ProcedureOnGoing == 0U)
  424. {
  425. /* Disable Flash Operation and Error source interrupt */
  426. #if defined (FLASH_SR_OBKERR)
  427. (*reg_cr) &= ~(FLASH_IT_EOP | FLASH_IT_WRPERR | FLASH_IT_PGSERR | \
  428. FLASH_IT_STRBERR | FLASH_IT_INCERR | FLASH_IT_OBKERR | \
  429. FLASH_IT_OBKWERR | FLASH_IT_OPTCHANGEERR);
  430. #else
  431. (*reg_cr) &= ~(FLASH_IT_EOP | FLASH_IT_WRPERR | FLASH_IT_PGSERR | \
  432. FLASH_IT_STRBERR | FLASH_IT_INCERR | FLASH_IT_OPTCHANGEERR);
  433. #endif /* FLASH_SR_OBKERR */
  434. }
  435. }
  436. /**
  437. * @brief FLASH end of operation interrupt callback
  438. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  439. * Mass Erase: Bank number which has been requested to erase
  440. * Sectors Erase: Sector which has been erased
  441. * (if 0xFFFFFFFF, it means that all the selected sectors have been erased)
  442. * Program: Address which was selected for data program
  443. * @retval None
  444. */
  445. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  446. {
  447. /* Prevent unused argument(s) compilation warning */
  448. UNUSED(ReturnValue);
  449. /* NOTE : This function Should not be modified, when the callback is needed,
  450. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  451. */
  452. }
  453. /**
  454. * @brief FLASH operation error interrupt callback
  455. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  456. * Mass Erase: Bank number which has been requested to erase
  457. * Sectors Erase: Sector number which returned an error
  458. * Program: Address which was selected for data program
  459. * @retval None
  460. */
  461. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  462. {
  463. /* Prevent unused argument(s) compilation warning */
  464. UNUSED(ReturnValue);
  465. /* NOTE : This function Should not be modified, when the callback is needed,
  466. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  467. */
  468. }
  469. /**
  470. * @}
  471. */
  472. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  473. * @brief Management functions
  474. *
  475. @verbatim
  476. ===============================================================================
  477. ##### Peripheral Control functions #####
  478. ===============================================================================
  479. [..]
  480. This subsection provides a set of functions allowing to control the FLASH
  481. memory operations.
  482. @endverbatim
  483. * @{
  484. */
  485. /**
  486. * @brief Unlock the FLASH control registers access
  487. * @retval HAL Status
  488. */
  489. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  490. {
  491. HAL_StatusTypeDef status = HAL_OK;
  492. if (READ_BIT(FLASH->NSCR, FLASH_CR_LOCK) != 0U)
  493. {
  494. /* Authorize the FLASH Control Register access */
  495. WRITE_REG(FLASH->NSKEYR, FLASH_KEY1);
  496. WRITE_REG(FLASH->NSKEYR, FLASH_KEY2);
  497. /* Verify Flash CR is unlocked */
  498. if (READ_BIT(FLASH->NSCR, FLASH_CR_LOCK) != 0U)
  499. {
  500. status = HAL_ERROR;
  501. }
  502. }
  503. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  504. if (status == HAL_OK)
  505. {
  506. if (READ_BIT(FLASH->SECCR, FLASH_CR_LOCK) != 0U)
  507. {
  508. /* Authorize the FLASH Control Register access */
  509. WRITE_REG(FLASH->SECKEYR, FLASH_KEY1);
  510. WRITE_REG(FLASH->SECKEYR, FLASH_KEY2);
  511. /* verify Flash CR is unlocked */
  512. if (READ_BIT(FLASH->SECCR, FLASH_CR_LOCK) != 0U)
  513. {
  514. status = HAL_ERROR;
  515. }
  516. }
  517. }
  518. #endif /* __ARM_FEATURE_CMSE */
  519. return status;
  520. }
  521. /**
  522. * @brief Locks the FLASH control registers access
  523. * @retval HAL Status
  524. */
  525. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  526. {
  527. HAL_StatusTypeDef status = HAL_OK;
  528. /* Set the LOCK Bit to lock the FLASH Control Register access */
  529. SET_BIT(FLASH->NSCR, FLASH_CR_LOCK);
  530. /* Verify Flash is locked */
  531. if (READ_BIT(FLASH->NSCR, FLASH_CR_LOCK) == 0U)
  532. {
  533. status = HAL_ERROR;
  534. }
  535. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  536. if (status == HAL_OK)
  537. {
  538. /* Set the LOCK Bit to lock the FLASH Control Register access */
  539. SET_BIT(FLASH->SECCR, FLASH_CR_LOCK);
  540. /* verify Flash is locked */
  541. if (READ_BIT(FLASH->SECCR, FLASH_CR_LOCK) == 0U)
  542. {
  543. status = HAL_ERROR;
  544. }
  545. }
  546. #endif /* __ARM_FEATURE_CMSE */
  547. return status;
  548. }
  549. /**
  550. * @brief Unlock the FLASH Option Control Registers access.
  551. * @retval HAL Status
  552. */
  553. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  554. {
  555. if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) != 0U)
  556. {
  557. /* Authorizes the Option Byte registers programming */
  558. WRITE_REG(FLASH->OPTKEYR, FLASH_OPT_KEY1);
  559. WRITE_REG(FLASH->OPTKEYR, FLASH_OPT_KEY2);
  560. /* Verify that the Option Bytes are unlocked */
  561. if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) != 0U)
  562. {
  563. return HAL_ERROR;
  564. }
  565. }
  566. return HAL_OK;
  567. }
  568. /**
  569. * @brief Lock the FLASH Option Control Registers access.
  570. * @retval HAL Status
  571. */
  572. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  573. {
  574. /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
  575. SET_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK);
  576. /* Verify that the Option Bytes are locked */
  577. if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) != 0U)
  578. {
  579. return HAL_OK;
  580. }
  581. return HAL_ERROR;
  582. }
  583. /**
  584. * @brief Launch the option bytes loading.
  585. * @retval HAL Status
  586. */
  587. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  588. {
  589. HAL_StatusTypeDef status;
  590. /* Set OPTSTRT Bit */
  591. SET_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTSTART);
  592. /* Wait for OB change operation to be completed */
  593. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  594. return status;
  595. }
  596. /**
  597. * @}
  598. */
  599. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
  600. * @brief Peripheral Errors functions
  601. *
  602. @verbatim
  603. ===============================================================================
  604. ##### Peripheral Errors functions #####
  605. ===============================================================================
  606. [..]
  607. This subsection permits to get in run-time Errors of the FLASH peripheral.
  608. @endverbatim
  609. * @{
  610. */
  611. /**
  612. * @brief Get the specific FLASH error flag.
  613. * @retval HAL_FLASH_ERRORCode The returned value can be:
  614. * @arg HAL_FLASH_ERROR_NONE : No error set
  615. * @arg HAL_FLASH_ERROR_WRP : Write Protection Error
  616. * @arg HAL_FLASH_ERROR_PGS : Program Sequence Error
  617. * @arg HAL_FLASH_ERROR_STRB : Strobe Error
  618. * @arg HAL_FLASH_ERROR_INC : Inconsistency Error
  619. * @arg HAL_FLASH_ERROR_OBK : OBK Error
  620. * @arg HAL_FLASH_ERROR_OBKW : OBK Write Error
  621. * @arg HAL_FLASH_ERROR_OB_CHANGE : Option Byte Change Error
  622. * @arg HAL_FLASH_ERROR_ECCC : ECC Single Correction Error
  623. * @arg HAL_FLASH_ERROR_ECCD : ECC Double Detection Error
  624. */
  625. uint32_t HAL_FLASH_GetError(void)
  626. {
  627. return pFlash.ErrorCode;
  628. }
  629. /**
  630. * @}
  631. */
  632. /**
  633. * @}
  634. */
  635. /* Private functions ---------------------------------------------------------*/
  636. /** @addtogroup FLASH_Private_Functions
  637. * @{
  638. */
  639. /**
  640. * @brief Wait for a FLASH operation to complete.
  641. * @param Timeout maximum flash operation timeout
  642. * @retval HAL_StatusTypeDef HAL Status
  643. */
  644. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  645. {
  646. /* Wait for the FLASH operation to complete by polling on BUSY, WBNE and DBNE flags to be reset.
  647. Even if the FLASH operation fails, the BUSY, WBNE and DBNE flags will be reset and an error
  648. flag will be set */
  649. uint32_t errorflag;
  650. const __IO uint32_t *reg_sr;
  651. __IO uint32_t *reg_ccr;
  652. uint32_t tickstart = HAL_GetTick();
  653. /* Access to SR register depends on operation type */
  654. #if defined (FLASH_OPTSR2_TZEN)
  655. reg_sr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECSR) : &(FLASH_NS->NSSR);
  656. #else
  657. reg_sr = &(FLASH_NS->NSSR);
  658. #endif /* FLASH_OPTSR2_TZEN */
  659. /* Wait on BSY, WBNE and DBNE flags to be reset */
  660. while (((*reg_sr) & (FLASH_FLAG_BSY | FLASH_FLAG_WBNE | FLASH_FLAG_DBNE)) != 0U)
  661. {
  662. if (Timeout != HAL_MAX_DELAY)
  663. {
  664. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  665. {
  666. return HAL_TIMEOUT;
  667. }
  668. }
  669. }
  670. /* Access to CCR register depends on operation type */
  671. #if defined (FLASH_OPTSR2_TZEN)
  672. reg_ccr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCCR) : &(FLASH_NS->NSCCR);
  673. #else
  674. reg_ccr = &(FLASH_NS->NSCCR);
  675. #endif /* FLASH_OPTSR2_TZEN */
  676. /* Check FLASH operation error flags */
  677. errorflag = ((*reg_sr) & FLASH_FLAG_SR_ERRORS);
  678. /* Add option byte error flag, if any */
  679. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  680. errorflag |= (FLASH->NSSR & FLASH_FLAG_OPTCHANGEERR);
  681. #endif /* __ARM_FEATURE_CMSE */
  682. /* In case of error reported in Flash SR or OPTSR registers */
  683. if (errorflag != 0U)
  684. {
  685. /*Save the error code*/
  686. pFlash.ErrorCode |= errorflag;
  687. /* Clear error flags */
  688. (*reg_ccr) = errorflag & FLASH_FLAG_SR_ERRORS;
  689. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  690. if ((errorflag & FLASH_FLAG_OPTCHANGEERR) != 0U)
  691. {
  692. FLASH->NSCCR = FLASH_FLAG_OPTCHANGEERR;
  693. }
  694. #endif /* __ARM_FEATURE_CMSE */
  695. return HAL_ERROR;
  696. }
  697. /* Check FLASH End of Operation flag */
  698. if (((*reg_sr) & FLASH_FLAG_EOP) != 0U)
  699. {
  700. /* Clear FLASH End of Operation pending bit */
  701. (*reg_ccr) = FLASH_FLAG_EOP;
  702. }
  703. /* If there is no error flag set */
  704. return HAL_OK;
  705. }
  706. /**
  707. * @brief Program a quad-word (128-bit) at a specified address.
  708. * @param FlashAddress specifies the address to be programmed.
  709. * @param DataAddress specifies the address of data to be programmed.
  710. * @retval None
  711. */
  712. static void FLASH_Program_QuadWord(uint32_t FlashAddress, uint32_t DataAddress)
  713. {
  714. uint8_t index = 4;
  715. uint32_t *dest_addr = (uint32_t *)FlashAddress;
  716. uint32_t *src_addr = (uint32_t *)DataAddress;
  717. uint32_t primask_bit;
  718. __IO uint32_t *reg_cr;
  719. /* Access to SECCR or NSCR registers depends on operation type */
  720. #if defined (FLASH_OPTSR2_TZEN)
  721. reg_cr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  722. #else
  723. reg_cr = &(FLASH_NS->NSCR);
  724. #endif /* FLASH_OPTSR2_TZEN */
  725. /* Set PG bit */
  726. SET_BIT((*reg_cr), FLASH_CR_PG);
  727. /* Enter critical section: Disable interrupts to avoid any interruption during the loop */
  728. primask_bit = __get_PRIMASK();
  729. __disable_irq();
  730. /* Program the quad-word */
  731. do
  732. {
  733. *dest_addr = *src_addr;
  734. dest_addr++;
  735. src_addr++;
  736. index--;
  737. } while (index != 0U);
  738. /* Exit critical section: restore previous priority mask */
  739. __set_PRIMASK(primask_bit);
  740. }
  741. #if defined (FLASH_SR_OBKERR)
  742. /**
  743. * @brief Program a quad-word (128-bit) of OBK at a specified address.
  744. * @param FlashAddress specifies the address to be programmed.
  745. * @param DataAddress specifies the address of data to be programmed.
  746. * @retval None
  747. */
  748. static void FLASH_Program_QuadWord_OBK(uint32_t FlashAddress, uint32_t DataAddress)
  749. {
  750. uint8_t index = 4;
  751. uint32_t *dest_addr = (uint32_t *)FlashAddress;
  752. uint32_t *src_addr = (uint32_t *)DataAddress;
  753. uint32_t primask_bit;
  754. __IO uint32_t *reg_cr;
  755. __IO uint32_t *reg_obkcfgr;
  756. /* Access to SECCR or NSCR registers depends on operation type */
  757. reg_cr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  758. reg_obkcfgr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECOBKCFGR) : &(FLASH_NS->NSOBKCFGR);
  759. /* Set PG bit */
  760. SET_BIT((*reg_cr), FLASH_CR_PG);
  761. /* Set ALT_SECT bit */
  762. SET_BIT((*reg_obkcfgr), pFlash.ProcedureOnGoing & FLASH_OBKCFGR_ALT_SECT);
  763. /* Enter critical section: Disable interrupts to avoid any interruption during the loop */
  764. primask_bit = __get_PRIMASK();
  765. __disable_irq();
  766. /* Program the quad-word */
  767. do
  768. {
  769. *dest_addr = *src_addr;
  770. dest_addr++;
  771. src_addr++;
  772. index--;
  773. } while (index != 0U);
  774. /* Exit critical section: restore previous priority mask */
  775. __set_PRIMASK(primask_bit);
  776. }
  777. #endif /* FLASH_SR_OBKERR */
  778. /**
  779. * @brief Program a half-word (16-bit) at a specified address.
  780. * @param FlashAddress specifies the address to be programmed.
  781. * @param DataAddress specifies the address of data to be programmed.
  782. * @retval None
  783. */
  784. static void FLASH_Program_HalfWord(uint32_t FlashAddress, uint32_t DataAddress)
  785. {
  786. __IO uint32_t *reg_cr;
  787. /* Access to SECCR or NSCR registers depends on operation type */
  788. #if defined (FLASH_OPTSR2_TZEN)
  789. reg_cr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  790. #else
  791. reg_cr = &(FLASH_NS->NSCR);
  792. #endif /* FLASH_OPTSR2_TZEN */
  793. /* Set HalfWord_PG bit */
  794. SET_BIT((*reg_cr), FLASH_CR_PG);
  795. /* Program a halfword word (16 bits) */
  796. *(__IO uint16_t *)FlashAddress = *(__IO uint16_t *)DataAddress;
  797. }
  798. #if defined(FLASH_EDATAR_EDATA_EN)
  799. /**
  800. * @brief Program a word (32-bit) at a specified address.
  801. * @param FlashAddress specifies the address to be programmed.
  802. * @param DataAddress specifies the address of data to be programmed.
  803. * @retval None
  804. */
  805. static void FLASH_Program_Word(uint32_t FlashAddress, uint32_t DataAddress)
  806. {
  807. __IO uint32_t *reg_cr;
  808. /* Access to SECCR or NSCR registers depends on operation type */
  809. #if defined (FLASH_OPTSR2_TZEN)
  810. reg_cr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  811. #else
  812. reg_cr = &(FLASH_NS->NSCR);
  813. #endif /* FLASH_OPTSR2_TZEN */
  814. /* Set PG bit */
  815. SET_BIT((*reg_cr), FLASH_CR_PG);
  816. *(__IO uint32_t *)FlashAddress = *(__IO uint32_t *)DataAddress;
  817. }
  818. #endif /* FLASH_EDATAR_EDATA_EN */
  819. /**
  820. * @}
  821. */
  822. #endif /* HAL_FLASH_MODULE_ENABLED */
  823. /**
  824. * @}
  825. */
  826. /**
  827. * @}
  828. */