1 /*!
2     \file    gd32f4xx_fmc.h
3     \brief   definitions for the FMC
4 
5     \version 2016-08-15, V1.0.0, firmware for GD32F4xx
6     \version 2018-12-12, V2.0.0, firmware for GD32F4xx
7     \version 2020-09-30, V2.1.0, firmware for GD32F4xx
8     \version 2020-12-20, V2.1.1, firmware for GD32F4xx
9     \version 2022-03-09, V3.0.0, firmware for GD32F4xx
10 */
11 
12 /*
13     Copyright (c) 2022, GigaDevice Semiconductor Inc.
14 
15     Redistribution and use in source and binary forms, with or without modification,
16 are permitted provided that the following conditions are met:
17 
18     1. Redistributions of source code must retain the above copyright notice, this
19        list of conditions and the following disclaimer.
20     2. Redistributions in binary form must reproduce the above copyright notice,
21        this list of conditions and the following disclaimer in the documentation
22        and/or other materials provided with the distribution.
23     3. Neither the name of the copyright holder nor the names of its contributors
24        may be used to endorse or promote products derived from this software without
25        specific prior written permission.
26 
27     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
36 OF SUCH DAMAGE.
37 */
38 
39 
40 #ifndef GD32F4XX_FMC_H
41 #define GD32F4XX_FMC_H
42 
43 #include "gd32f4xx.h"
44 
45 /* FMC and option byte definition */
46 #define FMC                        FMC_BASE                       /*!< FMC register base address */
47 #define OB                         OB_BASE                        /*!< option byte base address */
48 
49 /* registers definitions */
50 #define FMC_WS                     REG32((FMC) + 0x00000000U)     /*!< FMC wait state register */
51 #define FMC_KEY                    REG32((FMC) + 0x00000004U)     /*!< FMC unlock key register */
52 #define FMC_OBKEY                  REG32((FMC) + 0x00000008U)     /*!< FMC option byte unlock key register */
53 #define FMC_STAT                   REG32((FMC) + 0x0000000CU)     /*!< FMC status register */
54 #define FMC_CTL                    REG32((FMC) + 0x00000010U)     /*!< FMC control register */
55 #define FMC_OBCTL0                 REG32((FMC) + 0x00000014U)     /*!< FMC option byte control register 0 */
56 #define FMC_OBCTL1                 REG32((FMC) + 0x00000018U)     /*!< FMC option byte control register 1 */
57 #define FMC_PECFG                  REG32((FMC) + 0x00000020U)     /*!< FMC page erase configuration register */
58 #define FMC_PEKEY                  REG32((FMC) + 0x00000024U)     /*!< FMC unlock page erase key register */
59 #define FMC_WSEN                   REG32((FMC) + 0x000000FCU)     /*!< FMC wait state enable register */
60 #define FMC_PID                    REG32((FMC) + 0x00000100U)     /*!< FMC product ID register */
61 
62 #define OB_WP1                     REG32((OB) + 0x00000008U)      /*!< option byte write protection 1 */
63 #define OB_USER                    REG32((OB) + 0x00010000U)      /*!< option byte user value*/
64 #define OB_SPC                     REG32((OB) + 0x00010001U)      /*!< option byte security protection value */
65 #define OB_WP0                     REG32((OB) + 0x00010008U)      /*!< option byte write protection 0 */
66 
67 /* bits definitions */
68 /* FMC_WS */
69 #define FMC_WC_WSCNT               BITS(0,3)                      /*!< wait state counter */
70 
71 /* FMC_KEY */
72 #define FMC_KEY_KEY                BITS(0,31)                     /*!< FMC main flash key bits */
73 
74 /* FMC_OBKEY */
75 #define FMC_OBKEY_OBKEY            BITS(0,31)                     /*!< option byte key bits */
76 
77 /* FMC_STAT */
78 #define FMC_STAT_END               BIT(0)                         /*!< end of operation flag bit */
79 #define FMC_STAT_OPERR             BIT(1)                         /*!< flash operation error flag bit */
80 #define FMC_STAT_WPERR             BIT(4)                         /*!< erase/Program protection error flag bit */
81 #define FMC_STAT_PGMERR            BIT(6)                         /*!< program size not match error flag bit */
82 #define FMC_STAT_PGSERR            BIT(7)                         /*!< program sequence error flag bit */
83 #define FMC_STAT_RDDERR            BIT(8)                         /*!< read D-bus protection error flag bit */
84 #define FMC_STAT_BUSY              BIT(16)                        /*!< flash busy flag bit */
85 
86 /* FMC_CTL */
87 #define FMC_CTL_PG                 BIT(0)                         /*!< main flash program command bit */
88 #define FMC_CTL_SER                BIT(1)                         /*!< main flash sector erase command bit */
89 #define FMC_CTL_MER0               BIT(2)                         /*!< main flash mass erase for bank0 command bit */
90 #define FMC_CTL_SN                 BITS(3,7)                      /*!< select which sector number to be erased */
91 #define FMC_CTL_PSZ                BITS(8,9)                      /*!< program size bit */
92 #define FMC_CTL_MER1               BIT(15)                        /*!< main flash mass erase for bank1 command bit */
93 #define FMC_CTL_START              BIT(16)                        /*!< send erase command to FMC bit */
94 #define FMC_CTL_ENDIE              BIT(24)                        /*!< end of operation interrupt enable bit */
95 #define FMC_CTL_ERRIE              BIT(25)                        /*!< error interrupt enable bit */
96 #define FMC_CTL_LK                 BIT(31)                        /*!< FMC_CTL lock bit */
97 
98 /* FMC_OBCTL0 */
99 #define FMC_OBCTL0_OB_LK           BIT(0)                         /*!< FMC_OBCTL0 lock bit */
100 #define FMC_OBCTL0_OB_START        BIT(1)                         /*!< send option byte change command to FMC bit */
101 #define FMC_OBCTL0_BOR_TH          BITS(2,3)                      /*!< option byte BOR threshold value */
102 #define FMC_OBCTL0_BB              BIT(4)                         /*!< option byte boot bank value */
103 #define FMC_OBCTL0_NWDG_HW         BIT(5)                         /*!< option byte watchdog value */
104 #define FMC_OBCTL0_NRST_DPSLP      BIT(6)                         /*!< option byte deepsleep reset value */
105 #define FMC_OBCTL0_NRST_STDBY      BIT(7)                         /*!< option byte standby reset value */
106 #define FMC_OBCTL0_SPC             BITS(8,15)                     /*!< option byte Security Protection code */
107 #define FMC_OBCTL0_WP0             BITS(16,27)                    /*!< erase/program protection of each sector when DRP is 0 */
108 #define FMC_OBCTL0_DBS             BIT(30)                        /*!< double banks or single bank selection when flash size is 1M bytes */
109 #define FMC_OBCTL0_DRP             BIT(31)                        /*!< D-bus read protection bit */
110 
111 /* FMC_OBCTL1 */
112 #define FMC_OBCTL1_WP1             BITS(16,27)                    /*!< erase/program protection of each sector when DRP is 0 */
113 
114 /* FMC_PECFG */
115 #define FMC_PE_EN                  BIT(31)                        /*!< the enable bit of page erase function */
116 #define FMC_PE_ADDR                BITS(0,28)                     /*!< page erase address */
117 
118 /* FMC_PEKEY */
119 #define FMC_PE_KEY                 BITS(0,31)                     /*!< FMC_PECFG unlock key value */
120 
121 /* FMC_WSEN */
122 #define FMC_WSEN_WSEN              BIT(0)                         /*!< FMC wait state enable bit */
123 
124 /* FMC_PID */
125 #define FMC_PID_PID                BITS(0,31)                     /*!< product ID bits */
126 
127 /* constants definitions */
128 /* fmc state */
129 typedef enum
130 {
131     FMC_READY,                                                    /*!< the operation has been completed */
132     FMC_BUSY,                                                     /*!< the operation is in progress */
133     FMC_RDDERR,                                                   /*!< read D-bus protection error */
134     FMC_PGSERR,                                                   /*!< program sequence error */
135     FMC_PGMERR,                                                   /*!< program size not match error */
136     FMC_WPERR,                                                    /*!< erase/program protection error */
137     FMC_OPERR,                                                    /*!< operation error */
138     FMC_TOERR,                                                    /*!< timeout error */
139 }fmc_state_enum;
140 
141 /* unlock key */
142 #define UNLOCK_KEY0                ((uint32_t)0x45670123U)        /*!< unlock key 0 */
143 #define UNLOCK_KEY1                ((uint32_t)0xCDEF89ABU)        /*!< unlock key 1 */
144 #define UNLOCK_PE_KEY              ((uint32_t)0xA9B8C7D6U)        /*!< unlock page erase function key */
145 
146 #define OB_UNLOCK_KEY0             ((uint32_t)0x08192A3BU)        /*!< ob unlock key 0 */
147 #define OB_UNLOCK_KEY1             ((uint32_t)0x4C5D6E7FU)        /*!< ob unlock key 1 */
148 
149 /* option byte write protection */
150 #define OB_LWP                     ((uint32_t)0x000000FFU)        /*!< write protection low bits */
151 #define OB_HWP                     ((uint32_t)0x0000FF00U)        /*!< write protection high bits */
152 
153 /* FMC wait state counter */
154 #define WC_WSCNT(regval)           (BITS(0,3) & ((uint32_t)(regval)))
155 #define WS_WSCNT_0                 WC_WSCNT(0)                    /*!< FMC 0 wait */
156 #define WS_WSCNT_1                 WC_WSCNT(1)                    /*!< FMC 1 wait */
157 #define WS_WSCNT_2                 WC_WSCNT(2)                    /*!< FMC 2 wait */
158 #define WS_WSCNT_3                 WC_WSCNT(3)                    /*!< FMC 3 wait */
159 #define WS_WSCNT_4                 WC_WSCNT(4)                    /*!< FMC 4 wait */
160 #define WS_WSCNT_5                 WC_WSCNT(5)                    /*!< FMC 5 wait */
161 #define WS_WSCNT_6                 WC_WSCNT(6)                    /*!< FMC 6 wait */
162 #define WS_WSCNT_7                 WC_WSCNT(7)                    /*!< FMC 7 wait */
163 #define WS_WSCNT_8                 WC_WSCNT(8)                    /*!< FMC 8 wait */
164 #define WS_WSCNT_9                 WC_WSCNT(9)                    /*!< FMC 9 wait */
165 #define WS_WSCNT_10                WC_WSCNT(10)                   /*!< FMC 10 wait */
166 #define WS_WSCNT_11                WC_WSCNT(11)                   /*!< FMC 11 wait */
167 #define WS_WSCNT_12                WC_WSCNT(12)                   /*!< FMC 12 wait */
168 #define WS_WSCNT_13                WC_WSCNT(13)                   /*!< FMC 13 wait */
169 #define WS_WSCNT_14                WC_WSCNT(14)                   /*!< FMC 14 wait */
170 #define WS_WSCNT_15                WC_WSCNT(15)                   /*!< FMC 15 wait */
171 
172 /* option byte BOR threshold value */
173 #define OBCTL0_BOR_TH(regval)      (BITS(2,3) & ((uint32_t)(regval))<< 2)
174 #define OB_BOR_TH_VALUE3           OBCTL0_BOR_TH(0)               /*!< BOR threshold value 3 */
175 #define OB_BOR_TH_VALUE2           OBCTL0_BOR_TH(1)               /*!< BOR threshold value 2 */
176 #define OB_BOR_TH_VALUE1           OBCTL0_BOR_TH(2)               /*!< BOR threshold value 1 */
177 #define OB_BOR_TH_OFF              OBCTL0_BOR_TH(3)               /*!< no BOR function */
178 
179 /* option byte boot bank value */
180 #define OBCTL0_BB(regval)          (BIT(4) & ((uint32_t)(regval)<<4))
181 #define OB_BB_DISABLE              OBCTL0_BB(0)                   /*!< boot from bank0 */
182 #define OB_BB_ENABLE               OBCTL0_BB(1)                   /*!< boot from bank1 or bank0 if bank1 is void */
183 
184 /* option byte software/hardware free watch dog timer */
185 #define OBCTL0_NWDG_HW(regval)     (BIT(5) & ((uint32_t)(regval))<< 5)
186 #define OB_FWDGT_SW                OBCTL0_NWDG_HW(1)              /*!< software free watchdog */
187 #define OB_FWDGT_HW                OBCTL0_NWDG_HW(0)              /*!< hardware free watchdog */
188 
189 /* option byte reset or not entering deep sleep mode */
190 #define OBCTL0_NRST_DPSLP(regval)  (BIT(6) & ((uint32_t)(regval))<< 6)
191 #define OB_DEEPSLEEP_NRST          OBCTL0_NRST_DPSLP(1)           /*!< no reset when entering deepsleep mode */
192 #define OB_DEEPSLEEP_RST           OBCTL0_NRST_DPSLP(0)           /*!< generate a reset instead of entering deepsleep mode */
193 
194 /* option byte reset or not entering standby mode */
195 #define OBCTL0_NRST_STDBY(regval)  (BIT(7) & ((uint32_t)(regval))<< 7)
196 #define OB_STDBY_NRST              OBCTL0_NRST_STDBY(1)           /*!< no reset when entering deepsleep mode */
197 #define OB_STDBY_RST               OBCTL0_NRST_STDBY(0)           /*!< generate a reset instead of entering standby mode */
198 
199 /* read protect configure */
200 #define FMC_NSPC                   ((uint8_t)0xAAU)               /*!< no security protection */
201 #define FMC_LSPC                   ((uint8_t)0xABU)               /*!< low security protection */
202 #define FMC_HSPC                   ((uint8_t)0xCCU)               /*!< high security protection */
203 
204 /* option bytes write protection */
205 #define OB_WP_0                    ((uint32_t)0x00000001U)        /*!< erase/program protection of sector 0  */
206 #define OB_WP_1                    ((uint32_t)0x00000002U)        /*!< erase/program protection of sector 1  */
207 #define OB_WP_2                    ((uint32_t)0x00000004U)        /*!< erase/program protection of sector 2  */
208 #define OB_WP_3                    ((uint32_t)0x00000008U)        /*!< erase/program protection of sector 3  */
209 #define OB_WP_4                    ((uint32_t)0x00000010U)        /*!< erase/program protection of sector 4  */
210 #define OB_WP_5                    ((uint32_t)0x00000020U)        /*!< erase/program protection of sector 5  */
211 #define OB_WP_6                    ((uint32_t)0x00000040U)        /*!< erase/program protection of sector 6  */
212 #define OB_WP_7                    ((uint32_t)0x00000080U)        /*!< erase/program protection of sector 7  */
213 #define OB_WP_8                    ((uint32_t)0x00000100U)        /*!< erase/program protection of sector 8  */
214 #define OB_WP_9                    ((uint32_t)0x00000200U)        /*!< erase/program protection of sector 9  */
215 #define OB_WP_10                   ((uint32_t)0x00000400U)        /*!< erase/program protection of sector 10 */
216 #define OB_WP_11                   ((uint32_t)0x00000800U)        /*!< erase/program protection of sector 11 */
217 #define OB_WP_12                   ((uint32_t)0x00010000U)        /*!< erase/program protection of sector 12 */
218 #define OB_WP_13                   ((uint32_t)0x00020000U)        /*!< erase/program protection of sector 13 */
219 #define OB_WP_14                   ((uint32_t)0x00040000U)        /*!< erase/program protection of sector 14 */
220 #define OB_WP_15                   ((uint32_t)0x00080000U)        /*!< erase/program protection of sector 15 */
221 #define OB_WP_16                   ((uint32_t)0x00100000U)        /*!< erase/program protection of sector 16 */
222 #define OB_WP_17                   ((uint32_t)0x00200000U)        /*!< erase/program protection of sector 17 */
223 #define OB_WP_18                   ((uint32_t)0x00400000U)        /*!< erase/program protection of sector 18 */
224 #define OB_WP_19                   ((uint32_t)0x00800000U)        /*!< erase/program protection of sector 19 */
225 #define OB_WP_20                   ((uint32_t)0x01000000U)        /*!< erase/program protection of sector 20 */
226 #define OB_WP_21                   ((uint32_t)0x02000000U)        /*!< erase/program protection of sector 21 */
227 #define OB_WP_22                   ((uint32_t)0x04000000U)        /*!< erase/program protection of sector 22 */
228 #define OB_WP_23_27                ((uint32_t)0x08000000U)        /*!< erase/program protection of sector 23~27 */
229 #define OB_WP_ALL                  ((uint32_t)0x0FFF0FFFU)        /*!< erase/program protection of all sectors */
230 
231 /* option bytes D-bus read protection */
232 #define OB_DRP_0                   ((uint32_t)0x00000001U)        /*!< D-bus read protection protection of sector 0  */
233 #define OB_DRP_1                   ((uint32_t)0x00000002U)        /*!< D-bus read protection protection of sector 1  */
234 #define OB_DRP_2                   ((uint32_t)0x00000004U)        /*!< D-bus read protection protection of sector 2  */
235 #define OB_DRP_3                   ((uint32_t)0x00000008U)        /*!< D-bus read protection protection of sector 3  */
236 #define OB_DRP_4                   ((uint32_t)0x00000010U)        /*!< D-bus read protection protection of sector 4  */
237 #define OB_DRP_5                   ((uint32_t)0x00000020U)        /*!< D-bus read protection protection of sector 5  */
238 #define OB_DRP_6                   ((uint32_t)0x00000040U)        /*!< D-bus read protection protection of sector 6  */
239 #define OB_DRP_7                   ((uint32_t)0x00000080U)        /*!< D-bus read protection protection of sector 7  */
240 #define OB_DRP_8                   ((uint32_t)0x00000100U)        /*!< D-bus read protection protection of sector 8  */
241 #define OB_DRP_9                   ((uint32_t)0x00000200U)        /*!< D-bus read protection protection of sector 9  */
242 #define OB_DRP_10                  ((uint32_t)0x00000400U)        /*!< D-bus read protection protection of sector 10 */
243 #define OB_DRP_11                  ((uint32_t)0x00000800U)        /*!< D-bus read protection protection of sector 11 */
244 #define OB_DRP_12                  ((uint32_t)0x00010000U)        /*!< D-bus read protection protection of sector 12 */
245 #define OB_DRP_13                  ((uint32_t)0x00020000U)        /*!< D-bus read protection protection of sector 13 */
246 #define OB_DRP_14                  ((uint32_t)0x00040000U)        /*!< D-bus read protection protection of sector 14 */
247 #define OB_DRP_15                  ((uint32_t)0x00080000U)        /*!< D-bus read protection protection of sector 15 */
248 #define OB_DRP_16                  ((uint32_t)0x00100000U)        /*!< D-bus read protection protection of sector 16 */
249 #define OB_DRP_17                  ((uint32_t)0x00200000U)        /*!< D-bus read protection protection of sector 17 */
250 #define OB_DRP_18                  ((uint32_t)0x00400000U)        /*!< D-bus read protection protection of sector 18 */
251 #define OB_DRP_19                  ((uint32_t)0x00800000U)        /*!< D-bus read protection protection of sector 19 */
252 #define OB_DRP_20                  ((uint32_t)0x01000000U)        /*!< D-bus read protection protection of sector 20 */
253 #define OB_DRP_21                  ((uint32_t)0x02000000U)        /*!< D-bus read protection protection of sector 21 */
254 #define OB_DRP_22                  ((uint32_t)0x04000000U)        /*!< D-bus read protection protection of sector 22 */
255 #define OB_DRP_23_27               ((uint32_t)0x08000000U)        /*!< D-bus read protection protection of sector 23~27 */
256 #define OB_DRP_ALL                 ((uint32_t)0x0FFF0FFFU)        /*!< D-bus read protection protection of all sectors */
257 
258 /* double banks or single bank selection when flash size is 1M bytes */
259 #define OBCTL0_DBS(regval)         (BIT(30) & ((uint32_t)(regval) << 30U))
260 #define OB_DBS_DISABLE             OBCTL0_DBS(0)                  /*!< single bank when flash size is 1M bytes */
261 #define OB_DBS_ENABLE              OBCTL0_DBS(1)                  /*!< double bank when flash size is 1M bytes */
262 
263 /* option bytes D-bus read protection mode */
264 #define OBCTL0_DRP(regval)         (BIT(31) & ((uint32_t)(regval) << 31U))
265 #define OB_DRP_DISABLE             OBCTL0_DRP(0)                  /*!< the WPx bits used as erase/program protection of each sector */
266 #define OB_DRP_ENABLE              OBCTL0_DRP(1)                  /*!< the WPx bits used as erase/program protection and D-bus read protection of each sector */
267 
268 /* FMC sectors */
269 #define CTL_SN(regval)             (BITS(3,7) & ((uint32_t)(regval))<< 3)
270 #define CTL_SECTOR_NUMBER_0        CTL_SN(0)                      /*!< sector 0   */
271 #define CTL_SECTOR_NUMBER_1        CTL_SN(1)                      /*!< sector 1   */
272 #define CTL_SECTOR_NUMBER_2        CTL_SN(2)                      /*!< sector 2   */
273 #define CTL_SECTOR_NUMBER_3        CTL_SN(3)                      /*!< sector 3   */
274 #define CTL_SECTOR_NUMBER_4        CTL_SN(4)                      /*!< sector 4   */
275 #define CTL_SECTOR_NUMBER_5        CTL_SN(5)                      /*!< sector 5   */
276 #define CTL_SECTOR_NUMBER_6        CTL_SN(6)                      /*!< sector 6   */
277 #define CTL_SECTOR_NUMBER_7        CTL_SN(7)                      /*!< sector 7   */
278 #define CTL_SECTOR_NUMBER_8        CTL_SN(8)                      /*!< sector 8   */
279 #define CTL_SECTOR_NUMBER_9        CTL_SN(9)                      /*!< sector 9   */
280 #define CTL_SECTOR_NUMBER_10       CTL_SN(10)                     /*!< sector 10  */
281 #define CTL_SECTOR_NUMBER_11       CTL_SN(11)                     /*!< sector 11  */
282 #define CTL_SECTOR_NUMBER_24       CTL_SN(12)                     /*!< sector 24  */
283 #define CTL_SECTOR_NUMBER_25       CTL_SN(13)                     /*!< sector 25  */
284 #define CTL_SECTOR_NUMBER_26       CTL_SN(14)                     /*!< sector 26  */
285 #define CTL_SECTOR_NUMBER_27       CTL_SN(15)                     /*!< sector 27  */
286 #define CTL_SECTOR_NUMBER_12       CTL_SN(16)                     /*!< sector 12  */
287 #define CTL_SECTOR_NUMBER_13       CTL_SN(17)                     /*!< sector 13  */
288 #define CTL_SECTOR_NUMBER_14       CTL_SN(18)                     /*!< sector 14  */
289 #define CTL_SECTOR_NUMBER_15       CTL_SN(19)                     /*!< sector 15  */
290 #define CTL_SECTOR_NUMBER_16       CTL_SN(20)                     /*!< sector 16  */
291 #define CTL_SECTOR_NUMBER_17       CTL_SN(21)                     /*!< sector 17  */
292 #define CTL_SECTOR_NUMBER_18       CTL_SN(22)                     /*!< sector 18  */
293 #define CTL_SECTOR_NUMBER_19       CTL_SN(23)                     /*!< sector 19  */
294 #define CTL_SECTOR_NUMBER_20       CTL_SN(24)                     /*!< sector 20  */
295 #define CTL_SECTOR_NUMBER_21       CTL_SN(25)                     /*!< sector 21  */
296 #define CTL_SECTOR_NUMBER_22       CTL_SN(26)                     /*!< sector 22  */
297 #define CTL_SECTOR_NUMBER_23       CTL_SN(27)                     /*!< sector 23  */
298 
299 
300 /* FMC program size */
301 #define CTL_PSZ(regval)            (BITS(8,9) & ((uint32_t)(regval))<< 8U)
302 #define CTL_PSZ_BYTE               CTL_PSZ(0)                     /*!< FMC program by byte access */
303 #define CTL_PSZ_HALF_WORD          CTL_PSZ(1)                     /*!< FMC program by half-word access */
304 #define CTL_PSZ_WORD               CTL_PSZ(2)                     /*!< FMC program by word access */
305 
306 /* FMC interrupt enable */
307 #define FMC_INT_END                ((uint32_t)0x01000000U)        /*!< enable FMC end of program interrupt */
308 #define FMC_INT_ERR                ((uint32_t)0x02000000U)        /*!< enable FMC error interrupt */
309 
310 /* FMC flags */
311 #define FMC_FLAG_END               FMC_STAT_END                   /*!< FMC end of operation flag bit */
312 #define FMC_FLAG_OPERR             FMC_STAT_OPERR                 /*!< FMC operation error flag bit */
313 #define FMC_FLAG_WPERR             FMC_STAT_WPERR                 /*!< FMC erase/program protection error flag bit */
314 #define FMC_FLAG_PGMERR            FMC_STAT_PGMERR                /*!< FMC program size not match error flag bit */
315 #define FMC_FLAG_PGSERR            FMC_STAT_PGSERR                /*!< FMC program sequence error flag bit */
316 #define FMC_FLAG_RDDERR            FMC_STAT_RDDERR                /*!< FMC read D-bus protection error flag bit */
317 #define FMC_FLAG_BUSY              FMC_STAT_BUSY                  /*!< FMC busy flag */
318 
319 /* FMC interrupt flags */
320 #define FMC_INT_FLAG_END           FMC_STAT_END                   /*!< FMC end of operation interrupt flag */
321 #define FMC_INT_FLAG_OPERR         FMC_STAT_OPERR                 /*!< FMC operation error interrupt flag */
322 #define FMC_INT_FLAG_WPERR         FMC_STAT_WPERR                 /*!< FMC erase/program protection error interrupt flag */
323 #define FMC_INT_FLAG_PGMERR        FMC_STAT_PGMERR                /*!< FMC program size not match error interrupt flag */
324 #define FMC_INT_FLAG_PGSERR        FMC_STAT_PGSERR                /*!< FMC program sequence error interrupt flag */
325 #define FMC_INT_FLAG_RDDERR        FMC_STAT_RDDERR                /*!< FMC read D-bus protection error interrupt flag */
326 
327 
328 /* FMC time out */
329 #define FMC_TIMEOUT_COUNT          ((uint32_t)0x4FFFFFFFU)        /*!< count to judge of FMC timeout */
330 
331 /* function declarations */
332 /* FMC main memory programming functions */
333 /* set the FMC wait state counter */
334 void fmc_wscnt_set(uint32_t wscnt);
335 /* unlock the main FMC operation */
336 void fmc_unlock(void);
337 /* lock the main FMC operation */
338 void fmc_lock(void);
339 #if defined (GD32F425) || defined (GD32F427) || defined (GD32F470)
340 /* FMC erase page */
341 fmc_state_enum fmc_page_erase(uint32_t page_addr);
342 #endif
343 /* FMC erase sector */
344 fmc_state_enum fmc_sector_erase(uint32_t fmc_sector);
345 /* FMC erase whole chip */
346 fmc_state_enum fmc_mass_erase(void);
347 /* FMC erase whole bank0 */
348 fmc_state_enum fmc_bank0_erase(void);
349 /* FMC erase whole bank1 */
350 fmc_state_enum fmc_bank1_erase(void);
351 /* FMC program a word at the corresponding address */
352 fmc_state_enum fmc_word_program(uint32_t address, uint32_t data);
353 /* FMC program a half word at the corresponding address */
354 fmc_state_enum fmc_halfword_program(uint32_t address, uint16_t data);
355 /* FMC program a byte at the corresponding address */
356 fmc_state_enum fmc_byte_program(uint32_t address, uint8_t data);
357 
358 /* FMC option bytes programming functions */
359 /* unlock the option byte operation */
360 void ob_unlock(void);
361 /* lock the option byte operation */
362 void ob_lock(void);
363 /* send option byte change command */
364 void ob_start(void);
365 /* erase option byte */
366 void ob_erase(void);
367 /* enable write protect */
368 ErrStatus ob_write_protection_enable(uint32_t ob_wp);
369 /* disable write protect */
370 ErrStatus ob_write_protection_disable(uint32_t ob_wp);
371 /* enable erase/program protection and D-bus read protection */
372 void ob_drp_enable(uint32_t ob_drp);
373 /* disable erase/program protection and D-bus read protection */
374 void ob_drp_disable(void);
375 /* set the option byte security protection level */
376 void ob_security_protection_config(uint8_t ob_spc);
377 /* write the FMC option byte user */
378 void ob_user_write(uint32_t ob_fwdgt, uint32_t ob_deepsleep, uint32_t ob_stdby);
379 /* option byte BOR threshold value */
380 void ob_user_bor_threshold(uint32_t ob_bor_th);
381 /* configure the boot mode */
382 void ob_boot_mode_config(uint32_t boot_mode);
383 /* get the FMC option byte user */
384 uint8_t ob_user_get(void);
385 /* get the FMC option byte write protection */
386 uint16_t ob_write_protection0_get(void);
387 /* get the FMC option byte write protection */
388 uint16_t ob_write_protection1_get(void);
389 /* get the FMC erase/program protection and D-bus read protection option bytes value */
390 uint16_t ob_drp0_get(void);
391 /* get the FMC erase/program protection and D-bus read protection option bytes value */
392 uint16_t ob_drp1_get(void);
393 /* get option byte security protection code value */
394 FlagStatus ob_spc_get(void);
395 /* get the FMC threshold value */
396 uint8_t ob_user_bor_threshold_get(void);
397 
398 /* FMC interrupts and flags management functions */
399 /* get flag set or reset */
400 FlagStatus fmc_flag_get(uint32_t fmc_flag);
401 /* clear the FMC pending flag */
402 void fmc_flag_clear(uint32_t fmc_flag);
403 /* enable FMC interrupt */
404 void fmc_interrupt_enable(uint32_t fmc_int);
405 /* disable FMC interrupt */
406 void fmc_interrupt_disable(uint32_t fmc_int);
407 /* get FMC interrupt flag set or reset */
408 FlagStatus fmc_interrupt_flag_get(uint32_t fmc_int_flag);
409 /* clear the FMC interrupt flag */
410 void fmc_interrupt_flag_clear(uint32_t fmc_int_flag);
411 /* return the FMC state */
412 fmc_state_enum fmc_state_get(void);
413 /* check FMC ready or not */
414 fmc_state_enum fmc_ready_wait(uint32_t timeout);
415 
416 #endif /* GD32F4XX_FMC_H */
417