From adf7e692e2e80e54c4a509557bc18eaaad11c803 Mon Sep 17 00:00:00 2001 From: Brad Campbell Date: Fri, 11 Sep 2026 11:05:28 -0400 Subject: [PATCH] add a console2 test app This writes data to a second console syscall driver useful for testing boards that have more than one serial interface. --- examples/tests/console2/Makefile | 11 +++++++ examples/tests/console2/README.md | 36 +++++++++++++++++++++ examples/tests/console2/console2.c | 51 ++++++++++++++++++++++++++++++ examples/tests/console2/console2.h | 9 ++++++ examples/tests/console2/main.c | 33 +++++++++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 examples/tests/console2/Makefile create mode 100644 examples/tests/console2/README.md create mode 100644 examples/tests/console2/console2.c create mode 100644 examples/tests/console2/console2.h create mode 100644 examples/tests/console2/main.c diff --git a/examples/tests/console2/Makefile b/examples/tests/console2/Makefile new file mode 100644 index 000000000..54d6a7969 --- /dev/null +++ b/examples/tests/console2/Makefile @@ -0,0 +1,11 @@ +# Makefile for user application + +# Specify this directory relative to the current application. +TOCK_USERLAND_BASE_DIR = ../../.. + +# Which files to compile. +C_SRCS := $(wildcard *.c) + +# Include userland master makefile. Contains rules and flags for actually +# building the application. +include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk diff --git a/examples/tests/console2/README.md b/examples/tests/console2/README.md new file mode 100644 index 000000000..57e21f8a3 --- /dev/null +++ b/examples/tests/console2/README.md @@ -0,0 +1,36 @@ +Console2 Test App +================= + +This app tests writing to a second serial console, using the same +`capsules/core/src/console.rs` syscall driver. + +Second Console Driver Num +------------------------- + +The second console driver num is created by ORing the number 1 shifted 24 bits +left with the normal console driver num. + +```c +#define DRIVER_NUM_CONSOLE2 (DRIVER_NUM_CONSOLE | 0x01000000) +``` + +Expected Output +--------------- + +In the second serial port: + +``` +The is the second serial console. (0) +The is the second serial console. (1) +The is the second serial console. (2) +The is the second serial console. (3) +The is the second serial console. (4) +The is the second serial console. (5) +The is the second serial console. (6) +``` + +In the normal serial port: + +``` +[Console2] Writing to second serial console. +``` diff --git a/examples/tests/console2/console2.c b/examples/tests/console2/console2.c new file mode 100644 index 000000000..a59e83bf6 --- /dev/null +++ b/examples/tests/console2/console2.c @@ -0,0 +1,51 @@ +#include + +#include +#include + +#include "console2.h" + +#define DRIVER_NUM_CONSOLE2 (DRIVER_NUM_CONSOLE | 0x01000000) + +#define CONSOLE2_COMMAND_WRITE 1 +#define CONSOLE2_ALLOW_WRITE 1 +#define CONSOLE2_SUBSCRIBE_WRITE 1 + +static returncode_t console2_set_read_allow(const uint8_t* buffer, uint32_t len) { + allow_ro_return_t aval = allow_readonly(DRIVER_NUM_CONSOLE2, CONSOLE2_ALLOW_WRITE, (void*) buffer, len); + return tock_allow_ro_return_to_returncode(aval); +} + +static returncode_t console2_command_write(int length) { + syscall_return_t cval = command(DRIVER_NUM_CONSOLE2, CONSOLE2_COMMAND_WRITE, length, 0); + return tock_command_return_novalue_to_returncode(cval); +} + +static returncode_t console2_yield_wait_for_write(uint32_t* bytes_written) { + yield_waitfor_return_t ret; + ret = yield_wait_for(DRIVER_NUM_CONSOLE2, CONSOLE2_SUBSCRIBE_WRITE); + + *bytes_written = ret.data1; + + return tock_status_to_returncode(ret.data0); +} + + +bool console2_driver_exists(void) { + return driver_exists(DRIVER_NUM_CONSOLE2); +} + +returncode_t console2_sync_write(const uint8_t* buffer, uint32_t length, uint32_t* written) { + int err; + + err = console2_set_read_allow(buffer, length); + if (err != RETURNCODE_SUCCESS) return err; + defer { console2_set_read_allow(NULL, 0); + } + + err = console2_command_write((int) length); + if (err != RETURNCODE_SUCCESS) return err; + + err = console2_yield_wait_for_write(written); + return err; +} diff --git a/examples/tests/console2/console2.h b/examples/tests/console2/console2.h new file mode 100644 index 000000000..94065210b --- /dev/null +++ b/examples/tests/console2/console2.h @@ -0,0 +1,9 @@ +#pragma once + +// Implement writing data to the console driver on the second console driver num. + +#include +#include + +bool console2_driver_exists(void); +returncode_t console2_sync_write(const uint8_t* buffer, uint32_t length, uint32_t* written); diff --git a/examples/tests/console2/main.c b/examples/tests/console2/main.c new file mode 100644 index 000000000..dc21cb65e --- /dev/null +++ b/examples/tests/console2/main.c @@ -0,0 +1,33 @@ +// Licensed under the Apache License, Version 2.0 or the MIT License. +// SPDX-License-Identifier: Apache-2.0 OR MIT +// Copyright Tock Contributors 2026. + + + +#include + +#include + +#include "console2.h" + + + +int main(void) { + printf("[Console2] Writing to second serial console.\n"); + + const char TX[] = "The is the second serial console."; + uint32_t written = 0; + + // Write the the second console in a continuous loop. + int count = 0; + while (1) { + char out[256]; + int len = snprintf(out, 256, "%s (%d)\n", TX, count); + console2_sync_write((uint8_t*) out, len, &written); + count += 1; + + libtocksync_alarm_delay_ms(603); + } + + return 0; +}