Skip to content

Commit 6b66485

Browse files
committed
libtock-rs: Sync with upstream to Merge #365
The only major change from upstream that this preserves is customized external assembly linkage. This should be mostly removed once libtock switches to using `global_asm!` - now that that will be stabilized. This CL is a squash merge of the following commits: 28ebb70 Merge #365 a0440ba Merge #370 abff51c Merge #359 85242dc Merge #364 f1bd0d4 Merge #361 b0f8593 Merge #355 4c7ecb6 Merge #358 6f8b512 Merge #356 73cbbde Merge #357 022984f Merge #353 b2e6471 Merge #352 9eb12c0 Merge #351 This was retrieved with the following command: ``` git log ti50-internal/upstream/master --not \ $(git merge-base --all ti50-internal/upstream/master m/main) \ --first-parent ``` BUG=None TEST=ti50/common make build SOURCE=UPSTREAM(28ebb70) Change-Id: I0aa1606f407a0b4bcf0c76b62261414903479092
1 parent 6c635ca commit 6b66485

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1512
-293
lines changed

.cargo/config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ rustflags = [
1212
"-C", "relocation-model=static",
1313
"-C", "link-arg=-Tlayout.ld",
1414
]
15-
runner = "./tools/flash.sh"
15+
runner = ["cargo", "run", "-p", "runner", "--release"]

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
- name: Build and Test
4444
run: |
4545
sudo apt-get install binutils-arm-none-eabi \
46-
binutils-riscv64-unknown-elf
46+
binutils-riscv64-unknown-elf ninja-build
4747
cd "${GITHUB_WORKSPACE}"
4848
echo "[target.'cfg(all())']" >> .cargo/config
4949
echo 'rustflags = ["-D", "warnings"]' >> .cargo/config

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,16 @@ lto = true
8282
debug = true
8383

