check in sampler module

This commit is contained in:
David Lenfesty 2023-06-17 15:57:46 +00:00
parent 33465a27ac
commit a132f2e1f1

64
firmware/src/sampler.rs Normal file
View File

@ -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)
}