Skip to content

Commit a0e1f49

Browse files
committed
e-h: amend documents to add periods (.), add inline hints when necessary
In documentation style of the Rust standard library, first sentence of all modules, types, and functions documentation has a period. We follow Rust standard library style to make it easier for users to read. All functions in embedded-hal (especially type conversations, function fowarding calls) are now marked #[inline] to allow further optimizations. Signed-off-by: Zhouqi Jiang <[email protected]>
1 parent 9228fb2 commit a0e1f49

File tree

5 files changed

+123
-73
lines changed

5 files changed

+123
-73
lines changed

embedded-hal/src/delay.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
//! Delays
1+
//! Delays.
22
3-
/// Microsecond delay
4-
///
3+
/// Microsecond delay.
54
pub trait DelayUs {
65
/// Pauses execution for at minimum `us` microseconds. Pause can be longer
76
/// if the implementation requires it due to precision/timing issues.
87
fn delay_us(&mut self, us: u32);
98

109
/// Pauses execution for at minimum `ms` milliseconds. Pause can be longer
1110
/// if the implementation requires it due to precision/timing issues.
11+
#[inline]
1212
fn delay_ms(&mut self, ms: u32) {
1313
for _ in 0..ms {
1414
self.delay_us(1000);
@@ -20,10 +20,12 @@ impl<T> DelayUs for &mut T
2020
where
2121
T: DelayUs + ?Sized,
2222
{
23+
#[inline]
2324
fn delay_us(&mut self, us: u32) {
2425
T::delay_us(self, us)
2526
}
2627

28+
#[inline]
2729
fn delay_ms(&mut self, ms: u32) {
2830
T::delay_ms(self, ms)
2931
}

embedded-hal/src/digital.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
//! Digital I/O
1+
//! Digital I/O.
22
33
use core::{convert::From, ops::Not};
44

55
#[cfg(feature = "defmt-03")]
66
use crate::defmt;
77

8-
/// Error
8+
/// Error.
99
pub trait Error: core::fmt::Debug {
1010
/// Convert error to a generic error kind
1111
///
@@ -21,7 +21,7 @@ impl Error for core::convert::Infallible {
2121
}
2222
}
2323

24-
/// Error kind
24+
/// Error kind.
2525
///
2626
/// This represents a common set of operation errors. HAL implementations are
2727
/// free to define more specific or additional error types. However, by providing
@@ -35,12 +35,14 @@ pub enum ErrorKind {
3535
}
3636

3737
impl Error for ErrorKind {
38+
#[inline]
3839
fn kind(&self) -> ErrorKind {
3940
*self
4041
}
4142
}
4243

4344
impl core::fmt::Display for ErrorKind {
45+
#[inline]
4446
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
4547
match self {
4648
Self::Other => write!(
@@ -51,7 +53,7 @@ impl core::fmt::Display for ErrorKind {
5153
}
5254
}
5355

54-
/// Error type trait
56+
/// Error type trait.
5557
///
5658
/// This just defines the error type, to be used by the other traits.
5759
pub trait ErrorType {
@@ -67,7 +69,7 @@ impl<T: ErrorType + ?Sized> ErrorType for &mut T {
6769
type Error = T::Error;
6870
}
6971

70-
/// Digital output pin state
72+
/// Digital output pin state.
7173
///
7274
/// Conversion from `bool` and logical negation are also implemented
7375
/// for this type.
@@ -80,9 +82,9 @@ impl<T: ErrorType + ?Sized> ErrorType for &mut T {
8082
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
8183
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
8284
pub enum PinState {
83-
/// Low pin state
85+
/// Low pin state.
8486
Low,
85-
/// High pin state
87+
/// High pin state.
8688
High,
8789
}
8890

@@ -118,24 +120,25 @@ impl From<PinState> for bool {
118120
}
119121
}
120122

121-
/// Single digital push-pull output pin
123+
/// Single digital push-pull output pin.
122124
pub trait OutputPin: ErrorType {
123-
/// Drives the pin low
125+
/// Drives the pin low.
124126
///
125127
/// *NOTE* the actual electrical state of the pin may not actually be low, e.g. due to external
126-
/// electrical sources
128+
/// electrical sources.
127129
fn set_low(&mut self) -> Result<(), Self::Error>;
128130

129-
/// Drives the pin high
131+
/// Drives the pin high.
130132
///
131133
/// *NOTE* the actual electrical state of the pin may not actually be high, e.g. due to external
132-
/// electrical sources
134+
/// electrical sources.
133135
fn set_high(&mut self) -> Result<(), Self::Error>;
134136

135-
/// Drives the pin high or low depending on the provided value
137+
/// Drives the pin high or low depending on the provided value.
136138
///
137139
/// *NOTE* the actual electrical state of the pin may not actually be high or low, e.g. due to external
138-
/// electrical sources
140+
/// electrical sources.
141+
#[inline]
139142
fn set_state(&mut self, state: PinState) -> Result<(), Self::Error> {
140143
match state {
141144
PinState::Low => self.set_low(),
@@ -145,55 +148,61 @@ pub trait OutputPin: ErrorType {
145148
}
146149

147150
impl<T: OutputPin + ?Sized> OutputPin for &mut T {
151+
#[inline]
148152
fn set_low(&mut self) -> Result<(), Self::Error> {
149153
T::set_low(self)
150154
}
151155

156+
#[inline]
152157
fn set_high(&mut self) -> Result<(), Self::Error> {
153158
T::set_high(self)
154159
}
155160

161+
#[inline]
156162
fn set_state(&mut self, state: PinState) -> Result<(), Self::Error> {
157163
T::set_state(self, state)
158164
}
159165
}
160166

161-
/// Push-pull output pin that can read its output state
167+
/// Push-pull output pin that can read its output state.
162168
pub trait StatefulOutputPin: OutputPin {
163169
/// Is the pin in drive high mode?
164170
///
165-
/// *NOTE* this does *not* read the electrical state of the pin
171+
/// *NOTE* this does *not* read the electrical state of the pin.
166172
fn is_set_high(&self) -> Result<bool, Self::Error>;
167173

168174
/// Is the pin in drive low mode?
169175
///
170-
/// *NOTE* this does *not* read the electrical state of the pin
176+
/// *NOTE* this does *not* read the electrical state of the pin.
171177
fn is_set_low(&self) -> Result<bool, Self::Error>;
172178
}
173179

174180
impl<T: StatefulOutputPin + ?Sized> StatefulOutputPin for &mut T {
181+
#[inline]
175182
fn is_set_high(&self) -> Result<bool, Self::Error> {
176183
T::is_set_high(self)
177184
}
178185

186+
#[inline]
179187
fn is_set_low(&self) -> Result<bool, Self::Error> {
180188
T::is_set_low(self)
181189
}
182190
}
183191

184-
/// Output pin that can be toggled
192+
/// Output pin that can be toggled.
185193
pub trait ToggleableOutputPin: ErrorType {
186194
/// Toggle pin output.
187195
fn toggle(&mut self) -> Result<(), Self::Error>;
188196
}
189197

190198
impl<T: ToggleableOutputPin + ?Sized> ToggleableOutputPin for &mut T {
199+
#[inline]
191200
fn toggle(&mut self) -> Result<(), Self::Error> {
192201
T::toggle(self)
193202
}
194203
}
195204

196-
/// Single digital input pin
205+
/// Single digital input pin.
197206
pub trait InputPin: ErrorType {
198207
/// Is the input pin high?
199208
fn is_high(&self) -> Result<bool, Self::Error>;
@@ -203,10 +212,12 @@ pub trait InputPin: ErrorType {
203212
}
204213

205214
impl<T: InputPin + ?Sized> InputPin for &T {
215+
#[inline]
206216
fn is_high(&self) -> Result<bool, Self::Error> {
207217
T::is_high(self)
208218
}
209219

220+
#[inline]
210221
fn is_low(&self) -> Result<bool, Self::Error> {
211222
T::is_low(self)
212223
}

embedded-hal/src/i2c.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Blocking I2C API
1+
//! Blocking I2C API.
22
//!
33
//! This API supports 7-bit and 10-bit addresses. Traits feature an [`AddressMode`]
44
//! marker type parameter. Two implementation of the [`AddressMode`] exist:
@@ -157,9 +157,9 @@ use crate::private;
157157
#[cfg(feature = "defmt-03")]
158158
use crate::defmt;
159159

160-
/// I2C error
160+
/// I2C error.
161161
pub trait Error: core::fmt::Debug {
162-
/// Convert error to a generic I2C error kind
162+
/// Convert error to a generic I2C error kind.
163163
///
164164
/// By using this method, I2C errors freely defined by HAL implementations
165165
/// can be converted to a set of generic I2C errors upon which generic
@@ -168,12 +168,13 @@ pub trait Error: core::fmt::Debug {
168168
}
169169

170170
impl Error for core::convert::Infallible {
171+
#[inline]
171172
fn kind(&self) -> ErrorKind {
172173
match *self {}
173174
}
174175
}
175176

176-
/// I2C error kind
177+
/// I2C error kind.
177178
///
178179
/// This represents a common set of I2C operation errors. HAL implementations are
179180
/// free to define more specific or additional error types. However, by providing
@@ -185,19 +186,19 @@ pub enum ErrorKind {
185186
/// Bus error occurred. e.g. A START or a STOP condition is detected and is not
186187
/// located after a multiple of 9 SCL clock pulses.
187188
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.
189190
ArbitrationLoss,
190191
/// A bus operation was not acknowledged, e.g. due to the addressed device not
191192
/// being available on the bus or the device not being ready to process requests
192-
/// at the moment
193+
/// at the moment.
193194
NoAcknowledge(NoAcknowledgeSource),
194-
/// The peripheral receive buffer was overrun
195+
/// The peripheral receive buffer was overrun.
195196
Overrun,
196197
/// A different error occurred. The original error may contain more information.
197198
Other,
198199
}
199200

200-
/// I2C no acknowledge error source
201+
/// I2C no acknowledge error source.
201202
///
202203
/// In cases where it is possible, a device should indicate if a no acknowledge
203204
/// response was received to an address versus a no acknowledge to a data byte.
@@ -216,12 +217,14 @@ pub enum NoAcknowledgeSource {
216217
}
217218

218219
impl Error for ErrorKind {
220+
#[inline]
219221
fn kind(&self) -> ErrorKind {
220222
*self
221223
}
222224
}
223225

224226
impl core::fmt::Display for ErrorKind {
227+
#[inline]
225228
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
226229
match self {
227230
Self::Bus => write!(f, "Bus error occurred"),
@@ -237,6 +240,7 @@ impl core::fmt::Display for ErrorKind {
237240
}
238241

239242
impl core::fmt::Display for NoAcknowledgeSource {
243+
#[inline]
240244
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
241245
match self {
242246
Self::Address => write!(f, "The device did not acknowledge its address"),
@@ -246,7 +250,7 @@ impl core::fmt::Display for NoAcknowledgeSource {
246250
}
247251
}
248252

249-
/// I2C error type trait
253+
/// I2C error type trait.
250254
///
251255
/// This just defines the error type, to be used by the other traits.
252256
pub trait ErrorType {
@@ -258,15 +262,15 @@ impl<T: ErrorType + ?Sized> ErrorType for &mut T {
258262
type Error = T::Error;
259263
}
260264

261-
/// Address mode (7-bit / 10-bit)
265+
/// Address mode (7-bit / 10-bit).
262266
///
263267
/// Note: This trait is sealed and should not be implemented outside of this crate.
264268
pub trait AddressMode: private::Sealed + 'static {}
265269

266-
/// 7-bit address mode type
270+
/// 7-bit address mode type.
267271
pub type SevenBitAddress = u8;
268272

269-
/// 10-bit address mode type
273+
/// 10-bit address mode type.
270274
pub type TenBitAddress = u16;
271275

272276
impl AddressMode for SevenBitAddress {}
@@ -279,15 +283,15 @@ impl AddressMode for TenBitAddress {}
279283
#[derive(Debug, PartialEq, Eq)]
280284
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
281285
pub enum Operation<'a> {
282-
/// Read data into the provided buffer
286+
/// Read data into the provided buffer.
283287
Read(&'a mut [u8]),
284-
/// Write data from the provided buffer
288+
/// Write data from the provided buffer.
285289
Write(&'a [u8]),
286290
}
287291

288-
/// Blocking I2C
292+
/// Blocking I2C.
289293
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`.
291295
///
292296
/// # I2C Events (contract)
293297
///
@@ -305,11 +309,12 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
305309
/// - `MAK` = master acknowledge
306310
/// - `NMAK` = master no acknowledge
307311
/// - `SP` = stop condition
312+
#[inline]
308313
fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
309314
self.transaction(address, &mut [Operation::Read(read)])
310315
}
311316

312-
/// Writes bytes to slave with address `address`
317+
/// Writes bytes to slave with address `address`.
313318
///
314319
/// # I2C Events (contract)
315320
///
@@ -325,12 +330,13 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
325330
/// - `SAK` = slave acknowledge
326331
/// - `Bi` = ith byte of data
327332
/// - `SP` = stop condition
333+
#[inline]
328334
fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> {
329335
self.transaction(address, &mut [Operation::Write(write)])
330336
}
331337

332338
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `read` *in a
333-
/// single transaction*
339+
/// single transaction*.
334340
///
335341
/// # I2C Events (contract)
336342
///
@@ -351,6 +357,7 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
351357
/// - `MAK` = master acknowledge
352358
/// - `NMAK` = master no acknowledge
353359
/// - `SP` = stop condition
360+
#[inline]
354361
fn write_read(&mut self, address: A, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
355362
self.transaction(
356363
address,
@@ -379,18 +386,22 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
379386
}
380387

381388
impl<A: AddressMode, T: I2c<A> + ?Sized> I2c<A> for &mut T {
389+
#[inline]
382390
fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
383391
T::read(self, address, read)
384392
}
385393

394+
#[inline]
386395
fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> {
387396
T::write(self, address, write)
388397
}
389398

399+
#[inline]
390400
fn write_read(&mut self, address: A, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
391401
T::write_read(self, address, write, read)
392402
}
393403

404+
#[inline]
394405
fn transaction(
395406
&mut self,
396407
address: A,

0 commit comments

Comments
 (0)