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"
21
22
#include " QUECTEL_BG96_CellularInformation.h"
22
23
#include " QUECTEL_BG96_CellularContext.h"
23
24
#include " CellularLog.h"
24
- #include " DigitalOut.h"
25
- #include " mbed_wait_api.h"
26
25
27
26
using namespace mbed ;
28
27
using namespace events ;
28
+ using namespace rtos ;
29
29
30
30
#define CONNECT_DELIM " \r\n "
31
31
#define CONNECT_BUFFER_SIZE (1280 + 80 + 80 ) // AT response + sscanf format
32
32
#define CONNECT_TIMEOUT 8000
33
33
34
34
#define DEVICE_READY_URC " CPIN:"
35
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
+
36
48
static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
37
49
AT_CellularNetwork::RegistrationModeLAC, // C_EREG
38
50
AT_CellularNetwork::RegistrationModeLAC, // C_GREG
@@ -51,11 +63,16 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
51
63
1 , // PROPERTY_AT_CGEREP
52
64
};
53
65
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)
55
71
{
56
72
AT_CellularBase::set_cellular_properties (cellular_properties);
57
73
}
58
74
75
+
59
76
AT_CellularNetwork *QUECTEL_BG96::open_network_impl (ATHandler &at)
60
77
{
61
78
return new QUECTEL_BG96_CellularNetwork (at);
@@ -78,54 +95,55 @@ void QUECTEL_BG96::set_ready_cb(Callback<void()> callback)
78
95
79
96
nsapi_error_t QUECTEL_BG96::hard_power_on ()
80
97
{
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
+ }
88
106
89
107
return NSAPI_ERROR_OK;
90
108
}
91
109
92
110
nsapi_error_t QUECTEL_BG96::soft_power_on ()
93
111
{
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
+ }
116
134
}
117
135
118
136
return NSAPI_ERROR_OK;
119
137
}
120
138
121
139
nsapi_error_t QUECTEL_BG96::hard_power_off ()
122
140
{
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
+ }
129
147
130
148
return NSAPI_ERROR_OK;
131
149
}
@@ -142,30 +160,26 @@ nsapi_error_t QUECTEL_BG96::init()
142
160
_at->cmd_start (" AT+CMEE=1" ); // verbose responses
143
161
_at->cmd_stop_read_resp ();
144
162
145
- if (_at->get_last_error () != NSAPI_ERROR_OK)
146
- {
147
- do
148
- {
163
+ if (_at->get_last_error () == NSAPI_ERROR_OK) {
164
+ do {
149
165
_at->cmd_start (" AT+CFUN=1" ); // set full functionality
150
166
_at->cmd_stop_read_resp ();
151
167
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) {
155
170
// wait some time that modem gets ready for CFUN command, and try again
156
171
retry++;
157
172
_at->flush ();
158
- wait_ms (64 );
159
- }
160
- else
161
- {
173
+ ThisThread::sleep_for (64 ); // experimental value
174
+ } else {
162
175
// yes continue
163
176
break ;
164
177
}
165
178
166
179
/* code */
167
- } while ( (retry < 3 ) );
180
+ } while ((retry < 3 ));
168
181
}
182
+
169
183
return _at->unlock_return_error ();
170
184
}
171
185
@@ -178,7 +192,10 @@ CellularDevice *CellularDevice::get_default_instance()
178
192
tr_debug (" QUECTEL_BG96 flow control: RTS %d CTS %d" , MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS);
179
193
serial.set_flow_control (SerialBase::RTSCTS, MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS);
180
194
#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);
182
199
return &device;
183
200
}
184
201
#endif
0 commit comments