5 Commits 69b159e658 ... d9b82e0611

Author SHA1 Message Date
  Kevin Lee d9b82e0611 Add RTIC implementation 2 years ago
  Kevin Lee c06b62a1b9 Minor refactoring to clarify names 2 years ago
  Kevin Lee db03afa956 Remove timer references to interrupt handlers 2 years ago
  Kevin Lee acf3dbd036 Refactor peripheral definition 2 years ago
  Kevin Lee 403c3e85b7 Refactoring interrupt functions 2 years ago

+ 9 - 0
Nixie_Firmware_Rust/Cargo.toml

@@ -8,6 +8,7 @@ version = "0.1.0"
 [dependencies]
 cortex-m = "0.6.0"
 cortex-m-rt = "0.6.10"
+cortex-m-rtic = "0.5.8"
 cortex-m-semihosting = "0.3.3"
 tock-registers = "0.7.0"
 field-offset = "0.3.4"
@@ -17,3 +18,11 @@ stm32l4xx-hal = { version = "0.6.0", features = ["stm32l4x2", "rt"] }
 codegen-units = 1 # better optimizations
 debug = true # symbols are nice and they don't increase the size on Flash
 lto = true # better optimizations
+
+[[bin]]
+name = "nixie-clock-in-8"
+path = "src/main.rs"
+
+[[bin]]
+name = "nixie-clock-in-8-rtic"
+path = "src/main_rtic.rs"

+ 4 - 1
Nixie_Firmware_Rust/dfu.sh

@@ -1,5 +1,8 @@
 #!/bin/bash
 
 DIR="target/thumbv7em-none-eabihf/release"
-cargo objcopy --release -- -O binary $DIR/nixie-clock-in-8.bin
+cargo objcopy --release --bin nixie-clock-in-8 -- -O binary $DIR/nixie-clock-in-8.bin
 python2 ./dfu.py -b 0x08000000:$DIR/nixie-clock-in-8.bin -D 0x0483:0xdf11 $DIR/nixie-clock-in-8.dfu
+
+cargo objcopy --release --bin nixie-clock-in-8-rtic -- -O binary $DIR/nixie-clock-in-8-rtic.bin
+python2 ./dfu.py -b 0x08000000:$DIR/nixie-clock-in-8-rtic.bin -D 0x0483:0xdf11 $DIR/nixie-clock-in-8-rtic.dfu

+ 85 - 23
Nixie_Firmware_Rust/src/main.rs

@@ -34,14 +34,22 @@ mod tusb322;
 
 use nixie::*;
 
-static RTC_INT: Mutex<RefCell<Option<PB5<Input<Floating>>>>> = Mutex::new(RefCell::new(None));
-static FAULT_INT: Mutex<RefCell<Option<PA3<Input<PullUp>>>>> = Mutex::new(RefCell::new(None));
-static FAULT_LED: Mutex<RefCell<Option<PC15<Output<PushPull>>>>> = Mutex::new(RefCell::new(None));
-static I2C: Mutex<RefCell<Option<
-            I2c<I2C1, (PA9<Alternate<AF4, Output<OpenDrain>>>,PA10<Alternate<AF4,Output<OpenDrain>>>,),>,
-        >,>,> = Mutex::new(RefCell::new(None));
-static FPS_TIMER: Mutex<RefCell<Option<Timer<TIM2>>>> = Mutex::new(RefCell::new(None));
-static CYCLE_TIMER: Mutex<RefCell<Option<Timer<TIM7>>>> = Mutex::new(RefCell::new(None));
+// Local peripheral mappings
+type RtcInt = PB5<Input<Floating>>;
+type FaultInt = PA3<Input<PullUp>>;
+type FaultLed = PC15<Output<PushPull>>;
+type I2c1 = I2c<I2C1, (PA9<Alternate<AF4, Output<OpenDrain>>>,PA10<Alternate<AF4,Output<OpenDrain>>>)>;
+type FpsTimer = Timer<TIM2>;
+type CycleTimer = Timer<TIM7>;
+
+// Global peripheral singletons
+static RTC_INT: Mutex<RefCell<Option<RtcInt>>> = Mutex::new(RefCell::new(None));
+static FAULT_INT: Mutex<RefCell<Option<FaultInt>>> = Mutex::new(RefCell::new(None));
+static FAULT_LED: Mutex<RefCell<Option<FaultLed>>> = Mutex::new(RefCell::new(None));
+static I2C: Mutex<RefCell<Option<I2c1>>> = Mutex::new(RefCell::new(None));
+static REFRESH_TIMER: Mutex<RefCell<Option<FpsTimer>>> = Mutex::new(RefCell::new(None));
+static CYCLE_TIMER: Mutex<RefCell<Option<CycleTimer>>> = Mutex::new(RefCell::new(None));
+static CLOCK: Mutex<RefCell<Clock>> = Mutex::new(RefCell::new(Clock::default()));
 
 #[cfg(not(test))]
 #[entry]
