Skip to content

Commit b0f3510

Browse files
committed
Factor out 11-usart example code to included files
When running 'mdbook test' on the book the example code checking fails to handle for example macros like '#[entry]' and the local auxiliary crates. Having most code examples included from 'examples/' allows to check them at least with 'cargo build --target thumbv7em-none-eabihf --examples'.
1 parent 99b173b commit b0f3510

12 files changed

+225
-217
lines changed

src/11-usart/buffer-overrun.md

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,7 @@
33
If you wrote your program like this:
44

55
``` rust
6-
#![no_main]
7-
#![no_std]
8-
9-
#[allow(unused_imports)]
10-
use aux11::{entry, iprint, iprintln};
11-
12-
#[entry]
13-
fn main() -> ! {
14-
let (usart1, mono_timer, itm) = aux11::init();
15-
16-
// Send a string
17-
for byte in b"The quick brown fox jumps over the lazy dog.".iter() {
18-
usart1
19-
.tdr
20-
.write(|w| unsafe { w.tdr().bits(u16::from(*byte)) });
21-
}
22-
23-
loop {}
24-
}
6+
{{#include examples/buffer-overrun.rs}}
257
```
268

279
You probably received something like this on your computer when you executed the program compiled in
@@ -60,33 +42,7 @@ We can actually time how long it takes to execute the `for` loop. `aux11::init()
6042
`std::time`.
6143

6244
``` rust
63-
#![deny(unsafe_code)]
64-
#![no_main]
65-
#![no_std]
66-
67-
#[allow(unused_imports)]
68-
use aux11::{entry, iprint, iprintln};
69-
70-
#[entry]
71-
fn main() -> ! {
72-
let (usart1, mono_timer, mut itm) = aux11::init();
73-
74-
let instant = mono_timer.now();
75-
// Send a string
76-
for byte in b"The quick brown fox jumps over the lazy dog.".iter() {
77-
usart1.tdr.write(|w| w.tdr().bits(u16::from(*byte)));
78-
}
79-
let elapsed = instant.elapsed(); // in ticks
80-
81-
iprintln!(
82-
&mut itm.stim[0],
83-
"`for` loop took {} ticks ({} us)",
84-
elapsed,
85-
elapsed as f32 / mono_timer.frequency().0 as f32 * 1e6
86-
);
87-
88-
loop {}
89-
}
45+
{{#include examples/buffer-overrun-timed.rs}}
9046
```
9147

9248
In debug mode, I get:
@@ -109,37 +65,7 @@ to write to the `TDR` register without incurring in data loss.
10965
Let's use that to slowdown the processor.
11066

11167
``` rust
112-
#![no_main]
113-
#![no_std]
114-
115-
#[allow(unused_imports)]
116-
use aux11::{entry, iprint, iprintln};
117-
118-
#[entry]
119-
fn main() -> ! {
120-
let (usart1, mono_timer, mut itm) = aux11::init();
121-
122-
let instant = mono_timer.now();
123-
// Send a string
124-
for byte in b"The quick brown fox jumps over the lazy dog.".iter() {
125-
// wait until it's safe to write to TDR
126-
while usart1.isr.read().txe().bit_is_clear() {} // <- NEW!
127-
128-
usart1
129-
.tdr
130-
.write(|w| unsafe { w.tdr().bits(u16::from(*byte)) });
131-
}
132-
let elapsed = instant.elapsed(); // in ticks
133-
134-
iprintln!(
135-
&mut itm.stim[0],
136-
"`for` loop took {} ticks ({} us)",
137-
elapsed,
138-
elapsed as f32 / mono_timer.frequency().0 as f32 * 1e6
139-
);
140-
141-
loop {}
142-
}
68+
{{#include examples/buffer-overrun-txe.rs}}
14369
```
14470

14571
This time, running the program in debug or release mode should result in a complete string on the
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![deny(unsafe_code)]
2+
#![no_main]
3+
#![no_std]
4+
5+
#[allow(unused_imports)]
6+
use aux11::{entry, iprint, iprintln};
7+
8+
#[entry]
9+
fn main() -> ! {
10+
let (usart1, mono_timer, mut itm) = aux11::init();
11+
12+
let instant = mono_timer.now();
13+
// Send a string
14+
for byte in b"The quick brown fox jumps over the lazy dog.".iter() {
15+
usart1.tdr.write(|w| w.tdr().bits(u16::from(*byte)));
16+
}
17+
let elapsed = instant.elapsed(); // in ticks
18+
19+
iprintln!(
20+
&mut itm.stim[0],
21+
"`for` loop took {} ticks ({} us)",
22+
elapsed,
23+
elapsed as f32 / mono_timer.frequency().0 as f32 * 1e6
24+
);
25+
26+
loop {}
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
#[allow(unused_imports)]
5+
use aux11::{entry, iprint, iprintln};
6+
7+
#[entry]
8+
fn main() -> ! {
9+
let (usart1, mono_timer, mut itm) = aux11::init();
10+
11+
let instant = mono_timer.now();
12+
// Send a string
13+
for byte in b"The quick brown fox jumps over the lazy dog.".iter() {
14+
// wait until it's safe to write to TDR
15+
while usart1.isr.read().txe().bit_is_clear() {} // <- NEW!
16+
17+
usart1
18+
.tdr
19+
.write(|w| unsafe { w.tdr().bits(u16::from(*byte)) });
20+
}
21+
let elapsed = instant.elapsed(); // in ticks
22+
23+
iprintln!(
24+
&mut itm.stim[0],
25+
"`for` loop took {} ticks ({} us)",
26+
elapsed,
27+
elapsed as f32 / mono_timer.frequency().0 as f32 * 1e6
28+
);
29+
30+
loop {}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
#[allow(unused_imports)]
5+
use aux11::{entry, iprint, iprintln};
6+
7+
#[entry]
8+
fn main() -> ! {
9+
let (usart1, mono_timer, itm) = aux11::init();
10+
11+
// Send a string
12+
for byte in b"The quick brown fox jumps over the lazy dog.".iter() {
13+
usart1
14+
.tdr
15+
.write(|w| unsafe { w.tdr().bits(u16::from(*byte)) });
16+
}
17+
18+
loop {}
19+
}

src/11-usart/examples/echo.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
#[allow(unused_imports)]
5+
use aux11::{entry, iprint, iprintln};
6+
use heapless::{consts, Vec};
7+
8+
#[entry]
9+
fn main() -> ! {
10+
let (usart1, mono_timer, itm) = aux11::init();
11+
12+
// A buffer with 32 bytes of capacity
13+
let mut buffer: Vec<u8, consts::U32> = Vec::new();
14+
15+
loop {
16+
buffer.clear();
17+
18+
loop {
19+
while usart1.isr.read().rxne().bit_is_clear() {}
20+
let byte = usart1.rdr.read().rdr().bits() as u8;
21+
22+
if buffer.push(byte).is_err() {
23+
// buffer full
24+
for byte in b"error: buffer full\n\r" {
25+
while usart1.isr.read().txe().bit_is_clear() {}
26+
usart1
27+
.tdr
28+
.write(|w| unsafe { w.tdr().bits(u16::from(*byte)) });
29+
}
30+
31+
break;
32+
}
33+
34+
// Carriage return
35+
if byte == 13 {
36+
// Respond
37+
for byte in buffer.iter().rev().chain(&[b'\n', b'\r']) {
38+
while usart1.isr.read().txe().bit_is_clear() {}
39+
usart1
40+
.tdr
41+
.write(|w| unsafe { w.tdr().bits(u16::from(*byte)) });
42+
}
43+
44+
break;
45+
}
46+
}
47+
}
48+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![deny(unsafe_code)]
2+
#![no_main]
3+
#![no_std]
4+
5+
#[allow(unused_imports)]
6+
use aux11::{entry, iprint, iprintln};
7+
8+
#[entry]
9+
fn main() -> ! {
10+
let (usart1, mono_timer, itm) = aux11::init();
11+
12+
loop {
13+
// Wait until there's data available
14+
while usart1.isr.read().rxne().bit_is_clear() {}
15+
16+
// Retrieve the data
17+
let _byte = usart1.rdr.read().rdr().bits() as u8;
18+
19+
aux11::bkpt();
20+
}
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![deny(unsafe_code)]
2+
#![no_main]
3+
#![no_std]
4+
5+
#[allow(unused_imports)]
6+
use aux11::{entry, iprint, iprintln};
7+
use heapless::{consts, Vec};
8+
9+
#[entry]
10+
fn main() -> ! {
11+
let (usart1, mono_timer, itm) = aux11::init();
12+
13+
// A buffer with 32 bytes of capacity
14+
let mut buffer: Vec<u8, consts::U32> = Vec::new();
15+
16+
loop {
17+
buffer.clear();
18+
19+
// TODO Receive a user request. Each user request ends with ENTER
20+
// NOTE `buffer.push` returns a `Result`. Handle the error by responding
21+
// with an error message.
22+
23+
// TODO Send back the reversed string
24+
}
25+
}
26+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#![deny(unsafe_code)]
2+
#![no_main]
3+
#![no_std]
4+
5+
use core::fmt::{self, Write};
6+
7+
#[allow(unused_imports)]
8+
use aux11::{entry, iprint, iprintln, usart1};
9+
10+
macro_rules! uprint {
11+
($serial:expr, $($arg:tt)*) => {
12+
$serial.write_fmt(format_args!($($arg)*)).ok()
13+
};
14+
}
15+
16+
macro_rules! uprintln {
17+
($serial:expr, $fmt:expr) => {
18+
uprint!($serial, concat!($fmt, "\n"))
19+
};
20+
($serial:expr, $fmt:expr, $($arg:tt)*) => {
21+
uprint!($serial, concat!($fmt, "\n"), $($arg)*)
22+
};
23+
}
24+
25+
struct SerialPort {
26+
usart1: &'static mut usart1::RegisterBlock,
27+
}
28+
29+
impl fmt::Write for SerialPort {
30+
fn write_str(&mut self, s: &str) -> fmt::Result {
31+
// TODO implement this
32+
// hint: this will look very similar to the previous program
33+
Ok(())
34+
}
35+
}
36+
37+
#[entry]
38+
fn main() -> ! {
39+
let (usart1, mono_timer, itm) = aux11::init();
40+
41+
let mut serial = SerialPort { usart1 };
42+
43+
uprintln!(serial, "The answer is {}", 40 + 2);
44+
45+
loop {}
46+
}

src/11-usart/my-solution.md

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,5 @@
11
# My solution
22

33
```rust
4-
#![no_main]
5-
#![no_std]
6-
7-
#[allow(unused_imports)]
8-
use aux11::{entry, iprint, iprintln};
9-
use heapless::{consts, Vec};
10-
11-
#[entry]
12-
fn main() -> ! {
13-
let (usart1, mono_timer, itm) = aux11::init();
14-
15-
// A buffer with 32 bytes of capacity
16-
let mut buffer: Vec<u8, consts::U32> = Vec::new();
17-
18-
loop {
19-
buffer.clear();
20-
21-
loop {
22-
while usart1.isr.read().rxne().bit_is_clear() {}
23-
let byte = usart1.rdr.read().rdr().bits() as u8;
24-
25-
if buffer.push(byte).is_err() {
26-
// buffer full
27-
for byte in b"error: buffer full\n\r" {
28-
while usart1.isr.read().txe().bit_is_clear() {}
29-
usart1
30-
.tdr
31-
.write(|w| unsafe { w.tdr().bits(u16::from(*byte)) });
32-
}
33-
34-
break;
35-
}
36-
37-
// Carriage return
38-
if byte == 13 {
39-
// Respond
40-
for byte in buffer.iter().rev().chain(&[b'\n', b'\r']) {
41-
while usart1.isr.read().txe().bit_is_clear() {}
42-
usart1
43-
.tdr
44-
.write(|w| unsafe { w.tdr().bits(u16::from(*byte)) });
45-
}
46-
47-
break;
48-
}
49-
}
50-
}
51-
}
4+
{{#include examples/echo.rs}}
525
```

0 commit comments

Comments
 (0)