1 /********************************** (C) COPYRIGHT *******************************
2 * File Name : ch32v10x_gpio.c
3 * Author : WCH
4 * Version : V1.0.0
5 * Date : 2020/04/30
6 * Description : This file provides all the GPIO firmware functions.
7 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
8 * SPDX-License-Identifier: Apache-2.0
9 *******************************************************************************/
10 #include "ch32v10x_gpio.h"
11 #include "ch32v10x_rcc.h"
12
13 /* MASK */
14 #define ECR_PORTPINCONFIG_MASK ((uint16_t)0xFF80)
15 #define LSB_MASK ((uint16_t)0xFFFF)
16 #define DBGAFR_POSITION_MASK ((uint32_t)0x000F0000)
17 #define DBGAFR_SWJCFG_MASK ((uint32_t)0xF0FFFFFF)
18 #define DBGAFR_LOCATION_MASK ((uint32_t)0x00200000)
19 #define DBGAFR_NUMBITS_MASK ((uint32_t)0x00100000)
20
21 /*********************************************************************
22 * @fn GPIO_DeInit
23 *
24 * @brief Deinitializes the GPIOx peripheral registers to their default
25 * reset values.
26 *
27 * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
28 *
29 * @return none
30 */
GPIO_DeInit(GPIO_TypeDef * GPIOx)31 void GPIO_DeInit(GPIO_TypeDef *GPIOx)
32 {
33 if(GPIOx == GPIOA)
34 {
35 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLE);
36 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE);
37 }
38 else if(GPIOx == GPIOB)
39 {
40 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLE);
41 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLE);
42 }
43 else if(GPIOx == GPIOC)
44 {
45 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, ENABLE);
46 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, DISABLE);
47 }
48 else if(GPIOx == GPIOD)
49 {
50 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, ENABLE);
51 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, DISABLE);
52 }
53 else if(GPIOx == GPIOE)
54 {
55 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, ENABLE);
56 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, DISABLE);
57 }
58 else if(GPIOx == GPIOF)
59 {
60 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOF, ENABLE);
61 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOF, DISABLE);
62 }
63 else
64 {
65 if(GPIOx == GPIOG)
66 {
67 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOG, ENABLE);
68 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOG, DISABLE);
69 }
70 }
71 }
72
73 /*********************************************************************
74 * @fn GPIO_AFIODeInit
75 *
76 * @brief Deinitializes the Alternate Functions (remap, event control
77 * and EXTI configuration) registers to their default reset values.
78 *
79 * @return none
80 */
GPIO_AFIODeInit(void)81 void GPIO_AFIODeInit(void)
82 {
83 RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, ENABLE);
84 RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, DISABLE);
85 }
86
87 /*********************************************************************
88 * @fn GPIO_Init
89 *
90 * @brief GPIOx - where x can be (A..G) to select the GPIO peripheral.
91 *
92 * @param GPIO_InitStruct - pointer to a GPIO_InitTypeDef structure that
93 * contains the configuration information for the specified GPIO peripheral.
94 *
95 * @return none
96 */
GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_InitStruct)97 void GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)
98 {
99 uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
100 uint32_t tmpreg = 0x00, pinmask = 0x00;
101
102 currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
103
104 if((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
105 {
106 currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
107 }
108
109 if(((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)
110 {
111 tmpreg = GPIOx->CFGLR;
112
113 for(pinpos = 0x00; pinpos < 0x08; pinpos++)
114 {
115 pos = ((uint32_t)0x01) << pinpos;
116 currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
117
118 if(currentpin == pos)
119 {
120 pos = pinpos << 2;
121 pinmask = ((uint32_t)0x0F) << pos;
122 tmpreg &= ~pinmask;
123 tmpreg |= (currentmode << pos);
124
125 if(GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
126 {
127 GPIOx->BCR = (((uint32_t)0x01) << pinpos);
128 }
129 else
130 {
131 if(GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
132 {
133 GPIOx->BSHR = (((uint32_t)0x01) << pinpos);
134 }
135 }
136 }
137 }
138 GPIOx->CFGLR = tmpreg;
139 }
140
141 if(GPIO_InitStruct->GPIO_Pin > 0x00FF)
142 {
143 tmpreg = GPIOx->CFGHR;
144
145 for(pinpos = 0x00; pinpos < 0x08; pinpos++)
146 {
147 pos = (((uint32_t)0x01) << (pinpos + 0x08));
148 currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
149
150 if(currentpin == pos)
151 {
152 pos = pinpos << 2;
153 pinmask = ((uint32_t)0x0F) << pos;
154 tmpreg &= ~pinmask;
155 tmpreg |= (currentmode << pos);
156
157 if(GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
158 {
159 GPIOx->BCR = (((uint32_t)0x01) << (pinpos + 0x08));
160 }
161
162 if(GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
163 {
164 GPIOx->BSHR = (((uint32_t)0x01) << (pinpos + 0x08));
165 }
166 }
167 }
168 GPIOx->CFGHR = tmpreg;
169 }
170 }
171
172 /*********************************************************************
173 * @fn GPIO_StructInit
174 *
175 * @brief Fills each GPIO_InitStruct member with its default
176 *
177 * @param GPIO_InitStruct - pointer to a GPIO_InitTypeDef structure
178 * which will be initialized.
179 *
180 * @return none
181 */
GPIO_StructInit(GPIO_InitTypeDef * GPIO_InitStruct)182 void GPIO_StructInit(GPIO_InitTypeDef *GPIO_InitStruct)
183 {
184 GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
185 GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
186 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN_FLOATING;
187 }
188
189 /*********************************************************************
190 * @fn GPIO_ReadInputDataBit
191 *
192 * @brief GPIOx - where x can be (A..G) to select the GPIO peripheral.
193 *
194 * @param GPIO_Pin - specifies the port bit to read.
195 * This parameter can be GPIO_Pin_x where x can be (0..15).
196 *
197 * @return The input port pin value.
198 */
GPIO_ReadInputDataBit(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)199 uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
200 {
201 uint8_t bitstatus = 0x00;
202
203 if((GPIOx->INDR & GPIO_Pin) != (uint32_t)Bit_RESET)
204 {
205 bitstatus = (uint8_t)Bit_SET;
206 }
207 else
208 {
209 bitstatus = (uint8_t)Bit_RESET;
210 }
211
212 return bitstatus;
213 }
214
215 /*********************************************************************
216 * @fn GPIO_ReadInputData
217 *
218 * @brief Reads the specified GPIO input data port.
219 *
220 * @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
221 *
222 * @return The input port pin value.
223 */
GPIO_ReadInputData(GPIO_TypeDef * GPIOx)224 uint16_t GPIO_ReadInputData(GPIO_TypeDef *GPIOx)
225 {
226 return ((uint16_t)GPIOx->INDR);
227 }
228
229 /*********************************************************************
230 * @fn GPIO_ReadOutputDataBit
231 *
232 * @brief Reads the specified output data port bit.
233 *
234 * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
235 * GPIO_Pin - specifies the port bit to read.
236 * This parameter can be GPIO_Pin_x where x can be (0..15).
237 *
238 * @return none
239 */
GPIO_ReadOutputDataBit(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)240 uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
241 {
242 uint8_t bitstatus = 0x00;
243
244 if((GPIOx->OUTDR & GPIO_Pin) != (uint32_t)Bit_RESET)
245 {
246 bitstatus = (uint8_t)Bit_SET;
247 }
248 else
249 {
250 bitstatus = (uint8_t)Bit_RESET;
251 }
252
253 return bitstatus;
254 }
255
256 /*********************************************************************
257 * @fn GPIO_ReadOutputData
258 *
259 * @brief Reads the specified GPIO output data port.
260 *
261 * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
262 *
263 * @return GPIO output port pin value.
264 */
GPIO_ReadOutputData(GPIO_TypeDef * GPIOx)265 uint16_t GPIO_ReadOutputData(GPIO_TypeDef *GPIOx)
266 {
267 return ((uint16_t)GPIOx->OUTDR);
268 }
269
270 /*********************************************************************
271 * @fn GPIO_SetBits
272 *
273 * @brief Sets the selected data port bits.
274 *
275 * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
276 * GPIO_Pin - specifies the port bits to be written.
277 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
278 *
279 * @return none
280 */
GPIO_SetBits(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)281 void GPIO_SetBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
282 {
283 GPIOx->BSHR = GPIO_Pin;
284 }
285
286 /*********************************************************************
287 * @fn GPIO_ResetBits
288 *
289 * @brief Clears the selected data port bits.
290 *
291 * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
292 * GPIO_Pin - specifies the port bits to be written.
293 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
294 *
295 * @return none
296 */
GPIO_ResetBits(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)297 void GPIO_ResetBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
298 {
299 GPIOx->BCR = GPIO_Pin;
300 }
301
302 /*********************************************************************
303 * @fn GPIO_WriteBit
304 *
305 * @brief Sets or clears the selected data port bit.
306 *
307 * @param GPIO_Pin - specifies the port bit to be written.
308 * This parameter can be one of GPIO_Pin_x where x can be (0..15).
309 * BitVal - specifies the value to be written to the selected bit.
310 * Bit_SetL - to clear the port pin.
311 * Bit_SetH - to set the port pin.
312 *
313 * @return none
314 */
GPIO_WriteBit(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,BitAction BitVal)315 void GPIO_WriteBit(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
316 {
317 if(BitVal != Bit_RESET)
318 {
319 GPIOx->BSHR = GPIO_Pin;
320 }
321 else
322 {
323 GPIOx->BCR = GPIO_Pin;
324 }
325 }
326
327 /*********************************************************************
328 * @fn GPIO_Write
329 *
330 * @brief Writes data to the specified GPIO data port.
331 *
332 * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
333 * PortVal - specifies the value to be written to the port output data register.
334 *
335 * @return none
336 */
GPIO_Write(GPIO_TypeDef * GPIOx,uint16_t PortVal)337 void GPIO_Write(GPIO_TypeDef *GPIOx, uint16_t PortVal)
338 {
339 GPIOx->OUTDR = PortVal;
340 }
341
342 /*********************************************************************
343 * @fn GPIO_PinLockConfig
344 *
345 * @brief Locks GPIO Pins configuration registers.
346 *
347 * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
348 * GPIO_Pin - specifies the port bit to be written.
349 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
350 *
351 * @return none
352 */
GPIO_PinLockConfig(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)353 void GPIO_PinLockConfig(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
354 {
355 uint32_t tmp = 0x00010000;
356
357 tmp |= GPIO_Pin;
358 GPIOx->LCKR = tmp;
359 GPIOx->LCKR = GPIO_Pin;
360 GPIOx->LCKR = tmp;
361 tmp = GPIOx->LCKR;
362 tmp = GPIOx->LCKR;
363 }
364
365 /*********************************************************************
366 * @fn GPIO_EventOutputConfig
367 *
368 * @brief Selects the GPIO pin used as Event output.
369 *
370 * @param GPIO_PortSource - selects the GPIO port to be used as source
371 * for Event output.
372 * This parameter can be GPIO_PortSourceGPIOx where x can be (A..E).
373 * GPIO_PinSource - specifies the pin for the Event output.
374 * This parameter can be GPIO_PinSourcex where x can be (0..15).
375 *
376 * @return none
377 */
GPIO_EventOutputConfig(uint8_t GPIO_PortSource,uint8_t GPIO_PinSource)378 void GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)
379 {
380 uint32_t tmpreg = 0x00;
381
382 tmpreg = AFIO->ECR;
383 tmpreg &= ECR_PORTPINCONFIG_MASK;
384 tmpreg |= (uint32_t)GPIO_PortSource << 0x04;
385 tmpreg |= GPIO_PinSource;
386 AFIO->ECR = tmpreg;
387 }
388
389 /*********************************************************************
390 * @fn GPIO_EventOutputCmd
391 *
392 * @brief Enables or disables the Event Output.
393 *
394 * @param NewState - ENABLE or DISABLE.
395 *
396 * @return none
397 */
GPIO_EventOutputCmd(FunctionalState NewState)398 void GPIO_EventOutputCmd(FunctionalState NewState)
399 {
400 if(NewState)
401 {
402 AFIO->ECR |= (1 << 7);
403 }
404 else
405 {
406 AFIO->ECR &= ~(1 << 7);
407 }
408 }
409
410 /*********************************************************************
411 * @fn GPIO_PinRemapConfig
412 *
413 * @brief Changes the mapping of the specified pin.
414 *
415 * @param GPIO_Remap - selects the pin to remap.
416 * GPIO_Remap_SPI1 - SPI1 Alternate Function mapping
417 * GPIO_Remap_I2C1 - I2C1 Alternate Function mapping
418 * GPIO_Remap_USART1 - USART1 Alternate Function mapping
419 * GPIO_Remap_USART2 - USART2 Alternate Function mapping
420 * GPIO_PartialRemap_USART3 - USART3 Partial Alternate Function mapping
421 * GPIO_FullRemap_USART3 - USART3 Full Alternate Function mapping
422 * GPIO_PartialRemap_TIM1 - TIM1 Partial Alternate Function mapping
423 * GPIO_FullRemap_TIM1 - TIM1 Full Alternate Function mapping
424 * GPIO_PartialRemap1_TIM2 - TIM2 Partial1 Alternate Function mapping
425 * GPIO_PartialRemap2_TIM2 - TIM2 Partial2 Alternate Function mapping
426 * GPIO_FullRemap_TIM2 - TIM2 Full Alternate Function mapping
427 * GPIO_PartialRemap_TIM3 - TIM3 Partial Alternate Function mapping
428 * GPIO_FullRemap_TIM3 - TIM3 Full Alternate Function mapping
429 * GPIO_Remap_TIM4 - TIM4 Alternate Function mapping
430 * GPIO_Remap1_CAN1 - CAN1 Alternate Function mapping
431 * GPIO_Remap2_CAN1 - CAN1 Alternate Function mapping
432 * GPIO_Remap_PD01 - PD01 Alternate Function mapping
433 * GPIO_Remap_ADC1_ETRGINJ - ADC1 External Trigger Injected Conversion remapping
434 * GPIO_Remap_ADC1_ETRGREG - ADC1 External Trigger Regular Conversion remapping
435 * GPIO_Remap_ADC2_ETRGINJ - ADC2 External Trigger Injected Conversion remapping
436 * GPIO_Remap_ADC2_ETRGREG - ADC2 External Trigger Regular Conversion remapping
437 * GPIO_Remap_ETH - Ethernet remapping
438 * GPIO_Remap_CAN2 - CAN2 remapping
439 * GPIO_Remap_MII_RMII_SEL - MII or RMII selection
440 * GPIO_Remap_SWJ_NoJTRST - Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST
441 * GPIO_Remap_SWJ_JTAGDisable - JTAG-DP Disabled and SW-DP Enabled
442 * GPIO_Remap_SWJ_Disable - Full SWJ Disabled (JTAG-DP + SW-DP)
443 * GPIO_Remap_TIM2ITR1_PTP_SOF - Ethernet PTP output or USB OTG SOF (Start of Frame) connected
444 * to TIM2 Internal Trigger 1 for calibration
445 * GPIO_Remap_TIM2ITR1_PTP_SOF - Ethernet PTP output or USB OTG SOF (Start of Frame)
446 * GPIO_Remap_TIM8 - TIM8 Alternate Function mapping
447 * GPIO_PartialRemap_TIM9 - TIM9 Partial Alternate Function mapping
448 * GPIO_FullRemap_TIM9 - TIM9 Full Alternate Function mapping
449 * GPIO_PartialRemap_TIM10 - TIM10 Partial Alternate Function mapping
450 * GPIO_FullRemap_TIM10 - TIM10 Full Alternate Function mapping
451 * GPIO_Remap_FSMC_NADV - FSMC_NADV Alternate Function mapping
452 * GPIO_PartialRemap_USART4 - USART4 Partial Alternate Function mapping
453 * GPIO_FullRemap_USART4 - USART4 Full Alternate Function mapping
454 * GPIO_PartialRemap_USART5 - USART5 Partial Alternate Function mapping
455 * GPIO_FullRemap_USART5 - USART5 Full Alternate Function mapping
456 * GPIO_PartialRemap_USART6 - USART6 Partial Alternate Function mapping
457 * GPIO_FullRemap_USART6 - USART6 Full Alternate Function mapping
458 * GPIO_PartialRemap_USART7 - USART7 Partial Alternate Function mapping
459 * GPIO_FullRemap_USART7 - USART7 Full Alternate Function mapping
460 * GPIO_PartialRemap_USART8 - USART8 Partial Alternate Function mapping
461 * GPIO_FullRemap_USART8 - USART8 Full Alternate Function mapping
462 * GPIO_Remap_USART1_HighBit - USART1 Alternate Function mapping high bit
463 * NewState - ENABLE or DISABLE.
464 *
465 * @return none
466 */
GPIO_PinRemapConfig(uint32_t GPIO_Remap,FunctionalState NewState)467 void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState)
468 {
469 uint32_t tmp = 0x00, tmp1 = 0x00, tmpreg = 0x00, tmpmask = 0x00;
470
471 if((GPIO_Remap & 0x80000000) == 0x80000000)
472 {
473 tmpreg = AFIO->PCFR2;
474 }
475 else
476 {
477 tmpreg = AFIO->PCFR1;
478 }
479
480 tmpmask = (GPIO_Remap & DBGAFR_POSITION_MASK) >> 0x10;
481 tmp = GPIO_Remap & LSB_MASK;
482
483 if((GPIO_Remap & (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK)) == (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK))
484 {
485 tmpreg &= DBGAFR_SWJCFG_MASK;
486 AFIO->PCFR1 &= DBGAFR_SWJCFG_MASK;
487 }
488 else if((GPIO_Remap & DBGAFR_NUMBITS_MASK) == DBGAFR_NUMBITS_MASK)
489 {
490 tmp1 = ((uint32_t)0x03) << tmpmask;
491 tmpreg &= ~tmp1;
492 tmpreg |= ~DBGAFR_SWJCFG_MASK;
493 }
494 else
495 {
496 tmpreg &= ~(tmp << ((GPIO_Remap >> 0x15) * 0x10));
497 tmpreg |= ~DBGAFR_SWJCFG_MASK;
498 }
499
500 if(NewState != DISABLE)
501 {
502 tmpreg |= (tmp << ((GPIO_Remap >> 0x15) * 0x10));
503 }
504
505 if((GPIO_Remap & 0x80000000) == 0x80000000)
506 {
507 AFIO->PCFR2 = tmpreg;
508 }
509 else
510 {
511 AFIO->PCFR1 = tmpreg;
512 }
513 }
514
515 /*********************************************************************
516 * @fn GPIO_EXTILineConfig
517 *
518 * @brief Selects the GPIO pin used as EXTI Line.
519 *
520 * @param GPIO_PortSource - selects the GPIO port to be used as source for EXTI lines.
521 * This parameter can be GPIO_PortSourceGPIOx where x can be (A..G).
522 * GPIO_PinSource - specifies the EXTI line to be configured.
523 * This parameter can be GPIO_PinSourcex where x can be (0..15).
524 *
525 * @return none
526 */
GPIO_EXTILineConfig(uint8_t GPIO_PortSource,uint8_t GPIO_PinSource)527 void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)
528 {
529 uint32_t tmp = 0x00;
530
531 tmp = ((uint32_t)0x0F) << (0x04 * (GPIO_PinSource & (uint8_t)0x03));
532 AFIO->EXTICR[GPIO_PinSource >> 0x02] &= ~tmp;
533 AFIO->EXTICR[GPIO_PinSource >> 0x02] |= (((uint32_t)GPIO_PortSource) << (0x04 * (GPIO_PinSource & (uint8_t)0x03)));
534 }
535
536
537
538
539