ft32f0xx_flash.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_flash.h
  4. * @author FMD AE
  5. * @brief This file contains all the functions prototypes for the FLASH
  6. * firmware library.
  7. * @version V1.0.0
  8. * @data 2021-07-01
  9. ******************************************************************************
  10. */
  11. /* Define to prevent recursive inclusion -------------------------------------*/
  12. #ifndef __FT32F0XX_FLASH_H
  13. #define __FT32F0XX_FLASH_H
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /* Includes ------------------------------------------------------------------*/
  18. #include "ft32f0xx.h"
  19. /** @addtogroup FLASH
  20. * @{
  21. */
  22. /* Exported types ------------------------------------------------------------*/
  23. /**
  24. * @brief FLASH Status
  25. */
  26. typedef enum
  27. {
  28. FLASH_BUSY = 1,
  29. FLASH_ERROR_WRP,
  30. FLASH_ERROR_PROGRAM,
  31. FLASH_COMPLETE,
  32. FLASH_TIMEOUT
  33. }FLASH_Status;
  34. /* Exported constants --------------------------------------------------------*/
  35. /** @defgroup FLASH_Exported_Constants
  36. * @{
  37. */
  38. /** @defgroup FLASH_Latency
  39. * @{
  40. */
  41. #define FLASH_Latency_0 ((uint32_t)0x00000000) /*!< FLASH Zero Latency cycle */
  42. #define FLASH_Latency_1 ((uint32_t)0x00000001) /*!< FLASH One Latency cycle */
  43. #define FLASH_Latency_2 ((uint32_t)0x00000002)
  44. #define FLASH_Latency_3 ((uint32_t)0x00000003)
  45. #define FLASH_Latency_4 ((uint32_t)0x00000004)
  46. #define FLASH_Latency_5 ((uint32_t)0x00000005)
  47. #define FLASH_Latency_6 ((uint32_t)0x00000006)
  48. #define FLASH_Latency_7 ((uint32_t)0x00000007)
  49. #define FLASH_Latency_8 ((uint32_t)0x00000008)
  50. #define FLASH_Latency_9 ((uint32_t)0x00000009)
  51. #define FLASH_Latency_10 ((uint32_t)0x0000000a)
  52. #define FLASH_Latency_11 ((uint32_t)0x0000000b)
  53. #define FLASH_Latency_12 ((uint32_t)0x0000000c)
  54. #define FLASH_Latency_13 ((uint32_t)0x0000000d)
  55. #define FLASH_Latency_14 ((uint32_t)0x0000000e)
  56. #define FLASH_Latency_15 ((uint32_t)0x0000000f)
  57. #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_Latency_0) || \
  58. ((LATENCY) == FLASH_Latency_1) || \
  59. ((LATENCY) == FLASH_Latency_2 ) || \
  60. ((LATENCY) == FLASH_Latency_3 ) || \
  61. ((LATENCY) == FLASH_Latency_4 ) || \
  62. ((LATENCY) == FLASH_Latency_5 ) || \
  63. ((LATENCY) == FLASH_Latency_6 ) || \
  64. ((LATENCY) == FLASH_Latency_7 ) || \
  65. ((LATENCY) == FLASH_Latency_8 ) || \
  66. ((LATENCY) == FLASH_Latency_9 ) || \
  67. ((LATENCY) == FLASH_Latency_10) || \
  68. ((LATENCY) == FLASH_Latency_11) || \
  69. ((LATENCY) == FLASH_Latency_12) || \
  70. ((LATENCY) == FLASH_Latency_13) || \
  71. ((LATENCY) == FLASH_Latency_14) || \
  72. ((LATENCY) == FLASH_Latency_15))
  73. /**
  74. * @}
  75. */
  76. /** @defgroup FLASH_Interrupts
  77. * @{
  78. */
  79. #define FLASH_IT_EOP FLASH_CR_EOPIE /*!< End of programming interrupt source */
  80. #define FLASH_IT_ERR FLASH_CR_ERRIE /*!< Error interrupt source */
  81. #define IS_FLASH_IT(IT) ((((IT) & (uint32_t)0xFFFFEBFF) == 0x00000000) && (((IT) != 0x00000000)))
  82. /**
  83. * @}
  84. */
  85. /** @defgroup FLASH_Address
  86. * @{
  87. */
  88. #if defined(FT32F030X8) /*64K devices */
  89. #define IS_FLASH_PROGRAM_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) <= 0x0800FFFF))
  90. #elif defined (FT32F072xB) /*128K devices */
  91. #define IS_FLASH_PROGRAM_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) <= 0x0801FFFF))
  92. #endif
  93. /**
  94. * @}
  95. */
  96. /** @defgroup FLASH_OB_DATA_ADDRESS
  97. * @{
  98. */
  99. #define IS_OB_DATA_ADDRESS(ADDRESS) (((ADDRESS) == 0x1FFFF804))
  100. /**
  101. * @}
  102. */
  103. /** @defgroup FLASH_Option_Bytes_Write_Protection
  104. * @{
  105. */
  106. #define OB_WRP_Pages0to7 ((uint32_t)0x00000001) /* Write protection of page 0 to 7 */
  107. #define OB_WRP_Pages8to15 ((uint32_t)0x00000002) /* Write protection of page 8 to 15 */
  108. #define OB_WRP_Pages16to23 ((uint32_t)0x00000004) /* Write protection of page 16 to 23 */
  109. #define OB_WRP_Pages24to31 ((uint32_t)0x00000008) /* Write protection of page 24 to 31 */
  110. #define OB_WRP_Pages32to39 ((uint32_t)0x00000010) /* Write protection of page 32 to 39 */
  111. #define OB_WRP_Pages40to47 ((uint32_t)0x00000020) /* Write protection of page 40 to 47 */
  112. #define OB_WRP_Pages48to55 ((uint32_t)0x00000040) /* Write protection of page 48 to 55 */
  113. #define OB_WRP_Pages56to63 ((uint32_t)0x00000080) /* Write protection of page 56 to 63 */
  114. #define OB_WRP_Pages64to71 ((uint32_t)0x00000100) /* Write protection of page 64 to 71 */
  115. #define OB_WRP_Pages72to79 ((uint32_t)0x00000200) /* Write protection of page 72 to 79 */
  116. #define OB_WRP_Pages80to87 ((uint32_t)0x00000400) /* Write protection of page 80 to 87 */
  117. #define OB_WRP_Pages88to95 ((uint32_t)0x00000800) /* Write protection of page 88 to 95 */
  118. #define OB_WRP_Pages96to103 ((uint32_t)0x00001000) /* Write protection of page 96 to 103 */
  119. #define OB_WRP_Pages104to111 ((uint32_t)0x00002000) /* Write protection of page 104 to 111 */
  120. #define OB_WRP_Pages112to119 ((uint32_t)0x00004000) /* Write protection of page 112 to 119 */
  121. #define OB_WRP_Pages120to127 ((uint32_t)0x00008000) /* Write protection of page 120 to 127 */
  122. #define OB_WRP_AllPages ((uint32_t)0x0000FFFF) /*!< Write protection of all Sectors */
  123. #define IS_OB_WRP(PAGE) (((PAGE) != 0x0000000))
  124. /**
  125. * @}
  126. */
  127. /** @defgroup FLASH_Option_Bytes_Read_Protection
  128. * @{
  129. */
  130. /**
  131. * @brief FLASH_Read Protection Level
  132. */
  133. #define OB_RDP_Level_0 ((uint8_t)0xAA)
  134. #define OB_RDP_Level_1 ((uint8_t)0xBB)
  135. /*#define OB_RDP_Level_2 ((uint8_t)0xCC)*/ /* Warning: When enabling read protection level 2
  136. it's no more possible to go back to level 1 or 0 */
  137. #define IS_OB_RDP(LEVEL) (((LEVEL) == OB_RDP_Level_0)||\
  138. ((LEVEL) == OB_RDP_Level_1))/*||\
  139. ((LEVEL) == OB_RDP_Level_2))*/
  140. /**
  141. * @}
  142. */
  143. /** @defgroup FLASH_Option_Bytes_IWatchdog
  144. * @{
  145. */
  146. #if defined (FT32F072xB)
  147. #define OB_IWDG_SW ((uint8_t)0x01) /*!< Software IWDG selected */
  148. #define OB_IWDG_HW ((uint8_t)0x00) /*!< Hardware IWDG selected */
  149. #else
  150. #define OB_IWDG_SW ((uint8_t)0x00) /*!< Software IWDG selected */
  151. #define OB_IWDG_HW ((uint8_t)0x01) /*!< Hardware IWDG selected */
  152. #endif
  153. #define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))
  154. /**
  155. * @}
  156. */
  157. /** @defgroup FLASH_Option_Bytes_nRST_STOP
  158. * @{
  159. */
  160. #define OB_STOP_NoRST ((uint8_t)0x02) /*!< No reset generated when entering in STOP */
  161. #define OB_STOP_RST ((uint8_t)0x00) /*!< Reset generated when entering in STOP */
  162. #define IS_OB_STOP_SOURCE(SOURCE) (((SOURCE) == OB_STOP_NoRST) || ((SOURCE) == OB_STOP_RST))
  163. /**
  164. * @}
  165. */
  166. /** @defgroup FLASH_Option_Bytes_nRST_STDBY
  167. * @{
  168. */
  169. #define OB_STDBY_NoRST ((uint8_t)0x04) /*!< No reset generated when entering in STANDBY */
  170. #define OB_STDBY_RST ((uint8_t)0x00) /*!< Reset generated when entering in STANDBY */
  171. #define IS_OB_STDBY_SOURCE(SOURCE) (((SOURCE) == OB_STDBY_NoRST) || ((SOURCE) == OB_STDBY_RST))
  172. /**
  173. * @}
  174. */
  175. /** @defgroup FLASH_Option_Bytes_BOOT1
  176. * @{
  177. */
  178. #define OB_BOOT1_RESET ((uint8_t)0x00) /*!< BOOT1 Reset */
  179. #define OB_BOOT1_SET ((uint8_t)0x10) /*!< BOOT1 Set */
  180. #define IS_OB_BOOT1(BOOT1) (((BOOT1) == OB_BOOT1_RESET) || ((BOOT1) == OB_BOOT1_SET))
  181. /**
  182. * @}
  183. */
  184. /**
  185. * @}
  186. */
  187. /**
  188. * @}
  189. */
  190. /** @defgroup FLASH_Option_Bytes_VDDA_Analog_Monitoring
  191. * @{
  192. */
  193. #define OB_VDDA_ANALOG_ON ((uint8_t)0x20) /*!< Analog monitoring on VDDA Power source ON */
  194. #define OB_VDDA_ANALOG_OFF ((uint8_t)0x00) /*!< Analog monitoring on VDDA Power source OFF */
  195. #define IS_OB_VDDA_ANALOG(ANALOG) (((ANALOG) == OB_VDDA_ANALOG_ON) || ((ANALOG) == OB_VDDA_ANALOG_OFF))
  196. /**
  197. * @}
  198. */
  199. /** @defgroup FLASH_Option_Bytes_SRAM_Parity_Enable
  200. * @{
  201. */
  202. #define OB_SRAM_PARITY_SET ((uint8_t)0x00) /*!< SRAM parity enable Set */
  203. #define OB_SRAM_PARITY_RESET ((uint8_t)0x40) /*!< SRAM parity enable reset */
  204. #define IS_OB_SRAM_PARITY(PARITY) (((PARITY) == OB_SRAM_PARITY_SET) || ((PARITY) == OB_SRAM_PARITY_RESET))
  205. /**
  206. * @}
  207. */
  208. /** @defgroup FLASH_Flags
  209. * @{
  210. */
  211. #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Busy flag */
  212. #define FLASH_FLAG_PGERR FLASH_SR_PGERR /*!< FLASH Programming error flag */
  213. #define FLASH_FLAG_WRPERR FLASH_SR_WRPERR /*!< FLASH Write protected error flag */
  214. #define FLASH_FLAG_EOP FLASH_SR_EOP /*!< FLASH End of Programming flag */
  215. #define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFFFFCB) == 0x00000000) && ((FLAG) != 0x00000000))
  216. #define IS_FLASH_GET_FLAG(FLAG) (((FLAG) == FLASH_FLAG_BSY) || ((FLAG) == FLASH_FLAG_PGERR) || \
  217. ((FLAG) == FLASH_FLAG_WRPERR) || ((FLAG) == FLASH_FLAG_EOP))
  218. /**
  219. * @}
  220. */
  221. /** @defgroup FLASH_Timeout_definition
  222. * @{
  223. */
  224. #define FLASH_ER_PRG_TIMEOUT ((uint32_t)0x000B0000)
  225. /**
  226. * @}
  227. */
  228. /** @defgroup FLASH_Legacy
  229. * @{
  230. */
  231. #define FLASH_WRProt_Pages0to7 OB_WRP_Pages0to7
  232. #define FLASH_WRProt_Pages8to15 OB_WRP_Pages8to15
  233. #define FLASH_WRProt_Pages16to23 OB_WRP_Pages16to23
  234. #define FLASH_WRProt_Pages24to31 OB_WRP_Pages24to31
  235. #define FLASH_WRProt_Pages32to39 OB_WRP_Pages32to39
  236. #define FLASH_WRProt_Pages40to47 OB_WRP_Pages40to47
  237. #define FLASH_WRProt_Pages48to55 OB_WRP_Pages48to55
  238. #define FLASH_WRProt_Pages56to63 OB_WRP_Pages56to63
  239. #define FLASH_WRProt_Pages64to71 OB_WRP_Pages64to71
  240. #define FLASH_WRProt_Pages72to79 OB_WRP_Pages72to79
  241. #define FLASH_WRProt_Pages80to87 OB_WRP_Pages80to87
  242. #define FLASH_WRProt_Pages88to95 OB_WRP_Pages88to95
  243. #define FLASH_WRProt_Pages96to103 OB_WRP_Pages96to103
  244. #define FLASH_WRProt_Pages104to111 OB_WRP_Pages104to111
  245. #define FLASH_WRProt_Pages112to119 OB_WRP_Pages112to119
  246. #define FLASH_WRProt_Pages120to127 OB_WRP_Pages120to127
  247. #define FLASH_WRProt_AllPages OB_WRP_AllPages
  248. /**
  249. * @}
  250. */
  251. /**
  252. * @}
  253. */
  254. /* Exported macro ------------------------------------------------------------*/
  255. /* Exported functions ------------------------------------------------------- */
  256. /**
  257. * @brief FLASH memory functions that can be executed from FLASH.
  258. */
  259. /* FLASH Interface configuration functions ************************************/
  260. void FLASH_SetLatency(uint32_t FLASH_Latency);
  261. void FLASH_PrefetchBufferCmd(FunctionalState NewState);
  262. FlagStatus FLASH_GetPrefetchBufferStatus(void);
  263. /* FLASH Memory Programming functions *****************************************/
  264. void FLASH_Unlock(void);
  265. void FLASH_Lock(void);
  266. FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
  267. FLASH_Status FLASH_EraseAllPages(void);
  268. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
  269. #if defined(FT32F072xB)
  270. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
  271. #endif
  272. /* FLASH Option Bytes Programming functions *****************************************/
  273. void FLASH_OB_Unlock(void);
  274. void FLASH_OB_Lock(void);
  275. void FLASH_OB_Launch(void);
  276. FLASH_Status FLASH_OB_Erase(void);
  277. FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP);
  278. FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);
  279. FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
  280. FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
  281. FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
  282. FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity);
  283. FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
  284. #if defined(FT32F072xB)
  285. FLASH_Status FLASH_OB_ProgramData(uint32_t Address, uint8_t Data);
  286. #else
  287. FLASH_Status FLASH_OB_ProgramData(uint32_t Address, uint32_t Data);
  288. #endif
  289. uint8_t FLASH_OB_GetUser(void);
  290. uint32_t FLASH_OB_GetWRP(void);
  291. FlagStatus FLASH_OB_GetRDP(void);
  292. /* FLASH Interrupts and flags management functions **********************************/
  293. void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState);
  294. FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG);
  295. void FLASH_ClearFlag(uint32_t FLASH_FLAG);
  296. FLASH_Status FLASH_GetStatus(void);
  297. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout);
  298. /** @defgroup FLASH_Legacy
  299. * @{
  300. */
  301. #define FLASH_EraseOptionBytes FLASH_OB_Erase
  302. #define FLASH_EnableWriteProtection FLASH_OB_EnableWRP
  303. #define FLASH_UserOptionByteConfig FLASH_OB_UserConfig
  304. #define FLASH_ProgramOption4ByteData FLASH_OB_ProgramData
  305. #define FLASH_GetUserOptionByte FLASH_OB_GetUser
  306. #define FLASH_GetWriteProtectionOptionByte FLASH_OB_GetWRP
  307. /**
  308. * @}
  309. */
  310. #ifdef __cplusplus
  311. }
  312. #endif
  313. #endif /* __FT32F0XX_FLASH_H */
  314. /**
  315. * @}
  316. */
  317. /**
  318. * @}
  319. */
  320. /************************ (C) COPYRIGHT FMD *****END OF FILE****/