Skip to content

Commit de25160

Browse files
committed
Add ProtocolError
1 parent 285410a commit de25160

File tree

5 files changed

+34
-52
lines changed

5 files changed

+34
-52
lines changed

cross/get_fw_version/src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ fn main() -> ! {
6363
.ok()
6464
.unwrap();
6565

66-
let mut delay = cortex_m::delay::Delay::new(
67-
core.SYST,
68-
clocks.system_clock.freq().integer(),
69-
);
66+
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().integer());
7067

7168
// The single-cycle I/O block controls our GPIO pins
7269
let sio = hal::Sio::new(pac.SIO);

cross/join/src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ fn main() -> ! {
6666
.ok()
6767
.unwrap();
6868

69-
let mut delay = cortex_m::delay::Delay::new(
70-
core.SYST,
71-
clocks.system_clock.freq().integer(),
72-
);
69+
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().integer());
7370

7471
// The single-cycle I/O block controls our GPIO pins
7572
let sio = hal::Sio::new(pac.SIO);

esp32-wroom-rp/src/gpio.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ enum IOError {
4141

4242
/// Provides an internal pin interface that abstracts the extra control lines that
4343
/// are separate from a data bus (e.g. SPI/I2C).
44-
///
44+
///
4545
/// Not meant to be used outside of the crate.
4646
pub trait EspControlInterface {
4747
/// Initializes all controls pins to set ready communication with the NINA firmware.
@@ -98,27 +98,27 @@ where
9898
{
9999
fn init(&mut self) {
100100
// Chip select is active-low, so we'll initialize it to a driven-high state
101-
self.cs.set_high().ok().unwrap();
102-
self.gpio0.set_high().ok().unwrap();
103-
self.resetn.set_high().ok().unwrap();
101+
self.cs.set_high().ok();
102+
self.gpio0.set_high().ok();
103+
self.resetn.set_high().ok();
104104
self.get_esp_ready();
105105
}
106106

107107
fn reset<D: DelayMs<u16>>(&mut self, delay: &mut D) {
108-
self.gpio0.set_high().ok().unwrap();
109-
self.cs.set_high().ok().unwrap();
110-
self.resetn.set_low().ok().unwrap();
108+
self.gpio0.set_high().ok();
109+
self.cs.set_high().ok();
110+
self.resetn.set_low().ok();
111111
delay.delay_ms(10);
112-
self.resetn.set_high().ok().unwrap();
112+
self.resetn.set_high().ok();
113113
delay.delay_ms(750);
114114
}
115115

116116
fn esp_select(&mut self) {
117-
self.cs.set_low().ok().unwrap();
117+
self.cs.set_low().ok();
118118
}
119119

120120
fn esp_deselect(&mut self) {
121-
self.cs.set_high().ok().unwrap();
121+
self.cs.set_high().ok();
122122
}
123123

124124
fn get_esp_ready(&self) -> bool {

esp32-wroom-rp/src/protocol.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,8 @@ pub(crate) struct NinaProtocolHandler<B, C> {
236236
/// An EspControlPins instance
237237
pub control_pins: C,
238238
}
239+
240+
pub(crate) enum Error {
241+
NinaProtocolVersionMismatch,
242+
Timeout,
243+
}

esp32-wroom-rp/src/spi.rs

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ use super::protocol::{
77
};
88

99
use super::protocol::operation::Operation;
10+
use super::protocol::Error as ProtocolError;
1011
use super::{Error, FirmwareVersion, WifiCommon, ARRAY_LENGTH_PLACEHOLDER};
1112

1213
use embedded_hal::blocking::delay::DelayMs;
1314
use embedded_hal::blocking::spi::Transfer;
1415

16+
use core::convert::Infallible;
17+
1518
// TODO: this should eventually move into NinaCommandHandler
1619
#[repr(u8)]
1720
#[derive(Debug)]
@@ -251,44 +254,24 @@ where
251254
Ok(())
252255
}
253256

254-
fn get_byte(&mut self) -> Result<u8, self::Error> {
255-
// Blocking read, don't return until we've read a byte successfully
256-
loop {
257-
let word_out = &mut [ControlByte::Dummy as u8];
258-
match self.bus.transfer(word_out) {
259-
Ok(word) => {
260-
let byte: u8 = word[0] as u8;
261-
return Ok(byte);
262-
}
263-
Err(_e) => {
264-
continue;
265-
}
266-
}
267-
}
257+
fn get_byte(&mut self) -> Result<u8, Infallible> {
258+
let word_out = &mut [ControlByte::Dummy as u8];
259+
let word = self.bus.transfer(word_out).ok().unwrap();
260+
Ok(word[0] as u8)
268261
}
269262

270-
fn wait_for_byte(&mut self, wait_byte: u8) -> Result<bool, self::Error> {
271-
let mut timeout: u16 = 1000u16;
272-
273-
loop {
274-
match self.get_byte() {
275-
Ok(byte_read) => {
276-
if byte_read == ControlByte::Error as u8 {
277-
return Ok(false);
278-
//return Err(SPIError::Misc);
279-
} else if byte_read == wait_byte {
280-
return Ok(true);
281-
} else if timeout == 0 {
282-
return Ok(false);
283-
//return Err(SPIError::Timeout);
284-
}
285-
timeout -= 1;
286-
}
287-
Err(e) => {
288-
return Err(e);
289-
}
263+
fn wait_for_byte(&mut self, wait_byte: u8) -> Result<bool, ProtocolError> {
264+
let retry_limit: u16 = 1000u16;
265+
266+
for _ in 0..retry_limit {
267+
let byte_read = self.get_byte().ok().unwrap();
268+
if byte_read == ControlByte::Error as u8 {
269+
return Err(ProtocolError::NinaProtocolVersionMismatch);
270+
} else if byte_read == wait_byte {
271+
return Ok(true);
290272
}
291273
}
274+
Err(ProtocolError::Timeout)
292275
}
293276

294277
fn check_start_cmd(&mut self) -> Result<bool, self::Error> {

0 commit comments

Comments
 (0)