16
16
17
17
#include < SparkFun_u-blox_SARA-R5_Arduino_Library.h>
18
18
19
- SARA_R5::SARA_R5 (int powerPin, int resetPin, uint8_t maxInitDepth )
19
+ SARA_R5::SARA_R5 (int powerPin, int resetPin, uint8_t maxInitTries )
20
20
{
21
21
#ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED
22
22
_softSerial = NULL ;
@@ -26,7 +26,7 @@ SARA_R5::SARA_R5(int powerPin, int resetPin, uint8_t maxInitDepth)
26
26
_resetPin = resetPin;
27
27
_powerPin = powerPin;
28
28
_invertPowerPin = false ;
29
- _maxInitDepth = maxInitDepth ;
29
+ _maxInitTries = maxInitTries ;
30
30
_socketListenCallback = NULL ;
31
31
_socketReadCallback = NULL ;
32
32
_socketReadCallbackPlus = NULL ;
@@ -47,7 +47,6 @@ SARA_R5::SARA_R5(int powerPin, int resetPin, uint8_t maxInitDepth)
47
47
_autoTimeZoneForBegin = true ;
48
48
_bufferedPollReentrant = false ;
49
49
_pollReentrant = false ;
50
- _currentInitDepth = 0 ;
51
50
_saraResponseBacklogLength = 0 ;
52
51
}
53
52
@@ -690,8 +689,8 @@ size_t SARA_R5::write(const char *buffer, size_t size)
690
689
SARA_R5_error_t SARA_R5::at (void )
691
690
{
692
691
SARA_R5_error_t err;
693
-
694
- err = sendCommandWithResponse (NULL , SARA_R5_RESPONSE_OK_OR_ERROR , NULL ,
692
+
693
+ err = sendCommandWithResponse (NULL , SARA_R5_RESPONSE_OK , NULL ,
695
694
SARA_R5_STANDARD_RESPONSE_TIMEOUT);
696
695
697
696
return err;
@@ -705,19 +704,10 @@ SARA_R5_error_t SARA_R5::enableEcho(bool enable)
705
704
command = sara_r5_calloc_char (strlen (SARA_R5_COMMAND_ECHO) + 2 );
706
705
if (command == NULL )
707
706
return SARA_R5_ERROR_OUT_OF_MEMORY;
708
- if (enable)
709
- {
710
- sprintf (command, " %s1" , SARA_R5_COMMAND_ECHO);
711
- }
712
- else
713
- {
714
- sprintf (command, " %s0" , SARA_R5_COMMAND_ECHO);
715
- }
716
- err = sendCommandWithResponse (command, SARA_R5_RESPONSE_OK_OR_ERROR,
707
+ sprintf (command, " %s%d" , SARA_R5_COMMAND_ECHO, enable ? 1 : 0 );
708
+ err = sendCommandWithResponse (command, SARA_R5_RESPONSE_OK,
717
709
NULL , SARA_R5_STANDARD_RESPONSE_TIMEOUT);
718
-
719
710
free (command);
720
-
721
711
return err;
722
712
}
723
713
@@ -4628,7 +4618,11 @@ SARA_R5_error_t SARA_R5::appendFileContents(String filename, const char *str, in
4628
4618
4629
4619
err = sendCommandWithResponse (command, " >" , response,
4630
4620
SARA_R5_STANDARD_RESPONSE_TIMEOUT*2 );
4631
- delay (50 );
4621
+
4622
+ unsigned long writeDelay = millis ();
4623
+ while (millis () < (writeDelay + 50 ))
4624
+ delay (1 ); // uBlox specification says to wait 50ms after receiving "@" to write data.
4625
+
4632
4626
if (err == SARA_R5_ERROR_SUCCESS)
4633
4627
{
4634
4628
if (_printDebug == true )
@@ -5025,58 +5019,60 @@ void SARA_R5::modulePowerOn(void)
5025
5019
SARA_R5_error_t SARA_R5::init (unsigned long baud,
5026
5020
SARA_R5::SARA_R5_init_type_t initType)
5027
5021
{
5028
- SARA_R5_error_t err;
5029
-
5030
- // If we have recursively called init too many times, bail
5031
- _currentInitDepth++;
5032
- if (_currentInitDepth == _maxInitDepth)
5022
+ int retries = _maxInitTries;
5023
+ SARA_R5_error_t err = SARA_R5_ERROR_SUCCESS;
5024
+
5025
+ do
5033
5026
{
5034
5027
if (_printDebug == true )
5035
- _debugPort->println (F (" init: Module failed to init. Exiting." ));
5036
- return (SARA_R5_ERROR_NO_RESPONSE);
5037
- }
5038
-
5039
- if (_printDebug == true )
5040
- _debugPort->println (F (" init: Begin module init." ));
5028
+ _debugPort->println (F (" init: Begin module init." ));
5041
5029
5042
- // There's no 'easy' way to tell if the serial port has already been begun for us.
5043
- // We have to assume it has not been begun and so do it here.
5044
- // For special cases like Software Serial on ESP32, we need to begin _and_ end the port externally
5045
- // _before_ calling the SARA_R5 .begin.
5046
- // See SARA-R5_Example2_Identification_ESPSoftwareSerial for more details.
5047
- beginSerial (baud);
5030
+ beginSerial (baud);
5048
5031
5049
- if (initType == SARA_R5_INIT_AUTOBAUD)
5050
- {
5051
- if (_printDebug == true )
5052
- _debugPort->println (F (" init: Attempting autobaud connection to module." ));
5053
- if (autobaud (baud) != SARA_R5_ERROR_SUCCESS)
5032
+ if (initType == SARA_R5_INIT_AUTOBAUD)
5033
+ {
5034
+ if (_printDebug == true )
5035
+ _debugPort->println (F (" init: Attempting autobaud connection to module." ));
5036
+
5037
+ err = autobaud (baud);
5038
+ if (err != SARA_R5_ERROR_SUCCESS) {
5039
+ initType = SARA_R5_INIT_RESET;
5040
+ }
5041
+ }
5042
+ else if (initType == SARA_R5_INIT_RESET)
5054
5043
{
5055
- return init (baud, SARA_R5_INIT_RESET);
5044
+ if (_printDebug == true )
5045
+ _debugPort->println (F (" init: Power cycling module." ));
5046
+
5047
+ powerOff ();
5048
+ delay (SARA_R5_POWER_OFF_PULSE_PERIOD);
5049
+ powerOn ();
5050
+ delay (2000 );
5051
+
5052
+ err = at ();
5053
+ if (err != SARA_R5_ERROR_SUCCESS)
5054
+ {
5055
+ initType = SARA_R5_INIT_AUTOBAUD;
5056
+ }
5056
5057
}
5057
- }
5058
- else if (initType == SARA_R5_INIT_RESET)
5059
- {
5060
- if (_printDebug == true )
5061
- _debugPort->println (F (" init: Power cycling module." ));
5062
- powerOff ();
5063
- delay (SARA_R5_POWER_OFF_PULSE_PERIOD);
5064
- powerOn ();
5065
- delay (2000 );
5066
- if (at () != SARA_R5_ERROR_SUCCESS)
5058
+ if (err == SARA_R5_ERROR_SUCCESS)
5067
5059
{
5068
- return init (baud, SARA_R5_INIT_AUTOBAUD);
5060
+ err = enableEcho (false ); // = disableEcho
5061
+ if (err != SARA_R5_ERROR_SUCCESS)
5062
+ {
5063
+ if (_printDebug == true )
5064
+ _debugPort->println (F (" init: Module failed echo test." ));
5065
+ initType = SARA_R5_INIT_AUTOBAUD;
5066
+ }
5069
5067
}
5070
5068
}
5071
-
5072
- // Use disable echo to test response
5073
- err = enableEcho (false );
5074
-
5075
- if (err != SARA_R5_ERROR_SUCCESS)
5076
- {
5069
+ while ((retries --) && (err != SARA_R5_ERROR_SUCCESS));
5070
+
5071
+ // we tried but seems failed
5072
+ if (err != SARA_R5_ERROR_SUCCESS) {
5077
5073
if (_printDebug == true )
5078
- _debugPort->println (F (" init: Module failed echo test ." ));
5079
- return init (baud, SARA_R5_INIT_AUTOBAUD );
5074
+ _debugPort->println (F (" init: Module failed to init. Exiting ." ));
5075
+ return (SARA_R5_ERROR_NO_RESPONSE );
5080
5076
}
5081
5077
5082
5078
if (_printDebug == true )
@@ -5323,7 +5319,7 @@ SARA_R5_error_t SARA_R5::waitForResponse(const char *expectedResponse, const cha
5323
5319
}
5324
5320
else
5325
5321
{
5326
- responseIndex = 0 ;
5322
+ responseIndex = ((responseIndex < responseLen) && (c == expectedResponse[ 0 ])) ? 1 : 0 ;
5327
5323
}
5328
5324
if ((errorIndex < errorLen) && (c == expectedError[errorIndex]))
5329
5325
{
@@ -5335,7 +5331,7 @@ SARA_R5_error_t SARA_R5::waitForResponse(const char *expectedResponse, const cha
5335
5331
}
5336
5332
else
5337
5333
{
5338
- errorIndex = 0 ;
5334
+ errorIndex = ((errorIndex < errorLen) && (c == expectedError[ 0 ])) ? 1 : 0 ;
5339
5335
}
5340
5336
// _saraResponseBacklog is a global array that holds the backlog of any events
5341
5337
// that came in while waiting for response. To be processed later within bufferedPoll().
@@ -5362,7 +5358,6 @@ SARA_R5_error_t SARA_R5::waitForResponse(const char *expectedResponse, const cha
5362
5358
if (found == true )
5363
5359
{
5364
5360
if (true == _printAtDebug) {
5365
- _debugAtPort->print (" \r\n " );
5366
5361
_debugAtPort->print ((error == true ) ? expectedError : expectedResponse);
5367
5362
}
5368
5363
@@ -5446,7 +5441,7 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
5446
5441
}
5447
5442
else
5448
5443
{
5449
- errorIndex = 0 ;
5444
+ errorIndex = ((errorIndex < errorLen) && (c == expectedError[ 0 ])) ? 1 : 0 ;
5450
5445
}
5451
5446
if ((responseIndex < responseLen) && (c == expectedResponse[responseIndex]))
5452
5447
{
@@ -5457,7 +5452,7 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
5457
5452
}
5458
5453
else
5459
5454
{
5460
- responseIndex = 0 ;
5455
+ responseIndex = ((responseIndex < responseLen) && (c == expectedResponse[ 0 ])) ? 1 : 0 ;
5461
5456
}
5462
5457
// _saraResponseBacklog is a global array that holds the backlog of any events
5463
5458
// that came in while waiting for response. To be processed later within bufferedPoll().
@@ -5474,7 +5469,7 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
5474
5469
}
5475
5470
}
5476
5471
}
5477
-
5472
+
5478
5473
// if (_printDebug == true)
5479
5474
// if (printedSomething)
5480
5475
// _debugPort->println();
0 commit comments