Skip to content

Commit

Permalink
Add SYSTIMER based ESP32-C3 monotonic (#972)
Browse files Browse the repository at this point in the history
* add esp32c3 monotonic

* fix tests
  • Loading branch information
onsdagens authored Sep 29, 2024
1 parent 6e68a5e commit 805ea26
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 9 deletions.
34 changes: 34 additions & 0 deletions ci/expected/esp32c3/monotonic.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
QEMU 8.2.0 monitor - type 'help' for more information
(qemu) q
ESP-ROM:esp32c3-api1-20210207
Build:Feb 7 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:2
load:0x3fcd5820,len:0x1714
load:0x403cc710,len:0x968
load:0x403ce710,len:0x2f9c
entry 0x403cc710
I (0) boot: ESP-IDF v5.1.2-342-gbcf1645e44 2nd stage bootloader
I (0) boot: compile time Dec 12 2023 10:50:58
I (0) boot: chip revision: v0.3
I (0) boot.esp32c3: SPI Speed : 40MHz
I (0) boot.esp32c3: SPI Mode : SLOW READ
I (0) boot.esp32c3: SPI Flash Size : 4MB
I (0) boot: Enabling RNG early entropy source...
I (1) boot: Partition Table:
I (1) boot: ## Label Usage Type ST Offset Length
I (1) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (1) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (1) boot: 2 factory factory app 00 00 00010000 003f0000
I (1) boot: End of partition table
I (1) esp_image: REDACTED
I (3) esp_image: REDACTED
I (3) esp_image: REDACTED
I (8) esp_image: REDACTED
I (11) boot: Loaded app from partition at offset 0x10000
I (11) boot: Disabling RNG early entropy source...
init
hello from bar
hello from baz
hello from foo
5 changes: 1 addition & 4 deletions examples/esp32c3/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[target.riscv32imc-unknown-none-elf]
# Real hardware
#runner = "espflash flash --monitor"
# runner = "espflash flash --monitor"

# QEMU emulator
runner = "./runner.sh"
Expand All @@ -14,6 +14,3 @@ rustflags = [
]

target = "riscv32imc-unknown-none-elf"

[unstable]
build-std = ["core"]
78 changes: 78 additions & 0 deletions examples/esp32c3/Cargo.lock

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

3 changes: 2 additions & 1 deletion examples/esp32c3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "MIT OR Apache-2.0"

[dependencies]
rtic = {path = "../../rtic/"}
rtic-monotonics = {path = "../../rtic-monotonics/"}
esp-hal = { version = "0.16.1", features = ["esp32c3", "direct-vectoring", "interrupt-preemption"] }
esp-backtrace = { version = "0.11.0", features = [
"esp32c3",
Expand All @@ -21,4 +22,4 @@ esp-println = { version = "0.9.0", features = ["esp32c3", "uart"] }

[features]
test-critical-section = []
riscv-esp32c3-backend = ["rtic/riscv-esp32c3-backend"]
riscv-esp32c3-backend = ["rtic/riscv-esp32c3-backend", "rtic-monotonics/esp32c3-systimer"]
22 changes: 20 additions & 2 deletions examples/esp32c3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@ This crate uses the most convenient option in ``cargo-espflash`` and ``espflash`

## Running the crate

Uncomment the

```runner = "espflash flash --monitor"```

line in ``.cargo/config.toml``

and comment out (or remove)

```runner = "./runner.sh"```

Now, running

```cargo run --example sw_and_hw --features=riscv-esp32c3-backend (--release)```

should do the trick.
in the root of this crate should do the trick.

# Expected behavior
The program
The example ``sw_and_hw``
- Prints ``init``
- Enters a high prio task
- During the execution of the high prio task, the button should be non-functional
Expand All @@ -31,3 +43,9 @@ The program
- Exits the low prio task
- Prints ``idle``

The example ``monotonic``
- Prints ``init``
- Spawns the ``foo``, ``bar``, ``baz`` tasks (because of hardware interrupt latency dispatch, the order here may vary).
- Each task prints ``hello from $TASK`` on entry
- The tasks wait for 1, 2, 3 seconds respectively
- Once the wait period is over, each task exits printing ``bye from $TASK`` (now in the proper order).
51 changes: 51 additions & 0 deletions examples/esp32c3/examples/monotonic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![no_main]
#![no_std]
use esp_backtrace as _;
#[rtic::app(device = esp32c3, dispatchers = [])]
mod app {
use rtic_monotonics::esp32c3::prelude::*;
esp32c3_systimer_monotonic!(Mono);
use esp_hal as _;
use esp_println::println;

#[shared]
struct Shared {}

#[local]
struct Local {}

#[init]
fn init(cx: init::Context) -> (Shared, Local) {
println!("init");
let timer = cx.device.SYSTIMER;

Mono::start(timer);

foo::spawn().unwrap();
bar::spawn().unwrap();
baz::spawn().unwrap();

(Shared {}, Local {})
}

#[task]
async fn foo(_cx: foo::Context) {
println!("hello from foo");
Mono::delay(2_u64.secs()).await;
println!("bye from foo");
}

#[task]
async fn bar(_cx: bar::Context) {
println!("hello from bar");
Mono::delay(3_u64.secs()).await;
println!("bye from bar");
}

#[task]
async fn baz(_cx: baz::Context) {
println!("hello from baz");
Mono::delay(4_u64.secs()).await;
println!("bye from baz");
}
}
1 change: 0 additions & 1 deletion examples/esp32c3/examples/sw_and_hw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![no_main]
#![no_std]

#[rtic::app(device = esp32c3, dispatchers=[FROM_CPU_INTR0, FROM_CPU_INTR1])]
mod app {
use esp_backtrace as _;
Expand Down
2 changes: 1 addition & 1 deletion examples/esp32c3/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "nightly-2023-11-14"
channel = "stable"
components = ["rust-src"]
targets = ["riscv32imc-unknown-none-elf"]
3 changes: 3 additions & 0 deletions rtic-monotonics/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ For each category, *Added*, *Changed*, *Fixed* add new entries at the top!

## v2.0.2 - 2024-07-05

### Added
- `SYSTIMER` based monotonic for the ESP32-C3

### Fixed

- Fix `stm32` monotonic for timer peripherals with only two clock compare modules
Expand Down
9 changes: 9 additions & 0 deletions rtic-monotonics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ features = [
"stm32_tim4",
"stm32_tim5",
"stm32_tim15",
"esp32c3-systimer",
]
rustdoc-flags = ["--cfg", "docsrs"]

Expand Down Expand Up @@ -65,6 +66,11 @@ stm32-metapac = { version = "15.0.0", optional = true }
# i.MX RT
imxrt-ral = { version = "0.5.3", optional = true }


esp32c3 = {version = "0.22.0", optional = true }
riscv = {version = "0.11.1", optional = true }


[build-dependencies]
proc-macro2 = { version = "1.0.36", optional = true }
quote = { version = "1.0.15", optional = true }
Expand Down Expand Up @@ -104,6 +110,9 @@ imxrt = ["dep:cortex-m", "dep:imxrt-ral"]
imxrt_gpt1 = ["imxrt"]
imxrt_gpt2 = ["imxrt"]

# ESP32-C3 Timer
esp32c3-systimer = ["dep:esp32c3", "dep:riscv"]

# STM32 timers
# Use as `features = ["stm32g081kb", "stm32_tim15"]`
stm32_tim2 = []
Expand Down
Loading

0 comments on commit 805ea26

Please sign in to comment.