Skip to content

Commit fc5c0a6

Browse files
authored
Merge pull request #172 from Sh3Rm4n/testsuite
Add tests for rcc via defmt-test and support defmt in hal
2 parents 831f9cf + 01e54b1 commit fc5c0a6

File tree

18 files changed

+435
-81
lines changed

18 files changed

+435
-81
lines changed

.cargo/config.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1+
# Comment out probe-run runner and defmt link argument
2+
# to build and run tests based on probe-run and defmt
3+
#
4+
# TODO Support automatic link-arg switch based on features
5+
# [target.'cfg(feature = "..."'] are currently not supported.
6+
# Alternativly, we could create a build.rs and
7+
# at least set the link-arg. However this is unstabled
8+
# and only avaliable under the -Z extra-link-arg flag.
9+
# See https://github.com/rust-lang/cargo/pull/8441
110
[target.thumbv7em-none-eabihf]
211
runner = "arm-none-eabi-gdb"
12+
# Chip can be set via `--chip STM32F303VCTx` or `export PROBE_RUN_CHIP=STM32F303VCTx`
13+
# runner = "probe-run"
314
rustflags = [
415
"-C", "link-arg=-Tlink.x",
16+
# "-C", "link-arg=-Tdefmt.x",
517
]
618

719
[build]

.github/workflows/ci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,29 @@ jobs:
6868
command: check
6969
args: --features=stm32f303xc,stm32-usbd,rt,can --lib --examples
7070

