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.rs367
1 files changed, 367 insertions, 0 deletions
diff --git a/sw/eval-clock-cw-tx/src/main.rs b/sw/eval-clock-cw-tx/src/main.rs
new file mode 100644
index 0000000..4d2dae5
--- /dev/null
+++ b/sw/eval-clock-cw-tx/src/main.rs
@@ -0,0 +1,367 @@
+/*
+ The MIT License (MIT)
+
+ Copyright (c) 2021 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
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in all
+ copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+#![no_main]
+#![no_std]
+
+use core::mem::MaybeUninit;
+use cortex_m_rt::ExceptionFrame;
+use cortex_m_semihosting::hprintln;
+use panic_semihosting as _;
+
+use stm32f1xx_hal::{
+ prelude::*,
+ pac,
+ pac::interrupt,
+ i2c,
+ gpio,
+ delay::Delay,
+ timer::{CountDownTimer, 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;
+pub mod cw;
+pub mod state;
+pub mod si_clock;
+pub mod log10f;
+
+use state::*;
+
+const TICKS_PER_SECOND : u32 = 100;
+
+struct SharedWithISR {
+ state : State,
+ last_sequence_state_change : u32,
+ cw_ptt_timestamp : u32,
+ cw_key_n : gpio::gpioa::PA15<gpio::Output<gpio::OpenDrain>>,
+ ui : ui::UI,
+ cw_pwm: cw::CWPWM,
+ cw_keyer: cw::Keyer,
+ cw_paddle_tip: gpio::gpiob::PB8<gpio::Input<gpio::PullUp>>,
+ cw_paddle_ring: gpio::gpiob::PB9<gpio::Input<gpio::PullUp>>,
+ ptt_out_n: gpio::gpiob::PB3<gpio::Output<gpio::PushPull>>,
+ led : gpio::gpiob::PB14<gpio::Output<gpio::PushPull>>,
+}
+
+static mut SHARED: MaybeUninit<SharedWithISR> = MaybeUninit::uninit();
+static mut CLOCK_TIMER: MaybeUninit<CountDownTimer<pac::TIM2>> = MaybeUninit::uninit();
+static mut TICK_COUNTER: MaybeUninit<u32> = MaybeUninit::uninit();
+
+fn ticks_now() -> u32 {
+ cortex_m::interrupt::free(|_cs| unsafe { *TICK_COUNTER.as_ptr() })
+}
+
+fn get_state_copy() -> State {
+ cortex_m::interrupt::free(|_cs| unsafe {
+ (*SHARED.as_ptr()).state.clone()
+ })
+}
+
+#[cortex_m_rt::entry]
+fn main() -> ! {
+ let cp = cortex_m::Peripherals::take().unwrap();
+ 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 clocks = rcc.cfgr
+ .adcclk(2.mhz())
+ .freeze(&mut flash.acr);
+ let mut delay = Delay::new(cp.SYST, 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);
+
+ // Buttons as analog inputs (multi-level)
+ let pb0 = gpiob.pb0.into_floating_input(&mut gpiob.crl); // BTN1 Button B, has external pullup
+ let pb1 = gpiob.pb1.into_floating_input(&mut gpiob.crl); // BTN0 Button A, has external pullup
+ let pb12 = gpiob.pb12.into_pull_up_input(&mut gpiob.crh); // BTN2 Button C
+ let pb13 = gpiob.pb13.into_pull_up_input(&mut gpiob.crh); // BTN3 Button D
+ let pc15 = gpioc.pc15.into_pull_up_input(&mut gpioc.crh); // Encoder button
+ let ui = ui::UI::new(pb0, pb1, pb12, pb13, pc15);
+
+ 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 cw_paddle_tip = gpiob.pb8.into_pull_up_input(&mut gpiob.crh); // CW paddle tip
+ let cw_paddle_ring = gpiob.pb9.into_pull_up_input(&mut gpiob.crh); // CW paddle ring
+
+ // Configure PB14 as output. (LED)
+ let mut led = gpiob.pb14.into_push_pull_output(&mut gpiob.crh);
+ led.set_low().unwrap();
+
+ let (pa15, pb3, _pb4) = afio.mapr.disable_jtag(gpioa.pa15, gpiob.pb3, gpiob.pb4);
+ let cw_key_n = pa15.into_open_drain_output_with_state(&mut gpioa.crh, gpio::State::High);
+ let ptt_out_n = pb3.into_push_pull_output_with_state(&mut gpiob.crl, gpio::State::High);
+
+ let c1 = gpioa.pa6;
+ let c2 = gpioa.pa7;
+ let qei = Timer::tim3(dp.TIM3, &clocks, &mut rcc.apb1)
+ .qei((c1, c2), &mut afio.mapr, QeiOptions::default());
+
+ // Configure I2C1 to be used for Si5351 and display
+ let scl = gpiob.pb6.into_alternate_open_drain(&mut gpiob.crl);
+ let sda = gpiob.pb7.into_alternate_open_drain(&mut gpiob.crl);
+
+ let i2c = i2c::BlockingI2c::i2c1(
+ dp.I2C1,
+ (scl, sda),
+ &mut afio.mapr,
+ i2c::Mode::Standard {
+ frequency: 100_000.hz(),
+ },
+ clocks,
+ &mut rcc.apb1,
+ /* start_timeout_us */ 1000,
+ /* start_retries */ 10,
+ /* addr_timeout_us */ 1000,
+ /* data_timeout_us */ 1000,
+ );
+ let i2c_busmanager = shared_bus::BusManagerSimple::new(i2c);
+
+ const I2C_ADDRESS: u8 = 0b010_0000; // MCP23008, depending on solder bridges
+ let mut lcd = match HD44780::new_i2c_mcp23008(i2c_busmanager.acquire_i2c(), I2C_ADDRESS, &mut delay) {
+ Ok(lcd) => lcd,
+ Err(_) => panic!("HD44780 init fail"),
+ };
+
+ lcd.reset(&mut delay).unwrap();
+ lcd.clear(&mut delay).unwrap();
+ lcd.set_display_mode(
+ DisplayMode {
+ display: Display::On,
+ cursor_visibility: Cursor::Invisible,
+ cursor_blink: CursorBlink::Off,
+ },
+ &mut delay).unwrap();
+ 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();
+ 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,
+ cw_ptt_timestamp : 0,
+ cw_key_n,
+ ui,
+ cw_pwm,
+ cw_keyer : cw::Keyer::new(12, TICKS_PER_SECOND),
+ cw_paddle_tip, cw_paddle_ring, ptt_out_n, led
+ };
+
+ si_clock::SiClock::new(i2c_busmanager.acquire_i2c(), 0, shared.state.vfo())
+ };
+
+ ui::update_disp(&mut lcd, &get_state_copy(), &mut delay);
+
+ let mut last_encoder_count = qei.count();
+
+ {
+ let ticks = unsafe { &mut *TICK_COUNTER.as_mut_ptr() };
+ *ticks = 0;
+ }
+
+ {
+ 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.listen(Event::Update);
+ }
+
+ unsafe { pac::NVIC::unmask(pac::Interrupt::TIM2); }
+
+ let mut last_disp_update_counter = 1;
+ let mut previous_vfo = 0;
+
+ loop {
+ let mut update_disp_required = false;
+
+ let state = get_state_copy();
+
+ let encoder_count : u16 = qei.count();
+ if encoder_count != last_encoder_count {
+ let delta = encoder_count.wrapping_sub(last_encoder_count);
+ let delta = if delta > 0x7FFF { delta as i32 - 0x10000 } else { delta as i32 };
+
+ cortex_m::interrupt::free(|_cs| {
+ let shared = unsafe { &mut *SHARED.as_mut_ptr() };
+ shared.ui.update_encoder(&mut shared.state, delta);
+ if let Mode::CW(CWMode::Iambic) = shared.state.mode {
+ shared.cw_keyer.set_speed(shared.state.cw_wpm, TICKS_PER_SECOND)
+ }
+ });
+
+ siclock.set_vfo(state.vfo());
+ update_disp_required = true;
+ }
+
+ let vfo = state.vfo();
+ if previous_vfo != vfo {
+ siclock.set_vfo(vfo);
+ }
+ previous_vfo = vfo;
+
+ if last_disp_update_counter != state.update_disp_counter {
+ update_disp_required = true;
+ last_disp_update_counter = state.update_disp_counter;
+ }
+
+ if update_disp_required {
+ ui::update_disp(&mut lcd, &state, &mut delay);
+ }
+
+ last_encoder_count = encoder_count;
+
+ cortex_m::asm::wfi();
+ }
+}
+
+#[interrupt]
+fn TIM2() {
+ let timer = unsafe { &mut *CLOCK_TIMER.as_mut_ptr() };
+ timer.clear_update_interrupt_flag();
+
+ let ticks = unsafe { &mut *TICK_COUNTER.as_mut_ptr() };
+ *ticks += 1;
+
+ let mut shared = unsafe { &mut *SHARED.as_mut_ptr() };
+ let button_result = shared.ui.handle_buttons(&mut shared.state);
+
+ if button_result.display_update {
+ 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_ptt_delay : u32 = TICKS_PER_SECOND * 800 / 1000;
+ let cw_ptt = match shared.state.mode {
+ Mode::CW(_) => {
+ if cw_paddle_tip_low || cw_paddle_ring_low {
+ shared.cw_ptt_timestamp = *ticks;
+ true
+ }
+ else {
+ shared.cw_ptt_timestamp + cw_ptt_delay > *ticks
+ }
+ }
+ };
+
+ let cw_beep = match shared.state.mode {
+ Mode::CW(CWMode::StraightKey) => cw_paddle_tip_low,
+ Mode::CW(CWMode::Iambic) => shared.cw_keyer.tick(*ticks, cw_paddle_tip_low, cw_paddle_ring_low),
+ };
+
+ let next_state = match shared.state.sequence_state {
+ SequenceState::Rx => {
+ shared.ptt_out_n.set_high().unwrap();
+ if cw_ptt {
+ SequenceState::SwitchingCW
+ }
+ else {
+ SequenceState::Rx
+ }
+ },
+ SequenceState::SwitchingCW => {
+ shared.ptt_out_n.set_low().unwrap();
+ if cw_ptt {
+ SequenceState::TxCW
+ }
+ else {
+ SequenceState::Rx
+ }
+ },
+ SequenceState::TxCW => {
+ shared.ptt_out_n.set_low().unwrap();
+ if cw_ptt {
+ SequenceState::TxCW
+ }
+ else {
+ SequenceState::SwitchingCW
+ }
+ },
+ };
+
+ match shared.state.sequence_state {
+ SequenceState::TxCW => {
+ if cw_beep {
+ shared.led.set_low().unwrap();
+ shared.cw_pwm.on();
+ shared.cw_key_n.set_low().unwrap();
+ }
+ else {
+ shared.led.set_high().unwrap();
+ shared.cw_pwm.off();
+ shared.cw_key_n.set_high().unwrap();
+ }
+ },
+ _ => {
+ shared.led.set_high().unwrap();
+ shared.cw_pwm.off();
+ shared.cw_key_n.set_high().unwrap();
+ },
+ }
+
+ const SWITCHING_DELAY : u32 = TICKS_PER_SECOND * 80 / 1000;
+ if shared.state.sequence_state != next_state &&
+ shared.last_sequence_state_change + SWITCHING_DELAY <= *ticks {
+ shared.state.sequence_state = next_state;
+ shared.last_sequence_state_change = *ticks;
+ }
+}
+
+#[cortex_m_rt::exception]
+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();
+ cortex_m::asm::bkpt();
+ loop { }
+}
+
+#[cortex_m_rt::exception]
+fn DefaultHandler(irqn: i16) {
+ hprintln!("Unhandled exception (IRQn = {})", irqn).unwrap();
+ cortex_m::asm::bkpt();
+ loop { }
+}