refactor all hardware init in drivers
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
use alloc::boxed::Box;
|
||||
use embedded_hal_bus::util::AtomicCell;
|
||||
use esp_hal::gpio::interconnect::PeripheralInput;
|
||||
use esp_hal::i2c::master::Config;
|
||||
use esp_hal::{gpio::interconnect::PeripheralOutput, i2c::master::I2c};
|
||||
|
||||
type InnerI2cBus = esp_hal::i2c::master::I2c<'static, esp_hal::Blocking>;
|
||||
pub type I2cBus = &'static AtomicCell<InnerI2cBus>;
|
||||
|
||||
pub struct I2cBusPinConfiguration<I2C, SCL, SDA> {
|
||||
pub i2c_peripheral: I2C,
|
||||
pub scl: SCL,
|
||||
pub sda: SDA,
|
||||
}
|
||||
|
||||
impl<I2C, SCL, SDA> I2cBusPinConfiguration<I2C, SCL, SDA>
|
||||
where
|
||||
I2C: esp_hal::i2c::master::Instance + 'static,
|
||||
SCL: PeripheralOutput<'static> + PeripheralInput<'static>,
|
||||
SDA: PeripheralOutput<'static> + PeripheralInput<'static>,
|
||||
{
|
||||
pub fn build(self) -> I2cBus {
|
||||
let config = Config::default().with_frequency(esp_hal::time::Rate::from_khz(400));
|
||||
let i2c = match I2c::new(self.i2c_peripheral, config) {
|
||||
Ok(bus) => bus.with_sda(self.sda).with_scl(self.scl),
|
||||
Err(e) => {
|
||||
log::error!("Shared I2C bus initialization failed for GPIO17/GPIO18: {e:?}");
|
||||
panic!("Shared I2C bus initialization failed");
|
||||
}
|
||||
};
|
||||
let shared_i2c = Box::leak(Box::new(AtomicCell::new(i2c)));
|
||||
log::info!("Shared I2C bus ready on GPIO17 (SDA) and GPIO18 (SCL)");
|
||||
|
||||
shared_i2c
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
use display_interface_spi::SPIInterface;
|
||||
use embedded_graphics::draw_target::DrawTarget;
|
||||
use embedded_graphics::pixelcolor::Rgb565;
|
||||
use embedded_graphics::prelude::RgbColor;
|
||||
use embedded_hal_bus::spi::AtomicDevice;
|
||||
use embedded_hal_bus::util::AtomicCell;
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::gpio::Output;
|
||||
use ili9341::{DisplaySize240x320, Ili9341, Orientation};
|
||||
|
||||
use crate::drivers::spi_bus::{DisplaySpiBus, SharedOutput};
|
||||
|
||||
type GenericPrimaryDisplay<'a, BUS, DC, RES> =
|
||||
Ili9341<SPIInterface<AtomicDevice<'a, BUS, Output<'a>, Delay>, DC>, RES>;
|
||||
pub type PrimaryDisplay<'a> =
|
||||
GenericPrimaryDisplay<'a, DisplaySpiBus, SharedOutput<'a>, Output<'a>>;
|
||||
|
||||
pub struct PrimaryDisplayPinConfiguration<'a> {
|
||||
pub spi_bus: &'a AtomicCell<DisplaySpiBus>,
|
||||
pub cs: Output<'a>,
|
||||
pub reset: Output<'a>,
|
||||
pub dc: SharedOutput<'a>,
|
||||
}
|
||||
|
||||
impl<'a> PrimaryDisplayPinConfiguration<'a> {
|
||||
pub fn build(self) -> PrimaryDisplay<'a> {
|
||||
let spi_dev = AtomicDevice::new(self.spi_bus, self.cs, Delay::new()).unwrap();
|
||||
let iface = SPIInterface::new(spi_dev, self.dc);
|
||||
|
||||
let mut display = Ili9341::new(
|
||||
iface,
|
||||
self.reset,
|
||||
&mut Delay::new(),
|
||||
Orientation::Portrait,
|
||||
DisplaySize240x320,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
display.clear(Rgb565::BLACK).unwrap();
|
||||
display
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
use alloc::format;
|
||||
use ch1115::{Ch1115, Size128x64};
|
||||
use display_interface_spi::SPIInterface;
|
||||
use embedded_hal_bus::spi::AtomicDevice;
|
||||
use embedded_hal_bus::util::AtomicCell;
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::gpio::Output;
|
||||
|
||||
use crate::drivers::spi_bus::{DisplaySpiBus, SharedOutput, SharedReset};
|
||||
|
||||
pub type GenericSecondaryDisplay<'a, BUS, RST, DC> =
|
||||
Ch1115<SPIInterface<AtomicDevice<'a, BUS, Output<'a>, Delay>, DC>, RST, Size128x64>;
|
||||
pub type SecondaryDisplay<'a> =
|
||||
GenericSecondaryDisplay<'a, DisplaySpiBus, SharedReset<'a>, SharedOutput<'a>>;
|
||||
|
||||
pub struct SecondaryDisplayPinConfiguration<'a> {
|
||||
pub spi_bus: &'a AtomicCell<DisplaySpiBus>,
|
||||
pub cs_pins: [Output<'a>; 3],
|
||||
pub res: SharedReset<'a>,
|
||||
pub dc: SharedOutput<'a>,
|
||||
}
|
||||
|
||||
impl<'a> SecondaryDisplayPinConfiguration<'a> {
|
||||
pub fn build(self) -> [SecondaryDisplay<'a>; 3] {
|
||||
let mut displays = self.cs_pins.map(|cs| {
|
||||
let spi_dev = AtomicDevice::new(self.spi_bus, cs, Delay::new()).unwrap();
|
||||
let interface = SPIInterface::new(spi_dev, self.dc.clone());
|
||||
|
||||
Ch1115::new(interface, self.res.clone(), Size128x64)
|
||||
});
|
||||
|
||||
init_single_reset(&mut displays, self.res);
|
||||
|
||||
displays
|
||||
}
|
||||
}
|
||||
|
||||
fn init_single_reset<'a: 'b, 'b>(
|
||||
displays: impl IntoIterator<Item = &'b mut SecondaryDisplay<'a>>,
|
||||
shared_reset: SharedReset<'a>,
|
||||
) {
|
||||
let mut displays = displays.into_iter().peekable();
|
||||
|
||||
// Reset all secondaries via shared reset
|
||||
let Some(any_display) = displays.peek_mut() else {
|
||||
// no display to initialize
|
||||
return;
|
||||
};
|
||||
any_display.hard_reset(&mut Delay::new()).unwrap();
|
||||
|
||||
shared_reset.disable(true);
|
||||
|
||||
for (i, display) in displays.enumerate() {
|
||||
display
|
||||
.init(&mut Delay::new())
|
||||
.map_err(|e| format!("Failed to init secondary display {}: {e:?}", i + 1))
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
use alloc::boxed::Box;
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embedded_hal::digital::ErrorType;
|
||||
use embedded_hal::digital::OutputPin as EhOutputPin;
|
||||
use embedded_hal_bus::util::AtomicCell;
|
||||
use esp_hal::gpio::interconnect::{PeripheralInput, PeripheralOutput};
|
||||
use esp_hal::gpio::{Level, Output, OutputConfig, OutputPin};
|
||||
use esp_hal::spi::Mode;
|
||||
use esp_hal::spi::master::{Config as SpiConfig, Spi};
|
||||
use esp_hal::time::Rate;
|
||||
|
||||
use crate::drivers::primary_lcd::PrimaryDisplay;
|
||||
use crate::drivers::primary_lcd::PrimaryDisplayPinConfiguration;
|
||||
use crate::drivers::secondary_oled::SecondaryDisplay;
|
||||
use crate::drivers::secondary_oled::SecondaryDisplayPinConfiguration;
|
||||
|
||||
use core::cell::RefCell;
|
||||
use embassy_sync::blocking_mutex::Mutex as BlockingMutex;
|
||||
|
||||
pub struct SharedPin<'a, P> {
|
||||
mutex: &'a BlockingMutex<CriticalSectionRawMutex, RefCell<P>>,
|
||||
}
|
||||
|
||||
impl<'a, P> Clone for SharedPin<'a, P> {
|
||||
fn clone(&self) -> Self {
|
||||
Self { mutex: self.mutex }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, P> SharedPin<'a, P> {
|
||||
pub fn new(pin: P) -> Self {
|
||||
let mutexed = BlockingMutex::new(RefCell::new(pin));
|
||||
let mutex = Box::leak(Box::new(mutexed));
|
||||
Self { mutex }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, P: EhOutputPin> ErrorType for SharedPin<'a, P> {
|
||||
type Error = P::Error;
|
||||
}
|
||||
|
||||
impl<'a, P: EhOutputPin> EhOutputPin for SharedPin<'a, P> {
|
||||
fn set_low(&mut self) -> Result<(), Self::Error> {
|
||||
self.mutex.lock(|p| p.borrow_mut().set_low())
|
||||
}
|
||||
|
||||
fn set_high(&mut self) -> Result<(), Self::Error> {
|
||||
self.mutex.lock(|p| p.borrow_mut().set_high())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SharedResetPin<'a, P> {
|
||||
mutex: &'a BlockingMutex<CriticalSectionRawMutex, RefCell<(P, bool)>>,
|
||||
}
|
||||
|
||||
impl<'a, P> SharedResetPin<'a, P> {
|
||||
pub fn new(pin: P, disabled: bool) -> Self {
|
||||
let mutexed = BlockingMutex::new(RefCell::new((pin, disabled)));
|
||||
let mutex = Box::leak(Box::new(mutexed));
|
||||
Self { mutex }
|
||||
}
|
||||
|
||||
pub fn disable(&self, disabled: bool) {
|
||||
self.mutex.lock(|p| {
|
||||
p.borrow_mut().1 = disabled;
|
||||
});
|
||||
if disabled {
|
||||
log::debug!("Reset pin disabled (masked). No more resets now.");
|
||||
} else {
|
||||
log::debug!("Reset pin enabled (unmasked). Reset now possible.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, P> Clone for SharedResetPin<'a, P> {
|
||||
fn clone(&self) -> Self {
|
||||
Self { mutex: self.mutex }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, P: EhOutputPin> ErrorType for SharedResetPin<'a, P> {
|
||||
type Error = P::Error;
|
||||
}
|
||||
|
||||
impl<'a, P: EhOutputPin> EhOutputPin for SharedResetPin<'a, P> {
|
||||
fn set_low(&mut self) -> Result<(), Self::Error> {
|
||||
self.mutex.lock(|p| {
|
||||
let mut guard = p.borrow_mut();
|
||||
let disabled = guard.1;
|
||||
let pin = &mut guard.0;
|
||||
if !disabled { pin.set_low() } else { Ok(()) }
|
||||
})
|
||||
}
|
||||
|
||||
fn set_high(&mut self) -> Result<(), Self::Error> {
|
||||
self.mutex.lock(|p| {
|
||||
let mut guard = p.borrow_mut();
|
||||
let disabled = guard.1;
|
||||
let pin = &mut guard.0;
|
||||
if !disabled { pin.set_high() } else { Ok(()) }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub type SharedOutput<'a> = SharedPin<'a, Output<'a>>;
|
||||
pub type SharedReset<'a> = SharedResetPin<'a, Output<'a>>;
|
||||
pub type DisplaySpiBus = Spi<'static, esp_hal::Blocking>;
|
||||
|
||||
pub struct DisplayPinConfiguration<SPI, SCK, MOSI, MISO, CS0, CS1, CS2, CS3, RES1, RES2, DC> {
|
||||
pub spi_peripheral: SPI,
|
||||
pub sck: SCK,
|
||||
pub mosi: MOSI,
|
||||
pub miso: MISO,
|
||||
pub cs_primary: CS0,
|
||||
pub cs_secondary_1: CS1,
|
||||
pub cs_secondary_2: CS2,
|
||||
pub cs_secondary_3: CS3,
|
||||
pub reset_primary: RES1,
|
||||
pub reset_secondary: RES2,
|
||||
pub dc_pin: DC,
|
||||
}
|
||||
|
||||
impl<SPI, SCK, MOSI, MISO, CS0, CS1, CS2, CS3, RES1, RES2, DC>
|
||||
DisplayPinConfiguration<SPI, SCK, MOSI, MISO, CS0, CS1, CS2, CS3, RES1, RES2, DC>
|
||||
where
|
||||
SPI: esp_hal::spi::master::Instance + 'static,
|
||||
SCK: PeripheralOutput<'static>,
|
||||
MOSI: PeripheralOutput<'static>,
|
||||
MISO: PeripheralInput<'static>,
|
||||
CS0: OutputPin + 'static,
|
||||
CS1: OutputPin + 'static,
|
||||
CS2: OutputPin + 'static,
|
||||
CS3: OutputPin + 'static,
|
||||
RES1: OutputPin + 'static,
|
||||
RES2: OutputPin + 'static,
|
||||
DC: OutputPin + 'static,
|
||||
{
|
||||
pub fn build(self) -> (PrimaryDisplay<'static>, [SecondaryDisplay<'static>; 3]) {
|
||||
let spi = Spi::new(
|
||||
self.spi_peripheral,
|
||||
SpiConfig::default()
|
||||
.with_frequency(Rate::from_khz(20_000))
|
||||
.with_mode(Mode::_0),
|
||||
)
|
||||
.unwrap()
|
||||
.with_sck(self.sck)
|
||||
.with_mosi(self.mosi)
|
||||
.with_miso(self.miso);
|
||||
|
||||
let bus_static: &'static AtomicCell<_> = Box::leak(Box::new(AtomicCell::new(spi)));
|
||||
let reset_secondary = SharedResetPin::new(
|
||||
Output::new(self.reset_secondary, Level::High, OutputConfig::default()),
|
||||
false,
|
||||
);
|
||||
let dc = SharedPin::new(Output::new(
|
||||
self.dc_pin,
|
||||
Level::High,
|
||||
OutputConfig::default(),
|
||||
));
|
||||
|
||||
let primary = PrimaryDisplayPinConfiguration {
|
||||
spi_bus: bus_static,
|
||||
cs: low_active(self.cs_primary),
|
||||
reset: low_active(self.reset_primary),
|
||||
dc: dc.clone(),
|
||||
}
|
||||
.build();
|
||||
|
||||
let secondaries = SecondaryDisplayPinConfiguration {
|
||||
spi_bus: bus_static,
|
||||
cs_pins: [
|
||||
low_active(self.cs_secondary_1),
|
||||
low_active(self.cs_secondary_2),
|
||||
low_active(self.cs_secondary_3),
|
||||
],
|
||||
res: reset_secondary,
|
||||
dc,
|
||||
}
|
||||
.build();
|
||||
|
||||
(primary, secondaries)
|
||||
}
|
||||
}
|
||||
|
||||
fn low_active<'a, CS: OutputPin + 'a>(pin: CS) -> Output<'a> {
|
||||
Output::new(pin, Level::High, OutputConfig::default())
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
use embedded_hal::i2c::I2c;
|
||||
use embedded_hal_bus::i2c::AtomicDevice;
|
||||
use esp_hal::delay::Delay;
|
||||
use i2c_character_display::{CharacterDisplayPCF8574T, LcdDisplayType};
|
||||
use log::error;
|
||||
|
||||
type GenericTertiaryDisplay<I2C> = CharacterDisplayPCF8574T<I2C, Delay>;
|
||||
pub type TertiaryDisplay<'a> =
|
||||
GenericTertiaryDisplay<AtomicDevice<'a, esp_hal::i2c::master::I2c<'a, esp_hal::Blocking>>>;
|
||||
|
||||
pub fn init_tertiary_lcd<I2C>(i2c: I2C) -> GenericTertiaryDisplay<I2C>
|
||||
where
|
||||
I2C: I2c,
|
||||
{
|
||||
let mut lcd = CharacterDisplayPCF8574T::new(i2c, LcdDisplayType::Lcd16x2, Delay::new());
|
||||
match lcd.init() {
|
||||
Ok(()) => {
|
||||
log::info!("I2C tertiary LCD initialized on PCF8574T at address 0x27");
|
||||
lcd
|
||||
}
|
||||
Err(_) => {
|
||||
error!(
|
||||
"I2C tertiary LCD init failed on PCF8574T at address 0x27; check bus wiring, power, and device address"
|
||||
);
|
||||
panic!("tertiary LCD initialization failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_line<I2C>(
|
||||
lcd: &mut GenericTertiaryDisplay<I2C>,
|
||||
line: u8,
|
||||
text: &str,
|
||||
) -> Result<(), i2c_character_display::CharacterDisplayError<I2C>>
|
||||
where
|
||||
I2C: I2c,
|
||||
{
|
||||
if let Err(e) = lcd.set_cursor(0, line) {
|
||||
error!("I2C LCD cursor move failed on line {line}; bus or device may be unavailable");
|
||||
return Err(e);
|
||||
}
|
||||
if let Err(e) = lcd.print(text) {
|
||||
error!(
|
||||
"I2C LCD print failed on line {line}; payload length={} and device may be busy or disconnected",
|
||||
text.len()
|
||||
);
|
||||
return Err(e);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write_wrapped<I2C>(
|
||||
lcd: &mut GenericTertiaryDisplay<I2C>,
|
||||
text: &str,
|
||||
) -> Result<(), i2c_character_display::CharacterDisplayError<I2C>>
|
||||
where
|
||||
I2C: I2c,
|
||||
{
|
||||
const DISPLAY_WIDTH: usize = 16;
|
||||
const DISPLAY_ROWS: usize = 2;
|
||||
|
||||
if let Err(e) = lcd.clear() {
|
||||
error!(
|
||||
"I2C LCD clear failed before wrapped write; check the shared bus and display response"
|
||||
);
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
let mut offset = 0;
|
||||
for row in 0..DISPLAY_ROWS {
|
||||
let remaining = &text[offset..];
|
||||
if remaining.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let chunk = if remaining.len() > DISPLAY_WIDTH {
|
||||
&remaining[..DISPLAY_WIDTH]
|
||||
} else {
|
||||
remaining
|
||||
};
|
||||
|
||||
if let Err(e) = lcd.set_cursor(0, row as u8) {
|
||||
error!(
|
||||
"I2C LCD cursor move failed while writing wrapped row {row}; bus may be busy or device unresponsive"
|
||||
);
|
||||
return Err(e);
|
||||
}
|
||||
if let Err(e) = lcd.print(chunk) {
|
||||
error!(
|
||||
"I2C LCD wrapped write failed on row {row}; chunk length={} and text may exceed display width",
|
||||
chunk.len()
|
||||
);
|
||||
return Err(e);
|
||||
}
|
||||
offset += chunk.len();
|
||||
|
||||
if offset >= text.len() {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user