|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, 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 "STModCellular.h" |
| 19 | +#include "mbed_wait_api.h" |
| 20 | +#include "mbed_trace.h" |
| 21 | + |
| 22 | +#define TRACE_GROUP "CELL" |
| 23 | + |
| 24 | +using namespace mbed; |
| 25 | + |
| 26 | +STModCellular::STModCellular(FileHandle *fh) : STMOD_CELLULAR_MODEM(fh), |
| 27 | + m_powerkey(MBED_CONF_STMOD_CELLULAR_POWER), |
| 28 | + m_reset(MBED_CONF_STMOD_CELLULAR_RESET), |
| 29 | + m_simsel0(MBED_CONF_STMOD_CELLULAR_SIMSEL0), |
| 30 | + m_simsel1(MBED_CONF_STMOD_CELLULAR_SIMSEL1), |
| 31 | + m_mdmdtr(MBED_CONF_STMOD_CELLULAR_MDMDTR), |
| 32 | + m_sim_reset(MBED_CONF_STMOD_CELLULAR_SIM_RESET), |
| 33 | + m_sim_clk(MBED_CONF_STMOD_CELLULAR_SIM_CLK), |
| 34 | + m_sim_data(MBED_CONF_STMOD_CELLULAR_SIM_DATA) |
| 35 | +{ |
| 36 | + tr_debug("STModCellular creation\r\n"); |
| 37 | + |
| 38 | + // start with modem disabled |
| 39 | + m_powerkey.write(0); |
| 40 | + m_reset.write(1); |
| 41 | + wait_ms(200); |
| 42 | + m_reset.write(0); |
| 43 | + wait_ms(150); |
| 44 | + |
| 45 | + wait_ms(50); |
| 46 | + m_simsel0.write(MBED_CONF_STMOD_CELLULAR_SIM_SELECTION & 0x01); |
| 47 | + m_simsel1.write(MBED_CONF_STMOD_CELLULAR_SIM_SELECTION & 0x02); |
| 48 | + wait_ms(50); |
| 49 | +} |
| 50 | + |
| 51 | +STModCellular::~STModCellular() |
| 52 | +{ |
| 53 | +} |
| 54 | + |
| 55 | +nsapi_error_t STModCellular::soft_power_on() |
| 56 | +{ |
| 57 | + tr_debug("STMOD cellular modem power ON\r\n"); |
| 58 | + |
| 59 | +#if (MBED_CONF_STMOD_CELLULAR_TYPE == STMOD_UG96) |
| 60 | + tr_debug("Booting UG96\r\n"); |
| 61 | + m_reset.write(1); |
| 62 | + wait_ms(200); |
| 63 | + m_reset.write(0); |
| 64 | + wait_ms(150); |
| 65 | + m_powerkey.write(1); |
| 66 | + wait_ms(150); |
| 67 | + m_powerkey.write(0); |
| 68 | + /* Because modem status is not available on STMOD+ connector, |
| 69 | + * let's wait for Modem complete boot */ |
| 70 | + wait_ms(2300); |
| 71 | +#endif |
| 72 | +#if (MBED_CONF_STMOD_CELLULAR_TYPE == STMOD_BG96) |
| 73 | + tr_debug("Booting BG96\r\n"); |
| 74 | + m_powerkey.write(1); |
| 75 | + m_reset.write(1); |
| 76 | + wait_ms(150); |
| 77 | + m_powerkey.write(0); |
| 78 | + m_reset.write(0); |
| 79 | + wait_ms(100); |
| 80 | + m_powerkey.write(1); |
| 81 | + wait_ms(200); |
| 82 | + m_powerkey.write(0); |
| 83 | + wait_ms(5000); |
| 84 | +#endif |
| 85 | + |
| 86 | + nsapi_error_t err = STMOD_CELLULAR_MODEM::soft_power_on(); |
| 87 | + if (err != 0) { |
| 88 | + return err; |
| 89 | + } |
| 90 | + |
| 91 | + // wait for RDY |
| 92 | + _at->lock(); |
| 93 | + _at->set_at_timeout(5000); |
| 94 | + _at->set_stop_tag("RDY"); |
| 95 | + bool rdy = _at->consume_to_stop_tag(); |
| 96 | + (void)rdy; |
| 97 | + |
| 98 | + /* Modem may send more bytes are RDY flag */ |
| 99 | + _at->flush(); |
| 100 | + |
| 101 | + /* Turn OFF ECHO before anything else */ |
| 102 | + _at->set_stop_tag(mbed::OK); |
| 103 | + _at->cmd_start("ATE0"); |
| 104 | + _at->cmd_stop(); |
| 105 | + _at->consume_to_stop_tag(); |
| 106 | + |
| 107 | + _at->restore_at_timeout(); |
| 108 | + _at->unlock(); |
| 109 | + |
| 110 | + tr_debug("Modem %sready to receive AT commands", rdy ? "" : "NOT "); |
| 111 | + |
| 112 | +#if DEVICE_SERIAL_FC |
| 113 | + if ((MBED_CONF_STMOD_CELLULAR_CTS != NC) && (MBED_CONF_STMOD_CELLULAR_RTS != NC)) { |
| 114 | + tr_debug("Enable flow control\r\n"); |
| 115 | + |
| 116 | + _at->lock(); |
| 117 | + // enable CTS/RTS flowcontrol |
| 118 | + _at->set_stop_tag(mbed::OK); |
| 119 | + _at->set_at_timeout(400); |
| 120 | + _at->cmd_start("AT+IFC="); |
| 121 | + _at->write_int(2); |
| 122 | + _at->write_int(2); |
| 123 | + _at->cmd_stop_read_resp(); |
| 124 | + err = _at->get_last_error(); |
| 125 | + _at->restore_at_timeout(); |
| 126 | + _at->unlock(); |
| 127 | + |
| 128 | + if (err == NSAPI_ERROR_OK) { |
| 129 | + tr_debug("Flow control turned ON"); |
| 130 | + } else { |
| 131 | + tr_error("Failed to enable hw flow control"); |
| 132 | + } |
| 133 | + } |
| 134 | +#endif |
| 135 | + |
| 136 | + wait_ms(500); |
| 137 | + |
| 138 | +#if MBED_CONF_CELLULAR_DEBUG_AT |
| 139 | + _at->lock(); |
| 140 | + /* Verify Flow Control settings */ |
| 141 | + _at->cmd_start("AT+IFC?"); |
| 142 | + _at->cmd_stop_read_resp(); |
| 143 | + _at->unlock(); |
| 144 | +#endif // MBED_CONF_CELLULAR_DEBUG_AT |
| 145 | + |
| 146 | + return err; |
| 147 | +} |
| 148 | + |
| 149 | +nsapi_error_t STModCellular::soft_power_off() |
| 150 | +{ |
| 151 | + _at->cmd_start("AT+QPOWD"); |
| 152 | + _at->cmd_stop(); |
| 153 | + wait_ms(1000); |
| 154 | + // should wait for POWERED DOWN with a time out up to 65 second according to the manual. |
| 155 | + // we cannot afford such a long wait though. |
| 156 | + return STMOD_CELLULAR_MODEM::soft_power_off(); |
| 157 | +} |
| 158 | + |
| 159 | +#if MBED_CONF_STMOD_CELLULAR_PROVIDE_DEFAULT |
| 160 | +#include "UARTSerial.h" |
| 161 | +CellularDevice *CellularDevice::get_default_instance() |
| 162 | +{ |
| 163 | + tr_debug("MODEM default instance\r\n"); |
| 164 | + |
| 165 | + static UARTSerial serial(MBED_CONF_STMOD_CELLULAR_TX, MBED_CONF_STMOD_CELLULAR_RX, MBED_CONF_STMOD_CELLULAR_BAUDRATE); |
| 166 | +#if defined (MBED_CONF_STMOD_CELLULAR_RTS) && defined(MBED_CONF_STMOD_CELLULAR_CTS) |
| 167 | + tr_debug("STMOD_CELLULAR flow control: RTS %d CTS %d", MBED_CONF_STMOD_CELLULAR_RTS, MBED_CONF_STMOD_CELLULAR_CTS); |
| 168 | + serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_STMOD_CELLULAR_RTS, MBED_CONF_STMOD_CELLULAR_CTS); |
| 169 | +#endif |
| 170 | + static STModCellular device(&serial); |
| 171 | + return &device; |
| 172 | +} |
| 173 | +#endif |
| 174 | + |
0 commit comments