remove the chip select gpio-wait hack

We use the txrx call instead, which does not signal a completion
until the rx transaction is finished (which is after the final
bit is received of a pending SPI transaction).
This commit is contained in:
bunnie 2024-10-04 22:51:36 +08:00
parent 683aa63616
commit d21789beff

View file

@ -1,7 +1,7 @@
// `Command` vendored from https://github.com/ithinuel/sh1107-rs/tree/main // `Command` vendored from https://github.com/ithinuel/sh1107-rs/tree/main
use crate::{ use crate::{
ifram::IframRange, ifram::IframRange,
iox::{IoGpio, IoSetup, IoxPort, IoxValue}, iox::{IoGpio, IoSetup, IoxPort},
udma::{PeriphId, Spim, SpimClkPha, SpimClkPol, SpimCs, UdmaGlobalConfig}, udma::{PeriphId, Spim, SpimClkPha, SpimClkPol, SpimCs, UdmaGlobalConfig},
}; };
@ -150,16 +150,15 @@ pub struct Oled128x128<'a> {
active_buffer: BufferState, active_buffer: BufferState,
cd_port: IoxPort, cd_port: IoxPort,
cd_pin: u8, cd_pin: u8,
cs_pin: u8,
iox: &'a dyn IoGpio, iox: &'a dyn IoGpio,
} }
impl<'a> Oled128x128<'a> { impl<'a> Oled128x128<'a> {
pub fn new<T>(iox: &'a T, udma_global: &'a dyn UdmaGlobalConfig) -> Self pub fn new<T>(perclk_freq: u32, iox: &'a T, udma_global: &'a dyn UdmaGlobalConfig) -> Self
where where
T: IoSetup + IoGpio, T: IoSetup + IoGpio,
{ {
let (channel, cd_port, cd_pin, cs_pin) = crate::board::setup_display_pins(iox); let (channel, cd_port, cd_pin, _cs_pin) = crate::board::setup_display_pins(iox);
udma_global.clock(PeriphId::from(channel), true); udma_global.clock(PeriphId::from(channel), true);
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
let ifram_vaddr = crate::board::DISPLAY_IFRAM_ADDR; let ifram_vaddr = crate::board::DISPLAY_IFRAM_ADDR;
@ -180,7 +179,7 @@ impl<'a> Oled128x128<'a> {
Spim::new_with_ifram( Spim::new_with_ifram(
channel, channel,
2_000_000, 2_000_000,
50_000_000, perclk_freq / 2,
SpimClkPol::LeadingEdgeRise, SpimClkPol::LeadingEdgeRise,
SpimClkPha::CaptureOnLeading, SpimClkPha::CaptureOnLeading,
SpimCs::Cs0, SpimCs::Cs0,
@ -195,8 +194,9 @@ impl<'a> Oled128x128<'a> {
// an IFRAM range large enough to accommodate that. However, because we have // 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 // to round up any allocations to a full page length, we end up with extra
// unused space. // unused space.
4096 + 256, 4096 + 256, // the 256 is for direct commands
0, // re-use the Tx buffer for Rx in this implementation // the 2048 is for dummy-Rx so we can properly measure when the transaction is done
2048,
None, None,
None, None,
// Note: the IFRAM needs to be 16 bytes longer than the data range to accommodate // Note: the IFRAM needs to be 16 bytes longer than the data range to accommodate
@ -219,7 +219,6 @@ impl<'a> Oled128x128<'a> {
active_buffer: BufferState::A, active_buffer: BufferState::A,
cd_port, cd_port,
cd_pin, cd_pin,
cs_pin,
iox, iox,
} }
} }
@ -236,11 +235,6 @@ impl<'a> Oled128x128<'a> {
pub fn buffer(&self) -> &[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 /// Transfers the back buffer
pub fn draw(&mut self) { pub fn draw(&mut self) {
// this must be opposite of what `buffer` / `buffer_mut` returns // this must be opposite of what `buffer` / `buffer_mut` returns
@ -254,25 +248,24 @@ impl<'a> Oled128x128<'a> {
// the transaction is done before the data is done transmitting, and we have to // 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 // toggle set_data() only after the physical transaction is done, not after the
// the last UDMA action has been queued. // the last UDMA action has been queued.
while self.cs_active() {}
self.send_command(Command::SetPageAddress(page as u8).encode()); self.send_command(Command::SetPageAddress(page as u8).encode());
while self.cs_active() {}
self.send_command(Command::SetColumnAddress(0).encode()); self.send_command(Command::SetColumnAddress(0).encode());
while self.cs_active() {}
// wait for commands to finish before toggling set_data // wait for commands to finish before toggling set_data
// self.spim.tx_data_await(false); // self.spim.tx_data_await(false);
// crate::println!("Send page {}, offset {:x}", page, buffer_start + page * chunk_size); // crate::println!("Send page {}, offset {:x}", page, buffer_start + page * chunk_size);
self.set_data(); self.set_data();
// safety: data is already copied into the DMA buffer. size & len are in bounds. // safety: data is already copied into the DMA buffer. size & len are in bounds.
unsafe { unsafe {
self.spim.tx_data_async_from_parts::<u8>( self.spim
buffer_start + page * chunk_size, .txrx_data_async_from_parts::<u8>(
chunk_size, buffer_start + page * chunk_size,
true, chunk_size,
false, true,
); false,
)
.expect("Couldn't initiate oled data transfer");
} }
self.spim.tx_data_await(false); self.spim.txrx_await(false).unwrap();
} }
} }
@ -294,9 +287,11 @@ impl<'a> Oled128x128<'a> {
// crate::println!(""); // crate::println!("");
// safety: data is already copied into the DMA buffer. size & len are in bounds. // safety: data is already copied into the DMA buffer. size & len are in bounds.
unsafe { unsafe {
self.spim.tx_data_async_from_parts::<u8>(total_buf_len, len, true, false); self.spim
.txrx_data_async_from_parts::<u8>(total_buf_len, len, true, false)
.expect("Couldn't initiate oled command");
} }
self.spim.tx_data_await(false); self.spim.txrx_await(false).unwrap();
} }
pub fn init(&mut self) { pub fn init(&mut self) {