Implement the embedded-hal alpha traits for the GPIO and I2C drivers

This commit is contained in:
Jesse Braham 2022-06-02 13:41:53 -07:00 committed by Jesse Braham
parent 9b033d44db
commit 8663153e12
6 changed files with 164 additions and 34 deletions

View File

@ -523,6 +523,20 @@ macro_rules! impl_input {
}
}
impl<MODE> embedded_hal_1::digital::ErrorType for $pxi<Input<MODE>> {
type Error = Infallible;
}
impl<MODE> embedded_hal_1::digital::blocking::InputPin for $pxi<Input<MODE>> {
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.read_input() & (1 << $bit) != 0)
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(!self.is_high()?)
}
}
impl<MODE> $pxi<MODE> {
fn init_input(&self, pull_down: bool, pull_up: bool) {
let gpio = unsafe { &*GPIO::PTR };
@ -740,6 +754,46 @@ macro_rules! impl_output {
type Error = Infallible;
fn toggle(&mut self) -> Result<(), Self::Error> {
use embedded_hal::digital::v2::{StatefulOutputPin as _, OutputPin as _};
if self.is_set_high()? {
Ok(self.set_low()?)
} else {
Ok(self.set_high()?)
}
}
}
impl<MODE> embedded_hal_1::digital::ErrorType for $pxi<Output<MODE>> {
type Error = Infallible;
}
impl<MODE> embedded_hal_1::digital::blocking::OutputPin for $pxi<Output<MODE>> {
fn set_low(&mut self) -> Result<(), Self::Error> {
self.write_output_clear(1 << $bit);
Ok(())
}
fn set_high(&mut self) -> Result<(), Self::Error> {
self.write_output_set(1 << $bit);
Ok(())
}
}
impl<MODE> embedded_hal_1::digital::blocking::StatefulOutputPin for $pxi<Output<MODE>> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.read_output() & (1 << $bit) != 0)
}
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(!self.is_set_high()?)
}
}
impl<MODE> embedded_hal_1::digital::blocking::ToggleableOutputPin for $pxi<Output<MODE>> {
fn toggle(&mut self) -> Result<(), Self::Error> {
use embedded_hal_1::digital::blocking::{StatefulOutputPin as _, OutputPin as _};
if self.is_set_high()? {
Ok(self.set_low()?)
} else {
@ -1009,7 +1063,7 @@ macro_rules! gpio {
)+
) => {
use core::{convert::Infallible, marker::PhantomData};
use embedded_hal::digital::v2::{OutputPin as _, StatefulOutputPin as _};
use crate::pac::{GPIO, IO_MUX};
pub struct IO {

View File

@ -1,9 +1,9 @@
//! I2C Driver
//!
//! Supports multiple I2C peripheral instances
use core::convert::TryInto;
use embedded_hal::blocking::i2c::*;
use fugit::HertzU32;
use crate::{
@ -33,6 +33,18 @@ pub enum Error {
CommandNrExceeded,
}
impl embedded_hal_1::i2c::Error for Error {
fn kind(&self) -> embedded_hal_1::i2c::ErrorKind {
use embedded_hal_1::i2c::ErrorKind;
match self {
Self::ExceedingFifo => ErrorKind::Overrun,
Self::ArbitrationLost => ErrorKind::ArbitrationLoss,
_ => ErrorKind::Other,
}
}
}
/// I2C-specific setup errors
#[derive(Debug)]
pub enum SetupError {
@ -152,7 +164,7 @@ pub struct I2C<T> {
peripheral: T,
}
impl<T> Read for I2C<T>
impl<T> embedded_hal::blocking::i2c::Read for I2C<T>
where
T: Instance,
{
@ -163,7 +175,7 @@ where
}
}
impl<T> Write for I2C<T>
impl<T> embedded_hal::blocking::i2c::Write for I2C<T>
where
T: Instance,
{
@ -174,7 +186,7 @@ where
}
}
impl<T> WriteRead for I2C<T>
impl<T> embedded_hal::blocking::i2c::WriteRead for I2C<T>
where
T: Instance,
{
@ -190,6 +202,66 @@ where
}
}
impl<T> embedded_hal_1::i2c::ErrorType for I2C<T> {
type Error = Error;
}
impl<T> embedded_hal_1::i2c::blocking::I2c for I2C<T>
where
T: Instance,
{
fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
self.peripheral.master_read(address, buffer)
}
fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
self.peripheral.master_write(address, bytes)
}
fn write_iter<B>(&mut self, _address: u8, _bytes: B) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
todo!()
}
fn write_read(
&mut self,
address: u8,
bytes: &[u8],
buffer: &mut [u8],
) -> Result<(), Self::Error> {
self.peripheral.master_write_read(address, bytes, buffer)
}
fn write_iter_read<B>(
&mut self,
_address: u8,
_bytes: B,
_buffer: &mut [u8],
) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
todo!()
}
fn transaction<'a>(
&mut self,
_address: u8,
_operations: &mut [embedded_hal_1::i2c::blocking::Operation<'a>],
) -> Result<(), Self::Error> {
todo!()
}
fn transaction_iter<'a, O>(&mut self, _address: u8, _operations: O) -> Result<(), Self::Error>
where
O: IntoIterator<Item = embedded_hal_1::i2c::blocking::Operation<'a>>,
{
todo!()
}
}
impl<T> I2C<T>
where
T: Instance,

