Skip to content

Commit 2c6280b

Browse files
author
Seppo Takalo
authored
Merge pull request #11066 from AriParkkila/cell-fix-restart
Cellular: Fix cellular statemachine stop and BG96 power up
2 parents e88101f + 742bfd3 commit 2c6280b

File tree

3 files changed

+64
-55
lines changed

3 files changed

+64
-55
lines changed

features/cellular/framework/device/CellularDevice.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,20 @@ nsapi_error_t CellularDevice::create_state_machine()
124124
_nw->attach(callback(this, &CellularDevice::stm_callback));
125125
_state_machine = new CellularStateMachine(*this, *get_queue(), *_nw);
126126
_state_machine->set_cellular_callback(callback(this, &CellularDevice::stm_callback));
127-
err = _state_machine->start_dispatch();
128-
if (err) {
129-
tr_error("Start state machine failed.");
130-
delete _state_machine;
131-
_state_machine = NULL;
132-
}
133-
134127
if (strlen(_plmn)) {
135128
_state_machine->set_plmn(_plmn);
136129
}
137130
if (strlen(_sim_pin)) {
138131
_state_machine->set_sim_pin(_sim_pin);
139132
}
140133
}
134+
err = _state_machine->start_dispatch();
135+
if (err) {
136+
tr_error("Start state machine failed.");
137+
delete _state_machine;
138+
_state_machine = NULL;
139+
return err;
140+
}
141141
return err;
142142
}
143143

