Skip to content

Commit

Permalink
hal/imxrt: Support RTT as a HAL console
Browse files Browse the repository at this point in the history
UART is enabled by default (current behavior), but can be turned off
by defining UART_CONSOLE_KERNEL with an empty value. RTT is enabled by
defining RTT_ENABLED with a truthy value and (optionally) selecting a
channel to use by RTT_CONSOLE_KERNEL (0-based). UART and RTT may be used
at the same time.

RTT driver reuses the control block configured by plo, which must also
create a map named via RTT_SYSPAGE_MAP_NAME (default: "rtt"). Channel
configuration is not altered at all, so RTT will work even when
user-mode driver takes control - exception dumps will still be printed.

JIRA: RTOS-754
  • Loading branch information
Darchiv committed Sep 27, 2024
1 parent 52e5cf1 commit f19f0e6
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 15 deletions.
2 changes: 1 addition & 1 deletion hal/arm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
# TODO handle other common ARM stuff (e.g GIC) and
# select relevant components here

OBJS += $(addprefix $(PREFIX_O)hal/arm/, nvic.o scb.o)
OBJS += $(addprefix $(PREFIX_O)hal/arm/, nvic.o scb.o rtt.o)
154 changes: 154 additions & 0 deletions hal/arm/rtt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Phoenix-RTOS
*
* SEGGER's Real Time Transfer - simplified driver
*
* Copyright 2023-2024 Phoenix Systems
* Author: Gerard Swiderski, Daniel Sawka
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#include "include/errno.h"
#include "syspage.h"
#include "hal/arm/barriers.h"

#include <board_config.h>
#include "rtt.h"

#ifndef RTT_SYSPAGE_MAP_NAME
#define RTT_SYSPAGE_MAP_NAME "rtt"
#endif

#ifndef RTT_CB_SIZE
#define RTT_CB_SIZE 256
#endif


struct rtt_pipe {
const char *name;
volatile unsigned char *ptr;
unsigned int sz;
volatile unsigned int wr;
volatile unsigned int rd;
unsigned int flags;
};


struct rtt_desc {
char tag[16];
unsigned int txChannels;
unsigned int rxChannels;
struct rtt_pipe channels[];
};


static struct {
volatile struct rtt_desc *rtt;
} common;


static int rtt_check(unsigned int chan, rtt_dir_t dir)
{
if ((dir == rtt_dir_up) && (chan >= common.rtt->txChannels)) {
return -ENODEV;
}

if ((dir == rtt_dir_down) && (chan >= common.rtt->rxChannels)) {
return -ENODEV;
}

return 0;
}


int _hal_rttWrite(unsigned int chan, const void *buf, unsigned int count)
{
unsigned int sz;
unsigned int rd;
unsigned int wr;
unsigned int todo;
volatile unsigned char *dstBuf;

if (rtt_check(chan, rtt_dir_up) < 0) {
return -ENODEV;
}

hal_cpuDataMemoryBarrier();
dstBuf = common.rtt->channels[chan].ptr;
sz = common.rtt->channels[chan].sz - 1;
rd = (common.rtt->channels[chan].rd + sz) & sz;
wr = common.rtt->channels[chan].wr & sz;
todo = count;

/* TODO: Support all buffer modes (currently only trim is used, regardless of flags) */
while ((todo != 0) && (rd != wr)) {
dstBuf[wr] = *(unsigned char *)buf++;
wr = (wr + 1) & sz;
todo--;
}

hal_cpuDataMemoryBarrier();
common.rtt->channels[chan].wr = wr;

return count - todo;
}


int _hal_rttTxAvail(unsigned int chan)
{
unsigned int sz;
unsigned int rd;
unsigned int wr;

if (rtt_check(chan, rtt_dir_up) < 0) {
return -ENODEV;
}

hal_cpuDataMemoryBarrier();
sz = common.rtt->channels[chan].sz - 1;
rd = (common.rtt->channels[chan].rd + sz) & sz;
wr = common.rtt->channels[chan].wr & sz;

if (wr > rd) {
return sz + 1 - (wr - rd);
}
else {
return rd - wr;
}
}


