Skip to content
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

Portenta H7 + MAX Carrier support #117

Merged
merged 1 commit into from
Oct 23, 2023
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
8 changes: 4 additions & 4 deletions src/GPRS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ NB_NetworkStatus_t GPRS::attachGPRS(bool synchronous)

while (ready() == 0) {
if (_timeout && !((millis() - start) < _timeout)) {
_state = ERROR;
_state = NB_ERROR;
break;
}
delay(500);
Expand Down Expand Up @@ -106,7 +106,7 @@ int GPRS::ready()
case GPRS_STATE_WAIT_ATTACH_RESPONSE: {
if (ready > 1) {
_state = GPRS_STATE_IDLE;
_status = ERROR;
_status = NB_ERROR;
} else {
_state = GPRS_STATE_CHECK_ATTACHED;
ready = 0;
Expand All @@ -125,7 +125,7 @@ int GPRS::ready()
case GPRS_STATE_WAIT_CHECK_ATTACHED_RESPONSE: {
if (ready > 1) {
_state = GPRS_STATE_IDLE;
_status = ERROR;
_status = NB_ERROR;
} else {
if (_response.endsWith("1,1")) {
_state = GPRS_STATE_IDLE;
Expand All @@ -149,7 +149,7 @@ int GPRS::ready()
case GPRS_STATE_WAIT_DEATTACH_RESPONSE: {
if (ready > 1) {
_state = GPRS_STATE_IDLE;
_status = ERROR;
_status = NB_ERROR;
} else {
_state = GPRS_STATE_IDLE;
_status = IDLE;
Expand Down
63 changes: 62 additions & 1 deletion src/Modem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,63 @@ int ModemClass::isPowerOn()
return digitalRead(_vIntPin);
}

#ifdef ARDUINO_PORTENTA_H7_M7
#include <mbed.h>
#include <BQ24195.h>
mbed::DigitalInOut pwr(PD_4);
mbed::DigitalInOut rst(PE_3);
#endif

int ModemClass::begin(bool restart)
{
#ifdef ARDUINO_PORTENTA_H7_M7
pwr.input();
rst.input();
PMIC.begin();
PMIC.disableWatchdog();
// Set the input current limit to 2 A and the overload input voltage to 3.88 V
PMIC.setInputCurrentLimit(3.0);
PMIC.setInputVoltageLimit(3.88);
// set the minimum voltage used to feeding the module embed on Board
PMIC.setMinimumSystemVoltage(3.8);
// Set the desired charge voltage to 4.11 V
PMIC.setChargeVoltage(4.2);
// Set the charge current to 375 mA
// the charge current should be defined as maximum at (C for hour)/2h
// to avoid battery explosion (for example for a 750 mAh battery set to 0.375 A)
PMIC.setChargeCurrent(0.375);
PMIC.enableBoostMode();
PMIC.setInputCurrentLimit(3.0);

pwr.output();
pwr = 0;
delay(150);
pwr = 1;
delay(150);
pwr.input();
rst.output();
rst = 0;
delay(150);
rst = 1;
delay(150);
rst.input();
#endif

#ifndef ARDUINO_PORTENTA_H7_M7
// datasheet warns not to use _resetPin, this may lead to an unrecoverable state
pinMode(_powerOnPin, OUTPUT);
pinMode(_resetPin, OUTPUT);
digitalWrite(_resetPin, LOW);

if (restart) {
shutdown();
end();
}
#endif

_uart->begin(_baud > 115200 ? 115200 : _baud);

// power on module
#ifndef ARDUINO_PORTENTA_H7_M7
if (!isPowerOn()) {
digitalWrite(_powerOnPin, HIGH);
delay(150); // Datasheet says power-on pulse should be >=150ms, <=3200ms
Expand All @@ -79,6 +123,7 @@ int ModemClass::begin(bool restart)
return 0;
}
}
#endif

if (!autosense()) {
return 0;
Expand All @@ -105,7 +150,11 @@ int ModemClass::begin(bool restart)
int ModemClass::shutdown()
{
// AT command shutdown
#ifdef ARDUINO_PORTENTA_H7_M7
if (autosense(200)) {
#else
if (isPowerOn()) {
#endif
send("AT+CPWROFF");
if (waitForResponse(40000) != 1) {
return 0;
Expand All @@ -119,7 +168,11 @@ void ModemClass::end()
{
_uart->end();
// Hardware pin power off
#ifdef ARDUINO_PORTENTA_H7_M7
if (1) {
#else
if (isPowerOn()) {
#endif
digitalWrite(_powerOnPin, HIGH);
delay(1500); // Datasheet says power-off pulse should be >=1500ms
digitalWrite(_powerOnPin, LOW);
Expand Down Expand Up @@ -389,4 +442,12 @@ void ModemClass::setBaudRate(unsigned long baud)
_baud = baud;
}

#ifdef ARDUINO_PORTENTA_H7_M7
#include <mbed.h>
UART SerialSARA(PA_9, PA_10, NC, NC);
mbed::DigitalOut rts(PI_14, 0);
#define SARA_PWR_ON PD_4
#define SARA_RESETN PE_3
#endif

ModemClass MODEM(SerialSARA, 115200, SARA_RESETN, SARA_PWR_ON, SARA_VINT);
4 changes: 4 additions & 0 deletions src/Modem.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class ModemUrcHandler {
virtual void handleUrc(const String& urc) = 0;
};

#ifdef ARDUINO_PORTENTA_H7_M7
typedef UART Uart;
#endif

class ModemClass {
public:
ModemClass(Uart& uart, unsigned long baud, int resetPin, int powerOnPin, int vIntPin=SARA_VINT);
Expand Down
36 changes: 18 additions & 18 deletions src/NB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ enum {
};

NB::NB(bool debug) :
_state(ERROR),
_state(NB_ERROR),
_readyState(0),
_pin(NULL),
_apn(""),
Expand All @@ -78,7 +78,7 @@ NB_NetworkStatus_t NB::begin(const char* pin, const char* apn, bool restart, boo
NB_NetworkStatus_t NB::begin(const char* pin, const char* apn, const char* username, const char* password, bool restart, bool synchronous)
{
if (!MODEM.begin(restart)) {
_state = ERROR;
_state = NB_ERROR;
} else {
_pin = pin;
_apn = apn;
Expand All @@ -92,7 +92,7 @@ NB_NetworkStatus_t NB::begin(const char* pin, const char* apn, const char* usern

while (ready() == 0) {
if (_timeout && !((millis() - start) < _timeout)) {
_state = ERROR;
_state = NB_ERROR;
break;
}

Expand Down Expand Up @@ -142,7 +142,7 @@ bool NB::secureShutdown()

int NB::ready()
{
if (_state == ERROR) {
if (_state == NB_ERROR) {
return 2;
}

Expand All @@ -162,7 +162,7 @@ int NB::ready()

case READY_STATE_WAIT_SET_ERROR_DISABLED: {
if (ready > 1) {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
} else {
_readyState = READY_STATE_SET_MINIMUM_FUNCTIONALITY_MODE;
Expand All @@ -181,7 +181,7 @@ int NB::ready()

case READY_STATE_WAIT_SET_MINIMUM_FUNCTIONALITY_MODE:{
if (ready > 1) {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
} else {
_readyState = READY_STATE_CHECK_SIM;
Expand Down Expand Up @@ -212,7 +212,7 @@ int NB::ready()
_readyState = READY_STATE_UNLOCK_SIM;
ready = 0;
} else {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
}
}
Expand All @@ -228,15 +228,15 @@ int NB::ready()
_readyState = READY_STATE_WAIT_UNLOCK_SIM_RESPONSE;
ready = 0;
} else {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
}
break;
}

case READY_STATE_WAIT_UNLOCK_SIM_RESPONSE: {
if (ready > 1) {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
} else {
_readyState = READY_STATE_DETACH_DATA;
Expand All @@ -255,7 +255,7 @@ int NB::ready()

case READY_STATE_WAIT_DETACH_DATA:{
if (ready > 1) {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
} else {
_readyState = READY_STATE_SET_PREFERRED_MESSAGE_FORMAT;
Expand All @@ -274,7 +274,7 @@ int NB::ready()

case READY_STATE_WAIT_SET_PREFERRED_MESSAGE_FORMAT_RESPONSE: {
if (ready > 1) {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
} else {
_readyState = READY_STATE_SET_HEX_MODE;
Expand All @@ -293,7 +293,7 @@ int NB::ready()

case READY_STATE_WAIT_SET_HEX_MODE_RESPONSE: {
if (ready > 1) {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
} else {
_readyState = READY_STATE_SET_AUTOMATIC_TIME_ZONE;
Expand All @@ -312,7 +312,7 @@ int NB::ready()

case READY_STATE_WAIT_SET_AUTOMATIC_TIME_ZONE_RESPONSE: {
if (ready > 1) {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
} else {
_readyState = READY_STATE_SET_APN;
Expand All @@ -330,7 +330,7 @@ int NB::ready()

case READY_STATE_WAIT_SET_APN: {
if (ready > 1) {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
} else {
_readyState = READY_STATE_SET_APN_AUTH;
Expand All @@ -355,7 +355,7 @@ int NB::ready()

case READY_STATE_WAIT_SET_APN_AUTH: {
if (ready > 1) {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
} else {
_readyState = READY_STATE_SET_FULL_FUNCTIONALITY_MODE;
Expand All @@ -373,7 +373,7 @@ int NB::ready()

case READY_STATE_WAIT_SET_FULL_FUNCTIONALITY_MODE:{
if (ready > 1) {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
} else {
_readyState = READY_STATE_CHECK_REGISTRATION;
Expand All @@ -393,7 +393,7 @@ int NB::ready()

case READY_STATE_WAIT_CHECK_REGISTRATION_RESPONSE: {
if (ready > 1) {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
} else {
int status = _response.charAt(_response.length() - 1) - '0';
Expand All @@ -410,7 +410,7 @@ int NB::ready()
_state = CONNECTING;
ready = 0;
} else if (status == 3) {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NB.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <Arduino.h>

enum NB_NetworkStatus_t { ERROR, IDLE, CONNECTING, NB_READY, GPRS_READY, TRANSPARENT_CONNECTED, NB_OFF};
enum NB_NetworkStatus_t { NB_ERROR, IDLE, CONNECTING, NB_READY, GPRS_READY, TRANSPARENT_CONNECTED, NB_OFF};

class NB {

Expand Down