View File

@ -24,13 +24,14 @@ categories = [
]
[dependencies]
bare-metal = "1.0"
embedded-hal = { version = "0.2", features = ["unproven"] }
fugit = "0.3"
nb = "1.0"
void = { version = "1.0", default-features = false }
xtensa-lx = { version = "0.7", features = ["esp32"] }
xtensa-lx-rt = { version = "0.11", features = ["esp32"], optional = true }
bare-metal = "1.0"
embedded-hal = { version = "0.2", features = ["unproven"] }
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.8" }
fugit = "0.3"
nb = "1.0"
void = { version = "1.0", default-features = false }
xtensa-lx = { version = "0.7", features = ["esp32"] }
xtensa-lx-rt = { version = "0.11", features = ["esp32"], optional = true }
[dependencies.esp-hal-common]
path = "../esp-hal-common"

View File

@ -24,14 +24,15 @@ categories = [
]
[dependencies]
bare-metal = "1.0"
embedded-hal = { version = "0.2", features = ["unproven"] }
fugit = "0.3"
nb = "1.0"
r0 = "1.0"
riscv = "0.8"
riscv-rt = { version = "0.8", optional = true }
void = { version = "1.0", default-features = false }
bare-metal = "1.0"
embedded-hal = { version = "0.2", features = ["unproven"] }
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.8" }
fugit = "0.3"
nb = "1.0"
r0 = "1.0"
riscv = "0.8"
riscv-rt = { version = "0.8", optional = true }
void = { version = "1.0", default-features = false }
[dependencies.esp-hal-common]
path = "../esp-hal-common"

View File

@ -24,13 +24,14 @@ categories = [
]
[dependencies]
bare-metal = "1.0"
embedded-hal = { version = "0.2", features = ["unproven"] }
fugit = "0.3"
nb = "1.0"
void = { version = "1.0", default-features = false }
xtensa-lx = { version = "0.7", features = ["esp32s2"] }
xtensa-lx-rt = { version = "0.11", features = ["esp32s2"], optional = true }
bare-metal = "1.0"
embedded-hal = { version = "0.2", features = ["unproven"] }
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.8" }
fugit = "0.3"
nb = "1.0"
void = { version = "1.0", default-features = false }
xtensa-lx = { version = "0.7", features = ["esp32s2"] }
xtensa-lx-rt = { version = "0.11", features = ["esp32s2"], optional = true }
[dependencies.esp-hal-common]
path = "../esp-hal-common"

View File

@ -24,13 +24,14 @@ categories = [
]
[dependencies]
bare-metal = "1.0"
embedded-hal = { version = "0.2", features = ["unproven"] }
fugit = "0.3"
nb = "1.0"
void = { version = "1.0", default-features = false }
xtensa-lx = { version = "0.7", features = ["esp32s3"] }
xtensa-lx-rt = { version = "0.11", features = ["esp32s3"], optional = true }
bare-metal = "1.0"
embedded-hal = { version = "0.2", features = ["unproven"] }
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.8" }
fugit = "0.3"
nb = "1.0"
void = { version = "1.0", default-features = false }
xtensa-lx = { version = "0.7", features = ["esp32s3"] }
xtensa-lx-rt = { version = "0.11", features = ["esp32s3"], optional = true }
[dependencies.esp-hal-common]
path = "../esp-hal-common"