Skip to content

Commit 8aa1a88

Browse files
authored
Prefer cfg_if (#2003)
1 parent 1003ce0 commit 8aa1a88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+517
-386
lines changed

documentation/API-GUIDELINES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ The following paragraphs contain additional recommendations.
5353
- If necessary provide further context as comments (consider linking to code, PRs, TRM - make sure to use permanent links, e.g. include the hash when linking to a Git repository, include the revision, page number etc. when linking to TRMs)
5454
- Generally, follow common "good practices" and idiomatic Rust style
5555
- All `Future` objects (public or private) must be marked with ``#[must_use = "futures do nothing unless you `.await` or poll them"]``.
56+
- Prefer `cfg_if!` over multiple exclusive `#[cfg]` attributes. `cfg_if!` visually divides the options, often results in simpler conditions and simplifies adding new branches in the future.
5657

5758
## Modules Documentation
5859

esp-hal-embassy/src/executor/thread.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ impl Executor {
7171
#[cfg_attr(
7272
multi_core,
7373
doc = r#"
74+
7475
This will use software-interrupt 3 which isn't
7576
available for anything else to wake the other core(s).
7677
"#

esp-hal/src/aes/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
//! # Advanced Encryption Standard (AES).
22
//!
33
//! ## Overview
4+
//!
45
//! The AES accelerator is a hardware device that speeds up computation
56
//! using AES algorithm significantly, compared to AES algorithms implemented
67
//! solely in software. The AES accelerator has two working modes, which are
78
//! Typical AES and AES-DMA.
89
//!
910
//! ## Configuration
11+
//!
1012
//! The AES peripheral can be configured to encrypt or decrypt data using
1113
//! different encryption/decryption modes.
1214
//!
1315
//! When using AES-DMA, the peripheral can be configured to use different block
1416
//! cipher modes such as ECB, CBC, OFB, CTR, CFB8, and CFB128.
1517
//!
1618
//! ## Examples
17-
//! ### Encrypting and Decrypting a Message
19+
//!
20+
//! ### Encrypting and decrypting a message
21+
//!
1822
//! Simple example of encrypting and decrypting a message using AES-128:
23+
//!
1924
//! ```rust, no_run
2025
#![doc = crate::before_snippet!()]
2126
//! # use esp_hal::aes::{Aes, Mode};

esp-hal/src/analog/adc/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//! # Analog to Digital Converter (ADC)
22
//!
33
//! ## Overview
4+
//!
45
//! The ADC is integrated on the chip, and is capable of measuring analog
56
//! signals from specific analog I/O pins. One or more ADC units are available,
67
//! depending on the device being used.
78
//!
89
//! ## Configuration
10+
//!
911
//! The ADC can be configured to measure analog signals from specific pins. The
1012
//! configuration includes the resolution of the ADC, the attenuation of the
1113
//! input signal, and the pins to be measured.
@@ -15,10 +17,13 @@
1517
//! schemes can be used to improve the accuracy of the ADC readings.
1618
//!
1719
//! ## Usage
20+
//!
1821
//! The ADC driver implements the `embedded-hal@0.2.x` ADC traits.
1922
//!
20-
//! ## Examples
21-
//! #### Read an analog signal from a pin
23+
//! ## Example
24+
//!
25+
//! ### Read an analog signal from a pin
26+
//!
2227
//! ```rust, no_run
2328
#![doc = crate::before_snippet!()]
2429
//! # use esp_hal::analog::adc::AdcConfig;

esp-hal/src/clock/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
//! # Clock Control
1+
//! # CPU Clock Control
22
//!
33
//! ## Overview
4+
//!
45
//! Clocks are mainly sourced from oscillator (OSC), RC, and PLL circuits, and
56
//! then processed by the dividers or selectors, which allows most functional
67
//! modules to select their working clock according to their power consumption

esp-hal/src/dma/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! # Direct Memory Access (DMA)
22
//!
33
//! ## Overview
4+
//!
45
//! The DMA driver provides an interface to efficiently transfer data between
56
//! different memory regions and peripherals within the ESP microcontroller
67
//! without involving the CPU. The DMA controller is responsible for managing
@@ -10,8 +11,10 @@
1011
//! `ESP32-S2` are using older `PDMA` controller, whenever other chips are using
1112
//! newer `GDMA` controller.
1213
//!
13-
//! ## Examples
14-
//! ### Initialize and Utilize DMA Controller in `SPI`
14+
//! ## Example
15+
//!
16+
//! ### Initialize and utilize DMA controller in `SPI`
17+
//!
1518
//! ```rust, no_run
1619
#![doc = crate::before_snippet!()]
1720
//! # use esp_hal::dma_buffers;

esp-hal/src/gpio/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
//! # General Purpose I/Os (GPIO)
22
//!
33
//! ## Overview
4+
//!
45
//! Each pin can be used as a general-purpose I/O, or be connected to an
56
//! internal peripheral signal.
67
//!
78
//! ## Configuration
9+
//!
810
//! This driver supports various operations on GPIO pins, including setting the
911
//! pin mode, direction, and manipulating the pin state (setting high/low,
1012
//! toggling). It provides an interface to interact with GPIO pins on ESP chips,
1113
//! allowing developers to control and read the state of the pins.
1214
//!
1315
//! ## Usage
16+
//!
1417
//! This module also implements a number of traits from [embedded-hal] to
1518
//! provide a common interface for GPIO pins.
1619
//!
1720
//! To get access to the pins, you first need to convert them into a HAL
1821
//! designed struct from the pac struct `GPIO` and `IO_MUX` using `Io::new`.
1922
//!
2023
//! ### Pin Types
24+
//!
2125
//! - [Input] pins can be used as digital inputs.
2226
//! - [Output] and [OutputOpenDrain] pins can be used as digital outputs.
2327
//! - [Flex] pin is a pin that can be used as an input and output pin.
@@ -27,7 +31,9 @@
2731
//! but real pin cannot be used.
2832
//!
2933
//! ## Examples
34+
//!
3035
//! ### Set up a GPIO as an Output
36+
//!
3137
//! ```rust, no_run
3238
#![doc = crate::before_snippet!()]
3339
//! # use esp_hal::gpio::{Io, Level, Output};
@@ -37,6 +43,7 @@
3743
//! ```
3844
//!
3945
//! ### Blink an LED
46+
//!
4047
//! See the [Commonly Used Setup] section of the crate documentation.
4148
//!
4249
//! ### Inverting a signal using `AnyPin`

esp-hal/src/i2c.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,12 +1068,15 @@ pub trait Instance: crate::private::Sealed {
10681068
self.set_filter(Some(7), Some(7));
10691069

10701070
// Configure frequency
1071-
#[cfg(esp32)]
1072-
self.set_frequency(clocks.i2c_clock.convert(), frequency, timeout);
1073-
#[cfg(esp32s2)]
1074-
self.set_frequency(clocks.apb_clock.convert(), frequency, timeout);
1075-
#[cfg(not(any(esp32, esp32s2)))]
1076-
self.set_frequency(clocks.xtal_clock.convert(), frequency, timeout);
1071+
cfg_if::cfg_if! {
1072+
if #[cfg(esp32)] {
1073+
self.set_frequency(clocks.i2c_clock.convert(), frequency, timeout);
1074+
} else if #[cfg(esp32s2)] {
1075+
self.set_frequency(clocks.apb_clock.convert(), frequency, timeout);
1076+
} else {
1077+
self.set_frequency(clocks.xtal_clock.convert(), frequency, timeout);
1078+
}
1079+
}
10771080

10781081
self.update_config();
10791082

esp-hal/src/interrupt/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
//! We reserve a number of CPU interrupts, which cannot be used; see
2525
//! [`RESERVED_INTERRUPTS`].
2626
//!
27-
//! ## Examples
28-
//! ### Using the Peripheral Driver to Register an Interrupt Handler
27+
//! ## Example
28+
//!
29+
//! ### Using the peripheral driver to register an interrupt handler
30+
//!
2931
//! ```rust, no_run
3032
#![doc = crate::before_snippet!()]
3133
//! # use core::cell::RefCell;

esp-hal/src/ledc/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! # LED Controller (LEDC)
22
//!
33
//! ## Overview
4+
//!
45
//! The LEDC peripheral is primarily designed to control the intensity of LEDs,
56
//! although it can also be used to generate PWM signals for other purposes. It
67
//! has multiple channels which can generate independent waveforms that can be
@@ -15,6 +16,7 @@
1516
//! supported chips.
1617
//!
1718
//! ## Examples
19+
//!
1820
//! ### Low Speed Channel
1921
//! The following will configure the Low Speed Channel0 to 24kHz output with
2022
//! 10% duty using the ABPClock

0 commit comments

Comments
 (0)