Skip to content

Commit e49d7bb

Browse files
author
Ari Parkkila
committed
Cellular: Add BG96 power control
1 parent 93cb530 commit e49d7bb

File tree

3 files changed

+84
-52
lines changed

3 files changed

+84
-52
lines changed

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

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,36 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include "rtos/ThisThread.h"
1819
#include "QUECTEL_BG96.h"
1920
#include "QUECTEL_BG96_CellularNetwork.h"
2021
#include "QUECTEL_BG96_CellularStack.h"
2122
#include "QUECTEL_BG96_CellularInformation.h"
2223
#include "QUECTEL_BG96_CellularContext.h"
2324
#include "CellularLog.h"
24-
#include "DigitalOut.h"
25-
#include "mbed_wait_api.h"
2625

2726
using namespace mbed;
2827
using namespace events;
28+
using namespace rtos;
2929

3030
#define CONNECT_DELIM "\r\n"
3131
#define CONNECT_BUFFER_SIZE (1280 + 80 + 80) // AT response + sscanf format
3232
#define CONNECT_TIMEOUT 8000
3333

3434
#define DEVICE_READY_URC "CPIN:"
3535

36+
#if !defined(MBED_CONF_QUECTEL_BG96_PWR)
37+
#define MBED_CONF_QUECTEL_BG96_PWR NC
38+
#endif
39+
40+
#if !defined(MBED_CONF_QUECTEL_BG96_RST)
41+
#define MBED_CONF_QUECTEL_BG96_RST NC
42+
#endif
43+
44+
#if !defined(MBED_CONF_QUECTEL_BG96_POLARITY)
45+
#define MBED_CONF_QUECTEL_BG96_POLARITY 1 // active high
46+
#endif
47+
3648
static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
3749
AT_CellularNetwork::RegistrationModeLAC, // C_EREG
3850
AT_CellularNetwork::RegistrationModeLAC, // C_GREG
@@ -51,11 +63,16 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
5163
1, // PROPERTY_AT_CGEREP
5264
};
5365

54-
QUECTEL_BG96::QUECTEL_BG96(FileHandle *fh) : AT_CellularDevice(fh)
66+
QUECTEL_BG96::QUECTEL_BG96(FileHandle *fh, PinName pwr, bool active_high, PinName rst)
67+
: AT_CellularDevice(fh),
68+
_active_high(active_high),
69+
_pwr(pwr, !_active_high),
70+
_rst(rst, !_active_high)
5571
{
5672
AT_CellularBase::set_cellular_properties(cellular_properties);
5773
}
5874