@@ -169,14 +177,14 @@ fn main() -> ! {
     let mut pwm_enable = gpioa.pa7.into_push_pull_output_with_state(&mut gpioa.moder, &mut gpioa.otyper, State::High);
 
     // Initialize the PCA9685 display refresh timer
-    let fps_timer = Timer::tim2(dp.TIM2, nixie::DISPLAY_REFRESH_FPS.hz(), clocks, &mut rcc.apb1r1);
+    let refresh_timer = Timer::tim2(dp.TIM2, nixie::DISPLAY_REFRESH_FPS.hz(), clocks, &mut rcc.apb1r1);
 
     // Configure NVIC mask to enable interrupt for the display refresh timer
     unsafe { NVIC::unmask(Interrupt::TIM2) };
 
     // Save display refresh timer in static singleton so that interrupt has access to it
     free(|cs| {
-        FPS_TIMER.borrow(cs).replace(Some(fps_timer));
+        REFRESH_TIMER.borrow(cs).replace(Some(refresh_timer));
     });
 
     // Initiaize display cycle timer
@@ -208,10 +216,10 @@ fn main() -> ! {
     hv_enable.set_high().unwrap();
 
     // Cycle through all tubes on powerup
-    nixie::cycle_start(0);
-    nixie::cycle_start(1);
-    nixie::cycle_start(2);
-    nixie::cycle_start(3);
+    trigger_cycle(0);
+    trigger_cycle(1);
+    trigger_cycle(2);
+    trigger_cycle(3);
 
     loop {
         // Delay before cycling digits to prevent cathode poisoning
@@ -219,7 +227,7 @@ fn main() -> ! {
 
         // Choose a random tube to cycle
         let tube = (rng.get_random_data() % 4) as usize;
-        nixie::cycle_start(tube);
+        trigger_cycle(tube);
     }
 }
 
@@ -236,17 +244,50 @@ fn set_fault_led(state: State) {
     });
 }
 
