Skip to content

Commit

Permalink
tried critical section devices
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkamprath committed Sep 7, 2024
1 parent 49781fc commit 627e0ab
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ bench = false
[dependencies]
cortex-m = "0.7"
cortex-m-rt = "0.7"
critical-section = "1.1"
embedded-hal = { version = "1.0", features = ["defmt-03"] }
embedded-sdmmc = { version = "0.8", default-features = false, features = [
"defmt-log",
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ fn main() -> ! {
clocks.system_clock.freq(),
);
let i2c_ref_cell = RefCell::new(i2c);
let i2c_mutex = critical_section::Mutex::new(i2c_ref_cell);

// set up SPI
#[allow(clippy::type_complexity)]
let spi: rp_pico::hal::Spi<
Expand Down Expand Up @@ -170,7 +172,7 @@ fn main() -> ! {
channel_b,
pins.gpio14.into_pull_up_input(),
pins.gpio15.into_pull_up_input(),
&i2c_ref_cell,
&i2c_mutex,
pins.gpio21.into_pull_up_input(),
pins.gpio20.into_pull_up_input(),
sd,
Expand Down
12 changes: 7 additions & 5 deletions src/robot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ pub struct Robot<
motors: MotorController<INA1, INA2, INB1, INB2, ENA, ENB>,
button1: DebouncedButton<BUTT1, false, BUTTON_DEBOUNCE_TIME_MS>,
button2: DebouncedButton<BUTT2, false, BUTTON_DEBOUNCE_TIME_MS>,
pub heading_calculator: HeadingCalculator<embedded_hal_bus::i2c::RefCellDevice<'a, TWI>, DELAY>,
lcd: LcdBackpack<embedded_hal_bus::i2c::RefCellDevice<'a, TWI>, DELAY>,
pub heading_calculator:
HeadingCalculator<embedded_hal_bus::i2c::CriticalSectionDevice<'a, TWI>, DELAY>,
lcd: LcdBackpack<embedded_hal_bus::i2c::CriticalSectionDevice<'a, TWI>, DELAY>,
pub sd_card: FileStorage<SPI_DEV, DELAY>,
reset_display_start_millis: u32,
log_index: u32,
Expand Down Expand Up @@ -147,7 +148,7 @@ where
duty_b: ENB,
button1_pin: BUTT1,
button2_pin: BUTT2,
i2c_refcell: &'a RefCell<TWI>,
i2c_refcell: &'a critical_section::Mutex<RefCell<TWI>>,
left_counter_pin: LeftWheelCounterPin,
right_counter_pin: RightWheelCounterPin,
mut sd_card: FileStorage<SPI_DEV, DELAY>,
Expand All @@ -171,7 +172,8 @@ where
pac::NVIC::unmask(pac::Interrupt::IO_IRQ_BANK0);
}

let i2c_device = embedded_hal_bus::i2c::RefCellDevice::new(i2c_refcell);
// let i2c_device = embedded_hal_bus::i2c::RefCellDevice::new(i2c_refcell);
let i2c_device = embedded_hal_bus::i2c::CriticalSectionDevice::new(i2c_refcell);
let mut lcd = LcdBackpack::new(LcdDisplayType::Lcd16x2, i2c_device, delay.clone());
match lcd.init() {
Ok(_) => {
Expand Down Expand Up @@ -211,7 +213,7 @@ where
}
delay.delay_ms(5000);
let mut heading_calculator = HeadingCalculator::new(
embedded_hal_bus::i2c::RefCellDevice::new(i2c_refcell),
embedded_hal_bus::i2c::CriticalSectionDevice::new(i2c_refcell),
delay,
);
heading_calculator.reset();
Expand Down

0 comments on commit 627e0ab

Please sign in to comment.