Skip to content

Commit

Permalink
Add SPI examples for most Tier 1 BSPs
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeaurivage committed Oct 23, 2024
1 parent 24a42eb commit 5a9ed71
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 26 deletions.
11 changes: 11 additions & 0 deletions boards/atsame54_xpro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,21 @@ rtt-target = { version = "0.3", features = ["cortex-m"] }

[features]
default = ["rt", "atsamd-hal/same54p"]
dma = ["atsamd-hal/dma"]
rt = ["cortex-m-rt", "atsamd-hal/same54p-rt"]
usb = ["atsamd-hal/usb", "usb-device"]
can = ["atsamd-hal/can"]

[[example]]
name = "blinky_basic"

[[example]]
name = "blinky_rtic"

[[example]]
name = "mcan"
required-features = ["can"]

[[example]]
name = "spi"
required-features = ["dma"]
81 changes: 81 additions & 0 deletions boards/atsame54_xpro/examples/spi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//! This example showcases the i2c module.
#![no_std]
#![no_main]

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

use atsame54_xpro as bsp;

use bsp::entry;
use bsp::hal;
use bsp::pac;

use pac::Peripherals;

use hal::clock::GenericClockController;
use hal::dmac::{DmaController, PriorityLevel};
use hal::ehal::spi::SpiBus;
use hal::fugit::RateExtU32;

#[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 dmac = peripherals.dmac;
let pins = bsp::Pins::new(peripherals.port);

// Take SPI pins
let (miso, mosi, sclk) = (pins.miso, pins.mosi, pins.sclk);

// 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 chan1 = channels.1.init(PriorityLevel::Lvl0);

let mut spi = bsp::ext1_spi(
&mut clocks,
100.kHz(),
peripherals.sercom4,
&mut peripherals.mclk,
sclk,
mosi,
miso,
)
.with_dma_channels(chan0, chan1);

loop {
let mut source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
let mut dest = [0xff; 16];

// Read words into a buffer. The words sent will be be NOP word
// (by default, 0x00).
spi.read(&mut dest).unwrap();

// Send words from a buffer
spi.write(&source).unwrap();

// Simultaneously read and write from different buffers.
//
// If the source is longer than the destination, the words read
// in excess will be discarded.
//
// If the destination is longer than the source, the excess words
// sent will be the NOP word (by default, 0x00).
spi.transfer(&mut dest, &source).unwrap();

// Simultaneously read and write from the same buffer
spi.transfer_in_place(&mut source).unwrap();
}
}
4 changes: 4 additions & 0 deletions boards/feather_m0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,7 @@ required-features = ["dma"]
[[example]]
name = "i2c"
required-features = ["dma"]

[[example]]
name = "spi"
required-features = ["dma"]
82 changes: 82 additions & 0 deletions boards/feather_m0/examples/spi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//! This example showcases the i2c module.
#![no_std]
#![no_main]

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

use feather_m0 as bsp;

use bsp::entry;
use bsp::hal;
use bsp::pac;

use pac::Peripherals;

use hal::clock::GenericClockController;
use hal::dmac::{DmaController, PriorityLevel};
use hal::ehal::spi::SpiBus;
use hal::fugit::RateExtU32;

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

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

// Take SPI pins
let (miso, mosi, sclk) = (pins.miso, pins.mosi, pins.sclk);

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

// Create a Spi with DMA enabled
let mut spi = bsp::spi_master(
&mut clocks,
100.kHz(),
peripherals.sercom4,
&mut pm,
sclk,
mosi,
miso,
)
.with_dma_channels(chan0, chan1);

loop {
let mut source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
let mut dest = [0xff; 16];

// Read words into a buffer. The words sent will be be NOP word
// (by default, 0x00).
spi.read(&mut dest).unwrap();

// Send words from a buffer
spi.write(&source).unwrap();

// Simultaneously read and write from different buffers.
//
// If the source is longer than the destination, the words read
// in excess will be discarded.
//
// If the destination is longer than the source, the excess words
// sent will be the NOP word (by default, 0x00).
spi.transfer(&mut dest, &source).unwrap();

// Simultaneously read and write from the same buffer
spi.transfer_in_place(&mut source).unwrap();
}
}
6 changes: 5 additions & 1 deletion boards/feather_m4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ required-features = [ "usb"]

