Skip to content

Commit 4d1f5ba

Browse files
authored
Adds a lightweight support class for LTE functionality (#367)
1 parent f106035 commit 4d1f5ba

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
UBlox SARA helper class for the Challenger RP2040 LTE boards
3+
4+
Copyright (c) 2021 P. Oldberg <[email protected]>
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#include <Arduino.h>
22+
#include <ChallengerLTE.h>
23+
24+
Challenger2040LTEClass::Challenger2040LTEClass() {
25+
pinMode(PIN_SARA_ON, OUTPUT);
26+
digitalWrite(PIN_SARA_ON, LOW); // Output register must always be low
27+
pinMode(PIN_SARA_ON, INPUT_PULLUP);
28+
29+
pinMode(PIN_SARA_RST, INPUT_PULLUP); // Keep as input for now
30+
31+
pinMode(PIN_SARA_PWR, OUTPUT);
32+
digitalWrite(PIN_SARA_PWR, LOW); // No power to SARA yet
33+
serialPortConfigured = false;
34+
}
35+
36+
// Do a HW reset by applying a low pulse to the reset line for 1mSec
37+
bool Challenger2040LTEClass::doPowerOn() {
38+
bool ret;
39+
digitalWrite(PIN_SARA_PWR, HIGH); // Make sure LDO is on
40+
delay(100); // let the power stabilize
41+
pinMode(PIN_SARA_ON, OUTPUT); // Pull power on control low
42+
delay(150); // For 150mS
43+
pinMode(PIN_SARA_ON, INPUT_PULLUP); // before releasing it again.
44+
delay(1000); // Now wait for 1 second
45+
SARA_SERIAL_PORT.begin(DEFAULT_SARA_BAUDRATE);
46+
serialPortConfigured = true;
47+
ret = isAlive(); // Makie sure the modem is
48+
// up and running
49+
50+
delay(250); // Allow for any extra characters
51+
// before flushing the input buffer
52+
while(SARA_SERIAL_PORT.available()) SARA_SERIAL_PORT.read();
53+
54+
return ret;
55+
}
56+
57+
// Checks to see if the modem responds to the "AT" poll command.
58+
bool Challenger2040LTEClass::isAlive(uint32_t timeout) {
59+
SARA_SERIAL_PORT.setTimeout(100);
60+
SARA_SERIAL_PORT.println(F("AT"));
61+
String rdy = SARA_SERIAL_PORT.readStringUntil('\n');
62+
while(!rdy.startsWith(F("OK")) && --timeout) {
63+
SARA_SERIAL_PORT.println(F("AT"));
64+
rdy = SARA_SERIAL_PORT.readStringUntil('\n');
65+
//Serial.println(rdy);
66+
}
67+
SARA_SERIAL_PORT.setTimeout(1000); // Restore serial timeout
68+
if (timeout)
69+
return true;
70+
return false;
71+
}
72+
73+
// Return the current MNO profile
74+
// Returns -1 if the serial port is not yet setup or the number of the current
75+
// MNO profile setting from the modem.
76+
int Challenger2040LTEClass::getMNOProfile() {
77+
if (!serialPortConfigured)
78+
return -1;
79+
SARA_SERIAL_PORT.println(F("AT+UMNOPROF?"));
80+
String resp = getResponse();
81+
return resp.substring(resp.indexOf("+UMNOPROF: ") + 11).toInt();
82+
}
83+
84+
// Set a new MNO profile
85+
// Returns false if the serial port is not yet setup
86+
bool Challenger2040LTEClass::setMNOProfile(int profile) {
87+
if (!serialPortConfigured)
88+
return false;
89+
String cmd = "AT+UMNOPROF=" + String(profile) + ",1";
90+
SARA_SERIAL_PORT.println(cmd);
91+
92+
if (!getResponse().endsWith("OK")) {
93+
return false;
94+
}
95+
return true;
96+
}
97+
98+
// Disable power save features
99+
bool Challenger2040LTEClass::enablePS(bool enable) {
100+
if (!serialPortConfigured)
101+
return false;
102+
if (enable)
103+
SARA_SERIAL_PORT.println(F("AT+CPSMS=1"));
104+
else
105+
SARA_SERIAL_PORT.println(F("AT+CPSMS=0"));
106+
107+
if (!getResponse().endsWith("OK")) {
108+
return false;
109+
}
110+
return true;
111+
}
112+
113+
// Get a response from SARA
114+
// A default serial timeout of 2 seconds allow for reading really slow
115+
// responses which should accommodate most replies. Replies are then trimmed
116+
// from control characters and appended with a tab character as a separator.
117+
//
118+
String Challenger2040LTEClass::getResponse(int timeout) {
119+
SARA_SERIAL_PORT.setTimeout(2000); // allow for really slow responses
120+
121+
String resp = SARA_SERIAL_PORT.readStringUntil('\n');
122+
resp.trim();
123+
String acc = resp;
124+
while(resp.indexOf("OK") == -1 && resp.indexOf("ERROR") == -1 && --timeout) {
125+
resp = SARA_SERIAL_PORT.readStringUntil('\n');
126+
resp.trim();
127+
if (resp.length())
128+
acc += "\t" + resp;
129+
}
130+
return acc;
131+
}
132+
133+
Challenger2040LTEClass Challenger2040LTE;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
UBlox SARA helper class for the Challenger RP2040 LTE boards
3+
4+
Copyright (c) 2021 P. Oldberg <[email protected]>
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
#pragma once
21+
22+
#define DEFAULT_SARA_BAUDRATE 115200
23+
24+
class Challenger2040LTEClass {
25+
public:
26+
Challenger2040LTEClass();
27+
bool doPowerOn();
28+
bool isAlive(uint32_t timeout = 50);
29+
int getMNOProfile();
30+
bool setMNOProfile(int profile);
31+
bool enablePS(bool enable = true);
32+
String getResponse(int timeout = 5);
33+
34+
private:
35+
bool serialPortConfigured;
36+
};
37+
38+
extern Challenger2040LTEClass Challenger2040LTE;

variants/challenger_2040_lte/pins_arduino.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define PIN_SARA_ON (13u)
2222
#define PIN_SARA_RST (14u)
2323
#define PIN_SARA_PWR (15u)
24+
#define SARA_SERIAL_PORT Serial2
2425

2526
// SPI
2627
#define PIN_SPI0_MISO (24u)

0 commit comments

Comments
 (0)