Skip to content

Commit 55ada63

Browse files
committed
clocks & timers
1 parent d79c186 commit 55ada63

27 files changed

+645
-350
lines changed

.cargo/config.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[target.thumbv6m-none-eabi]
2-
runner = "arm-none-eabi-gdb -q"
3-
# runner = "probe-run --chip STM32G030F4"
2+
# runner = "arm-none-eabi-gdb -q"
3+
runner = "probe-run --chip STM32C031C6T3"
44

55
rustflags = [
66
"-C", "linker=arm-none-eabi-ld",

examples/blinky.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![no_std]
22
#![no_main]
33
#![deny(warnings)]
4-
#![deny(unsafe_code)]
54

65
extern crate panic_halt;
76
extern crate stm32c0xx_hal as hal;
@@ -14,10 +13,12 @@ use hal::stm32;
1413
fn main() -> ! {
1514
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
1615
let mut rcc = dp.RCC.constrain();
16+
1717
let port_a = dp.GPIOA.split(&mut rcc);
1818
let mut led = port_a.pa5.into_push_pull_output();
19+
1920
loop {
20-
led.toggle().unwrap();
21+
led.toggle().ok();
2122
for _ in 0..1_000_000 {
2223
cortex_m::asm::nop();
2324
}

examples/blinky_delay.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![deny(warnings)]
2+
#![no_main]
3+
#![no_std]
4+
5+
extern crate cortex_m;
6+
extern crate cortex_m_rt as rt;
7+
extern crate panic_semihosting;
8+
extern crate stm32c0xx_hal as hal;
9+
10+
use hal::prelude::*;
11+
use hal::stm32;
12+
use rt::entry;
13+
14+
#[entry]
15+
fn main() -> ! {
16+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
17+
let mut rcc = dp.RCC.constrain();
18+
19+
let gpioa = dp.GPIOA.split(&mut rcc);
20+
let mut led = gpioa.pa5.into_push_pull_output();
21+
22+
let mut delay = dp.TIM3.delay(&mut rcc);
23+
loop {
24+
delay.delay(500.millis());
25+
led.toggle().ok();
26+
}
27+
}

examples/blinky_timer.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![deny(warnings)]
2+
#![no_main]
3+
#![no_std]
4+
5+
extern crate cortex_m;
6+
extern crate cortex_m_rt as rt;
7+
extern crate panic_semihosting;
8+
extern crate stm32c0xx_hal as hal;
9+
10+
use hal::prelude::*;
11+
use hal::stm32;
12+
use nb::block;
13+
use rt::entry;
14+
15+
#[entry]
16+
fn main() -> ! {
17+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
18+
let mut rcc = dp.RCC.constrain();
19+
20+
let gpioa = dp.GPIOA.split(&mut rcc);
21+
let mut led = gpioa.pa5.into_push_pull_output();
22+
23+
let mut timer = dp.TIM17.timer(&mut rcc);
24+
timer.start(500.millis());
25+
26+
loop {
27+
led.toggle().unwrap();
28+
block!(timer.wait()).unwrap();
29+
}
30+
}

examples/button.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ use hal::stm32;
1414
fn main() -> ! {
1515
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
1616
let mut rcc = dp.RCC.constrain();
17+
1718
let port_a = dp.GPIOA.split(&mut rcc);
1819
let port_c = dp.GPIOC.split(&mut rcc);
1920

2021
let button = port_c.pc13.into_pull_up_input();
2122
let mut led = port_a.pa5.into_push_pull_output();
2223

2324
loop {
24-
if button.is_high().unwrap() {
25-
led.set_low().unwrap();
25+
if button.is_high().unwrap_or_default() {
26+
led.set_low().ok();
2627
} else {
27-
led.set_high().unwrap();
28+
led.set_high().ok();
2829
}
2930
}
3031
}

examples/clockout.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![deny(warnings)]
2+
#![no_main]
3+
#![no_std]
4+
5+
extern crate cortex_m;
6+
extern crate cortex_m_rt as rt;
7+
extern crate panic_halt;
8+
extern crate stm32c0xx_hal as hal;
9+
10+
use hal::gpio::Speed;
11+
use hal::prelude::*;
12+
use hal::rcc::{Config, LSCOSrc, MCOSrc, Prescaler};
13+
use hal::stm32;
14+
use rt::entry;
15+
16+
#[allow(clippy::empty_loop)]
17+
#[entry]
18+
fn main() -> ! {
19+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
20+
let mut rcc = dp.RCC.freeze(Config::hsi(Prescaler::NotDivided));
21+
let gpioa = dp.GPIOA.split(&mut rcc);
22+
23+
let mut mco =
24+
gpioa
25+
.pa9
26+
.set_speed(Speed::VeryHigh)
27+
.mco(MCOSrc::SysClk, Prescaler::Div2, &mut rcc);
28+
mco.enable();
29+
30+
let mut lsco = gpioa.pa2.lsco(LSCOSrc::LSE, &mut rcc);
31+
lsco.enable();
32+
33+
loop {}
34+
}

examples/rtic.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#![no_std]
2+
#![no_main]
3+
#![deny(warnings)]
4+
5+
extern crate cortex_m;
6+
extern crate cortex_m_rt as rt;
7+
extern crate panic_semihosting;
8+
extern crate rtic;
9+
extern crate stm32c0xx_hal as hal;
10+
11+
use hal::exti::Event;
12+
use hal::gpio::*;
13+
use hal::prelude::*;
14+
use hal::stm32;
15+
use hal::time::*;
16+
use hal::timer::*;
17+
18+
#[rtic::app(device = hal::stm32, peripherals = true)]
19+
mod app {
20+
use super::*;
21+
22+
#[shared]
23+
struct Shared {
24+
timer: Timer<stm32::TIM17>,
25+
}
26+
27+
#[local]
28+
struct Local {
29+
exti: stm32::EXTI,
30+
led: PA5<Output<PushPull>>,
31+
}
32+
33+
#[init]
34+
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
35+
let mut rcc = ctx.device.RCC.constrain();
36+
let gpioa = ctx.device.GPIOA.split(&mut rcc);
37+
let gpioc = ctx.device.GPIOC.split(&mut rcc);
38+
39+
let mut timer = ctx.device.TIM17.timer(&mut rcc);
40+
timer.start(Hertz::Hz(3).into_duration());
41+
timer.listen();
42+
43+
let mut exti = ctx.device.EXTI;
44+
gpioc.pc13.listen(SignalEdge::Falling, &mut exti);
45+
46+
(
47+
Shared { timer },
48+
Local {
49+
exti,
50+
led: gpioa.pa5.into_push_pull_output(),
51+
},
52+
init::Monotonics(),
53+
)
54+
}
55+
56+
#[task(binds = TIM17, shared = [timer], local = [led])]
57+
fn timer_tick(mut ctx: timer_tick::Context) {
58+
ctx.local.led.toggle().ok();
59+
ctx.shared.timer.lock(|tim| tim.clear_irq());
60+
}
61+
62+
#[task(binds = EXTI4_15, shared = [timer], local = [exti])]
63+
fn button_click(mut ctx: button_click::Context) {
64+
ctx.shared.timer.lock(|tim| {
65+
if tim.enabled() {
66+
tim.pause();
67+
} else {
68+
tim.resume();
69+
}
70+
});
71+
ctx.local.exti.unpend(Event::GPIO13);
72+
}
73+
74+
#[idle]
75+
fn idle(_: idle::Context) -> ! {
76+
loop {
77+
cortex_m::asm::wfi();
78+
}
79+
}
80+
}

examples/uart.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![deny(warnings)]
2+
#![deny(unsafe_code)]
3+
#![no_main]
4+
#![no_std]
5+
6+
extern crate cortex_m;
7+
extern crate cortex_m_rt as rt;
8+
extern crate panic_halt;
9+
extern crate stm32c0xx_hal as hal;
10+
11+
use core::fmt::Write;
12+
13+
use hal::prelude::*;
14+
use hal::serial::Config;
15+
use hal::stm32;
16+
use nb::block;
17+
use rt::entry;
18+
19+
#[entry]
20+
fn main() -> ! {
21+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
22+
let mut rcc = dp.RCC.constrain();
23+
let gpioa = dp.GPIOA.split(&mut rcc);
24+
let mut usart = dp
25+
.USART2
26+
.usart((gpioa.pa2, gpioa.pa3), Config::default(), &mut rcc)
27+
.unwrap();
28+
29+
writeln!(usart, "Hello\r").unwrap();
30+
31+
let mut cnt = 0;
32+
loop {
33+
let byte = block!(usart.read()).unwrap_or(0);
34+
writeln!(usart, "{}: {}\r", cnt, byte).unwrap();
35+
cnt += 1;
36+
}
37+
}

examples/watchdog.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![deny(warnings)]
2+
#![no_main]
3+
#![no_std]
4+
5+
extern crate cortex_m;
6+
extern crate cortex_m_rt as rt;
7+
extern crate cortex_m_semihosting;
8+
extern crate panic_halt;
9+
extern crate stm32c0xx_hal as hal;
10+
11+
use hal::prelude::*;
12+
use hal::stm32;
13+
use rt::entry;
14+
use hal::rcc::Config;
15+
16+
#[allow(clippy::empty_loop)]
17+
#[entry]
18+
fn main() -> ! {
19+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
20+
let mut rcc = dp.RCC.freeze(Config::hse(48.MHz()));
21+
22+
let port_a = dp.GPIOA.split(&mut rcc);
23+
let mut led = port_a.pa5.into_push_pull_output();
24+
25+
let mut watchdog = dp.WWDG.constrain(&mut rcc);
26+
// let mut watchdog = dp.IWDG.constrain();
27+
28+
led.set_high().ok();
29+
watchdog.start(20.millis());
30+
31+
loop {}
32+
}

src/analog/adc.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ where
316316
.smpr // set sampling time set 1 (ADSTART must be 0)
317317
.modify(|_, w| w.smp1().bits(self.sample_time as u8));
318318

319-
//TODO: fix this
319+
todo!();
320320
// self.rb
321321
// .chselr() // set activ channel acording chapter 15.12.9 (ADC_CFGR1; CHSELRMOD=0)
322322
// .modify(|_, w| unsafe { w.chsel().bits(1 << PIN::channel()) });
@@ -370,10 +370,9 @@ where
370370
.smpr
371371
.modify(|_, w| w.smp1().bits(self.sample_time as u8));
372372

373-
//TODO: fix this
374-
// self.rb
375-
// .chselr()
376-
// .modify(|_, w| unsafe { w.chsel().bits(1 << PIN::channel()) });
373+
self.rb
374+
.chselr0()
375+
.modify(|_, w| unsafe { w.bits(1 << PIN::channel()) });
377376

378377
self.rb.isr.modify(|_, w| w.eos().set_bit());
379378
self.rb.cr.modify(|_, w| w.adstart().set_bit());
@@ -432,11 +431,8 @@ macro_rules! int_adc {
432431
}
433432

434433
int_adc! {
435-
//TODO; check channel ids
436434
VTemp: (9, tsen),
437435
VRef: (10, vrefen),
438-
// Vdda: (15, --),
439-
// Vssa: (16, --),
440436
}
441437

442438
macro_rules! adc_pin {
@@ -461,13 +457,10 @@ adc_pin! {
461457
Channel6: (PA6<Analog>, 6u8),
462458
Channel7: (PA7<Analog>, 7u8),
463459
Channel8: (PA8<Analog>, 8u8),
464-
// Channel9: (<Analog>, 9u8),
465-
// Channel10: (<Analog>, 10u8),
466460
Channel11: (PA11<Analog>, 11u8),
467461
Channel12: (PA12<Analog>, 12u8),
468462
Channel13: (PA13<Analog>, 13u8),
469463
Channel14: (PA14<Analog>, 14u8),
470-
//TODO check sequecer
471464
Channel17: (PB0<Analog>, 17u8),
472465
Channel18: (PB1<Analog>, 18u8),
473466
Channel19: (PB2<Analog>, 19u8),

0 commit comments

Comments
 (0)