int _hal_rttReset(unsigned int chan, rtt_dir_t dir)
{
if (rtt_check(chan, dir) < 0) {
return -ENODEV;
}

hal_cpuDataMemoryBarrier();
if (dir == rtt_dir_up) {
common.rtt->channels[chan].wr = common.rtt->channels[chan].rd;
}
else {
chan = common.rtt->txChannels + chan;
common.rtt->channels[chan].rd = common.rtt->channels[chan].wr;
}
hal_cpuDataMemoryBarrier();
return 0;
}


int _hal_rttInit(void)
{
const syspage_map_t *map = syspage_mapNameResolve(RTT_SYSPAGE_MAP_NAME);

if (map == NULL) {
return -ENOENT;
}

/* TODO: Place CB always at the start of the map? */
common.rtt = (void *)(map->end - RTT_CB_SIZE);
return 0;
}
47 changes: 47 additions & 0 deletions hal/arm/rtt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Phoenix-RTOS
*
* SEGGER's Real Time Transfer - simplified driver
*
* Copyright 2023-2024 Phoenix Systems
* Author: Gerard Swiderski, Daniel Sawka
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#ifndef HAL_ARM_RTT_H_
#define HAL_ARM_RTT_H_


typedef enum {
rtt_dir_up, /* tx: target -> host */
rtt_dir_down, /* rx: host -> target */
} rtt_dir_t;


typedef enum {
rtt_mode_skip = 0, /* write if the whole message can be written at once; discard the message otherwise */
rtt_mode_trim = 1, /* write anything if possible; discard the remaining unwritten data otherwise */
rtt_mode_blocking = 2, /* wait until writable */
} rtt_mode_t;


/* Initialize rtt internal structures */
int _hal_rttInit(void);


/* Non-blocking write to channel */
int _hal_rttWrite(unsigned int chan, const void *buf, unsigned int count);


/* Check for available space in tx */
int _hal_rttTxAvail(unsigned int chan);


/* Reset fifo pointers */
int _hal_rttReset(unsigned int chan, rtt_dir_t dir);


#endif /* end of HAL_ARM_RTT_H_ */
48 changes: 41 additions & 7 deletions hal/armv7m/imxrt/10xx/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Operating system kernel
*
* HAL console (iMXRT USART)
* HAL console (iMXRT USART + RTT)
*
* Copyright 2016-2017 Phoenix Systems
* Author: Pawel Pisarczyk, Artur Wodejko, Aleksander Kaminski
Expand All @@ -14,21 +14,35 @@
*/

#include "hal/console.h"
#include "hal/arm/rtt.h"
#include "include/arch/armv7m/imxrt/10xx/imxrt10xx.h"
#include "imxrt10xx.h"
#include "lib/helpers.h"
#include <arch/cpu.h>

#include <board_config.h>

#ifndef UART_CONSOLE
#define UART_CONSOLE 1
#ifndef UART_CONSOLE_KERNEL
#ifdef UART_CONSOLE
#define UART_CONSOLE_KERNEL UART_CONSOLE
#else
#define UART_CONSOLE_KERNEL 1
#endif
#endif

#ifndef RTT_CONSOLE_KERNEL
#if RTT_ENABLED
#define RTT_CONSOLE_KERNEL 0
#else
#define RTT_CONSOLE_KERNEL
#endif
#endif

#define CONCAT3(a, b, c) a##b##c
#define CONSOLE_BAUD(n) (CONCAT3(UART, n, _BAUDRATE))

#if CONSOLE_BAUD(UART_CONSOLE)
#define CONSOLE_BAUDRATE CONSOLE_BAUD(UART_CONSOLE)
#if !ISEMPTY(UART_CONSOLE_KERNEL) && CONSOLE_BAUD(UART_CONSOLE_KERNEL)
#define CONSOLE_BAUDRATE CONSOLE_BAUD(UART_CONSOLE_KERNEL)
#else
#define CONSOLE_BAUDRATE 115200
#endif
Expand Down Expand Up @@ -63,16 +77,23 @@ void hal_consolePrint(int attr, const char *s)

void hal_consolePutch(char c)
{
#if RTT_ENABLED && !ISEMPTY(RTT_CONSOLE_KERNEL)
_hal_rttWrite(RTT_CONSOLE_KERNEL, &c, 1);
#endif

#if !ISEMPTY(UART_CONSOLE_KERNEL)
while (!(*(console_common.uart + uart_stat) & (1 << 23)))
;

*(console_common.uart + uart_data) = c;
#endif
}


