stm32f10x_flash.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_flash.c
  4. * @author MCD Application Team
  5. * @version V3.5.0
  6. * @date 11-March-2011
  7. * @brief This file provides all the FLASH firmware functions.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  19. ******************************************************************************
  20. */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f10x_flash.h"
  23. /** @addtogroup STM32F10x_StdPeriph_Driver
  24. * @{
  25. */
  26. /** @defgroup FLASH
  27. * @brief FLASH driver modules
  28. * @{
  29. */
  30. /** @defgroup FLASH_Private_TypesDefinitions
  31. * @{
  32. */
  33. /**
  34. * @}
  35. */
  36. /** @defgroup FLASH_Private_Defines
  37. * @{
  38. */
  39. /* Flash Access Control Register bits */
  40. #define ACR_LATENCY_Mask ((uint32_t)0x00000038)
  41. #define ACR_HLFCYA_Mask ((uint32_t)0xFFFFFFF7)
  42. #define ACR_PRFTBE_Mask ((uint32_t)0xFFFFFFEF)
  43. /* Flash Access Control Register bits */
  44. #define ACR_PRFTBS_Mask ((uint32_t)0x00000020)
  45. /* Flash Control Register bits */
  46. #define CR_PG_Set ((uint32_t)0x00000001)
  47. #define CR_PG_Reset ((uint32_t)0x00001FFE)
  48. #define CR_PER_Set ((uint32_t)0x00000002)
  49. #define CR_PER_Reset ((uint32_t)0x00001FFD)
  50. #define CR_MER_Set ((uint32_t)0x00000004)
  51. #define CR_MER_Reset ((uint32_t)0x00001FFB)
  52. #define CR_OPTPG_Set ((uint32_t)0x00000010)
  53. #define CR_OPTPG_Reset ((uint32_t)0x00001FEF)
  54. #define CR_OPTER_Set ((uint32_t)0x00000020)
  55. #define CR_OPTER_Reset ((uint32_t)0x00001FDF)
  56. #define CR_STRT_Set ((uint32_t)0x00000040)
  57. #define CR_LOCK_Set ((uint32_t)0x00000080)
  58. /* FLASH Mask */
  59. #define RDPRT_Mask ((uint32_t)0x00000002)
  60. #define WRP0_Mask ((uint32_t)0x000000FF)
  61. #define WRP1_Mask ((uint32_t)0x0000FF00)
  62. #define WRP2_Mask ((uint32_t)0x00FF0000)
  63. #define WRP3_Mask ((uint32_t)0xFF000000)
  64. #define OB_USER_BFB2 ((uint16_t)0x0008)
  65. /* FLASH Keys */
  66. #define RDP_Key ((uint16_t)0x00A5)
  67. #define FLASH_KEY1 ((uint32_t)0x45670123)
  68. #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
  69. /* FLASH BANK address */
  70. #define FLASH_BANK1_END_ADDRESS ((uint32_t)0x807FFFF)
  71. /* Delay definition */
  72. #define EraseTimeout ((uint32_t)0x000B0000)
  73. #define ProgramTimeout ((uint32_t)0x00002000)
  74. /**
  75. * @}
  76. */
  77. /** @defgroup FLASH_Private_Macros
  78. * @{
  79. */
  80. /**
  81. * @}
  82. */
  83. /** @defgroup FLASH_Private_Variables
  84. * @{
  85. */
  86. /**
  87. * @}
  88. */
  89. /** @defgroup FLASH_Private_FunctionPrototypes
  90. * @{
  91. */
  92. /**
  93. * @}
  94. */
  95. /** @defgroup FLASH_Private_Functions
  96. * @{
  97. */
  98. /**
  99. @code
  100. This driver provides functions to configure and program the Flash memory of all STM32F10x devices,
  101. including the latest STM32F10x_XL density devices.
  102. STM32F10x_XL devices feature up to 1 Mbyte with dual bank architecture for read-while-write (RWW) capability:
  103. - bank1: fixed size of 512 Kbytes (256 pages of 2Kbytes each)
  104. - bank2: up to 512 Kbytes (up to 256 pages of 2Kbytes each)
  105. While other STM32F10x devices features only one bank with memory up to 512 Kbytes.
  106. In version V3.3.0, some functions were updated and new ones were added to support
  107. STM32F10x_XL devices. Thus some functions manages all devices, while other are
  108. dedicated for XL devices only.
  109. The table below presents the list of available functions depending on the used STM32F10x devices.
  110. ***************************************************
  111. * Legacy functions used for all STM32F10x devices *
  112. ***************************************************
  113. +----------------------------------------------------------------------------------------------------------------------------------+
  114. | Functions prototypes |STM32F10x_XL|Other STM32F10x| Comments |
  115. | | devices | devices | |
  116. |----------------------------------------------------------------------------------------------------------------------------------|
  117. |FLASH_SetLatency | Yes | Yes | No change |
  118. |----------------------------------------------------------------------------------------------------------------------------------|
  119. |FLASH_HalfCycleAccessCmd | Yes | Yes | No change |
  120. |----------------------------------------------------------------------------------------------------------------------------------|
  121. |FLASH_PrefetchBufferCmd | Yes | Yes | No change |
  122. |----------------------------------------------------------------------------------------------------------------------------------|
  123. |FLASH_Unlock | Yes | Yes | - For STM32F10X_XL devices: unlock Bank1 and Bank2. |
  124. | | | | - For other devices: unlock Bank1 and it is equivalent |
  125. | | | | to FLASH_UnlockBank1 function. |
  126. |----------------------------------------------------------------------------------------------------------------------------------|
  127. |FLASH_Lock | Yes | Yes | - For STM32F10X_XL devices: lock Bank1 and Bank2. |
  128. | | | | - For other devices: lock Bank1 and it is equivalent |
  129. | | | | to FLASH_LockBank1 function. |
  130. |----------------------------------------------------------------------------------------------------------------------------------|
  131. |FLASH_ErasePage | Yes | Yes | - For STM32F10x_XL devices: erase a page in Bank1 and Bank2 |
  132. | | | | - For other devices: erase a page in Bank1 |
  133. |----------------------------------------------------------------------------------------------------------------------------------|
  134. |FLASH_EraseAllPages | Yes | Yes | - For STM32F10x_XL devices: erase all pages in Bank1 and Bank2 |
  135. | | | | - For other devices: erase all pages in Bank1 |
  136. |----------------------------------------------------------------------------------------------------------------------------------|
  137. |FLASH_EraseOptionBytes | Yes | Yes | No change |
  138. |----------------------------------------------------------------------------------------------------------------------------------|
  139. |FLASH_ProgramWord | Yes | Yes | Updated to program up to 1MByte (depending on the used device) |
  140. |----------------------------------------------------------------------------------------------------------------------------------|
  141. |FLASH_ProgramHalfWord | Yes | Yes | Updated to program up to 1MByte (depending on the used device) |
  142. |----------------------------------------------------------------------------------------------------------------------------------|
  143. |FLASH_ProgramOptionByteData | Yes | Yes | No change |
  144. |----------------------------------------------------------------------------------------------------------------------------------|
  145. |FLASH_EnableWriteProtection | Yes | Yes | No change |
  146. |----------------------------------------------------------------------------------------------------------------------------------|
  147. |FLASH_ReadOutProtection | Yes | Yes | No change |
  148. |----------------------------------------------------------------------------------------------------------------------------------|
  149. |FLASH_UserOptionByteConfig | Yes | Yes | No change |
  150. |----------------------------------------------------------------------------------------------------------------------------------|
  151. |FLASH_GetUserOptionByte | Yes | Yes | No change |
  152. |----------------------------------------------------------------------------------------------------------------------------------|
  153. |FLASH_GetWriteProtectionOptionByte | Yes | Yes | No change |
  154. |----------------------------------------------------------------------------------------------------------------------------------|
  155. |FLASH_GetReadOutProtectionStatus | Yes | Yes | No change |
  156. |----------------------------------------------------------------------------------------------------------------------------------|
  157. |FLASH_GetPrefetchBufferStatus | Yes | Yes | No change |
  158. |----------------------------------------------------------------------------------------------------------------------------------|
  159. |FLASH_ITConfig | Yes | Yes | - For STM32F10x_XL devices: enable Bank1 and Bank2's interrupts|
  160. | | | | - For other devices: enable Bank1's interrupts |
  161. |----------------------------------------------------------------------------------------------------------------------------------|
  162. |FLASH_GetFlagStatus | Yes | Yes | - For STM32F10x_XL devices: return Bank1 and Bank2's flag status|
  163. | | | | - For other devices: return Bank1's flag status |
  164. |----------------------------------------------------------------------------------------------------------------------------------|
  165. |FLASH_ClearFlag | Yes | Yes | - For STM32F10x_XL devices: clear Bank1 and Bank2's flag |
  166. | | | | - For other devices: clear Bank1's flag |
  167. |----------------------------------------------------------------------------------------------------------------------------------|
  168. |FLASH_GetStatus | Yes | Yes | - Return the status of Bank1 (for all devices) |
  169. | | | | equivalent to FLASH_GetBank1Status function |
  170. |----------------------------------------------------------------------------------------------------------------------------------|
  171. |FLASH_WaitForLastOperation | Yes | Yes | - Wait for Bank1 last operation (for all devices) |
  172. | | | | equivalent to: FLASH_WaitForLastBank1Operation function |
  173. +----------------------------------------------------------------------------------------------------------------------------------+
  174. ************************************************************************************************************************
  175. * New functions used for all STM32F10x devices to manage Bank1: *
  176. * - These functions are mainly useful for STM32F10x_XL density devices, to have separate control for Bank1 and bank2 *
  177. * - For other devices, these functions are optional (covered by functions listed above) *
  178. ************************************************************************************************************************
  179. +----------------------------------------------------------------------------------------------------------------------------------+
  180. | Functions prototypes |STM32F10x_XL|Other STM32F10x| Comments |
  181. | | devices | devices | |
  182. |----------------------------------------------------------------------------------------------------------------------------------|
  183. | FLASH_UnlockBank1 | Yes | Yes | - Unlock Bank1 |
  184. |----------------------------------------------------------------------------------------------------------------------------------|
  185. |FLASH_LockBank1 | Yes | Yes | - Lock Bank1 |
  186. |----------------------------------------------------------------------------------------------------------------------------------|
  187. | FLASH_EraseAllBank1Pages | Yes | Yes | - Erase all pages in Bank1 |
  188. |----------------------------------------------------------------------------------------------------------------------------------|
  189. | FLASH_GetBank1Status | Yes | Yes | - Return the status of Bank1 |
  190. |----------------------------------------------------------------------------------------------------------------------------------|
  191. | FLASH_WaitForLastBank1Operation | Yes | Yes | - Wait for Bank1 last operation |
  192. +----------------------------------------------------------------------------------------------------------------------------------+
  193. *****************************************************************************
  194. * New Functions used only with STM32F10x_XL density devices to manage Bank2 *
  195. *****************************************************************************
  196. +----------------------------------------------------------------------------------------------------------------------------------+
  197. | Functions prototypes |STM32F10x_XL|Other STM32F10x| Comments |
  198. | | devices | devices | |
  199. |----------------------------------------------------------------------------------------------------------------------------------|
  200. | FLASH_UnlockBank2 | Yes | No | - Unlock Bank2 |
  201. |----------------------------------------------------------------------------------------------------------------------------------|
  202. |FLASH_LockBank2 | Yes | No | - Lock Bank2 |
  203. |----------------------------------------------------------------------------------------------------------------------------------|
  204. | FLASH_EraseAllBank2Pages | Yes | No | - Erase all pages in Bank2 |
  205. |----------------------------------------------------------------------------------------------------------------------------------|
  206. | FLASH_GetBank2Status | Yes | No | - Return the status of Bank2 |
  207. |----------------------------------------------------------------------------------------------------------------------------------|
  208. | FLASH_WaitForLastBank2Operation | Yes | No | - Wait for Bank2 last operation |
  209. |----------------------------------------------------------------------------------------------------------------------------------|
  210. | FLASH_BootConfig | Yes | No | - Configure to boot from Bank1 or Bank2 |
  211. +----------------------------------------------------------------------------------------------------------------------------------+
  212. @endcode
  213. */
  214. /**
  215. * @brief Sets the code latency value.
  216. * @note This function can be used for all STM32F10x devices.
  217. * @param FLASH_Latency: specifies the FLASH Latency value.
  218. * This parameter can be one of the following values:
  219. * @arg FLASH_Latency_0: FLASH Zero Latency cycle
  220. * @arg FLASH_Latency_1: FLASH One Latency cycle
  221. * @arg FLASH_Latency_2: FLASH Two Latency cycles
  222. * @retval None
  223. */
  224. void FLASH_SetLatency(uint32_t FLASH_Latency)
  225. {
  226. uint32_t tmpreg = 0;
  227. /* Check the parameters */
  228. assert_param(IS_FLASH_LATENCY(FLASH_Latency));
  229. /* Read the ACR register */
  230. tmpreg = FLASH->ACR;
  231. /* Sets the Latency value */
  232. tmpreg &= ACR_LATENCY_Mask;
  233. tmpreg |= FLASH_Latency;
  234. /* Write the ACR register */
  235. FLASH->ACR = tmpreg;
  236. }
  237. /**
  238. * @brief Enables or disables the Half cycle flash access.
  239. * @note This function can be used for all STM32F10x devices.
  240. * @param FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.
  241. * This parameter can be one of the following values:
  242. * @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable
  243. * @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable
  244. * @retval None
  245. */
  246. void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess)
  247. {
  248. /* Check the parameters */
  249. assert_param(IS_FLASH_HALFCYCLEACCESS_STATE(FLASH_HalfCycleAccess));
  250. /* Enable or disable the Half cycle access */
  251. FLASH->ACR &= ACR_HLFCYA_Mask;
  252. FLASH->ACR |= FLASH_HalfCycleAccess;
  253. }
  254. /**
  255. * @brief Enables or disables the Prefetch Buffer.
  256. * @note This function can be used for all STM32F10x devices.
  257. * @param FLASH_PrefetchBuffer: specifies the Prefetch buffer status.
  258. * This parameter can be one of the following values:
  259. * @arg FLASH_PrefetchBuffer_Enable: FLASH Prefetch Buffer Enable
  260. * @arg FLASH_PrefetchBuffer_Disable: FLASH Prefetch Buffer Disable
  261. * @retval None
  262. */
  263. void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer)
  264. {
  265. /* Check the parameters */
  266. assert_param(IS_FLASH_PREFETCHBUFFER_STATE(FLASH_PrefetchBuffer));
  267. /* Enable or disable the Prefetch Buffer */
  268. FLASH->ACR &= ACR_PRFTBE_Mask;
  269. FLASH->ACR |= FLASH_PrefetchBuffer;
  270. }
  271. /**
  272. * @brief Unlocks the FLASH Program Erase Controller.
  273. * @note This function can be used for all STM32F10x devices.
  274. * - For STM32F10X_XL devices this function unlocks Bank1 and Bank2.
  275. * - For all other devices it unlocks Bank1 and it is equivalent
  276. * to FLASH_UnlockBank1 function..
  277. * @param None
  278. * @retval None
  279. */
  280. void FLASH_Unlock(void)
  281. {
  282. /* Authorize the FPEC of Bank1 Access */
  283. FLASH->KEYR = FLASH_KEY1;
  284. FLASH->KEYR = FLASH_KEY2;
  285. #ifdef STM32F10X_XL
  286. /* Authorize the FPEC of Bank2 Access */
  287. FLASH->KEYR2 = FLASH_KEY1;
  288. FLASH->KEYR2 = FLASH_KEY2;
  289. #endif /* STM32F10X_XL */
  290. }
  291. /**
  292. * @brief Unlocks the FLASH Bank1 Program Erase Controller.
  293. * @note This function can be used for all STM32F10x devices.
  294. * - For STM32F10X_XL devices this function unlocks Bank1.
  295. * - For all other devices it unlocks Bank1 and it is
  296. * equivalent to FLASH_Unlock function.
  297. * @param None
  298. * @retval None
  299. */
  300. void FLASH_UnlockBank1(void)
  301. {
  302. /* Authorize the FPEC of Bank1 Access */
  303. FLASH->KEYR = FLASH_KEY1;
  304. FLASH->KEYR = FLASH_KEY2;
  305. }
  306. #ifdef STM32F10X_XL
  307. /**
  308. * @brief Unlocks the FLASH Bank2 Program Erase Controller.
  309. * @note This function can be used only for STM32F10X_XL density devices.
  310. * @param None
  311. * @retval None
  312. */
  313. void FLASH_UnlockBank2(void)
  314. {
  315. /* Authorize the FPEC of Bank2 Access */
  316. FLASH->KEYR2 = FLASH_KEY1;
  317. FLASH->KEYR2 = FLASH_KEY2;
  318. }
  319. #endif /* STM32F10X_XL */
  320. /**
  321. * @brief Locks the FLASH Program Erase Controller.
  322. * @note This function can be used for all STM32F10x devices.
  323. * - For STM32F10X_XL devices this function Locks Bank1 and Bank2.
  324. * - For all other devices it Locks Bank1 and it is equivalent
  325. * to FLASH_LockBank1 function.
  326. * @param None
  327. * @retval None
  328. */
  329. void FLASH_Lock(void)
  330. {
  331. /* Set the Lock Bit to lock the FPEC and the CR of Bank1 */
  332. FLASH->CR |= CR_LOCK_Set;
  333. #ifdef STM32F10X_XL
  334. /* Set the Lock Bit to lock the FPEC and the CR of Bank2 */
  335. FLASH->CR2 |= CR_LOCK_Set;
  336. #endif /* STM32F10X_XL */
  337. }
  338. /**
  339. * @brief Locks the FLASH Bank1 Program Erase Controller.
  340. * @note this function can be used for all STM32F10x devices.
  341. * - For STM32F10X_XL devices this function Locks Bank1.
  342. * - For all other devices it Locks Bank1 and it is equivalent
  343. * to FLASH_Lock function.
  344. * @param None
  345. * @retval None
  346. */
  347. void FLASH_LockBank1(void)
  348. {
  349. /* Set the Lock Bit to lock the FPEC and the CR of Bank1 */
  350. FLASH->CR |= CR_LOCK_Set;
  351. }
  352. #ifdef STM32F10X_XL
  353. /**
  354. * @brief Locks the FLASH Bank2 Program Erase Controller.
  355. * @note This function can be used only for STM32F10X_XL density devices.
  356. * @param None
  357. * @retval None
  358. */
  359. void FLASH_LockBank2(void)
  360. {
  361. /* Set the Lock Bit to lock the FPEC and the CR of Bank2 */
  362. FLASH->CR2 |= CR_LOCK_Set;
  363. }
  364. #endif /* STM32F10X_XL */
  365. /**
  366. * @brief Erases a specified FLASH page.
  367. * @note This function can be used for all STM32F10x devices.
  368. * @param Page_Address: The page address to be erased.
  369. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  370. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  371. */
  372. FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
  373. {
  374. FLASH_Status status = FLASH_COMPLETE;
  375. /* Check the parameters */
  376. assert_param(IS_FLASH_ADDRESS(Page_Address));
  377. #ifdef STM32F10X_XL
  378. if(Page_Address < FLASH_BANK1_END_ADDRESS)
  379. {
  380. /* Wait for last operation to be completed */
  381. status = FLASH_WaitForLastBank1Operation(EraseTimeout);
  382. if(status == FLASH_COMPLETE)
  383. {
  384. /* if the previous operation is completed, proceed to erase the page */
  385. FLASH->CR|= CR_PER_Set;
  386. FLASH->AR = Page_Address;
  387. FLASH->CR|= CR_STRT_Set;
  388. /* Wait for last operation to be completed */
  389. status = FLASH_WaitForLastBank1Operation(EraseTimeout);
  390. /* Disable the PER Bit */
  391. FLASH->CR &= CR_PER_Reset;
  392. }
  393. }
  394. else
  395. {
  396. /* Wait for last operation to be completed */
  397. status = FLASH_WaitForLastBank2Operation(EraseTimeout);
  398. if(status == FLASH_COMPLETE)
  399. {
  400. /* if the previous operation is completed, proceed to erase the page */
  401. FLASH->CR2|= CR_PER_Set;
  402. FLASH->AR2 = Page_Address;
  403. FLASH->CR2|= CR_STRT_Set;
  404. /* Wait for last operation to be completed */
  405. status = FLASH_WaitForLastBank2Operation(EraseTimeout);
  406. /* Disable the PER Bit */
  407. FLASH->CR2 &= CR_PER_Reset;
  408. }
  409. }
  410. #else
  411. /* Wait for last operation to be completed */
  412. status = FLASH_WaitForLastOperation(EraseTimeout);
  413. if(status == FLASH_COMPLETE)
  414. {
  415. /* if the previous operation is completed, proceed to erase the page */
  416. FLASH->CR|= CR_PER_Set;
  417. FLASH->AR = Page_Address;
  418. FLASH->CR|= CR_STRT_Set;
  419. /* Wait for last operation to be completed */
  420. status = FLASH_WaitForLastOperation(EraseTimeout);
  421. /* Disable the PER Bit */
  422. FLASH->CR &= CR_PER_Reset;
  423. }
  424. #endif /* STM32F10X_XL */
  425. /* Return the Erase Status */
  426. return status;
  427. }
  428. /**
  429. * @brief Erases all FLASH pages.
  430. * @note This function can be used for all STM32F10x devices.
  431. * @param None
  432. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  433. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  434. */
  435. FLASH_Status FLASH_EraseAllPages(void)
  436. {
  437. FLASH_Status status = FLASH_COMPLETE;
  438. #ifdef STM32F10X_XL
  439. /* Wait for last operation to be completed */
  440. status = FLASH_WaitForLastBank1Operation(EraseTimeout);
  441. if(status == FLASH_COMPLETE)
  442. {
  443. /* if the previous operation is completed, proceed to erase all pages */
  444. FLASH->CR |= CR_MER_Set;
  445. FLASH->CR |= CR_STRT_Set;
  446. /* Wait for last operation to be completed */
  447. status = FLASH_WaitForLastBank1Operation(EraseTimeout);
  448. /* Disable the MER Bit */
  449. FLASH->CR &= CR_MER_Reset;
  450. }
  451. if(status == FLASH_COMPLETE)
  452. {
  453. /* if the previous operation is completed, proceed to erase all pages */
  454. FLASH->CR2 |= CR_MER_Set;
  455. FLASH->CR2 |= CR_STRT_Set;
  456. /* Wait for last operation to be completed */
  457. status = FLASH_WaitForLastBank2Operation(EraseTimeout);
  458. /* Disable the MER Bit */
  459. FLASH->CR2 &= CR_MER_Reset;
  460. }
  461. #else
  462. /* Wait for last operation to be completed */
  463. status = FLASH_WaitForLastOperation(EraseTimeout);
  464. if(status == FLASH_COMPLETE)
  465. {
  466. /* if the previous operation is completed, proceed to erase all pages */
  467. FLASH->CR |= CR_MER_Set;
  468. FLASH->CR |= CR_STRT_Set;
  469. /* Wait for last operation to be completed */
  470. status = FLASH_WaitForLastOperation(EraseTimeout);
  471. /* Disable the MER Bit */
  472. FLASH->CR &= CR_MER_Reset;
  473. }
  474. #endif /* STM32F10X_XL */
  475. /* Return the Erase Status */
  476. return status;
  477. }
  478. /**
  479. * @brief Erases all Bank1 FLASH pages.
  480. * @note This function can be used for all STM32F10x devices.
  481. * - For STM32F10X_XL devices this function erases all Bank1 pages.
  482. * - For all other devices it erases all Bank1 pages and it is equivalent
  483. * to FLASH_EraseAllPages function.
  484. * @param None
  485. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  486. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  487. */
  488. FLASH_Status FLASH_EraseAllBank1Pages(void)
  489. {
  490. FLASH_Status status = FLASH_COMPLETE;
  491. /* Wait for last operation to be completed */
  492. status = FLASH_WaitForLastBank1Operation(EraseTimeout);
  493. if(status == FLASH_COMPLETE)
  494. {
  495. /* if the previous operation is completed, proceed to erase all pages */
  496. FLASH->CR |= CR_MER_Set;
  497. FLASH->CR |= CR_STRT_Set;
  498. /* Wait for last operation to be completed */
  499. status = FLASH_WaitForLastBank1Operation(EraseTimeout);
  500. /* Disable the MER Bit */
  501. FLASH->CR &= CR_MER_Reset;
  502. }
  503. /* Return the Erase Status */
  504. return status;
  505. }
  506. #ifdef STM32F10X_XL
  507. /**
  508. * @brief Erases all Bank2 FLASH pages.
  509. * @note This function can be used only for STM32F10x_XL density devices.
  510. * @param None
  511. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  512. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  513. */
  514. FLASH_Status FLASH_EraseAllBank2Pages(void)
  515. {
  516. FLASH_Status status = FLASH_COMPLETE;
  517. /* Wait for last operation to be completed */
  518. status = FLASH_WaitForLastBank2Operation(EraseTimeout);
  519. if(status == FLASH_COMPLETE)
  520. {
  521. /* if the previous operation is completed, proceed to erase all pages */
  522. FLASH->CR2 |= CR_MER_Set;
  523. FLASH->CR2 |= CR_STRT_Set;
  524. /* Wait for last operation to be completed */
  525. status = FLASH_WaitForLastBank2Operation(EraseTimeout);
  526. /* Disable the MER Bit */
  527. FLASH->CR2 &= CR_MER_Reset;
  528. }
  529. /* Return the Erase Status */
  530. return status;
  531. }
  532. #endif /* STM32F10X_XL */
  533. /**
  534. * @brief Erases the FLASH option bytes.
  535. * @note This functions erases all option bytes except the Read protection (RDP).
  536. * @note This function can be used for all STM32F10x devices.
  537. * @param None
  538. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  539. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  540. */
  541. FLASH_Status FLASH_EraseOptionBytes(void)
  542. {
  543. uint16_t rdptmp = RDP_Key;
  544. FLASH_Status status = FLASH_COMPLETE;
  545. /* Get the actual read protection Option Byte value */
  546. if(FLASH_GetReadOutProtectionStatus() != RESET)
  547. {
  548. rdptmp = 0x00;
  549. }
  550. /* Wait for last operation to be completed */
  551. status = FLASH_WaitForLastOperation(EraseTimeout);
  552. if(status == FLASH_COMPLETE)
  553. {
  554. /* Authorize the small information block programming */
  555. FLASH->OPTKEYR = FLASH_KEY1;
  556. FLASH->OPTKEYR = FLASH_KEY2;
  557. /* if the previous operation is completed, proceed to erase the option bytes */
  558. FLASH->CR |= CR_OPTER_Set;
  559. FLASH->CR |= CR_STRT_Set;
  560. /* Wait for last operation to be completed */
  561. status = FLASH_WaitForLastOperation(EraseTimeout);
  562. if(status == FLASH_COMPLETE)
  563. {
  564. /* if the erase operation is completed, disable the OPTER Bit */
  565. FLASH->CR &= CR_OPTER_Reset;
  566. /* Enable the Option Bytes Programming operation */
  567. FLASH->CR |= CR_OPTPG_Set;
  568. /* Restore the last read protection Option Byte value */
  569. OB->RDP = (uint16_t)rdptmp;
  570. /* Wait for last operation to be completed */
  571. status = FLASH_WaitForLastOperation(ProgramTimeout);
  572. if(status != FLASH_TIMEOUT)
  573. {
  574. /* if the program operation is completed, disable the OPTPG Bit */
  575. FLASH->CR &= CR_OPTPG_Reset;
  576. }
  577. }
  578. else
  579. {
  580. if (status != FLASH_TIMEOUT)
  581. {
  582. /* Disable the OPTPG Bit */
  583. FLASH->CR &= CR_OPTPG_Reset;
  584. }
  585. }
  586. }
  587. /* Return the erase status */
  588. return status;
  589. }
  590. /**
  591. * @brief Programs a word at a specified address.
  592. * @note This function can be used for all STM32F10x devices.
  593. * @param Address: specifies the address to be programmed.
  594. * @param Data: specifies the data to be programmed.
  595. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  596. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  597. */
  598. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
  599. {
  600. FLASH_Status status = FLASH_COMPLETE;
  601. __IO uint32_t tmp = 0;
  602. /* Check the parameters */
  603. assert_param(IS_FLASH_ADDRESS(Address));
  604. #ifdef STM32F10X_XL
  605. if(Address < FLASH_BANK1_END_ADDRESS - 2)
  606. {
  607. /* Wait for last operation to be completed */
  608. status = FLASH_WaitForLastBank1Operation(ProgramTimeout);
  609. if(status == FLASH_COMPLETE)
  610. {
  611. /* if the previous operation is completed, proceed to program the new first
  612. half word */
  613. FLASH->CR |= CR_PG_Set;
  614. *(__IO uint16_t*)Address = (uint16_t)Data;
  615. /* Wait for last operation to be completed */
  616. status = FLASH_WaitForLastOperation(ProgramTimeout);
  617. if(status == FLASH_COMPLETE)
  618. {
  619. /* if the previous operation is completed, proceed to program the new second
  620. half word */
  621. tmp = Address + 2;
  622. *(__IO uint16_t*) tmp = Data >> 16;
  623. /* Wait for last operation to be completed */
  624. status = FLASH_WaitForLastOperation(ProgramTimeout);
  625. /* Disable the PG Bit */
  626. FLASH->CR &= CR_PG_Reset;
  627. }
  628. else
  629. {
  630. /* Disable the PG Bit */
  631. FLASH->CR &= CR_PG_Reset;
  632. }
  633. }
  634. }
  635. else if(Address == (FLASH_BANK1_END_ADDRESS - 1))
  636. {
  637. /* Wait for last operation to be completed */
  638. status = FLASH_WaitForLastBank1Operation(ProgramTimeout);
  639. if(status == FLASH_COMPLETE)
  640. {
  641. /* if the previous operation is completed, proceed to program the new first
  642. half word */
  643. FLASH->CR |= CR_PG_Set;
  644. *(__IO uint16_t*)Address = (uint16_t)Data;
  645. /* Wait for last operation to be completed */
  646. status = FLASH_WaitForLastBank1Operation(ProgramTimeout);
  647. /* Disable the PG Bit */
  648. FLASH->CR &= CR_PG_Reset;
  649. }
  650. else
  651. {
  652. /* Disable the PG Bit */
  653. FLASH->CR &= CR_PG_Reset;
  654. }
  655. /* Wait for last operation to be completed */
  656. status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
  657. if(status == FLASH_COMPLETE)
  658. {
  659. /* if the previous operation is completed, proceed to program the new second
  660. half word */
  661. FLASH->CR2 |= CR_PG_Set;
  662. tmp = Address + 2;
  663. *(__IO uint16_t*) tmp = Data >> 16;
  664. /* Wait for last operation to be completed */
  665. status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
  666. /* Disable the PG Bit */
  667. FLASH->CR2 &= CR_PG_Reset;
  668. }
  669. else
  670. {
  671. /* Disable the PG Bit */
  672. FLASH->CR2 &= CR_PG_Reset;
  673. }
  674. }
  675. else
  676. {
  677. /* Wait for last operation to be completed */
  678. status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
  679. if(status == FLASH_COMPLETE)
  680. {
  681. /* if the previous operation is completed, proceed to program the new first
  682. half word */
  683. FLASH->CR2 |= CR_PG_Set;
  684. *(__IO uint16_t*)Address = (uint16_t)Data;
  685. /* Wait for last operation to be completed */
  686. status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
  687. if(status == FLASH_COMPLETE)
  688. {
  689. /* if the previous operation is completed, proceed to program the new second
  690. half word */
  691. tmp = Address + 2;
  692. *(__IO uint16_t*) tmp = Data >> 16;
  693. /* Wait for last operation to be completed */
  694. status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
  695. /* Disable the PG Bit */
  696. FLASH->CR2 &= CR_PG_Reset;
  697. }
  698. else
  699. {
  700. /* Disable the PG Bit */
  701. FLASH->CR2 &= CR_PG_Reset;
  702. }
  703. }
  704. }
  705. #else
  706. /* Wait for last operation to be completed */
  707. status = FLASH_WaitForLastOperation(ProgramTimeout);
  708. if(status == FLASH_COMPLETE)
  709. {
  710. /* if the previous operation is completed, proceed to program the new first
  711. half word */
  712. FLASH->CR |= CR_PG_Set;
  713. *(__IO uint16_t*)Address = (uint16_t)Data;
  714. /* Wait for last operation to be completed */
  715. status = FLASH_WaitForLastOperation(ProgramTimeout);
  716. if(status == FLASH_COMPLETE)
  717. {
  718. /* if the previous operation is completed, proceed to program the new second
  719. half word */
  720. tmp = Address + 2;
  721. *(__IO uint16_t*) tmp = Data >> 16;
  722. /* Wait for last operation to be completed */
  723. status = FLASH_WaitForLastOperation(ProgramTimeout);
  724. /* Disable the PG Bit */
  725. FLASH->CR &= CR_PG_Reset;
  726. }
  727. else
  728. {
  729. /* Disable the PG Bit */
  730. FLASH->CR &= CR_PG_Reset;
  731. }
  732. }
  733. #endif /* STM32F10X_XL */
  734. /* Return the Program Status */
  735. return status;
  736. }
  737. /**
  738. * @brief Programs a half word at a specified address.
  739. * @note This function can be used for all STM32F10x devices.
  740. * @param Address: specifies the address to be programmed.
  741. * @param Data: specifies the data to be programmed.
  742. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  743. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  744. */
  745. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
  746. {
  747. FLASH_Status status = FLASH_COMPLETE;
  748. /* Check the parameters */
  749. assert_param(IS_FLASH_ADDRESS(Address));
  750. #ifdef STM32F10X_XL
  751. /* Wait for last operation to be completed */
  752. status = FLASH_WaitForLastOperation(ProgramTimeout);
  753. if(Address < FLASH_BANK1_END_ADDRESS)
  754. {
  755. if(status == FLASH_COMPLETE)
  756. {
  757. /* if the previous operation is completed, proceed to program the new data */
  758. FLASH->CR |= CR_PG_Set;
  759. *(__IO uint16_t*)Address = Data;
  760. /* Wait for last operation to be completed */
  761. status = FLASH_WaitForLastBank1Operation(ProgramTimeout);
  762. /* Disable the PG Bit */
  763. FLASH->CR &= CR_PG_Reset;
  764. }
  765. }
  766. else
  767. {
  768. if(status == FLASH_COMPLETE)
  769. {
  770. /* if the previous operation is completed, proceed to program the new data */
  771. FLASH->CR2 |= CR_PG_Set;
  772. *(__IO uint16_t*)Address = Data;
  773. /* Wait for last operation to be completed */
  774. status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
  775. /* Disable the PG Bit */
  776. FLASH->CR2 &= CR_PG_Reset;
  777. }
  778. }
  779. #else
  780. /* Wait for last operation to be completed */
  781. status = FLASH_WaitForLastOperation(ProgramTimeout);
  782. if(status == FLASH_COMPLETE)
  783. {
  784. /* if the previous operation is completed, proceed to program the new data */
  785. FLASH->CR |= CR_PG_Set;
  786. *(__IO uint16_t*)Address = Data;
  787. /* Wait for last operation to be completed */
  788. status = FLASH_WaitForLastOperation(ProgramTimeout);
  789. /* Disable the PG Bit */
  790. FLASH->CR &= CR_PG_Reset;
  791. }
  792. #endif /* STM32F10X_XL */
  793. /* Return the Program Status */
  794. return status;
  795. }
  796. /**
  797. * @brief Programs a half word at a specified Option Byte Data address.
  798. * @note This function can be used for all STM32F10x devices.
  799. * @param Address: specifies the address to be programmed.
  800. * This parameter can be 0x1FFFF804 or 0x1FFFF806.
  801. * @param Data: specifies the data to be programmed.
  802. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  803. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  804. */
  805. FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)
  806. {
  807. FLASH_Status status = FLASH_COMPLETE;
  808. /* Check the parameters */
  809. assert_param(IS_OB_DATA_ADDRESS(Address));
  810. status = FLASH_WaitForLastOperation(ProgramTimeout);
  811. if(status == FLASH_COMPLETE)
  812. {
  813. /* Authorize the small information block programming */
  814. FLASH->OPTKEYR = FLASH_KEY1;
  815. FLASH->OPTKEYR = FLASH_KEY2;
  816. /* Enables the Option Bytes Programming operation */
  817. FLASH->CR |= CR_OPTPG_Set;
  818. *(__IO uint16_t*)Address = Data;
  819. /* Wait for last operation to be completed */
  820. status = FLASH_WaitForLastOperation(ProgramTimeout);
  821. if(status != FLASH_TIMEOUT)
  822. {
  823. /* if the program operation is completed, disable the OPTPG Bit */
  824. FLASH->CR &= CR_OPTPG_Reset;
  825. }
  826. }
  827. /* Return the Option Byte Data Program Status */
  828. return status;
  829. }
  830. /**
  831. * @brief Write protects the desired pages
  832. * @note This function can be used for all STM32F10x devices.
  833. * @param FLASH_Pages: specifies the address of the pages to be write protected.
  834. * This parameter can be:
  835. * @arg For @b STM32_Low-density_devices: value between FLASH_WRProt_Pages0to3 and FLASH_WRProt_Pages28to31
  836. * @arg For @b STM32_Medium-density_devices: value between FLASH_WRProt_Pages0to3
  837. * and FLASH_WRProt_Pages124to127
  838. * @arg For @b STM32_High-density_devices: value between FLASH_WRProt_Pages0to1 and
  839. * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to255
  840. * @arg For @b STM32_Connectivity_line_devices: value between FLASH_WRProt_Pages0to1 and
  841. * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to127
  842. * @arg For @b STM32_XL-density_devices: value between FLASH_WRProt_Pages0to1 and
  843. * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to511
  844. * @arg FLASH_WRProt_AllPages
  845. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  846. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  847. */
  848. FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages)
  849. {
  850. uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
  851. FLASH_Status status = FLASH_COMPLETE;
  852. /* Check the parameters */
  853. assert_param(IS_FLASH_WRPROT_PAGE(FLASH_Pages));
  854. FLASH_Pages = (uint32_t)(~FLASH_Pages);
  855. WRP0_Data = (uint16_t)(FLASH_Pages & WRP0_Mask);
  856. WRP1_Data = (uint16_t)((FLASH_Pages & WRP1_Mask) >> 8);
  857. WRP2_Data = (uint16_t)((FLASH_Pages & WRP2_Mask) >> 16);
  858. WRP3_Data = (uint16_t)((FLASH_Pages & WRP3_Mask) >> 24);
  859. /* Wait for last operation to be completed */
  860. status = FLASH_WaitForLastOperation(ProgramTimeout);
  861. if(status == FLASH_COMPLETE)
  862. {
  863. /* Authorizes the small information block programming */
  864. FLASH->OPTKEYR = FLASH_KEY1;
  865. FLASH->OPTKEYR = FLASH_KEY2;
  866. FLASH->CR |= CR_OPTPG_Set;
  867. if(WRP0_Data != 0xFF)
  868. {
  869. OB->WRP0 = WRP0_Data;
  870. /* Wait for last operation to be completed */
  871. status = FLASH_WaitForLastOperation(ProgramTimeout);
  872. }
  873. if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
  874. {
  875. OB->WRP1 = WRP1_Data;
  876. /* Wait for last operation to be completed */
  877. status = FLASH_WaitForLastOperation(ProgramTimeout);
  878. }
  879. if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
  880. {
  881. OB->WRP2 = WRP2_Data;
  882. /* Wait for last operation to be completed */
  883. status = FLASH_WaitForLastOperation(ProgramTimeout);
  884. }
  885. if((status == FLASH_COMPLETE)&& (WRP3_Data != 0xFF))
  886. {
  887. OB->WRP3 = WRP3_Data;
  888. /* Wait for last operation to be completed */
  889. status = FLASH_WaitForLastOperation(ProgramTimeout);
  890. }
  891. if(status != FLASH_TIMEOUT)
  892. {
  893. /* if the program operation is completed, disable the OPTPG Bit */
  894. FLASH->CR &= CR_OPTPG_Reset;
  895. }
  896. }
  897. /* Return the write protection operation Status */
  898. return status;
  899. }
  900. /**
  901. * @brief Enables or disables the read out protection.
  902. * @note If the user has already programmed the other option bytes before calling
  903. * this function, he must re-program them since this function erases all option bytes.
  904. * @note This function can be used for all STM32F10x devices.
  905. * @param Newstate: new state of the ReadOut Protection.
  906. * This parameter can be: ENABLE or DISABLE.
  907. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  908. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  909. */
  910. FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState)
  911. {
  912. FLASH_Status status = FLASH_COMPLETE;
  913. /* Check the parameters */
  914. assert_param(IS_FUNCTIONAL_STATE(NewState));
  915. status = FLASH_WaitForLastOperation(EraseTimeout);
  916. if(status == FLASH_COMPLETE)
  917. {
  918. /* Authorizes the small information block programming */
  919. FLASH->OPTKEYR = FLASH_KEY1;
  920. FLASH->OPTKEYR = FLASH_KEY2;
  921. FLASH->CR |= CR_OPTER_Set;
  922. FLASH->CR |= CR_STRT_Set;
  923. /* Wait for last operation to be completed */
  924. status = FLASH_WaitForLastOperation(EraseTimeout);
  925. if(status == FLASH_COMPLETE)
  926. {
  927. /* if the erase operation is completed, disable the OPTER Bit */
  928. FLASH->CR &= CR_OPTER_Reset;
  929. /* Enable the Option Bytes Programming operation */
  930. FLASH->CR |= CR_OPTPG_Set;
  931. if(NewState != DISABLE)
  932. {
  933. OB->RDP = 0x00;
  934. }
  935. else
  936. {
  937. OB->RDP = RDP_Key;
  938. }
  939. /* Wait for last operation to be completed */
  940. status = FLASH_WaitForLastOperation(EraseTimeout);
  941. if(status != FLASH_TIMEOUT)
  942. {
  943. /* if the program operation is completed, disable the OPTPG Bit */
  944. FLASH->CR &= CR_OPTPG_Reset;
  945. }
  946. }
  947. else
  948. {
  949. if(status != FLASH_TIMEOUT)
  950. {
  951. /* Disable the OPTER Bit */
  952. FLASH->CR &= CR_OPTER_Reset;
  953. }
  954. }
  955. }
  956. /* Return the protection operation Status */
  957. return status;
  958. }
  959. /**
  960. * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  961. * @note This function can be used for all STM32F10x devices.
  962. * @param OB_IWDG: Selects the IWDG mode
  963. * This parameter can be one of the following values:
  964. * @arg OB_IWDG_SW: Software IWDG selected
  965. * @arg OB_IWDG_HW: Hardware IWDG selected
  966. * @param OB_STOP: Reset event when entering STOP mode.
  967. * This parameter can be one of the following values:
  968. * @arg OB_STOP_NoRST: No reset generated when entering in STOP
  969. * @arg OB_STOP_RST: Reset generated when entering in STOP
  970. * @param OB_STDBY: Reset event when entering Standby mode.
  971. * This parameter can be one of the following values:
  972. * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
  973. * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
  974. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  975. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  976. */
  977. FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY)
  978. {
  979. FLASH_Status status = FLASH_COMPLETE;
  980. /* Check the parameters */
  981. assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
  982. assert_param(IS_OB_STOP_SOURCE(OB_STOP));
  983. assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
  984. /* Authorize the small information block programming */
  985. FLASH->OPTKEYR = FLASH_KEY1;
  986. FLASH->OPTKEYR = FLASH_KEY2;
  987. /* Wait for last operation to be completed */
  988. status = FLASH_WaitForLastOperation(ProgramTimeout);
  989. if(status == FLASH_COMPLETE)
  990. {
  991. /* Enable the Option Bytes Programming operation */
  992. FLASH->CR |= CR_OPTPG_Set;
  993. OB->USER = OB_IWDG | (uint16_t)(OB_STOP | (uint16_t)(OB_STDBY | ((uint16_t)0xF8)));
  994. /* Wait for last operation to be completed */
  995. status = FLASH_WaitForLastOperation(ProgramTimeout);
  996. if(status != FLASH_TIMEOUT)
  997. {
  998. /* if the program operation is completed, disable the OPTPG Bit */
  999. FLASH->CR &= CR_OPTPG_Reset;
  1000. }
  1001. }
  1002. /* Return the Option Byte program Status */
  1003. return status;
  1004. }
  1005. #ifdef STM32F10X_XL
  1006. /**
  1007. * @brief Configures to boot from Bank1 or Bank2.
  1008. * @note This function can be used only for STM32F10x_XL density devices.
  1009. * @param FLASH_BOOT: select the FLASH Bank to boot from.
  1010. * This parameter can be one of the following values:
  1011. * @arg FLASH_BOOT_Bank1: At startup, if boot pins are set in boot from user Flash
  1012. * position and this parameter is selected the device will boot from Bank1(Default).
  1013. * @arg FLASH_BOOT_Bank2: At startup, if boot pins are set in boot from user Flash
  1014. * position and this parameter is selected the device will boot from Bank2 or Bank1,
  1015. * depending on the activation of the bank. The active banks are checked in
  1016. * the following order: Bank2, followed by Bank1.
  1017. * The active bank is recognized by the value programmed at the base address
  1018. * of the respective bank (corresponding to the initial stack pointer value
  1019. * in the interrupt vector table).
  1020. * For more information, please refer to AN2606 from www.st.com.
  1021. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  1022. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  1023. */
  1024. FLASH_Status FLASH_BootConfig(uint16_t FLASH_BOOT)
  1025. {
  1026. FLASH_Status status = FLASH_COMPLETE;
  1027. assert_param(IS_FLASH_BOOT(FLASH_BOOT));
  1028. /* Authorize the small information block programming */
  1029. FLASH->OPTKEYR = FLASH_KEY1;
  1030. FLASH->OPTKEYR = FLASH_KEY2;
  1031. /* Wait for last operation to be completed */
  1032. status = FLASH_WaitForLastOperation(ProgramTimeout);
  1033. if(status == FLASH_COMPLETE)
  1034. {
  1035. /* Enable the Option Bytes Programming operation */
  1036. FLASH->CR |= CR_OPTPG_Set;
  1037. if(FLASH_BOOT == FLASH_BOOT_Bank1)
  1038. {
  1039. OB->USER |= OB_USER_BFB2;
  1040. }
  1041. else
  1042. {
  1043. OB->USER &= (uint16_t)(~(uint16_t)(OB_USER_BFB2));
  1044. }
  1045. /* Wait for last operation to be completed */
  1046. status = FLASH_WaitForLastOperation(ProgramTimeout);
  1047. if(status != FLASH_TIMEOUT)
  1048. {
  1049. /* if the program operation is completed, disable the OPTPG Bit */
  1050. FLASH->CR &= CR_OPTPG_Reset;
  1051. }
  1052. }
  1053. /* Return the Option Byte program Status */
  1054. return status;
  1055. }
  1056. #endif /* STM32F10X_XL */
  1057. /**
  1058. * @brief Returns the FLASH User Option Bytes values.
  1059. * @note This function can be used for all STM32F10x devices.
  1060. * @param None
  1061. * @retval The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)
  1062. * and RST_STDBY(Bit2).
  1063. */
  1064. uint32_t FLASH_GetUserOptionByte(void)
  1065. {
  1066. /* Return the User Option Byte */
  1067. return (uint32_t)(FLASH->OBR >> 2);
  1068. }
  1069. /**
  1070. * @brief Returns the FLASH Write Protection Option Bytes Register value.
  1071. * @note This function can be used for all STM32F10x devices.
  1072. * @param None
  1073. * @retval The FLASH Write Protection Option Bytes Register value
  1074. */
  1075. uint32_t FLASH_GetWriteProtectionOptionByte(void)
  1076. {
  1077. /* Return the Flash write protection Register value */
  1078. return (uint32_t)(FLASH->WRPR);
  1079. }
  1080. /**
  1081. * @brief Checks whether the FLASH Read Out Protection Status is set or not.
  1082. * @note This function can be used for all STM32F10x devices.
  1083. * @param None
  1084. * @retval FLASH ReadOut Protection Status(SET or RESET)
  1085. */
  1086. FlagStatus FLASH_GetReadOutProtectionStatus(void)
  1087. {
  1088. FlagStatus readoutstatus = RESET;
  1089. if ((FLASH->OBR & RDPRT_Mask) != (uint32_t)RESET)
  1090. {
  1091. readoutstatus = SET;
  1092. }
  1093. else
  1094. {
  1095. readoutstatus = RESET;
  1096. }
  1097. return readoutstatus;
  1098. }
  1099. /**
  1100. * @brief Checks whether the FLASH Prefetch Buffer status is set or not.
  1101. * @note This function can be used for all STM32F10x devices.
  1102. * @param None
  1103. * @retval FLASH Prefetch Buffer Status (SET or RESET).
  1104. */
  1105. FlagStatus FLASH_GetPrefetchBufferStatus(void)
  1106. {
  1107. FlagStatus bitstatus = RESET;
  1108. if ((FLASH->ACR & ACR_PRFTBS_Mask) != (uint32_t)RESET)
  1109. {
  1110. bitstatus = SET;
  1111. }
  1112. else
  1113. {
  1114. bitstatus = RESET;
  1115. }
  1116. /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
  1117. return bitstatus;
  1118. }
  1119. /**
  1120. * @brief Enables or disables the specified FLASH interrupts.
  1121. * @note This function can be used for all STM32F10x devices.
  1122. * - For STM32F10X_XL devices, enables or disables the specified FLASH interrupts
  1123. for Bank1 and Bank2.
  1124. * - For other devices it enables or disables the specified FLASH interrupts for Bank1.
  1125. * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or disabled.
  1126. * This parameter can be any combination of the following values:
  1127. * @arg FLASH_IT_ERROR: FLASH Error Interrupt
  1128. * @arg FLASH_IT_EOP: FLASH end of operation Interrupt
  1129. * @param NewState: new state of the specified Flash interrupts.
  1130. * This parameter can be: ENABLE or DISABLE.
  1131. * @retval None
  1132. */
  1133. void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
  1134. {
  1135. #ifdef STM32F10X_XL
  1136. /* Check the parameters */
  1137. assert_param(IS_FLASH_IT(FLASH_IT));
  1138. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1139. if((FLASH_IT & 0x80000000) != 0x0)
  1140. {
  1141. if(NewState != DISABLE)
  1142. {
  1143. /* Enable the interrupt sources */
  1144. FLASH->CR2 |= (FLASH_IT & 0x7FFFFFFF);
  1145. }
  1146. else
  1147. {
  1148. /* Disable the interrupt sources */
  1149. FLASH->CR2 &= ~(uint32_t)(FLASH_IT & 0x7FFFFFFF);
  1150. }
  1151. }
  1152. else
  1153. {
  1154. if(NewState != DISABLE)
  1155. {
  1156. /* Enable the interrupt sources */
  1157. FLASH->CR |= FLASH_IT;
  1158. }
  1159. else
  1160. {
  1161. /* Disable the interrupt sources */
  1162. FLASH->CR &= ~(uint32_t)FLASH_IT;
  1163. }
  1164. }
  1165. #else
  1166. /* Check the parameters */
  1167. assert_param(IS_FLASH_IT(FLASH_IT));
  1168. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1169. if(NewState != DISABLE)
  1170. {
  1171. /* Enable the interrupt sources */
  1172. FLASH->CR |= FLASH_IT;
  1173. }
  1174. else
  1175. {
  1176. /* Disable the interrupt sources */
  1177. FLASH->CR &= ~(uint32_t)FLASH_IT;
  1178. }
  1179. #endif /* STM32F10X_XL */
  1180. }
  1181. /**
  1182. * @brief Checks whether the specified FLASH flag is set or not.
  1183. * @note This function can be used for all STM32F10x devices.
  1184. * - For STM32F10X_XL devices, this function checks whether the specified
  1185. * Bank1 or Bank2 flag is set or not.
  1186. * - For other devices, it checks whether the specified Bank1 flag is
  1187. * set or not.
  1188. * @param FLASH_FLAG: specifies the FLASH flag to check.
  1189. * This parameter can be one of the following values:
  1190. * @arg FLASH_FLAG_BSY: FLASH Busy flag
  1191. * @arg FLASH_FLAG_PGERR: FLASH Program error flag
  1192. * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
  1193. * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
  1194. * @arg FLASH_FLAG_OPTERR: FLASH Option Byte error flag
  1195. * @retval The new state of FLASH_FLAG (SET or RESET).
  1196. */
  1197. FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
  1198. {
  1199. FlagStatus bitstatus = RESET;
  1200. #ifdef STM32F10X_XL
  1201. /* Check the parameters */
  1202. assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;
  1203. if(FLASH_FLAG == FLASH_FLAG_OPTERR)
  1204. {
  1205. if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)
  1206. {
  1207. bitstatus = SET;
  1208. }
  1209. else
  1210. {
  1211. bitstatus = RESET;
  1212. }
  1213. }
  1214. else
  1215. {
  1216. if((FLASH_FLAG & 0x80000000) != 0x0)
  1217. {
  1218. if((FLASH->SR2 & FLASH_FLAG) != (uint32_t)RESET)
  1219. {
  1220. bitstatus = SET;
  1221. }
  1222. else
  1223. {
  1224. bitstatus = RESET;
  1225. }
  1226. }
  1227. else
  1228. {
  1229. if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
  1230. {
  1231. bitstatus = SET;
  1232. }
  1233. else
  1234. {
  1235. bitstatus = RESET;
  1236. }
  1237. }
  1238. }
  1239. #else
  1240. /* Check the parameters */
  1241. assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;
  1242. if(FLASH_FLAG == FLASH_FLAG_OPTERR)
  1243. {
  1244. if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)
  1245. {
  1246. bitstatus = SET;
  1247. }
  1248. else
  1249. {
  1250. bitstatus = RESET;
  1251. }
  1252. }
  1253. else
  1254. {
  1255. if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
  1256. {
  1257. bitstatus = SET;
  1258. }
  1259. else
  1260. {
  1261. bitstatus = RESET;
  1262. }
  1263. }
  1264. #endif /* STM32F10X_XL */
  1265. /* Return the new state of FLASH_FLAG (SET or RESET) */
  1266. return bitstatus;
  1267. }
  1268. /**
  1269. * @brief Clears the FLASH's pending flags.
  1270. * @note This function can be used for all STM32F10x devices.
  1271. * - For STM32F10X_XL devices, this function clears Bank1 or Bank2’s pending flags
  1272. * - For other devices, it clears Bank1’s pending flags.
  1273. * @param FLASH_FLAG: specifies the FLASH flags to clear.
  1274. * This parameter can be any combination of the following values:
  1275. * @arg FLASH_FLAG_PGERR: FLASH Program error flag
  1276. * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
  1277. * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
  1278. * @retval None
  1279. */
  1280. void FLASH_ClearFlag(uint32_t FLASH_FLAG)
  1281. {
  1282. #ifdef STM32F10X_XL
  1283. /* Check the parameters */
  1284. assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;
  1285. if((FLASH_FLAG & 0x80000000) != 0x0)
  1286. {
  1287. /* Clear the flags */
  1288. FLASH->SR2 = FLASH_FLAG;
  1289. }
  1290. else
  1291. {
  1292. /* Clear the flags */
  1293. FLASH->SR = FLASH_FLAG;
  1294. }
  1295. #else
  1296. /* Check the parameters */
  1297. assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;
  1298. /* Clear the flags */
  1299. FLASH->SR = FLASH_FLAG;
  1300. #endif /* STM32F10X_XL */
  1301. }
  1302. /**
  1303. * @brief Returns the FLASH Status.
  1304. * @note This function can be used for all STM32F10x devices, it is equivalent
  1305. * to FLASH_GetBank1Status function.
  1306. * @param None
  1307. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  1308. * FLASH_ERROR_WRP or FLASH_COMPLETE
  1309. */
  1310. FLASH_Status FLASH_GetStatus(void)
  1311. {
  1312. FLASH_Status flashstatus = FLASH_COMPLETE;
  1313. if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
  1314. {
  1315. flashstatus = FLASH_BUSY;
  1316. }
  1317. else
  1318. {
  1319. if((FLASH->SR & FLASH_FLAG_PGERR) != 0)
  1320. {
  1321. flashstatus = FLASH_ERROR_PG;
  1322. }
  1323. else
  1324. {
  1325. if((FLASH->SR & FLASH_FLAG_WRPRTERR) != 0 )
  1326. {
  1327. flashstatus = FLASH_ERROR_WRP;
  1328. }
  1329. else
  1330. {
  1331. flashstatus = FLASH_COMPLETE;
  1332. }
  1333. }
  1334. }
  1335. /* Return the Flash Status */
  1336. return flashstatus;
  1337. }
  1338. /**
  1339. * @brief Returns the FLASH Bank1 Status.
  1340. * @note This function can be used for all STM32F10x devices, it is equivalent
  1341. * to FLASH_GetStatus function.
  1342. * @param None
  1343. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  1344. * FLASH_ERROR_WRP or FLASH_COMPLETE
  1345. */
  1346. FLASH_Status FLASH_GetBank1Status(void)
  1347. {
  1348. FLASH_Status flashstatus = FLASH_COMPLETE;
  1349. if((FLASH->SR & FLASH_FLAG_BANK1_BSY) == FLASH_FLAG_BSY)
  1350. {
  1351. flashstatus = FLASH_BUSY;
  1352. }
  1353. else
  1354. {
  1355. if((FLASH->SR & FLASH_FLAG_BANK1_PGERR) != 0)
  1356. {
  1357. flashstatus = FLASH_ERROR_PG;
  1358. }
  1359. else
  1360. {
  1361. if((FLASH->SR & FLASH_FLAG_BANK1_WRPRTERR) != 0 )
  1362. {
  1363. flashstatus = FLASH_ERROR_WRP;
  1364. }
  1365. else
  1366. {
  1367. flashstatus = FLASH_COMPLETE;
  1368. }
  1369. }
  1370. }
  1371. /* Return the Flash Status */
  1372. return flashstatus;
  1373. }
  1374. #ifdef STM32F10X_XL
  1375. /**
  1376. * @brief Returns the FLASH Bank2 Status.
  1377. * @note This function can be used for STM32F10x_XL density devices.
  1378. * @param None
  1379. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  1380. * FLASH_ERROR_WRP or FLASH_COMPLETE
  1381. */
  1382. FLASH_Status FLASH_GetBank2Status(void)
  1383. {
  1384. FLASH_Status flashstatus = FLASH_COMPLETE;
  1385. if((FLASH->SR2 & (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) == (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF))
  1386. {
  1387. flashstatus = FLASH_BUSY;
  1388. }
  1389. else
  1390. {
  1391. if((FLASH->SR2 & (FLASH_FLAG_BANK2_PGERR & 0x7FFFFFFF)) != 0)
  1392. {
  1393. flashstatus = FLASH_ERROR_PG;
  1394. }
  1395. else
  1396. {
  1397. if((FLASH->SR2 & (FLASH_FLAG_BANK2_WRPRTERR & 0x7FFFFFFF)) != 0 )
  1398. {
  1399. flashstatus = FLASH_ERROR_WRP;
  1400. }
  1401. else
  1402. {
  1403. flashstatus = FLASH_COMPLETE;
  1404. }
  1405. }
  1406. }
  1407. /* Return the Flash Status */
  1408. return flashstatus;
  1409. }
  1410. #endif /* STM32F10X_XL */
  1411. /**
  1412. * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
  1413. * @note This function can be used for all STM32F10x devices,
  1414. * it is equivalent to FLASH_WaitForLastBank1Operation.
  1415. * - For STM32F10X_XL devices this function waits for a Bank1 Flash operation
  1416. * to complete or a TIMEOUT to occur.
  1417. * - For all other devices it waits for a Flash operation to complete
  1418. * or a TIMEOUT to occur.
  1419. * @param Timeout: FLASH programming Timeout
  1420. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  1421. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  1422. */
  1423. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
  1424. {
  1425. FLASH_Status status = FLASH_COMPLETE;
  1426. /* Check for the Flash Status */
  1427. status = FLASH_GetBank1Status();
  1428. /* Wait for a Flash operation to complete or a TIMEOUT to occur */
  1429. while((status == FLASH_BUSY) && (Timeout != 0x00))
  1430. {
  1431. status = FLASH_GetBank1Status();
  1432. Timeout--;
  1433. }
  1434. if(Timeout == 0x00 )
  1435. {
  1436. status = FLASH_TIMEOUT;
  1437. }
  1438. /* Return the operation status */
  1439. return status;
  1440. }
  1441. /**
  1442. * @brief Waits for a Flash operation on Bank1 to complete or a TIMEOUT to occur.
  1443. * @note This function can be used for all STM32F10x devices,
  1444. * it is equivalent to FLASH_WaitForLastOperation.
  1445. * @param Timeout: FLASH programming Timeout
  1446. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  1447. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  1448. */
  1449. FLASH_Status FLASH_WaitForLastBank1Operation(uint32_t Timeout)
  1450. {
  1451. FLASH_Status status = FLASH_COMPLETE;
  1452. /* Check for the Flash Status */
  1453. status = FLASH_GetBank1Status();
  1454. /* Wait for a Flash operation to complete or a TIMEOUT to occur */
  1455. while((status == FLASH_FLAG_BANK1_BSY) && (Timeout != 0x00))
  1456. {
  1457. status = FLASH_GetBank1Status();
  1458. Timeout--;
  1459. }
  1460. if(Timeout == 0x00 )
  1461. {
  1462. status = FLASH_TIMEOUT;
  1463. }
  1464. /* Return the operation status */
  1465. return status;
  1466. }
  1467. #ifdef STM32F10X_XL
  1468. /**
  1469. * @brief Waits for a Flash operation on Bank2 to complete or a TIMEOUT to occur.
  1470. * @note This function can be used only for STM32F10x_XL density devices.
  1471. * @param Timeout: FLASH programming Timeout
  1472. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  1473. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  1474. */
  1475. FLASH_Status FLASH_WaitForLastBank2Operation(uint32_t Timeout)
  1476. {
  1477. FLASH_Status status = FLASH_COMPLETE;
  1478. /* Check for the Flash Status */
  1479. status = FLASH_GetBank2Status();
  1480. /* Wait for a Flash operation to complete or a TIMEOUT to occur */
  1481. while((status == (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) && (Timeout != 0x00))
  1482. {
  1483. status = FLASH_GetBank2Status();
  1484. Timeout--;
  1485. }
  1486. if(Timeout == 0x00 )
  1487. {
  1488. status = FLASH_TIMEOUT;
  1489. }
  1490. /* Return the operation status */
  1491. return status;
  1492. }
  1493. #endif /* STM32F10X_XL */
  1494. /**
  1495. * @}
  1496. */
  1497. /**
  1498. * @}
  1499. */
  1500. /**
  1501. * @}
  1502. */
  1503. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/