HAL_flash.c 26 KB

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