Skip to content

Commit

Permalink
Add examples for coop sched
Browse files Browse the repository at this point in the history
  • Loading branch information
vicitori committed Dec 24, 2024
1 parent b7390b9 commit e37ceb5
Show file tree
Hide file tree
Showing 21 changed files with 442 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[build]
rustflags = [
"-C", "link-arg=-Ttext=0x80010000",
"-C", "link-arg=-emain",
]

target = "mips64el-unknown-linux-gnuabi64"

[unstable]
build-std = ["core", "alloc"]

[target.mips64el-unknown-linux-gnuabi64]
linker = "lld"
14 changes: 14 additions & 0 deletions examples/rust-examples/mips64/scheduler/cooperative/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "example_mips64_cooperative_scheduler"
version = "0.4.0"
edition = "2021"

[profile.release]
panic = "abort"
debug = true

[dependencies]
# Specifying Martos version
#martos = "0.4.0"
# Specifying current Martos version path for ci
martos = { path = "../../../../../", features = ["cooperative"] }
38 changes: 38 additions & 0 deletions examples/rust-examples/mips64/scheduler/cooperative/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Rust example for mips64 architecture

Presented here is a straightforward Rust example utilizing Martos with a cooperative scheduler.

The program begins with a main task that increments a shared counter on each iteration.
Once the counter reaches the value of 25, the main task dynamically adds an inner task to the task manager.
The inner task also increments the shared counter on each iteration and stops execution when the counter becomes
divisible by 10. This setup showcases Martos' flexibility in managing tasks, including adding new tasks dynamically
during execution.

## How to install dependencies

Below is an illustrative example demonstrating the installation of building toolchains on a Linux (Ubuntu/Debian):

```
apt update && apt install curl build-essential lld
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
rustup toolchain install nightly
rustup default 1.71
rustup target add mips64el-unknown-linux-gnuabi64
rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
```

## How to build the example

Below, you will find an illustrative example showcasing the building process on a Linux system (Ubuntu/Debian):

```
cargo +nightly build --release
```

## How to run the example

Below, you will find an illustrative example showcasing the running on a Linux system (Ubuntu/Debian):

```
cargo +nightly run
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"
63 changes: 63 additions & 0 deletions examples/rust-examples/mips64/scheduler/cooperative/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#![no_std]
#![no_main]

use core::sync::atomic::{AtomicU32, Ordering};
use martos::{
init_system,
task_manager::{TaskManager, TaskManagerTrait},
};

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

static COUNTER: AtomicU32 = AtomicU32::new(1);

/// Setup function for the main task.
fn main_task_setup_fn() {}

/// Loop function for the main task.
fn main_task_loop_fn() {
let count = COUNTER.fetch_add(1, Ordering::Relaxed);
if count == 25 {
TaskManager::add_task(
inner_task_setup_fn,
inner_task_loop_fn,
inner_task_stop_condition_fn,
);
}
}

/// Stop condition for the main task.
fn main_task_stop_condition_fn() -> bool {
false
}

/// Setup function for the inner task.
fn inner_task_setup_fn() {}

/// Loop function for the inner task.
fn inner_task_loop_fn() {
COUNTER.fetch_add(1, Ordering::Relaxed);
}

/// Stop condition for the inner task.
fn inner_task_stop_condition_fn() -> bool {
let value = unsafe { COUNTER.as_ptr().read() };
value % 10 == 0
}

#[no_mangle]
pub extern "C" fn __start() -> ! {
// Initialize Martos.
init_system();
// Add task to execute.
TaskManager::add_task(
main_task_setup_fn,
main_task_loop_fn,
main_task_stop_condition_fn,
);
// Start task manager.
TaskManager::start_task_manager();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "example_risc_v_esp32c6_scheduler"
name = "example_risc_v_esp32c6_cooperative_scheduler"
version = "0.3.0"
edition = "2021"

Expand All @@ -10,7 +10,7 @@ debug = true
# Specifying Martos version
#martos = "0.3.0"
# Specifying current Martos version path for ci
martos = { path = "../../../../", features = ["preemptive"]}
martos = { path = "../../../../../", features = ["cooperative"] }
esp-hal = "0.21.1"
esp-backtrace = { version = "0.14.1", features = ["esp32c6", "panic-handler", "exception-handler", "println"] }
esp-println = { version = "0.11.0", features = ["esp32c6"] }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Rust example for risc-v esp32c6 architecture

Presented here is a straightforward Rust example utilizing Martos with a cooperative scheduler.

The example begins with a main task that handles a counter, incrementing it by 1 and printing the updated value.
An inner task is dynamically added once the counter reaches 25, and it increments the counter by 1. The inner task
periodically terminates itself when the counter is a multiple of 10, showcasing dynamic task creation and cooperative
task switching.

## How to install dependencies

For comprehensive guidance on installing the necessary dependencies for developing applications targeting the RISC-V
ESP32-C6 architecture,
please refer to [the official website](https://docs.esp-rs.org/book/installation/riscv-and-xtensa.html).
Below is an illustrative example demonstrating the installation of building toolchains on a Linux (Ubuntu/Debian):

```
apt-get -qq update
apt-get install -y -q build-essential curl
curl https://sh.rustup.rs -sSf | sh -s -- -y
cargo install espup
espup install
```

## How to build the example

For a thorough guide on developing projects for the RISC-V ESP32-C6 architecture across various operating systems,
we recommend
consulting [the official website](https://docs.esp-rs.org/book/installation/riscv-and-xtensa.html#3-set-up-the-environment-variables).
Below, you will find an illustrative example showcasing the building process on a Linux system (Ubuntu/Debian):

```
. $HOME/export-esp.sh
cargo build
```

## How to run the example

For detailed instructions on running projects for the RISC-V ESP32-C6 architecture across various operating systems,
we recommend consulting [the official website](https://docs.esp-rs.org/book/tooling/espflash.html).
Below, you will find an illustrative example showcasing the running on a Linux system (Ubuntu/Debian):

```
cargo run
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#![no_std]
#![no_main]

