@@ -72,14 +72,12 @@ pub mod config {
72
72
DataBits8 ,
73
73
DataBits9 ,
74
74
}
75
-
76
75
#[ derive( Copy , Clone , PartialEq ) ]
77
76
pub enum Parity {
78
77
ParityNone ,
79
78
ParityEven ,
80
79
ParityOdd ,
81
80
}
82
-
83
81
#[ derive( Copy , Clone , PartialEq ) ]
84
82
pub enum StopBits {
85
83
#[ doc = "1 stop bit" ]
@@ -91,34 +89,60 @@ pub mod config {
91
89
#[ doc = "1.5 stop bits" ]
92
90
STOP1P5 ,
93
91
}
94
-
92
+ # [ derive ( Copy , Clone , PartialEq ) ]
95
93
pub enum BitOrder {
96
94
LsbFirst ,
97
95
MsbFirst ,
98
96
}
99
-
97
+ # [ derive ( Copy , Clone , PartialEq ) ]
100
98
pub enum ClockPhase {
101
99
First ,
102
100
Second ,
103
101
}
104
-
102
+ # [ derive ( Copy , Clone , PartialEq ) ]
105
103
pub enum ClockPolarity {
106
104
IdleHigh ,
107
105
IdleLow ,
108
106
}
109
107
108
+ /// A structure for specifying the USART or UART configuration. Fields
109
+ /// relating to synchronous mode are ignored for UART peripherals.
110
+ ///
111
+ /// This structure uses builder semantics to generate the configuration.
112
+ ///
113
+ /// ```
114
+ /// let config = Config::new().partity_odd();
115
+ /// ```
116
+ #[ derive( Copy , Clone ) ]
110
117
pub struct Config {
111
118
pub baudrate : Hertz ,
112
119
pub wordlength : WordLength ,
113
120
pub parity : Parity ,
114
121
pub stopbits : StopBits ,
115
- pub clockphase : ClockPhase ,
116
122
pub bitorder : BitOrder ,
123
+ pub clockphase : ClockPhase ,
117
124
pub clockpolarity : ClockPolarity ,
118
125
pub lastbitclockpulse : bool ,
119
126
}
120
127
121
128
impl Config {
129
+ /// Create a default configuration for the USART or UART interface
130
+ ///
131
+ /// * 8 bits, 1 stop bit, no parity (8N1)
132
+ /// * LSB first
133
+ pub fn new < T : Into < Hertz > > ( frequency : T ) -> Self {
134
+ Config {
135
+ baudrate : frequency. into ( ) ,
136
+ wordlength : WordLength :: DataBits8 ,
137
+ parity : Parity :: ParityNone ,
138
+ stopbits : StopBits :: STOP1 ,
139
+ bitorder : BitOrder :: LsbFirst ,
140
+ clockphase : ClockPhase :: First ,
141
+ clockpolarity : ClockPolarity :: IdleLow ,
142
+ lastbitclockpulse : false ,
143
+ }
144
+ }
145
+
122
146
pub fn baudrate ( mut self , baudrate : impl Into < Hertz > ) -> Self {
123
147
self . baudrate = baudrate. into ( ) ;
124
148
self
@@ -149,36 +173,46 @@ pub mod config {
149
173
self
150
174
}
151
175
176
+ /// Specify the number of stop bits
152
177
pub fn stopbits ( mut self , stopbits : StopBits ) -> Self {
153
178
self . stopbits = stopbits;
154
179
self
155
180
}
181
+ /// Specify the bit order
182
+ pub fn bitorder ( mut self , bitorder : BitOrder ) -> Self {
183
+ self . bitorder = bitorder;
184
+ self
185
+ }
186
+ /// Specify the clock phase. Only applies to USART peripherals
187
+ pub fn clockphase ( mut self , clockphase : ClockPhase ) -> Self {
188
+ self . clockphase = clockphase;
189
+ self
190
+ }
191
+ /// Specify the clock polarity. Only applies to USART peripherals
192
+ pub fn clockpolarity ( mut self , clockpolarity : ClockPolarity ) -> Self {
193
+ self . clockpolarity = clockpolarity;
194
+ self
195
+ }
196
+ /// Specify if the last bit transmitted in each word has a corresponding
197
+ /// clock pulse in the SCLK pin. Only applies to USART peripherals
198
+ pub fn lastbitclockpulse ( mut self , lastbitclockpulse : bool ) -> Self {
199
+ self . lastbitclockpulse = lastbitclockpulse;
200
+ self
201
+ }
156
202
}
157
203
158
204
#[ derive( Debug ) ]
159
205
pub struct InvalidConfig ;
160
206
161
207
impl Default for Config {
162
208
fn default ( ) -> Config {
163
- Config {
164
- baudrate : Hertz ( 19_200 ) , // 19k2 baud
165
- wordlength : WordLength :: DataBits8 ,
166
- parity : Parity :: ParityNone ,
167
- stopbits : StopBits :: STOP1 ,
168
- clockphase : ClockPhase :: First ,
169
- bitorder : BitOrder :: LsbFirst ,
170
- clockpolarity : ClockPolarity :: IdleLow ,
171
- lastbitclockpulse : false ,
172
- }
209
+ Self :: new ( Hertz ( 19_200 ) ) // 19k2 baud
173
210
}
174
211
}
175
212
176
213
impl < T : Into < Hertz > > From < T > for Config {
177
- fn from ( f : T ) -> Config {
178
- Config {
179
- baudrate : f. into ( ) ,
180
- ..Default :: default ( )
181
- }
214
+ fn from ( frequency : T ) -> Config {
215
+ Self :: new ( frequency)
182
216
}
183
217
}
184
218
}
0 commit comments