Skip to content

Commit 7c944ff

Browse files
bors[bot]burrbull
andauthored
Merge #332
332: Serial::tx/rx r=therealprof a=burrbull Co-authored-by: Andrey Zgarbul <zgarbul.andrey@gmail.com>
2 parents c3ec5f5 + ede8bb2 commit 7c944ff

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

CHANGELOG.md

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

1010
### Added
1111

12+
- `Serial:tx` and `Serial::rx` that take only 1 pin
1213
- Instead of `Alternate<AF1>` you can just use `Alternate<1>`.
1314
- `PinState` and `get/set_state`.
1415
- Inherent methods for infallible digital operations.

examples/serial-9bit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn main() -> ! {
6262
let rx_pin = gpioa.pa3.into_alternate();
6363

6464
// configure serial
65-
let serial = Serial::usart2(
65+
let serial = Serial::new(
6666
dp.USART2,
6767
(tx_pin, rx_pin),
6868
Config::default().baudrate(9600.bps()).wordlength_9(),

examples/serial.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,16 @@ fn main() -> ! {
2525

2626
// define RX/TX pins
2727
let tx_pin = gpioa.pa2.into_alternate();
28-
let rx_pin = gpioa.pa3.into_alternate();
2928

3029
// configure serial
31-
let serial = Serial::usart2(
30+
let mut tx = Serial::tx(
3231
dp.USART2,
33-
(tx_pin, rx_pin),
32+
tx_pin,
3433
Config::default().baudrate(9600.bps()),
3534
clocks,
3635
)
3736
.unwrap();
3837

39-
let (mut tx, mut _rx) = serial.split();
40-
4138
let mut value: u8 = 0;
4239

4340
loop {

src/serial.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,42 @@ where
724724
}
725725
.config_stop(config))
726726
}
727+
}
728+
729+
impl<USART, TX, WORD> Serial<USART, (TX, NoRx), WORD>
730+
where
731+
(TX, NoRx): Pins<USART>,
732+
USART: Instance,
733+
{
734+
pub fn tx(
735+
usart: USART,
736+
tx_pin: TX,
737+
config: config::Config,
738+
clocks: Clocks,
739+
) -> Result<Tx<USART, WORD>, config::InvalidConfig> {
740+
Self::new(usart, (tx_pin, NoRx), config, clocks).map(|s| s.split().0)
741+
}
742+
}
727743

744+
impl<USART, RX, WORD> Serial<USART, (NoTx, RX), WORD>
745+
where
746+
(NoTx, RX): Pins<USART>,
747+
USART: Instance,
748+
{
749+
pub fn rx(
750+
usart: USART,
751+
rx_pin: RX,
752+
config: config::Config,
753+
clocks: Clocks,
754+
) -> Result<Rx<USART, WORD>, config::InvalidConfig> {
755+
Self::new(usart, (NoTx, rx_pin), config, clocks).map(|s| s.split().1)
756+
}
757+
}
758+
759+
impl<USART, PINS, WORD> Serial<USART, PINS, WORD>
760+
where
761+
USART: Instance,
762+
{
728763
/// Starts listening for an interrupt event
729764
///
730765
/// Note, you will also have to enable the corresponding interrupt
@@ -1358,9 +1393,8 @@ where
13581393
Serial<USART, PINS>: serial::Write<u8>,
13591394
{
13601395
fn write_str(&mut self, s: &str) -> fmt::Result {
1361-
s.as_bytes()
1362-
.iter()
1363-
.try_for_each(|c| block!(self.write(*c)))
1396+
s.bytes()
1397+
.try_for_each(|c| block!(self.write(c)))
13641398
.map_err(|_| fmt::Error)
13651399
}
13661400
}
@@ -1370,9 +1404,8 @@ where
13701404
Tx<USART>: serial::Write<u8>,
13711405
{
13721406
fn write_str(&mut self, s: &str) -> fmt::Result {
1373-
s.as_bytes()
1374-
.iter()
1375-
.try_for_each(|c| block!(self.write(*c)))
1407+
s.bytes()
1408+
.try_for_each(|c| block!(self.write(c)))
13761409
.map_err(|_| fmt::Error)
13771410
}
13781411
}

0 commit comments

Comments
 (0)