diff options
Diffstat (limited to 'examples/stm32f4/src/bin/i2c_slave_blocking.rs')
| -rw-r--r-- | examples/stm32f4/src/bin/i2c_slave_blocking.rs | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/examples/stm32f4/src/bin/i2c_slave_blocking.rs b/examples/stm32f4/src/bin/i2c_slave_blocking.rs new file mode 100644 index 000000000..ee06d4ac4 --- /dev/null +++ b/examples/stm32f4/src/bin/i2c_slave_blocking.rs | |||
| @@ -0,0 +1,132 @@ | |||
| 1 | //! Complete I2C slave example using blocking operations | ||
| 2 | //! | ||
| 3 | //! This example shows how to set up an STM32F4 as an I2C slave device | ||
| 4 | //! that can handle both read and write transactions from master devices. | ||
| 5 | |||
| 6 | #![no_std] | ||
| 7 | #![no_main] | ||
| 8 | |||
| 9 | use defmt::{error, info}; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_stm32::i2c::{self, Address, I2c, SlaveAddrConfig, SlaveCommand, SlaveCommandKind}; | ||
| 12 | use embassy_stm32::time::Hertz; | ||
| 13 | use embassy_stm32::{bind_interrupts, peripherals}; | ||
| 14 | use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; | ||
| 15 | use embassy_sync::mutex::Mutex; | ||
| 16 | use embassy_time::{Duration, Timer}; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | pub const I2C_SLAVE_ADDR: u8 = 0x42; | ||
| 20 | pub const BUFFER_SIZE: usize = 8; | ||
| 21 | static I2C_BUFFER: Mutex<ThreadModeRawMutex, [u8; BUFFER_SIZE]> = Mutex::new([0; BUFFER_SIZE]); | ||
| 22 | |||
| 23 | bind_interrupts!(struct Irqs { | ||
| 24 | I2C1_EV => i2c::EventInterruptHandler<peripherals::I2C1>; | ||
| 25 | I2C1_ER => i2c::ErrorInterruptHandler<peripherals::I2C1>; | ||
| 26 | }); | ||
| 27 | |||
| 28 | #[embassy_executor::main] | ||
| 29 | async fn main(spawner: Spawner) { | ||
| 30 | let p = embassy_stm32::init(Default::default()); | ||
| 31 | |||
| 32 | // Configure I2C | ||
| 33 | let mut i2c_config = i2c::Config::default(); | ||
| 34 | i2c_config.sda_pullup = false; | ||
| 35 | i2c_config.scl_pullup = false; | ||
| 36 | i2c_config.frequency = Hertz(100_000); | ||
| 37 | i2c_config.timeout = embassy_time::Duration::from_millis(30000); | ||
| 38 | |||
| 39 | // Initialize I2C as master first | ||
| 40 | let i2c_master = I2c::new_blocking( | ||
| 41 | p.I2C1, p.PB8, // SCL | ||
| 42 | p.PB9, // SDA | ||
| 43 | i2c_config, | ||
| 44 | ); | ||
| 45 | |||
| 46 | // Convert to slave+master mode | ||
| 47 | let slave_config = SlaveAddrConfig::basic(I2C_SLAVE_ADDR); | ||
| 48 | let i2c_slave = i2c_master.into_slave_multimaster(slave_config); | ||
| 49 | |||
| 50 | spawner.spawn(i2c_slave_task(i2c_slave).unwrap()); | ||
| 51 | } | ||
| 52 | |||
| 53 | #[embassy_executor::task] | ||
| 54 | pub async fn i2c_slave_task(mut i2c_slave: I2c<'static, embassy_stm32::mode::Blocking, i2c::mode::MultiMaster>) { | ||
| 55 | info!("Blocking I2C slave ready at address 0x{:02X}", I2C_SLAVE_ADDR); | ||
| 56 | |||
| 57 | loop { | ||
| 58 | match i2c_slave.blocking_listen() { | ||
| 59 | Ok(SlaveCommand { | ||
| 60 | kind: SlaveCommandKind::Write, | ||
| 61 | address, | ||
| 62 | }) => { | ||
| 63 | let addr_val = match address { | ||
| 64 | Address::SevenBit(addr) => addr, | ||
| 65 | Address::TenBit(addr) => (addr & 0xFF) as u8, | ||
| 66 | }; | ||
| 67 | |||
| 68 | info!("I2C: Received write command - Address 0x{:02X}", addr_val); | ||
| 69 | let mut data_buffer = I2C_BUFFER.lock().await; | ||
| 70 | |||
| 71 | match i2c_slave.blocking_respond_to_write(&mut *data_buffer) { | ||
| 72 | Ok(bytes_received) => { | ||
| 73 | info!( | ||
| 74 | "I2C: Received {} bytes - Buffer now contains: 0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X}", | ||
| 75 | bytes_received, | ||
| 76 | data_buffer[0], | ||
| 77 | data_buffer[1], | ||
| 78 | data_buffer[2], | ||
| 79 | data_buffer[3], | ||
| 80 | data_buffer[4], | ||
| 81 | data_buffer[5], | ||
| 82 | data_buffer[6], | ||
| 83 | data_buffer[7] | ||
| 84 | ); | ||
| 85 | } | ||
| 86 | Err(e) => { | ||
| 87 | error!("I2C: Write error: {}", format_i2c_error(&e)); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | Ok(SlaveCommand { | ||
| 93 | kind: SlaveCommandKind::Read, | ||
| 94 | address, | ||
| 95 | }) => { | ||
| 96 | let addr_val = match address { | ||
| 97 | Address::SevenBit(addr) => addr, | ||
| 98 | Address::TenBit(addr) => (addr & 0xFF) as u8, // Show low byte for 10-bit | ||
| 99 | }; | ||
| 100 | |||
| 101 | info!("I2C: Received read command - Address 0x{:02X}", addr_val); | ||
| 102 | let data_buffer = I2C_BUFFER.lock().await; | ||
| 103 | |||
| 104 | match i2c_slave.blocking_respond_to_read(&data_buffer[..BUFFER_SIZE]) { | ||
| 105 | Ok(bytes_sent) => { | ||
| 106 | info!("I2C: Responded to read - {} bytes sent", bytes_sent); | ||
| 107 | } | ||
| 108 | Err(e) => { | ||
| 109 | error!("I2C: Read error: {}", format_i2c_error(&e)); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | Err(e) => { | ||
| 115 | error!("I2C: Listen error: {}", format_i2c_error(&e)); | ||
| 116 | Timer::after(Duration::from_millis(100)).await; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | fn format_i2c_error(e: &embassy_stm32::i2c::Error) -> &'static str { | ||
| 123 | match e { | ||
| 124 | embassy_stm32::i2c::Error::Bus => "Bus", | ||
| 125 | embassy_stm32::i2c::Error::Arbitration => "Arbitration", | ||
| 126 | embassy_stm32::i2c::Error::Nack => "Nack", | ||
| 127 | embassy_stm32::i2c::Error::Timeout => "Timeout", | ||
| 128 | embassy_stm32::i2c::Error::Crc => "Crc", | ||
| 129 | embassy_stm32::i2c::Error::Overrun => "Overrun", | ||
| 130 | embassy_stm32::i2c::Error::ZeroLengthTransfer => "ZeroLengthTransfer", | ||
| 131 | } | ||
| 132 | } | ||
