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

Add ADC usage (for stm32f303 devices) #47

Merged
merged 32 commits into from
Jul 19, 2020
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b6934fe
working example for 303xB/C/D/E
strom-und-spiele Nov 22, 2019
40a3a2c
add example for adc
strom-und-spiele Nov 22, 2019
2d20228
add dev-dependencies for examples
strom-und-spiele Nov 22, 2019
611f219
add sub versions stm32f303xx to adc
strom-und-spiele Nov 28, 2019
62716c3
Add ADC34 and accessibility to ADC register
strom-und-spiele Nov 28, 2019
13857e3
Extend example to adc3
strom-und-spiele Nov 28, 2019
dc7f63a
Add ADC support for all f303 sub variants.
strom-und-spiele Dec 29, 2019
3a57d6b
Update ADC Code to stm32f3 bump
strom-und-spiele Dec 29, 2019
49d19db
Reworked ADC example to work with all f303 devices.
strom-und-spiele Dec 29, 2019
f1ee27c
Apply consistent namings from code review
strom-und-spiele Jan 6, 2020
e2d37c8
Update consistency 2
strom-und-spiele Jan 6, 2020
3e29640
Implement Default as Trait.
strom-und-spiele Jan 6, 2020
5b8b96a
Update Example
strom-und-spiele Jan 6, 2020
3e84d05
removing deprecated unsafe blocks from adc
strom-und-spiele Apr 7, 2020
1988a0d
Tread EOS flag better
strom-und-spiele Apr 7, 2020
204fa84
Make use of CKMODE_A
strom-und-spiele Apr 11, 2020
a09800b
Allows CKMODE to be set when initalizing the adc
strom-und-spiele Apr 11, 2020
86c049b
Update adc example with more context
strom-und-spiele Apr 12, 2020
c130f7a
drop panic-halt dependency
strom-und-spiele Jul 18, 2020
54cf1ed
replace stm32 by pac
strom-und-spiele Jul 18, 2020
d8a007f
update example comments
strom-und-spiele Jul 18, 2020
09af232
fixup! update example comments
strom-und-spiele Jul 18, 2020
9ea3dcf
refactoring based on review
strom-und-spiele Jul 18, 2020
1d812ae
update feature gates
strom-und-spiele Jul 18, 2020
5e86d48
remove u8 annotations
strom-und-spiele Jul 18, 2020
3258be6
resorted pin => channel mapping
strom-und-spiele Jul 18, 2020
a9832b9
panic on wrong settings instead of returning None obj
strom-und-spiele Jul 18, 2020
153b51a
Use descriptive methods on register calls
strom-und-spiele Jul 18, 2020
776c5fd
refactor CkMode and Align
strom-und-spiele Jul 18, 2020
0c472c7
-newlines and indents
strom-und-spiele Jul 18, 2020
ff7a430
fix some errors from the review
strom-und-spiele Jul 19, 2020
26610c6
Change default Sampletime to T_1
strom-und-spiele Jul 19, 2020
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Implement `InputPin` for `Output<OpenDrain>` pins ([#114](https://github.com/stm32-rs/stm32f3xx-hal/pull/114))
- 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))
- ADC support for `stm32f303` devices.

### Fixed

Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ default-features = false
panic-semihosting = "0.5"
usb-device = "0.2"
usbd-serial = "0.1"
cortex-m-semihosting = "0.3"

[features]
default = ["unproven"]
Expand Down Expand Up @@ -110,3 +111,7 @@ required-features = ["stm32f303"]
[[example]]
name = "serial_dma"
required-features = ["stm32f303"]

[[example]]
name = "adc"
required-features = ["stm32f303"]
59 changes: 59 additions & 0 deletions examples/adc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#![no_std]
#![no_main]

//! Example usage for ADC on STM32F303

extern crate panic_semihosting;

use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;

use stm32f3xx_hal::{adc, pac, prelude::*};

#[entry]
/// Main Thread
fn main() -> ! {
// Get peripherals, clocks and freeze them
let mut dp = pac::Peripherals::take().unwrap();
let mut rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut dp.FLASH.constrain().acr);

// set up adc1
let mut adc1 = adc::Adc::adc1(
dp.ADC1, // The ADC we are going to control
// The following is only needed to make sure the clock signal for the ADC is set up
// correctly.
&mut dp.ADC1_2,
&mut rcc.ahb,
adc::CkMode::default(),
clocks,
);

// Set up pin PA0 as analog pin.
// This pin is connected to the user button on the stm32f3discovery board.
let mut gpio_a = dp.GPIOA.split(&mut rcc.ahb);
let mut adc1_in1_pin = gpio_a.pa0.into_analog(&mut gpio_a.moder, &mut gpio_a.pupdr);

// Be aware that the values in the table below depend on the input of VREF.
// To have a stable VREF input, put a condensator and a volt limiting diode in front of it.
//
// Also know that integer division and the ADC hardware unit always round down.
// To make up for those errors, see this forum entry:
// [https://forum.allaboutcircuits.com/threads/why-adc-1024-is-correct-and-adc-1023-is-just-plain-wrong.80018/]
hprintln!("
The ADC has a 12 bit resolution, i.e. if your reference Value is 3V:
approx. ADC value | approx. volt value
==================+===================
0 | 0 mV
2048 | 1500 mV
4095 | 3000 mV

If you are using a STM32F3Discovery, PA0 is connected to the User Button.
Pressing it should connect the user Button to to HIGH and the value should change from 0 to 4095.
").expect("Error using hprintln.");

loop {
let adc1_in1_data: u16 = adc1.read(&mut adc1_in1_pin).expect("Error reading adc1.");
hprintln!("PA0 reads {}", adc1_in1_data).ok();
}
}
Loading