1
+ //! API for the integrated USART ports
2
+ //!
3
+ //! This only implements the usual asynchronous bidirectional 8-bit transfers, everything else is missing
4
+ //!
5
+ //! # Example
6
+ //! Serial Echo
7
+ //! ``` no_run
8
+ //! use stm32f0xx_hal as hal;
9
+ //!
10
+ //! use crate::hal::stm32;
11
+ //! use crate::hal::prelude::*;
12
+ //! use crate::hal::serial::Serial;
13
+ //! use nb::block;
14
+ //!
15
+ //! let mut p = stm32::Peripherals::take().unwrap();
16
+ //!
17
+ //! let mut led = gpioa.pa1.into_push_pull_pull_output();
18
+ //! let rcc = p.RCC.constrain().cfgr.freeze();
19
+ //! let mut timer = Timer::tim1(p.TIM1, Hertz(1), clocks);
20
+ //! loop {
21
+ //! led.toggle();
22
+ //! block!(timer.wait()).ok();
23
+ //! }
24
+ //! ```
25
+
1
26
use core:: fmt:: { Result , Write } ;
2
27
use core:: ops:: Deref ;
3
28
use core:: ptr;
@@ -98,30 +123,31 @@ macro_rules! usart {
98
123
$(
99
124
use crate :: stm32:: $USART;
100
125
impl <PINS > Serial <$USART, PINS > {
101
- pub fn $usart( usart: $USART, pins: PINS , baud_rate: Bps , clocks: Clocks ) -> Self
102
- where
103
- PINS : Pins <$USART>,
104
- {
105
- // NOTE(unsafe) This executes only during initialisation
106
- let rcc = unsafe { & ( * stm32:: RCC :: ptr( ) ) } ;
126
+ /// Creates a new serial instance
127
+ pub fn $usart( usart: $USART, pins: PINS , baud_rate: Bps , clocks: Clocks ) -> Self
128
+ where
129
+ PINS : Pins <$USART>,
130
+ {
131
+ // NOTE(unsafe) This executes only during initialisation
132
+ let rcc = unsafe { & ( * stm32:: RCC :: ptr( ) ) } ;
107
133
108
- /* Enable clock for USART */
109
- rcc. $apbenr. modify( |_, w| w. $usartXen( ) . set_bit( ) ) ;
134
+ /* Enable clock for USART */
135
+ rcc. $apbenr. modify( |_, w| w. $usartXen( ) . set_bit( ) ) ;
110
136
111
- // Calculate correct baudrate divisor on the fly
112
- let brr = clocks. pclk( ) . 0 / baud_rate. 0 ;
113
- usart. brr. write( |w| unsafe { w. bits( brr) } ) ;
137
+ // Calculate correct baudrate divisor on the fly
138
+ let brr = clocks. pclk( ) . 0 / baud_rate. 0 ;
139
+ usart. brr. write( |w| unsafe { w. bits( brr) } ) ;
114
140
115
- /* Reset other registers to disable advanced USART features */
116
- usart. cr2. reset( ) ;
117
- usart. cr3. reset( ) ;
141
+ /* Reset other registers to disable advanced USART features */
142
+ usart. cr2. reset( ) ;
143
+ usart. cr3. reset( ) ;
118
144
119
- /* Enable transmission and receiving */
120
- usart. cr1. modify( |_, w| unsafe { w. bits( 0xD ) } ) ;
145
+ /* Enable transmission and receiving */
146
+ usart. cr1. modify( |_, w| unsafe { w. bits( 0xD ) } ) ;
121
147
122
- Serial { usart, pins }
123
- }
148
+ Serial { usart, pins }
124
149
}
150
+ }
125
151
) +
126
152
}
127
153
}
@@ -157,6 +183,7 @@ where
157
183
{
158
184
type Error = Error ;
159
185
186
+ /// Tries to read a byte from the uart
160
187
fn read ( & mut self ) -> nb:: Result < u8 , Error > {
161
188
// NOTE(unsafe) atomic read with no side effects
162
189
let isr = unsafe { ( * self . usart ) . isr . read ( ) } ;
@@ -184,6 +211,7 @@ where
184
211
{
185
212
type Error = Void ;
186
213
214
+ /// Ensures that none of the previously written words are still buffered
187
215
fn flush ( & mut self ) -> nb:: Result < ( ) , Self :: Error > {
188
216
// NOTE(unsafe) atomic read with no side effects
189
217
let isr = unsafe { ( * self . usart ) . isr . read ( ) } ;
@@ -195,6 +223,8 @@ where
195
223
}
196
224
}
197
225
226
+ /// Tries to write a byte to the uart
227
+ /// Fails if the transmit buffer is full
198
228
fn write ( & mut self , byte : u8 ) -> nb:: Result < ( ) , Self :: Error > {
199
229
// NOTE(unsafe) atomic read with no side effects
200
230
let isr = unsafe { ( * self . usart ) . isr . read ( ) } ;
@@ -215,6 +245,8 @@ where
215
245
USART : Deref < Target = SerialRegisterBlock > ,
216
246
PINS : Pins < USART > ,
217
247
{
248
+ /// Splits the UART Peripheral in a Tx and an Rx part
249
+ /// This is required for sending/receiving
218
250
pub fn split ( self ) -> ( Tx < USART > , Rx < USART > ) {
219
251
(
220
252
Tx {
0 commit comments