Skip to content

Commit b2ef855

Browse files
committed
serial
1 parent d66dd86 commit b2ef855

File tree

2 files changed

+131
-13
lines changed

2 files changed

+131
-13
lines changed

src/serial.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ use crate::rcc::{BusClock, Clocks, Enable, Reset};
7575
use crate::time::{Bps, U32Ext};
7676

7777
mod hal_02;
78+
mod hal_1;
7879

7980
// USART REMAPPING, see: https://www.st.com/content/ccc/resource/technical/document/reference_manual/59/b9/ba/7f/11/af/43/d5/CD00171190.pdf/files/CD00171190.pdf/jcr:content/translations/en.CD00171190.pdf
8081
// Section 9.3.8
@@ -134,18 +135,7 @@ inst! {
134135
}
135136

136137
/// Serial error
137-
#[derive(Debug)]
138-
#[non_exhaustive]
139-
pub enum Error {
140-
/// Framing error
141-
Framing,
142-
/// Noise error
143-
Noise,
144-
/// RX buffer overrun
145-
Overrun,
146-
/// Parity check error
147-
Parity,
148-
}
138+
pub use embedded_hal_one::serial::ErrorKind as Error;
149139

150140
pub enum WordLength {
151141
/// When parity is enabled, a word has 7 data bits + 1 parity bit,
@@ -514,7 +504,7 @@ impl<USART: Instance> Rx<USART> {
514504
let err = if sr.pe().bit_is_set() {
515505
Some(Error::Parity)
516506
} else if sr.fe().bit_is_set() {
517-
Some(Error::Framing)
507+
Some(Error::FrameFormat)
518508
} else if sr.ne().bit_is_set() {
519509
Some(Error::Noise)
520510
} else if sr.ore().bit_is_set() {

src/serial/hal_1.rs

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
use super::*;
2+
use embedded_hal_one::{serial::blocking, serial::nb as serial, serial::ErrorType};
3+
4+
impl<USART: Instance> ErrorType for Tx<USART> {
5+
type Error = Infallible;
6+
}
7+
8+
impl<USART: Instance> ErrorType for Rx<USART> {
9+
type Error = Error;
10+
}
11+
12+
impl<USART: Instance, PINS> ErrorType for Serial<USART, PINS> {
13+
type Error = Error;
14+
}
15+
16+
impl<USART: Instance> serial::Write<u8> for Tx<USART> {
17+
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
18+
self.write(word)
19+
}
20+
21+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
22+
self.flush()
23+
}
24+
}
25+
26+
impl<USART: Instance> serial::Write<u16> for Tx<USART> {
27+
fn write(&mut self, word: u16) -> nb::Result<(), Self::Error> {
28+
self.write_u16(word)
29+
}
30+
31+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
32+
self.flush()
33+
}
34+
}
35+
36+
impl<USART: Instance> serial::Read<u8> for Rx<USART> {
37+
fn read(&mut self) -> nb::Result<u8, Self::Error> {
38+
self.read()
39+
}
40+
}
41+
42+
impl<USART: Instance> serial::Read<u16> for Rx<USART> {
43+
fn read(&mut self) -> nb::Result<u16, Self::Error> {
44+
self.read_u16()
45+
}
46+
}
47+
48+
impl<USART: Instance, PINS> serial::Write<u8> for Serial<USART, PINS> {
49+
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
50+
self.tx.write(word).unwrap();
51+
Ok(())
52+
}
53+
54+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
55+
self.tx.flush().unwrap();
56+
Ok(())
57+
}
58+
}
59+
60+
impl<USART: Instance, PINS> serial::Write<u16> for Serial<USART, PINS> {
61+
fn write(&mut self, word: u16) -> nb::Result<(), Self::Error> {
62+
self.tx.write_u16(word).unwrap();
63+
Ok(())
64+
}
65+
66+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
67+
self.tx.flush().unwrap();
68+
Ok(())
69+
}
70+
}
71+
72+
impl<USART: Instance, PINS> serial::Read<u8> for Serial<USART, PINS> {
73+
fn read(&mut self) -> nb::Result<u8, Error> {
74+
self.rx.read()
75+
}
76+
}
77+
78+
impl<USART: Instance, PINS> serial::Read<u16> for Serial<USART, PINS> {
79+
fn read(&mut self) -> nb::Result<u16, Error> {
80+
self.rx.read_u16()
81+
}
82+
}
83+
84+
// Blocking
85+
86+
impl<USART: Instance> blocking::Write<u8> for Tx<USART> {
87+
fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
88+
self.bwrite_all(buffer)
89+
}
90+
91+
fn flush(&mut self) -> Result<(), Self::Error> {
92+
self.bflush()
93+
}
94+
}
95+
96+
impl<USART: Instance> blocking::Write<u16> for Tx<USART> {
97+
fn write(&mut self, buffer: &[u16]) -> Result<(), Self::Error> {
98+
self.bwrite_all_u16(buffer)
99+
}
100+
101+
fn flush(&mut self) -> Result<(), Self::Error> {
102+
self.bflush()
103+
}
104+
}
105+
106+
impl<USART: Instance, PINS> blocking::Write<u8> for Serial<USART, PINS> {
107+
fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
108+
self.tx.bwrite_all(buffer).unwrap();
109+
Ok(())
110+
}
111+
112+
fn flush(&mut self) -> Result<(), Self::Error> {
113+
self.tx.bflush().unwrap();
114+
Ok(())
115+
}
116+
}
117+
118+
impl<USART: Instance, PINS> blocking::Write<u16> for Serial<USART, PINS> {
119+
fn write(&mut self, buffer: &[u16]) -> Result<(), Self::Error> {
120+
self.tx.bwrite_all_u16(buffer).unwrap();
121+
Ok(())
122+
}
123+
124+
fn flush(&mut self) -> Result<(), Self::Error> {
125+
self.tx.bflush().unwrap();
126+
Ok(())
127+
}
128+
}

0 commit comments

Comments
 (0)