Skip to content

Commit

Permalink
Merge pull request #1 from IvanArkhipov1999/task-manager
Browse files Browse the repository at this point in the history
Task manager
  • Loading branch information
IvanArkhipov1999 authored Mar 9, 2024
2 parents 7da8110 + f8e888c commit 45911cb
Show file tree
Hide file tree
Showing 23 changed files with 665 additions and 273 deletions.
44 changes: 41 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
#on:
# - push

env:
CARGO_TERM_COLOR: always
Expand All @@ -13,9 +15,9 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- uses: actions/checkout@v3
- name: Build --lib
run: cargo build --verbose

test:
runs-on: ubuntu-latest
Expand All @@ -30,3 +32,39 @@ jobs:
- uses: actions/checkout@v3
- name: Fmt
run: cargo fmt --all -- --check

# TODO: change to cargo build --example
xtensa-esp32-rust-example:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# TODO: to avoid setupping, ci should pass on prepared dockers on self-hosted runners
- name: Setup build environment
run: cargo install espup && espup install
- name: Build
run: cd ./examples/rust-examples/xtensa-esp32 && . $HOME/export-esp.sh && cargo build
- name: Fmt
run: cd ./examples/rust-examples/xtensa-esp32 && cargo fmt --all -- --check

xtensa-esp32-static-library:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# TODO: to avoid setupping, ci should pass on prepared dockers on self-hosted runners
- name: Setup build environment
run: cargo install espup && espup install
- name: Build
run: cd ./c-library/xtensa-esp32 && . $HOME/export-esp.sh && cargo build
- name: Fmt
run: cd ./c-library/xtensa-esp32 && cargo fmt --all -- --check

xtensa-esp32-c-example:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# TODO: to avoid setupping, ci should pass on prepared dockers on self-hosted runners
- name: Setup build environment
run: cargo install espup && espup install && sudo apt-get install -y git wget flex bison gperf python3 python3-pip python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0 && mkdir -p ~/esp && cd ~/esp && git clone -b v5.2 --recursive https://github.com/espressif/esp-idf.git && cd ~/esp/esp-idf && ./install.sh esp32
# TODO: to avoid building static library, this job should use artifacts from xtensa-esp32-static-library job
- name: Build
run: cd ./c-library/xtensa-esp32 && . $HOME/export-esp.sh && cargo build && cd ../../examples/c-examples/xtensa-esp32 && . $HOME/esp/esp-idf/export.sh && idf.py build
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ Cargo.lock
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
*.pdb

# Espessif generated files and directories
sdkconfig
build/
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ name = "ma_rtos"
version = "0.1.0"
edition = "2021"

[dependencies]
lazy_static = "1.4"
[features]
default = []
c-library = []
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
# MA-RTOS
# Martos

Martos is a simple RTOS for developing multi-agent real-time systems.
Software for Martos can be written on both Rust (recommended) and C languages.

In current version it has only primitive task manager and timer counter.

## Programming on Rust
For writing software on Rust you can use Martos as dependency:
```
[dependencies]
ma_rtos = ...
```

Rust examples for different architecures you can see in examples/rust-examples.

## Programming on C
For writing software on C you can link your project with Martos static library.
You can get Martos static library from release artifacts.
If you want to build Martos static library yourself, see c-library directory.
It contains static library targets for different architectures.

C examples for different architecures you can see in examples/c-examples.
19 changes: 19 additions & 0 deletions c-library/xtensa-esp32/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[target.xtensa-esp32-none-elf]
runner = "espflash flash --monitor"


