Skip to content

Commit 470cd83

Browse files
Merge #417
417: Serial changes v3 r=burrbull a=AndreySmirnov81 ### Breaking changes - Passing the `Clock` parameter to `Serial` by reference. - `Serial::usart1/2/3` -> `Serial::new`. - `Serial` implements `Write<WORD>` and `Read<WORD>` for `WORD` simultaneously as u8 and u16. ### Added - Allow access to the `Tx` and `Rx` parts of the `Serial` without the need for splitting. - Allow `Serial` reconfiguration by references to the `Tx` and `Rx` parts. - Allow `Serial` release after splitting. Instead of #414 and #409. Co-authored-by: Andrey Smirnov <19325847+AndreySmirnov81@users.noreply.github.com>
2 parents b88151f + 559f58f commit 470cd83

12 files changed

+507
-575
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
- `gpio`: port and pin generics first, then mode, `PinMode` for modes instead of pins, other cleanups
1111

12+
### Breaking changes
13+
14+
- Passing the `Clock` parameter to `Serial` by reference.
15+
- `Serial::usart1/2/3` -> `Serial::new`.
16+
- `Serial` implements `Write<WORD>` and `Read<WORD>` for `WORD` simultaneously as u8 and u16.
17+
18+
### Added
19+
20+
- Allow access to the `Tx` and `Rx` parts of the `Serial` without the need for splitting.
21+
- Allow `Serial` reconfiguration by references to the `Tx` and `Rx` parts.
22+
- Allow `Serial` release after splitting.
23+
1224
## [v0.9.0] - 2022-03-02
1325