void _hal_consoleInit(void)
#if !ISEMPTY(UART_CONSOLE_KERNEL)
static void _hal_uartInit(void)
{
u32 t, console = UART_CONSOLE - 1;
u32 t, console = UART_CONSOLE_KERNEL - 1;

static const struct {
volatile u32 *base;
Expand Down Expand Up @@ -155,3 +176,16 @@ void _hal_consoleInit(void)
/* Enable TX and RX */
*(console_common.uart + uart_ctrl) |= (1 << 19) | (1 << 18);
}
#endif


void _hal_consoleInit(void)
{
#if RTT_ENABLED && !ISEMPTY(RTT_CONSOLE_KERNEL)
_hal_rttInit();
#endif

#if !ISEMPTY(UART_CONSOLE_KERNEL)
_hal_uartInit();
#endif
}
48 changes: 41 additions & 7 deletions hal/armv7m/imxrt/117x/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Operating system kernel
*
* HAL console (i.MX RT1170 UART)
* HAL console (i.MX RT1170 UART + RTT)
*
* Copyright 2016-2017, 2019 Phoenix Systems
* Author: Pawel Pisarczyk, Artur Wodejko, Aleksander Kaminski
Expand All @@ -14,21 +14,35 @@
*/

#include "hal/console.h"
#include "hal/arm/rtt.h"
#include "include/arch/armv7m/imxrt/11xx/imxrt1170.h"
#include "imxrt117x.h"
#include "lib/helpers.h"
#include <arch/cpu.h>

#include <board_config.h>

#ifndef UART_CONSOLE
#define UART_CONSOLE 11
#ifndef UART_CONSOLE_KERNEL
#ifdef UART_CONSOLE
#define UART_CONSOLE_KERNEL UART_CONSOLE
#else
#define UART_CONSOLE_KERNEL 11
#endif
#endif

#ifndef RTT_CONSOLE_KERNEL
#if RTT_ENABLED
#define RTT_CONSOLE_KERNEL 0
#else
#define RTT_CONSOLE_KERNEL
#endif
#endif

#define CONCAT3(a, b, c) a##b##c
#define CONSOLE_BAUD(n) (CONCAT3(UART, n, _BAUDRATE))

#if CONSOLE_BAUD(UART_CONSOLE)
#define CONSOLE_BAUDRATE CONSOLE_BAUD(UART_CONSOLE)
#if !ISEMPTY(UART_CONSOLE_KERNEL) && CONSOLE_BAUD(UART_CONSOLE_KERNEL)
#define CONSOLE_BAUDRATE CONSOLE_BAUD(UART_CONSOLE_KERNEL)
#else
#define CONSOLE_BAUDRATE 115200
#endif
Expand Down Expand Up @@ -63,16 +77,23 @@ void hal_consolePrint(int attr, const char *s)

void hal_consolePutch(char c)
{
#if RTT_ENABLED && !ISEMPTY(RTT_CONSOLE_KERNEL)
_hal_rttWrite(RTT_CONSOLE_KERNEL, &c, 1);
#endif

#if !ISEMPTY(UART_CONSOLE_KERNEL)
while (!(*(console_common.uart + uart_stat) & (1 << 23)))
;

*(console_common.uart + uart_data) = c;
#endif
}


void _hal_consoleInit(void)
#if !ISEMPTY(UART_CONSOLE_KERNEL)
static void _hal_uartInit(void)
{
u32 t, console = UART_CONSOLE - 1;
u32 t, console = UART_CONSOLE_KERNEL - 1;

static const struct {
volatile u32 *base;
Expand Down Expand Up @@ -156,3 +177,16 @@ void _hal_consoleInit(void)
/* Enable TX and RX */
*(console_common.uart + uart_ctrl) |= (1 << 19) | (1 << 18);
}
#endif


void _hal_consoleInit(void)
{
#if RTT_ENABLED && !ISEMPTY(RTT_CONSOLE_KERNEL)
_hal_rttInit();
#endif

#if !ISEMPTY(UART_CONSOLE_KERNEL)
_hal_uartInit();
#endif
}
Loading

0 comments on commit f19f0e6

Please sign in to comment.