features/cellular/framework/device/CellularStateMachine.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ void CellularStateMachine::stop()
103103
{
104104
tr_debug("CellularStateMachine stop");
105105
if (_queue_thread) {
106-
_queue.break_dispatch();
107106
_queue_thread->terminate();
108107
delete _queue_thread;
109108
_queue_thread = NULL;
@@ -366,6 +365,9 @@ void CellularStateMachine::state_device_ready()
366365
_status = 0;
367366
enter_to_state(STATE_SIM_PIN);
368367
}
368+
} else {
369+
_status = 0;
370+
enter_to_state(STATE_INIT);
369371
}
370372
}
371373
if (_cb_data.error != NSAPI_ERROR_OK) {
@@ -546,7 +548,7 @@ bool CellularStateMachine::get_current_status(CellularStateMachine::CellularStat
546548
void CellularStateMachine::event()
547549
{
548550
// Don't send Signal quality when in signal quality state or it can confuse callback functions when running retry logic
549-
if (_state != STATE_SIGNAL_QUALITY) {
551+
if (_state > STATE_SIGNAL_QUALITY) {
550552
_cb_data.error = _network.get_signal_quality(_signal_quality.rssi, &_signal_quality.ber);
551553
_cb_data.data = &_signal_quality;
552554

@@ -624,15 +626,21 @@ void CellularStateMachine::event()
624626

625627
nsapi_error_t CellularStateMachine::start_dispatch()
626628
{
627-
MBED_ASSERT(!_queue_thread);
629+
if (!_queue_thread) {
630+
_queue_thread = new rtos::Thread(osPriorityNormal, 2048, NULL, "stm_queue");
631+
_event_id = STM_STOPPED;
632+
}
628633

629-
_queue_thread = new rtos::Thread(osPriorityNormal, 2048, NULL, "stm_queue");
630-
if (_queue_thread->start(callback(&_queue, &events::EventQueue::dispatch_forever)) != osOK) {
631-
report_failure("Failed to start thread.");
632-
stop();
633-
return NSAPI_ERROR_NO_MEMORY;
634+
if (_event_id == STM_STOPPED) {
635+
if (_queue_thread->start(callback(&_queue, &events::EventQueue::dispatch_forever)) != osOK) {
636+
report_failure("Failed to start thread.");
637+
stop();
638+
return NSAPI_ERROR_NO_MEMORY;
639+
}
634640
}
635641

642+
_event_id = -1;
643+
636644
return NSAPI_ERROR_OK;
637645
}
638646

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,36 @@ nsapi_error_t QUECTEL_BG96::hard_power_on()
111111

112112
nsapi_error_t QUECTEL_BG96::soft_power_on()
113113
{
114-
if (_rst.is_connected()) {
115-
tr_info("Reset modem");
116-
_rst = !_active_high;
117-
ThisThread::sleep_for(100);
118-
_rst = _active_high;
119-
ThisThread::sleep_for(150 + 460); // RESET_N timeout from BG96_Hardware_Design_V1.1
120-
_rst = !_active_high;
121-
ThisThread::sleep_for(500);
114+
if (!_rst.is_connected()) {
115+
return NSAPI_ERROR_OK;
116+
}
122117

123-
// wait for RDY
124-
_at->lock();
125-
_at->set_at_timeout(10 * 1000);
126-
_at->resp_start();
127-
_at->set_stop_tag("RDY");
128-
bool rdy = _at->consume_to_stop_tag();
129-
_at->set_stop_tag(OK);
130-
_at->restore_at_timeout();
131-
_at->unlock();
118+
tr_info("Reset modem");
119+
_rst = !_active_high;
120+
ThisThread::sleep_for(100);
121+
_rst = _active_high;
122+
ThisThread::sleep_for(150 + 460); // RESET_N timeout from BG96_Hardware_Design_V1.1
123+
_rst = !_active_high;
124+
ThisThread::sleep_for(500);
132125

133-
if (!rdy) {
134-
return NSAPI_ERROR_DEVICE_ERROR;
135-
}
126+
// wait for RDY
127+
_at->lock();
128+
_at->set_at_timeout(10 * 1000);
129+
_at->resp_start();
130+
_at->set_stop_tag("RDY");
131+
bool rdy = _at->consume_to_stop_tag();
132+
_at->set_stop_tag(OK);
133+
_at->restore_at_timeout();
134+
135+
if (!rdy) {
136+
// check if modem was silently powered on
137+
_at->clear_error();
138+
_at->set_at_timeout(100);
139+
_at->cmd_start("AT");
140+
_at->cmd_stop_read_resp();
141+
_at->restore_at_timeout();
136142
}
137-
138-
return NSAPI_ERROR_OK;
143+
return _at->unlock_return_error();
139144
}
140145

141146
nsapi_error_t QUECTEL_BG96::hard_power_off()
@@ -162,26 +167,22 @@ nsapi_error_t QUECTEL_BG96::init()
162167
_at->cmd_start("AT+CMEE=1"); // verbose responses
163168
_at->cmd_stop_read_resp();
164169

165-
if (_at->get_last_error() == NSAPI_ERROR_OK) {
166-
do {
167-
_at->cmd_start("AT+CFUN=1"); // set full functionality
168-
_at->cmd_stop_read_resp();
169-
170-
// CFUN executed ok
171-
if (_at->get_last_error() != NSAPI_ERROR_OK) {
172-
// wait some time that modem gets ready for CFUN command, and try again
173-
retry++;
174-
_at->flush();
175-
ThisThread::sleep_for(64); // experimental value
176-
} else {
177-
// yes continue
178-
break;
179-
}
180-
181-
/* code */
182-
} while ((retry < 3));
170+
if (_at->get_last_error() != NSAPI_ERROR_OK) {
171+
return _at->unlock_return_error();
183172
}
184173

174+
do {
175+
_at->clear_error();
176+
_at->cmd_start("AT+CFUN=1"); // set full functionality
177+
_at->cmd_stop_read_resp();
178+
if (_at->get_last_error() == NSAPI_ERROR_OK) {
179+
break;
180+
}
181+
// wait some time that modem gets ready for CFUN command, and try again
182+
retry++;
183+
ThisThread::sleep_for(64); // experimental value
184+
} while (retry < 3);
185+
185186
return _at->unlock_return_error();
186187
}
187188

0 commit comments

Comments
 (0)