From 74fa427b5d91532b74b839a6c7421b00eff49dc6 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Fri, 3 May 2024 15:57:50 -0700 Subject: [PATCH] use controller board LED, adjust blink rate based on potentiometer --- Cargo.toml | 26 +++++--------------------- README.md | 21 ++++++++++----------- build.rs | 3 +++ src/main.rs | 33 ++++++++++++++++++++++----------- 4 files changed, 40 insertions(+), 43 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fe3e336..fca9008 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,35 +3,19 @@ authors = ["Joe Ardent "] edition = "2021" readme = "README.md" name = "tinfoc" -version = "0.1.0" +version = "0.0.1" [dependencies] -cortex-m = "0.7" +cortex-m = { version = "0.7", features = ["critical-section-single-core"] } cortex-m-rt = "0.7" -cortex-m-semihosting = "0.5" -# panic-halt = "0.2" -panic-semihosting = "0.6" -embedded-hal = "1" -nb = "1" +defmt = "0.3" +defmt-rtt = "0.4" +panic-halt = "0.2" [dependencies.stm32f3xx-hal] version = "0.10" features = ["stm32f302x8", "ld", "rt", "can", "rtc"] -# Uncomment for the panic example. -# panic-itm = "0.4.1" - -# Uncomment for the allocator example. -# alloc-cortex-m = "0.4.0" - -# Uncomment for the device example. -# Update `memory.x`, set target to `thumbv7em-none-eabihf` in `.cargo/config`, -# and then use `cargo build --example device` to build it. -# [dependencies.stm32f3] -# features = ["stm32f303", "rt"] -# version = "0.7.1" - -# this lets you use `cargo fix`! [[bin]] name = "tinfoc" test = false diff --git a/README.md b/README.md index 11f2501..2fd3a2d 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ A crate that provides an [FOC](https://www.st.com/en/applications/industrial-motor-control/3-phase-field-oriented-control-foc.html) motor controller using STM32 Cortex microcontrollers. +Or rather, it *would* if it were done, but it is not. It's currently my embedded programming +playground project, but I do intend on making it useful and good. I'll let you know when that's the +case, but until then, it's going to be a work in progress. + ## Dependencies The MCU I'm using is the @@ -24,21 +28,16 @@ linux, you'll need to: sudo apt install openocd gdb-multiarch ``` +[probe-rs](https://probe.rs/) is a convenient tool for interacting with your dev board. ## Running on hardware Assuming you've plugged your board into your computer via USB: 1. build TinFOC with `cargo build` -2. connect to the board with `openocd &` -3. start gdb: `gdb-multiarch -q target/thumbv7em-none-eabihf/debug/tinfoc` -4. inside gdb, attach to openocd: `target remote :3333` -5. inside gdb, load the program into the MCU's flash: `load` +2. load it onto the board and run it with: + - `probe-rs run --chip=STM32F302R8Tx target/thumbv7em-none-eabihf/debug/tinfoc` -Assuming that was successful, you can then enter `continue` in the gdb console, which will run the -program on the hardware; you should see the `LD2` LED start blinking green, on and off, every 2 -seconds. If you disconnect the board from power and then reconnect, it will automatically run the -program. - -There are no doubt other ways to flash the board, and I'll update here when I adjust my flow; I'm a -newb when it comes to this stuff. +If all went well, and your IHM07M1 motor controller board is attached to the Nucleo dev board, the +red LED on the IHM07M1 should start to blink, and you can turn the potentiometer to adjust the +blinking rate. diff --git a/build.rs b/build.rs index 2a51f4e..85a0cf0 100644 --- a/build.rs +++ b/build.rs @@ -40,4 +40,7 @@ fn main() { // Set the linker script to the one provided by cortex-m-rt. println!("cargo:rustc-link-arg=-Tlink.x"); + + // use defmt + println!("cargo:rustc-link-arg=-Tdefmt.x"); } diff --git a/src/main.rs b/src/main.rs index 47ad31f..4632792 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,33 +1,44 @@ #![no_std] #![no_main] -// pick a panicking behavior -//extern crate panic_halt; // you can put a breakpoint on `rust_begin_unwind` to catch panics -// extern crate panic_abort; // requires nightly -// extern crate panic_itm; // logs messages over ITM; requires ITM support -extern crate panic_semihosting; // logs messages to the host stderr; requires a debugger +extern crate panic_halt; // you can put a breakpoint on `rust_begin_unwind` to catch panics -use cortex_m_rt::entry; +use stm32f3xx_hal::{self as hal, adc, pac, prelude::*}; -//use embedded_hal::digital::OutputPin; -use stm32f3xx_hal::{self as hal, pac, prelude::*}; +use defmt_rtt as _; -#[entry] +static POT_MAX: u16 = 4095; + +#[cortex_m_rt::entry] fn main() -> ! { let sp = pac::Peripherals::take().unwrap(); let mut rcc = sp.RCC.constrain(); let mut gpiob = sp.GPIOB.split(&mut rcc.ahb); let mut led = gpiob - .pb13 + .pb2 .into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper); + let mut pot = gpiob.pb1.into_analog(&mut gpiob.moder, &mut gpiob.pupdr); + let mut flash = sp.FLASH.constrain(); let clocks = rcc.cfgr.freeze(&mut flash.acr); let cp = cortex_m::Peripherals::take().unwrap(); let mut delay = hal::delay::Delay::new(cp.SYST, clocks); + let adc_common = adc::CommonAdc::new(sp.ADC1_2, &clocks, &mut rcc.ahb); + let mut padc = adc::Adc::new( + sp.ADC1, + adc::config::Config::default(), + &clocks, + &adc_common, + ); + loop { + let pot_out: u16 = padc.read(&mut pot).unwrap_or(0); + let pot_out = (POT_MAX - pot_out) as f32 / POT_MAX as f32; + let pot_out = (4000.0 * pot_out) as u32; + led.toggle().unwrap(); - delay.delay_ms(2000_u32); + delay.delay_ms(pot_out); } }