[[example]]
name = "i2c"
required-features = ["atsamd-hal/dma"]
required-features = ["dma"]

[[example]]
name = "spi"
required-features = ["dma"]
81 changes: 81 additions & 0 deletions boards/feather_m4/examples/spi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//! This example showcases the i2c module.
#![no_std]
#![no_main]

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

use feather_m4 as bsp;

use bsp::entry;
use bsp::hal;
use bsp::pac;

use pac::Peripherals;

use hal::clock::GenericClockController;
use hal::dmac::{DmaController, PriorityLevel};
use hal::ehal::spi::SpiBus;
use hal::fugit::RateExtU32;

#[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 dmac = peripherals.dmac;
let pins = bsp::Pins::new(peripherals.port);

// Take SPI pins
let (miso, mosi, sclk) = (pins.miso, pins.mosi, pins.sclk);

// 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 chan1 = channels.1.init(PriorityLevel::Lvl0);

let mut spi = bsp::spi_master(
&mut clocks,
100.kHz(),
peripherals.sercom1,
&mut peripherals.mclk,
sclk,
mosi,
miso,
)
.with_dma_channels(chan0, chan1);

loop {
let mut source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
let mut dest = [0xff; 16];

// Read words into a buffer. The words sent will be be NOP word
// (by default, 0x00).
spi.read(&mut dest).unwrap();

// Send words from a buffer
spi.write(&source).unwrap();

// Simultaneously read and write from different buffers.
//
// If the source is longer than the destination, the words read
// in excess will be discarded.
//
// If the destination is longer than the source, the excess words
// sent will be the NOP word (by default, 0x00).
spi.transfer(&mut dest, &source).unwrap();

// Simultaneously read and write from the same buffer
spi.transfer_in_place(&mut source).unwrap();
}
}
4 changes: 4 additions & 0 deletions boards/metro_m0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ name = "blinky_basic"
[[example]]
name = "i2c"
required-features = ["dma"]

[[example]]
name = "spi"
required-features = ["dma"]
82 changes: 82 additions & 0 deletions boards/metro_m0/examples/spi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//! This example showcases the i2c module.
#![no_std]
#![no_main]

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

use metro_m0 as bsp;

use bsp::entry;
use bsp::hal;
use bsp::pac;

use pac::Peripherals;

use hal::clock::GenericClockController;
use hal::dmac::{DmaController, PriorityLevel};
use hal::ehal::spi::SpiBus;
use hal::fugit::RateExtU32;

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

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

// Take SPI pins
let (miso, mosi, sclk) = (pins.miso, pins.mosi, pins.sclk);

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

// Create a Spi with DMA enabled
let mut spi = bsp::spi_master(
&mut clocks,
100.kHz(),
peripherals.sercom4,
&mut pm,
sclk,
mosi,
miso,
)
.with_dma_channels(chan0, chan1);

loop {
let mut source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
let mut dest = [0xff; 16];

// Read words into a buffer. The words sent will be be NOP word
// (by default, 0x00).
spi.read(&mut dest).unwrap();

// Send words from a buffer
spi.write(&source).unwrap();

// Simultaneously read and write from different buffers.
//
// If the source is longer than the destination, the words read
// in excess will be discarded.
//
// If the destination is longer than the source, the excess words
// sent will be the NOP word (by default, 0x00).
spi.transfer(&mut dest, &source).unwrap();

// Simultaneously read and write from the same buffer
spi.transfer_in_place(&mut source).unwrap();
}
}
7 changes: 4 additions & 3 deletions boards/metro_m4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ name = "pwm"
[[example]]
name = "serial"

[[example]]
name = "spi"

[[example]]
name = "timer"

Expand All @@ -93,3 +90,7 @@ required-features = ["usb"]
[[example]]
name = "i2c"
required-features = ["dma"]

[[example]]
name = "spi"
required-features = ["dma"]
Loading

0 comments on commit 5a9ed71

Please sign in to comment.