8484
[workspace]
85-
exclude = [ "tock" ]
85+
exclude = ["tock", "tock2"]
8686
members = [
87+
"apis/leds",
8788
"apis/low_level_debug",
8889
"codegen",
8990
"core",
9091
"libtock2",
9192
"panic_handlers/small_panic",
9293
"platform",
94+
"runner",
9395
"runtime",
9496
"syscalls_tests",
9597
"test_runner",

Makefile

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ usage:
1111
@echo "libtock-rs currently includes support for the following platforms:"
1212
@echo " - hail"
1313
@echo " - nrf52840"
14+
@echo " - microbit_v2"
1415
@echo " - nucleo_f429zi"
1516
@echo " - nucleo_f446re"
1617
@echo " - opentitan"
@@ -25,6 +26,7 @@ usage:
2526
@echo " Set the DEBUG flag to enable the debug build"
2627
@echo " Set the FEATURES flag to enable features"
2728
@echo "Run 'make flash-<board> EXAMPLE=<>' to flash EXAMPLE to that board"
29+
@echo "Run 'make qemu-example EXAMPLE=<>' to run EXAMPLE in QEMU"
2830
@echo "Run 'make test' to test any local changes you have made"
2931
@echo "Run 'make print-sizes' to print size data for the example binaries"
3032

@@ -37,8 +39,8 @@ release=--release
3739
endif
3840

3941
.PHONY: setup
40-
setup: setup-qemu
41-
cargo install elf2tab --version 0.6.0
42+
setup: setup-qemu setup-qemu-2
43+
cargo install elf2tab
4244
cargo install stack-sizes
4345
cargo miri setup
4446

@@ -48,6 +50,12 @@ setup: setup-qemu
4850
setup-qemu:
4951
CI=true $(MAKE) -C tock ci-setup-qemu
5052

53+
# Sets up QEMU in the tock2/ directory. We use Tock's QEMU which may contain
54+
# patches to better support boards that Tock supports.
55+
.PHONY: setup-qemu-2
56+
setup-qemu-2:
57+
CI=true $(MAKE) -C tock2 ci-setup-qemu
58+
5159
# Builds a Tock kernel for the HiFive board for use by QEMU tests.
5260
.PHONY: kernel-hifive
5361
kernel-hifive:
@@ -62,11 +70,25 @@ kernel-hifive-2:
6270
$(MAKE) -C tock2/boards/hifive1 \
6371
$(CURDIR)/tock2/target/riscv32imac-unknown-none-elf/release/hifive1.elf
6472

73+
# Builds a Tock kernel for the OpenTitan board on the cw310 FPGA for use by QEMU
74+
# tests.
75+
.PHONY: kernel-opentitan
76+
kernel-opentitan:
77+
CARGO_TARGET_RISCV32IMC_UNKNOWN_NONE_ELF_RUNNER="[]" \
78+
$(MAKE) -C tock2/boards/opentitan/earlgrey-cw310 \
79+
$(CURDIR)/tock2/target/riscv32imc-unknown-none-elf/release/earlgrey-cw310.elf
80+
6581
# Prints out the sizes of the example binaries.
6682
.PHONY: print-sizes
6783
print-sizes: examples
6884
cargo run --release -p print_sizes
6985

86+
# Runs a libtock2 example in QEMU on a simulated HiFive board.
87+
.PHONY: qemu-example
88+
qemu-example: kernel-hifive-2
89+
LIBTOCK_PLATFORM="hifive1" cargo run --example "$(EXAMPLE)" -p libtock2 \
90+
--release --target=riscv32imac-unknown-none-elf -- --deploy qemu
91+
7092
# Runs the libtock_test tests in QEMU on a simulated HiFive board.
7193
.PHONY: test-qemu-hifive
7294
test-qemu-hifive: kernel-hifive
@@ -99,7 +121,7 @@ EXCLUDE_MIRI := $(EXCLUDE_RUNTIME) --exclude libtock_codegen \
99121
# Arguments to pass to cargo to exclude `std` and crates that depend on it. Used
100122
# when we build a crate for an embedded target, as those targets lack `std`.
101123
EXCLUDE_STD := --exclude libtock_unittest --exclude print_sizes \
102-
--exclude syscalls_tests --exclude test_runner
124+
--exclude runner --exclude syscalls_tests --exclude test_runner
103125

104126
# Some of our crates should build with a stable toolchain. This verifies those
105127
# crates don't depend on unstable features by using cargo check. We specify a
@@ -111,7 +133,7 @@ test-stable:
111133
$(EXCLUDE_RUNTIME) --exclude libtock --exclude libtock_core
112134

113135
.PHONY: test
114-
test: examples test-qemu-hifive test-stable
136+
test: examples test-stable
115137
PLATFORM=nrf52 cargo test $(EXCLUDE_RUNTIME) --workspace
116138
# TODO: When we have a working embedded test harness, change the libtock2
117139
# builds to --all-targets rather than --examples.
@@ -153,6 +175,14 @@ hail:
153175
flash-hail:
154176
PLATFORM=hail cargo run $(release) --target=thumbv7em-none-eabi --example $(EXAMPLE) $(features)
155177

178+
.PHONY: microbit_v2
179+
microbit_v2:
180+
PLATFORM=microbit_v2 cargo build $(release) --target=thumbv7em-none-eabi --examples $(features)
181+
182+
.PHONY: flash-microbit_v2
183+
flash-microbit_v2:
184+
PLATFORM=microbit_v2 cargo run $(release) --target=thumbv7em-none-eabi --example $(EXAMPLE) $(features)
185+
156186
.PHONY: nucleo_f429zi
157187
nucleo_f429zi:
158188
PLATFORM=nucleo_f429zi cargo build $(release) --target=thumbv7em-none-eabi --examples $(features)
@@ -229,3 +259,4 @@ flash-msp432:
229259
clean:
230260
cargo clean
231261
$(MAKE) -C tock clean
262+
$(MAKE) -C tock2 clean

apis/leds/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "libtock_leds"
3+
version = "0.1.0"
4+
authors = ["Tock Project Developers <[email protected]>"]
5+
license = "MIT/Apache-2.0"
6+
edition = "2018"
7+
repository = "https://www.github.com/tock/libtock-rs"
8+
description = "libtock leds driver"
9+
10+
[dependencies]
11+
libtock_platform = { path = "../../platform" }
12+
13+
[dev-dependencies]
14+
libtock_unittest = { path = "../../unittest" }

apis/leds/src/lib.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#![no_std]
2+
3+
use libtock_platform::{ErrorCode, Syscalls};
4+
5+
/// The LEDs driver
6+
///
7+
/// # Example
8+
/// ```ignore
9+
/// use libtock2::Leds;
10+
///
11+
/// // Turn on led 0
12+
/// let _ = Leds::on(0);
13+
/// ```
14+
pub struct Leds<S: Syscalls>(S);
15+
16+
impl<S: Syscalls> Leds<S> {
17+
/// Run a check against the leds capsule to ensure it is present.
18+
///
19+
/// Returns `Ok(number_of_leds)` if the driver was present. This does not necessarily mean
20+
/// that the driver is working, as it may still fail to allocate grant
21+
/// memory.
22+
pub fn count() -> Result<u32, ErrorCode> {
23+
S::command(DRIVER_ID, LEDS_COUNT, 0, 0).to_result()
24+
}
25+
26+
pub fn on(led: u32) -> Result<(), ErrorCode> {
27+
S::command(DRIVER_ID, LED_ON, led, 0).to_result()
28+
}
29+
30+
pub fn off(led: u32) -> Result<(), ErrorCode> {
31+
S::command(DRIVER_ID, LED_OFF, led, 0).to_result()
32+
}
33+
34+
pub fn toggle(led: u32) -> Result<(), ErrorCode> {
35+
S::command(DRIVER_ID, LED_TOGGLE, led, 0).to_result()
36+
}
37+
}
38+
39+
const DRIVER_ID: u32 = 2;
40+
41+
// Command IDs
42+
const LEDS_COUNT: u32 = 0;
43+
const LED_ON: u32 = 1;
44+
const LED_OFF: u32 = 2;
45+
const LED_TOGGLE: u32 = 3;
46+
47+
#[cfg(test)]
48+
mod tests;

apis/leds/src/tests.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use libtock_platform::ErrorCode;
2+
use libtock_unittest::fake;
3+
4+
type Leds = super::Leds<fake::Syscalls>;
5+
6+
#[test]
7+
fn no_driver() {
8+
let _kernel = fake::Kernel::new();
9+
assert_eq!(Leds::count(), Err(ErrorCode::NoDevice));
10+
}
11+
12+
#[test]
13+
fn driver_check() {
14+
let kernel = fake::Kernel::new();
15+
let driver = fake::Leds::<10>::new();
16+
kernel.add_driver(&driver);
17+
18+
assert_eq!(Leds::count(), Ok(10));
19+
for led in 0..10 {
20+
assert_eq!(driver.get_led(led), Some(false));
21+
}
22+
}
23+
24+
#[test]
25+
fn num_leds() {
26+
let kernel = fake::Kernel::new();
27+
let driver = fake::Leds::<10>::new();
28+
kernel.add_driver(&driver);
29+
assert_eq!(Leds::count().unwrap_or_default(), 10);
30+
}
31+
32+
#[test]
33+
fn on() {
34+
let kernel = fake::Kernel::new();
35+
let driver = fake::Leds::<10>::new();
36+
kernel.add_driver(&driver);
37+
38+
assert_eq!(Leds::on(0), Ok(()));
39+
assert_eq!(driver.get_led(0), Some(true));
40+
}
41+
42+
#[test]
43+
fn off() {
44+
let kernel = fake::Kernel::new();
45+
let driver = fake::Leds::<10>::new();
46+
kernel.add_driver(&driver);
47+
48+
assert_eq!(Leds::off(0), Ok(()));
49+
assert_eq!(driver.get_led(0), Some(false));
50+
}
51+
52+
#[test]
53+
fn toggle() {
54+
let kernel = fake::Kernel::new();
55+
let driver = fake::Leds::<10>::new();
56+
kernel.add_driver(&driver);
57+
58+
assert_eq!(Leds::toggle(0), Ok(()));
59+
assert_eq!(driver.get_led(0), Some(true));
60+
assert_eq!(Leds::toggle(0), Ok(()));
61+
assert_eq!(driver.get_led(0), Some(false));
62+
}
63+
64+
#[test]
65+
fn on_off() {
66+
let kernel = fake::Kernel::new();
67+
let driver = fake::Leds::<10>::new();
68+
kernel.add_driver(&driver);
69+
70+
assert_eq!(Leds::on(0), Ok(()));
71+
assert_eq!(driver.get_led(0), Some(true));
72+
assert_eq!(Leds::off(0), Ok(()));
73+
assert_eq!(driver.get_led(0), Some(false));
74+
}
75+
76+
#[test]
77+
fn no_led() {
78+
let kernel = fake::Kernel::new();
79+
let driver = fake::Leds::<10>::new();
80+
kernel.add_driver(&driver);
81+
82+
assert_eq!(Leds::on(11), Err(ErrorCode::Invalid));
83+
for led in 0..Leds::count().unwrap_or_default() {
84+
assert_eq!(driver.get_led(led), Some(false));
85+
}
86+
}

0 commit comments

Comments
 (0)