From c109262a7f535cb658b7b9cf4ae1f4fa4f5c2843 Mon Sep 17 00:00:00 2001 From: Yuichi Nakamura Date: Fri, 5 Nov 2021 23:49:52 +0900 Subject: [PATCH] rp2040: add rp2040_gpio_get_function_pin() --- arch/arm/src/rp2040/rp2040_gpio.c | 150 ++++++++++++++++++ arch/arm/src/rp2040/rp2040_gpio.h | 46 ++++++ .../src/rp2040_boardinitialize.c | 2 + 3 files changed, 198 insertions(+) diff --git a/arch/arm/src/rp2040/rp2040_gpio.c b/arch/arm/src/rp2040/rp2040_gpio.c index f197f79ead7..6dd5ee30e8e 100644 --- a/arch/arm/src/rp2040/rp2040_gpio.c +++ b/arch/arm/src/rp2040/rp2040_gpio.c @@ -51,6 +51,42 @@ static xcpt_t g_gpio_irq_handlers[RP2040_GPIO_NUM]; static FAR void *g_gpio_irq_args[RP2040_GPIO_NUM]; static int g_gpio_irq_modes[RP2040_GPIO_NUM]; +/* GPIO pins function assignment */ + +static int g_gpio_function[RP2040_GPIO_NUM]; + +/* GPIO pins function mapping table */ + +static const int g_gpio_function_mapping_spi[2][5] = +{ + { 0, 4, 16, 20, -1 }, /* pin numbers assignable to SPI0 */ + { 8, 12, 24, 28, -1 }, /* pin numbers assignable to SPI1 */ +}; + +static const int g_gpio_function_mapping_uart[2][5] = +{ + { 0, 12, 16, 28, -1 }, /* pin numbers assignable to UART0 */ + { 4, 8, 20, 24, -1 }, /* pin numbers assignable to UART1 */ +}; + +static const int g_gpio_function_mapping_i2c[2][9] = +{ + { 0, 4, 8, 12, 16, 20, 24, 28, -1 }, /* pin numbers assignable to I2C0 */ + { 2, 6, 10, 14, 18, 22, 26, -1, -1 }, /* pin numbers assignable to I2C1 */ +}; + +static const int g_gpio_function_mapping_pwm[8][3] = +{ + { 0, 16, -1 }, /* pin numbers assignable to PWM0 */ + { 2, 18, -1 }, /* pin numbers assignable to PWM1 */ + { 4, 20, -1 }, /* pin numbers assignable to PWM2 */ + { 6, 22, -1 }, /* pin numbers assignable to PWM3 */ + { 8, 24, -1 }, /* pin numbers assignable to PWM4 */ + { 10, 26, -1 }, /* pin numbers assignable to PWM5 */ + { 12, 28, -1 }, /* pin numbers assignable to PWM6 */ + { 14, -1, -1 }, /* pin numbers assignable to PWM7 */ +}; + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -116,6 +152,84 @@ static int rp2040_gpio_interrupt(int irq, void *context, FAR void *arg) * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: r2040_gpio_get_function_pin + * + * Description: + * Get the GPIO pin number to which the specified function is assigned + * + ****************************************************************************/ + +int rp2040_gpio_get_function_pin(uint32_t func, uint32_t port) +{ + int i; + const int *mapping; + + /* Get GPIO pins function mapping table */ + + switch (func) + { + case RP2040_GPIO_FUNC_SPI: + if (port >= 2) + { + return -1; + } + + mapping = g_gpio_function_mapping_spi[port]; + break; + + case RP2040_GPIO_FUNC_UART: + if (port >= 2) + { + return -1; + } + + mapping = g_gpio_function_mapping_uart[port]; + break; + + case RP2040_GPIO_FUNC_I2C: + if (port >= 2) + { + return -1; + } + + mapping = g_gpio_function_mapping_i2c[port]; + break; + + case RP2040_GPIO_FUNC_PWM: + if (port >= 8) + { + return -1; + } + + mapping = g_gpio_function_mapping_pwm[port]; + break; + + default: + return -1; + } + + /* Find the specified function in the current assignment */ + + for (i = 0; mapping[i] >= 0; i++) + { + if (g_gpio_function[mapping[i]] == func) + { + return mapping[i]; + } + } + + return -1; +} + +/**************************************************************************** + * Name: r2040_gpio_set_function + * + * Description: + * Assign functions to the specified GPIO pin + * + ****************************************************************************/ + void rp2040_gpio_set_function(uint32_t gpio, uint32_t func) { DEBUGASSERT(gpio < RP2040_GPIO_NUM); @@ -126,8 +240,18 @@ void rp2040_gpio_set_function(uint32_t gpio, uint32_t func) putreg32(func & RP2040_IO_BANK0_GPIO_CTRL_FUNCSEL_MASK, RP2040_IO_BANK0_GPIO_CTRL(gpio)); + + g_gpio_function[gpio] = func; } +/**************************************************************************** + * Name: r2040_gpio_set_pulls + * + * Description: + * Set pull-up or pull-down to the specified GPIO pin + * + ****************************************************************************/ + void rp2040_gpio_set_pulls(uint32_t gpio, int up, int down) { DEBUGASSERT(gpio < RP2040_GPIO_NUM); @@ -138,6 +262,14 @@ void rp2040_gpio_set_pulls(uint32_t gpio, int up, int down) RP2040_PADS_BANK0_GPIO(gpio)); } +/**************************************************************************** + * Name: r2040_gpio_init + * + * Description: + * Initialize software-controlled GPIO function + * + ****************************************************************************/ + void rp2040_gpio_init(uint32_t gpio) { DEBUGASSERT(gpio < RP2040_GPIO_NUM); @@ -229,3 +361,21 @@ void rp2040_gpio_disable_irq(uint32_t gpio) clrbits_reg32(0xf << ((gpio % 8) * 4), reg); } } + +/**************************************************************************** + * Name: r2040_gpio_initialize + * + * Description: + * Initialize GPIO function management + * + ****************************************************************************/ + +void rp2040_gpio_initialize(void) +{ + int i; + + for (i = 0; i < RP2040_GPIO_NUM; i++) + { + g_gpio_function[i] = RP2040_GPIO_FUNC_NULL; + } +} diff --git a/arch/arm/src/rp2040/rp2040_gpio.h b/arch/arm/src/rp2040/rp2040_gpio.h index 854729a4a10..afad28ac3a2 100644 --- a/arch/arm/src/rp2040/rp2040_gpio.h +++ b/arch/arm/src/rp2040/rp2040_gpio.h @@ -130,8 +130,44 @@ static inline void rp2040_gpio_setdir(uint32_t gpio, int out) * Public Function Prototypes ****************************************************************************/ +/**************************************************************************** + * Name: r2040_gpio_get_function_pin + * + * Description: + * Get the GPIO pin number to which the specified function is assigned + * + ****************************************************************************/ + +int rp2040_gpio_get_function_pin(uint32_t func, uint32_t port); + +/**************************************************************************** + * Name: r2040_gpio_set_function + * + * Description: + * Assign functions to the specified GPIO pin + * + ****************************************************************************/ + void rp2040_gpio_set_function(uint32_t gpio, uint32_t func); + +/**************************************************************************** + * Name: r2040_gpio_set_pulls + * + * Description: + * Set pull-up or pull-down to the specified GPIO pin + * + ****************************************************************************/ + void rp2040_gpio_set_pulls(uint32_t gpio, int up, int down); + +/**************************************************************************** + * Name: r2040_gpio_init + * + * Description: + * Initialize software-controlled GPIO function + * + ****************************************************************************/ + void rp2040_gpio_init(uint32_t gpio); /**************************************************************************** @@ -165,6 +201,16 @@ void rp2040_gpio_enable_irq(uint32_t gpio); void rp2040_gpio_disable_irq(uint32_t gpio); +/**************************************************************************** + * Name: r2040_gpio_initialize + * + * Description: + * Initialize GPIO function management + * + ****************************************************************************/ + +void rp2040_gpio_initialize(void); + #undef EXTERN #if defined(__cplusplus) } diff --git a/boards/arm/rp2040/raspberrypi-pico/src/rp2040_boardinitialize.c b/boards/arm/rp2040/raspberrypi-pico/src/rp2040_boardinitialize.c index 2d059c8d95b..7bdb9c4e546 100644 --- a/boards/arm/rp2040/raspberrypi-pico/src/rp2040_boardinitialize.c +++ b/boards/arm/rp2040/raspberrypi-pico/src/rp2040_boardinitialize.c @@ -55,6 +55,8 @@ void rp2040_boardearlyinitialize(void) { + rp2040_gpio_initialize(); + /* Disable IE on GPIO 26-29 */ clrbits_reg32(RP2040_PADS_BANK0_GPIO_IE, RP2040_PADS_BANK0_GPIO(26));