From ffa084be2c4f7ed0bbfd5397d51b8f893c64d90a Mon Sep 17 00:00:00 2001 From: Rogerio Goncalves Date: Wed, 15 May 2024 18:24:37 +0100 Subject: [PATCH] rp2040: Add kconfig options for rp2040 uart (#6549) (#243) Modified serial.c and Kconfig to dynamically select all possible UART combinations for RP2040 Signed-off-by: Hriday Keni Co-authored-by: Amken USA <166057890+amken3d@users.noreply.github.com> --- src/rp2040/Kconfig | 47 +++++++++++++++++++++-------- src/rp2040/serial.c | 73 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 97 insertions(+), 23 deletions(-) diff --git a/src/rp2040/Kconfig b/src/rp2040/Kconfig index 59891649e..ec8a1af32 100644 --- a/src/rp2040/Kconfig +++ b/src/rp2040/Kconfig @@ -95,19 +95,40 @@ config RP2040_STAGE2_CLKDIV ###################################################################### choice - prompt "Communication interface" - config RP2040_USB - bool "USB" - select USBSERIAL - config RP2040_SERIAL_UART0 - bool "Serial (on UART0 GPIO1/GPIO0)" - select SERIAL - config RP2040_CANBUS - bool "CAN bus" - select CANSERIAL - config RP2040_USBCANBUS - bool "USB to CAN bus bridge" - select USBCANBUS + prompt "Communication Interface" + config RP2040_USB + bool "USBSERIAL" + select USBSERIAL + config RP2040_SERIAL_UART0_PINS_0_1 + bool "UART0 on GPIO0/GPIO1" + select SERIAL + config RP2040_SERIAL_UART0_PINS_12_13 + bool "UART0 on GPIO12/GPIO13" if LOW_LEVEL_OPTIONS + select SERIAL + config RP2040_SERIAL_UART0_PINS_16_17 + bool "UART0 on GPIO16/GPIO17" if LOW_LEVEL_OPTIONS + select SERIAL + config RP2040_SERIAL_UART0_PINS_28_29 + bool "UART0 on GPIO28/GPIO29" if LOW_LEVEL_OPTIONS + select SERIAL + config RP2040_SERIAL_UART1_PINS_4_5 + bool "UART1 on GPIO4/GPIO5" if LOW_LEVEL_OPTIONS + select SERIAL + config RP2040_SERIAL_UART1_PINS_8_9 + bool "UART1 on GPIO8/GPIO9" if LOW_LEVEL_OPTIONS + select SERIAL + config RP2040_SERIAL_UART1_PINS_20_21 + bool "UART1 on GPIO20/GPIO21" if LOW_LEVEL_OPTIONS + select SERIAL + config RP2040_SERIAL_UART1_PINS_24_25 + bool "UART1 on GPIO24/GPIO25" if LOW_LEVEL_OPTIONS + select SERIAL + config RP2040_CANBUS + bool "CAN bus" + select CANSERIAL + config RP2040_USBCANBUS + bool "USB to CAN bus bridge" + select USBCANBUS endchoice config RP2040_CANBUS_GPIO_RX diff --git a/src/rp2040/serial.c b/src/rp2040/serial.c index abfdcb8b3..351a873e5 100644 --- a/src/rp2040/serial.c +++ b/src/rp2040/serial.c @@ -4,20 +4,64 @@ // // This file may be distributed under the terms of the GNU GPLv3 license. + #include // uint32_t -#include "autoconf.h" // CONFIG_SERIAL +#include "autoconf.h" // Include configuration header #include "board/armcm_boot.h" // armcm_enable_irq #include "board/irq.h" // irq_save #include "board/serial_irq.h" // serial_rx_data -#include "hardware/structs/resets.h" // RESETS_RESET_UART0_BITS -#include "hardware/structs/uart.h" // UART0_BASE -#include "internal.h" // UART0_IRQn +#include "hardware/structs/resets.h" // RESETS_RESET_UART0/1_BITS +#include "hardware/structs/uart.h" // uart0_hw, uart1_hw +#include "internal.h" // UART0_IRQn, UART1_IRQn #include "sched.h" // DECL_INIT -#define UARTx uart0_hw -#define UARTx_IRQn UART0_IRQ_IRQn -#define GPIO_Rx 1 -#define GPIO_Tx 0 +// Dynamically select UART and IRQ based on configuration + + + #if CONFIG_RP2040_SERIAL_UART0_PINS_0_1 + #define GPIO_Rx 1 + #define GPIO_Tx 0 + #define UARTx uart0_hw + #define UARTx_IRQn UART0_IRQ_IRQn + #elif CONFIG_RP2040_SERIAL_UART0_PINS_12_13 + #define GPIO_Rx 13 + #define GPIO_Tx 12 + #define UARTx uart0_hw + #define UARTx_IRQn UART0_IRQ_IRQn + #elif CONFIG_RP2040_SERIAL_UART0_PINS_16_17 + #define GPIO_Rx 17 + #define GPIO_Tx 16 + #define UARTx uart0_hw + #define UARTx_IRQn UART0_IRQ_IRQn + #elif CONFIG_RP2040_SERIAL_UART0_PINS_28_29 + #define GPIO_Rx 29 + #define GPIO_Tx 28 + #define UARTx uart1_hw + #define UARTx_IRQn UART1_IRQ_IRQn + #define UARTx uart0_hw + #define UARTx_IRQn UART0_IRQ_IRQn + #elif CONFIG_RP2040_SERIAL_UART1_PINS_4_5 + #define GPIO_Rx 5 + #define GPIO_Tx 4 + #define UARTx uart1_hw + #define UARTx_IRQn UART1_IRQ_IRQn + #elif CONFIG_RP2040_SERIAL_UART1_PINS_8_9 + #define GPIO_Rx 9 + #define GPIO_Tx 8 + #define UARTx uart1_hw + #define UARTx_IRQn UART1_IRQ_IRQn + #elif CONFIG_RP2040_SERIAL_UART1_PINS_20_21 + #define GPIO_Rx 20 + #define GPIO_Tx 21 + #define UARTx uart1_hw + #define UARTx_IRQn UART1_IRQ_IRQn + #elif CONFIG_RP2040_SERIAL_UART1_PINS_24_25 + #define GPIO_Rx 24 + #define GPIO_Tx 25 + #define UARTx uart1_hw + #define UARTx_IRQn UART1_IRQ_IRQn + #endif + // Write tx bytes to the serial port static void @@ -67,10 +111,19 @@ serial_enable_tx_irq(void) void serial_init(void) { - enable_pclock(RESETS_RESET_UART0_BITS); + + uint32_t pclk= 0x00; + if (UARTx == uart0_hw){ + enable_pclock(RESETS_RESET_UART0_BITS); + pclk= get_pclock_frequency(RESETS_RESET_UART0_BITS); + } else { + enable_pclock(RESETS_RESET_UART1_BITS); + pclk = get_pclock_frequency(RESETS_RESET_UART1_BITS); + } + // Setup baud - uint32_t pclk = get_pclock_frequency(RESETS_RESET_UART0_BITS); + uint32_t div = DIV_ROUND_CLOSEST(pclk * 4, CONFIG_SERIAL_BAUD); UARTx->ibrd = div >> 6; UARTx->fbrd = div & 0x3f;