aboutsummaryrefslogtreecommitdiffstats
path: root/sw/eval-clock-cw-tx/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'sw/eval-clock-cw-tx/src/main.rs')
-rw-r--r--sw/eval-clock-cw-tx/src/main.rs122
1 files changed, 60 insertions, 62 deletions
diff --git a/sw/eval-clock-cw-tx/src/main.rs b/sw/eval-clock-cw-tx/src/main.rs
index 2dce7bc..b256926 100644
--- a/sw/eval-clock-cw-tx/src/main.rs
+++ b/sw/eval-clock-cw-tx/src/main.rs
@@ -1,7 +1,7 @@
/*
The MIT License (MIT)
- Copyright (c) 2021 Matthias P. Braendli
+ Copyright (c) 2023 Matthias P. Braendli
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -24,7 +24,6 @@
#![no_main]
#![no_std]
-
use core::mem::MaybeUninit;
use cortex_m_rt::ExceptionFrame;
use cortex_m_semihosting::hprintln;
@@ -36,13 +35,11 @@ use stm32f1xx_hal::{
pac::interrupt,
i2c,
gpio,
- delay::Delay,
- timer::{CountDownTimer, Timer, Event},
+ gpio::PinState,
+ timer::{CounterHz, Timer, Event},
qei::QeiOptions,
};
-use embedded_hal::digital::v2::OutputPin;
-use embedded_hal::digital::v2::InputPin;
use hd44780_driver::{Cursor, CursorBlink, Display, DisplayMode, HD44780};
pub mod ui;
@@ -57,12 +54,12 @@ use state::*;
const TICKS_PER_SECOND : u32 = 100;
struct SharedWithISR {
- state : State,
- last_sequence_state_change : u32,
- feldhell_ptt : bool,
- cw_ptt_timestamp : u32,
- cw_key_out_n : gpio::gpioa::PA15<gpio::Output<gpio::PushPull>>,
- ui : ui::UI,
+ state: State,
+ last_sequence_state_change: u32,
+ feldhell_ptt: bool,
+ cw_ptt_timestamp: u32,
+ cw_key_out_n: gpio::gpioa::PA15<gpio::Output<gpio::PushPull>>,
+ ui: ui::UI,
cw_pwm: cw::CWPWM,
cw_keyer: cw::Keyer,
cw_paddle_tip: gpio::gpiob::PB8<gpio::Input<gpio::PullUp>>,
@@ -73,7 +70,7 @@ struct SharedWithISR {
}
static mut SHARED: MaybeUninit<SharedWithISR> = MaybeUninit::uninit();
-static mut CLOCK_TIMER: MaybeUninit<CountDownTimer<pac::TIM2>> = MaybeUninit::uninit();
+static mut CLOCK_TIMER: MaybeUninit<CounterHz<pac::TIM2>> = MaybeUninit::uninit();
static mut TICK_COUNTER: MaybeUninit<u32> = MaybeUninit::uninit();
fn _ticks_now() -> u32 {
@@ -86,27 +83,27 @@ fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
- let mut rcc = dp.RCC.constrain();
- let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
+ let rcc = dp.RCC.constrain();
+ let mut afio = dp.AFIO.constrain();
let clocks = rcc.cfgr
- .use_hse(16.mhz())
- .sysclk(48.mhz())
- .pclk1(24.mhz())
- .adcclk(2.mhz())
+ .use_hse(16.MHz())
+ .sysclk(48.MHz())
+ .pclk1(24.MHz())
+ .adcclk(2.MHz())
.freeze(&mut flash.acr);
assert!(clocks.usbclk_valid());
- let mut delay = Delay::new(cp.SYST, clocks);
+ let mut delay = cp.SYST.delay(&clocks);
delay.delay_ms(200u16);
- let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
- let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
- let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
+ let mut gpioa = dp.GPIOA.split();
+ let mut gpiob = dp.GPIOB.split();
+ let mut gpioc = dp.GPIOC.split();
- let mut timer4 = Timer::tim4(dp.TIM4, &clocks, &mut rcc.apb1)
- .start_count_down(usb::TIMER_FREQ_HZ.hz());
+ let mut timer4 = Timer::new(dp.TIM4, &clocks).counter_hz();
+ timer4.start(usb::TIMER_FREQ_HZ.Hz()).unwrap();
timer4.listen(Event::Update);
let usb_dm = gpioa.pa11;
@@ -124,8 +121,9 @@ fn main() -> ! {
let cw_pwm = {
let pa8 = gpioa.pa8.into_alternate_push_pull(&mut gpioa.crh); // CW PWM output using TIM1 Ch1
- let tim1 = Timer::tim1(dp.TIM1, &clocks, &mut rcc.apb2);
- cw::CWPWM::new(pa8, tim1, &mut afio.mapr)
+ let pwm = dp.TIM1.pwm_hz(pa8, &mut afio.mapr, cw::SIDETONE_FREQ.Hz(), &clocks);
+ let channel = pwm.split();
+ cw::CWPWM::new(channel)
};
let cw_paddle_tip = gpiob.pb8.into_pull_up_input(&mut gpiob.crh); // CW paddle tip
@@ -133,16 +131,16 @@ fn main() -> ! {
// Configure PB14 as output. (LED)
let mut led = gpiob.pb14.into_push_pull_output(&mut gpiob.crh);
- led.set_low().unwrap();
+ led.set_low();
let (pa15, pb3, _pb4) = afio.mapr.disable_jtag(gpioa.pa15, gpiob.pb3, gpiob.pb4);
- let cw_key_out_n = pa15.into_push_pull_output_with_state(&mut gpioa.crh, gpio::State::High);
- let ptt_out = pb3.into_push_pull_output_with_state(&mut gpiob.crl, gpio::State::Low);
- let seq_switch = gpiob.pb5.into_push_pull_output_with_state(&mut gpiob.crl, gpio::State::Low);
+ let cw_key_out_n = pa15.into_push_pull_output_with_state(&mut gpioa.crh, PinState::High);
+ let ptt_out = pb3.into_push_pull_output_with_state(&mut gpiob.crl, PinState::Low);
+ let seq_switch = gpiob.pb5.into_push_pull_output_with_state(&mut gpiob.crl, PinState::Low);
let c1 = gpioa.pa6;
let c2 = gpioa.pa7;
- let qei = Timer::tim3(dp.TIM3, &clocks, &mut rcc.apb1)
+ let qei = Timer::new(dp.TIM3, &clocks)
.qei((c1, c2), &mut afio.mapr, QeiOptions::default());
// Configure I2C1 to be used for Si5351 and display
@@ -154,10 +152,9 @@ fn main() -> ! {
(scl, sda),
&mut afio.mapr,
i2c::Mode::Standard {
- frequency: 100_000.hz(),
+ frequency: 100_000.Hz(),
},
clocks,
- &mut rcc.apb1,
/* start_timeout_us */ 1000,
/* start_retries */ 10,
/* addr_timeout_us */ 1000,
@@ -183,20 +180,20 @@ fn main() -> ! {
lcd.set_cursor_pos(0, &mut delay).unwrap();
lcd.write_str(" HB9EGM ", &mut delay).unwrap();
lcd.set_cursor_pos(40, &mut delay).unwrap();
- lcd.write_str(" 30m CW TX 2021 ", &mut delay).unwrap();
+ lcd.write_str(" 30m TX 2023 ", &mut delay).unwrap();
delay.delay_ms(1_500u16);
let mut siclock = {
let shared = unsafe { &mut *SHARED.as_mut_ptr() };
*shared = SharedWithISR {
- state : State::new(),
- last_sequence_state_change : 0,
- feldhell_ptt : false,
- cw_ptt_timestamp : 0,
+ state: State::new(),
+ last_sequence_state_change: 0,
+ feldhell_ptt: false,
+ cw_ptt_timestamp: 0,
cw_key_out_n,
ui,
cw_pwm,
- cw_keyer : cw::Keyer::new(12, TICKS_PER_SECOND),
+ cw_keyer: cw::Keyer::new(12, TICKS_PER_SECOND),
cw_paddle_tip, cw_paddle_ring, ptt_out, seq_switch, led,
};
@@ -214,8 +211,8 @@ fn main() -> ! {
{
let timer = unsafe { &mut *CLOCK_TIMER.as_mut_ptr() };
- *timer = Timer::tim2(dp.TIM2, &clocks, &mut rcc.apb1)
- .start_count_down(TICKS_PER_SECOND.hz());
+ *timer = Timer::new(dp.TIM2, &clocks).counter_hz();
+ timer.start(TICKS_PER_SECOND.Hz()).unwrap();
timer.listen(Event::Update);
}
@@ -248,6 +245,7 @@ fn main() -> ! {
(*shared).state.clone()
});
+
let encoder_count : u16 = qei.count();
if encoder_count != last_encoder_count {
let delta = encoder_count.wrapping_sub(last_encoder_count);
@@ -296,7 +294,7 @@ fn main() -> ! {
#[interrupt]
fn TIM2() {
let timer = unsafe { &mut *CLOCK_TIMER.as_mut_ptr() };
- timer.clear_update_interrupt_flag();
+ timer.clear_interrupt(Event::Update);
let ticks = unsafe { &mut *TICK_COUNTER.as_mut_ptr() };
*ticks += 1;
@@ -308,8 +306,8 @@ fn TIM2() {
shared.state.update_disp_counter += 1;
}
- let cw_paddle_tip_low = shared.cw_paddle_tip.is_low().unwrap();
- let cw_paddle_ring_low = shared.cw_paddle_ring.is_low().unwrap();
+ let cw_paddle_tip_low = shared.cw_paddle_tip.is_low();
+ let cw_paddle_ring_low = shared.cw_paddle_ring.is_low();
let cw_ptt_delay : u32 = TICKS_PER_SECOND * 800 / 1000;
let ptt = match shared.state.mode {
@@ -335,8 +333,8 @@ fn TIM2() {
let next_state = match shared.state.sequence_state {
SequenceState::Rx => {
- shared.ptt_out.set_low().unwrap();
- shared.seq_switch.set_low().unwrap();
+ shared.ptt_out.set_low();
+ shared.seq_switch.set_low();
if ptt {
if shared.state.mode == Mode::FeldHell {
SequenceState::Switching(SequenceMode::FeldHell)
@@ -350,8 +348,8 @@ fn TIM2() {
}
},
SequenceState::Switching(m) => {
- shared.ptt_out.set_low().unwrap();
- shared.seq_switch.set_high().unwrap();
+ shared.ptt_out.set_low();
+ shared.seq_switch.set_high();
if ptt {
SequenceState::Tx(m)
}
@@ -360,8 +358,8 @@ fn TIM2() {
}
},
SequenceState::Tx(m) => {
- shared.ptt_out.set_high().unwrap();
- shared.seq_switch.set_high().unwrap();
+ shared.ptt_out.set_high();
+ shared.seq_switch.set_high();
if ptt {
SequenceState::Tx(m)
}
@@ -375,22 +373,22 @@ fn TIM2() {
SequenceState::Tx(SequenceMode::CW) => {
if cw_beep {
shared.cw_pwm.on();
- shared.cw_key_out_n.set_low().unwrap();
- shared.led.set_low().unwrap();
+ shared.cw_key_out_n.set_low();
+ shared.led.set_low();
}
else {
shared.cw_pwm.off();
- shared.cw_key_out_n.set_high().unwrap();
- shared.led.set_high().unwrap();
+ shared.cw_key_out_n.set_high();
+ shared.led.set_high();
}
},
SequenceState::Tx(SequenceMode::FeldHell) => {
- shared.led.set_low().unwrap();
+ shared.led.set_low();
},
_ => {
- shared.led.set_high().unwrap();
+ shared.led.set_high();
shared.cw_pwm.off();
- shared.cw_key_out_n.set_high().unwrap();
+ shared.cw_key_out_n.set_high();
},
}
@@ -404,20 +402,20 @@ fn TIM2() {
#[allow(non_snake_case)]
#[cortex_m_rt::exception]
-fn HardFault(ef: &ExceptionFrame) -> ! {
+unsafe fn HardFault(ef: &ExceptionFrame) -> ! {
let periph = unsafe { cortex_m::Peripherals::steal() };
let hfsr = periph.SCB.hfsr.read();
let cfsr = periph.SCB.cfsr.read();
- hprintln!("Hardfault {:x} {:x} at {:x}\n", hfsr, cfsr, ef.pc).unwrap();
+ hprintln!("Hardfault {:x} {:x} at {:x}\n", hfsr, cfsr, ef.pc());
cortex_m::asm::bkpt();
loop { }
}
#[allow(non_snake_case)]
#[cortex_m_rt::exception]
-fn DefaultHandler(irqn: i16) {
- hprintln!("Unhandled exception (IRQn = {})", irqn).unwrap();
+unsafe fn DefaultHandler(irqn: i16) {
+ hprintln!("Unhandled exception (IRQn = {})", irqn);
cortex_m::asm::bkpt();
loop { }
}