Skip to content

Commit ca7ce90

Browse files
Serial implements Write<WORD> and Read<WORD> for WORD simultaneously as u8 and u16.
Allow access to the `Tx` and `Rx` parts of the `Serial` without the need for splitting.
1 parent a5af849 commit ca7ce90

File tree

9 files changed

+172
-199
lines changed

9 files changed

+172
-199
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111

1212
### Breaking changes
1313

14-
- Passing the `Clock` parameter to `Serial` by reference
15-
- `Serial::usart1/2/3` -> `Serial::new`
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.
1621

1722
## [v0.9.0] - 2022-03-02
1823

examples/serial-dma-circ.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn main() -> ! {
5656
&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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn main() -> ! {
5555
&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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn main() -> ! {
5555
&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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn main() -> ! {
5555
&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.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ fn main() -> ! {
7070

7171
// Loopback test. Write `X` and wait until the write is successful.
7272
let sent = b'X';
73-
block!(serial.write(sent)).ok();
73+
block!(serial.tx.write(sent)).ok();
7474

7575
// Read the byte that was just sent. Blocks until the read is complete
76-
let received = block!(serial.read()).unwrap();
76+
let received = block!(serial.rx.read()).unwrap();
7777

7878
// Since we have connected tx and rx, the byte we sent should be the one we received
7979
assert_eq!(received, sent);

examples/serial_9bits.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
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
};
2021

2122
// The address of the slave device.
@@ -25,7 +26,10 @@ const SLAVE_ADDR: u8 = 123;
2526
const MSG_MAX_LEN: usize = u8::MAX as usize;
2627

2728
// 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 {
29+
fn receive_msg<RX>(serial_rx: &mut RX, buf: &mut [u8; MSG_MAX_LEN]) -> usize
30+
where
31+
RX: embedded_hal::serial::Read<u16, Error = serial::Error>,
32+
{
2933
enum RxPhase {
3034
Start,
3135
Length,
@@ -72,13 +76,14 @@ fn receive_msg(serial_rx: &mut Rx3_16, buf: &mut [u8; MSG_MAX_LEN]) -> usize {
7276
}
7377

7478
// Send message.
75-
fn send_msg(mut serial_tx: Tx3_16, msg: &[u8]) -> Tx3_16 {
79+
fn send_msg<TX>(serial_tx: &mut TX, msg: &[u8])
80+
where
81+
TX: embedded_hal::serial::Write<u8, Error = Infallible>
82+
+ embedded_hal::serial::Write<u16, Error = Infallible>,
83+
{
7684
// Send address.
7785
block!(serial_tx.write(SLAVE_ADDR as u16 | 0x100)).ok();
7886

79-
// Switching from u16 to u8 data.
80-
let mut serial_tx = serial_tx.with_u8_data();
81-
8287
// Send message len.
8388
assert!(msg.len() <= MSG_MAX_LEN);
8489
block!(serial_tx.write(msg.len() as u8)).ok();
@@ -87,9 +92,6 @@ fn send_msg(mut serial_tx: Tx3_16, msg: &[u8]) -> Tx3_16 {
8792
for &b in msg {
8893
block!(serial_tx.write(b)).ok();
8994
}
90-
91-
// Switching back from u8 to u16 data.
92-
serial_tx.with_u16_data()
9395
}
9496

9597
#[entry]
@@ -126,9 +128,7 @@ fn main() -> ! {
126128
.wordlength_9bits()
127129
.parity_none(),
128130
&clocks,
129-
)
130-
// Switching the 'Word' type parameter for the 'Read' and 'Write' traits from u8 to u16.
131-
.with_u16_data();
131+
);
132132

133133
// Split the serial struct into a transmitting and a receiving part.
134134
let (mut serial_tx, mut serial_rx) = serial.split();
@@ -140,6 +140,6 @@ fn main() -> ! {
140140
// Receive message from master device.
141141
let received_msg_len = receive_msg(&mut serial_rx, &mut buf);
142142
// Send the received message back.
143-
serial_tx = send_msg(serial_tx, &buf[..received_msg_len]);
143+
send_msg(&mut serial_tx, &buf[..received_msg_len]);
144144
}
145145
}

examples/serial_reconfigure.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ fn main() -> ! {
7070

7171
// Loopback test. Write `X` and wait until the write is successful.
7272
let sent = b'X';
73-
block!(serial.write(sent)).ok();
73+
block!(serial.tx.write(sent)).ok();
7474

7575
// Read the byte that was just sent. Blocks until the read is complete
76-
let received = block!(serial.read()).unwrap();
76+
let received = block!(serial.rx.read()).unwrap();
7777

7878
// Since we have connected tx and rx, the byte we sent should be the one we received
7979
assert_eq!(received, sent);
@@ -87,8 +87,8 @@ fn main() -> ! {
8787

8888
// Let's see if it works.'
8989
let sent = b'Y';
90-
block!(serial.write(sent)).ok();
91-
let received = block!(serial.read()).unwrap();
90+
block!(serial.tx.write(sent)).ok();
91+
let received = block!(serial.rx.read()).unwrap();
9292
assert_eq!(received, sent);
9393
asm::bkpt();
9494

0 commit comments

Comments
 (0)