stm32f4xx_flash.c 36 KB

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