Skip to content

Commit 02dfb64

Browse files
committed
GSM: add function to wait for modem ready with timeout
1 parent aec787b commit 02dfb64

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

libraries/GSM/src/GSM.cpp

+23-4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ int arduino::GSMClass::begin(const char* pin, const char* apn, const char* usern
6161

6262
_device = _context->get_device();
6363

64+
if (!isReady()) {
65+
DEBUG_ERROR("Cellular device not ready");
66+
return 0;
67+
}
68+
6469
_device->modem_debug_on(_at_debug);
6570

6671
_device->set_cmux_status_flag(_cmuxGSMenable);
@@ -160,10 +165,24 @@ void arduino::GSMClass::reset() {
160165
delay(1);
161166
digitalWrite(MBED_CONF_GEMALTO_CINTERION_ON, HIGH);
162167
delay(1);
163-
// this timer is to make sure that at boottime and when the CMUX is used,
164-
// ^SYSTART is received in time to avoid stranger behaviour
165-
// from HW serial
166-
delay(2000);
168+
}
169+
170+
bool arduino::GSMClass::isReady(const int timeout) {
171+
if (!_device) {
172+
DEBUG_ERROR("No device found");
173+
return false;
174+
}
175+
176+
const unsigned int start = millis();
177+
while (_device->is_ready() != NSAPI_ERROR_OK) {
178+
179+
if (millis() - start > timeout) {
180+
DEBUG_WARNING("Timeout waiting device ready");
181+
return false;
182+
}
183+
delay(100);
184+
}
185+
return true;
167186
}
168187

169188
arduino::GSMClass GSM;

libraries/GSM/src/GSM.h

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class GSMClass : public MbedSocketClass {
174174
void onStatusChange(nsapi_event_t ev, intptr_t in);
175175
#endif
176176
void reset();
177+
bool isReady(const int timeout = 5000);
177178
};
178179

179180
}

0 commit comments

Comments
 (0)