Skip to content

Commit 9b41804

Browse files
committed
Merge branch 'develop' into bug/mqtt-rapid-publish
2 parents 0b387e5 + 2df68e4 commit 9b41804

File tree

5 files changed

+204
-11
lines changed

5 files changed

+204
-11
lines changed

examples/gpio/gpio.ino

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @brief This example demonstrates how to use the GPIO pins on the board, where
3+
* for the AVR-IoT Cellular board the pin naming is a bit different than in a
4+
* regular arduino environment.
5+
*/
6+
#include <Arduino.h>
7+
8+
/**
9+
* @brief Note that it is important that variables modified in an interrupt are
10+
* volatile, so that the compiler don't optimize them away.
11+
*/
12+
static volatile bool button_pressed = false;
13+
14+
/**
15+
* @brief Gets called when the SW0 button is pressed.
16+
*/
17+
static void buttonInterrupt(void) { button_pressed = true; }
18+
19+
void setup() {
20+
// The pins on the board are not named in a typical way as with standard
21+
// Arduino (with numbers), but with port and pin. The pinout can be seen in
22+
// the hardware user guide located at (figure 2-1, page 7):
23+
// https://ww1.microchip.com/downloads/en/DeviceDoc/AVR-IoT-Cellular-Mini-HW-UserGuide-DS50003320.pdf
24+
//
25+
// In order to modify GPIO pins, we need to use the light gray values showed
26+
// in the hardware user guide (e.g. PB2, which stands for port B and pin 2)
27+
28+
// To configure a pin as output (in this case PB2, which is the user led),
29+
// do the following:
30+
pinConfigure(PIN_PB2, PIN_DIR_OUTPUT);
31+
32+
// Then we can turn on the led by a regular digitalWrite (note that the LED
33+
// is active low)
34+
digitalWrite(PIN_PB2, LOW);
35+
36+
// In order to configure a pin as an input, for example the button SW0 at
37+
// PD2, we can do the following:
38+
pinConfigure(PIN_PD2, PIN_DIR_INPUT);
39+
40+
// Then we can e.g. attach an interrupt to the button when it is pressed (on
41+
// the falling edge). If we want to have an interrupt when the button is
42+
// released, we'd need to use the rising edge instead.
43+
//
44+
// We could also use digitalRead(PIN_PD2) in a loop in order to check the
45+
// value continuously
46+
attachInterrupt(PIN_PD2, buttonInterrupt, FALLING);
47+
48+
// We start the Serial3, which is used to print messages. As one can also
49+
// see on the same page in the hardware user guide, the USART3 is used for
50+
// sending messages to the debugger, which again is connected to the
51+
// computer via USB. We thus have to be careful to use Serial3 and not
52+
// Serial for printing
53+
Serial3.begin(115200);
54+
55+
// Analog functionality is quite similar, we can for example set up analog
56+
// reading of the voltage measurement pin on the board. As one can see in
57+
// the hardware user guide, this pin is PE0 (under power supply
58+
// connections).
59+
60+
// First we need to set the voltage measure pin (PB3) high to tell the
61+
// hardware that we want to read the supply voltage:
62+
pinConfigure(PIN_PB3, PIN_DIR_OUTPUT);
63+
digitalWrite(PIN_PB3, HIGH);
64+
65+
// Delay some to let the changes take effect
66+
delay(100);
67+
68+
// Now we can do a regular analog read. We do one analogRead first as the
69+
// result initially might be unstable and not settled.
70+
analogRead(PIN_PE0);
71+
72+
float analog_value = (float)analogRead(PIN_PE0);
73+
74+
// The analog value will be a value from 0 to 1023 (10 bit resolution), so
75+
// we divide by 1023 to get a value between 0 and 1:
76+
analog_value /= 1023.0f;
77+
78+
// The input pin runs on a logic level of 3.3 V, so we have to upscale by
79+
// that
80+
analog_value *= 3.3f;
81+
82+
// The voltage read is actually 1/4 of the true value since it is within a
83+
// voltage divider, so we have to multiply by 4 (look at page 32 in the
84+
// hardware user guide under VMUX Voltage Measure):
85+
analog_value *= 4.0f;
86+
87+
// You should see a voltage of approximately 4.8 V for when the board is
88+
// connected through USB (USB is delivering 5 V, but there are some
89+
// voltage drop over the components on the way to the voltage measurement
90+
// circuit)
91+
Serial3.print("The voltage supplied is: ");
92+
Serial3.println(analog_value);
93+
}
94+
95+
void loop() {
96+
97+
if (button_pressed) {
98+
Serial3.println("Button pressed");
99+
button_pressed = false;
100+
}
101+
}

