Skip to content

Cellular: Telit ME910 driver #10484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions features/cellular/framework/targets/TELIT/ME910/TELIT_ME910.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* Copyright (c) 2017, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "TELIT_ME910.h"
#include "TELIT_ME910_CellularContext.h"
#include "AT_CellularNetwork.h"
#include "PinNames.h"
#include "rtos/ThisThread.h"

using namespace mbed;
using namespace rtos;
using namespace events;

#if !defined(MBED_CONF_TELIT_ME910_PWR)
#define MBED_CONF_TELIT_ME910_PWR NC
#endif

#if !defined(MBED_CONF_TELIT_ME910_TX)
#define MBED_CONF_TELIT_ME910_TX NC
#endif

#if !defined(MBED_CONF_TELIT_ME910_RX)
#define MBED_CONF_TELIT_ME910_RX NC
#endif

#if !defined(MBED_CONF_TELIT_ME910_POLARITY)
#define MBED_CONF_TELIT_ME910_POLARITY 1 // active high
#endif

static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = {
AT_CellularNetwork::RegistrationModeLAC, // C_EREG
AT_CellularNetwork::RegistrationModeLAC, // C_GREG
AT_CellularNetwork::RegistrationModeLAC, // C_REG
0, // AT_CGSN_WITH_TYPE
0, // AT_CGDATA
1, // AT_CGAUTH
1, // AT_CNMI
1, // AT_CSMP
1, // AT_CMGF
1, // AT_CSDH
1, // PROPERTY_IPV4_STACK
1, // PROPERTY_IPV6_STACK
1, // PROPERTY_IPV4V6_STACK
0, // PROPERTY_NON_IP_PDP_TYPE
1, // PROPERTY_AT_CGEREP
};

//the delay between sending AT commands
static const uint16_t DEFAULT_DELAY_BETWEEN_AT_COMMANDS = 20;

TELIT_ME910::TELIT_ME910(FileHandle *fh, PinName pwr, bool active_high)
: AT_CellularDevice(fh),
_active_high(active_high),
_pwr_key(pwr, !_active_high)
{
AT_CellularBase::set_cellular_properties(cellular_properties);
}

AT_CellularContext *TELIT_ME910::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req)
{
return new TELIT_ME910_CellularContext(at, this, apn, cp_req, nonip_req);
}


uint16_t TELIT_ME910::get_send_delay() const
{
return DEFAULT_DELAY_BETWEEN_AT_COMMANDS;
}

nsapi_error_t TELIT_ME910::init()
{
nsapi_error_t err = AT_CellularDevice::init();
if (err != NSAPI_ERROR_OK) {
return err;
}
_at->lock();
#if defined (MBED_CONF_TELIT_ME910_RTS) && defined (MBED_CONF_TELIT_ME910_CTS)
_at->cmd_start("AT&K3;&C1;&D0");
#else
_at->cmd_start("AT&K0;&C1;&D0");
#endif
_at->cmd_stop_read_resp();

// AT#QSS=1
// Enable the Query SIM Status unsolicited indication in the ME. The format of the
// unsolicited indication is the following:
// #QSS: <status>
// The ME informs at
// every SIM status change through the basic unsolicited indication where <status> range is 0...1
// <status> values:
// - 0: SIM not inserted
// - 1: SIM inserted
_at->cmd_start("AT#QSS=1");
_at->cmd_stop_read_resp();

// AT#PSNT=1
// Set command enables unsolicited result code for packet service network type (PSNT)
// having the following format:
// #PSNT:<nt>
// <nt> values:
// - 0: GPRS network
// - 4: LTE network
// - 5: unknown or not registered
_at->cmd_start("AT#PSNT=1");
_at->cmd_stop_read_resp();

// AT+CMER=2
// Set command enables sending of unsolicited result codes from TA to TE in the case of
// indicator state changes.
// Current setting: buffer +CIEV Unsolicited Result Codes in the TA when TA-TE link is
// reserved (e.g. on-line data mode) and flush them to the TE after
// reservation; otherwise forward them directly to the TE
_at->cmd_start("AT+CMER=2");
_at->cmd_stop_read_resp();

// AT+CMEE=2
// Set command disables the use of result code +CME ERROR: <err> as an indication of an
// error relating to the +Cxxx command issued. When enabled, device related errors cause the +CME
// ERROR: <err> final result code instead of the default ERROR final result code. ERROR is returned
// normally when the error message is related to syntax, invalid parameters or DTE functionality.
// Current setting: enable and use verbose <err> values
_at->cmd_start("AT+CMEE=2");
_at->cmd_stop_read_resp();

// AT&W&P
// - AT&W: Execution command stores on profile <n> the complete configuration of the device. If
// parameter is omitted, the command has the same behavior of AT&W0.
// - AT&P: Execution command defines which full profile will be loaded at startup. If parameter
// is omitted, the command has the same behavior as AT&P0
_at->cmd_start("AT&W&P");
_at->cmd_stop_read_resp();

return _at->unlock_return_error();
}

