Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement i2c
Browse files Browse the repository at this point in the history
jbeaurivage committed Jan 31, 2024
1 parent 1698b0b commit bebd7c3
Showing 1 changed file with 67 additions and 5 deletions.
72 changes: 67 additions & 5 deletions hal/src/sercom/i2c/impl_ehal.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
//! `embedded-hal` trait implementations for [`I2c`]s
//! [`embedded-hal`] trait implementations for [`I2c`]s
use super::{config::AnyConfig, flags::Error, I2c};
use crate::ehal_02::blocking::i2c::{Read, Write, WriteRead};
use crate::ehal::i2c::{self, ErrorKind, ErrorType, NoAcknowledgeSource};

impl<C: AnyConfig> Write for I2c<C> {
impl i2c::Error for Error {
fn kind(&self) -> ErrorKind {
match self {
Error::BusError => ErrorKind::Bus,
Error::ArbitrationLost => ErrorKind::ArbitrationLoss,
Error::LengthError => ErrorKind::Other,
Error::Nack => ErrorKind::NoAcknowledge(NoAcknowledgeSource::Unknown),
Error::Timeout => ErrorKind::Other,
}
}
}

impl<C: AnyConfig> ErrorType for I2c<C> {
type Error = Error;
}

impl<C: AnyConfig> i2c::I2c for I2c<C> {
fn transaction(
&mut self,
address: u8,
operations: &mut [i2c::Operation<'_>],
) -> Result<(), Self::Error> {
for op in operations {
match op {
i2c::Operation::Read(buf) => {
self.do_read(address, buf)?;
self.cmd_stop();
}
i2c::Operation::Write(bytes) => {
self.do_write(address, bytes)?;
self.cmd_stop();
}
}
}

Ok(())
}

fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
self.do_write(address, bytes)?;
self.cmd_stop();
Ok(())
}

fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
self.do_read(address, buffer)?;
self.cmd_stop();
Ok(())
}

fn write_read(
&mut self,
address: u8,
bytes: &[u8],
buffer: &mut [u8],
) -> Result<(), Self::Error> {
self.do_write_read(address, bytes, buffer)?;
self.cmd_stop();
Ok(())
}
}

impl<C: AnyConfig> crate::ehal_02::blocking::i2c::Write for I2c<C> {
type Error = Error;

fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> {
@@ -13,7 +75,7 @@ impl<C: AnyConfig> Write for I2c<C> {
}
}

impl<C: AnyConfig> Read for I2c<C> {
impl<C: AnyConfig> crate::ehal_02::blocking::i2c::Read for I2c<C> {
type Error = Error;

fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
@@ -23,7 +85,7 @@ impl<C: AnyConfig> Read for I2c<C> {
}
}

impl<C: AnyConfig> WriteRead for I2c<C> {
impl<C: AnyConfig> crate::ehal_02::blocking::i2c::WriteRead for I2c<C> {
type Error = Error;

fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> {

0 comments on commit bebd7c3

Please sign in to comment.