1
- //! Blocking I2C API
1
+ //! Blocking I2C API.
2
2
//!
3
3
//! This API supports 7-bit and 10-bit addresses. Traits feature an [`AddressMode`]
4
4
//! marker type parameter. Two implementation of the [`AddressMode`] exist:
@@ -157,9 +157,9 @@ use crate::private;
157
157
#[ cfg( feature = "defmt-03" ) ]
158
158
use crate :: defmt;
159
159
160
- /// I2C error
160
+ /// I2C error.
161
161
pub trait Error : core:: fmt:: Debug {
162
- /// Convert error to a generic I2C error kind
162
+ /// Convert error to a generic I2C error kind.
163
163
///
164
164
/// By using this method, I2C errors freely defined by HAL implementations
165
165
/// can be converted to a set of generic I2C errors upon which generic
@@ -168,12 +168,13 @@ pub trait Error: core::fmt::Debug {
168
168
}
169
169
170
170
impl Error for core:: convert:: Infallible {
171
+ #[ inline]
171
172
fn kind ( & self ) -> ErrorKind {
172
173
match * self { }
173
174
}
174
175
}
175
176
176
- /// I2C error kind
177
+ /// I2C error kind.
177
178
///
178
179
/// This represents a common set of I2C operation errors. HAL implementations are
179
180
/// free to define more specific or additional error types. However, by providing
@@ -185,19 +186,19 @@ pub enum ErrorKind {
185
186
/// Bus error occurred. e.g. A START or a STOP condition is detected and is not
186
187
/// located after a multiple of 9 SCL clock pulses.
187
188
Bus ,
188
- /// The arbitration was lost, e.g. electrical problems with the clock signal
189
+ /// The arbitration was lost, e.g. electrical problems with the clock signal.
189
190
ArbitrationLoss ,
190
191
/// A bus operation was not acknowledged, e.g. due to the addressed device not
191
192
/// being available on the bus or the device not being ready to process requests
192
- /// at the moment
193
+ /// at the moment.
193
194
NoAcknowledge ( NoAcknowledgeSource ) ,
194
- /// The peripheral receive buffer was overrun
195
+ /// The peripheral receive buffer was overrun.
195
196
Overrun ,
196
197
/// A different error occurred. The original error may contain more information.
197
198
Other ,
198
199
}
199
200
200
- /// I2C no acknowledge error source
201
+ /// I2C no acknowledge error source.
201
202
///
202
203
/// In cases where it is possible, a device should indicate if a no acknowledge
203
204
/// response was received to an address versus a no acknowledge to a data byte.
@@ -216,12 +217,14 @@ pub enum NoAcknowledgeSource {
216
217
}
217
218
218
219
impl Error for ErrorKind {
220
+ #[ inline]
219
221
fn kind ( & self ) -> ErrorKind {
220
222
* self
221
223
}
222
224
}
223
225
224
226
impl core:: fmt:: Display for ErrorKind {
227
+ #[ inline]
225
228
fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
226
229
match self {
227
230
Self :: Bus => write ! ( f, "Bus error occurred" ) ,
@@ -237,6 +240,7 @@ impl core::fmt::Display for ErrorKind {
237
240
}
238
241
239
242
impl core:: fmt:: Display for NoAcknowledgeSource {
243
+ #[ inline]
240
244
fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
241
245
match self {
242
246
Self :: Address => write ! ( f, "The device did not acknowledge its address" ) ,
@@ -246,7 +250,7 @@ impl core::fmt::Display for NoAcknowledgeSource {
246
250
}
247
251
}
248
252
249
- /// I2C error type trait
253
+ /// I2C error type trait.
250
254
///
251
255
/// This just defines the error type, to be used by the other traits.
252
256
pub trait ErrorType {
@@ -258,15 +262,15 @@ impl<T: ErrorType + ?Sized> ErrorType for &mut T {
258
262
type Error = T :: Error ;
259
263
}
260
264
261
- /// Address mode (7-bit / 10-bit)
265
+ /// Address mode (7-bit / 10-bit).
262
266
///
263
267
/// Note: This trait is sealed and should not be implemented outside of this crate.
264
268
pub trait AddressMode : private:: Sealed + ' static { }
265
269
266
- /// 7-bit address mode type
270
+ /// 7-bit address mode type.
267
271
pub type SevenBitAddress = u8 ;
268
272
269
- /// 10-bit address mode type
273
+ /// 10-bit address mode type.
270
274
pub type TenBitAddress = u16 ;
271
275
272
276
impl AddressMode for SevenBitAddress { }
@@ -279,15 +283,15 @@ impl AddressMode for TenBitAddress {}
279
283
#[ derive( Debug , PartialEq , Eq ) ]
280
284
#[ cfg_attr( feature = "defmt-03" , derive( defmt:: Format ) ) ]
281
285
pub enum Operation < ' a > {
282
- /// Read data into the provided buffer
286
+ /// Read data into the provided buffer.
283
287
Read ( & ' a mut [ u8 ] ) ,
284
- /// Write data from the provided buffer
288
+ /// Write data from the provided buffer.
285
289
Write ( & ' a [ u8 ] ) ,
286
290
}
287
291
288
- /// Blocking I2C
292
+ /// Blocking I2C.
289
293
pub trait I2c < A : AddressMode = SevenBitAddress > : ErrorType {
290
- /// Reads enough bytes from slave with `address` to fill `read`
294
+ /// Reads enough bytes from slave with `address` to fill `read`.
291
295
///
292
296
/// # I2C Events (contract)
293
297
///
@@ -305,11 +309,12 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
305
309
/// - `MAK` = master acknowledge
306
310
/// - `NMAK` = master no acknowledge
307
311
/// - `SP` = stop condition
312
+ #[ inline]
308
313
fn read ( & mut self , address : A , read : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
309
314
self . transaction ( address, & mut [ Operation :: Read ( read) ] )
310
315
}
311
316
312
- /// Writes bytes to slave with address `address`
317
+ /// Writes bytes to slave with address `address`.
313
318
///
314
319
/// # I2C Events (contract)
315
320
///
@@ -325,12 +330,13 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
325
330
/// - `SAK` = slave acknowledge
326
331
/// - `Bi` = ith byte of data
327
332
/// - `SP` = stop condition
333
+ #[ inline]
328
334
fn write ( & mut self , address : A , write : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
329
335
self . transaction ( address, & mut [ Operation :: Write ( write) ] )
330
336
}
331
337
332
338
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `read` *in a
333
- /// single transaction*
339
+ /// single transaction*.
334
340
///
335
341
/// # I2C Events (contract)
336
342
///
@@ -351,6 +357,7 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
351
357
/// - `MAK` = master acknowledge
352
358
/// - `NMAK` = master no acknowledge
353
359
/// - `SP` = stop condition
360
+ #[ inline]
354
361
fn write_read ( & mut self , address : A , write : & [ u8 ] , read : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
355
362
self . transaction (
356
363
address,
@@ -379,18 +386,22 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
379
386
}
380
387
381
388
impl < A : AddressMode , T : I2c < A > + ?Sized > I2c < A > for & mut T {
389
+ #[ inline]
382
390
fn read ( & mut self , address : A , read : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
383
391
T :: read ( self , address, read)
384
392
}
385
393
394
+ #[ inline]
386
395
fn write ( & mut self , address : A , write : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
387
396
T :: write ( self , address, write)
388
397
}
389
398
399
+ #[ inline]
390
400
fn write_read ( & mut self , address : A , write : & [ u8 ] , read : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
391
401
T :: write_read ( self , address, write, read)
392
402
}
393
403
404
+ #[ inline]
394
405
fn transaction (
395
406
& mut self ,
396
407
address : A ,
0 commit comments