use core::sync::atomic::{AtomicU32, Ordering};
use esp_hal::entry;
use esp_println::println;
use martos::{
init_system,
task_manager::{TaskManager, TaskManagerTrait},
};

/// Counter to work with in loop.
static COUNTER: AtomicU32 = AtomicU32::new(1);

/// Setup function for the main task.
fn main_task_setup_fn() {
println!("Main task setup.\n");
}

/// Loop function for the main task.
fn main_task_loop_fn() {
let count = COUNTER.fetch_add(1, Ordering::Relaxed);
println!("Main task loop: Counter = {}.\n", unsafe {
COUNTER.as_ptr().read()
});
if count == 25 {
TaskManager::add_task(
inner_task_setup_fn,
inner_task_loop_fn,
inner_task_stop_condition_fn,
);
}
}

/// Stop condition for the main task.
fn main_task_stop_condition_fn() -> bool {
false
}

/// Setup function for the inner task.
fn inner_task_setup_fn() {
println!("Inner task setup.\n");
}

/// Loop function for the inner task.
fn inner_task_loop_fn() {
COUNTER.fetch_add(1, Ordering::Relaxed);
println!("Inner task loop: Counter = {}.\n", unsafe {
COUNTER.as_ptr().read()
});
}

/// Stop condition for the inner task.
fn inner_task_stop_condition_fn() -> bool {
let value = unsafe { COUNTER.as_ptr().read() };
value % 10 == 0
}

#[entry]
fn main() -> ! {
// Initialize Martos.
init_system();
// Add task to execute.
TaskManager::add_task(
main_task_setup_fn,
main_task_loop_fn,
main_task_stop_condition_fn,
);
// Start task manager.
TaskManager::start_task_manager();
}
49 changes: 0 additions & 49 deletions examples/rust-examples/risc-v-esp32-c6/scheduler/src/main.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "example_xtensa_esp32_cooperative_scheduler"
version = "0.3.0"
edition = "2021"

[profile.release]
debug = true

[dependencies]
# Specifying Martos version
#martos = "0.3.0"
# Specifying current Martos version path for ci
martos = { path = "../../../../../", features = ["cooperative"] }
esp-hal = "0.21.1"
esp-backtrace = { version = "0.14.1", features = ["esp32", "panic-handler", "exception-handler", "println"] }
esp-println = { version = "0.11.0", features = ["esp32"] }

[features]
default = ["esp-hal/esp32", "esp-backtrace/esp32", "esp-println/esp32"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Rust example for xtensa esp32 architecture

Presented here is a straightforward Rust example utilizing Martos with a cooperative scheduler.

The example begins with a main task that handles a counter, incrementing it by 1 and printing the updated value.
An inner task is dynamically added once the counter reaches 25, and it increments the counter by 1. The inner task
periodically terminates itself when the counter is a multiple of 10, showcasing dynamic task creation and cooperative
task switching.

## How to install dependencies

For comprehensive guidance on installing the necessary dependencies for developing applications targeting the Xtensa
ESP32 architecture,
please refer to [the official website](https://docs.esp-rs.org/book/installation/riscv-and-xtensa.html).
Below is an illustrative example demonstrating the installation of building toolchains on a Linux (Ubuntu/Debian):

```
apt-get -qq update
apt-get install -y -q build-essential curl
curl https://sh.rustup.rs -sSf | sh -s -- -y
cargo install espup
espup install
```

## How to build the example

For a thorough guide on developing projects for the Xtensa ESP32 architecture across various operating systems,
we recommend
consulting [the official website](https://docs.esp-rs.org/book/installation/riscv-and-xtensa.html#3-set-up-the-environment-variables).
Below, you will find an illustrative example showcasing the building process on a Linux system (Ubuntu/Debian):

```
. $HOME/export-esp.sh
cargo build --release
```

## How to run the example

For detailed instructions on running projects for the Xtensa ESP32 architecture across various operating systems,
we recommend consulting [the official website](https://docs.esp-rs.org/book/tooling/espflash.html).
Below, you will find an illustrative example showcasing the running on a Linux system (Ubuntu/Debian):

```
cargo run
```
Loading

0 comments on commit e37ceb5

Please sign in to comment.