|
| 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 |
0 commit comments