consolidate spi pin setup

This commit is contained in:
bunnie 2024-10-02 00:23:48 +08:00
parent 20dd9b2c14
commit 2e086f4152
3 changed files with 120 additions and 217 deletions

View file

@ -373,118 +373,9 @@ pub fn early_init() {
// setup the I/O pins
let mut iox = Iox::new(utralib::generated::HW_IOX_BASE as *mut u32);
let mut udma_global = GlobalConfig::new(utralib::generated::HW_UDMA_CTRL_BASE as *mut u32);
#[cfg(feature = "spi-alt-channel")]
let channel = {
// JQSPI1
// SPIM_CLK_A[0]
setup_port(
&mut iox,
IoxPort::PD,
4,
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive4mA),
Some(IoxEnable::Enable),
None,
None,
);
// SPIM_SD[0-3]_A[0]
for i in 0..3 {
setup_port(
&mut iox,
IoxPort::PD,
i,
Some(IoxFunction::AF1),
None,
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
None,
None,
);
}
// SPIM_CSN0_A[0]
setup_port(
&mut iox,
IoxPort::PD,
5,
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
None,
None,
);
// SPIM_CSN0_A[1]
setup_port(
&mut iox,
IoxPort::PD,
6,
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
None,
None,
);
udma_global.clock_on(PeriphId::Spim0); // JQSPI1
SpimChannel::Channel0
};
#[cfg(not(feature = "spi-alt-channel"))]
let channel = {
// JPC7_13
// SPIM_CLK_A[1]
setup_port(
&mut iox,
IoxPort::PC,
11,
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
None,
None,
);
// SPIM_SD[0-3]_A[1]
for i in 7..11 {
setup_port(
&mut iox,
IoxPort::PC,
i,
Some(IoxFunction::AF1),
None,
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Disable),
None,
None,
);
}
// SPIM_CSN0_A[1]
setup_port(
&mut iox,
IoxPort::PC,
12,
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Disable),
None,
None,
);
// SPIM_CSN0_A[1]
setup_port(
&mut iox,
IoxPort::PC,
13,
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Disable),
None,
None,
);
udma_global.clock_on(PeriphId::Spim1); // JPC7_13
SpimChannel::Channel1
};
let channel = cramium_hal::board::setup_memory_pins(&iox);
udma_global.clock_off(PeriphId::from(channel));
crate::println!("Configuring SPI channel: {:?}", channel);
// safety: this is safe because clocks have been set up
let mut flash_spim = unsafe {

View file

@ -0,0 +1,115 @@
// Types from https://github.com/ithinuel/sh1107-rs/tree/main
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DisplayState {
Off,
On,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Direction {
Normal,
Inverted,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum AddressMode {
Page,
Column,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DisplayMode {
BlackOnWhite,
WhiteOnBlack,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Command {
SetColumnAddress(u8),
SetAddressMode(AddressMode),
SetDisplayMode(DisplayMode),
ForceEntireDisplay(bool),
SetClkDividerOscFrequency {
divider: u8,
osc_freq_ratio: i8,
},
SetMultiplexRatio(u8),
SetStartLine(u8),
SetSegmentReMap(bool),
SetCOMScanDirection(Direction),
SetDisplayOffset(u8),
SetContrastControl(u8),
/// Set Charge & Discharge period
SetChargePeriods {
precharge: Option<u8>,
discharge: u8,
},
SetVCOMHDeselectLevel(u8),
SetDCDCSettings(u8),
DisplayOnOff(DisplayState),
SetPageAddress(u8),
StartReadModifyWrite,
EndReadModifyWrite,
Nop,
}
impl Command {
fn encode(self) -> impl Iterator<Item = u8> {
use either::Either::*;
match self {
Self::SetColumnAddress(addr) => {
assert!(addr < 128);
Right([addr & 0xF, 0x10 | ((addr & 0x70) >> 4)])
}
Self::SetAddressMode(mode) => Left(0x20 | if let AddressMode::Page = mode { 0 } else { 1 }),
Self::SetContrastControl(contrast) => Right([0x81, contrast]),
Self::SetSegmentReMap(is_remapped) => Left(0xA0 | if is_remapped { 1 } else { 0 }),
Self::SetMultiplexRatio(ratio) => {
assert!((1..=128).contains(&ratio));
Right([0xA8, ratio - 1])
}
Self::ForceEntireDisplay(state) => Left(0xA4 | if state { 1 } else { 0 }),
Self::SetDisplayMode(mode) => {
Left(0xA6 | if let DisplayMode::WhiteOnBlack = mode { 1 } else { 0 })
}
Self::SetDisplayOffset(offset) => Right([0xD3, offset & 0x7F]),
Self::SetDCDCSettings(cfg) => Right([0xAD, 0x80 | (cfg & 0x0F)]),
Self::DisplayOnOff(state) => Left(0xAE | if let DisplayState::On = state { 1 } else { 0 }),
Self::SetPageAddress(addr) => {
assert!(addr < 16);
Left(0xB0 | (addr & 0x0F))
}
Self::SetCOMScanDirection(dir) => Left(0xC0 | if let Direction::Normal = dir { 0 } else { 0x08 }),
Self::SetClkDividerOscFrequency { divider, osc_freq_ratio } => {
assert!(osc_freq_ratio % 5 == 0, "osc_freq_ratio must be a multiple of 5.");
assert!((-25..=50).contains(&osc_freq_ratio), "osc_freq_ratio must be within [-25; 50]");
assert!((1..=16).contains(&divider), "divider must be in [1; 16]");
let osc_freq_ratio = osc_freq_ratio / 5 + 5;
Right([0xD5, ((osc_freq_ratio & 0xF) << 4) as u8 | (divider - 1)])
}
Self::SetChargePeriods { precharge, discharge } => {
let precharge = if let Some(v) = precharge {
assert!((1..=15).contains(&v));
v
} else {
0
};
assert!((1..=15).contains(&discharge));
let arg = discharge << 4 | precharge;
Right([0xD9, arg])
}
Self::SetVCOMHDeselectLevel(arg) => Right([0xDB, arg]),
Self::SetStartLine(line) => {
assert!(line < 128);
Right([0xDC, line & 0x7F])
}
Self::StartReadModifyWrite => Left(0xE0),
Self::EndReadModifyWrite => Left(0xEE),
Self::Nop => Left(0xE3),
}
.map_left(|v| [v])
.into_iter()
}
}

View file

@ -2,7 +2,6 @@ use core::mem::size_of;
use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag};
use cramium_hal::ifram::IframRange;
use cramium_hal::iox::IoSetup;
use cramium_hal::iox::*;
use cramium_hal::sce;
use cramium_hal::udma::*;
@ -44,110 +43,8 @@ impl SwapHal {
// setup the I/O pins
let iox = Iox::new(utralib::generated::HW_IOX_BASE as *mut u32);
#[cfg(feature = "spi-alt-channel")]
let channel = {
// JQSPI1
// SPIM_CLK_A[0]
iox.setup_pin(
IoxPort::PD,
4,
Some(IoxDir::Output),
Some(IoxFunction::AF1),
None,
None,
Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive4mA),
);
// SPIM_SD[0-3]_A[0]
for i in 0..3 {
iox.setup_pin(
IoxPort::PD,
i,
None,
Some(IoxFunction::AF1),
None,
None,
Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive2mA),
);
}
// SPIM_CSN0_A[0]
iox.setup_pin(
IoxPort::PD,
5,
Some(IoxDir::Output),
Some(IoxFunction::AF1),
None,
None,
Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive2mA),
);
// SPIM_CSN0_A[1]
iox.setup_pin(
IoxPort::PD,
6,
Some(IoxDir::Output),
Some(IoxFunction::AF1),
None,
None,
Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive2mA),
);
udma_global.clock_on(PeriphId::Spim0); // JQSPI1
SpimChannel::Channel0
};
#[cfg(not(feature = "spi-alt-channel"))]
let channel = {
// JPC7_13
// SPIM_CLK_A[1]
iox.setup_pin(
IoxPort::PC,
11,
Some(IoxDir::Output),
Some(IoxFunction::AF1),
None,
None,
Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive4mA),
);
// SPIM_SD[0-3]_A[1]
for i in 7..11 {
iox.setup_pin(
IoxPort::PC,
i,
None,
Some(IoxFunction::AF1),
None,
None,
Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive2mA),
);
}
// SPIM_CSN0_A[1]
iox.setup_pin(
IoxPort::PC,
12,
Some(IoxDir::Output),
Some(IoxFunction::AF1),
None,
None,
Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive2mA),
);
// SPIM_CSN0_A[1]
iox.setup_pin(
IoxPort::PC,
13,
Some(IoxDir::Output),
Some(IoxFunction::AF1),
None,
None,
Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive2mA),
);
udma_global.clock_on(PeriphId::Spim1); // JPC7_13
SpimChannel::Channel1
};
let channel = cramium_hal::board::setup_memory_pins(&iox);
udma_global.clock_off(PeriphId::from(channel));
// safety: this is safe because clocks have been set up
let mut flash_spim = unsafe {