71+
check-tests:
72+
name: Check Tests
73+
runs-on: ubuntu-latest
74+
strategy:
75+
matrix:
76+
test:
77+
- rcc
78+
mcu:
79+
- stm32f303xc
80+
- stm32f303xd
81+
steps:
82+
- uses: actions/checkout@v2
83+
- uses: actions-rs/toolchain@v1
84+
with:
85+
toolchain: stable
86+
target: thumbv7em-none-eabihf
87+
override: true
88+
profile: minimal
89+
- uses: actions-rs/cargo@v1
90+
with:
91+
command: check
92+
args: --features=rt,defmt,${{ matrix.mcu }} --test ${{ matrix.test }}
93+
7194
clippy:
7295
name: Clippy
7396
runs-on: ubuntu-latest

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2424
- Support for HSE bypass and CSS ([#156](https://github.com/stm32-rs/stm32f3xx-hal/pull/156))
2525
- Impls for missing I2C pin definitions ([#164](https://github.com/stm32-rs/stm32f3xx-hal/pull/164))
2626
- Support I2C3 ([#164](https://github.com/stm32-rs/stm32f3xx-hal/pull/164))
27+
- Support for [`defmt`][defmt] ([#172](https://github.com/stm32-rs/stm32f3xx-hal/pull/172))
28+
- Now [defmt][] features are available.
29+
- Currently these are only used for panicking calls, like
30+
`assert!` `panic!` or `unwrap()`. These are enabled using the [defmt][]
31+
[filter][].
32+
- For now [defmt][] is mostly intended for internal development and testing
33+
to further reduce panicking calls in this crate.
34+
The support of this feature is subject to change as the development
35+
of [defmt][] is advancing.
36+
37+
[defmt]: https://github.com/knurling-rs/defmt
38+
[filter]: https://defmt.ferrous-systems.com/filtering.html
2739

2840
### Changed
2941

Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ optional = true
5050
version = "1"
5151
default-features = false
5252

53+
[dependencies.defmt]
54+
version = "0.1.2"
55+
optional = true
56+
5357
[dev-dependencies]
5458
panic-semihosting = "0.5"
5559
usb-device = "0.2"
5660
usbd-serial = "0.1"
5761
cortex-m-semihosting = "0.3"
62+
panic-probe = "0.1.0"
63+
defmt-rtt = "0.1.0"
64+
defmt-test = "0.1.0"
5865

5966
[features]
6067
default = ["unproven"]
@@ -95,6 +102,13 @@ stm32f328 = ["gpio-f333", "stm32f3/stm32f3x8", "device-selected"]
95102
stm32f358 = ["gpio-f303", "stm32f3/stm32f3x8", "device-selected"]
96103
stm32f398 = ["gpio-f303e", "stm32f3/stm32f3x8", "device-selected"]
97104

105+
defmt-default = ["defmt"]
106+
defmt-trace = ["defmt"]
107+
defmt-debug = ["defmt"]
108+
defmt-info = ["defmt"]
109+
defmt-warn = ["defmt"]
110+
defmt-error = ["defmt"]
111+
98112
[profile.dev]
99113
debug = true
100114
lto = true
@@ -139,3 +153,8 @@ required-features = ["stm32f303xc"]
139153
[[example]]
140154
name = "gpio_erased"
141155
required-features = ["rt", "stm32f303xc"]
156+
157+
[[test]]
158+
name = "rcc"
159+
required-features = ["rt", "defmt"]
160+
harness = false

examples/adc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
//! Example usage for ADC on STM32F303
55
6-
extern crate panic_semihosting;
6+
use panic_semihosting as _;
77

88
use cortex_m_rt::entry;
99
use cortex_m_semihosting::hprintln;

src/adc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,10 @@ macro_rules! adc_hal {
323323
operation_mode: None,
324324
};
325325
if !(this_adc.clocks_welldefined(clocks)) {
326-
panic!("Clock settings not well defined");
326+
crate::panic!("Clock settings not well defined");
327327
}
328328
if !(this_adc.enable_clock(ahb, adc_common)){
329-
panic!("Clock already enabled with a different setting");
329+
crate::panic!("Clock already enabled with a different setting");
330330
}
331331
this_adc.set_align(Align::default());
332332
this_adc.calibrate();
@@ -365,7 +365,7 @@ macro_rules! adc_hal {
365365
}
366366

367367
fn set_sequence_len(&mut self, len: u8) {
368-
assert!(len - 1 < 16, "ADC sequence length must be in 1..=16");
368+
crate::assert!(len - 1 < 16, "ADC sequence length must be in 1..=16");
369369
self.rb.sqr1.modify(|_, w| w.l().bits(len - 1));
370370
}
371371

@@ -464,7 +464,7 @@ macro_rules! adc_hal {
464464
16 => self.rb.smpr2.modify(|_, w| w.smp16().bits(smp.bitcode())),
465465
17 => self.rb.smpr2.modify(|_, w| w.smp17().bits(smp.bitcode())),
466466
18 => self.rb.smpr2.modify(|_, w| w.smp18().bits(smp.bitcode())),
467-
_ => unreachable!(),
467+
_ => crate::unreachable!(),
468468
};
469469
}
470470

src/can.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl embedded_hal_can::Filter for CanFilter {
188188
/// Constuct a mask filter. This method accepts two parameters, the mask which designates which
189189
/// bits are actually matched againts and the filter, with the actual bits to match.
190190
fn from_mask(mask: u32, filter: u32) -> Self {
191-
assert!(
191+
crate::assert!(
192192
mask < MAX_EXTENDED_ID,
193193
"Mask cannot have bits higher than 29"
194194
);
@@ -370,7 +370,7 @@ impl embedded_hal_can::Transmitter for CanTransmitter {
370370
0 => can.tsr.read().tme0().bit_is_set(),
371371
1 => can.tsr.read().tme1().bit_is_set(),
372372
2 => can.tsr.read().tme2().bit_is_set(),
373-
_ => unreachable!(),
373+
_ => crate::unreachable!(),
374374
};
375375

376376
if !free {
@@ -400,7 +400,7 @@ impl embedded_hal_can::Transmitter for CanTransmitter {
400400
5 => tx.tdhr.modify(|_, w| w.data5().bits(*d)),
401401
6 => tx.tdhr.modify(|_, w| w.data6().bits(*d)),
402402
7 => tx.tdhr.modify(|_, w| w.data7().bits(*d)),
403-
_ => unreachable!(),
403+
_ => crate::unreachable!(),
404404
}
405405
}
406406
}
@@ -447,7 +447,7 @@ impl Receiver for CanFifo {
447447
5 => data[5] = data_high.data5().bits(),
448448
6 => data[6] = data_high.data6().bits(),
449449
7 => data[7] = data_high.data7().bits(),
450-
_ => unreachable!(),
450+
_ => crate::unreachable!(),
451451
}
452452
}
453453

@@ -504,15 +504,15 @@ impl Receiver for CanFifo {
504504
can.ffa1r.modify(|_, w| match self.idx {
505505
0 => w.ffa0().clear_bit(),
506506
1 => w.ffa0().set_bit(),
507-
_ => unreachable!(),
507+
_ => crate::unreachable!(),
508508
});
509509

510510
let index = filter
511511
.index
512512
.unwrap_or_else(|| FILTER_INDEX.fetch_add(1, Ordering::Acquire))
513513
as usize;
514514

515-
assert!(index < 28, "Filter index out of range");
515+
crate::assert!(index < 28, "Filter index out of range");
516516

517517
can.fb[index]
518518
.fr1
@@ -554,7 +554,7 @@ impl CanFrame {
554554
///
555555
/// This function will panic if length of `data` is greater than `8`
556556
pub fn new_data(id: CanId, data: &[u8]) -> CanFrame {
557-
assert!((0..8).contains(&data.len()));
557+
crate::assert!((0..8).contains(&data.len()));
558558

559559
let mut frame = Self {
560560
id,

src/delay.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl DelayUs<u32> for Delay {
5050
fn delay_us(&mut self, us: u32) {
5151
let rvr = us * (self.clocks.sysclk().0 / 1_000_000);
5252

53-
assert!(rvr < (1 << 24));
53+
crate::assert!(rvr < (1 << 24));
5454

5555
self.syst.set_reload(rvr);
5656
self.syst.clear_current();

src/dma.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<B, C: Channel, T: Target> Transfer<B, C, T> {
6161
// method we can call is `write_buffer`, which is allowed by
6262
// `WriteBuffer`'s safety requirements.
6363
let (ptr, len) = unsafe { buffer.write_buffer() };
64-
let len = u16::try_from(len).expect("buffer is too large");
64+
let len = crate::expect!(u16::try_from(len).ok(), "buffer is too large");
6565

6666
// NOTE(unsafe) We are using the address of a 'static WriteBuffer here,
6767
// which is guaranteed to be safe for DMA.
@@ -88,7 +88,7 @@ impl<B, C: Channel, T: Target> Transfer<B, C, T> {
8888
// `&mut self` methods we can call, so we are safe according to
8989
// `ReadBuffer`'s safety requirements.
9090
let (ptr, len) = unsafe { buffer.read_buffer() };
91-
let len = u16::try_from(len).expect("buffer is too large");
91+
let len = crate::expect!(u16::try_from(len).ok(), "buffer is too large");
9292

9393
// NOTE(unsafe) We are using the address of a 'static ReadBuffer here,
9494
// which is guaranteed to be safe for DMA.
@@ -110,7 +110,7 @@ impl<B, C: Channel, T: Target> Transfer<B, C, T> {
110110
where
111111
T: OnChannel<C>,
112112
{
113-
assert!(!channel.is_enabled());
113+
crate::assert!(!channel.is_enabled());
114114

115115
atomic::compiler_fence(Ordering::Release);
116116

@@ -128,13 +128,13 @@ impl<B, C: Channel, T: Target> Transfer<B, C, T> {
128128

129129
/// Is this transfer complete?
130130
pub fn is_complete(&self) -> bool {
131-
let inner = self.inner.as_ref().unwrap();
131+
let inner = crate::unwrap!(self.inner.as_ref());
132132
inner.channel.event_occurred(Event::TransferComplete)
133133
}
134134

135135
/// Stop this transfer and return ownership over its parts
136136
pub fn stop(mut self) -> (B, C, T) {
137-
let mut inner = self.inner.take().unwrap();
137+
let mut inner = crate::unwrap!(self.inner.take());
138138
inner.stop();
139139

140140
(inner.buffer, inner.channel, inner.target)
@@ -280,7 +280,7 @@ pub trait Channel: private::Channel {
280280
/// Callers must ensure the given address is the address of a peripheral
281281
/// register that supports DMA.
282282
unsafe fn set_peripheral_address(&mut self, address: u32, inc: Increment) {
283-
assert!(!self.is_enabled());
283+
crate::assert!(!self.is_enabled());
284284

285285
self.ch().par.write(|w| w.pa().bits(address));
286286
self.ch().cr.modify(|_, w| w.pinc().variant(inc.into()));
@@ -300,7 +300,7 @@ pub trait Channel: private::Channel {
300300
/// Callers must ensure the given address is a valid memory address
301301
/// that will remain valid as long as at is used by DMA.
302302
unsafe fn set_memory_address(&mut self, address: u32, inc: Increment) {
303-
assert!(!self.is_enabled());
303+
crate::assert!(!self.is_enabled());
304304

305305
self.ch().mar.write(|w| w.ma().bits(address));
306306
self.ch().cr.modify(|_, w| w.minc().variant(inc.into()));
@@ -314,7 +314,7 @@ pub trait Channel: private::Channel {
314314
///
315315
/// Panics if this channel is enabled.
316316
fn set_transfer_length(&mut self, len: u16) {
317-
assert!(!self.is_enabled());
317+
crate::assert!(!self.is_enabled());
318318

319319
self.ch().ndtr.write(|w| w.ndt().bits(len));
320320
}
@@ -331,7 +331,7 @@ pub trait Channel: private::Channel {
331331
1 => BITS8,
332332
2 => BITS16,
333333
4 => BITS32,
334-
s => panic!("unsupported word size: {}", s),
334+
s => crate::panic!("unsupported word size: {:?}", s),
335335
};
336336

337337
self.ch().cr.modify(|_, w| {

src/i2c.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<I2C, SCL, SDA> I2c<I2C, (SCL, SDA)> {
120120
{
121121
let freq = freq.into().0;
122122

123-
assert!(freq <= 1_000_000);
123+
crate::assert!(freq <= 1_000_000);
124124

125125
I2C::enable_clock(apb1);
126126

@@ -171,11 +171,11 @@ impl<I2C, SCL, SDA> I2c<I2C, (SCL, SDA)> {
171171
(presc, scll, sclh, sdadel, scldel)
172172
};
173173

174-
assert!(presc < 16);
175-
assert!(scldel < 16);
176-
assert!(sdadel < 16);
177-
let sclh = u8::try_from(sclh).unwrap();
178-
let scll = u8::try_from(scll).unwrap();
174+
crate::assert!(presc < 16);
175+
crate::assert!(scldel < 16);
176+
crate::assert!(sdadel < 16);
177+
let sclh = crate::unwrap!(u8::try_from(sclh).ok());
178+
let scll = crate::unwrap!(u8::try_from(scll).ok());
179179

180180
// Configure for "fast mode" (400 KHz)
181181
// NOTE(write): writes all non-reserved bits.
@@ -211,7 +211,7 @@ where
211211
type Error = Error;
212212

213213
fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
214-
assert!(!buffer.is_empty());
214+
crate::assert!(!buffer.is_empty());
215215

216216
// Detect Bus busy
217217
if self.i2c.isr.read().busy().is_busy() {
@@ -339,7 +339,7 @@ where
339339
type Error = Error;
340340

341341
fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> {
342-
assert!(!bytes.is_empty() && !buffer.is_empty());
342+
crate::assert!(!bytes.is_empty() && !buffer.is_empty());
343343

344344
// Detect Bus busy
345345
if self.i2c.isr.read().busy().is_busy() {

0 commit comments

Comments
 (0)