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 */
2873int 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
70118static 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
0 commit comments