From a132f2e1f159f83e9f534afbcfa036610373b7f6 Mon Sep 17 00:00:00 2001 From: David Lenfesty Date: Sat, 17 Jun 2023 15:57:46 +0000 Subject: [PATCH] check in sampler module --- firmware/src/sampler.rs | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 firmware/src/sampler.rs diff --git a/firmware/src/sampler.rs b/firmware/src/sampler.rs new file mode 100644 index 0000000..1e21c9f --- /dev/null +++ b/firmware/src/sampler.rs @@ -0,0 +1,64 @@ +//! Driver for custom sampler peripheral + +use crate::{write_reg, read_reg}; + +const BASE: u32 = 0x8040_0000; + +const REG_CR: u32 = 0x00; +const REG_SR: u32 = 0x04; +const REG_TRIGGER_RUN_LEN: u32 = 0x08; +const REG_THRESH_VALUE: u32 = 0x0c; +const REG_THRESH_PERIOD: u32 = 0x10; +const REG_DECAY_VALUE: u32 = 0x14; +const REG_DECAY_PERIOD: u32 = 0x18; + +const REG_BUFFER_LEN_OFFSET: u32 = 0x100 << 2; +const SAMPLE_MEM_LEN: u32 = 0x8000 << 2; + +pub fn start_sampling() { + unsafe { + write_reg(BASE + REG_CR, 0x01); + } +} + +pub fn stop_sampling() { + unsafe { + write_reg(BASE + REG_CR, 0x02); + } +} + +pub fn clear_buffers() { + unsafe { + write_reg(BASE + REG_CR, 0x04); + } +} + +#[derive(Clone, Copy, Debug)] +pub struct Status { + pub capture_complete: bool, + pub sampling: bool, +} + +pub fn read_status() -> Status { + unsafe { + let status: u32 = read_reg(BASE + REG_SR); + + Status { + capture_complete: status & 0x01 != 0, + sampling: status & 0x02 != 0, + } + } +} + +/// Get a slice of the sample buffer memory. Returns u32 because each sample is +/// aligned to 32 bits, actual samples are 9 bits of real data. +/// +/// SAFETY: This buffer can get cleared, so make sure you only read from it while +/// it's valid, and also make sure the sampler index is not larger than the number +/// of samplers (0-indexed). +pub unsafe fn get_sample_buffer(sampler: u8) -> &'static [u32] { + let len: u32 = read_reg(BASE + REG_BUFFER_LEN_OFFSET + (sampler as u32 * 4)); + let addr = BASE + SAMPLE_MEM_LEN * (sampler as u32 + 1); + + core::slice::from_raw_parts(addr as *const u32, len as usize) +} \ No newline at end of file