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

Initial DMA support #86

Merged
merged 17 commits into from
Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Support for safe one-shot DMA transfers ([#86](https://github.com/stm32-rs/stm32f3xx-hal/pull/86))
- DMA support for serial reception and transmission ([#86](https://github.com/stm32-rs/stm32f3xx-hal/pull/86))

### Fixed

- `PLL` was calculated wrong for devices, which do not divide `HSI` ([#67](https://github.com/stm32-rs/stm32f3xx-hal/pull/67))
Expand Down
14 changes: 11 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ version = "0.2"
features = ["const-fn"]

[dependencies.cast]
default-features = false
version = "0.2"

[dependencies.void]
default-features = false

[dependencies.stable_deref_trait]
version = "1"
default-features = false

[dependencies.stm32-usbd]
version = "0.5"
optional = true

[dependencies.void]
version = "1"
default-features = false

[dev-dependencies]
panic-semihosting = "0.5"
usb-device = "0.2"
Expand Down Expand Up @@ -102,3 +106,7 @@ required-features = ["rt", "stm32f303xc", "stm32-usbd"]
[[example]]
name = "spi"
required-features = ["stm32f303"]

[[example]]
name = "serial_dma"
required-features = ["stm32f303"]
68 changes: 68 additions & 0 deletions examples/serial_dma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! Example of transmitting data over serial interface using DMA.
//! For this to work, the PA9 and PA10 pins must be connected.
//! Target board: STM32F3DISCOVERY

#![no_std]
#![no_main]

use panic_semihosting as _;

use cortex_m::singleton;
use cortex_m_rt::entry;
use stm32f3xx_hal::{prelude::*, serial::Serial, stm32};

#[entry]
fn main() -> ! {
let dp = stm32::Peripherals::take().unwrap();

let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);

let pins = (
gpioa.pa9.into_af7(&mut gpioa.moder, &mut gpioa.afrh),
gpioa.pa10.into_af7(&mut gpioa.moder, &mut gpioa.afrh),
);
let serial = Serial::usart1(dp.USART1, pins, 9600.bps(), clocks, &mut rcc.apb2);
let (tx, rx) = serial.split();

let dma1 = dp.DMA1.split(&mut rcc.ahb);

// the data we are going to send over serial
let tx_buf = singleton!(: [u8; 9] = *b"hello DMA").unwrap();
// the buffer we are going to receive the transmitted data in
let rx_buf = singleton!(: [u8; 9] = [0; 9]).unwrap();

// DMA channel selection depends on the peripheral:
// - USART1: TX = 4, RX = 5
// - USART2: TX = 6, RX = 7
// - USART3: TX = 3, RX = 2
let (tx_channel, rx_channel) = (dma1.ch4, dma1.ch5);

// start separate DMAs for sending and receiving the data
let sending = tx.write_all(tx_buf, tx_channel);
let receiving = rx.read_exact(rx_buf, rx_channel);

// block until all data was transmitted and received
let (tx_buf, tx_channel, tx) = sending.wait();
let (rx_buf, rx_channel, rx) = receiving.wait();

assert_eq!(tx_buf, rx_buf);

// After a transfer is finished its parts can be re-used for another one.
tx_buf.copy_from_slice(b"hi again!");

let sending = tx.write_all(tx_buf, tx_channel);
let receiving = rx.read_exact(rx_buf, rx_channel);

let (tx_buf, ..) = sending.wait();
let (rx_buf, ..) = receiving.wait();

assert_eq!(tx_buf, rx_buf);

loop {
continue;
}
}
Loading