stm32f7xx_hal_flash_ex.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_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. @verbatim
  11. ==============================================================================
  12. ##### Flash Extension features #####
  13. ==============================================================================
  14. [..] Comparing to other previous devices, the FLASH interface for STM32F76xx/STM32F77xx
  15. devices contains the following additional features
  16. (+) Capacity up to 2 Mbyte with dual bank architecture supporting read-while-write
  17. capability (RWW)
  18. (+) Dual bank memory organization
  19. (+) Dual boot mode
  20. ##### How to use this driver #####
  21. ==============================================================================
  22. [..] This driver provides functions to configure and program the FLASH memory
  23. of all STM32F7xx 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 sector, 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 functions: Use HAL_FLASHEx_OBProgram() to :
  32. (++) Set/Reset the write protection
  33. (++) Set the Read protection Level
  34. (++) Set the BOR level
  35. (++) Program the user Option Bytes
  36. @endverbatim
  37. ******************************************************************************
  38. * @attention
  39. *
  40. * Copyright (c) 2017 STMicroelectronics.
  41. * All rights reserved.
  42. *
  43. * This software is licensed under terms that can be found in the LICENSE file in
  44. * the root directory of this software component.
  45. * If no LICENSE file comes with this software, it is provided AS-IS.
  46. ******************************************************************************
  47. */
  48. /* Includes ------------------------------------------------------------------*/
  49. #include "stm32f7xx_hal.h"
  50. /** @addtogroup STM32F7xx_HAL_Driver
  51. * @{
  52. */
  53. /** @defgroup FLASHEx FLASHEx
  54. * @brief FLASH HAL Extension module driver
  55. * @{
  56. */
  57. #ifdef HAL_FLASH_MODULE_ENABLED
  58. /* Private typedef -----------------------------------------------------------*/
  59. /* Private define ------------------------------------------------------------*/
  60. /** @addtogroup FLASHEx_Private_Constants
  61. * @{
  62. */
  63. #define SECTOR_MASK 0xFFFFFF07U
  64. #define FLASH_TIMEOUT_VALUE 50000U/* 50 s */
  65. /**
  66. * @}
  67. */
  68. /* Private macro -------------------------------------------------------------*/
  69. /* Private variables ---------------------------------------------------------*/
  70. /** @addtogroup FLASHEx_Private_Variables
  71. * @{
  72. */
  73. extern FLASH_ProcessTypeDef pFlash;
  74. /**
  75. * @}
  76. */
  77. /* Private function prototypes -----------------------------------------------*/
  78. /** @addtogroup FLASHEx_Private_Functions
  79. * @{
  80. */
  81. /* Option bytes control */
  82. static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector);
  83. static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector);
  84. static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level);
  85. static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level);
  86. static HAL_StatusTypeDef FLASH_OB_BootAddressConfig(uint32_t BootOption, uint32_t Address);
  87. static uint32_t FLASH_OB_GetUser(void);
  88. static uint32_t FLASH_OB_GetWRP(void);
  89. static uint8_t FLASH_OB_GetRDP(void);
  90. static uint32_t FLASH_OB_GetBOR(void);
  91. static uint32_t FLASH_OB_GetBootAddress(uint32_t BootOption);
  92. #if defined (FLASH_OPTCR_nDBANK)
  93. static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks);
  94. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, \
  95. uint32_t Iwdgstdby, uint32_t NDBank, uint32_t NDBoot);
  96. #else
  97. static void FLASH_MassErase(uint8_t VoltageRange);
  98. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, uint32_t Iwdgstdby);
  99. #endif /* FLASH_OPTCR_nDBANK */
  100. #if defined (FLASH_OPTCR2_PCROP)
  101. static HAL_StatusTypeDef FLASH_OB_PCROP_Config(uint32_t PCROPSector);
  102. static HAL_StatusTypeDef FLASH_OB_PCROP_RDP_Config(uint32_t Pcrop_Rdp);
  103. static uint32_t FLASH_OB_GetPCROP(void);
  104. static uint32_t FLASH_OB_GetPCROPRDP(void);
  105. #endif /* FLASH_OPTCR2_PCROP */
  106. extern HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  107. /**
  108. * @}
  109. */
  110. /* Exported functions --------------------------------------------------------*/
  111. /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
  112. * @{
  113. */
  114. /** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
  115. * @brief Extended IO operation functions
  116. *
  117. @verbatim
  118. ===============================================================================
  119. ##### Extended programming operation functions #####
  120. ===============================================================================
  121. [..]
  122. This subsection provides a set of functions allowing to manage the Extension FLASH
  123. programming operations Operations.
  124. @endverbatim
  125. * @{
  126. */
  127. /**
  128. * @brief Perform a mass erase or erase the specified FLASH memory sectors
  129. * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  130. * contains the configuration information for the erasing.
  131. *
  132. * @param[out] SectorError pointer to variable that
  133. * contains the configuration information on faulty sector in case of error
  134. * (0xFFFFFFFF means that all the sectors have been correctly erased)
  135. *
  136. * @retval HAL Status
  137. */
  138. HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
  139. {
  140. HAL_StatusTypeDef status = HAL_ERROR;
  141. uint32_t index = 0;
  142. /* Process Locked */
  143. __HAL_LOCK(&pFlash);
  144. /* Check the parameters */
  145. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  146. /* Wait for last operation to be completed */
  147. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  148. if(status == HAL_OK)
  149. {
  150. /*Initialization of SectorError variable*/
  151. *SectorError = 0xFFFFFFFFU;
  152. if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  153. {
  154. /*Mass erase to be done*/
  155. #if defined (FLASH_OPTCR_nDBANK)
  156. FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
  157. #else
  158. FLASH_MassErase((uint8_t) pEraseInit->VoltageRange);
  159. #endif /* FLASH_OPTCR_nDBANK */
  160. /* Wait for last operation to be completed */
  161. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  162. /* if the erase operation is completed, disable the MER Bit */
  163. FLASH->CR &= (~FLASH_MER_BIT);
  164. }
  165. else
  166. {
  167. /* Check the parameters */
  168. assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
  169. /* Erase by sector by sector to be done*/
  170. for(index = pEraseInit->Sector; index < (pEraseInit->NbSectors + pEraseInit->Sector); index++)
  171. {
  172. FLASH_Erase_Sector(index, (uint8_t) pEraseInit->VoltageRange);
  173. /* Wait for last operation to be completed */
  174. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  175. /* If the erase operation is completed, disable the SER Bit and SNB Bits */
  176. CLEAR_BIT(FLASH->CR, (FLASH_CR_SER | FLASH_CR_SNB));
  177. if(status != HAL_OK)
  178. {
  179. /* In case of error, stop erase procedure and return the faulty sector*/
  180. *SectorError = index;
  181. break;
  182. }
  183. }
  184. }
  185. }
  186. /* Process Unlocked */
  187. __HAL_UNLOCK(&pFlash);
  188. return status;
  189. }
  190. /**
  191. * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
  192. * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  193. * contains the configuration information for the erasing.
  194. *
  195. * @retval HAL Status
  196. */
  197. HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
  198. {
  199. HAL_StatusTypeDef status = HAL_OK;
  200. /* Process Locked */
  201. __HAL_LOCK(&pFlash);
  202. /* Check the parameters */
  203. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  204. /* Enable End of FLASH Operation interrupt */
  205. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
  206. /* Enable Error source interrupt */
  207. __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
  208. /* Clear pending flags (if any) */
  209. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
  210. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_ERSERR);
  211. if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  212. {
  213. /*Mass erase to be done*/
  214. pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
  215. #if defined (FLASH_OPTCR_nDBANK)
  216. FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
  217. #else
  218. FLASH_MassErase((uint8_t) pEraseInit->VoltageRange);
  219. #endif /* FLASH_OPTCR_nDBANK */
  220. }
  221. else
  222. {
  223. /* Erase by sector to be done*/
  224. /* Check the parameters */
  225. assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
  226. pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE;
  227. pFlash.NbSectorsToErase = pEraseInit->NbSectors;
  228. pFlash.Sector = pEraseInit->Sector;
  229. pFlash.VoltageForErase = (uint8_t)pEraseInit->VoltageRange;
  230. /*Erase 1st sector and wait for IT*/
  231. FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->VoltageRange);
  232. }
  233. return status;
  234. }
  235. /**
  236. * @brief Program option bytes
  237. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  238. * contains the configuration information for the programming.
  239. *
  240. * @retval HAL Status
  241. */
  242. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
  243. {
  244. HAL_StatusTypeDef status = HAL_ERROR;
  245. /* Process Locked */
  246. __HAL_LOCK(&pFlash);
  247. /* Check the parameters */
  248. assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
  249. /* Write protection configuration */
  250. if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
  251. {
  252. assert_param(IS_WRPSTATE(pOBInit->WRPState));
  253. if(pOBInit->WRPState == OB_WRPSTATE_ENABLE)
  254. {
  255. /*Enable of Write protection on the selected Sector*/
  256. status = FLASH_OB_EnableWRP(pOBInit->WRPSector);
  257. }
  258. else
  259. {
  260. /*Disable of Write protection on the selected Sector*/
  261. status = FLASH_OB_DisableWRP(pOBInit->WRPSector);
  262. }
  263. }
  264. /* Read protection configuration */
  265. if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
  266. {
  267. status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
  268. }
  269. /* USER configuration */
  270. if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
  271. {
  272. #if defined (FLASH_OPTCR_nDBANK)
  273. status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_WWDG_SW,
  274. pOBInit->USERConfig & OB_IWDG_SW,
  275. pOBInit->USERConfig & OB_STOP_NO_RST,
  276. pOBInit->USERConfig & OB_STDBY_NO_RST,
  277. pOBInit->USERConfig & OB_IWDG_STOP_ACTIVE,
  278. pOBInit->USERConfig & OB_IWDG_STDBY_ACTIVE,
  279. pOBInit->USERConfig & OB_NDBANK_SINGLE_BANK,
  280. pOBInit->USERConfig & OB_DUAL_BOOT_DISABLE);
  281. #else
  282. status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_WWDG_SW,
  283. pOBInit->USERConfig & OB_IWDG_SW,
  284. pOBInit->USERConfig & OB_STOP_NO_RST,
  285. pOBInit->USERConfig & OB_STDBY_NO_RST,
  286. pOBInit->USERConfig & OB_IWDG_STOP_ACTIVE,
  287. pOBInit->USERConfig & OB_IWDG_STDBY_ACTIVE);
  288. #endif /* FLASH_OPTCR_nDBANK */
  289. }
  290. /* BOR Level configuration */
  291. if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
  292. {
  293. status = FLASH_OB_BOR_LevelConfig(pOBInit->BORLevel);
  294. }
  295. /* Boot 0 Address configuration */
  296. if((pOBInit->OptionType & OPTIONBYTE_BOOTADDR_0) == OPTIONBYTE_BOOTADDR_0)
  297. {
  298. status = FLASH_OB_BootAddressConfig(OPTIONBYTE_BOOTADDR_0, pOBInit->BootAddr0);
  299. }
  300. /* Boot 1 Address configuration */
  301. if((pOBInit->OptionType & OPTIONBYTE_BOOTADDR_1) == OPTIONBYTE_BOOTADDR_1)
  302. {
  303. status = FLASH_OB_BootAddressConfig(OPTIONBYTE_BOOTADDR_1, pOBInit->BootAddr1);
  304. }
  305. #if defined (FLASH_OPTCR2_PCROP)
  306. /* PCROP configuration */
  307. if((pOBInit->OptionType & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
  308. {
  309. status = FLASH_OB_PCROP_Config(pOBInit->PCROPSector);
  310. }
  311. /* PCROP_RDP configuration */
  312. if((pOBInit->OptionType & OPTIONBYTE_PCROP_RDP) == OPTIONBYTE_PCROP_RDP)
  313. {
  314. status = FLASH_OB_PCROP_RDP_Config(pOBInit->PCROPRdp);
  315. }
  316. #endif /* FLASH_OPTCR2_PCROP */
  317. /* Process Unlocked */
  318. __HAL_UNLOCK(&pFlash);
  319. return status;
  320. }
  321. /**
  322. * @brief Get the Option byte configuration
  323. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  324. * contains the configuration information for the programming.
  325. *
  326. * @retval None
  327. */
  328. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
  329. {
  330. pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER |\
  331. OPTIONBYTE_BOR | OPTIONBYTE_BOOTADDR_0 | OPTIONBYTE_BOOTADDR_1;
  332. /*Get WRP*/
  333. pOBInit->WRPSector = FLASH_OB_GetWRP();
  334. /*Get RDP Level*/
  335. pOBInit->RDPLevel = FLASH_OB_GetRDP();
  336. /*Get USER*/
  337. pOBInit->USERConfig = FLASH_OB_GetUser();
  338. /*Get BOR Level*/
  339. pOBInit->BORLevel = FLASH_OB_GetBOR();
  340. /*Get Boot Address when Boot pin = 0 */
  341. pOBInit->BootAddr0 = FLASH_OB_GetBootAddress(OPTIONBYTE_BOOTADDR_0);
  342. /*Get Boot Address when Boot pin = 1 */
  343. pOBInit->BootAddr1 = FLASH_OB_GetBootAddress(OPTIONBYTE_BOOTADDR_1);
  344. #if defined (FLASH_OPTCR2_PCROP)
  345. /*Get PCROP Sectors */
  346. pOBInit->PCROPSector = FLASH_OB_GetPCROP();
  347. /*Get PCROP_RDP Value */
  348. pOBInit->PCROPRdp = FLASH_OB_GetPCROPRDP();
  349. #endif /* FLASH_OPTCR2_PCROP */
  350. }
  351. /**
  352. * @}
  353. */
  354. #if defined (FLASH_OPTCR_nDBANK)
  355. /**
  356. * @brief Full erase of FLASH memory sectors
  357. * @param VoltageRange The device voltage range which defines the erase parallelism.
  358. * This parameter can be one of the following values:
  359. * @arg VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
  360. * the operation will be done by byte (8-bit)
  361. * @arg VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
  362. * the operation will be done by half word (16-bit)
  363. * @arg VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
  364. * the operation will be done by word (32-bit)
  365. * @arg VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
  366. * the operation will be done by double word (64-bit)
  367. * @param Banks Banks to be erased
  368. * This parameter can be one of the following values:
  369. * @arg FLASH_BANK_1: Bank1 to be erased
  370. * @arg FLASH_BANK_2: Bank2 to be erased
  371. * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
  372. *
  373. * @retval HAL Status
  374. */
  375. static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks)
  376. {
  377. /* Check the parameters */
  378. assert_param(IS_VOLTAGERANGE(VoltageRange));
  379. assert_param(IS_FLASH_BANK(Banks));
  380. /* if the previous operation is completed, proceed to erase all sectors */
  381. FLASH->CR &= CR_PSIZE_MASK;
  382. if(Banks == FLASH_BANK_BOTH)
  383. {
  384. /* bank1 & bank2 will be erased*/
  385. FLASH->CR |= FLASH_MER_BIT;
  386. }
  387. else if(Banks == FLASH_BANK_2)
  388. {
  389. /*Only bank2 will be erased*/
  390. FLASH->CR |= FLASH_CR_MER2;
  391. }
  392. else
  393. {
  394. /*Only bank1 will be erased*/
  395. FLASH->CR |= FLASH_CR_MER1;
  396. }
  397. FLASH->CR |= FLASH_CR_STRT | ((uint32_t)VoltageRange <<8);
  398. /* Data synchronous Barrier (DSB) Just after the write operation
  399. This will force the CPU to respect the sequence of instruction (no optimization).*/
  400. __DSB();
  401. }
  402. /**
  403. * @brief Erase the specified FLASH memory sector
  404. * @param Sector FLASH sector to erase
  405. * The value of this parameter depend on device used within the same series
  406. * @param VoltageRange The device voltage range which defines the erase parallelism.
  407. * This parameter can be one of the following values:
  408. * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
  409. * the operation will be done by byte (8-bit)
  410. * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
  411. * the operation will be done by half word (16-bit)
  412. * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
  413. * the operation will be done by word (32-bit)
  414. * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
  415. * the operation will be done by double word (64-bit)
  416. *
  417. * @retval None
  418. */
  419. void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
  420. {
  421. uint32_t tmp_psize = 0;
  422. /* Check the parameters */
  423. assert_param(IS_FLASH_SECTOR(Sector));
  424. assert_param(IS_VOLTAGERANGE(VoltageRange));
  425. if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
  426. {
  427. tmp_psize = FLASH_PSIZE_BYTE;
  428. }
  429. else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
  430. {
  431. tmp_psize = FLASH_PSIZE_HALF_WORD;
  432. }
  433. else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
  434. {
  435. tmp_psize = FLASH_PSIZE_WORD;
  436. }
  437. else
  438. {
  439. tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
  440. }
  441. /* Need to add offset of 4 when sector higher than FLASH_SECTOR_11 */
  442. if(Sector > FLASH_SECTOR_11)
  443. {
  444. Sector += 4;
  445. }
  446. /* If the previous operation is completed, proceed to erase the sector */
  447. FLASH->CR &= CR_PSIZE_MASK;
  448. FLASH->CR |= tmp_psize;
  449. CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
  450. FLASH->CR |= FLASH_CR_SER | (Sector << FLASH_CR_SNB_Pos);
  451. FLASH->CR |= FLASH_CR_STRT;
  452. /* Data synchronous Barrier (DSB) Just after the write operation
  453. This will force the CPU to respect the sequence of instruction (no optimization).*/
  454. __DSB();
  455. }
  456. /**
  457. * @brief Return the FLASH Write Protection Option Bytes value.
  458. * @retval uint32_t FLASH Write Protection Option Bytes value
  459. */
  460. static uint32_t FLASH_OB_GetWRP(void)
  461. {
  462. /* Return the FLASH write protection Register value */
  463. return ((uint32_t)(FLASH->OPTCR & 0x0FFF0000));
  464. }
  465. /**
  466. * @brief Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  467. * @param Wwdg Selects the IWDG mode
  468. * This parameter can be one of the following values:
  469. * @arg OB_WWDG_SW: Software WWDG selected
  470. * @arg OB_WWDG_HW: Hardware WWDG selected
  471. * @param Iwdg Selects the WWDG mode
  472. * This parameter can be one of the following values:
  473. * @arg OB_IWDG_SW: Software IWDG selected
  474. * @arg OB_IWDG_HW: Hardware IWDG selected
  475. * @param Stop Reset event when entering STOP mode.
  476. * This parameter can be one of the following values:
  477. * @arg OB_STOP_NO_RST: No reset generated when entering in STOP
  478. * @arg OB_STOP_RST: Reset generated when entering in STOP
  479. * @param Stdby Reset event when entering Standby mode.
  480. * This parameter can be one of the following values:
  481. * @arg OB_STDBY_NO_RST: No reset generated when entering in STANDBY
  482. * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
  483. * @param Iwdgstop Independent watchdog counter freeze in Stop mode.
  484. * This parameter can be one of the following values:
  485. * @arg OB_IWDG_STOP_FREEZE: Freeze IWDG counter in STOP
  486. * @arg OB_IWDG_STOP_ACTIVE: IWDG counter active in STOP
  487. * @param Iwdgstdby Independent watchdog counter freeze in standby mode.
  488. * This parameter can be one of the following values:
  489. * @arg OB_IWDG_STDBY_FREEZE: Freeze IWDG counter in STANDBY
  490. * @arg OB_IWDG_STDBY_ACTIVE: IWDG counter active in STANDBY
  491. * @param NDBank Flash Single Bank mode enabled.
  492. * This parameter can be one of the following values:
  493. * @arg OB_NDBANK_SINGLE_BANK: enable 256 bits mode (Flash is a single bank)
  494. * @arg OB_NDBANK_DUAL_BANK: disable 256 bits mode (Flash is a dual bank in 128 bits mode)
  495. * @param NDBoot Flash Dual boot mode disable.
  496. * This parameter can be one of the following values:
  497. * @arg OB_DUAL_BOOT_DISABLE: Disable Dual Boot
  498. * @arg OB_DUAL_BOOT_ENABLE: Enable Dual Boot
  499. * @retval HAL Status
  500. */
  501. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, \
  502. uint32_t Iwdgstdby, uint32_t NDBank, uint32_t NDBoot)
  503. {
  504. uint32_t useroptionmask = 0x00;
  505. uint32_t useroptionvalue = 0x00;
  506. HAL_StatusTypeDef status = HAL_OK;
  507. /* Check the parameters */
  508. assert_param(IS_OB_WWDG_SOURCE(Wwdg));
  509. assert_param(IS_OB_IWDG_SOURCE(Iwdg));
  510. assert_param(IS_OB_STOP_SOURCE(Stop));
  511. assert_param(IS_OB_STDBY_SOURCE(Stdby));
  512. assert_param(IS_OB_IWDG_STOP_FREEZE(Iwdgstop));
  513. assert_param(IS_OB_IWDG_STDBY_FREEZE(Iwdgstdby));
  514. assert_param(IS_OB_NDBANK(NDBank));
  515. assert_param(IS_OB_NDBOOT(NDBoot));
  516. /* Wait for last operation to be completed */
  517. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  518. if(status == HAL_OK)
  519. {
  520. useroptionmask = (FLASH_OPTCR_WWDG_SW | FLASH_OPTCR_IWDG_SW | FLASH_OPTCR_nRST_STOP | \
  521. FLASH_OPTCR_nRST_STDBY | FLASH_OPTCR_IWDG_STOP | FLASH_OPTCR_IWDG_STDBY | \
  522. FLASH_OPTCR_nDBOOT | FLASH_OPTCR_nDBANK);
  523. useroptionvalue = (Iwdg | Wwdg | Stop | Stdby | Iwdgstop | Iwdgstdby | NDBoot | NDBank);
  524. /* Update User Option Byte */
  525. MODIFY_REG(FLASH->OPTCR, useroptionmask, useroptionvalue);
  526. }
  527. return status;
  528. }
  529. /**
  530. * @brief Return the FLASH User Option Byte value.
  531. * @retval uint32_t FLASH User Option Bytes values: WWDG_SW(Bit4), IWDG_SW(Bit5), nRST_STOP(Bit6),
  532. * nRST_STDBY(Bit7), nDBOOT(Bit28), nDBANK(Bit29), IWDG_STDBY(Bit30) and IWDG_STOP(Bit31).
  533. */
  534. static uint32_t FLASH_OB_GetUser(void)
  535. {
  536. /* Return the User Option Byte */
  537. return ((uint32_t)(FLASH->OPTCR & 0xF00000F0U));
  538. }
  539. #else
  540. /**
  541. * @brief Full erase of FLASH memory sectors
  542. * @param VoltageRange The device voltage range which defines the erase parallelism.
  543. * This parameter can be one of the following values:
  544. * @arg VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
  545. * the operation will be done by byte (8-bit)
  546. * @arg VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
  547. * the operation will be done by half word (16-bit)
  548. * @arg VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
  549. * the operation will be done by word (32-bit)
  550. * @arg VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
  551. * the operation will be done by double word (64-bit)
  552. *
  553. * @retval HAL Status
  554. */
  555. static void FLASH_MassErase(uint8_t VoltageRange)
  556. {
  557. /* Check the parameters */
  558. assert_param(IS_VOLTAGERANGE(VoltageRange));
  559. /* if the previous operation is completed, proceed to erase all sectors */
  560. FLASH->CR &= CR_PSIZE_MASK;
  561. FLASH->CR |= FLASH_CR_MER;
  562. FLASH->CR |= FLASH_CR_STRT | ((uint32_t)VoltageRange <<8);
  563. /* Data synchronous Barrier (DSB) Just after the write operation
  564. This will force the CPU to respect the sequence of instruction (no optimization).*/
  565. __DSB();
  566. }
  567. /**
  568. * @brief Erase the specified FLASH memory sector
  569. * @param Sector FLASH sector to erase
  570. * The value of this parameter depend on device used within the same series
  571. * @param VoltageRange The device voltage range which defines the erase parallelism.
  572. * This parameter can be one of the following values:
  573. * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
  574. * the operation will be done by byte (8-bit)
  575. * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
  576. * the operation will be done by half word (16-bit)
  577. * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
  578. * the operation will be done by word (32-bit)
  579. * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
  580. * the operation will be done by double word (64-bit)
  581. *
  582. * @retval None
  583. */
  584. void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
  585. {
  586. uint32_t tmp_psize = 0;
  587. /* Check the parameters */
  588. assert_param(IS_FLASH_SECTOR(Sector));
  589. assert_param(IS_VOLTAGERANGE(VoltageRange));
  590. if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
  591. {
  592. tmp_psize = FLASH_PSIZE_BYTE;
  593. }
  594. else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
  595. {
  596. tmp_psize = FLASH_PSIZE_HALF_WORD;
  597. }
  598. else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
  599. {
  600. tmp_psize = FLASH_PSIZE_WORD;
  601. }
  602. else
  603. {
  604. tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
  605. }
  606. /* If the previous operation is completed, proceed to erase the sector */
  607. FLASH->CR &= CR_PSIZE_MASK;
  608. FLASH->CR |= tmp_psize;
  609. FLASH->CR &= SECTOR_MASK;
  610. FLASH->CR |= FLASH_CR_SER | (Sector << FLASH_CR_SNB_Pos);
  611. FLASH->CR |= FLASH_CR_STRT;
  612. /* Data synchronous Barrier (DSB) Just after the write operation
  613. This will force the CPU to respect the sequence of instruction (no optimization).*/
  614. __DSB();
  615. }
  616. /**
  617. * @brief Return the FLASH Write Protection Option Bytes value.
  618. * @retval uint32_t FLASH Write Protection Option Bytes value
  619. */
  620. static uint32_t FLASH_OB_GetWRP(void)
  621. {
  622. /* Return the FLASH write protection Register value */
  623. return ((uint32_t)(FLASH->OPTCR & 0x00FF0000));
  624. }
  625. /**
  626. * @brief Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  627. * @param Wwdg Selects the IWDG mode
  628. * This parameter can be one of the following values:
  629. * @arg OB_WWDG_SW: Software WWDG selected
  630. * @arg OB_WWDG_HW: Hardware WWDG selected
  631. * @param Iwdg Selects the WWDG mode
  632. * This parameter can be one of the following values:
  633. * @arg OB_IWDG_SW: Software IWDG selected
  634. * @arg OB_IWDG_HW: Hardware IWDG selected
  635. * @param Stop Reset event when entering STOP mode.
  636. * This parameter can be one of the following values:
  637. * @arg OB_STOP_NO_RST: No reset generated when entering in STOP
  638. * @arg OB_STOP_RST: Reset generated when entering in STOP
  639. * @param Stdby Reset event when entering Standby mode.
  640. * This parameter can be one of the following values:
  641. * @arg OB_STDBY_NO_RST: No reset generated when entering in STANDBY
  642. * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
  643. * @param Iwdgstop Independent watchdog counter freeze in Stop mode.
  644. * This parameter can be one of the following values:
  645. * @arg OB_IWDG_STOP_FREEZE: Freeze IWDG counter in STOP
  646. * @arg OB_IWDG_STOP_ACTIVE: IWDG counter active in STOP
  647. * @param Iwdgstdby Independent watchdog counter freeze in standby mode.
  648. * This parameter can be one of the following values:
  649. * @arg OB_IWDG_STDBY_FREEZE: Freeze IWDG counter in STANDBY
  650. * @arg OB_IWDG_STDBY_ACTIVE: IWDG counter active in STANDBY
  651. * @retval HAL Status
  652. */
  653. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, uint32_t Iwdgstdby)
  654. {
  655. uint32_t useroptionmask = 0x00;
  656. uint32_t useroptionvalue = 0x00;
  657. HAL_StatusTypeDef status = HAL_OK;
  658. /* Check the parameters */
  659. assert_param(IS_OB_WWDG_SOURCE(Wwdg));
  660. assert_param(IS_OB_IWDG_SOURCE(Iwdg));
  661. assert_param(IS_OB_STOP_SOURCE(Stop));
  662. assert_param(IS_OB_STDBY_SOURCE(Stdby));
  663. assert_param(IS_OB_IWDG_STOP_FREEZE(Iwdgstop));
  664. assert_param(IS_OB_IWDG_STDBY_FREEZE(Iwdgstdby));
  665. /* Wait for last operation to be completed */
  666. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  667. if(status == HAL_OK)
  668. {
  669. useroptionmask = (FLASH_OPTCR_WWDG_SW | FLASH_OPTCR_IWDG_SW | FLASH_OPTCR_nRST_STOP | \
  670. FLASH_OPTCR_nRST_STDBY | FLASH_OPTCR_IWDG_STOP | FLASH_OPTCR_IWDG_STDBY);
  671. useroptionvalue = (Iwdg | Wwdg | Stop | Stdby | Iwdgstop | Iwdgstdby);
  672. /* Update User Option Byte */
  673. MODIFY_REG(FLASH->OPTCR, useroptionmask, useroptionvalue);
  674. }
  675. return status;
  676. }
  677. /**
  678. * @brief Return the FLASH User Option Byte value.
  679. * @retval uint32_t FLASH User Option Bytes values: WWDG_SW(Bit4), IWDG_SW(Bit5), nRST_STOP(Bit6),
  680. * nRST_STDBY(Bit7), IWDG_STDBY(Bit30) and IWDG_STOP(Bit31).
  681. */
  682. static uint32_t FLASH_OB_GetUser(void)
  683. {
  684. /* Return the User Option Byte */
  685. return ((uint32_t)(FLASH->OPTCR & 0xC00000F0U));
  686. }
  687. #endif /* FLASH_OPTCR_nDBANK */
  688. /**
  689. * @brief Enable the write protection of the desired bank1 or bank2 sectors
  690. *
  691. * @note When the memory read protection level is selected (RDP level = 1),
  692. * it is not possible to program or erase the flash sector i if CortexM7
  693. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  694. *
  695. * @param WRPSector specifies the sector(s) to be write protected.
  696. * This parameter can be one of the following values:
  697. * @arg WRPSector: A value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_7 (for STM32F74xxx/STM32F75xxx devices)
  698. * or a value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_11 (in Single Bank mode for STM32F76xxx/STM32F77xxx devices)
  699. * or a value between OB_WRP_DB_SECTOR_0 and OB_WRP_DB_SECTOR_23 (in Dual Bank mode for STM32F76xxx/STM32F77xxx devices)
  700. * @arg OB_WRP_SECTOR_All
  701. *
  702. * @retval HAL FLASH State
  703. */
  704. static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector)
  705. {
  706. HAL_StatusTypeDef status = HAL_OK;
  707. /* Check the parameters */
  708. assert_param(IS_OB_WRP_SECTOR(WRPSector));
  709. /* Wait for last operation to be completed */
  710. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  711. if(status == HAL_OK)
  712. {
  713. /*Write protection enabled on sectors */
  714. FLASH->OPTCR &= (~WRPSector);
  715. }
  716. return status;
  717. }
  718. /**
  719. * @brief Disable the write protection of the desired bank1 or bank 2 sectors
  720. *
  721. * @note When the memory read protection level is selected (RDP level = 1),
  722. * it is not possible to program or erase the flash sector i if CortexM4
  723. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  724. *
  725. * @param WRPSector specifies the sector(s) to be write protected.
  726. * This parameter can be one of the following values:
  727. * @arg WRPSector: A value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_7 (for STM32F74xxx/STM32F75xxx devices)
  728. * or a value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_11 (in Single Bank mode for STM32F76xxx/STM32F77xxx devices)
  729. * or a value between OB_WRP_DB_SECTOR_0 and OB_WRP_DB_SECTOR_23 (in Dual Bank mode for STM32F76xxx/STM32F77xxx devices)
  730. * @arg OB_WRP_Sector_All
  731. *
  732. *
  733. * @retval HAL Status
  734. */
  735. static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector)
  736. {
  737. HAL_StatusTypeDef status = HAL_OK;
  738. /* Check the parameters */
  739. assert_param(IS_OB_WRP_SECTOR(WRPSector));
  740. /* Wait for last operation to be completed */
  741. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  742. if(status == HAL_OK)
  743. {
  744. /* Write protection disabled on sectors */
  745. FLASH->OPTCR |= (WRPSector);
  746. }
  747. return status;
  748. }
  749. /**
  750. * @brief Set the read protection level.
  751. * @param Level specifies the read protection level.
  752. * This parameter can be one of the following values:
  753. * @arg OB_RDP_LEVEL_0: No protection
  754. * @arg OB_RDP_LEVEL_1: Read protection of the memory
  755. * @arg OB_RDP_LEVEL_2: Full chip protection
  756. *
  757. * @note WARNING: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
  758. *
  759. * @retval HAL Status
  760. */
  761. static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level)
  762. {
  763. HAL_StatusTypeDef status = HAL_OK;
  764. /* Check the parameters */
  765. assert_param(IS_OB_RDP_LEVEL(Level));
  766. /* Wait for last operation to be completed */
  767. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  768. if(status == HAL_OK)
  769. {
  770. *(__IO uint8_t*)OPTCR_BYTE1_ADDRESS = Level;
  771. }
  772. return status;
  773. }
  774. /**
  775. * @brief Set the BOR Level.
  776. * @param Level specifies the Option Bytes BOR Reset Level.
  777. * This parameter can be one of the following values:
  778. * @arg OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
  779. * @arg OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
  780. * @arg OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
  781. * @arg OB_BOR_OFF: Supply voltage ranges from 1.62 to 2.1 V
  782. * @retval HAL Status
  783. */
  784. static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level)
  785. {
  786. /* Check the parameters */
  787. assert_param(IS_OB_BOR_LEVEL(Level));
  788. /* Set the BOR Level */
  789. MODIFY_REG(FLASH->OPTCR, FLASH_OPTCR_BOR_LEV, Level);
  790. return HAL_OK;
  791. }
  792. /**
  793. * @brief Configure Boot base address.
  794. *
  795. * @param BootOption specifies Boot base address depending from Boot pin = 0 or pin = 1
  796. * This parameter can be one of the following values:
  797. * @arg OPTIONBYTE_BOOTADDR_0 : Boot address based when Boot pin = 0
  798. * @arg OPTIONBYTE_BOOTADDR_1 : Boot address based when Boot pin = 1
  799. * @param Address specifies Boot base address
  800. * This parameter can be one of the following values:
  801. * @arg OB_BOOTADDR_ITCM_RAM : Boot from ITCM RAM (0x00000000)
  802. * @arg OB_BOOTADDR_SYSTEM : Boot from System memory bootloader (0x00100000)
  803. * @arg OB_BOOTADDR_ITCM_FLASH : Boot from Flash on ITCM interface (0x00200000)
  804. * @arg OB_BOOTADDR_AXIM_FLASH : Boot from Flash on AXIM interface (0x08000000)
  805. * @arg OB_BOOTADDR_DTCM_RAM : Boot from DTCM RAM (0x20000000)
  806. * @arg OB_BOOTADDR_SRAM1 : Boot from SRAM1 (0x20010000)
  807. * @arg OB_BOOTADDR_SRAM2 : Boot from SRAM2 (0x2004C000)
  808. *
  809. * @retval HAL Status
  810. */
  811. static HAL_StatusTypeDef FLASH_OB_BootAddressConfig(uint32_t BootOption, uint32_t Address)
  812. {
  813. HAL_StatusTypeDef status = HAL_OK;
  814. /* Check the parameters */
  815. assert_param(IS_OB_BOOT_ADDRESS(Address));
  816. /* Wait for last operation to be completed */
  817. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  818. if(status == HAL_OK)
  819. {
  820. if(BootOption == OPTIONBYTE_BOOTADDR_0)
  821. {
  822. MODIFY_REG(FLASH->OPTCR1, FLASH_OPTCR1_BOOT_ADD0, Address);
  823. }
  824. else
  825. {
  826. MODIFY_REG(FLASH->OPTCR1, FLASH_OPTCR1_BOOT_ADD1, (Address << 16));
  827. }
  828. }
  829. return status;
  830. }
  831. /**
  832. * @brief Returns the FLASH Read Protection level.
  833. * @retval FlagStatus FLASH ReadOut Protection Status:
  834. * This parameter can be one of the following values:
  835. * @arg OB_RDP_LEVEL_0: No protection
  836. * @arg OB_RDP_LEVEL_1: Read protection of the memory
  837. * @arg OB_RDP_LEVEL_2: Full chip protection
  838. */
  839. static uint8_t FLASH_OB_GetRDP(void)
  840. {
  841. uint8_t readstatus = OB_RDP_LEVEL_0;
  842. if ((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS)) == OB_RDP_LEVEL_0)
  843. {
  844. readstatus = OB_RDP_LEVEL_0;
  845. }
  846. else if ((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS)) == OB_RDP_LEVEL_2)
  847. {
  848. readstatus = OB_RDP_LEVEL_2;
  849. }
  850. else
  851. {
  852. readstatus = OB_RDP_LEVEL_1;
  853. }
  854. return readstatus;
  855. }
  856. /**
  857. * @brief Returns the FLASH BOR level.
  858. * @retval uint32_t The FLASH BOR level:
  859. * - OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
  860. * - OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
  861. * - OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
  862. * - OB_BOR_OFF : Supply voltage ranges from 1.62 to 2.1 V
  863. */
  864. static uint32_t FLASH_OB_GetBOR(void)
  865. {
  866. /* Return the FLASH BOR level */
  867. return ((uint32_t)(FLASH->OPTCR & 0x0C));
  868. }
  869. /**
  870. * @brief Configure Boot base address.
  871. *
  872. * @param BootOption specifies Boot base address depending from Boot pin = 0 or pin = 1
  873. * This parameter can be one of the following values:
  874. * @arg OPTIONBYTE_BOOTADDR_0 : Boot address based when Boot pin = 0
  875. * @arg OPTIONBYTE_BOOTADDR_1 : Boot address based when Boot pin = 1
  876. *
  877. * @retval uint32_t Boot Base Address:
  878. * - OB_BOOTADDR_ITCM_RAM : Boot from ITCM RAM (0x00000000)
  879. * - OB_BOOTADDR_SYSTEM : Boot from System memory bootloader (0x00100000)
  880. * - OB_BOOTADDR_ITCM_FLASH : Boot from Flash on ITCM interface (0x00200000)
  881. * - OB_BOOTADDR_AXIM_FLASH : Boot from Flash on AXIM interface (0x08000000)
  882. * - OB_BOOTADDR_DTCM_RAM : Boot from DTCM RAM (0x20000000)
  883. * - OB_BOOTADDR_SRAM1 : Boot from SRAM1 (0x20010000)
  884. * - OB_BOOTADDR_SRAM2 : Boot from SRAM2 (0x2004C000)
  885. */
  886. static uint32_t FLASH_OB_GetBootAddress(uint32_t BootOption)
  887. {
  888. uint32_t Address = 0;
  889. /* Return the Boot base Address */
  890. if(BootOption == OPTIONBYTE_BOOTADDR_0)
  891. {
  892. Address = FLASH->OPTCR1 & FLASH_OPTCR1_BOOT_ADD0;
  893. }
  894. else
  895. {
  896. Address = ((FLASH->OPTCR1 & FLASH_OPTCR1_BOOT_ADD1) >> 16);
  897. }
  898. return Address;
  899. }
  900. #if defined (FLASH_OPTCR2_PCROP)
  901. /**
  902. * @brief Set the PCROP protection for sectors.
  903. * @param PCROPSector specifies the sector(s) to be PCROP protected.
  904. * This parameter can be one of the following values:
  905. * @arg OB_PCROP_SECTOR_x: A value between OB_PCROP_SECTOR_0 and OB_PCROP_SECTOR_7
  906. * @arg OB_PCROP_SECTOR_ALL
  907. *
  908. * @retval HAL Status
  909. */
  910. static HAL_StatusTypeDef FLASH_OB_PCROP_Config(uint32_t PCROPSector)
  911. {
  912. HAL_StatusTypeDef status = HAL_OK;
  913. /* Check the parameters */
  914. assert_param(IS_OB_PCROP_SECTOR(PCROPSector));
  915. /* Wait for last operation to be completed */
  916. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  917. if(status == HAL_OK)
  918. {
  919. MODIFY_REG(FLASH->OPTCR2, FLASH_OPTCR2_PCROP, PCROPSector);
  920. }
  921. return status;
  922. }
  923. /**
  924. * @brief Set the PCROP_RDP value
  925. * @param Pcrop_Rdp specifies the PCROP_RDP bit value.
  926. *
  927. * @retval HAL Status
  928. */
  929. static HAL_StatusTypeDef FLASH_OB_PCROP_RDP_Config(uint32_t Pcrop_Rdp)
  930. {
  931. HAL_StatusTypeDef status = HAL_OK;
  932. /* Check the parameters */
  933. assert_param(IS_OB_PCROP_RDP_VALUE(Pcrop_Rdp));
  934. /* Wait for last operation to be completed */
  935. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  936. if(status == HAL_OK)
  937. {
  938. MODIFY_REG(FLASH->OPTCR2, FLASH_OPTCR2_PCROP_RDP, Pcrop_Rdp);
  939. }
  940. return status;
  941. }
  942. /**
  943. * @brief Return the FLASH PCROP Protection Option Bytes value.
  944. * @retval uint32_t FLASH PCROP Protection Option Bytes value
  945. */
  946. static uint32_t FLASH_OB_GetPCROP(void)
  947. {
  948. /* Return the FLASH write protection Register value */
  949. return ((uint32_t)(FLASH->OPTCR2 & FLASH_OPTCR2_PCROP));
  950. }
  951. /**
  952. * @brief Return the FLASH PCROP_RDP option byte value.
  953. * @retval uint32_t FLASH PCROP_RDP option byte value
  954. */
  955. static uint32_t FLASH_OB_GetPCROPRDP(void)
  956. {
  957. /* Return the FLASH write protection Register value */
  958. return ((uint32_t)(FLASH->OPTCR2 & FLASH_OPTCR2_PCROP_RDP));
  959. }
  960. #endif /* FLASH_OPTCR2_PCROP */
  961. /**
  962. * @}
  963. */
  964. #endif /* HAL_FLASH_MODULE_ENABLED */
  965. /**
  966. * @}
  967. */
  968. /**
  969. * @}
  970. */