Skip to content
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

grand_central_m4(feat): Adding i2c example #787

Merged
merged 2 commits into from
Nov 21, 2024
Merged
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
6 changes: 6 additions & 0 deletions boards/grand_central_m4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ ws2812-timer-delay = "0.3"

[features]
default = ["rt", "atsamd-hal/samd51p"]
dma = ["atsamd-hal/dma"]
supersimple33 marked this conversation as resolved.
Show resolved Hide resolved
max-channels = ["dma", "atsamd-hal/max-channels"]
rt = ["cortex-m-rt", "atsamd-hal/samd51p-rt"]
usb = ["atsamd-hal/usb", "usb-device"]

Expand All @@ -47,6 +49,10 @@ chip = "ATSAMD51P20A"
[[example]]
name = "blinky_basic"

[[example]]
name = "i2c"
required-features = ["dma"]

[[example]]
name = "eic"

Expand Down
72 changes: 72 additions & 0 deletions boards/grand_central_m4/examples/i2c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//! This example showcases the i2c module, and uses DMA to perform I2C
//! transactions.

#![no_std]
#![no_main]

#[cfg(not(feature = "use_semihosting"))]
use panic_halt as _;
#[cfg(feature = "use_semihosting")]
use panic_semihosting as _;

use grand_central_m4 as bsp;

use bsp::hal;
use bsp::pac;
use bsp::{entry, periph_alias, pin_alias};

use cortex_m::asm;
use pac::Peripherals;

use hal::clock::GenericClockController;
use hal::dmac::{DmaController, PriorityLevel};
use hal::ehal::i2c::I2c;
use hal::fugit::RateExtU32;
use hal::sercom::i2c;

// This example is based on the BMP388 pressure sensor. Adjust the device and
// register addresses to your liking
const ADDRESS: u8 = 0x76;

#[entry]
fn main() -> ! {
let mut peripherals = Peripherals::take().unwrap();
let mut clocks = GenericClockController::with_external_32kosc(
peripherals.gclk,
&mut peripherals.mclk,
&mut peripherals.osc32kctrl,
&mut peripherals.oscctrl,
&mut peripherals.nvmctrl,
);

let mclk = peripherals.mclk;
let dmac = peripherals.dmac;
let pins = bsp::Pins::new(peripherals.port);

// Take SDA and SCL
let (sda, scl) = (pin_alias!(pins.sda), pin_alias!(pins.scl));

// Setup DMA channels for later use
let mut dmac = DmaController::init(dmac, &mut peripherals.pm);
let channels = dmac.split();
let chan0 = channels.0.init(PriorityLevel::Lvl0);

let gclk0 = clocks.gclk0();
let sercom5_clock = &clocks.sercom5_core(&gclk0).unwrap();
let pads = i2c::Pads::new(sda, scl);
let i2c_sercom = periph_alias!(peripherals.i2c_sercom);
let mut i2c = i2c::Config::new(&mclk, i2c_sercom, pads, sercom5_clock.freq())
.baud(100.kHz())
.enable()
.with_dma_channel(chan0);

let mut received = [0x00; 1];

// Test writing then reading from an I2C chip
i2c.write_read(ADDRESS, &[0x00; 8], &mut received).unwrap();

loop {
// Go to sleep
asm::wfi();
}
}