From 77a5ddf9aef1e784023ce5e12732ca26081de271 Mon Sep 17 00:00:00 2001 From: Siwei Xu Date: Sat, 8 Jan 2022 19:26:29 +0800 Subject: [PATCH] [bsp/n32g452xx] add rt_pin_get support --- bsp/n32g452xx/Libraries/rt_drivers/drv_gpio.c | 23 +++++++++++++++++-- .../n32g452xx-mini-system/applications/main.c | 13 +++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/bsp/n32g452xx/Libraries/rt_drivers/drv_gpio.c b/bsp/n32g452xx/Libraries/rt_drivers/drv_gpio.c index 56cdfadf6c..f41d4dcae0 100644 --- a/bsp/n32g452xx/Libraries/rt_drivers/drv_gpio.c +++ b/bsp/n32g452xx/Libraries/rt_drivers/drv_gpio.c @@ -20,9 +20,9 @@ #define __N32_PIN(index, rcc, gpio, gpio_index) \ { \ 0, RCC_##rcc##_PERIPH_GPIO##gpio, GPIO##gpio, GPIO_PIN_##gpio_index \ -, GPIO##gpio##_PORT_SOURCE, GPIO_PIN_SOURCE##gpio_index \ +, GPIO##gpio##_PORT_SOURCE, GPIO_PIN_SOURCE##gpio_index, "P" #gpio "." #gpio_index \ } -#define __N32_PIN_DEFAULT {-1, 0, 0, 0, 0, 0} +#define __N32_PIN_DEFAULT {-1, 0, 0, 0, 0, 0, ""} /* N32 GPIO driver */ struct pin_index @@ -33,6 +33,7 @@ struct pin_index uint32_t pin; uint8_t port_source; uint8_t pin_source; + const char* name; }; static const struct pin_index pins[] = @@ -473,6 +474,23 @@ const struct pin_index *get_pin(uint8_t pin) return index; }; +rt_base_t n32_pin_get(const char *name) +{ + rt_base_t i; + + for (i = 0; i < ITEM_NUM(pins); i++) + { + if (rt_strcmp(pins[i].name, name) == 0) + { + /* in get_pin function, use pin parameter as index of pins array */ + return i; + } + } + + /* refers content of pins array, map to __N32_PIN_DEFAULT */ + return 0; +} + void n32_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value) { const struct pin_index *index; @@ -754,6 +772,7 @@ const static struct rt_pin_ops _n32_pin_ops = n32_pin_attach_irq, n32_pin_dettach_irq, n32_pin_irq_enable, + n32_pin_get }; int n32_hw_pin_init(void) diff --git a/bsp/n32g452xx/n32g452xx-mini-system/applications/main.c b/bsp/n32g452xx/n32g452xx-mini-system/applications/main.c index 11fdf6f84b..fcb0110413 100644 --- a/bsp/n32g452xx/n32g452xx-mini-system/applications/main.c +++ b/bsp/n32g452xx/n32g452xx-mini-system/applications/main.c @@ -12,19 +12,24 @@ #include /* defined the LED1 pin: PB5 */ -#define LED1_PIN 57 +#define LED1_NAME "PB.5" +rt_base_t led1_pin; int main(void) { uint32_t Speed = 200; + + /* get LED1 pin id by name */ + led1_pin = rt_pin_get(LED1_NAME); + /* set LED1 pin mode to output */ - rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT); + rt_pin_mode(led1_pin, PIN_MODE_OUTPUT); while (1) { - rt_pin_write(LED1_PIN, PIN_LOW); + rt_pin_write(led1_pin, PIN_LOW); rt_thread_mdelay(Speed); - rt_pin_write(LED1_PIN, PIN_HIGH); + rt_pin_write(led1_pin, PIN_HIGH); rt_thread_mdelay(Speed); } } -- GitLab