|
| 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 | +#include "UARTSerial.h" |
| 24 | + |
| 25 | +using namespace mbed; |
| 26 | +using namespace rtos; |
| 27 | +using namespace events; |
| 28 | + |
| 29 | +#if !defined(MBED_CONF_QUECTEL_EC2X_PWR) |
| 30 | +#define MBED_CONF_QUECTEL_EC2X_PWR NC |
| 31 | +#endif |
| 32 | + |
| 33 | +#if !defined(MBED_CONF_QUECTEL_EC2X_RST) |
| 34 | +#define MBED_CONF_QUECTEL_EC2X_RST NC |
| 35 | +#endif |
| 36 | + |
| 37 | +#if !defined(MBED_CONF_QUECTEL_EC2X_TX) |
| 38 | +#define MBED_CONF_QUECTEL_EC2X_TX NC |
| 39 | +#endif |
| 40 | + |
| 41 | +#if !defined(MBED_CONF_QUECTEL_EC2X_RX) |
| 42 | +#define MBED_CONF_QUECTEL_EC2X_RX NC |
| 43 | +#endif |
| 44 | + |
| 45 | +#if !defined(MBED_CONF_QUECTEL_EC2X_POLARITY) |
| 46 | +#define MBED_CONF_QUECTEL_EC2X_POLARITY 1 // active high |
| 47 | +#endif |
| 48 | + |
| 49 | +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { |
| 50 | + AT_CellularNetwork::RegistrationModeLAC, // C_EREG |
| 51 | + AT_CellularNetwork::RegistrationModeLAC, // C_GREG |
| 52 | + AT_CellularNetwork::RegistrationModeLAC, // C_REG |
| 53 | + 0, // AT_CGSN_WITH_TYPE |
| 54 | + 1, // AT_CGDATA |
| 55 | + 0, // AT_CGAUTH |
| 56 | + 1, // AT_CNMI |
| 57 | + 1, // AT_CSMP |
| 58 | + 1, // AT_CMGF |
| 59 | + 1, // AT_CSDH |
| 60 | + 1, // PROPERTY_IPV4_STACK |
| 61 | + 1, // PROPERTY_IPV6_STACK |
| 62 | + 1, // PROPERTY_IPV4V6_STACK |
| 63 | + 0, // PROPERTY_NON_IP_PDP_TYPE |
| 64 | + 1, // PROPERTY_AT_CGEREP |
| 65 | +}; |
| 66 | + |
| 67 | +QUECTEL_EC2X::QUECTEL_EC2X(FileHandle *fh, PinName pwr, bool active_high, PinName rst) |
| 68 | + : AT_CellularDevice(fh), |
| 69 | + _active_high(active_high), |
| 70 | + _pwr_key(pwr, !_active_high), |
| 71 | + _rst(rst, !_active_high) |
| 72 | +{ |
| 73 | + AT_CellularBase::set_cellular_properties(cellular_properties); |
| 74 | +} |
| 75 | + |
| 76 | +CellularDevice *CellularDevice::get_default_instance() |
| 77 | +{ |
| 78 | + static UARTSerial serial(MBED_CONF_QUECTEL_EC2X_TX, |
| 79 | + MBED_CONF_QUECTEL_EC2X_RX, |
| 80 | + MBED_CONF_QUECTEL_EC2X_BAUDRATE); |
| 81 | +#if defined(MBED_CONF_QUECTEL_EC2X_RTS) && defined(MBED_CONF_QUECTEL_EC2X_CTS) |
| 82 | + serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_QUECTEL_EC2X_RTS, MBED_CONF_QUECTEL_EC2X_CTS); |
| 83 | +#endif |
| 84 | + static QUECTEL_EC2X device(&serial, |
| 85 | + MBED_CONF_QUECTEL_EC2X_PWR, |
| 86 | + MBED_CONF_QUECTEL_EC2X_POLARITY, |
| 87 | + MBED_CONF_QUECTEL_EC2X_RST); |
| 88 | + return &device; |
| 89 | +} |
| 90 | + |
| 91 | +nsapi_error_t QUECTEL_EC2X::press_power_button(uint32_t timeout) |
| 92 | +{ |
| 93 | + _pwr_key = _active_high; |
| 94 | + ThisThread::sleep_for(timeout); |
| 95 | + _pwr_key = !_active_high; |
| 96 | + ThisThread::sleep_for(100); |
| 97 | + |
| 98 | + return NSAPI_ERROR_OK; |
| 99 | +} |
| 100 | + |
| 101 | +nsapi_error_t QUECTEL_EC2X::hard_power_on() |
| 102 | +{ |
| 103 | + return press_power_button(600); |
| 104 | +} |
| 105 | + |
| 106 | +nsapi_error_t QUECTEL_EC2X::hard_power_off() |
| 107 | + |
| 108 | +{ |
| 109 | + return press_power_button(750); |
| 110 | +} |
| 111 | + |
| 112 | +nsapi_error_t QUECTEL_EC2X::soft_power_on() |
| 113 | +{ |
| 114 | + if (_rst.is_connected()) { |
| 115 | + _rst = _active_high; |
| 116 | + ThisThread::sleep_for(460); |
| 117 | + _rst = !_active_high; |
| 118 | + ThisThread::sleep_for(100); |
| 119 | + |
| 120 | + _at->lock(); |
| 121 | + |
| 122 | + _at->set_at_timeout(5000); |
| 123 | + _at->resp_start(); |
| 124 | + _at->set_stop_tag("RDY"); |
| 125 | + bool rdy = _at->consume_to_stop_tag(); |
| 126 | + _at->set_stop_tag(OK); |
| 127 | + |
| 128 | + _at->unlock(); |
| 129 | + |
| 130 | + if (!rdy) { |
| 131 | + return NSAPI_ERROR_DEVICE_ERROR; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return NSAPI_ERROR_OK; |
| 136 | +} |
| 137 | + |
| 138 | +nsapi_error_t QUECTEL_EC2X::soft_power_off() |
| 139 | +{ |
| 140 | + return hard_power_off(); |
| 141 | +} |
0 commit comments