Skip to content

Commit 9a3c175

Browse files
authored
Merge pull request #10588 from AriParkkila/cell-bg96-power
Cell bg96 power
2 parents 93e4a82 + e49d7bb commit 9a3c175

File tree

3 files changed

+146
-3
lines changed

3 files changed

+146
-3
lines changed

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

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
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"
@@ -24,13 +25,26 @@
2425

2526
using namespace mbed;
2627
using namespace events;
28+
using namespace rtos;
2729

2830
#define CONNECT_DELIM "\r\n"
2931
#define CONNECT_BUFFER_SIZE (1280 + 80 + 80) // AT response + sscanf format
3032
#define CONNECT_TIMEOUT 8000
3133

3234
#define DEVICE_READY_URC "CPIN:"
3335

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+
3448
static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
3549
AT_CellularNetwork::RegistrationModeLAC, // C_EREG
3650
AT_CellularNetwork::RegistrationModeLAC, // C_GREG
@@ -49,11 +63,16 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
4963
1, // PROPERTY_AT_CGEREP
5064
};
5165

52-
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)
5371
{
5472
AT_CellularBase::set_cellular_properties(cellular_properties);
5573
}
5674

75+
5776
AT_CellularNetwork *QUECTEL_BG96::open_network_impl(ATHandler &at)
5877
{
5978
return new QUECTEL_BG96_CellularNetwork(at);
@@ -74,6 +93,96 @@ void QUECTEL_BG96::set_ready_cb(Callback<void()> callback)
7493
_at->set_urc_handler(DEVICE_READY_URC, callback);
7594
}
7695

96+
nsapi_error_t QUECTEL_BG96::hard_power_on()
97+
{
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+
}
106+
107+
return NSAPI_ERROR_OK;
108+
}
109+
110+
nsapi_error_t QUECTEL_BG96::soft_power_on()
111+
{
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+
}
134+
}
135+
136+
return NSAPI_ERROR_OK;
137+
}
138+
139+
nsapi_error_t QUECTEL_BG96::hard_power_off()
140+
{
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+
}
147+
148+
return NSAPI_ERROR_OK;
149+
}
150+
151+
nsapi_error_t QUECTEL_BG96::init()
152+
{
153+
int retry = 0;
154+
155+
_at->lock();
156+
_at->flush();
157+
_at->cmd_start("ATE0"); // echo off
158+
_at->cmd_stop_read_resp();
159+
160+
_at->cmd_start("AT+CMEE=1"); // verbose responses
161+
_at->cmd_stop_read_resp();
162+
163+
if (_at->get_last_error() == NSAPI_ERROR_OK) {
164+
do {
165+
_at->cmd_start("AT+CFUN=1"); // set full functionality
166+
_at->cmd_stop_read_resp();
167+
168+
// CFUN executed ok
169+
if (_at->get_last_error() != NSAPI_ERROR_OK) {
170+
// wait some time that modem gets ready for CFUN command, and try again
171+
retry++;
172+
_at->flush();
173+
ThisThread::sleep_for(64); // experimental value
174+
} else {
175+
// yes continue
176+
break;
177+
}
178+
179+
/* code */
180+
} while ((retry < 3));
181+
}
182+
183+
return _at->unlock_return_error();
184+
}
185+
77186
#if MBED_CONF_QUECTEL_BG96_PROVIDE_DEFAULT
78187
#include "UARTSerial.h"
79188
CellularDevice *CellularDevice::get_default_instance()
@@ -83,7 +192,10 @@ CellularDevice *CellularDevice::get_default_instance()
83192
tr_debug("QUECTEL_BG96 flow control: RTS %d CTS %d", MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS);
84193
serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS);
85194
#endif
86-
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);
87199
return &device;
88200
}
89201
#endif

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,33 @@
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);
4041
virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false);
4142
virtual AT_CellularInformation *open_information_impl(ATHandler &at);
4243
virtual void set_ready_cb(Callback<void()> callback);
44+
virtual nsapi_error_t hard_power_on();
45+
virtual nsapi_error_t hard_power_off();
46+
virtual nsapi_error_t soft_power_on();
47+
virtual nsapi_error_t init();
4348

4449
public:
4550
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;
4657
};
4758
} // namespace mbed
4859
#endif // QUECTEL_BG96_H_

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
"help": "CTS pin for serial connection",
1818
"value": null
1919
},
20+
"pwr": {
21+
"help": "Power control pin",
22+
"value": null
23+
},
24+
"rst": {
25+
"help": "Reset control pin",
26+
"value": null
27+
},
28+
"polarity": {
29+
"help": "Pin polarity, 1 = Active high, 0 = Active low",
30+
"value": null
31+
},
2032
"baudrate" : {
2133
"help": "Serial connection baud rate",
2234
"value": 115200
@@ -25,5 +37,13 @@
2537
"help": "Provide as default CellularDevice [true/false]",
2638
"value": false
2739
}
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+
}
2848
}
2949
}

0 commit comments

Comments
 (0)