Skip to content

Commit

Permalink
Merge pull request #75 from ArtemKhel/main
Browse files Browse the repository at this point in the history
Preemptive task manager
  • Loading branch information
IvanArkhipov1999 authored Dec 9, 2024
2 parents c98e56f + 387d8c3 commit fedc883
Show file tree
Hide file tree
Showing 34 changed files with 587 additions and 106 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ jobs:
- name: Fmt
run: cd ./examples/rust-examples/xtensa-esp32/wifi && cargo fmt --all -- --check

xtensa-esp32-rust-example-scheduler:
runs-on: ubuntu-latest
env:
CARGO_HOME: /root/.cargo
RUSTUP_HOME: /root/.rustup
container:
image: arkhipovivan1/xtensa-esp32-rust:latest
options: --user root
steps:
- uses: actions/checkout@v3
- name: Build
run: cd ./examples/rust-examples/xtensa-esp32/scheduler && . /root/export-esp.sh && cargo build
- name: Fmt
run: cd ./examples/rust-examples/xtensa-esp32/scheduler && cargo fmt --all -- --check

xtensa-esp32-static-library:
runs-on: ubuntu-latest
env:
Expand Down Expand Up @@ -140,7 +155,7 @@ jobs:
- name: Build
run: cd ./examples/c-examples/xtensa-esp32 && . /root/esp/esp-idf/export.sh && make && esptool.py --chip esp32 elf2image --flash_mode="dio" --flash_freq "40m" --flash_size "4MB" -o main.bin main.elf


risc-v-esp32c6-rust-example-hello-world:
runs-on: ubuntu-latest
env:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ license = "MIT OR Apache-2.0"
[features]
default = []
c-library = []
cooperative = []
preemptive = []
network = ["esp-wifi"]
mips64_timer_tests = []

[dependencies]
cfg-if = "1.0.0"

[target.'cfg(any(target_arch = "riscv32", target_arch = "xtensa"))'.dependencies]
esp-alloc = "0.5.0"
Expand Down
3 changes: 3 additions & 0 deletions examples/c-examples/risc-v-esp32c6/ld/esp32c6.ld
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ SECTIONS
INCLUDE "ld/esp32c6.rom.ld"
PROVIDE (ets_update_cpu_frequency_rom = ets_update_cpu_frequency);

INCLUDE "ld/esp32c6.rom.ld"
PROVIDE (ets_update_cpu_frequency_rom = ets_update_cpu_frequency);