[build]
rustflags = [
"-C", "link-arg=-Tlinkall.x",

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

target = "xtensa-esp32-none-elf"

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

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

21 changes: 21 additions & 0 deletions c-library/xtensa-esp32/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "xtensa_esp32_static_lib"
version = "0.1.0"
edition = "2021"

[lib]
name = "xtensa_esp32_static_lib"
crate-type = ["staticlib"]

[profile.release]
debug = true

[dependencies]
# TODO: path should be from git, then from crates.io
ma_rtos = { path = "../../", features = ["c-library"] }
esp32-hal = "0.18.0"
esp-backtrace = { version = "0.11.0", features = ["esp32", "panic-handler", "exception-handler", "println"] }
esp-println = { version = "0.9.0", features = ["esp32"] }

[features]
default = ["esp32-hal/xtal-40mhz"]
2 changes: 2 additions & 0 deletions c-library/xtensa-esp32/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "esp"
36 changes: 36 additions & 0 deletions c-library/xtensa-esp32/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// TODO: maybe all this should be in martos, not in c-library folder

#![no_std]

use ma_rtos::task_manager::TaskExecutor;
use ma_rtos::timer::{TickType, Timer};

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

#[no_mangle]
pub extern "C" fn setup_timer() {
Timer::setup_timer()
}

#[no_mangle]
pub extern "C" fn loop_timer() {
Timer::loop_timer()
}

#[no_mangle]
pub extern "C" fn stop_condition_timer() -> bool {
Timer::stop_condition_timer()
}

#[no_mangle]
pub extern "C" fn get_tick_counter() -> TickType {
Timer::get_tick_counter()
}

#[no_mangle]
pub extern "C" fn new_task_executor() -> TaskExecutor {
TaskExecutor::new()
}
6 changes: 6 additions & 0 deletions examples/c-examples/xtensa-esp32/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(hello_world)
1 change: 1 addition & 0 deletions examples/c-examples/xtensa-esp32/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

7 changes: 7 additions & 0 deletions examples/c-examples/xtensa-esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
idf_component_register(SRCS "hello_world_main.c"
INCLUDE_DIRS "")

# Linking with martos static library. Set here path to it in your project.
# Here path is so for ci.
# TODO: should be more beautiful linking
target_link_libraries(${COMPONENT_LIB} ${CMAKE_CURRENT_LIST_DIR}/../../../../c-library/xtensa-esp32/target/xtensa-esp32-none-elf/debug/libxtensa_esp32_static_lib.a -Wl,--allow-multiple-definition)
11 changes: 11 additions & 0 deletions examples/c-examples/xtensa-esp32/main/hello_world_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>

extern long get_tick_counter();

void app_main(void)
{
long tick = get_tick_counter();
printf("tick in c = %ld\n", tick);

for (;;) {}
}
19 changes: 19 additions & 0 deletions examples/rust-examples/xtensa-esp32/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[target.xtensa-esp32-none-elf]
runner = "espflash flash --monitor"


[build]
rustflags = [
"-C", "link-arg=-Tlinkall.x",

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

target = "xtensa-esp32-none-elf"

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

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

17 changes: 17 additions & 0 deletions examples/rust-examples/xtensa-esp32/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "example_xtensa_esp32"
version = "0.1.0"
edition = "2021"

[profile.release]
debug = true

[dependencies]
# TODO: path should be from git, then from crates.io
ma_rtos = { path = "../../../" }
esp32-hal = "0.18.0"
esp-backtrace = { version = "0.11.0", features = ["esp32", "panic-handler", "exception-handler", "println"] }
esp-println = { version = "0.9.0", features = ["esp32"] }

[features]
default = ["esp32-hal/xtal-40mhz"]
2 changes: 2 additions & 0 deletions examples/rust-examples/xtensa-esp32/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "esp"
42 changes: 42 additions & 0 deletions examples/rust-examples/xtensa-esp32/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![no_std]
#![no_main]

use core::sync::atomic::{AtomicU32, Ordering};
use esp32_hal::entry;
use esp_backtrace as _;
use esp_println::println;
use ma_rtos::task_manager;

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

/// Setup function for task to execute.
fn setup_fn() {
println!("Setup hello world!")
}

/// Loop function for task to execute.
fn loop_fn() {
COUNTER.fetch_add(1, Ordering::Relaxed);
println!("Loop hello world!");
println!("Counter = {}", unsafe { COUNTER.as_ptr().read() });
}

/// Stop condition function for task to execute.
fn stop_condition_fn() -> bool {
let value = unsafe { COUNTER.as_ptr().read() };
if value % 50 == 0 {
return true;
}
return false;
}

#[entry]
fn main() -> ! {
// Creating task executer.
let mut task_executor = task_manager::TaskExecutor::new();
// Add task to execute.
task_executor.add_task(setup_fn, loop_fn, stop_condition_fn);
// Start task manager.
task_executor.start_task_manager();
}
17 changes: 0 additions & 17 deletions src/connection.rs

This file was deleted.

28 changes: 2 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
use crate::timer::{TickType, Timer};
use lazy_static::lazy_static;
use std::thread;
use std::time::Duration;
#![no_std]

pub mod connection;
pub mod task_manager;
pub mod timer;

lazy_static! {
static ref TIMER: Timer = Timer::new(0, 5, 0.1);
}

#[no_mangle]
pub extern "C" fn init_timer() {
TIMER.start();
thread::sleep(Duration::from_millis(5));
}

#[no_mangle]
pub extern "C" fn stop_timer() {
thread::sleep(Duration::from_millis(5));
TIMER.stop();
}

#[no_mangle]
pub extern "C" fn get_tick_counter() -> TickType {
TIMER.get_tick_counter()
}
Loading

0 comments on commit 45911cb

Please sign in to comment.