|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2018 ARM Limited |
| 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 | +#if MBED_CONF_NSAPI_PRESENT |
| 19 | + |
| 20 | +#include "QUECTEL_EC2X.h" |
| 21 | +#include "ONBOARD_QUECTEL_EG25.h" |
| 22 | +#include "gpio_api.h" |
| 23 | +#include "platform/mbed_thread.h" |
| 24 | +#include "PinNames.h" |
| 25 | +#include "drivers/BufferedSerial.h" |
| 26 | +#include "CellularLog.h" |
| 27 | +#include "mbed_wait_api.h" |
| 28 | + |
| 29 | +using namespace mbed; |
| 30 | + |
| 31 | +ONBOARD_QUECTEL_EG25::ONBOARD_QUECTEL_EG25(FileHandle *fh, PinName pwr, bool active_high, PinName rst) : QUECTEL_EC2X(fh, pwr, active_high, rst) |
| 32 | +{ |
| 33 | + initialized = 0; |
| 34 | + ls = new DigitalOut(LS_LED); |
| 35 | + net_status = new InterruptIn(NET_STATUS); |
| 36 | +} |
| 37 | + |
| 38 | +nsapi_error_t ONBOARD_QUECTEL_EG25::hard_power_on() |
| 39 | +{ |
| 40 | + onboard_modem_init(); |
| 41 | + return NSAPI_ERROR_OK; |
| 42 | +} |
| 43 | + |
| 44 | +nsapi_error_t ONBOARD_QUECTEL_EG25::hard_power_off() |
| 45 | +{ |
| 46 | + onboard_modem_deinit(); |
| 47 | + return NSAPI_ERROR_OK; |
| 48 | +} |
| 49 | + |
| 50 | +nsapi_error_t ONBOARD_QUECTEL_EG25::soft_power_on() |
| 51 | +{ |
| 52 | + // See base function description. This function is for power on but reset too. |
| 53 | + onboard_modem_power_down(); |
| 54 | + onboard_modem_power_up(); |
| 55 | + return NSAPI_ERROR_OK; |
| 56 | +} |
| 57 | + |
| 58 | +nsapi_error_t ONBOARD_QUECTEL_EG25::soft_power_off() |
| 59 | +{ |
| 60 | + onboard_modem_power_down(); |
| 61 | + return NSAPI_ERROR_OK; |
| 62 | +} |
| 63 | + |
| 64 | +CellularDevice *CellularDevice::get_target_default_instance() |
| 65 | +{ |
| 66 | + static BufferedSerial serial(MDMTXD, MDMRXD, 115200); |
| 67 | +#if DEVICE_SERIAL_FC |
| 68 | + if (MDMRTS != NC && MDMCTS != NC) { |
| 69 | + tr_debug("Modem flow control: RTS %d CTS %d", MDMRTS, MDMCTS); |
| 70 | + serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS); |
| 71 | + } |
| 72 | +#endif |
| 73 | + static ONBOARD_QUECTEL_EG25 device(&serial, MDMPWRON, 0, MDMRST); |
| 74 | + return &device; |
| 75 | +} |
| 76 | + |
| 77 | +void ONBOARD_QUECTEL_EG25::press_power_button(int time_us) |
| 78 | +{ |
| 79 | + gpio_t gpio; |
| 80 | + gpio_init_inout(&gpio, MDMPWRON, PIN_OUTPUT, OpenDrainNoPull, 0); |
| 81 | + wait_us(time_us); |
| 82 | + gpio_init_inout(&gpio, MDMPWRON, PIN_INPUT, PullNone, 1); |
| 83 | +} |
| 84 | + |
| 85 | +void ONBOARD_QUECTEL_EG25::onboard_modem_init() |
| 86 | +{ |
| 87 | + // PWRKEY/RADIO_ONOFF = output open drain |
| 88 | + // RADIO_PWR = Enables the regulator. High = on. Has pulldown on board. |
| 89 | + // RADIO_RESET/MDMRST = Reset the radio. Open drain or float. |
| 90 | + // RADIO_STATUS/STATUS = input with pull up. Gets driven low when radio is on. |
| 91 | + |
| 92 | + gpio_t gpio; |
| 93 | + // Enable radio power regulator and buffer, configure RESET_N and PWR_ON. |
| 94 | + gpio_init_inout(&gpio, RADIO_PWR, PIN_OUTPUT, PushPullNoPull, 1); |
| 95 | + gpio_init_inout(&gpio, BUF_EN, PIN_OUTPUT, OpenDrainNoPull, 0); |
| 96 | + // MDMRST and MDMPWRON get driven low via open drain. |
| 97 | + gpio_init_inout(&gpio, MDMRST, PIN_INPUT, PullNone, 1); |
| 98 | + gpio_init_inout(&gpio, MDMPWRON, PIN_INPUT, PullNone, 1); |
| 99 | + gpio_init_in_ex(&gpio, RADIO_STATUS, PullUp); |
| 100 | + // Quectel hardware design guide recommends >= 30ms from powered until |
| 101 | + // PWRKEY(MDMPWRON) is pulled low. |
| 102 | + wait_us(40000); |
| 103 | + initialized = 1; |
| 104 | +} |
| 105 | + |
| 106 | +void ONBOARD_QUECTEL_EG25::onboard_modem_deinit() |
| 107 | +{ |
| 108 | + // Make sure to power down before removing power! |
| 109 | + onboard_modem_power_down(); |
| 110 | + gpio_t gpio; |
| 111 | + |
| 112 | + // Set all to inputs no pull. Let pull resistors do their thing. Allows |
| 113 | + // for lowest power draw. Disable radio power regulator and buffer. |
| 114 | + gpio_init_inout(&gpio, MDMRST, PIN_INPUT, PullNone, 1); |
| 115 | + gpio_init_inout(&gpio, MDMPWRON, PIN_INPUT, PullNone, 1); |
| 116 | + gpio_init_inout(&gpio, BUF_EN, PIN_INPUT, PullNone, 1); |
| 117 | + gpio_init_inout(&gpio, RADIO_PWR, PIN_INPUT, PullNone, 0); |
| 118 | + initialized = 0; |
| 119 | +} |
| 120 | + |
| 121 | +void ONBOARD_QUECTEL_EG25::link_status() |
| 122 | +{ |
| 123 | + ls->write(net_status->read()); |
| 124 | +} |
| 125 | + |
| 126 | +void ONBOARD_QUECTEL_EG25::onboard_modem_power_up() |
| 127 | +{ |
| 128 | + // NET_STATUS = input. InterruptIn make LS LED follow. |
| 129 | + // LS_LED = output. Follow NET_STATUS. |
| 130 | + net_status->rise(callback(this,&ONBOARD_QUECTEL_EG25::link_status)); |
| 131 | + net_status->fall(callback(this,&ONBOARD_QUECTEL_EG25::link_status)); |
| 132 | + |
| 133 | + // Make sure the radio is initialized so it can be powered on. |
| 134 | + if (!initialized){ |
| 135 | + onboard_modem_init(); |
| 136 | + } |
| 137 | + |
| 138 | + gpio_t status; |
| 139 | + gpio_init_in_ex(&status, RADIO_STATUS, PullUp); |
| 140 | + |
| 141 | + int radio_off = gpio_read(&status); |
| 142 | + // If radio is on, do nothing. |
| 143 | + if (!radio_off) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + // Activate PWRKEY at least 500ms to turn on. |
| 148 | + press_power_button(550000); |
| 149 | + |
| 150 | + // Wait 10s for status to go low before retrying. |
| 151 | + uint8_t timeout = 10; |
| 152 | + do { |
| 153 | + thread_sleep_for(1000); |
| 154 | + radio_off = gpio_read(&status); |
| 155 | + if(!timeout--){ |
| 156 | + press_power_button(550000); |
| 157 | + timeout = 10; |
| 158 | + } |
| 159 | + } while (radio_off); |
| 160 | + |
| 161 | + _at.lock(); |
| 162 | + |
| 163 | + _at.set_at_timeout(15000); |
| 164 | + _at.resp_start(); |
| 165 | + _at.set_stop_tag("RDY"); |
| 166 | + bool rdy = _at.consume_to_stop_tag(); |
| 167 | + _at.set_stop_tag(OK); |
| 168 | + |
| 169 | + _at.unlock(); |
| 170 | + if (rdy) { |
| 171 | + tr_debug("Radio outputted RDY"); |
| 172 | + } else { |
| 173 | + tr_debug("Radio did not output RDY within 15s."); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +void ONBOARD_QUECTEL_EG25::onboard_modem_power_down() |
| 178 | +{ |
| 179 | + gpio_t status; |
| 180 | + gpio_init_in_ex(&status, RADIO_STATUS, PullUp); |
| 181 | + // Need volatile so status check is not optimized out. |
| 182 | + volatile int radio_off = gpio_read(&status); |
| 183 | + // Do nothing if it's already off. |
| 184 | + if (radio_off) { |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + // Make sure the I/O are properly initialized. |
| 189 | + if (!initialized){ |
| 190 | + onboard_modem_init(); |
| 191 | + } |
| 192 | + |
| 193 | + do { |
| 194 | + // Activate PWRKEY for at least 650ms to turn off. |
| 195 | + press_power_button(680000); |
| 196 | + // Wait 40s for status to go high before resetting. |
| 197 | + uint8_t timeout = 40; |
| 198 | + do { |
| 199 | + thread_sleep_for(1000); |
| 200 | + radio_off = gpio_read(&status); |
| 201 | + } while (!radio_off && timeout); |
| 202 | + |
| 203 | + if (radio_off) { |
| 204 | + break; |
| 205 | + } |
| 206 | + else { |
| 207 | + onboard_modem_reset(); |
| 208 | + } |
| 209 | + } |
| 210 | + while (!radio_off); |
| 211 | +} |
| 212 | + |
| 213 | +void ONBOARD_QUECTEL_EG25::onboard_modem_reset() |
| 214 | +{ |
| 215 | + gpio_t gpio; |
| 216 | + gpio_init_inout(&gpio, MDMRST, PIN_OUTPUT, OpenDrainNoPull, 0); |
| 217 | + thread_sleep_for(500); |
| 218 | + gpio_init_inout(&gpio, MDMRST, PIN_INPUT, PullNone, 1); |
| 219 | + thread_sleep_for(100); |
| 220 | +} |
| 221 | + |
| 222 | +#endif // MBED_CONF_NSAPI_PRESENT |
0 commit comments