1 /*
2  * Copyright (c) 2020-2020, BLUETRUM Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef AB32VG1_HAL_GPIO_H__
8 #define AB32VG1_HAL_GPIO_H__
9 
10 #include "ab32vg1_hal_def.h"
11 
12 struct gpio_init
13 {
14     uint8_t  pin;
15     uint8_t  dir;
16     uint8_t  de;
17     uint8_t  pull;
18     uint32_t alternate;
19     uint32_t af_con;        /*!< Alternate function control
20                                 [4:0]: Mapping name
21                                 [6:5]: Mapping control register
22                                 [7]:   Mapping enable bit */
23 };
24 typedef struct gpio_init *gpio_init_t;
25 
26 enum
27 {
28     GPIOxSET = 0x00,
29     GPIOxCLR,
30     GPIOx,
31     GPIOxDIR,
32     GPIOxDE,
33     GPIOxFEN,
34     GPIOxDRV,
35     GPIOxPU,
36     GPIOxPD,
37     GPIOxPU200K,
38     GPIOxPD200K,
39     GPIOxPU300K,
40     GPIOxPD300K,
41 };
42 
43 /* Private constants */
44 
45 #define FUNCMCONx(x)            *(volatile uint32_t*)(SFR0_BASE + (0x07 + (x))*4)
46 
47 /* Exported constants */
48 #define GPIO_DIR_INPUT      (0x01u)
49 #define GPIO_DIR_OUTPUT     (0x02u)
50 
51 #define GPIO_DIGITAL        (0x01u)
52 #define GPIO_ANALOG         (0x02u)
53 
54 #define GPIO_AFDIS              (0u << 7)
55 #define GPIO_AFEN               (1u << 7)
56 #define GPIO_AFCON0             (0u << 5)     /*!< When using UARTT0 UART1 HSUART SPI0 and SD0 */
57 #define GPIO_AFCON1             (1u << 5)     /*!< When using LPWM0 LPWM1 LPWM2 LPWM3 SPI1 UART2 and CLKOUT */
58 #define GPIO_AFCON2             (2u << 5)     /*!< When using IR TIMER3 TIMER4 TIMER5 and IIS */
59 #define GPIO_AFCON_MASK         (0x3u << 5)
60 #define GPIO_GET_AFCON(af_con)  (uint8_t)(((af_con) & (GPIO_AFCON_MASK)) >> 5)
61 
62 #define GPIO_NOPULL         (0x00u)
63 #define GPIO_PULLUP         (0x01u)
64 #define GPIO_PULLDOWN       (0x02u)
65 
66 #define GPIO_PIN_LOW        (0x00u)
67 #define GPIO_PIN_HIGH       (0x01u)
68 
69 #define GPIOA_BASE          ((hal_sfr_t)(&GPIOASET))
70 #define GPIOB_BASE          ((hal_sfr_t)(&GPIOBSET))
71 #define GPIOE_BASE          ((hal_sfr_t)(&GPIOESET))
72 #define GPIOF_BASE          ((hal_sfr_t)(&GPIOFSET))
73 
74 #define GPIO_PIN_0          (BIT(0))
75 #define GPIO_PIN_1          (BIT(1))
76 #define GPIO_PIN_2          (BIT(2))
77 #define GPIO_PIN_3          (BIT(3))
78 #define GPIO_PIN_4          (BIT(4))
79 #define GPIO_PIN_5          (BIT(5))
80 #define GPIO_PIN_6          (BIT(6))
81 #define GPIO_PIN_7          (BIT(7))
82 
83 /* Include GPIO HAL Extended module */
84 #include "ab32vg1_hal_gpio_ex.h"
85 
86 /* Initialization and de-initialization functions */
87 void hal_gpio_init(hal_sfr_t gpiox, gpio_init_t gpio_init);
88 
89 /* IO operation function */
90 uint8_t hal_gpio_read(hal_sfr_t gpiox, uint8_t pin);
91 void hal_gpio_write(hal_sfr_t gpiox, uint8_t pin, uint8_t state);
92 void hal_gpio_toggle(hal_sfr_t gpiox, uint8_t pin);
93 
94 #endif
95