Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers QUECTEL_EC2X.cpp Source File

QUECTEL_EC2X.cpp

00001 /*
00002  * Copyright (c) 2019, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "QUECTEL_EC2X.h"
00019 
00020 #include "PinNames.h"
00021 #include "AT_CellularNetwork.h"
00022 #include "rtos/ThisThread.h"
00023 #include "UARTSerial.h"
00024 
00025 using namespace mbed;
00026 using namespace rtos;
00027 using namespace events;
00028 
00029 #if !defined(MBED_CONF_QUECTEL_EC2X_PWR)
00030 #define MBED_CONF_QUECTEL_EC2X_PWR    NC
00031 #endif
00032 
00033 #if !defined(MBED_CONF_QUECTEL_EC2X_RST)
00034 #define MBED_CONF_QUECTEL_EC2X_RST    NC
00035 #endif
00036 
00037 #if !defined(MBED_CONF_QUECTEL_EC2X_TX)
00038 #define MBED_CONF_QUECTEL_EC2X_TX    NC
00039 #endif
00040 
00041 #if !defined(MBED_CONF_QUECTEL_EC2X_RX)
00042 #define MBED_CONF_QUECTEL_EC2X_RX    NC
00043 #endif
00044 
00045 #if !defined(MBED_CONF_QUECTEL_EC2X_POLARITY)
00046 #define MBED_CONF_QUECTEL_EC2X_POLARITY    1 // active high
00047 #endif
00048 
00049 static const intptr_t cellular_properties[AT_CellularDevice::PROPERTY_MAX] = {
00050     AT_CellularNetwork::RegistrationModeLAC,    // C_EREG
00051     AT_CellularNetwork::RegistrationModeLAC,    // C_GREG
00052     AT_CellularNetwork::RegistrationModeLAC,    // C_REG
00053     0,  // AT_CGSN_WITH_TYPE
00054     1,  // AT_CGDATA
00055     0,  // AT_CGAUTH
00056     1,  // AT_CNMI
00057     1,  // AT_CSMP
00058     1,  // AT_CMGF
00059     1,  // AT_CSDH
00060     1,  // PROPERTY_IPV4_STACK
00061     1,  // PROPERTY_IPV6_STACK
00062     1,  // PROPERTY_IPV4V6_STACK
00063     0,  // PROPERTY_NON_IP_PDP_TYPE
00064     1,  // PROPERTY_AT_CGEREP
00065 };
00066 
00067 QUECTEL_EC2X::QUECTEL_EC2X(FileHandle *fh, PinName pwr, bool active_high, PinName rst)
00068     : AT_CellularDevice(fh),
00069       _active_high(active_high),
00070       _pwr_key(pwr, !_active_high),
00071       _rst(rst, !_active_high)
00072 {
00073     set_cellular_properties(cellular_properties);
00074 }
00075 
00076 #if MBED_CONF_QUECTEL_EC2X_PROVIDE_DEFAULT
00077 CellularDevice *CellularDevice::get_default_instance()
00078 {
00079     static UARTSerial serial(MBED_CONF_QUECTEL_EC2X_TX,
00080                              MBED_CONF_QUECTEL_EC2X_RX,
00081                              MBED_CONF_QUECTEL_EC2X_BAUDRATE);
00082 #if defined(MBED_CONF_QUECTEL_EC2X_RTS) && defined(MBED_CONF_QUECTEL_EC2X_CTS)
00083     serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_QUECTEL_EC2X_RTS, MBED_CONF_QUECTEL_EC2X_CTS);
00084 #endif
00085     static QUECTEL_EC2X device(&serial,
00086                                MBED_CONF_QUECTEL_EC2X_PWR,
00087                                MBED_CONF_QUECTEL_EC2X_POLARITY,
00088                                MBED_CONF_QUECTEL_EC2X_RST);
00089     return &device;
00090 }
00091 #endif
00092 
00093 nsapi_error_t QUECTEL_EC2X::press_power_button(uint32_t timeout)
00094 {
00095     _pwr_key = _active_high;
00096     ThisThread::sleep_for(timeout);
00097     _pwr_key = !_active_high;
00098     ThisThread::sleep_for(100);
00099 
00100     return NSAPI_ERROR_OK ;
00101 }
00102 
00103 nsapi_error_t QUECTEL_EC2X::hard_power_on()
00104 {
00105     return press_power_button(600);
00106 }
00107 
00108 nsapi_error_t QUECTEL_EC2X::hard_power_off()
00109 
00110 {
00111     return press_power_button(750);
00112 }
00113 
00114 nsapi_error_t QUECTEL_EC2X::soft_power_on()
00115 {
00116     if (_rst.is_connected()) {
00117         _rst = _active_high;
00118         ThisThread::sleep_for(460);
00119         _rst = !_active_high;
00120         ThisThread::sleep_for(100);
00121 
00122         _at->lock();
00123 
00124         _at->set_at_timeout(5000);
00125         _at->resp_start();
00126         _at->set_stop_tag("RDY");
00127         bool rdy = _at->consume_to_stop_tag();
00128         _at->set_stop_tag(OK);
00129 
00130         _at->unlock();
00131 
00132         if (!rdy) {
00133             return NSAPI_ERROR_DEVICE_ERROR ;
00134         }
00135     }
00136 
00137     return NSAPI_ERROR_OK ;
00138 }
00139 
00140 nsapi_error_t QUECTEL_EC2X::soft_power_off()
00141 {
00142     return hard_power_off();
00143 }