-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for ADC DMA. Update changelog.
- Loading branch information
1 parent
7f674b9
commit 2d53bde
Showing
3 changed files
with
138 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use cortex_m_semihosting::hprintln; | ||
use panic_semihosting as _; | ||
|
||
use rtic::cyccnt::U32Ext; | ||
|
||
use stm32f4xx_hal::{ | ||
adc::{ | ||
config::{AdcConfig, Dma, SampleTime, Scan, Sequence}, | ||
Adc, Temperature, | ||
}, | ||
dma::{config::DmaConfig, Channel0, PeripheralToMemory, Stream0, StreamsTuple, Transfer}, | ||
prelude::*, | ||
signature::{VtempCal110, VtempCal30}, | ||
stm32, | ||
stm32::{ADC1, DMA2}, | ||
}; | ||
|
||
const POLLING_PERIOD: u32 = 168_000_000 / 2; | ||
|
||
type DMATransfer = | ||
Transfer<Stream0<DMA2>, Channel0, Adc<ADC1>, PeripheralToMemory, &'static mut [u16; 2]>; | ||
|
||
#[rtic::app(device = stm32f4xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] | ||
const APP: () = { | ||
struct Resources { | ||
transfer: DMATransfer, | ||
} | ||
|
||
#[init(schedule=[polling])] | ||
fn init(cx: init::Context) -> init::LateResources { | ||
let device: stm32::Peripherals = cx.device; | ||
|
||
let rcc = device.RCC.constrain(); | ||
let _clocks = rcc | ||
.cfgr | ||
.use_hse(25.mhz()) | ||
.require_pll48clk() | ||
.sysclk(84.mhz()) | ||
.hclk(84.mhz()) | ||
.pclk1(42.mhz()) | ||
.pclk2(84.mhz()) | ||
.freeze(); | ||
|
||
let gpiob = device.GPIOB.split(); | ||
let voltage = gpiob.pb1.into_analog(); | ||
|
||
let dma = StreamsTuple::new(device.DMA2); | ||
|
||
let config = DmaConfig::default() | ||
.transfer_complete_interrupt(true) | ||
.memory_increment(true) | ||
.double_buffer(true); | ||
|
||
let adc_config = AdcConfig::default() | ||
.dma(Dma::Continuous) | ||
.scan(Scan::Enabled); | ||
|
||
let mut adc = Adc::adc1(device.ADC1, true, adc_config); | ||
adc.configure_channel(&Temperature, Sequence::One, SampleTime::Cycles_480); | ||
adc.configure_channel(&voltage, Sequence::Two, SampleTime::Cycles_480); | ||
adc.enable_temperature_and_vref(); | ||
|
||
let first_buffer = cortex_m::singleton!(: [u16; 2] = [0; 2]).unwrap(); | ||
let second_buffer = cortex_m::singleton!(: [u16; 2] = [0; 2]).unwrap(); | ||
let transfer = Transfer::init(dma.0, adc, first_buffer, Some(second_buffer), config); | ||
|
||
let now = cx.start; | ||
cx.schedule.polling(now + POLLING_PERIOD.cycles()).unwrap(); | ||
|
||
init::LateResources { transfer } | ||
} | ||
|
||
#[task(resources = [transfer], schedule = [polling])] | ||
fn polling(cx: polling::Context) { | ||
let transfer: &mut DMATransfer = cx.resources.transfer; | ||
transfer.start(|adc| { | ||
adc.start_conversion(); | ||
}); | ||
|
||
cx.schedule | ||
.polling(cx.scheduled + POLLING_PERIOD.cycles()) | ||
.unwrap(); | ||
} | ||
|
||
#[task(binds = DMA2_STREAM0, resources = [transfer])] | ||
fn dma(cx: dma::Context) { | ||
let transfer: &mut DMATransfer = cx.resources.transfer; | ||
|
||
let result = unsafe { | ||
transfer.next_transfer_with(|data, _| { | ||
let raw_temp = data[0]; | ||
let raw_volt = data[1]; | ||
(data, (raw_temp, raw_volt)) | ||
}) | ||
} | ||
.unwrap(); | ||
|
||
let cal30 = VtempCal30::get().read() as f32; | ||
let cal110 = VtempCal110::get().read() as f32; | ||
|
||
let temperature = (110.0 - 30.0) * ((result.0 as f32) - cal30) / (cal110 - cal30) + 30.0; | ||
let voltage = (result.1 as f32) / ((2_i32.pow(12) - 1) as f32) * 3.3; | ||
|
||
hprintln!("temperature: {}, voltage: {}", temperature, voltage).unwrap(); | ||
} | ||
|
||
extern "C" { | ||
fn EXTI0(); | ||
} | ||
}; |