stm32f2xx_flash.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_flash.c
  4. * @author MCD Application Team
  5. * @version V1.1.3
  6. * @date 31-December-2021
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the FLASH peripheral:
  9. * - FLASH Interface configuration
  10. * - FLASH Memory Programming
  11. * - Option Bytes Programming
  12. * - Interrupts and flags management
  13. *
  14. * @verbatim
  15. *
  16. * ===================================================================
  17. * How to use this driver
  18. * ===================================================================
  19. *
  20. * This driver provides functions to configure and program the FLASH
  21. * memory of all STM32F2xx devices.
  22. * These functions are split in 4 groups:
  23. *
  24. * 1. FLASH Interface configuration functions: this group includes the
  25. * management of the following features:
  26. * - Set the latency
  27. * - Enable/Disable the prefetch buffer
  28. * - Enable/Disable the Instruction cache and the Data cache
  29. * - Reset the Instruction cache and the Data cache
  30. *
  31. * 2. FLASH Memory Programming functions: this group includes all needed
  32. * functions to erase and program the main memory:
  33. * - Lock and Unlock the FLASH interface
  34. * - Erase function: Erase sector, erase all sectors
  35. * - Program functions: byte, half word, word and double word
  36. *
  37. * 3. Option Bytes Programming functions: this group includes all needed
  38. * functions to manage the Option Bytes:
  39. * - Set/Reset the write protection
  40. * - Set the Read protection Level
  41. * - Set the BOR level
  42. * - Program the user Option Bytes
  43. * - Launch the Option Bytes loader
  44. *
  45. * 4. Interrupts and flags management functions: this group
  46. * includes all needed functions to:
  47. * - Enable/Disable the FLASH interrupt sources
  48. * - Get flags status
  49. * - Clear flags
  50. * - Get FLASH operation status
  51. * - Wait for last FLASH operation
  52. *
  53. * @endverbatim
  54. *
  55. ******************************************************************************
  56. * @attention
  57. *
  58. * Copyright (c) 2012 STMicroelectronics.
  59. * All rights reserved.
  60. *
  61. * This software is licensed under terms that can be found in the LICENSE file
  62. * in the root directory of this software component.
  63. * If no LICENSE file comes with this software, it is provided AS-IS.
  64. *
  65. ******************************************************************************
  66. */
  67. /* Includes ------------------------------------------------------------------*/
  68. #include "stm32f2xx_flash.h"
  69. /** @addtogroup STM32F2xx_StdPeriph_Driver
  70. * @{
  71. */
  72. /** @defgroup FLASH
  73. * @brief FLASH driver modules
  74. * @{
  75. */
  76. /* Private typedef -----------------------------------------------------------*/
  77. /* Private define ------------------------------------------------------------*/
  78. #define SECTOR_MASK ((uint32_t)0xFFFFFF07)
  79. /* Private macro -------------------------------------------------------------*/
  80. /* Private variables ---------------------------------------------------------*/
  81. /* Private function prototypes -----------------------------------------------*/
  82. /* Private functions ---------------------------------------------------------*/
  83. /** @defgroup FLASH_Private_Functions
  84. * @{
  85. */
  86. /** @defgroup FLASH_Group1 FLASH Interface configuration functions
  87. * @brief FLASH Interface configuration functions
  88. *
  89. @verbatim
  90. ===============================================================================
  91. FLASH Interface configuration functions
  92. ===============================================================================
  93. This group includes the following functions:
  94. - void FLASH_SetLatency(uint32_t FLASH_Latency)
  95. To correctly read data from FLASH memory, the number of wait states (LATENCY)
  96. must be correctly programmed according to the frequency of the CPU clock
  97. (HCLK) and the supply voltage of the device.
  98. +-------------------------------------------------------------------------------------+
  99. | Latency | HCLK clock frequency (MHz) |
  100. | |---------------------------------------------------------------------|
  101. | | voltage range | voltage range | voltage range | voltage range |
  102. | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V |
  103. |---------------|----------------|----------------|-----------------|-----------------|
  104. |0WS(1CPU cycle)|0 < HCLK <= 30 |0 < HCLK <= 24 |0 < HCLK <= 18 |0 < HCLK <= 16 |
  105. |---------------|----------------|----------------|-----------------|-----------------|
  106. |1WS(2CPU cycle)|30 < HCLK <= 60 |24 < HCLK <= 48 |18 < HCLK <= 36 |16 < HCLK <= 32 |
  107. |---------------|----------------|----------------|-----------------|-----------------|
  108. |2WS(3CPU cycle)|60 < HCLK <= 90 |48 < HCLK <= 72 |36 < HCLK <= 54 |32 < HCLK <= 48 |
  109. |---------------|----------------|----------------|-----------------|-----------------|
  110. |3WS(4CPU cycle)|90 < HCLK <= 120|72 < HCLK <= 96 |54 < HCLK <= 72 |48 < HCLK <= 64 |
  111. |---------------|----------------|----------------|-----------------|-----------------|
  112. |4WS(5CPU cycle)| NA |96 < HCLK <= 120|72 < HCLK <= 90 |64 < HCLK <= 80 |
  113. |---------------|----------------|----------------|-----------------|-----------------|
  114. |5WS(6CPU cycle)| NA | NA |90 < HCLK <= 108 |80 < HCLK <= 96 |
  115. |---------------|----------------|----------------|-----------------|-----------------|
  116. |6WS(7CPU cycle)| NA | NA |108 < HCLK <= 120|96 < HCLK <= 112 |
  117. |---------------|----------------|----------------|-----------------|-----------------|
  118. |7WS(8CPU cycle)| NA | NA | NA |112 < HCLK <= 120|
  119. |***************|****************|****************|*****************|*****************|*****************************+
  120. | | voltage range | voltage range | voltage range | voltage range | voltage range 2.7 V - 3.6 V |
  121. | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V | with External Vpp = 9V |
  122. |---------------|----------------|----------------|-----------------|-----------------|-----------------------------|
  123. |Max Parallelism| x32 | x16 | x8 | x64 |
  124. |---------------|----------------|----------------|-----------------|-----------------|-----------------------------|
  125. |PSIZE[1:0] | 10 | 01 | 00 | 11 |
  126. +-------------------------------------------------------------------------------------------------------------------+
  127. - void FLASH_PrefetchBufferCmd(FunctionalState NewState)
  128. - void FLASH_InstructionCacheCmd(FunctionalState NewState)
  129. - void FLASH_DataCacheCmd(FunctionalState NewState)
  130. - void FLASH_InstructionCacheReset(void)
  131. - void FLASH_DataCacheReset(void)
  132. The unlock sequence is not needed for these functions.
  133. @endverbatim
  134. * @{
  135. */
  136. /**
  137. * @brief Sets the code latency value.
  138. * @param FLASH_Latency: specifies the FLASH Latency value.
  139. * This parameter can be one of the following values:
  140. * @arg FLASH_Latency_0: FLASH Zero Latency cycle
  141. * @arg FLASH_Latency_1: FLASH One Latency cycle
  142. * @arg FLASH_Latency_2: FLASH Two Latency cycles
  143. * @arg FLASH_Latency_3: FLASH Three Latency cycles
  144. * @arg FLASH_Latency_4: FLASH Four Latency cycles
  145. * @arg FLASH_Latency_5: FLASH Five Latency cycles
  146. * @arg FLASH_Latency_6: FLASH Six Latency cycles
  147. * @arg FLASH_Latency_7: FLASH Seven Latency cycles
  148. * @retval None
  149. */
  150. void FLASH_SetLatency(uint32_t FLASH_Latency)
  151. {
  152. /* Check the parameters */
  153. assert_param(IS_FLASH_LATENCY(FLASH_Latency));
  154. /* Perform Byte access to FLASH_ACR[8:0] to set the Latency value */
  155. *(__IO uint8_t *)ACR_BYTE0_ADDRESS = (uint8_t)FLASH_Latency;
  156. }
  157. /**
  158. * @brief Enables or disables the Prefetch Buffer.
  159. * @param NewState: new state of the Prefetch Buffer.
  160. * This parameter can be: ENABLE or DISABLE.
  161. * @retval None
  162. */
  163. void FLASH_PrefetchBufferCmd(FunctionalState NewState)
  164. {
  165. /* Check the parameters */
  166. assert_param(IS_FUNCTIONAL_STATE(NewState));
  167. /* Enable or disable the Prefetch Buffer */
  168. if(NewState != DISABLE)
  169. {
  170. FLASH->ACR |= FLASH_ACR_PRFTEN;
  171. }
  172. else
  173. {
  174. FLASH->ACR &= (~FLASH_ACR_PRFTEN);
  175. }
  176. }
  177. /**
  178. * @brief Enables or disables the Instruction Cache feature.
  179. * @param NewState: new state of the Instruction Cache.
  180. * This parameter can be: ENABLE or DISABLE.
  181. * @retval None
  182. */
  183. void FLASH_InstructionCacheCmd(FunctionalState NewState)
  184. {
  185. /* Check the parameters */
  186. assert_param(IS_FUNCTIONAL_STATE(NewState));
  187. if(NewState != DISABLE)
  188. {
  189. FLASH->ACR |= FLASH_ACR_ICEN;
  190. }
  191. else
  192. {
  193. FLASH->ACR &= (~FLASH_ACR_ICEN);
  194. }
  195. }
  196. /**
  197. * @brief Enables or disables the Data Cache feature.
  198. * @param NewState: new state of the Data Cache.
  199. * This parameter can be: ENABLE or DISABLE.
  200. * @retval None
  201. */
  202. void FLASH_DataCacheCmd(FunctionalState NewState)
  203. {
  204. /* Check the parameters */
  205. assert_param(IS_FUNCTIONAL_STATE(NewState));
  206. if(NewState != DISABLE)
  207. {
  208. FLASH->ACR |= FLASH_ACR_DCEN;
  209. }
  210. else
  211. {
  212. FLASH->ACR &= (~FLASH_ACR_DCEN);
  213. }
  214. }
  215. /**
  216. * @brief Resets the Instruction Cache.
  217. * @note This function must be used only when the Instruction Cache is disabled.
  218. * @param None
  219. * @retval None
  220. */
  221. void FLASH_InstructionCacheReset(void)
  222. {
  223. FLASH->ACR |= FLASH_ACR_ICRST;
  224. }
  225. /**
  226. * @brief Resets the Data Cache.
  227. * @note This function must be used only when the Data Cache is disabled.
  228. * @param None
  229. * @retval None
  230. */
  231. void FLASH_DataCacheReset(void)
  232. {
  233. FLASH->ACR |= FLASH_ACR_DCRST;
  234. }
  235. /**
  236. * @}
  237. */
  238. /** @defgroup FLASH_Group2 FLASH Memory Programming functions
  239. * @brief FLASH Memory Programming functions
  240. *
  241. @verbatim
  242. ===============================================================================
  243. FLASH Memory Programming functions
  244. ===============================================================================
  245. This group includes the following functions:
  246. - void FLASH_Unlock(void)
  247. - void FLASH_Lock(void)
  248. - FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange)
  249. - FLASH_Status FLASH_EraseAllSectors(uint8_t VoltageRange)
  250. - FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data)
  251. - FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
  252. - FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
  253. - FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data)
  254. Any operation of erase or program should follow these steps:
  255. 1. Call the FLASH_Unlock() function to enable the FLASH control register access
  256. 2. Call the desired function to erase sector(s) or program data
  257. 3. Call the FLASH_Lock() function to disable the FLASH control register access
  258. (recommended to protect the FLASH memory against possible unwanted operation)
  259. @endverbatim
  260. * @{
  261. */
  262. /**
  263. * @brief Unlocks the FLASH control register access
  264. * @param None
  265. * @retval None
  266. */
  267. void FLASH_Unlock(void)
  268. {
  269. if((FLASH->CR & FLASH_CR_LOCK) != RESET)
  270. {
  271. /* Authorize the FLASH Registers access */
  272. FLASH->KEYR = FLASH_KEY1;
  273. FLASH->KEYR = FLASH_KEY2;
  274. }
  275. }
  276. /**
  277. * @brief Locks the FLASH control register access
  278. * @param None
  279. * @retval None
  280. */
  281. void FLASH_Lock(void)
  282. {
  283. /* Set the LOCK Bit to lock the FLASH Registers access */
  284. FLASH->CR |= FLASH_CR_LOCK;
  285. }
  286. /**
  287. * @brief Erases a specified FLASH Sector.
  288. *
  289. * @param FLASH_Sector: The Sector number to be erased.
  290. * This parameter can be a value between FLASH_Sector_0 and FLASH_Sector_11
  291. *
  292. * @param VoltageRange: The device voltage range which defines the erase parallelism.
  293. * This parameter can be one of the following values:
  294. * @arg VoltageRange_1: when the device voltage range is 1.8V to 2.1V,
  295. * the operation will be done by byte (8-bit)
  296. * @arg VoltageRange_2: when the device voltage range is 2.1V to 2.7V,
  297. * the operation will be done by half word (16-bit)
  298. * @arg VoltageRange_3: when the device voltage range is 2.7V to 3.6V,
  299. * the operation will be done by word (32-bit)
  300. * @arg VoltageRange_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
  301. * the operation will be done by double word (64-bit)
  302. *
  303. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
  304. * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
  305. */
  306. FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange)
  307. {
  308. uint32_t tmp_psize = 0x0;
  309. FLASH_Status status = FLASH_COMPLETE;
  310. /* Check the parameters */
  311. assert_param(IS_FLASH_SECTOR(FLASH_Sector));
  312. assert_param(IS_VOLTAGERANGE(VoltageRange));
  313. if(VoltageRange == VoltageRange_1)
  314. {
  315. tmp_psize = FLASH_PSIZE_BYTE;
  316. }
  317. else if(VoltageRange == VoltageRange_2)
  318. {
  319. tmp_psize = FLASH_PSIZE_HALF_WORD;
  320. }
  321. else if(VoltageRange == VoltageRange_3)
  322. {
  323. tmp_psize = FLASH_PSIZE_WORD;
  324. }
  325. else
  326. {
  327. tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
  328. }
  329. /* Wait for last operation to be completed */
  330. status = FLASH_WaitForLastOperation();
  331. if(status == FLASH_COMPLETE)
  332. {
  333. /* if the previous operation is completed, proceed to erase the sector */
  334. FLASH->CR &= CR_PSIZE_MASK;
  335. FLASH->CR |= tmp_psize;
  336. FLASH->CR &= SECTOR_MASK;
  337. FLASH->CR |= FLASH_CR_SER | FLASH_Sector;
  338. FLASH->CR |= FLASH_CR_STRT;
  339. /* Wait for last operation to be completed */
  340. status = FLASH_WaitForLastOperation();
  341. /* if the erase operation is completed, disable the SER Bit */
  342. FLASH->CR &= (~FLASH_CR_SER);
  343. FLASH->CR &= SECTOR_MASK;
  344. }
  345. /* Return the Erase Status */
  346. return status;
  347. }
  348. /**
  349. * @brief Erases all FLASH Sectors.
  350. *
  351. * @param VoltageRange: The device voltage range which defines the erase parallelism.
  352. * This parameter can be one of the following values:
  353. * @arg VoltageRange_1: when the device voltage range is 1.8V to 2.1V,
  354. * the operation will be done by byte (8-bit)
  355. * @arg VoltageRange_2: when the device voltage range is 2.1V to 2.7V,
  356. * the operation will be done by half word (16-bit)
  357. * @arg VoltageRange_3: when the device voltage range is 2.7V to 3.6V,
  358. * the operation will be done by word (32-bit)
  359. * @arg VoltageRange_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
  360. * the operation will be done by double word (64-bit)
  361. *
  362. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
  363. * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
  364. */
  365. FLASH_Status FLASH_EraseAllSectors(uint8_t VoltageRange)
  366. {
  367. uint32_t tmp_psize = 0x0;
  368. FLASH_Status status = FLASH_COMPLETE;
  369. /* Wait for last operation to be completed */
  370. status = FLASH_WaitForLastOperation();
  371. assert_param(IS_VOLTAGERANGE(VoltageRange));
  372. if(VoltageRange == VoltageRange_1)
  373. {
  374. tmp_psize = FLASH_PSIZE_BYTE;
  375. }
  376. else if(VoltageRange == VoltageRange_2)
  377. {
  378. tmp_psize = FLASH_PSIZE_HALF_WORD;
  379. }
  380. else if(VoltageRange == VoltageRange_3)
  381. {
  382. tmp_psize = FLASH_PSIZE_WORD;
  383. }
  384. else
  385. {
  386. tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
  387. }
  388. if(status == FLASH_COMPLETE)
  389. {
  390. /* if the previous operation is completed, proceed to erase all sectors */
  391. FLASH->CR &= CR_PSIZE_MASK;
  392. FLASH->CR |= tmp_psize;
  393. FLASH->CR |= FLASH_CR_MER;
  394. FLASH->CR |= FLASH_CR_STRT;
  395. /* Wait for last operation to be completed */
  396. status = FLASH_WaitForLastOperation();
  397. /* if the erase operation is completed, disable the MER Bit */
  398. FLASH->CR &= (~FLASH_CR_MER);
  399. }
  400. /* Return the Erase Status */
  401. return status;
  402. }
  403. /**
  404. * @brief Programs a double word (64-bit) at a specified address.
  405. * @note This function must be used when the device voltage range is from
  406. * 2.7V to 3.6V and an External Vpp is present.
  407. * @param Address: specifies the address to be programmed.
  408. * @param Data: specifies the data to be programmed.
  409. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
  410. * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
  411. */
  412. FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data)
  413. {
  414. FLASH_Status status = FLASH_COMPLETE;
  415. /* Check the parameters */
  416. assert_param(IS_FLASH_ADDRESS(Address));
  417. /* Wait for last operation to be completed */
  418. status = FLASH_WaitForLastOperation();
  419. if(status == FLASH_COMPLETE)
  420. {
  421. /* if the previous operation is completed, proceed to program the new data */
  422. FLASH->CR &= CR_PSIZE_MASK;
  423. FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
  424. FLASH->CR |= FLASH_CR_PG;
  425. *(__IO uint64_t*)Address = Data;
  426. /* Wait for last operation to be completed */
  427. status = FLASH_WaitForLastOperation();
  428. /* if the program operation is completed, disable the PG Bit */
  429. FLASH->CR &= (~FLASH_CR_PG);
  430. }
  431. /* Return the Program Status */
  432. return status;
  433. }
  434. /**
  435. * @brief Programs a word (32-bit) at a specified address.
  436. * @param Address: specifies the address to be programmed.
  437. * This parameter can be any address in Program memory zone or in OTP zone.
  438. * @note This function must be used when the device voltage range is from 2.7V to 3.6V.
  439. * @param Data: specifies the data to be programmed.
  440. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
  441. * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
  442. */
  443. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
  444. {
  445. FLASH_Status status = FLASH_COMPLETE;
  446. /* Check the parameters */
  447. assert_param(IS_FLASH_ADDRESS(Address));
  448. /* Wait for last operation to be completed */
  449. status = FLASH_WaitForLastOperation();
  450. if(status == FLASH_COMPLETE)
  451. {
  452. /* if the previous operation is completed, proceed to program the new data */
  453. FLASH->CR &= CR_PSIZE_MASK;
  454. FLASH->CR |= FLASH_PSIZE_WORD;
  455. FLASH->CR |= FLASH_CR_PG;
  456. *(__IO uint32_t*)Address = Data;
  457. /* Wait for last operation to be completed */
  458. status = FLASH_WaitForLastOperation();
  459. /* if the program operation is completed, disable the PG Bit */
  460. FLASH->CR &= (~FLASH_CR_PG);
  461. }
  462. /* Return the Program Status */
  463. return status;
  464. }
  465. /**
  466. * @brief Programs a half word (16-bit) at a specified address.
  467. * @note This function must be used when the device voltage range is from 2.1V to 3.6V.
  468. * @param Address: specifies the address to be programmed.
  469. * This parameter can be any address in Program memory zone or in OTP zone.
  470. * @param Data: specifies the data to be programmed.
  471. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
  472. * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
  473. */
  474. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
  475. {
  476. FLASH_Status status = FLASH_COMPLETE;
  477. /* Check the parameters */
  478. assert_param(IS_FLASH_ADDRESS(Address));
  479. /* Wait for last operation to be completed */
  480. status = FLASH_WaitForLastOperation();
  481. if(status == FLASH_COMPLETE)
  482. {
  483. /* if the previous operation is completed, proceed to program the new data */
  484. FLASH->CR &= CR_PSIZE_MASK;
  485. FLASH->CR |= FLASH_PSIZE_HALF_WORD;
  486. FLASH->CR |= FLASH_CR_PG;
  487. *(__IO uint16_t*)Address = Data;
  488. /* Wait for last operation to be completed */
  489. status = FLASH_WaitForLastOperation();
  490. /* if the program operation is completed, disable the PG Bit */
  491. FLASH->CR &= (~FLASH_CR_PG);
  492. }
  493. /* Return the Program Status */
  494. return status;
  495. }
  496. /**
  497. * @brief Programs a byte (8-bit) at a specified address.
  498. * @note This function can be used within all the device supply voltage ranges.
  499. * @param Address: specifies the address to be programmed.
  500. * This parameter can be any address in Program memory zone or in OTP zone.
  501. * @param Data: specifies the data to be programmed.
  502. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
  503. * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
  504. */
  505. FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data)
  506. {
  507. FLASH_Status status = FLASH_COMPLETE;
  508. /* Check the parameters */
  509. assert_param(IS_FLASH_ADDRESS(Address));
  510. /* Wait for last operation to be completed */
  511. status = FLASH_WaitForLastOperation();
  512. if(status == FLASH_COMPLETE)
  513. {
  514. /* if the previous operation is completed, proceed to program the new data */
  515. FLASH->CR &= CR_PSIZE_MASK;
  516. FLASH->CR |= FLASH_PSIZE_BYTE;
  517. FLASH->CR |= FLASH_CR_PG;
  518. *(__IO uint8_t*)Address = Data;
  519. /* Wait for last operation to be completed */
  520. status = FLASH_WaitForLastOperation();
  521. /* if the program operation is completed, disable the PG Bit */
  522. FLASH->CR &= (~FLASH_CR_PG);
  523. }
  524. /* Return the Program Status */
  525. return status;
  526. }
  527. /**
  528. * @}
  529. */
  530. /** @defgroup FLASH_Group3 Option Bytes Programming functions
  531. * @brief Option Bytes Programming functions
  532. *
  533. @verbatim
  534. ===============================================================================
  535. Option Bytes Programming functions
  536. ===============================================================================
  537. This group includes the following functions:
  538. - void FLASH_OB_Unlock(void)
  539. - void FLASH_OB_Lock(void)
  540. - void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
  541. - void FLASH_OB_RDPConfig(uint8_t OB_RDP)
  542. - void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
  543. - void FLASH_OB_BORConfig(uint8_t OB_BOR)
  544. - FLASH_Status FLASH_ProgramOTP(uint32_t Address, uint32_t Data)
  545. - FLASH_Status FLASH_OB_Launch(void)
  546. - uint32_t FLASH_OB_GetUser(void)
  547. - uint8_t FLASH_OB_GetWRP(void)
  548. - uint8_t FLASH_OB_GetRDP(void)
  549. - uint8_t FLASH_OB_GetBOR(void)
  550. Any operation of erase or program should follow these steps:
  551. 1. Call the FLASH_OB_Unlock() function to enable the FLASH option control register access
  552. 2. Call one or several functions to program the desired Option Bytes:
  553. - void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState) => to Enable/Disable
  554. the desired sector write protection
  555. - void FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the desired read Protection Level
  556. - void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY) => to configure
  557. the user Option Bytes.
  558. - void FLASH_OB_BORConfig(uint8_t OB_BOR) => to set the BOR Level
  559. 3. Once all needed Option Bytes to be programmed are correctly written, call the
  560. FLASH_OB_Launch() function to launch the Option Bytes programming process.
  561. @note When changing the IWDG mode from HW to SW or from SW to HW, a system
  562. reset is needed to make the change effective.
  563. 4. Call the FLASH_OB_Lock() function to disable the FLASH option control register
  564. access (recommended to protect the Option Bytes against possible unwanted operations)
  565. @endverbatim
  566. * @{
  567. */
  568. /**
  569. * @brief Unlocks the FLASH Option Control Registers access.
  570. * @param None
  571. * @retval None
  572. */
  573. void FLASH_OB_Unlock(void)
  574. {
  575. if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
  576. {
  577. /* Authorizes the Option Byte register programming */
  578. FLASH->OPTKEYR = FLASH_OPT_KEY1;
  579. FLASH->OPTKEYR = FLASH_OPT_KEY2;
  580. }
  581. }
  582. /**
  583. * @brief Locks the FLASH Option Control Registers access.
  584. * @param None
  585. * @retval None
  586. */
  587. void FLASH_OB_Lock(void)
  588. {
  589. /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
  590. FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
  591. }
  592. /**
  593. * @brief Enables or disables the write protection of the desired sectors
  594. * @param OB_WRP: specifies the sector(s) to be write protected or unprotected.
  595. * This parameter can be one of the following values:
  596. * @arg OB_WRP: A value between OB_WRP_Sector0 and OB_WRP_Sector11
  597. * @arg OB_WRP_Sector_All
  598. * @param Newstate: new state of the Write Protection.
  599. * This parameter can be: ENABLE or DISABLE.
  600. * @retval None
  601. */
  602. void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
  603. {
  604. FLASH_Status status = FLASH_COMPLETE;
  605. /* Check the parameters */
  606. assert_param(IS_OB_WRP(OB_WRP));
  607. assert_param(IS_FUNCTIONAL_STATE(NewState));
  608. status = FLASH_WaitForLastOperation();
  609. if(status == FLASH_COMPLETE)
  610. {
  611. if(NewState != DISABLE)
  612. {
  613. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~OB_WRP);
  614. }
  615. else
  616. {
  617. *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)OB_WRP;
  618. }
  619. }
  620. }
  621. /**
  622. * @brief Sets the read protection level.
  623. * @param OB_RDP: specifies the read protection level.
  624. * This parameter can be one of the following values:
  625. * @arg OB_RDP_Level_0: No protection
  626. * @arg OB_RDP_Level_1: Read protection of the memory
  627. * @arg OB_RDP_Level_2: Full chip protection
  628. *
  629. * !!!Warning!!! When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
  630. *
  631. * @retval None
  632. */
  633. void FLASH_OB_RDPConfig(uint8_t OB_RDP)
  634. {
  635. FLASH_Status status = FLASH_COMPLETE;
  636. /* Check the parameters */
  637. assert_param(IS_OB_RDP(OB_RDP));
  638. status = FLASH_WaitForLastOperation();
  639. if(status == FLASH_COMPLETE)
  640. {
  641. *(__IO uint8_t*)OPTCR_BYTE1_ADDRESS = OB_RDP;
  642. }
  643. }
  644. /**
  645. * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  646. * @param OB_IWDG: Selects the IWDG mode
  647. * This parameter can be one of the following values:
  648. * @arg OB_IWDG_SW: Software IWDG selected
  649. * @arg OB_IWDG_HW: Hardware IWDG selected
  650. * @param OB_STOP: Reset event when entering STOP mode.
  651. * This parameter can be one of the following values:
  652. * @arg OB_STOP_NoRST: No reset generated when entering in STOP
  653. * @arg OB_STOP_RST: Reset generated when entering in STOP
  654. * @param OB_STDBY: Reset event when entering Standby mode.
  655. * This parameter can be one of the following values:
  656. * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
  657. * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
  658. * @retval None
  659. */
  660. void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
  661. {
  662. uint8_t optiontmp = 0xFF;
  663. FLASH_Status status = FLASH_COMPLETE;
  664. /* Check the parameters */
  665. assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
  666. assert_param(IS_OB_STOP_SOURCE(OB_STOP));
  667. assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
  668. /* Wait for last operation to be completed */
  669. status = FLASH_WaitForLastOperation();
  670. if(status == FLASH_COMPLETE)
  671. {
  672. /* Mask OPTLOCK, OPTSTRT and BOR_LEV bits */
  673. optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS) & (uint8_t)0x0F);
  674. /* Update User Option Byte */
  675. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS = OB_IWDG | (uint8_t)(OB_STDBY | (uint8_t)(OB_STOP | ((uint8_t)optiontmp)));
  676. }
  677. }
  678. /**
  679. * @brief Sets the BOR Level.
  680. * @param OB_BOR: specifies the Option Bytes BOR Reset Level.
  681. * This parameter can be one of the following values:
  682. * @arg OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
  683. * @arg OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
  684. * @arg OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
  685. * @arg OB_BOR_OFF: Supply voltage ranges from 1.62 to 2.1 V
  686. * @retval None
  687. */
  688. void FLASH_OB_BORConfig(uint8_t OB_BOR)
  689. {
  690. /* Check the parameters */
  691. assert_param(IS_OB_BOR(OB_BOR));
  692. /* Set the BOR Level */
  693. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BOR_LEV);
  694. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= OB_BOR;
  695. }
  696. /**
  697. * @brief Launch the option byte loading.
  698. * @param None
  699. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
  700. * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
  701. */
  702. FLASH_Status FLASH_OB_Launch(void)
  703. {
  704. FLASH_Status status = FLASH_COMPLETE;
  705. /* Set the OPTSTRT bit in OPTCR register */
  706. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
  707. /* Wait for last operation to be completed */
  708. status = FLASH_WaitForLastOperation();
  709. return status;
  710. }
  711. /**
  712. * @brief Returns the FLASH User Option Bytes values.
  713. * @param None
  714. * @retval The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1)
  715. * and RST_STDBY(Bit2).
  716. */
  717. uint8_t FLASH_OB_GetUser(void)
  718. {
  719. /* Return the User Option Byte */
  720. return (uint8_t)(FLASH->OPTCR >> 5);
  721. }
  722. /**
  723. * @brief Returns the FLASH Write Protection Option Bytes value.
  724. * @param None
  725. * @retval The FLASH Write Protection Option Bytes value
  726. */
  727. uint16_t FLASH_OB_GetWRP(void)
  728. {
  729. /* Return the FLASH write protection Register value */
  730. return (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
  731. }
  732. /**
  733. * @brief Returns the FLASH Read Protection level.
  734. * @param None
  735. * @retval FLASH ReadOut Protection Status:
  736. * - SET, when OB_RDP_Level_1 or OB_RDP_Level_2 is set
  737. * - RESET, when OB_RDP_Level_0 is set
  738. */
  739. FlagStatus FLASH_OB_GetRDP(void)
  740. {
  741. FlagStatus readstatus = RESET;
  742. if ((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) != (uint8_t)OB_RDP_Level_0))
  743. {
  744. readstatus = SET;
  745. }
  746. else
  747. {
  748. readstatus = RESET;
  749. }
  750. return readstatus;
  751. }
  752. /**
  753. * @brief Returns the FLASH BOR level.
  754. * @param None
  755. * @retval The FLASH BOR level:
  756. * - OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
  757. * - OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
  758. * - OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
  759. * - OB_BOR_OFF : Supply voltage ranges from 1.62 to 2.1 V
  760. */
  761. uint8_t FLASH_OB_GetBOR(void)
  762. {
  763. /* Return the FLASH BOR level */
  764. return (uint8_t)(*(__IO uint8_t *)(OPTCR_BYTE0_ADDRESS) & (uint8_t)0x0C);
  765. }
  766. /**
  767. * @}
  768. */
  769. /** @defgroup FLASH_Group4 Interrupts and flags management functions
  770. * @brief Interrupts and flags management functions
  771. *
  772. @verbatim
  773. ===============================================================================
  774. Interrupts and flags management functions
  775. ===============================================================================
  776. @endverbatim
  777. * @{
  778. */
  779. /**
  780. * @brief Enables or disables the specified FLASH interrupts.
  781. * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or disabled.
  782. * This parameter can be any combination of the following values:
  783. * @arg FLASH_IT_ERR: FLASH Error Interrupt
  784. * @arg FLASH_IT_EOP: FLASH end of operation Interrupt
  785. * @retval None
  786. */
  787. void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
  788. {
  789. /* Check the parameters */
  790. assert_param(IS_FLASH_IT(FLASH_IT));
  791. assert_param(IS_FUNCTIONAL_STATE(NewState));
  792. if(NewState != DISABLE)
  793. {
  794. /* Enable the interrupt sources */
  795. FLASH->CR |= FLASH_IT;
  796. }
  797. else
  798. {
  799. /* Disable the interrupt sources */
  800. FLASH->CR &= ~(uint32_t)FLASH_IT;
  801. }
  802. }
  803. /**
  804. * @brief Checks whether the specified FLASH flag is set or not.
  805. * @param FLASH_FLAG: specifies the FLASH flag to check.
  806. * This parameter can be one of the following values:
  807. * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
  808. * @arg FLASH_FLAG_OPERR: FLASH operation Error flag
  809. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  810. * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
  811. * @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
  812. * @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
  813. * @arg FLASH_FLAG_BSY: FLASH Busy flag
  814. * @retval The new state of FLASH_FLAG (SET or RESET).
  815. */
  816. FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
  817. {
  818. FlagStatus bitstatus = RESET;
  819. /* Check the parameters */
  820. assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
  821. if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
  822. {
  823. bitstatus = SET;
  824. }
  825. else
  826. {
  827. bitstatus = RESET;
  828. }
  829. /* Return the new state of FLASH_FLAG (SET or RESET) */
  830. return bitstatus;
  831. }
  832. /**
  833. * @brief Clears the FLASH's pending flags.
  834. * @param FLASH_FLAG: specifies the FLASH flags to clear.
  835. * This parameter can be any combination of the following values:
  836. * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
  837. * @arg FLASH_FLAG_OPERR: FLASH operation Error flag
  838. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  839. * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
  840. * @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
  841. * @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
  842. * @retval None
  843. */
  844. void FLASH_ClearFlag(uint32_t FLASH_FLAG)
  845. {
  846. /* Check the parameters */
  847. assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
  848. /* Clear the flags */
  849. FLASH->SR = FLASH_FLAG;
  850. }
  851. /**
  852. * @brief Returns the FLASH Status.
  853. * @param None
  854. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
  855. * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
  856. */
  857. FLASH_Status FLASH_GetStatus(void)
  858. {
  859. FLASH_Status flashstatus = FLASH_COMPLETE;
  860. if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
  861. {
  862. flashstatus = FLASH_BUSY;
  863. }
  864. else
  865. {
  866. if((FLASH->SR & FLASH_FLAG_WRPERR) != (uint32_t)0x00)
  867. {
  868. flashstatus = FLASH_ERROR_WRP;
  869. }
  870. else
  871. {
  872. if((FLASH->SR & (uint32_t)0xEF) != (uint32_t)0x00)
  873. {
  874. flashstatus = FLASH_ERROR_PROGRAM;
  875. }
  876. else
  877. {
  878. if((FLASH->SR & FLASH_FLAG_OPERR) != (uint32_t)0x00)
  879. {
  880. flashstatus = FLASH_ERROR_OPERATION;
  881. }
  882. else
  883. {
  884. flashstatus = FLASH_COMPLETE;
  885. }
  886. }
  887. }
  888. }
  889. /* Return the FLASH Status */
  890. return flashstatus;
  891. }
  892. /**
  893. * @brief Waits for a FLASH operation to complete.
  894. * @param None
  895. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
  896. * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
  897. */
  898. FLASH_Status FLASH_WaitForLastOperation(void)
  899. {
  900. __IO FLASH_Status status = FLASH_COMPLETE;
  901. /* Check for the FLASH Status */
  902. status = FLASH_GetStatus();
  903. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  904. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  905. flag will be set */
  906. while(status == FLASH_BUSY)
  907. {
  908. status = FLASH_GetStatus();
  909. }
  910. /* Return the operation status */
  911. return status;
  912. }
  913. /**
  914. * @}
  915. */
  916. /**
  917. * @}
  918. */
  919. /**
  920. * @}
  921. */
  922. /**
  923. * @}
  924. */