Skip to content

Commit

Permalink
Refactor semihosting print structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyma98 committed Aug 26, 2024
1 parent d5352c4 commit 897677a
Show file tree
Hide file tree
Showing 51 changed files with 400 additions and 209 deletions.
7 changes: 5 additions & 2 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#![no_main]

extern crate alloc;
use hopter::{debug::semihosting, hprintln, task::main};
use hopter::{
debug::semihosting::{self, dbg_println},
task::main,
};

// Attribute `#[main]` marks the function as the entry function for the main
// task. The function name can be arbitrary. The main function should accept
Expand All @@ -11,7 +14,7 @@ use hopter::{debug::semihosting, hprintln, task::main};
fn main(_: cortex_m::Peripherals) {
// Print via semihosting. When using QEMU with semihosting option enabled,
// the characters will appear on the QEMU console.
hprintln!("hello world!");
dbg_println!("hello world!");

// When running with QEMU, this will cause the QEMU process to terminate.
// Do not include this line when running with OpenOCD, because it will
Expand Down
8 changes: 6 additions & 2 deletions examples/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
#![no_main]

extern crate alloc;
use hopter::{debug::semihosting, hprintln, task::main, time};
use hopter::{
debug::semihosting::{self, dbg_println},
task::main,
time,
};

// Attribute `#[main]` marks the function as the entry function for the main
// task. The function name can be arbitrary. The main function should accept
Expand All @@ -14,7 +18,7 @@ fn main(_: cortex_m::Peripherals) {
time::sleep_ms(1000);
// Print via semihosting. When using QEMU with semihosting option enabled,
// the characters will appear on the QEMU console.
hprintln!("hello");
dbg_println!("hello");
}

// When running with QEMU, this will cause the QEMU process to terminate.
Expand Down
8 changes: 6 additions & 2 deletions examples/task_panic_restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

extern crate alloc;
use core::sync::atomic::{AtomicUsize, Ordering};
use hopter::{debug::semihosting, hprintln, schedule, task, task::main};
use hopter::{
debug::semihosting::{self, dbg_println},
schedule, task,
task::main,
};

// Attribute `#[main]` marks the function as the entry function for the main
// task. The function name can be arbitrary. The main function should accept
Expand All @@ -26,7 +30,7 @@ fn will_panic() {
// Every time the task runs we increment it by 1.
let cnt = CNT.fetch_add(1, Ordering::SeqCst);

hprintln!("Current count: {}", cnt);
dbg_println!("Current count: {}", cnt);

// Panic and get restarted for 5 times.
if cnt < 5 {
Expand Down
5 changes: 2 additions & 3 deletions examples/tests/debug/cpu_load/load_40_percent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use alloc::sync::Arc;
use hopter::{
debug::{
cpu_load::{LoadInspector, MicrosecPrecision},
semihosting,
semihosting::{self, dbg_println},
},
hprintln,
interrupt::declare::irq,
sync::SpinIrqSafe,
task,
Expand Down Expand Up @@ -75,7 +74,7 @@ fn print_load() {
time::sleep_ms(500);
if let Some(usage) = LOAD_INSPECTOR.lock().as_ref() {
let (x, y) = usage.get_cpu_load();
hprintln!("CPU load {}.{}%", x, y);
dbg_println!("CPU load {}.{}%", x, y);
}
}
semihosting::terminate(true);
Expand Down
7 changes: 3 additions & 4 deletions examples/tests/interrupt/unwind/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ extern crate alloc;

use core::sync::atomic::{AtomicUsize, Ordering};
use hopter::{
debug::semihosting,
hprintln,
debug::semihosting::{self, dbg_println},
interrupt::declare::{handler, irq},
sync::SpinIrqSafe,
task::main,
Expand Down Expand Up @@ -62,7 +61,7 @@ extern "C" fn tim2_handler() {
panic!();
}

hprintln!("TIM2 IRQ count {}", prev_cnt);
dbg_println!("TIM2 IRQ count {}", prev_cnt);

if prev_cnt >= 5 {
semihosting::terminate(true);
Expand All @@ -73,6 +72,6 @@ struct PrintOnDrop(&'static str);

impl Drop for PrintOnDrop {
fn drop(&mut self) {
hprintln!("{}", self.0);
dbg_println!("{}", self.0);
}
}
12 changes: 9 additions & 3 deletions examples/tests/sync/channel/concurrency_and_stress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ extern crate alloc;
use alloc::vec;
use alloc::vec::Vec;
use core::sync::atomic::{AtomicUsize, Ordering};
use hopter::{debug::semihosting, hprintln, sync, sync::Producer, task, task::main};
use hopter::{
debug::semihosting::{self, dbg_println},
sync,
sync::Producer,
task,
task::main,
};

const NUM_TASKS: usize = 4;
const NUM_ITEMS: usize = 3; // Number of items each task will produce
Expand Down Expand Up @@ -65,10 +71,10 @@ fn main(_: cortex_m::Peripherals) {

// Check if the produced results match the expected sequence
if results != compare_vec {
hprintln!("Test Failed");
dbg_println!("Test Failed");
semihosting::terminate(false);
}
hprintln!("Test Passed");
dbg_println!("Test Passed");
semihosting::terminate(true);
}

Expand Down
16 changes: 11 additions & 5 deletions examples/tests/sync/channel/multiple_consumers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
#![no_std]

extern crate alloc;
use hopter::{debug::semihosting, hprintln, sync, sync::Consumer, task, task::main};
use hopter::{
debug::semihosting::{self, dbg_println},
sync,
sync::Consumer,
task,
task::main,
};

#[main]
fn main(_: cortex_m::Peripherals) {
Expand Down Expand Up @@ -41,14 +47,14 @@ fn main(_: cortex_m::Peripherals) {

// Check if the channel is empty after both consumers have finished
if consumer.try_consume_allow_isr() != None {
hprintln!("Channel not empty");
dbg_println!("Channel not empty");
semihosting::terminate(false);
}
hprintln!("Test Passed");
dbg_println!("Test Passed");
semihosting::terminate(true);
}

fn consume_function(consumer: &mut Consumer<usize, 4>) {
hprintln!("{}", consumer.consume());
hprintln!("{}", consumer.consume());
dbg_println!("{}", consumer.consume());
dbg_println!("{}", consumer.consume());
}
12 changes: 9 additions & 3 deletions examples/tests/sync/channel/multiple_producers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
#![no_std]

extern crate alloc;
use hopter::{debug::semihosting, hprintln, sync, sync::Producer, task, task::main};
use hopter::{
debug::semihosting::{self, dbg_println},
sync,
sync::Producer,
task,
task::main,
};

#[main]
fn main(_: cortex_m::Peripherals) {
Expand Down Expand Up @@ -42,10 +48,10 @@ fn main(_: cortex_m::Peripherals) {

// Verify the consumed values against the expected order
if values != [1, 2, 3, 4] {
hprintln!("Test Failed");
dbg_println!("Test Failed");
semihosting::terminate(false);
}
hprintln!("Test Passed");
dbg_println!("Test Passed");
semihosting::terminate(true);
}

Expand Down
10 changes: 7 additions & 3 deletions examples/tests/sync/channel/produce_consume_single_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
#![no_std]

extern crate alloc;
use hopter::{debug::semihosting, hprintln, sync, task::main};
use hopter::{
debug::semihosting::{self, dbg_println},
sync,
task::main,
};

#[main]
fn main(_: cortex_m::Peripherals) {
Expand All @@ -21,10 +25,10 @@ fn main(_: cortex_m::Peripherals) {
for i in 0..4 {
let value = consumer.consume();
if value != 23 + i {
hprintln!("Test Failed");
dbg_println!("Test Failed");
semihosting::terminate(false);
}
}
hprintln!("Test Passed");
dbg_println!("Test Passed");
semihosting::terminate(true);
}
11 changes: 5 additions & 6 deletions examples/tests/sync/channel/try_consume_from_isr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ extern crate alloc;
use core::sync::atomic::{AtomicUsize, Ordering};
use hopter::{
config,
debug::semihosting,
hprintln,
debug::semihosting::{self, dbg_println},
interrupt::declare::{handler, irq},
sync,
sync::{Consumer, Producer, SpinIrqSafe},
Expand Down Expand Up @@ -90,23 +89,23 @@ extern "C" fn tim2_handler() {
match result {
// The first 5 consume attempt should be successful.
Some(value) => {
hprintln!("Consumed {}", value);
dbg_println!("Consumed {}", value);
if COUNT.load(Ordering::SeqCst) > 5 {
semihosting::terminate(false);
}
}
// The 6th consume attempt should be unsuccessful.
None => {
hprintln!("Failed to consume");
dbg_println!("Failed to consume");
if COUNT.load(Ordering::SeqCst) == 6 {
semihosting::terminate(true);
}
hprintln!("Unexpectedly succeed to consume");
dbg_println!("Unexpectedly succeed to consume");
semihosting::terminate(false);
}
}
} else {
hprintln!("Consumer not initialized!");
dbg_println!("Consumer not initialized!");
semihosting::terminate(false);
}
}
14 changes: 9 additions & 5 deletions examples/tests/sync/channel/try_consume_from_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#![no_std]

extern crate alloc;
use hopter::{debug::semihosting, hprintln, sync, task::main};
use hopter::{
debug::semihosting::{self, dbg_println},
sync,
task::main,
};

#[main]
fn main(_: cortex_m::Peripherals) {
Expand All @@ -15,7 +19,7 @@ fn main(_: cortex_m::Peripherals) {

// Check against expected behavior
if result != None {
hprintln!("consumed from an empty channel");
dbg_println!("consumed from an empty channel");
semihosting::terminate(false);
}

Expand All @@ -29,7 +33,7 @@ fn main(_: cortex_m::Peripherals) {
for _i in 0..4 {
let result = consumer.try_consume_allow_isr();
if result == None {
hprintln!("failed to consume from a non-empty channel");
dbg_println!("failed to consume from a non-empty channel");
semihosting::terminate(false);
}
}
Expand All @@ -38,11 +42,11 @@ fn main(_: cortex_m::Peripherals) {
let final_result = consumer.try_consume_allow_isr();
match final_result {
None => {
hprintln!("Test Passed");
dbg_println!("Test Passed");
semihosting::terminate(true);
}
Some(_t) => {
hprintln!("consumed from an empty channel");
dbg_println!("consumed from an empty channel");
semihosting::terminate(false);
}
}
Expand Down
11 changes: 5 additions & 6 deletions examples/tests/sync/channel/try_produce_from_isr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ extern crate alloc;
use core::sync::atomic::{AtomicUsize, Ordering};
use hopter::{
config,
debug::semihosting,
hprintln,
debug::semihosting::{self, dbg_println},
interrupt::declare::{handler, irq},
sync,
sync::{Consumer, Producer, SpinIrqSafe},
Expand Down Expand Up @@ -74,7 +73,7 @@ fn consume_function(consumer: Consumer<usize, 2>) {
// should be able to hold two more values produced.
for _ in 0..3 {
let val = consumer.consume();
hprintln!("Consumed {}", val);
dbg_println!("Consumed {}", val);
}
}

Expand All @@ -98,16 +97,16 @@ extern "C" fn tim2_handler() {
}
// The 6th produce attempt should be unsuccessful.
Err(_) => {
hprintln!("Failed to produce");
dbg_println!("Failed to produce");
if COUNT.load(Ordering::SeqCst) == 6 {
semihosting::terminate(true);
}
hprintln!("Unexpectedly succeed to produce");
dbg_println!("Unexpectedly succeed to produce");
semihosting::terminate(false);
}
}
} else {
hprintln!("Producer not initialized!");
dbg_println!("Producer not initialized!");
semihosting::terminate(false);
}
}
8 changes: 4 additions & 4 deletions examples/tests/sync/channel/try_produce_from_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![no_std]

extern crate alloc;
use hopter::{debug::semihosting, hprintln, sync, task::main};
use hopter::{debug::semihosting::{self, dbg_println}, sync, task::main};

#[main]
fn main(_: cortex_m::Peripherals) {
Expand All @@ -21,7 +21,7 @@ fn main(_: cortex_m::Peripherals) {
for i in 4..=7 {
let result = producer.try_produce_allow_isr(i);
if result != Err(i) {
hprintln!("Test Failed");
dbg_println!("Test Failed");
semihosting::terminate(false);
}
}
Expand All @@ -30,10 +30,10 @@ fn main(_: cortex_m::Peripherals) {
for i in 0..4 {
let value = consumer.consume();
if value != i {
hprintln!("Test Failed");
dbg_println!("Test Failed");
semihosting::terminate(false);
}
}
hprintln!("Test Passed");
dbg_println!("Test Passed");
semihosting::terminate(true);
}
Loading

0 comments on commit 897677a

Please sign in to comment.