drivers: serial: uart_rtt converted to devicetree

Instantiate RTT UART instances from devicetree nodes instead of from
Kconfig symbols. While RTT is implemented using software, not hardware,
it is emulating a hardware device, and thus should be configured through
devicetree. This allows the simulated UART device to be selected via
devicetree aliases and chosen nodes.

The following devicetree snippet will instantiate RTT channels 0 and 2
as UART devices.
```
/ {
	rtt0: rtt_terminal {
		compatible = "segger,rtt-uart";
		label = "rtt_terminal";
		status = "okay";
	};

	rtt2: rtt_secondary {
		compatible = "segger,rtt-uart";
		label = "rtt_app_specific";
		status = "okay";
	};
};
```

Fixes the RTT portion of #10621.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
Jordan Yates 2020-08-21 14:50:27 +10:00 committed by Carles Cufí
commit 3e33d73381
3 changed files with 68 additions and 90 deletions

View file

@ -11,8 +11,11 @@ menuconfig UART_RTT
if UART_RTT
# Workaround for not being able to have commas in macro arguments
DT_COMPAT_SEGGER_RTT_UART := segger,rtt-uart
config UART_RTT_0
bool "Enable UART on RTT channel 0"
def_bool $(dt_nodelabel_has_compat,rtt0,$(DT_COMPAT_SEGGER_RTT_UART))
depends on SEGGER_RTT_MAX_NUM_UP_BUFFERS >= 1 && SEGGER_RTT_MAX_NUM_DOWN_BUFFERS >= 1
depends on SEGGER_RTT_MODE_NO_BLOCK_SKIP
select SERIAL_HAS_DRIVER
@ -21,83 +24,29 @@ config UART_RTT_0
Enable UART on (default) RTT channel 0. Default channel has to be configured in non-blocking skip mode.
config UART_RTT_1
bool "Enable UART on RTT channel 1"
def_bool $(dt_nodelabel_has_compat,rtt1,$(DT_COMPAT_SEGGER_RTT_UART))
depends on SEGGER_RTT_MAX_NUM_UP_BUFFERS >= 2 && SEGGER_RTT_MAX_NUM_DOWN_BUFFERS >= 2
select SERIAL_HAS_DRIVER
select UART_RTT_DRIVER
help
Enable UART on RTT channel 1
if UART_RTT_1
config UART_RTT_1_TX_BUFFER_SIZE
int "Size of RTT_1 TX buffer (up to host)"
range 1 65535
default 1024
help
Size of the RTT up buffer for UART 1 transmission.
config UART_RTT_1_RX_BUFFER_SIZE
int "Size of RTT_1 RX buffer (down from host)"
range 1 65535
default 16
help
Size of the RTT down buffer for UART 1 reception.
endif
config UART_RTT_2
bool "Enable UART on RTT channel 2"
def_bool $(dt_nodelabel_has_compat,rtt2,$(DT_COMPAT_SEGGER_RTT_UART))
depends on SEGGER_RTT_MAX_NUM_UP_BUFFERS >= 3 && SEGGER_RTT_MAX_NUM_DOWN_BUFFERS >= 3
select SERIAL_HAS_DRIVER
select UART_RTT_DRIVER
help
Enable UART on RTT channel 2
if UART_RTT_2
config UART_RTT_2_TX_BUFFER_SIZE
int "Size of RTT_2 TX buffer (up to host)"
range 1 65535
default 1024
help
Size of the RTT up buffer for UART 2 transmission.
config UART_RTT_2_RX_BUFFER_SIZE
int "Size of RTT_2 RX buffer (down from host)"
range 1 65535
default 16
help
Size of the RTT down buffer for UART 2 reception.
endif
config UART_RTT_3
bool "Enable UART on RTT channel 3"
def_bool $(dt_nodelabel_has_compat,rtt3,$(DT_COMPAT_SEGGER_RTT_UART))
depends on SEGGER_RTT_MAX_NUM_UP_BUFFERS >= 4 && SEGGER_RTT_MAX_NUM_DOWN_BUFFERS >= 4
select SERIAL_HAS_DRIVER
select UART_RTT_DRIVER
help
Enable UART on RTT channel 3
if UART_RTT_3
config UART_RTT_3_TX_BUFFER_SIZE
int "Size of RTT_3 TX buffer (up to host)"
range 1 65535
default 1024
help
Size of the RTT up buffer for UART 3 transmission.
config UART_RTT_3_RX_BUFFER_SIZE
int "Size of RTT_3 RX buffer (down from host)"
range 1 65535
default 16
help
Size of the RTT down buffer for UART 3 reception.
endif
config UART_RTT_DRIVER
bool

