Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers STModCellular.cpp Source File

STModCellular.cpp

00001 /*
00002  * Copyright (c) 2018, 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 "STModCellular.h"
00019 #include "rtos/ThisThread.h"
00020 #include "mbed_trace.h"
00021 
00022 #define TRACE_GROUP "STMOD"
00023 
00024 using namespace mbed;
00025 
00026 STModCellular::STModCellular(FileHandle *fh) : STMOD_CELLULAR_MODEM(fh),
00027     m_powerkey(MBED_CONF_STMOD_CELLULAR_POWER),
00028     m_reset(MBED_CONF_STMOD_CELLULAR_RESET),
00029     m_simsel0(MBED_CONF_STMOD_CELLULAR_SIMSEL0),
00030     m_simsel1(MBED_CONF_STMOD_CELLULAR_SIMSEL1),
00031     m_mdmdtr(MBED_CONF_STMOD_CELLULAR_MDMDTR),
00032     m_sim_reset(MBED_CONF_STMOD_CELLULAR_SIM_RESET),
00033     m_sim_clk(MBED_CONF_STMOD_CELLULAR_SIM_CLK),
00034     m_sim_data(MBED_CONF_STMOD_CELLULAR_SIM_DATA)
00035 {
00036     tr_info("STModCellular creation");
00037 
00038     // start with modem disabled
00039     m_powerkey.write(0);
00040     m_reset.write(1);
00041     rtos::ThisThread::sleep_for(200);
00042     m_reset.write(0);
00043     rtos::ThisThread::sleep_for(150);
00044 
00045     rtos::ThisThread::sleep_for(50);
00046     m_simsel0.write(MBED_CONF_STMOD_CELLULAR_SIM_SELECTION & 0x01);
00047     m_simsel1.write(MBED_CONF_STMOD_CELLULAR_SIM_SELECTION & 0x02);
00048     rtos::ThisThread::sleep_for(50);
00049 }
00050 
00051 STModCellular::~STModCellular()
00052 {
00053 }
00054 
00055 nsapi_error_t STModCellular::soft_power_on()
00056 {
00057     tr_debug("STMOD cellular modem power ON");
00058 
00059 #if (MBED_CONF_STMOD_CELLULAR_TYPE == STMOD_UG96)
00060     tr_info("Booting UG96");
00061     m_reset.write(1);
00062     rtos::ThisThread::sleep_for(200);
00063     m_reset.write(0);
00064     rtos::ThisThread::sleep_for(150);
00065     m_powerkey.write(1);
00066     rtos::ThisThread::sleep_for(150);
00067     m_powerkey.write(0);
00068     /* Because modem status is not available on STMOD+ connector,
00069      * let's wait for Modem complete boot */
00070     rtos::ThisThread::sleep_for(2300);
00071 #elif (MBED_CONF_STMOD_CELLULAR_TYPE == STMOD_BG96)
00072     tr_info("Booting BG96");
00073     m_powerkey.write(1);
00074     m_reset.write(1);
00075     rtos::ThisThread::sleep_for(150);
00076     m_powerkey.write(0);
00077     m_reset.write(0);
00078     rtos::ThisThread::sleep_for(100);
00079     m_powerkey.write(1);
00080     rtos::ThisThread::sleep_for(200);
00081     m_powerkey.write(0);
00082     rtos::ThisThread::sleep_for(5000);
00083 #endif
00084 
00085     nsapi_error_t err = STMOD_CELLULAR_MODEM::soft_power_on();
00086     if (err != 0) {
00087         return err;
00088     }
00089 
00090     // wait for RDY
00091     _at->lock();
00092     _at->set_at_timeout(5000);
00093     _at->set_stop_tag("RDY");
00094     bool rdy = _at->consume_to_stop_tag();
00095     (void)rdy;
00096 
00097     /*  Modem may send more bytes are RDY flag */
00098     _at->flush();
00099 
00100     /* Turn OFF ECHO before anything else */
00101     _at->set_stop_tag(mbed::OK);
00102     _at->cmd_start("ATE0");
00103     _at->cmd_stop();
00104     _at->consume_to_stop_tag();
00105 
00106     _at->restore_at_timeout();
00107     _at->unlock();
00108 
00109     tr_info("Modem %sready to receive AT commands", rdy ? "" : "NOT ");
00110 
00111     if ((MBED_CONF_STMOD_CELLULAR_CTS != NC) && (MBED_CONF_STMOD_CELLULAR_RTS != NC)) {
00112         tr_info("Enable flow control");
00113 
00114         pin_mode(MBED_CONF_STMOD_CELLULAR_CTS, PullDown);
00115 
00116         _at->lock();
00117         // enable CTS/RTS flowcontrol
00118         _at->set_stop_tag(mbed::OK);
00119         _at->set_at_timeout(400);
00120         _at->cmd_start("AT+IFC=");
00121         _at->write_int(2);
00122         _at->write_int(2);
00123         _at->cmd_stop_read_resp();
00124         err = _at->get_last_error();
00125         _at->restore_at_timeout();
00126         _at->unlock();
00127 
00128         if (err == NSAPI_ERROR_OK ) {
00129             tr_debug("Flow control turned ON");
00130         } else {
00131             tr_error("Failed to enable hw flow control");
00132         }
00133     }
00134 
00135     rtos::ThisThread::sleep_for(500);
00136 
00137 #if MBED_CONF_CELLULAR_DEBUG_AT
00138     _at->lock();
00139     /*  Verify Flow Control settings */
00140     _at->cmd_start("AT+IFC?");
00141     _at->cmd_stop_read_resp();
00142     _at->unlock();
00143 #endif // MBED_CONF_CELLULAR_DEBUG_AT
00144 
00145     return err;
00146 }
00147 
00148 nsapi_error_t STModCellular::soft_power_off()
00149 {
00150     _at->cmd_start("AT+QPOWD");
00151     _at->cmd_stop();
00152     rtos::ThisThread::sleep_for(1000);
00153     // should wait for POWERED DOWN with a time out up to 65 second according to the manual.
00154     // we cannot afford such a long wait though.
00155     return STMOD_CELLULAR_MODEM::soft_power_off();
00156 }
00157 
00158 #if MBED_CONF_STMOD_CELLULAR_PROVIDE_DEFAULT
00159 #include "UARTSerial.h"
00160 CellularDevice *CellularDevice::get_default_instance()
00161 {
00162     tr_debug("STMOD_CELLULAR default instance");
00163 
00164     static UARTSerial serial(MBED_CONF_STMOD_CELLULAR_TX, MBED_CONF_STMOD_CELLULAR_RX, MBED_CONF_STMOD_CELLULAR_BAUDRATE);
00165     if ((MBED_CONF_STMOD_CELLULAR_CTS != NC) && (MBED_CONF_STMOD_CELLULAR_RTS != NC)) {
00166         serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_STMOD_CELLULAR_RTS, MBED_CONF_STMOD_CELLULAR_CTS);
00167     }
00168     static STModCellular device(&serial);
00169     return &device;
00170 }
00171 #endif
00172