Compare commits

...

3 commits

Author SHA1 Message Date
Joe Ardent
8616e14d29 more tidy 2026-07-02 18:45:36 -07:00
Joe Ardent
b08e04eaa5 tidy 2026-07-02 18:36:04 -07:00
Joe Ardent
1892056ea8 reads PA0 for ADC and prints the raw value 2026-07-02 18:30:51 -07:00
3 changed files with 42 additions and 62 deletions

View file

@ -11,6 +11,7 @@ default-run = "treatbot-5k"
[[bin]] [[bin]]
name = "treatbot-5k" name = "treatbot-5k"
test = false test = false
bench = false
[lib] [lib]
harness = false harness = false

View file

@ -15,6 +15,7 @@ fn panic() -> ! {
/// Terminates the application and makes a semihosting-capable debug tool exit /// Terminates the application and makes a semihosting-capable debug tool exit
/// with status code 0. /// with status code 0.
pub fn exit() -> ! { pub fn exit() -> ! {
defmt::info!("goodbye");
semihosting::process::exit(0); semihosting::process::exit(0);
} }

View file

@ -1,89 +1,67 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
use defmt::{info, unwrap}; use defmt::unwrap;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_stm32::flash::{Flash, InterruptHandler}; use embassy_stm32::{
use embassy_stm32::gpio::{AnyPin, Level, Output, Speed}; adc::{Adc, SampleTime},
use embassy_stm32::{bind_interrupts, Peri}; gpio::{AnyPin, Level, Output, Speed},
peripherals::{ADC1, PA0},
Config, Peri,
};
use embassy_time::Timer; use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
FLASH => InterruptHandler;
});
#[embassy_executor::main] #[embassy_executor::main]
async fn main(spawner: Spawner) { async fn main(spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let mut config = Config::default();
info!("Hello Flash!");
config.rcc.mux.adc12sel = embassy_stm32::rcc::mux::Adcsel::SYS;
let p = embassy_stm32::init(config);
let a = Adc::new(p.ADC1, Default::default());
let channel = p.PA0.into();
spawner.spawn(unwrap!(read_adc1_pa0(a, channel)));
// Led should blink uninterrupted during erase operation
spawner.spawn(unwrap!(blinky(p.PA5.into()))); spawner.spawn(unwrap!(blinky(p.PA5.into())));
// let mut f = Flash::new(p.FLASH, Irqs); Timer::after_secs(20).await;
// // Test on bank 2 so the CPU doesn't stall (code runs from bank 1). treatbot_5k::exit()
// // G474RE in dual-bank mode: bank 2 starts at 256KB offset. }
// // Erase 4KB (2 pages at 2KB each in dual-bank mode).
// test_flash(&mut f, 256 * 1024, 4 * 1024).await;
//treatbot_5k::exit() #[embassy_executor::task]
async fn read_adc1_pa0(mut a: Adc<'static, ADC1>, mut channel: Peri<'static, PA0>) {
loop {
let val = a.blocking_read(&mut channel, SampleTime::CYCLES12_5);
defmt::info!("read {}", val);
Timer::after_secs(1).await;
}
} }
#[embassy_executor::task] #[embassy_executor::task]
async fn blinky(p: Peri<'static, AnyPin>) { async fn blinky(p: Peri<'static, AnyPin>) {
let mut led = Output::new(p, Level::High, Speed::Low); let mut led = Output::new(p, Level::High, Speed::Low);
led.set_low();
loop { loop {
info!("high");
led.set_high(); led.set_high();
Timer::after_millis(300).await; Timer::after_millis(300).await;
info!("low"); led.set_low();
Timer::after_millis(300).await;
led.set_high();
Timer::after_millis(300).await;
led.set_low();
Timer::after_millis(300).await;
led.set_high();
Timer::after_millis(600).await;
led.set_low(); led.set_low();
Timer::after_millis(300).await; Timer::after_millis(300).await;
} }
} }
async fn _test_flash(f: &mut Flash<'_>, offset: u32, size: u32) {
info!("Testing offset: {=u32:#X}, size: {=u32:#X}", offset, size);
info!("Reading...");
let mut buf = [0u8; 32];
unwrap!(f.blocking_read(offset, &mut buf));
info!("Read: {=[u8]:x}", buf);
info!("Erasing...");
unwrap!(f.erase(offset, offset + size).await);
info!("Reading...");
let mut buf = [0u8; 32];
unwrap!(f.blocking_read(offset, &mut buf));
info!("Read after erase: {=[u8]:x}", buf);
info!("Writing...");
unwrap!(
f.write(
offset,
&[
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32
]
)
.await
);
info!("Reading...");
let mut buf = [0u8; 32];
unwrap!(f.blocking_read(offset, &mut buf));
info!("Read: {=[u8]:x}", buf);
assert_eq!(
&buf[..],
&[
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32
]
);
info!("Flash async test passed!");
}