Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/tests/console2/Makefile
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions examples/tests/console2/README.md
Original file line number Diff line number Diff line change
@@ -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.
```
51 changes: 51 additions & 0 deletions examples/tests/console2/console2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <stdbool.h>

#include <libtock-sync/services/alarm.h>
#include <libtock/defer.h>

#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;
}
9 changes: 9 additions & 0 deletions examples/tests/console2/console2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

// Implement writing data to the console driver on the second console driver num.

#include <libtock/tock.h>
#include <libtock/interface/syscalls/console_syscalls.h>

bool console2_driver_exists(void);
returncode_t console2_sync_write(const uint8_t* buffer, uint32_t length, uint32_t* written);
33 changes: 33 additions & 0 deletions examples/tests/console2/main.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

#include <libtock-sync/services/alarm.h>

#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;
}
Loading