use controller board LED, adjust blink rate based on potentiometer

This commit is contained in:
Joe Ardent 2024-05-03 15:57:50 -07:00
parent c408a06e63
commit 74fa427b5d
4 changed files with 40 additions and 43 deletions

View File

@ -3,35 +3,19 @@ authors = ["Joe Ardent <code@ardent.nebcorp.com>"]
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

View File

@ -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.

View File

@ -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");
}

View File

@ -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);
}
}