1 /*!
2  * @file        apm32f0xx_fmc.h
3  *
4  * @brief       This file contains all the functions prototypes for the FMC firmware library
5  *
6  * @version     V1.0.3
7  *
8  * @date        2022-09-20
9  *
10  * @attention
11  *
12  *  Copyright (C) 2020-2022 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be useful and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 /* Define to prevent recursive inclusion */
27 #ifndef __APM32F0XX_FMC_H
28 #define __APM32F0XX_FMC_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /* Includes */
35 #include "apm32f0xx.h"
36 
37 /** @addtogroup APM32F0xx_StdPeriphDriver
38   @{
39 */
40 
41 /** @addtogroup FMC_Driver
42   @{
43 */
44 
45 /** @defgroup FMC_Macros Macros
46   @{
47 */
48 
49 /* Macros description */
50 
51 /* Flash Read protection key */
52 #define FMC_RP_KEY            ((uint32_t)0XA5)
53 
54 /* Flash key definition */
55 #define FMC_KEY_1             ((uint32_t)0x45670123)
56 #define FMC_KEY_2             ((uint32_t)0xCDEF89AB)
57 
58 #define FMC_OB_KEY_1          ((uint32_t)0x45670123)
59 #define FMC_OB_KEY_2          ((uint32_t)0xCDEF89AB)
60 
61 /* Delay definition */
62 #define FMC_DELAY_ERASE       ((uint32_t)0x000B0000)
63 #define FMC_DELAY_PROGRAM     ((uint32_t)0x00002000)
64 
65 /* 32K and 64K Flash devices */
66 #if !defined (APM32F030xC) && !defined (APM32F070xB) && !defined (APM32F071xB) && !defined (APM32F072xB) && !defined (APM32F091)
67 /* Flash write protect page definition */
68 #define FLASH_WRP_PAGE_0_3               ((uint32_t)0x00000001) /*!< Write protection of page 0 to 3 */
69 #define FLASH_WRP_PAGE_4_7               ((uint32_t)0x00000002) /*!< Write protection of page 4 to 7 */
70 #define FLASH_WRP_PAGE_8_11              ((uint32_t)0x00000004) /*!< Write protection of page 8 to 11 */
71 #define FLASH_WRP_PAGE_12_15             ((uint32_t)0x00000008) /*!< Write protection of page 12 to 15 */
72 #define FLASH_WRP_PAGE_16_19             ((uint32_t)0x00000010) /*!< Write protection of page 16 to 19 */
73 #define FLASH_WRP_PAGE_20_23             ((uint32_t)0x00000020) /*!< Write protection of page 20 to 23 */
74 #define FLASH_WRP_PAGE_24_27             ((uint32_t)0x00000040) /*!< Write protection of page 24 to 27 */
75 #define FLASH_WRP_PAGE_28_31             ((uint32_t)0x00000080) /*!< Write protection of page 28 to 31 */
76 #define FLASH_WRP_PAGE_32_35             ((uint32_t)0x00000100) /*!< Write protection of page 32 to 35 */
77 #define FLASH_WRP_PAGE_36_39             ((uint32_t)0x00000200) /*!< Write protection of page 36 to 39 */
78 #define FLASH_WRP_PAGE_40_43             ((uint32_t)0x00000400) /*!< Write protection of page 40 to 43 */
79 #define FLASH_WRP_PAGE_44_47             ((uint32_t)0x00000800) /*!< Write protection of page 44 to 47 */
80 #define FLASH_WRP_PAGE_48_51             ((uint32_t)0x00001000) /*!< Write protection of page 48 to 51 */
81 #define FLASH_WRP_PAGE_52_55             ((uint32_t)0x00002000) /*!< Write protection of page 52 to 55 */
82 #define FLASH_WRP_PAGE_56_59             ((uint32_t)0x00004000) /*!< Write protection of page 56 to 59 */
83 #define FLASH_WRP_PAGE_60_63             ((uint32_t)0x00008000) /*!< Write protection of page 60 to 63 */
84 #define FLASH_WRP_PAGE_ALL               ((uint32_t)0x0000FFFF) /*!< Write protection of all Sectors */
85 
86 /* 128K and 256K Flash devices */
87 #else
88 /* Flash write protect page definition */
89 #define FMC_WRP_PAGE_0_1               ((uint32_t)0x00000001) /*!< Write protection of page 0 to 1 */
90 #define FMC_WRP_PAGE_2_3               ((uint32_t)0x00000002) /*!< Write protection of page 2 to 3 */
91 #define FMC_WRP_PAGE_4_5               ((uint32_t)0x00000004) /*!< Write protection of page 4 to 5 */
92 #define FMC_WRP_PAGE_6_7               ((uint32_t)0x00000008) /*!< Write protection of page 6 to 7 */
93 #define FMC_WRP_PAGE_8_9               ((uint32_t)0x00000010) /*!< Write protection of page 8 to 5 */
94 #define FMC_WRP_PAGE_10_11             ((uint32_t)0x00000020) /*!< Write protection of page 10 to 5 */
95 #define FMC_WRP_PAGE_12_13             ((uint32_t)0x00000040) /*!< Write protection of page 12 to 5 */
96 #define FMC_WRP_PAGE_14_15             ((uint32_t)0x00000080) /*!< Write protection of page 14 to 5 */
97 #define FMC_WRP_PAGE_16_17             ((uint32_t)0x00000100) /*!< Write protection of page 16 to 15 */
98 #define FMC_WRP_PAGE_18_19             ((uint32_t)0x00000200) /*!< Write protection of page 18 to 19 */
99 #define FMC_WRP_PAGE_20_21             ((uint32_t)0x00000400) /*!< Write protection of page 20 to 21 */
100 #define FMC_WRP_PAGE_22_23             ((uint32_t)0x00000800) /*!< Write protection of page 22 to 23 */
101 #define FMC_WRP_PAGE_24_25             ((uint32_t)0x00001000) /*!< Write protection of page 24 to 25 */
102 #define FMC_WRP_PAGE_26_27             ((uint32_t)0x00002000) /*!< Write protection of page 26 to 27 */
103 #define FMC_WRP_PAGE_28_29             ((uint32_t)0x00004000) /*!< Write protection of page 28 to 29 */
104 #define FMC_WRP_PAGE_30_31             ((uint32_t)0x00008000) /*!< Write protection of page 30 to 31 */
105 #define FMC_WRP_PAGE_32_33             ((uint32_t)0x00010000) /*!< Write protection of page 32 to 33 */
106 #define FMC_WRP_PAGE_34_35             ((uint32_t)0x00020000) /*!< Write protection of page 34 to 35 */
107 #define FMC_WRP_PAGE_36_37             ((uint32_t)0x00040000) /*!< Write protection of page 36 to 37 */
108 #define FMC_WRP_PAGE_38_39             ((uint32_t)0x00080000) /*!< Write protection of page 38 to 39 */
109 #define FMC_WRP_PAGE_40_41             ((uint32_t)0x00100000) /*!< Write protection of page 40 to 41 */
110 #define FMC_WRP_PAGE_42_43             ((uint32_t)0x00200000) /*!< Write protection of page 42 to 43 */
111 #define FMC_WRP_PAGE_44_45             ((uint32_t)0x00400000) /*!< Write protection of page 44 to 45 */
112 #define FMC_WRP_PAGE_46_47             ((uint32_t)0x00800000) /*!< Write protection of page 46 to 47 */
113 #define FMC_WRP_PAGE_48_49             ((uint32_t)0x01000000) /*!< Write protection of page 48 to 49 */
114 #define FMC_WRP_PAGE_50_51             ((uint32_t)0x02000000) /*!< Write protection of page 50 to 51 */
115 #define FMC_WRP_PAGE_52_53             ((uint32_t)0x04000000) /*!< Write protection of page 52 to 53 */
116 #define FMC_WRP_PAGE_54_55             ((uint32_t)0x08000000) /*!< Write protection of page 54 to 55 */
117 #define FMC_WRP_PAGE_56_57             ((uint32_t)0x10000000) /*!< Write protection of page 56 to 57 */
118 #define FMC_WRP_PAGE_58_59             ((uint32_t)0x20000000) /*!< Write protection of page 58 to 59 */
119 #define FMC_WRP_PAGE_60_61             ((uint32_t)0x40000000) /*!< Write protection of page 20 to 21 */
120 #define FMC_WRP_PAGE_ALL               ((uint32_t)0xFFFFFFFF) /*!< Write protection of page all */
121 
122 #endif
123 /**@} end of group FMC_Macros*/
124 
125 
126 /** @defgroup FMC_Enumerations Enumerations
127   @{
128 */
129 
130 /**
131  * @brief Flash Latency
132  */
133 typedef enum
134 {
135     FMC_LATENCY_0,                           /*!< Flash zero latency cycle */
136     FMC_LATENCY_1                            /*!< Flash one latency cycle */
137 } FMC_LATENCY_T;
138 
139 /**
140  * @brief   Flash definition
141  */
142 typedef enum
143 {
144     FMC_FLAG_BUSY = ((uint8_t)0x01),         /*!< Busy flag */
145     FMC_FLAG_PE   = ((uint8_t)0x04),         /*!< Program error flag */
146     FMC_FLAG_WPE  = ((uint8_t)0x10),         /*!< Write protection flag */
147     FMC_FLAG_OC   = ((uint8_t)0x20),         /*!< Operation complete flag */
148 } FMC_FLAG_T;
149 
150 /**
151  * @brief   Flash Status
152  */
153 typedef enum
154 {
155     FMC_STATE_COMPLETE = ((uint8_t)0),       /*!< Operation complete */
156     FMC_STATE_BUSY     = ((uint8_t)1),       /*!< Busy */
157     FMC_STATE_PG_ERR   = ((uint8_t)2),       /*!< Program error */
158     FMC_STATE_WRP_ERR  = ((uint8_t)3),       /*!< Write Protection error */
159     FMC_STATE_TIMEOUT  = ((uint8_t)4),       /*!< Time out */
160 } FMC_STATE_T;
161 
162 /**
163  * @brief   Interrupt source
164  */
165 typedef enum
166 {
167     FMC_INT_ERROR = ((uint32_t)0x400),       /*!< Error interrupt */
168     FMC_INT_COMPLETE  = ((uint32_t)0x1000),  /*!< Operation complete interrupt */
169 } FMC_INT_T;
170 
171 /**
172  * @brief   Protection Level
173  */
174 typedef enum
175 {
176     FMC_RDP_LEVEL_0 = ((uint8_t)0xAA),       /*!< Protection Level 0 */
177     FMC_RDP_LEVEL_1 = ((uint8_t)0xBB),       /*!< Protection Level 1 */
178 } FMC_RDP_T;
179 
180 /**
181  * @brief   Option byte WDG mode activation
182  */
183 typedef enum
184 {
185     FMC_OB_IWDT_HW    = ((uint8_t)0X00),     /*!< activated by hardware */
186     FMC_OB_IWDT_SW    = ((uint8_t)0X01),     /*!< activated by software */
187 } FMC_OB_IWDT_T;
188 
189 /**
190  * @brief   Option byte STOP mode activation
191  */
192 typedef enum
193 {
194     FMC_OB_STOP_RESET = ((uint8_t)0X00),     /*!< Reset generated when entering in STOP */
195     FMC_OB_STOP_NRST  = ((uint8_t)0X02),     /*!< No reset generated when entering in STOP */
196 } FMC_OB_STOP_T;
197 
198 /**
199  * @brief   Option byte STDBY mode activation
200  */
201 typedef enum
202 {
203     FMC_OB_STDBY_RESET = ((uint8_t)0X00),    /*!< Reset generated when entering in STDBY */
204     FMC_OB_STDBY_NRST  = ((uint8_t)0X04),    /*!< No reset generated when entering in STDBY */
205 } FMC_OB_STDBY_T;
206 
207 /**
208  * @brief   Flash Option Bytes BOOT0
209  */
210 typedef enum
211 {
212     FMC_OB_BOOT0_RESET  = ((uint8_t)0X00),   /*!< BOOT0 Reset */
213     FMC_OB_BOOT0_SET = ((uint8_t)0X08),      /*!< BOOT0 Set */
214 } FMC_OB_BOOT0_T;
215 
216 /**
217  * @brief   Flash Option Bytes BOOT1
218  */
219 typedef enum
220 {
221     FMC_OB_BOOT1_RESET    = ((uint8_t)0X00), /*!< BOOT1 Reset */
222     FMC_OB_BOOT1_SET      = ((uint8_t)0X10), /*!< BOOT1 Set */
223 } FMC_OB_BOOT1_T;
224 
225 /**
226  * @brief   Flash Option Bytes VDDA Analog Monitoring
227  */
228 typedef enum
229 {
230     FMC_OB_VDDA_ANALOG_OFF = ((uint8_t)0X00), /*!< Analog monitoring on VDDA Power source OFF */
231     FMC_OB_VDDA_ANALOG_ON  = ((uint8_t)0X20), /*!< Analog monitoring on VDDA Power source ON */
232 } FMC_OB_VDDA_ANALOG_T;
233 
234 /**
235  * @brief   Flash Option Bytes SRAM Parity Enable
236  */
237 typedef enum
238 {
239     FMC_OB_SRAM_PARITY_SET    = ((uint8_t)0X00), /*!< SRAM parity enable Set */
240     FMC_OB_SRAM_PARITY_RESET  = ((uint8_t)0X40), /*!< SRAM parity enable reset */
241 } FMC_OB_SRAM_PARITY_T;
242 
243 /**
244  * @brief   flash Option Bytes BOOT0SW
245  */
246 typedef enum
247 {
248     FMC_OB_BOOT0_SW   = ((uint8_t)0X00),     /*!< BOOT0 pin disabled */
249     FMC_OB_BOOT0_HW   = ((uint8_t)0X80),     /*!< BOOT0 pin bonded with GPIO */
250 } FMC_OB_BOOT0SW_T;
251 
252 /**@} end of group FMC_Enumerations*/
253 
254 /** @defgroup FMC_Structures Structures
255   @{
256 */
257 
258 /**
259  * @brief   User Option byte config struct definition
260  */
261 typedef struct
262 {
263     FMC_OB_IWDT_T  iwdtSw;
264     FMC_OB_STOP_T  stopce;
265     FMC_OB_STDBY_T stdbyce;
266 } FMC_UserConfig_T;
267 
268 /**@} end of group FMC_Structures*/
269 
270 /** @defgroup FMC_Variables Variables
271   @{
272 */
273 
274 /**@} end of group FMC_Variables*/
275 
276 /** @defgroup FMC_Functions Functions
277   @{
278 */
279 
280 /* Function description */
281 
282 /* Latency */
283 void FMC_SetLatency(FMC_LATENCY_T latency);
284 
285 /* Prefetch Buffer */
286 void FMC_EnablePrefetchBuffer(void);
287 void FMC_DisablePrefetchBuffer(void);
288 uint8_t FMC_ReadPrefetchBufferStatus(void);
289 
290 /* Lock */
291 void FMC_Unlock(void);
292 void FMC_Lock(void);
293 
294 /* Erase and Program */
295 FMC_STATE_T FMC_ErasePage(uint32_t pageAddr);
296 FMC_STATE_T FMC_EraseAllPages(void);
297 
298 FMC_STATE_T FMC_ProgramWord(uint32_t addr, uint32_t data);
299 FMC_STATE_T FMC_ProgramHalfWord(uint32_t addr, uint16_t data);
300 
301 /* FMC Option Bytes Programming functions */
302 void FMC_UnlockOptionByte(void);
303 void FMC_LockOptionByte(void);
304 void FMC_LaunchOptionByte(void);
305 FMC_STATE_T FMC_EraseOptionByte(void);
306 FMC_STATE_T FMC_EnableWriteProtection(uint32_t page);
307 FMC_STATE_T FMC_ConfigReadOutProtection(FMC_RDP_T rdp);
308 FMC_STATE_T FMC_ConfigOptionByteUser(FMC_UserConfig_T* userConfig);
309 FMC_STATE_T FMC_EnableOptionByteBOOT(void);
310 FMC_STATE_T FMC_DisableOptionByteBOOT(void);
311 FMC_STATE_T FMC_EnableOptionByteVDDA(void);
312 FMC_STATE_T FMC_DisableOptionByteVDDA(void);
313 FMC_STATE_T FMC_EnableOptionByteSRAMParity(void);
314 FMC_STATE_T FMC_DisableOptionByteSRAMParity(void);
315 FMC_STATE_T FMC_WriteOptionByteUser(uint8_t ob_user);
316 FMC_STATE_T FMC_ProgramOptionByteData(uint32_t addr, uint8_t data);
317 uint8_t FMC_ReadOptionByteUser(void);
318 uint32_t FMC_ReadOptionByteWriteProtection(void);
319 uint8_t FMC_GetReadProtectionStatus(void);
320 
321 /* Interrupt and Flag */
322 void FMC_EnableInterrupt(uint32_t interrupt);
323 void FMC_DisableInterrupt(uint32_t interrupt);
324 uint8_t FMC_ReadStatusFlag(FMC_FLAG_T flag);
325 void FMC_ClearStatusFlag(uint8_t flag);
326 
327 /* State management */
328 FMC_STATE_T FMC_ReadState(void);
329 FMC_STATE_T FMC_WaitForReady(uint32_t timeOut);
330 
331 #ifdef __cplusplus
332 }
333 #endif
334 
335 #endif /* __APM32F0XX_FMC_H */
336 
337 /**@} end of group FMC_Functions*/
338 /**@} end of group FMC_Driver*/
339 /**@} end of group APM32F0xx_StdPeriphDriver*/
340