stm32f4xx_flash.c 37 KB

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