stm32g0xx_hal_flash_ex.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_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 extended peripheral:
  8. * + Extended programming operations functions
  9. *
  10. @verbatim
  11. ==============================================================================
  12. ##### Flash Extended features #####
  13. ==============================================================================
  14. [..] Comparing to other previous devices, the FLASH interface for STM32G0xx
  15. devices contains the following additional features
  16. (+) Capacity up to 128 Kbytes with single bank architecture supporting read-while-write
  17. capability (RWW)
  18. (+) Single bank memory organization
  19. (+) PCROP protection
  20. ##### How to use this driver #####
  21. ==============================================================================
  22. [..] This driver provides functions to configure and program the FLASH memory
  23. of all STM32G0xx devices. It includes
  24. (#) Flash Memory Erase functions:
  25. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  26. HAL_FLASH_Lock() functions
  27. (++) Erase function: Erase page, erase all sectors
  28. (++) There are two modes of erase :
  29. (+++) Polling Mode using HAL_FLASHEx_Erase()
  30. (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
  31. (#) Option Bytes Programming function: Use HAL_FLASHEx_OBProgram() to :
  32. (++) Set/Reset the write protection
  33. (++) Set the Read protection Level
  34. (++) Program the user Option Bytes
  35. (++) Configure the PCROP protection
  36. (++) Set Securable memory area and boot entry point
  37. (#) Get Option Bytes Configuration function: Use HAL_FLASHEx_OBGetConfig() to :
  38. (++) Get the value of a write protection area
  39. (++) Know if the read protection is activated
  40. (++) Get the value of the user Option Bytes
  41. (++) Get Securable memory area and boot entry point information
  42. (#) Enable or disable debugger usage using HAL_FLASHEx_EnableDebugger and
  43. HAL_FLASHEx_DisableDebugger.
  44. (#) Check is flash content is empty or not using HAL_FLASHEx_FlashEmptyCheck.
  45. and modify this setting (for flash loader purpose e.g.) using
  46. HAL_FLASHEx_ForceFlashEmpty.
  47. (#) Enable securable memory area protectionusing HAL_FLASHEx_EnableSecMemProtection
  48. @endverbatim
  49. ******************************************************************************
  50. * @attention
  51. *
  52. * Copyright (c) 2018 STMicroelectronics.
  53. * All rights reserved.
  54. *
  55. * This software is licensed under terms that can be found in the LICENSE file in
  56. * the root directory of this software component.
  57. * If no LICENSE file comes with this software, it is provided AS-IS.
  58. ******************************************************************************
  59. */
  60. /* Includes ------------------------------------------------------------------*/
  61. #include "stm32g0xx_hal.h"
  62. /** @addtogroup STM32G0xx_HAL_Driver
  63. * @{
  64. */
  65. /** @defgroup FLASHEx FLASHEx
  66. * @brief FLASH Extended HAL module driver
  67. * @{
  68. */
  69. #ifdef HAL_FLASH_MODULE_ENABLED
  70. /* Private typedef -----------------------------------------------------------*/
  71. /* Private define ------------------------------------------------------------*/
  72. /* Private macro -------------------------------------------------------------*/
  73. /* Private variables ---------------------------------------------------------*/
  74. /* Private function prototypes -----------------------------------------------*/
  75. /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
  76. * @{
  77. */
  78. static void FLASH_MassErase(uint32_t Banks);
  79. void FLASH_FlushCaches(void);
  80. static void FLASH_OB_WRPConfig(uint32_t WRPArea, uint32_t WRPStartOffset, uint32_t WRDPEndOffset);
  81. static void FLASH_OB_GetWRP(uint32_t WRPArea, uint32_t *WRPStartOffset, uint32_t *WRDPEndOffset);
  82. static void FLASH_OB_OptrConfig(uint32_t UserType, uint32_t UserConfig, uint32_t RDPLevel);
  83. static uint32_t FLASH_OB_GetRDP(void);
  84. static uint32_t FLASH_OB_GetUser(void);
  85. #if defined(FLASH_PCROP_SUPPORT)
  86. static void FLASH_OB_PCROP1AConfig(uint32_t PCROPConfig, uint32_t PCROP1AStartAddr,
  87. uint32_t PCROP1AEndAddr);
  88. static void FLASH_OB_PCROP1BConfig(uint32_t PCROP1BStartAddr, uint32_t PCROP1BEndAddr);
  89. static void FLASH_OB_GetPCROP1A(uint32_t *PCROPConfig, uint32_t *PCROP1AStartAddr,
  90. uint32_t *PCROP1AEndAddr);
  91. static void FLASH_OB_GetPCROP1B(uint32_t *PCROP1BStartAddr, uint32_t *PCROP1BEndAddr);
  92. #if defined(FLASH_DBANK_SUPPORT)
  93. static void FLASH_OB_PCROP2AConfig(uint32_t PCROP2AStartAddr, uint32_t PCROP2AEndAddr);
  94. static void FLASH_OB_PCROP2BConfig(uint32_t PCROP2BStartAddr, uint32_t PCROP2BEndAddr);
  95. static void FLASH_OB_GetPCROP2A(uint32_t *PCROP2AStartAddr, uint32_t *PCROP2AEndAddr);
  96. static void FLASH_OB_GetPCROP2B(uint32_t *PCROP2BStartAddr, uint32_t *PCROP2BEndAddr);
  97. #endif /* FLASH_DBANK_SUPPORT */
  98. #endif /* FLASH_PCROP_SUPPORT */
  99. #if defined(FLASH_SECURABLE_MEMORY_SUPPORT)
  100. #if defined(FLASH_DBANK_SUPPORT)
  101. static void FLASH_OB_SecMemConfig(uint32_t BootEntry, uint32_t SecSize, uint32_t SecSize2);
  102. static void FLASH_OB_GetSecMem(uint32_t *BootEntry, uint32_t *SecSize, uint32_t *SecSize2);
  103. #else
  104. static void FLASH_OB_SecMemConfig(uint32_t BootEntry, uint32_t SecSize);
  105. static void FLASH_OB_GetSecMem(uint32_t *BootEntry, uint32_t *SecSize);
  106. #endif /* FLASH_DBANK_SUPPORT */
  107. #endif /* FLASH_SECURABLE_MEMORY_SUPPORT */
  108. /**
  109. * @}
  110. */
  111. /* Exported functions -------------------------------------------------------*/
  112. /** @defgroup FLASHEx_Exported_Functions FLASH Extended Exported Functions
  113. * @{
  114. */
  115. /** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
  116. * @brief Extended IO operation functions
  117. *
  118. @verbatim
  119. ===============================================================================
  120. ##### Extended programming operation functions #####
  121. ===============================================================================
  122. [..]
  123. This subsection provides a set of functions allowing to manage the Extended FLASH
  124. programming operations Operations.
  125. @endverbatim
  126. * @{
  127. */
  128. /**
  129. * @brief Perform a mass erase or erase the specified FLASH memory pages.
  130. * @param[in] pEraseInit Pointer to an @ref FLASH_EraseInitTypeDef structure that
  131. * contains the configuration information for the erasing.
  132. * @param[out] PageError Pointer to variable that contains the configuration
  133. * information on faulty page in case of error (0xFFFFFFFF means that all
  134. * the pages have been correctly erased)
  135. * @retval HAL Status
  136. */
  137. HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
  138. {
  139. HAL_StatusTypeDef status;
  140. uint32_t index;
  141. /* Check the parameters */
  142. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  143. /* Process Locked */
  144. __HAL_LOCK(&pFlash);
  145. /* Reset error code */
  146. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  147. /* Wait for last operation to be completed */
  148. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  149. if (status == HAL_OK)
  150. {
  151. #if !defined(FLASH_DBANK_SUPPORT)
  152. /* For single bank product force Banks to Bank 1 */
  153. pEraseInit->Banks = FLASH_BANK_1;
  154. #endif /* FLASH_DBANK_SUPPORT */
  155. if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASS)
  156. {
  157. /* Proceed to Mass Erase */
  158. FLASH_MassErase(pEraseInit->Banks);
  159. /* Wait for last operation to be completed */
  160. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  161. }
  162. else
  163. {
  164. /*Initialization of PageError variable*/
  165. *PageError = 0xFFFFFFFFU;
  166. for (index = pEraseInit->Page; index < (pEraseInit->Page + pEraseInit->NbPages); index++)
  167. {
  168. /* Start erase page */
  169. FLASH_PageErase(pEraseInit->Banks, index);
  170. /* Wait for last operation to be completed */
  171. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  172. if (status != HAL_OK)
  173. {
  174. /* In case of error, stop erase procedure and return the faulty address */
  175. *PageError = index;
  176. break;
  177. }
  178. }
  179. /* If operation is completed or interrupted, disable the Page Erase Bit */
  180. CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
  181. }
  182. }
  183. /* Process Unlocked */
  184. __HAL_UNLOCK(&pFlash);
  185. /* return status */
  186. return status;
  187. }
  188. /**
  189. * @brief Perform a mass erase or erase the specified FLASH memory pages with interrupt enabled.
  190. * @param pEraseInit Pointer to an @ref FLASH_EraseInitTypeDef structure that
  191. * contains the configuration information for the erasing.
  192. * @retval HAL Status
  193. */
  194. HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
  195. {
  196. HAL_StatusTypeDef status;
  197. /* Check the parameters */
  198. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  199. /* Process Locked */
  200. __HAL_LOCK(&pFlash);
  201. /* Reset error code */
  202. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  203. /* save procedure for interrupt treatment */
  204. pFlash.ProcedureOnGoing = pEraseInit->TypeErase;
  205. /* Wait for last operation to be completed */
  206. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  207. if (status != HAL_OK)
  208. {
  209. /* Process Unlocked */
  210. __HAL_UNLOCK(&pFlash);
  211. }
  212. else
  213. {
  214. #if !defined(FLASH_DBANK_SUPPORT)
  215. /* For single bank product force Banks to Bank 1 */
  216. pEraseInit->Banks = FLASH_BANK_1;
  217. #endif /* FLASH_DBANK_SUPPORT */
  218. /* Store Bank number */
  219. pFlash.Banks = pEraseInit->Banks;
  220. /* Enable End of Operation and Error interrupts */
  221. FLASH->CR |= FLASH_CR_EOPIE | FLASH_CR_ERRIE;
  222. if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASS)
  223. {
  224. /* Set Page to 0 for Interrupt callback management */
  225. pFlash.Page = 0;
  226. /* Proceed to Mass Erase */
  227. FLASH_MassErase(pEraseInit->Banks);
  228. }
  229. else
  230. {
  231. /* Erase by page to be done */
  232. pFlash.NbPagesToErase = pEraseInit->NbPages;
  233. pFlash.Page = pEraseInit->Page;
  234. /*Erase 1st page and wait for IT */
  235. FLASH_PageErase(pEraseInit->Banks, pEraseInit->Page);
  236. }
  237. }
  238. /* return status */
  239. return status;
  240. }
  241. /**
  242. * @brief Program Option bytes.
  243. * @param pOBInit Pointer to an @ref FLASH_OBProgramInitTypeDef structure that
  244. * contains the configuration information for the programming.
  245. * @note To configure any option bytes, the option lock bit OPTLOCK must be
  246. * cleared with the call of @ref HAL_FLASH_OB_Unlock() function.
  247. * @note New option bytes configuration will be taken into account only
  248. * - after an option bytes launch through the call of @ref HAL_FLASH_OB_Launch()
  249. * - a Power On Reset
  250. * - an exit from Standby or Shutdown mode.
  251. * @retval HAL Status
  252. */
  253. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
  254. {
  255. uint32_t optr;
  256. HAL_StatusTypeDef status;
  257. /* Check the parameters */
  258. assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
  259. /* Process Locked */
  260. __HAL_LOCK(&pFlash);
  261. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  262. /* Write protection configuration */
  263. if ((pOBInit->OptionType & OPTIONBYTE_WRP) != 0x00U)
  264. {
  265. /* Configure of Write protection on the selected area */
  266. FLASH_OB_WRPConfig(pOBInit->WRPArea, pOBInit->WRPStartOffset, pOBInit->WRPEndOffset);
  267. }
  268. /* Option register */
  269. if ((pOBInit->OptionType & (OPTIONBYTE_RDP | OPTIONBYTE_USER)) == (OPTIONBYTE_RDP | OPTIONBYTE_USER))
  270. {
  271. /* Fully modify OPTR register with RDP & user data */
  272. FLASH_OB_OptrConfig(pOBInit->USERType, pOBInit->USERConfig, pOBInit->RDPLevel);
  273. }
  274. else if ((pOBInit->OptionType & OPTIONBYTE_RDP) != 0x00U)
  275. {
  276. /* Only modify RDP so get current user data */
  277. optr = FLASH_OB_GetUser();
  278. FLASH_OB_OptrConfig(optr, optr, pOBInit->RDPLevel);
  279. }
  280. else if ((pOBInit->OptionType & OPTIONBYTE_USER) != 0x00U)
  281. {
  282. /* Only modify user so get current RDP level */
  283. optr = FLASH_OB_GetRDP();
  284. FLASH_OB_OptrConfig(pOBInit->USERType, pOBInit->USERConfig, optr);
  285. }
  286. else
  287. {
  288. /* nothing to do */
  289. }
  290. #if defined(FLASH_PCROP_SUPPORT)
  291. /* PCROP Configuration */
  292. if ((pOBInit->OptionType & OPTIONBYTE_PCROP) != 0x00U)
  293. {
  294. /* Check the parameters */
  295. assert_param(IS_OB_PCROP_CONFIG(pOBInit->PCROPConfig));
  296. if ((pOBInit->PCROPConfig & (OB_PCROP_ZONE_A | OB_PCROP_RDP_ERASE)) != 0x00U)
  297. {
  298. /* Configure the 1A Proprietary code readout protection */
  299. FLASH_OB_PCROP1AConfig(pOBInit->PCROPConfig, pOBInit->PCROP1AStartAddr, pOBInit->PCROP1AEndAddr);
  300. }
  301. if ((pOBInit->PCROPConfig & OB_PCROP_ZONE_B) != 0x00U)
  302. {
  303. /* Configure the 1B Proprietary code readout protection */
  304. FLASH_OB_PCROP1BConfig(pOBInit->PCROP1BStartAddr, pOBInit->PCROP1BEndAddr);
  305. }
  306. #if defined(FLASH_DBANK_SUPPORT)
  307. if ((pOBInit->PCROPConfig & OB_PCROP_ZONE2_A) != 0x00U)
  308. {
  309. /* Configure the 2A Proprietary code readout protection */
  310. FLASH_OB_PCROP2AConfig(pOBInit->PCROP2AStartAddr, pOBInit->PCROP2AEndAddr);
  311. }
  312. if ((pOBInit->PCROPConfig & OB_PCROP_ZONE2_B) != 0x00U)
  313. {
  314. /* Configure the 2B Proprietary code readout protection */
  315. FLASH_OB_PCROP2BConfig(pOBInit->PCROP2BStartAddr, pOBInit->PCROP2BEndAddr);
  316. }
  317. #endif /* FLASH_DBANK_SUPPORT */
  318. }
  319. #endif /* FLASH_PCROP_SUPPORT */
  320. #if defined(FLASH_SECURABLE_MEMORY_SUPPORT)
  321. /* Securable Memory Area Configuration */
  322. if ((pOBInit->OptionType & OPTIONBYTE_SEC) != 0x00U)
  323. {
  324. #if defined(FLASH_DBANK_SUPPORT)
  325. /* Configure the securable memory area protection */
  326. FLASH_OB_SecMemConfig(pOBInit->BootEntryPoint, pOBInit->SecSize, pOBInit->SecSize2);
  327. #else
  328. /* Configure the securable memory area protection */
  329. FLASH_OB_SecMemConfig(pOBInit->BootEntryPoint, pOBInit->SecSize);
  330. #endif /* FLASH_DBANK_SUPPORT */
  331. }
  332. #endif /* FLASH_SECURABLE_MEMORY_SUPPORT */
  333. /* Wait for last operation to be completed */
  334. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  335. if (status == HAL_OK)
  336. {
  337. /* Set OPTSTRT Bit */
  338. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  339. /* Wait for last operation to be completed */
  340. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  341. /* If the option byte program operation is completed, disable the OPTSTRT Bit */
  342. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  343. }
  344. /* Process Unlocked */
  345. __HAL_UNLOCK(&pFlash);
  346. /* return status */
  347. return status;
  348. }
  349. /**
  350. * @brief Get the Option bytes configuration.
  351. * @note warning: this API only read flash register, it does not reflect any
  352. * change that would have been programmed between previous Option byte
  353. * loading and current call.
  354. * @param pOBInit Pointer to an @ref FLASH_OBProgramInitTypeDef structure that contains the
  355. * configuration information. The fields pOBInit->WRPArea should
  356. * indicate which area is requested for the WRP.
  357. * @retval None
  358. */
  359. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
  360. {
  361. pOBInit->OptionType = OPTIONBYTE_ALL;
  362. /* Get write protection on the selected area */
  363. FLASH_OB_GetWRP(pOBInit->WRPArea, &(pOBInit->WRPStartOffset), &(pOBInit->WRPEndOffset));
  364. /* Get Read protection level */
  365. pOBInit->RDPLevel = FLASH_OB_GetRDP();
  366. /* Get the user option bytes */
  367. pOBInit->USERConfig = FLASH_OB_GetUser();
  368. pOBInit->USERType = OB_USER_ALL;
  369. #if defined(FLASH_PCROP_SUPPORT)
  370. /* Get the Proprietary code readout protection */
  371. FLASH_OB_GetPCROP1A(&(pOBInit->PCROPConfig), &(pOBInit->PCROP1AStartAddr), &(pOBInit->PCROP1AEndAddr));
  372. FLASH_OB_GetPCROP1B(&(pOBInit->PCROP1BStartAddr), &(pOBInit->PCROP1BEndAddr));
  373. pOBInit->PCROPConfig |= (OB_PCROP_ZONE_A | OB_PCROP_ZONE_B);
  374. #if defined(FLASH_DBANK_SUPPORT)
  375. FLASH_OB_GetPCROP2A(&(pOBInit->PCROP2AStartAddr), &(pOBInit->PCROP2AEndAddr));
  376. FLASH_OB_GetPCROP2B(&(pOBInit->PCROP2BStartAddr), &(pOBInit->PCROP2BEndAddr));
  377. pOBInit->PCROPConfig |= (OB_PCROP_ZONE2_A | OB_PCROP_ZONE2_B);
  378. #endif /* FLASH_DBANK_SUPPORT */
  379. #endif /* FLASH_PCROP_SUPPORT */
  380. #if defined(FLASH_SECURABLE_MEMORY_SUPPORT)
  381. #if defined(FLASH_DBANK_SUPPORT)
  382. /* Get the Securable Memory Area protection */
  383. FLASH_OB_GetSecMem(&(pOBInit->BootEntryPoint), &(pOBInit->SecSize), &(pOBInit->SecSize2));
  384. #else
  385. /* Get the Securable Memory Area protection */
  386. FLASH_OB_GetSecMem(&(pOBInit->BootEntryPoint), &(pOBInit->SecSize));
  387. #endif /* FLASH_DBANK_SUPPORT */
  388. #endif /* FLASH_SECURABLE_MEMORY_SUPPORT */
  389. }
  390. #if defined(FLASH_ACR_DBG_SWEN)
  391. /**
  392. * @brief Enable Debugger.
  393. * @note After calling this API, flash interface allow debugger intrusion.
  394. * @retval None
  395. */
  396. void HAL_FLASHEx_EnableDebugger(void)
  397. {
  398. FLASH->ACR |= FLASH_ACR_DBG_SWEN;
  399. }
  400. /**
  401. * @brief Disable Debugger.
  402. * @note After calling this API, Debugger is disabled: it is no more possible to
  403. * break, see CPU register, etc...
  404. * @retval None
  405. */
  406. void HAL_FLASHEx_DisableDebugger(void)
  407. {
  408. FLASH->ACR &= ~FLASH_ACR_DBG_SWEN;
  409. }
  410. #endif /* FLASH_ACR_DBG_SWEN */
  411. /**
  412. * @brief Flash Empty check
  413. * @note This API checks if first location in Flash is programmed or not.
  414. * This check is done once by Option Byte Loader.
  415. * @retval 0 if 1st location is not programmed else
  416. */
  417. uint32_t HAL_FLASHEx_FlashEmptyCheck(void)
  418. {
  419. return ((FLASH->ACR & FLASH_ACR_PROGEMPTY));
  420. }
  421. /**
  422. * @brief Force Empty check value.
  423. * @note Allows to modify program empty check value in order to force this
  424. * infrmation in Flash Interface, for all next reset that do not launch
  425. * Option Byte Loader.
  426. * @param FlashEmpty this parameter can be a value of @ref FLASHEx_Empty_Check
  427. * @retval None
  428. */
  429. void HAL_FLASHEx_ForceFlashEmpty(uint32_t FlashEmpty)
  430. {
  431. uint32_t acr;
  432. assert_param(IS_FLASH_EMPTY_CHECK(FlashEmpty));
  433. acr = (FLASH->ACR & ~FLASH_ACR_PROGEMPTY);
  434. FLASH->ACR = (acr | FlashEmpty);
  435. }
  436. #if defined(FLASH_SECURABLE_MEMORY_SUPPORT)
  437. /**
  438. * @brief Securable memory area protection enable
  439. * @param Banks Select Bank to be secured.
  440. * This parameter can be a value of @ref FLASH_Banks
  441. * @note On some devices, there is only 1 bank so parameter has to be set FLASH_BANK_1.
  442. * @note This API locks Securable memory area which is defined in SEC_SIZE option byte
  443. * (that can be retrieved calling HAL_FLASHEx_OBGetConfig API and checking
  444. * Secsize).
  445. * @note SEC_PROT bit can only be set, it will be reset by system reset.
  446. * @retval None
  447. */
  448. void HAL_FLASHEx_EnableSecMemProtection(uint32_t Banks)
  449. {
  450. #if defined(FLASH_DBANK_SUPPORT)
  451. assert_param(IS_FLASH_BANK(Banks));
  452. if (Banks == (FLASH_BANK_2 | FLASH_BANK_1))
  453. {
  454. FLASH->CR |= (FLASH_CR_SEC_PROT2 | FLASH_CR_SEC_PROT);
  455. }
  456. else if (Banks == FLASH_BANK_2)
  457. {
  458. FLASH->CR |= FLASH_CR_SEC_PROT2;
  459. }
  460. else
  461. #else
  462. UNUSED(Banks);
  463. #endif /* FLASH_DBANK_SUPPORT */
  464. {
  465. FLASH->CR |= FLASH_CR_SEC_PROT;
  466. }
  467. }
  468. #endif /* FLASH_SECURABLE_MEMORY_SUPPORT */
  469. /**
  470. * @}
  471. */
  472. /**
  473. * @}
  474. */
  475. /* Private functions ---------------------------------------------------------*/
  476. /** @addtogroup FLASHEx_Private_Functions
  477. * @{
  478. */
  479. /**
  480. * @brief Mass erase of FLASH memory.
  481. * @param Banks: Banks to be erased
  482. * This parameter can be a combination of the following values:
  483. * @arg FLASH_BANK_1: Bank1 to be erased
  484. * @arg FLASH_BANK_2: Bank2 to be erased*
  485. * @note (*) availability depends on devices
  486. * @retval None
  487. */
  488. static void FLASH_MassErase(uint32_t Banks)
  489. {
  490. /* Check the parameters */
  491. assert_param(IS_FLASH_BANK(Banks));
  492. /* Set the Mass Erase Bit and start bit */
  493. FLASH->CR |= (FLASH_CR_STRT | Banks);
  494. }
  495. /**
  496. * @brief Erase the specified FLASH memory page.
  497. * @param Banks: Banks to be erased
  498. * This parameter can one of the following values:
  499. * @arg FLASH_BANK_1: Bank1 to be erased
  500. * @arg FLASH_BANK_2: Bank2 to be erased*
  501. * @param Page FLASH page to erase
  502. * This parameter must be a value between 0 and (max number of pages in Flash - 1)
  503. * @note (*) availability depends on devices
  504. * @retval None
  505. */
  506. void FLASH_PageErase(uint32_t Banks, uint32_t Page)
  507. {
  508. uint32_t tmp;
  509. /* Check the parameters */
  510. assert_param(IS_FLASH_BANK(Banks));
  511. assert_param(IS_FLASH_PAGE(Page));
  512. /* Get configuration register, then clear page number */
  513. tmp = (FLASH->CR & ~FLASH_CR_PNB);
  514. #if defined(FLASH_DBANK_SUPPORT)
  515. /* Check if page has to be erased in bank 1 or 2 */
  516. if (Banks != FLASH_BANK_1)
  517. {
  518. tmp |= FLASH_CR_BKER;
  519. }
  520. else
  521. {
  522. tmp &= ~FLASH_CR_BKER;
  523. }
  524. #else
  525. /* Prevent unused argument(s) compilation warning */
  526. UNUSED(Banks);
  527. #endif /* FLASH_DBANK_SUPPORT */
  528. /* Set page number, Page Erase bit & Start bit */
  529. FLASH->CR = (tmp | (FLASH_CR_STRT | (Page << FLASH_CR_PNB_Pos) | FLASH_CR_PER));
  530. }
  531. /**
  532. * @brief Flush the instruction cache.
  533. * @retval None
  534. */
  535. void FLASH_FlushCaches(void)
  536. {
  537. /* Flush instruction cache */
  538. if (READ_BIT(FLASH->ACR, FLASH_ACR_ICEN) != 0U)
  539. {
  540. /* Disable instruction cache */
  541. __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
  542. /* Reset instruction cache */
  543. __HAL_FLASH_INSTRUCTION_CACHE_RESET();
  544. /* Enable instruction cache */
  545. __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
  546. }
  547. }
  548. /**
  549. * @brief Configure the write protection of the desired pages.
  550. * @note When WRP is active in a zone, it cannot be erased or programmed.
  551. * Consequently, a software mass erase cannot be performed if one zone
  552. * is write-protected.
  553. * @note When the memory read protection level is selected (RDP level = 1),
  554. * it is not possible to program or erase Flash memory if the CPU debug
  555. * features are connected (JTAG or single wire) or boot code is being
  556. * executed from RAM or System flash, even if WRP is not activated.
  557. * @param WRPArea Specifies the area to be configured.
  558. * This parameter can be one of the following values:
  559. * @arg @ref OB_WRPAREA_ZONE_A Flash Zone A
  560. * @arg @ref OB_WRPAREA_ZONE_B Flash Zone B
  561. * @arg @ref OB_WRPAREA_ZONE2_A Flash Bank 2 Zone A (*)
  562. * @arg @ref OB_WRPAREA_ZONE2_B Flash Bank 2 Zone B (*)
  563. * @note (*) availability depends on devices
  564. * @param WRPStartOffset Specifies the start page of the write protected area
  565. * This parameter can be page number between 0 and (max number of pages in the Flash Bank - 1)
  566. * @param WRDPEndOffset Specifies the end page of the write protected area
  567. * This parameter can be page number between WRPStartOffset and (max number of pages in the Flash Bank - 1)
  568. * @retval None
  569. */
  570. static void FLASH_OB_WRPConfig(uint32_t WRPArea, uint32_t WRPStartOffset, uint32_t WRDPEndOffset)
  571. {
  572. /* Check the parameters */
  573. assert_param(IS_OB_WRPAREA(WRPArea));
  574. assert_param(IS_FLASH_PAGE(WRPStartOffset));
  575. assert_param(IS_FLASH_PAGE(WRDPEndOffset));
  576. /* Configure the write protected area */
  577. if (WRPArea == OB_WRPAREA_ZONE_A)
  578. {
  579. FLASH->WRP1AR = ((WRDPEndOffset << FLASH_WRP1AR_WRP1A_END_Pos) | WRPStartOffset);
  580. }
  581. #if defined(FLASH_DBANK_SUPPORT)
  582. else if (WRPArea == OB_WRPAREA_ZONE2_A)
  583. {
  584. FLASH->WRP2AR = ((WRDPEndOffset << FLASH_WRP2AR_WRP2A_END_Pos) | WRPStartOffset);
  585. }
  586. else if (WRPArea == OB_WRPAREA_ZONE2_B)
  587. {
  588. FLASH->WRP2BR = ((WRDPEndOffset << FLASH_WRP2BR_WRP2B_END_Pos) | WRPStartOffset);
  589. }
  590. #endif /* FLASH_DBANK_SUPPORT */
  591. else
  592. {
  593. FLASH->WRP1BR = ((WRDPEndOffset << FLASH_WRP1BR_WRP1B_END_Pos) | WRPStartOffset);
  594. }
  595. }
  596. /**
  597. * @brief Return the FLASH Write Protection Option Bytes value.
  598. * @param[in] WRPArea Specifies the area to be returned.
  599. * This parameter can be one of the following values:
  600. * @arg @ref OB_WRPAREA_ZONE_A Flash Zone A
  601. * @arg @ref OB_WRPAREA_ZONE_B Flash Zone B
  602. * @arg @ref OB_WRPAREA_ZONE2_A Flash Bank 2 Zone A (*)
  603. * @arg @ref OB_WRPAREA_ZONE2_B Flash Bank 2 Zone B (*)
  604. * @note (*) availability depends on devices
  605. * @param[out] WRPStartOffset Specifies the address where to copied the start page
  606. * of the write protected area
  607. * @param[out] WRDPEndOffset Dpecifies the address where to copied the end page of
  608. * the write protected area
  609. * @retval None
  610. */
  611. static void FLASH_OB_GetWRP(uint32_t WRPArea, uint32_t *WRPStartOffset, uint32_t *WRDPEndOffset)
  612. {
  613. /* Check the parameters */
  614. assert_param(IS_OB_WRPAREA(WRPArea));
  615. /* Get the configuration of the write protected area */
  616. if (WRPArea == OB_WRPAREA_ZONE_A)
  617. {
  618. *WRPStartOffset = READ_BIT(FLASH->WRP1AR, FLASH_WRP1AR_WRP1A_STRT);
  619. *WRDPEndOffset = (READ_BIT(FLASH->WRP1AR, FLASH_WRP1AR_WRP1A_END) >> FLASH_WRP1AR_WRP1A_END_Pos);
  620. }
  621. #if defined(FLASH_DBANK_SUPPORT)
  622. else if (WRPArea == OB_WRPAREA_ZONE2_A)
  623. {
  624. *WRPStartOffset = READ_BIT(FLASH->WRP2AR, FLASH_WRP2AR_WRP2A_STRT);
  625. *WRDPEndOffset = (READ_BIT(FLASH->WRP2AR, FLASH_WRP2AR_WRP2A_END) >> FLASH_WRP2AR_WRP2A_END_Pos);
  626. }
  627. else if (WRPArea == OB_WRPAREA_ZONE2_B)
  628. {
  629. *WRPStartOffset = READ_BIT(FLASH->WRP2BR, FLASH_WRP2BR_WRP2B_STRT);
  630. *WRDPEndOffset = (READ_BIT(FLASH->WRP2BR, FLASH_WRP2BR_WRP2B_END) >> FLASH_WRP2BR_WRP2B_END_Pos);
  631. }
  632. #endif /* FLASH_DBANK_SUPPORT */
  633. else
  634. {
  635. *WRPStartOffset = READ_BIT(FLASH->WRP1BR, FLASH_WRP1BR_WRP1B_STRT);
  636. *WRDPEndOffset = (READ_BIT(FLASH->WRP1BR, FLASH_WRP1BR_WRP1B_END) >> FLASH_WRP1BR_WRP1B_END_Pos);
  637. }
  638. }
  639. /**
  640. * @brief Set user & RDP configuration
  641. * @note !!! Warning : When enabling OB_RDP level 2 it is no more possible
  642. * to go back to level 1 or 0 !!!
  643. * @param UserType The FLASH User Option Bytes to be modified.
  644. * This parameter can be a combination of @ref FLASH_OB_USER_Type
  645. * @param UserConfig The FLASH User Option Bytes values.
  646. * This parameter can be a combination of:
  647. * @arg @ref FLASH_OB_USER_BOR_ENABLE(*)
  648. * @arg @ref FLASH_OB_USER_BOR_LEVEL(*)
  649. * @arg @ref FLASH_OB_USER_RESET_CONFIG(*)
  650. * @arg @ref FLASH_OB_USER_nRST_STOP
  651. * @arg @ref FLASH_OB_USER_nRST_STANDBY
  652. * @arg @ref FLASH_OB_USER_nRST_SHUTDOWN(*)
  653. * @arg @ref FLASH_OB_USER_IWDG_SW
  654. * @arg @ref FLASH_OB_USER_IWDG_STOP
  655. * @arg @ref FLASH_OB_USER_IWDG_STANDBY
  656. * @arg @ref FLASH_OB_USER_WWDG_SW
  657. * @arg @ref FLASH_OB_USER_SRAM_PARITY
  658. * @arg @ref FLASH_OB_USER_BANK_SWAP(*)
  659. * @arg @ref FLASH_OB_USER_DUAL_BANK(*)
  660. * @arg @ref FLASH_OB_USER_nBOOT_SEL
  661. * @arg @ref FLASH_OB_USER_nBOOT1
  662. * @arg @ref FLASH_OB_USER_nBOOT0
  663. * @arg @ref FLASH_OB_USER_INPUT_RESET_HOLDER(*)
  664. * @param RDPLevel specifies the read protection level.
  665. * This parameter can be one of the following values:
  666. * @arg @ref OB_RDP_LEVEL_0 No protection
  667. * @arg @ref OB_RDP_LEVEL_1 Memory Read protection
  668. * @arg @ref OB_RDP_LEVEL_2 Full chip protection
  669. * @note (*) availability depends on devices
  670. * @retval None
  671. */
  672. static void FLASH_OB_OptrConfig(uint32_t UserType, uint32_t UserConfig, uint32_t RDPLevel)
  673. {
  674. uint32_t optr;
  675. /* Check the parameters */
  676. assert_param(IS_OB_USER_TYPE(UserType));
  677. assert_param(IS_OB_USER_CONFIG(UserType, UserConfig));
  678. assert_param(IS_OB_RDP_LEVEL(RDPLevel));
  679. /* Configure the RDP level in the option bytes register */
  680. optr = FLASH->OPTR;
  681. optr &= ~(UserType | FLASH_OPTR_RDP);
  682. FLASH->OPTR = (optr | UserConfig | RDPLevel);
  683. }
  684. /**
  685. * @brief Return the FLASH Read Protection level.
  686. * @retval FLASH ReadOut Protection Status:
  687. * This return value can be one of the following values:
  688. * @arg @ref OB_RDP_LEVEL_0 No protection
  689. * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
  690. * @arg @ref OB_RDP_LEVEL_2 Full chip protection
  691. */
  692. static uint32_t FLASH_OB_GetRDP(void)
  693. {
  694. uint32_t rdplvl = READ_BIT(FLASH->OPTR, FLASH_OPTR_RDP);
  695. if ((rdplvl != OB_RDP_LEVEL_0) && (rdplvl != OB_RDP_LEVEL_2))
  696. {
  697. return (OB_RDP_LEVEL_1);
  698. }
  699. else
  700. {
  701. return rdplvl;
  702. }
  703. }
  704. /**
  705. * @brief Return the FLASH User Option Byte value.
  706. * @retval The FLASH User Option Bytes values. It will be a combination of all the following values:
  707. * @arg @ref FLASH_OB_USER_BOR_ENABLE(*)
  708. * @arg @ref FLASH_OB_USER_BOR_LEVEL(*)
  709. * @arg @ref FLASH_OB_USER_RESET_CONFIG(*)
  710. * @arg @ref FLASH_OB_USER_nRST_STOP
  711. * @arg @ref FLASH_OB_USER_nRST_STANDBY
  712. * @arg @ref FLASH_OB_USER_nRST_SHUTDOWN(*)
  713. * @arg @ref FLASH_OB_USER_IWDG_SW
  714. * @arg @ref FLASH_OB_USER_IWDG_STOP
  715. * @arg @ref FLASH_OB_USER_IWDG_STANDBY
  716. * @arg @ref FLASH_OB_USER_WWDG_SW
  717. * @arg @ref FLASH_OB_USER_SRAM_PARITY
  718. * @arg @ref FLASH_OB_USER_BANK_SWAP(*)
  719. * @arg @ref FLASH_OB_USER_DUAL_BANK(*)
  720. * @arg @ref FLASH_OB_USER_nBOOT_SEL
  721. * @arg @ref FLASH_OB_USER_nBOOT1
  722. * @arg @ref FLASH_OB_USER_nBOOT0
  723. * @arg @ref FLASH_OB_USER_INPUT_RESET_HOLDER(*)
  724. * @note (*) availability depends on devices
  725. */
  726. static uint32_t FLASH_OB_GetUser(void)
  727. {
  728. uint32_t user = ((FLASH->OPTR & ~FLASH_OPTR_RDP) & OB_USER_ALL);
  729. return user;
  730. }
  731. #if defined(FLASH_PCROP_SUPPORT)
  732. /**
  733. * @brief Configure the 1A Proprietary code readout protection & erase configuration on RDP regression.
  734. * @note It is recommended to align PCROP zone with page granularity when using PCROP_RDP or avoid
  735. * having some executable code in a page where PCROP zone starts or ends.
  736. * @note Minimum PCROP area size is 2 times the chosen granularity: PCROPA_STRT and PCROPA_END.
  737. * So if the requirement is to be able to read-protect 1KB areas, the ROP granularity
  738. * has to be set to 512 Bytes
  739. * @param PCROPConfig specifies the erase configuration (OB_PCROP_RDP_NOT_ERASE or OB_PCROP_RDP_ERASE)
  740. * on RDP level 1 regression.
  741. * @param PCROP1AStartAddr Specifies the Zone 1A Start address of the Proprietary code readout protection
  742. * This parameter can be an address between begin and end of the flash
  743. * @param PCROP1AEndAddr Specifies the Zone 1A end address of the Proprietary code readout protection
  744. * This parameter can be an address between PCROP1AStartAddr and end of the flash
  745. * @retval None
  746. */
  747. static void FLASH_OB_PCROP1AConfig(uint32_t PCROPConfig, uint32_t PCROP1AStartAddr, uint32_t PCROP1AEndAddr)
  748. {
  749. uint32_t startoffset;
  750. uint32_t endoffset;
  751. uint32_t pcrop1aend;
  752. uint32_t ropbase;
  753. /* Check the parameters */
  754. assert_param(IS_OB_PCROP_CONFIG(PCROPConfig));
  755. #if defined(FLASH_DBANK_SUPPORT)
  756. /* Check if banks are swapped (valid if only one bank) */
  757. if (((FLASH->OPTR & FLASH_OPTR_nSWAP_BANK) != FLASH_OPTR_nSWAP_BANK) && (FLASH_BANK_NB == 2U))
  758. {
  759. /* Check the parameters */
  760. assert_param(IS_FLASH_MAIN_SECONDHALF_MEM_ADDRESS(PCROP1AStartAddr));
  761. assert_param(IS_FLASH_MAIN_SECONDHALF_MEM_ADDRESS(PCROP1AEndAddr));
  762. /* Bank swap, bank 1 read only protection is on second half of Flash */
  763. ropbase = (FLASH_BASE + FLASH_BANK_SIZE);
  764. }
  765. else
  766. #endif /* FLASH_DBANK_SUPPORT */
  767. {
  768. /* Check the parameters */
  769. assert_param(IS_FLASH_MAIN_FIRSTHALF_MEM_ADDRESS(PCROP1AStartAddr));
  770. assert_param(IS_FLASH_MAIN_FIRSTHALF_MEM_ADDRESS(PCROP1AEndAddr));
  771. /* No Bank swap, bank 1 read only protection is on first half of Flash */
  772. ropbase = FLASH_BASE;
  773. }
  774. /* get pcrop 1A end register */
  775. pcrop1aend = FLASH->PCROP1AER;
  776. /* Configure the Proprietary code readout protection offset */
  777. if ((PCROPConfig & OB_PCROP_ZONE_A) != 0x00U)
  778. {
  779. /* Compute offset depending on pcrop granularity */
  780. startoffset = ((PCROP1AStartAddr - ropbase) >> FLASH_PCROP_GRANULARITY_OFFSET);
  781. endoffset = ((PCROP1AEndAddr - ropbase) >> FLASH_PCROP_GRANULARITY_OFFSET);
  782. /* Set Zone A start offset */
  783. FLASH->PCROP1ASR = startoffset;
  784. /* Set Zone A end offset */
  785. pcrop1aend &= ~FLASH_PCROP1AER_PCROP1A_END;
  786. pcrop1aend |= endoffset;
  787. }
  788. /* Set RDP erase protection if needed. This bit is only set & will be reset by mass erase */
  789. if ((PCROPConfig & OB_PCROP_RDP_ERASE) != 0x00U)
  790. {
  791. pcrop1aend |= FLASH_PCROP1AER_PCROP_RDP;
  792. }
  793. /* set 1A End register */
  794. FLASH->PCROP1AER = pcrop1aend;
  795. }
  796. /**
  797. * @brief Configure the 1B Proprietary code readout protection.
  798. * @note It is recommended to align PCROP zone with page granularity when using PCROP_RDP or avoid
  799. * having some executable code in a page where PCROP zone starts or ends.
  800. * @note Minimum PCROP area size is 2 times the chosen granularity: PCROPB_STRT and PCROPB_END.
  801. * So if the requirement is to be able to read-protect 1KB areas, the ROP granularity
  802. * has to be set to 512 Bytes
  803. * @param PCROP1BStartAddr Specifies the Zone 1B Start address of the Proprietary code readout protection
  804. * This parameter can be an address between begin and end of the flash
  805. * @param PCROP1BEndAddr Specifies the Zone 1B end address of the Proprietary code readout protection
  806. * This parameter can be an address between PCROP1BStartAddr and end of the flash
  807. * @retval None
  808. */
  809. static void FLASH_OB_PCROP1BConfig(uint32_t PCROP1BStartAddr, uint32_t PCROP1BEndAddr)
  810. {
  811. uint32_t startoffset;
  812. uint32_t endoffset;
  813. uint32_t ropbase;
  814. #if defined(FLASH_DBANK_SUPPORT)
  815. /* Check if banks are swapped (valid if only one bank) */
  816. if (((FLASH->OPTR & FLASH_OPTR_nSWAP_BANK) != FLASH_OPTR_nSWAP_BANK) && (FLASH_BANK_NB == 2U))
  817. {
  818. /* Check the parameters */
  819. assert_param(IS_FLASH_MAIN_SECONDHALF_MEM_ADDRESS(PCROP1BStartAddr));
  820. assert_param(IS_FLASH_MAIN_SECONDHALF_MEM_ADDRESS(PCROP1BEndAddr));
  821. /* Bank swap, bank 1 read only protection is on second half of Flash */
  822. ropbase = (FLASH_BASE + FLASH_BANK_SIZE);
  823. }
  824. else
  825. #endif /* FLASH_DBANK_SUPPORT */
  826. {
  827. /* Check the parameters */
  828. assert_param(IS_FLASH_MAIN_FIRSTHALF_MEM_ADDRESS(PCROP1BStartAddr));
  829. assert_param(IS_FLASH_MAIN_FIRSTHALF_MEM_ADDRESS(PCROP1BEndAddr));
  830. /* No Bank swap, bank 1 read only protection is on first half of Flash */
  831. ropbase = FLASH_BASE;
  832. }
  833. /* Configure the Proprietary code readout protection offset */
  834. startoffset = ((PCROP1BStartAddr - ropbase) >> FLASH_PCROP_GRANULARITY_OFFSET);
  835. endoffset = ((PCROP1BEndAddr - ropbase) >> FLASH_PCROP_GRANULARITY_OFFSET);
  836. /* Set Zone B start offset */
  837. FLASH->PCROP1BSR = startoffset;
  838. /* Set Zone B end offset */
  839. FLASH->PCROP1BER = endoffset;
  840. }
  841. /**
  842. * @brief Return the FLASH PCROP Protection Option Bytes value.
  843. * @param PCROPConfig [out] specifies the configuration of PCROP_RDP option.
  844. * @param PCROP1AStartAddr [out] Specifies the address where to copied the start address
  845. * of the 1A Proprietary code readout protection
  846. * @param PCROP1AEndAddr [out] Specifies the address where to copied the end address of
  847. * the 1A Proprietary code readout protection
  848. * @retval None
  849. */
  850. static void FLASH_OB_GetPCROP1A(uint32_t *PCROPConfig, uint32_t *PCROP1AStartAddr, uint32_t *PCROP1AEndAddr)
  851. {
  852. uint32_t pcrop;
  853. uint32_t ropbase;
  854. #if defined(FLASH_DBANK_SUPPORT)
  855. /* Check if banks are swapped (valid if only one bank) */
  856. if (((FLASH->OPTR & FLASH_OPTR_nSWAP_BANK) != FLASH_OPTR_nSWAP_BANK) && (FLASH_BANK_NB == 2U))
  857. {
  858. /* Bank swap, bank 1 read only protection is on second half of Flash */
  859. ropbase = (FLASH_BASE + FLASH_BANK_SIZE);
  860. }
  861. else
  862. #endif /* FLASH_DBANK_SUPPORT */
  863. {
  864. /* No Bank swap, bank 1 read only protection is on first half of Flash */
  865. ropbase = FLASH_BASE;
  866. }
  867. pcrop = (FLASH->PCROP1ASR & FLASH_PCROP1ASR_PCROP1A_STRT);
  868. *PCROP1AStartAddr = (pcrop << FLASH_PCROP_GRANULARITY_OFFSET);
  869. *PCROP1AStartAddr += ropbase;
  870. pcrop = FLASH->PCROP1AER;
  871. *PCROP1AEndAddr = ((pcrop & FLASH_PCROP1AER_PCROP1A_END) << FLASH_PCROP_GRANULARITY_OFFSET);
  872. *PCROP1AEndAddr += (ropbase + FLASH_PCROP_GRANULARITY - 1U);
  873. *PCROPConfig &= ~OB_PCROP_RDP_ERASE;
  874. *PCROPConfig |= (pcrop & FLASH_PCROP1AER_PCROP_RDP);
  875. }
  876. /**
  877. * @brief Return the FLASH PCROP Protection Option Bytes value.
  878. * @param PCROP1BStartAddr [out] Specifies the address where to copied the start address
  879. * of the 1B Proprietary code readout protection
  880. * @param PCROP1BEndAddr [out] Specifies the address where to copied the end address of
  881. * the 1B Proprietary code readout protection
  882. * @retval None
  883. */
  884. static void FLASH_OB_GetPCROP1B(uint32_t *PCROP1BStartAddr, uint32_t *PCROP1BEndAddr)
  885. {
  886. uint32_t pcrop;
  887. uint32_t ropbase;
  888. #if defined(FLASH_DBANK_SUPPORT)
  889. /* Check if banks are swapped (valid if only one bank) */
  890. if (((FLASH->OPTR & FLASH_OPTR_nSWAP_BANK) != FLASH_OPTR_nSWAP_BANK) && (FLASH_BANK_NB == 2U))
  891. {
  892. /* Bank swap, bank 1 read only protection is on second half of Flash */
  893. ropbase = (FLASH_BASE + FLASH_BANK_SIZE);
  894. }
  895. else
  896. #endif /* FLASH_DBANK_SUPPORT */
  897. {
  898. /* No Bank swap, bank 1 read only protection is on first half of Flash */
  899. ropbase = FLASH_BASE;
  900. }
  901. pcrop = (FLASH->PCROP1BSR & FLASH_PCROP1BSR_PCROP1B_STRT);
  902. *PCROP1BStartAddr = (pcrop << FLASH_PCROP_GRANULARITY_OFFSET);
  903. *PCROP1BStartAddr += ropbase;
  904. pcrop = (FLASH->PCROP1BER & FLASH_PCROP1BER_PCROP1B_END);
  905. *PCROP1BEndAddr = (pcrop << FLASH_PCROP_GRANULARITY_OFFSET);
  906. *PCROP1BEndAddr += (ropbase + FLASH_PCROP_GRANULARITY - 1U);
  907. }
  908. #if defined(FLASH_DBANK_SUPPORT)
  909. /**
  910. * @brief Configure the 2A Proprietary code readout protection.
  911. * @note It is recommended to align PCROP zone with page granularity when using PCROP_RDP or avoid
  912. * having some executable code in a page where PCROP zone starts or ends.
  913. * @note Minimum PCROP area size is 2 times the chosen granularity: PCROPA_STRT and PCROPA_END.
  914. * So if the requirement is to be able to read-protect 1KB areas, the ROP granularity
  915. * has to be set to 512 Bytes
  916. * @param PCROP2AStartAddr Specifies the Zone 2A Start address of the Proprietary code readout protection
  917. * This parameter can be an address between begin and end of the flash
  918. * @param PCROP2AEndAddr Specifies the Zone 2A end address of the Proprietary code readout protection
  919. * This parameter can be an address between PCROP2AStartAddr and end of the flash
  920. * @retval None
  921. */
  922. static void FLASH_OB_PCROP2AConfig(uint32_t PCROP2AStartAddr, uint32_t PCROP2AEndAddr)
  923. {
  924. uint32_t startoffset;
  925. uint32_t endoffset;
  926. uint32_t ropbase;
  927. /* Check if banks are swapped */
  928. if ((FLASH->OPTR & FLASH_OPTR_nSWAP_BANK) != 0x00u)
  929. {
  930. /* Check the parameters */
  931. assert_param(IS_FLASH_MAIN_SECONDHALF_MEM_ADDRESS(PCROP2AStartAddr));
  932. assert_param(IS_FLASH_MAIN_SECONDHALF_MEM_ADDRESS(PCROP2AEndAddr));
  933. /* No Bank swap, bank 2 read only protection is on second half of Flash */
  934. ropbase = (FLASH_BASE + FLASH_BANK_SIZE);
  935. }
  936. else
  937. {
  938. /* Check the parameters */
  939. assert_param(IS_FLASH_MAIN_FIRSTHALF_MEM_ADDRESS(PCROP2AStartAddr));
  940. assert_param(IS_FLASH_MAIN_FIRSTHALF_MEM_ADDRESS(PCROP2AEndAddr));
  941. /* Bank swap, bank 2 read only protection is on first half of Flash */
  942. ropbase = FLASH_BASE;
  943. }
  944. /* Configure the Proprietary code readout protection offset */
  945. startoffset = ((PCROP2AStartAddr - ropbase) >> FLASH_PCROP_GRANULARITY_OFFSET);
  946. endoffset = ((PCROP2AEndAddr - ropbase) >> FLASH_PCROP_GRANULARITY_OFFSET);
  947. /* Set Zone A start offset */
  948. FLASH->PCROP2ASR = startoffset;
  949. /* Set Zone A end offset */
  950. FLASH->PCROP2AER = endoffset;
  951. }
  952. /**
  953. * @brief Configure the 2B Proprietary code readout protection.
  954. * @note It is recommended to align PCROP zone with page granularity when using PCROP_RDP or avoid
  955. * having some executable code in a page where PCROP zone starts or ends.
  956. * @note Minimum PCROP area size is 2 times the chosen granularity: PCROP_STRT and PCROP_END.
  957. * So if the requirement is to be able to read-protect 1KB areas, the ROP granularity
  958. * has to be set to 512 Bytes
  959. * @param PCROP2BStartAddr Specifies the Zone 2B Start address of the Proprietary code readout protection
  960. * This parameter can be an address between begin and end of the flash
  961. * @param PCROP2BEndAddr Specifies the Zone 2B end address of the Proprietary code readout protection
  962. * This parameter can be an address between PCROP2BStartAddr and end of the flash
  963. * @retval None
  964. */
  965. static void FLASH_OB_PCROP2BConfig(uint32_t PCROP2BStartAddr, uint32_t PCROP2BEndAddr)
  966. {
  967. uint32_t startoffset;
  968. uint32_t endoffset;
  969. uint32_t ropbase;
  970. /* Check if banks are swapped */
  971. if ((FLASH->OPTR & FLASH_OPTR_nSWAP_BANK) != 0x00u)
  972. {
  973. /* Check the parameters */
  974. assert_param(IS_FLASH_MAIN_SECONDHALF_MEM_ADDRESS(PCROP2BStartAddr));
  975. assert_param(IS_FLASH_MAIN_SECONDHALF_MEM_ADDRESS(PCROP2BEndAddr));
  976. /* No Bank swap, bank 2 read only protection is on second half of Flash */
  977. ropbase = (FLASH_BASE + FLASH_BANK_SIZE);
  978. }
  979. else
  980. {
  981. /* Check the parameters */
  982. assert_param(IS_FLASH_MAIN_FIRSTHALF_MEM_ADDRESS(PCROP2BStartAddr));
  983. assert_param(IS_FLASH_MAIN_FIRSTHALF_MEM_ADDRESS(PCROP2BEndAddr));
  984. /* Bank swap, bank 2 read only protection is on first half of Flash */
  985. ropbase = FLASH_BASE;
  986. }
  987. /* Configure the Proprietary code readout protection offset */
  988. startoffset = ((PCROP2BStartAddr - ropbase) >> FLASH_PCROP_GRANULARITY_OFFSET);
  989. endoffset = ((PCROP2BEndAddr - ropbase) >> FLASH_PCROP_GRANULARITY_OFFSET);
  990. /* Set Zone B start offset */
  991. FLASH->PCROP2BSR = startoffset;
  992. /* Set Zone B end offset */
  993. FLASH->PCROP2BER = endoffset;
  994. }
  995. /**
  996. * @brief Return the FLASH PCROP Protection Option Bytes value.
  997. * @param PCROP2AStartAddr [out] Specifies the address where to copied the start address
  998. * of the 2A Proprietary code readout protection
  999. * @param PCROP2AEndAddr [out] Specifies the address where to copied the end address of
  1000. * the 2A Proprietary code readout protection
  1001. * @retval None
  1002. */
  1003. static void FLASH_OB_GetPCROP2A(uint32_t *PCROP2AStartAddr, uint32_t *PCROP2AEndAddr)
  1004. {
  1005. uint32_t pcrop;
  1006. uint32_t ropbase;
  1007. /* Check if banks are swapped */
  1008. if ((FLASH->OPTR & FLASH_OPTR_nSWAP_BANK) != 0x00u)
  1009. {
  1010. /* No Bank swap, bank 2 read only protection is on second half of Flash */
  1011. ropbase = (FLASH_BASE + FLASH_BANK_SIZE);
  1012. }
  1013. else
  1014. {
  1015. /* Bank swap, bank 2 read only protection is on first half of Flash */
  1016. ropbase = FLASH_BASE;
  1017. }
  1018. pcrop = (FLASH->PCROP2ASR & FLASH_PCROP2ASR_PCROP2A_STRT);
  1019. *PCROP2AStartAddr = (pcrop << FLASH_PCROP_GRANULARITY_OFFSET);
  1020. *PCROP2AStartAddr += ropbase;
  1021. pcrop = (FLASH->PCROP2AER & FLASH_PCROP2AER_PCROP2A_END);
  1022. *PCROP2AEndAddr = (pcrop << FLASH_PCROP_GRANULARITY_OFFSET);
  1023. *PCROP2AEndAddr += (ropbase + FLASH_PCROP_GRANULARITY - 1U);
  1024. }
  1025. /**
  1026. * @brief Return the FLASH PCROP Protection Option Bytes value.
  1027. * @param PCROP2BStartAddr [out] Specifies the address where to copied the start address
  1028. * of the 2B Proprietary code readout protection
  1029. * @param PCROP2BEndAddr [out] Specifies the address where to copied the end address of
  1030. * the 2B Proprietary code readout protection
  1031. * @retval None
  1032. */
  1033. static void FLASH_OB_GetPCROP2B(uint32_t *PCROP2BStartAddr, uint32_t *PCROP2BEndAddr)
  1034. {
  1035. uint32_t pcrop;
  1036. uint32_t ropbase;
  1037. /* Check if banks are swapped */
  1038. if ((FLASH->OPTR & FLASH_OPTR_nSWAP_BANK) != 0x00u)
  1039. {
  1040. /* No Bank swap, bank 2 read only protection is on second half of Flash */
  1041. ropbase = (FLASH_BASE + FLASH_BANK_SIZE);
  1042. }
  1043. else
  1044. {
  1045. /* Bank swap, bank 2 read only protection is on first half of Flash */
  1046. ropbase = FLASH_BASE;
  1047. }
  1048. pcrop = (FLASH->PCROP2BSR & FLASH_PCROP2BSR_PCROP2B_STRT);
  1049. *PCROP2BStartAddr = (pcrop << FLASH_PCROP_GRANULARITY_OFFSET);
  1050. *PCROP2BStartAddr += ropbase;
  1051. pcrop = (FLASH->PCROP2BER & FLASH_PCROP2BER_PCROP2B_END);
  1052. *PCROP2BEndAddr = (pcrop << FLASH_PCROP_GRANULARITY_OFFSET);
  1053. *PCROP2BEndAddr += (ropbase + FLASH_PCROP_GRANULARITY - 1U);
  1054. }
  1055. #endif /* FLASH_DBANK_SUPPORT */
  1056. #endif /* FLASH_PCROP_SUPPORT */
  1057. #if defined(FLASH_SECURABLE_MEMORY_SUPPORT)
  1058. #if defined(FLASH_DBANK_SUPPORT)
  1059. /**
  1060. * @brief Configure Securable Memory area feature.
  1061. * @param BootEntry specifies if boot scheme is forced to Flash (System or user) or not
  1062. * This parameter can be one of the following values:
  1063. * @arg @ref OB_BOOT_ENTRY_FORCED_NONE No boot entry forced
  1064. * @arg @ref OB_BOOT_ENTRY_FORCED_FLASH Flash selected as unique entry boot
  1065. * @param SecSize specifies number of pages to protect as securable memory area, starting from
  1066. * beginning of Bank1 (page 0).
  1067. * @param SecSize2 specifies number of pages to protect as securable memory area, starting from
  1068. * beginning of Bank2 (page 0).
  1069. * @retval None
  1070. */
  1071. static void FLASH_OB_SecMemConfig(uint32_t BootEntry, uint32_t SecSize, uint32_t SecSize2)
  1072. {
  1073. uint32_t secmem;
  1074. /* Check the parameters */
  1075. assert_param(IS_OB_SEC_BOOT_LOCK(BootEntry));
  1076. assert_param(IS_OB_SEC_SIZE(SecSize));
  1077. if ((FLASH_BANK_NB == 2U))
  1078. {
  1079. assert_param(IS_OB_SEC_SIZE(SecSize2));
  1080. }
  1081. /* Set securable memory area configuration */
  1082. secmem = (FLASH->SECR & ~(FLASH_SECR_BOOT_LOCK | FLASH_SECR_SEC_SIZE | FLASH_SECR_SEC_SIZE2));
  1083. FLASH->SECR = (secmem | BootEntry | SecSize | (SecSize2 << FLASH_SECR_SEC_SIZE2_Pos));
  1084. }
  1085. /**
  1086. * @brief Return the FLASH Securable memory area protection Option Bytes value.
  1087. * @param BootEntry specifies boot scheme configuration
  1088. * @param SecSize specifies number of pages to protect as secure memory area, starting from
  1089. * beginning of Bank1 (page 0).
  1090. * @param SecSize2 specifies number of pages to protect as secure memory area, starting from
  1091. * beginning of Bank2 (page 0).
  1092. * @retval None
  1093. */
  1094. static void FLASH_OB_GetSecMem(uint32_t *BootEntry, uint32_t *SecSize, uint32_t *SecSize2)
  1095. {
  1096. uint32_t secmem = FLASH->SECR;
  1097. *BootEntry = (secmem & FLASH_SECR_BOOT_LOCK);
  1098. *SecSize = (secmem & FLASH_SECR_SEC_SIZE);
  1099. *SecSize2 = (secmem & FLASH_SECR_SEC_SIZE2) >> FLASH_SECR_SEC_SIZE2_Pos;
  1100. }
  1101. #else
  1102. /**
  1103. * @brief Configure Securable Memory area feature.
  1104. * @param BootEntry specifies if boot scheme is forced to Flash (System or user) or not
  1105. * This parameter can be one of the following values:
  1106. * @arg @ref OB_BOOT_ENTRY_FORCED_NONE No boot entry forced
  1107. * @arg @ref OB_BOOT_ENTRY_FORCED_FLASH FLash selected as unique entry boot
  1108. * @param SecSize specifies number of pages to protect as securable memory area, starting from
  1109. * beginning of the Flash (page 0).
  1110. * @retval None
  1111. */
  1112. static void FLASH_OB_SecMemConfig(uint32_t BootEntry, uint32_t SecSize)
  1113. {
  1114. uint32_t secmem;
  1115. /* Check the parameters */
  1116. assert_param(IS_OB_SEC_BOOT_LOCK(BootEntry));
  1117. assert_param(IS_OB_SEC_SIZE(SecSize));
  1118. /* Set securable memory area configuration */
  1119. secmem = (FLASH->SECR & ~(FLASH_SECR_BOOT_LOCK | FLASH_SECR_SEC_SIZE));
  1120. FLASH->SECR = (secmem | BootEntry | SecSize);
  1121. }
  1122. /**
  1123. * @brief Return the FLASH Securable memory area protection Option Bytes value.
  1124. * @param BootEntry specifies boot scheme configuration
  1125. * @param SecSize specifies number of pages to protect as secure memory area, starting from
  1126. * beginning of the Flash (page 0).
  1127. * @retval None
  1128. */
  1129. static void FLASH_OB_GetSecMem(uint32_t *BootEntry, uint32_t *SecSize)
  1130. {
  1131. uint32_t secmem = FLASH->SECR;
  1132. *BootEntry = (secmem & FLASH_SECR_BOOT_LOCK);
  1133. *SecSize = (secmem & FLASH_SECR_SEC_SIZE);
  1134. }
  1135. #endif /* FLASH_DBANK_SUPPORT */
  1136. #endif /* FLASH_SECURABLE_MEMORY_SUPPORT */
  1137. /**
  1138. * @}
  1139. */
  1140. /**
  1141. * @}
  1142. */
  1143. #endif /* HAL_FLASH_MODULE_ENABLED */
  1144. /**
  1145. * @}
  1146. */
  1147. /**
  1148. * @}
  1149. */