embassy hal

This commit is contained in:
Priec
2025-10-19 23:26:33 +02:00
parent cc94aab412
commit 8fbe8e05eb
11 changed files with 631 additions and 16 deletions

View File

@@ -4,7 +4,7 @@
use cortex_m_rt::entry;
use panic_halt as _;
use stm32u5::stm32u575 as pac;
use pac::interrupt; // Import the interrupt enum!
use pac::interrupt; // Import the interrupt enum
// Timer frequency constants
const PRESCALER: u16 = 0;
@@ -13,17 +13,13 @@ const PULSE1_VALUE: u32 = 50000; // For 800 Hz toggle
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
// Enable TIM2 clock
dp.RCC.apb1enr1().modify(|_, w| w.tim2en().set_bit());
// Enable GPIOA clock for TIM2_CH4 (PA3)
dp.RCC.ahb2enr1().modify(|_, w| w.gpioaen().set_bit());
// Configure PA3 as AF1 (TIM2_CH4)
// Fixed: moder3 -> mode3
dp.GPIOA.moder().modify(|_, w| w.mode3().alternate());
// Fixed: afsel3 -> afrl3
dp.GPIOA.afrl().modify(|_, w| w.afrel3().af1());
// Configure TIM2
@@ -62,20 +58,12 @@ fn main() -> ! {
fn TIM2() {
unsafe {
let tim2 = &(*pac::TIM2::ptr());
// Check if CH4 compare interrupt
if tim2.sr().read().cc4if().bit_is_set() {
// Clear interrupt flag
tim2.sr().modify(|_, w| w.cc4if().clear_bit());
// Update next compare value
// Use wrapping arithmetic
let current = tim2.ccr4().read().bits();
let arr = tim2.arr().read().bits();
let next = if current + PULSE1_VALUE < arr {
current + PULSE1_VALUE
} else {
(current + PULSE1_VALUE) - arr
};
let next = current.wrapping_add(PULSE1_VALUE);
tim2.ccr4().write(|w| w.bits(next));
}
}