stm32f10x_flash.c 62 KB

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