-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblinky.rs
55 lines (46 loc) · 1.89 KB
/
blinky.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
49
50
51
52
53
54
55
//! Blinks an LED
//!
//! This assumes that a LED is connected to pc13 as is the case on the blue pill board.
//!
//! Note: Without additional hardware, PC13 should not be used to drive an LED, see page 5.1.2 of
//! the reference manual for an explanation. This is not an issue on the blue pill.
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use panic_halt as _;
use core::hint::spin_loop;
use cortex_m_rt::entry;
use embedded_hal::digital::OutputPin;
use gd32f1x0_hal::{pac, prelude::*, timer::Timer};
#[entry]
fn main() -> ! {
// Get access to the core peripherals from the cortex-m crate
let cp = cortex_m::Peripherals::take().unwrap();
// Get access to the device specific peripherals from the peripheral access crate
let dp = pac::Peripherals::take().unwrap();
// Take ownership over the raw rcu and flash devices and convert them into the corresponding HAL
// structs.
let mut flash = dp.fmc.constrain();
let mut rcu = dp.rcu.constrain();
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in
// `clocks`
let clocks = rcu.cfgr.freeze(&mut flash.ws);
// Acquire the GPIOC peripheral
let mut gpioc = dp.gpioc.split(&mut rcu.ahb);
// Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function
// in order to configure the port. For pins 0-7, crl should be passed instead.
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.config);
// Configure the syst timer to trigger an update every second
let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.hz());
// Wait for the timer to trigger an update and change the state of the LED
loop {
while !timer.has_elapsed() {
spin_loop();
}
led.set_high().unwrap();
while !timer.has_elapsed() {
spin_loop();
}
led.set_low().unwrap();
}
}