stm32f10x_flash.c 32 KB

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