1 /*! 2 \file gd32e50x_fmc.h 3 \brief definitions for the FMC 4 5 \version 2020-03-10, V1.0.0, firmware for GD32E50x 6 \version 2020-08-26, V1.1.0, firmware for GD32E50x 7 \version 2020-12-31, V1.1.1, firmware for GD32E50x 8 \version 2021-03-23, V1.2.0, firmware for GD32E50x 9 */ 10 11 /* 12 Copyright (c) 2021, GigaDevice Semiconductor Inc. 13 14 Redistribution and use in source and binary forms, with or without modification, 15 are permitted provided that the following conditions are met: 16 17 1. Redistributions of source code must retain the above copyright notice, this 18 list of conditions and the following disclaimer. 19 2. Redistributions in binary form must reproduce the above copyright notice, 20 this list of conditions and the following disclaimer in the documentation 21 and/or other materials provided with the distribution. 22 3. Neither the name of the copyright holder nor the names of its contributors 23 may be used to endorse or promote products derived from this software without 24 specific prior written permission. 25 26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 30 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 33 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 35 OF SUCH DAMAGE. 36 */ 37 38 #ifndef GD32E50X_FMC_H 39 #define GD32E50X_FMC_H 40 41 #include "gd32e50x.h" 42 43 /* FMC and option bytes definition */ 44 #define FMC FMC_BASE /*!< FMC base address */ 45 #define OB OB_BASE /*!< option bytes base address */ 46 47 /* registers definitions */ 48 #define FMC_WS REG32(FMC + 0x00000000U) /*!< FMC wait state register */ 49 #define FMC_KEY REG32(FMC + 0x00000004U) /*!< FMC unlock key register */ 50 #define FMC_OBKEY REG32(FMC + 0x00000008U) /*!< FMC option bytes unlock key register */ 51 #define FMC_STAT REG32(FMC + 0x0000000CU) /*!< FMC status register */ 52 #define FMC_CTL REG32(FMC + 0x00000010U) /*!< FMC control register */ 53 #define FMC_ADDR REG32(FMC + 0x00000014U) /*!< FMC address register */ 54 #define FMC_OBSTAT REG32(FMC + 0x0000001CU) /*!< FMC option bytes status register */ 55 #define FMC_WP REG32(FMC + 0x00000020U) /*!< FMC erase/program protection register */ 56 #define FMC_PID REG32(FMC + 0x00000100U) /*!< FMC product ID register */ 57 58 #define OP_BYTE(x) REG32(OB + ((uint32_t)((uint32_t)0x04U * (x)))) /*!< option bytes value */ 59 #define OB_SPC_USER REG32(OB + 0x00000000U) /*!< option bytes security protection value and user value */ 60 #define OB_DATA REG32(OB + 0x00000004U) /*!< option bytes data value */ 61 #define OB_WP0 REG32(OB + 0x00000008U) /*!< option bytes write protection value 0 */ 62 #define OB_WP1 REG32(OB + 0x0000000CU) /*!< option bytes write protection value 1 */ 63 64 /* bits definitions */ 65 /* FMC_WS */ 66 #define FMC_WS_WSCNT BITS(0,2) /*!< wait state counter */ 67 #define FMC_WS_PFEN BIT(4) /*!< pre-fetch enable */ 68 #define FMC_WS_ICEN BIT(9) /*!< IBUS cache enable */ 69 #define FMC_WS_DCEN BIT(10) /*!< DBUS cache enable */ 70 #define FMC_WS_ICRST BIT(11) /*!< IBUS cache reset */ 71 #define FMC_WS_DCRST BIT(12) /*!< DBUS cache reset */ 72 73 /* FMC_KEY */ 74 #define FMC_KEY_KEY BITS(0,31) /*!< FMC_CTL unlock key */ 75 76 /* FMC_OBKEY */ 77 #define FMC_OBKEY_OBKEY BITS(0,31) /*!< option bytes unlock key */ 78 79 /* FMC_STAT */ 80 #define FMC_STAT_BUSY BIT(0) /*!< flash busy flag */ 81 #define FMC_STAT_PGERR BIT(2) /*!< flash program error flag */ 82 #define FMC_STAT_PGAERR BIT(3) /*!< flash program alignment error flag */ 83 #define FMC_STAT_WPERR BIT(4) /*!< erase/program protection error flag */ 84 #define FMC_STAT_ENDF BIT(5) /*!< end of operation flag */ 85 86 /* FMC_CTL */ 87 #define FMC_CTL_PG BIT(0) /*!< main flash program command */ 88 #define FMC_CTL_PER BIT(1) /*!< main flash page erase command */ 89 #define FMC_CTL_MER BIT(2) /*!< main flash mass erase command */ 90 #define FMC_CTL_OBPG BIT(4) /*!< option bytes program command */ 91 #define FMC_CTL_OBER BIT(5) /*!< option bytes erase command */ 92 #define FMC_CTL_START BIT(6) /*!< send erase command to FMC */ 93 #define FMC_CTL_LK BIT(7) /*!< FMC_CTL lock */ 94 #define FMC_CTL_OBWEN BIT(9) /*!< option bytes erase/program enable */ 95 #define FMC_CTL_ERRIE BIT(10) /*!< error interrupt enable */ 96 #define FMC_CTL_ENDIE BIT(12) /*!< end of operation interrupt enable */ 97 98 /* FMC_ADDR */ 99 #define FMC_ADDR_ADDR BITS(0,31) /*!< address of flash to be erased/programmed */ 100 101 /* FMC_OBSTAT */ 102 #define FMC_OBSTAT_OBERR BIT(0) /*!< option bytes read error */ 103 #define FMC_OBSTAT_SPC BIT(1) /*!< option bytes security protection code */ 104 #define FMC_OBSTAT_USER BITS(2,9) /*!< store USER of option bytes block after system reset */ 105 #define FMC_OBSTAT_DATA BITS(10,25) /*!< store DATA of option bytes block after system reset */ 106 107 /* FMC_WP */ 108 #define FMC_WP_WP BITS(0,31) /*!< store WP of option bytes block after system reset */ 109 110 /* FMC_PID */ 111 #define FMC_PID_PID BITS(0,31) /*!< product ID */ 112 113 /* constants definitions */ 114 /* fmc state */ 115 typedef enum 116 { 117 FMC_READY, /*!< the operation has been completed */ 118 FMC_BUSY, /*!< the operation is in progress */ 119 FMC_PGERR, /*!< program error */ 120 FMC_PGAERR, /*!< program alignment error */ 121 FMC_WPERR, /*!< erase/program protection error */ 122 FMC_TOERR, /*!< timeout error */ 123 FMC_OB_HSPC, /*!< high security protection */ 124 }fmc_state_enum; 125 126 /* unlock key */ 127 #define UNLOCK_KEY0 ((uint32_t)0x45670123U) /*!< unlock key 0 */ 128 #define UNLOCK_KEY1 ((uint32_t)0xCDEF89ABU) /*!< unlock key 1 */ 129 130 /* FMC wait state added */ 131 #define WS_WSCNT(regval) (BITS(0,2) & ((uint32_t)(regval))) 132 #define FMC_WAIT_STATE_0 WS_WSCNT(0) /*!< 0 wait state added */ 133 #define FMC_WAIT_STATE_1 WS_WSCNT(1) /*!< 1 wait state added */ 134 #define FMC_WAIT_STATE_2 WS_WSCNT(2) /*!< 2 wait state added */ 135 #define FMC_WAIT_STATE_3 WS_WSCNT(3) /*!< 3 wait state added */ 136 #define FMC_WAIT_STATE_4 WS_WSCNT(4) /*!< 4 wait state added */ 137 138 /* read protection configuration */ 139 #define FMC_NSPC ((uint8_t)0xA5U) /*!< no security protection */ 140 #define FMC_LSPC ((uint8_t)0xBBU) /*!< low security protection */ 141 #define FMC_HSPC ((uint8_t)0xCCU) /*!< high security protection */ 142 143 /* option bytes software/hardware free watch dog timer */ 144 #define OB_FWDGT_HW ((uint8_t)0x00U) /*!< hardware free watchdog */ 145 #define OB_FWDGT_SW ((uint8_t)0x01U) /*!< software free watchdog */ 146 147 /* option bytes reset or not entering deep sleep mode */ 148 #define OB_DEEPSLEEP_RST ((uint8_t)0x00U) /*!< generate a reset instead of entering deepsleep mode */ 149 #define OB_DEEPSLEEP_NRST ((uint8_t)0x02U) /*!< no reset when entering deepsleep mode */ 150 151 /* option bytes reset or not entering standby mode */ 152 #define OB_STDBY_RST ((uint8_t)0x00U) /*!< generate a reset instead of entering standby mode */ 153 #define OB_STDBY_NRST ((uint8_t)0x04U) /*!< no reset when entering standby mode */ 154 155 /* option bytes BOR threshold value */ 156 #define OB_BOR_TH_VALUE3 ((uint8_t)0x00U) /*!< BOR threshold value 3 */ 157 #define OB_BOR_TH_VALUE2 ((uint8_t)0x40U) /*!< BOR threshold value 2 */ 158 #define OB_BOR_TH_VALUE1 ((uint8_t)0x80U) /*!< BOR threshold value 1 */ 159 #define OB_BOR_TH_OFF ((uint8_t)0xC0U) /*!< no BOR function */ 160 161 /* option bytes write protection */ 162 #define OB_WP_NONE ((uint32_t)0x00000000U) /*!< disable all erase/program protection */ 163 #define OB_WP_0 ((uint32_t)0x00000001U) /*!< erase/program protection of sector 0 */ 164 #define OB_WP_1 ((uint32_t)0x00000002U) /*!< erase/program protection of sector 1 */ 165 #define OB_WP_2 ((uint32_t)0x00000004U) /*!< erase/program protection of sector 2 */ 166 #define OB_WP_3 ((uint32_t)0x00000008U) /*!< erase/program protection of sector 3 */ 167 #define OB_WP_4 ((uint32_t)0x00000010U) /*!< erase/program protection of sector 4 */ 168 #define OB_WP_5 ((uint32_t)0x00000020U) /*!< erase/program protection of sector 5 */ 169 #define OB_WP_6 ((uint32_t)0x00000040U) /*!< erase/program protection of sector 6 */ 170 #define OB_WP_7 ((uint32_t)0x00000080U) /*!< erase/program protection of sector 7 */ 171 #define OB_WP_8 ((uint32_t)0x00000100U) /*!< erase/program protection of sector 8 */ 172 #define OB_WP_9 ((uint32_t)0x00000200U) /*!< erase/program protection of sector 9 */ 173 #define OB_WP_10 ((uint32_t)0x00000400U) /*!< erase/program protection of sector 10 */ 174 #define OB_WP_11 ((uint32_t)0x00000800U) /*!< erase/program protection of sector 11 */ 175 #define OB_WP_12 ((uint32_t)0x00001000U) /*!< erase/program protection of sector 12 */ 176 #define OB_WP_13 ((uint32_t)0x00002000U) /*!< erase/program protection of sector 13 */ 177 #define OB_WP_14 ((uint32_t)0x00004000U) /*!< erase/program protection of sector 14 */ 178 #define OB_WP_15 ((uint32_t)0x00008000U) /*!< erase/program protection of sector 15 */ 179 #define OB_WP_16 ((uint32_t)0x00010000U) /*!< erase/program protection of sector 16 */ 180 #define OB_WP_17 ((uint32_t)0x00020000U) /*!< erase/program protection of sector 17 */ 181 #define OB_WP_18 ((uint32_t)0x00040000U) /*!< erase/program protection of sector 18 */ 182 #define OB_WP_19 ((uint32_t)0x00080000U) /*!< erase/program protection of sector 19 */ 183 #define OB_WP_20 ((uint32_t)0x00100000U) /*!< erase/program protection of sector 20 */ 184 #define OB_WP_21 ((uint32_t)0x00200000U) /*!< erase/program protection of sector 21 */ 185 #define OB_WP_22 ((uint32_t)0x00400000U) /*!< erase/program protection of sector 22 */ 186 #define OB_WP_23 ((uint32_t)0x00800000U) /*!< erase/program protection of sector 23 */ 187 #define OB_WP_24 ((uint32_t)0x01000000U) /*!< erase/program protection of sector 24 */ 188 #define OB_WP_25 ((uint32_t)0x02000000U) /*!< erase/program protection of sector 25 */ 189 #define OB_WP_26 ((uint32_t)0x04000000U) /*!< erase/program protection of sector 26 */ 190 #define OB_WP_27 ((uint32_t)0x08000000U) /*!< erase/program protection of sector 27 */ 191 #define OB_WP_28 ((uint32_t)0x10000000U) /*!< erase/program protection of sector 28 */ 192 #define OB_WP_29 ((uint32_t)0x20000000U) /*!< erase/program protection of sector 29 */ 193 #define OB_WP_30 ((uint32_t)0x40000000U) /*!< erase/program protection of sector 30 */ 194 #define OB_WP_31 ((uint32_t)0x80000000U) /*!< erase/program protection of sector 31 ~ 63 */ 195 #define OB_WP_ALL ((uint32_t)0xFFFFFFFFU) /*!< erase/program protection of all sectors */ 196 197 /* FMC interrupt enable */ 198 #define FMC_INT_ERR FMC_CTL_ERRIE /*!< FMC error interrupt enable */ 199 #define FMC_INT_END FMC_CTL_ENDIE /*!< FMC end of operation interrupt enable */ 200 201 /* FMC flags */ 202 #define FMC_FLAG_BUSY FMC_STAT_BUSY /*!< FMC busy flag */ 203 #define FMC_FLAG_PGERR FMC_STAT_PGERR /*!< FMC operation error flag */ 204 #define FMC_FLAG_PGAERR FMC_STAT_PGAERR /*!< FMC program alignment error flag */ 205 #define FMC_FLAG_WPERR FMC_STAT_WPERR /*!< FMC erase/program protection error flag */ 206 #define FMC_FLAG_END FMC_STAT_ENDF /*!< FMC end of operation flag */ 207 208 /* FMC interrupt flags */ 209 #define FMC_INT_FLAG_PGERR FMC_STAT_PGERR /*!< FMC operation error interrupt flag */ 210 #define FMC_INT_FLAG_PGAERR FMC_STAT_PGAERR /*!< FMC program alignment error interrupt flag */ 211 #define FMC_INT_FLAG_WPERR FMC_STAT_WPERR /*!< FMC erase/program protection error interrupt flag */ 212 #define FMC_INT_FLAG_END FMC_STAT_ENDF /*!< FMC end of operation interrupt flag */ 213 214 /* FMC timeout */ 215 #define FMC_TIMEOUT_COUNT ((uint32_t)0x00100000U) /*!< FMC timeout count value */ 216 217 /* function declarations */ 218 /* FMC main memory programming functions */ 219 /* unlock the main FMC operation */ 220 void fmc_unlock(void); 221 /* lock the main FMC operation */ 222 void fmc_lock(void); 223 /* set the wait state */ 224 void fmc_wscnt_set(uint32_t wscnt); 225 /* enable pre-fetch */ 226 void fmc_prefetch_enable(void); 227 /* disable pre-fetch */ 228 void fmc_prefetch_disable(void); 229 /* enable IBUS cache */ 230 void fmc_ibus_enable(void); 231 /* disable IBUS cache */ 232 void fmc_ibus_disable(void); 233 /* reset IBUS cache */ 234 void fmc_ibus_reset(void); 235 /* enable DBUS cache */ 236 void fmc_dbus_enable(void); 237 /* disable DBUS cache */ 238 void fmc_dbus_disable(void); 239 /* reset DBUS cache */ 240 void fmc_dbus_reset(void); 241 /* FMC erase page */ 242 fmc_state_enum fmc_page_erase(uint32_t page_address); 243 /* FMC erase whole chip */ 244 fmc_state_enum fmc_mass_erase(void); 245 /* FMC program a word at the corresponding address */ 246 fmc_state_enum fmc_word_program(uint32_t address, uint32_t data); 247 248 /* FMC option bytes programming functions */ 249 /* unlock the option bytes operation */ 250 void ob_unlock(void); 251 /* lock the option bytes operation */ 252 void ob_lock(void); 253 /* erase the option bytes */ 254 fmc_state_enum ob_erase(void); 255 /* enable write protection */ 256 fmc_state_enum ob_write_protection_enable(uint32_t ob_wp); 257 /* configure the option bytes security protection */ 258 fmc_state_enum ob_security_protection_config(uint8_t ob_spc); 259 /* program option bytes USER */ 260 fmc_state_enum ob_user_write(uint8_t ob_fwdgt, uint8_t ob_deepsleep, uint8_t ob_stdby, uint8_t ob_bor_th); 261 /* program option bytes DATA */ 262 fmc_state_enum ob_data_program(uint16_t ob_data); 263 /* get the value of option bytes USER */ 264 uint8_t ob_user_get(void); 265 /* get the value of option bytes DATA */ 266 uint16_t ob_data_get(void); 267 /* get the value of option bytes write protection */ 268 uint32_t ob_write_protection_get(void); 269 /* get option bytes security protection state */ 270 FlagStatus ob_security_protection_flag_get(void); 271 272 /* FMC interrupts and flags management functions */ 273 /* get FMC flag status */ 274 FlagStatus fmc_flag_get(uint32_t flag); 275 /* clear the FMC flag */ 276 void fmc_flag_clear(uint32_t flag); 277 /* enable FMC interrupt */ 278 void fmc_interrupt_enable(uint32_t interrupt); 279 /* disable FMC interrupt */ 280 void fmc_interrupt_disable(uint32_t interrupt); 281 /* get FMC interrupt flag */ 282 FlagStatus fmc_interrupt_flag_get(uint32_t flag); 283 /* clear FMC interrupt flag */ 284 void fmc_interrupt_flag_clear(uint32_t flag); 285 286 #endif /* GD32E50X_FMC_H */ 287