Skip to content

Commit 3db6e4f

Browse files
author
Hasnain Virk
committed
Basic driver for Quectel EC25/EC21
Provides basic power up / power down sequences for Quectel EC2X series modems. Can be used in PPP mode. For using the on-board IP stack, we will need to add and implement classes that provide context. Driver constructor takes power and reset control pins along with the FileHandle. A default construction is provided which can be chosen by the application in its mbed_app.json. Otherwise the user is free to construct as per demand.
1 parent 2a694cf commit 3db6e4f

File tree

5 files changed

+408
-0
lines changed

5 files changed

+408
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "QUECTEL_EC2X.h"
19+
20+
#include "PinNames.h"
21+
#include "AT_CellularNetwork.h"
22+
#include "rtos/ThisThread.h"
23+
24+
using namespace mbed;
25+
using namespace rtos;
26+
using namespace events;
27+
28+
static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
29+
AT_CellularNetwork::RegistrationModeLAC, // C_EREG
30+
AT_CellularNetwork::RegistrationModeLAC, // C_GREG
31+
AT_CellularNetwork::RegistrationModeEnable, // C_REG
32+
1, // AT_CGSN_WITH_TYPE
33+
1, // AT_CGDATA
34+
0, // AT_CGAUTH
35+
1, // AT_CNMI
36+
1, // AT_CSMP
37+
1, // AT_CMGF
38+
1, // AT_CSDH
39+
1, // PROPERTY_IPV4_STACK
40+
1, // PROPERTY_IPV6_STACK
41+
1, // PROPERTY_IPV4V6_STACK
42+
0, // PROPERTY_NON_IP_PDP_TYPE
43+
1, // PROPERTY_AT_CGEREP
44+
};
45+
46+
QUECTEL_EC2X::QUECTEL_EC2X(FileHandle *fh, PinName pwr, PinName rst)
47+
: AT_CellularDevice(fh),
48+
_pwr_key(pwr, 0),
49+
_rst(rst, 0)
50+
51+
{
52+
AT_CellularBase::set_cellular_properties(cellular_properties);
53+
}
54+
55+
#if MBED_CONF_QUECTEL_EC2X_PROVIDE_DEFAULT
56+
#include "UARTSerial.h"
57+
CellularDevice *CellularDevice::get_default_instance()
58+
{
59+
static UARTSerial serial(MBED_CONF_QUECTEL_EC2X_TX,
60+
MBED_CONF_QUECTEL_EC2X_RX,
61+
MBED_CONF_QUECTEL_EC2X_BAUDRATE);
62+
#if defined(MBED_CONF_QUECTEL_EC2X_RTS) && defined(MBED_CONF_QUECTEL_EC2X_CTS)
63+
tr_debug("QUECTEL_EC2X_PPP flow control: RTS %d CTS %d", MBED_CONF_QUECTEL_EC2X_RTS, MBED_CONF_QUECTEL_EC2X_CTS);
64+
serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_QUECTEL_EC2X_RTS, MBED_CONF_QUECTEL_EC2X_CTS);
65+
#endif
66+
static QUECTEL_EC2X device(&serial, MBED_CONF_QUECTEL_EC2X_PWR, MBED_CONF_QUECTEL_EC2X_RST);
67+
return &device;
68+
}
69+
70+
nsapi_error_t QUECTEL_EC2X::hard_power_on()
71+
{
72+
if (_pwr_key.is_connected()) {
73+
_pwr_key = 1;
74+
ThisThread::sleep_for(600);
75+
_pwr_key = 0;
76+
ThisThread::sleep_for(100);
77+
}
78+
79+
return NSAPI_ERROR_OK;
80+
}
81+
82+
nsapi_error_t QUECTEL_EC2X::hard_power_off()
83+
84+
{
85+
if (_pwr_key.is_connected()) {
86+
_pwr_key = 1;
87+
ThisThread::sleep_for(750);
88+
_pwr_key = 0;
89+
ThisThread::sleep_for(100);
90+
}
91+
92+
return NSAPI_ERROR_OK;
93+
}
94+
95+
nsapi_error_t QUECTEL_EC2X::soft_power_on()
96+
{
97+
if (_rst.is_connected()) {
98+
_rst = 1;
99+
ThisThread::sleep_for(460);
100+
_rst = 0;
101+
ThisThread::sleep_for(100);
102+
}
103+
104+
_at->lock();
105+
106+
_at->set_at_timeout(5000);
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+
112+
_at->unlock();
113+
114+
if (!rdy) {
115+
return NSAPI_ERROR_DEVICE_ERROR;
116+
}
117+
118+
return NSAPI_ERROR_OK;
119+
}
120+
121+
nsapi_error_t QUECTEL_EC2X::soft_power_off()
122+
{
123+
if (_pwr_key.is_connected()) {
124+
_pwr_key = 1;
125+
ThisThread::sleep_for(750);
126+
_pwr_key = 0;
127+
ThisThread::sleep_for(100);
128+
}
129+
130+
return NSAPI_ERROR_OK;
131+
}
132+
133+
#endif
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef QUECTEL_EC2X_H
19+
#define QUECTEL_EC2X_H
20+
21+
#ifdef TARGET_FF_ARDUINO
22+
#ifndef MBED_CONF_QUECTEL_EC2X_TX
23+
#define MBED_CONF_QUECTEL_EC2X_TX D1
24+
#endif
25+
#ifndef MBED_CONF_QUECTEL_EC2X_RX
26+
#define MBED_CONF_QUECTEL_EC2X_RX D0
27+
#endif
28+
#endif /* TARGET_FF_ARDUINO */
29+
30+
#include "DigitalOut.h"
31+
#include "AT_CellularDevice.h"
32+
33+
namespace mbed {
34+
35+
class QUECTEL_EC2X : public AT_CellularDevice {
36+
public:
37+
QUECTEL_EC2X(FileHandle *fh, PinName pwr = NC, PinName rst = NC);
38+
virtual nsapi_error_t hard_power_on();
39+
virtual nsapi_error_t hard_power_off();
40+
virtual nsapi_error_t soft_power_on();
41+
virtual nsapi_error_t soft_power_off();
42+
43+
private:
44+
DigitalOut _pwr_key;
45+
DigitalOut _rst;
46+
};
47+
48+
} // namespace mbed
49+
50+
#endif // QUECTEL_EC2X_H
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "QUECTEL_EC2X_PPP.h"
19+
20+
#include "PinNames.h"
21+
#include "AT_CellularNetwork.h"
22+
#include "rtos/ThisThread.h"
23+
24+
using namespace mbed;
25+
using namespace rtos;
26+
using namespace events;
27+
28+
static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
29+
AT_CellularNetwork::RegistrationModeLAC, // C_EREG
30+
AT_CellularNetwork::RegistrationModeLAC, // C_GREG
31+
AT_CellularNetwork::RegistrationModeEnable, // C_REG
32+
1, // AT_CGSN_WITH_TYPE
33+
1, // AT_CGDATA
34+
0, // AT_CGAUTH
35+
1, // AT_CNMI
36+
1, // AT_CSMP
37+
1, // AT_CMGF
38+
1, // AT_CSDH
39+
1, // PROPERTY_IPV4_STACK
40+
1, // PROPERTY_IPV6_STACK
41+
1, // PROPERTY_IPV4V6_STACK
42+
0, // PROPERTY_NON_IP_PDP_TYPE
43+
1, // PROPERTY_AT_CGEREP
44+
};
45+
46+
QUECTEL_EC2X_PPP::QUECTEL_EC2X_PPP(FileHandle *fh, PinName pwr, PinName rst)
47+
: AT_CellularDevice(fh),
48+
_pwr_key(pwr, 0),
49+
_rst(rst, 0)
50+
51+
{
52+
AT_CellularBase::set_cellular_properties(cellular_properties);
53+
}
54+
55+
#if MBED_CONF_QUECTEL_EC2X_PPP_PROVIDE_DEFAULT
56+
57+
#if !NSAPI_PPP_AVAILABLE
58+
#error Must define lwip.ppp-enabled
59+
#endif
60+
61+
#include "UARTSerial.h"
62+
CellularDevice *CellularDevice::get_default_instance()
63+
{
64+
static UARTSerial serial(MBED_CONF_QUECTEL_EC2X_PPP_TX,
65+
MBED_CONF_QUECTEL_EC2X_PPP_RX,
66+
MBED_CONF_QUECTEL_EC2X_PPP_BAUDRATE);
67+
#if defined(MBED_CONF_QUECTEL_EC2X_PPP_RTS) && defined(MBED_CONF_QUECTEL_EC2X_PPP_CTS)
68+
tr_debug("QUECTEL_EC2X_PPP flow control: RTS %d CTS %d", MBED_CONF_QUECTEL_EC2X_PPP_RTS, MBED_CONF_QUECTEL_EC2X_PPP_CTS);
69+
serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_QUECTEL_EC2X_PPP_RTS, MBED_CONF_QUECTEL_EC2X_PPP_CTS);
70+
#endif
71+
static QUECTEL_EC2X_PPP device(&serial, MBED_CONF_QUECTEL_EC2X_PPP_PWR, MBED_CONF_QUECTEL_EC2X_PPP_RST);
72+
return &device;
73+
}
74+
75+
nsapi_error_t QUECTEL_EC2X_PPP::hard_power_on()
76+
{
77+
if (_pwr_key.is_connected()) {
78+
_pwr_key = 1;
79+
ThisThread::sleep_for(600);
80+
_pwr_key = 0;
81+
ThisThread::sleep_for(100);
82+
}
83+
84+
return NSAPI_ERROR_OK;
85+
}
86+
87+
nsapi_error_t QUECTEL_EC2X_PPP::hard_power_off()
88+
89+
{
90+
if (_pwr_key.is_connected()) {
91+
_pwr_key = 1;
92+
ThisThread::sleep_for(750);
93+
_pwr_key = 0;
94+
ThisThread::sleep_for(100);
95+
}
96+
97+
return NSAPI_ERROR_OK;
98+
}
99+
100+
nsapi_error_t QUECTEL_EC2X_PPP::soft_power_on()
101+
{
102+
if (_rst.is_connected()) {
103+
_rst = 1;
104+
ThisThread::sleep_for(460);
105+
_rst = 0;
106+
ThisThread::sleep_for(100);
107+
}
108+
109+
_at->lock();
110+
111+
_at->set_at_timeout(5000);
112+
_at->resp_start();
113+
_at->set_stop_tag("RDY");
114+
bool rdy = _at->consume_to_stop_tag();
115+
_at->set_stop_tag(OK);
116+
117+
_at->unlock();
118+
119+
if (!rdy) {
120+
return NSAPI_ERROR_DEVICE_ERROR;
121+
}
122+
123+
return NSAPI_ERROR_OK;
124+
}
125+
126+
nsapi_error_t QUECTEL_EC2X_PPP::soft_power_off()
127+
{
128+
if (_pwr_key.is_connected()) {
129+
_pwr_key = 1;
130+
ThisThread::sleep_for(750);
131+
_pwr_key = 0;
132+
ThisThread::sleep_for(100);
133+
}
134+
135+
return NSAPI_ERROR_OK;
136+
}
137+
138+
#endif
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef QUECTEL_EC2X_PPP_H
19+
#define QUECTEL_EC2X_PPP_H
20+
21+
#ifdef TARGET_FF_ARDUINO
22+
#ifndef MBED_CONF_QUECTEL_EC2X_PPP_TX
23+
#define MBED_CONF_QUECTEL_EC2X_PPP_TX D1
24+
#endif
25+
#ifndef MBED_CONF_QUECTEL_EC2X_PPP_RX
26+
#define MBED_CONF_QUECTEL_EC2X_PPP_RX D0
27+
#endif
28+
#endif /* TARGET_FF_ARDUINO */
29+
30+
#include "DigitalOut.h"
31+
#include "AT_CellularDevice.h"
32+
33+
namespace mbed {
34+
35+
class QUECTEL_EC2X_PPP : public AT_CellularDevice {
36+
public:
37+
QUECTEL_EC2X_PPP(FileHandle *fh, PinName pwr = NC, PinName rst = NC);
38+
virtual nsapi_error_t hard_power_on();
39+
virtual nsapi_error_t hard_power_off();
40+
virtual nsapi_error_t soft_power_on();
41+
virtual nsapi_error_t soft_power_off();
42+
43+
private:
44+
DigitalOut _pwr_key;
45+
DigitalOut _rst;
46+
};
47+
48+
} // namespace mbed
49+
50+
#endif // QUECTEL_EC2X_PPP_H

0 commit comments

Comments
 (0)