Skip to content

Commit f9a2c4a

Browse files
committed
driver: gd32e1: add gpio read func.
Now `samples/basic/button` is work. Signed-off-by: YuLong Yao <[email protected]>
1 parent e9fa65f commit f9a2c4a

File tree

3 files changed

+118
-10
lines changed

3 files changed

+118
-10
lines changed

boards/arm/gd32e103v_eval/gd32e103v_eval.dts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,43 @@
2121

2222
leds {
2323
compatible = "gpio-leds";
24-
green_led: led_0 {
24+
yellow_led0: led_0 {
2525
gpios = <&gpioc 0 GPIO_ACTIVE_HIGH>;
2626
label = "User LD2";
2727
};
28+
yellow_led1: led_1 {
29+
gpios = <&gpioc 2 GPIO_ACTIVE_HIGH>;
30+
label = "User LD3";
31+
};
32+
yellow_led2: led_2 {
33+
gpios = <&gpioe 0 GPIO_ACTIVE_HIGH>;
34+
label = "User LD4";
35+
};
36+
yellow_led3: led_3 {
37+
gpios = <&gpioe 1 GPIO_ACTIVE_HIGH>;
38+
label = "User LD5";
39+
};
40+
};
41+
42+
gpio_keys {
43+
compatible = "gpio-keys";
44+
wakeup_key: button0 {
45+
label = "Wakeup";
46+
gpios = <&gpioa 0 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>;
47+
};
48+
tamper_key: button1 {
49+
label = "Tamper";
50+
gpios = <&gpioc 13 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>;
51+
};
52+
user_key: button2 {
53+
label = "User Key";
54+
gpios = <&gpiob 14 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>;
55+
};
2856
};
2957

3058
aliases {
31-
led0 = &green_led;
59+
led0 = &yellow_led0;
60+
sw0 = &user_key;
3261
};
3362
};
3463

drivers/gpio/gpio_gd32.c

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,62 @@
2222
#include "gpio_gd32.h"
2323
#include "gpio_utils.h"
2424

25+
static int gpio_gd32_flags_to_conf(int flags, int *pincfg)
26+
{
27+
28+
if ((flags & GPIO_OUTPUT) != 0) {
29+
/* Output only or Output/Input */
30+
31+
*pincfg = GD32_PINCFG_MODE_OUTPUT;
32+
33+
if ((flags & GPIO_SINGLE_ENDED) != 0) {
34+
if (flags & GPIO_LINE_OPEN_DRAIN) {
35+
*pincfg |= GD32_PINCFG_OPEN_DRAIN;
36+
} else {
37+
/* Output can't be open source */
38+
return -ENOTSUP;
39+
}
40+
} else {
41+
*pincfg |= GD32_PINCFG_PUSH_PULL;
42+
}
43+
44+
if ((flags & GPIO_PULL_UP) != 0) {
45+
*pincfg |= GD32_PINCFG_PULL_UP;
46+
} else if ((flags & GPIO_PULL_DOWN) != 0) {
47+
*pincfg |= GD32_PINCFG_PULL_DOWN;
48+
}
49+
50+
} else if ((flags & GPIO_INPUT) != 0) {
51+
/* Input */
52+
53+
*pincfg = GD32_PINCFG_MODE_INPUT;
54+
55+
if ((flags & GPIO_PULL_UP) != 0) {
56+
*pincfg |= GD32_PINCFG_PULL_UP;
57+
} else if ((flags & GPIO_PULL_DOWN) != 0) {
58+
*pincfg |= GD32_PINCFG_PULL_DOWN;
59+
} else {
60+
*pincfg |= GD32_PINCFG_FLOATING;
61+
}
62+
} else {
63+
/* Desactivated: Analog */
64+
*pincfg = GD32_PINCFG_MODE_ANALOG;
65+
}
66+
67+
return 0;
68+
}
69+
2570
/**
2671
* @brief Configure the hardware.
2772
*/
2873
int gpio_gd32_configure(const struct device *dev, int pin, int conf, int altf)
2974
{
3075
const struct gpio_gd32_config *cfg = dev->config;
31-
// todo: change mode and speed later
32-
gpio_init((uint32_t)cfg->base, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, BIT(pin));
76+
77+
ARG_UNUSED(altf);
78+
79+
// todo: change speed by dts
80+
gpio_init((uint32_t)cfg->base, conf, GPIO_OSPEED_10MHZ, BIT(pin));
3381

3482
return 0;
3583
}
@@ -69,6 +117,9 @@ static inline uint32_t gpio_gd32_pin_to_exti_line(int pin)
69117

