-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
354: Fix and clean up 11-usart by bumping dependencies r=adamgreig a=sirhcel This is an alternate version of #267 which fixes the panic by bumping dependencies. It also addresses the previously unsafe access to the USARTs transmit and receive data registers (TDR and RDR, stm32-rs/stm32-rs#558) by bumping the BSP to the brand spanking new release 0.7.0 which comes with a fixed PAC and HAL. Thank you very much @adamgreig, @Sh3Rm4n, and @rubberduck203 for your patience and support along this journey! This PR also bumps non-critical dependencies to their actual releases. For example `heapless`, which makes use of const generics nowadays. I did not succeed in getting `mdbook test` to actually check the example code. I moved it over to the `examples` directory and included it in the Markdown files so that it can be checked with `cargo build --target thumbv7em-none-eabihf --examples`. Co-authored-by: Christian Meusel <christian.meusel@posteo.de>
- Loading branch information
Showing
18 changed files
with
299 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ path = "auxiliary" | |
|
||
[dependencies.heapless] | ||
default-features = false | ||
version = "0.3.7" | ||
version = "0.7.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use stm32f3_discovery::stm32f3xx_hal as hal; | ||
|
||
use cortex_m::peripheral::DWT; | ||
use hal::{ | ||
rcc::Clocks, | ||
time::rate::Hertz, | ||
}; | ||
|
||
/// A monotonic nondecreasing timer. This is a resurrection of MonoTimer from | ||
/// the stm32f3xx-hal where it got removed after 0.6.1. | ||
#[derive(Clone, Copy)] | ||
pub struct MonoTimer { | ||
frequency: Hertz, | ||
} | ||
|
||
// TODO: What about a refactoring to implement Clock from embedded-time? | ||
impl MonoTimer { | ||
/// Creates a new `Monotonic` timer | ||
pub fn new(mut dwt: DWT, clocks: Clocks) -> Self { | ||
dwt.enable_cycle_counter(); | ||
|
||
// now the CYCCNT counter can't be stopped or resetted | ||
drop(dwt); | ||
|
||
MonoTimer { | ||
frequency: clocks.hclk(), | ||
} | ||
} | ||
|
||
/// Returns the frequency at which the monotonic timer is operating at | ||
pub fn frequency(self) -> Hertz { | ||
self.frequency | ||
} | ||
|
||
/// Returns an `Instant` corresponding to "now" | ||
pub fn now(self) -> Instant { | ||
Instant { | ||
now: DWT::get_cycle_count(), | ||
} | ||
} | ||
} | ||
|
||
/// A measurement of a monotonically nondecreasing clock | ||
#[derive(Clone, Copy)] | ||
pub struct Instant { | ||
now: u32, | ||
} | ||
|
||
impl Instant { | ||
/// Ticks elapsed since the `Instant` was created | ||
pub fn elapsed(self) -> u32 { | ||
DWT::get_cycle_count().wrapping_sub(self.now) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#![deny(unsafe_code)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
#[allow(unused_imports)] | ||
use aux11::{entry, iprint, iprintln}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let (usart1, mono_timer, mut itm) = aux11::init(); | ||
|
||
let instant = mono_timer.now(); | ||
// Send a string | ||
for byte in b"The quick brown fox jumps over the lazy dog.".iter() { | ||
usart1.tdr.write(|w| w.tdr().bits(u16::from(*byte))); | ||
} | ||
let elapsed = instant.elapsed(); // in ticks | ||
|
||
iprintln!( | ||
&mut itm.stim[0], | ||
"`for` loop took {} ticks ({} us)", | ||
elapsed, | ||
elapsed as f32 / mono_timer.frequency().0 as f32 * 1e6 | ||
); | ||
|
||
loop {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
#[allow(unused_imports)] | ||
use aux11::{entry, iprint, iprintln}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let (usart1, mono_timer, mut itm) = aux11::init(); | ||
|
||
let instant = mono_timer.now(); | ||
// Send a string | ||
for byte in b"The quick brown fox jumps over the lazy dog.".iter() { | ||
// wait until it's safe to write to TDR | ||
while usart1.isr.read().txe().bit_is_clear() {} // <- NEW! | ||
|
||
usart1 | ||
.tdr | ||
.write(|w| w.tdr().bits(u16::from(*byte))); | ||
} | ||
let elapsed = instant.elapsed(); // in ticks | ||
|
||
iprintln!( | ||
&mut itm.stim[0], | ||
"`for` loop took {} ticks ({} us)", | ||
elapsed, | ||
elapsed as f32 / mono_timer.frequency().0 as f32 * 1e6 | ||
); | ||
|
||
loop {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
#[allow(unused_imports)] | ||
use aux11::{entry, iprint, iprintln}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let (usart1, _mono_timer, _itm) = aux11::init(); | ||
|
||
// Send a string | ||
for byte in b"The quick brown fox jumps over the lazy dog.".iter() { | ||
usart1 | ||
.tdr | ||
.write(|w| w.tdr().bits(u16::from(*byte))); | ||
} | ||
|
||
loop {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
#[allow(unused_imports)] | ||
use aux11::{entry, iprint, iprintln}; | ||
use heapless::Vec; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let (usart1, _mono_timer, _itm) = aux11::init(); | ||
|
||
// A buffer with 32 bytes of capacity | ||
let mut buffer: Vec<u8, 32> = Vec::new(); | ||
|
||
loop { | ||
buffer.clear(); | ||
|
||
loop { | ||
while usart1.isr.read().rxne().bit_is_clear() {} | ||
let byte = usart1.rdr.read().rdr().bits() as u8; | ||
|
||
if buffer.push(byte).is_err() { | ||
// buffer full | ||
for byte in b"error: buffer full\n\r" { | ||
while usart1.isr.read().txe().bit_is_clear() {} | ||
usart1 | ||
.tdr | ||
.write(|w| w.tdr().bits(u16::from(*byte))); | ||
} | ||
|
||
break; | ||
} | ||
|
||
// Carriage return | ||
if byte == 13 { | ||
// Respond | ||
for byte in buffer.iter().rev().chain(&[b'\n', b'\r']) { | ||
while usart1.isr.read().txe().bit_is_clear() {} | ||
usart1 | ||
.tdr | ||
.write(|w| w.tdr().bits(u16::from(*byte))); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#![deny(unsafe_code)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
#[allow(unused_imports)] | ||
use aux11::{entry, iprint, iprintln}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let (usart1, _mono_timer, _itm) = aux11::init(); | ||
|
||
loop { | ||
// Wait until there's data available | ||
while usart1.isr.read().rxne().bit_is_clear() {} | ||
|
||
// Retrieve the data | ||
let _byte = usart1.rdr.read().rdr().bits() as u8; | ||
|
||
aux11::bkpt(); | ||
} | ||
} |
Oops, something went wrong.