Skip to content

Commit 65894c2

Browse files
jhodappcalebbourg
authored andcommitted
Adds a first host-side integration test that tests for the protocol::ProtocolError::NinaProtocolVersionMismatch error
1 parent 4985dd0 commit 65894c2

File tree

8 files changed

+105
-38
lines changed

8 files changed

+105
-38
lines changed

cross/get_fw_version/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ fn main() -> ! {
8686
let spi = hal::Spi::<_, _, 8>::new(pac.SPI0);
8787

8888
// Exchange the uninitialized SPI driver for an initialized one
89-
let spi = spi.init(
89+
let mut spi = spi.init(
9090
&mut pac.RESETS,
9191
clocks.peripheral_clock.freq(),
9292
8_000_000u32.Hz(),
9393
&MODE_0,
9494
);
9595

96-
let esp_pins = esp32_wroom_rp::gpio::EspControlPins {
96+
let mut esp_pins = esp32_wroom_rp::gpio::EspControlPins {
9797
// CS on pin x (GPIO7)
9898
cs: pins.gpio7.into_mode::<hal::gpio::PushPullOutput>(),
9999
// GPIO0 on pin x (GPIO2)
@@ -103,7 +103,7 @@ fn main() -> ! {
103103
// ACK on pin x (GPIO10)
104104
ack: pins.gpio10.into_mode::<hal::gpio::FloatingInput>(),
105105
};
106-
let mut wifi = esp32_wroom_rp::wifi::Wifi::init(spi, esp_pins, &mut delay).unwrap();
106+
let mut wifi = esp32_wroom_rp::wifi::Wifi::init(&mut spi, &mut esp_pins, &mut delay).unwrap();
107107
let firmware_version = wifi.firmware_version();
108108
defmt::info!("NINA firmware version: {:?}", firmware_version);
109109

cross/join/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ fn main() -> ! {
8989
let spi = hal::Spi::<_, _, 8>::new(pac.SPI0);
9090

9191
// Exchange the uninitialized SPI driver for an initialized one
92-
let spi = spi.init(
92+
let mut spi = spi.init(
9393
&mut pac.RESETS,
9494
clocks.peripheral_clock.freq(),
9595
8_000_000u32.Hz(),
9696
&MODE_0,
9797
);
9898

99-
let esp_pins = esp32_wroom_rp::gpio::EspControlPins {
99+
let mut esp_pins = esp32_wroom_rp::gpio::EspControlPins {
100100
// CS on pin x (GPIO7)
101101
cs: pins.gpio7.into_mode::<hal::gpio::PushPullOutput>(),
102102
// GPIO0 on pin x (GPIO2)
@@ -107,7 +107,8 @@ fn main() -> ! {
107107
ack: pins.gpio10.into_mode::<hal::gpio::FloatingInput>(),
108108
};
109109

110-
let mut wifi = esp32_wroom_rp::wifi::Wifi::init(spi, esp_pins, &mut delay).unwrap();
110+
let mut wifi = esp32_wroom_rp::wifi::Wifi::init(&mut spi, &mut esp_pins, &mut delay).unwrap();
111+
111112
let result = wifi.join(SSID, PASSPHRASE);
112113
defmt::info!("Join Result: {:?}", result);
113114

esp32-wroom-rp/src/gpio.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub trait EspControlInterface {
7575
/// A structured representation of all GPIO pins that control a ESP32-WROOM NINA firmware-based
7676
/// device outside of commands sent over the SPI/I²C bus. Pass a single instance of this struct
7777
/// into `Wifi::init()`.
78-
pub struct EspControlPins<CS: OutputPin, GPIO0: OutputPin, RESETN: OutputPin, ACK: InputPin> {
78+
pub struct EspControlPins<CS, GPIO0, RESETN, ACK> {
7979
/// Chip select pin to let the NINA firmware know we're going to send it a command over
8080
/// the SPI bus.
8181
pub cs: CS,
@@ -131,13 +131,13 @@ where
131131

132132
fn wait_for_esp_ready(&self) {
133133
while self.get_esp_ready() != true {
134-
cortex_m::asm::nop(); // Make sure rustc doesn't optimize this loop out
134+
//cortex_m::asm::nop(); // Make sure rustc doesn't optimize this loop out
135135
}
136136
}
137137

138138
fn wait_for_esp_ack(&self) {
139139
while self.get_esp_ack() == false {
140-
cortex_m::asm::nop(); // Make sure rustc doesn't optimize this loop out
140+
//cortex_m::asm::nop(); // Make sure rustc doesn't optimize this loop out
141141
}
142142
}
143143

@@ -148,6 +148,17 @@ where
148148
}
149149
}
150150

151+
impl Default for EspControlPins<(), (), (), ()> {
152+
fn default() -> Self {
153+
Self {
154+
cs: (),
155+
gpio0: (),
156+
resetn: (),
157+
ack: ()
158+
}
159+
}
160+
}
161+
151162
#[cfg(test)]
152163
mod gpio_tests {
153164
use super::EspControlPins;

esp32-wroom-rp/src/lib.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
//! ack: pins.gpio10.into_mode::<hal::gpio::FloatingInput>(),
7676
//! };
7777
//!
78-
//! let mut wifi = esp32_wroom_rp::spi::Wifi::init(spi, esp_pins, &mut delay).unwrap();
78+
//! let mut wifi = esp32_wroom_rp::spi::Wifi::init(&mut spi, &mut esp_pins, &mut delay).unwrap();
7979
//! let version = wifi.firmware_version();
8080
//! ```
8181
@@ -88,7 +88,7 @@ pub mod gpio;
8888
/// Fundamental interface for controlling a connected ESP32-WROOM NINA firmware-based Wifi board.
8989
pub mod wifi;
9090

91-
mod protocol;
91+
pub mod protocol;
9292
mod spi;
9393

9494
use protocol::{ProtocolInterface, ProtocolError};
@@ -99,7 +99,7 @@ use embedded_hal::blocking::delay::DelayMs;
9999
const ARRAY_LENGTH_PLACEHOLDER: usize = 8;
100100

101101
/// Highest level error types for this crate.
102-
#[derive(Debug)]
102+
#[derive(Debug, PartialEq)]
103103
pub enum Error {
104104
/// SPI/I2C related communications error with the ESP32 WiFi target
105105
Bus,
@@ -118,13 +118,7 @@ impl Format for Error {
118118

119119
impl From<protocol::ProtocolError> for Error {
120120
fn from(err: protocol::ProtocolError) -> Self {
121-
match err {
122-
protocol::ProtocolError::CommunicationTimeout => Error::Protocol(err),
123-
protocol::ProtocolError::InvalidCommand => Error::Protocol(err),
124-
protocol::ProtocolError::InvalidNumberOfParameters => Error::Protocol(err),
125-
protocol::ProtocolError::NinaProtocolVersionMismatch => Error::Protocol(err),
126-
protocol::ProtocolError::TooManyParameters => Error::Protocol(err),
127-
}
121+
Error::Protocol(err)
128122
}
129123
}
130124

@@ -167,7 +161,7 @@ impl Format for FirmwareVersion {
167161
}
168162
}
169163

170-
#[derive(Debug, Default)]
164+
#[derive(Debug)]
171165
struct WifiCommon<PH> {
172166
protocol_handler: PH,
173167
}

esp32-wroom-rp/src/protocol.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub mod operation;
1+
pub(crate) mod operation;
22

33
use super::*;
44

@@ -8,7 +8,7 @@ use defmt::{write, Format, Formatter};
88

99
use heapless::{String, Vec};
1010

11-
pub const MAX_NINA_PARAM_LENGTH: usize = 255;
11+
pub(crate) const MAX_NINA_PARAM_LENGTH: usize = 255;
1212

1313
#[repr(u8)]
1414
#[derive(Copy, Clone, Debug)]
@@ -231,15 +231,15 @@ pub(crate) trait ProtocolInterface {
231231
fn get_conn_status(&mut self) -> Result<u8, ProtocolError>;
232232
}
233233

234-
#[derive(Debug, Default)]
235-
pub(crate) struct NinaProtocolHandler<B, C> {
234+
#[derive(Debug)]
235+
pub(crate) struct NinaProtocolHandler<'a, B, C> {
236236
/// A Spi or I2c instance
237-
pub bus: B,
237+
pub bus: &'a mut B,
238238
/// An EspControlPins instance
239-
pub control_pins: C,
239+
pub control_pins: &'a mut C,
240240
}
241241

242-
#[derive(Debug)]
242+
#[derive(Debug, PartialEq)]
243243
pub enum ProtocolError {
244244
NinaProtocolVersionMismatch,
245245
CommunicationTimeout,

esp32-wroom-rp/src/spi.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ enum ControlByte {
2727
}
2828

2929
/// Fundamental struct for controlling a connected ESP32-WROOM NINA firmware-based Wifi board.
30-
#[derive(Debug, Default)]
31-
pub struct Wifi<B, C> {
32-
common: WifiCommon<NinaProtocolHandler<B, C>>,
30+
#[derive(Debug)]
31+
pub struct Wifi<'a, B, C> {
32+
common: WifiCommon<NinaProtocolHandler<'a, B, C>>,
3333
}
3434

35-
impl<S, C> Wifi<S, C>
35+
impl<'a, S, C> Wifi<'a, S, C>
3636
where
3737
S: Transfer<u8>,
3838
C: EspControlInterface,
3939
{
4040
/// Initializes the ESP32-WROOM Wifi device.
4141
/// Calling this function puts the connected ESP32-WROOM device in a known good state to accept commands.
4242
pub fn init<D: DelayMs<u16>>(
43-
spi: S,
44-
control_pins: C,
43+
spi: &'a mut S,
44+
control_pins: &'a mut C,
4545
delay: &mut D,
46-
) -> Result<Wifi<S, C>, Error> {
46+
) -> Result<Wifi<'a, S, C>, Error> {
4747
let mut wifi = Wifi {
4848
common: WifiCommon {
4949
protocol_handler: NinaProtocolHandler {
@@ -82,7 +82,7 @@ where
8282
}
8383

8484
// All SPI-specific aspects of the NinaProtocolHandler go here in this struct impl
85-
impl<S, C> ProtocolInterface for NinaProtocolHandler<S, C>
85+
impl<'a, S, C> ProtocolInterface for NinaProtocolHandler<'a, S, C>
8686
where
8787
S: Transfer<u8>,
8888
C: EspControlInterface,
@@ -142,7 +142,7 @@ where
142142
}
143143
}
144144

145-
impl<S, C> NinaProtocolHandler<S, C>
145+
impl<'a, S, C> NinaProtocolHandler<'a, S, C>
146146
where
147147
S: Transfer<u8>,
148148
C: EspControlInterface,
@@ -199,6 +199,8 @@ where
199199

200200
for byte in buf {
201201
let write_buf = &mut [byte];
202+
// FIXME: temporary for test writing debugging
203+
defmt::debug!("0x{:02x}, ", write_buf[0]);
202204
self.bus.transfer(write_buf).ok();
203205
}
204206

@@ -213,7 +215,7 @@ where
213215
cmd: &NinaCommand,
214216
num_params: u8,
215217
) -> Result<[u8; ARRAY_LENGTH_PLACEHOLDER], ProtocolError> {
216-
self.check_start_cmd().ok().unwrap();
218+
self.check_start_cmd()?;
217219
let byte_to_check: u8 = *cmd as u8 | ControlByte::Reply as u8;
218220
let result = self.read_and_check_byte(&byte_to_check).ok().unwrap();
219221
// Ensure we see a cmd byte

host-tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ description = "Host-side tests for the Rust-based Espressif ESP32-WROOM WiFi dri
1212

1313
[dev-dependencies]
1414
embedded-hal-mock = "0.8.0"
15+
esp32-wroom-rp = { path = "../esp32-wroom-rp" }

host-tests/tests/spi.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,60 @@
1+
use embedded_hal_mock::spi;
2+
use embedded_hal_mock::pin::{
3+
Mock as PinMock, State as PinState, Transaction as PinTransaction,
4+
};
5+
use embedded_hal_mock::delay::MockNoop;
6+
7+
use esp32_wroom_rp::wifi::Wifi;
8+
use esp32_wroom_rp::gpio::EspControlInterface;
9+
10+
struct EspControlMock {}
11+
12+
impl EspControlInterface for EspControlMock {
13+
fn init(&mut self) {}
14+
15+
fn reset<D>(&mut self, _delay: &mut D) {}
16+
17+
fn get_esp_ack(&self) -> bool {
18+
true
19+
}
20+
21+
fn wait_for_esp_select(&mut self) {}
22+
23+
fn wait_for_esp_ack(&self) {}
24+
25+
fn wait_for_esp_ready(&self) {}
26+
27+
fn esp_select(&mut self) {}
28+
29+
fn esp_deselect(&mut self) {}
30+
31+
fn get_esp_ready(&self) -> bool {
32+
true
33+
}
34+
}
35+
136
#[test]
2-
fn do_something_with_spi_succeeds() {}
37+
fn invalid_command_induces_nina_protocol_version_mismatch_error() {
38+
let spi_expectations = vec![
39+
// send_cmd()
40+
spi::Transaction::transfer(vec![0xe0], vec![0x0]),
41+
spi::Transaction::transfer(vec![0x37], vec![0x0]),
42+
spi::Transaction::transfer(vec![0x0], vec![0x0]),
43+
spi::Transaction::transfer(vec![0xee], vec![0x0]),
44+
45+
// wait_response_cmd()
46+
spi::Transaction::transfer(vec![0xff], vec![0xef]),
47+
];
48+
let mut spi = spi::Mock::new(&spi_expectations);
49+
50+
let mut delay = MockNoop::new();
51+
52+
let mut pins = EspControlMock {};
53+
54+
let mut wifi = Wifi::init(&mut spi, &mut pins, &mut delay).ok().unwrap();
55+
let f = wifi.firmware_version();
56+
57+
assert_eq!(f.unwrap_err(), esp32_wroom_rp::Error::Protocol(esp32_wroom_rp::protocol::ProtocolError::NinaProtocolVersionMismatch));
58+
59+
spi.done();
60+
}

0 commit comments

Comments
 (0)