-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: Add DMA examples for SPI, I2C, and UART to Tier 1 BSPs
- Loading branch information
1 parent
220d135
commit ebbe7d1
Showing
20 changed files
with
788 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! This example showcases the spi module, and uses DMA to perform SPI | ||
//! transactions. | ||
#![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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//! This example shows how to use the UART to perform blocking transfers using | ||
//! DMA and the embedded-io traits. | ||
#![no_std] | ||
#![no_main] | ||
|
||
#[cfg(not(feature = "use_semihosting"))] | ||
use panic_halt as _; | ||
#[cfg(feature = "use_semihosting")] | ||
use panic_semihosting as _; | ||
|
||
use bsp::hal; | ||
use bsp::pac; | ||
use feather_m0 as bsp; | ||
|
||
use bsp::{entry, periph_alias, pin_alias}; | ||
use hal::clock::GenericClockController; | ||
use hal::dmac::{DmaController, PriorityLevel}; | ||
use hal::embedded_io::{Read, Write}; | ||
use hal::fugit::RateExtU32; | ||
|
||
use pac::Peripherals; | ||
|
||
#[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); | ||
|
||
// 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); | ||
|
||
// Take peripheral and pins | ||
let uart_sercom = periph_alias!(peripherals.uart_sercom); | ||
let uart_rx = pin_alias!(pins.uart_rx); | ||
let uart_tx = pin_alias!(pins.uart_tx); | ||
|
||
// Setup UART peripheral and attach DMA channels | ||
let uart = bsp::uart( | ||
&mut clocks, | ||
9600.Hz(), | ||
uart_sercom, | ||
&mut pm, | ||
uart_rx, | ||
uart_tx, | ||
) | ||
.with_rx_channel(chan0) | ||
.with_tx_channel(chan1); | ||
|
||
// Split uart in rx + tx halves | ||
let (mut rx, mut tx) = uart.split(); | ||
|
||
loop { | ||
// Make buffers to store data to send/receive | ||
let mut rx_buffer = [0x00; 50]; | ||
let mut tx_buffer = [0x00; 50]; | ||
|
||
// For fun, store numbers from 0 to 49 in buffer | ||
for (i, c) in tx_buffer.iter_mut().enumerate() { | ||
*c = i as u8; | ||
} | ||
|
||
// Send data using DMA... | ||
tx.write(&tx_buffer).unwrap(); | ||
|
||
//...and receive using DMA | ||
rx.flush_rx_buffer(); | ||
rx.read(&mut rx_buffer).unwrap(); | ||
} | ||
} |
Oops, something went wrong.