stm32f10x_flash.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_flash.c
  4. * @author MCD Application Team
  5. * @version V3.1.2
  6. * @date 09/28/2009
  7. * @brief This file provides all the FLASH firmware functions.
  8. ******************************************************************************
  9. * @copy
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f10x_flash.h"
  22. /** @addtogroup STM32F10x_StdPeriph_Driver
  23. * @{
  24. */
  25. /** @defgroup FLASH
  26. * @brief FLASH driver modules
  27. * @{
  28. */
  29. /** @defgroup FLASH_Private_TypesDefinitions
  30. * @{
  31. */
  32. /**
  33. * @}
  34. */
  35. /** @defgroup FLASH_Private_Defines
  36. * @{
  37. */
  38. /* Flash Access Control Register bits */
  39. #define ACR_LATENCY_Mask ((uint32_t)0x00000038)
  40. #define ACR_HLFCYA_Mask ((uint32_t)0xFFFFFFF7)
  41. #define ACR_PRFTBE_Mask ((uint32_t)0xFFFFFFEF)
  42. /* Flash Access Control Register bits */
  43. #define ACR_PRFTBS_Mask ((uint32_t)0x00000020)
  44. /* Flash Control Register bits */
  45. #define CR_PG_Set ((uint32_t)0x00000001)
  46. #define CR_PG_Reset ((uint32_t)0x00001FFE)
  47. #define CR_PER_Set ((uint32_t)0x00000002)
  48. #define CR_PER_Reset ((uint32_t)0x00001FFD)
  49. #define CR_MER_Set ((uint32_t)0x00000004)
  50. #define CR_MER_Reset ((uint32_t)0x00001FFB)
  51. #define CR_OPTPG_Set ((uint32_t)0x00000010)
  52. #define CR_OPTPG_Reset ((uint32_t)0x00001FEF)
  53. #define CR_OPTER_Set ((uint32_t)0x00000020)
  54. #define CR_OPTER_Reset ((uint32_t)0x00001FDF)
  55. #define CR_STRT_Set ((uint32_t)0x00000040)
  56. #define CR_LOCK_Set ((uint32_t)0x00000080)
  57. /* FLASH Mask */
  58. #define RDPRT_Mask ((uint32_t)0x00000002)
  59. #define WRP0_Mask ((uint32_t)0x000000FF)
  60. #define WRP1_Mask ((uint32_t)0x0000FF00)
  61. #define WRP2_Mask ((uint32_t)0x00FF0000)
  62. #define WRP3_Mask ((uint32_t)0xFF000000)
  63. /* FLASH Keys */
  64. #define RDP_Key ((uint16_t)0x00A5)
  65. #define FLASH_KEY1 ((uint32_t)0x45670123)
  66. #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
  67. /* Delay definition */
  68. #define EraseTimeout ((uint32_t)0x00000FFF)
  69. #define ProgramTimeout ((uint32_t)0x0000000F)
  70. /**
  71. * @}
  72. */
  73. /** @defgroup FLASH_Private_Macros
  74. * @{
  75. */
  76. /**
  77. * @}
  78. */
  79. /** @defgroup FLASH_Private_Variables
  80. * @{
  81. */
  82. /**
  83. * @}
  84. */
  85. /** @defgroup FLASH_Private_FunctionPrototypes
  86. * @{
  87. */
  88. static void delay(void);
  89. /**
  90. * @}
  91. */
  92. /** @defgroup FLASH_Private_Functions
  93. * @{
  94. */
  95. /**
  96. * @brief Sets the code latency value.
  97. * @param FLASH_Latency: specifies the FLASH Latency value.
  98. * This parameter can be one of the following values:
  99. * @arg FLASH_Latency_0: FLASH Zero Latency cycle
  100. * @arg FLASH_Latency_1: FLASH One Latency cycle
  101. * @arg FLASH_Latency_2: FLASH Two Latency cycles
  102. * @retval None
  103. */
  104. void FLASH_SetLatency(uint32_t FLASH_Latency)
  105. {
  106. uint32_t tmpreg = 0;
  107. /* Check the parameters */
  108. assert_param(IS_FLASH_LATENCY(FLASH_Latency));
  109. /* Read the ACR register */
  110. tmpreg = FLASH->ACR;
  111. /* Sets the Latency value */
  112. tmpreg &= ACR_LATENCY_Mask;
  113. tmpreg |= FLASH_Latency;
  114. /* Write the ACR register */
  115. FLASH->ACR = tmpreg;
  116. }
  117. /**
  118. * @brief Enables or disables the Half cycle flash access.
  119. * @param FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.
  120. * This parameter can be one of the following values:
  121. * @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable
  122. * @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable
  123. * @retval None
  124. */
  125. void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess)
  126. {
  127. /* Check the parameters */
  128. assert_param(IS_FLASH_HALFCYCLEACCESS_STATE(FLASH_HalfCycleAccess));
  129. /* Enable or disable the Half cycle access */
  130. FLASH->ACR &= ACR_HLFCYA_Mask;
  131. FLASH->ACR |= FLASH_HalfCycleAccess;
  132. }
  133. /**
  134. * @brief Enables or disables the Prefetch Buffer.
  135. * @param FLASH_PrefetchBuffer: specifies the Prefetch buffer status.
  136. * This parameter can be one of the following values:
  137. * @arg FLASH_PrefetchBuffer_Enable: FLASH Prefetch Buffer Enable
  138. * @arg FLASH_PrefetchBuffer_Disable: FLASH Prefetch Buffer Disable
  139. * @retval None
  140. */
  141. void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer)
  142. {
  143. /* Check the parameters */
  144. assert_param(IS_FLASH_PREFETCHBUFFER_STATE(FLASH_PrefetchBuffer));
  145. /* Enable or disable the Prefetch Buffer */
  146. FLASH->ACR &= ACR_PRFTBE_Mask;
  147. FLASH->ACR |= FLASH_PrefetchBuffer;
  148. }
  149. /**
  150. * @brief Unlocks the FLASH Program Erase Controller.
  151. * @param None
  152. * @retval None
  153. */
  154. void FLASH_Unlock(void)
  155. {
  156. /* Authorize the FPEC Access */
  157. FLASH->KEYR = FLASH_KEY1;
  158. FLASH->KEYR = FLASH_KEY2;
  159. }
  160. /**
  161. * @brief Locks the FLASH Program Erase Controller.
  162. * @param None
  163. * @retval None
  164. */
  165. void FLASH_Lock(void)
  166. {
  167. /* Set the Lock Bit to lock the FPEC and the FCR */
  168. FLASH->CR |= CR_LOCK_Set;
  169. }
  170. /**
  171. * @brief Erases a specified FLASH page.
  172. * @param Page_Address: The page address to be erased.
  173. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  174. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  175. */
  176. FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
  177. {
  178. FLASH_Status status = FLASH_COMPLETE;
  179. /* Check the parameters */
  180. assert_param(IS_FLASH_ADDRESS(Page_Address));
  181. /* Wait for last operation to be completed */
  182. status = FLASH_WaitForLastOperation(EraseTimeout);
  183. if(status == FLASH_COMPLETE)
  184. {
  185. /* if the previous operation is completed, proceed to erase the page */
  186. FLASH->CR|= CR_PER_Set;
  187. FLASH->AR = Page_Address;
  188. FLASH->CR|= CR_STRT_Set;
  189. /* Wait for last operation to be completed */
  190. status = FLASH_WaitForLastOperation(EraseTimeout);
  191. if(status != FLASH_TIMEOUT)
  192. {
  193. /* if the erase operation is completed, disable the PER Bit */
  194. FLASH->CR &= CR_PER_Reset;
  195. }
  196. }
  197. /* Return the Erase Status */
  198. return status;
  199. }
  200. /**
  201. * @brief Erases all FLASH pages.
  202. * @param None
  203. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  204. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  205. */
  206. FLASH_Status FLASH_EraseAllPages(void)
  207. {
  208. FLASH_Status status = FLASH_COMPLETE;
  209. /* Wait for last operation to be completed */
  210. status = FLASH_WaitForLastOperation(EraseTimeout);
  211. if(status == FLASH_COMPLETE)
  212. {
  213. /* if the previous operation is completed, proceed to erase all pages */
  214. FLASH->CR |= CR_MER_Set;
  215. FLASH->CR |= CR_STRT_Set;
  216. /* Wait for last operation to be completed */
  217. status = FLASH_WaitForLastOperation(EraseTimeout);
  218. if(status != FLASH_TIMEOUT)
  219. {
  220. /* if the erase operation is completed, disable the MER Bit */
  221. FLASH->CR &= CR_MER_Reset;
  222. }
  223. }
  224. /* Return the Erase Status */
  225. return status;
  226. }
  227. /**
  228. * @brief Erases the FLASH option bytes.
  229. * @note This functions erases all option bytes and then deactivates the Read
  230. * protection. If the user needs to keep the Read protection activated,
  231. * he has to enable it after this function call (using
  232. * FLASH_ReadOutProtection function)
  233. * @param None
  234. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  235. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  236. */
  237. FLASH_Status FLASH_EraseOptionBytes(void)
  238. {
  239. FLASH_Status status = FLASH_COMPLETE;
  240. /* Wait for last operation to be completed */
  241. status = FLASH_WaitForLastOperation(EraseTimeout);
  242. if(status == FLASH_COMPLETE)
  243. {
  244. /* Authorize the small information block programming */
  245. FLASH->OPTKEYR = FLASH_KEY1;
  246. FLASH->OPTKEYR = FLASH_KEY2;
  247. /* if the previous operation is completed, proceed to erase the option bytes */
  248. FLASH->CR |= CR_OPTER_Set;
  249. FLASH->CR |= CR_STRT_Set;
  250. /* Wait for last operation to be completed */
  251. status = FLASH_WaitForLastOperation(EraseTimeout);
  252. if(status == FLASH_COMPLETE)
  253. {
  254. /* if the erase operation is completed, disable the OPTER Bit */
  255. FLASH->CR &= CR_OPTER_Reset;
  256. /* Enable the Option Bytes Programming operation */
  257. FLASH->CR |= CR_OPTPG_Set;
  258. /* Disable the Read protection */
  259. OB->RDP= RDP_Key;
  260. /* Wait for last operation to be completed */
  261. status = FLASH_WaitForLastOperation(ProgramTimeout);
  262. if(status != FLASH_TIMEOUT)
  263. {
  264. /* if the program operation is completed, disable the OPTPG Bit */
  265. FLASH->CR &= CR_OPTPG_Reset;
  266. }
  267. }
  268. else
  269. {
  270. if (status != FLASH_TIMEOUT)
  271. {
  272. /* Disable the OPTPG Bit */
  273. FLASH->CR &= CR_OPTPG_Reset;
  274. }
  275. }
  276. }
  277. /* Return the erase status */
  278. return status;
  279. }
  280. /**
  281. * @brief Programs a word at a specified address.
  282. * @param Address: specifies the address to be programmed.
  283. * @param Data: specifies the data to be programmed.
  284. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  285. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  286. */
  287. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
  288. {
  289. FLASH_Status status = FLASH_COMPLETE;
  290. __IO uint32_t tmp = 0;
  291. /* Check the parameters */
  292. assert_param(IS_FLASH_ADDRESS(Address));
  293. /* Wait for last operation to be completed */
  294. status = FLASH_WaitForLastOperation(ProgramTimeout);
  295. if(status == FLASH_COMPLETE)
  296. {
  297. /* if the previous operation is completed, proceed to program the new first
  298. half word */
  299. FLASH->CR |= CR_PG_Set;
  300. *(__IO uint16_t*)Address = (uint16_t)Data;
  301. /* Wait for last operation to be completed */
  302. status = FLASH_WaitForLastOperation(ProgramTimeout);
  303. if(status == FLASH_COMPLETE)
  304. {
  305. /* if the previous operation is completed, proceed to program the new second
  306. half word */
  307. tmp = Address + 2;
  308. *(__IO uint16_t*) tmp = Data >> 16;
  309. /* Wait for last operation to be completed */
  310. status = FLASH_WaitForLastOperation(ProgramTimeout);
  311. if(status != FLASH_TIMEOUT)
  312. {
  313. /* Disable the PG Bit */
  314. FLASH->CR &= CR_PG_Reset;
  315. }
  316. }
  317. else
  318. {
  319. if (status != FLASH_TIMEOUT)
  320. {
  321. /* Disable the PG Bit */
  322. FLASH->CR &= CR_PG_Reset;
  323. }
  324. }
  325. }
  326. /* Return the Program Status */
  327. return status;
  328. }
  329. /**
  330. * @brief Programs a half word at a specified address.
  331. * @param Address: specifies the address to be programmed.
  332. * @param Data: specifies the data to be programmed.
  333. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  334. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  335. */
  336. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
  337. {
  338. FLASH_Status status = FLASH_COMPLETE;
  339. /* Check the parameters */
  340. assert_param(IS_FLASH_ADDRESS(Address));
  341. /* Wait for last operation to be completed */
  342. status = FLASH_WaitForLastOperation(ProgramTimeout);
  343. if(status == FLASH_COMPLETE)
  344. {
  345. /* if the previous operation is completed, proceed to program the new data */
  346. FLASH->CR |= CR_PG_Set;
  347. *(__IO uint16_t*)Address = Data;
  348. /* Wait for last operation to be completed */
  349. status = FLASH_WaitForLastOperation(ProgramTimeout);
  350. if(status != FLASH_TIMEOUT)
  351. {
  352. /* if the program operation is completed, disable the PG Bit */
  353. FLASH->CR &= CR_PG_Reset;
  354. }
  355. }
  356. /* Return the Program Status */
  357. return status;
  358. }
  359. /**
  360. * @brief Programs a half word at a specified Option Byte Data address.
  361. * @param Address: specifies the address to be programmed.
  362. * This parameter can be 0x1FFFF804 or 0x1FFFF806.
  363. * @param Data: specifies the data to be programmed.
  364. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  365. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  366. */
  367. FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)
  368. {
  369. FLASH_Status status = FLASH_COMPLETE;
  370. /* Check the parameters */
  371. assert_param(IS_OB_DATA_ADDRESS(Address));
  372. status = FLASH_WaitForLastOperation(ProgramTimeout);
  373. if(status == FLASH_COMPLETE)
  374. {
  375. /* Authorize the small information block programming */
  376. FLASH->OPTKEYR = FLASH_KEY1;
  377. FLASH->OPTKEYR = FLASH_KEY2;
  378. /* Enables the Option Bytes Programming operation */
  379. FLASH->CR |= CR_OPTPG_Set;
  380. *(__IO uint16_t*)Address = Data;
  381. /* Wait for last operation to be completed */
  382. status = FLASH_WaitForLastOperation(ProgramTimeout);
  383. if(status != FLASH_TIMEOUT)
  384. {
  385. /* if the program operation is completed, disable the OPTPG Bit */
  386. FLASH->CR &= CR_OPTPG_Reset;
  387. }
  388. }
  389. /* Return the Option Byte Data Program Status */
  390. return status;
  391. }
  392. /**
  393. * @brief Write protects the desired pages
  394. * @param FLASH_Pages: specifies the address of the pages to be write protected.
  395. * This parameter can be:
  396. * @arg For @b STM32_Low-density_devices: value between FLASH_WRProt_Pages0to3 and FLASH_WRProt_Pages28to31
  397. * @arg For @b STM32_Medium-density_devices: value between FLASH_WRProt_Pages0to3
  398. * and FLASH_WRProt_Pages124to127
  399. * @arg For @b STM32_High-density_devices: value between FLASH_WRProt_Pages0to1 and
  400. * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to255
  401. * @arg For @b STM32_Connectivity_line_devices: value between FLASH_WRProt_Pages0to1 and
  402. * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to127
  403. * @arg FLASH_WRProt_AllPages
  404. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  405. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  406. */
  407. FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages)
  408. {
  409. uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
  410. FLASH_Status status = FLASH_COMPLETE;
  411. /* Check the parameters */
  412. assert_param(IS_FLASH_WRPROT_PAGE(FLASH_Pages));
  413. FLASH_Pages = (uint32_t)(~FLASH_Pages);
  414. WRP0_Data = (uint16_t)(FLASH_Pages & WRP0_Mask);
  415. WRP1_Data = (uint16_t)((FLASH_Pages & WRP1_Mask) >> 8);
  416. WRP2_Data = (uint16_t)((FLASH_Pages & WRP2_Mask) >> 16);
  417. WRP3_Data = (uint16_t)((FLASH_Pages & WRP3_Mask) >> 24);
  418. /* Wait for last operation to be completed */
  419. status = FLASH_WaitForLastOperation(ProgramTimeout);
  420. if(status == FLASH_COMPLETE)
  421. {
  422. /* Authorizes the small information block programming */
  423. FLASH->OPTKEYR = FLASH_KEY1;
  424. FLASH->OPTKEYR = FLASH_KEY2;
  425. FLASH->CR |= CR_OPTPG_Set;
  426. if(WRP0_Data != 0xFF)
  427. {
  428. OB->WRP0 = WRP0_Data;
  429. /* Wait for last operation to be completed */
  430. status = FLASH_WaitForLastOperation(ProgramTimeout);
  431. }
  432. if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
  433. {
  434. OB->WRP1 = WRP1_Data;
  435. /* Wait for last operation to be completed */
  436. status = FLASH_WaitForLastOperation(ProgramTimeout);
  437. }
  438. if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
  439. {
  440. OB->WRP2 = WRP2_Data;
  441. /* Wait for last operation to be completed */
  442. status = FLASH_WaitForLastOperation(ProgramTimeout);
  443. }
  444. if((status == FLASH_COMPLETE)&& (WRP3_Data != 0xFF))
  445. {
  446. OB->WRP3 = WRP3_Data;
  447. /* Wait for last operation to be completed */
  448. status = FLASH_WaitForLastOperation(ProgramTimeout);
  449. }
  450. if(status != FLASH_TIMEOUT)
  451. {
  452. /* if the program operation is completed, disable the OPTPG Bit */
  453. FLASH->CR &= CR_OPTPG_Reset;
  454. }
  455. }
  456. /* Return the write protection operation Status */
  457. return status;
  458. }
  459. /**
  460. * @brief Enables or disables the read out protection.
  461. * @note If the user has already programmed the other option bytes before calling
  462. * this function, he must re-program them since this function erases all option bytes.
  463. * @param Newstate: new state of the ReadOut Protection.
  464. * This parameter can be: ENABLE or DISABLE.
  465. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  466. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  467. */
  468. FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState)
  469. {
  470. FLASH_Status status = FLASH_COMPLETE;
  471. /* Check the parameters */
  472. assert_param(IS_FUNCTIONAL_STATE(NewState));
  473. status = FLASH_WaitForLastOperation(EraseTimeout);
  474. if(status == FLASH_COMPLETE)
  475. {
  476. /* Authorizes the small information block programming */
  477. FLASH->OPTKEYR = FLASH_KEY1;
  478. FLASH->OPTKEYR = FLASH_KEY2;
  479. FLASH->CR |= CR_OPTER_Set;
  480. FLASH->CR |= CR_STRT_Set;
  481. /* Wait for last operation to be completed */
  482. status = FLASH_WaitForLastOperation(EraseTimeout);
  483. if(status == FLASH_COMPLETE)
  484. {
  485. /* if the erase operation is completed, disable the OPTER Bit */
  486. FLASH->CR &= CR_OPTER_Reset;
  487. /* Enable the Option Bytes Programming operation */
  488. FLASH->CR |= CR_OPTPG_Set;
  489. if(NewState != DISABLE)
  490. {
  491. OB->RDP = 0x00;
  492. }
  493. else
  494. {
  495. OB->RDP = RDP_Key;
  496. }
  497. /* Wait for last operation to be completed */
  498. status = FLASH_WaitForLastOperation(EraseTimeout);
  499. if(status != FLASH_TIMEOUT)
  500. {
  501. /* if the program operation is completed, disable the OPTPG Bit */
  502. FLASH->CR &= CR_OPTPG_Reset;
  503. }
  504. }
  505. else
  506. {
  507. if(status != FLASH_TIMEOUT)
  508. {
  509. /* Disable the OPTER Bit */
  510. FLASH->CR &= CR_OPTER_Reset;
  511. }
  512. }
  513. }
  514. /* Return the protection operation Status */
  515. return status;
  516. }
  517. /**
  518. * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  519. * @param OB_IWDG: Selects the IWDG mode
  520. * This parameter can be one of the following values:
  521. * @arg OB_IWDG_SW: Software IWDG selected
  522. * @arg OB_IWDG_HW: Hardware IWDG selected
  523. * @param OB_STOP: Reset event when entering STOP mode.
  524. * This parameter can be one of the following values:
  525. * @arg OB_STOP_NoRST: No reset generated when entering in STOP
  526. * @arg OB_STOP_RST: Reset generated when entering in STOP
  527. * @param OB_STDBY: Reset event when entering Standby mode.
  528. * This parameter can be one of the following values:
  529. * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
  530. * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
  531. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  532. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  533. */
  534. FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY)
  535. {
  536. FLASH_Status status = FLASH_COMPLETE;
  537. /* Check the parameters */
  538. assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
  539. assert_param(IS_OB_STOP_SOURCE(OB_STOP));
  540. assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
  541. /* Authorize the small information block programming */
  542. FLASH->OPTKEYR = FLASH_KEY1;
  543. FLASH->OPTKEYR = FLASH_KEY2;
  544. /* Wait for last operation to be completed */
  545. status = FLASH_WaitForLastOperation(ProgramTimeout);
  546. if(status == FLASH_COMPLETE)
  547. {
  548. /* Enable the Option Bytes Programming operation */
  549. FLASH->CR |= CR_OPTPG_Set;
  550. OB->USER = OB_IWDG | (uint16_t)(OB_STOP | (uint16_t)(OB_STDBY | ((uint16_t)0xF8)));
  551. /* Wait for last operation to be completed */
  552. status = FLASH_WaitForLastOperation(ProgramTimeout);
  553. if(status != FLASH_TIMEOUT)
  554. {
  555. /* if the program operation is completed, disable the OPTPG Bit */
  556. FLASH->CR &= CR_OPTPG_Reset;
  557. }
  558. }
  559. /* Return the Option Byte program Status */
  560. return status;
  561. }
  562. /**
  563. * @brief Returns the FLASH User Option Bytes values.
  564. * @param None
  565. * @retval The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)
  566. * and RST_STDBY(Bit2).
  567. */
  568. uint32_t FLASH_GetUserOptionByte(void)
  569. {
  570. /* Return the User Option Byte */
  571. return (uint32_t)(FLASH->OBR >> 2);
  572. }
  573. /**
  574. * @brief Returns the FLASH Write Protection Option Bytes Register value.
  575. * @param None
  576. * @retval The FLASH Write Protection Option Bytes Register value
  577. */
  578. uint32_t FLASH_GetWriteProtectionOptionByte(void)
  579. {
  580. /* Return the Falsh write protection Register value */
  581. return (uint32_t)(FLASH->WRPR);
  582. }
  583. /**
  584. * @brief Checks whether the FLASH Read Out Protection Status is set or not.
  585. * @param None
  586. * @retval FLASH ReadOut Protection Status(SET or RESET)
  587. */
  588. FlagStatus FLASH_GetReadOutProtectionStatus(void)
  589. {
  590. FlagStatus readoutstatus = RESET;
  591. if ((FLASH->OBR & RDPRT_Mask) != (uint32_t)RESET)
  592. {
  593. readoutstatus = SET;
  594. }
  595. else
  596. {
  597. readoutstatus = RESET;
  598. }
  599. return readoutstatus;
  600. }
  601. /**
  602. * @brief Checks whether the FLASH Prefetch Buffer status is set or not.
  603. * @param None
  604. * @retval FLASH Prefetch Buffer Status (SET or RESET).
  605. */
  606. FlagStatus FLASH_GetPrefetchBufferStatus(void)
  607. {
  608. FlagStatus bitstatus = RESET;
  609. if ((FLASH->ACR & ACR_PRFTBS_Mask) != (uint32_t)RESET)
  610. {
  611. bitstatus = SET;
  612. }
  613. else
  614. {
  615. bitstatus = RESET;
  616. }
  617. /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
  618. return bitstatus;
  619. }
  620. /**
  621. * @brief Enables or disables the specified FLASH interrupts.
  622. * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or disabled.
  623. * This parameter can be any combination of the following values:
  624. * @arg FLASH_IT_ERROR: FLASH Error Interrupt
  625. * @arg FLASH_IT_EOP: FLASH end of operation Interrupt
  626. * @param NewState: new state of the specified Flash interrupts.
  627. * This parameter can be: ENABLE or DISABLE.
  628. * @retval None
  629. */
  630. void FLASH_ITConfig(uint16_t FLASH_IT, FunctionalState NewState)
  631. {
  632. /* Check the parameters */
  633. assert_param(IS_FLASH_IT(FLASH_IT));
  634. assert_param(IS_FUNCTIONAL_STATE(NewState));
  635. if(NewState != DISABLE)
  636. {
  637. /* Enable the interrupt sources */
  638. FLASH->CR |= FLASH_IT;
  639. }
  640. else
  641. {
  642. /* Disable the interrupt sources */
  643. FLASH->CR &= ~(uint32_t)FLASH_IT;
  644. }
  645. }
  646. /**
  647. * @brief Checks whether the specified FLASH flag is set or not.
  648. * @param FLASH_FLAG: specifies the FLASH flag to check.
  649. * This parameter can be one of the following values:
  650. * @arg FLASH_FLAG_BSY: FLASH Busy flag
  651. * @arg FLASH_FLAG_PGERR: FLASH Program error flag
  652. * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
  653. * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
  654. * @arg FLASH_FLAG_OPTERR: FLASH Option Byte error flag
  655. * @retval The new state of FLASH_FLAG (SET or RESET).
  656. */
  657. FlagStatus FLASH_GetFlagStatus(uint16_t FLASH_FLAG)
  658. {
  659. FlagStatus bitstatus = RESET;
  660. /* Check the parameters */
  661. assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;
  662. if(FLASH_FLAG == FLASH_FLAG_OPTERR)
  663. {
  664. if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)
  665. {
  666. bitstatus = SET;
  667. }
  668. else
  669. {
  670. bitstatus = RESET;
  671. }
  672. }
  673. else
  674. {
  675. if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
  676. {
  677. bitstatus = SET;
  678. }
  679. else
  680. {
  681. bitstatus = RESET;
  682. }
  683. }
  684. /* Return the new state of FLASH_FLAG (SET or RESET) */
  685. return bitstatus;
  686. }
  687. /**
  688. * @brief Clears the FLASH’s pending flags.
  689. * @param FLASH_FLAG: specifies the FLASH flags to clear.
  690. * This parameter can be any combination of the following values:
  691. * @arg FLASH_FLAG_PGERR: FLASH Program error flag
  692. * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
  693. * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
  694. * @retval None
  695. */
  696. void FLASH_ClearFlag(uint16_t FLASH_FLAG)
  697. {
  698. /* Check the parameters */
  699. assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;
  700. /* Clear the flags */
  701. FLASH->SR = FLASH_FLAG;
  702. }
  703. /**
  704. * @brief Returns the FLASH Status.
  705. * @param None
  706. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  707. * FLASH_ERROR_WRP or FLASH_COMPLETE
  708. */
  709. FLASH_Status FLASH_GetStatus(void)
  710. {
  711. FLASH_Status flashstatus = FLASH_COMPLETE;
  712. if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
  713. {
  714. flashstatus = FLASH_BUSY;
  715. }
  716. else
  717. {
  718. if((FLASH->SR & FLASH_FLAG_PGERR) != 0)
  719. {
  720. flashstatus = FLASH_ERROR_PG;
  721. }
  722. else
  723. {
  724. if((FLASH->SR & FLASH_FLAG_WRPRTERR) != 0 )
  725. {
  726. flashstatus = FLASH_ERROR_WRP;
  727. }
  728. else
  729. {
  730. flashstatus = FLASH_COMPLETE;
  731. }
  732. }
  733. }
  734. /* Return the Flash Status */
  735. return flashstatus;
  736. }
  737. /**
  738. * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
  739. * @param Timeout: FLASH progamming Timeout
  740. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  741. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  742. */
  743. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
  744. {
  745. FLASH_Status status = FLASH_COMPLETE;
  746. /* Check for the Flash Status */
  747. status = FLASH_GetStatus();
  748. /* Wait for a Flash operation to complete or a TIMEOUT to occur */
  749. while((status == FLASH_BUSY) && (Timeout != 0x00))
  750. {
  751. delay();
  752. status = FLASH_GetStatus();
  753. Timeout--;
  754. }
  755. if(Timeout == 0x00 )
  756. {
  757. status = FLASH_TIMEOUT;
  758. }
  759. /* Return the operation status */
  760. return status;
  761. }
  762. /**
  763. * @brief Inserts a time delay.
  764. * @param None
  765. * @retval None
  766. */
  767. static void delay(void)
  768. {
  769. __IO uint32_t i = 0;
  770. for(i = 0xFF; i != 0; i--)
  771. {
  772. }
  773. }
  774. /**
  775. * @}
  776. */
  777. /**
  778. * @}
  779. */
  780. /**
  781. * @}
  782. */
  783. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/