diff --git a/cmds/console.c b/cmds/console.c index f5c9561c..72a172a1 100644 --- a/cmds/console.c +++ b/cmds/console.c @@ -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; } diff --git a/hal/armv7m/imxrt/10xx/console.c b/hal/armv7m/imxrt/10xx/console.c index a6a6bbe5..f3e9b2ec 100644 --- a/hal/armv7m/imxrt/10xx/console.c +++ b/hal/armv7m/imxrt/10xx/console.c @@ -16,6 +16,10 @@ #include #include +#include + +#if !ISEMPTY(UART_CONSOLE_PLO) + #define CONCAT(a, b) a##b #define CONCAT2(a, b) CONCAT(a, b) @@ -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) */ diff --git a/hal/armv7m/imxrt/117x/console.c b/hal/armv7m/imxrt/117x/console.c index f8879be8..b53e2277 100644 --- a/hal/armv7m/imxrt/117x/console.c +++ b/hal/armv7m/imxrt/117x/console.c @@ -16,6 +16,10 @@ #include #include +#include + +#if !ISEMPTY(UART_CONSOLE_PLO) + #define CONCAT(a, b) a##b #define CONCAT2(a, b) CONCAT(a, b) @@ -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) */ diff --git a/hal/armv7m/imxrt/hal.c b/hal/armv7m/imxrt/hal.c index e5bad736..1aff7d19 100644 --- a/hal/armv7m/imxrt/hal.c +++ b/hal/armv7m/imxrt/hal.c @@ -15,6 +15,9 @@ #include #include +#include +#include +#include #include "otp.h" @@ -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(); diff --git a/hal/armv7m/stm32/hal.c b/hal/armv7m/stm32/hal.c index f2b75788..b39229d6 100644 --- a/hal/armv7m/stm32/hal.c +++ b/hal/armv7m/stm32/hal.c @@ -15,6 +15,9 @@ #include #include +#include +#include +#include struct { hal_syspage_t *hs; @@ -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(); diff --git a/hal/armv7m/stm32/l4/console.c b/hal/armv7m/stm32/l4/console.c index 3d446a98..e77bccf1 100644 --- a/hal/armv7m/stm32/l4/console.c +++ b/hal/armv7m/stm32/l4/console.c @@ -14,8 +14,11 @@ */ #include +#include #include "stm32l4.h" +#if !ISEMPTY(UART_CONSOLE_PLO) + static struct { volatile u32 *base; @@ -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) */ diff --git a/hal/armv7m/stm32/l4/peripherals.h b/hal/armv7m/stm32/l4/peripherals.h index 7832fd8e..b88945d9 100644 --- a/hal/armv7m/stm32/l4/peripherals.h +++ b/hal/armv7m/stm32/l4/peripherals.h @@ -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) diff --git a/lib/console.c b/lib/console.c index 32bd0e9b..b3594aac 100644 --- a/lib/console.c +++ b/lib/console.c @@ -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); } diff --git a/lib/helpers.h b/lib/helpers.h new file mode 100644 index 00000000..1f2c3342 --- /dev/null +++ b/lib/helpers.h @@ -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