@@ -7,11 +7,14 @@ use super::protocol::{
77} ;
88
99use super :: protocol:: operation:: Operation ;
10+ use super :: protocol:: Error as ProtocolError ;
1011use super :: { Error , FirmwareVersion , WifiCommon , ARRAY_LENGTH_PLACEHOLDER } ;
1112
1213use embedded_hal:: blocking:: delay:: DelayMs ;
1314use 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