Skip to content

Commit cd77dee

Browse files
authored
Try #164:
2 parents 4d27d24 + 9d9edc9 commit cd77dee

27 files changed

+137
-164
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Cargo.lock
22
layout.ld
3+
platform
34
target

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ license = "MIT/Apache-2.0"
66
edition = "2018"
77

88
[features]
9-
alloc = [ "linked_list_allocator" ]
9+
alloc = ["libtock-core/alloc"]
10+
custom_panic_handler = ["libtock-core/custom_panic_handler"]
11+
custom_alloc_error_handler = ["libtock-core/custom_alloc_error_handler"]
1012

1113
[dependencies]
1214
core = { package = "async-support", path = "async-support" }
15+
libtock-core = { path = "core" }
1316
libtock_codegen = { path = "codegen" }
14-
linked_list_allocator = { optional = true, version = "=0.6.5", default-features = false }
1517
futures = { version = "0.3.1", default-features = false, features = ["unstable", "cfg-target-has-atomic"] }
1618

1719
[dev-dependencies]
@@ -52,4 +54,5 @@ lto = true
5254
members = [
5355
"async-support",
5456
"codegen",
57+
"core"
5558
]

core/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "libtock-core"
3+
version = "0.1.0"
4+
authors = ["torfmaster <[email protected]>", "Woyten <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[features]
10+
alloc = [ "linked_list_allocator" ]
11+
custom_panic_handler = []
12+
custom_alloc_error_handler = []
13+
14+
[dependencies]
15+
linked_list_allocator = { optional = true, version = "=0.6.5", default-features = false }

core/src/alloc.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use core::alloc::GlobalAlloc;
2+
use core::alloc::Layout;
3+
use core::ptr;
4+
use core::ptr::NonNull;
5+
use linked_list_allocator::Heap;
6+
7+
pub static mut HEAP: Heap = Heap::empty();
8+
9+
struct TockAllocator;
10+
11+
unsafe impl GlobalAlloc for TockAllocator {
12+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
13+
HEAP.allocate_first_fit(layout)
14+
.ok()
15+
.map_or(ptr::null_mut(), NonNull::as_ptr)
16+
}
17+
18+
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
19+
HEAP.deallocate(NonNull::new_unchecked(ptr), layout)
20+
}
21+
}
22+
23+
#[global_allocator]
24+
static ALLOCATOR: TockAllocator = TockAllocator;
25+
26+
#[cfg(not(feature = "custom_alloc_error_handler"))]
27+
#[alloc_error_handler]
28+
unsafe fn alloc_error_handler(_: Layout) -> ! {
29+
use crate::syscalls;
30+
31+
// Print 0x01 using the LowLevelDebug capsule (if available).
32+
let _ = syscalls::command1_insecure(8, 2, 0x01);
33+
34+
// Signal a panic using the LowLevelDebug capsule (if available).
35+
let _ = syscalls::command1_insecure(8, 1, 0x01);
36+
37+
loop {
38+
syscalls::raw::yieldk();
39+
}
40+
}
File renamed without changes.

core/src/debug/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[cfg_attr(target_arch = "arm", path = "platform_arm.rs")]
2+
#[cfg_attr(target_arch = "riscv32", path = "platform_riscv32.rs")]
3+
mod platform;
4+
5+
pub use platform::*;

core/src/debug/platform.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn get_stack_pointer() -> usize {
2+
panic!("Not implemented yet")
3+
}

core/src/debug/platform_arm.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub fn get_stack_pointer() -> usize {
2+
let stack_pointer;
3+
unsafe { asm!("mov $0, sp" : "=r"(stack_pointer) : : : "volatile") };
4+
stack_pointer
5+
}

core/src/debug/platform_riscv32.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub fn get_stack_pointer() -> usize {
2+
let stack_pointer;
3+
unsafe { asm!("mv $0, sp" : "=r"(stack_pointer) : : : "volatile") };
4+
stack_pointer
5+
}

src/entry_point/mod.rs renamed to core/src/entry_point/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ use core::ptr;
5757

