Skip to content

Commit

Permalink
Merge pull request #117 from facchinm/portenta_max_carrier
Browse files Browse the repository at this point in the history
Portenta H7 + MAX Carrier support
  • Loading branch information
facchinm authored Oct 23, 2023
2 parents 47f4bd1 + 02e444d commit 13cef68
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 24 deletions.
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.indexOf("1,1") >= 0) {
_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 @@ -60,7 +60,7 @@ enum {
};

NB::NB(bool debug) :
_state(ERROR),
_state(NB_ERROR),
_readyState(0),
_pin(NULL),
_apn(""),
Expand All @@ -86,7 +86,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 @@ -100,7 +100,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 @@ -152,7 +152,7 @@ bool NB::secureShutdown()

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

Expand All @@ -172,7 +172,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 @@ -191,7 +191,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 @@ -222,7 +222,7 @@ int NB::ready()
_readyState = READY_STATE_UNLOCK_SIM;
ready = 0;
} else {
_state = ERROR;
_state = NB_ERROR;
ready = 2;
}
}
Expand All @@ -238,15 +238,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 @@ -265,7 +265,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 @@ -284,7 +284,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 @@ -303,7 +303,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 @@ -322,7 +322,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 @@ -340,7 +340,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 @@ -365,7 +365,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 @@ -383,7 +383,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 @@ -403,7 +403,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 @@ -420,7 +420,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

0 comments on commit 13cef68

Please sign in to comment.