Skip to content

Commit 4bb5df5

Browse files
bors[bot]andresv
andauthored
Merge #161 #162
161: readme: add instructions for tests r=korken89 a=andresv It took me some time to figure out which features must be used for tests. So this pull request removes some friction. 162: histbuf: replace slow modulo operations r=korken89 a=andresv STM32G081 Cortex-M0 64MHz thumbv6m-none-eabi Rust 1.43.1 RTFM application where data from ADC channels is added to `HistoryBuffers`: ```Rust // this is shared with DMA static mut ADC_DATA: [u16; 6] = [0; 6]; ... // this interrupt fires when DMA has finished sequencing all ADC channels #[task(binds = DMA_CHANNEL1, resources = [pa9, adc_data, hist0, hist1, hist2, hist3, hist4, hist5])] fn adc_data_ready(ctx: adc_data_ready::Context) { let dma = unsafe { &(*stm32::DMA::ptr()) }; // transfer complete flag clear dma.ifcr.write(|w| w.ctcif1().set_bit()); ctx.resources.pa9.set_high().unwrap(); ctx.resources.hist0.write(ctx.resources.adc_data[0]); ctx.resources.hist1.write(ctx.resources.adc_data[1]); ctx.resources.hist2.write(ctx.resources.adc_data[2]); ctx.resources.hist3.write(ctx.resources.adc_data[3]); ctx.resources.hist4.write(ctx.resources.adc_data[4]); ctx.resources.hist5.write(ctx.resources.adc_data[5]); ctx.resources.pa9.set_low().unwrap(); } ``` Time is measured from `PA9` pin using logic analyzer. ``` [profile.release] opt-level = "s" codegen-units = 1 debug = true lto = true ``` - before `27.6 us` - with fix `4.4 us` ``` [profile.release] opt-level = 2 codegen-units = 1 debug = true lto = true ``` - before `25.9 us` - with fix `2.9 us` Co-authored-by: Andres Vahter <andres.vahter@gmail.com>
3 parents ab9f251 + cfe596b + c6e4f59 commit 4bb5df5

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@
55

66
> `static` friendly data structures that don't require dynamic memory allocation
77
8-
# [Documentation](https://japaric.github.io/heapless/heapless/index.html)
8+
## [Documentation](https://japaric.github.io/heapless/heapless/index.html)
99

10-
# [Change log](CHANGELOG.md)
10+
## [Change log](CHANGELOG.md)
1111

12-
# License
12+
## Tests
13+
14+
```bash
15+
# run all
16+
cargo test --features 'serde','x86-sync-pool'
17+
# run only for example histbuf tests
18+
cargo test histbuf --features 'serde','x86-sync-pool'
19+
```
20+
21+
## License
1322

1423
Licensed under either of
1524

src/histbuf.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ where
122122
/// Writes an element to the buffer, overwriting the oldest value.
123123
pub fn write(&mut self, t: T) {
124124
self.data[self.write_at] = t;
125-
self.write_at = (self.write_at + 1) % self.len();
125+
self.write_at += 1;
126+
if self.write_at == self.len() {
127+
self.write_at = 0;
128+
}
126129
}
127130

128131
/// Clones and writes all elements in a slice to the buffer.
@@ -152,7 +155,11 @@ where
152155
/// assert_eq!(x.recent(), &10);
153156
/// ```
154157
pub fn recent(&self) -> &T {
155-
&self.data[(self.write_at + self.len() - 1) % self.len()]
158+
if self.write_at == 0 {
159+
&self.data[self.len() - 1]
160+
} else {
161+
&self.data[self.write_at - 1]
162+
}
156163
}
157164

158165
/// Returns the array slice backing the buffer, without keeping track

0 commit comments

Comments
 (0)