diff --git a/libs/cramium-hal/src/sh1107.rs b/libs/cramium-hal/src/sh1107.rs index 08e0d1dd8..bb3b6d043 100644 --- a/libs/cramium-hal/src/sh1107.rs +++ b/libs/cramium-hal/src/sh1107.rs @@ -1,4 +1,13 @@ -// Types from https://github.com/ithinuel/sh1107-rs/tree/main +// `Command` vendored from https://github.com/ithinuel/sh1107-rs/tree/main +use crate::{ + ifram::IframRange, + iox::{IoGpio, IoSetup, IoxPort, IoxValue}, + udma::{PeriphId, Spim, SpimClkPha, SpimClkPol, SpimCs, UdmaGlobalConfig}, +}; + +pub const COLUMN: u8 = 128; +pub const ROW: u8 = 128; +pub const PAGE: u8 = ROW / 8; #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum DisplayState { @@ -113,3 +122,219 @@ impl Command { .into_iter() } } + +#[repr(usize)] +#[derive(Eq, PartialEq, Copy, Clone)] +enum BufferState { + A = 0, + B = 2048, +} +impl BufferState { + pub fn swap(self) -> Self { if self == BufferState::A { BufferState::B } else { BufferState::A } } + + pub fn as_index(&self) -> usize { + match self { + BufferState::A => 0, + BufferState::B => 1, + } + } +} + +pub struct Oled128x128<'a> { + spim: Spim, + // front and back buffers + buffers: [&'static mut [u8]; 2], + // length of the sideband memory region for queuing commands to the OLED. Must be allocated + // immediately after the total frame buffer length + pub sideband_len: usize, + active_buffer: BufferState, + cd_port: IoxPort, + cd_pin: u8, + cs_pin: u8, + iox: &'a dyn IoGpio, +} + +impl<'a> Oled128x128<'a> { + pub fn new(iox: &'a T, udma_global: &'a dyn UdmaGlobalConfig) -> Self + where + T: IoSetup + IoGpio, + { + let (channel, cd_port, cd_pin, cs_pin) = crate::board::setup_display_pins(iox); + udma_global.clock(PeriphId::from(channel), true); + #[cfg(not(feature = "std"))] + let ifram_vaddr = crate::board::DISPLAY_IFRAM_ADDR; + #[cfg(feature = "std")] + let ifram_vaddr = { + xous::map_memory( + xous::MemoryAddress::new(crate::board::DISPLAY_IFRAM_ADDR), + None, + 4096 * 2, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map window for display IFRAM") + .as_ptr() as usize + }; + // safety: safe because the udma clock is turned on, and IFRAM is pulling from + // statically allocated bank + let mut spim = unsafe { + Spim::new_with_ifram( + channel, + 2_000_000, + 50_000_000, + SpimClkPol::LeadingEdgeRise, + SpimClkPha::CaptureOnLeading, + SpimCs::Cs0, + 0, + 0, + None, + // 2x buffers reserved: (128 * 128 / 8) * 2 = 4096 + // Add 256 for display commands. + // Internally, an extra 16 is added for UDMA SPIM commands (these are commands + // to the SPIM hardware itself, not transmitted to the display). + // The +16 is not reported here because it's out of band, but we need to allocate + // an IFRAM range large enough to accommodate that. However, because we have + // to round up any allocations to a full page length, we end up with extra + // unused space. + 4096 + 256, + 0, // re-use the Tx buffer for Rx in this implementation + None, + None, + // Note: the IFRAM needs to be 16 bytes longer than the data range to accommodate + // command sending. But because we have to round up to a whole page, we end up wasting 4080 + // bytes. + IframRange::from_raw_parts(crate::board::DISPLAY_IFRAM_ADDR, ifram_vaddr, 4096 * 2), + ) + }; + spim.set_endianness(crate::udma::SpimEndian::MsbFirst); + Self { + spim, + // safety: this is safe because these ranges are in fact allocated, and all values can be + // represented. They have a static lifetime because they are mapped to hardware. There is + // in fact an unsafe front/back buffer contention, but that's the whole point of this routine, + // to safely manage that. + buffers: [unsafe { core::slice::from_raw_parts_mut(ifram_vaddr as *mut u8, 2048) }, unsafe { + core::slice::from_raw_parts_mut((ifram_vaddr + 2048) as *mut u8, 2048) + }], + sideband_len: 256, + active_buffer: BufferState::A, + cd_port, + cd_pin, + cs_pin, + iox, + } + } + + fn set_data(&self) { self.iox.set_gpio_pin_value(self.cd_port, self.cd_pin, crate::iox::IoxValue::High); } + + fn set_command(&self) { + self.iox.set_gpio_pin_value(self.cd_port, self.cd_pin, crate::iox::IoxValue::Low); + } + + pub fn buffer_swap(&mut self) { self.active_buffer = self.active_buffer.swap(); } + + pub fn buffer_mut(&mut self) -> &mut [u8] { self.buffers[self.active_buffer.as_index()] } + + pub fn buffer(&self) -> &[u8] { self.buffers[self.active_buffer.as_index()] } + + // This is needed because the UDMA block will tell you the transaction is done the moment its DMA + // is finished. There is no way, afaik, to tell if a transaction has physically finished on the pins + // without reading back the CS pin. + fn cs_active(&self) -> bool { self.iox.get_gpio_pin_value(self.cd_port, self.cs_pin) == IoxValue::Low } + + /// Transfers the back buffer + pub fn draw(&mut self) { + // this must be opposite of what `buffer` / `buffer_mut` returns + let buffer_start = self.active_buffer.swap() as usize; + let chunk_size = 128; + let chunks = self.buffer().len() / chunk_size; + // we don't do this with an iterator because it involves an immutable borrow of + // `buffer`, which prevents us from doing anything with the interface inside the loop. + for page in 0..chunks { + // The cs_active() waits are necessary because the UDMA block will eagerly report + // the transaction is done before the data is done transmitting, and we have to + // toggle set_data() only after the physical transaction is done, not after the + // the last UDMA action has been queued. + while self.cs_active() {} + self.send_command(Command::SetPageAddress(page as u8).encode()); + while self.cs_active() {} + self.send_command(Command::SetColumnAddress(0).encode()); + while self.cs_active() {} + // wait for commands to finish before toggling set_data + // self.spim.tx_data_await(false); + // crate::println!("Send page {}, offset {:x}", page, buffer_start + page * chunk_size); + self.set_data(); + // safety: data is already copied into the DMA buffer. size & len are in bounds. + unsafe { + self.spim.tx_data_async_from_parts::( + buffer_start + page * chunk_size, + chunk_size, + true, + false, + ); + } + self.spim.tx_data_await(false); + } + } + + pub fn send_command<'b, U>(&'b mut self, cmd: U) + where + U: IntoIterator + 'b, + { + self.set_command(); + let total_buf_len = self.buffers.iter().map(|x| x.len()).sum(); + let mut len = 0; // track the full length of the iterator + // emplace the command in the sideband area, which is after both frame buffers + // crate::print!("cmd: "); + let ifram_raw: &mut [u8] = self.spim.tx_buf_mut(); + for (src, dst) in cmd.into_iter().zip(ifram_raw[total_buf_len..].iter_mut()) { + // crate::print!("{:x} ", src); + *dst = src; + len += 1; + } + // crate::println!(""); + // safety: data is already copied into the DMA buffer. size & len are in bounds. + unsafe { + self.spim.tx_data_async_from_parts::(total_buf_len, len, true, false); + } + self.spim.tx_data_await(false); + } + + pub fn init(&mut self) { + use Command::*; + let init_sequence = [ + DisplayOnOff(DisplayState::Off), + SetDCDCSettings(0x0), + SetStartLine(0), + SetDisplayOffset(0), + SetContrastControl(0x4f), + SetAddressMode(AddressMode::Page), + SetSegmentReMap(true), + SetCOMScanDirection(Direction::Normal), + SetMultiplexRatio(128), + SetClkDividerOscFrequency { divider: 1, osc_freq_ratio: 5 }, + SetChargePeriods { precharge: Some(2), discharge: 2 }, + SetVCOMHDeselectLevel(0x35), + SetPageAddress(0), + ForceEntireDisplay(false), + SetDisplayMode(DisplayMode::BlackOnWhite), + DisplayOnOff(DisplayState::On), + ]; + + for command in init_sequence { + let bytes = command.encode(); + self.send_command(bytes); + } + } + + pub fn put_pixel(&mut self, x: u8, y: u8, on: bool) { + if x > COLUMN || y > ROW { + return; + } + let buffer = self.buffer_mut(); + if on { + buffer[x as usize + (y as usize / 8) * COLUMN as usize] |= 1 << (y % 8); + } else { + buffer[x as usize + (y as usize / 8) * COLUMN as usize] &= !(1 << (y % 8)); + } + } +}