You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1141 lines
41 KiB

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