diff options
| author | Caleb Jamison <[email protected]> | 2023-09-09 18:25:23 -0400 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-09-10 23:01:15 +0200 |
| commit | 2d9f50addc5f509f5549e69f594c382cebe739e6 (patch) | |
| tree | 41861a3120a578dcfa744b6777ec5b592cd63c97 /tests | |
| parent | 18da91e2529b7973d0476e5c239709b3851db14b (diff) | |
I2c slave take 2
refactored to split modules
renamed to match upstream docs
slight improvement to slave error handling
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/rp/src/bin/i2c.rs | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/tests/rp/src/bin/i2c.rs b/tests/rp/src/bin/i2c.rs index 63dd00233..a6cf48afe 100644 --- a/tests/rp/src/bin/i2c.rs +++ b/tests/rp/src/bin/i2c.rs | |||
| @@ -5,10 +5,9 @@ | |||
| 5 | use defmt::{assert_eq, info, panic, unwrap}; | 5 | use defmt::{assert_eq, info, panic, unwrap}; |
| 6 | use embassy_executor::Executor; | 6 | use embassy_executor::Executor; |
| 7 | use embassy_executor::_export::StaticCell; | 7 | use embassy_executor::_export::StaticCell; |
| 8 | use embassy_rp::bind_interrupts; | ||
| 9 | use embassy_rp::i2c::{self, Async, InterruptHandler}; | ||
| 10 | use embassy_rp::multicore::{spawn_core1, Stack}; | 8 | use embassy_rp::multicore::{spawn_core1, Stack}; |
| 11 | use embassy_rp::peripherals::{I2C0, I2C1}; | 9 | use embassy_rp::peripherals::{I2C0, I2C1}; |
| 10 | use embassy_rp::{bind_interrupts, i2c, i2c_slave}; | ||
| 12 | use embedded_hal_1::i2c::Operation; | 11 | use embedded_hal_1::i2c::Operation; |
| 13 | use embedded_hal_async::i2c::I2c; | 12 | use embedded_hal_async::i2c::I2c; |
| 14 | use {defmt_rtt as _, panic_probe as _, panic_probe as _, panic_probe as _}; | 13 | use {defmt_rtt as _, panic_probe as _, panic_probe as _, panic_probe as _}; |
| @@ -20,14 +19,14 @@ static EXECUTOR1: StaticCell<Executor> = StaticCell::new(); | |||
| 20 | use crate::i2c::AbortReason; | 19 | use crate::i2c::AbortReason; |
| 21 | 20 | ||
| 22 | bind_interrupts!(struct Irqs { | 21 | bind_interrupts!(struct Irqs { |
| 23 | I2C0_IRQ => InterruptHandler<I2C0>; | 22 | I2C0_IRQ => i2c::InterruptHandler<I2C0>; |
| 24 | I2C1_IRQ => InterruptHandler<I2C1>; | 23 | I2C1_IRQ => i2c::InterruptHandler<I2C1>; |
| 25 | }); | 24 | }); |
| 26 | 25 | ||
| 27 | const DEV_ADDR: u8 = 0x42; | 26 | const DEV_ADDR: u8 = 0x42; |
| 28 | 27 | ||
| 29 | #[embassy_executor::task] | 28 | #[embassy_executor::task] |
| 30 | async fn device_task(mut dev: i2c::I2cDevice<'static, I2C1>) -> ! { | 29 | async fn device_task(mut dev: i2c_slave::I2cSlave<'static, I2C1>) -> ! { |
| 31 | info!("Device start"); | 30 | info!("Device start"); |
| 32 | 31 | ||
| 33 | let mut count = 0xD0; | 32 | let mut count = 0xD0; |
| @@ -35,33 +34,33 @@ async fn device_task(mut dev: i2c::I2cDevice<'static, I2C1>) -> ! { | |||
| 35 | loop { | 34 | loop { |
| 36 | let mut buf = [0u8; 128]; | 35 | let mut buf = [0u8; 128]; |
| 37 | match dev.listen(&mut buf).await { | 36 | match dev.listen(&mut buf).await { |
| 38 | Ok(i2c::Command::GeneralCall(len)) => { | 37 | Ok(i2c_slave::Command::GeneralCall(len)) => { |
| 39 | assert_eq!(buf[..len], [0xCA, 0x11], "recieving the general call failed"); | 38 | assert_eq!(buf[..len], [0xCA, 0x11], "recieving the general call failed"); |
| 40 | info!("General Call - OK"); | 39 | info!("General Call - OK"); |
| 41 | } | 40 | } |
| 42 | Ok(i2c::Command::Read) => { | 41 | Ok(i2c_slave::Command::Read) => { |
| 43 | loop { | 42 | loop { |
| 44 | //info!("Responding to read, count {}", count); | 43 | //info!("Responding to read, count {}", count); |
| 45 | let a = dev.respond_to_read(&[count]).await; | 44 | let a = dev.respond_to_read(&[count]).await; |
| 46 | //info!("x {}", a); | 45 | //info!("x {}", a); |
| 47 | match a { | 46 | match a { |
| 48 | Ok(x) => match x { | 47 | Ok(x) => match x { |
| 49 | i2c::ReadStatus::Done => break, | 48 | i2c_slave::ReadStatus::Done => break, |
| 50 | i2c::ReadStatus::NeedMoreBytes => count += 1, | 49 | i2c_slave::ReadStatus::NeedMoreBytes => count += 1, |
| 51 | i2c::ReadStatus::LeftoverBytes(x) => { | 50 | i2c_slave::ReadStatus::LeftoverBytes(x) => { |
| 52 | info!("tried to write {} extra bytes", x); | 51 | info!("tried to write {} extra bytes", x); |
| 53 | break; | 52 | break; |
| 54 | } | 53 | } |
| 55 | }, | 54 | }, |
| 56 | Err(e) => match e { | 55 | Err(e) => match e { |
| 57 | embassy_rp::i2c::Error::Abort(AbortReason::Other(n)) => panic!("Other {:b}", n), | 56 | embassy_rp::i2c_slave::Error::Abort(AbortReason::Other(n)) => panic!("Other {:b}", n), |
| 58 | _ => panic!("{}", e), | 57 | _ => panic!("{}", e), |
| 59 | }, | 58 | }, |
| 60 | } | 59 | } |
| 61 | } | 60 | } |
| 62 | count += 1; | 61 | count += 1; |
| 63 | } | 62 | } |
| 64 | Ok(i2c::Command::Write(len)) => match len { | 63 | Ok(i2c_slave::Command::Write(len)) => match len { |
| 65 | 1 => { | 64 | 1 => { |
| 66 | assert_eq!(buf[..len], [0xAA], "recieving a single byte failed"); | 65 | assert_eq!(buf[..len], [0xAA], "recieving a single byte failed"); |
| 67 | info!("Single Byte Write - OK") | 66 | info!("Single Byte Write - OK") |
| @@ -83,7 +82,7 @@ async fn device_task(mut dev: i2c::I2cDevice<'static, I2C1>) -> ! { | |||
| 83 | } | 82 | } |
| 84 | _ => panic!("Invalid write length {}", len), | 83 | _ => panic!("Invalid write length {}", len), |
| 85 | }, | 84 | }, |
| 86 | Ok(i2c::Command::WriteRead(len)) => { | 85 | Ok(i2c_slave::Command::WriteRead(len)) => { |
| 87 | info!("device recieved write read: {:x}", buf[..len]); | 86 | info!("device recieved write read: {:x}", buf[..len]); |
| 88 | match buf[0] { | 87 | match buf[0] { |
| 89 | 0xC2 => { | 88 | 0xC2 => { |
| @@ -101,7 +100,7 @@ async fn device_task(mut dev: i2c::I2cDevice<'static, I2C1>) -> ! { | |||
| 101 | } | 100 | } |
| 102 | } | 101 | } |
| 103 | Err(e) => match e { | 102 | Err(e) => match e { |
| 104 | embassy_rp::i2c::Error::Abort(AbortReason::Other(n)) => panic!("Other {:b}", n), | 103 | embassy_rp::i2c_slave::Error::Abort(AbortReason::Other(n)) => panic!("Other {:b}", n), |
| 105 | _ => panic!("{}", e), | 104 | _ => panic!("{}", e), |
| 106 | }, | 105 | }, |
| 107 | } | 106 | } |
| @@ -109,7 +108,7 @@ async fn device_task(mut dev: i2c::I2cDevice<'static, I2C1>) -> ! { | |||
| 109 | } | 108 | } |
| 110 | 109 | ||
| 111 | #[embassy_executor::task] | 110 | #[embassy_executor::task] |
| 112 | async fn controller_task(mut con: i2c::I2c<'static, I2C0, Async>) { | 111 | async fn controller_task(mut con: i2c::I2c<'static, I2C0, i2c::Async>) { |
| 113 | info!("Device start"); | 112 | info!("Device start"); |
| 114 | 113 | ||
| 115 | { | 114 | { |
| @@ -194,9 +193,9 @@ fn main() -> ! { | |||
| 194 | 193 | ||
| 195 | let d_sda = p.PIN_19; | 194 | let d_sda = p.PIN_19; |
| 196 | let d_scl = p.PIN_18; | 195 | let d_scl = p.PIN_18; |
| 197 | let mut config = i2c::DeviceConfig::default(); | 196 | let mut config = i2c_slave::Config::default(); |
| 198 | config.addr = DEV_ADDR as u16; | 197 | config.addr = DEV_ADDR as u16; |
| 199 | let device = i2c::I2cDevice::new(p.I2C1, d_sda, d_scl, Irqs, config); | 198 | let device = i2c_slave::I2cSlave::new(p.I2C1, d_sda, d_scl, Irqs, config); |
| 200 | 199 | ||
| 201 | spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || { | 200 | spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || { |
| 202 | let executor1 = EXECUTOR1.init(Executor::new()); | 201 | let executor1 = EXECUTOR1.init(Executor::new()); |
