Skip to content

Commit

Permalink
rp2040: Add kconfig options for rp2040 uart (#6549) (#243)
Browse files Browse the repository at this point in the history
Modified serial.c and Kconfig to dynamically select all possible UART combinations for RP2040

Signed-off-by: Hriday Keni <[email protected]>
Co-authored-by: Amken USA <[email protected]>
  • Loading branch information
rogerlz and amken3d authored May 15, 2024
1 parent 6debf89 commit ffa084b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 23 deletions.
47 changes: 34 additions & 13 deletions src/rp2040/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 63 additions & 10 deletions src/rp2040/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,64 @@
//
// This file may be distributed under the terms of the GNU GPLv3 license.


#include <stdint.h> // 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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ffa084b

Please sign in to comment.