Skip to content

Commit c1ec87c

Browse files
committed
Adds an example to the Wifi module and cleans up some public type documentation
1 parent b88597a commit c1ec87c

File tree

6 files changed

+76
-34
lines changed

6 files changed

+76
-34
lines changed

esp32-wroom-rp/src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
//!
88
//! It's intended to communicate with recent versions of most [Arduino-derived WiFiNINA firmwares](https://www.arduino.cc/reference/en/libraries/wifinina/)
99
//! that run on an ESP32-WROOM-XX WiFi module. For example, Adafruit makes such WiFi hardware, referred to as the [Airlift](https://www.adafruit.com/product/4201), and maintains its [firmware](https://github.com/adafruit/nina-fw).
10-
//!
10+
//!
1111
//! This driver is implemented on top of [embedded-hal](https://github.com/rust-embedded/embedded-hal/), which makes it platform-independent, but is currently only intended
1212
//! to be used with [rp2040-hal](https://github.com/rp-rs/rp-hal/tree/main/rp2040-hal) for your application.
13-
//!
13+
//!
1414
//! Please see the README.md for details on where to obtain and how to connect your RP2040-based device (e.g. Pico) to your ESP32-WROOM-XX WiFi board.
15-
//!
15+
//!
1616
//! Once connected, note that all communication with the WiFi board occurs via a SPI bus. As the example below (and all examples under the directory `cross/`)
1717
//! show, you first need to create an `embedded_hal::spi::Spi` instance. See the [rp2040-hal documentation](https://docs.rs/rp2040-hal/0.6.0/rp2040_hal/spi/index.html) along
1818
//! with the datasheet for your device on what specific SPI ports are available to you.
19-
//!
19+
//!
2020
//! You'll also need to reserve 4 important [GPIO pins](https://docs.rs/rp2040-hal/0.6.0/rp2040_hal/gpio/index.html) (3 output, 1 input) that are used to mediate communication between the two boards. The examples
2121
//! also demonstrate how to do this through instantiating an instance of `esp32_wroom_rp::gpio::EspControlPins`.
22-
//!
22+
//!
2323
//! **NOTE:** This crate is still under active development. This API will remain volatile until 1.0.0.
2424
//!
2525
//! ## Usage
@@ -50,7 +50,7 @@
5050
//! use fugit::RateExtU32;
5151
//! use hal::clocks::Clock;
5252
//! use hal::pac;
53-
//!
53+
//!
5454
//! use esp32_wroom_rp::gpio::EspControlPins;
5555
//! use esp32_wroom_rp::wifi::Wifi;
5656
//!
@@ -59,11 +59,11 @@
5959
//! #[link_section = ".boot2"]
6060
//! #[used]
6161
//! pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
62-
//!
62+
//!
6363
//! // External high-speed crystal on the Raspberry Pi Pico board is 12 MHz. Adjust
6464
//! // if your board has a different frequency
6565
//! const XTAL_FREQ_HZ: u32 = 12_000_000u32;
66-
//!
66+
//!
6767
//! // Entry point to our bare-metal application.
6868
//! //
6969
//! // The `#[entry]` macro ensures the Cortex-M start-up code calls this function
@@ -138,11 +138,11 @@
138138
//! loop {}
139139
//! }
140140
//! ```
141-
//!
141+
//!
142142
//! ## More examples
143-
//!
143+
//!
144144
//! Please refer to the `cross/` directory in this crate's source for examples that demonstrate how to use every part of its public API.
145-
//!
145+
//!
146146
147147
#![doc(html_root_url = "https://docs.rs/esp32-wroom-rp")]
148148
#![doc(issue_tracker_base_url = "https://github.com/Jim-Hodapp-Coaching/esp32-wroom-rp/issues")]

esp32-wroom-rp/src/network.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
//! Defines common network functions, types and error definitions.
22
//!
3-
//! ## Usage
4-
//!
5-
//! ```no_run
6-
//! ```
7-
//!
83
94
use defmt::{write, Format, Formatter};
105

esp32-wroom-rp/src/protocol.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
//! Defines functions, types and error definitions related to the WiFiNINA protocol communication specification.
22
//!
3-
//! ## Usage
4-
//!
5-
//! ```no_run
6-
//! ```
7-
//!
83
94
pub(crate) mod operation;
105

esp32-wroom-rp/src/spi.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
//! Serial Peripheral Interface (SPI) for Wifi
1+
//! Serial Peripheral Interface (SPI)
2+
//!
3+
//! Contains all SPI bus related structs, types and errors. Also responsible for
4+
//! implementing WifiNINA protocol communication over a selected SPI interface.
5+
//!
6+
//! Note: Currently everything in this file is private and considered internal to the crate.
7+
//!
28
use core::convert::Infallible;
39

410
use embedded_hal::blocking::delay::DelayMs;

esp32-wroom-rp/src/tcp_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
//! );
4040
//! }
4141
//! ```
42-
//!
42+
//!
4343
4444
use embedded_hal::blocking::delay::DelayMs;
4545
use embedded_hal::blocking::spi::Transfer;

