Skip to content

Commit

Permalink
fix bugs from first test
Browse files Browse the repository at this point in the history
  • Loading branch information
vk2seb committed Nov 5, 2023
1 parent 9ac6d22 commit 8a12be7
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions firmware/litex-fw/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const SCOPE_SAMPLES: usize = 256;
const N_CHANNELS: usize = 4;
const BUF_SZ_WORDS: usize = 512;
const BUF_SZ_SAMPLES: usize = BUF_SZ_WORDS * 2;
const TICK_MS: u32 = 5;

// MUST be aligned to 4-byte (word) boundary for RV32. These buffers are directly
// accessed by DMA that iterates across words!.
Expand Down Expand Up @@ -217,7 +218,7 @@ impl State {

self.breathe.tick();

self.encoder.update_ticks(uptime_ms);
self.encoder.update_ticks(TICK_MS);

if self.encoder.pending_short_press() {
opts.toggle_modify();
Expand Down Expand Up @@ -454,7 +455,7 @@ impl Encoder {
}
}

fn update_ticks(&mut self, uptime_ms: u32) {
fn update_ticks(&mut self, ms_per_tick: u32) {
let enc_now: u32 = self.encoder.csr_state.read().bits() >> 2;
let mut enc_delta: i32 = (enc_now as i32) - (self.enc_last as i32);
if enc_delta > 10 {
Expand All @@ -466,7 +467,7 @@ impl Encoder {
self.enc_last = enc_now;

if self.button.in_.read().bits() != 0 {
self.btn_held_ms += uptime_ms;
self.btn_held_ms += ms_per_tick;
} else {
if self.btn_held_ms > 0 {
self.short_press = true;
Expand All @@ -489,7 +490,7 @@ impl Encoder {

fn pending_long_press(&mut self) -> bool {
let result = self.long_press;
self.short_press = false;
self.long_press = false;
result
}

Expand Down Expand Up @@ -625,7 +626,7 @@ fn main() -> ! {

let character_style = MonoTextStyle::new(&FONT_5X7, Gray4::WHITE);

timer.set_periodic_event(5); // 5ms tick
timer.set_periodic_event(TICK_MS);

let mut spi_dma = SpiDma::new(peripherals.SPI_DMA, pac::OLED_SPI::PTR);

Expand All @@ -642,12 +643,8 @@ fn main() -> ! {

// Enable machine external interrupts (basically everything added on by LiteX).
riscv::register::mie::set_mext();

// Finally enable interrupts
riscv::interrupt::enable();
}


let thin_stroke = PrimitiveStyle::with_stroke(Gray4::WHITE, 1);

let uart_midi = UartMidi::new(peripherals.UART_MIDI);
Expand All @@ -669,6 +666,13 @@ fn main() -> ! {
scope.register(Interrupt::DMA_ROUTER0, dma_router0);
scope.register(Interrupt::TIMER0, timer0);

unsafe {
// WARN: Don't do this before IRQs are registered for this scope,
// otherwise you'll hang forever :)
// Finally enable interrupts
riscv::interrupt::enable();
}

loop {

trace_main.start(&timer);
Expand Down

0 comments on commit 8a12be7

Please sign in to comment.