stm32f0xx_flash.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_flash.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 05-December-2014
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the FLASH peripheral:
  9. * - FLASH Interface configuration
  10. * - FLASH Memory Programming
  11. * - Option Bytes Programming
  12. * - Interrupts and flags management
  13. *
  14. * @verbatim
  15. ===============================================================================
  16. ##### How to use this driver #####
  17. ===============================================================================
  18. [..] This driver provides functions to configure and program the Flash
  19. memory of all STM32F0xx devices. These functions are split in 4 groups
  20. (#) FLASH Interface configuration functions: this group includes the
  21. management of following features:
  22. (++) Set the latency
  23. (++) Enable/Disable the prefetch buffer
  24. (#) FLASH Memory Programming functions: this group includes all needed
  25. functions to erase and program the main memory:
  26. (++) Lock and Unlock the Flash interface.
  27. (++) Erase function: Erase Page, erase all pages.
  28. (++) Program functions: Half Word and Word write.
  29. (#) FLASH Option Bytes Programming functions: this group includes all
  30. needed functions to:
  31. (++) Lock and Unlock the Flash Option bytes.
  32. (++) Launch the Option Bytes loader
  33. (++) Erase the Option Bytes
  34. (++)Set/Reset the write protection
  35. (++) Set the Read protection Level
  36. (++) Program the user option Bytes
  37. (++) Set/Reset the BOOT1 bit
  38. (++) Enable/Disable the VDDA Analog Monitoring
  39. (++) Get the user option bytes
  40. (++) Get the Write protection
  41. (++) Get the read protection status
  42. (#) FLASH Interrupts and flag management functions: this group includes
  43. all needed functions to:
  44. (++) Enable/Disable the flash interrupt sources
  45. (++) Get flags status
  46. (++) Clear flags
  47. (++) Get Flash operation status
  48. (++) Wait for last flash operation
  49. @endverbatim
  50. ******************************************************************************
  51. * @attention
  52. *
  53. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  54. *
  55. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  56. * You may not use this file except in compliance with the License.
  57. * You may obtain a copy of the License at:
  58. *
  59. * http://www.st.com/software_license_agreement_liberty_v2
  60. *
  61. * Unless required by applicable law or agreed to in writing, software
  62. * distributed under the License is distributed on an "AS IS" BASIS,
  63. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  64. * See the License for the specific language governing permissions and
  65. * limitations under the License.
  66. *
  67. ******************************************************************************
  68. */
  69. /* Includes ------------------------------------------------------------------*/
  70. #include "stm32f0xx_flash.h"
  71. /** @addtogroup STM32F0xx_StdPeriph_Driver
  72. * @{
  73. */
  74. /** @defgroup FLASH
  75. * @brief FLASH driver modules
  76. * @{
  77. */
  78. /* Private typedef -----------------------------------------------------------*/
  79. /* Private define ------------------------------------------------------------*/
  80. /* Private macro -------------------------------------------------------------*/
  81. /* Private variables ---------------------------------------------------------*/
  82. /* Private function prototypes -----------------------------------------------*/
  83. /* Private functions ---------------------------------------------------------*/
  84. /** @defgroup FLASH_Private_Functions
  85. * @{
  86. */
  87. /** @defgroup FLASH_Group1 FLASH Interface configuration functions
  88. * @brief FLASH Interface configuration functions
  89. *
  90. @verbatim
  91. ===============================================================================
  92. ##### FLASH Interface configuration functions #####
  93. ===============================================================================
  94. [..] FLASH_Interface configuration_Functions, includes the following functions:
  95. (+) void FLASH_SetLatency(uint32_t FLASH_Latency):
  96. [..] To correctly read data from Flash memory, the number of wait states (LATENCY)
  97. must be correctly programmed according to the frequency of the CPU clock (HCLK)
  98. [..]
  99. +--------------------------------------------- +
  100. | Wait states | HCLK clock frequency (MHz) |
  101. |---------------|------------------------------|
  102. |0WS(1CPU cycle)| 0 < HCLK <= 24 |
  103. |---------------|------------------------------|
  104. |1WS(2CPU cycle)| 24 < HCLK <= 48 |
  105. +----------------------------------------------+
  106. [..]
  107. (+) void FLASH_PrefetchBufferCmd(FunctionalState NewState);
  108. [..]
  109. All these functions don't need the unlock sequence.
  110. @endverbatim
  111. * @{
  112. */
  113. /**
  114. * @brief Sets the code latency value.
  115. * @param FLASH_Latency: specifies the FLASH Latency value.
  116. * This parameter can be one of the following values:
  117. * @arg FLASH_Latency_0: FLASH Zero Latency cycle
  118. * @arg FLASH_Latency_1: FLASH One Latency cycle
  119. * @retval None
  120. */
  121. void FLASH_SetLatency(uint32_t FLASH_Latency)
  122. {
  123. uint32_t tmpreg = 0;
  124. /* Check the parameters */
  125. assert_param(IS_FLASH_LATENCY(FLASH_Latency));
  126. /* Read the ACR register */
  127. tmpreg = FLASH->ACR;
  128. /* Sets the Latency value */
  129. tmpreg &= (uint32_t) (~((uint32_t)FLASH_ACR_LATENCY));
  130. tmpreg |= FLASH_Latency;
  131. /* Write the ACR register */
  132. FLASH->ACR = tmpreg;
  133. }
  134. /**
  135. * @brief Enables or disables the Prefetch Buffer.
  136. * @param NewState: new state of the FLASH prefetch buffer.
  137. * This parameter can be: ENABLE or DISABLE.
  138. * @retval None
  139. */
  140. void FLASH_PrefetchBufferCmd(FunctionalState NewState)
  141. {
  142. /* Check the parameters */
  143. assert_param(IS_FUNCTIONAL_STATE(NewState));
  144. if(NewState != DISABLE)
  145. {
  146. FLASH->ACR |= FLASH_ACR_PRFTBE;
  147. }
  148. else
  149. {
  150. FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_PRFTBE));
  151. }
  152. }
  153. /**
  154. * @brief Checks whether the FLASH Prefetch Buffer status is set or not.
  155. * @param None
  156. * @retval FLASH Prefetch Buffer Status (SET or RESET).
  157. */
  158. FlagStatus FLASH_GetPrefetchBufferStatus(void)
  159. {
  160. FlagStatus bitstatus = RESET;
  161. if ((FLASH->ACR & FLASH_ACR_PRFTBS) != (uint32_t)RESET)
  162. {
  163. bitstatus = SET;
  164. }
  165. else
  166. {
  167. bitstatus = RESET;
  168. }
  169. /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
  170. return bitstatus;
  171. }
  172. /**
  173. * @}
  174. */
  175. /** @defgroup FLASH_Group2 FLASH Memory Programming functions
  176. * @brief FLASH Memory Programming functions
  177. *
  178. @verbatim
  179. ===============================================================================
  180. ##### FLASH Memory Programming functions #####
  181. ===============================================================================
  182. [..] The FLASH Memory Programming functions, includes the following functions:
  183. (+) void FLASH_Unlock(void);
  184. (+) void FLASH_Lock(void);
  185. (+) FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
  186. (+) FLASH_Status FLASH_EraseAllPages(void);
  187. (+) FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
  188. (+) FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
  189. [..] Any operation of erase or program should follow these steps:
  190. (#) Call the FLASH_Unlock() function to enable the flash control register and
  191. program memory access
  192. (#) Call the desired function to erase page or program data
  193. (#) Call the FLASH_Lock() to disable the flash program memory access
  194. (recommended to protect the FLASH memory against possible unwanted operation)
  195. @endverbatim
  196. * @{
  197. */
  198. /**
  199. * @brief Unlocks the FLASH control register and program memory access.
  200. * @param None
  201. * @retval None
  202. */
  203. void FLASH_Unlock(void)
  204. {
  205. if((FLASH->CR & FLASH_CR_LOCK) != RESET)
  206. {
  207. /* Unlocking the program memory access */
  208. FLASH->KEYR = FLASH_FKEY1;
  209. FLASH->KEYR = FLASH_FKEY2;
  210. }
  211. }
  212. /**
  213. * @brief Locks the Program memory access.
  214. * @param None
  215. * @retval None
  216. */
  217. void FLASH_Lock(void)
  218. {
  219. /* Set the LOCK Bit to lock the FLASH control register and program memory access */
  220. FLASH->CR |= FLASH_CR_LOCK;
  221. }
  222. /**
  223. * @brief Erases a specified page in program memory.
  224. * @note To correctly run this function, the FLASH_Unlock() function must be called before.
  225. * @note Call the FLASH_Lock() to disable the flash memory access (recommended
  226. * to protect the FLASH memory against possible unwanted operation)
  227. * @param Page_Address: The page address in program memory to be erased.
  228. * @note A Page is erased in the Program memory only if the address to load
  229. * is the start address of a page (multiple of 1024 bytes).
  230. * @retval FLASH Status: The returned value can be:
  231. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  232. */
  233. FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
  234. {
  235. FLASH_Status status = FLASH_COMPLETE;
  236. /* Check the parameters */
  237. assert_param(IS_FLASH_PROGRAM_ADDRESS(Page_Address));
  238. /* Wait for last operation to be completed */
  239. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  240. if(status == FLASH_COMPLETE)
  241. {
  242. /* If the previous operation is completed, proceed to erase the page */
  243. FLASH->CR |= FLASH_CR_PER;
  244. FLASH->AR = Page_Address;
  245. FLASH->CR |= FLASH_CR_STRT;
  246. /* Wait for last operation to be completed */
  247. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  248. /* Disable the PER Bit */
  249. FLASH->CR &= ~FLASH_CR_PER;
  250. }
  251. /* Return the Erase Status */
  252. return status;
  253. }
  254. /**
  255. * @brief Erases all FLASH pages.
  256. * @note To correctly run this function, the FLASH_Unlock() function must be called before.
  257. * @note Call the FLASH_Lock() to disable the flash memory access (recommended
  258. * to protect the FLASH memory against possible unwanted operation)
  259. * @param None
  260. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  261. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  262. */
  263. FLASH_Status FLASH_EraseAllPages(void)
  264. {
  265. FLASH_Status status = FLASH_COMPLETE;
  266. /* Wait for last operation to be completed */
  267. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  268. if(status == FLASH_COMPLETE)
  269. {
  270. /* if the previous operation is completed, proceed to erase all pages */
  271. FLASH->CR |= FLASH_CR_MER;
  272. FLASH->CR |= FLASH_CR_STRT;
  273. /* Wait for last operation to be completed */
  274. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  275. /* Disable the MER Bit */
  276. FLASH->CR &= ~FLASH_CR_MER;
  277. }
  278. /* Return the Erase Status */
  279. return status;
  280. }
  281. /**
  282. * @brief Programs a word at a specified address.
  283. * @note To correctly run this function, the FLASH_Unlock() function must be called before.
  284. * @note Call the FLASH_Lock() to disable the flash memory access (recommended
  285. * to protect the FLASH memory against possible unwanted operation)
  286. * @param Address: specifies the address to be programmed.
  287. * @param Data: specifies the data to be programmed.
  288. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  289. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  290. */
  291. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
  292. {
  293. FLASH_Status status = FLASH_COMPLETE;
  294. __IO uint32_t tmp = 0;
  295. /* Check the parameters */
  296. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  297. /* Wait for last operation to be completed */
  298. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  299. if(status == FLASH_COMPLETE)
  300. {
  301. /* If the previous operation is completed, proceed to program the new first
  302. half word */
  303. FLASH->CR |= FLASH_CR_PG;
  304. *(__IO uint16_t*)Address = (uint16_t)Data;
  305. /* Wait for last operation to be completed */
  306. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  307. if(status == FLASH_COMPLETE)
  308. {
  309. /* If the previous operation is completed, proceed to program the new second
  310. half word */
  311. tmp = Address + 2;
  312. *(__IO uint16_t*) tmp = Data >> 16;
  313. /* Wait for last operation to be completed */
  314. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  315. /* Disable the PG Bit */
  316. FLASH->CR &= ~FLASH_CR_PG;
  317. }
  318. else
  319. {
  320. /* Disable the PG Bit */
  321. FLASH->CR &= ~FLASH_CR_PG;
  322. }
  323. }
  324. /* Return the Program Status */
  325. return status;
  326. }
  327. /**
  328. * @brief Programs a half word at a specified address.
  329. * @note To correctly run this function, the FLASH_Unlock() function must be called before.
  330. * @note Call the FLASH_Lock() to disable the flash memory access (recommended
  331. * to protect the FLASH memory against possible unwanted operation)
  332. * @param Address: specifies the address to be programmed.
  333. * @param Data: specifies the data to be programmed.
  334. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  335. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  336. */
  337. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
  338. {
  339. FLASH_Status status = FLASH_COMPLETE;
  340. /* Check the parameters */
  341. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  342. /* Wait for last operation to be completed */
  343. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  344. if(status == FLASH_COMPLETE)
  345. {
  346. /* If the previous operation is completed, proceed to program the new data */
  347. FLASH->CR |= FLASH_CR_PG;
  348. *(__IO uint16_t*)Address = Data;
  349. /* Wait for last operation to be completed */
  350. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  351. /* Disable the PG Bit */
  352. FLASH->CR &= ~FLASH_CR_PG;
  353. }
  354. /* Return the Program Status */
  355. return status;
  356. }
  357. /**
  358. * @}
  359. */
  360. /** @defgroup FLASH_Group3 Option Bytes Programming functions
  361. * @brief Option Bytes Programming functions
  362. *
  363. @verbatim
  364. ===============================================================================
  365. ##### Option Bytes Programming functions #####
  366. ===============================================================================
  367. [..] The FLASH_Option Bytes Programming_functions, includes the following functions:
  368. (+) void FLASH_OB_Unlock(void);
  369. (+) void FLASH_OB_Lock(void);
  370. (+) void FLASH_OB_Launch(void);
  371. (+) FLASH_Status FLASH_OB_Erase(void);
  372. (+) FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
  373. (+) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);
  374. (+) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
  375. (+) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
  376. (+) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
  377. (+) FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
  378. (+) FLASH_OB_ProgramData(uint32_t Address, uint8_t Data);
  379. (+) uint8_t FLASH_OB_GetUser(void);
  380. (+) uint32_t FLASH_OB_GetWRP(void);
  381. (+) FlagStatus FLASH_OB_GetRDP(void);
  382. [..] Any operation of erase or program should follow these steps:
  383. (#) Call the FLASH_OB_Unlock() function to enable the Option Bytes registers access
  384. (#) Call one or several functions to program the desired option bytes
  385. (++) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the desired read Protection Level
  386. (++) FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
  387. => to Enable/Disable the desired sector write protection
  388. (++) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
  389. => to configure the user option Bytes: IWDG, STOP and the Standby.
  390. (++) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
  391. => to set or reset BOOT1
  392. (++) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
  393. => to enable or disable the VDDA Analog Monitoring
  394. (++) You can write all User Options bytes at once using a single function
  395. by calling FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
  396. (++) FLASH_OB_ProgramData(uint32_t Address, uint8_t Data) to program the
  397. two half word in the option bytes
  398. (#) Once all needed option bytes to be programmed are correctly written, call the
  399. FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
  400. (#) Call the FLASH_OB_Lock() to disable the Option Bytes registers access (recommended
  401. to protect the option Bytes against possible unwanted operations)
  402. @endverbatim
  403. * @{
  404. */
  405. /**
  406. * @brief Unlocks the option bytes block access.
  407. * @param None
  408. * @retval None
  409. */
  410. void FLASH_OB_Unlock(void)
  411. {
  412. if((FLASH->CR & FLASH_CR_OPTWRE) == RESET)
  413. {
  414. /* Unlocking the option bytes block access */
  415. FLASH->OPTKEYR = FLASH_OPTKEY1;
  416. FLASH->OPTKEYR = FLASH_OPTKEY2;
  417. }
  418. }
  419. /**
  420. * @brief Locks the option bytes block access.
  421. * @param None
  422. * @retval None
  423. */
  424. void FLASH_OB_Lock(void)
  425. {
  426. /* Set the OPTWREN Bit to lock the option bytes block access */
  427. FLASH->CR &= ~FLASH_CR_OPTWRE;
  428. }
  429. /**
  430. * @brief Launch the option byte loading.
  431. * @param None
  432. * @retval None
  433. */
  434. void FLASH_OB_Launch(void)
  435. {
  436. /* Set the OBL_Launch bit to launch the option byte loading */
  437. FLASH->CR |= FLASH_CR_OBL_LAUNCH;
  438. }
  439. /**
  440. * @brief Erases the FLASH option bytes.
  441. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  442. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  443. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  444. * @note This functions erases all option bytes except the Read protection (RDP).
  445. * @param None
  446. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  447. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  448. */
  449. FLASH_Status FLASH_OB_Erase(void)
  450. {
  451. uint16_t rdptmp = OB_RDP_Level_0;
  452. FLASH_Status status = FLASH_COMPLETE;
  453. /* Get the actual read protection Option Byte value */
  454. if(FLASH_OB_GetRDP() != RESET)
  455. {
  456. rdptmp = 0x00;
  457. }
  458. /* Wait for last operation to be completed */
  459. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  460. if(status == FLASH_COMPLETE)
  461. {
  462. /* If the previous operation is completed, proceed to erase the option bytes */
  463. FLASH->CR |= FLASH_CR_OPTER;
  464. FLASH->CR |= FLASH_CR_STRT;
  465. /* Wait for last operation to be completed */
  466. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  467. if(status == FLASH_COMPLETE)
  468. {
  469. /* If the erase operation is completed, disable the OPTER Bit */
  470. FLASH->CR &= ~FLASH_CR_OPTER;
  471. /* Enable the Option Bytes Programming operation */
  472. FLASH->CR |= FLASH_CR_OPTPG;
  473. /* Restore the last read protection Option Byte value */
  474. OB->RDP = (uint16_t)rdptmp;
  475. /* Wait for last operation to be completed */
  476. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  477. if(status != FLASH_TIMEOUT)
  478. {
  479. /* if the program operation is completed, disable the OPTPG Bit */
  480. FLASH->CR &= ~FLASH_CR_OPTPG;
  481. }
  482. }
  483. else
  484. {
  485. if (status != FLASH_TIMEOUT)
  486. {
  487. /* Disable the OPTPG Bit */
  488. FLASH->CR &= ~FLASH_CR_OPTPG;
  489. }
  490. }
  491. }
  492. /* Return the erase status */
  493. return status;
  494. }
  495. /**
  496. * @brief Write protects the desired pages
  497. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  498. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  499. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  500. * @param OB_WRP: specifies the address of the pages to be write protected.
  501. * This parameter can be:
  502. * @arg OB_WRP_Pages0to3..OB_WRP_Pages60to63
  503. * @arg OB_WRP_AllPages
  504. * @retval FLASH Status: The returned value can be:
  505. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  506. */
  507. FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP)
  508. {
  509. uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
  510. FLASH_Status status = FLASH_COMPLETE;
  511. /* Check the parameters */
  512. assert_param(IS_OB_WRP(OB_WRP));
  513. OB_WRP = (uint32_t)(~OB_WRP);
  514. WRP0_Data = (uint16_t)(OB_WRP & OB_WRP0_WRP0);
  515. WRP1_Data = (uint16_t)((OB_WRP >> 8) & OB_WRP0_WRP0);
  516. WRP2_Data = (uint16_t)((OB_WRP >> 16) & OB_WRP0_WRP0) ;
  517. WRP3_Data = (uint16_t)((OB_WRP >> 24) & OB_WRP0_WRP0) ;
  518. /* Wait for last operation to be completed */
  519. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  520. if(status == FLASH_COMPLETE)
  521. {
  522. FLASH->CR |= FLASH_CR_OPTPG;
  523. if(WRP0_Data != 0xFF)
  524. {
  525. OB->WRP0 = WRP0_Data;
  526. /* Wait for last operation to be completed */
  527. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  528. }
  529. if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
  530. {
  531. OB->WRP1 = WRP1_Data;
  532. /* Wait for last operation to be completed */
  533. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  534. }
  535. if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
  536. {
  537. OB->WRP2 = WRP2_Data;
  538. /* Wait for last operation to be completed */
  539. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  540. }
  541. if((status == FLASH_COMPLETE) && (WRP3_Data != 0xFF))
  542. {
  543. OB->WRP3 = WRP3_Data;
  544. /* Wait for last operation to be completed */
  545. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  546. }
  547. if(status != FLASH_TIMEOUT)
  548. {
  549. /* if the program operation is completed, disable the OPTPG Bit */
  550. FLASH->CR &= ~FLASH_CR_OPTPG;
  551. }
  552. }
  553. /* Return the write protection operation Status */
  554. return status;
  555. }
  556. /**
  557. * @brief Enables or disables the read out protection.
  558. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  559. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  560. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  561. * @param FLASH_ReadProtection_Level: specifies the read protection level.
  562. * This parameter can be:
  563. * @arg OB_RDP_Level_0: No protection
  564. * @arg OB_RDP_Level_1: Read protection of the memory
  565. * @arg OB_RDP_Level_2: Chip protection
  566. * @note When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
  567. * @retval FLASH Status: The returned value can be:
  568. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  569. */
  570. FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP)
  571. {
  572. FLASH_Status status = FLASH_COMPLETE;
  573. /* Check the parameters */
  574. assert_param(IS_OB_RDP(OB_RDP));
  575. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  576. if(status == FLASH_COMPLETE)
  577. {
  578. FLASH->CR |= FLASH_CR_OPTER;
  579. FLASH->CR |= FLASH_CR_STRT;
  580. /* Wait for last operation to be completed */
  581. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  582. if(status == FLASH_COMPLETE)
  583. {
  584. /* If the erase operation is completed, disable the OPTER Bit */
  585. FLASH->CR &= ~FLASH_CR_OPTER;
  586. /* Enable the Option Bytes Programming operation */
  587. FLASH->CR |= FLASH_CR_OPTPG;
  588. OB->RDP = OB_RDP;
  589. /* Wait for last operation to be completed */
  590. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  591. if(status != FLASH_TIMEOUT)
  592. {
  593. /* if the program operation is completed, disable the OPTPG Bit */
  594. FLASH->CR &= ~FLASH_CR_OPTPG;
  595. }
  596. }
  597. else
  598. {
  599. if(status != FLASH_TIMEOUT)
  600. {
  601. /* Disable the OPTER Bit */
  602. FLASH->CR &= ~FLASH_CR_OPTER;
  603. }
  604. }
  605. }
  606. /* Return the protection operation Status */
  607. return status;
  608. }
  609. /**
  610. * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  611. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  612. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  613. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  614. * @param OB_IWDG: Selects the WDG mode
  615. * This parameter can be one of the following values:
  616. * @arg OB_IWDG_SW: Software WDG selected
  617. * @arg OB_IWDG_HW: Hardware WDG selected
  618. * @param OB_STOP: Reset event when entering STOP mode.
  619. * This parameter can be one of the following values:
  620. * @arg OB_STOP_NoRST: No reset generated when entering in STOP
  621. * @arg OB_STOP_RST: Reset generated when entering in STOP
  622. * @param OB_STDBY: Reset event when entering Standby mode.
  623. * This parameter can be one of the following values:
  624. * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
  625. * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
  626. * @retval FLASH Status: The returned value can be:
  627. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  628. */
  629. FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
  630. {
  631. FLASH_Status status = FLASH_COMPLETE;
  632. /* Check the parameters */
  633. assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
  634. assert_param(IS_OB_STOP_SOURCE(OB_STOP));
  635. assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
  636. /* Wait for last operation to be completed */
  637. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  638. if(status == FLASH_COMPLETE)
  639. {
  640. /* Enable the Option Bytes Programming operation */
  641. FLASH->CR |= FLASH_CR_OPTPG;
  642. OB->USER = (uint16_t)((uint16_t)(OB_IWDG | OB_STOP) | (uint16_t)(OB_STDBY | 0xF8));
  643. /* Wait for last operation to be completed */
  644. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  645. if(status != FLASH_TIMEOUT)
  646. {
  647. /* If the program operation is completed, disable the OPTPG Bit */
  648. FLASH->CR &= ~FLASH_CR_OPTPG;
  649. }
  650. }
  651. /* Return the Option Byte program Status */
  652. return status;
  653. }
  654. /**
  655. * @brief Sets or resets the BOOT1 option bit.
  656. * @param OB_BOOT1: Set or Reset the BOOT1 option bit.
  657. * This parameter can be one of the following values:
  658. * @arg OB_BOOT1_RESET: BOOT1 option bit reset
  659. * @arg OB_BOOT1_SET: BOOT1 option bit set
  660. * @retval None
  661. */
  662. FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
  663. {
  664. FLASH_Status status = FLASH_COMPLETE;
  665. /* Check the parameters */
  666. assert_param(IS_OB_BOOT1(OB_BOOT1));
  667. /* Wait for last operation to be completed */
  668. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  669. if(status == FLASH_COMPLETE)
  670. {
  671. /* Enable the Option Bytes Programming operation */
  672. FLASH->CR |= FLASH_CR_OPTPG;
  673. OB->USER = OB_BOOT1 | 0xEF;
  674. /* Wait for last operation to be completed */
  675. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  676. if(status != FLASH_TIMEOUT)
  677. {
  678. /* If the program operation is completed, disable the OPTPG Bit */
  679. FLASH->CR &= ~FLASH_CR_OPTPG;
  680. }
  681. }
  682. /* Return the Option Byte program Status */
  683. return status;
  684. }
  685. /**
  686. * @brief Sets or resets the BOOT0 option bit.
  687. * @note This function is applicable only for the STM32F042 devices.
  688. * @param OB_BOOT0: Set or Reset the BOOT0 option bit.
  689. * This parameter can be one of the following values:
  690. * @arg OB_BOOT0_RESET: BOOT0 option bit reset
  691. * @arg OB_BOOT0_SET: BOOT0 option bit set
  692. * @retval None
  693. */
  694. FLASH_Status FLASH_OB_BOOT0Config(uint8_t OB_BOOT0)
  695. {
  696. FLASH_Status status = FLASH_COMPLETE;
  697. /* Check the parameters */
  698. assert_param(IS_OB_BOOT0(OB_BOOT0));
  699. /* Wait for last operation to be completed */
  700. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  701. if(status == FLASH_COMPLETE)
  702. {
  703. /* Enable the Option Bytes Programming operation */
  704. FLASH->CR |= FLASH_CR_OPTPG;
  705. OB->USER = OB_BOOT0 | 0xF7;
  706. /* Wait for last operation to be completed */
  707. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  708. if(status != FLASH_TIMEOUT)
  709. {
  710. /* If the program operation is completed, disable the OPTPG Bit */
  711. FLASH->CR &= ~FLASH_CR_OPTPG;
  712. }
  713. }
  714. /* Return the Option Byte program Status */
  715. return status;
  716. }
  717. /**
  718. * @brief Sets or resets the BOOT0SW option bit.
  719. * @note This function is applicable only for the STM32F042 devices.
  720. * @param OB_BOOT0SW: Set or Reset the BOOT0_SW option bit.
  721. * This parameter can be one of the following values:
  722. * @arg OB_BOOT0_SW: BOOT0_SW option bit reset
  723. * @arg OB_BOOT0_HW: BOOT0_SW option bit set
  724. * @retval None
  725. */
  726. FLASH_Status FLASH_OB_BOOT0SWConfig(uint8_t OB_BOOT0SW)
  727. {
  728. FLASH_Status status = FLASH_COMPLETE;
  729. /* Check the parameters */
  730. assert_param(IS_OB_BOOT0SW(OB_BOOT0SW));
  731. /* Wait for last operation to be completed */
  732. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  733. if(status == FLASH_COMPLETE)
  734. {
  735. /* Enable the Option Bytes Programming operation */
  736. FLASH->CR |= FLASH_CR_OPTPG;
  737. OB->USER = OB_BOOT0SW | 0x7F;
  738. /* Wait for last operation to be completed */
  739. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  740. if(status != FLASH_TIMEOUT)
  741. {
  742. /* If the program operation is completed, disable the OPTPG Bit */
  743. FLASH->CR &= ~FLASH_CR_OPTPG;
  744. }
  745. }
  746. /* Return the Option Byte program Status */
  747. return status;
  748. }
  749. /**
  750. * @brief Sets or resets the analogue monitoring on VDDA Power source.
  751. * @param OB_VDDA_ANALOG: Selects the analog monitoring on VDDA Power source.
  752. * This parameter can be one of the following values:
  753. * @arg OB_VDDA_ANALOG_ON: Analog monitoring on VDDA Power source ON
  754. * @arg OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source OFF
  755. * @retval None
  756. */
  757. FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
  758. {
  759. FLASH_Status status = FLASH_COMPLETE;
  760. /* Check the parameters */
  761. assert_param(IS_OB_VDDA_ANALOG(OB_VDDA_ANALOG));
  762. /* Wait for last operation to be completed */
  763. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  764. if(status == FLASH_COMPLETE)
  765. {
  766. /* Enable the Option Bytes Programming operation */
  767. FLASH->CR |= FLASH_CR_OPTPG;
  768. OB->USER = OB_VDDA_ANALOG | 0xDF;
  769. /* Wait for last operation to be completed */
  770. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  771. if(status != FLASH_TIMEOUT)
  772. {
  773. /* if the program operation is completed, disable the OPTPG Bit */
  774. FLASH->CR &= ~FLASH_CR_OPTPG;
  775. }
  776. }
  777. /* Return the Option Byte program Status */
  778. return status;
  779. }
  780. /**
  781. * @brief Sets or resets the SRAM parity.
  782. * @param OB_SRAM_Parity: Set or Reset the SRAM parity enable bit.
  783. * This parameter can be one of the following values:
  784. * @arg OB_SRAM_PARITY_SET: Set SRAM parity.
  785. * @arg OB_SRAM_PARITY_RESET: Reset SRAM parity.
  786. * @retval None
  787. */
  788. FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity)
  789. {
  790. FLASH_Status status = FLASH_COMPLETE;
  791. /* Check the parameters */
  792. assert_param(IS_OB_SRAM_PARITY(OB_SRAM_Parity));
  793. /* Wait for last operation to be completed */
  794. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  795. if(status == FLASH_COMPLETE)
  796. {
  797. /* Enable the Option Bytes Programming operation */
  798. FLASH->CR |= FLASH_CR_OPTPG;
  799. OB->USER = OB_SRAM_Parity | 0xBF;
  800. /* Wait for last operation to be completed */
  801. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  802. if(status != FLASH_TIMEOUT)
  803. {
  804. /* if the program operation is completed, disable the OPTPG Bit */
  805. FLASH->CR &= ~FLASH_CR_OPTPG;
  806. }
  807. }
  808. /* Return the Option Byte program Status */
  809. return status;
  810. }
  811. /**
  812. * @brief Programs the FLASH User Option Byte: IWDG_SW, RST_STOP, RST_STDBY,
  813. * BOOT1 and VDDA ANALOG monitoring.
  814. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  815. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  816. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  817. * @param OB_USER: Selects all user option bytes
  818. * This parameter is a combination of the following values:
  819. * @arg OB_IWDG_SW / OB_IWDG_HW: Software / Hardware WDG selected
  820. * @arg OB_STOP_NoRST / OB_STOP_RST: No reset / Reset generated when entering in STOP
  821. * @arg OB_STDBY_NoRST / OB_STDBY_RST: No reset / Reset generated when entering in STANDBY
  822. * @arg OB_BOOT1_RESET / OB_BOOT1_SET: BOOT1 Reset / Set
  823. * @arg OB_VDDA_ANALOG_ON / OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source ON / OFF
  824. * @arg OB_SRAM_PARITY_SET / OB_SRAM_PARITY_RESET: SRAM Parity SET / RESET
  825. * @arg OB_BOOT0_RESET / OB_BOOT0_SET: BOOT0 Reset / Set
  826. * @arg OB_BOOT0_SW / OB_BOOT0_SW: BOOT0 pin disabled / BOOT0 pin bonded with GPIO
  827. * @retval FLASH Status: The returned value can be:
  828. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  829. */
  830. FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
  831. {
  832. FLASH_Status status = FLASH_COMPLETE;
  833. /* Wait for last operation to be completed */
  834. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  835. if(status == FLASH_COMPLETE)
  836. {
  837. /* Enable the Option Bytes Programming operation */
  838. FLASH->CR |= FLASH_CR_OPTPG;
  839. OB->USER = OB_USER;
  840. /* Wait for last operation to be completed */
  841. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  842. if(status != FLASH_TIMEOUT)
  843. {
  844. /* If the program operation is completed, disable the OPTPG Bit */
  845. FLASH->CR &= ~FLASH_CR_OPTPG;
  846. }
  847. }
  848. /* Return the Option Byte program Status */
  849. return status;
  850. }
  851. /**
  852. * @brief Programs a half word at a specified Option Byte Data address.
  853. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  854. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  855. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  856. * @param Address: specifies the address to be programmed.
  857. * This parameter can be 0x1FFFF804 or 0x1FFFF806.
  858. * @param Data: specifies the data to be programmed.
  859. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  860. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  861. */
  862. FLASH_Status FLASH_OB_ProgramData(uint32_t Address, uint8_t Data)
  863. {
  864. FLASH_Status status = FLASH_COMPLETE;
  865. /* Check the parameters */
  866. assert_param(IS_OB_DATA_ADDRESS(Address));
  867. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  868. if(status == FLASH_COMPLETE)
  869. {
  870. /* Enables the Option Bytes Programming operation */
  871. FLASH->CR |= FLASH_CR_OPTPG;
  872. *(__IO uint16_t*)Address = Data;
  873. /* Wait for last operation to be completed */
  874. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  875. if(status != FLASH_TIMEOUT)
  876. {
  877. /* If the program operation is completed, disable the OPTPG Bit */
  878. FLASH->CR &= ~FLASH_CR_OPTPG;
  879. }
  880. }
  881. /* Return the Option Byte Data Program Status */
  882. return status;
  883. }
  884. /**
  885. * @brief Returns the FLASH User Option Bytes values.
  886. * @param None
  887. * @retval The FLASH User Option Bytes .
  888. */
  889. uint8_t FLASH_OB_GetUser(void)
  890. {
  891. /* Return the User Option Byte */
  892. return (uint8_t)(FLASH->OBR >> 8);
  893. }
  894. /**
  895. * @brief Returns the FLASH Write Protection Option Bytes value.
  896. * @param None
  897. * @retval The FLASH Write Protection Option Bytes value
  898. */
  899. uint32_t FLASH_OB_GetWRP(void)
  900. {
  901. /* Return the FLASH write protection Register value */
  902. return (uint32_t)(FLASH->WRPR);
  903. }
  904. /**
  905. * @brief Checks whether the FLASH Read out Protection Status is set or not.
  906. * @param None
  907. * @retval FLASH ReadOut Protection Status(SET or RESET)
  908. */
  909. FlagStatus FLASH_OB_GetRDP(void)
  910. {
  911. FlagStatus readstatus = RESET;
  912. if ((uint8_t)(FLASH->OBR & (FLASH_OBR_RDPRT1 | FLASH_OBR_RDPRT2)) != RESET)
  913. {
  914. readstatus = SET;
  915. }
  916. else
  917. {
  918. readstatus = RESET;
  919. }
  920. return readstatus;
  921. }
  922. /**
  923. * @}
  924. */
  925. /** @defgroup FLASH_Group4 Interrupts and flags management functions
  926. * @brief Interrupts and flags management functions
  927. *
  928. @verbatim
  929. ===============================================================================
  930. ##### Interrupts and flags management functions #####
  931. ===============================================================================
  932. @endverbatim
  933. * @{
  934. */
  935. /**
  936. * @brief Enables or disables the specified FLASH interrupts.
  937. * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or
  938. * disabled.
  939. * This parameter can be any combination of the following values:
  940. * @arg FLASH_IT_EOP: FLASH end of programming Interrupt
  941. * @arg FLASH_IT_ERR: FLASH Error Interrupt
  942. * @retval None
  943. */
  944. void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
  945. {
  946. /* Check the parameters */
  947. assert_param(IS_FLASH_IT(FLASH_IT));
  948. assert_param(IS_FUNCTIONAL_STATE(NewState));
  949. if(NewState != DISABLE)
  950. {
  951. /* Enable the interrupt sources */
  952. FLASH->CR |= FLASH_IT;
  953. }
  954. else
  955. {
  956. /* Disable the interrupt sources */
  957. FLASH->CR &= ~(uint32_t)FLASH_IT;
  958. }
  959. }
  960. /**
  961. * @brief Checks whether the specified FLASH flag is set or not.
  962. * @param FLASH_FLAG: specifies the FLASH flag to check.
  963. * This parameter can be one of the following values:
  964. * @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag
  965. * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
  966. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  967. * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
  968. * @retval The new state of FLASH_FLAG (SET or RESET).
  969. */
  970. FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
  971. {
  972. FlagStatus bitstatus = RESET;
  973. /* Check the parameters */
  974. assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
  975. if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
  976. {
  977. bitstatus = SET;
  978. }
  979. else
  980. {
  981. bitstatus = RESET;
  982. }
  983. /* Return the new state of FLASH_FLAG (SET or RESET) */
  984. return bitstatus;
  985. }
  986. /**
  987. * @brief Clears the FLASH's pending flags.
  988. * @param FLASH_FLAG: specifies the FLASH flags to clear.
  989. * This parameter can be any combination of the following values:
  990. * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
  991. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  992. * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
  993. * @retval None
  994. */
  995. void FLASH_ClearFlag(uint32_t FLASH_FLAG)
  996. {
  997. /* Check the parameters */
  998. assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
  999. /* Clear the flags */
  1000. FLASH->SR = FLASH_FLAG;
  1001. }
  1002. /**
  1003. * @brief Returns the FLASH Status.
  1004. * @param None
  1005. * @retval FLASH Status: The returned value can be:
  1006. * FLASH_BUSY, FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE.
  1007. */
  1008. FLASH_Status FLASH_GetStatus(void)
  1009. {
  1010. FLASH_Status FLASHstatus = FLASH_COMPLETE;
  1011. if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
  1012. {
  1013. FLASHstatus = FLASH_BUSY;
  1014. }
  1015. else
  1016. {
  1017. if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)
  1018. {
  1019. FLASHstatus = FLASH_ERROR_WRP;
  1020. }
  1021. else
  1022. {
  1023. if((FLASH->SR & (uint32_t)(FLASH_SR_PGERR)) != (uint32_t)0x00)
  1024. {
  1025. FLASHstatus = FLASH_ERROR_PROGRAM;
  1026. }
  1027. else
  1028. {
  1029. FLASHstatus = FLASH_COMPLETE;
  1030. }
  1031. }
  1032. }
  1033. /* Return the FLASH Status */
  1034. return FLASHstatus;
  1035. }
  1036. /**
  1037. * @brief Waits for a FLASH operation to complete or a TIMEOUT to occur.
  1038. * @param Timeout: FLASH programming Timeout
  1039. * @retval FLASH Status: The returned value can be: FLASH_BUSY,
  1040. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  1041. */
  1042. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
  1043. {
  1044. FLASH_Status status = FLASH_COMPLETE;
  1045. /* Check for the FLASH Status */
  1046. status = FLASH_GetStatus();
  1047. /* Wait for a FLASH operation to complete or a TIMEOUT to occur */
  1048. while((status == FLASH_BUSY) && (Timeout != 0x00))
  1049. {
  1050. status = FLASH_GetStatus();
  1051. Timeout--;
  1052. }
  1053. if(Timeout == 0x00 )
  1054. {
  1055. status = FLASH_TIMEOUT;
  1056. }
  1057. /* Return the operation status */
  1058. return status;
  1059. }
  1060. /**
  1061. * @}
  1062. */
  1063. /**
  1064. * @}
  1065. */
  1066. /**
  1067. * @}
  1068. */
  1069. /**
  1070. * @}
  1071. */
  1072. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/