Skip to content

Commit cd7bd5f

Browse files
yonschcarlescufi
authored andcommitted
drivers: serial: Added support for raspberry pi
Added a serial driver for the RP2040. Only polling API is supported. Signed-off-by: Yonatan Schachter <[email protected]>
1 parent fd59038 commit cd7bd5f

File tree

7 files changed

+122
-0
lines changed

7 files changed

+122
-0
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@
354354
/drivers/serial/uart_mcux_iuart.c @Mani-Sadhasivam
355355
/drivers/serial/Kconfig.rtt @carlescufi @pkral78
356356
/drivers/serial/uart_rtt.c @carlescufi @pkral78
357+
/drivers/serial/*rpi_pico* @yonsch
357358
/drivers/serial/Kconfig.xlnx @wjliang
358359
/drivers/serial/uart_xlnx_ps.c @wjliang
359360
/drivers/serial/uart_xlnx_uartlite.c @henrikbrixandersen

drivers/serial/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ zephyr_library_sources_ifdef(CONFIG_UART_SAM0 uart_sam0.c)
3232
zephyr_library_sources_ifdef(CONFIG_UART_PSOC6 uart_psoc6.c)
3333
zephyr_library_sources_ifdef(CONFIG_UART_PL011 uart_pl011.c)
3434
zephyr_library_sources_ifdef(CONFIG_UART_RV32M1_LPUART uart_rv32m1_lpuart.c)
35+
zephyr_library_sources_ifdef(CONFIG_UART_RPI_PICO uart_rpi_pico.c)
3536
zephyr_library_sources_ifdef(CONFIG_UART_LITEUART uart_liteuart.c)
3637
zephyr_library_sources_ifdef(CONFIG_UART_RTT_DRIVER uart_rtt.c)
3738
zephyr_library_sources_ifdef(CONFIG_UART_XLNX_PS uart_xlnx_ps.c)

drivers/serial/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ source "drivers/serial/Kconfig.pl011"
159159

160160
source "drivers/serial/Kconfig.rv32m1_lpuart"
161161

162+
source "drivers/serial/Kconfig.rpi_pico"
163+
162164
source "drivers/serial/Kconfig.litex"
163165

164166
source "drivers/serial/Kconfig.rtt"

drivers/serial/Kconfig.rpi_pico

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2021 Yonatan Schachter
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Workaround for not being able to have commas in macro arguments
5+
DT_COMPAT_RPI_PICO_UART := raspberrypi,pico-uart
6+
7+
config UART_RPI_PICO
8+
bool "Raspberry Pi UART driver"
9+
default $(dt_compat_enabled,$(DT_COMPAT_RPI_PICO_UART))
10+
select SERIAL_HAS_DRIVER
11+
select PICOSDK_USE_UART

drivers/serial/uart_rpi_pico.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2021, Yonatan Schachter
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <drivers/uart.h>
8+
#include <drivers/pinctrl.h>
9+
10+
/* pico-sdk includes */
11+
#include <hardware/uart.h>
12+
13+
#define DT_DRV_COMPAT raspberrypi_pico_uart
14+
15+
struct uart_rpi_config {
16+
uart_inst_t *const uart_dev;
17+
uint32_t baudrate;
18+
const struct pinctrl_dev_config *pcfg;
19+
};
20+
21+
static int uart_rpi_poll_in(const struct device *dev, unsigned char *c)
22+
{
23+
const struct uart_rpi_config *config = dev->config;
24+
25+
if (!uart_is_readable(config->uart_dev)) {
26+
return -1;
27+
}
28+
29+
*c = (unsigned char)uart_get_hw(config->uart_dev)->dr;
30+
return 0;
31+
}
32+
33+
static void uart_rpi_poll_out(const struct device *dev, unsigned char c)
34+
{
35+
const struct uart_rpi_config *config = dev->config;
36+
37+
uart_putc_raw(config->uart_dev, c);
38+
}
39+
40+
static int uart_rpi_init(const struct device *dev)
41+
{
42+
const struct uart_rpi_config *config = dev->config;
43+
int ret;
44+
45+
ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
46+
if (ret < 0) {
47+
return ret;
48+
}
49+
50+
uart_init(config->uart_dev, config->baudrate);
51+
52+
return 0;
53+
}
54+
55+
static const struct uart_driver_api uart_rpi_driver_api = {
56+
.poll_in = uart_rpi_poll_in,
57+
.poll_out = uart_rpi_poll_out,
58+
};
59+
60+
#define RPI_UART_INIT(idx) \
61+
PINCTRL_DT_INST_DEFINE(idx); \
62+
static const struct uart_rpi_config uart_rpi_cfg_##idx = { \
63+
.uart_dev = (uart_inst_t *)DT_INST_REG_ADDR(idx), \
64+
.baudrate = DT_INST_PROP(idx, current_speed), \
65+
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(idx), \
66+
}; \
67+
\
68+
DEVICE_DT_INST_DEFINE(idx, &uart_rpi_init, \
69+
NULL, \
70+
NULL, \
71+
&uart_rpi_cfg_##idx, PRE_KERNEL_1, \
72+
CONFIG_SERIAL_INIT_PRIORITY, \
73+
&uart_rpi_driver_api);
74+
75+
DT_INST_FOREACH_STATUS_OKAY(RPI_UART_INIT)

dts/arm/rpi_pico/rp2040.dtsi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@
5050
status = "okay";
5151
label = "PINCTRL";
5252
};
53+
54+
uart0: uart@40034000 {
55+
compatible = "raspberrypi,pico-uart";
56+
reg = <0x40034000 DT_SIZE_K(4)>;
57+
clocks = <&peripheral_clk>;
58+
interrupts = <20 RPI_PICO_DEFAULT_IRQ_PRIORITY>;
59+
interrupt-names = "uart0";
60+
label = "UART_0";
61+
status = "disabled";
62+
};
63+
64+
uart1: uart@40038000 {
65+
compatible = "raspberrypi,pico-uart";
66+
reg = <0x40038000 DT_SIZE_K(4)>;
67+
clocks = <&peripheral_clk>;
68+
interrupts = <21 RPI_PICO_DEFAULT_IRQ_PRIORITY>;
69+
interrupt-names = "uart1";
70+
label = "UART_1";
71+
status = "disabled";
72+
};
5373
};
5474
};
5575

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
description: Raspberry Pi Pico UART
2+
3+
compatible: "raspberrypi,pico-uart"
4+
5+
include: [uart-controller.yaml, pinctrl-device.yaml]
6+
7+
properties:
8+
reg:
9+
required: true
10+
11+
interrupts:
12+
required: true

0 commit comments

Comments
 (0)