esp32-wroom-rp/src/wifi.rs

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,52 @@
33
//! ## Usage
44
//!
55
//! ```no_run
6+
//! let mut wifi = Wifi::init(spi, esp_pins, &mut delay).unwrap();
7+
//!
8+
//! let result = wifi.join(ssid, passphrase);
9+
//! defmt::info!("Join Result: {:?}", result);
10+
//!
11+
//! defmt::info!("Entering main loop");
12+
//!
13+
//! let mut sleep: u32 = 1500;
14+
//! loop {
15+
//! match wifi.get_connection_status() {
16+
//! Ok(status) => {
17+
//! defmt::info!("Connection status: {:?}", status);
18+
//! delay.delay_ms(sleep);
19+
//!
20+
//! if status == ConnectionStatus::Connected {
21+
//! defmt::info!("Connected to network: {:?}", SSID);
22+
//!
23+
//! // The IPAddresses of two DNS servers to resolve hostnames with.
24+
//! // Note that failover from ip1 to ip2 is fully functional.
25+
//! let ip1: IpAddress = [9, 9, 9, 9];
26+
//! let ip2: IpAddress = [8, 8, 8, 8];
27+
//! let dns_result = wifi.set_dns(ip1, Some(ip2));
28+
//!
29+
//! defmt::info!("set_dns result: {:?}", dns_result);
30+
//!
31+
//! match wifi.resolve(hostname) {
32+
//! Ok(ip) => {
33+
//! defmt::info!("Server IP: {:?}", ip);
34+
//! }
35+
//! Err(e) => {
36+
//! defmt::error!("Failed to resolve hostname {}", hostname);
37+
//! defmt::error!("Err: {}", e);
38+
//! }
39+
//! }
40+
//!
41+
//! defmt::info!("Leaving network: {:?}", ssid);
42+
//! wifi.leave().ok();
43+
//! } else if status == ConnectionStatus::Disconnected {
44+
//! sleep = 20000; // No need to loop as often after disconnecting
45+
//! }
46+
//! }
47+
//! Err(e) => {
48+
//! defmt::error!("Failed to get connection result: {:?}", e);
49+
//! }
50+
//! }
51+
//! }
652
//! ```
753
//!
854
@@ -98,7 +144,7 @@ impl Format for ConnectionStatus {
98144
}
99145
}
100146

101-
/// Fundamental struct for controlling a connected ESP32-WROOM NINA firmware-based Wifi board.
147+
/// Base type for controlling an ESP32-WROOM NINA firmware-based WiFi board.
102148
#[derive(Debug)]
103149
pub struct Wifi<B, C> {
104150
pub(crate) protocol_handler: RefCell<NinaProtocolHandler<B, C>>,
@@ -109,8 +155,8 @@ where
109155
S: Transfer<u8>,
110156
C: EspControlInterface,
111157
{
112-
/// Initializes the ESP32-WROOM Wifi device.
113-
/// Calling this function puts the connected ESP32-WROOM device in a known good state to accept commands.
158+
/// Initialize the ESP32-WROOM WiFi device.
159+
/// Call this function to put the connected ESP32-WROOM device in a known good state to accept commands.
114160
pub fn init<D: DelayMs<u16>>(
115161
spi: S,
116162
esp32_control_pins: C,
@@ -128,42 +174,42 @@ where
128174
Ok(wifi)
129175
}
130176

131-
/// Retrieves the NINA firmware version contained on the connected ESP32-WROOM device (e.g. 1.7.4).
177+
/// Retrieve the NINA firmware version contained on the connected ESP32-WROOM device (e.g. 1.7.4).
132178
pub fn firmware_version(&mut self) -> Result<FirmwareVersion, Error> {
133179
self.protocol_handler.borrow_mut().get_fw_version()
134180
}
135181

136-
/// Joins a WiFi network given an SSID and a Passphrase.
182+
/// Join a WiFi network given an SSID and a Passphrase.
137183
pub fn join(&mut self, ssid: &str, passphrase: &str) -> Result<(), Error> {
138184
self.protocol_handler
139185
.borrow_mut()
140186
.set_passphrase(ssid, passphrase)
141187
}
142188

143-
/// Disconnects from a joined WiFi network.
189+
/// Disconnect from a previously joined WiFi network.
144190
pub fn leave(&mut self) -> Result<(), Error> {
145191
self.protocol_handler.borrow_mut().disconnect()
146192
}
147193

148-
/// Retrieves the current WiFi network connection status.
194+
/// Retrieve the current WiFi network [`ConnectionStatus`].
149195
pub fn get_connection_status(&mut self) -> Result<ConnectionStatus, Error> {
150196
self.protocol_handler.borrow_mut().get_conn_status()
151197
}
152198

153-
/// Sets 1 or 2 DNS servers that are used for network hostname resolution.
199+
/// Set 1 or 2 DNS servers that are used for network hostname resolution.
154200
pub fn set_dns(&mut self, dns1: IpAddress, dns2: Option<IpAddress>) -> Result<(), Error> {
155201
self.protocol_handler
156202
.borrow_mut()
157203
.set_dns_config(dns1, dns2)
158204
}
159205

160-
/// Queries the DNS server(s) provided via [set_dns] for the associated IP address to the provided hostname.
206+
/// Query the DNS server(s) provided via [`set_dns`] for the associated IP address to the provided hostname.
161207
pub fn resolve(&mut self, hostname: &str) -> Result<IpAddress, Error> {
162208
self.protocol_handler.borrow_mut().resolve(hostname)
163209
}
164210

165-
/// Provides a reference to the `Spi` bus instance typically used when cleaning up
166-
/// an instance of `Wifi`.
211+
/// Return a reference to the `Spi` bus instance typically used when cleaning up
212+
/// an instance of [`Wifi`].
167213
pub fn destroy(self) -> S {
168214
self.protocol_handler.into_inner().bus.into_inner()
169215
}

0 commit comments

Comments
 (0)