@@ -118,33 +118,38 @@ macro_rules! wait_for_flag {
118
118
119
119
if sr1. berr( ) . bit_is_set( ) {
120
120
$i2c. sr1. write( |w| w. berr( ) . clear_bit( ) ) ;
121
- Err ( Other ( Error :: Bus ) )
121
+ Err ( Error :: Bus . into ( ) )
122
122
} else if sr1. arlo( ) . bit_is_set( ) {
123
123
$i2c. sr1. write( |w| w. arlo( ) . clear_bit( ) ) ;
124
- Err ( Other ( Error :: Arbitration ) )
124
+ Err ( Error :: Arbitration . into ( ) )
125
125
} else if sr1. af( ) . bit_is_set( ) {
126
126
$i2c. sr1. write( |w| w. af( ) . clear_bit( ) ) ;
127
- Err ( Other ( Error :: Acknowledge ) )
127
+ Err ( Error :: Acknowledge . into ( ) )
128
128
} else if sr1. ovr( ) . bit_is_set( ) {
129
129
$i2c. sr1. write( |w| w. ovr( ) . clear_bit( ) ) ;
130
- Err ( Other ( Error :: Overrun ) )
130
+ Err ( Error :: Overrun . into ( ) )
131
131
} else if sr1. $flag( ) . bit_is_set( ) {
132
132
Ok ( ( ) )
133
133
} else {
134
- Err ( WouldBlock )
134
+ Err ( nb :: Error :: WouldBlock )
135
135
}
136
136
} } ;
137
137
}
138
138
139
139
macro_rules! busy_wait {
140
140
( $nb_expr: expr, $exit_cond: expr) => { {
141
141
loop {
142
- let res = $nb_expr;
143
- if res != Err ( WouldBlock ) {
144
- break res;
145
- }
146
- if $exit_cond {
147
- break res;
142
+ match $nb_expr {
143
+ Err ( nb:: Error :: Other ( e) ) => {
144
+ #[ allow( unreachable_code) ]
145
+ break Err ( e) ;
146
+ }
147
+ Err ( nb:: Error :: WouldBlock ) => {
148
+ if $exit_cond {
149
+ break Err ( Error :: Timeout ) ;
150
+ }
151
+ }
152
+ Ok ( x) => break Ok ( x) ,
148
153
}
149
154
}
150
155
} } ;
@@ -190,26 +195,26 @@ where
190
195
/// Check if START condition is generated. If the condition is not generated, this
191
196
/// method returns `WouldBlock` so the program can act accordingly
192
197
/// (busy wait, async, ...)
193
- fn wait_after_sent_start ( & mut self ) -> NbResult < ( ) , Error > {
198
+ fn wait_after_sent_start ( & mut self ) -> nb :: Result < ( ) , Error > {
194
199
wait_for_flag ! ( self . nb. i2c, sb)
195
200
}
196
201
197
202
/// Check if STOP condition is generated. If the condition is not generated, this
198
203
/// method returns `WouldBlock` so the program can act accordingly
199
204
/// (busy wait, async, ...)
200
- fn wait_for_stop ( & mut self ) -> NbResult < ( ) , Error > {
205
+ fn wait_for_stop ( & mut self ) -> nb :: Result < ( ) , Error > {
201
206
if self . nb . i2c . cr1 . read ( ) . stop ( ) . is_no_stop ( ) {
202
207
Ok ( ( ) )
203
208
} else {
204
- Err ( WouldBlock )
209
+ Err ( nb :: Error :: WouldBlock )
205
210
}
206
211
}
207
212
208
- fn send_start_and_wait ( & mut self ) -> NbResult < ( ) , Error > {
213
+ fn send_start_and_wait ( & mut self ) -> Result < ( ) , Error > {
209
214
// According to http://www.st.com/content/ccc/resource/technical/document/errata_sheet/f5/50/c9/46/56/db/4a/f6/CD00197763.pdf/files/CD00197763.pdf/jcr:content/translations/en.CD00197763.pdf
210
215
// 2.14.4 Wrong behavior of I2C peripheral in master mode after a misplaced STOP
211
216
let mut retries_left = self . start_retries ;
212
- let mut last_ret: NbResult < ( ) , Error > = Err ( WouldBlock ) ;
217
+ let mut last_ret = Ok ( ( ) ) ;
213
218
while retries_left > 0 {
214
219
self . nb . send_start ( ) ;
215
220
last_ret = busy_wait_cycles ! ( self . wait_after_sent_start( ) , self . timeouts. start) ;
@@ -223,17 +228,17 @@ where
223
228
last_ret
224
229
}
225
230
226
- fn send_addr_and_wait ( & mut self , addr : u8 , read : bool ) -> NbResult < ( ) , Error > {
231
+ fn send_addr_and_wait ( & mut self , addr : u8 , read : bool ) -> Result < ( ) , Error > {
227
232
self . nb . i2c . sr1 . read ( ) ;
228
233
self . nb . send_addr ( addr, read) ;
229
234
let ret = busy_wait_cycles ! ( wait_for_flag!( self . nb. i2c, addr) , self . timeouts. addr) ;
230
- if ret == Err ( Other ( Error :: Acknowledge ) ) {
235
+ if ret == Err ( Error :: Acknowledge ) {
231
236
self . nb . send_stop ( ) ;
232
237
}
233
238
ret
234
239
}
235
240
236
- fn write_bytes_and_wait ( & mut self , bytes : & [ u8 ] ) -> NbResult < ( ) , Error > {
241
+ fn write_bytes_and_wait ( & mut self , bytes : & [ u8 ] ) -> Result < ( ) , Error > {
237
242
self . nb . i2c . sr1 . read ( ) ;
238
243
self . nb . i2c . sr2 . read ( ) ;
239
244
@@ -248,12 +253,12 @@ where
248
253
Ok ( ( ) )
249
254
}
250
255
251
- fn write_without_stop ( & mut self , addr : u8 , bytes : & [ u8 ] ) -> NbResult < ( ) , Error > {
256
+ fn write_without_stop ( & mut self , addr : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Error > {
252
257
self . send_start_and_wait ( ) ?;
253
258
self . send_addr_and_wait ( addr, false ) ?;
254
259
255
260
let ret = self . write_bytes_and_wait ( bytes) ;
256
- if ret == Err ( Other ( Error :: Acknowledge ) ) {
261
+ if ret == Err ( Error :: Acknowledge ) {
257
262
self . nb . send_stop ( ) ;
258
263
}
259
264
ret
@@ -264,7 +269,7 @@ impl<I2C, PINS> Write for BlockingI2c<I2C, PINS>
264
269
where
265
270
I2C : Instance ,
266
271
{
267
- type Error = NbError < Error > ;
272
+ type Error = Error ;
268
273
269
274
fn write ( & mut self , addr : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
270
275
self . write_without_stop ( addr, bytes) ?;
@@ -279,7 +284,7 @@ impl<I2C, PINS> Read for BlockingI2c<I2C, PINS>
279
284
where
280
285
I2C : Instance ,
281
286
{
282
- type Error = NbError < Error > ;
287
+ type Error = Error ;
283
288
284
289
fn read ( & mut self , addr : u8 , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
285
290
self . send_start_and_wait ( ) ?;
@@ -351,7 +356,7 @@ impl<I2C, PINS> WriteRead for BlockingI2c<I2C, PINS>
351
356
where
352
357
I2C : Instance ,
353
358
{
354
- type Error = NbError < Error > ;
359
+ type Error = Error ;
355
360
356
361
fn write_read ( & mut self , addr : u8 , bytes : & [ u8 ] , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
357
362
if !bytes. is_empty ( ) {
0 commit comments