1426
### Added

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-dma-circ.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ fn main() -> ! {
4848
// let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
4949
// let rx = gpiob.pb11;
5050

51-
let serial = Serial::usart1(
51+
let serial = Serial::new(
5252
p.USART1,
5353
(tx, rx),
5454
&mut afio.mapr,
5555
Config::default().baudrate(9_600.bps()),
56-
clocks,
56+
&clocks,
5757
);
5858

59-
let rx = serial.split().1.with_dma(channels.5);
59+
let rx = serial.rx.with_dma(channels.5);
6060
let buf = singleton!(: [[u8; 8]; 2] = [[0; 8]; 2]).unwrap();
6161

6262
let mut circ_buffer = rx.circ_read(buf);

examples/serial-dma-peek.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ fn main() -> ! {
4747
// let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
4848
// let rx = gpiob.pb11;
4949

50-
let serial = Serial::usart1(
50+
let serial = Serial::new(
5151
p.USART1,
5252
(tx, rx),
5353
&mut afio.mapr,
5454
Config::default(),
55-
clocks,
55+
&clocks,
5656
);
5757

58-
let rx = serial.split().1.with_dma(channels.5);
58+
let rx = serial.rx.with_dma(channels.5);
5959
let buf = singleton!(: [u8; 8] = [0; 8]).unwrap();
6060

6161
let t = rx.read(buf);

examples/serial-dma-rx.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ fn main() -> ! {
4747
// let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
4848
// let rx = gpiob.pb11;
4949

50-
let serial = Serial::usart1(
50+
let serial = Serial::new(
5151
p.USART1,
5252
(tx, rx),
5353
&mut afio.mapr,
5454
Config::default().baudrate(9_600.bps()),
55-
clocks,
55+
&clocks,
5656
);
5757

58-
let rx = serial.split().1.with_dma(channels.5);
58+
let rx = serial.rx.with_dma(channels.5);
5959
let buf = singleton!(: [u8; 8] = [0; 8]).unwrap();
6060

6161
let (_buf, _rx) = rx.read(buf).wait();

examples/serial-dma-tx.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ fn main() -> ! {
4747
// let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
4848
// let rx = gpiob.pb11;
4949

50-
let serial = Serial::usart1(
50+
let serial = Serial::new(
5151
p.USART1,
5252
(tx, rx),
5353
&mut afio.mapr,
5454
Config::default().baudrate(9600.bps()),
55-
clocks,
55+
&clocks,
5656
);
5757

58-
let tx = serial.split().0.with_dma(channels.4);
58+
let tx = serial.tx.with_dma(channels.4);
5959

6060
let (_, tx) = tx.write(b"The quick brown fox").wait();
6161

examples/serial-fmt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ 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.
62-
let serial = Serial::usart3(
62+
let serial = Serial::new(
6363
p.USART3,
6464
(tx, rx),
6565
&mut afio.mapr,
6666
Config::default().baudrate(9600.bps()),
67-
clocks,
67+
&clocks,
6868
);
6969

7070
// Split the serial struct into a receiving and a transmitting part

examples/serial.rs

Lines changed: 7 additions & 6 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,22 +59,22 @@ 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.
63-
let mut serial = Serial::usart3(
64+
let mut serial = Serial::new(
6465
p.USART3,
6566
(tx, rx),
6667
&mut afio.mapr,
6768
Config::default().baudrate(9600.bps()),
68-
clocks,
69+
&clocks,
6970
);
7071

7172
// Loopback test. Write `X` and wait until the write is successful.
7273
let sent = b'X';
73-
block!(serial.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
76-
let received = block!(serial.read()).unwrap();
77+
let received = block!(serial.rx.read()).unwrap();
7778

7879
// Since we have connected tx and rx, the byte we sent should be the one we received
7980
assert_eq!(received, sent);
@@ -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: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
#![no_main]
1010
#![no_std]
1111

12+
use core::convert::Infallible;
1213
use cortex_m_rt::entry;
1314
use nb::block;
1415
use panic_halt as _;
1516
use stm32f1xx_hal::{
1617
pac,
1718
prelude::*,
18-
serial::{Config, Rx3_16, Serial, Tx3_16},
19+
serial::{self, Config, Serial},
1920
};
21+
use unwrap_infallible::UnwrapInfallible;
2022

2123
// The address of the slave device.
2224
const SLAVE_ADDR: u8 = 123;
@@ -25,7 +27,10 @@ const SLAVE_ADDR: u8 = 123;
2527
const MSG_MAX_LEN: usize = u8::MAX as usize;
2628

2729
// Receives a message addressed to the slave device. Returns the size of the received message.
28-
fn receive_msg(serial_rx: &mut Rx3_16, buf: &mut [u8; MSG_MAX_LEN]) -> usize {
30+
fn receive_msg<RX>(serial_rx: &mut RX, buf: &mut [u8; MSG_MAX_LEN]) -> usize
31+
where
32+
RX: embedded_hal::serial::Read<u16, Error = serial::Error>,
33+
{
2934
enum RxPhase {
3035
Start,
3136
Length,
@@ -72,24 +77,22 @@ fn receive_msg(serial_rx: &mut Rx3_16, buf: &mut [u8; MSG_MAX_LEN]) -> usize {
7277
}
7378

7479
// Send message.
75-
fn send_msg(mut serial_tx: Tx3_16, msg: &[u8]) -> Tx3_16 {
80+
fn send_msg<TX>(serial_tx: &mut TX, msg: &[u8])
81+
where
82+
TX: embedded_hal::serial::Write<u8, Error = Infallible>
83+
+ embedded_hal::serial::Write<u16, Error = Infallible>,
84+
{
7685
// Send address.
77-
block!(serial_tx.write(SLAVE_ADDR as u16 | 0x100)).ok();
78-
79-
// Switching from u16 to u8 data.
80-
let mut serial_tx = serial_tx.with_u8_data();
86+
block!(serial_tx.write(SLAVE_ADDR as u16 | 0x100)).unwrap_infallible();
8187

8288
// Send message len.
8389
assert!(msg.len() <= MSG_MAX_LEN);
84-
block!(serial_tx.write(msg.len() as u8)).ok();
90+
block!(serial_tx.write(msg.len() as u8)).unwrap_infallible();
8591

8692
// Send message.
8793
for &b in msg {
88-
block!(serial_tx.write(b)).ok();
94+
block!(serial_tx.write(b)).unwrap_infallible();
8995
}
90-
91-
// Switching back from u8 to u16 data.
92-
serial_tx.with_u16_data()
9396
}
9497

9598
#[entry]
@@ -115,20 +118,18 @@ fn main() -> ! {
115118
let tx_pin = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
116119
let rx_pin = gpiob.pb11;
117120

118-
// 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
119122
// the registers are used to enable and configure the device.
120-
let serial = Serial::usart3(
123+
let serial = Serial::new(
121124
p.USART3,
122125
(tx_pin, rx_pin),
123126
&mut afio.mapr,
124127
Config::default()
125128
.baudrate(9600.bps())
126129
.wordlength_9bits()
127130
.parity_none(),
128-
clocks,
129-
)
130-
// Switching the 'Word' type parameter for the 'Read' and 'Write' traits from u8 to u16.
131-
.with_u16_data();
131+
&clocks,
132+
);
132133

133134
// Split the serial struct into a transmitting and a receiving part.
134135
let (mut serial_tx, mut serial_rx) = serial.split();
@@ -140,6 +141,6 @@ fn main() -> ! {
140141
// Receive message from master device.
141142
let received_msg_len = receive_msg(&mut serial_rx, &mut buf);
142143
// Send the received message back.
143-
serial_tx = send_msg(serial_tx, &buf[..received_msg_len]);
144+
send_msg(&mut serial_tx, &buf[..received_msg_len]);
144145
}
145146
}

examples/serial_config.rs

Lines changed: 6 additions & 5 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,9 +57,9 @@ 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.
61-
let serial = Serial::usart3(
62+
let serial = Serial::new(
6263
p.USART3,
6364
(tx, rx),
6465
&mut afio.mapr,
@@ -67,15 +68,15 @@ fn main() -> ! {
6768
.stopbits(serial::StopBits::STOP2)
6869
.wordlength_9bits()
6970
.parity_odd(),
70-
clocks,
71+
&clocks,
7172
);
7273

7374
// Split the serial struct into a receiving and a transmitting part
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
}

0 commit comments

Comments
 (0)