aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-04-09 23:37:22 +0200
committerDario Nieuwenhuis <[email protected]>2021-04-20 02:30:14 +0200
commit258ba533bdf8dc353513bbd5d409e8b12d307d5f (patch)
tree0eb01732e66103dec5063ca4427a941c47d11545
parentaa65d5ccaf0b144063d41d650e4b46c24172166c (diff)
Implement GPIO input
-rw-r--r--embassy-stm32-examples/src/bin/blinky.rs4
-rw-r--r--embassy-stm32-examples/src/bin/button.rs57
-rw-r--r--embassy-stm32/src/chip/f429.rs8
-rw-r--r--embassy-stm32/src/gpio.rs61
4 files changed, 101 insertions, 29 deletions
diff --git a/embassy-stm32-examples/src/bin/blinky.rs b/embassy-stm32-examples/src/bin/blinky.rs
index a90992be4..ec6c00fe1 100644
--- a/embassy-stm32-examples/src/bin/blinky.rs
+++ b/embassy-stm32-examples/src/bin/blinky.rs
@@ -43,10 +43,10 @@ fn main() -> ! {
43 loop { 43 loop {
44 info!("high"); 44 info!("high");
45 led.set_high().unwrap(); 45 led.set_high().unwrap();
46 cortex_m::asm::delay(1_000_000); 46 cortex_m::asm::delay(10_000_000);
47 47
48 info!("low"); 48 info!("low");
49 led.set_low().unwrap(); 49 led.set_low().unwrap();
50 cortex_m::asm::delay(1_000_000); 50 cortex_m::asm::delay(10_000_000);
51 } 51 }
52} 52}
diff --git a/embassy-stm32-examples/src/bin/button.rs b/embassy-stm32-examples/src/bin/button.rs
new file mode 100644
index 000000000..c02e34c09
--- /dev/null
+++ b/embassy-stm32-examples/src/bin/button.rs
@@ -0,0 +1,57 @@
1#![no_std]
2#![no_main]
3#![feature(trait_alias)]
4#![feature(min_type_alias_impl_trait)]
5#![feature(impl_trait_in_bindings)]
6#![feature(type_alias_impl_trait)]
7
8#[path = "../example_common.rs"]
9mod example_common;
10use embassy_stm32::gpio::{Input, Level, Output, Pull};
11use embedded_hal::digital::v2::{InputPin, OutputPin};
12use example_common::*;
13
14use cortex_m_rt::entry;
15use stm32f4::stm32f429 as pac;
16
17#[entry]
18fn main() -> ! {
19 info!("Hello World!");
20
21 let pp = pac::Peripherals::take().unwrap();
22
23 pp.DBGMCU.cr.modify(|_, w| {
24 w.dbg_sleep().set_bit();
25 w.dbg_standby().set_bit();
26 w.dbg_stop().set_bit()
27 });
28 pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
29
30 pp.RCC.ahb1enr.modify(|_, w| {
31 w.gpioaen().enabled();
32 w.gpioben().enabled();
33 w.gpiocen().enabled();
34 w.gpioden().enabled();
35 w.gpioeen().enabled();
36 w.gpiofen().enabled();
37 w
38 });
39
40 let p = embassy_stm32::Peripherals::take().unwrap();
41 let button = Input::new(p.PC13, Pull::Down);
42 let mut led1 = Output::new(p.PB0, Level::High);
43 let _led2 = Output::new(p.PB7, Level::High);
44 let mut led3 = Output::new(p.PB14, Level::High);
45
46 loop {
47 if button.is_high().unwrap() {
48 info!("high");
49 led1.set_high().unwrap();
50 led3.set_low().unwrap();
51 } else {
52 info!("low");
53 led1.set_low().unwrap();
54 led3.set_high().unwrap();
55 }
56 }
57}
diff --git a/embassy-stm32/src/chip/f429.rs b/embassy-stm32/src/chip/f429.rs
index 1c2fdc0c6..26956e01f 100644
--- a/embassy-stm32/src/chip/f429.rs
+++ b/embassy-stm32/src/chip/f429.rs
@@ -1,6 +1,10 @@
1use embassy_extras::peripherals; 1use embassy_extras::peripherals;
2 2
3peripherals!( 3peripherals!(
4 PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PA8, PA9, PA10, PA11, PA12, PA13, PA14, PA15, PB0, PB1, 4 // GPIO Port A
5 PB2, PB3, PB4, PB5, PB6, PB7, PB8, PB9, PB10, PB11, PB12, PB13, PB14, PB15, 5 PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PA8, PA9, PA10, PA11, PA12, PA13, PA14, PA15,
6 // GPIO Port B
7 PB0, PB1, PB2, PB3, PB4, PB5, PB6, PB7, PB8, PB9, PB10, PB11, PB12, PB13, PB14, PB15,
8 // GPIO Port C
9 PC0, PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10, PC11, PC12, PC13, PC14, PC15,
6); 10);
diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs
index 80fcbba24..f963400a7 100644
--- a/embassy-stm32/src/gpio.rs
+++ b/embassy-stm32/src/gpio.rs
@@ -5,9 +5,8 @@ use core::marker::PhantomData;
5use embassy::util::PeripheralBorrow; 5use embassy::util::PeripheralBorrow;
6use embassy_extras::{impl_unborrow, unborrow}; 6use embassy_extras::{impl_unborrow, unborrow};
7use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin}; 7use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin};
8use gpio::vals::{self, Pupdr}; 8use gpio::vals;
9 9
10use crate::pac;
11use crate::pac::gpio_v2 as gpio; 10use crate::pac::gpio_v2 as gpio;
12use crate::peripherals; 11use crate::peripherals;
13 12
@@ -27,7 +26,6 @@ pub enum Pull {
27 Down, 26 Down,
28} 27}
29 28
30/*
31/// GPIO input driver. 29/// GPIO input driver.
32pub struct Input<'d, T: Pin> { 30pub struct Input<'d, T: Pin> {
33 pub(crate) pin: T, 31 pub(crate) pin: T,
@@ -38,23 +36,17 @@ impl<'d, T: Pin> Input<'d, T> {
38 pub fn new(pin: impl PeripheralBorrow<Target = T> + 'd, pull: Pull) -> Self { 36 pub fn new(pin: impl PeripheralBorrow<Target = T> + 'd, pull: Pull) -> Self {
39 unborrow!(pin); 37 unborrow!(pin);
40 38
41 pin.conf().write(|w| { 39 cortex_m::interrupt::free(|_| unsafe {
42 w.dir().input(); 40 let r = pin.block();
43 w.input().connect(); 41 let n = pin.pin() as usize;
44 match pull { 42 let val = match pull {
45 Pull::None => { 43 Pull::None => vals::Pupdr::FLOATING,
46 w.pull().disabled(); 44 Pull::Up => vals::Pupdr::PULLUP,
47 } 45 Pull::Down => vals::Pupdr::PULLDOWN,
48 Pull::Up => { 46 };
49 w.pull().pullup(); 47 r.pupdr().modify(|w| w.set_pupdr(n, val));
50 } 48 r.otyper().modify(|w| w.set_ot(n, vals::Ot::PUSHPULL));
51 Pull::Down => { 49 r.moder().modify(|w| w.set_moder(n, vals::Moder::INPUT));
52 w.pull().pulldown();
53 }
54 }
55 w.drive().s0s1();
56 w.sense().disabled();
57 w
58 }); 50 });
59 51
60 Self { 52 Self {
@@ -66,7 +58,11 @@ impl<'d, T: Pin> Input<'d, T> {
66 58
67impl<'d, T: Pin> Drop for Input<'d, T> { 59impl<'d, T: Pin> Drop for Input<'d, T> {
68 fn drop(&mut self) { 60 fn drop(&mut self) {
69 self.pin.conf().reset(); 61 cortex_m::interrupt::free(|_| unsafe {
62 let r = self.pin.block();
63 let n = self.pin.pin() as usize;
64 r.pupdr().modify(|w| w.set_pupdr(n, vals::Pupdr::FLOATING));
65 });
70 } 66 }
71} 67}
72 68
@@ -78,12 +74,11 @@ impl<'d, T: Pin> InputPin for Input<'d, T> {
78 } 74 }
79 75
80 fn is_low(&self) -> Result<bool, Self::Error> { 76 fn is_low(&self) -> Result<bool, Self::Error> {
81 Ok(self.pin.block().in_.read().bits() & (1 << self.pin.pin()) == 0) 77 let state = unsafe { self.pin.block().idr().read().idr(self.pin.pin() as _) };
78 Ok(state == vals::Idr::LOW)
82 } 79 }
83} 80}
84 81
85*/
86
87/// Digital input or output level. 82/// Digital input or output level.
88#[derive(Debug, Eq, PartialEq)] 83#[derive(Debug, Eq, PartialEq)]
89#[cfg_attr(feature = "defmt", derive(defmt::Format))] 84#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -111,7 +106,6 @@ impl<'d, T: Pin> Output<'d, T> {
111 let r = pin.block(); 106 let r = pin.block();
112 let n = pin.pin() as usize; 107 let n = pin.pin() as usize;
113 r.pupdr().modify(|w| w.set_pupdr(n, vals::Pupdr::FLOATING)); 108 r.pupdr().modify(|w| w.set_pupdr(n, vals::Pupdr::FLOATING));
114 r.otyper().modify(|w| w.set_ot(n, vals::Ot::PUSHPULL));
115 r.moder().modify(|w| w.set_moder(n, vals::Moder::OUTPUT)); 109 r.moder().modify(|w| w.set_moder(n, vals::Moder::OUTPUT));
116 }); 110 });
117 111
@@ -127,6 +121,7 @@ impl<'d, T: Pin> Drop for Output<'d, T> {
127 cortex_m::interrupt::free(|_| unsafe { 121 cortex_m::interrupt::free(|_| unsafe {
128 let r = self.pin.block(); 122 let r = self.pin.block();
129 let n = self.pin.pin() as usize; 123 let n = self.pin.pin() as usize;
124 r.pupdr().modify(|w| w.set_pupdr(n, vals::Pupdr::FLOATING));
130 r.moder().modify(|w| w.set_moder(n, vals::Moder::INPUT)); 125 r.moder().modify(|w| w.set_moder(n, vals::Moder::INPUT));
131 }); 126 });
132 } 127 }
@@ -368,3 +363,19 @@ impl_pin!(PB12, 1, 12);
368impl_pin!(PB13, 1, 13); 363impl_pin!(PB13, 1, 13);
369impl_pin!(PB14, 1, 14); 364impl_pin!(PB14, 1, 14);
370impl_pin!(PB15, 1, 15); 365impl_pin!(PB15, 1, 15);
366impl_pin!(PC0, 2, 0);
367impl_pin!(PC1, 2, 1);
368impl_pin!(PC2, 2, 2);
369impl_pin!(PC3, 2, 3);
370impl_pin!(PC4, 2, 4);
371impl_pin!(PC5, 2, 5);
372impl_pin!(PC6, 2, 6);
373impl_pin!(PC7, 2, 7);
374impl_pin!(PC8, 2, 8);
375impl_pin!(PC9, 2, 9);
376impl_pin!(PC10, 2, 10);
377impl_pin!(PC11, 2, 11);
378impl_pin!(PC12, 2, 12);
379impl_pin!(PC13, 2, 13);
380impl_pin!(PC14, 2, 14);
381impl_pin!(PC15, 2, 15);