15
15
* limitations under the License.
16
16
*/
17
17
18
+ #include " rtos/ThisThread.h"
18
19
#include " QUECTEL_BG96.h"
19
20
#include " QUECTEL_BG96_CellularNetwork.h"
20
21
#include " QUECTEL_BG96_CellularStack.h"
24
25
25
26
using namespace mbed ;
26
27
using namespace events ;
28
+ using namespace rtos ;
27
29
28
30
#define CONNECT_DELIM " \r\n "
29
31
#define CONNECT_BUFFER_SIZE (1280 + 80 + 80 ) // AT response + sscanf format
30
32
#define CONNECT_TIMEOUT 8000
31
33
32
34
#define DEVICE_READY_URC " CPIN:"
33
35
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
+
34
48
static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
35
49
AT_CellularNetwork::RegistrationModeLAC, // C_EREG
36
50
AT_CellularNetwork::RegistrationModeLAC, // C_GREG
@@ -49,11 +63,16 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
49
63
1 , // PROPERTY_AT_CGEREP
50
64
};
51
65
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)
53
71
{
54
72
AT_CellularBase::set_cellular_properties (cellular_properties);
55
73
}
56
74
75
+
57
76
AT_CellularNetwork *QUECTEL_BG96::open_network_impl (ATHandler &at)
58
77
{
59
78
return new QUECTEL_BG96_CellularNetwork (at);
@@ -74,6 +93,96 @@ void QUECTEL_BG96::set_ready_cb(Callback<void()> callback)
74
93
_at->set_urc_handler (DEVICE_READY_URC, callback);
75
94
}
76
95
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
+
77
186
#if MBED_CONF_QUECTEL_BG96_PROVIDE_DEFAULT
78
187
#include " UARTSerial.h"
79
188
CellularDevice *CellularDevice::get_default_instance ()
@@ -83,7 +192,10 @@ CellularDevice *CellularDevice::get_default_instance()
83
192
tr_debug (" QUECTEL_BG96 flow control: RTS %d CTS %d" , MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS);
84
193
serial.set_flow_control (SerialBase::RTSCTS, MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS);
85
194
#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);
87
199
return &device;
88
200
}
89
201
#endif
0 commit comments