stm32f2xx_hal_flash_ex.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_hal_flash_ex.h
  4. * @author MCD Application Team
  5. * @brief Header file of FLASH HAL Extension module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Define to prevent recursive inclusion -------------------------------------*/
  20. #ifndef __STM32F2xx_HAL_FLASH_EX_H
  21. #define __STM32F2xx_HAL_FLASH_EX_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32f2xx_hal_def.h"
  27. /** @addtogroup STM32F2xx_HAL_Driver
  28. * @{
  29. */
  30. /** @addtogroup FLASHEx
  31. * @{
  32. */
  33. /* Exported types ------------------------------------------------------------*/
  34. /** @defgroup FLASHEx_Exported_Types FLASH Exported Types
  35. * @{
  36. */
  37. /**
  38. * @brief FLASH Erase structure definition
  39. */
  40. typedef struct
  41. {
  42. uint32_t TypeErase; /*!< Mass erase or sector Erase.
  43. This parameter can be a value of @ref FLASHEx_Type_Erase */
  44. uint32_t Banks; /*!< Select banks to erase when Mass erase is enabled.
  45. This parameter must be a value of @ref FLASHEx_Banks */
  46. uint32_t Sector; /*!< Initial FLASH sector to erase when Mass erase is disabled
  47. This parameter must be a value of @ref FLASHEx_Sectors */
  48. uint32_t NbSectors; /*!< Number of sectors to be erased.
  49. This parameter must be a value between 1 and (max number of sectors - value of Initial sector)*/
  50. uint32_t VoltageRange;/*!< The device voltage range which defines the erase parallelism
  51. This parameter must be a value of @ref FLASHEx_Voltage_Range */
  52. } FLASH_EraseInitTypeDef;
  53. /**
  54. * @brief FLASH Option Bytes Program structure definition
  55. */
  56. typedef struct
  57. {
  58. uint32_t OptionType; /*!< Option byte to be configured.
  59. This parameter can be a value of @ref FLASHEx_Option_Type */
  60. uint32_t WRPState; /*!< Write protection activation or deactivation.
  61. This parameter can be a value of @ref FLASHEx_WRP_State */
  62. uint32_t WRPSector; /*!< Specifies the sector(s) to be write protected.
  63. The value of this parameter depend on device used within the same series */
  64. uint32_t Banks; /*!< Select banks for WRP activation/deactivation of all sectors.
  65. This parameter must be a value of @ref FLASHEx_Banks */
  66. uint32_t RDPLevel; /*!< Set the read protection level.
  67. This parameter can be a value of @ref FLASHEx_Option_Bytes_Read_Protection */
  68. uint32_t BORLevel; /*!< Set the BOR Level.
  69. This parameter can be a value of @ref FLASHEx_BOR_Reset_Level */
  70. uint8_t USERConfig; /*!< Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY. */
  71. } FLASH_OBProgramInitTypeDef;
  72. /* Exported constants --------------------------------------------------------*/
  73. /** @defgroup FLASHEx_Exported_Constants FLASH Exported Constants
  74. * @{
  75. */
  76. /** @defgroup FLASHEx_Type_Erase FLASH Type Erase
  77. * @{
  78. */
  79. #define FLASH_TYPEERASE_SECTORS 0x00000000U /*!< Sectors erase only */
  80. #define FLASH_TYPEERASE_MASSERASE 0x00000001U /*!< Flash Mass erase activation */
  81. /**
  82. * @}
  83. */
  84. /** @defgroup FLASHEx_Voltage_Range FLASH Voltage Range
  85. * @{
  86. */
  87. #define FLASH_VOLTAGE_RANGE_1 0x00000000U /*!< Device operating range: 1.8V to 2.1V */
  88. #define FLASH_VOLTAGE_RANGE_2 0x00000001U /*!< Device operating range: 2.1V to 2.7V */
  89. #define FLASH_VOLTAGE_RANGE_3 0x00000002U /*!< Device operating range: 2.7V to 3.6V */
  90. #define FLASH_VOLTAGE_RANGE_4 0x00000003U /*!< Device operating range: 2.7V to 3.6V + External Vpp */
  91. /**
  92. * @}
  93. */
  94. /** @defgroup FLASHEx_WRP_State FLASH WRP State
  95. * @{
  96. */
  97. #define OB_WRPSTATE_DISABLE 0x00000000U /*!< Disable the write protection of the desired bank 1 sectors */
  98. #define OB_WRPSTATE_ENABLE 0x00000001U /*!< Enable the write protection of the desired bank 1 sectors */
  99. /**
  100. * @}
  101. */
  102. /** @defgroup FLASHEx_Option_Type FLASH Option Type
  103. * @{
  104. */
  105. #define OPTIONBYTE_WRP 0x00000001U /*!< WRP option byte configuration */
  106. #define OPTIONBYTE_RDP 0x00000002U /*!< RDP option byte configuration */
  107. #define OPTIONBYTE_USER 0x00000004U /*!< USER option byte configuration */
  108. #define OPTIONBYTE_BOR 0x00000008U /*!< BOR option byte configuration */
  109. /**
  110. * @}
  111. */
  112. /** @defgroup FLASHEx_Option_Bytes_Read_Protection FLASH Option Bytes Read Protection
  113. * @{
  114. */
  115. #define OB_RDP_LEVEL_0 ((uint8_t)0xAA)
  116. #define OB_RDP_LEVEL_1 ((uint8_t)0x55)
  117. #define OB_RDP_LEVEL_2 ((uint8_t)0xCC) /*!< Warning: When enabling read protection level 2
  118. it s no more possible to go back to level 1 or 0 */
  119. /**
  120. * @}
  121. */
  122. /** @defgroup FLASHEx_Option_Bytes_IWatchdog FLASH Option Bytes IWatchdog
  123. * @{
  124. */
  125. #define OB_IWDG_SW ((uint8_t)0x20) /*!< Software IWDG selected */
  126. #define OB_IWDG_HW ((uint8_t)0x00) /*!< Hardware IWDG selected */
  127. /**
  128. * @}
  129. */
  130. /** @defgroup FLASHEx_Option_Bytes_nRST_STOP FLASH Option Bytes nRST_STOP
  131. * @{
  132. */
  133. #define OB_STOP_NO_RST ((uint8_t)0x40) /*!< No reset generated when entering in STOP */
  134. #define OB_STOP_RST ((uint8_t)0x00) /*!< Reset generated when entering in STOP */
  135. /**
  136. * @}
  137. */
  138. /** @defgroup FLASHEx_Option_Bytes_nRST_STDBY FLASH Option Bytes nRST_STDBY
  139. * @{
  140. */
  141. #define OB_STDBY_NO_RST ((uint8_t)0x80) /*!< No reset generated when entering in STANDBY */
  142. #define OB_STDBY_RST ((uint8_t)0x00) /*!< Reset generated when entering in STANDBY */
  143. /**
  144. * @}
  145. */
  146. /** @defgroup FLASHEx_BOR_Reset_Level FLASH BOR Reset Level
  147. * @{
  148. */
  149. #define OB_BOR_LEVEL3 ((uint8_t)0x00) /*!< Supply voltage ranges from 2.70 to 3.60 V */
  150. #define OB_BOR_LEVEL2 ((uint8_t)0x04) /*!< Supply voltage ranges from 2.40 to 2.70 V */
  151. #define OB_BOR_LEVEL1 ((uint8_t)0x08) /*!< Supply voltage ranges from 2.10 to 2.40 V */
  152. #define OB_BOR_OFF ((uint8_t)0x0C) /*!< Supply voltage ranges from 1.62 to 2.10 V */
  153. /**
  154. * @}
  155. */
  156. /**
  157. * @}
  158. */
  159. /** @defgroup FLASH_Latency FLASH Latency
  160. * @{
  161. */
  162. #define FLASH_LATENCY_0 FLASH_ACR_LATENCY_0WS /*!< FLASH Zero Latency cycle */
  163. #define FLASH_LATENCY_1 FLASH_ACR_LATENCY_1WS /*!< FLASH One Latency cycle */
  164. #define FLASH_LATENCY_2 FLASH_ACR_LATENCY_2WS /*!< FLASH Two Latency cycles */
  165. #define FLASH_LATENCY_3 FLASH_ACR_LATENCY_3WS /*!< FLASH Three Latency cycles */
  166. #define FLASH_LATENCY_4 FLASH_ACR_LATENCY_4WS /*!< FLASH Four Latency cycles */
  167. #define FLASH_LATENCY_5 FLASH_ACR_LATENCY_5WS /*!< FLASH Five Latency cycles */
  168. #define FLASH_LATENCY_6 FLASH_ACR_LATENCY_6WS /*!< FLASH Six Latency cycles */
  169. #define FLASH_LATENCY_7 FLASH_ACR_LATENCY_7WS /*!< FLASH Seven Latency cycles */
  170. /**
  171. * @}
  172. */
  173. /** @defgroup FLASHEx_Banks FLASH Banks
  174. * @{
  175. */
  176. #define FLASH_BANK_1 1U /*!< Bank 1 */
  177. /**
  178. * @}
  179. */
  180. /** @defgroup FLASHEx_MassErase_bit FLASH Mass Erase bit
  181. * @{
  182. */
  183. #define FLASH_MER_BIT FLASH_CR_MER /*!< only 1 MER Bit */
  184. /**
  185. * @}
  186. */
  187. /** @defgroup FLASHEx_Sectors FLASH Sectors
  188. * @{
  189. */
  190. #define FLASH_SECTOR_0 0U /*!< Sector Number 0 */
  191. #define FLASH_SECTOR_1 1U /*!< Sector Number 1 */
  192. #define FLASH_SECTOR_2 2U /*!< Sector Number 2 */
  193. #define FLASH_SECTOR_3 3U /*!< Sector Number 3 */
  194. #define FLASH_SECTOR_4 4U /*!< Sector Number 4 */
  195. #define FLASH_SECTOR_5 5U /*!< Sector Number 5 */
  196. #define FLASH_SECTOR_6 6U /*!< Sector Number 6 */
  197. #define FLASH_SECTOR_7 7U /*!< Sector Number 7 */
  198. #define FLASH_SECTOR_8 8U /*!< Sector Number 8 */
  199. #define FLASH_SECTOR_9 9U /*!< Sector Number 9 */
  200. #define FLASH_SECTOR_10 10U /*!< Sector Number 10 */
  201. #define FLASH_SECTOR_11 11U /*!< Sector Number 11 */
  202. /**
  203. * @}
  204. */
  205. /** @defgroup FLASHEx_Option_Bytes_Write_Protection FLASH Option Bytes Write Protection
  206. * @{
  207. */
  208. #define OB_WRP_SECTOR_0 0x00000001U /*!< Write protection of Sector0 */
  209. #define OB_WRP_SECTOR_1 0x00000002U /*!< Write protection of Sector1 */
  210. #define OB_WRP_SECTOR_2 0x00000004U /*!< Write protection of Sector2 */
  211. #define OB_WRP_SECTOR_3 0x00000008U /*!< Write protection of Sector3 */
  212. #define OB_WRP_SECTOR_4 0x00000010U /*!< Write protection of Sector4 */
  213. #define OB_WRP_SECTOR_5 0x00000020U /*!< Write protection of Sector5 */
  214. #define OB_WRP_SECTOR_6 0x00000040U /*!< Write protection of Sector6 */
  215. #define OB_WRP_SECTOR_7 0x00000080U /*!< Write protection of Sector7 */
  216. #define OB_WRP_SECTOR_8 0x00000100U /*!< Write protection of Sector8 */
  217. #define OB_WRP_SECTOR_9 0x00000200U /*!< Write protection of Sector9 */
  218. #define OB_WRP_SECTOR_10 0x00000400U /*!< Write protection of Sector10 */
  219. #define OB_WRP_SECTOR_11 0x00000800U /*!< Write protection of Sector11 */
  220. #define OB_WRP_SECTOR_All 0x00000FFFU /*!< Write protection of all Sectors */
  221. /**
  222. * @}
  223. */
  224. /**
  225. * @}
  226. */
  227. /* Exported macro ------------------------------------------------------------*/
  228. /* Exported functions --------------------------------------------------------*/
  229. /** @addtogroup FLASHEx_Exported_Functions
  230. * @{
  231. */
  232. /** @addtogroup FLASHEx_Exported_Functions_Group1
  233. * @{
  234. */
  235. /* Extension Program operation functions *************************************/
  236. HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError);
  237. HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit);
  238. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
  239. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit);
  240. /**
  241. * @}
  242. */
  243. /**
  244. * @}
  245. */
  246. /* Private types -------------------------------------------------------------*/
  247. /* Private variables ---------------------------------------------------------*/
  248. /** @defgroup FLASHEx_Private_Variables FLASH Private Variables
  249. * @{
  250. */
  251. /**
  252. * @}
  253. */
  254. /* Private constants ---------------------------------------------------------*/
  255. /** @defgroup FLASHEx_Private_Constants FLASH Private Constants
  256. * @{
  257. */
  258. #define FLASH_SECTOR_TOTAL 12U
  259. /**
  260. * @}
  261. */
  262. /* Private macros ------------------------------------------------------------*/
  263. /** @defgroup FLASHEx_Private_Macros FLASH Private Macros
  264. * @{
  265. */
  266. /** @defgroup FLASHEx_IS_FLASH_Definitions FLASH Private macros to check input parameters
  267. * @{
  268. */
  269. #define IS_FLASH_TYPEERASE(VALUE)(((VALUE) == FLASH_TYPEERASE_SECTORS) || \
  270. ((VALUE) == FLASH_TYPEERASE_MASSERASE))
  271. #define IS_VOLTAGERANGE(RANGE)(((RANGE) == FLASH_VOLTAGE_RANGE_1) || \
  272. ((RANGE) == FLASH_VOLTAGE_RANGE_2) || \
  273. ((RANGE) == FLASH_VOLTAGE_RANGE_3) || \
  274. ((RANGE) == FLASH_VOLTAGE_RANGE_4))
  275. #define IS_WRPSTATE(VALUE)(((VALUE) == OB_WRPSTATE_DISABLE) || \
  276. ((VALUE) == OB_WRPSTATE_ENABLE))
  277. #define IS_OPTIONBYTE(VALUE)(((VALUE) <= (OPTIONBYTE_WRP|OPTIONBYTE_RDP|OPTIONBYTE_USER|OPTIONBYTE_BOR)))
  278. #define IS_OB_RDP_LEVEL(LEVEL) (((LEVEL) == OB_RDP_LEVEL_0) ||\
  279. ((LEVEL) == OB_RDP_LEVEL_1) ||\
  280. ((LEVEL) == OB_RDP_LEVEL_2))
  281. #define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))
  282. #define IS_OB_STOP_SOURCE(SOURCE) (((SOURCE) == OB_STOP_NO_RST) || ((SOURCE) == OB_STOP_RST))
  283. #define IS_OB_STDBY_SOURCE(SOURCE) (((SOURCE) == OB_STDBY_NO_RST) || ((SOURCE) == OB_STDBY_RST))
  284. #define IS_OB_BOR_LEVEL(LEVEL) (((LEVEL) == OB_BOR_LEVEL1) || ((LEVEL) == OB_BOR_LEVEL2) ||\
  285. ((LEVEL) == OB_BOR_LEVEL3) || ((LEVEL) == OB_BOR_OFF))
  286. #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_LATENCY_0) || \
  287. ((LATENCY) == FLASH_LATENCY_1) || \
  288. ((LATENCY) == FLASH_LATENCY_2) || \
  289. ((LATENCY) == FLASH_LATENCY_3) || \
  290. ((LATENCY) == FLASH_LATENCY_4) || \
  291. ((LATENCY) == FLASH_LATENCY_5) || \
  292. ((LATENCY) == FLASH_LATENCY_6) || \
  293. ((LATENCY) == FLASH_LATENCY_7))
  294. #define IS_FLASH_BANK(BANK) (((BANK) == FLASH_BANK_1))
  295. #define IS_FLASH_SECTOR(SECTOR) (((SECTOR) == FLASH_SECTOR_0) || ((SECTOR) == FLASH_SECTOR_1) ||\
  296. ((SECTOR) == FLASH_SECTOR_2) || ((SECTOR) == FLASH_SECTOR_3) ||\
  297. ((SECTOR) == FLASH_SECTOR_4) || ((SECTOR) == FLASH_SECTOR_5) ||\
  298. ((SECTOR) == FLASH_SECTOR_6) || ((SECTOR) == FLASH_SECTOR_7) ||\
  299. ((SECTOR) == FLASH_SECTOR_8) || ((SECTOR) == FLASH_SECTOR_9) ||\
  300. ((SECTOR) == FLASH_SECTOR_10) || ((SECTOR) == FLASH_SECTOR_11))
  301. #define IS_FLASH_ADDRESS(ADDRESS) ((((ADDRESS) >= FLASH_BASE) && ((ADDRESS) <= FLASH_END)) || \
  302. (((ADDRESS) >= FLASH_OTP_BASE) && ((ADDRESS) <= FLASH_OTP_END)))
  303. #define IS_FLASH_NBSECTORS(NBSECTORS) (((NBSECTORS) != 0U) && ((NBSECTORS) <= FLASH_SECTOR_TOTAL))
  304. #define IS_OB_WRP_SECTOR(SECTOR)((((SECTOR) & 0xFFFFF000U) == 0x00000000U) && ((SECTOR) != 0x00000000U))
  305. /**
  306. * @}
  307. */
  308. /**
  309. * @}
  310. */
  311. /* Private functions ---------------------------------------------------------*/
  312. /** @defgroup FLASHEx_Private_Functions FLASH Private Functions
  313. * @{
  314. */
  315. void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange);
  316. void FLASH_FlushCaches(void);
  317. /**
  318. * @}
  319. */
  320. /**
  321. * @}
  322. */
  323. /**
  324. * @}
  325. */
  326. #ifdef __cplusplus
  327. }
  328. #endif
  329. #endif /* __STM32F2xx_HAL_FLASH_EX_H */
  330. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/