#if MBED_CONF_TELIT_ME910_PROVIDE_DEFAULT
#include "UARTSerial.h"
CellularDevice *CellularDevice::get_default_instance()
{
static UARTSerial serial(MBED_CONF_TELIT_ME910_TX, MBED_CONF_TELIT_ME910_RX, MBED_CONF_TELIT_ME910_BAUDRATE);
#if defined (MBED_CONF_TELIT_ME910_RTS) && defined (MBED_CONF_TELIT_ME910_CTS)
serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_TELIT_ME910_RTS, MBED_CONF_TELIT_ME910_CTS);
#endif
static TELIT_ME910 device(&serial,
MBED_CONF_TELIT_ME910_PWR,
MBED_CONF_TELIT_ME910_POLARITY);
return &device;
}
#endif

nsapi_error_t TELIT_ME910::hard_power_on()
{
soft_power_on();

return NSAPI_ERROR_OK;
}

nsapi_error_t TELIT_ME910::soft_power_on()
{
_pwr_key = _active_high;
ThisThread::sleep_for(500);
_pwr_key = !_active_high;
ThisThread::sleep_for(5000);
_pwr_key = _active_high;
ThisThread::sleep_for(5000);

return NSAPI_ERROR_OK;
}

nsapi_error_t TELIT_ME910::hard_power_off()
{
_pwr_key = !_active_high;
ThisThread::sleep_for(10000);

return NSAPI_ERROR_OK;
}

nsapi_error_t TELIT_ME910::soft_power_off()
{
return AT_CellularDevice::soft_power_off();
}
56 changes: 56 additions & 0 deletions features/cellular/framework/targets/TELIT/ME910/TELIT_ME910.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2017, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CELLULAR_TARGETS_TELIT_ME910_TELIT_ME910_H_
#define CELLULAR_TARGETS_TELIT_ME910_TELIT_ME910_H_

#ifdef TARGET_FF_ARDUINO
#ifndef MBED_CONF_TELIT_ME910_TX
#define MBED_CONF_TELIT_ME910_TX D1
#endif
#ifndef MBED_CONF_TELIT_ME910_RX
#define MBED_CONF_TELIT_ME910_RX D0
#endif
#endif /* TARGET_FF_ARDUINO */

#include "DigitalOut.h"
#include "AT_CellularDevice.h"

namespace mbed {

class TELIT_ME910 : public AT_CellularDevice {
public:
/**
* Constructs the Telit ME910 series driver. It is mandatory to provide
* a FileHandle object, the power pin and the polarity of the pin.
*/
TELIT_ME910(FileHandle *fh, PinName pwr, bool active_high);

protected: // AT_CellularDevice
virtual uint16_t get_send_delay() const;
virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false);
virtual nsapi_error_t init();
virtual nsapi_error_t hard_power_on();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to be implemented if defined, or if don't have the default power implementation the declaration of power-functions should be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AriParkkila I added the default power implementation for the ME910. I was toying around with this since we have a separate, board-specific power enable pin that I had to accommodate, but that functionality doesn't make sense here (should be in our application/BSP code), so now I just have the necessary power on sequences as suggested in the ME910 manual.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AriParkkila I see that a couple of the CI jobs are failing because my power implementation references the MDMPWRON pin:

[Error] TELIT_ME910.cpp@95,29: 'MDMPWRON' was not declared in this scope
[Error] TELIT_ME910.cpp@109,29: 'MDMPWRON' was not declared in this scope

Is there a recommended way to handle this? Use a #if check for the MDMPWRON pin? Use a #if check for MODEM_ON_BOARD?

Is it better to just use the default power implementation here in the driver and leave it up to the end user to implement this in an onboard_modem_api.c file?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trowbridgec can you follow the implementation from EC2X.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trowbridgec You should declare the pins as class variables, e.g. TELIT_ME910.h:

