-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_yellow.rs
48 lines (39 loc) · 1020 Bytes
/
led_yellow.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! User LED PA3
use stm33f40x::{GPIOA, RCC};
/// LED connected to pin PA3
pub const LED: PA3 = PA3;
/// Pin PA3. There's an LED connected to this pin
pub struct PA3;
/// Initializes the user LED
pub fn init(gpioa: &GPIOA, rcc: &RCC) {
// power on GPIOA
rcc.ahb1enr.modify(|_, w| w.gpioaen().set_bit());
// configure PA3 as output
gpioa.moder.modify(|_, w| w.moder3().bits(1));
}
impl PA3 {
/// Turns the LED on
pub fn on(&self) {
unsafe {
(*GPIOA.get()).odr.modify(|_, w| w.odr3().bit(true));
}
}
/// Turns the LED off
pub fn off(&self) {
unsafe {
(*GPIOA.get()).odr.modify(|_, w| w.odr3().bit(false));
}
}
/// True if LED is ON, false otherwise.
pub fn is_on(&self) -> bool {
unsafe { (*GPIOA.get()).odr.read().odr3().bit_is_set() }
}
/// Toggles LED state.
pub fn toggle(&self) {
if LED.is_on() {
LED.off();
} else {
LED.on();
}
}
}