Skip to content

Commit d1b0e4d

Browse files
committed
spi: add logging, start spi transfer task
1 parent 8c197ed commit d1b0e4d

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

examples/spi-async-rtic.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#![no_main]
22
#![no_std]
33

4-
// use defmt_rtt as _;
5-
use panic_probe as _;
4+
mod utilities;
65

76
use embedded_hal_async::spi::SpiBus;
87
use rtic::app;
@@ -40,11 +39,18 @@ mod app {
4039

4140
#[init]
4241
fn init(ctx: init::Context) -> (Shared, Local) {
42+
utilities::logger::init();
43+
4344
let pwr = ctx.device.PWR.constrain();
4445
let pwrcfg = pwr.vos0().freeze();
4546

4647
let rcc = ctx.device.RCC.constrain();
47-
let ccdr = rcc.sys_ck(250.MHz()).freeze(pwrcfg, &ctx.device.SBS);
48+
let ccdr = rcc
49+
.sys_ck(192.MHz())
50+
.pll1_q_ck(64.MHz())
51+
.freeze(pwrcfg, &ctx.device.SBS);
52+
53+
log::info!("Starting RTIC SPI example...");
4854

4955
// Uncomment if use SysTick as monotonic timer
5056
Mono::start(ctx.core.SYST, ccdr.clocks.sysclk().raw());
@@ -62,23 +68,27 @@ mod app {
6268
let tx_ch = channels.0;
6369
let rx_ch = channels.1;
6470

65-
let spi = ctx.device.SPI2.spi(
66-
(sck, miso, mosi),
67-
SpiConfig::new(spi::MODE_0),
68-
1.MHz(),
69-
ccdr.peripheral.SPI2,
70-
&ccdr.clocks,
71-
).use_dma_duplex(tx_ch, rx_ch);
72-
73-
tick::spawn().ok();
71+
let spi = ctx
72+
.device
73+
.SPI2
74+
.spi(
75+
(sck, miso, mosi),
76+
SpiConfig::new(spi::MODE_0),
77+
1.MHz(),
78+
ccdr.peripheral.SPI2,
79+
&ccdr.clocks,
80+
)
81+
.use_dma_duplex(tx_ch, rx_ch);
82+
83+
tick::spawn().unwrap();
84+
spi_transfer::spawn().unwrap();
7485
(
7586
Shared {},
7687
Local {
7788
led,
7889
spi,
7990
source: [0; 40],
8091
dest: [0; 40],
81-
8292
},
8393
)
8494
}
@@ -88,17 +98,26 @@ mod app {
8898
loop {
8999
ctx.local.led.toggle();
90100
*ctx.local.count += 1;
91-
// defmt::info!("Tick {}", *ctx.local.count);
101+
log::info!("Tick {}", *ctx.local.count);
92102
Mono::delay(1000.millis()).await;
93103
}
94104
}
95105

96106
#[task(local = [spi, count: u32 = 0, source, dest], priority = 2)]
97107
async fn spi_transfer(ctx: spi_transfer::Context) {
98108
loop {
109+
log::info!("Starting SPI transfer");
99110
ctx.local.source.fill(*ctx.local.count as u8);
111+
ctx.local.dest.fill(0);
100112
*ctx.local.count += 1;
101-
ctx.local.spi.transfer(ctx.local.dest, ctx.local.source).await.unwrap();
113+
ctx.local
114+
.spi
115+
.transfer(ctx.local.dest, ctx.local.source)
116+
.await
117+
.unwrap();
118+
119+
assert_eq!(ctx.local.source, ctx.local.dest);
120+
log::info!("Success!");
102121
Mono::delay(1000.millis()).await;
103122
}
104123
}

0 commit comments

Comments
 (0)