5858
#[cfg_attr(target_arch = "riscv32", path = "start_item_riscv32.rs")]
5959
#[cfg_attr(target_arch = "arm", path = "start_item_arm.rs")]
60-
#[cfg_attr(
61-
not(any(target_arch = "arm", target_arch = "riscv32")),
62-
path = "start_item_mock.rs"
63-
)]
6460
mod start_item;
6561

6662
/// The header encoded at the beginning of .text by the linker script. It is

src/lang_items.rs renamed to core/src/lang_items.rs

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,7 @@
1818
//! `rustc_main`. That's covered by the `_start` function in the root of this
1919
//! crate.
2020
21-
use crate::drivers;
22-
use crate::leds::LedsDriver;
23-
use crate::result::TockResult;
24-
use crate::timer::Duration;
25-
use crate::timer::ParallelSleepDriver;
26-
use core::executor;
27-
use core::panic::PanicInfo;
28-
use futures::future;
21+
use crate::syscalls;
2922

3023
#[lang = "start"]
3124
extern "C" fn start<T>(main: fn() -> T, _argc: isize, _argv: *const *const u8) -> bool
@@ -45,52 +38,25 @@ impl Termination for () {
4538
fn check_result(self) {}
4639
}
4740

48-
impl Termination for TockResult<()> {
41+
impl<S, T> Termination for Result<S, T> {
4942
fn check_result(self) {
5043
if self.is_err() {
5144
unsafe { report_panic() };
5245
}
5346
}
5447
}
5548

49+
#[cfg(not(feature = "custom_panic_handler"))]
5650
#[panic_handler]
57-
unsafe fn panic_handler(_info: &PanicInfo) -> ! {
51+
unsafe fn panic_handler(_info: &core::panic::PanicInfo) -> ! {
5852
report_panic()
5953
}
6054

6155
unsafe fn report_panic() -> ! {
6256
// Signal a panic using the LowLevelDebug capsule (if available).
63-
super::debug::low_level_status_code(1);
57+
let _ = syscalls::command1_insecure(8, 1, 1);
6458

65-
// Flash all LEDs (if available).
66-
executor::block_on(async {
67-
let mut drivers = drivers::retrieve_drivers_unsafe();
68-
69-
let leds_driver = drivers.leds.init_driver();
70-
let mut timer_driver = drivers.timer.create_timer_driver();
71-
let timer_driver = timer_driver.activate();
72-
73-
if let (Ok(leds_driver), Ok(timer_driver)) = (leds_driver, timer_driver) {
74-
let _ = blink_all_leds(&leds_driver, &timer_driver).await;
75-
} else {
76-
future::pending::<()>().await
77-
}
78-
loop {}
79-
})
80-
}
81-
82-
async fn blink_all_leds(
83-
leds_driver: &LedsDriver<'_>,
84-
timer_driver: &ParallelSleepDriver<'_>,
85-
) -> TockResult<()> {
8659
loop {
87-
for led in leds_driver.leds() {
88-
led.on()?;
89-
}
90-
timer_driver.sleep(Duration::from_ms(100)).await?;
91-
for led in leds_driver.leds() {
92-
led.off()?;
93-
}
94-
timer_driver.sleep(Duration::from_ms(100)).await?;
60+
syscalls::raw::yieldk();
9561
}
9662
}

core/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(asm, lang_items, naked_functions)]
2+
#![cfg_attr(any(target_arch = "arm", target_arch = "riscv32"), no_std)]
3+
#![cfg_attr(feature = "alloc", feature(alloc_error_handler))]
4+
5+
#[cfg(feature = "alloc")]
6+
mod alloc;
7+
mod entry_point;
8+
#[cfg(any(target_arch = "arm", target_arch = "riscv32"))]
9+
mod lang_items;
10+
11+
pub mod callback;
12+
pub mod debug;
13+
pub mod memop;
14+
pub mod result;
15+
pub mod shared_memory;
16+
pub mod syscalls;
17+
pub mod unwind_symbols;
File renamed without changes.

core/src/result.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#[derive(Copy, Clone)]
2+
pub struct SubscribeError {
3+
pub driver_number: usize,
4+
pub subscribe_number: usize,
5+
pub return_code: isize,
6+
}
7+
8+
#[derive(Copy, Clone)]
9+
pub struct CommandError {
10+
pub driver_number: usize,
11+
pub command_number: usize,
12+
pub arg1: usize,
13+
pub arg2: usize,
14+
pub return_code: isize,
15+
}
16+
17+
#[derive(Copy, Clone)]
18+
pub struct AllowError {
19+
pub driver_number: usize,
20+
pub allow_number: usize,
21+
pub return_code: isize,
22+
}
23+
24+
pub const SUCCESS: isize = 0;
25+
pub const FAIL: isize = -1;
26+
pub const EBUSY: isize = -2;
27+
pub const EALREADY: isize = -3;
28+
pub const EINVAL: isize = -6;
29+
pub const ESIZE: isize = -7;
30+
pub const ENOMEM: isize = -9;
File renamed without changes.

src/syscalls/mod.rs renamed to core/src/syscalls/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#[cfg_attr(target_arch = "riscv32", path = "platform_riscv32.rs")]
22
#[cfg_attr(target_arch = "arm", path = "platform_arm.rs")]
3-
#[cfg_attr(
4-
not(any(target_arch = "arm", target_arch = "riscv32")),
5-
path = "platform_mock.rs"
6-
)]
73
mod platform;
84

