stm32h5xx_hal_flash_ex.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  1. /**
  2. ******************************************************************************
  3. * @file stm32h5xx_hal_flash_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended FLASH HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the FLASH extension peripheral:
  8. * + Extended programming operations functions
  9. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * Copyright (c) 2023 STMicroelectronics.
  14. * All rights reserved.
  15. *
  16. * This software is licensed under terms that can be found in the LICENSE file
  17. * in the root directory of this software component.
  18. * If no LICENSE file comes with this software, it is provided AS-IS.
  19. *
  20. ******************************************************************************
  21. @verbatim
  22. ==============================================================================
  23. ##### Flash Extension features #####
  24. ==============================================================================
  25. [..] Comparing to other previous devices, the FLASH interface for STM32H5xx
  26. devices contains the following additional features
  27. (+) Capacity up to 2 Mbyte with dual bank architecture supporting read-while-write
  28. capability (RWW)
  29. (+) Dual bank memory organization
  30. (+) Product State protection
  31. (+) Write protection
  32. (+) Secure access only protection
  33. (+) Bank / register swapping (when Dual-Bank)
  34. (+) Watermark-based secure protection
  35. (+) Block-based secure protection
  36. (+) Block-based privilege protection
  37. (+) Hide Protection areas
  38. ##### How to use this driver #####
  39. ==============================================================================
  40. [..] This driver provides functions to configure and program the FLASH memory
  41. of all STM32H5xx devices. It includes
  42. (#) FLASH Memory Erase functions:
  43. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  44. HAL_FLASH_Lock() functions
  45. (++) Erase function: Sector erase, bank erase and dual-bank mass erase
  46. (++) There are two modes of erase :
  47. (+++) Polling Mode using HAL_FLASHEx_Erase()
  48. (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
  49. (#) Option Bytes Programming functions: Use HAL_FLASHEx_OBProgram() to:
  50. (++) Configure the write protection per bank
  51. (++) Set the Product State
  52. (++) Program the user Option Bytes
  53. (++) Configure the watermark security for each area
  54. (++) Configure the Hide protection areas
  55. (++) Configure the Boot addresses
  56. (#) Get Option Bytes Configuration function: Use HAL_FLASHEx_OBGetConfig() to:
  57. (++) Get the value of a write protection area
  58. (++) Get the Product State
  59. (++) Get the value of the user Option Bytes
  60. (++) Get the configuration of watermark security areas
  61. (++) Get the configuration of Hide protection areas
  62. (++) Get the value of a boot address
  63. (#) Block-based secure / privilege area configuration function: Use HAL_FLASHEx_ConfigBBAttributes()
  64. (++) Bit-field allowing to secure or un-secure each sector
  65. (++) Bit-field allowing to privilege or un-privilege each sector
  66. (#) Get the block-based secure / privilege area configuration function: Use HAL_FLASHEx_GetConfigBBAttributes()
  67. (++) Return the configuration of the block-based security and privilege for all the sectors
  68. (#) Privilege mode configuration function: Use HAL_FLASHEx_ConfigPrivMode()
  69. (++) FLASH register can be protected against non-privilege accesses
  70. (#) Get the privilege mode configuration function: Use HAL_FLASHEx_GetPrivMode()
  71. (++) Return if the FLASH registers are protected against non-privilege accesses
  72. @endverbatim
  73. */
  74. /* Includes ------------------------------------------------------------------*/
  75. #include "stm32h5xx_hal.h"
  76. /** @addtogroup STM32H5xx_HAL_Driver
  77. * @{
  78. */
  79. /** @defgroup FLASHEx FLASHEx
  80. * @brief FLASH HAL Extension module driver
  81. * @{
  82. */
  83. #ifdef HAL_FLASH_MODULE_ENABLED
  84. /* Private typedef -----------------------------------------------------------*/
  85. /* Private define ------------------------------------------------------------*/
  86. /* Private macro -------------------------------------------------------------*/
  87. /* Private variables ---------------------------------------------------------*/
  88. /* Private function prototypes -----------------------------------------------*/
  89. /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
  90. * @{
  91. */
  92. static void FLASH_MassErase(uint32_t Banks);
  93. #if defined (FLASH_SR_OBKERR)
  94. static void FLASH_OBKErase(void);
  95. #endif /* FLASH_SR_OBKERR */
  96. static void FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks);
  97. static void FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Bank);
  98. static void FLASH_OB_GetWRP(uint32_t Bank, uint32_t *WRPState, uint32_t *WRPSector);
  99. static void FLASH_OB_ProdStateConfig(uint32_t ProdStateConfig);
  100. static uint32_t FLASH_OB_GetProdState(void);
  101. static void FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig1, uint32_t UserConfig2);
  102. static void FLASH_OB_GetUser(uint32_t *UserConfig1, uint32_t *UserConfig2);
  103. static void FLASH_OB_BootAddrConfig(uint32_t BootOption, uint32_t BootAddress);
  104. static void FLASH_OB_BootLockConfig(uint32_t BootLockOption, uint32_t BootLockConfig);
  105. static void FLASH_OB_GetBootConfig(uint32_t BootOption, uint32_t *BootAddress, uint32_t *BootLockConfig);
  106. static void FLASH_OB_OTP_LockConfig(uint32_t OTP_Block);
  107. static uint32_t FLASH_OB_OTP_GetLock(void);
  108. static void FLASH_OB_HDPConfig(uint32_t Banks, uint32_t HDPStartSector, uint32_t HDPEndSector);
  109. static void FLASH_OB_GetHDP(uint32_t Bank, uint32_t *HDPStartSector, uint32_t *HDPEndSector);
  110. #if defined(FLASH_EDATAR_EDATA_EN)
  111. static void FLASH_OB_EDATAConfig(uint32_t Banks, uint32_t EDATASize);
  112. static void FLASH_OB_GetEDATA(uint32_t Bank, uint32_t *EDATASize);
  113. #endif /* FLASH_EDATAR_EDATA_EN */
  114. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  115. static void FLASH_OB_WMSECConfig(uint32_t Banks, uint32_t WMSecStartSector, uint32_t WMSecEndSector);
  116. static void FLASH_OB_GetWMSEC(uint32_t Bank, uint32_t *WMSecStartSector, uint32_t *WMSecEndSector);
  117. #endif /* __ARM_FEATURE_CMSE */
  118. /**
  119. * @}
  120. */
  121. /* Exported functions ---------------------------------------------------------*/
  122. /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
  123. * @{
  124. */
  125. /** @defgroup FLASHEx_Exported_Functions_Group1 FLASHEx Extended IO operation functions
  126. * @brief FLASHEx Extended IO operation functions
  127. *
  128. @verbatim
  129. ===============================================================================
  130. ##### Extended programming operation functions #####
  131. ===============================================================================
  132. [..]
  133. This subsection provides a set of functions allowing to manage the Extended FLASH
  134. programming operations Operations.
  135. @endverbatim
  136. * @{
  137. */
  138. /**
  139. * @brief Perform a mass erase or erase the specified FLASH memory sectors
  140. * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  141. * contains the configuration information for the erasing.
  142. *
  143. * @param[out] SectorError pointer to variable that contains the configuration
  144. * information on faulty sector in case of error (0xFFFFFFFF means that all
  145. * the sectors have been correctly erased).
  146. *
  147. * @retval HAL Status
  148. */
  149. HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
  150. {
  151. HAL_StatusTypeDef status;
  152. uint32_t sector_index;
  153. __IO uint32_t *reg_cr;
  154. /* Check the parameters */
  155. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  156. /* Process Locked */
  157. __HAL_LOCK(&pFlash);
  158. /* Reset error code */
  159. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  160. /* Wait for last operation to be completed */
  161. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  162. if (status == HAL_OK)
  163. {
  164. /* Current operation type */
  165. pFlash.ProcedureOnGoing = pEraseInit->TypeErase;
  166. /* Access to SECCR or NSCR depends on operation type */
  167. #if defined (FLASH_OPTSR2_TZEN)
  168. reg_cr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  169. #else
  170. reg_cr = &(FLASH_NS->NSCR);
  171. #endif /* FLASH_OPTSR2_TZEN */
  172. if ((pEraseInit->TypeErase & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEERASE_MASSERASE)
  173. {
  174. /* Mass erase to be done */
  175. FLASH_MassErase(pEraseInit->Banks);
  176. /* Wait for last operation to be completed */
  177. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  178. }
  179. #if defined (FLASH_SR_OBKERR)
  180. else if (pEraseInit->TypeErase == FLASH_TYPEERASE_OBK_ALT)
  181. {
  182. /* OBK erase to be done */
  183. FLASH_OBKErase();
  184. /* Wait for last operation to be completed */
  185. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  186. }
  187. #endif /* FLASH_SR_OBKERR */
  188. else
  189. {
  190. /* Initialization of SectorError variable */
  191. *SectorError = 0xFFFFFFFFU;
  192. /* Erase by sector by sector to be done*/
  193. for (sector_index = pEraseInit->Sector; sector_index < (pEraseInit->NbSectors + pEraseInit->Sector); \
  194. sector_index++)
  195. {
  196. FLASH_Erase_Sector(sector_index, pEraseInit->Banks);
  197. /* Wait for last operation to be completed */
  198. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  199. if (status != HAL_OK)
  200. {
  201. /* In case of error, stop erase procedure and return the faulty sector */
  202. *SectorError = sector_index;
  203. break;
  204. }
  205. }
  206. }
  207. /* If the erase operation is completed, disable the associated bits */
  208. CLEAR_BIT((*reg_cr), (pEraseInit->TypeErase) & (~(FLASH_NON_SECURE_MASK)));
  209. }
  210. /* Process Unlocked */
  211. __HAL_UNLOCK(&pFlash);
  212. return status;
  213. }
  214. /**
  215. * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
  216. * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  217. * contains the configuration information for the erasing.
  218. *
  219. * @retval HAL Status
  220. */
  221. HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
  222. {
  223. HAL_StatusTypeDef status;
  224. __IO uint32_t *reg_cr;
  225. /* Check the parameters */
  226. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  227. /* Reset error code */
  228. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  229. /* Wait for last operation to be completed */
  230. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  231. if (status != HAL_OK)
  232. {
  233. return status;
  234. }
  235. else
  236. {
  237. /* Set internal variables used by the IRQ handler */
  238. pFlash.ProcedureOnGoing = pEraseInit->TypeErase;
  239. pFlash.Bank = pEraseInit->Banks;
  240. /* Access to SECCR or NSCR depends on operation type */
  241. #if defined (FLASH_OPTSR2_TZEN)
  242. reg_cr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  243. #else
  244. reg_cr = &(FLASH_NS->NSCR);
  245. #endif /* FLASH_OPTSR2_TZEN */
  246. /* Enable End of Operation and Error interrupts */
  247. #if defined (FLASH_SR_OBKERR)
  248. (*reg_cr) |= (FLASH_IT_EOP | FLASH_IT_WRPERR | FLASH_IT_PGSERR | \
  249. FLASH_IT_STRBERR | FLASH_IT_INCERR | FLASH_IT_OBKERR | \
  250. FLASH_IT_OBKWERR);
  251. #else
  252. (*reg_cr) |= (FLASH_IT_EOP | FLASH_IT_WRPERR | FLASH_IT_PGSERR | \
  253. FLASH_IT_STRBERR | FLASH_IT_INCERR);
  254. #endif /* FLASH_SR_OBKERR */
  255. if ((pEraseInit->TypeErase & (~FLASH_NON_SECURE_MASK)) == FLASH_TYPEERASE_MASSERASE)
  256. {
  257. /* Mass erase to be done */
  258. FLASH_MassErase(pEraseInit->Banks);
  259. }
  260. #if defined (FLASH_SR_OBKERR)
  261. else if (pEraseInit->TypeErase == FLASH_TYPEERASE_OBK_ALT)
  262. {
  263. /* OBK erase to be done */
  264. FLASH_OBKErase();
  265. }
  266. #endif /* FLASH_SR_OBKERR */
  267. else
  268. {
  269. /* Erase by sector to be done */
  270. pFlash.NbSectorsToErase = pEraseInit->NbSectors;
  271. pFlash.Sector = pEraseInit->Sector;
  272. /* Erase first sector and wait for IT */
  273. FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->Banks);
  274. }
  275. }
  276. return status;
  277. }
  278. /**
  279. * @brief Program option bytes
  280. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  281. * contains the configuration information for the programming.
  282. *
  283. * @note To configure any option bytes, the option lock bit OPTLOCK must be
  284. * cleared with the call of HAL_FLASH_OB_Unlock() function.
  285. * @note New option bytes configuration will be taken into account in two cases:
  286. * - after an option bytes launch through the call of HAL_FLASH_OB_Launch()
  287. * - after a power-on reset (BOR reset or exit from Standby/Shutdown modes)
  288. * @retval HAL Status
  289. */
  290. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
  291. {
  292. HAL_StatusTypeDef status;
  293. /* Check the parameters */
  294. assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
  295. /* Process Locked */
  296. __HAL_LOCK(&pFlash);
  297. /* Reset Error Code */
  298. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  299. /* Current operation type */
  300. pFlash.ProcedureOnGoing = FLASH_TYPEPROGRAM_OB;
  301. /* Wait for last operation to be completed */
  302. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  303. if (status == HAL_OK)
  304. {
  305. /*Write protection configuration*/
  306. if ((pOBInit->OptionType & OPTIONBYTE_WRP) != 0U)
  307. {
  308. assert_param(IS_WRPSTATE(pOBInit->WRPState));
  309. if (pOBInit->WRPState == OB_WRPSTATE_ENABLE)
  310. {
  311. /* Enable write protection on the selected sectors */
  312. FLASH_OB_EnableWRP(pOBInit->WRPSector, pOBInit->Banks);
  313. }
  314. else
  315. {
  316. /* Disable write protection on the selected sectors */
  317. FLASH_OB_DisableWRP(pOBInit->WRPSector, pOBInit->Banks);
  318. }
  319. }
  320. /* Product State configuration */
  321. if ((pOBInit->OptionType & OPTIONBYTE_PROD_STATE) != 0U)
  322. {
  323. /* Configure the product state */
  324. FLASH_OB_ProdStateConfig(pOBInit->ProductState);
  325. }
  326. /* User Configuration */
  327. if ((pOBInit->OptionType & OPTIONBYTE_USER) != 0U)
  328. {
  329. /* Configure the user option bytes */
  330. FLASH_OB_UserConfig(pOBInit->USERType, pOBInit->USERConfig, pOBInit->USERConfig2);
  331. }
  332. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  333. /* Watermark secure configuration */
  334. if ((pOBInit->OptionType & OPTIONBYTE_WMSEC) != 0U)
  335. {
  336. /* Configure the watermark-based secure area */
  337. FLASH_OB_WMSECConfig(pOBInit->Banks, pOBInit->WMSecStartSector, pOBInit->WMSecEndSector);
  338. }
  339. #endif /* __ARM_FEATURE_CMSE */
  340. /* Boot Address configuration */
  341. if ((pOBInit->OptionType & OPTIONBYTE_BOOTADDR) != 0U)
  342. {
  343. FLASH_OB_BootAddrConfig(pOBInit->BootConfig, pOBInit->BootAddr);
  344. }
  345. /* Unique boot entry point configuration */
  346. if ((pOBInit->OptionType & OPTIONBYTE_BOOT_LOCK) != 0U)
  347. {
  348. /* Configure the unique boot entry point */
  349. FLASH_OB_BootLockConfig(pOBInit->BootConfig, pOBInit->BootLock);
  350. }
  351. /* OTP Block Lock configuration */
  352. if ((pOBInit->OptionType & OPTIONBYTE_OTP_LOCK) != 0U)
  353. {
  354. FLASH_OB_OTP_LockConfig(pOBInit->OTPBlockLock);
  355. }
  356. /* Hide Protection area configuration */
  357. if ((pOBInit->OptionType & OPTIONBYTE_HDP) != 0U)
  358. {
  359. FLASH_OB_HDPConfig(pOBInit->Banks, pOBInit->HDPStartSector, pOBInit->HDPEndSector);
  360. }
  361. #if defined(FLASH_EDATAR_EDATA_EN)
  362. /* Flash high-cycle data area configuration */
  363. if ((pOBInit->OptionType & OPTIONBYTE_EDATA) != 0U)
  364. {
  365. FLASH_OB_EDATAConfig(pOBInit->Banks, pOBInit->EDATASize);
  366. }
  367. #endif /* FLASH_EDATAR_EDATA_EN */
  368. }
  369. /* Process Unlocked */
  370. __HAL_UNLOCK(&pFlash);
  371. return status;
  372. }
  373. /**
  374. * @brief Get the Option byte configuration
  375. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  376. * contains the configuration information for the programming.
  377. * @note The parameter Banks of the pOBInit structure must be set exclusively to FLASH_BANK_1 or FLASH_BANK_2,
  378. * as this parameter is use to get the given Bank WRP, PCROP and secured area configuration.
  379. *
  380. * @retval None
  381. */
  382. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
  383. {
  384. pOBInit->OptionType = (OPTIONBYTE_USER | OPTIONBYTE_PROD_STATE);
  385. /* Get Product State */
  386. pOBInit->ProductState = FLASH_OB_GetProdState();
  387. /* Get the user option bytes */
  388. FLASH_OB_GetUser(&(pOBInit->USERConfig), &(pOBInit->USERConfig2));
  389. if ((pOBInit->Banks == FLASH_BANK_1) || (pOBInit->Banks == FLASH_BANK_2))
  390. {
  391. /* Get write protection on the selected area */
  392. pOBInit->OptionType |= OPTIONBYTE_WRP;
  393. FLASH_OB_GetWRP(pOBInit->Banks, &(pOBInit->WRPState), &(pOBInit->WRPSector));
  394. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  395. /* Get the configuration of the watermark secure area for the selected area */
  396. pOBInit->OptionType |= OPTIONBYTE_WMSEC;
  397. FLASH_OB_GetWMSEC(pOBInit->Banks, &(pOBInit->WMSecStartSector), &(pOBInit->WMSecEndSector));
  398. #endif /* __ARM_FEATURE_CMSE */
  399. /* Get the configuration of the hide protection for the selected area */
  400. pOBInit->OptionType |= OPTIONBYTE_HDP;
  401. FLASH_OB_GetHDP(pOBInit->Banks, &(pOBInit->HDPStartSector), &(pOBInit->HDPEndSector));
  402. #if defined (FLASH_EDATAR_EDATA_EN)
  403. /* Get the Flash high-cycle data configuration for the selected area */
  404. pOBInit->OptionType |= OPTIONBYTE_EDATA;
  405. FLASH_OB_GetEDATA(pOBInit->Banks, &(pOBInit->EDATASize));
  406. #endif /* FLASH_EDATAR_EDATA_EN */
  407. }
  408. /* Get boot configuration */
  409. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  410. if ((pOBInit->BootConfig == OB_BOOT_NS) || (pOBInit->BootConfig == OB_BOOT_SEC))
  411. #else
  412. if (pOBInit->BootConfig == OB_BOOT_NS)
  413. #endif /* __ARM_FEATURE_CMSE */
  414. {
  415. pOBInit->OptionType |= OPTIONBYTE_BOOTADDR | OPTIONBYTE_BOOT_LOCK;
  416. FLASH_OB_GetBootConfig(pOBInit->BootConfig, &(pOBInit->BootAddr), &(pOBInit->BootLock));
  417. }
  418. /* Get OTP Block Lock */
  419. pOBInit->OptionType |= OPTIONBYTE_OTP_LOCK;
  420. pOBInit->OTPBlockLock = FLASH_OB_OTP_GetLock();
  421. }
  422. #if defined (FLASH_SR_OBKERR)
  423. /**
  424. * @brief Unlock the FLASH OBK register access
  425. * @retval HAL Status
  426. */
  427. HAL_StatusTypeDef HAL_FLASHEx_OBK_Unlock(void)
  428. {
  429. HAL_StatusTypeDef status = HAL_OK;
  430. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  431. if (READ_BIT(FLASH->SECOBKCFGR, FLASH_OBKCFGR_LOCK) != 0U)
  432. {
  433. /* Authorize the FLASH OBK Register access */
  434. WRITE_REG(FLASH->SECOBKKEYR, FLASH_OBK_KEY1);
  435. WRITE_REG(FLASH->SECOBKKEYR, FLASH_OBK_KEY2);
  436. /* Verify Flash OBK Register is unlocked */
  437. if (READ_BIT(FLASH->SECOBKCFGR, FLASH_OBKCFGR_LOCK) != 0U)
  438. {
  439. status = HAL_ERROR;
  440. }
  441. }
  442. #else
  443. if (READ_BIT(FLASH->NSOBKCFGR, FLASH_OBKCFGR_LOCK) != 0U)
  444. {
  445. /* Authorize the FLASH OBK Register access */
  446. WRITE_REG(FLASH->NSOBKKEYR, FLASH_OBK_KEY1);
  447. WRITE_REG(FLASH->NSOBKKEYR, FLASH_OBK_KEY2);
  448. /* Verify Flash OBK Register is unlocked */
  449. if (READ_BIT(FLASH->NSOBKCFGR, FLASH_OBKCFGR_LOCK) != 0U)
  450. {
  451. status = HAL_ERROR;
  452. }
  453. }
  454. #endif /* __ARM_FEATURE_CMSE */
  455. return status;
  456. }
  457. /**
  458. * @brief Locks the FLASH OBK register access
  459. * @retval HAL Status
  460. */
  461. HAL_StatusTypeDef HAL_FLASHEx_OBK_Lock(void)
  462. {
  463. HAL_StatusTypeDef status = HAL_ERROR;
  464. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  465. /* Set the LOCK Bit to lock the FLASH OBK Register access */
  466. SET_BIT(FLASH->SECOBKCFGR, FLASH_OBKCFGR_LOCK);
  467. /* verify Flash is locked */
  468. if (READ_BIT(FLASH->SECOBKCFGR, FLASH_OBKCFGR_LOCK) != 0U)
  469. {
  470. status = HAL_OK;
  471. }
  472. #else
  473. /* Set the LOCK Bit to lock the FLASH OBK Register access */
  474. SET_BIT(FLASH->NSOBKCFGR, FLASH_OBKCFGR_LOCK);
  475. /* Verify Flash OBK is locked */
  476. if (READ_BIT(FLASH->NSOBKCFGR, FLASH_OBKCFGR_LOCK) != 0U)
  477. {
  478. status = HAL_OK;
  479. }
  480. #endif /* __ARM_FEATURE_CMSE */
  481. return status;
  482. }
  483. /**
  484. * @brief Swap the FLASH Option Bytes Keys (OBK)
  485. * @param SwapOffset Specifies the number of keys to be swapped.
  486. * This parameter can be a value between 0 (no OBK data swapped) and 511 (all OBK data swapped).
  487. * Typical value are available in @ref FLASH_OBK_SWAP_Offset
  488. * @retval HAL Status
  489. */
  490. HAL_StatusTypeDef HAL_FLASHEx_OBK_Swap(uint32_t SwapOffset)
  491. {
  492. HAL_StatusTypeDef status;
  493. __IO uint32_t *reg_obkcfgr;
  494. /* Wait for last operation to be completed */
  495. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  496. if (status == HAL_OK)
  497. {
  498. /* Access to SECOBKCFGR or NSOBKCFGR registers depends on operation type */
  499. reg_obkcfgr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECOBKCFGR) : &(FLASH_NS->NSOBKCFGR);
  500. /* Set OBK swap offset */
  501. MODIFY_REG((*reg_obkcfgr), FLASH_OBKCFGR_SWAP_OFFSET, (SwapOffset << FLASH_OBKCFGR_SWAP_OFFSET_Pos));
  502. /* Set OBK swap request */
  503. SET_BIT((*reg_obkcfgr), FLASH_OBKCFGR_SWAP_SECT_REQ);
  504. /* Wait for last operation to be completed */
  505. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  506. }
  507. return status;
  508. }
  509. /**
  510. * @brief Swap the FLASH Option Bytes Keys (OBK) with interrupt enabled
  511. * @param SwapOffset Specifies the number of keys to be swapped.
  512. * This parameter can be a value between 0 (no OBK data swapped) and 511 (all OBK data swapped).
  513. * Typical value are available in @ref FLASH_OBK_SWAP_Offset
  514. * @retval HAL Status
  515. */
  516. HAL_StatusTypeDef HAL_FLASHEx_OBK_Swap_IT(uint32_t SwapOffset)
  517. {
  518. HAL_StatusTypeDef status;
  519. __IO uint32_t *reg_obkcfgr;
  520. /* Wait for last operation to be completed */
  521. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  522. if (status == HAL_OK)
  523. {
  524. /* Access to SECOBKCFGR or NSOBKCFGR registers depends on operation type */
  525. reg_obkcfgr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECOBKCFGR) : &(FLASH_NS->NSOBKCFGR);
  526. /* Enable End of Operation and Error interrupts */
  527. (*reg_obkcfgr) |= (FLASH_IT_EOP | FLASH_IT_WRPERR | FLASH_IT_PGSERR | FLASH_IT_STRBERR | FLASH_IT_INCERR);
  528. /* Set OBK swap offset */
  529. MODIFY_REG((*reg_obkcfgr), FLASH_OBKCFGR_SWAP_OFFSET, (SwapOffset << FLASH_OBKCFGR_SWAP_OFFSET_Pos));
  530. /* Set OBK swap request */
  531. SET_BIT((*reg_obkcfgr), FLASH_OBKCFGR_SWAP_SECT_REQ);
  532. }
  533. return status;
  534. }
  535. #endif /* FLASH_SR_OBKERR */
  536. /**
  537. * @brief Return the on-going Flash Operation. After a system reset, return
  538. * the interrupted Flash operation, if any.
  539. * @param pFlashOperation [out] pointer to a FLASH_OperationTypeDef structure
  540. * that contains the Flash operation information.
  541. * @retval None
  542. */
  543. void HAL_FLASHEx_GetOperation(FLASH_OperationTypeDef *pFlashOperation)
  544. {
  545. uint32_t opsr_reg = FLASH->OPSR;
  546. /* Get Flash operation Type */
  547. pFlashOperation->OperationType = opsr_reg & FLASH_OPSR_CODE_OP;
  548. /* Get Flash operation memory */
  549. #if defined (FLASH_EDATAR_EDATA_EN)
  550. pFlashOperation->FlashArea = opsr_reg & (FLASH_OPSR_DATA_OP | FLASH_OPSR_BK_OP | \
  551. FLASH_OPSR_SYSF_OP | FLASH_OPSR_OTP_OP);
  552. #else
  553. pFlashOperation->FlashArea = opsr_reg & (FLASH_OPSR_BK_OP | FLASH_OPSR_SYSF_OP | \
  554. FLASH_OPSR_OTP_OP);
  555. #endif /* FLASH_EDATAR_EDATA_EN */
  556. /* Get Flash operation address */
  557. pFlashOperation->Address = opsr_reg & FLASH_OPSR_ADDR_OP;
  558. }
  559. /**
  560. * @}
  561. */
  562. /** @defgroup FLASHEx_Exported_Functions_Group2 FLASHEx Extension Protection configuration functions
  563. * @brief Extension Protection configuration functions
  564. * @{
  565. */
  566. /**
  567. * @brief Configure the block-based secure area.
  568. *
  569. * @param pBBAttributes pointer to an FLASH_BBAttributesTypeDef structure that
  570. * contains the configuration information for the programming.
  571. *
  572. * @note The field pBBAttributes->Bank should indicate which area is requested
  573. * for the block-based attributes.
  574. * @note The field pBBAttributes->BBAttributesType should indicate which
  575. * block-base attribute type is requested: Secure or Privilege.
  576. *
  577. * @retval HAL Status
  578. */
  579. HAL_StatusTypeDef HAL_FLASHEx_ConfigBBAttributes(FLASH_BBAttributesTypeDef *pBBAttributes)
  580. {
  581. HAL_StatusTypeDef status;
  582. uint8_t index;
  583. __IO uint32_t *reg;
  584. /* Check the parameters */
  585. assert_param(IS_FLASH_BANK_EXCLUSIVE(pBBAttributes->Bank));
  586. assert_param(IS_FLASH_BB_EXCLUSIVE(pBBAttributes->BBAttributesType));
  587. /* Wait for last operation to be completed */
  588. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  589. if (status == HAL_OK)
  590. {
  591. /* Set the first Block-Based register to write */
  592. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  593. if (pBBAttributes->BBAttributesType == FLASH_BB_SEC)
  594. {
  595. if (pBBAttributes->Bank == FLASH_BANK_1)
  596. {
  597. reg = &(FLASH->SECBB1R1);
  598. }
  599. else
  600. {
  601. reg = &(FLASH->SECBB2R1);
  602. }
  603. }
  604. else
  605. #endif /* __ARM_FEATURE_CMSE */
  606. {
  607. if (pBBAttributes->Bank == FLASH_BANK_1)
  608. {
  609. reg = &(FLASH->PRIVBB1R1);
  610. }
  611. else
  612. {
  613. reg = &(FLASH->PRIVBB2R1);
  614. }
  615. }
  616. /* Modify the register values and check that new attributes are taken in account */
  617. for (index = 0; index < FLASH_BLOCKBASED_NB_REG; index++)
  618. {
  619. *reg = pBBAttributes->BBAttributes_array[index] & FLASH_PRIVBBR_PRIVBB;
  620. if ((*reg) != (pBBAttributes->BBAttributes_array[index] & FLASH_PRIVBBR_PRIVBB))
  621. {
  622. status = HAL_ERROR;
  623. }
  624. reg++;
  625. }
  626. /* ISB instruction is called to be sure next instructions are performed with correct attributes */
  627. __ISB();
  628. }
  629. /* Process Unlocked */
  630. __HAL_UNLOCK(&pFlash);
  631. return status;
  632. }
  633. /**
  634. * @brief Return the block-based attributes.
  635. *
  636. * @param pBBAttributes [in/out] pointer to an FLASH_BBAttributesTypeDef structure
  637. * that contains the configuration information.
  638. * @note The field pBBAttributes->Bank should indicate which area is requested
  639. * for the block-based attributes.
  640. * @note The field pBBAttributes->BBAttributesType should indicate which
  641. * block-base attribute type is requested: Secure or Privilege.
  642. *
  643. * @retval None
  644. */
  645. void HAL_FLASHEx_GetConfigBBAttributes(FLASH_BBAttributesTypeDef *pBBAttributes)
  646. {
  647. uint8_t index;
  648. __IO uint32_t *reg;
  649. /* Check the parameters */
  650. assert_param(IS_FLASH_BANK_EXCLUSIVE(pBBAttributes->Bank));
  651. assert_param(IS_FLASH_BB_EXCLUSIVE(pBBAttributes->BBAttributesType));
  652. /* Set the first Block-Based register to read */
  653. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  654. if (pBBAttributes->BBAttributesType == FLASH_BB_SEC)
  655. {
  656. if (pBBAttributes->Bank == FLASH_BANK_1)
  657. {
  658. reg = &(FLASH->SECBB1R1);
  659. }
  660. else
  661. {
  662. reg = &(FLASH->SECBB2R1);
  663. }
  664. }
  665. else
  666. #endif /* __ARM_FEATURE_CMSE */
  667. {
  668. if (pBBAttributes->Bank == FLASH_BANK_1)
  669. {
  670. reg = &(FLASH->PRIVBB1R1);
  671. }
  672. else
  673. {
  674. reg = &(FLASH->PRIVBB2R1);
  675. }
  676. }
  677. /* Read the register values */
  678. for (index = 0; index < FLASH_BLOCKBASED_NB_REG; index++)
  679. {
  680. pBBAttributes->BBAttributes_array[index] = (*reg) & FLASH_PRIVBBR_PRIVBB;
  681. reg++;
  682. }
  683. }
  684. /**
  685. * @brief Configuration of the privilege attribute.
  686. *
  687. * @param PrivMode indicate privilege mode configuration
  688. * This parameter can be one of the following values:
  689. * @arg FLASH_SPRIV_GRANTED: access to secure Flash registers is granted to privileged or unprivileged access
  690. * @arg FLASH_SPRIV_DENIED: access to secure Flash registers is denied to unprivileged access
  691. * @arg FLASH_NSPRIV_GRANTED: access to non-secure Flash registers is granted to privileged or unprivileged access
  692. * @arg FLASH_NSPRIV_DENIED: access to non-secure Flash registers is denied to unprivilege access
  693. *
  694. * @retval None
  695. */
  696. void HAL_FLASHEx_ConfigPrivMode(uint32_t PrivMode)
  697. {
  698. /* Check the parameters */
  699. assert_param(IS_FLASH_CFGPRIVMODE(PrivMode));
  700. #if defined (FLASH_PRIVCFGR_SPRIV)
  701. MODIFY_REG(FLASH->PRIVCFGR, (FLASH_PRIVCFGR_SPRIV | FLASH_PRIVCFGR_NSPRIV), PrivMode);
  702. #else
  703. MODIFY_REG(FLASH->PRIVCFGR, FLASH_PRIVCFGR_NSPRIV, PrivMode);
  704. #endif /* FLASH_PRIVCFGR_SPRIV */
  705. }
  706. /**
  707. * @brief Return the value of the privilege attribute.
  708. *
  709. * @retval It indicates the privilege mode configuration.
  710. * This return value can be one of the following values:
  711. * @arg FLASH_SPRIV_GRANTED: access to secure Flash registers is granted to privileged or unprivileged access
  712. * @arg FLASH_SPRIV_DENIED: access to secure Flash registers is denied to unprivileged access
  713. * @arg FLASH_NSPRIV_GRANTED: access to non-secure Flash registers is granted to privileged or unprivileged access
  714. * @arg FLASH_NSPRIV_DENIED: access to Flash registers is denied to unprivilege accessP
  715. */
  716. uint32_t HAL_FLASHEx_GetPrivMode(void)
  717. {
  718. #if defined (FLASH_PRIVCFGR_SPRIV)
  719. return (FLASH->PRIVCFGR & (FLASH_PRIVCFGR_SPRIV | FLASH_PRIVCFGR_NSPRIV));
  720. #else
  721. return (FLASH->PRIVCFGR & FLASH_PRIVCFGR_NSPRIV);
  722. #endif /* FLASH_PRIVCFGR_SPRIV */
  723. }
  724. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  725. /**
  726. * @brief Configuration of the security inversion.
  727. *
  728. * @param SecInvState indicate the flash security state configuration
  729. * This parameter can be one of the following values:
  730. * @arg FLASH_SEC_INV_DISABLE: Security state of Flash is not inverted
  731. * @arg FLASH_SEC_INV_ENABLE: Security state of Flash is inverted
  732. *
  733. * @retval HAL Status
  734. */
  735. HAL_StatusTypeDef HAL_FLASHEx_ConfigSecInversion(uint32_t SecInvState)
  736. {
  737. HAL_StatusTypeDef status;
  738. /* Check the parameters */
  739. assert_param(IS_FLASH_CFGSECINV(SecInvState));
  740. /* Process Locked */
  741. __HAL_LOCK(&pFlash);
  742. /* Wait for last operation to be completed */
  743. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  744. if (status == HAL_OK)
  745. {
  746. MODIFY_REG(FLASH->SECCR, FLASH_CR_INV, SecInvState);
  747. }
  748. /* Process Unlocked */
  749. __HAL_UNLOCK(&pFlash);
  750. return status;
  751. }
  752. /**
  753. * @brief Return the value of the security inversion.
  754. *
  755. * @retval It indicates the flash security state configuration
  756. * This return value can be one of the following values:
  757. * @arg FLASH_SEC_INV_DISABLE: Security state of Flash is not inverted
  758. * @arg FLASH_SEC_INV_ENABLE: Security state of Flash is inverted
  759. */
  760. uint32_t HAL_FLASHEx_GetSecInversion(void)
  761. {
  762. return (FLASH->SECCR & FLASH_CR_INV);
  763. }
  764. #endif /* __ARM_FEATURE_CMSE */
  765. /**
  766. * @brief Configure the HDP extension area.
  767. *
  768. * @param pHDPExtension pointer to an FLASH_HDPExtentionTypeDef structure that
  769. * contains the configuration information.
  770. *
  771. * @note The field pHDPExtension->Banks should indicate which area is requested
  772. * for the HDP extension.
  773. * @note The field pHDPExtension->NbSectors should indicate the number of
  774. * sector to be added to the HDP area.
  775. *
  776. * @retval HAL Status
  777. */
  778. HAL_StatusTypeDef HAL_FLASHEx_ConfigHDPExtension(const FLASH_HDPExtensionTypeDef *pHDPExtension)
  779. {
  780. /* Check the parameters */
  781. assert_param(IS_FLASH_BANK(pHDPExtension->Banks));
  782. assert_param(IS_FLASH_SECTOR(pHDPExtension->NbSectors));
  783. /* Set the HDP extension register */
  784. if (pHDPExtension->Banks == FLASH_BANK_1)
  785. {
  786. MODIFY_REG(FLASH->HDPEXTR, FLASH_HDPEXTR_HDP1_EXT, pHDPExtension->NbSectors);
  787. }
  788. else if (pHDPExtension->Banks == FLASH_BANK_2)
  789. {
  790. MODIFY_REG(FLASH->HDPEXTR, FLASH_HDPEXTR_HDP2_EXT, (pHDPExtension->NbSectors << FLASH_HDPEXTR_HDP2_EXT_Pos));
  791. }
  792. else
  793. {
  794. FLASH->HDPEXTR = (pHDPExtension->NbSectors << FLASH_HDPEXTR_HDP2_EXT_Pos) | pHDPExtension->NbSectors;
  795. }
  796. return HAL_OK;
  797. }
  798. /**
  799. * @}
  800. */
  801. /**
  802. * @}
  803. */
  804. /* Private functions ---------------------------------------------------------*/
  805. /** @addtogroup FLASHEx_Private_Functions
  806. * @{
  807. */
  808. /**
  809. * @brief Mass erase of FLASH memory
  810. * @param Banks Banks to be erased
  811. * This parameter can be one of the following values:
  812. * @arg FLASH_BANK_1: Bank1 to be erased
  813. * @arg FLASH_BANK_2: Bank2 to be erased
  814. * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
  815. * @retval None
  816. */
  817. static void FLASH_MassErase(uint32_t Banks)
  818. {
  819. __IO uint32_t *reg_cr;
  820. /* Check the parameters */
  821. assert_param(IS_FLASH_BANK(Banks));
  822. /* Access to SECCR or NSCR registers depends on operation type */
  823. #if defined (FLASH_OPTSR2_TZEN)
  824. reg_cr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  825. #else
  826. reg_cr = &(FLASH_NS->NSCR);
  827. #endif /* FLASH_OPTSR2_TZEN */
  828. /* Flash Mass Erase */
  829. if ((Banks & FLASH_BANK_BOTH) == FLASH_BANK_BOTH)
  830. {
  831. /* Set Mass Erase Bit */
  832. SET_BIT((*reg_cr), FLASH_CR_MER | FLASH_CR_START);
  833. }
  834. else
  835. {
  836. /* Proceed to erase Flash Bank */
  837. if ((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  838. {
  839. /* Erase Bank1 */
  840. MODIFY_REG((*reg_cr), (FLASH_CR_BKSEL | FLASH_CR_BER | FLASH_CR_START), (FLASH_CR_BER | FLASH_CR_START));
  841. }
  842. if ((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  843. {
  844. /* Erase Bank2 */
  845. SET_BIT((*reg_cr), (FLASH_CR_BER | FLASH_CR_BKSEL | FLASH_CR_START));
  846. }
  847. }
  848. }
  849. /**
  850. * @brief Erase the specified FLASH memory sector
  851. * @param Sector FLASH sector to erase
  852. * This parameter can be a value of @ref FLASH_Sectors
  853. * @param Banks Bank(s) where the sector will be erased
  854. * This parameter can be one of the following values:
  855. * @arg FLASH_BANK_1: Sector in bank 1 to be erased
  856. * @arg FLASH_BANK_2: Sector in bank 2 to be erased
  857. * @retval None
  858. */
  859. void FLASH_Erase_Sector(uint32_t Sector, uint32_t Banks)
  860. {
  861. __IO uint32_t *reg_cr;
  862. /* Check the parameters */
  863. assert_param(IS_FLASH_SECTOR(Sector));
  864. assert_param(IS_FLASH_BANK_EXCLUSIVE(Banks));
  865. /* Access to SECCR or NSCR registers depends on operation type */
  866. #if defined (FLASH_OPTSR2_TZEN)
  867. reg_cr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  868. #else
  869. reg_cr = &(FLASH_NS->NSCR);
  870. #endif /* FLASH_OPTSR2_TZEN */
  871. if ((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  872. {
  873. /* Reset Sector Number for Bank1 */
  874. (*reg_cr) &= ~(FLASH_CR_SNB | FLASH_CR_BKSEL);
  875. (*reg_cr) |= (FLASH_CR_SER | (Sector << FLASH_CR_SNB_Pos) | FLASH_CR_START);
  876. }
  877. else
  878. {
  879. /* Reset Sector Number for Bank2 */
  880. (*reg_cr) &= ~(FLASH_CR_SNB);
  881. (*reg_cr) |= (FLASH_CR_SER | FLASH_CR_BKSEL | (Sector << FLASH_CR_SNB_Pos) | FLASH_CR_START);
  882. }
  883. }
  884. #if defined (FLASH_SR_OBKERR)
  885. /**
  886. * @brief Erase of FLASH OBK
  887. * @retval None
  888. */
  889. static void FLASH_OBKErase()
  890. {
  891. __IO uint32_t *reg_obkcfgr;
  892. /* Access to SECOBKCFGR or NSOBKCFGR registers depends on operation type */
  893. reg_obkcfgr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECOBKCFGR) : &(FLASH_NS->NSOBKCFGR);
  894. /* Set OBK Erase Bit */
  895. SET_BIT((*reg_obkcfgr), FLASH_OBKCFGR_ALT_SECT_ERASE);
  896. }
  897. #endif /* FLASH_SR_OBKERR */
  898. /**
  899. * @brief Enable the write protection of the desired bank1 or bank 2 sectors
  900. * @param WRPSector specifies the sectors to be write protected.
  901. * This parameter can be a value of @ref FLASH_OB_Write_Protection_Sectors
  902. *
  903. * @param Banks the specific bank to apply WRP sectors
  904. * This parameter can be one of the following values:
  905. * @arg FLASH_BANK_1: enable WRP on specified bank1 sectors
  906. * @arg FLASH_BANK_2: enable WRP on specified bank2 sectors
  907. * @arg FLASH_BANK_BOTH: enable WRP on both bank1 and bank2 specified sectors
  908. *
  909. * @retval None
  910. */
  911. static void FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
  912. {
  913. /* Check the parameters */
  914. assert_param(IS_FLASH_BANK(Banks));
  915. if ((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  916. {
  917. /* Enable Write Protection for bank 1 */
  918. FLASH->WRP1R_PRG &= (~(WRPSector & FLASH_WRPR_WRPSG));
  919. }
  920. if ((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  921. {
  922. /* Enable Write Protection for bank 2 */
  923. FLASH->WRP2R_PRG &= (~(WRPSector & FLASH_WRPR_WRPSG));
  924. }
  925. }
  926. /**
  927. * @brief Disable the write protection of the desired bank1 or bank 2 sectors
  928. * @param WRPSector specifies the sectors to disable write protection.
  929. * This parameter can be a value of @ref FLASH_OB_Write_Protection_Sectors
  930. *
  931. * @param Banks the specific bank to apply WRP sectors
  932. * This parameter can be one of the following values:
  933. * @arg FLASH_BANK_1: disable WRP on specified bank1 sectors
  934. * @arg FLASH_BANK_2: disable WRP on specified bank2 sectors
  935. * @arg FLASH_BANK_BOTH: disable WRP on both bank1 and bank2 specified sectors
  936. *
  937. * @retval None
  938. */
  939. static void FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
  940. {
  941. /* Check the parameters */
  942. assert_param(IS_FLASH_BANK(Banks));
  943. if ((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  944. {
  945. /* Disable Write Protection for bank 1 */
  946. FLASH->WRP1R_PRG |= (WRPSector & FLASH_WRPR_WRPSG);
  947. }
  948. if ((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  949. {
  950. /* Disable Write Protection for bank 2 */
  951. FLASH->WRP2R_PRG |= (WRPSector & FLASH_WRPR_WRPSG);
  952. }
  953. }
  954. /**
  955. * @brief Get the write protection of the given bank 1 or bank 2 sectors
  956. * @param[in] Bank specifies the bank where to get the write protection sectors.
  957. * This parameter can be exclusively one of the following values:
  958. * @arg FLASH_BANK_1: Get bank1 WRP sectors
  959. * @arg FLASH_BANK_2: Get bank2 WRP sectors
  960. *
  961. * @param[out] WRPState returns the write protection state of the returned sectors.
  962. * This parameter can be one of the following values:
  963. * @arg WRPState: OB_WRPSTATE_DISABLE or OB_WRPSTATE_ENABLE
  964. * @param[out] WRPSector returns the write protected sectors on the given bank .
  965. * This parameter can be a value of @ref FLASH_OB_Write_Protection_Sectors
  966. *
  967. * @retval None
  968. */
  969. static void FLASH_OB_GetWRP(uint32_t Bank, uint32_t *WRPState, uint32_t *WRPSector)
  970. {
  971. uint32_t regvalue = 0U;
  972. if (Bank == FLASH_BANK_1)
  973. {
  974. regvalue = FLASH->WRP1R_CUR;
  975. }
  976. if (Bank == FLASH_BANK_2)
  977. {
  978. regvalue = FLASH->WRP2R_CUR;
  979. }
  980. (*WRPSector) = (~regvalue) & FLASH_WRPR_WRPSG;
  981. if (*WRPSector == 0U)
  982. {
  983. (*WRPState) = OB_WRPSTATE_DISABLE;
  984. }
  985. else
  986. {
  987. (*WRPState) = OB_WRPSTATE_ENABLE;
  988. }
  989. }
  990. /**
  991. * @brief Set the product state.
  992. *
  993. * @note To configure the product state, the option lock bit OPTLOCK must be
  994. * cleared with the call of the HAL_FLASH_OB_Unlock() function.
  995. * @note To validate the product state, the option bytes must be reloaded
  996. * through the call of the HAL_FLASH_OB_Launch() function.
  997. *
  998. * @param ProductState specifies the product state.
  999. * This parameter can be a value of @ref FLASH_OB_Product_State
  1000. *
  1001. * @retval None
  1002. */
  1003. static void FLASH_OB_ProdStateConfig(uint32_t ProductState)
  1004. {
  1005. /* Check the parameters */
  1006. assert_param(IS_OB_PRODUCT_STATE(ProductState));
  1007. /* Configure the Product State in the option bytes register */
  1008. MODIFY_REG(FLASH->OPTSR_PRG, FLASH_OPTSR_PRODUCT_STATE, ProductState);
  1009. }
  1010. /**
  1011. * @brief Get the the product state.
  1012. * @retval ProductState returns the product state.
  1013. * This returned value can a value of @ref FLASH_OB_Product_State
  1014. */
  1015. static uint32_t FLASH_OB_GetProdState(void)
  1016. {
  1017. return (FLASH->OPTSR_CUR & FLASH_OPTSR_PRODUCT_STATE);
  1018. }
  1019. /**
  1020. * @brief Program the FLASH User Option Byte.
  1021. *
  1022. * @note To configure the user option bytes, the option lock bit OPTLOCK must
  1023. * be cleared with the call of the HAL_FLASH_OB_Unlock() function.
  1024. * @note To validate the user option bytes, the option bytes must be reloaded
  1025. * through the call of the HAL_FLASH_OB_Launch() function.
  1026. *
  1027. * @param UserType specifies The FLASH User Option Bytes to be modified.
  1028. * This parameter can be a combination of @ref FLASH_OB_USER_Type
  1029. *
  1030. * @param UserConfig1 specifies values of the selected User Option Bytes.
  1031. * This parameter can be a combination of @ref FLASH_OB_USER_BOR_LEVEL,
  1032. * @ref FLASH_OB_USER_BORH_EN, @ref FLASH_OB_USER_IWDG_SW,
  1033. * @ref FLASH_OB_USER_WWDG_SW, @ref FLASH_OB_USER_nRST_STOP,
  1034. * @ref FLASH_OB_USER_nRST_STANDBY, @ref FLASH_OB_USER_IO_VDD_HSLV,
  1035. * @ref FLASH_OB_USER_IO_VDDIO2_HSLV, @ref FLASH_OB_USER_IWDG_STOP,
  1036. * @ref FLASH_OB_USER_IWDG_STANDBY, @ref FLASH_OB_USER_BOOT_UBE and @ref OB_USER_SWAP_BANK.
  1037. * @param UserConfig2 specifies values of the selected User Option Bytes.
  1038. * @ref FLASH_OB_USER_SRAM1_3_RST, @ref FLASH_OB_USER_SRAM2_RST,
  1039. * @ref FLASH_OB_USER_BKPRAM_ECC, @ref FLASH_OB_USER_SRAM3_ECC,
  1040. * @ref FLASH_OB_USER_SRAM2_ECC, @ref FLASH_OB_USER_SRAM1_ECC,
  1041. * @ref FLASH_OB_USER_SRAM1_RST and @ref OB_USER_TZEN.
  1042. * @retval None
  1043. */
  1044. static void FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig1, uint32_t UserConfig2)
  1045. {
  1046. uint32_t optr_reg1_val = 0U;
  1047. uint32_t optr_reg1_mask = 0U;
  1048. uint32_t optr_reg2_val = 0U;
  1049. uint32_t optr_reg2_mask = 0U;
  1050. /* Check the parameters */
  1051. assert_param(IS_OB_USER_TYPE(UserType));
  1052. if ((UserType & OB_USER_BOR_LEV) != 0U)
  1053. {
  1054. /* BOR level option byte should be modified */
  1055. assert_param(IS_OB_USER_BOR_LEVEL(UserConfig1 & FLASH_OPTSR_BOR_LEV));
  1056. /* Set value and mask for BOR level option byte */
  1057. optr_reg1_val |= (UserConfig1 & FLASH_OPTSR_BOR_LEV);
  1058. optr_reg1_mask |= FLASH_OPTSR_BOR_LEV;
  1059. }
  1060. if ((UserType & OB_USER_BORH_EN) != 0U)
  1061. {
  1062. /* BOR high enable status bit should be modified */
  1063. assert_param(IS_OB_USER_BORH_EN(UserConfig1 & FLASH_OPTSR_BORH_EN));
  1064. /* Set value and mask for BOR high enable status bit */
  1065. optr_reg1_val |= (UserConfig1 & FLASH_OPTSR_BORH_EN);
  1066. optr_reg1_mask |= FLASH_OPTSR_BORH_EN;
  1067. }
  1068. if ((UserType & OB_USER_IWDG_SW) != 0U)
  1069. {
  1070. /* IWDG_SW option byte should be modified */
  1071. assert_param(IS_OB_USER_IWDG(UserConfig1 & FLASH_OPTSR_IWDG_SW));
  1072. /* Set value and mask for IWDG_SW option byte */
  1073. optr_reg1_val |= (UserConfig1 & FLASH_OPTSR_IWDG_SW);
  1074. optr_reg1_mask |= FLASH_OPTSR_IWDG_SW;
  1075. }
  1076. if ((UserType & OB_USER_WWDG_SW) != 0U)
  1077. {
  1078. /* WWDG_SW option byte should be modified */
  1079. assert_param(IS_OB_USER_WWDG(UserConfig1 & FLASH_OPTSR_WWDG_SW));
  1080. /* Set value and mask for WWDG_SW option byte */
  1081. optr_reg1_val |= (UserConfig1 & FLASH_OPTSR_WWDG_SW);
  1082. optr_reg1_mask |= FLASH_OPTSR_WWDG_SW;
  1083. }
  1084. if ((UserType & OB_USER_NRST_STOP) != 0U)
  1085. {
  1086. /* nRST_STOP option byte should be modified */
  1087. assert_param(IS_OB_USER_STOP(UserConfig1 & FLASH_OPTSR_NRST_STOP));
  1088. /* Set value and mask for nRST_STOP option byte */
  1089. optr_reg1_val |= (UserConfig1 & FLASH_OPTSR_NRST_STOP);
  1090. optr_reg1_mask |= FLASH_OPTSR_NRST_STOP;
  1091. }
  1092. if ((UserType & OB_USER_NRST_STDBY) != 0U)
  1093. {
  1094. /* nRST_STDBY option byte should be modified */
  1095. assert_param(IS_OB_USER_STANDBY(UserConfig1 & FLASH_OPTSR_NRST_STDBY));
  1096. /* Set value and mask for nRST_STDBY option byte */
  1097. optr_reg1_val |= (UserConfig1 & FLASH_OPTSR_NRST_STDBY);
  1098. optr_reg1_mask |= FLASH_OPTSR_NRST_STDBY;
  1099. }
  1100. if ((UserType & OB_USER_IO_VDD_HSLV) != 0U)
  1101. {
  1102. /* IO_VDD_HSLV option byte should be modified */
  1103. assert_param(IS_OB_USER_IO_VDD_HSLV(UserConfig1 & FLASH_OPTSR_IO_VDD_HSLV));
  1104. /* Set value and mask for IO_VDD_HSLV option byte */
  1105. optr_reg1_val |= (UserConfig1 & FLASH_OPTSR_IO_VDD_HSLV);
  1106. optr_reg1_mask |= FLASH_OPTSR_IO_VDD_HSLV;
  1107. }
  1108. if ((UserType & OB_USER_IO_VDDIO2_HSLV) != 0U)
  1109. {
  1110. /* IO_VDD_HSLV option byte should be modified */
  1111. assert_param(IS_OB_USER_IO_VDDIO2_HSLV(UserConfig1 & FLASH_OPTSR_IO_VDDIO2_HSLV));
  1112. /* Set value and mask for IO_VDD_HSLV option byte */
  1113. optr_reg1_val |= (UserConfig1 & FLASH_OPTSR_IO_VDDIO2_HSLV);
  1114. optr_reg1_mask |= FLASH_OPTSR_IO_VDDIO2_HSLV;
  1115. }
  1116. if ((UserType & OB_USER_IWDG_STOP) != 0U)
  1117. {
  1118. /* IWDG_STOP option byte should be modified */
  1119. assert_param(IS_OB_USER_IWDG_STOP(UserConfig1 & FLASH_OPTSR_IWDG_STOP));
  1120. /* Set value and mask for IWDG_STOP option byte */
  1121. optr_reg1_val |= (UserConfig1 & FLASH_OPTSR_IWDG_STOP);
  1122. optr_reg1_mask |= FLASH_OPTSR_IWDG_STOP;
  1123. }
  1124. if ((UserType & OB_USER_IWDG_STDBY) != 0U)
  1125. {
  1126. /* IWDG_STDBY option byte should be modified */
  1127. assert_param(IS_OB_USER_IWDG_STDBY(UserConfig1 & FLASH_OPTSR_IWDG_STDBY));
  1128. /* Set value and mask for IWDG_STDBY option byte */
  1129. optr_reg1_val |= (UserConfig1 & FLASH_OPTSR_IWDG_STDBY);
  1130. optr_reg1_mask |= FLASH_OPTSR_IWDG_STDBY;
  1131. }
  1132. #if defined (FLASH_OPTSR_BOOT_UBE)
  1133. if ((UserType & OB_USER_BOOT_UBE) != 0U)
  1134. {
  1135. /* SWAP_BANK option byte should be modified */
  1136. assert_param(IS_OB_USER_BOOT_UBE(UserConfig1 & FLASH_OPTSR_BOOT_UBE));
  1137. /* Set value and mask for BOOT_UBE option byte */
  1138. optr_reg1_val |= (UserConfig1 & FLASH_OPTSR_BOOT_UBE);
  1139. optr_reg1_mask |= FLASH_OPTSR_BOOT_UBE;
  1140. }
  1141. #endif /* FLASH_OPTSR_BOOT_UBE */
  1142. if ((UserType & OB_USER_SWAP_BANK) != 0U)
  1143. {
  1144. /* SWAP_BANK option byte should be modified */
  1145. assert_param(IS_OB_USER_SWAP_BANK(UserConfig1 & FLASH_OPTSR_SWAP_BANK));
  1146. /* Set value and mask for SWAP_BANK option byte */
  1147. optr_reg1_val |= (UserConfig1 & FLASH_OPTSR_SWAP_BANK);
  1148. optr_reg1_mask |= FLASH_OPTSR_SWAP_BANK;
  1149. }
  1150. #if defined (FLASH_OPTSR2_SRAM1_3_RST)
  1151. if ((UserType & OB_USER_SRAM1_3_RST) != 0U)
  1152. {
  1153. /* SRAM13_RST option byte should be modified */
  1154. assert_param(IS_OB_USER_SRAM1_3_RST(UserConfig2 & FLASH_OPTSR2_SRAM1_3_RST));
  1155. /* Set value and mask for SRAM13_RST option byte */
  1156. optr_reg2_val |= (UserConfig2 & FLASH_OPTSR2_SRAM1_3_RST);
  1157. optr_reg2_mask |= FLASH_OPTSR2_SRAM1_3_RST;
  1158. }
  1159. #endif /* FLASH_OPTSR2_SRAM1_3_RST */
  1160. #if defined (FLASH_OPTSR2_SRAM1_RST)
  1161. if ((UserType & OB_USER_SRAM1_RST) != 0U)
  1162. {
  1163. /* SRAM1_RST option byte should be modified */
  1164. assert_param(IS_OB_USER_SRAM1_RST(UserConfig2 & FLASH_OPTSR2_SRAM1_RST));
  1165. /* Set value and mask for SRAM1_RST option byte */
  1166. optr_reg2_val |= (UserConfig2 & FLASH_OPTSR2_SRAM1_RST);
  1167. optr_reg2_mask |= FLASH_OPTSR2_SRAM1_RST;
  1168. }
  1169. #endif /* FLASH_OPTSR2_SRAM1_RST */
  1170. if ((UserType & OB_USER_SRAM2_RST) != 0U)
  1171. {
  1172. /* SRAM2_RST option byte should be modified */
  1173. assert_param(IS_OB_USER_SRAM2_RST(UserConfig2 & FLASH_OPTSR2_SRAM2_RST));
  1174. /* Set value and mask for SRAM2_RST option byte */
  1175. optr_reg2_val |= (UserConfig2 & FLASH_OPTSR2_SRAM2_RST);
  1176. optr_reg2_mask |= FLASH_OPTSR2_SRAM2_RST;
  1177. }
  1178. if ((UserType & OB_USER_BKPRAM_ECC) != 0U)
  1179. {
  1180. /* BKPRAM_ECC option byte should be modified */
  1181. assert_param(IS_OB_USER_BKPRAM_ECC(UserConfig2 & FLASH_OPTSR2_BKPRAM_ECC));
  1182. /* Set value and mask for BKPRAM_ECC option byte */
  1183. optr_reg2_val |= (UserConfig2 & FLASH_OPTSR2_BKPRAM_ECC);
  1184. optr_reg2_mask |= FLASH_OPTSR2_BKPRAM_ECC;
  1185. }
  1186. #if defined (FLASH_OPTSR2_SRAM3_ECC)
  1187. if ((UserType & OB_USER_SRAM3_ECC) != 0U)
  1188. {
  1189. /* SRAM3_ECC option byte should be modified */
  1190. assert_param(IS_OB_USER_SRAM3_ECC(UserConfig2 & FLASH_OPTSR2_SRAM3_ECC));
  1191. /* Set value and mask for SRAM3_ECC option byte */
  1192. optr_reg2_val |= (UserConfig2 & FLASH_OPTSR2_SRAM3_ECC);
  1193. optr_reg2_mask |= FLASH_OPTSR2_SRAM3_ECC;
  1194. }
  1195. #endif /* FLASH_OPTSR2_SRAM3_ECC */
  1196. if ((UserType & OB_USER_SRAM2_ECC) != 0U)
  1197. {
  1198. /* SRAM2_ECC option byte should be modified */
  1199. assert_param(IS_OB_USER_SRAM2_ECC(UserConfig2 & FLASH_OPTSR2_SRAM2_ECC));
  1200. /* Set value and mask for SRAM2_ECC option byte */
  1201. optr_reg2_val |= (UserConfig2 & FLASH_OPTSR2_SRAM2_ECC);
  1202. optr_reg2_mask |= FLASH_OPTSR2_SRAM2_ECC;
  1203. }
  1204. #if defined (FLASH_OPTSR2_SRAM1_ECC)
  1205. if ((UserType & OB_USER_SRAM1_ECC) != 0U)
  1206. {
  1207. /* SRAM2_ECC option byte should be modified */
  1208. assert_param(IS_OB_USER_SRAM1_ECC(UserConfig2 & FLASH_OPTSR2_SRAM1_ECC));
  1209. /* Set value and mask for SRAM2_ECC option byte */
  1210. optr_reg2_val |= (UserConfig2 & FLASH_OPTSR2_SRAM1_ECC);
  1211. optr_reg2_mask |= FLASH_OPTSR2_SRAM1_ECC;
  1212. }
  1213. #endif /* FLASH_OPTSR2_SRAM1_ECC */
  1214. #if defined (FLASH_OPTSR2_USBPD_DIS)
  1215. if ((UserType & OB_USER_USBPD_DIS) != 0U)
  1216. {
  1217. /* USBPD_DIS option byte should be modified */
  1218. assert_param(IS_OB_USER_USBPD_DIS(UserConfig2 & FLASH_OPTSR2_USBPD_DIS));
  1219. /* Set value and mask for USBPD_DIS option byte */
  1220. optr_reg2_val |= (UserConfig2 & FLASH_OPTSR2_USBPD_DIS);
  1221. optr_reg2_mask |= FLASH_OPTSR2_USBPD_DIS;
  1222. }
  1223. #endif /* FLASH_OPTSR2_USBPD_DIS */
  1224. #if defined (FLASH_OPTSR2_TZEN)
  1225. if ((UserType & OB_USER_TZEN) != 0U)
  1226. {
  1227. /* TZEN option byte should be modified */
  1228. assert_param(IS_OB_USER_TZEN(UserConfig2 & FLASH_OPTSR2_TZEN));
  1229. /* Set value and mask for TZEN option byte */
  1230. optr_reg2_val |= (UserConfig2 & FLASH_OPTSR2_TZEN);
  1231. optr_reg2_mask |= FLASH_OPTSR2_TZEN;
  1232. }
  1233. #endif /* FLASH_OPTSR2_TZEN */
  1234. /* Check to write first User OB register or/and second one */
  1235. if ((UserType & 0xFFFU) != 0U)
  1236. {
  1237. /* Configure the option bytes register */
  1238. MODIFY_REG(FLASH->OPTSR_PRG, optr_reg1_mask, optr_reg1_val);
  1239. }
  1240. if ((UserType & 0xFF000U) != 0U)
  1241. {
  1242. /* Configure the option bytes register */
  1243. MODIFY_REG(FLASH->OPTSR2_PRG, optr_reg2_mask, optr_reg2_val);
  1244. }
  1245. }
  1246. /**
  1247. * @brief Return the FLASH User Option Byte values.
  1248. * @param UserConfig1 FLASH User Option Bytes values
  1249. * 2M: IWDG_SW(Bit3), WWDG_SW(Bit4), nRST_STOP(Bit 6), nRST_STDY(Bit 7),
  1250. * PRODUCT_STATE(Bit[8:15]), IO_VDD_HSLV(Bit 16), IO_VDDTO2_HSLV(Bit 17),
  1251. * IWDG_STOP(Bit 20), IWDG_STDBY (Bit 21), BOOT_UBE(Bit[22:29]) and SWAP_BANK(Bit 31).
  1252. * 128K: IWDG_SW(Bit3), WWDG_SW(Bit4), nRST_STOP(Bit 6), nRST_STDY(Bit 7),
  1253. * PRODUCT_STATE(Bit[8:15]), IO_VDD_HSLV(Bit16), IO_VDDIO2_HSLV(Bit17), IWDG_STOP(Bit 20),
  1254. * IWDG_STDBY (Bit 21) and SWAP_BANK(Bit 31).
  1255. * @param UserConfig2 FLASH User Option Bytes values
  1256. * 2M: SRAM1_3_RST(Bit2), SRAM2_RST(Bit 3), BKPRAM_ECC(Bit 4), SRAM3_ECC(Bit 5),
  1257. * SRAM2_ECC(Bit 6).
  1258. * 128K: SRAM2_RST(Bit 3), BKPRAM_ECC(Bit 4), SRAM2_ECC(Bit 6),
  1259. * SRAM1_RST(Bit9), SRAM1_ECC(Bit10).
  1260. * @retval None
  1261. */
  1262. static void FLASH_OB_GetUser(uint32_t *UserConfig1, uint32_t *UserConfig2)
  1263. {
  1264. (*UserConfig1) = FLASH->OPTSR_CUR & (~FLASH_OPTSR_PRODUCT_STATE);
  1265. (*UserConfig2) = FLASH->OPTSR2_CUR;
  1266. }
  1267. /**
  1268. * @brief Configure Boot address
  1269. * @param BootOption specifies the Boot address option byte to be programmed.
  1270. * This parameter can be one of the following values:
  1271. * @arg OB_BOOTADDR_NS: Non-secure boot address
  1272. * @arg OB_BOOTADDR_SEC: Secure boot address
  1273. * @param BootAddress: specifies the boot address value
  1274. * This parameter can be sector number between 0 and 0xFFFFFF00
  1275. * @retval None
  1276. */
  1277. static void FLASH_OB_BootAddrConfig(uint32_t BootOption, uint32_t BootAddress)
  1278. {
  1279. /* Check the parameters */
  1280. assert_param(IS_OB_BOOT_CONFIG(BootOption));
  1281. if (BootOption == OB_BOOT_NS)
  1282. {
  1283. MODIFY_REG(FLASH->NSBOOTR_PRG, FLASH_BOOTR_BOOTADD, BootAddress);
  1284. }
  1285. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  1286. else if (BootOption == OB_BOOT_SEC)
  1287. {
  1288. MODIFY_REG(FLASH->SECBOOTR_PRG, FLASH_BOOTR_BOOTADD, BootAddress);
  1289. }
  1290. #endif /* __ARM_FEATURE_CMSE */
  1291. else
  1292. {
  1293. /* Empty statement (to be compliant MISRA 15.7) */
  1294. }
  1295. }
  1296. /**
  1297. * @brief Configure the boot lock.
  1298. *
  1299. * @param BootOption select the BOOT_LOCK option: secure or non-secure.
  1300. * This parameter can be one of the following values:
  1301. * @arg OB_BOOT_LOCK_SEC: Boot Lock mode deactivated
  1302. * @arg OB_BOOT_LOCK_NS: Boot Lock mode activated
  1303. *
  1304. * @param BootLockConfig specifies the activation of the BOOT_LOCK.
  1305. * This parameter can be one of the following values:
  1306. * @arg OB_BOOT_LOCK_DISABLE: Boot Lock mode deactivated
  1307. * @arg OB_BOOT_LOCK_ENABLE: Boot Lock mode activated
  1308. *
  1309. * @retval None
  1310. */
  1311. static void FLASH_OB_BootLockConfig(uint32_t BootOption, uint32_t BootLockConfig)
  1312. {
  1313. /* Check the parameters */
  1314. assert_param(IS_OB_BOOT_CONFIG(BootOption));
  1315. assert_param(IS_OB_BOOT_LOCK(BootLockConfig));
  1316. /* Configure the option bytes register */
  1317. if (BootOption == OB_BOOT_NS)
  1318. {
  1319. MODIFY_REG(FLASH->NSBOOTR_PRG, FLASH_BOOTR_BOOT_LOCK, BootLockConfig);
  1320. }
  1321. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  1322. else if (BootOption == OB_BOOT_SEC)
  1323. {
  1324. MODIFY_REG(FLASH->SECBOOTR_PRG, FLASH_BOOTR_BOOT_LOCK, BootLockConfig);
  1325. }
  1326. #endif /* __ARM_FEATURE_CMSE */
  1327. else
  1328. {
  1329. /* Empty statement (to be compliant MISRA 15.7) */
  1330. }
  1331. }
  1332. /**
  1333. * @brief Get the boot configuration
  1334. * @param[in] BootOption specifies the boot address option byte to be returned.
  1335. * This parameter can be one of the following values:
  1336. * @arg OB_BOOT_NS: Non-secure boot address
  1337. * @arg OB_BOOT_SEC: Secure boot address
  1338. *
  1339. * @param[out] BootAddress specifies the boot address value
  1340. *
  1341. * @param[out] BootLockConfig returns the activation of the BOOT_LOCK.
  1342. * This parameter can be one of the following values:
  1343. * @arg OB_BOOT_LOCK_DISABLE: Boot Lock mode deactivated
  1344. * @arg OB_BOOT_LOCK_ENABLE: Boot Lock mode activated
  1345. * @retval None
  1346. */
  1347. static void FLASH_OB_GetBootConfig(uint32_t BootOption, uint32_t *BootAddress, uint32_t *BootLockConfig)
  1348. {
  1349. if (BootOption == OB_BOOT_NS)
  1350. {
  1351. *BootAddress = FLASH->NSBOOTR_CUR & FLASH_BOOTR_BOOTADD;
  1352. *BootLockConfig = FLASH->NSBOOTR_CUR & FLASH_BOOTR_BOOT_LOCK;
  1353. }
  1354. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  1355. else if (BootOption == OB_BOOT_SEC)
  1356. {
  1357. *BootAddress = (FLASH->SECBOOTR_CUR & FLASH_BOOTR_BOOTADD);
  1358. *BootLockConfig = (FLASH->SECBOOTR_CUR & FLASH_BOOTR_BOOT_LOCK);
  1359. }
  1360. #endif /* __ARM_FEATURE_CMSE */
  1361. else
  1362. {
  1363. /* Empty statement (to be compliant MISRA 15.7) */
  1364. }
  1365. }
  1366. /**
  1367. * @brief Configure the OTP Block Lock.
  1368. * @param OTP_Block specifies the OTP Block to lock.
  1369. * This parameter can be a value of @ref FLASH_OTP_Blocks
  1370. * @retval None
  1371. */
  1372. static void FLASH_OB_OTP_LockConfig(uint32_t OTP_Block)
  1373. {
  1374. /* Configure the OTP Block lock in the option bytes register */
  1375. FLASH->OTPBLR_PRG |= OTP_Block;
  1376. }
  1377. /**
  1378. * @brief Get the OTP Block Lock.
  1379. * @retval OTP_Block specifies the OTP Block to lock.
  1380. * This return value can be a value of @ref FLASH_OTP_Blocks
  1381. */
  1382. static uint32_t FLASH_OB_OTP_GetLock(void)
  1383. {
  1384. return (FLASH->OTPBLR_CUR);
  1385. }
  1386. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  1387. /**
  1388. * @brief Configure the watermark-based secure area.
  1389. *
  1390. * @param Banks specifies the bank where to apply Watermark protection
  1391. * This parameter can be one of the following values:
  1392. * @arg FLASH_BANK_1: configure Watermark on bank1
  1393. * @arg FLASH_BANK_2: configure Watermark on bank2
  1394. * @arg FLASH_BANK_BOTH: configure Watermark on both bank1 and bank2
  1395. *
  1396. * @param WMSecStartSector specifies the start sector of the secure area
  1397. * This parameter can be sector number between 0 and (max number of sectors in the bank - 1)
  1398. *
  1399. * @param WMSecEndSector specifies the end sector of the secure area
  1400. * This parameter can be sector number between WMSecStartSector and WMSecEndSector(max number of sectors
  1401. * in the bank - 1)
  1402. *
  1403. * @retval None
  1404. */
  1405. static void FLASH_OB_WMSECConfig(uint32_t Banks, uint32_t WMSecStartSector, uint32_t WMSecEndSector)
  1406. {
  1407. /* Check the parameters */
  1408. assert_param(IS_FLASH_BANK(Banks));
  1409. assert_param(IS_FLASH_SECTOR(WMSecStartSector));
  1410. assert_param(IS_FLASH_SECTOR(WMSecEndSector));
  1411. /* Write SECWM registers */
  1412. if ((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  1413. {
  1414. /* Configure Watermark Protection for bank 1 */
  1415. FLASH->SECWM1R_PRG = ((WMSecEndSector << FLASH_SECWMR_SECWM_END_Pos) | WMSecStartSector);
  1416. }
  1417. if ((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  1418. {
  1419. /* Configure Watermark Protection for bank 2 */
  1420. FLASH->SECWM2R_PRG = ((WMSecEndSector << FLASH_SECWMR_SECWM_END_Pos) | WMSecStartSector);
  1421. }
  1422. }
  1423. /**
  1424. * @brief Return the watermark-based secure area configuration.
  1425. *
  1426. * @param Bank [in] specifies the bank where to get the watermark protection.
  1427. * This parameter can be exclusively one of the following values:
  1428. * @arg FLASH_BANK_1: Get bank1 watermark configuration
  1429. * @arg FLASH_BANK_2: Get bank2 watermark configuration
  1430. *
  1431. * @param WMSecStartSector [out] specifies the start sector of the secure area
  1432. *
  1433. * @param WMSecEndSector [out] specifies the end sector of the secure area
  1434. *
  1435. * @retval None
  1436. */
  1437. static void FLASH_OB_GetWMSEC(uint32_t Bank, uint32_t *WMSecStartSector, uint32_t *WMSecEndSector)
  1438. {
  1439. uint32_t regvalue = 0U;
  1440. /* Read SECWM register */
  1441. if (Bank == FLASH_BANK_1)
  1442. {
  1443. regvalue = FLASH->SECWM1R_CUR;
  1444. }
  1445. if (Bank == FLASH_BANK_2)
  1446. {
  1447. regvalue = FLASH->SECWM2R_CUR;
  1448. }
  1449. /* Get configuration of secure area */
  1450. *WMSecStartSector = (regvalue & FLASH_SECWMR_SECWM_STRT);
  1451. *WMSecEndSector = ((regvalue & FLASH_SECWMR_SECWM_END) >> FLASH_SECWMR_SECWM_END_Pos);
  1452. }
  1453. #endif /* __ARM_FEATURE_CMSE */
  1454. /**
  1455. * @brief Configure the hide protection area.
  1456. *
  1457. * @param Banks specifies the bank where to apply hide protection
  1458. * This parameter can be one of the following values:
  1459. * @arg FLASH_BANK_1: configure HDP on bank1
  1460. * @arg FLASH_BANK_2: configure HDP on bank2
  1461. * @arg FLASH_BANK_BOTH: configure HDP on both bank1 and bank2
  1462. *
  1463. * @param HDPStartSector specifies the start sector of the hide protection area
  1464. * This parameter can be sector number between 0 and (max number of sectors in the bank - 1)
  1465. *
  1466. * @param HDPEndSector specifies the end sector of the hide protection area
  1467. * This parameter can be sector number between HDPStartSector and HDPEndSector (max number of sectors
  1468. * in the bank - 1)
  1469. *
  1470. * @retval None
  1471. */
  1472. static void FLASH_OB_HDPConfig(uint32_t Banks, uint32_t HDPStartSector, uint32_t HDPEndSector)
  1473. {
  1474. /* Check the parameters */
  1475. assert_param(IS_FLASH_BANK(Banks));
  1476. assert_param(IS_FLASH_SECTOR(HDPStartSector));
  1477. assert_param(IS_FLASH_SECTOR(HDPEndSector));
  1478. /* Write HDP registers */
  1479. if ((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  1480. {
  1481. /* Configure hide Protection for bank 1 */
  1482. FLASH->HDP1R_PRG = ((HDPEndSector << FLASH_HDPR_HDP_END_Pos) | HDPStartSector);
  1483. }
  1484. if ((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  1485. {
  1486. /* Configure hide Protection for bank 2 */
  1487. FLASH->HDP2R_PRG = ((HDPEndSector << FLASH_HDPR_HDP_END_Pos) | HDPStartSector);
  1488. }
  1489. }
  1490. /**
  1491. * @brief Return the hide protection area configuration.
  1492. *
  1493. * @param Bank [in] specifies the bank where to get the HDP protection.
  1494. * This parameter can be exclusively one of the following values:
  1495. * @arg FLASH_BANK_1: Get bank1 HDP configuration
  1496. * @arg FLASH_BANK_2: Get bank2 HDP configuration
  1497. *
  1498. * @param HDPStartSector [out] specifies the start sector of the HDP area
  1499. *
  1500. * @param HDPEndSector [out] specifies the end sector of the HDP area
  1501. *
  1502. * @retval None
  1503. */
  1504. static void FLASH_OB_GetHDP(uint32_t Bank, uint32_t *HDPStartSector, uint32_t *HDPEndSector)
  1505. {
  1506. uint32_t regvalue = 0U;
  1507. /* Read SECWM register */
  1508. if (Bank == FLASH_BANK_1)
  1509. {
  1510. regvalue = FLASH->HDP1R_CUR;
  1511. }
  1512. if (Bank == FLASH_BANK_2)
  1513. {
  1514. regvalue = FLASH->HDP2R_CUR;
  1515. }
  1516. /* Get configuration of HDP area */
  1517. *HDPStartSector = (regvalue & FLASH_HDPR_HDP_STRT);
  1518. *HDPEndSector = ((regvalue & FLASH_HDPR_HDP_END) >> FLASH_HDPR_HDP_END_Pos);
  1519. }
  1520. #if defined(FLASH_EDATAR_EDATA_EN)
  1521. /**
  1522. * @brief Configure the Flash high-cycle area.
  1523. *
  1524. * @param Banks specifies the bank where to apply Flash high-cycle data area
  1525. * This parameter can be one of the following values:
  1526. * @arg FLASH_BANK_1: configure Flash high-cycle area on bank1
  1527. * @arg FLASH_BANK_2: configure Flash high-cycle area on bank2
  1528. * @arg FLASH_BANK_BOTH: configure Flash high-cycle area on both bank1 and bank2
  1529. *
  1530. * @param EDATASize specifies the size (in sectors) of the Flash high-cycle data area
  1531. * This parameter can be sectors number between 0 and 8
  1532. * 0: Disable all EDATA sectors.
  1533. * 1: The last sector is reserved for flash high-cycle data.
  1534. * 2: The two last sectors are reserved for flash high-cycle data.
  1535. * 3: The three last sectors are reserved for flash high-cycle data
  1536. * 4: The four last sectors is reserved for flash high-cycle data.
  1537. * 5: The five last sectors are reserved for flash high-cycle data.
  1538. * 6: The six last sectors are reserved for flash high-cycle data.
  1539. * 7: The seven last sectors are reserved for flash high-cycle data.
  1540. * 8: The eight last sectors are reserved for flash high-cycle data.
  1541. *
  1542. * @retval None
  1543. */
  1544. static void FLASH_OB_EDATAConfig(uint32_t Banks, uint32_t EDATASize)
  1545. {
  1546. /* Check the parameters */
  1547. assert_param(IS_FLASH_BANK(Banks));
  1548. assert_param(IS_FLASH_EDATA_SIZE(EDATASize));
  1549. if (EDATASize != 0U)
  1550. {
  1551. /* Write EDATA registers */
  1552. if ((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  1553. {
  1554. /* Configure Flash high-cycle data for bank 1 */
  1555. FLASH->EDATA1R_PRG = (FLASH_EDATAR_EDATA_EN | (EDATASize - 1U));
  1556. }
  1557. if ((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  1558. {
  1559. /* Configure Flash high-cycle data for bank 2 */
  1560. FLASH->EDATA2R_PRG = (FLASH_EDATAR_EDATA_EN | (EDATASize - 1U));
  1561. }
  1562. }
  1563. else
  1564. {
  1565. /* Write EDATA registers */
  1566. if ((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  1567. {
  1568. /* Disable Flash high-cycle data for bank 1 */
  1569. FLASH->EDATA1R_PRG = 0U;
  1570. }
  1571. if ((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  1572. {
  1573. /* Disable Flash high-cycle data for bank 2 */
  1574. FLASH->EDATA2R_PRG = 0U;
  1575. }
  1576. }
  1577. }
  1578. /**
  1579. * @brief Return the Flash high-cycle data area configuration.
  1580. *
  1581. * @param Bank [in] specifies the bank where to get the Flash high-cycle data configuration.
  1582. * This parameter can be exclusively one of the following values:
  1583. * @arg FLASH_BANK_1: Get bank1 Flash high-cycle data configuration
  1584. * @arg FLASH_BANK_2: Get bank2 Flash high-cycle data configuration
  1585. *
  1586. * @param EDATASize [out] specifies the size (in sectors) of the Flash high-cycle data area
  1587. *
  1588. * @retval None
  1589. */
  1590. static void FLASH_OB_GetEDATA(uint32_t Bank, uint32_t *EDATASize)
  1591. {
  1592. uint32_t regvalue = 0U;
  1593. /* Read SECWM register */
  1594. if (Bank == FLASH_BANK_1)
  1595. {
  1596. regvalue = FLASH->EDATA1R_CUR;
  1597. }
  1598. if (Bank == FLASH_BANK_2)
  1599. {
  1600. regvalue = FLASH->EDATA2R_CUR;
  1601. }
  1602. /* Get configuration of secure area */
  1603. if ((regvalue & FLASH_EDATAR_EDATA_EN) != 0U)
  1604. {
  1605. /* Encoding of Edata Area size is register value + 1 */
  1606. *EDATASize = (regvalue & FLASH_EDATAR_EDATA_STRT) + 1U;
  1607. }
  1608. else
  1609. {
  1610. /* No defined Edata area */
  1611. *EDATASize = 0U;
  1612. }
  1613. }
  1614. #endif /* FLASH_EDATAR_EDATA_EN */
  1615. /**
  1616. * @}
  1617. */
  1618. /** @defgroup FLASHEx_Exported_Functions_Group3 Extended ECC operation functions
  1619. * @brief Extended ECC operation functions
  1620. *
  1621. @verbatim
  1622. ===============================================================================
  1623. ##### Extended ECC operation functions #####
  1624. ===============================================================================
  1625. [..]
  1626. This subsection provides a set of functions allowing to manage the Extended FLASH
  1627. ECC Operations.
  1628. @endverbatim
  1629. * @{
  1630. */
  1631. /**
  1632. * @brief Enable ECC correction interrupt
  1633. * @param None
  1634. * @retval None
  1635. */
  1636. void HAL_FLASHEx_EnableEccCorrectionInterrupt(void)
  1637. {
  1638. __HAL_FLASH_ENABLE_IT(FLASH_IT_ECCC);
  1639. }
  1640. /**
  1641. * @brief Disable ECC correction interrupt
  1642. * @param None
  1643. * @retval None
  1644. */
  1645. void HAL_FLASHEx_DisableEccCorrectionInterrupt(void)
  1646. {
  1647. __HAL_FLASH_DISABLE_IT(FLASH_IT_ECCC);
  1648. }
  1649. /**
  1650. * @brief Get the ECC error information.
  1651. * @param pData Pointer to an FLASH_EccInfoTypeDef structure that contains the
  1652. * ECC error information.
  1653. * @note This function should be called before ECC bit is cleared
  1654. * (in callback function)
  1655. * @retval None
  1656. */
  1657. void HAL_FLASHEx_GetEccInfo(FLASH_EccInfoTypeDef *pData)
  1658. {
  1659. uint32_t correction_reg = FLASH->ECCCORR;
  1660. uint32_t detection_reg = FLASH->ECCDETR;
  1661. uint32_t data_reg = FLASH->ECCDR;
  1662. uint32_t addr_reg = 0xFFFFFFFFU;
  1663. /* Check if the operation is a correction or a detection*/
  1664. if ((correction_reg & FLASH_ECCR_ECCC) != 0U)
  1665. {
  1666. /* Get area and offset address values from ECCCORR register*/
  1667. pData->Area = correction_reg & (~(FLASH_ECCR_ECCIE | FLASH_ECCR_ADDR_ECC | FLASH_ECCR_ECCC));
  1668. addr_reg = (correction_reg & FLASH_ECCR_ADDR_ECC);
  1669. }
  1670. else if ((detection_reg & FLASH_ECCR_ECCD) != 0U)
  1671. {
  1672. /* Get area and offset address values from ECCDETR register */
  1673. pData->Area = detection_reg & (~(FLASH_ECCR_ADDR_ECC | FLASH_ECCR_ECCD));
  1674. addr_reg = (detection_reg & FLASH_ECCR_ADDR_ECC);
  1675. }
  1676. else
  1677. {
  1678. /* Do nothing */
  1679. }
  1680. /* Check that an ECC single or double error has occurred to continue the calculation of area address */
  1681. if (addr_reg != 0xFFFFFFFFU)
  1682. {
  1683. /* Get address value according to area value*/
  1684. switch (pData->Area)
  1685. {
  1686. case FLASH_ECC_AREA_USER_BANK1:
  1687. /*
  1688. * One error detection/correction or two error detections per 128-bit flash word
  1689. * Therefore, the address returned by ECC registers in bank1 represents 128-bit flash word,
  1690. * to get the correct address value, we must do a shift by 4 bits
  1691. */
  1692. addr_reg = addr_reg << 4U;
  1693. pData->Address = FLASH_BASE + addr_reg;
  1694. break;
  1695. case FLASH_ECC_AREA_USER_BANK2:
  1696. /*
  1697. * One error detection/correction or two error detections per 128-bit flash word
  1698. * Therefore, the address returned by ECC registers in bank2 represents 128-bit flash word,
  1699. * to get the correct address value, we must do a shift by 4 bits
  1700. */
  1701. addr_reg = addr_reg << 4U;
  1702. pData->Address = FLASH_BASE + FLASH_BANK_SIZE + addr_reg;
  1703. break;
  1704. case FLASH_ECC_AREA_SYSTEM:
  1705. /* check system flash bank */
  1706. if ((correction_reg & FLASH_ECCR_BK_ECC) == FLASH_ECCR_BK_ECC)
  1707. {
  1708. pData->Address = FLASH_SYSTEM_BASE + FLASH_SYSTEM_SIZE + addr_reg;
  1709. }
  1710. else
  1711. {
  1712. pData->Address = FLASH_SYSTEM_BASE + addr_reg;
  1713. }
  1714. break;
  1715. #if defined (FLASH_SR_OBKERR)
  1716. case FLASH_ECC_AREA_OBK:
  1717. pData->Address = FLASH_OBK_BASE + addr_reg;
  1718. break;
  1719. #endif /* FLASH_SR_OBKERR */
  1720. #if defined (FLASH_EDATAR_EDATA_EN)
  1721. case FLASH_ECC_AREA_EDATA:
  1722. /* check flash high-cycle data bank */
  1723. if ((correction_reg & FLASH_ECCR_BK_ECC) == FLASH_ECCR_BK_ECC)
  1724. {
  1725. /*
  1726. * addr_reg is the address returned by the ECC register along with an offset value depends on area
  1727. * To calculate the exact address set by user while an ECC occurred, we must subtract the offset value,
  1728. * In addition, the address returned by ECC registers represents 128-bit flash word (multiply by 4),
  1729. */
  1730. pData->Address = FLASH_EDATA_BASE + FLASH_BANK_SIZE + ((addr_reg - FLASH_ADDRESS_OFFSET_EDATA) * 4U);
  1731. }
  1732. else
  1733. {
  1734. pData->Address = FLASH_EDATA_BASE + ((addr_reg - FLASH_ADDRESS_OFFSET_EDATA) * 4U);
  1735. }
  1736. break;
  1737. #endif /* FLASH_EDATAR_EDATA_EN */
  1738. case FLASH_ECC_AREA_OTP:
  1739. /* Address returned by the ECC is an halfword, multiply by 4 to get the exact address*/
  1740. pData->Address = FLASH_OTP_BASE + ((addr_reg - FLASH_ADDRESS_OFFSET_OTP) * 4U);
  1741. break;
  1742. default:
  1743. /* Do nothing */
  1744. break;
  1745. }
  1746. }
  1747. pData->Data = data_reg & FLASH_ECCR_ADDR_ECC;
  1748. }
  1749. /**
  1750. * @brief Handle Flash ECC Detection interrupt request.
  1751. * @retval None
  1752. */
  1753. void HAL_FLASHEx_ECCD_IRQHandler(void)
  1754. {
  1755. /* Check if the ECC double error occurred*/
  1756. if (READ_BIT(FLASH->ECCDETR, FLASH_ECCR_ECCD) != 0U)
  1757. {
  1758. /* FLASH ECC detection user callback */
  1759. HAL_FLASHEx_EccDetectionCallback();
  1760. /* Clear ECCD flag
  1761. note : this step will clear all the information related to the flash ecc detection
  1762. */
  1763. SET_BIT(FLASH->ECCDETR, FLASH_ECCR_ECCD);
  1764. }
  1765. }
  1766. /**
  1767. * @brief FLASH ECC Correction interrupt callback.
  1768. * @retval None
  1769. */
  1770. __weak void HAL_FLASHEx_EccCorrectionCallback(void)
  1771. {
  1772. /* NOTE : This function should not be modified, when the callback is needed,
  1773. the HAL_FLASHEx_EccCorrectionCallback could be implemented in the user file
  1774. */
  1775. }
  1776. /**
  1777. * @brief FLASH ECC Detection interrupt callback.
  1778. * @retval None
  1779. */
  1780. __weak void HAL_FLASHEx_EccDetectionCallback(void)
  1781. {
  1782. /* NOTE : This function should not be modified, when the callback is needed,
  1783. the HAL_FLASHEx_EccDetectionCallback could be implemented in the user file
  1784. */
  1785. }
  1786. /**
  1787. * @}
  1788. */
  1789. /**
  1790. * @}
  1791. */
  1792. #endif /* HAL_FLASH_MODULE_ENABLED */
  1793. /**
  1794. * @}
  1795. */
  1796. /**
  1797. * @}
  1798. */