Skip to content

Commit ea91e5d

Browse files
authored
Avoid recession to allow working in a task.
- Avoid nasty recursion in init just use a do - while loop. recursion is almost always a bad thing. - switch the parser to more sticks end tags \r\nXX\r\n - fix an issue in the waiting for response to break a special case where a spurious duplicate first character e.g. \r\r\nXX\r\n would not be detected as successful expected response. this happens e.g. if ATE0\r is sent while echo is enabled you will get ATE0\r\r\nXX\r\n
1 parent 4d70bc7 commit ea91e5d

File tree

2 files changed

+66
-72
lines changed

2 files changed

+66
-72
lines changed

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include <SparkFun_u-blox_SARA-R5_Arduino_Library.h>
1818

19-
SARA_R5::SARA_R5(int powerPin, int resetPin, uint8_t maxInitDepth)
19+
SARA_R5::SARA_R5(int powerPin, int resetPin, uint8_t maxInitTries)
2020
{
2121
#ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED
2222
_softSerial = NULL;
@@ -26,7 +26,7 @@ SARA_R5::SARA_R5(int powerPin, int resetPin, uint8_t maxInitDepth)
2626
_resetPin = resetPin;
2727
_powerPin = powerPin;
2828
_invertPowerPin = false;
29-
_maxInitDepth = maxInitDepth;
29+
_maxInitTries = maxInitTries;
3030
_socketListenCallback = NULL;
3131
_socketReadCallback = NULL;
3232
_socketReadCallbackPlus = NULL;
@@ -47,7 +47,6 @@ SARA_R5::SARA_R5(int powerPin, int resetPin, uint8_t maxInitDepth)
4747
_autoTimeZoneForBegin = true;
4848
_bufferedPollReentrant = false;
4949
_pollReentrant = false;
50-
_currentInitDepth = 0;
5150
_saraResponseBacklogLength = 0;
5251
}
5352

@@ -690,8 +689,8 @@ size_t SARA_R5::write(const char *buffer, size_t size)
690689
SARA_R5_error_t SARA_R5::at(void)
691690
{
692691
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,
695694
SARA_R5_STANDARD_RESPONSE_TIMEOUT);
696695

697696
return err;
@@ -705,19 +704,10 @@ SARA_R5_error_t SARA_R5::enableEcho(bool enable)
705704
command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_ECHO) + 2);
706705
if (command == NULL)
707706
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,
717709
NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT);
718-
719710
free(command);
720-
721711
return err;
722712
}
723713

@@ -4628,7 +4618,11 @@ SARA_R5_error_t SARA_R5::appendFileContents(String filename, const char *str, in
46284618

46294619
err = sendCommandWithResponse(command, ">", response,
46304620
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+
46324626
if (err == SARA_R5_ERROR_SUCCESS)
46334627
{
46344628
if (_printDebug == true)
@@ -5025,58 +5019,60 @@ void SARA_R5::modulePowerOn(void)
50255019
SARA_R5_error_t SARA_R5::init(unsigned long baud,
50265020
SARA_R5::SARA_R5_init_type_t initType)
50275021
{
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
50335026
{
50345027
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."));
50415029

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);
50485031

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)
50545043
{
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+
}
50565057
}
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)
50675059
{
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+
}
50695067
}
50705068
}
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) {
50775073
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);
50805076
}
50815077

