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

feat: add spi rx dma (and migrate to embedded-dma) #165

Closed
Closed
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ edition = "2018"
cortex-m = "0.6.3"
nb = "0.1.1"
stm32l4 = "0.13.0"
embedded-dma = "0.1"

[dependencies.rand_core]
version = "0.6.2"
Expand Down
14 changes: 8 additions & 6 deletions examples/rtic_frame_serial_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use hal::{
dma::{self, DMAFrame, FrameReader, FrameSender},
pac::USART2,
prelude::*,
rcc::{ClockSecuritySystem, CrystalBypass, MsiFreq},
serial::{self, Config, Serial},
Expand All @@ -23,6 +24,8 @@ use heapless::{
use panic_halt as _;
use rtic::app;
use stm32l4xx_hal as hal;
use stm32l4xx_hal::dma::{RxDma, TxDma};
use stm32l4xx_hal::serial::{Rx, Tx};

// The pool gives out `Box<DMAFrame>`s that can hold 8 bytes
pool!(
Expand All @@ -34,8 +37,8 @@ pool!(
const APP: () = {
struct Resources {
rx: serial::Rx<hal::stm32::USART2>,
frame_reader: FrameReader<Box<SerialDMAPool>, dma::dma1::C6, 8>,
frame_sender: FrameSender<Box<SerialDMAPool>, dma::dma1::C7, 8>,
frame_reader: FrameReader<Box<SerialDMAPool>, RxDma<Rx<USART2>, dma::dma1::C6>, 8>,
frame_sender: FrameSender<Box<SerialDMAPool>, TxDma<Tx<USART2>, dma::dma1::C7>, 8>,
}

#[init]
Expand Down Expand Up @@ -88,16 +91,15 @@ const APP: () = {
let fr = if let Some(dma_buf) = SerialDMAPool::alloc() {
// Set up the first reader frame
let dma_buf = dma_buf.init(DMAFrame::new());
serial_rx.frame_read(dma_ch6, dma_buf)
serial_rx.with_dma(dma_ch6).frame_reader(dma_buf)
} else {
unreachable!()
};

// Serial frame sender (DMA based)
let fs: FrameSender<Box<SerialDMAPool>, _, 8> = serial_tx.frame_sender(dma_ch7);
let fs: FrameSender<Box<SerialDMAPool>, _, 8> = serial_tx.with_dma(dma_ch7).frame_sender();

init::LateResources {
rx: serial_rx,
frame_reader: fr,
frame_sender: fs,
}
Expand All @@ -109,7 +111,7 @@ const APP: () = {
#[task(binds = USART2, resources = [rx, frame_reader, frame_sender], priority = 3)]
fn serial_isr(cx: serial_isr::Context) {
// Check for character match
if cx.resources.rx.is_character_match(true) {
if cx.resources.frame_reader.check_character_match(true) {
if let Some(dma_buf) = SerialDMAPool::alloc() {
let dma_buf = dma_buf.init(DMAFrame::new());
let buf = cx.resources.frame_reader.character_match_interrupt(dma_buf);
Expand Down
4 changes: 2 additions & 2 deletions examples/serial_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern crate stm32l4xx_hal as hal;
// #[macro_use(block)]
// extern crate nb;

use crate::hal::dma::Half;
use crate::hal::dma::{CircReadDma, Half};
use crate::hal::prelude::*;
use crate::hal::serial::{Config, Serial};
use crate::rt::ExceptionFrame;
Expand Down Expand Up @@ -68,7 +68,7 @@ fn main() -> ! {

let buf = singleton!(: [[u8; 8]; 2] = [[0; 8]; 2]).unwrap();

let mut circ_buffer = rx.circ_read(channels.5, buf);
let mut circ_buffer = rx.with_dma(channels.5).circ_read(buf);

for _ in 0..2 {
while circ_buffer.readable_half().unwrap() != Half::First {}
Expand Down
3 changes: 2 additions & 1 deletion examples/serial_dma_partial_peek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern crate stm32l4xx_hal as hal;

use crate::cortex_m::asm;
use crate::hal::delay::Delay;
use crate::hal::dma::CircReadDma;
use crate::hal::prelude::*;
use crate::hal::serial::{Config, Serial};
use crate::rt::ExceptionFrame;
Expand Down Expand Up @@ -71,7 +72,7 @@ fn main() -> ! {

let buf = singleton!(: [[u8; 8]; 2] = [[0; 8]; 2]).unwrap();

let mut circ_buffer = rx.circ_read(channels.5, buf);
let mut circ_buffer = rx.with_dma(channels.5).circ_read(buf);

// wait for 3 seconds, enter data on serial
timer.delay_ms(1000_u32);
Expand Down
4 changes: 2 additions & 2 deletions examples/serial_dma_us2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern crate stm32l4xx_hal as hal;
// #[macro_use(block)]
// extern crate nb;

use crate::hal::dma::Half;
use crate::hal::dma::{CircReadDma, Half};
use crate::hal::prelude::*;
use crate::hal::serial::{Config, Serial};
use crate::rt::ExceptionFrame;
Expand Down Expand Up @@ -67,7 +67,7 @@ fn main() -> ! {

let buf = singleton!(: [[u8; 8]; 2] = [[0; 8]; 2]).unwrap();

let mut circ_buffer = rx.circ_read(channels.6, buf);
let mut circ_buffer = rx.with_dma(channels.6).circ_read(buf);

for _ in 0..2 {
while circ_buffer.readable_half().unwrap() != Half::First {}
Expand Down
Loading