1 ////////////////////////////////////////////////////////////////////////////////
2 /// @file     hal_flash.h
3 /// @author   AE TEAM
4 /// @brief    THIS FILE CONTAINS ALL THE FUNCTIONS PROTOTYPES FOR THE FLASH
5 ///           FIRMWARE LIBRARY.
6 ////////////////////////////////////////////////////////////////////////////////
7 /// @attention
8 ///
9 /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
10 /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
11 /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
12 /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
13 /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
14 /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
15 ///
16 /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
17 ////////////////////////////////////////////////////////////////////////////////
18 
19 // Define to prevent recursive inclusion
20 #ifndef __HAL_FLASH_H
21 #define __HAL_FLASH_H
22 
23 // Files includes
24 #include "types.h"
25 #include "reg_common.h"
26 #include "reg_flash.h"
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 /// @addtogroup MM32_Hardware_Abstract_Layer
30 /// @{
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 /// @defgroup FLASH_HAL
34 /// @brief FLASH HAL modules
35 /// @{
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// @defgroup FLASH_Exported_Types
39 /// @{
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// @brief  FLASH Status
43 ////////////////////////////////////////////////////////////////////////////////
44 typedef enum {
45     FLASH_BUSY = 1,                                                             ///< FLASH busy status
46     FLASH_ERROR_PG,                                                             ///< FLASH programming error status
47     FLASH_ERROR_WRP,                                                            ///< FLASH write protection error status
48     FLASH_COMPLETE,                                                             ///< FLASH end of operation status
49     FLASH_TIMEOUT                                                               ///< FLASH Last operation timed out status
50 } FLASH_Status;
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// @brief  FLASH Latency
54 ////////////////////////////////////////////////////////////////////////////////
55 typedef enum {
56     FLASH_Latency_0 = FLASH_ACR_LATENCY_0,                                      ///< FLASH Zero Latency cycle
57     FLASH_Latency_1 = FLASH_ACR_LATENCY_1,                                      ///< FLASH One Latency cycle
58     FLASH_Latency_2 = FLASH_ACR_LATENCY_2,                                      ///< FLASH Two Latency cycles
59     FLASH_Latency_3 = FLASH_ACR_LATENCY_3                                       ///< FLASH Three Latency cycles
60 } FLASH_Latency_TypeDef;
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// @brief  Half_Cycle_Enable_Disable
64 ////////////////////////////////////////////////////////////////////////////////
65 typedef enum {
66     FLASH_HalfCycleAccess_Enable  = FLASH_ACR_HLFCYA,                           ///< FLASH Half Cycle Enable
67     FLASH_HalfCycleAccess_Disable = (s32)~FLASH_ACR_HLFCYA                              ///< FLASH Half Cycle Disable
68 } FLASH_HalfCycleAccess_TypeDef;
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// @brief  Prefetch_Buffer_Enable_Disable
72 ////////////////////////////////////////////////////////////////////////////////
73 typedef enum {
74     FLASH_PrefetchBuffer_Enable  = FLASH_ACR_PRFTBE,                            ///< FLASH Prefetch Buffer Enable
75     FLASH_PrefetchBuffer_Disable = (s32)~FLASH_ACR_PRFTBE                           ///< FLASH Prefetch Buffer Disable
76 } FLASH_PrefetchBuffer_TypeDef;
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// @brief  Option_Bytes_IWatchdog
80 ////////////////////////////////////////////////////////////////////////////////
81 typedef enum {
82     OB_IWDG_SW = 0x0001,                                                        ///< Software IWDG selected
83     OB_IWDG_HW = 0x0000                                                         ///< Hardware IWDG selected
84 } OB_IWDG_TypeDef;
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// @brief  Option_Bytes_nRST_STOP
88 ////////////////////////////////////////////////////////////////////////////////
89 typedef enum {
90     OB_STOP_NoRST = 0x0002,                                                     ///< No reset generated when entering in STOP
91     OB_STOP_RST   = 0x0000                                                      ///< Reset generated when entering in STOP
92 } OB_STOP_TypeDef;
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// @brief  Option_Bytes_nRST_STDBY
96 ////////////////////////////////////////////////////////////////////////////////
97 typedef enum {
98     OB_STDBY_NoRST = 0x0004,                                                    ///< No reset generated when entering in STANDBY
99     OB_STDBY_RST   = 0x0000                                                     ///< Reset generated when entering in STANDBY
100 } OB_STDBY_TypeDef;
101 
102 ////////////////////////////////////////////////////////////////////////////////
103 /// @brief  FLASH_Interrupts
104 ////////////////////////////////////////////////////////////////////////////////
105 typedef enum {
106     FLASH_IT_ERROR = FLASH_CR_ERRIE,                                            ///< FPEC error interrupt source
107     FLASH_IT_EOP   = FLASH_CR_EOPIE                                             ///< End of FLASH Operation Interrupt source
108 } FLASH_IT_TypeDef;
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// @brief  FLASH_Flags
112 ////////////////////////////////////////////////////////////////////////////////
113 typedef enum {
114     FLASH_FLAG_EOP      = FLASH_SR_EOP,                                         ///< FLASH End of Operation flag
115     FLASH_FLAG_PGERR    = FLASH_SR_PGERR,                                       ///< FLASH Program error flag
116     FLASH_FLAG_WRPRTERR = FLASH_SR_WRPRTERR,                                    ///< FLASH Write protected error flag
117     FLASH_FLAG_BSY      = FLASH_SR_BUSY,                                        ///< FLASH Busy flag
118     FLASH_FLAG_OPTERR   = FLASH_OBR_OPTERR                                      ///< FLASH Option Byte error flag
119 } FLASH_FLAG_TypeDef;
120 
121 /// @}
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// @defgroup FLASH_Exported_Constants
125 /// @{
126 
127 
128 #define RDP_Key ((u16)0x00A5)
129 #define FLASH_KEY1 ((u32)0x45670123)
130 #define FLASH_KEY2 ((u32)0xCDEF89AB)
131 #define EraseTimeout ((u32)0x00000FFF)
132 #define ProgramTimeout ((u32)0x0000000F)
133 
134 #define FLASH_WRProt_Pages0to3      ((u32)0x00000001)  ///< Write protection of page 0 to 3
135 #define FLASH_WRProt_Pages4to7      ((u32)0x00000002)  ///< Write protection of page 4 to 7
136 #define FLASH_WRProt_Pages8to11     ((u32)0x00000004)  ///< Write protection of page 8 to 11
137 #define FLASH_WRProt_Pages12to15    ((u32)0x00000008)  ///< Write protection of page 12 to 15
138 #define FLASH_WRProt_Pages16to19    ((u32)0x00000010)  ///< Write protection of page 16 to 19
139 #define FLASH_WRProt_Pages20to23    ((u32)0x00000020)  ///< Write protection of page 20 to 23
140 #define FLASH_WRProt_Pages24to27    ((u32)0x00000040)  ///< Write protection of page 24 to 27
141 #define FLASH_WRProt_Pages28to31    ((u32)0x00000080)  ///< Write protection of page 28 to 31
142 #define FLASH_WRProt_Pages32to35    ((u32)0x00000100)  ///< Write protection of page 32 to 35
143 #define FLASH_WRProt_Pages36to39    ((u32)0x00000200)  ///< Write protection of page 36 to 39
144 #define FLASH_WRProt_Pages40to43    ((u32)0x00000400)  ///< Write protection of page 40 to 43
145 #define FLASH_WRProt_Pages44to47    ((u32)0x00000800)  ///< Write protection of page 44 to 47
146 #define FLASH_WRProt_Pages48to51    ((u32)0x00001000)  ///< Write protection of page 48 to 51
147 #define FLASH_WRProt_Pages52to55    ((u32)0x00002000)  ///< Write protection of page 52 to 55
148 #define FLASH_WRProt_Pages56to59    ((u32)0x00004000)  ///< Write protection of page 56 to 59
149 #define FLASH_WRProt_Pages60to63    ((u32)0x00008000)  ///< Write protection of page 60 to 63
150 #define FLASH_WRProt_Pages64to67    ((u32)0x00010000)  ///< Write protection of page 64 to 67
151 #define FLASH_WRProt_Pages68to71    ((u32)0x00020000)  ///< Write protection of page 68 to 71
152 #define FLASH_WRProt_Pages72to75    ((u32)0x00040000)  ///< Write protection of page 72 to 75
153 #define FLASH_WRProt_Pages76to79    ((u32)0x00080000)  ///< Write protection of page 76 to 79
154 #define FLASH_WRProt_Pages80to83    ((u32)0x00100000)  ///< Write protection of page 80 to 83
155 #define FLASH_WRProt_Pages84to87    ((u32)0x00200000)  ///< Write protection of page 84 to 87
156 #define FLASH_WRProt_Pages88to91    ((u32)0x00400000)  ///< Write protection of page 88 to 91
157 #define FLASH_WRProt_Pages92to95    ((u32)0x00800000)  ///< Write protection of page 92 to 95
158 #define FLASH_WRProt_Pages96to99    ((u32)0x01000000)  ///< Write protection of page 96 to 99
159 #define FLASH_WRProt_Pages100to103  ((u32)0x02000000)  ///< Write protection of page 100 to 103
160 #define FLASH_WRProt_Pages104to107  ((u32)0x04000000)  ///< Write protection of page 104 to 107
161 #define FLASH_WRProt_Pages108to111  ((u32)0x08000000)  ///< Write protection of page 108 to 111
162 #define FLASH_WRProt_Pages112to115  ((u32)0x10000000)  ///< Write protection of page 112 to 115
163 #define FLASH_WRProt_Pages116to119  ((u32)0x20000000)  ///< Write protection of page 115 to 119
164 #define FLASH_WRProt_Pages120to123  ((u32)0x40000000)  ///< Write protection of page 120 to 123
165 #define FLASH_WRProt_Pages124to127  ((u32)0x80000000)  ///< Write protection of page 124 to 127
166 
167 /// @}
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// @defgroup FLASH_Exported_Variables
171 /// @{
172 
173 #ifdef _HAL_FLASH_C_
174 
175 #define GLOBAL
176 #else
177 #define GLOBAL extern
178 #endif
179 
180 #undef GLOBAL
181 
182 /// @}
183 
184 ////////////////////////////////////////////////////////////////////////////////
185 /// @defgroup FLASH_Exported_Functions
186 /// @{
187 void  FLASH_SetLatency(FLASH_Latency_TypeDef latency);
188 void  FLASH_HalfCycleAccessCmd(FLASH_HalfCycleAccess_TypeDef half_cycle_access);
189 void  FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_TypeDef prefetch_buffer);
190 void  FLASH_Unlock(void);
191 void  FLASH_Lock(void);
192 void  FLASH_OPTB_Enable(void);
193 void  FLASH_ITConfig(FLASH_IT_TypeDef interrupt, FunctionalState state);
194 void  FLASH_ClearFlag(u16 flag);
195 void  exFLASH_EraseEE(u32 page_address);
196 void  exFLASH_ProgramEE(u16* buf, u32 address, u16 len);
197 void  exFLASH_WriteEE(u16* buf, u32 page_address, u16 len);
198 void* exFLASH_Locate(u32 page_address, u16 len);
199 void* exFLASH_ReadEE(u32 page_address, u16 len);
200 
201 u8  exFLASH_FindEmpty(u16* ptr, u16 len);
202 u32 FLASH_GetUserOptionByte(void);
203 u32 FLASH_GetWriteProtectionOptionByte(void);
204 
205 FLASH_Status FLASH_ErasePage(u32 page_address);
206 FLASH_Status FLASH_EraseAllPages(void);
207 FLASH_Status FLASH_EraseOptionBytes(void);
208 FLASH_Status FLASH_EraseProtect(void);
209 FLASH_Status FLASH_ProgramHalfWord(u32 address, u16 data);
210 FLASH_Status FLASH_ProgramWord(u32 address, u32 data);
211 FLASH_Status FLASH_ProgramOptionHalfWord(u32 address, u16 data);
212 FLASH_Status FLASH_ProgramOptionByteData(u32 address, u8 data);
213 FLASH_Status FLASH_ProgramProtect(u32 address, u16 data);
214 FLASH_Status FLASH_EnableWriteProtection(u32 page);
215 FLASH_Status FLASH_UserOptionByteConfig(OB_IWDG_TypeDef ob_iwdg, OB_STOP_TypeDef ob_stop, OB_STDBY_TypeDef ob_standby);
216 FLASH_Status FLASH_GetStatus(void);
217 FLASH_Status FLASH_WaitForLastOperation(u32 time_out);
218 FLASH_Status FLASH_ReadOutProtection(FunctionalState state);
219 FlagStatus   FLASH_GetPrefetchBufferStatus(void);
220 FlagStatus   FLASH_GetFlagStatus(u16 flag);
221 
222 /// @}
223 
224 /// @}
225 
226 /// @}
227 
228 ////////////////////////////////////////////////////////////////////////////////
229 #endif //__HAL_FLASH_H
230 ////////////////////////////////////////////////////////////////////////////////
231