Skip to content

Commit fe9f03f

Browse files
committed
MCU8MASS-852 Make featherwing pins and buttons have interrupts during power down/save so they can be used as wake up sources
1 parent 850f166 commit fe9f03f

File tree

3 files changed

+126
-72
lines changed

3 files changed

+126
-72
lines changed

examples/power_down/power_down.ino

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,30 @@
1111
#include <mcp9808.h>
1212
#include <veml3328.h>
1313

14+
#define SW0 PIN_PD2
15+
16+
static bool woke_up_from_button = false;
17+
18+
void button_pressed_callback(void) {
19+
if (PORTD.INTFLAGS & PIN2_bm) {
20+
// Reset the interupt flag so that we can process the next incoming
21+
// interrupt
22+
PORTD.INTFLAGS = PIN2_bm;
23+
24+
woke_up_from_button = true;
25+
}
26+
}
27+
1428
void setup() {
1529
Log.begin(115200);
1630

1731
LedCtrl.begin();
1832
LedCtrl.startupCycle();
1933

34+
// Attach interrupt callback to detect if SW0 was pressed during sleep
35+
pinConfigure(SW0, PIN_DIR_INPUT);
36+
attachInterrupt(SW0, button_pressed_callback, FALLING);
37+
2038
// Now we configure the low power module for power down configuration, where
2139
// the cellular modem and the CPU will be powered down
2240
LowPower.configurePowerDown();
@@ -43,6 +61,11 @@ void loop() {
4361
// Power down for 60 seconds
4462
LowPower.powerDown(60);
4563

64+
if (woke_up_from_button) {
65+
Log.info("Button was pressed");
66+
woke_up_from_button = false;
67+
}
68+
4669
Log.info("Woke up!");
4770

4871
// Do work ...

examples/power_save/power_save.ino

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,30 @@
1212
#include <sequans_controller.h>
1313
#include <veml3328.h>
1414

15+
#define SW0 PIN_PD2
16+
17+
static bool woke_up_from_button = false;
18+
19+
void button_pressed_callback(void) {
20+
if (PORTD.INTFLAGS & PIN2_bm) {
21+
// Reset the interupt flag so that we can process the next incoming
22+
// interrupt
23+
PORTD.INTFLAGS = PIN2_bm;
24+
25+
woke_up_from_button = true;
26+
}
27+
}
28+
1529
void setup() {
1630
Log.begin(115200);
1731

1832
LedCtrl.begin();
1933
LedCtrl.startupCycle();
2034

35+
// Attach interrupt callback to detect if SW0 was pressed during sleep
36+
pinConfigure(SW0, PIN_DIR_INPUT);
37+
attachInterrupt(SW0, button_pressed_callback, FALLING);
38+
2139
// Now we configure the power save configuration. Note that this has to be
2240
// done before calling Lte.begin().
2341
//
@@ -71,6 +89,11 @@ void loop() {
7189

7290
LowPower.powerSave();
7391

92+
if (woke_up_from_button) {
93+
Log.info("Button was pressed");
94+
woke_up_from_button = false;
95+
}
96+
7497
Log.info("Woke up!");
7598

7699
// Do work ...

src/low_power.cpp

Lines changed: 80 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -449,22 +449,28 @@ static void powerDownPeripherals(const bool keep_modem_active) {
449449
// If no comment is specified, the pin is set to input with input buffer
450450
// disabled and pull-up on
451451

452+
// On all pins along the feather, interrupts are left on for both edges in
453+
// case the user wants to wake the device up from external signals. This is
454+
// also the case for the buttons SW0 and SW1
455+
456+
// clang-format off
457+
452458
// Pin - Description - Comment
453459
// PA0 - LED0 (CELLULAR) -
454460
// PA1 - LED1 (CONNECTION) -
455461
// PA2 - LED2 (DATA) -
456462
// PA3 - LED3 (ERROR) -
457-
// PA4 - SPI0 MOSI -
458-
// PA5 - SPI0 MISO -
459-
// PA6 - SPI0 MSCK -
460-
// PA7 - CLKO -
463+
// PA4 - SPI0 MOSI (Feather) -
464+
// PA5 - SPI0 MISO (Feather) -
465+
// PA6 - SPI0 MSCK (Feather) -
466+
// PA7 - CLKO (Feather) -
461467

462468
// PB0 - USART3 TX - No pullup, measuring yields lower uA
463469
// PB1 - USART3 RX -
464470
// PB2 - LED4 (USER) -
465471
// PB3 - VOLTAGE MEASURE EN - Output, low, no pullup
466472
// PB4 - LOWQ EN - Output, low, no pullup
467-
// PB5 - SPI0 CS -
473+
// PB5 - SPI0 CS (Feather) -
468474
// PB6 - NC -
469475
// PB7 - NC -
470476

@@ -477,33 +483,35 @@ static void powerDownPeripherals(const bool keep_modem_active) {
477483
// PC6 - RING0 (modem) - Source for wake up for PSM
478484
// PC7 - RTS0 (Modem) - Has external pullup
479485

480-
// PD0 - GPIO -
481-
// PD1 - GPIO A1 -
482-
// PD2 - SW0 button -
483-
// PD3 - GPIO A2 -
484-
// PD4 - GPIO A3 -
485-
// PD5 - GPIO A4 -
486-
// PD6 - DAC A0 -
487-
// PD7 - AREF A5 -
488-
489-
// PE0 - VMUX Measure -
490-
// PE1 - GPIO D6 -
491-
// PE2 - GPIO D5 -
486+
// PD0 - GPIO D9 (Feather) -
487+
// PD1 - GPIO A1 (Feather) -
488+
// PD2 - SW0 button (Feather) -
489+
// PD3 - GPIO A2 (Feather) -
490+
// PD4 - GPIO A3 (Feather) -
491+
// PD5 - GPIO A4 (Feather) -
492+
// PD6 - DAC A0 (Feather) -
493+
// PD7 - AREF A5 (Feather) -
494+
495+
// PE0 - VMUX Measure - Not pulled up
496+
// PE1 - GPIO D6 (Feather) -
497+
// PE2 - GPIO D5 (Feather) -
492498
// PE3 - SPI0 CS (EEPROM) - Active low, so nothing extra done here
493499
// PE4 - NC -
494500
// PE5 - NC -
495501
// PE6 - NC -
496502
// PE7 - NC -
497503

498-
// PF0 - XTAL32K1 - Input buffer not disabled, no pullup
499-
// PF1 - XTAL32K2 - Input buffer not disabled, no pullup
500-
// PF2 - I2C1 SDA - Has external pullup
501-
// PF3 - I2C1 SCL - Has external pullup
502-
// PF4 - USART2 TX -
503-
// PF5 - USART2 RX -
504+
// PF0 - XTAL32K1 - Input buffer not disabled, no pullup. Is used for PIT
505+
// PF1 - XTAL32K2 - Input buffer not disabled, no pullup. Is used for PIT
506+
// PF2 - I2C1 SDA (Feather) - Has external pullup
507+
// PF3 - I2C1 SCL (Feather) - Has external pullup
508+
// PF4 - USART2 TX (Feather) -
509+
// PF5 - USART2 RX (Feather) -
504510
// PF6 - SW1 button -
505511
// PF7 - NC -
506512

513+
// clang-format on
514+
507515
PORTA.DIR = 0x00;
508516
PORTB.DIR = PIN3_bm | PIN4_bm;
509517

@@ -530,62 +538,62 @@ static void powerDownPeripherals(const bool keep_modem_active) {
530538
PORTE.OUT = 0x00;
531539
PORTF.OUT = 0x00;
532540

533-
PORTA.PIN0CTRL = 0x0C;
534-
PORTA.PIN1CTRL = 0x0C;
535-
PORTA.PIN2CTRL = 0x0C;
536-
PORTA.PIN3CTRL = 0x0C;
537-
PORTA.PIN4CTRL = 0x0C;
538-
PORTA.PIN5CTRL = 0x0C;
539-
PORTA.PIN6CTRL = 0x0C;
540-
PORTA.PIN7CTRL = 0x0C;
541-
542-
PORTB.PIN0CTRL = 0x04;
543-
PORTB.PIN1CTRL = 0x0C;
544-
PORTB.PIN2CTRL = 0x0C;
545-
PORTB.PIN3CTRL = 0x04;
546-
PORTB.PIN4CTRL = 0x04;
547-
PORTB.PIN5CTRL = 0x0C;
548-
PORTB.PIN6CTRL = 0x0C;
549-
PORTB.PIN7CTRL = 0x0C;
550-
551-
PORTC.PIN2CTRL = 0x04;
552-
PORTC.PIN3CTRL = 0x04;
541+
PORTA.PIN0CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
542+
PORTA.PIN1CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
543+
PORTA.PIN2CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
544+
PORTA.PIN3CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
545+
PORTA.PIN4CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
546+
PORTA.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
547+
PORTA.PIN6CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
548+
PORTA.PIN7CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
549+
550+
PORTB.PIN0CTRL = PORT_ISC_INPUT_DISABLE_gc;
551+
PORTB.PIN1CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
552+
PORTB.PIN2CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
553+
PORTB.PIN3CTRL = PORT_ISC_INPUT_DISABLE_gc;
554+
PORTB.PIN4CTRL = PORT_ISC_INPUT_DISABLE_gc;
555+
PORTB.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
556+
PORTB.PIN6CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
557+
PORTB.PIN7CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
558+
559+
PORTC.PIN2CTRL = PORT_ISC_INPUT_DISABLE_gc;
560+
PORTC.PIN3CTRL = PORT_ISC_INPUT_DISABLE_gc;
553561

554562
if (!keep_modem_active) {
555-
PORTC.PIN0CTRL = 0x0C;
556-
PORTC.PIN1CTRL = 0x0C;
557-
PORTC.PIN4CTRL = 0x0C;
558-
PORTC.PIN5CTRL = 0x04;
559-
PORTC.PIN6CTRL = 0x04;
560-
PORTC.PIN7CTRL = 0x04;
563+
PORTC.PIN0CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
564+
PORTC.PIN1CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
565+
PORTC.PIN4CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
566+
PORTC.PIN5CTRL = PORT_ISC_INPUT_DISABLE_gc;
567+
PORTC.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc;
568+
PORTC.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc;
561569
}
562570

563-
PORTD.PIN0CTRL = 0x0C;
564-
PORTD.PIN1CTRL = 0x0C;
565-
PORTD.PIN2CTRL = 0x0C;
566-
PORTD.PIN3CTRL = 0x0C;
567-
PORTD.PIN4CTRL = 0x0C;
568-
PORTD.PIN5CTRL = 0x0C;
569-
PORTD.PIN6CTRL = 0x0C;
570-
PORTD.PIN7CTRL = 0x0C;
571-
572-
PORTE.PIN0CTRL = 0x04;
573-
PORTE.PIN1CTRL = 0x0C;
574-
PORTE.PIN2CTRL = 0x0C;
575-
PORTE.PIN3CTRL = 0x0C;
576-
PORTE.PIN4CTRL = 0x0C;
577-
PORTE.PIN5CTRL = 0x0C;
578-
PORTE.PIN6CTRL = 0x0C;
579-
PORTE.PIN7CTRL = 0x0C;
571+
PORTD.PIN0CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
572+
PORTD.PIN1CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
573+
PORTD.PIN2CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
574+
PORTD.PIN3CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
575+
PORTD.PIN4CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
576+
PORTD.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
577+
PORTD.PIN6CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
578+
PORTD.PIN7CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
579+
580+
PORTE.PIN0CTRL = PORT_ISC_INPUT_DISABLE_gc;
581+
PORTE.PIN1CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
582+
PORTE.PIN2CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
583+
PORTE.PIN3CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
584+
PORTE.PIN4CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
585+
PORTE.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
586+
PORTE.PIN6CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
587+
PORTE.PIN7CTRL = PORT_PULLUPEN_bm | PORT_ISC_INPUT_DISABLE_gc;
580588

581589
PORTF.PIN0CTRL = 0x00;
582590
PORTF.PIN1CTRL = 0x00;
583-
PORTF.PIN2CTRL = 0x04;
584-
PORTF.PIN3CTRL = 0x04;
585-
PORTF.PIN4CTRL = 0x0C;
586-
PORTF.PIN5CTRL = 0x0C;
587-
PORTF.PIN6CTRL = 0x0C;
588-
PORTF.PIN7CTRL = 0x0C;
591+
PORTF.PIN2CTRL = PORT_ISC_BOTHEDGES_gc;
592+
PORTF.PIN3CTRL = PORT_ISC_BOTHEDGES_gc;
593+
PORTF.PIN4CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
594+
PORTF.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
595+
PORTF.PIN6CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
596+
PORTF.PIN7CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
589597
}
590598

591599
/**

0 commit comments

Comments
 (0)