75+
5976
AT_CellularNetwork *QUECTEL_BG96::open_network_impl(ATHandler &at)
6077
{
6178
return new QUECTEL_BG96_CellularNetwork(at);
@@ -78,54 +95,55 @@ void QUECTEL_BG96::set_ready_cb(Callback<void()> callback)
7895

7996
nsapi_error_t QUECTEL_BG96::hard_power_on()
8097
{
81-
DigitalOut ModemResetIn (MBED_CONF_QUECTEL_BG96_PWR);
82-
83-
wait_ms(250);
84-
ModemResetIn = 0;
85-
wait_ms(250);
86-
ModemResetIn = 1;
87-
wait_ms(500);
98+
if (_pwr.is_connected()) {
99+
tr_info("Modem power on");
100+
ThisThread::sleep_for(250);
101+
_pwr = !_active_high;
102+
ThisThread::sleep_for(250); // BG96_Hardware_Design_V1.1 says 100 ms, but 250 ms seems to be more robust
103+
_pwr = _active_high;
104+
ThisThread::sleep_for(500);
105+
}
88106

89107
return NSAPI_ERROR_OK;
90108
}
91109

92110
nsapi_error_t QUECTEL_BG96::soft_power_on()
93111
{
94-
// turn moden on
95-
DigitalOut ModemPwrkey (MBED_CONF_QUECTEL_BG96_RST);
96-
97-
ModemPwrkey = 0;
98-
wait_ms(100);
99-
ModemPwrkey = 1;
100-
wait_ms(500);
101-
ModemPwrkey = 0;
102-
wait_ms(500);
103-
104-
// wait for RDY
105-
_at->lock();
106-
_at->set_at_timeout(60000);
107-
_at->resp_start();
108-
_at->set_stop_tag("RDY");
109-
bool rdy = _at->consume_to_stop_tag();
110-
_at->set_stop_tag(OK);
111-
_at->unlock();
112-
113-
if (!rdy)
114-
{
115-
return NSAPI_ERROR_DEVICE_ERROR;
112+
if (_rst.is_connected()) {
113+
tr_info("Reset modem");
114+
_rst = !_active_high;
115+
ThisThread::sleep_for(100);
116+
_rst = _active_high;
117+
ThisThread::sleep_for(150 + 460); // RESET_N timeout from BG96_Hardware_Design_V1.1
118+
_rst = !_active_high;
119+
ThisThread::sleep_for(500);
120+
121+
// wait for RDY
122+
_at->lock();
123+
_at->set_at_timeout(10 * 1000);
124+
_at->resp_start();
125+
_at->set_stop_tag("RDY");
126+
bool rdy = _at->consume_to_stop_tag();
127+
_at->set_stop_tag(OK);
128+
_at->restore_at_timeout();
129+
_at->unlock();
130+
131+
if (!rdy) {
132+
return NSAPI_ERROR_DEVICE_ERROR;
133+
}
116134
}
117135

118136
return NSAPI_ERROR_OK;
119137
}
120138

121139
nsapi_error_t QUECTEL_BG96::hard_power_off()
122140
{
123-
// turn moden off
124-
DigitalOut ModemPwrkey (MBED_CONF_QUECTEL_BG96_PWR);
125-
126-
ModemPwrkey = 1;
127-
wait_ms(250);
128-
ModemPwrkey = 0;
141+
if (_pwr.is_connected()) {
142+
tr_info("Modem power off");
143+
_pwr = _active_high;
144+
ThisThread::sleep_for(650); // from BG96_Hardware_Design_V1.1
145+
_pwr = !_active_high;
146+
}
129147

130148
return NSAPI_ERROR_OK;
131149
}
@@ -142,30 +160,26 @@ nsapi_error_t QUECTEL_BG96::init()
142160
_at->cmd_start("AT+CMEE=1"); // verbose responses
143161
_at->cmd_stop_read_resp();
144162

145-
if (_at->get_last_error() != NSAPI_ERROR_OK)
146-
{
147-
do
148-
{
163+
if (_at->get_last_error() == NSAPI_ERROR_OK) {
164+
do {
149165
_at->cmd_start("AT+CFUN=1"); // set full functionality
150166
_at->cmd_stop_read_resp();
151167

152-
// CFUN executed ok
153-
if (_at->get_last_error() != NSAPI_ERROR_OK)
154-
{
168+
// CFUN executed ok
169+
if (_at->get_last_error() != NSAPI_ERROR_OK) {
155170
// wait some time that modem gets ready for CFUN command, and try again
156171
retry++;
157172
_at->flush();
158-
wait_ms(64);
159-
}
160-
else
161-
{
173+
ThisThread::sleep_for(64); // experimental value
174+
} else {
162175
// yes continue
163176
break;
164177
}
165178

166179
/* code */
167-
} while ( (retry < 3) );
180+
} while ((retry < 3));
168181
}
182+
169183
return _at->unlock_return_error();
170184
}
171185

@@ -178,7 +192,10 @@ CellularDevice *CellularDevice::get_default_instance()
178192
tr_debug("QUECTEL_BG96 flow control: RTS %d CTS %d", MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS);
179193
serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS);
180194
#endif
181-
static QUECTEL_BG96 device(&serial);
195+
static QUECTEL_BG96 device(&serial,
196+
MBED_CONF_QUECTEL_BG96_PWR,
197+
MBED_CONF_QUECTEL_BG96_POLARITY,
198+
MBED_CONF_QUECTEL_BG96_RST);
182199
return &device;
183200
}
184201
#endif

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
#endif
2828
#endif /* TARGET_FF_ARDUINO */
2929

30+
#include "DigitalOut.h"
3031
#include "AT_CellularDevice.h"
3132

3233
namespace mbed {
3334

3435
class QUECTEL_BG96 : public AT_CellularDevice {
3536
public:
36-
QUECTEL_BG96(FileHandle *fh);
37+
QUECTEL_BG96(FileHandle *fh, PinName pwr = NC, bool active_high = true, PinName rst = NC);
3738

3839
protected: // AT_CellularDevice
3940
virtual AT_CellularNetwork *open_network_impl(ATHandler &at);
@@ -47,6 +48,12 @@ class QUECTEL_BG96 : public AT_CellularDevice {
4748

4849
public:
4950
void handle_urc(FileHandle *fh);
51+
52+
private:
53+
nsapi_error_t press_power_button(uint32_t timeout);
54+
bool _active_high;
55+
DigitalOut _pwr;
56+
DigitalOut _rst;
5057
};
5158
} // namespace mbed
5259
#endif // QUECTEL_BG96_H_

features/cellular/framework/targets/QUECTEL/BG96/mbed_lib.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,13 @@
3737
"help": "Provide as default CellularDevice [true/false]",
3838
"value": false
3939
}
40+
},
41+
"target_overrides": {
42+
"MTB_STM_L475": {
43+
"tx": "SERIAL_TX",
44+
"rx": "SERIAL_RX",
45+
"pwr": "PE_15",
46+
"rst": "PE_9"
47+
}
4048
}
4149
}

0 commit comments

Comments
 (0)