1 /*!
2     \file    gd32f4xx_syscfg.c
3     \brief   SYSCFG driver
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 2022-03-09, V3.0.0, firmware for GD32F4xx
9 */
10 
11 /*
12     Copyright (c) 2022, 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 #include "gd32f4xx_syscfg.h"
39 
40 /*!
41     \brief    reset the SYSCFG registers
42     \param[in]  none
43     \param[out] none
44     \retval     none
45 */
syscfg_deinit(void)46 void syscfg_deinit(void)
47 {
48     rcu_periph_reset_enable(RCU_SYSCFGRST);
49     rcu_periph_reset_disable(RCU_SYSCFGRST);
50 }
51 
52 /*!
53     \brief    configure the boot mode
54     \param[in]  syscfg_bootmode: selects the memory remapping
55                 only one parameter can be selected which is shown as below:
56       \arg        SYSCFG_BOOTMODE_FLASH: main flash memory (0x08000000~0x083BFFFF) is mapped at address 0x00000000
57       \arg        SYSCFG_BOOTMODE_BOOTLOADER: boot loader (0x1FFF0000 - 0x1FFF77FF) is mapped at address 0x00000000
58       \arg        SYSCFG_BOOTMODE_EXMC_SRAM: SRAM/NOR 0 and 1 of EXMC (0x60000000~0x67FFFFFF) is mapped at address 0x00000000
59       \arg        SYSCFG_BOOTMODE_SRAM: SRAM0 of on-chip SRAM (0x20000000~0x2001BFFF) is mapped at address 0x00000000
60       \arg        SYSCFG_BOOTMODE_EXMC_SDRAM: SDRAM bank0 of EXMC (0xC0000000~0xC7FFFFFF) is mapped at address 0x00000000
61     \param[out] none
62     \retval     none
63 */
syscfg_bootmode_config(uint8_t syscfg_bootmode)64 void syscfg_bootmode_config(uint8_t syscfg_bootmode)
65 {
66     /* reset the SYSCFG_CFG0_BOOT_MODE bit and set according to syscfg_bootmode */
67     SYSCFG_CFG0 &= ~SYSCFG_CFG0_BOOT_MODE;
68     SYSCFG_CFG0 |= (uint32_t)syscfg_bootmode;
69 }
70 
71 /*!
72     \brief    FMC memory mapping swap
73     \param[in]  syscfg_fmc_swap: selects the interal flash bank swapping
74                 only one parameter can be selected which is shown as below:
75       \arg        SYSCFG_FMC_SWP_BANK0: bank 0 is mapped at address 0x08000000 and bank 1 is mapped at address 0x08100000
76       \arg        SYSCFG_FMC_SWP_BANK1: bank 1 is mapped at address 0x08000000 and bank 0 is mapped at address 0x08100000
77     \param[out] none
78     \retval     none
79 */
syscfg_fmc_swap_config(uint32_t syscfg_fmc_swap)80 void syscfg_fmc_swap_config(uint32_t syscfg_fmc_swap)
81 {
82     uint32_t reg;
83     reg = SYSCFG_CFG0;
84     /* reset the FMC_SWP bit and set according to syscfg_fmc_swap */
85     reg &= ~SYSCFG_CFG0_FMC_SWP;
86     SYSCFG_CFG0 = (reg | syscfg_fmc_swap);
87 }
88 
89 /*!
90     \brief    EXMC memory mapping swap
91     \param[in]  syscfg_exmc_swap: selects the memories in EXMC swapping
92                 only one parameter can be selected which is shown as below:
93       \arg        SYSCFG_EXMC_SWP_ENABLE: SDRAM bank 0 and bank 1 are swapped with NAND bank 1 and PC card
94       \arg        SYSCFG_EXMC_SWP_DISABLE: no memory mapping swap
95     \param[out] none
96     \retval     none
97 */
syscfg_exmc_swap_config(uint32_t syscfg_exmc_swap)98 void syscfg_exmc_swap_config(uint32_t syscfg_exmc_swap)
99 {
100     uint32_t reg;
101 
102     reg = SYSCFG_CFG0;
103     /* reset the SYSCFG_CFG0_EXMC_SWP bits and set according to syscfg_exmc_swap */
104     reg &= ~SYSCFG_CFG0_EXMC_SWP;
105     SYSCFG_CFG0 = (reg | syscfg_exmc_swap);
106 }
107 
108 /*!
109     \brief    configure the GPIO pin as EXTI Line
110     \param[in]  exti_port: specify the GPIO port used in EXTI
111                 only one parameter can be selected which is shown as below:
112       \arg        EXTI_SOURCE_GPIOx(x = A,B,C,D,E,F,G,H,I): EXTI GPIO port
113     \param[in]  exti_pin: specify the EXTI line
114                 only one parameter can be selected which is shown as below:
115       \arg        EXTI_SOURCE_PINx(x = 0..15): EXTI GPIO pin
116     \param[out] none
117     \retval     none
118 */
syscfg_exti_line_config(uint8_t exti_port,uint8_t exti_pin)119 void syscfg_exti_line_config(uint8_t exti_port, uint8_t exti_pin)
120 {
121     uint32_t clear_exti_mask = ~((uint32_t)EXTI_SS_MASK << (EXTI_SS_MSTEP(exti_pin)));
122     uint32_t config_exti_mask = ((uint32_t)exti_port) << (EXTI_SS_MSTEP(exti_pin));
123 
124     switch(exti_pin / EXTI_SS_JSTEP) {
125     case EXTISS0:
126         /* clear EXTI source line(0..3) */
127         SYSCFG_EXTISS0 &= clear_exti_mask;
128         /* configure EXTI soure line(0..3) */
129         SYSCFG_EXTISS0 |= config_exti_mask;
130         break;
131     case EXTISS1:
132         /* clear EXTI soure line(4..7) */
133         SYSCFG_EXTISS1 &= clear_exti_mask;
134         /* configure EXTI soure line(4..7) */
135         SYSCFG_EXTISS1 |= config_exti_mask;
136         break;
137     case EXTISS2:
138         /* clear EXTI soure line(8..11) */
139         SYSCFG_EXTISS2 &= clear_exti_mask;
140         /* configure EXTI soure line(8..11) */
141         SYSCFG_EXTISS2 |= config_exti_mask;
142         break;
143     case EXTISS3:
144         /* clear EXTI soure line(12..15) */
145         SYSCFG_EXTISS3 &= clear_exti_mask;
146         /* configure EXTI soure line(12..15) */
147         SYSCFG_EXTISS3 |= config_exti_mask;
148         break;
149     default:
150         break;
151     }
152 }
153 
154 /*!
155     \brief    configure the PHY interface for the ethernet MAC
156     \param[in]  syscfg_enet_phy_interface: specifies the media interface mode.
157                 only one parameter can be selected which is shown as below:
158       \arg        SYSCFG_ENET_PHY_MII: MII mode is selected
159       \arg        SYSCFG_ENET_PHY_RMII: RMII mode is selected
160     \param[out] none
161     \retval     none
162 */
syscfg_enet_phy_interface_config(uint32_t syscfg_enet_phy_interface)163 void syscfg_enet_phy_interface_config(uint32_t syscfg_enet_phy_interface)
164 {
165     uint32_t reg;
166 
167     reg = SYSCFG_CFG1;
168     /* reset the ENET_PHY_SEL bit and set according to syscfg_enet_phy_interface */
169     reg &= ~SYSCFG_CFG1_ENET_PHY_SEL;
170     SYSCFG_CFG1 = (reg | syscfg_enet_phy_interface);
171 }
172 
173 /*!
174     \brief    configure the I/O compensation cell
175     \param[in]  syscfg_compensation: specifies the I/O compensation cell mode
176                 only one parameter can be selected which is shown as below:
177       \arg        SYSCFG_COMPENSATION_ENABLE: I/O compensation cell is enabled
178       \arg        SYSCFG_COMPENSATION_DISABLE: I/O compensation cell is disabled
179     \param[out] none
180     \retval     none
181 */
syscfg_compensation_config(uint32_t syscfg_compensation)182 void syscfg_compensation_config(uint32_t syscfg_compensation)
183 {
184     uint32_t reg;
185 
186     reg = SYSCFG_CPSCTL;
187     /* reset the SYSCFG_CPSCTL_CPS_EN bit and set according to syscfg_compensation */
188     reg &= ~SYSCFG_CPSCTL_CPS_EN;
189     SYSCFG_CPSCTL = (reg | syscfg_compensation);
190 }
191 
192 /*!
193     \brief    checks whether the I/O compensation cell ready flag is set or not
194     \param[in]  none
195     \param[out] none
196     \retval     FlagStatus: SET or RESET
197   */
syscfg_flag_get(void)198 FlagStatus syscfg_flag_get(void)
199 {
200     if(((uint32_t)RESET) != (SYSCFG_CPSCTL & SYSCFG_CPSCTL_CPS_RDY)) {
201         return SET;
202     } else {
203         return RESET;
204     }
205 }
206