stm32f2xx_hal_flash_ex.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_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. ##### How to use this driver #####
  15. ==============================================================================
  16. [..] This driver provides functions to configure and program the FLASH memory
  17. of all STM32F2xx devices. It includes
  18. (#) FLASH Memory Erase functions:
  19. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  20. HAL_FLASH_Lock() functions
  21. (++) Erase function: Erase sector, erase all sectors
  22. (++) There are two modes of erase :
  23. (+++) Polling Mode using HAL_FLASHEx_Erase()
  24. (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
  25. (#) Option Bytes Programming functions: Use HAL_FLASHEx_OBProgram() to :
  26. (++) Set/Reset the write protection
  27. (++) Set the Read protection Level
  28. (++) Set the BOR level
  29. (++) Program the user Option Bytes
  30. @endverbatim
  31. ******************************************************************************
  32. * @attention
  33. *
  34. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  35. * All rights reserved.</center></h2>
  36. *
  37. * This software component is licensed by ST under BSD 3-Clause license,
  38. * the "License"; You may not use this file except in compliance with the
  39. * License. You may obtain a copy of the License at:
  40. * opensource.org/licenses/BSD-3-Clause
  41. *
  42. ******************************************************************************
  43. */
  44. /* Includes ------------------------------------------------------------------*/
  45. #include "stm32f2xx_hal.h"
  46. /** @addtogroup STM32F2xx_HAL_Driver
  47. * @{
  48. */
  49. /** @defgroup FLASHEx FLASHEx
  50. * @brief FLASH HAL Extension module driver
  51. * @{
  52. */
  53. #ifdef HAL_FLASH_MODULE_ENABLED
  54. /* Private typedef -----------------------------------------------------------*/
  55. /* Private define ------------------------------------------------------------*/
  56. /** @addtogroup FLASHEx_Private_Constants
  57. * @{
  58. */
  59. #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
  60. /**
  61. * @}
  62. */
  63. /* Private macro -------------------------------------------------------------*/
  64. /* Private variables ---------------------------------------------------------*/
  65. /** @addtogroup FLASHEx_Private_Variables
  66. * @{
  67. */
  68. extern FLASH_ProcessTypeDef pFlash;
  69. /**
  70. * @}
  71. */
  72. /* Private function prototypes -----------------------------------------------*/
  73. /** @addtogroup FLASHEx_Private_Functions
  74. * @{
  75. */
  76. /* Option bytes control */
  77. static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks);
  78. static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks);
  79. static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks);
  80. static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level);
  81. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t Iwdg, uint8_t Stop, uint8_t Stdby);
  82. static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level);
  83. static uint8_t FLASH_OB_GetUser(void);
  84. static uint16_t FLASH_OB_GetWRP(void);
  85. static uint8_t FLASH_OB_GetRDP(void);
  86. static uint8_t FLASH_OB_GetBOR(void);
  87. extern HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  88. /**
  89. * @}
  90. */
  91. /* Exported functions --------------------------------------------------------*/
  92. /** @defgroup FLASHEx_Exported_Functions FLASH Exported Functions
  93. * @{
  94. */
  95. /** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
  96. * @brief Extended IO operation functions
  97. *
  98. @verbatim
  99. ===============================================================================
  100. ##### Extended programming operation functions #####
  101. ===============================================================================
  102. [..]
  103. This subsection provides a set of functions allowing to manage the Extension FLASH
  104. programming operations.
  105. @endverbatim
  106. * @{
  107. */
  108. /**
  109. * @brief Perform a mass erase or erase the specified FLASH memory sectors
  110. * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  111. * contains the configuration information for the erasing.
  112. *
  113. * @param[out] SectorError pointer to variable that
  114. * contains the configuration information on faulty sector in case of error
  115. * (0xFFFFFFFF means that all the sectors have been correctly erased)
  116. *
  117. * @retval HAL Status
  118. */
  119. HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
  120. {
  121. HAL_StatusTypeDef status = HAL_ERROR;
  122. uint32_t index = 0U;
  123. /* Process Locked */
  124. __HAL_LOCK(&pFlash);
  125. /* Check the parameters */
  126. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  127. /* Wait for last operation to be completed */
  128. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  129. if(status == HAL_OK)
  130. {
  131. /*Initialization of SectorError variable*/
  132. *SectorError = 0xFFFFFFFFU;
  133. if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  134. {
  135. /*Mass erase to be done*/
  136. FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
  137. /* Wait for last operation to be completed */
  138. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  139. /* if the erase operation is completed, disable the MER Bit */
  140. FLASH->CR &= (~FLASH_MER_BIT);
  141. }
  142. else
  143. {
  144. /* Check the parameters */
  145. assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
  146. /* Erase by sector by sector to be done*/
  147. for(index = pEraseInit->Sector; index < (pEraseInit->NbSectors + pEraseInit->Sector); index++)
  148. {
  149. FLASH_Erase_Sector(index, (uint8_t) pEraseInit->VoltageRange);
  150. /* Wait for last operation to be completed */
  151. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  152. /* If the erase operation is completed, disable the SER and SNB Bits */
  153. CLEAR_BIT(FLASH->CR, (FLASH_CR_SER | FLASH_CR_SNB));
  154. if(status != HAL_OK)
  155. {
  156. /* In case of error, stop erase procedure and return the faulty sector*/
  157. *SectorError = index;
  158. break;
  159. }
  160. }
  161. }
  162. /* Flush the caches to be sure of the data consistency */
  163. FLASH_FlushCaches();
  164. }
  165. /* Process Unlocked */
  166. __HAL_UNLOCK(&pFlash);
  167. return status;
  168. }
  169. /**
  170. * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
  171. * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  172. * contains the configuration information for the erasing.
  173. *
  174. * @retval HAL Status
  175. */
  176. HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
  177. {
  178. HAL_StatusTypeDef status = HAL_OK;
  179. /* Process Locked */
  180. __HAL_LOCK(&pFlash);
  181. /* Check the parameters */
  182. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  183. /* Enable End of FLASH Operation interrupt */
  184. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
  185. /* Enable Error source interrupt */
  186. __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
  187. /* Clear pending flags (if any) */
  188. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
  189. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_PGSERR);
  190. if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  191. {
  192. /*Mass erase to be done*/
  193. pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
  194. pFlash.Bank = pEraseInit->Banks;
  195. FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
  196. }
  197. else
  198. {
  199. /* Erase by sector to be done*/
  200. /* Check the parameters */
  201. assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
  202. pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE;
  203. pFlash.NbSectorsToErase = pEraseInit->NbSectors;
  204. pFlash.Sector = pEraseInit->Sector;
  205. pFlash.VoltageForErase = (uint8_t)pEraseInit->VoltageRange;
  206. /*Erase 1st sector and wait for IT*/
  207. FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->VoltageRange);
  208. }
  209. return status;
  210. }
  211. /**
  212. * @brief Program option bytes
  213. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  214. * contains the configuration information for the programming.
  215. *
  216. * @retval HAL Status
  217. */
  218. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
  219. {
  220. HAL_StatusTypeDef status = HAL_ERROR;
  221. /* Process Locked */
  222. __HAL_LOCK(&pFlash);
  223. /* Check the parameters */
  224. assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
  225. /*Write protection configuration*/
  226. if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
  227. {
  228. assert_param(IS_WRPSTATE(pOBInit->WRPState));
  229. if(pOBInit->WRPState == OB_WRPSTATE_ENABLE)
  230. {
  231. /*Enable of Write protection on the selected Sector*/
  232. status = FLASH_OB_EnableWRP(pOBInit->WRPSector, pOBInit->Banks);
  233. }
  234. else
  235. {
  236. /*Disable of Write protection on the selected Sector*/
  237. status = FLASH_OB_DisableWRP(pOBInit->WRPSector, pOBInit->Banks);
  238. }
  239. }
  240. /*Read protection configuration*/
  241. if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
  242. {
  243. status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
  244. }
  245. /*USER configuration*/
  246. if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
  247. {
  248. status = FLASH_OB_UserConfig(pOBInit->USERConfig&OB_IWDG_SW,
  249. pOBInit->USERConfig&OB_STOP_NO_RST,
  250. pOBInit->USERConfig&OB_STDBY_NO_RST);
  251. }
  252. /*BOR Level configuration*/
  253. if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
  254. {
  255. status = FLASH_OB_BOR_LevelConfig(pOBInit->BORLevel);
  256. }
  257. /* Process Unlocked */
  258. __HAL_UNLOCK(&pFlash);
  259. return status;
  260. }
  261. /**
  262. * @brief Get the Option byte configuration
  263. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  264. * contains the configuration information for the programming.
  265. *
  266. * @retval None
  267. */
  268. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
  269. {
  270. pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
  271. /*Get WRP*/
  272. pOBInit->WRPSector = (uint32_t)FLASH_OB_GetWRP();
  273. /*Get RDP Level*/
  274. pOBInit->RDPLevel = (uint32_t)FLASH_OB_GetRDP();
  275. /*Get USER*/
  276. pOBInit->USERConfig = (uint8_t)FLASH_OB_GetUser();
  277. /*Get BOR Level*/
  278. pOBInit->BORLevel = (uint32_t)FLASH_OB_GetBOR();
  279. }
  280. /**
  281. * @}
  282. */
  283. /**
  284. * @brief Erase the specified FLASH memory sector
  285. * @param Sector FLASH sector to erase
  286. * The value of this parameter depend on device used within the same series
  287. * @param VoltageRange The device voltage range which defines the erase parallelism.
  288. * This parameter can be one of the following values:
  289. * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
  290. * the operation will be done by byte (8-bit)
  291. * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
  292. * the operation will be done by half word (16-bit)
  293. * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
  294. * the operation will be done by word (32-bit)
  295. * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
  296. * the operation will be done by double word (64-bit)
  297. *
  298. * @retval None
  299. */
  300. void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
  301. {
  302. uint32_t tmp_psize = 0U;
  303. /* Check the parameters */
  304. assert_param(IS_FLASH_SECTOR(Sector));
  305. assert_param(IS_VOLTAGERANGE(VoltageRange));
  306. if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
  307. {
  308. tmp_psize = FLASH_PSIZE_BYTE;
  309. }
  310. else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
  311. {
  312. tmp_psize = FLASH_PSIZE_HALF_WORD;
  313. }
  314. else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
  315. {
  316. tmp_psize = FLASH_PSIZE_WORD;
  317. }
  318. else
  319. {
  320. tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
  321. }
  322. /* If the previous operation is completed, proceed to erase the sector */
  323. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  324. FLASH->CR |= tmp_psize;
  325. CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
  326. FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
  327. FLASH->CR |= FLASH_CR_STRT;
  328. }
  329. /**
  330. * @brief Flush the instruction and data caches
  331. * @retval None
  332. */
  333. void FLASH_FlushCaches(void)
  334. {
  335. /* Flush instruction cache */
  336. if(READ_BIT(FLASH->ACR, FLASH_ACR_ICEN) != RESET)
  337. {
  338. /* Disable instruction cache */
  339. __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
  340. /* Reset instruction cache */
  341. __HAL_FLASH_INSTRUCTION_CACHE_RESET();
  342. /* Enable instruction cache */
  343. __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
  344. }
  345. /* Flush data cache */
  346. if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != RESET)
  347. {
  348. /* Disable data cache */
  349. __HAL_FLASH_DATA_CACHE_DISABLE();
  350. /* Reset data cache */
  351. __HAL_FLASH_DATA_CACHE_RESET();
  352. /* Enable data cache */
  353. __HAL_FLASH_DATA_CACHE_ENABLE();
  354. }
  355. }
  356. /**
  357. * @brief Mass erase of FLASH memory
  358. * @param VoltageRange The device voltage range which defines the erase parallelism.
  359. * This parameter can be one of the following values:
  360. * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
  361. * the operation will be done by byte (8-bit)
  362. * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
  363. * the operation will be done by half word (16-bit)
  364. * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
  365. * the operation will be done by word (32-bit)
  366. * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
  367. * the operation will be done by double word (64-bit)
  368. *
  369. * @param Banks Banks to be erased
  370. * This parameter can be one of the following values:
  371. * @arg FLASH_BANK_1: Bank1 to be erased
  372. *
  373. * @retval None
  374. */
  375. static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks)
  376. {
  377. /* Prevent unused argument(s) compilation warning */
  378. UNUSED(Banks);
  379. /* Check the parameters */
  380. assert_param(IS_VOLTAGERANGE(VoltageRange));
  381. assert_param(IS_FLASH_BANK(Banks));
  382. /* If the previous operation is completed, proceed to erase all sectors */
  383. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  384. FLASH->CR |= FLASH_CR_MER;
  385. FLASH->CR |= FLASH_CR_STRT | ((uint32_t)VoltageRange <<8U);
  386. }
  387. /**
  388. * @brief Enable the write protection of the desired bank 1 sectors
  389. *
  390. * @note When the memory read protection level is selected (RDP level = 1),
  391. * it is not possible to program or erase the flash sector i if CortexM3
  392. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  393. *
  394. * @param WRPSector specifies the sector(s) to be write protected.
  395. * The value of this parameter depend on device used within the same series
  396. *
  397. * @param Banks Enable write protection on all the sectors for the specific bank
  398. * This parameter can be one of the following values:
  399. * @arg FLASH_BANK_1: WRP on all sectors of bank1
  400. *
  401. * @retval HAL Status
  402. */
  403. static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
  404. {
  405. HAL_StatusTypeDef status = HAL_OK;
  406. /* Prevent unused argument(s) compilation warning */
  407. UNUSED(Banks);
  408. /* Check the parameters */
  409. assert_param(IS_OB_WRP_SECTOR(WRPSector));
  410. assert_param(IS_FLASH_BANK(Banks));
  411. /* Wait for last operation to be completed */
  412. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  413. if(status == HAL_OK)
  414. {
  415. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~WRPSector);
  416. }
  417. return status;
  418. }
  419. /**
  420. * @brief Disable the write protection of the desired bank 1 sectors
  421. *
  422. * @note When the memory read protection level is selected (RDP level = 1),
  423. * it is not possible to program or erase the flash sector if CortexM3
  424. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  425. *
  426. * @param WRPSector specifies the sector(s) to be write protected.
  427. * The value of this parameter depend on device used within the same series
  428. *
  429. * @param Banks Enable write protection on all the sectors for the specific bank
  430. * This parameter can be one of the following values:
  431. * @arg FLASH_BANK_1: WRP on all sectors of bank1
  432. *
  433. * @retval HAL Status
  434. */
  435. static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
  436. {
  437. HAL_StatusTypeDef status = HAL_OK;
  438. /* Prevent unused argument(s) compilation warning */
  439. UNUSED(Banks);
  440. /* Check the parameters */
  441. assert_param(IS_OB_WRP_SECTOR(WRPSector));
  442. assert_param(IS_FLASH_BANK(Banks));
  443. /* Wait for last operation to be completed */
  444. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  445. if(status == HAL_OK)
  446. {
  447. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)WRPSector;
  448. }
  449. return status;
  450. }
  451. /**
  452. * @brief Set the read protection level.
  453. * @param Level specifies the read protection level.
  454. * This parameter can be one of the following values:
  455. * @arg OB_RDP_LEVEL_0: No protection
  456. * @arg OB_RDP_LEVEL_1: Read protection of the memory
  457. * @arg OB_RDP_LEVEL_2: Full chip protection
  458. *
  459. * @note WARNING: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
  460. *
  461. * @retval HAL Status
  462. */
  463. static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level)
  464. {
  465. HAL_StatusTypeDef status = HAL_OK;
  466. /* Check the parameters */
  467. assert_param(IS_OB_RDP_LEVEL(Level));
  468. /* Wait for last operation to be completed */
  469. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  470. if(status == HAL_OK)
  471. {
  472. *(__IO uint8_t*)OPTCR_BYTE1_ADDRESS = Level;
  473. }
  474. return status;
  475. }
  476. /**
  477. * @brief Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  478. * @param Iwdg Selects the IWDG mode
  479. * This parameter can be one of the following values:
  480. * @arg OB_IWDG_SW: Software IWDG selected
  481. * @arg OB_IWDG_HW: Hardware IWDG selected
  482. * @param Stop Reset event when entering STOP mode.
  483. * This parameter can be one of the following values:
  484. * @arg OB_STOP_NO_RST: No reset generated when entering in STOP
  485. * @arg OB_STOP_RST: Reset generated when entering in STOP
  486. * @param Stdby Reset event when entering Standby mode.
  487. * This parameter can be one of the following values:
  488. * @arg OB_STDBY_NO_RST: No reset generated when entering in STANDBY
  489. * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
  490. * @retval HAL Status
  491. */
  492. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t Iwdg, uint8_t Stop, uint8_t Stdby)
  493. {
  494. uint8_t optiontmp = 0xFF;
  495. HAL_StatusTypeDef status = HAL_OK;
  496. /* Check the parameters */
  497. assert_param(IS_OB_IWDG_SOURCE(Iwdg));
  498. assert_param(IS_OB_STOP_SOURCE(Stop));
  499. assert_param(IS_OB_STDBY_SOURCE(Stdby));
  500. /* Wait for last operation to be completed */
  501. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  502. if(status == HAL_OK)
  503. {
  504. /* Mask OPTLOCK, OPTSTRT, BOR_LEV and BFB2 bits */
  505. optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS) & (uint8_t)0x1F);
  506. /* Update User Option Byte */
  507. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS = Iwdg | (uint8_t)(Stdby | (uint8_t)(Stop | ((uint8_t)optiontmp)));
  508. }
  509. return status;
  510. }
  511. /**
  512. * @brief Set the BOR Level.
  513. * @param Level specifies the Option Bytes BOR Reset Level.
  514. * This parameter can be one of the following values:
  515. * @arg OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
  516. * @arg OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
  517. * @arg OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
  518. * @arg OB_BOR_OFF: Supply voltage ranges from 1.62 to 2.1 V
  519. * @retval HAL Status
  520. */
  521. static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level)
  522. {
  523. /* Check the parameters */
  524. assert_param(IS_OB_BOR_LEVEL(Level));
  525. /* Set the BOR Level */
  526. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BOR_LEV);
  527. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= Level;
  528. return HAL_OK;
  529. }
  530. /**
  531. * @brief Return the FLASH User Option Byte value.
  532. * @retval uint8_t FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1)
  533. * and RST_STDBY(Bit2).
  534. */
  535. static uint8_t FLASH_OB_GetUser(void)
  536. {
  537. /* Return the User Option Byte */
  538. return ((uint8_t)(FLASH->OPTCR & 0xE0));
  539. }
  540. /**
  541. * @brief Return the FLASH Write Protection Option Bytes value.
  542. * @retval uint16_t FLASH Write Protection Option Bytes value
  543. */
  544. static uint16_t FLASH_OB_GetWRP(void)
  545. {
  546. /* Return the FLASH write protection Register value */
  547. return (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
  548. }
  549. /**
  550. * @brief Returns the FLASH Read Protection level.
  551. * @retval FLASH ReadOut Protection Status:
  552. * This parameter can be one of the following values:
  553. * @arg OB_RDP_LEVEL_0: No protection
  554. * @arg OB_RDP_LEVEL_1: Read protection of the memory
  555. * @arg OB_RDP_LEVEL_2: Full chip protection
  556. */
  557. static uint8_t FLASH_OB_GetRDP(void)
  558. {
  559. uint8_t readstatus = OB_RDP_LEVEL_0;
  560. if((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) == (uint8_t)OB_RDP_LEVEL_2))
  561. {
  562. readstatus = OB_RDP_LEVEL_2;
  563. }
  564. else if((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) == (uint8_t)OB_RDP_LEVEL_0))
  565. {
  566. readstatus = OB_RDP_LEVEL_0;
  567. }
  568. else
  569. {
  570. readstatus = OB_RDP_LEVEL_1;
  571. }
  572. return readstatus;
  573. }
  574. /**
  575. * @brief Returns the FLASH BOR level.
  576. * @retval uint8_t The FLASH BOR level:
  577. * - OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
  578. * - OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
  579. * - OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
  580. * - OB_BOR_OFF : Supply voltage ranges from 1.62 to 2.1 V
  581. */
  582. static uint8_t FLASH_OB_GetBOR(void)
  583. {
  584. /* Return the FLASH BOR level */
  585. return (uint8_t)(*(__IO uint8_t *)(OPTCR_BYTE0_ADDRESS) & (uint8_t)0x0C);
  586. }
  587. /**
  588. * @}
  589. */
  590. #endif /* HAL_FLASH_MODULE_ENABLED */
  591. /**
  592. * @}
  593. */
  594. /**
  595. * @}
  596. */
  597. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/