+// Trigger the start of a new cycle sequence
+fn trigger_cycle(tube: usize) {
+    free(|cs| {
+        let mut cycle_timer_ref = CYCLE_TIMER.borrow(cs).borrow_mut();
+        let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
+        if let Some(ref mut cycle_timer) = cycle_timer_ref.deref_mut() {
+            // Trigger the start of a cycling sequence
+            clock_ref.deref_mut().cycle_start(tube);
+
+            // Start the timer to cycle through individual digits
+            cycle_timer.listen(Event::TimeOut);
+        }
+    });
+}
+
 // Interrupt handler for 1HZ signal from offchip RTC (DS3231)
 #[interrupt]
 fn EXTI9_5() {
     free(|cs| {
         let mut rtc_int_ref = RTC_INT.borrow(cs).borrow_mut();
         let mut i2c_int_ref = I2C.borrow(cs).borrow_mut();
+        let mut refresh_timer_ref = REFRESH_TIMER.borrow(cs).borrow_mut();
+        let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
         if let Some(ref mut rtc_int) = rtc_int_ref.deref_mut() {
             if let Some(ref mut i2c) = i2c_int_ref.deref_mut() {
-                if rtc_int.check_interrupt() {
-                    nixie::rtc_interrupt(i2c);
-                    rtc_int.clear_interrupt_pending_bit();
+                if let Some(ref mut refresh_timer) = refresh_timer_ref.deref_mut() {
+                    if rtc_int.check_interrupt() {
+                        // Read new time from DS3231
+                        let (second, minute, hour) = ds3231::get_time(DS3231_ADDR, i2c);
+                        let (weekday, day, month, _, _) = ds3231::get_date(DS3231_ADDR, i2c);
+
+                        // Calculate new values and account for DST
+                        let hour = if ds3231::in_dst(weekday, day, month, hour) { (hour + 1) % 12 } else { hour % 12 };
+                        let hour = if hour == 0 { 12 } else { hour };
+
+                        // Trigger the processing of a new time value
+                        clock_ref.deref_mut().rtc_tick(second, minute, hour);
+
+                        // Start the refresh timer to update the display
+                        refresh_timer.listen(Event::TimeOut);
+                        
+                        // Clear the interrupt flag for the timer
+                        rtc_int.clear_interrupt_pending_bit();
+                    }
                 }
             }
         }
@@ -272,8 +313,21 @@ fn EXTI3() {
 fn TIM2() {
     free(|cs| {
         let mut i2c_int_ref = I2C.borrow(cs).borrow_mut();
+        let mut refresh_timer_ref = REFRESH_TIMER.borrow(cs).borrow_mut();
+        let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
         if let Some(ref mut i2c) = i2c_int_ref.deref_mut() {
-            nixie::fps_interrupt(i2c);
+            if let Some(ref mut refresh_timer) = refresh_timer_ref.deref_mut() {
+                // Compute updates for non-static digits
+                let updated = clock_ref.deref_mut().fps_tick();
+
+                // Write new values if values have changed, otherwise disable the refresh timer
+                if updated {
+                    clock_ref.deref_mut().write_i2c(i2c);
+                    refresh_timer.clear_interrupt(Event::TimeOut);
+                } else {
+                    refresh_timer.unlisten(Event::TimeOut);
+                }
+            }
         }
     });
 }
@@ -283,11 +337,19 @@ fn TIM2() {
 fn TIM7() {
     free(|cs| {
         let mut cycle_timer_ref = CYCLE_TIMER.borrow(cs).borrow_mut();
+        let mut refresh_timer_ref = REFRESH_TIMER.borrow(cs).borrow_mut();
+        let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
         if let Some(ref mut cycle_timer) = cycle_timer_ref.deref_mut() {
-            if nixie::cycle_interrupt() {
-                cycle_timer.unlisten(Event::TimeOut);
-            } else {
-                cycle_timer.clear_interrupt(Event::TimeOut);
+            if let Some(ref mut refresh_timer) = refresh_timer_ref.deref_mut() {
+                // Trigger the next step in the cycling sequence
+                if clock_ref.deref_mut().cycle_tick() {
+                    cycle_timer.unlisten(Event::TimeOut);
+                } else {
+                    cycle_timer.clear_interrupt(Event::TimeOut);
+                }
+
+                // Start the refresh timer to update the display
+                refresh_timer.listen(Event::TimeOut);
             }
         }
     });

+ 329 - 0
Nixie_Firmware_Rust/src/main_rtic.rs

@@ -0,0 +1,329 @@
+#![cfg_attr(test, allow(unused_imports))]
+#![cfg_attr(not(test), no_std)]
+#![cfg_attr(not(test), no_main)]
+#![feature(half_open_range_patterns)]
+#![feature(exclusive_range_pattern)]
+#![feature(destructuring_assignment)]
+#![allow(dead_code)]
+
+// custom panic handler
+#[cfg(not(test))]
+use core::panic::PanicInfo;
+
+use core::{cell::RefCell, ops::DerefMut};
+use cortex_m::interrupt::{Mutex, free};
+use stm32l4xx_hal::{delay::Delay, device::{I2C1, TIM2, TIM7}, gpio::{
+        Alternate, Edge, Floating, Input, OpenDrain, Output, PullUp, PushPull, AF4, PA3, PB5, PC15,
+    }, gpio::{State, PA10, PA9}, i2c::I2c, prelude::*, rcc, rng::Rng, timer::{Timer, Event}};
+
+mod ds3231;
+mod nixie;
+mod pca9685;
+mod tusb322;
+
+use nixie::*;
+
+type FaultLed = PC15<Output<PushPull>>;
+static FAULT_LED: Mutex<RefCell<Option<FaultLed>>> = Mutex::new(RefCell::new(None));
+
+#[rtic::app(device = stm32l4xx_hal::pac, peripherals = true)]
+const APP: () = {
+    struct Resources {
+        #[init(Clock::default())]
+        clock: Clock,
+
+        // Late initialized resources (shared peripherals)
+        rtc_int: PB5<Input<Floating>>,
+        fault_int: PA3<Input<PullUp>>,
+        i2c: I2c<I2C1, (PA9<Alternate<AF4, Output<OpenDrain>>>,PA10<Alternate<AF4,Output<OpenDrain>>>)>,
+        refresh_timer: Timer<TIM2>,
+        cycle_timer: Timer<TIM7>,
+        delay_timer: Delay,
+        rng: Rng,
+    }
+
+    #[init(spawn = [start_cycle])]
+    fn init(ctx: init::Context) -> init::LateResources {
+        let mut dp = ctx.device;
+        let cp = ctx.core;
+
+        // Consume the raw peripheral and return a new object that implements a higher level API
+        let mut flash = dp.FLASH.constrain();
+        let mut rcc = dp.RCC.constrain();
+        let mut pwr = dp.PWR.constrain(&mut rcc.apb1r1);
+
+        // Configure clocks to run at maximum frequency off internal oscillator
+        let clocks = rcc
+            .cfgr
+            .pll_source(rcc::PllSource::HSI16)
+            .sysclk(64.mhz())
+            .hclk(64.mhz())
+            .pclk1(64.mhz())
+            .pclk2(64.mhz())
+            .hsi48(true)
+            .freeze(&mut flash.acr, &mut pwr);
+
+        // Configure delay timer that operates off systick timer
+        let mut delay_timer = Delay::new(cp.SYST, clocks);
+
+        // Split GPIO peripheral into independent pins and registers
+        let mut gpioa = dp.GPIOA.split(&mut rcc.ahb2);
+        let mut gpiob = dp.GPIOB.split(&mut rcc.ahb2);
+        let mut gpioc = dp.GPIOC.split(&mut rcc.ahb2);
+
+        // Configure high voltage PSU enable pin on PA2
+        let mut hv_enable = gpioa.pa2.into_push_pull_output_with_state(&mut gpioa.moder, &mut gpioa.otyper, State::Low);
+
+        // Configure serial port
+        // let tx = gpiob.pb6.into_af7(&mut gpiob.moder, &mut gpiob.afrl);
+        // let rx = gpiob.pb7.into_af7(&mut gpiob.moder, &mut gpiob.afrl);
+        // let _serial = Serial::usart1(
+        //     dp.USART1,
+        //     (tx, rx),
+        //     Config::default().baudrate(115_200.bps()),
+        //     clocks,
+        //     &mut rcc.apb2,
+        // );
+
+        // Configure fault LED output on PC15
+        let fault_led = gpioc.pc15.into_push_pull_output_with_state(&mut gpioc.moder, &mut gpioc.otyper, State::Low);
+
+        // Store fault LED in static singleton so that it's accessible from anywhere
+        free(|cs| {
+            FAULT_LED.borrow(cs).replace(Some(fault_led));
+        });
+
+        // Configure fault input interrupt on PA3
+        let mut fault_int = gpioa.pa3.into_pull_up_input(&mut gpioa.moder, &mut gpioa.pupdr);
+        fault_int.make_interrupt_source(&mut dp.SYSCFG, &mut rcc.apb2);
+        fault_int.enable_interrupt(&mut dp.EXTI);
+        fault_int.trigger_on_edge(&mut dp.EXTI, Edge::FALLING);
+
+        // Sanity check that fault pin isn't already set (active low)
+        if fault_int.is_low().unwrap() {
+            panic!();
+        }
+
+        // Enable RNG peripheral
+        let rng = dp.RNG.enable(&mut rcc.ahb2, clocks);
+
+        // Configure I2C SCL pin
+        let scl = gpioa.pa9.into_open_drain_output(&mut gpioa.moder, &mut gpioa.otyper);
+        let scl = scl.into_af4(&mut gpioa.moder, &mut gpioa.afrh);
+
+        // Configure I2C SDA pin
+        let sda = gpioa.pa10.into_open_drain_output(&mut gpioa.moder, &mut gpioa.otyper);
+        let sda = sda.into_af4(&mut gpioa.moder, &mut gpioa.afrh);
+
+        // Initialize I2C (configured for 1Mhz, but actually runs at 600kHz)
+        let mut i2c = I2c::i2c1(dp.I2C1, (scl, sda), 1.mhz(), clocks, &mut rcc.apb1r1);
+
+        // Initialize TUSB322 (USB Type-C configuration chip)
+        tusb322::init(TUSB322_ADDR, &mut i2c);
+
+        // Initialize DS3231 (RTC)
+        ds3231::init(DS3231_ADDR, &mut i2c);
+
+        // ds3231::set_date(DS3231_ADDR, &mut i2c, ds3231::Weekday::Wednesday, 15, 9, 21, 20);
+        // ds3231::set_time(DS3231_ADDR, &mut i2c, 00, 37, 12);
+
+        // Configure input interrupt pin from DS3231 on PB5
+        // Interrupt is pulled high, with open drain on DS3231 
+        // Interrupt enable is automatically handled by RTIC's #[task]
+        let mut rtc_int = gpiob.pb5.into_floating_input(&mut gpiob.moder, &mut gpiob.pupdr);
+        rtc_int.make_interrupt_source(&mut dp.SYSCFG, &mut rcc.apb2);
+        rtc_int.enable_interrupt(&mut dp.EXTI);
+        rtc_int.trigger_on_edge(&mut dp.EXTI, Edge::FALLING);
+
+        // Configure DAC AMP enable pin for AD8591 on PB1
+        let mut _dac_enable = gpiob.pb1.into_push_pull_output_with_state(&mut gpiob.moder, &mut gpiob.otyper, State::High);
+
+        // Configure DAC VIN for AD8591 on PA5
+        // Note that this pin should actually be configured as analog output (for DAC)
+        // but stm32l4xx_hal doesn't have support for the DAC as of now. We also currently
+        // set the output to only the highest possible voltage, so the same functionality
+        // can be achieved by configuring the pin as a digital output set to high.
+        let mut _dac_output = gpioa.pa5.into_push_pull_output_with_state(&mut gpioa.moder, &mut gpioa.otyper, State::High);
+
+        // Configure PWM enable pin (active low) for PCA9685 on PA7
+        let mut pwm_enable = gpioa.pa7.into_push_pull_output_with_state(&mut gpioa.moder, &mut gpioa.otyper, State::High);
+
+        // Initialize the PCA9685 display refresh timer
+        // Interrupt enable is automatically handled by RTIC's #[task]
+        let refresh_timer = Timer::tim2(dp.TIM2, nixie::DISPLAY_REFRESH_FPS.hz(), clocks, &mut rcc.apb1r1);
+
+        // Initiaize display cycle timer
+        // Interrupt enable is automatically handled by RTIC's #[task]
+        let cycle_timer = Timer::tim7(dp.TIM7, (1000 / nixie::CYCLE_FADE_DURATION_MS).hz(), clocks, &mut rcc.apb1r1);
+
+        // Small delay to ensure that PCA9685 is fully powered on before writing to it
+        delay_timer.delay_us(10_u32);
+
+        // Initialize PCA9685 (PWM driver)
+        pca9685::init(PCA9685_ALL_CALL, &mut i2c);
+
+        // Enable PWM output after PCA9685 has been initialized
+        pwm_enable.set_low().unwrap();
+
+        // Enable the high voltage power supply last
+        hv_enable.set_high().unwrap();
+
+        // Spawn tasks to cycle through all tubes on powerup
+        ctx.spawn.start_cycle(0).unwrap();
+        ctx.spawn.start_cycle(1).unwrap();
+        ctx.spawn.start_cycle(2).unwrap();
+        ctx.spawn.start_cycle(3).unwrap();
+
+        init::LateResources { rtc_int, fault_int, i2c, refresh_timer, cycle_timer, delay_timer, rng }
+    }
+
+    #[idle(spawn = [start_cycle], resources = [delay_timer, rng])]
+    fn idle(ctx: idle::Context) -> ! {
+        let idle::Resources {delay_timer, rng} = ctx.resources;
+
+        loop {
+            // Delay before cycling digits to prevent cathode poisoning
+            delay_timer.delay_ms(CYCLE_REFRESH_INTERVAL * 1000);
+
+            // Choose a random tube to cycle
+            let tube = (rng.get_random_data() % 4) as usize;
+            ctx.spawn.start_cycle(tube).unwrap();
+        }
+    }
+
+    // Interrupt handler for 1HZ signal from offchip RTC (DS3231)
+    #[task(binds = EXTI9_5, priority = 1, resources = [clock, rtc_int, i2c, refresh_timer])]
+    fn rtc_interrupt(ctx: rtc_interrupt::Context) {
+        let rtc_interrupt::Resources {mut clock, rtc_int, mut i2c, mut refresh_timer} = ctx.resources;
+
+        let (mut second, mut minute, mut hour) = (0, 0, 0);
+        let (mut weekday, mut day, mut month) = (ds3231::Weekday::Sunday, 0, 0);
+
+        // Lower priority task requires a critical section to access shared peripheral
+        i2c.lock(|i2c| {
+            // Read new time from DS3231
+            (second, minute, hour) = ds3231::get_time(DS3231_ADDR, i2c);
+            (weekday, day, month, ..) = ds3231::get_date(DS3231_ADDR, i2c);
+        });
+
+        // Calculate new values and account for DST
+        let hour = if ds3231::in_dst(weekday, day, month, hour) { (hour + 1) % 12 } else { hour % 12 };
+        let hour = if hour == 0 { 12 } else { hour };
+
+        // Lower priority task requires a critical section to access shared peripheral
+        clock.lock(|clock| {
+            // Trigger the processing of a new time value
+            clock.rtc_tick(second, minute, hour);
+        });
+
+        // Start the refresh timer to update the display
+        // Lower priority task requires a critical section to access shared peripheral
+        refresh_timer.lock(|refresh_timer| {
+            refresh_timer.listen(Event::TimeOut);
+        });
+        
+        // Clear the interrupt flag for the timer
+        rtc_int.clear_interrupt_pending_bit();
+    }
+
+    // Interrupt handler for fault interrupt from USB monitor (TUSB322)
+    // Configured as priority 4 for highest priority (pre-empts all other interrupts)
+    #[task(binds = EXTI3, priority = 4, resources = [fault_int])]
+    fn fault_interrupt(ctx: fault_interrupt::Context) {
+        let fault_interrupt::Resources {fault_int} = ctx.resources;
+
+        if fault_int.check_interrupt() {
+            fault_int.clear_interrupt_pending_bit();
+            panic!();
+        }
+    }
+
+    // Interrupt handler for internal timer that drives display refresh rate
+    #[task(binds = TIM2, priority = 3, resources = [clock, i2c, refresh_timer])]
+    fn refresh_interrupt(ctx: refresh_interrupt::Context) {
+        let refresh_interrupt::Resources {clock, refresh_timer, i2c} = ctx.resources;
+
+        // Compute updates for non-static digits
+        let updated = clock.fps_tick();
+
+        // Write new values if values have changed, otherwise disable the refresh timer
+        if updated {
+            clock.write_i2c(i2c);
+            refresh_timer.clear_interrupt(Event::TimeOut);
+        } else {
+            refresh_timer.unlisten(Event::TimeOut);
+        }
+    }
+
+    // Interrupt handler for internal timer that drives individual digits within a cycle sequence
+    #[task(binds = TIM7, priority = 2, resources = [clock, refresh_timer, cycle_timer])]
+    fn cycle_interrupt(ctx: cycle_interrupt::Context) {
+        let cycle_interrupt::Resources {mut clock, mut refresh_timer, cycle_timer} = ctx.resources;
+        let mut cycle_ended = true;
+
+        // Lower priority task requires a critical section to access shared data
+        clock.lock(|clock| {
+            cycle_ended = clock.cycle_tick();
+        });
+
+        // Trigger the next step in the cycling sequence
+        if cycle_ended {
+            cycle_timer.unlisten(Event::TimeOut);
+        } else {
+            cycle_timer.clear_interrupt(Event::TimeOut);
+        }
+
+        // Start the refresh timer to update the display
+        // Lower priority task requires a critical section to access shared peripheral
+        refresh_timer.lock(|refresh_timer| {
+            refresh_timer.listen(Event::TimeOut);
+        });
+    }
+
+    // Trigger the start of a new cycle sequence
+    #[task(resources = [clock, cycle_timer], priority = 2, capacity = 4)]
+    fn start_cycle(ctx: start_cycle::Context, tube: usize) {
+        let start_cycle::Resources {mut clock, cycle_timer} = ctx.resources;
+
+        // Lower priority task requires a critical section to access shared data
+        clock.lock(|clock| {
+            // Trigger the start of a cycling sequence
+            clock.cycle_start(tube);
+        });
+
+        // Start the timer to cycle through individual digits
+        cycle_timer.listen(Event::TimeOut);
+    }
+
+    // RTIC requires that unused interrupts are declared in an extern block when
+    // using software tasks; these free interrupts will be used to dispatch the
+    // software tasks.
+    extern "C" {
+        fn TSC();
+        fn LCD();
+        fn AES();
+    }
+};
+
+// Helper function to set onboard LED state
+fn set_fault_led(state: State) {
+    free(|cs| {
+        let mut led_ref = FAULT_LED.borrow(cs).borrow_mut();
+        if let Some(ref mut led) = led_ref.deref_mut() {
+            match state {
+                State::High => led.set_high().unwrap(),
+                State::Low => led.set_low().unwrap(),
+            };
+        }
+    });
+}
+
+// Custom panic handler
+#[panic_handler]
+#[cfg(not(test))]
+fn panic(_info: &PanicInfo) -> ! {
+    set_fault_led(State::High);
+    loop {
+        continue;
+    }
+}

+ 13 - 116
Nixie_Firmware_Rust/src/nixie.rs

@@ -1,15 +1,11 @@
-use core::{cell::RefCell, ops::DerefMut};
-
-use cortex_m::interrupt::{free, Mutex};
 use stm32l4xx_hal::{
     prelude::{
         _embedded_hal_blocking_i2c_Read, _embedded_hal_blocking_i2c_Write,
         _embedded_hal_blocking_i2c_WriteRead,
     },
-    timer::Event,
 };
 
-use crate::{ds3231, pca9685};
+use crate::pca9685;
 
 pub const DS3231_ADDR: u8 = 0x68;
 pub const TUSB322_ADDR: u8 = 0x47;
@@ -208,9 +204,7 @@ impl Tube {
     }
 }
 
-static CLOCK: Mutex<RefCell<Clock>> = Mutex::new(RefCell::new(Clock::default()));
-
-struct Clock {
+pub struct Clock {
     tubes: [Tube; NUM_TUBES],
     dot: Digit,
     minute: Option<u32>,
@@ -218,7 +212,7 @@ struct Clock {
 }
 
 impl Clock {
-    const fn default() -> Self {
+    pub const fn default() -> Self {
         const TUBE_INIT: Tube = Tube::default();
         Self {
             tubes: [TUBE_INIT; NUM_TUBES],
@@ -287,15 +281,6 @@ impl Clock {
         // Store the last set value for the next update
         self.hour = Some(hour);
         self.minute = Some(minute);
-
-        // Start the display refresh timer to update the display
-        #[cfg(not(test))]
-        free(|cs| {
-            let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
-            if let Some(ref mut timer) = timer_ref.deref_mut() {
-                timer.listen(Event::TimeOut);
-            }
-        });
     }
 
     // Updates the display with values due to fade in/out
@@ -405,17 +390,19 @@ impl Clock {
             }
         });
 
-        #[cfg(not(test))]
-        free(|cs| {
-            let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
-            if let Some(ref mut timer) = timer_ref.deref_mut() {
-                timer.listen(Event::TimeOut);
-            }
-        });
-
         cycle_ended
     }
 
+    // Start cycling sequence for the given tube to prevent long term damage from cathode poisoning
+    pub fn cycle_start(&mut self, tube: usize) {
+        self.tubes[tube].cycle = Some(CycleSettings {
+            last_digit: self.tubes[tube].last_digit,
+            next_digit: 0,
+            iteration: CYCLE_ITERATIONS,
+            last_fade_duration: DIGIT_FADE_DURATION_MS,
+        });
+    }
+
     // Writes updated PWM values to each PCA9685 
     pub fn write_i2c<T>(&mut self, i2c: &mut T)
     where
@@ -532,96 +519,6 @@ impl Clock {
     }
 }
 
-// Periodically called to update the display with a new time
-pub fn rtc_interrupt<T>(i2c: &mut T)
-where
-    T: _embedded_hal_blocking_i2c_WriteRead
-        + _embedded_hal_blocking_i2c_Read
-        + _embedded_hal_blocking_i2c_Write,
-{
-    // Read new time from DS3231
-    let (second, minute, hour) = ds3231::get_time(DS3231_ADDR, i2c);
-    let (weekday, day, month, _, _) = ds3231::get_date(DS3231_ADDR, i2c);
-
-    // Calculate new values and account for DST
-    let hour = if ds3231::in_dst(weekday, day, month, hour) { (hour + 1) % 12 } else { hour % 12 };
-    let hour = if hour == 0 { 12 } else { hour };
-
-    free(|cs| {
-        let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
-        let clock = clock_ref.deref_mut();
-        clock.rtc_tick(second, minute, hour);
-    });
-}
-
-// Periodically called to trigger a display refresh
-pub fn fps_interrupt<T>(i2c: &mut T)
-where
-    T: _embedded_hal_blocking_i2c_WriteRead
-        + _embedded_hal_blocking_i2c_Read
-        + _embedded_hal_blocking_i2c_Write,
-{
-    free(|cs| {
-        let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
-        let clock = clock_ref.deref_mut();
-
-        // Compute updates for non-static digits
-        let updated = clock.fps_tick();
-
-        // Write new values if values have changed, otherwise disable the refresh timer
-        if updated {
-            clock.write_i2c(i2c);
-            free(|cs| {
-                let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
-                if let Some(ref mut timer) = timer_ref.deref_mut() {
-                    timer.clear_interrupt(Event::TimeOut);
-                }
-            })
-        } else {
-            free(|cs| {
-                let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
-                if let Some(ref mut timer) = timer_ref.deref_mut() {
-                    timer.unlisten(Event::TimeOut);
-                }
-            })
-        }
-    });
-}
-
-// Periodically called during a cycle sequence to increment through digits
-// Returns true if the cycle sequence has completed
-pub fn cycle_interrupt() -> bool {
-    free(|cs| {
-        let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
-        let clock = clock_ref.deref_mut();
-
-        clock.cycle_tick()
-    })
-}
-
-// Start cycling sequence for the given tube to prevent long term damage from cathode poisoning
-pub fn cycle_start(tube: usize) {
-    free(|cs| {
-        let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
-        let clock = clock_ref.deref_mut();
-
-        clock.tubes[tube].cycle = Some(CycleSettings {
-            last_digit: clock.tubes[tube].last_digit,
-            next_digit: 0,
-            iteration: CYCLE_ITERATIONS,
-            last_fade_duration: DIGIT_FADE_DURATION_MS,
-        });
-    });
-
-    // Start the timer to cycle through individual digits
-    free(|cs| {
-        let mut cycle_timer_ref = super::CYCLE_TIMER.borrow(cs).borrow_mut();
-        if let Some(ref mut cycle_timer) = cycle_timer_ref.deref_mut() {
-            cycle_timer.listen(Event::TimeOut);
-        }
-    });
-}
-
 #[cfg(test)]
 mod test {
     use super::*;