stm32f0xx_flash.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_flash.h
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 05-December-2014
  7. * @brief This file contains all the functions prototypes for the FLASH
  8. * firmware library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  13. *
  14. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  15. * You may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at:
  17. *
  18. * http://www.st.com/software_license_agreement_liberty_v2
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *
  26. ******************************************************************************
  27. */
  28. /* Define to prevent recursive inclusion -------------------------------------*/
  29. #ifndef __STM32F0XX_FLASH_H
  30. #define __STM32F0XX_FLASH_H
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm32f0xx.h"
  36. /** @addtogroup STM32F0xx_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup FLASH
  40. * @{
  41. */
  42. /* Exported types ------------------------------------------------------------*/
  43. /**
  44. * @brief FLASH Status
  45. */
  46. typedef enum
  47. {
  48. FLASH_BUSY = 1,
  49. FLASH_ERROR_WRP,
  50. FLASH_ERROR_PROGRAM,
  51. FLASH_COMPLETE,
  52. FLASH_TIMEOUT
  53. }FLASH_Status;
  54. /* Exported constants --------------------------------------------------------*/
  55. /** @defgroup FLASH_Exported_Constants
  56. * @{
  57. */
  58. /** @defgroup FLASH_Latency
  59. * @{
  60. */
  61. #define FLASH_Latency_0 ((uint32_t)0x00000000) /*!< FLASH Zero Latency cycle */
  62. #define FLASH_Latency_1 FLASH_ACR_LATENCY /*!< FLASH One Latency cycle */
  63. #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_Latency_0) || \
  64. ((LATENCY) == FLASH_Latency_1))
  65. /**
  66. * @}
  67. */
  68. /** @defgroup FLASH_Interrupts
  69. * @{
  70. */
  71. #define FLASH_IT_EOP FLASH_CR_EOPIE /*!< End of programming interrupt source */
  72. #define FLASH_IT_ERR FLASH_CR_ERRIE /*!< Error interrupt source */
  73. #define IS_FLASH_IT(IT) ((((IT) & (uint32_t)0xFFFFEBFF) == 0x00000000) && (((IT) != 0x00000000)))
  74. /**
  75. * @}
  76. */
  77. /** @defgroup FLASH_Address
  78. * @{
  79. */
  80. #if defined(STM32F042) || defined(STM32F031) || defined(STM32F070x6) /*32K devices */
  81. #define IS_FLASH_PROGRAM_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) <= 0x08007FFF))
  82. #elif defined(STM32F030) || defined(STM32F051) /*64K devices */
  83. #define IS_FLASH_PROGRAM_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) <= 0x0800FFFF))
  84. #elif defined(STM32F072) || defined(STM32F070xB) /*128K devices */
  85. #define IS_FLASH_PROGRAM_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) <= 0x0801FFFF))
  86. #else /* STM32F091 || STM32F030 || STM32F030xC */ /*256K Flash devices */
  87. #define IS_FLASH_PROGRAM_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) <= 0x0800FFFF))
  88. #endif /* STM32F042 || STM32F031 || STM32F070x6 */
  89. /**
  90. * @}
  91. */
  92. /** @defgroup FLASH_OB_DATA_ADDRESS
  93. * @{
  94. */
  95. #define IS_OB_DATA_ADDRESS(ADDRESS) (((ADDRESS) == 0x1FFFF804) || ((ADDRESS) == 0x1FFFF806))
  96. /**
  97. * @}
  98. */
  99. /** @defgroup FLASH_Option_Bytes_Write_Protection
  100. * @{
  101. */
  102. #if !defined (STM32F072) && !defined (STM32F070xB) && !defined (STM32F091) && !defined (STM32F030) && !defined (STM32F030xC) /* 32K and 64K Flash devices */
  103. #define OB_WRP_Pages0to3 ((uint32_t)0x00000001) /* Write protection of page 0 to 3 */
  104. #define OB_WRP_Pages4to7 ((uint32_t)0x00000002) /* Write protection of page 4 to 7 */
  105. #define OB_WRP_Pages8to11 ((uint32_t)0x00000004) /* Write protection of page 8 to 11 */
  106. #define OB_WRP_Pages12to15 ((uint32_t)0x00000008) /* Write protection of page 12 to 15 */
  107. #define OB_WRP_Pages16to19 ((uint32_t)0x00000010) /* Write protection of page 16 to 19 */
  108. #define OB_WRP_Pages20to23 ((uint32_t)0x00000020) /* Write protection of page 20 to 23 */
  109. #define OB_WRP_Pages24to27 ((uint32_t)0x00000040) /* Write protection of page 24 to 27 */
  110. #define OB_WRP_Pages28to31 ((uint32_t)0x00000080) /* Write protection of page 28 to 31 */
  111. #define OB_WRP_Pages32to35 ((uint32_t)0x00000100) /* Write protection of page 32 to 35 */
  112. #define OB_WRP_Pages36to39 ((uint32_t)0x00000200) /* Write protection of page 36 to 39 */
  113. #define OB_WRP_Pages40to43 ((uint32_t)0x00000400) /* Write protection of page 40 to 43 */
  114. #define OB_WRP_Pages44to47 ((uint32_t)0x00000800) /* Write protection of page 44 to 47 */
  115. #define OB_WRP_Pages48to51 ((uint32_t)0x00001000) /* Write protection of page 48 to 51 */
  116. #define OB_WRP_Pages52to55 ((uint32_t)0x00002000) /* Write protection of page 52 to 55 */
  117. #define OB_WRP_Pages56to59 ((uint32_t)0x00004000) /* Write protection of page 56 to 59 */
  118. #define OB_WRP_Pages60to63 ((uint32_t)0x00008000) /* Write protection of page 60 to 63 */
  119. #define OB_WRP_AllPages ((uint32_t)0x0000FFFF) /*!< Write protection of all Sectors */
  120. #define IS_OB_WRP(PAGE) (((PAGE) != 0x0000000))
  121. #else /* 128K and 256K Flash devices */
  122. #define OB_WRP_Pages0to1 ((uint32_t)0x00000001) /* Write protection of page 0 to 1 */
  123. #define OB_WRP_Pages2to3 ((uint32_t)0x00000002) /* Write protection of page 2 to 3 */
  124. #define OB_WRP_Pages4to5 ((uint32_t)0x00000004) /* Write protection of page 4 to 5 */
  125. #define OB_WRP_Pages6to7 ((uint32_t)0x00000008) /* Write protection of page 6 to 7 */
  126. #define OB_WRP_Pages8to9 ((uint32_t)0x00000010) /* Write protection of page 8 to 9 */
  127. #define OB_WRP_Pages10to11 ((uint32_t)0x00000020) /* Write protection of page 10 to 11 */
  128. #define OB_WRP_Pages12to13 ((uint32_t)0x00000040) /* Write protection of page 12 to 13 */
  129. #define OB_WRP_Pages14to15 ((uint32_t)0x00000080) /* Write protection of page 14 to 15 */
  130. #define OB_WRP_Pages16to17 ((uint32_t)0x00000100) /* Write protection of page 16 to 17 */
  131. #define OB_WRP_Pages18to19 ((uint32_t)0x00000200) /* Write protection of page 18 to 19 */
  132. #define OB_WRP_Pages20to21 ((uint32_t)0x00000400) /* Write protection of page 20 to 21 */
  133. #define OB_WRP_Pages22to23 ((uint32_t)0x00000800) /* Write protection of page 22 to 23 */
  134. #define OB_WRP_Pages24to25 ((uint32_t)0x00001000) /* Write protection of page 24 to 25 */
  135. #define OB_WRP_Pages26to27 ((uint32_t)0x00002000) /* Write protection of page 26 to 27 */
  136. #define OB_WRP_Pages28to29 ((uint32_t)0x00004000) /* Write protection of page 28 to 29 */
  137. #define OB_WRP_Pages30to31 ((uint32_t)0x00008000) /* Write protection of page 30 to 31 */
  138. #define OB_WRP_Pages32to33 ((uint32_t)0x00010000) /* Write protection of page 32 to 33 */
  139. #define OB_WRP_Pages34to35 ((uint32_t)0x00020000) /* Write protection of page 34 to 35 */
  140. #define OB_WRP_Pages36to37 ((uint32_t)0x00040000) /* Write protection of page 36 to 37 */
  141. #define OB_WRP_Pages38to39 ((uint32_t)0x00080000) /* Write protection of page 38 to 39 */
  142. #define OB_WRP_Pages40to41 ((uint32_t)0x00100000) /* Write protection of page 40 to 41 */
  143. #define OB_WRP_Pages42to43 ((uint32_t)0x00200000) /* Write protection of page 42 to 43 */
  144. #define OB_WRP_Pages44to45 ((uint32_t)0x00400000) /* Write protection of page 44 to 45 */
  145. #define OB_WRP_Pages46to47 ((uint32_t)0x00800000) /* Write protection of page 46 to 47 */
  146. #define OB_WRP_Pages48to49 ((uint32_t)0x01000000) /* Write protection of page 48 to 49 */
  147. #define OB_WRP_Pages50to51 ((uint32_t)0x02000000) /* Write protection of page 50 to 51 */
  148. #define OB_WRP_Pages52to53 ((uint32_t)0x04000000) /* Write protection of page 52 to 53 */
  149. #define OB_WRP_Pages54to55 ((uint32_t)0x08000000) /* Write protection of page 54 to 55 */
  150. #define OB_WRP_Pages56to57 ((uint32_t)0x10000000) /* Write protection of page 56 to 57 */
  151. #define OB_WRP_Pages58to59 ((uint32_t)0x20000000) /* Write protection of page 58 to 59 */
  152. #define OB_WRP_Pages60to61 ((uint32_t)0x40000000) /* Write protection of page 60 to 61 */
  153. #if defined(STM32F091) || defined(STM32F030xC) /* 256K Flash devices */
  154. #define OB_WRP_Pages62to127 ((uint32_t)0x80000000) /* Write protection of page 62 to 127 */
  155. #else /* 128K Flash devices */
  156. #define OB_WRP_Pages62to63 ((uint32_t)0x80000000) /* Write protection of page 62 to 63 */
  157. #endif /* STM32F091 || STM32F030xC */
  158. #define OB_WRP_AllPages ((uint32_t)0xFFFFFFFF) /*!< Write protection of all Sectors */
  159. #define IS_OB_WRP(PAGE) (((PAGE) != 0x0000000))
  160. #endif /* STM32F072 || STM32F070xB || STM32F091 || STM32F030 || STM32F030xC */
  161. /**
  162. * @}
  163. */
  164. /** @defgroup FLASH_Option_Bytes_Read_Protection
  165. * @{
  166. */
  167. /**
  168. * @brief FLASH_Read Protection Level
  169. */
  170. #define OB_RDP_Level_0 ((uint8_t)0xAA)
  171. #define OB_RDP_Level_1 ((uint8_t)0xBB)
  172. /*#define OB_RDP_Level_2 ((uint8_t)0xCC)*/ /* Warning: When enabling read protection level 2
  173. it's no more possible to go back to level 1 or 0 */
  174. #define IS_OB_RDP(LEVEL) (((LEVEL) == OB_RDP_Level_0)||\
  175. ((LEVEL) == OB_RDP_Level_1))/*||\
  176. ((LEVEL) == OB_RDP_Level_2))*/
  177. /**
  178. * @}
  179. */
  180. /** @defgroup FLASH_Option_Bytes_IWatchdog
  181. * @{
  182. */
  183. #define OB_IWDG_SW ((uint8_t)0x01) /*!< Software IWDG selected */
  184. #define OB_IWDG_HW ((uint8_t)0x00) /*!< Hardware IWDG selected */
  185. #define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))
  186. /**
  187. * @}
  188. */
  189. /** @defgroup FLASH_Option_Bytes_nRST_STOP
  190. * @{
  191. */
  192. #define OB_STOP_NoRST ((uint8_t)0x02) /*!< No reset generated when entering in STOP */
  193. #define OB_STOP_RST ((uint8_t)0x00) /*!< Reset generated when entering in STOP */
  194. #define IS_OB_STOP_SOURCE(SOURCE) (((SOURCE) == OB_STOP_NoRST) || ((SOURCE) == OB_STOP_RST))
  195. /**
  196. * @}
  197. */
  198. /** @defgroup FLASH_Option_Bytes_nRST_STDBY
  199. * @{
  200. */
  201. #define OB_STDBY_NoRST ((uint8_t)0x04) /*!< No reset generated when entering in STANDBY */
  202. #define OB_STDBY_RST ((uint8_t)0x00) /*!< Reset generated when entering in STANDBY */
  203. #define IS_OB_STDBY_SOURCE(SOURCE) (((SOURCE) == OB_STDBY_NoRST) || ((SOURCE) == OB_STDBY_RST))
  204. /**
  205. * @}
  206. */
  207. /** @defgroup FLASH_Option_Bytes_BOOT1
  208. * @{
  209. */
  210. #define OB_BOOT1_RESET ((uint8_t)0x00) /*!< BOOT1 Reset */
  211. #define OB_BOOT1_SET ((uint8_t)0x10) /*!< BOOT1 Set */
  212. #define IS_OB_BOOT1(BOOT1) (((BOOT1) == OB_BOOT1_RESET) || ((BOOT1) == OB_BOOT1_SET))
  213. /**
  214. * @}
  215. */
  216. /** @defgroup FLASH_Option_Bytes_BOOT0
  217. * @{
  218. */
  219. #define OB_BOOT0_RESET ((uint8_t)0x00) /*!< BOOT0 Reset */
  220. #define OB_BOOT0_SET ((uint8_t)0x08) /*!< BOOT0 Set */
  221. #define IS_OB_BOOT0(BOOT0) (((BOOT0) == OB_BOOT0_RESET) || ((BOOT0) == OB_BOOT0_SET))
  222. /**
  223. * @}
  224. */
  225. /** @defgroup FLASH_Option_Bytes_BOOT0SW
  226. * @{
  227. */
  228. #define OB_BOOT0_SW ((uint8_t)0x00) /*!< BOOT0 pin disabled */
  229. #define OB_BOOT0_HW ((uint8_t)0x80) /*!< BOOT0 pin bonded with GPIO */
  230. #define IS_OB_BOOT0SW(BOOT0) (((BOOT0) == OB_BOOT0_SW) || ((BOOT0) == OB_BOOT0_HW))
  231. /**
  232. * @}
  233. */
  234. /** @defgroup FLASH_Option_Bytes_VDDA_Analog_Monitoring
  235. * @{
  236. */
  237. #define OB_VDDA_ANALOG_ON ((uint8_t)0x20) /*!< Analog monitoring on VDDA Power source ON */
  238. #define OB_VDDA_ANALOG_OFF ((uint8_t)0x00) /*!< Analog monitoring on VDDA Power source OFF */
  239. #define IS_OB_VDDA_ANALOG(ANALOG) (((ANALOG) == OB_VDDA_ANALOG_ON) || ((ANALOG) == OB_VDDA_ANALOG_OFF))
  240. /**
  241. * @}
  242. */
  243. /** @defgroup FLASH_Option_Bytes_SRAM_Parity_Enable
  244. * @{
  245. */
  246. #define OB_SRAM_PARITY_SET ((uint8_t)0x00) /*!< SRAM parity enable Set */
  247. #define OB_SRAM_PARITY_RESET ((uint8_t)0x40) /*!< SRAM parity enable reset */
  248. #define IS_OB_SRAM_PARITY(PARITY) (((PARITY) == OB_SRAM_PARITY_SET) || ((PARITY) == OB_SRAM_PARITY_RESET))
  249. /**
  250. * @}
  251. */
  252. /** @defgroup FLASH_Flags
  253. * @{
  254. */
  255. #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Busy flag */
  256. #define FLASH_FLAG_PGERR FLASH_SR_PGERR /*!< FLASH Programming error flag */
  257. #define FLASH_FLAG_WRPERR FLASH_SR_WRPERR /*!< FLASH Write protected error flag */
  258. #define FLASH_FLAG_EOP FLASH_SR_EOP /*!< FLASH End of Programming flag */
  259. #define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFFFFCB) == 0x00000000) && ((FLAG) != 0x00000000))
  260. #define IS_FLASH_GET_FLAG(FLAG) (((FLAG) == FLASH_FLAG_BSY) || ((FLAG) == FLASH_FLAG_PGERR) || \
  261. ((FLAG) == FLASH_FLAG_WRPERR) || ((FLAG) == FLASH_FLAG_EOP))
  262. /**
  263. * @}
  264. */
  265. /** @defgroup FLASH_Timeout_definition
  266. * @{
  267. */
  268. #define FLASH_ER_PRG_TIMEOUT ((uint32_t)0x000B0000)
  269. /**
  270. * @}
  271. */
  272. /** @defgroup FLASH_Legacy
  273. * @{
  274. */
  275. #define FLASH_WRProt_Pages0to3 OB_WRP_Pages0to3
  276. #define FLASH_WRProt_Pages4to7 OB_WRP_Pages4to7
  277. #define FLASH_WRProt_Pages8to11 OB_WRP_Pages8to11
  278. #define FLASH_WRProt_Pages12to15 OB_WRP_Pages12to15
  279. #define FLASH_WRProt_Pages16to19 OB_WRP_Pages16to19
  280. #define FLASH_WRProt_Pages20to23 OB_WRP_Pages20to23
  281. #define FLASH_WRProt_Pages24to27 OB_WRP_Pages24to27
  282. #define FLASH_WRProt_Pages28to31 OB_WRP_Pages28to31
  283. #define FLASH_WRProt_Pages32to35 OB_WRP_Pages32to35
  284. #define FLASH_WRProt_Pages36to39 OB_WRP_Pages36to39
  285. #define FLASH_WRProt_Pages40to43 OB_WRP_Pages40to21
  286. #define FLASH_WRProt_Pages44to47 OB_WRP_Pages44to23
  287. #define FLASH_WRProt_Pages48to51 OB_WRP_Pages48to51
  288. #define FLASH_WRProt_Pages52to55 OB_WRP_Pages52to55
  289. #define FLASH_WRProt_Pages56to59 OB_WRP_Pages56to59
  290. #define FLASH_WRProt_Pages60to63 OB_WRP_Pages60to63
  291. #define FLASH_WRProt_AllPages OB_WRP_AllPages
  292. /**
  293. * @}
  294. */
  295. /**
  296. * @}
  297. */
  298. /* Exported macro ------------------------------------------------------------*/
  299. /* Exported functions ------------------------------------------------------- */
  300. /**
  301. * @brief FLASH memory functions that can be executed from FLASH.
  302. */
  303. /* FLASH Interface configuration functions ************************************/
  304. void FLASH_SetLatency(uint32_t FLASH_Latency);
  305. void FLASH_PrefetchBufferCmd(FunctionalState NewState);
  306. FlagStatus FLASH_GetPrefetchBufferStatus(void);
  307. /* FLASH Memory Programming functions *****************************************/
  308. void FLASH_Unlock(void);
  309. void FLASH_Lock(void);
  310. FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
  311. FLASH_Status FLASH_EraseAllPages(void);
  312. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
  313. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
  314. /* FLASH Option Bytes Programming functions *****************************************/
  315. void FLASH_OB_Unlock(void);
  316. void FLASH_OB_Lock(void);
  317. void FLASH_OB_Launch(void);
  318. FLASH_Status FLASH_OB_Erase(void);
  319. FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP);
  320. FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);
  321. FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
  322. FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
  323. FLASH_Status FLASH_OB_BOOT0Config(uint8_t OB_BOOT0);
  324. FLASH_Status FLASH_OB_BOOT0SWConfig(uint8_t OB_BOOT0SW);
  325. FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
  326. FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity);
  327. FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
  328. FLASH_Status FLASH_OB_ProgramData(uint32_t Address, uint8_t Data);
  329. uint8_t FLASH_OB_GetUser(void);
  330. uint32_t FLASH_OB_GetWRP(void);
  331. FlagStatus FLASH_OB_GetRDP(void);
  332. /* FLASH Interrupts and flags management functions **********************************/
  333. void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState);
  334. FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG);
  335. void FLASH_ClearFlag(uint32_t FLASH_FLAG);
  336. FLASH_Status FLASH_GetStatus(void);
  337. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout);
  338. /** @defgroup FLASH_Legacy
  339. * @{
  340. */
  341. #define FLASH_EraseOptionBytes FLASH_OB_Erase
  342. #define FLASH_EnableWriteProtection FLASH_OB_EnableWRP
  343. #define FLASH_UserOptionByteConfig FLASH_OB_UserConfig
  344. #define FLASH_ProgramOptionByteData FLASH_OB_ProgramData
  345. #define FLASH_GetUserOptionByte FLASH_OB_GetUser
  346. #define FLASH_GetWriteProtectionOptionByte FLASH_OB_GetWRP
  347. /**
  348. * @}
  349. */
  350. #ifdef __cplusplus
  351. }
  352. #endif
  353. #endif /* __STM32F0XX_FLASH_H */
  354. /**
  355. * @}
  356. */
  357. /**
  358. * @}
  359. */
  360. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/