stm32f2xx_flash.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_flash.h
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 18-April-2011
  7. * @brief This file contains all the functions prototypes for the FLASH
  8. * firmware library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  20. ******************************************************************************
  21. */
  22. /* Define to prevent recursive inclusion -------------------------------------*/
  23. #ifndef __STM32F2xx_FLASH_H
  24. #define __STM32F2xx_FLASH_H
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /* Includes ------------------------------------------------------------------*/
  29. #include "stm32f2xx.h"
  30. /** @addtogroup STM32F2xx_StdPeriph_Driver
  31. * @{
  32. */
  33. /** @addtogroup FLASH
  34. * @{
  35. */
  36. /* Exported types ------------------------------------------------------------*/
  37. /**
  38. * @brief FLASH Status
  39. */
  40. typedef enum
  41. {
  42. FLASH_BUSY = 1,
  43. FLASH_ERROR_PGS,
  44. FLASH_ERROR_PGP,
  45. FLASH_ERROR_PGA,
  46. FLASH_ERROR_WRP,
  47. FLASH_ERROR_PROGRAM,
  48. FLASH_ERROR_OPERATION,
  49. FLASH_COMPLETE
  50. }FLASH_Status;
  51. /* Exported constants --------------------------------------------------------*/
  52. /** @defgroup FLASH_Exported_Constants
  53. * @{
  54. */
  55. /** @defgroup Flash_Latency
  56. * @{
  57. */
  58. #define FLASH_Latency_0 ((uint8_t)0x0000) /*!< FLASH Zero Latency cycle */
  59. #define FLASH_Latency_1 ((uint8_t)0x0001) /*!< FLASH One Latency cycle */
  60. #define FLASH_Latency_2 ((uint8_t)0x0002) /*!< FLASH Two Latency cycles */
  61. #define FLASH_Latency_3 ((uint8_t)0x0003) /*!< FLASH Three Latency cycles */
  62. #define FLASH_Latency_4 ((uint8_t)0x0004) /*!< FLASH Four Latency cycles */
  63. #define FLASH_Latency_5 ((uint8_t)0x0005) /*!< FLASH Five Latency cycles */
  64. #define FLASH_Latency_6 ((uint8_t)0x0006) /*!< FLASH Six Latency cycles */
  65. #define FLASH_Latency_7 ((uint8_t)0x0007) /*!< FLASH Seven Latency cycles */
  66. #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_Latency_0) || \
  67. ((LATENCY) == FLASH_Latency_1) || \
  68. ((LATENCY) == FLASH_Latency_2) || \
  69. ((LATENCY) == FLASH_Latency_3) || \
  70. ((LATENCY) == FLASH_Latency_4) || \
  71. ((LATENCY) == FLASH_Latency_5) || \
  72. ((LATENCY) == FLASH_Latency_6) || \
  73. ((LATENCY) == FLASH_Latency_7))
  74. /**
  75. * @}
  76. */
  77. /** @defgroup FLASH_Voltage_Range
  78. * @{
  79. */
  80. #define VoltageRange_1 ((uint8_t)0x00) /*!< Device operating range: 1.8V to 2.1V */
  81. #define VoltageRange_2 ((uint8_t)0x01) /*!<Device operating range: 2.1V to 2.7V */
  82. #define VoltageRange_3 ((uint8_t)0x02) /*!<Device operating range: 2.7V to 3.6V */
  83. #define VoltageRange_4 ((uint8_t)0x03) /*!<Device operating range: 2.7V to 3.6V + External Vpp */
  84. #define IS_VOLTAGERANGE(RANGE)(((RANGE) == VoltageRange_1) || \
  85. ((RANGE) == VoltageRange_2) || \
  86. ((RANGE) == VoltageRange_3) || \
  87. ((RANGE) == VoltageRange_4))
  88. /**
  89. * @}
  90. */
  91. /** @defgroup FLASH_Sectors
  92. * @{
  93. */
  94. #define FLASH_Sector_0 ((uint16_t)0x0000) /*!< Sector Number 0 */
  95. #define FLASH_Sector_1 ((uint16_t)0x0008) /*!< Sector Number 1 */
  96. #define FLASH_Sector_2 ((uint16_t)0x0010) /*!< Sector Number 2 */
  97. #define FLASH_Sector_3 ((uint16_t)0x0018) /*!< Sector Number 3 */
  98. #define FLASH_Sector_4 ((uint16_t)0x0020) /*!< Sector Number 4 */
  99. #define FLASH_Sector_5 ((uint16_t)0x0028) /*!< Sector Number 5 */
  100. #define FLASH_Sector_6 ((uint16_t)0x0030) /*!< Sector Number 6 */
  101. #define FLASH_Sector_7 ((uint16_t)0x0038) /*!< Sector Number 7 */
  102. #define FLASH_Sector_8 ((uint16_t)0x0040) /*!< Sector Number 8 */
  103. #define FLASH_Sector_9 ((uint16_t)0x0048) /*!< Sector Number 9 */
  104. #define FLASH_Sector_10 ((uint16_t)0x0050) /*!< Sector Number 10 */
  105. #define FLASH_Sector_11 ((uint16_t)0x0058) /*!< Sector Number 11 */
  106. #define IS_FLASH_SECTOR(SECTOR) (((SECTOR) == FLASH_Sector_0) || ((SECTOR) == FLASH_Sector_1) ||\
  107. ((SECTOR) == FLASH_Sector_2) || ((SECTOR) == FLASH_Sector_3) ||\
  108. ((SECTOR) == FLASH_Sector_4) || ((SECTOR) == FLASH_Sector_5) ||\
  109. ((SECTOR) == FLASH_Sector_6) || ((SECTOR) == FLASH_Sector_7) ||\
  110. ((SECTOR) == FLASH_Sector_8) || ((SECTOR) == FLASH_Sector_9) ||\
  111. ((SECTOR) == FLASH_Sector_10) || ((SECTOR) == FLASH_Sector_11))
  112. #define IS_FLASH_ADDRESS(ADDRESS) ((((ADDRESS) >= 0x08000000) && ((ADDRESS) < 0x080FFFFF)) ||\
  113. (((ADDRESS) >= 0x1FFF7800) && ((ADDRESS) < 0x1FFF7A0F)))
  114. /**
  115. * @}
  116. */
  117. /** @defgroup Option_Bytes_Write_Protection
  118. * @{
  119. */
  120. #define OB_WRP_Sector_0 ((uint32_t)0x00000001) /*!< Write protection of Sector0 */
  121. #define OB_WRP_Sector_1 ((uint32_t)0x00000002) /*!< Write protection of Sector1 */
  122. #define OB_WRP_Sector_2 ((uint32_t)0x00000004) /*!< Write protection of Sector2 */
  123. #define OB_WRP_Sector_3 ((uint32_t)0x00000008) /*!< Write protection of Sector3 */
  124. #define OB_WRP_Sector_4 ((uint32_t)0x00000010) /*!< Write protection of Sector4 */
  125. #define OB_WRP_Sector_5 ((uint32_t)0x00000020) /*!< Write protection of Sector5 */
  126. #define OB_WRP_Sector_6 ((uint32_t)0x00000040) /*!< Write protection of Sector6 */
  127. #define OB_WRP_Sector_7 ((uint32_t)0x00000080) /*!< Write protection of Sector7 */
  128. #define OB_WRP_Sector_8 ((uint32_t)0x00000100) /*!< Write protection of Sector8 */
  129. #define OB_WRP_Sector_9 ((uint32_t)0x00000200) /*!< Write protection of Sector9 */
  130. #define OB_WRP_Sector_10 ((uint32_t)0x00000400) /*!< Write protection of Sector10 */
  131. #define OB_WRP_Sector_11 ((uint32_t)0x00000800) /*!< Write protection of Sector11 */
  132. #define OB_WRP_Sector_All ((uint32_t)0x00000FFF) /*!< Write protection of all Sectors */
  133. #define IS_OB_WRP(SECTOR)((((SECTOR) & (uint32_t)0xFFFFF000) == 0x00000000) && ((SECTOR) != 0x00000000))
  134. /**
  135. * @}
  136. */
  137. /** @defgroup FLASH_Option_Bytes_Read_Protection
  138. * @{
  139. */
  140. #define OB_RDP_Level_0 ((uint8_t)0xAA)
  141. #define OB_RDP_Level_1 ((uint8_t)0x55)
  142. /*#define OB_RDP_Level_2 ((uint8_t)0xCC)*/ /*!< Warning: When enabling read protection level 2
  143. it's no more possible to go back to level 1 or 0 */
  144. #define IS_OB_RDP(LEVEL) (((LEVEL) == OB_RDP_Level_0)||\
  145. ((LEVEL) == OB_RDP_Level_1))/*||\
  146. ((LEVEL) == OB_RDP_Level_2))*/
  147. /**
  148. * @}
  149. */
  150. /** @defgroup FLASH_Option_Bytes_IWatchdog
  151. * @{
  152. */
  153. #define OB_IWDG_SW ((uint8_t)0x20) /*!< Software IWDG selected */
  154. #define OB_IWDG_HW ((uint8_t)0x00) /*!< Hardware IWDG selected */
  155. #define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))
  156. /**
  157. * @}
  158. */
  159. /** @defgroup FLASH_Option_Bytes_nRST_STOP
  160. * @{
  161. */
  162. #define OB_STOP_NoRST ((uint8_t)0x40) /*!< No reset generated when entering in STOP */
  163. #define OB_STOP_RST ((uint8_t)0x00) /*!< Reset generated when entering in STOP */
  164. #define IS_OB_STOP_SOURCE(SOURCE) (((SOURCE) == OB_STOP_NoRST) || ((SOURCE) == OB_STOP_RST))
  165. /**
  166. * @}
  167. */
  168. /** @defgroup FLASH_Option_Bytes_nRST_STDBY
  169. * @{
  170. */
  171. #define OB_STDBY_NoRST ((uint8_t)0x80) /*!< No reset generated when entering in STANDBY */
  172. #define OB_STDBY_RST ((uint8_t)0x00) /*!< Reset generated when entering in STANDBY */
  173. #define IS_OB_STDBY_SOURCE(SOURCE) (((SOURCE) == OB_STDBY_NoRST) || ((SOURCE) == OB_STDBY_RST))
  174. /**
  175. * @}
  176. */
  177. /** @defgroup FLASH_BOR_Reset_Level
  178. * @{
  179. */
  180. #define OB_BOR_LEVEL3 ((uint8_t)0x00) /*!< Supply voltage ranges from 2.70 to 3.60 V */
  181. #define OB_BOR_LEVEL2 ((uint8_t)0x04) /*!< Supply voltage ranges from 2.40 to 2.70 V */
  182. #define OB_BOR_LEVEL1 ((uint8_t)0x08) /*!< Supply voltage ranges from 2.10 to 2.40 V */
  183. #define OB_BOR_OFF ((uint8_t)0x0C) /*!< Supply voltage ranges from 1.62 to 2.10 V */
  184. #define IS_OB_BOR(LEVEL) (((LEVEL) == OB_BOR_LEVEL1) || ((LEVEL) == OB_BOR_LEVEL2) ||\
  185. ((LEVEL) == OB_BOR_LEVEL3) || ((LEVEL) == OB_BOR_OFF))
  186. /**
  187. * @}
  188. */
  189. /** @defgroup FLASH_Interrupts
  190. * @{
  191. */
  192. #define FLASH_IT_EOP ((uint32_t)0x01000000) /*!< End of FLASH Operation Interrupt source */
  193. #define FLASH_IT_ERR ((uint32_t)0x02000000) /*!< Error Interrupt source */
  194. #define IS_FLASH_IT(IT) ((((IT) & (uint32_t)0xFCFFFFFF) == 0x00000000) && ((IT) != 0x00000000))
  195. /**
  196. * @}
  197. */
  198. /** @defgroup FLASH_Flags
  199. * @{
  200. */
  201. #define FLASH_FLAG_EOP ((uint32_t)0x00000001) /*!< FLASH End of Operation flag */
  202. #define FLASH_FLAG_OPERR ((uint32_t)0x00000002) /*!< FLASH operation Error flag */
  203. #define FLASH_FLAG_WRPERR ((uint32_t)0x00000010) /*!< FLASH Write protected error flag */
  204. #define FLASH_FLAG_PGAERR ((uint32_t)0x00000020) /*!< FLASH Programming Alignment error flag */
  205. #define FLASH_FLAG_PGPERR ((uint32_t)0x00000040) /*!< FLASH Programming Parallelism error flag */
  206. #define FLASH_FLAG_PGSERR ((uint32_t)0x00000080) /*!< FLASH Programming Sequence error flag */
  207. #define FLASH_FLAG_BSY ((uint32_t)0x00010000) /*!< FLASH Busy flag */
  208. #define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFFFF0C) == 0x00000000) && ((FLAG) != 0x00000000))
  209. #define IS_FLASH_GET_FLAG(FLAG) (((FLAG) == FLASH_FLAG_EOP) || ((FLAG) == FLASH_FLAG_OPERR) || \
  210. ((FLAG) == FLASH_FLAG_WRPERR) || ((FLAG) == FLASH_FLAG_PGAERR) || \
  211. ((FLAG) == FLASH_FLAG_PGPERR) || ((FLAG) == FLASH_FLAG_PGSERR) || \
  212. ((FLAG) == FLASH_FLAG_BSY))
  213. /**
  214. * @}
  215. */
  216. /** @defgroup FLASH_Program_Parallelism
  217. * @{
  218. */
  219. #define FLASH_PSIZE_BYTE ((uint32_t)0x00000000)
  220. #define FLASH_PSIZE_HALF_WORD ((uint32_t)0x00000100)
  221. #define FLASH_PSIZE_WORD ((uint32_t)0x00000200)
  222. #define FLASH_PSIZE_DOUBLE_WORD ((uint32_t)0x00000300)
  223. #define CR_PSIZE_MASK ((uint32_t)0xFFFFFCFF)
  224. /**
  225. * @}
  226. */
  227. /** @defgroup FLASH_Keys
  228. * @{
  229. */
  230. #define RDP_KEY ((uint16_t)0x00A5)
  231. #define FLASH_KEY1 ((uint32_t)0x45670123)
  232. #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
  233. #define FLASH_OPT_KEY1 ((uint32_t)0x08192A3B)
  234. #define FLASH_OPT_KEY2 ((uint32_t)0x4C5D6E7F)
  235. /**
  236. * @}
  237. */
  238. /**
  239. * @brief ACR register byte 0 (Bits[8:0]) base address
  240. */
  241. #define ACR_BYTE0_ADDRESS ((uint32_t)0x40023C00)
  242. /**
  243. * @brief OPTCR register byte 3 (Bits[24:16]) base address
  244. */
  245. #define OPTCR_BYTE0_ADDRESS ((uint32_t)0x40023C14)
  246. #define OPTCR_BYTE1_ADDRESS ((uint32_t)0x40023C15)
  247. #define OPTCR_BYTE2_ADDRESS ((uint32_t)0x40023C16)
  248. /**
  249. * @}
  250. */
  251. /* Exported macro ------------------------------------------------------------*/
  252. /* Exported functions --------------------------------------------------------*/
  253. /* FLASH Interface configuration functions ************************************/
  254. void FLASH_SetLatency(uint32_t FLASH_Latency);
  255. void FLASH_PrefetchBufferCmd(FunctionalState NewState);
  256. void FLASH_InstructionCacheCmd(FunctionalState NewState);
  257. void FLASH_DataCacheCmd(FunctionalState NewState);
  258. void FLASH_InstructionCacheReset(void);
  259. void FLASH_DataCacheReset(void);
  260. /* FLASH Memory Programming functions *****************************************/
  261. void FLASH_Unlock(void);
  262. void FLASH_Lock(void);
  263. FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange);
  264. FLASH_Status FLASH_EraseAllSectors(uint8_t VoltageRange);
  265. FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data);
  266. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
  267. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
  268. FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data);
  269. /* Option Bytes Programming functions *****************************************/
  270. void FLASH_OB_Unlock(void);
  271. void FLASH_OB_Lock(void);
  272. void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
  273. void FLASH_OB_RDPConfig(uint8_t OB_RDP);
  274. void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
  275. void FLASH_OB_BORConfig(uint8_t OB_BOR);
  276. FLASH_Status FLASH_OB_Launch(void);
  277. uint8_t FLASH_OB_GetUser(void);
  278. uint16_t FLASH_OB_GetWRP(void);
  279. FlagStatus FLASH_OB_GetRDP(void);
  280. uint8_t FLASH_OB_GetBOR(void);
  281. /* Interrupts and flags management functions **********************************/
  282. void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState);
  283. FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG);
  284. void FLASH_ClearFlag(uint32_t FLASH_FLAG);
  285. FLASH_Status FLASH_GetStatus(void);
  286. FLASH_Status FLASH_WaitForLastOperation(void);
  287. #ifdef __cplusplus
  288. }
  289. #endif
  290. #endif /* __STM32F2xx_FLASH_H */
  291. /**
  292. * @}
  293. */
  294. /**
  295. * @}
  296. */
  297. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/