Skip to content

Commit 28ea1e3

Browse files
authored
Merge pull request #102 from burrbull/fugit-rate
use fugit rate/duration instead of custom, update examples
2 parents 900594d + d3f1368 commit 28ea1e3

32 files changed

+255
-352
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ default-target = "thumbv6m-none-eabi"
1818
[dependencies]
1919
cortex-m = "0.7.1"
2020
nb = "1.0.0"
21+
fugit = "0.3.5"
2122

2223
[dependencies.stm32g0]
2324
version = "0.13.0"

examples/adc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn main() -> ! {
3030
adc.set_oversampling_shift(16);
3131
adc.oversampling_enable(true);
3232

33-
delay.delay(20.us()); // Wait for ADC voltage regulator to stabilize
33+
delay.delay(20.micros()); // Wait for ADC voltage regulator to stabilize
3434
adc.calibrate();
3535

3636
let mut adc_pin = gpioa.pa0.into_analog();

examples/adc_ext_trig_double_dma_serial.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ unsafe fn DMA_CHANNEL1() {
6363
// Address is in byte, value in 2Bytes, this is why second dma buffer ist added with BUFFER_SIZE
6464
// and not BUFFER_SIZE/2
6565

66-
let dma = &(*stm32g0::stm32g031::DMA::ptr());
66+
let dma = &(*hal::stm32::DMA::ptr());
6767
let htif1 = dma.isr.read().htif1().bit();
6868
let tcif1 = dma.isr.read().tcif1().bit();
6969
// set the global clear bit of DMA channel1
@@ -107,7 +107,7 @@ fn main() -> ! {
107107

108108
let mut dma = dp.DMA.split(&mut rcc, dp.DMAMUX);
109109

110-
let adc_ptr = unsafe { &(*stm32g0::stm32g031::ADC::ptr()) };
110+
let adc_ptr = unsafe { &(*hal::stm32::ADC::ptr()) };
111111
let adc_data_register_addr = &adc_ptr.dr as *const _ as u32;
112112

113113
let adc_buffer1_addr: u32 = adc_buffer1.as_ptr() as u32;
@@ -125,8 +125,7 @@ fn main() -> ! {
125125
// https://sourceware.org/gdb/current/onlinedocs/gdb/Memory.html
126126

127127
// dma ch1 reads from ADC register into memory
128-
dma.ch1
129-
.select_peripheral(stm32g0xx_hal::dmamux::DmaMuxIndex::ADC);
128+
dma.ch1.select_peripheral(hal::dmamux::DmaMuxIndex::ADC);
130129
// The dma continuesly fills the buffer, when its full, it starts over again
131130
dma.ch1.set_circular_mode(true);
132131

@@ -180,7 +179,7 @@ fn main() -> ! {
180179
// this is only available on timer TIM2, TIM3, TIM4 and TIM1
181180
unsafe {
182181
// get pointer of timer 2
183-
let tim = &(*stm32g0::stm32g031::TIM2::ptr());
182+
let tim = &(*hal::stm32::TIM2::ptr());
184183
//
185184
tim.cr2.modify(|_, w| w.mms().bits(3 as u8));
186185
}
@@ -191,7 +190,7 @@ fn main() -> ! {
191190

192191
// don't enabel the timer bevor the dma
193192
// Set up a timer expiring after
194-
timer.start(50.us());
193+
timer.start(50.micros());
195194
timer.listen();
196195

197196
//enable DMA_CHANNEL1 interrupt

examples/blinky_delay.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ fn main() -> ! {
2424

2525
loop {
2626
led.toggle().unwrap();
27-
delay.delay(500.ms());
27+
delay.delay(500.millis());
2828
}
2929
}

examples/blinky_random.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn main() -> ! {
3737
match rng.gen_range(20, 200) {
3838
Ok(period) => {
3939
led.toggle().unwrap();
40-
delay.delay(period.ms());
40+
delay.delay(period.millis());
4141
}
4242
Err(err) => hprintln!("RNG error: {:?}", err).unwrap(),
4343
}

examples/blinky_timer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn main() -> ! {
2323
let mut led = gpioa.pa5.into_push_pull_output();
2424

2525
let mut timer = dp.TIM17.timer(&mut rcc);
26-
timer.start(500.ms());
26+
timer.start(500.millis());
2727

2828
loop {
2929
led.toggle().unwrap();

examples/button.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ fn main() -> ! {
2828

2929
loop {
3030
let wait = match button.is_high() {
31-
Ok(true) => 300.ms(),
32-
Ok(false) => 100.ms(),
31+
Ok(true) => 300.millis(),
32+
Ok(false) => 100.millis(),
3333
_ => unreachable!(),
3434
};
3535
delay.delay(wait);

examples/ir_remote.rs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use infrared::protocols::nec::NecCommand;
2020
use infrared::{protocols::Nec, Sender};
2121
use rtic::app;
2222

23-
const IR_SAMPLERATE: Hertz = Hertz(20_000);
23+
const IR_SAMPLERATE: Hertz = Hertz::kHz(20);
2424
const STROBE_COMMAND: NecCommand = NecCommand {
2525
addr: 0,
2626
cmd: 15,
@@ -31,15 +31,22 @@ type IrPin = PwmPin<stm32::TIM17, timer::Channel1>;
3131
type IrTimer = Timer<stm32::TIM16>;
3232

3333
#[app(device = hal::stm32, peripherals = true)]
34-
const APP: () = {
35-
struct Resources {
36-
timer: IrTimer,
34+
mod app {
35+
use super::*;
36+
37+
#[shared]
38+
struct Shared {
3739
transmitter: Sender<Nec, IrPin>,
40+
}
41+
42+
#[local]
43+
struct Local {
44+
timer: IrTimer,
3845
exti: stm32::EXTI,
3946
}
4047

4148
#[init]
42-
fn init(mut ctx: init::Context) -> init::LateResources {
49+
fn init(mut ctx: init::Context) -> (Shared, Local, init::Monotonics) {
4350
let mut rcc = ctx.device.RCC.freeze(rcc::Config::pll());
4451

4552
let gpiob = ctx.device.GPIOB.split(&mut rcc);
@@ -48,33 +55,35 @@ const APP: () = {
4855
gpioc.pc13.listen(SignalEdge::Falling, &mut ctx.device.EXTI);
4956

5057
let mut timer = ctx.device.TIM16.timer(&mut rcc);
51-
timer.start(IR_SAMPLERATE);
58+
timer.start(IR_SAMPLERATE.into_duration());
5259
timer.listen();
5360

54-
let carrier_timer = ctx.device.TIM17.pwm(38.khz(), &mut rcc);
61+
let carrier_timer = ctx.device.TIM17.pwm(38.kHz(), &mut rcc);
5562
let mut ir_pin = carrier_timer.bind_pin(gpiob.pb9);
5663
ir_pin.set_duty(ir_pin.get_max_duty() / 2);
57-
let transmitter = Sender::new(IR_SAMPLERATE.0, ir_pin);
64+
let transmitter = Sender::new(IR_SAMPLERATE.raw(), ir_pin);
5865

59-
init::LateResources {
60-
timer,
61-
transmitter,
62-
exti: ctx.device.EXTI,
63-
}
66+
(
67+
Shared { transmitter },
68+
Local {
69+
timer,
70+
exti: ctx.device.EXTI,
71+
},
72+
init::Monotonics(),
73+
)
6474
}
6575

66-
#[task(binds = TIM16, resources = [timer, transmitter])]
67-
fn timer_tick(ctx: timer_tick::Context) {
68-
ctx.resources.transmitter.tick();
69-
ctx.resources.timer.clear_irq();
76+
#[task(binds = TIM16, local = [timer], shared = [transmitter])]
77+
fn timer_tick(mut ctx: timer_tick::Context) {
78+
ctx.shared.transmitter.lock(|tx| tx.tick());
79+
ctx.local.timer.clear_irq();
7080
}
7181

72-
#[task(binds = EXTI4_15, resources = [exti, transmitter])]
73-
fn button_click(ctx: button_click::Context) {
74-
ctx.resources
82+
#[task(binds = EXTI4_15, local = [exti], shared = [transmitter])]
83+
fn button_click(mut ctx: button_click::Context) {
84+
ctx.shared
7585
.transmitter
76-
.load(&STROBE_COMMAND)
77-
.expect("failed to send IR command");
78-
ctx.resources.exti.unpend(Event::GPIO13);
86+
.lock(|tx| tx.load(&STROBE_COMMAND).expect("failed to send IR command"));
87+
ctx.local.exti.unpend(Event::GPIO13);
7988
}
80-
};
89+
}

examples/opm.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@ use hal::timer::opm::Opm;
1818
use rtic::app;
1919

2020
#[app(device = hal::stm32, peripherals = true)]
21-
const APP: () = {
22-
struct Resources {
21+
mod app {
22+
use super::*;
23+
24+
#[shared]
25+
struct Shared {}
26+
27+
#[local]
28+
struct Local {
2329
exti: stm32::EXTI,
2430
led: PA5<Output<PushPull>>,
2531
opm: Opm<stm32::TIM3>,
2632
}
2733

2834
#[init]
29-
fn init(ctx: init::Context) -> init::LateResources {
35+
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
3036
let mut rcc = ctx.device.RCC.freeze(rcc::Config::pll());
3137
let mut exti = ctx.device.EXTI;
3238

@@ -37,7 +43,7 @@ const APP: () = {
3743
let led = gpioa.pa5.into_push_pull_output();
3844
gpioc.pc13.listen(SignalEdge::Falling, &mut exti);
3945

40-
let opm = ctx.device.TIM3.opm(4.ms(), &mut rcc);
46+
let opm = ctx.device.TIM3.opm(4.millis(), &mut rcc);
4147

4248
let mut opm_ch1 = opm.bind_pin(gpioa.pa6);
4349
let mut opm_ch2 = opm.bind_pin(gpioa.pa7);
@@ -55,13 +61,13 @@ const APP: () = {
5561
opm_ch3.enable();
5662
opm_ch4.enable();
5763

58-
init::LateResources { opm, exti, led }
64+
(Shared {}, Local { opm, exti, led }, init::Monotonics())
5965
}
6066

61-
#[task(binds = EXTI4_15, resources = [exti, led, opm])]
67+
#[task(binds = EXTI4_15, local = [exti, led, opm])]
6268
fn button_click(ctx: button_click::Context) {
63-
ctx.resources.led.toggle().unwrap();
64-
ctx.resources.opm.generate();
65-
ctx.resources.exti.unpend(Event::GPIO13);
69+
ctx.local.led.toggle().unwrap();
70+
ctx.local.opm.generate();
71+
ctx.local.exti.unpend(Event::GPIO13);
6672
}
67-
};
73+
}

examples/pwm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() -> ! {
2020

2121
let mut rcc = dp.RCC.constrain();
2222
let gpioa = dp.GPIOA.split(&mut rcc);
23-
let mut pwm = dp.TIM1.pwm(10.khz(), &mut rcc);
23+
let mut pwm = dp.TIM1.pwm(10.kHz(), &mut rcc);
2424

2525
let mut pwm_ch1 = pwm.bind_pin(gpioa.pa8);
2626
let mut pwm_ch2 = pwm.bind_pin(gpioa.pa9);
@@ -41,7 +41,7 @@ fn main() -> ! {
4141
pwm_ch2.set_duty(max / 16);
4242
asm::bkpt();
4343

44-
pwm.set_freq(20.khz());
44+
pwm.set_freq(20.kHz());
4545

4646
loop {}
4747
}

0 commit comments

Comments
 (0)