Skip to content

Commit 224efb5

Browse files
serial: refactoring
1 parent f05d837 commit 224efb5

File tree

7 files changed

+80
-147
lines changed

7 files changed

+80
-147
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ heapless = "0.7.10"
4545
mfrc522 = "0.2.0"
4646
usb-device = "0.2.8"
4747
usbd-serial = "0.1.1"
48+
unwrap-infallible = "0.1.5"
4849

4950
[features]
5051
device-selected = []

examples/serial-fmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn main() -> ! {
5757
// Take ownership over pb11
5858
let rx = gpiob.pb11;
5959

60-
// Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of
60+
// Set up the usart device. Take ownership over the USART register and tx/rx pins. The rest of
6161
// the registers are used to enable and configure the device.
6262
let serial = Serial::new(
6363
p.USART3,

examples/serial.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use stm32f1xx_hal::{
1919
prelude::*,
2020
serial::{Config, Serial},
2121
};
22+
use unwrap_infallible::UnwrapInfallible;
2223

2324
#[entry]
2425
fn main() -> ! {
@@ -58,7 +59,7 @@ fn main() -> ! {
5859
// Take ownership over pb11
5960
let rx = gpiob.pb11;
6061

61-
// Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of
62+
// Set up the usart device. Take ownership over the USART register and tx/rx pins. The rest of
6263
// the registers are used to enable and configure the device.
6364
let mut serial = Serial::new(
6465
p.USART3,
@@ -70,7 +71,7 @@ fn main() -> ! {
7071

7172
// Loopback test. Write `X` and wait until the write is successful.
7273
let sent = b'X';
73-
block!(serial.tx.write(sent)).ok();
74+
block!(serial.tx.write(sent)).unwrap_infallible();
7475

7576
// Read the byte that was just sent. Blocks until the read is complete
7677
let received = block!(serial.rx.read()).unwrap();
@@ -84,7 +85,7 @@ fn main() -> ! {
8485
// You can also split the serial struct into a receiving and a transmitting part
8586
let (mut tx, mut rx) = serial.split();
8687
let sent = b'Y';
87-
block!(tx.write(sent)).ok();
88+
block!(tx.write(sent)).unwrap_infallible();
8889
let received = block!(rx.read()).unwrap();
8990
assert_eq!(received, sent);
9091
asm::bkpt();

examples/serial_9bits.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use stm32f1xx_hal::{
1818
prelude::*,
1919
serial::{self, Config, Serial},
2020
};
21+
use unwrap_infallible::UnwrapInfallible;
2122

2223
// The address of the slave device.
2324
const SLAVE_ADDR: u8 = 123;
@@ -82,15 +83,15 @@ where
8283
+ embedded_hal::serial::Write<u16, Error = Infallible>,
8384
{
8485
// Send address.
85-
block!(serial_tx.write(SLAVE_ADDR as u16 | 0x100)).ok();
86+
block!(serial_tx.write(SLAVE_ADDR as u16 | 0x100)).unwrap_infallible();
8687

8788
// Send message len.
8889
assert!(msg.len() <= MSG_MAX_LEN);
89-
block!(serial_tx.write(msg.len() as u8)).ok();
90+
block!(serial_tx.write(msg.len() as u8)).unwrap_infallible();
9091

9192
// Send message.
9293
for &b in msg {
93-
block!(serial_tx.write(b)).ok();
94+
block!(serial_tx.write(b)).unwrap_infallible();
9495
}
9596
}
9697

@@ -117,7 +118,7 @@ fn main() -> ! {
117118
let tx_pin = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
118119
let rx_pin = gpiob.pb11;
119120

120-
// Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of
121+
// Set up the usart device. Take ownership over the USART register and tx/rx pins. The rest of
121122
// the registers are used to enable and configure the device.
122123
let serial = Serial::new(
123124
p.USART3,

examples/serial_config.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use stm32f1xx_hal::{
1717
prelude::*,
1818
serial::{self, Serial},
1919
};
20+
use unwrap_infallible::UnwrapInfallible;
2021

2122
#[entry]
2223
fn main() -> ! {
@@ -56,7 +57,7 @@ fn main() -> ! {
5657
// Take ownership over pb11
5758
let rx = gpiob.pb11;
5859

59-
// Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of
60+
// Set up the usart device. Take ownership over the USART register and tx/rx pins. The rest of
6061
// the registers are used to enable and configure the device.
6162
let serial = Serial::new(
6263
p.USART3,
@@ -74,8 +75,8 @@ fn main() -> ! {
7475
let (mut tx, _rx) = serial.split();
7576

7677
let sent = b'U';
77-
block!(tx.write(sent)).ok();
78-
block!(tx.write(sent)).ok();
78+
block!(tx.write(sent)).unwrap_infallible();
79+
block!(tx.write(sent)).unwrap_infallible();
7980

8081
loop {}
8182
}

examples/serial_reconfigure.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use stm32f1xx_hal::{
1919
prelude::*,
2020
serial::{self, Config, Serial},
2121
};
22+
use unwrap_infallible::UnwrapInfallible;
2223

2324
#[entry]
2425
fn main() -> ! {
@@ -58,7 +59,7 @@ fn main() -> ! {
5859
// Take ownership over pb11
5960
let rx = gpiob.pb11;
6061

61-
// Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of
62+
// Set up the usart device. Take ownership over the USART register and tx/rx pins. The rest of
6263
// the registers are used to enable and configure the device.
6364
let mut serial = Serial::new(
6465
p.USART3,
@@ -70,7 +71,7 @@ fn main() -> ! {
7071

7172
// Loopback test. Write `X` and wait until the write is successful.
7273
let sent = b'X';
73-
block!(serial.tx.write(sent)).ok();
74+
block!(serial.tx.write(sent)).unwrap_infallible();
7475

7576
// Read the byte that was just sent. Blocks until the read is complete
7677
let received = block!(serial.rx.read()).unwrap();
@@ -87,7 +88,7 @@ fn main() -> ! {
8788

8889
// Let's see if it works.'
8990
let sent = b'Y';
90-
block!(serial.tx.write(sent)).ok();
91+
block!(serial.tx.write(sent)).unwrap_infallible();
9192
let received = block!(serial.rx.read()).unwrap();
9293
assert_eq!(received, sent);
9394
asm::bkpt();

0 commit comments

Comments
 (0)