50825078
if (_printDebug == true)
@@ -5323,7 +5319,7 @@ SARA_R5_error_t SARA_R5::waitForResponse(const char *expectedResponse, const cha
53235319
}
53245320
else
53255321
{
5326-
responseIndex = 0;
5322+
responseIndex = ((responseIndex < responseLen) && (c == expectedResponse[0])) ? 1 : 0;
53275323
}
53285324
if ((errorIndex < errorLen) && (c == expectedError[errorIndex]))
53295325
{
@@ -5335,7 +5331,7 @@ SARA_R5_error_t SARA_R5::waitForResponse(const char *expectedResponse, const cha
53355331
}
53365332
else
53375333
{
5338-
errorIndex = 0;
5334+
errorIndex = ((errorIndex < errorLen) && (c == expectedError[0])) ? 1 : 0;
53395335
}
53405336
//_saraResponseBacklog is a global array that holds the backlog of any events
53415337
//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
53625358
if (found == true)
53635359
{
53645360
if (true == _printAtDebug) {
5365-
_debugAtPort->print("\r\n");
53665361
_debugAtPort->print((error == true) ? expectedError : expectedResponse);
53675362
}
53685363

@@ -5446,7 +5441,7 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
54465441
}
54475442
else
54485443
{
5449-
errorIndex = 0;
5444+
errorIndex = ((errorIndex < errorLen) && (c == expectedError[0])) ? 1 : 0;
54505445
}
54515446
if ((responseIndex < responseLen) && (c == expectedResponse[responseIndex]))
54525447
{
@@ -5457,7 +5452,7 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
54575452
}
54585453
else
54595454
{
5460-
responseIndex = 0;
5455+
responseIndex = ((responseIndex < responseLen) && (c == expectedResponse[0])) ? 1 : 0;
54615456
}
54625457
//_saraResponseBacklog is a global array that holds the backlog of any events
54635458
//that came in while waiting for response. To be processed later within bufferedPoll().
@@ -5474,7 +5469,7 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
54745469
}
54755470
}
54765471
}
5477-
5472+
54785473
// if (_printDebug == true)
54795474
// if (printedSomething)
54805475
// _debugPort->println();

src/SparkFun_u-blox_SARA-R5_Arduino_Library.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ const char SARA_R5_SEC_PROFILE[] = "+USECPRF";
185185
const char SARA_R5_SEC_MANAGER[] = "+USECMNG";
186186

187187
// ### Response
188-
const char SARA_R5_RESPONSE_OK[] = "OK\r\n";
189-
const char SARA_R5_RESPONSE_ERROR[] = "ERROR\r\n";
190-
const char SARA_R5_RESPONSE_CONNECT[] = "CONNECT\r\n";
188+
const char SARA_R5_RESPONSE_OK[] = "\r\nOK\r\n";
189+
const char SARA_R5_RESPONSE_ERROR[] = "\r\nERROR\r\n";
190+
const char SARA_R5_RESPONSE_CONNECT[] = "\r\nCONNECT\r\n";
191191
#define SARA_R5_RESPONSE_OK_OR_ERROR NULL
192192

193193
// CTRL+Z and ESC ASCII codes for SMS message sends
@@ -573,8 +573,8 @@ class SARA_R5 : public Print
573573
public:
574574
// Constructor
575575
// The library will use the powerPin and resetPin (if provided) to power the module off/on and perform an emergency reset
576-
// maxInitDepth sets the maximum number of initialization attempts (recursive). .init is called by .begin.
577-
SARA_R5(int powerPin = SARA_R5_POWER_PIN, int resetPin = SARA_R5_RESET_PIN, uint8_t maxInitDepth = 9);
576+
// maxInitTries sets the maximum number of initialization attempts. .init is called by .begin.
577+
SARA_R5(int powerPin = SARA_R5_POWER_PIN, int resetPin = SARA_R5_RESET_PIN, uint8_t maxInitTries = 9);
578578

579579
// Begin -- initialize module and ensure it's connected
580580
#ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED
@@ -949,8 +949,7 @@ class SARA_R5 : public Print
949949
unsigned long _baud;
950950
IPAddress _lastRemoteIP;
951951
IPAddress _lastLocalIP;
952-
uint8_t _maxInitDepth;
953-
uint8_t _currentInitDepth = 0;
952+
uint8_t _maxInitTries;
954953
bool _autoTimeZoneForBegin = true;
955954
bool _bufferedPollReentrant = false; // Prevent reentry of bufferedPoll - just in case it gets called from a callback
956955
bool _pollReentrant = false; // Prevent reentry of poll - just in case it gets called from a callback

0 commit comments

Comments
 (0)