1 /**
2   ******************************************************************************
3   * @file    stm32h7rsxx_hal_flash_ex.h
4   * @author  MCD Application Team
5   * @brief   Header file of FLASH HAL Extended module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2022 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 
19 /* Define to prevent recursive inclusion -------------------------------------*/
20 #ifndef STM32H7RSxx_HAL_FLASH_EX_H
21 #define STM32H7RSxx_HAL_FLASH_EX_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32h7rsxx_hal_def.h"
29 
30 /** @addtogroup STM32H7RSxx_HAL_Driver
31   * @{
32   */
33 
34 /** @addtogroup FLASHEx
35   * @{
36   */
37 
38 /* Exported types ------------------------------------------------------------*/
39 /** @addtogroup FLASH_Exported_Types FLASH Exported Types
40   * @{
41   */
42 /**
43   * @brief  FLASH Erase structure definition
44   */
45 typedef struct
46 {
47   uint32_t Index;     /*!< Index of the key.
48                            This parameter can be a value between 0 and 31 */
49   uint32_t Size;      /*!< Size of the key.
50                            This parameter must be a value of @ref FLASH_Key_Size */
51   uint32_t HDPLLevel; /*!< HDPL level of the key.
52                            This parameter must be a value of @ref FLASH_KEY_Level */
53 } FLASH_KeyConfigTypeDef;
54 
55 /**
56   * @brief  FLASH CRC configuration structure definition
57   */
58 typedef struct
59 {
60   uint32_t TypeCRC;      /*!< CRC Selection Type.
61                               This parameter can be a value of @ref FLASHEx_CRC_Selection_Type */
62 
63   uint32_t BurstSize;    /*!< CRC Burst Size.
64                               This parameter can be a value of @ref FLASHEx_CRC_Burst_Size */
65 
66   uint32_t Sector;       /*!< Initial FLASH sector from which starts the CRC computation
67                              This parameter must be a value between 0 and (max number of sector - 1)*/
68 
69   uint32_t NbSectors;    /*!< Number of sectors to be computed.
70                               This parameter must be a value between 1 and
71                               (max number of sectors - value of Initial sector)*/
72 
73   uint32_t CRCStartAddr; /*!< CRC Start address.
74                               This parameter must be a value between begin address and end address of a bank */
75 
76   uint32_t CRCEndAddr;   /*!< CRC End address.
77                               This parameter must be a value between CRC Start address and end address of a bank */
78 
79 } FLASH_CRCInitTypeDef;
80 
81 /**
82   * @brief  FLASH ECC information structure definition
83   */
84 typedef struct
85 {
86   uint32_t Area;        /*!< Area from which an ECC was detected.
87                              This parameter can be a value of @ref FLASHEx_ECC_Area  */
88   uint32_t Address;     /*!< Flash address from which en ECC error was detected.
89                              This parameter must be a value between begin address and end address of the Flash */
90   uint32_t MasterID;    /*!< Master that initiated transfer on which error was detected
91                              This parameter can be a value of @ref FLASHEx_ECC_Master */
92 } FLASH_EccInfoTypeDef;
93 
94 /**
95   * @}
96   */
97 
98 /* Exported constants --------------------------------------------------------*/
99 /** @addtogroup FLASHEx_Exported_Constants
100   * @{
101   */
102 /** @defgroup FLASH_Key_Size FLASH Option Bytes Key Size
103   * @{
104   */
105 #define FLASH_KEY_32_BITS           0x0000000U             /*!< Key size is 32 bits */
106 #define FLASH_KEY_64_BITS           FLASH_OBKCR_OBKSIZE_0  /*!< Key size is 64 bits */
107 #define FLASH_KEY_128_BITS          FLASH_OBKCR_OBKSIZE_1  /*!< Key size is 128 bits */
108 #define FLASH_KEY_256_BITS          FLASH_OBKCR_OBKSIZE    /*!< Key size is 256 bits */
109 /**
110   * @}
111   */
112 
113 /** @defgroup FLASH_KEY_Level FLASH Option Bytes Key HDPL level
114   * @{
115   */
116 #define FLASH_KEY_LEVEL_CURRENT  0x00000000U           /*!< Key stored for the HDPL indicated in SBS_HDPLSR */
117 #define FLASH_KEY_LEVEL_NEXT     FLASH_OBKCR_NEXTKL_0  /*!< Key stored for the HDPL indicated in SBS_HDPLSR + 1 */
118 #define FLASH_KEY_LEVEL_PLUS_TWO FLASH_OBKCR_NEXTKL_1  /*!< Key stored for the HDPL indicated in SBS_HDPLSR + 2 */
119 /**
120   * @}
121   */
122 
123 /** @defgroup FLASHEx_CRC_Selection_Type FLASH CRC Selection Type
124   * @{
125   */
126 #define FLASH_CRC_ADDR         0x00000000U                                      /*!< CRC selection type by address  */
127 #define FLASH_CRC_SECTORS      FLASH_CRCCR_CRC_BY_SECT                          /*!< CRC selection type by sectors  */
128 #define FLASH_CRC_BANK         (FLASH_CRCCR_ALL_SECT | FLASH_CRCCR_CRC_BY_SECT) /*!< CRC selection type all bank */
129 /**
130   * @}
131   */
132 
133 /** @defgroup FLASHEx_CRC_Burst_Size FLASH CRC Burst Size
134   * @{
135   */
136 #define FLASH_CRC_BURST_SIZE_4    0x00000000U              /*!< Every burst has a size of 4 Flash words (64 Bytes) */
137 #define FLASH_CRC_BURST_SIZE_16   FLASH_CRCCR_CRC_BURST_0  /*!< Every burst has a size of 16 Flash words (256 Bytes) */
138 #define FLASH_CRC_BURST_SIZE_64   FLASH_CRCCR_CRC_BURST_1  /*!< Every burst has a size of 64 Flash words (1 kByte) */
139 #define FLASH_CRC_BURST_SIZE_256  FLASH_CRCCR_CRC_BURST    /*!< Every burst has a size of 256 Flash words (4 kBytes) */
140 /**
141   * @}
142   */
143 
144 /** @defgroup FLASHEx_ECC_Area FLASH ECC Area
145   * @{
146   */
147 #define FLASH_ECC_AREA_USER_BANK1 0x00000000U  /*!< FLASH bank 1 area */
148 #define FLASH_ECC_AREA_SYSTEM     0x00000001U  /*!< System FLASH area */
149 #define FLASH_ECC_AREA_OTP        0x00000002U  /*!< FLASH OTP area */
150 #define FLASH_ECC_AREA_READ_ONLY  0x00000004U  /*!< FLASH Read-only area */
151 /**
152   * @}
153   */
154 
155 /** @defgroup FLASHEx_ECC_Master FLASH ECC Master
156   * @{
157   */
158 #define FLASH_ECC_MASTER_CPU1     0x00000000U  /*!< ECC error occurs on a CPU1 transaction */
159 /**
160   * @}
161   */
162 
163 /**
164   * @}
165   */
166 
167 /* Exported macro ------------------------------------------------------------*/
168 
169 /* Exported functions --------------------------------------------------------*/
170 /** @addtogroup FLASHEx_Exported_Functions
171   * @{
172   */
173 
174 /* Extended Program operation functions  *************************************/
175 /** @addtogroup FLASHEx_Exported_Functions_Group1
176   * @{
177   */
178 HAL_StatusTypeDef HAL_FLASHEx_Erase(const FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError);
179 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(const FLASH_EraseInitTypeDef *pEraseInit);
180 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(const FLASH_OBProgramInitTypeDef *pOBInit);
181 void              HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit);
182 /**
183   * @}
184   */
185 
186 /** @addtogroup FLASHEx_Exported_Functions_Group2
187   * @{
188   */
189 HAL_StatusTypeDef HAL_FLASHEx_OTPLockConfig(uint32_t OTPLBlock);
190 uint32_t          HAL_FLASHEx_GetOTPLock(void);
191 HAL_StatusTypeDef HAL_FLASHEx_KeyConfig(const FLASH_KeyConfigTypeDef *pKeyConfig, const uint32_t *pKey);
192 HAL_StatusTypeDef HAL_FLASHEx_GetKey(const FLASH_KeyConfigTypeDef *pKeyConfig, uint32_t *pKey);
193 HAL_StatusTypeDef HAL_FLASHEx_ComputeCRC(const FLASH_CRCInitTypeDef *pCRCInit, uint32_t *CRC_Result);
194 /**
195   * @}
196   */
197 
198 /** @addtogroup FLASHEx_Exported_Functions_Group3
199   * @{
200   */
201 void              HAL_FLASHEx_EnableEccCorrectionInterrupt(void);
202 void              HAL_FLASHEx_DisableEccCorrectionInterrupt(void);
203 void              HAL_FLASHEx_EnableEccDetectionInterrupt(void);
204 void              HAL_FLASHEx_DisableEccDetectionInterrupt(void);
205 void              HAL_FLASHEx_GetEccInfo(FLASH_EccInfoTypeDef *pData);
206 void              HAL_FLASHEx_ECCD_IRQHandler(void);
207 __weak void       HAL_FLASHEx_EccDetectionCallback(void);
208 __weak void       HAL_FLASHEx_EccCorrectionCallback(void);
209 /**
210   * @}
211   */
212 
213 /**
214   * @}
215   */
216 
217 /* Private function ----------------------------------------------------------*/
218 /** @addtogroup FLASHEx_Private_Functions FLASHEx Private Functions
219   * @{
220   */
221 void FLASH_SectorErase(uint32_t Sector);
222 /**
223   * @}
224   */
225 
226 /* Private macros ------------------------------------------------------------*/
227 /** @defgroup FLASHEx_Private_Macros FLASH Private Macros
228   *  @{
229   */
230 #define IS_KEY_INDEX(VALUE)            ((VALUE) < 0x20U)
231 
232 #define IS_KEY_SIZE(VALUE)             (((VALUE) == FLASH_KEY_32_BITS)  || ((VALUE) == FLASH_KEY_64_BITS) || \
233                                         ((VALUE) == FLASH_KEY_128_BITS) || ((VALUE) == FLASH_KEY_256_BITS))
234 
235 #define IS_KEY_HDPL_LEVEL(VALUE)       (((VALUE) == FLASH_KEY_LEVEL_CURRENT) || \
236                                         ((VALUE) == FLASH_KEY_LEVEL_NEXT)    || \
237                                         ((VALUE) == FLASH_KEY_LEVEL_PLUS_TWO))
238 
239 #define IS_OB_OTP_BLOCK(VALUE)         ((VALUE) <= 0xFFFFU)
240 
241 #define IS_FLASH_TYPE_CRC(VALUE)       (((VALUE) == FLASH_CRC_ADDR) || ((VALUE) == FLASH_CRC_SECTORS) ||\
242                                         ((VALUE) == FLASH_CRC_BANK))
243 
244 #define IS_FLASH_BURST_SIZE_CRC(VALUE) (((VALUE) == FLASH_CRC_BURST_SIZE_4) || ((VALUE) == FLASH_CRC_BURST_SIZE_16) ||\
245                                         ((VALUE) == FLASH_CRC_BURST_SIZE_64) || ((VALUE) == FLASH_CRC_BURST_SIZE_256))
246 /**
247   * @}
248   */
249 
250 /**
251   * @}
252   */
253 
254 /**
255   * @}
256   */
257 
258 #ifdef __cplusplus
259 }
260 #endif
261 
262 #endif /* STM32H7RSxx_HAL_FLASH_EX_H */
263