Skip to content

Commit

Permalink
hal/armv7m: Support RTT as an early console
Browse files Browse the repository at this point in the history
Currently RTT is available as a plo device and also mirrors UART output
via hooks. This change makes it possible to replace UART completely by
RTT - just define `UART_CONSOLE_PLO` as empty and keep RTT_ENABLED_PLO
truthy (enabled by default).

A print is moved from `lib_consoleSet()` to console command to avoid
output before the first plo log ("Phoenix-RTOS loader ...").

JIRA: RTOS-754
  • Loading branch information
Darchiv committed Oct 7, 2024
1 parent 32fcc3b commit 3b0f4d5
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmds/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static int cmd_console(int argc, char *argv[])
return CMD_EXIT_FAILURE;
}

lib_printf("\nconsole: Setting console to %d.%d", major, minor);
lib_consoleSet(major, minor);
return CMD_EXIT_SUCCESS;
}
Expand Down
24 changes: 24 additions & 0 deletions hal/armv7m/imxrt/10xx/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include <board_config.h>

#include <hal/hal.h>
#include <lib/helpers.h>

#if !ISEMPTY(UART_CONSOLE_PLO)


#define CONCAT(a, b) a##b
#define CONCAT2(a, b) CONCAT(a, b)
Expand Down Expand Up @@ -159,3 +163,23 @@ void console_init(void)
/* Enable TX and RX */
*(halconsole_common.uart + uart_ctrl) |= (1 << 19) | (1 << 18);
}

#else

void hal_consoleSetHooks(ssize_t (*writeHook)(int, const void *, size_t))
{
(void)writeHook;
}


void hal_consolePrint(const char *s)
{
(void)s;
}


void console_init(void)
{
}

#endif /* #if !ISEMPTY(UART_CONSOLE_PLO) */
24 changes: 24 additions & 0 deletions hal/armv7m/imxrt/117x/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include <board_config.h>

#include <hal/hal.h>
#include <lib/helpers.h>

#if !ISEMPTY(UART_CONSOLE_PLO)


#define CONCAT(a, b) a##b
#define CONCAT2(a, b) CONCAT(a, b)
Expand Down Expand Up @@ -220,3 +224,23 @@ void console_init(void)
/* Enable TX and RX */
*(halconsole_common.uart + uart_ctrl) |= (1 << 19) | (1 << 18);
}

#else

void hal_consoleSetHooks(ssize_t (*writeHook)(int, const void *, size_t))
{
(void)writeHook;
}


void hal_consolePrint(const char *s)
{
(void)s;
}


void console_init(void)
{
}

#endif /* #if !ISEMPTY(UART_CONSOLE_PLO) */
9 changes: 9 additions & 0 deletions hal/armv7m/imxrt/hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

#include <board_config.h>
#include <hal/hal.h>
#include <devices/devs.h>
#include <lib/helpers.h>
#include <lib/console.h>
#include "otp.h"


Expand Down Expand Up @@ -68,6 +71,12 @@ void hal_init(void)

#if RTT_ENABLED_PLO
rtt_init(__rttmem_rttcb);

#if ISEMPTY(UART_CONSOLE_PLO)
/* TODO: Channel number is already hardcoded in halconsole_common.writeHook.
Maybe let user redefine RTT channel to be used in plo? */
lib_consoleSet(DEV_PIPE, 0);
#endif
#endif

console_init();
Expand Down
9 changes: 9 additions & 0 deletions hal/armv7m/stm32/hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

#include <board_config.h>
#include <hal/hal.h>
#include <devices/devs.h>
#include <lib/helpers.h>
#include <lib/console.h>

struct {
hal_syspage_t *hs;
Expand Down Expand Up @@ -65,6 +68,12 @@ void hal_init(void)

#if RTT_ENABLED_PLO
rtt_init(__rttmem_rttcb);

#if ISEMPTY(UART_CONSOLE_PLO)
/* TODO: Channel number is already hardcoded in halconsole_common.writeHook.
Maybe let user redefine RTT channel to be used in plo? */
lib_consoleSet(DEV_PIPE, 0);
#endif
#endif

console_init();
Expand Down
23 changes: 23 additions & 0 deletions hal/armv7m/stm32/l4/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
*/

#include <hal/hal.h>
#include <lib/helpers.h>
#include "stm32l4.h"

#if !ISEMPTY(UART_CONSOLE_PLO)


static struct {
volatile u32 *base;
Expand Down Expand Up @@ -95,3 +98,23 @@ void console_init(void)
*(halconsole_common.base + cr1) |= 1;
hal_cpuDataMemoryBarrier();
}

#else

void hal_consoleSetHooks(ssize_t (*writeHook)(int, const void *, size_t))
{
(void)writeHook;
}


void hal_consolePrint(const char *s)
{
(void)s;
}


void console_init(void)
{
}

#endif /* #if !ISEMPTY(UART_CONSOLE_PLO) */
4 changes: 4 additions & 0 deletions hal/armv7m/stm32/l4/peripherals.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
#define UART_CONSOLE 2
#endif

#ifndef UART_CONSOLE_PLO
#define UART_CONSOLE_PLO UART_CONSOLE
#endif

#define UART_BAUDRATE 115200

#define UART1_BASE ((void *)0x40013800)
Expand Down
1 change: 0 additions & 1 deletion lib/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ void lib_consoleSet(unsigned major, unsigned minor)
console_common.major = major;
console_common.minor = minor;
console_common.init = 1;
lib_printf("\nconsole: Setting console to %d.%d", major, minor);
}


Expand Down
35 changes: 35 additions & 0 deletions lib/helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Phoenix-RTOS
*
* Generic useful macros
*
* Copyright 2024 Phoenix Systems
* Author: Daniel Sawka
*
* %LICENSE%
*/

#ifndef _LIB_HELPERS_H_
#define _LIB_HELPERS_H_

/*
These macros should only be used in `#if` and `#elif` directives, because undefined identifiers expand to 0 there.
Otherwise there will be "use of undefined identifier" errors (an exception: identifier is first checked for
existence with e.g. `#ifdef`).
Anything that expands to a numerical expression (or to an empty value) is fine. String literals don't work.
*/

/*
These macros produce a logically correct result only if X is defined. You may use `#ifdef` or `defined()`
for this purpose. Unfortunately, `defined()` cannot be conveniently put inside a macro as this is undefined
behavior (see -Wexpansion-to-defined for details), so you have to use it directly on the spot, for example:
`#if defined(PERIPH1) && !ISEMPTY(PERIPH1)
// Use PERIPH1
#endif`
*/

/* True if X is empty (has no value). The result in #if is valid only if defined(X) is true */
#define ISEMPTY(X) ((0 - X - 1) == 1 && (X + 0) != -2)

#endif

0 comments on commit 3b0f4d5

Please sign in to comment.