examples/sandbox/sandbox.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void setup() {
288288
attachInterrupt(PIN_PD2, sendHeartbeatInterrupt, FALLING);
289289

290290
// Set PF6 as input (reset button)
291-
pinConfigure(PIN_PF6, PIN_DIR_INPUT);
291+
pinConfigure(PIN_PF6, PIN_DIR_INPUT | PIN_PULLUP_ON);
292292
attachInterrupt(PIN_PF6, resetInterrupt, FALLING);
293293

294294
sei();
@@ -323,6 +323,9 @@ void setup() {
323323
sprintf(mqtt_sub_topic, MQTT_SUB_TOPIC_FMT, thing_name);
324324
sprintf(mqtt_pub_topic, MQTT_PUB_TOPIC_FMT, thing_name);
325325

326+
Log.info("Will now connect to the operator. If the board hasn't previously "
327+
"connected to the operator/network, establishing the "
328+
"connection the first time might take some time.");
326329
connectLTE();
327330
}
328331

examples/serial/serial.ino

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @brief This file demonstrates how to use Serial for printing out messages on
3+
* the board. It also demonstrates how to use the logging module in the AVR-IoT
4+
* Cellular Library.
5+
*/
6+
#include <Arduino.h>
7+
8+
#include "log.h"
9+
10+
void setup() {
11+
12+
// As can be seen on page 7 in the hardware user guide for the AVR-IoT
13+
// Cellular Mini:
14+
// https://ww1.microchip.com/downloads/en/DeviceDoc/AVR-IoT-Cellular-Mini-HW-UserGuide-DS50003320.pdf,
15+
// the USART/Serial connected to the debugger which is thereafter connected
16+
// to the USB port, is the Serial3. It is thus important that we use Serial3
17+
// and not Serial when printing messages.
18+
Serial3.begin(115200);
19+
Serial3.println("Hello world from Serial3");
20+
Serial3.end();
21+
22+
delay(500);
23+
24+
// The AVR-IoT Cellular Library also comes with a logging library, which
25+
// uses Serial3 under the hood and can be initialised in a similar way. It
26+
// will add a prefix to the particular logging level used (debug, info,
27+
// warning, error).
28+
Log.begin(115200);
29+
Log.info("Hello world from Log");
30+
31+
// If no prefix is wanted, we can use raw:
32+
Log.raw("This is a message without a prefix");
33+
34+
// The log level determines which messages are printed between the levels.
35+
// We can adjust the level as we want, the following will be printed under
36+
// the different levels:
37+
//
38+
// debug: All messages
39+
// info: info, warning and error messages
40+
// warn: Warning and error messages
41+
// error: Only error messages
42+
// none: No messages
43+
//
44+
// Note that raw messages always will be printed
45+
Log.setLogLevel(LogLevel::DEBUG);
46+
Log.debug("A debug message");
47+
Log.info("An info message");
48+
Log.warn("A warning message");
49+
Log.error("An error message");
50+
51+
Log.setLogLevel(LogLevel::INFO);
52+
Log.raw(""); // Just to add a newline
53+
Log.debug("This will not be printed now");
54+
Log.info("An info message");
55+
Log.warn("A warning message");
56+
Log.error("An error message");
57+
58+
Log.setLogLevel(LogLevel::WARN);
59+
Log.raw(""); // Just to add a newline
60+
Log.debug("This will not be printed now");
61+
Log.info("This will not be printed now");
62+
Log.warn("A warning message");
63+
Log.error("An error message");
64+
65+
Log.setLogLevel(LogLevel::ERROR);
66+
Log.raw(""); // Just to add a newline
67+
Log.debug("This will not be printed now");
68+
Log.info("This will not be printed now");
69+
Log.warn("This will not be printed now");
70+
Log.error("An error message");
71+
72+
Log.setLogLevel(LogLevel::NONE);
73+
Log.raw(""); // Just to add a newline
74+
Log.debug("This will not be printed now");
75+
Log.info("This will not be printed now");
76+
Log.warn("This will not be printed now");
77+
Log.error("This will not be printed now");
78+
Log.raw(""); // Just to add a newline
79+
80+
Log.setLogLevel(LogLevel::INFO);
81+
82+
// The logging library can also format strings by using the f postfix in the
83+
// function name, so we don't have to use multiple print calls as with the
84+
// standard Arduino Serial. This makes printing more complex things far more
85+
// structured and work in the same way as the printf in the standard library
86+
// of C. Note we have to add a \r\n for a carriage return and new line when
87+
// the format based functions are used.
88+
Log.infof("This is a number: %d\r\n", 10);
89+
Log.infof("This is a string: %s\r\n", "Hello world");
90+
Log.infof("This is a hexadecimal and a string: %X - %s\r\n",
91+
31,
92+
"Hello world");
93+
}
94+
95+
void loop() {}

src/log.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,7 @@ void LogClass::debugf(const char* format, ...) {
119119
}
120120
}
121121

122-
void LogClass::raw(const char str[]) {
123-
if (log_level >= LogLevel::DEBUG) {
124-
this->print(str, DEBUG_LEVEL_FMT);
125-
}
126-
127-
this->uart->print(str);
128-
}
122+
void LogClass::raw(const char str[]) { this->print(str, ""); }
129123

130124
void LogClass::raw(const String str) { this->raw(str.c_str()); }
131125

src/low_power.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ void LowPowerClass::powerSave(void) {
566566
Log.warnf("Operator was not able to match the requested power "
567567
"save mode period of %d seconds. ",
568568
period_requested);
569-
Log.rawf("Operator sat the period to %d seconds.\r\n", period);
569+
Log.rawf("Operator set the period to %d seconds.\r\n", period);
570570
}
571571

572572
retrieved_period = true;
@@ -662,12 +662,12 @@ float LowPowerClass::getSupplyVoltage(void) {
662662
}
663663

664664
// The default resolution is 10 bits, so divide by that to get the fraction
665-
// of VDD, which is 3.3 V
665+
// of VDD, which is 3.3 V (which logic level at the input pin is at)
666666
//
667667
// The voltage is in a voltage divider, and is divided by 4, so have to
668668
// multiply it up
669669
float value = 4.0f * 3.3f * ((float)analogRead(VOLTAGE_MEASURE_PIN)) /
670-
1024.0f;
670+
1023.0f;
671671

672672
return value;
673673
}

0 commit comments

Comments
 (0)