Skip to content

Add I2C slave #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- PWM complementary output capability for TIM1 with new example to demonstrate
- Implement interface for reading and writing to the internal flash memory and an example for demonstration.
- PWM output on complementary channels only for single channel timers (TIM16 + TIM17)
- I2C slave added

### Fixed

Expand Down
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ cortex-m-rt = "0.7"
panic-halt = "0.2"
usb-device = "0.2.7"
usbd-serial = "0.1.1"
rtt-target = "0.5.0"
cortex-m = { version = "0.7.6", features = ["critical-section-single-core"]}


[features]
device-selected = []
Expand Down Expand Up @@ -98,3 +101,7 @@ required-features = ["stm32f042", "rt"]
[[example]]
name = "usb_serial"
required-features = ["rt", "stm32f042", "stm32-usbd"]

[[example]]
name = "i2c_slave"
required-features = ["stm32f030x4", "rt"]
89 changes: 89 additions & 0 deletions examples/i2c_slave.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#![no_main]
#![no_std]

use crate::hal::{
gpio::*,
pac::{interrupt, Interrupt, Peripherals},
};
use cortex_m_rt::entry;
use panic_halt as _;

use rtt_target::{rprintln, rtt_init_print};
use stm32f0xx_hal::i2c_slave::{self, I2CSlave, State};
use stm32f0xx_hal::{self as hal, prelude::*};

use core::cell::RefCell;
use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals};
type SCL = gpioa::PA9<Alternate<AF4>>;
type SDA = gpioa::PA10<Alternate<AF4>>;
type I2C = hal::pac::I2C1;
// Make I2C pin globally available
static GI2C: Mutex<RefCell<Option<I2CSlave<I2C, SCL, SDA>>>> = Mutex::new(RefCell::new(None));

#[interrupt]
fn I2C1() {
static mut I2C: Option<I2CSlave<I2C, SCL, SDA>> = None;

let i2c = I2C.get_or_insert_with(|| {
cortex_m::interrupt::free(|cs| {
// Move I2C pin here, leaving a None in its place
GI2C.borrow(cs).replace(None).unwrap()
})
});
match i2c.interrupt() {
Ok(State::Buzy(flag)) => {
rprintln!("I2C is busy {:?}", flag);
}
Ok(State::DataReceived(reg)) => {
let data = i2c.get_received_data();
rprintln!("Reg: {:?} Data: {:?}", reg, data);
}
Ok(State::DataRequested(reg)) => {
rprintln!("Data requested: {:?}", reg);

if let Err(e) = i2c.send_data(Some(&[0x01, 0x02, 0x03])) {
rprintln!("Error {:?}", e);
}
}
Err(e) => {
rprintln!("Error {:?}", e);
}
}
}

static I2C_ADDR: u8 = 0x52;
#[entry]
fn main() -> ! {
rtt_init_print!();
rprintln!("Starting I2C Slave example...");
if let (Some(mut p), Some(cp)) = (Peripherals::take(), c_m_Peripherals::take()) {
cortex_m::interrupt::free(move |cs| {
let mut rcc = p
.RCC
.configure()
.sysclk(48.mhz())
.pclk(24.mhz())
.freeze(&mut p.FLASH);
let gpioa = p.GPIOA.split(&mut rcc);

// Configure pins for I2C
let sda = gpioa.pa10.into_alternate_af4(cs);
let scl = gpioa.pa9.into_alternate_af4(cs);
let i2c = i2c_slave::I2CSlave::i2c1_slave(p.I2C1, (scl, sda), I2C_ADDR, &mut rcc);
*GI2C.borrow(cs).borrow_mut() = Some(i2c);

// Enable I2C IRQ, set prio 1 and clear any pending IRQs
let mut nvic = cp.NVIC;
unsafe {
nvic.set_priority(Interrupt::I2C1, 1);
cortex_m::peripheral::NVIC::unmask(Interrupt::I2C1);
}

cortex_m::peripheral::NVIC::unpend(Interrupt::I2C1);
});
}

loop {
continue;
}
}
Loading
Loading