    bool _active_high;
    DigitalOut _pwr;
    DigitalOut _rst;

And then use the variables instead of compile-time defines, e.g. TELIT_ME910.cpp:

nsapi_error_t TELIT_ME910::hard_power_on()
{
    if (_pwr.is_connected()) {
        _pwr = _active_high;

In addition, the pins need to be provided in the class constructor.

virtual nsapi_error_t hard_power_off();
virtual nsapi_error_t soft_power_on();
virtual nsapi_error_t soft_power_off();
private:
bool _active_high;
DigitalOut _pwr_key;
};
} // namespace mbed
#endif /* CELLULAR_TARGETS_TELIT_ME910_TELIT_ME910_H_ */
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "TELIT_ME910_CellularContext.h"
#include "CellularLog.h"

#include "Semaphore.h"

namespace mbed {

TELIT_ME910_CellularContext::TELIT_ME910_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) :
AT_CellularContext(at, device, apn, cp_req, nonip_req)
{
}

TELIT_ME910_CellularContext::~TELIT_ME910_CellularContext()
{
}

bool TELIT_ME910_CellularContext::get_context()
{
bool modem_supports_ipv6 = get_property(PROPERTY_IPV6_PDP_TYPE);
bool modem_supports_ipv4 = get_property(PROPERTY_IPV4_PDP_TYPE);
_at.cmd_start("AT+CGDCONT?");
_at.cmd_stop();
_at.resp_start("+CGDCONT:");
_cid = -1;
int cid_max = 0; // needed when creating new context
char apn[MAX_ACCESSPOINT_NAME_LENGTH];
int apn_len = 0;

while (_at.info_resp()) {
int cid = _at.read_int();
if (cid > cid_max) {
cid_max = cid;
}
char pdp_type_from_context[10];
int pdp_type_len = _at.read_string(pdp_type_from_context, sizeof(pdp_type_from_context) - 1);
if (pdp_type_len > 0) {
apn_len = _at.read_string(apn, sizeof(apn) - 1);
if (apn_len >= 0) {
if (_apn && apn_len > 0 && (strcmp(apn, _apn) != 0)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This modified version of the get_context() function adds a check here to only skip over (i.e. continue) a candidate APN if it's string length is greater than 0 (i.e. it's not an empty string).

Original:

if (_apn && (strcmp(apn, _apn) != 0)) {
    continue;
}

Modified:

if (_apn && apn_len > 0 && (strcmp(apn, _apn) != 0)) {
    continue;
}

Copy link
Contributor

@jarvte jarvte Jun 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So even if _apn has been defined you wan't to accept an pdp context with empty apn if context matches by pdp type?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jarvte The initial motivation for this change was a scenario that I saw with this module where 6 contexts were already defined (based on the return value from the AT+CGDCONT? command), but all 6 had an empty-string APN. The logic of the current function would reject all 6 of the contexts (because it doesn't exactly match the target APN), and would therefore try to create a new, 7th context which would be denied by the module (it only supports 6 contexts).

I proposed changing the logic of the get_context() function to NOT skip over a context with an empty-string APN (i.e. ONLY skip over a context if it has a non-empty-string APN that DOESN'T match the target APN).

Sorry if I'm explaining this poorly; see PR #10442 for more info.

continue;
}

// APN matched -> Check PDP type
pdp_type_t pdp_type = string_to_pdp_type(pdp_type_from_context);

// Accept exact matching PDP context type or dual PDP context for IPv4/IPv6 only modems
if (get_property(pdp_type_t_to_cellular_property(pdp_type)) ||
((pdp_type == IPV4V6_PDP_TYPE && (modem_supports_ipv4 || modem_supports_ipv6)) && !_nonip_req)) {
_pdp_type = pdp_type;
_cid = cid;
break;
}
}
}
}

_at.resp_stop();
if (_cid == -1) { // no suitable context was found so create a new one
if (!set_new_context(1)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function could probably be removed? This line seems to be the only diff to parent implementation, and this is likely incorrect, this should use _cid after the last in use, see parent implementation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being able to write a custom version of the get_context() function for this driver was the motivation for PR #10442. This function has 2 slight modifications from what's in AT_CellularContext.cpp:

  1. The modified if statement in the comment above.
  2. If we can't find a good APN candidate, the logic that seems to work best for this module is to just use context 1 instead of creating a 7th (which is not allowed).

return false;
}
}

// save the apn
if (apn_len > 0 && !_apn) {
memcpy(_found_apn, apn, apn_len + 1);
}

tr_info("Found PDP context %d", _cid);
return true;
}

} /* namespace mbed */
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TELIT_ME910_CELLULARCONTEXT_H_
#define TELIT_ME910_CELLULARCONTEXT_H_

#include "AT_CellularContext.h"

namespace mbed {

class TELIT_ME910_CellularContext: public AT_CellularContext {
public:
TELIT_ME910_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req = false, bool nonip_req = false);
virtual ~TELIT_ME910_CellularContext();
protected:
virtual bool get_context();
};

} /* namespace mbed */

#endif // TELIT_ME910_CELLULARCONTEXT_H_
Loading