/**
* Appendix: Memory Usage of ROM bootloader
*
Expand Down
2 changes: 1 addition & 1 deletion examples/c-examples/risc-v-esp32c6/ld/esp32c6.rom.ld
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,4 @@ slc_init_credit = 0x40000ae8;
slc_reattach = 0x40000aec;
slc_send_to_host_chain = 0x40000af0;
slc_set_host_io_max_window = 0x40000af4;
slc_to_host_chain_recycle = 0x40000af8;
slc_to_host_chain_recycle = 0x40000af8;
6 changes: 4 additions & 2 deletions examples/rust-examples/mips64/dynamic-memory/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ extern crate alloc;

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

/// Counter to work with in loop.
static COUNTER: AtomicU32 = AtomicU32::new(1);
Expand Down
6 changes: 4 additions & 2 deletions examples/rust-examples/mips64/hello-world/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#![no_main]

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

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
Expand Down
8 changes: 5 additions & 3 deletions examples/rust-examples/mips64/timer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ extern crate alloc;

use alloc::vec::Vec;
use core::sync::atomic::{AtomicU32, Ordering};
use martos::init_system;
use martos::task_manager::TaskManager;
use martos::timer::Timer;
use martos::{
init_system,
task_manager::{TaskManager, TaskManagerTrait},
timer::Timer,
};

/// Counter to work with in loop.
static COUNTER: AtomicU32 = AtomicU32::new(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use core::sync::atomic::{AtomicU32, Ordering};
use esp_backtrace as _;
use esp_hal::entry;
use esp_println::println;
use martos::init_system;
use martos::task_manager::TaskManager;
use martos::{
init_system,
task_manager::{TaskManager, TaskManagerTrait},
};

/// Counter to work with in loop.
static COUNTER: AtomicU32 = AtomicU32::new(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use core::sync::atomic::{AtomicU32, Ordering};
use esp_backtrace as _;
use esp_hal::entry;
use esp_println::println;
use martos::init_system;
use martos::task_manager::TaskManager;
use martos::{
init_system,
task_manager::{TaskManager, TaskManagerTrait},
};

/// Counter to work with in loop.
static COUNTER: AtomicU32 = AtomicU32::new(1);
Expand Down
8 changes: 5 additions & 3 deletions examples/rust-examples/risc-v-esp32-c6/timer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ use core::sync::atomic::{AtomicU32, Ordering};
use esp_backtrace as _;
use esp_hal::entry;
use esp_println::println;
use martos::init_system;
use martos::task_manager::TaskManager;
use martos::timer::Timer;
use martos::{
init_system,
task_manager::{TaskManager, TaskManagerTrait},
timer::Timer,
};

/// Counter to work with in loop.
static COUNTER: AtomicU32 = AtomicU32::new(1);
Expand Down
2 changes: 1 addition & 1 deletion examples/rust-examples/risc-v-esp32-c6/wifi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use esp_println::println;
use esp_wifi::esp_now::{EspNow, PeerInfo, BROADCAST_ADDRESS};
use martos::get_esp_now;
use martos::init_system;
use martos::task_manager::TaskManager;
use martos::task_manager::{TaskManager, TaskManagerTrait};

/// Esp-now object for network
static mut ESP_NOW: Option<EspNow> = None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use core::sync::atomic::{AtomicU32, Ordering};
use esp_backtrace as _;
use esp_hal::entry;
use esp_println::println;
use martos::init_system;
use martos::task_manager::TaskManager;
use martos::{
init_system,
task_manager::{TaskManager, TaskManagerTrait},
};

/// Counter to work with in loop.
static COUNTER: AtomicU32 = AtomicU32::new(1);
Expand Down
6 changes: 4 additions & 2 deletions examples/rust-examples/xtensa-esp32/hello-world/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use core::sync::atomic::{AtomicU32, Ordering};
use esp_backtrace as _;
use esp_hal::entry;
use esp_println::println;
use martos::init_system;
use martos::task_manager::TaskManager;
use martos::{
init_system,
task_manager::{TaskManager, TaskManagerTrait},
};

/// Counter to work with in loop.
static COUNTER: AtomicU32 = AtomicU32::new(1);
Expand Down
15 changes: 15 additions & 0 deletions examples/rust-examples/xtensa-esp32/scheduler/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[build]
rustflags = [
"-C", "link-arg=-Tlinkall.x",

"-C", "link-arg=-nostartfiles",
]

target = "xtensa-esp32-none-elf"

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

[target.'cfg(any(target_arch = "riscv32", target_arch = "xtensa"))']
runner = "espflash flash --monitor"

19 changes: 19 additions & 0 deletions examples/rust-examples/xtensa-esp32/scheduler/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "example_xtensa_esp32_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 = ["preemptive"]}
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"]
36 changes: 36 additions & 0 deletions examples/rust-examples/xtensa-esp32/scheduler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Rust example for xtensa esp32 architecture

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

Two tasks are created. They take turns increasing the counter value and printing it out until it reaches 20. Periodically, one preempts the other

## 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
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "esp"
50 changes: 50 additions & 0 deletions examples/rust-examples/xtensa-esp32/scheduler/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#![no_std]
#![no_main]

use core::sync::atomic::{AtomicU32, Ordering};
use esp_backtrace as _;
use esp_hal::entry;
use esp_hal::xtensa_lx_rt::xtensa_lx::timer::delay;
use esp_println::println;
use martos::{
init_system,
task_manager::{TaskManager, TaskManagerTrait},
};

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

/// Loop function for task to execute.
fn loop_fn_1() {
let old = COUNTER.fetch_add(1, Ordering::Relaxed);
println!("Loop 0; Counter = {}", old);
delay(10_000_000);
}

fn loop_fn_2() {
let old = COUNTER.fetch_add(1, Ordering::Relaxed);
println!("Loop 1; Counter = {}", old);
delay(10_000_000);
}

fn setup() {
println!("Setup")
}
fn stop() -> bool {
if COUNTER.fetch_add(0, Ordering::Relaxed) > 20 {
true
} else {
false
}
}

#[entry]
fn main() -> ! {
// Initialize Martos.
init_system();
// Add task to execute.
TaskManager::add_task(setup, loop_fn_1, stop);
TaskManager::add_task(setup, loop_fn_2, stop);
// Start task manager.
TaskManager::start_task_manager();
}
8 changes: 5 additions & 3 deletions examples/rust-examples/xtensa-esp32/timer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ use core::sync::atomic::{AtomicU32, Ordering};
use esp_backtrace as _;
use esp_hal::entry;
use esp_println::println;
use martos::init_system;
use martos::task_manager::TaskManager;
use martos::timer::Timer;
use martos::{
init_system,
task_manager::{TaskManager, TaskManagerTrait},
timer::Timer,
};

/// Counter to work with in loop.
static COUNTER: AtomicU32 = AtomicU32::new(1);
Expand Down
6 changes: 4 additions & 2 deletions examples/rust-examples/xtensa-esp32/wifi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use esp_hal::{entry, time};
use esp_println::println;
use esp_wifi::esp_now::{EspNow, PeerInfo, BROADCAST_ADDRESS};
use martos::get_esp_now;
use martos::init_system;
use martos::task_manager::TaskManager;
use martos::{
init_system,
task_manager::{TaskManager, TaskManagerTrait},
};

/// Esp-now object for network
static mut ESP_NOW: Option<EspNow> = None;
Expand Down
2 changes: 1 addition & 1 deletion src/c_api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{task_manager, timer};
use core::time::Duration;
use task_manager::TaskManager;
use task_manager::{TaskManager, TaskManagerTrait};
use timer::Timer;

/// The structure represents duration in seconds and microseconds.
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![no_std]
extern crate alloc;

mod ports;
use ports::PortTrait;
Expand Down
Loading

0 comments on commit fedc883

Please sign in to comment.