1 /*!
2  * @file        apm32f0xx_syscfg.c
3  *
4  * @brief       This file contains all the functions for the SYSCFG peripheral
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 #include "apm32f0xx_syscfg.h"
27 
28 /** @addtogroup APM32F0xx_StdPeriphDriver
29   @{
30 */
31 
32 /** @addtogroup SYSCFG_Driver SYSCFG Driver
33   @{
34 */
35 
36 /** @defgroup SYSCFG_Macros Macros
37   @{
38 */
39 
40 /**@} end of group SYSCFG_Macros */
41 
42 /** @defgroup SYSCFG_Enumerations Enumerations
43   @{
44 */
45 
46 /**@} end of group SYSCFG_Enumerations */
47 
48 /** @defgroup SYSCFG_Structures Structures
49   @{
50 */
51 
52 /**@} end of group SYSCFG_Structures */
53 
54 /** @defgroup SYSCFG_Variables Variables
55   @{
56 */
57 
58 /**@} end of group SYSCFG_Variables */
59 
60 /** @defgroup SYSCFG_Functions Functions
61   @{
62 */
63 
64 /*!
65  * @brief        Set SYSCFG CFG0/1 EINTCFG1/2/3/4 register to reset value
66  *
67  * @param        None
68  *
69  * @retval       None
70  */
SYSCFG_Reset(void)71 void SYSCFG_Reset(void)
72 {
73     SYSCFG->CFG1 &= (uint32_t) SYSCFG_CFG1_MEMMODE;
74     SYSCFG->EINTCFG1 = 0;
75     SYSCFG->EINTCFG2 = 0;
76     SYSCFG->EINTCFG3 = 0;
77     SYSCFG->EINTCFG4 = 0;
78     SYSCFG->CFG2 |= (uint32_t) SYSCFG_CFG2_SRAMPEF;
79 }
80 
81 /*!
82  * @brief       SYSCFG Memory Remap selects
83  *
84  * @param       memory: selects the memory remapping
85  *                      The parameter can be one of following values:
86  *                      @arg SYSCFG_MEMORY_REMAP_FMC:      SYSCFG MemoryRemap Flash
87  *                      @arg SYSCFG_MEMORY_REMAP_SYSTEM:   SYSCFG MemoryRemap SystemMemory
88  *                      @arg SYSCFG_MEMORY_REMAP_SRAM:     SYSCFG MemoryRemap SRAM
89  *
90  * @retval      None
91  */
SYSCFG_MemoryRemapSelect(uint8_t memory)92 void SYSCFG_MemoryRemapSelect(uint8_t memory)
93 {
94     SYSCFG->CFG1_B.MMSEL = (uint8_t)memory;
95 }
96 
97 /*!
98  * @brief       Enables SYSCFG DMA Channel Remap
99  *
100  * @param       channel: selects the DMA channels remap.
101  *                       The parameter can be any combination of following values:
102  *                       @arg SYSCFG_DAM_REMAP_ADC:      ADC DMA remap
103  *                       @arg SYSCFG_DAM_REMAP_USART1TX: USART1 TX DMA remap
104  *                       @arg SYSCFG_DAM_REMAP_USART1RX: USART1 RX DMA remap
105  *                       @arg SYSCFG_DAM_REMAP_TMR16:    Timer 16 DMA remap
106  *                       @arg SYSCFG_DAM_REMAP_TMR17:    Timer 17 DMA remap
107  *                       @arg SYSCFG_DAM_REMAP_TMR16_2�� Timer 16 DMA remap2(only for APM32F072)
108  *                       @arg SYSCFG_DAM_REMAP_TMR17_2�� Timer 17 DMA remap2(only for APM32F072)
109  *                       @arg SYSCFG_DAM_REMAP_SPI2��    SPI2 DMA remap(only for APM32F072)
110  *                       @arg SYSCFG_DAM_REMAP_USART2��  USART1 TX DMA remap(only for APM32F072)
111  *                       @arg SYSCFG_DAM_REMAP_USART3��  USART1 RX DMA remap(only for APM32F072)
112  *                       @arg SYSCFG_DAM_REMAP_I2C1��    I2C1 DMA remap(only for APM32F072)
113  *                       @arg SYSCFG_DAM_REMAP_TMR1��    Timer 1 DMA remap(only for APM32F072)
114  *                       @arg SYSCFG_DAM_REMAP_TMR2��    Timer 2 DMA remap(only for APM32F072)
115  *                       @arg SYSCFG_DAM_REMAP_TMR3��    Timer 3 DMA remap(only for APM32F072)
116  *
117  * @retval      None
118  */
SYSCFG_EnableDMAChannelRemap(uint32_t channel)119 void SYSCFG_EnableDMAChannelRemap(uint32_t channel)
120 {
121     SYSCFG->CFG1 |= (uint32_t)channel;
122 }
123 
124 /*!
125  * @brief       Disables SYSCFG DMA Channel Remap
126  *
127  * @param       channel: selects the DMA channels remap.
128  *                       The parameter can be any combination of following values:
129  *                       @arg SYSCFG_DAM_REMAP_ADC:      ADC DMA remap
130  *                       @arg SYSCFG_DAM_REMAP_USART1TX: USART1 TX DMA remap
131  *                       @arg SYSCFG_DAM_REMAP_USART1RX: USART1 RX DMA remap
132  *                       @arg SYSCFG_DAM_REMAP_TMR16:    Timer 16 DMA remap
133  *                       @arg SYSCFG_DAM_REMAP_TMR17:    Timer 17 DMA remap
134  *                       @arg SYSCFG_DAM_REMAP_TMR16_2�� Timer 16 DMA remap2(only for APM32F072)
135  *                       @arg SYSCFG_DAM_REMAP_TMR17_2�� Timer 17 DMA remap2(only for APM32F072)
136  *                       @arg SYSCFG_DAM_REMAP_SPI2��    SPI2 DMA remap(only for APM32F072)
137  *                       @arg SYSCFG_DAM_REMAP_USART2��  USART1 TX DMA remap(only for APM32F072)
138  *                       @arg SYSCFG_DAM_REMAP_USART3��  USART1 RX DMA remap(only for APM32F072)
139  *                       @arg SYSCFG_DAM_REMAP_I2C1��    I2C1 DMA remap(only for APM32F072)
140  *                       @arg SYSCFG_DAM_REMAP_TMR1��    Timer 1 DMA remap(only for APM32F072)
141  *                       @arg SYSCFG_DAM_REMAP_TMR2��    Timer 2 DMA remap(only for APM32F072)
142  *                       @arg SYSCFG_DAM_REMAP_TMR3��    Timer 3 DMA remap(only for APM32F072)
143  *
144  * @retval      None
145  */
SYSCFG_DisableDMAChannelRemap(uint32_t channel)146 void SYSCFG_DisableDMAChannelRemap(uint32_t channel)
147 {
148     SYSCFG->CFG1 &= (uint32_t)~channel;
149 }
150 
151 /*!
152  * @brief       Enables SYSCFG I2C Fast Mode Plus
153  *
154  * @param       pin:     selects the pin.
155  *                       The parameter can be combination of following values:
156  *                       @arg SYSCFG_I2C_FMP_PB6:    I2C PB6 Fast mode plus
157  *                       @arg SYSCFG_I2C_FMP_PB7:    I2C PB7 Fast mode plus
158  *                       @arg SYSCFG_I2C_FMP_PB8:    I2C PB8 Fast mode plus
159  *                       @arg SYSCFG_I2C_FMP_PB9:    I2C PB9 Fast mode plus
160  *                       @arg SYSCFG_I2C_FMP_PA9:    I2C PA9 Fast mode plus(only for APM32F030 and APM32F091)
161  *                       @arg SYSCFG_I2C_FMP_PA10:   I2C PA10 Fast mode plus(only for APM32F030 and APM32F091)
162  *                       @arg SYSCFG_I2C_FMP_I2C1:   PB10, PB11, PF6 and PF7
163  *                       @arg SYSCFG_I2C_FMP_I2C2:   I2C2 Fast mode plus(only for APM32F072 and APM32F091)
164  *
165  * @retval      None
166  */
SYSCFG_EnableI2CFastModePlus(uint32_t pin)167 void SYSCFG_EnableI2CFastModePlus(uint32_t pin)
168 {
169     SYSCFG->CFG1 |= (uint32_t)pin;
170 }
171 
172 /*!
173  * @brief       Disables SYSCFG I2C Fast Mode Plus
174  *
175  * @param       pin:     selects the pin.
176  *                       The parameter can be combination of following values:
177  *                       @arg SYSCFG_I2C_FMP_PB6:    I2C PB6 Fast mode plus
178  *                       @arg SYSCFG_I2C_FMP_PB7:    I2C PB7 Fast mode plus
179  *                       @arg SYSCFG_I2C_FMP_PB8:    I2C PB8 Fast mode plus
180  *                       @arg SYSCFG_I2C_FMP_PB9:    I2C PB9 Fast mode plus
181  *                       @arg SYSCFG_I2C_FMP_PA9:    I2C PA9 Fast mode plus(only for APM32F030 and APM32F091)
182  *                       @arg SYSCFG_I2C_FMP_PA10:   I2C PA10 Fast mode plus(only for APM32F030 and APM32F091)
183  *                       @arg SYSCFG_I2C_FMP_I2C1:   PB10, PB11, PF6 and PF7
184  *                       @arg SYSCFG_I2C_FMP_I2C2:   I2C2 Fast mode plus(only for APM32F072 and APM32F091)
185  *
186  * @retval      None
187  */
SYSCFG_DisableI2CFastModePlus(uint32_t pin)188 void SYSCFG_DisableI2CFastModePlus(uint32_t pin)
189 {
190     SYSCFG->CFG1 &= (uint32_t)~pin;
191 }
192 
193 /*!
194  * @brief       Select the modulation envelope source
195  *
196  * @param       IRDAEnv: selects the envelope source
197  *                       The parameter can be one of following values:
198  *                       @arg SYSCFG_IRDA_ENV_TMR16:     Timer16 as IRDA Modulation envelope source
199  *                       @arg SYSCFG_IRDA_ENV_USART1:    USART1 as IRDA Modulation envelope source
200  *                       @arg SYSCFG_IRDA_ENV_USART2:    USART4 as IRDA Modulation envelope source
201  *
202  * @retval      None
203  *
204  * @note        It's only for APM32F091 devices.
205  */
SYSCFG_SelectIRDAEnv(SYSCFG_IRDA_ENV_T IRDAEnv)206 void SYSCFG_SelectIRDAEnv(SYSCFG_IRDA_ENV_T IRDAEnv)
207 {
208     SYSCFG->CFG1 &= ~(0x000000C0);
209     SYSCFG->CFG1 |= (IRDAEnv);
210 }
211 
212 /*!
213  * @brief       Selects the GPIO pin used as EINT Line.
214  *
215  * @param       port:   selects the port can be GPIOA/B/C/D/E/F
216  *
217  * @param       pin:    selects the pin can be SYSCFG_PIN_(0..15)
218  *
219  * @retval      None
220  *
221  * @note        GPIOE only for APM32F072 and APM32F091
222  */
223 
SYSCFG_EINTLine(SYSCFG_PORT_T port,SYSCFG_PIN_T pin)224 void SYSCFG_EINTLine(SYSCFG_PORT_T port, SYSCFG_PIN_T pin)
225 {
226     uint32_t status;
227 
228     status = (((uint32_t)0x0F) & port) << (0x04 * (pin & (uint8_t)0x03));
229 
230     if (pin <= 0x03)
231     {
232         SYSCFG->EINTCFG1 |= status;
233     }
234     else if ((0x04 <= pin) & (pin <= 0x07))
235     {
236         SYSCFG->EINTCFG2 |= status;
237     }
238     else if ((0x08 <= pin) & (pin <= 0x0B))
239     {
240         SYSCFG->EINTCFG3 |= status;
241     }
242     else if ((0x0C <= pin) & (pin <= 0x0F))
243     {
244         SYSCFG->EINTCFG4 |= status;
245     }
246 }
247 
248 /*!
249  * @brief       Selected parameter to the break input of TMR1.
250  *
251  * @param       lock:   selects the configuration to break
252  *                      The parameter can be one of following values:
253  *                      @arg SYSCFG_LOCK_LOCKUP: Cortex-M0 LOCKUP bit
254  *                      @arg SYSCFG_LOCK_SRAM:  SRAM parity lock bit
255  *                      @arg SYSCFG_LOCK_PVD:  PVD lock enable bit
256  *
257  * @retval      None
258  */
SYSCFG_BreakLock(uint32_t lock)259 void SYSCFG_BreakLock(uint32_t lock)
260 {
261     SYSCFG->CFG2_B.LOCK = 0;
262     SYSCFG->CFG2_B.SRAMLOCK = 0;
263     SYSCFG->CFG2_B.PVDLOCK = 0;
264 
265     if (lock == SYSCFG_LOCK_LOCKUP)
266     {
267         SYSCFG->CFG2_B.LOCK = BIT_SET;
268     }
269     if (lock == SYSCFG_LOCK_SRAM)
270     {
271         SYSCFG->CFG2_B.SRAMLOCK = BIT_SET;
272     }
273     if (lock == SYSCFG_LOCK_PVD)
274     {
275         SYSCFG->CFG2_B.PVDLOCK = BIT_SET;
276     }
277 }
278 
279 /*!
280  * @brief       Read the specified SYSCFG flag
281  *
282  * @param       flag:   SRAM Parity error flag
283  *                      @arg SYSCFG_CFG2_SRAMPEF
284  *
285  * @retval      None
286  */
SYSCFG_ReadStatusFlag(uint32_t flag)287 uint8_t SYSCFG_ReadStatusFlag(uint32_t flag)
288 {
289     uint32_t status;
290 
291     status = (uint32_t)(SYSCFG->CFG2 & flag);
292 
293     if (status == flag)
294     {
295         return SET;
296     }
297 
298     return RESET;
299 }
300 
301 /*!
302  * @brief       Clear the specified SYSCFG flag
303  *
304  * @param       flag:   SRAM Parity error flag
305  *                      @arg SYSCFG_CFG2_SRAMPEF
306  *
307  * @retval      None
308  */
SYSCFG_ClearStatusFlag(uint8_t flag)309 void SYSCFG_ClearStatusFlag(uint8_t flag)
310 {
311     SYSCFG->CFG2 |= (uint32_t) flag;
312 }
313 
314 /**@} end of group SYSCFG_Functions*/
315 /**@} end of group SYSCFG_Driver*/
316 /**@} end of group APM32F0xx_StdPeriphDriver*/
317