View file

@ -7,6 +7,8 @@
#include <drivers/uart.h>
#include <SEGGER_RTT.h>
#define DT_DRV_COMPAT segger_rtt_uart
struct uart_rtt_config {
void *up_buffer;
size_t up_size;
@ -77,44 +79,44 @@ static const struct uart_driver_api uart_rtt_driver_api = {
.poll_out = uart_rtt_poll_out,
};
#if CONFIG_UART_RTT_0
#define UART_RTT(idx) DT_NODELABEL(rtt##idx)
#define UART_RTT_PROP(idx, prop) DT_PROP(UART_RTT(idx), prop)
#define UART_RTT_CONFIG_NAME(idx) uart_rtt##idx##_config
DEVICE_AND_API_INIT(uart_rtt0, "RTT_0", uart_rtt_init, NULL, NULL,
/* Initialize UART device after RTT init. */
PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
&uart_rtt_driver_api);
#define UART_RTT_CONFIG(idx) \
static \
uint8_t uart_rtt##idx##_tx_buf[UART_RTT_PROP(idx, tx_buffer_size)]; \
static \
uint8_t uart_rtt##idx##_rx_buf[UART_RTT_PROP(idx, rx_buffer_size)]; \
\
static const struct uart_rtt_config UART_RTT_CONFIG_NAME(idx) = { \
.up_buffer = uart_rtt##idx##_tx_buf, \
.up_size = sizeof(uart_rtt##idx##_tx_buf), \
.down_buffer = uart_rtt##idx##_rx_buf, \
.down_size = sizeof(uart_rtt##idx##_rx_buf), \
}
#endif
#define UART_RTT_CHANNEL(n) \
static uint8_t \
uart_rtt##n##_tx_buffer[CONFIG_UART_RTT_##n##_TX_BUFFER_SIZE]; \
static uint8_t \
uart_rtt##n##_rx_buffer[CONFIG_UART_RTT_##n##_RX_BUFFER_SIZE]; \
\
static const char uart_rtt##n##_name[] = "RTT_" #n "\0"; \
\
static const struct uart_rtt_config uart_rtt##n##_config = { \
.channel = n, \
.up_buffer = uart_rtt##n##_tx_buffer, \
.up_size = sizeof(uart_rtt##n##_tx_buffer), \
.down_buffer = uart_rtt##n##_rx_buffer, \
.down_size = sizeof(uart_rtt##n##_rx_buffer), \
}; \
\
DEVICE_AND_API_INIT(uart_rtt##n, uart_rtt##n##_name, uart_rtt_init, \
NULL, &uart_rtt##n##_config, PRE_KERNEL_2, \
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
#define UART_RTT_INIT(idx, config) \
DEVICE_AND_API_INIT(uart_rtt##idx, DT_LABEL(UART_RTT(idx)), \
uart_rtt_init, NULL, config, \
PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
&uart_rtt_driver_api)
#if CONFIG_UART_RTT_1
UART_RTT_CHANNEL(1);
#ifdef CONFIG_UART_RTT_0
UART_RTT_INIT(0, NULL);
#endif
#if CONFIG_UART_RTT_2
UART_RTT_CHANNEL(2);
#ifdef CONFIG_UART_RTT_1
UART_RTT_CONFIG(1);
UART_RTT_INIT(1, &UART_RTT_CONFIG_NAME(1));
#endif
#if CONFIG_UART_RTT_3
UART_RTT_CHANNEL(3);
#ifdef CONFIG_UART_RTT_2
UART_RTT_CONFIG(2);
UART_RTT_INIT(2, &UART_RTT_CONFIG_NAME(2));
#endif
#ifdef CONFIG_UART_RTT_3
UART_RTT_CONFIG(3);
UART_RTT_INIT(3, &UART_RTT_CONFIG_NAME(3));
#endif

View file

@ -0,0 +1,27 @@
# Copyright (c) 2020, CSIRO.
# SPDX-License-Identifier: Apache-2.0
description: Segger RTT UART
compatible: "segger,rtt-uart"
include: uart-controller.yaml
properties:
tx-buffer-size:
type: int
default: 1024
description: |
Size of the RTT up buffer for transmission
Not used for RTT channel 0 as channel 0 is initialized at compile time,
see SEGGER_RTT_BUFFER_SIZE_UP.
required: false
rx-buffer-size:
type: int
default: 16
description: |
Size of the RTT down buffer for reception
Not used for RTT channel 0 as channel 0 is initialized at compile time,
see SEGGER_RTT_BUFFER_SIZE_DOWN.
required: false