Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

391 righe
17 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_flash.h
  4. * @author MCD Application Team
  5. * @version V1.1.0
  6. * @date 11-January-2013
  7. * @brief This file contains all the functions prototypes for the FLASH
  8. * firmware library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT 2013 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 __STM32F4xx_FLASH_H
  30. #define __STM32F4xx_FLASH_H
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm32f4xx.h"
  36. /** @addtogroup STM32F4xx_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_PGS,
  50. FLASH_ERROR_PGP,
  51. FLASH_ERROR_PGA,
  52. FLASH_ERROR_WRP,
  53. FLASH_ERROR_PROGRAM,
  54. FLASH_ERROR_OPERATION,
  55. FLASH_COMPLETE
  56. }FLASH_Status;
  57. /* Exported constants --------------------------------------------------------*/
  58. /** @defgroup FLASH_Exported_Constants
  59. * @{
  60. */
  61. /** @defgroup Flash_Latency
  62. * @{
  63. */
  64. #define FLASH_Latency_0 ((uint8_t)0x0000) /*!< FLASH Zero Latency cycle */
  65. #define FLASH_Latency_1 ((uint8_t)0x0001) /*!< FLASH One Latency cycle */
  66. #define FLASH_Latency_2 ((uint8_t)0x0002) /*!< FLASH Two Latency cycles */
  67. #define FLASH_Latency_3 ((uint8_t)0x0003) /*!< FLASH Three Latency cycles */
  68. #define FLASH_Latency_4 ((uint8_t)0x0004) /*!< FLASH Four Latency cycles */
  69. #define FLASH_Latency_5 ((uint8_t)0x0005) /*!< FLASH Five Latency cycles */
  70. #define FLASH_Latency_6 ((uint8_t)0x0006) /*!< FLASH Six Latency cycles */
  71. #define FLASH_Latency_7 ((uint8_t)0x0007) /*!< FLASH Seven Latency cycles */
  72. #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_Latency_0) || \
  73. ((LATENCY) == FLASH_Latency_1) || \
  74. ((LATENCY) == FLASH_Latency_2) || \
  75. ((LATENCY) == FLASH_Latency_3) || \
  76. ((LATENCY) == FLASH_Latency_4) || \
  77. ((LATENCY) == FLASH_Latency_5) || \
  78. ((LATENCY) == FLASH_Latency_6) || \
  79. ((LATENCY) == FLASH_Latency_7))
  80. /**
  81. * @}
  82. */
  83. /** @defgroup FLASH_Voltage_Range
  84. * @{
  85. */
  86. #define VoltageRange_1 ((uint8_t)0x00) /*!< Device operating range: 1.8V to 2.1V */
  87. #define VoltageRange_2 ((uint8_t)0x01) /*!<Device operating range: 2.1V to 2.7V */
  88. #define VoltageRange_3 ((uint8_t)0x02) /*!<Device operating range: 2.7V to 3.6V */
  89. #define VoltageRange_4 ((uint8_t)0x03) /*!<Device operating range: 2.7V to 3.6V + External Vpp */
  90. #define IS_VOLTAGERANGE(RANGE)(((RANGE) == VoltageRange_1) || \
  91. ((RANGE) == VoltageRange_2) || \
  92. ((RANGE) == VoltageRange_3) || \
  93. ((RANGE) == VoltageRange_4))
  94. /**
  95. * @}
  96. */
  97. /** @defgroup FLASH_Sectors
  98. * @{
  99. */
  100. #define FLASH_Sector_0 ((uint16_t)0x0000) /*!< Sector Number 0 */
  101. #define FLASH_Sector_1 ((uint16_t)0x0008) /*!< Sector Number 1 */
  102. #define FLASH_Sector_2 ((uint16_t)0x0010) /*!< Sector Number 2 */
  103. #define FLASH_Sector_3 ((uint16_t)0x0018) /*!< Sector Number 3 */
  104. #define FLASH_Sector_4 ((uint16_t)0x0020) /*!< Sector Number 4 */
  105. #define FLASH_Sector_5 ((uint16_t)0x0028) /*!< Sector Number 5 */
  106. #define FLASH_Sector_6 ((uint16_t)0x0030) /*!< Sector Number 6 */
  107. #define FLASH_Sector_7 ((uint16_t)0x0038) /*!< Sector Number 7 */
  108. #define FLASH_Sector_8 ((uint16_t)0x0040) /*!< Sector Number 8 */
  109. #define FLASH_Sector_9 ((uint16_t)0x0048) /*!< Sector Number 9 */
  110. #define FLASH_Sector_10 ((uint16_t)0x0050) /*!< Sector Number 10 */
  111. #define FLASH_Sector_11 ((uint16_t)0x0058) /*!< Sector Number 11 */
  112. #define FLASH_Sector_12 ((uint16_t)0x0080) /*!< Sector Number 12 */
  113. #define FLASH_Sector_13 ((uint16_t)0x0088) /*!< Sector Number 13 */
  114. #define FLASH_Sector_14 ((uint16_t)0x0090) /*!< Sector Number 14 */
  115. #define FLASH_Sector_15 ((uint16_t)0x0098) /*!< Sector Number 15 */
  116. #define FLASH_Sector_16 ((uint16_t)0x00A0) /*!< Sector Number 16 */
  117. #define FLASH_Sector_17 ((uint16_t)0x00A8) /*!< Sector Number 17 */
  118. #define FLASH_Sector_18 ((uint16_t)0x00B0) /*!< Sector Number 18 */
  119. #define FLASH_Sector_19 ((uint16_t)0x00B8) /*!< Sector Number 19 */
  120. #define FLASH_Sector_20 ((uint16_t)0x00C0) /*!< Sector Number 20 */
  121. #define FLASH_Sector_21 ((uint16_t)0x00C8) /*!< Sector Number 21 */
  122. #define FLASH_Sector_22 ((uint16_t)0x00D0) /*!< Sector Number 22 */
  123. #define FLASH_Sector_23 ((uint16_t)0x00D8) /*!< Sector Number 23 */
  124. #define IS_FLASH_SECTOR(SECTOR) (((SECTOR) == FLASH_Sector_0) || ((SECTOR) == FLASH_Sector_1) ||\
  125. ((SECTOR) == FLASH_Sector_2) || ((SECTOR) == FLASH_Sector_3) ||\
  126. ((SECTOR) == FLASH_Sector_4) || ((SECTOR) == FLASH_Sector_5) ||\
  127. ((SECTOR) == FLASH_Sector_6) || ((SECTOR) == FLASH_Sector_7) ||\
  128. ((SECTOR) == FLASH_Sector_8) || ((SECTOR) == FLASH_Sector_9) ||\
  129. ((SECTOR) == FLASH_Sector_10) || ((SECTOR) == FLASH_Sector_11) ||\
  130. ((SECTOR) == FLASH_Sector_12) || ((SECTOR) == FLASH_Sector_13) ||\
  131. ((SECTOR) == FLASH_Sector_14) || ((SECTOR) == FLASH_Sector_15) ||\
  132. ((SECTOR) == FLASH_Sector_16) || ((SECTOR) == FLASH_Sector_17) ||\
  133. ((SECTOR) == FLASH_Sector_18) || ((SECTOR) == FLASH_Sector_19) ||\
  134. ((SECTOR) == FLASH_Sector_20) || ((SECTOR) == FLASH_Sector_21) ||\
  135. ((SECTOR) == FLASH_Sector_22) || ((SECTOR) == FLASH_Sector_23))
  136. #define IS_FLASH_ADDRESS(ADDRESS) ((((ADDRESS) >= 0x08000000) && ((ADDRESS) < 0x081FFFFF)) ||\
  137. (((ADDRESS) >= 0x1FFF7800) && ((ADDRESS) < 0x1FFF7A0F)))
  138. /**
  139. * @}
  140. */
  141. /** @defgroup Option_Bytes_Write_Protection
  142. * @{
  143. */
  144. #define OB_WRP_Sector_0 ((uint32_t)0x00000001) /*!< Write protection of Sector0 */
  145. #define OB_WRP_Sector_1 ((uint32_t)0x00000002) /*!< Write protection of Sector1 */
  146. #define OB_WRP_Sector_2 ((uint32_t)0x00000004) /*!< Write protection of Sector2 */
  147. #define OB_WRP_Sector_3 ((uint32_t)0x00000008) /*!< Write protection of Sector3 */
  148. #define OB_WRP_Sector_4 ((uint32_t)0x00000010) /*!< Write protection of Sector4 */
  149. #define OB_WRP_Sector_5 ((uint32_t)0x00000020) /*!< Write protection of Sector5 */
  150. #define OB_WRP_Sector_6 ((uint32_t)0x00000040) /*!< Write protection of Sector6 */
  151. #define OB_WRP_Sector_7 ((uint32_t)0x00000080) /*!< Write protection of Sector7 */
  152. #define OB_WRP_Sector_8 ((uint32_t)0x00000100) /*!< Write protection of Sector8 */
  153. #define OB_WRP_Sector_9 ((uint32_t)0x00000200) /*!< Write protection of Sector9 */
  154. #define OB_WRP_Sector_10 ((uint32_t)0x00000400) /*!< Write protection of Sector10 */
  155. #define OB_WRP_Sector_11 ((uint32_t)0x00000800) /*!< Write protection of Sector11 */
  156. #define OB_WRP_Sector_12 ((uint32_t)0x00000001) /*!< Write protection of Sector12 */
  157. #define OB_WRP_Sector_13 ((uint32_t)0x00000002) /*!< Write protection of Sector13 */
  158. #define OB_WRP_Sector_14 ((uint32_t)0x00000004) /*!< Write protection of Sector14 */
  159. #define OB_WRP_Sector_15 ((uint32_t)0x00000008) /*!< Write protection of Sector15 */
  160. #define OB_WRP_Sector_16 ((uint32_t)0x00000010) /*!< Write protection of Sector16 */
  161. #define OB_WRP_Sector_17 ((uint32_t)0x00000020) /*!< Write protection of Sector17 */
  162. #define OB_WRP_Sector_18 ((uint32_t)0x00000040) /*!< Write protection of Sector18 */
  163. #define OB_WRP_Sector_19 ((uint32_t)0x00000080) /*!< Write protection of Sector19 */
  164. #define OB_WRP_Sector_20 ((uint32_t)0x00000100) /*!< Write protection of Sector20 */
  165. #define OB_WRP_Sector_21 ((uint32_t)0x00000200) /*!< Write protection of Sector21 */
  166. #define OB_WRP_Sector_22 ((uint32_t)0x00000400) /*!< Write protection of Sector22 */
  167. #define OB_WRP_Sector_23 ((uint32_t)0x00000800) /*!< Write protection of Sector23 */
  168. #define OB_WRP_Sector_All ((uint32_t)0x00000FFF) /*!< Write protection of all Sectors */
  169. #define IS_OB_WRP(SECTOR)((((SECTOR) & (uint32_t)0xFFFFF000) == 0x00000000) && ((SECTOR) != 0x00000000))
  170. /**
  171. * @}
  172. */
  173. /** @defgroup FLASH_Option_Bytes_Read_Protection
  174. * @{
  175. */
  176. #define OB_RDP_Level_0 ((uint8_t)0xAA)
  177. #define OB_RDP_Level_1 ((uint8_t)0x55)
  178. /*#define OB_RDP_Level_2 ((uint8_t)0xCC)*/ /*!< Warning: When enabling read protection level 2
  179. it's no more possible to go back to level 1 or 0 */
  180. #define IS_OB_RDP(LEVEL) (((LEVEL) == OB_RDP_Level_0)||\
  181. ((LEVEL) == OB_RDP_Level_1))/*||\
  182. ((LEVEL) == OB_RDP_Level_2))*/
  183. /**
  184. * @}
  185. */
  186. /** @defgroup FLASH_Option_Bytes_IWatchdog
  187. * @{
  188. */
  189. #define OB_IWDG_SW ((uint8_t)0x20) /*!< Software IWDG selected */
  190. #define OB_IWDG_HW ((uint8_t)0x00) /*!< Hardware IWDG selected */
  191. #define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))
  192. /**
  193. * @}
  194. */
  195. /** @defgroup FLASH_Option_Bytes_nRST_STOP
  196. * @{
  197. */
  198. #define OB_STOP_NoRST ((uint8_t)0x40) /*!< No reset generated when entering in STOP */
  199. #define OB_STOP_RST ((uint8_t)0x00) /*!< Reset generated when entering in STOP */
  200. #define IS_OB_STOP_SOURCE(SOURCE) (((SOURCE) == OB_STOP_NoRST) || ((SOURCE) == OB_STOP_RST))
  201. /**
  202. * @}
  203. */
  204. /** @defgroup FLASH_Option_Bytes_nRST_STDBY
  205. * @{
  206. */
  207. #define OB_STDBY_NoRST ((uint8_t)0x80) /*!< No reset generated when entering in STANDBY */
  208. #define OB_STDBY_RST ((uint8_t)0x00) /*!< Reset generated when entering in STANDBY */
  209. #define IS_OB_STDBY_SOURCE(SOURCE) (((SOURCE) == OB_STDBY_NoRST) || ((SOURCE) == OB_STDBY_RST))
  210. /**
  211. * @}
  212. */
  213. /** @defgroup FLASH_BOR_Reset_Level
  214. * @{
  215. */
  216. #define OB_BOR_LEVEL3 ((uint8_t)0x00) /*!< Supply voltage ranges from 2.70 to 3.60 V */
  217. #define OB_BOR_LEVEL2 ((uint8_t)0x04) /*!< Supply voltage ranges from 2.40 to 2.70 V */
  218. #define OB_BOR_LEVEL1 ((uint8_t)0x08) /*!< Supply voltage ranges from 2.10 to 2.40 V */
  219. #define OB_BOR_OFF ((uint8_t)0x0C) /*!< Supply voltage ranges from 1.62 to 2.10 V */
  220. #define IS_OB_BOR(LEVEL) (((LEVEL) == OB_BOR_LEVEL1) || ((LEVEL) == OB_BOR_LEVEL2) ||\
  221. ((LEVEL) == OB_BOR_LEVEL3) || ((LEVEL) == OB_BOR_OFF))
  222. /**
  223. * @}
  224. */
  225. /** @defgroup FLASH_Interrupts
  226. * @{
  227. */
  228. #define FLASH_IT_EOP ((uint32_t)0x01000000) /*!< End of FLASH Operation Interrupt source */
  229. #define FLASH_IT_ERR ((uint32_t)0x02000000) /*!< Error Interrupt source */
  230. #define IS_FLASH_IT(IT) ((((IT) & (uint32_t)0xFCFFFFFF) == 0x00000000) && ((IT) != 0x00000000))
  231. /**
  232. * @}
  233. */
  234. /** @defgroup FLASH_Flags
  235. * @{
  236. */
  237. #define FLASH_FLAG_EOP ((uint32_t)0x00000001) /*!< FLASH End of Operation flag */
  238. #define FLASH_FLAG_OPERR ((uint32_t)0x00000002) /*!< FLASH operation Error flag */
  239. #define FLASH_FLAG_WRPERR ((uint32_t)0x00000010) /*!< FLASH Write protected error flag */
  240. #define FLASH_FLAG_PGAERR ((uint32_t)0x00000020) /*!< FLASH Programming Alignment error flag */
  241. #define FLASH_FLAG_PGPERR ((uint32_t)0x00000040) /*!< FLASH Programming Parallelism error flag */
  242. #define FLASH_FLAG_PGSERR ((uint32_t)0x00000080) /*!< FLASH Programming Sequence error flag */
  243. #define FLASH_FLAG_BSY ((uint32_t)0x00010000) /*!< FLASH Busy flag */
  244. #define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFFFE0C) == 0x00000000) && ((FLAG) != 0x00000000))
  245. #define IS_FLASH_GET_FLAG(FLAG) (((FLAG) == FLASH_FLAG_EOP) || ((FLAG) == FLASH_FLAG_OPERR) || \
  246. ((FLAG) == FLASH_FLAG_WRPERR) || ((FLAG) == FLASH_FLAG_PGAERR) || \
  247. ((FLAG) == FLASH_FLAG_PGPERR) || ((FLAG) == FLASH_FLAG_PGSERR) || \
  248. ((FLAG) == FLASH_FLAG_BSY))
  249. /**
  250. * @}
  251. */
  252. /** @defgroup FLASH_Program_Parallelism
  253. * @{
  254. */
  255. #define FLASH_PSIZE_BYTE ((uint32_t)0x00000000)
  256. #define FLASH_PSIZE_HALF_WORD ((uint32_t)0x00000100)
  257. #define FLASH_PSIZE_WORD ((uint32_t)0x00000200)
  258. #define FLASH_PSIZE_DOUBLE_WORD ((uint32_t)0x00000300)
  259. #define CR_PSIZE_MASK ((uint32_t)0xFFFFFCFF)
  260. /**
  261. * @}
  262. */
  263. /** @defgroup FLASH_Keys
  264. * @{
  265. */
  266. #define RDP_KEY ((uint16_t)0x00A5)
  267. #define FLASH_KEY1 ((uint32_t)0x45670123)
  268. #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
  269. #define FLASH_OPT_KEY1 ((uint32_t)0x08192A3B)
  270. #define FLASH_OPT_KEY2 ((uint32_t)0x4C5D6E7F)
  271. /**
  272. * @}
  273. */
  274. /**
  275. * @brief ACR register byte 0 (Bits[7:0]) base address
  276. */
  277. #define ACR_BYTE0_ADDRESS ((uint32_t)0x40023C00)
  278. /**
  279. * @brief OPTCR register byte 0 (Bits[7:0]) base address
  280. */
  281. #define OPTCR_BYTE0_ADDRESS ((uint32_t)0x40023C14)
  282. /**
  283. * @brief OPTCR register byte 1 (Bits[15:8]) base address
  284. */
  285. #define OPTCR_BYTE1_ADDRESS ((uint32_t)0x40023C15)
  286. /**
  287. * @brief OPTCR register byte 2 (Bits[23:16]) base address
  288. */
  289. #define OPTCR_BYTE2_ADDRESS ((uint32_t)0x40023C16)
  290. /**
  291. * @brief OPTCR register byte 3 (Bits[31:24]) base address
  292. */
  293. #define OPTCR_BYTE3_ADDRESS ((uint32_t)0x40023C17)
  294. /**
  295. * @brief OPTCR1 register byte 0 (Bits[7:0]) base address
  296. */
  297. #define OPTCR1_BYTE2_ADDRESS ((uint32_t)0x40023C1A)
  298. /**
  299. * @}
  300. */
  301. /* Exported macro ------------------------------------------------------------*/
  302. /* Exported functions --------------------------------------------------------*/
  303. /* FLASH Interface configuration functions ************************************/
  304. void FLASH_SetLatency(uint32_t FLASH_Latency);
  305. void FLASH_PrefetchBufferCmd(FunctionalState NewState);
  306. void FLASH_InstructionCacheCmd(FunctionalState NewState);
  307. void FLASH_DataCacheCmd(FunctionalState NewState);
  308. void FLASH_InstructionCacheReset(void);
  309. void FLASH_DataCacheReset(void);
  310. /* FLASH Memory Programming functions *****************************************/
  311. void FLASH_Unlock(void);
  312. void FLASH_Lock(void);
  313. FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange);
  314. FLASH_Status FLASH_EraseAllSectors(uint8_t VoltageRange);
  315. FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data);
  316. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
  317. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
  318. FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data);
  319. /* Option Bytes Programming functions *****************************************/
  320. void FLASH_OB_Unlock(void);
  321. void FLASH_OB_Lock(void);
  322. void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
  323. void FLASH_OB_WRP1Config(uint32_t OB_WRP, FunctionalState NewState);
  324. void FLASH_OB_RDPConfig(uint8_t OB_RDP);
  325. void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
  326. void FLASH_OB_BORConfig(uint8_t OB_BOR);
  327. FLASH_Status FLASH_OB_Launch(void);
  328. uint8_t FLASH_OB_GetUser(void);
  329. uint16_t FLASH_OB_GetWRP(void);
  330. uint16_t FLASH_OB_GetWRP1(void);
  331. FlagStatus FLASH_OB_GetRDP(void);
  332. uint8_t FLASH_OB_GetBOR(void);
  333. /* Interrupts and flags management functions **********************************/
  334. void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState);
  335. FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG);
  336. void FLASH_ClearFlag(uint32_t FLASH_FLAG);
  337. FLASH_Status FLASH_GetStatus(void);
  338. FLASH_Status FLASH_WaitForLastOperation(void);
  339. #ifdef __cplusplus
  340. }
  341. #endif
  342. #endif /* __STM32F4xx_FLASH_H */
  343. /**
  344. * @}
  345. */
  346. /**
  347. * @}
  348. */
  349. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/