Skip to content

Commit e159ecf

Browse files
committed
[WIP] initial API design
samd basic functions ported
1 parent e549d1e commit e159ecf

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

keywords.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#######################################
2+
# Syntax Coloring Map For Energy Saving
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
ArduinoLowPower KEYWORD1
10+
LowPower KEYWORD1
11+
12+
#######################################
13+
# Methods and Functions (KEYWORD2)
14+
#######################################
15+
16+
idle KEYWORD2
17+
sleep KEYWORD2
18+
deepSleep KEYWORD2
19+
20+
#######################################
21+
# Constants (LITERAL1)
22+
#######################################

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Arduino Low Power
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino LLC
5+
sentence=Power save primitives features for 32 bit boards
6+
paragraph=With this library you can manage the low power states of newer Arduino boards
7+
category=Device Control
8+
url=http://arduino.cc/libraries/ArduinoLowPower
9+
architectures=samd,arc32

src/ArduinoLowPower.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "ArduinoLowPower.h"
2+
3+
#ifdef ARDUINO_ARCH_SAMD
4+
5+
#include "WInterrupts.h"
6+
7+
void ArduinoLowPowerClass::idle() {
8+
SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
9+
PM->SLEEP.reg = 2;
10+
__DSB();
11+
__WFI();
12+
}
13+
14+
void ArduinoLowPowerClass::idle(uint32_t millis) {
15+
setAlarmIn(millis);
16+
idle();
17+
}
18+
19+
void ArduinoLowPowerClass::sleep() {
20+
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
21+
__DSB();
22+
__WFI();
23+
}
24+
25+
void ArduinoLowPowerClass::sleep(uint32_t millis) {
26+
setAlarmIn(millis);
27+
sleep();
28+
}
29+
30+
void ArduinoLowPowerClass::deepSleep() {
31+
sleep();
32+
}
33+
34+
void ArduinoLowPowerClass::deepSleep(uint32_t millis) {
35+
sleep(millis);
36+
}
37+
38+
void ArduinoLowPowerClass::setAlarmIn(uint32_t millis) {
39+
40+
if (!rtc.isConfigured()) {
41+
attachInterruptWakeup(RTC_ALARM_WAKEUP, NULL, 0);
42+
}
43+
44+
uint32_t now = rtc.getEpoch();
45+
rtc.setAlarmEpoch(now + millis/1000);
46+
rtc.enableAlarm(rtc.MATCH_YYMMDDHHMMSS);
47+
}
48+
49+
void ArduinoLowPowerClass::attachInterruptWakeup(uint32_t pin, voidFuncPtr callback, uint32_t mode) {
50+
51+
if (pin > PINS_COUNT) {
52+
// check for external wakeup sources
53+
// only enables the wakeup bit, no callback
54+
// RTC library should call this API to enable the alarm subsystem
55+
switch (pin) {
56+
case RTC_ALARM_WAKEUP:
57+
rtc.begin();
58+
rtc.attachInterrupt(callback);
59+
/*case UART_WAKEUP:*/
60+
}
61+
return;
62+
}
63+
64+
EExt_Interrupts in = g_APinDescription[pin].ulExtInt;
65+
if (in == NOT_AN_INTERRUPT || in == EXTERNAL_INT_NMI)
66+
return;
67+
68+
attachInterrupt(pin, callback, mode);
69+
70+
#if 0
71+
// enable EIC clock
72+
GCLK->CLKCTRL.bit.CLKEN = 0; //disable GCLK module
73+
while (GCLK->STATUS.bit.SYNCBUSY);
74+
75+
GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK6 | GCLK_CLKCTRL_ID( GCM_EIC )) ; //EIC clock switched on GCLK6
76+
while (GCLK->STATUS.bit.SYNCBUSY);
77+
78+
GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(6)); //source for GCLK6 is OSCULP32K
79+
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);
80+
81+
GCLK->GENCTRL.bit.RUNSTDBY = 1; //GCLK6 run standby
82+
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);
83+
#endif
84+
85+
// Enable wakeup capability on pin in case being used during sleep
86+
EIC->WAKEUP.reg |= (1 << in);
87+
88+
/* Errata: Make sure that the Flash does not power all the way down
89+
* when in sleep mode. */
90+
NVMCTRL->CTRLB.bit.SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val;
91+
}
92+
93+
#endif
94+
95+
ArduinoLowPowerClass LowPower;

src/ArduinoLowPower.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef _ARDUINO_LOW_POWER_H_
2+
#define _ARDUINO_LOW_POWER_H_
3+
4+
#include <Arduino.h>
5+
6+
#ifdef ARDUINO_ARCH_SAMD
7+
#include "RTCZero.h"
8+
#endif
9+
10+
//typedef void (*voidFuncPtr)( void ) ;
11+
12+
class ArduinoLowPowerClass {
13+
public:
14+
void idle(void);
15+
void idle(uint32_t millis);
16+
17+
void sleep(void);
18+
void sleep(uint32_t millis);
19+
20+
void deepSleep(void);
21+
void deepSleep(uint32_t millis);
22+
23+
void attachInterruptWakeup(uint32_t pin, voidFuncPtr callback, uint32_t mode);
24+
25+
private:
26+
#ifdef ARDUINO_ARCH_SAMD
27+
void setAlarmIn(uint32_t millis);
28+
RTCZero rtc;
29+
#define RTC_ALARM_WAKEUP 0xFF
30+
#endif
31+
};
32+
33+
extern ArduinoLowPowerClass LowPower;
34+
35+
#endif

0 commit comments

Comments
 (0)