Cellular: Add BG96 power control

pull/10588/head
Ari Parkkila 2019-05-16 00:00:48 -07:00
parent 93cb530b94
commit e49d7bbdcd
3 changed files with 82 additions and 50 deletions

View File

@ -15,17 +15,17 @@
* limitations under the License. * limitations under the License.
*/ */
#include "rtos/ThisThread.h"
#include "QUECTEL_BG96.h" #include "QUECTEL_BG96.h"
#include "QUECTEL_BG96_CellularNetwork.h" #include "QUECTEL_BG96_CellularNetwork.h"
#include "QUECTEL_BG96_CellularStack.h" #include "QUECTEL_BG96_CellularStack.h"
#include "QUECTEL_BG96_CellularInformation.h" #include "QUECTEL_BG96_CellularInformation.h"
#include "QUECTEL_BG96_CellularContext.h" #include "QUECTEL_BG96_CellularContext.h"
#include "CellularLog.h" #include "CellularLog.h"
#include "DigitalOut.h"
#include "mbed_wait_api.h"
using namespace mbed; using namespace mbed;
using namespace events; using namespace events;
using namespace rtos;
#define CONNECT_DELIM "\r\n" #define CONNECT_DELIM "\r\n"
#define CONNECT_BUFFER_SIZE (1280 + 80 + 80) // AT response + sscanf format #define CONNECT_BUFFER_SIZE (1280 + 80 + 80) // AT response + sscanf format
@ -33,6 +33,18 @@ using namespace events;
#define DEVICE_READY_URC "CPIN:" #define DEVICE_READY_URC "CPIN:"
#if !defined(MBED_CONF_QUECTEL_BG96_PWR)
#define MBED_CONF_QUECTEL_BG96_PWR NC
#endif
#if !defined(MBED_CONF_QUECTEL_BG96_RST)
#define MBED_CONF_QUECTEL_BG96_RST NC
#endif
#if !defined(MBED_CONF_QUECTEL_BG96_POLARITY)
#define MBED_CONF_QUECTEL_BG96_POLARITY 1 // active high
#endif
static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
AT_CellularNetwork::RegistrationModeLAC, // C_EREG AT_CellularNetwork::RegistrationModeLAC, // C_EREG
AT_CellularNetwork::RegistrationModeLAC, // C_GREG AT_CellularNetwork::RegistrationModeLAC, // C_GREG
@ -51,11 +63,16 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
1, // PROPERTY_AT_CGEREP 1, // PROPERTY_AT_CGEREP
}; };
QUECTEL_BG96::QUECTEL_BG96(FileHandle *fh) : AT_CellularDevice(fh) QUECTEL_BG96::QUECTEL_BG96(FileHandle *fh, PinName pwr, bool active_high, PinName rst)
: AT_CellularDevice(fh),
_active_high(active_high),
_pwr(pwr, !_active_high),
_rst(rst, !_active_high)
{ {
AT_CellularBase::set_cellular_properties(cellular_properties); AT_CellularBase::set_cellular_properties(cellular_properties);
} }
AT_CellularNetwork *QUECTEL_BG96::open_network_impl(ATHandler &at) AT_CellularNetwork *QUECTEL_BG96::open_network_impl(ATHandler &at)
{ {
return new QUECTEL_BG96_CellularNetwork(at); return new QUECTEL_BG96_CellularNetwork(at);
@ -78,54 +95,55 @@ void QUECTEL_BG96::set_ready_cb(Callback<void()> callback)
nsapi_error_t QUECTEL_BG96::hard_power_on() nsapi_error_t QUECTEL_BG96::hard_power_on()
{ {
DigitalOut ModemResetIn (MBED_CONF_QUECTEL_BG96_PWR); if (_pwr.is_connected()) {
tr_info("Modem power on");
wait_ms(250); ThisThread::sleep_for(250);
ModemResetIn = 0; _pwr = !_active_high;
wait_ms(250); ThisThread::sleep_for(250); // BG96_Hardware_Design_V1.1 says 100 ms, but 250 ms seems to be more robust
ModemResetIn = 1; _pwr = _active_high;
wait_ms(500); ThisThread::sleep_for(500);
}
return NSAPI_ERROR_OK; return NSAPI_ERROR_OK;
} }
nsapi_error_t QUECTEL_BG96::soft_power_on() nsapi_error_t QUECTEL_BG96::soft_power_on()
{ {
// turn moden on if (_rst.is_connected()) {
DigitalOut ModemPwrkey (MBED_CONF_QUECTEL_BG96_RST); tr_info("Reset modem");
_rst = !_active_high;
ModemPwrkey = 0; ThisThread::sleep_for(100);
wait_ms(100); _rst = _active_high;
ModemPwrkey = 1; ThisThread::sleep_for(150 + 460); // RESET_N timeout from BG96_Hardware_Design_V1.1
wait_ms(500); _rst = !_active_high;
ModemPwrkey = 0; ThisThread::sleep_for(500);
wait_ms(500);
// wait for RDY // wait for RDY
_at->lock(); _at->lock();
_at->set_at_timeout(60000); _at->set_at_timeout(10 * 1000);
_at->resp_start(); _at->resp_start();
_at->set_stop_tag("RDY"); _at->set_stop_tag("RDY");
bool rdy = _at->consume_to_stop_tag(); bool rdy = _at->consume_to_stop_tag();
_at->set_stop_tag(OK); _at->set_stop_tag(OK);
_at->restore_at_timeout();
_at->unlock(); _at->unlock();
if (!rdy) if (!rdy) {
{
return NSAPI_ERROR_DEVICE_ERROR; return NSAPI_ERROR_DEVICE_ERROR;
} }
}
return NSAPI_ERROR_OK; return NSAPI_ERROR_OK;
} }
nsapi_error_t QUECTEL_BG96::hard_power_off() nsapi_error_t QUECTEL_BG96::hard_power_off()
{ {
// turn moden off if (_pwr.is_connected()) {
DigitalOut ModemPwrkey (MBED_CONF_QUECTEL_BG96_PWR); tr_info("Modem power off");
_pwr = _active_high;
ModemPwrkey = 1; ThisThread::sleep_for(650); // from BG96_Hardware_Design_V1.1
wait_ms(250); _pwr = !_active_high;
ModemPwrkey = 0; }
return NSAPI_ERROR_OK; return NSAPI_ERROR_OK;
} }
@ -142,23 +160,18 @@ nsapi_error_t QUECTEL_BG96::init()
_at->cmd_start("AT+CMEE=1"); // verbose responses _at->cmd_start("AT+CMEE=1"); // verbose responses
_at->cmd_stop_read_resp(); _at->cmd_stop_read_resp();
if (_at->get_last_error() != NSAPI_ERROR_OK) if (_at->get_last_error() == NSAPI_ERROR_OK) {
{ do {
do
{
_at->cmd_start("AT+CFUN=1"); // set full functionality _at->cmd_start("AT+CFUN=1"); // set full functionality
_at->cmd_stop_read_resp(); _at->cmd_stop_read_resp();
// CFUN executed ok // CFUN executed ok
if (_at->get_last_error() != NSAPI_ERROR_OK) if (_at->get_last_error() != NSAPI_ERROR_OK) {
{
// wait some time that modem gets ready for CFUN command, and try again // wait some time that modem gets ready for CFUN command, and try again
retry++; retry++;
_at->flush(); _at->flush();
wait_ms(64); ThisThread::sleep_for(64); // experimental value
} } else {
else
{
// yes continue // yes continue
break; break;
} }
@ -166,6 +179,7 @@ nsapi_error_t QUECTEL_BG96::init()
/* code */ /* code */
} while ((retry < 3)); } while ((retry < 3));
} }
return _at->unlock_return_error(); return _at->unlock_return_error();
} }
@ -178,7 +192,10 @@ CellularDevice *CellularDevice::get_default_instance()
tr_debug("QUECTEL_BG96 flow control: RTS %d CTS %d", MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS); tr_debug("QUECTEL_BG96 flow control: RTS %d CTS %d", MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS);
serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS); serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS);
#endif #endif
static QUECTEL_BG96 device(&serial); static QUECTEL_BG96 device(&serial,
MBED_CONF_QUECTEL_BG96_PWR,
MBED_CONF_QUECTEL_BG96_POLARITY,
MBED_CONF_QUECTEL_BG96_RST);
return &device; return &device;
} }
#endif #endif

