diff --git a/firmware/rust1/Cargo.lock b/firmware/rust1/Cargo.lock index 6dcddbba42b2cdcdd5c92c21f50ec20eb6d1f72c..4d809cecf582817b44d9450f8eab196fcca8408e 100644 --- a/firmware/rust1/Cargo.lock +++ b/firmware/rust1/Cargo.lock @@ -587,48 +587,6 @@ dependencies = [ "rp2040-boot2", ] -[[package]] -name = "embassy-rp-examples" -version = "0.1.0" -dependencies = [ - "byte-slice-cast 1.2.2", - "cortex-m", - "cortex-m-rt", - "defmt", - "defmt-rtt", - "display-interface", - "display-interface-spi", - "embassy-embedded-hal", - "embassy-executor", - "embassy-futures", - "embassy-lora", - "embassy-net", - "embassy-rp", - "embassy-sync", - "embassy-time", - "embassy-usb", - "embassy-usb-logger", - "embedded-graphics", - "embedded-hal 1.0.0-alpha.10", - "embedded-hal-async", - "embedded-io", - "embedded-storage", - "fixed", - "fixed-macro", - "futures", - "heapless", - "log", - "lora-phy", - "lorawan", - "lorawan-device", - "panic-probe", - "pio", - "pio-proc", - "smart-leds", - "st7789", - "static_cell", -] - [[package]] name = "embassy-sync" version = "0.2.0" @@ -1049,6 +1007,50 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "heizung" +version = "0.1.0" +dependencies = [ + "byte-slice-cast 1.2.2", + "cortex-m", + "cortex-m-rt", + "defmt", + "defmt-rtt", + "display-interface", + "display-interface-spi", + "embassy-cortex-m", + "embassy-embedded-hal", + "embassy-executor", + "embassy-futures", + "embassy-hal-common", + "embassy-lora", + "embassy-net", + "embassy-rp", + "embassy-sync", + "embassy-time", + "embassy-usb", + "embassy-usb-logger", + "embedded-graphics", + "embedded-hal 1.0.0-alpha.10", + "embedded-hal-async", + "embedded-io", + "embedded-storage", + "fixed", + "fixed-macro", + "futures", + "heapless", + "log", + "lora-phy", + "lorawan", + "lorawan-device", + "panic-probe", + "pio", + "pio-proc", + "smart-leds", + "st7789", + "static_cell", +] + [[package]] name = "hermit-abi" version = "0.3.1" diff --git a/firmware/rust1/Cargo.toml b/firmware/rust1/Cargo.toml index 678eb3e0bb6732f223e431e129eb83c6f7a524a4..7268c1f8a6719d4045975c9eb957da29f4ff3a5d 100644 --- a/firmware/rust1/Cargo.toml +++ b/firmware/rust1/Cargo.toml @@ -1,10 +1,15 @@ [package] edition = "2021" -name = "embassy-rp-examples" +name = "heizung" version = "0.1.0" license = "MIT OR Apache-2.0" +[features] +default = ["defmt"] +defmt = [] + + [dependencies] embassy-embedded-hal = { version = "0.1.0", path = "./embassy/embassy-embedded-hal", features = ["defmt"] } embassy-sync = { version = "0.2.0", path = "./embassy/embassy-sync", features = ["defmt"] } @@ -19,6 +24,9 @@ embassy-lora = { version = "0.1.0", path = "./embassy/embassy-lora", features = lora-phy = { version = "1" } lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"] } lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"] } +# for i2c.rs copied from embassy-rp +embassy-cortex-m = { version = "0.1.0", path = "./embassy/embassy-cortex-m", features = ["prio-bits-2"]} +embassy-hal-common = {version = "0.1.0", path = "./embassy/embassy-hal-common" } defmt = "0.3" defmt-rtt = "0.4" diff --git a/firmware/rust1/src/bin/heizung.rs b/firmware/rust1/src/bin/heizung.rs index e461f5e498c99cfdc8c5260ea60a11d83fd448c3..0c1d7674d38c6cf4bd2597d5be3d96ade55b0071 100644 --- a/firmware/rust1/src/bin/heizung.rs +++ b/firmware/rust1/src/bin/heizung.rs @@ -7,7 +7,8 @@ use embassy_executor::Spawner; use embassy_rp::gpio; use embassy_time::{Duration, Timer}; use embassy_rp::gpio::{Input, Level, Output, Pull}; -use embassy_rp::i2c::{self}; +//use embassy_rp::i2c; +use heizung::rp::i2c; use embassy_rp::interrupt; use embedded_hal_async::i2c::I2c; use embassy_rp::peripherals::{UART0, I2C0}; diff --git a/firmware/rust1/src/lib.rs b/firmware/rust1/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..26e5f5c2c222b0247e1981eb41c010e1c068ce8a --- /dev/null +++ b/firmware/rust1/src/lib.rs @@ -0,0 +1,2 @@ +#![no_std] +pub mod rp; diff --git a/firmware/rust1/src/lib/i2c.rs b/firmware/rust1/src/rp/i2c.rs similarity index 92% rename from firmware/rust1/src/lib/i2c.rs rename to firmware/rust1/src/rp/i2c.rs index 3e9946f80c0d99a26495b3471634e7bb58f36563..780470107ccc5e80a54521877cadd70b88f6229e 100644 --- a/firmware/rust1/src/lib/i2c.rs +++ b/firmware/rust1/src/rp/i2c.rs @@ -12,9 +12,9 @@ use embassy_hal_common::{into_ref, PeripheralRef}; use embassy_sync::waitqueue::AtomicWaker; use pac::i2c; -use crate::gpio::sealed::Pin; -use crate::gpio::AnyPin; -use crate::{pac, peripherals, Peripheral}; +use embassy_rp::gpio::Pin; +use embassy_rp::gpio::AnyPin; +use embassy_rp::{pac, peripherals, Peripheral}; /// I2C error abort reason #[derive(Debug)] @@ -63,6 +63,7 @@ pub struct I2c<'d, T: Instance, M: Mode> { phantom: PhantomData<(&'d mut T, M)>, } +/* impl<'d, T: Instance> I2c<'d, T, Blocking> { pub fn new_blocking( peri: impl Peripheral<P = T> + 'd, @@ -74,6 +75,7 @@ impl<'d, T: Instance> I2c<'d, T, Blocking> { Self::new_inner(peri, scl.map_into(), sda.map_into(), config) } } +*/ impl<'d, T: Instance> I2c<'d, T, Async> { pub fn new_async( @@ -325,6 +327,39 @@ impl<'d, T: Instance> I2c<'d, T, Async> { } } +// copy of internal method because we need it here +fn io<'d>(pin : &PeripheralRef<'d, AnyPin>) -> pac::io::Gpio { + use embassy_rp::gpio::Bank; + let block = match pin.bank() { + Bank::Bank0 => embassy_rp::pac::IO_BANK0, + Bank::Qspi => embassy_rp::pac::IO_QSPI, + }; + block.gpio(pin.pin() as _) +} + +// copy of internal method because we need it here +fn pad_ctrl<'d>(pin : &PeripheralRef<'d, AnyPin>) -> embassy_rp::pac::common::Reg<pac::pads::regs::GpioCtrl, embassy_rp::pac::common::RW> { + use embassy_rp::gpio::Bank; + let block = match pin.bank() { + Bank::Bank0 => embassy_rp::pac::PADS_BANK0, + Bank::Qspi => embassy_rp::pac::PADS_QSPI, + }; + block.gpio(pin.pin() as _) +} + +// copy of internal method because we need it here +pub unsafe fn reset(peris: embassy_rp::pac::resets::regs::Peripherals) { + embassy_rp::pac::RESETS.reset().write_value(peris); +} + +// copy of internal method because we need it here +pub unsafe fn unreset_wait(peris: embassy_rp::pac::resets::regs::Peripherals) { + // TODO use the "atomic clear" register version + embassy_rp::pac::RESETS.reset().modify(|v| *v = embassy_rp::pac::resets::regs::Peripherals(v.0 & !peris.0)); + while ((!embassy_rp::pac::RESETS.reset_done().read().0) & peris.0) != 0 {} +} + + impl<'d, T: Instance + 'd, M: Mode> I2c<'d, T, M> { fn new_inner( _peri: impl Peripheral<P = T> + 'd, @@ -341,8 +376,8 @@ impl<'d, T: Instance + 'd, M: Mode> I2c<'d, T, M> { unsafe { let reset = T::reset(); - crate::reset::reset(reset); - crate::reset::unreset_wait(reset); + self::reset(reset); + unreset_wait(reset); p.ic_enable().write(|w| w.set_enable(false)); @@ -363,17 +398,17 @@ impl<'d, T: Instance + 'd, M: Mode> I2c<'d, T, M> { p.ic_rx_tl().write(|w| w.set_rx_tl(0)); // Configure SCL & SDA pins - scl.io().ctrl().write(|w| w.set_funcsel(3)); - sda.io().ctrl().write(|w| w.set_funcsel(3)); + io(&scl).ctrl().write(|w| w.set_funcsel(3)); + io(&sda).ctrl().write(|w| w.set_funcsel(3)); - scl.pad_ctrl().write(|w| { + pad_ctrl(&scl).write(|w| { w.set_schmitt(true); w.set_ie(true); w.set_od(false); w.set_pue(true); w.set_pde(false); }); - sda.pad_ctrl().write(|w| { + pad_ctrl(&sda).write(|w| { w.set_schmitt(true); w.set_ie(true); w.set_od(false); @@ -386,7 +421,7 @@ impl<'d, T: Instance + 'd, M: Mode> I2c<'d, T, M> { // There are some subtleties to I2C timing which we are completely // ignoring here See: // https://github.com/raspberrypi/pico-sdk/blob/bfcbefafc5d2a210551a4d9d80b4303d4ae0adf7/src/rp2_common/hardware_i2c/i2c.c#L69 - let clk_base = crate::clocks::clk_peri_freq(); + let clk_base = embassy_rp::clocks::clk_peri_freq(); let period = (clk_base + config.frequency / 2) / config.frequency; let lcnt = period * 3 / 5; // spend 3/5 (60%) of the period low @@ -437,9 +472,9 @@ impl<'d, T: Instance + 'd, M: Mode> I2c<'d, T, M> { return Err(Error::AddressOutOfRange(addr)); } - if i2c_reserved_addr(addr) { - return Err(Error::AddressReserved(addr)); - } + //if i2c_reserved_addr(addr) { + // return Err(Error::AddressReserved(addr)); + //} let p = T::regs(); unsafe { @@ -596,6 +631,7 @@ impl<'d, T: Instance + 'd, M: Mode> I2c<'d, T, M> { } } +/* mod eh02 { use super::*; @@ -645,6 +681,7 @@ mod eh02 { } } } +*/ #[cfg(feature = "unstable-traits")] mod eh1 { @@ -758,7 +795,7 @@ mod nightly { } } -fn i2c_reserved_addr(addr: u16) -> bool { +fn _i2c_reserved_addr(addr: u16) -> bool { (addr & 0x78) == 0 || (addr & 0x78) == 0x78 } @@ -772,8 +809,8 @@ mod sealed { type Interrupt: Interrupt; - fn regs() -> crate::pac::i2c::I2c; - fn reset() -> crate::pac::resets::regs::Peripherals; + fn regs() -> embassy_rp::pac::i2c::I2c; + fn reset() -> embassy_rp::pac::resets::regs::Peripherals; fn waker() -> &'static AtomicWaker; } @@ -806,7 +843,7 @@ macro_rules! impl_instance { const TX_DREQ: u8 = $tx_dreq; const RX_DREQ: u8 = $rx_dreq; - type Interrupt = crate::interrupt::$irq; + type Interrupt = embassy_rp::interrupt::$irq; #[inline] fn regs() -> pac::i2c::I2c { @@ -834,8 +871,8 @@ macro_rules! impl_instance { impl_instance!(I2C0, I2C0_IRQ, set_i2c0, 32, 33); impl_instance!(I2C1, I2C1_IRQ, set_i2c1, 34, 35); -pub trait SdaPin<T: Instance>: sealed::SdaPin<T> + crate::gpio::Pin {} -pub trait SclPin<T: Instance>: sealed::SclPin<T> + crate::gpio::Pin {} +pub trait SdaPin<T: Instance>: sealed::SdaPin<T> + embassy_rp::gpio::Pin {} +pub trait SclPin<T: Instance>: sealed::SclPin<T> + embassy_rp::gpio::Pin {} macro_rules! impl_pin { ($pin:ident, $instance:ident, $function:ident) => { diff --git a/firmware/rust1/src/rp/mod.rs b/firmware/rust1/src/rp/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..eec2637b4759dab71c8283f8b3d8c20a0665e030 --- /dev/null +++ b/firmware/rust1/src/rp/mod.rs @@ -0,0 +1 @@ +pub mod i2c;