95
use crate::callback::CallbackSubscription;
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/alloc.rs

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/debug/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
mod low_level_debug;
44

55
use crate::drivers;
6+
use libtock_core::debug as core_debug;
67

78
pub use low_level_debug::*;
89

@@ -21,22 +22,15 @@ pub fn print_as_hex(value: usize) {
2122
let _ = console.write(buffer);
2223
}
2324

24-
#[cfg(target_arch = "arm")]
2525
pub fn print_stack_pointer() {
26-
let stack_pointer;
27-
unsafe { asm!("mov $0, sp" : "=r"(stack_pointer) : : : "volatile") };
28-
2926
let mut buffer = [b'\n'; 15];
3027
buffer[0..4].clone_from_slice(b"SP: ");
31-
write_as_hex(&mut buffer[4..15], stack_pointer);
28+
write_as_hex(&mut buffer[4..15], core_debug::get_stack_pointer());
3229
let drivers = unsafe { drivers::retrieve_drivers_unsafe() };
3330
let mut console = drivers.console.create_console();
3431
let _ = console.write(buffer);
3532
}
3633

37-
#[cfg(target_arch = "riscv32")]
38-
pub fn print_stack_pointer() {}
39-
4034
#[inline(always)]
4135
/// Dumps address
4236
/// # Safety

src/lib.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
1-
#![feature(asm, lang_items, naked_functions)]
2-
#![cfg_attr(feature = "alloc", feature(alloc_error_handler))]
31
#![cfg_attr(any(target_arch = "arm", target_arch = "riscv32"), no_std)]
42

5-
#[cfg(feature = "alloc")]
6-
mod alloc;
7-
mod entry_point;
8-
#[cfg(any(target_arch = "arm", target_arch = "riscv32"))]
9-
mod lang_items;
10-
113
pub mod adc;
124
pub mod ble_composer;
135
pub mod ble_parser;
146
pub mod buttons;
15-
pub mod callback;
167
pub mod console;
178
pub mod debug;
189
pub mod drivers;
1910
pub mod electronics;
2011
pub mod futures;
2112
pub mod gpio;
2213
pub mod leds;
23-
pub mod memop;
2414
pub mod result;
2515
pub mod rng;
2616
pub mod sensors;
27-
pub mod shared_memory;
2817
pub mod simple_ble;
29-
pub mod syscalls;
3018
pub mod temperature;
3119
pub mod timer;
32-
pub mod unwind_symbols;
3320

3421
pub use drivers::retrieve_drivers;
3522
pub use libtock_codegen::main;
23+
pub use libtock_core::*;

0 commit comments

Comments
 (0)