70118
static int gpio_gd32_port_get_raw(const struct device *dev, uint32_t *value)
71119
{
120+
const struct gpio_gd32_config *cfg = dev->config;
121+
122+
*value = (uint32_t)gpio_input_port_get((uint32_t)cfg->base);
72123

73124
return 0;
74125
}
@@ -105,6 +156,8 @@ static int gpio_gd32_port_toggle_bits(const struct device *dev,
105156
return 0;
106157
}
107158

159+
160+
108161
/**
109162
* @brief Configure pin or port
110163
*/
@@ -115,14 +168,13 @@ static int gpio_gd32_config(const struct device *dev,
115168
int err = 0;
116169
int pincfg = 0;
117170

118-
// todo:
119171
/* figure out if we can map the requested GPIO
120172
* configuration
121173
*/
122-
// err = gpio_gd32_flags_to_conf(flags, &pincfg);
123-
// if (err != 0) {
124-
// goto exit;
125-
// }
174+
err = gpio_gd32_flags_to_conf(flags, &pincfg);
175+
if (err != 0) {
176+
goto exit;
177+
}
126178

127179
if ((flags & GPIO_OUTPUT) != 0) {
128180
if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0) {
@@ -134,7 +186,7 @@ static int gpio_gd32_config(const struct device *dev,
134186

135187
gpio_gd32_configure(dev, pin, pincfg, 0);
136188

137-
// exit:
189+
exit:
138190
return err;
139191
}
140192

drivers/gpio/gpio_gd32.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,33 @@
1919

2020
#define GD32_PORT_NOT_AVAILABLE 0xFFFFFFFF
2121

22+
#if CONFIG_SOC_SERIES_GD32E1X
23+
#define GD32_CLOCK_BUS_GPIO GD32_CLOCK_BUS_APB2
24+
#define GD32_PERIPH_GPIOA LL_APB2_GRP1_PERIPH_GPIOA
25+
#define GD32_PERIPH_GPIOB LL_APB2_GRP1_PERIPH_GPIOB
26+
#define GD32_PERIPH_GPIOC LL_APB2_GRP1_PERIPH_GPIOC
27+
#define GD32_PERIPH_GPIOD LL_APB2_GRP1_PERIPH_GPIOD
28+
#define GD32_PERIPH_GPIOE LL_APB2_GRP1_PERIPH_GPIOE
29+
#define GD32_PERIPH_GPIOF LL_APB2_GRP1_PERIPH_GPIOF
30+
#define GD32_PERIPH_GPIOG LL_APB2_GRP1_PERIPH_GPIOG
31+
#endif
32+
33+
#if CONFIG_SOC_SERIES_GD32E1X
34+
#define GD32_PINCFG_MODE_OUTPUT (GD32_MODE_OUTPUT \
35+
| GD32_CNF_GP_OUTPUT \
36+
| GD32_CNF_PUSH_PULL)
37+
#define GD32_PINCFG_MODE_INPUT GD32_MODE_INPUT
38+
#define GD32_PINCFG_MODE_ANALOG (GD32_MODE_INPUT \
39+
| GD32_CNF_IN_ANALOG)
40+
#define GD32_PINCFG_PUSH_PULL GD32_CNF_PUSH_PULL
41+
#define GD32_PINCFG_OPEN_DRAIN GD32_CNF_OPEN_DRAIN
42+
#define GD32_PINCFG_PULL_UP (GD32_CNF_IN_PUPD | GD32_PUPD_PULL_UP)
43+
#define GD32_PINCFG_PULL_DOWN (GD32_CNF_IN_PUPD | \
44+
GD32_PUPD_PULL_DOWN)
45+
#define GD32_PINCFG_FLOATING (GD32_CNF_IN_FLOAT | \
46+
GD32_PUPD_NO_PULL)
47+
#endif
48+
2249
/**
2350
* @brief configuration of GPIO device
2451
*/

0 commit comments

Comments
 (0)