adds blocking pwm working impl for the spinnyboy in rust based on the esp32c6

This commit is contained in:
Robert Schauklies 2025-12-21 18:52:16 +01:00
parent dd341bbb67
commit 6f2ab7cb95
9 changed files with 1490 additions and 0 deletions

View file

@ -0,0 +1,16 @@
[target.riscv32imac-unknown-none-elf]
runner = "probe-rs run --chip=esp32c6 --preverify --always-print-stacktrace --no-location --catch-hardfault"
[env]
[build]
rustflags = [
# Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.)
# NOTE: May negatively impact performance of produced code
"-C", "force-frame-pointers",
]
target = "riscv32imac-unknown-none-elf"
[unstable]
build-std = ["alloc", "core"]

22
spinnyboy_rust/.gitignore vendored Normal file
View file

@ -0,0 +1,22 @@
# will have compiled files and executables
debug/
target/
# Editor configuration
.vscode/
.zed/
.helix/
.nvim.lua
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

1254
spinnyboy_rust/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

34
spinnyboy_rust/Cargo.toml Normal file
View file

@ -0,0 +1,34 @@
[package]
edition = "2024"
name = "spinnyboy_rust"
rust-version = "1.88"
version = "0.1.0"
[[bin]]
name = "spinnyboy_rust"
path = "./src/bin/main.rs"
[dependencies]
esp-hal = { version = "1.0.0", features = ["esp32c6", "unstable"] }
esp-bootloader-esp-idf = { version = "0.4.0", features = ["esp32c6"] }
critical-section = "1.2.0"
esp-alloc = "0.9.0"
rtt-target = "0.6.2"
[profile.dev]
# Rust debug is too slow.
# For debug builds always builds with some optimization
opt-level = "s"
[profile.release]
codegen-units = 1 # LLVM can perform better optimizations using a single thread
debug = 2
debug-assertions = false
incremental = false
lto = 'fat'
opt-level = 's'
overflow-checks = false

56
spinnyboy_rust/build.rs Normal file
View file

@ -0,0 +1,56 @@
fn main() {
linker_be_nice();
// make sure linkall.x is the last linker script (otherwise might cause problems with flip-link)
println!("cargo:rustc-link-arg=-Tlinkall.x");
}
fn linker_be_nice() {
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 {
let kind = &args[1];
let what = &args[2];
match kind.as_str() {
"undefined-symbol" => match what.as_str() {
"_defmt_timestamp" => {
eprintln!();
eprintln!(
"💡 `defmt` not found - make sure `defmt.x` is added as a linker script and you have included `use defmt_rtt as _;`"
);
eprintln!();
}
"_stack_start" => {
eprintln!();
eprintln!("💡 Is the linker script `linkall.x` missing?");
eprintln!();
}
"esp_rtos_initialized" | "esp_rtos_yield_task" | "esp_rtos_task_create" => {
eprintln!();
eprintln!(
"💡 `esp-radio` has no scheduler enabled. Make sure you have initialized `esp-rtos` or provided an external scheduler."
);
eprintln!();
}
"embedded_test_linker_file_not_added_to_rustflags" => {
eprintln!();
eprintln!(
"💡 `embedded-test` not found - make sure `embedded-test.x` is added as a linker script for tests"
);
eprintln!();
}
_ => (),
},
// we don't have anything helpful for "missing-lib" yet
_ => {
std::process::exit(1);
}
}
std::process::exit(0);
}
println!(
"cargo:rustc-link-arg=--error-handling-script={}",
std::env::current_exe().unwrap().display()
);
}

View file

@ -0,0 +1,4 @@
[toolchain]
channel = "stable"
components = ["rust-src"]
targets = ["riscv32imac-unknown-none-elf"]

19
spinnyboy_rust/shell.nix Normal file
View file

@ -0,0 +1,19 @@
{ pkgs ? import <nixpkgs> {}}:
let rust-toolchain = with pkgs; symlinkJoin {
name = "rust-toolchain";
paths = [
probe-rs-tools
rustup
rustc
cargo
rustfmt
rust-analyzer
clippy
rustPlatform.rustcSrc
];
};
in pkgs.mkShell {
buildInputs = [rust-toolchain];
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
RUST_BACKTRACE = 1;
}

View file

@ -0,0 +1,84 @@
#![no_std]
#![no_main]
#![deny(
clippy::mem_forget,
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
holding buffers for the duration of a data transfer."
)]
use esp_hal::clock::CpuClock;
use esp_hal::mcpwm::operator::PwmPinConfig;
use esp_hal::mcpwm::timer::PwmWorkingMode;
use esp_hal::time::Rate;
use esp_hal::timer::timg::TimerGroup;
use esp_hal::main;
use esp_hal::time::{Duration, Instant};
use esp_hal::delay::Delay;
use rtt_target::rprintln;
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}
extern crate alloc;
// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();
#[main]
fn main() -> ! {
// generator version: 1.0.1
rtt_target::rtt_init_print!();
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
let system = peripherals.SYSTEM;
let mut delay = Delay::new();
esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 65536);
let clock_config = esp_hal::mcpwm::PeripheralClockConfig::with_frequency(Rate::from_mhz(32)).unwrap();
let mut mcpwm = esp_hal::mcpwm::McPwm::new(peripherals.MCPWM0, clock_config);
mcpwm.operator0.set_timer(&mcpwm.timer0);
let pin = peripherals.GPIO18;
let mut pwm_pin = mcpwm.operator0.with_pin_a(pin, PwmPinConfig::UP_ACTIVE_HIGH);
let timer_clock_cfg = clock_config
.timer_clock_with_frequency(19_999, PwmWorkingMode::Increase, Rate::from_hz(50))
.unwrap();
mcpwm.timer0.start(timer_clock_cfg);
pwm_pin.set_timestamp(1000);
delay.delay_millis(3000);
rprintln!("WUT?");
pwm_pin.set_timestamp(1121);
delay.delay_millis(1000);
pwm_pin.set_timestamp(1055);
delay.delay_millis(3000);
// Example: Ramp from 0% to 50% throttle
//the afro esc starts turning at roughly setup gets a PWM at
//loop{
// for pulse in 1120..1500 {
// rprintln!("pulse:{}",pulse);
// pwm_pin.set_timestamp(pulse);
// Timer::after_millis(3000).await;
// }
// Timer::after_millis(1000).await;
// // Return to idle
// pwm_pin.set_timestamp(1000);
// Timer::after_millis(1000).await;
// }
loop {
rprintln!("Hello world!");
delay.delay_millis(3000);
}
// for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.0.0/examples/src/bin
}

View file

@ -0,0 +1 @@
#![no_std]