stm32f7xx_hal_flash_ex.c 41 KB

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