Update embedded-hal crates.
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||
runner = "probe-run --chip RP2040"
|
||||
runner = "probe-rs-cli run --chip RP2040"
|
||||
|
||||
[build]
|
||||
target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
|
||||
|
@ -6,6 +6,7 @@ license = "MIT OR Apache-2.0"
|
||||
|
||||
|
||||
[dependencies]
|
||||
embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal", features = ["defmt"] }
|
||||
embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] }
|
||||
embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
|
||||
embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] }
|
||||
@ -30,8 +31,8 @@ display-interface = "0.4.1"
|
||||
byte-slice-cast = { version = "1.2.0", default-features = false }
|
||||
smart-leds = "0.3.0"
|
||||
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" }
|
||||
embedded-hal-async = "0.2.0-alpha.0"
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10" }
|
||||
embedded-hal-async = "0.2.0-alpha.1"
|
||||
embedded-io = { version = "0.4.0", features = ["async", "defmt"] }
|
||||
embedded-storage = { version = "0.3" }
|
||||
static_cell = "1.0.0"
|
||||
|
@ -5,10 +5,13 @@
|
||||
use core::cell::RefCell;
|
||||
|
||||
use defmt::*;
|
||||
use embassy_embedded_hal::shared_bus::blocking::spi::SpiDeviceWithConfig;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_rp::gpio::{Level, Output};
|
||||
use embassy_rp::spi;
|
||||
use embassy_rp::spi::{Blocking, Spi};
|
||||
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
|
||||
use embassy_sync::blocking_mutex::Mutex;
|
||||
use embassy_time::Delay;
|
||||
use embedded_graphics::image::{Image, ImageRawLE};
|
||||
use embedded_graphics::mono_font::ascii::FONT_10X20;
|
||||
@ -21,10 +24,9 @@ use st7789::{Orientation, ST7789};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
use crate::my_display_interface::SPIDeviceInterface;
|
||||
use crate::shared_spi::SpiDeviceWithCs;
|
||||
use crate::touch::Touch;
|
||||
|
||||
//const DISPLAY_FREQ: u32 = 64_000_000;
|
||||
const DISPLAY_FREQ: u32 = 64_000_000;
|
||||
const TOUCH_FREQ: u32 = 200_000;
|
||||
|
||||
#[embassy_executor::main]
|
||||
@ -43,16 +45,20 @@ async fn main(_spawner: Spawner) {
|
||||
//let touch_irq = p.PIN_17;
|
||||
|
||||
// create SPI
|
||||
let mut config = spi::Config::default();
|
||||
config.frequency = TOUCH_FREQ; // use the lowest freq
|
||||
config.phase = spi::Phase::CaptureOnSecondTransition;
|
||||
config.polarity = spi::Polarity::IdleHigh;
|
||||
let mut display_config = spi::Config::default();
|
||||
display_config.frequency = DISPLAY_FREQ;
|
||||
display_config.phase = spi::Phase::CaptureOnSecondTransition;
|
||||
display_config.polarity = spi::Polarity::IdleHigh;
|
||||
let mut touch_config = spi::Config::default();
|
||||
touch_config.frequency = TOUCH_FREQ;
|
||||
touch_config.phase = spi::Phase::CaptureOnSecondTransition;
|
||||
touch_config.polarity = spi::Polarity::IdleHigh;
|
||||
|
||||
let spi: Spi<'_, _, Blocking> = Spi::new_blocking(p.SPI1, clk, mosi, miso, config);
|
||||
let spi_bus = RefCell::new(spi);
|
||||
let spi: Spi<'_, _, Blocking> = Spi::new_blocking(p.SPI1, clk, mosi, miso, touch_config.clone());
|
||||
let spi_bus: Mutex<NoopRawMutex, _> = Mutex::new(RefCell::new(spi));
|
||||
|
||||
let display_spi = SpiDeviceWithCs::new(&spi_bus, Output::new(display_cs, Level::High));
|
||||
let touch_spi = SpiDeviceWithCs::new(&spi_bus, Output::new(touch_cs, Level::High));
|
||||
let display_spi = SpiDeviceWithConfig::new(&spi_bus, Output::new(display_cs, Level::High), display_config);
|
||||
let touch_spi = SpiDeviceWithConfig::new(&spi_bus, Output::new(touch_cs, Level::High), touch_config);
|
||||
|
||||
let mut touch = Touch::new(touch_spi);
|
||||
|
||||
@ -104,85 +110,9 @@ async fn main(_spawner: Spawner) {
|
||||
}
|
||||
}
|
||||
|
||||
mod shared_spi {
|
||||
use core::cell::RefCell;
|
||||
use core::fmt::Debug;
|
||||
|
||||
use embedded_hal_1::digital::OutputPin;
|
||||
use embedded_hal_1::spi;
|
||||
use embedded_hal_1::spi::SpiDevice;
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||
pub enum SpiDeviceWithCsError<BUS, CS> {
|
||||
#[allow(unused)] // will probably use in the future when adding a flush() to SpiBus
|
||||
Spi(BUS),
|
||||
Cs(CS),
|
||||
}
|
||||
|
||||
impl<BUS, CS> spi::Error for SpiDeviceWithCsError<BUS, CS>
|
||||
where
|
||||
BUS: spi::Error + Debug,
|
||||
CS: Debug,
|
||||
{
|
||||
fn kind(&self) -> spi::ErrorKind {
|
||||
match self {
|
||||
Self::Spi(e) => e.kind(),
|
||||
Self::Cs(_) => spi::ErrorKind::Other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SpiDeviceWithCs<'a, BUS, CS> {
|
||||
bus: &'a RefCell<BUS>,
|
||||
cs: CS,
|
||||
}
|
||||
|
||||
impl<'a, BUS, CS> SpiDeviceWithCs<'a, BUS, CS> {
|
||||
pub fn new(bus: &'a RefCell<BUS>, cs: CS) -> Self {
|
||||
Self { bus, cs }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, BUS, CS> spi::ErrorType for SpiDeviceWithCs<'a, BUS, CS>
|
||||
where
|
||||
BUS: spi::ErrorType,
|
||||
CS: OutputPin,
|
||||
{
|
||||
type Error = SpiDeviceWithCsError<BUS::Error, CS::Error>;
|
||||
}
|
||||
|
||||
impl<'a, BUS, CS> SpiDevice for SpiDeviceWithCs<'a, BUS, CS>
|
||||
where
|
||||
BUS: spi::SpiBusFlush,
|
||||
CS: OutputPin,
|
||||
{
|
||||
type Bus = BUS;
|
||||
|
||||
fn transaction<R>(
|
||||
&mut self,
|
||||
f: impl FnOnce(&mut Self::Bus) -> Result<R, BUS::Error>,
|
||||
) -> Result<R, Self::Error> {
|
||||
let mut bus = self.bus.borrow_mut();
|
||||
self.cs.set_low().map_err(SpiDeviceWithCsError::Cs)?;
|
||||
|
||||
let f_res = f(&mut bus);
|
||||
|
||||
// On failure, it's important to still flush and deassert CS.
|
||||
let flush_res = bus.flush();
|
||||
let cs_res = self.cs.set_high();
|
||||
|
||||
let f_res = f_res.map_err(SpiDeviceWithCsError::Spi)?;
|
||||
flush_res.map_err(SpiDeviceWithCsError::Spi)?;
|
||||
cs_res.map_err(SpiDeviceWithCsError::Cs)?;
|
||||
|
||||
Ok(f_res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Driver for the XPT2046 resistive touchscreen sensor
|
||||
mod touch {
|
||||
use embedded_hal_1::spi::{SpiBus, SpiBusRead, SpiBusWrite, SpiDevice};
|
||||
use embedded_hal_1::spi::{Operation, SpiDevice};
|
||||
|
||||
struct Calibration {
|
||||
x1: i32,
|
||||
@ -209,7 +139,6 @@ mod touch {
|
||||
impl<SPI> Touch<SPI>
|
||||
where
|
||||
SPI: SpiDevice,
|
||||
SPI::Bus: SpiBus,
|
||||
{
|
||||
pub fn new(spi: SPI) -> Self {
|
||||
Self { spi }
|
||||
@ -219,13 +148,12 @@ mod touch {
|
||||
let mut x = [0; 2];
|
||||
let mut y = [0; 2];
|
||||
self.spi
|
||||
.transaction(|bus| {
|
||||
bus.write(&[0x90])?;
|
||||
bus.read(&mut x)?;
|
||||
bus.write(&[0xd0])?;
|
||||
bus.read(&mut y)?;
|
||||
Ok(())
|
||||
})
|
||||
.transaction(&mut [
|
||||
Operation::Write(&[0x90]),
|
||||
Operation::Read(&mut x),
|
||||
Operation::Write(&[0xd0]),
|
||||
Operation::Read(&mut y),
|
||||
])
|
||||
.unwrap();
|
||||
|
||||
let x = (u16::from_be_bytes(x) >> 3) as i32;
|
||||
@ -247,7 +175,7 @@ mod touch {
|
||||
mod my_display_interface {
|
||||
use display_interface::{DataFormat, DisplayError, WriteOnlyDataCommand};
|
||||
use embedded_hal_1::digital::OutputPin;
|
||||
use embedded_hal_1::spi::{SpiBusWrite, SpiDevice};
|
||||
use embedded_hal_1::spi::SpiDeviceWrite;
|
||||
|
||||
/// SPI display interface.
|
||||
///
|
||||
@ -259,8 +187,7 @@ mod my_display_interface {
|
||||
|
||||
impl<SPI, DC> SPIDeviceInterface<SPI, DC>
|
||||
where
|
||||
SPI: SpiDevice,
|
||||
SPI::Bus: SpiBusWrite,
|
||||
SPI: SpiDeviceWrite,
|
||||
DC: OutputPin,
|
||||
{
|
||||
/// Create new SPI interface for communciation with a display driver
|
||||
@ -271,42 +198,27 @@ mod my_display_interface {
|
||||
|
||||
impl<SPI, DC> WriteOnlyDataCommand for SPIDeviceInterface<SPI, DC>
|
||||
where
|
||||
SPI: SpiDevice,
|
||||
SPI::Bus: SpiBusWrite,
|
||||
SPI: SpiDeviceWrite,
|
||||
DC: OutputPin,
|
||||
{
|
||||
fn send_commands(&mut self, cmds: DataFormat<'_>) -> Result<(), DisplayError> {
|
||||
let r = self.spi.transaction(|bus| {
|
||||
// 1 = data, 0 = command
|
||||
if let Err(_) = self.dc.set_low() {
|
||||
return Ok(Err(DisplayError::DCError));
|
||||
}
|
||||
// 1 = data, 0 = command
|
||||
self.dc.set_low().map_err(|_| DisplayError::DCError)?;
|
||||
|
||||
// Send words over SPI
|
||||
send_u8(bus, cmds)?;
|
||||
|
||||
Ok(Ok(()))
|
||||
});
|
||||
r.map_err(|_| DisplayError::BusWriteError)?
|
||||
send_u8(&mut self.spi, cmds).map_err(|_| DisplayError::BusWriteError)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn send_data(&mut self, buf: DataFormat<'_>) -> Result<(), DisplayError> {
|
||||
let r = self.spi.transaction(|bus| {
|
||||
// 1 = data, 0 = command
|
||||
if let Err(_) = self.dc.set_high() {
|
||||
return Ok(Err(DisplayError::DCError));
|
||||
}
|
||||
// 1 = data, 0 = command
|
||||
self.dc.set_high().map_err(|_| DisplayError::DCError)?;
|
||||
|
||||
// Send words over SPI
|
||||
send_u8(bus, buf)?;
|
||||
|
||||
Ok(Ok(()))
|
||||
});
|
||||
r.map_err(|_| DisplayError::BusWriteError)?
|
||||
send_u8(&mut self.spi, buf).map_err(|_| DisplayError::BusWriteError)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn send_u8<T: SpiBusWrite>(spi: &mut T, words: DataFormat<'_>) -> Result<(), T::Error> {
|
||||
fn send_u8<T: SpiDeviceWrite>(spi: &mut T, words: DataFormat<'_>) -> Result<(), T::Error> {
|
||||
match words {
|
||||
DataFormat::U8(slice) => spi.write(slice),
|
||||
DataFormat::U16(slice) => {
|
||||
|
@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
|
||||
embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] }
|
||||
embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
|
||||
embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
|
||||
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f103c8", "unstable-pac", "memory-x", "time-driver-any"] }
|
||||
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f103c8", "unstable-pac", "memory-x", "time-driver-any", "unstable-traits" ] }
|
||||
embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] }
|
||||
embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
|
||||
|
||||
|
@ -19,8 +19,8 @@ defmt-rtt = "0.4"
|
||||
cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
|
||||
cortex-m-rt = "0.7.0"
|
||||
embedded-hal = "0.2.6"
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" }
|
||||
embedded-hal-async = { version = "=0.2.0-alpha.0" }
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10" }
|
||||
embedded-hal-async = { version = "=0.2.0-alpha.1" }
|
||||
embedded-nal-async = "0.4.0"
|
||||
panic-probe = { version = "0.3", features = ["print-defmt"] }
|
||||
futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
|
||||
|
@ -19,8 +19,8 @@ defmt-rtt = "0.4"
|
||||
cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
|
||||
cortex-m-rt = "0.7.0"
|
||||
embedded-hal = "0.2.6"
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" }
|
||||
embedded-hal-async = { version = "=0.2.0-alpha.0" }
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10" }
|
||||
embedded-hal-async = { version = "=0.2.0-alpha.1" }
|
||||
embedded-nal-async = "0.4.0"
|
||||
panic-probe = { version = "0.3", features = ["print-defmt"] }
|
||||
futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
|
||||
|
@ -18,8 +18,8 @@ defmt-rtt = "0.4"
|
||||
cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
|
||||
cortex-m-rt = "0.7.0"
|
||||
embedded-hal = "0.2.6"
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" }
|
||||
embedded-hal-async = { version = "=0.2.0-alpha.0" }
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10" }
|
||||
embedded-hal-async = { version = "=0.2.0-alpha.1" }
|
||||
panic-probe = { version = "0.3", features = ["print-defmt"] }
|
||||
futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
|
||||
heapless = { version = "0.7.5", default-features = false }
|
||||
|
Reference in New Issue
Block a user