View File

@ -27,13 +27,14 @@
#endif #endif
#endif /* TARGET_FF_ARDUINO */ #endif /* TARGET_FF_ARDUINO */
#include "DigitalOut.h"
#include "AT_CellularDevice.h" #include "AT_CellularDevice.h"
namespace mbed { namespace mbed {
class QUECTEL_BG96 : public AT_CellularDevice { class QUECTEL_BG96 : public AT_CellularDevice {
public: public:
QUECTEL_BG96(FileHandle *fh); QUECTEL_BG96(FileHandle *fh, PinName pwr = NC, bool active_high = true, PinName rst = NC);
protected: // AT_CellularDevice protected: // AT_CellularDevice
virtual AT_CellularNetwork *open_network_impl(ATHandler &at); virtual AT_CellularNetwork *open_network_impl(ATHandler &at);
@ -47,6 +48,12 @@ protected: // AT_CellularDevice
public: public:
void handle_urc(FileHandle *fh); void handle_urc(FileHandle *fh);
private:
nsapi_error_t press_power_button(uint32_t timeout);
bool _active_high;
DigitalOut _pwr;
DigitalOut _rst;
}; };
} // namespace mbed } // namespace mbed
#endif // QUECTEL_BG96_H_ #endif // QUECTEL_BG96_H_

View File

@ -37,5 +37,13 @@
"help": "Provide as default CellularDevice [true/false]", "help": "Provide as default CellularDevice [true/false]",
"value": false "value": false
} }
},
"target_overrides": {
"MTB_STM_L475": {
"tx": "SERIAL_TX",
"rx": "SERIAL_RX",
"pwr": "PE_15",
"rst": "PE_9"
}
} }
} }