stm32f4xx_hal_flash_ex.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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 STM32F427xx/437xx and
  15. STM32F429xx/439xx 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. (+) PCROP protection for all banks
  20. ##### How to use this driver #####
  21. ==============================================================================
  22. [..] This driver provides functions to configure and program the FLASH memory
  23. of all STM32F427xx/437xx, STM32F429xx/439xx, STM32F469xx/479xx and STM32F446xx
  24. devices. It includes
  25. (#) FLASH Memory Erase functions:
  26. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  27. HAL_FLASH_Lock() functions
  28. (++) Erase function: Erase sector, erase all sectors
  29. (++) There are two modes of erase :
  30. (+++) Polling Mode using HAL_FLASHEx_Erase()
  31. (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
  32. (#) Option Bytes Programming functions: Use HAL_FLASHEx_OBProgram() to :
  33. (++) Set/Reset the write protection
  34. (++) Set the Read protection Level
  35. (++) Set the BOR level
  36. (++) Program the user Option Bytes
  37. (#) Advanced Option Bytes Programming functions: Use HAL_FLASHEx_AdvOBProgram() to :
  38. (++) Extended space (bank 2) erase function
  39. (++) Full FLASH space (2 Mo) erase (bank 1 and bank 2)
  40. (++) Dual Boot activation
  41. (++) Write protection configuration for bank 2
  42. (++) PCROP protection configuration and control for both banks
  43. @endverbatim
  44. ******************************************************************************
  45. * @attention
  46. *
  47. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  48. *
  49. * Redistribution and use in source and binary forms, with or without modification,
  50. * are permitted provided that the following conditions are met:
  51. * 1. Redistributions of source code must retain the above copyright notice,
  52. * this list of conditions and the following disclaimer.
  53. * 2. Redistributions in binary form must reproduce the above copyright notice,
  54. * this list of conditions and the following disclaimer in the documentation
  55. * and/or other materials provided with the distribution.
  56. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  57. * may be used to endorse or promote products derived from this software
  58. * without specific prior written permission.
  59. *
  60. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  61. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  62. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  63. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  64. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  65. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  66. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  67. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  68. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  69. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  70. *
  71. ******************************************************************************
  72. */
  73. /* Includes ------------------------------------------------------------------*/
  74. #include "stm32f4xx_hal.h"
  75. /** @addtogroup STM32F4xx_HAL_Driver
  76. * @{
  77. */
  78. /** @defgroup FLASHEx FLASHEx
  79. * @brief FLASH HAL Extension module driver
  80. * @{
  81. */
  82. #ifdef HAL_FLASH_MODULE_ENABLED
  83. /* Private typedef -----------------------------------------------------------*/
  84. /* Private define ------------------------------------------------------------*/
  85. /** @addtogroup FLASHEx_Private_Constants
  86. * @{
  87. */
  88. #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
  89. /**
  90. * @}
  91. */
  92. /* Private macro -------------------------------------------------------------*/
  93. /* Private variables ---------------------------------------------------------*/
  94. /** @addtogroup FLASHEx_Private_Variables
  95. * @{
  96. */
  97. extern FLASH_ProcessTypeDef pFlash;
  98. /**
  99. * @}
  100. */
  101. /* Private function prototypes -----------------------------------------------*/
  102. /** @addtogroup FLASHEx_Private_Functions
  103. * @{
  104. */
  105. /* Option bytes control */
  106. static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks);
  107. static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks);
  108. static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks);
  109. static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level);
  110. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t Iwdg, uint8_t Stop, uint8_t Stdby);
  111. static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level);
  112. static uint8_t FLASH_OB_GetUser(void);
  113. static uint16_t FLASH_OB_GetWRP(void);
  114. static uint8_t FLASH_OB_GetRDP(void);
  115. static uint8_t FLASH_OB_GetBOR(void);
  116. #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F411xE) ||\
  117. defined(STM32F446xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) ||\
  118. defined(STM32F423xx)
  119. static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t Sector);
  120. static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t Sector);
  121. #endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx
  122. STM32F413xx || STM32F423xx */
  123. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
  124. static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks);
  125. static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks);
  126. static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t BootConfig);
  127. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
  128. extern HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  129. /**
  130. * @}
  131. */
  132. /* Exported functions --------------------------------------------------------*/
  133. /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
  134. * @{
  135. */
  136. /** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
  137. * @brief Extended IO operation functions
  138. *
  139. @verbatim
  140. ===============================================================================
  141. ##### Extended programming operation functions #####
  142. ===============================================================================
  143. [..]
  144. This subsection provides a set of functions allowing to manage the Extension FLASH
  145. programming operations.
  146. @endverbatim
  147. * @{
  148. */
  149. /**
  150. * @brief Perform a mass erase or erase the specified FLASH memory sectors
  151. * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  152. * contains the configuration information for the erasing.
  153. *
  154. * @param[out] SectorError pointer to variable that
  155. * contains the configuration information on faulty sector in case of error
  156. * (0xFFFFFFFFU means that all the sectors have been correctly erased)
  157. *
  158. * @retval HAL Status
  159. */
  160. HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
  161. {
  162. HAL_StatusTypeDef status = HAL_ERROR;
  163. uint32_t index = 0U;
  164. /* Process Locked */
  165. __HAL_LOCK(&pFlash);
  166. /* Check the parameters */
  167. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  168. /* Wait for last operation to be completed */
  169. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  170. if(status == HAL_OK)
  171. {
  172. /*Initialization of SectorError variable*/
  173. *SectorError = 0xFFFFFFFFU;
  174. if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  175. {
  176. /*Mass erase to be done*/
  177. FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
  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 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. /* Flush the caches to be sure of the data consistency */
  204. FLASH_FlushCaches();
  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_PGSERR);
  231. if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  232. {
  233. /*Mass erase to be done*/
  234. pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
  235. pFlash.Bank = pEraseInit->Banks;
  236. FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
  237. }
  238. else
  239. {
  240. /* Erase by sector to be done*/
  241. /* Check the parameters */
  242. assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
  243. pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE;
  244. pFlash.NbSectorsToErase = pEraseInit->NbSectors;
  245. pFlash.Sector = pEraseInit->Sector;
  246. pFlash.VoltageForErase = (uint8_t)pEraseInit->VoltageRange;
  247. /*Erase 1st sector and wait for IT*/
  248. FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->VoltageRange);
  249. }
  250. return status;
  251. }
  252. /**
  253. * @brief Program option bytes
  254. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  255. * contains the configuration information for the programming.
  256. *
  257. * @retval HAL Status
  258. */
  259. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
  260. {
  261. HAL_StatusTypeDef status = HAL_ERROR;
  262. /* Process Locked */
  263. __HAL_LOCK(&pFlash);
  264. /* Check the parameters */
  265. assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
  266. /*Write protection configuration*/
  267. if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
  268. {
  269. assert_param(IS_WRPSTATE(pOBInit->WRPState));
  270. if(pOBInit->WRPState == OB_WRPSTATE_ENABLE)
  271. {
  272. /*Enable of Write protection on the selected Sector*/
  273. status = FLASH_OB_EnableWRP(pOBInit->WRPSector, pOBInit->Banks);
  274. }
  275. else
  276. {
  277. /*Disable of Write protection on the selected Sector*/
  278. status = FLASH_OB_DisableWRP(pOBInit->WRPSector, pOBInit->Banks);
  279. }
  280. }
  281. /*Read protection configuration*/
  282. if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
  283. {
  284. status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
  285. }
  286. /*USER configuration*/
  287. if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
  288. {
  289. status = FLASH_OB_UserConfig(pOBInit->USERConfig&OB_IWDG_SW,
  290. pOBInit->USERConfig&OB_STOP_NO_RST,
  291. pOBInit->USERConfig&OB_STDBY_NO_RST);
  292. }
  293. /*BOR Level configuration*/
  294. if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
  295. {
  296. status = FLASH_OB_BOR_LevelConfig(pOBInit->BORLevel);
  297. }
  298. /* Process Unlocked */
  299. __HAL_UNLOCK(&pFlash);
  300. return status;
  301. }
  302. /**
  303. * @brief Get the Option byte configuration
  304. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  305. * contains the configuration information for the programming.
  306. *
  307. * @retval None
  308. */
  309. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
  310. {
  311. pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
  312. /*Get WRP*/
  313. pOBInit->WRPSector = (uint32_t)FLASH_OB_GetWRP();
  314. /*Get RDP Level*/
  315. pOBInit->RDPLevel = (uint32_t)FLASH_OB_GetRDP();
  316. /*Get USER*/
  317. pOBInit->USERConfig = (uint8_t)FLASH_OB_GetUser();
  318. /*Get BOR Level*/
  319. pOBInit->BORLevel = (uint32_t)FLASH_OB_GetBOR();
  320. }
  321. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  322. defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) ||\
  323. defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F469xx) ||\
  324. defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
  325. defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  326. /**
  327. * @brief Program option bytes
  328. * @param pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
  329. * contains the configuration information for the programming.
  330. *
  331. * @retval HAL Status
  332. */
  333. HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
  334. {
  335. HAL_StatusTypeDef status = HAL_ERROR;
  336. /* Check the parameters */
  337. assert_param(IS_OBEX(pAdvOBInit->OptionType));
  338. /*Program PCROP option byte*/
  339. if(((pAdvOBInit->OptionType) & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
  340. {
  341. /* Check the parameters */
  342. assert_param(IS_PCROPSTATE(pAdvOBInit->PCROPState));
  343. if((pAdvOBInit->PCROPState) == OB_PCROP_STATE_ENABLE)
  344. {
  345. /*Enable of Write protection on the selected Sector*/
  346. #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
  347. defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
  348. defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  349. status = FLASH_OB_EnablePCROP(pAdvOBInit->Sectors);
  350. #else /* STM32F427xx || STM32F437xx || STM32F429xx|| STM32F439xx || STM32F469xx || STM32F479xx */
  351. status = FLASH_OB_EnablePCROP(pAdvOBInit->SectorsBank1, pAdvOBInit->SectorsBank2, pAdvOBInit->Banks);
  352. #endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx ||
  353. STM32F413xx || STM32F423xx */
  354. }
  355. else
  356. {
  357. /*Disable of Write protection on the selected Sector*/
  358. #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
  359. defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
  360. defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  361. status = FLASH_OB_DisablePCROP(pAdvOBInit->Sectors);
  362. #else /* STM32F427xx || STM32F437xx || STM32F429xx|| STM32F439xx || STM32F469xx || STM32F479xx */
  363. status = FLASH_OB_DisablePCROP(pAdvOBInit->SectorsBank1, pAdvOBInit->SectorsBank2, pAdvOBInit->Banks);
  364. #endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx ||
  365. STM32F413xx || STM32F423xx */
  366. }
  367. }
  368. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
  369. /*Program BOOT config option byte*/
  370. if(((pAdvOBInit->OptionType) & OPTIONBYTE_BOOTCONFIG) == OPTIONBYTE_BOOTCONFIG)
  371. {
  372. status = FLASH_OB_BootConfig(pAdvOBInit->BootConfig);
  373. }
  374. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
  375. return status;
  376. }
  377. /**
  378. * @brief Get the OBEX byte configuration
  379. * @param pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
  380. * contains the configuration information for the programming.
  381. *
  382. * @retval None
  383. */
  384. void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
  385. {
  386. #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
  387. defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
  388. defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  389. /*Get Sector*/
  390. pAdvOBInit->Sectors = (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
  391. #else /* STM32F427xx || STM32F437xx || STM32F429xx|| STM32F439xx || STM32F469xx || STM32F479xx */
  392. /*Get Sector for Bank1*/
  393. pAdvOBInit->SectorsBank1 = (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
  394. /*Get Sector for Bank2*/
  395. pAdvOBInit->SectorsBank2 = (*(__IO uint16_t *)(OPTCR1_BYTE2_ADDRESS));
  396. /*Get Boot config OB*/
  397. pAdvOBInit->BootConfig = *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS;
  398. #endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx ||
  399. STM32F413xx || STM32F423xx */
  400. }
  401. /**
  402. * @brief Select the Protection Mode
  403. *
  404. * @note After PCROP activated Option Byte modification NOT POSSIBLE! excepted
  405. * Global Read Out Protection modification (from level1 to level0)
  406. * @note Once SPRMOD bit is active unprotection of a protected sector is not possible
  407. * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
  408. * @note This function can be used only for STM32F42xxx/STM32F43xxx/STM32F401xx/STM32F411xx/STM32F446xx/
  409. * STM32F469xx/STM32F479xx/STM32F412xx/STM32F413xx devices.
  410. *
  411. * @retval HAL Status
  412. */
  413. HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
  414. {
  415. uint8_t optiontmp = 0xFF;
  416. /* Mask SPRMOD bit */
  417. optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE3_ADDRESS) & (uint8_t)0x7F);
  418. /* Update Option Byte */
  419. *(__IO uint8_t *)OPTCR_BYTE3_ADDRESS = (uint8_t)(OB_PCROP_SELECTED | optiontmp);
  420. return HAL_OK;
  421. }
  422. /**
  423. * @brief Deselect the Protection Mode
  424. *
  425. * @note After PCROP activated Option Byte modification NOT POSSIBLE! excepted
  426. * Global Read Out Protection modification (from level1 to level0)
  427. * @note Once SPRMOD bit is active unprotection of a protected sector is not possible
  428. * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
  429. * @note This function can be used only for STM32F42xxx/STM32F43xxx/STM32F401xx/STM32F411xx/STM32F446xx/
  430. * STM32F469xx/STM32F479xx/STM32F412xx/STM32F413xx devices.
  431. *
  432. * @retval HAL Status
  433. */
  434. HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
  435. {
  436. uint8_t optiontmp = 0xFF;
  437. /* Mask SPRMOD bit */
  438. optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE3_ADDRESS) & (uint8_t)0x7F);
  439. /* Update Option Byte */
  440. *(__IO uint8_t *)OPTCR_BYTE3_ADDRESS = (uint8_t)(OB_PCROP_DESELECTED | optiontmp);
  441. return HAL_OK;
  442. }
  443. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F401xC || STM32F401xE || STM32F410xx ||\
  444. STM32F411xE || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx ||
  445. STM32F413xx || STM32F423xx */
  446. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
  447. /**
  448. * @brief Returns the FLASH Write Protection Option Bytes value for Bank 2
  449. * @note This function can be used only for STM32F42xxx/STM32F43xxx/STM32F469xx/STM32F479xx devices.
  450. * @retval The FLASH Write Protection Option Bytes value
  451. */
  452. uint16_t HAL_FLASHEx_OB_GetBank2WRP(void)
  453. {
  454. /* Return the FLASH write protection Register value */
  455. return (*(__IO uint16_t *)(OPTCR1_BYTE2_ADDRESS));
  456. }
  457. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
  458. /**
  459. * @}
  460. */
  461. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
  462. /**
  463. * @brief Full erase of FLASH memory sectors
  464. * @param VoltageRange The device voltage range which defines the erase parallelism.
  465. * This parameter can be one of the following values:
  466. * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
  467. * the operation will be done by byte (8-bit)
  468. * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
  469. * the operation will be done by half word (16-bit)
  470. * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
  471. * the operation will be done by word (32-bit)
  472. * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
  473. * the operation will be done by double word (64-bit)
  474. *
  475. * @param Banks Banks to be erased
  476. * This parameter can be one of the following values:
  477. * @arg FLASH_BANK_1: Bank1 to be erased
  478. * @arg FLASH_BANK_2: Bank2 to be erased
  479. * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
  480. *
  481. * @retval HAL Status
  482. */
  483. static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks)
  484. {
  485. /* Check the parameters */
  486. assert_param(IS_VOLTAGERANGE(VoltageRange));
  487. assert_param(IS_FLASH_BANK(Banks));
  488. /* if the previous operation is completed, proceed to erase all sectors */
  489. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  490. if(Banks == FLASH_BANK_BOTH)
  491. {
  492. /* bank1 & bank2 will be erased*/
  493. FLASH->CR |= FLASH_MER_BIT;
  494. }
  495. else if(Banks == FLASH_BANK_1)
  496. {
  497. /*Only bank1 will be erased*/
  498. FLASH->CR |= FLASH_CR_MER1;
  499. }
  500. else
  501. {
  502. /*Only bank2 will be erased*/
  503. FLASH->CR |= FLASH_CR_MER2;
  504. }
  505. FLASH->CR |= FLASH_CR_STRT | ((uint32_t)VoltageRange <<8U);
  506. }
  507. /**
  508. * @brief Erase the specified FLASH memory sector
  509. * @param Sector FLASH sector to erase
  510. * The value of this parameter depend on device used within the same series
  511. * @param VoltageRange The device voltage range which defines the erase parallelism.
  512. * This parameter can be one of the following values:
  513. * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
  514. * the operation will be done by byte (8-bit)
  515. * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
  516. * the operation will be done by half word (16-bit)
  517. * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
  518. * the operation will be done by word (32-bit)
  519. * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
  520. * the operation will be done by double word (64-bit)
  521. *
  522. * @retval None
  523. */
  524. void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
  525. {
  526. uint32_t tmp_psize = 0U;
  527. /* Check the parameters */
  528. assert_param(IS_FLASH_SECTOR(Sector));
  529. assert_param(IS_VOLTAGERANGE(VoltageRange));
  530. if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
  531. {
  532. tmp_psize = FLASH_PSIZE_BYTE;
  533. }
  534. else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
  535. {
  536. tmp_psize = FLASH_PSIZE_HALF_WORD;
  537. }
  538. else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
  539. {
  540. tmp_psize = FLASH_PSIZE_WORD;
  541. }
  542. else
  543. {
  544. tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
  545. }
  546. /* Need to add offset of 4 when sector higher than FLASH_SECTOR_11 */
  547. if(Sector > FLASH_SECTOR_11)
  548. {
  549. Sector += 4U;
  550. }
  551. /* If the previous operation is completed, proceed to erase the sector */
  552. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  553. FLASH->CR |= tmp_psize;
  554. CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
  555. FLASH->CR |= FLASH_CR_SER | (Sector << FLASH_CR_SNB_Pos);
  556. FLASH->CR |= FLASH_CR_STRT;
  557. }
  558. /**
  559. * @brief Enable the write protection of the desired bank1 or bank 2 sectors
  560. *
  561. * @note When the memory read protection level is selected (RDP level = 1),
  562. * it is not possible to program or erase the flash sector i if CortexM4
  563. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  564. * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
  565. *
  566. * @param WRPSector specifies the sector(s) to be write protected.
  567. * This parameter can be one of the following values:
  568. * @arg WRPSector: A value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_23
  569. * @arg OB_WRP_SECTOR_All
  570. * @note BANK2 starts from OB_WRP_SECTOR_12
  571. *
  572. * @param Banks Enable write protection on all the sectors for the specific bank
  573. * This parameter can be one of the following values:
  574. * @arg FLASH_BANK_1: WRP on all sectors of bank1
  575. * @arg FLASH_BANK_2: WRP on all sectors of bank2
  576. * @arg FLASH_BANK_BOTH: WRP on all sectors of bank1 & bank2
  577. *
  578. * @retval HAL FLASH State
  579. */
  580. static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
  581. {
  582. HAL_StatusTypeDef status = HAL_OK;
  583. /* Check the parameters */
  584. assert_param(IS_OB_WRP_SECTOR(WRPSector));
  585. assert_param(IS_FLASH_BANK(Banks));
  586. /* Wait for last operation to be completed */
  587. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  588. if(status == HAL_OK)
  589. {
  590. if(((WRPSector == OB_WRP_SECTOR_All) && ((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))) ||
  591. (WRPSector < OB_WRP_SECTOR_12))
  592. {
  593. if(WRPSector == OB_WRP_SECTOR_All)
  594. {
  595. /*Write protection on all sector of BANK1*/
  596. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~(WRPSector>>12));
  597. }
  598. else
  599. {
  600. /*Write protection done on sectors of BANK1*/
  601. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~WRPSector);
  602. }
  603. }
  604. else
  605. {
  606. /*Write protection done on sectors of BANK2*/
  607. *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~(WRPSector>>12));
  608. }
  609. /*Write protection on all sector of BANK2*/
  610. if((WRPSector == OB_WRP_SECTOR_All) && (Banks == FLASH_BANK_BOTH))
  611. {
  612. /* Wait for last operation to be completed */
  613. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  614. if(status == HAL_OK)
  615. {
  616. *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~(WRPSector>>12));
  617. }
  618. }
  619. }
  620. return status;
  621. }
  622. /**
  623. * @brief Disable the write protection of the desired bank1 or bank 2 sectors
  624. *
  625. * @note When the memory read protection level is selected (RDP level = 1),
  626. * it is not possible to program or erase the flash sector i if CortexM4
  627. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  628. * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
  629. *
  630. * @param WRPSector specifies the sector(s) to be write protected.
  631. * This parameter can be one of the following values:
  632. * @arg WRPSector: A value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_23
  633. * @arg OB_WRP_Sector_All
  634. * @note BANK2 starts from OB_WRP_SECTOR_12
  635. *
  636. * @param Banks Disable write protection on all the sectors for the specific bank
  637. * This parameter can be one of the following values:
  638. * @arg FLASH_BANK_1: Bank1 to be erased
  639. * @arg FLASH_BANK_2: Bank2 to be erased
  640. * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
  641. *
  642. * @retval HAL Status
  643. */
  644. static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
  645. {
  646. HAL_StatusTypeDef status = HAL_OK;
  647. /* Check the parameters */
  648. assert_param(IS_OB_WRP_SECTOR(WRPSector));
  649. assert_param(IS_FLASH_BANK(Banks));
  650. /* Wait for last operation to be completed */
  651. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  652. if(status == HAL_OK)
  653. {
  654. if(((WRPSector == OB_WRP_SECTOR_All) && ((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))) ||
  655. (WRPSector < OB_WRP_SECTOR_12))
  656. {
  657. if(WRPSector == OB_WRP_SECTOR_All)
  658. {
  659. /*Write protection on all sector of BANK1*/
  660. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)(WRPSector>>12);
  661. }
  662. else
  663. {
  664. /*Write protection done on sectors of BANK1*/
  665. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)WRPSector;
  666. }
  667. }
  668. else
  669. {
  670. /*Write protection done on sectors of BANK2*/
  671. *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)(WRPSector>>12);
  672. }
  673. /*Write protection on all sector of BANK2*/
  674. if((WRPSector == OB_WRP_SECTOR_All) && (Banks == FLASH_BANK_BOTH))
  675. {
  676. /* Wait for last operation to be completed */
  677. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  678. if(status == HAL_OK)
  679. {
  680. *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)(WRPSector>>12);
  681. }
  682. }
  683. }
  684. return status;
  685. }
  686. /**
  687. * @brief Configure the Dual Bank Boot.
  688. *
  689. * @note This function can be used only for STM32F42xxx/43xxx devices.
  690. *
  691. * @param BootConfig specifies the Dual Bank Boot Option byte.
  692. * This parameter can be one of the following values:
  693. * @arg OB_Dual_BootEnabled: Dual Bank Boot Enable
  694. * @arg OB_Dual_BootDisabled: Dual Bank Boot Disabled
  695. * @retval None
  696. */
  697. static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t BootConfig)
  698. {
  699. HAL_StatusTypeDef status = HAL_OK;
  700. /* Check the parameters */
  701. assert_param(IS_OB_BOOT(BootConfig));
  702. /* Wait for last operation to be completed */
  703. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  704. if(status == HAL_OK)
  705. {
  706. /* Set Dual Bank Boot */
  707. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BFB2);
  708. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= BootConfig;
  709. }
  710. return status;
  711. }
  712. /**
  713. * @brief Enable the read/write protection (PCROP) of the desired
  714. * sectors of Bank 1 and/or Bank 2.
  715. * @note This function can be used only for STM32F42xxx/43xxx devices.
  716. * @param SectorBank1 Specifies the sector(s) to be read/write protected or unprotected for bank1.
  717. * This parameter can be one of the following values:
  718. * @arg OB_PCROP: A value between OB_PCROP_SECTOR_0 and OB_PCROP_SECTOR_11
  719. * @arg OB_PCROP_SECTOR__All
  720. * @param SectorBank2 Specifies the sector(s) to be read/write protected or unprotected for bank2.
  721. * This parameter can be one of the following values:
  722. * @arg OB_PCROP: A value between OB_PCROP_SECTOR_12 and OB_PCROP_SECTOR_23
  723. * @arg OB_PCROP_SECTOR__All
  724. * @param Banks Enable PCROP protection on all the sectors for the specific bank
  725. * This parameter can be one of the following values:
  726. * @arg FLASH_BANK_1: WRP on all sectors of bank1
  727. * @arg FLASH_BANK_2: WRP on all sectors of bank2
  728. * @arg FLASH_BANK_BOTH: WRP on all sectors of bank1 & bank2
  729. *
  730. * @retval HAL Status
  731. */
  732. static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks)
  733. {
  734. HAL_StatusTypeDef status = HAL_OK;
  735. assert_param(IS_FLASH_BANK(Banks));
  736. /* Wait for last operation to be completed */
  737. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  738. if(status == HAL_OK)
  739. {
  740. if((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))
  741. {
  742. assert_param(IS_OB_PCROP(SectorBank1));
  743. /*Write protection done on sectors of BANK1*/
  744. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)SectorBank1;
  745. }
  746. else
  747. {
  748. assert_param(IS_OB_PCROP(SectorBank2));
  749. /*Write protection done on sectors of BANK2*/
  750. *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)SectorBank2;
  751. }
  752. /*Write protection on all sector of BANK2*/
  753. if(Banks == FLASH_BANK_BOTH)
  754. {
  755. assert_param(IS_OB_PCROP(SectorBank2));
  756. /* Wait for last operation to be completed */
  757. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  758. if(status == HAL_OK)
  759. {
  760. /*Write protection done on sectors of BANK2*/
  761. *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)SectorBank2;
  762. }
  763. }
  764. }
  765. return status;
  766. }
  767. /**
  768. * @brief Disable the read/write protection (PCROP) of the desired
  769. * sectors of Bank 1 and/or Bank 2.
  770. * @note This function can be used only for STM32F42xxx/43xxx devices.
  771. * @param SectorBank1 specifies the sector(s) to be read/write protected or unprotected for bank1.
  772. * This parameter can be one of the following values:
  773. * @arg OB_PCROP: A value between OB_PCROP_SECTOR_0 and OB_PCROP_SECTOR_11
  774. * @arg OB_PCROP_SECTOR__All
  775. * @param SectorBank2 Specifies the sector(s) to be read/write protected or unprotected for bank2.
  776. * This parameter can be one of the following values:
  777. * @arg OB_PCROP: A value between OB_PCROP_SECTOR_12 and OB_PCROP_SECTOR_23
  778. * @arg OB_PCROP_SECTOR__All
  779. * @param Banks Disable PCROP protection on all the sectors for the specific bank
  780. * This parameter can be one of the following values:
  781. * @arg FLASH_BANK_1: WRP on all sectors of bank1
  782. * @arg FLASH_BANK_2: WRP on all sectors of bank2
  783. * @arg FLASH_BANK_BOTH: WRP on all sectors of bank1 & bank2
  784. *
  785. * @retval HAL Status
  786. */
  787. static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks)
  788. {
  789. HAL_StatusTypeDef status = HAL_OK;
  790. /* Check the parameters */
  791. assert_param(IS_FLASH_BANK(Banks));
  792. /* Wait for last operation to be completed */
  793. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  794. if(status == HAL_OK)
  795. {
  796. if((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))
  797. {
  798. assert_param(IS_OB_PCROP(SectorBank1));
  799. /*Write protection done on sectors of BANK1*/
  800. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~SectorBank1);
  801. }
  802. else
  803. {
  804. /*Write protection done on sectors of BANK2*/
  805. assert_param(IS_OB_PCROP(SectorBank2));
  806. *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~SectorBank2);
  807. }
  808. /*Write protection on all sector of BANK2*/
  809. if(Banks == FLASH_BANK_BOTH)
  810. {
  811. assert_param(IS_OB_PCROP(SectorBank2));
  812. /* Wait for last operation to be completed */
  813. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  814. if(status == HAL_OK)
  815. {
  816. /*Write protection done on sectors of BANK2*/
  817. *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~SectorBank2);
  818. }
  819. }
  820. }
  821. return status;
  822. }
  823. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
  824. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
  825. defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) ||\
  826. defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F412Zx) ||\
  827. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) ||\
  828. defined(STM32F423xx)
  829. /**
  830. * @brief Mass erase of FLASH memory
  831. * @param VoltageRange The device voltage range which defines the erase parallelism.
  832. * This parameter can be one of the following values:
  833. * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
  834. * the operation will be done by byte (8-bit)
  835. * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
  836. * the operation will be done by half word (16-bit)
  837. * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
  838. * the operation will be done by word (32-bit)
  839. * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
  840. * the operation will be done by double word (64-bit)
  841. *
  842. * @param Banks Banks to be erased
  843. * This parameter can be one of the following values:
  844. * @arg FLASH_BANK_1: Bank1 to be erased
  845. *
  846. * @retval None
  847. */
  848. static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks)
  849. {
  850. /* Check the parameters */
  851. assert_param(IS_VOLTAGERANGE(VoltageRange));
  852. assert_param(IS_FLASH_BANK(Banks));
  853. /* If the previous operation is completed, proceed to erase all sectors */
  854. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  855. FLASH->CR |= FLASH_CR_MER;
  856. FLASH->CR |= FLASH_CR_STRT | ((uint32_t)VoltageRange <<8U);
  857. }
  858. /**
  859. * @brief Erase the specified FLASH memory sector
  860. * @param Sector FLASH sector to erase
  861. * The value of this parameter depend on device used within the same series
  862. * @param VoltageRange The device voltage range which defines the erase parallelism.
  863. * This parameter can be one of the following values:
  864. * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
  865. * the operation will be done by byte (8-bit)
  866. * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
  867. * the operation will be done by half word (16-bit)
  868. * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
  869. * the operation will be done by word (32-bit)
  870. * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
  871. * the operation will be done by double word (64-bit)
  872. *
  873. * @retval None
  874. */
  875. void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
  876. {
  877. uint32_t tmp_psize = 0U;
  878. /* Check the parameters */
  879. assert_param(IS_FLASH_SECTOR(Sector));
  880. assert_param(IS_VOLTAGERANGE(VoltageRange));
  881. if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
  882. {
  883. tmp_psize = FLASH_PSIZE_BYTE;
  884. }
  885. else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
  886. {
  887. tmp_psize = FLASH_PSIZE_HALF_WORD;
  888. }
  889. else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
  890. {
  891. tmp_psize = FLASH_PSIZE_WORD;
  892. }
  893. else
  894. {
  895. tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
  896. }
  897. /* If the previous operation is completed, proceed to erase the sector */
  898. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  899. FLASH->CR |= tmp_psize;
  900. CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
  901. FLASH->CR |= FLASH_CR_SER | (Sector << FLASH_CR_SNB_Pos);
  902. FLASH->CR |= FLASH_CR_STRT;
  903. }
  904. /**
  905. * @brief Enable the write protection of the desired bank 1 sectors
  906. *
  907. * @note When the memory read protection level is selected (RDP level = 1),
  908. * it is not possible to program or erase the flash sector i if CortexM4
  909. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  910. * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
  911. *
  912. * @param WRPSector specifies the sector(s) to be write protected.
  913. * The value of this parameter depend on device used within the same series
  914. *
  915. * @param Banks Enable write protection on all the sectors for the specific bank
  916. * This parameter can be one of the following values:
  917. * @arg FLASH_BANK_1: WRP on all sectors of bank1
  918. *
  919. * @retval HAL Status
  920. */
  921. static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
  922. {
  923. HAL_StatusTypeDef status = HAL_OK;
  924. /* Check the parameters */
  925. assert_param(IS_OB_WRP_SECTOR(WRPSector));
  926. assert_param(IS_FLASH_BANK(Banks));
  927. /* Wait for last operation to be completed */
  928. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  929. if(status == HAL_OK)
  930. {
  931. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~WRPSector);
  932. }
  933. return status;
  934. }
  935. /**
  936. * @brief Disable the write protection of the desired bank 1 sectors
  937. *
  938. * @note When the memory read protection level is selected (RDP level = 1),
  939. * it is not possible to program or erase the flash sector i if CortexM4
  940. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  941. * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
  942. *
  943. * @param WRPSector specifies the sector(s) to be write protected.
  944. * The value of this parameter depend on device used within the same series
  945. *
  946. * @param Banks Enable write protection on all the sectors for the specific bank
  947. * This parameter can be one of the following values:
  948. * @arg FLASH_BANK_1: WRP on all sectors of bank1
  949. *
  950. * @retval HAL Status
  951. */
  952. static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
  953. {
  954. HAL_StatusTypeDef status = HAL_OK;
  955. /* Check the parameters */
  956. assert_param(IS_OB_WRP_SECTOR(WRPSector));
  957. assert_param(IS_FLASH_BANK(Banks));
  958. /* Wait for last operation to be completed */
  959. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  960. if(status == HAL_OK)
  961. {
  962. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)WRPSector;
  963. }
  964. return status;
  965. }
  966. #endif /* STM32F40xxx || STM32F41xxx || STM32F401xx || STM32F410xx || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx
  967. STM32F413xx || STM32F423xx */
  968. #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
  969. defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
  970. defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  971. /**
  972. * @brief Enable the read/write protection (PCROP) of the desired sectors.
  973. * @note This function can be used only for STM32F401xx devices.
  974. * @param Sector specifies the sector(s) to be read/write protected or unprotected.
  975. * This parameter can be one of the following values:
  976. * @arg OB_PCROP: A value between OB_PCROP_Sector0 and OB_PCROP_Sector5
  977. * @arg OB_PCROP_Sector_All
  978. * @retval HAL Status
  979. */
  980. static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t Sector)
  981. {
  982. HAL_StatusTypeDef status = HAL_OK;
  983. /* Check the parameters */
  984. assert_param(IS_OB_PCROP(Sector));
  985. /* Wait for last operation to be completed */
  986. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  987. if(status == HAL_OK)
  988. {
  989. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)Sector;
  990. }
  991. return status;
  992. }
  993. /**
  994. * @brief Disable the read/write protection (PCROP) of the desired sectors.
  995. * @note This function can be used only for STM32F401xx devices.
  996. * @param Sector specifies the sector(s) to be read/write protected or unprotected.
  997. * This parameter can be one of the following values:
  998. * @arg OB_PCROP: A value between OB_PCROP_Sector0 and OB_PCROP_Sector5
  999. * @arg OB_PCROP_Sector_All
  1000. * @retval HAL Status
  1001. */
  1002. static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t Sector)
  1003. {
  1004. HAL_StatusTypeDef status = HAL_OK;
  1005. /* Check the parameters */
  1006. assert_param(IS_OB_PCROP(Sector));
  1007. /* Wait for last operation to be completed */
  1008. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  1009. if(status == HAL_OK)
  1010. {
  1011. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~Sector);
  1012. }
  1013. return status;
  1014. }
  1015. #endif /* STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx
  1016. STM32F413xx || STM32F423xx */
  1017. /**
  1018. * @brief Set the read protection level.
  1019. * @param Level specifies the read protection level.
  1020. * This parameter can be one of the following values:
  1021. * @arg OB_RDP_LEVEL_0: No protection
  1022. * @arg OB_RDP_LEVEL_1: Read protection of the memory
  1023. * @arg OB_RDP_LEVEL_2: Full chip protection
  1024. *
  1025. * @note WARNING: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
  1026. *
  1027. * @retval HAL Status
  1028. */
  1029. static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level)
  1030. {
  1031. HAL_StatusTypeDef status = HAL_OK;
  1032. /* Check the parameters */
  1033. assert_param(IS_OB_RDP_LEVEL(Level));
  1034. /* Wait for last operation to be completed */
  1035. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  1036. if(status == HAL_OK)
  1037. {
  1038. *(__IO uint8_t*)OPTCR_BYTE1_ADDRESS = Level;
  1039. }
  1040. return status;
  1041. }
  1042. /**
  1043. * @brief Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  1044. * @param Iwdg Selects the IWDG mode
  1045. * This parameter can be one of the following values:
  1046. * @arg OB_IWDG_SW: Software IWDG selected
  1047. * @arg OB_IWDG_HW: Hardware IWDG selected
  1048. * @param Stop Reset event when entering STOP mode.
  1049. * This parameter can be one of the following values:
  1050. * @arg OB_STOP_NO_RST: No reset generated when entering in STOP
  1051. * @arg OB_STOP_RST: Reset generated when entering in STOP
  1052. * @param Stdby Reset event when entering Standby mode.
  1053. * This parameter can be one of the following values:
  1054. * @arg OB_STDBY_NO_RST: No reset generated when entering in STANDBY
  1055. * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
  1056. * @retval HAL Status
  1057. */
  1058. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t Iwdg, uint8_t Stop, uint8_t Stdby)
  1059. {
  1060. uint8_t optiontmp = 0xFF;
  1061. HAL_StatusTypeDef status = HAL_OK;
  1062. /* Check the parameters */
  1063. assert_param(IS_OB_IWDG_SOURCE(Iwdg));
  1064. assert_param(IS_OB_STOP_SOURCE(Stop));
  1065. assert_param(IS_OB_STDBY_SOURCE(Stdby));
  1066. /* Wait for last operation to be completed */
  1067. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  1068. if(status == HAL_OK)
  1069. {
  1070. /* Mask OPTLOCK, OPTSTRT, BOR_LEV and BFB2 bits */
  1071. optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS) & (uint8_t)0x1F);
  1072. /* Update User Option Byte */
  1073. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS = Iwdg | (uint8_t)(Stdby | (uint8_t)(Stop | ((uint8_t)optiontmp)));
  1074. }
  1075. return status;
  1076. }
  1077. /**
  1078. * @brief Set the BOR Level.
  1079. * @param Level specifies the Option Bytes BOR Reset Level.
  1080. * This parameter can be one of the following values:
  1081. * @arg OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
  1082. * @arg OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
  1083. * @arg OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
  1084. * @arg OB_BOR_OFF: Supply voltage ranges from 1.62 to 2.1 V
  1085. * @retval HAL Status
  1086. */
  1087. static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level)
  1088. {
  1089. /* Check the parameters */
  1090. assert_param(IS_OB_BOR_LEVEL(Level));
  1091. /* Set the BOR Level */
  1092. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BOR_LEV);
  1093. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= Level;
  1094. return HAL_OK;
  1095. }
  1096. /**
  1097. * @brief Return the FLASH User Option Byte value.
  1098. * @retval uint8_t FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1)
  1099. * and RST_STDBY(Bit2).
  1100. */
  1101. static uint8_t FLASH_OB_GetUser(void)
  1102. {
  1103. /* Return the User Option Byte */
  1104. return ((uint8_t)(FLASH->OPTCR & 0xE0));
  1105. }
  1106. /**
  1107. * @brief Return the FLASH Write Protection Option Bytes value.
  1108. * @retval uint16_t FLASH Write Protection Option Bytes value
  1109. */
  1110. static uint16_t FLASH_OB_GetWRP(void)
  1111. {
  1112. /* Return the FLASH write protection Register value */
  1113. return (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
  1114. }
  1115. /**
  1116. * @brief Returns the FLASH Read Protection level.
  1117. * @retval FLASH ReadOut Protection Status:
  1118. * This parameter can be one of the following values:
  1119. * @arg OB_RDP_LEVEL_0: No protection
  1120. * @arg OB_RDP_LEVEL_1: Read protection of the memory
  1121. * @arg OB_RDP_LEVEL_2: Full chip protection
  1122. */
  1123. static uint8_t FLASH_OB_GetRDP(void)
  1124. {
  1125. uint8_t readstatus = OB_RDP_LEVEL_0;
  1126. if((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) == (uint8_t)OB_RDP_LEVEL_2))
  1127. {
  1128. readstatus = OB_RDP_LEVEL_2;
  1129. }
  1130. else if((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) == (uint8_t)OB_RDP_LEVEL_1))
  1131. {
  1132. readstatus = OB_RDP_LEVEL_1;
  1133. }
  1134. else
  1135. {
  1136. readstatus = OB_RDP_LEVEL_0;
  1137. }
  1138. return readstatus;
  1139. }
  1140. /**
  1141. * @brief Returns the FLASH BOR level.
  1142. * @retval uint8_t The FLASH BOR level:
  1143. * - OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
  1144. * - OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
  1145. * - OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
  1146. * - OB_BOR_OFF : Supply voltage ranges from 1.62 to 2.1 V
  1147. */
  1148. static uint8_t FLASH_OB_GetBOR(void)
  1149. {
  1150. /* Return the FLASH BOR level */
  1151. return (uint8_t)(*(__IO uint8_t *)(OPTCR_BYTE0_ADDRESS) & (uint8_t)0x0C);
  1152. }
  1153. /**
  1154. * @brief Flush the instruction and data caches
  1155. * @retval None
  1156. */
  1157. void FLASH_FlushCaches(void)
  1158. {
  1159. /* Flush instruction cache */
  1160. if(READ_BIT(FLASH->ACR, FLASH_ACR_ICEN)!= RESET)
  1161. {
  1162. /* Disable instruction cache */
  1163. __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
  1164. /* Reset instruction cache */
  1165. __HAL_FLASH_INSTRUCTION_CACHE_RESET();
  1166. /* Enable instruction cache */
  1167. __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
  1168. }
  1169. /* Flush data cache */
  1170. if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != RESET)
  1171. {
  1172. /* Disable data cache */
  1173. __HAL_FLASH_DATA_CACHE_DISABLE();
  1174. /* Reset data cache */
  1175. __HAL_FLASH_DATA_CACHE_RESET();
  1176. /* Enable data cache */
  1177. __HAL_FLASH_DATA_CACHE_ENABLE();
  1178. }
  1179. }
  1180. /**
  1181. * @}
  1182. */
  1183. #endif /* HAL_FLASH_MODULE_ENABLED */
  1184. /**
  1185. * @}
  1186. */
  1187. /**
  1188. * @}
  1189. */
  1190. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/