Skip to content

Commit 9e16487

Browse files
authored
Merge pull request #6048 from t-ikegami/atmel-samd-fix-alarm
Fix atmel-samd alarm module
2 parents f32b588 + 6499d18 commit 9e16487

File tree

8 files changed

+372
-370
lines changed

8 files changed

+372
-370
lines changed

ports/atmel-samd/boards/seeeduino_wio_terminal/board.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ uint8_t display_init_sequence[] = {
5151
0xc1, 0x01, 0x10, // Power control SAP[2:0];BT[3:0]
5252
0xc5, 0x02, 0x3e, 0x28, // VCM control
5353
0xc7, 0x01, 0x86, // VCM control2
54-
0x36, 0x01, 0x38, // Memory Access Control
54+
0x36, 0x01, 0xe8, // Memory Access Control
5555
0x37, 0x01, 0x00, // Vertical scroll zero
5656
0x3a, 0x01, 0x55, // COLMOD: Pixel Format Set
5757
0xb1, 0x02, 0x00, 0x18, // Frame Rate Control (In Normal Mode/Full Colors)
@@ -88,7 +88,7 @@ void board_init(void) {
8888
240, // Height
8989
0, // column start
9090
0, // row start
91-
180, // rotation
91+
0, // rotation
9292
16, // Color depth
9393
false, // Grayscale
9494
false, // pixels in a byte share a row. Only valid for depths < 8
@@ -129,6 +129,8 @@ void board_init(void) {
129129
common_hal_digitalio_digitalinout_never_reset(&CTR_3V3);
130130
common_hal_digitalio_digitalinout_never_reset(&USB_HOST_ENABLE);
131131

132+
// reset pin after fake deep sleep
133+
reset_pin_number(pin_PA18.number);
132134
}
133135

134136
bool board_requests_safe_mode(void) {
@@ -139,4 +141,12 @@ void reset_board(void) {
139141
}
140142

141143
void board_deinit(void) {
144+
common_hal_displayio_release_displays();
145+
common_hal_digitalio_digitalinout_deinit(&CTR_5V);
146+
common_hal_digitalio_digitalinout_deinit(&CTR_3V3);
147+
common_hal_digitalio_digitalinout_deinit(&USB_HOST_ENABLE);
148+
149+
// Turn off RTL8720DN before the deep sleep.
150+
// Pin state is kept during BACKUP sleep.
151+
gpio_set_pin_direction(pin_PA18.number, GPIO_DIRECTION_OUT);
142152
}

ports/atmel-samd/common-hal/alarm/SleepMemory.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,24 @@
3232
#include "shared-bindings/nvm/ByteArray.h"
3333

3434
void alarm_sleep_memory_reset(void) {
35-
3635
}
3736

3837
uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self) {
39-
mp_raise_NotImplementedError(translate("Sleep Memory not available"));
40-
return 0;
38+
return BKUPRAM_SIZE;
4139
}
4240

4341
bool common_hal_alarm_sleep_memory_set_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, const uint8_t *values, uint32_t len) {
44-
mp_raise_NotImplementedError(translate("Sleep Memory not available"));
45-
return false;
42+
if (start_index + len > BKUPRAM_SIZE) {
43+
return false;
44+
}
45+
memcpy((uint8_t *)(BKUPRAM_ADDR + start_index), values, len);
46+
return true;
4647
}
4748

4849
void common_hal_alarm_sleep_memory_get_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, uint8_t *values, uint32_t len) {
49-
mp_raise_NotImplementedError(translate("Sleep Memory not available"));
50+
if (start_index + len > BKUPRAM_SIZE) {
51+
return;
52+
}
53+
memcpy(values, (uint8_t *)(BKUPRAM_ADDR + start_index), len);
5054
return;
5155
}

ports/atmel-samd/common-hal/alarm/SleepMemory.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
typedef struct {
3333
mp_obj_base_t base;
34-
uint8_t *start_address;
35-
uint8_t len;
3634
} alarm_sleep_memory_obj_t;
3735

3836
extern void alarm_sleep_memory_reset(void);

ports/atmel-samd/common-hal/alarm/__init__.c

Lines changed: 66 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -39,62 +39,50 @@
3939
#include "supervisor/port.h"
4040
#include "supervisor/workflow.h"
4141

42+
STATIC uint32_t TAMPID = 0;
43+
4244
// Singleton instance of SleepMemory.
4345
const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = {
4446
.base = {
4547
.type = &alarm_sleep_memory_type,
4648
},
4749
};
48-
// TODO: make a custom enum to avoid weird values like PM_SLEEPCFG_SLEEPMODE_BACKUP_Val?
49-
STATIC volatile uint32_t _target;
50-
STATIC bool fake_sleep;
51-
STATIC bool pin_wake;
5250

5351
void alarm_reset(void) {
5452
// Reset the alarm flag
55-
SAMD_ALARM_FLAG = 0x00;
5653
alarm_pin_pinalarm_reset();
5754
alarm_time_timealarm_reset();
5855
}
5956

60-
samd_sleep_source_t alarm_get_wakeup_cause(void) {
61-
// If in light/fake sleep, check modules
62-
if (alarm_pin_pinalarm_woke_this_cycle()) {
63-
return SAMD_WAKEUP_GPIO;
64-
}
65-
if (alarm_time_timealarm_woke_this_cycle()) {
66-
return SAMD_WAKEUP_RTC;
67-
}
68-
if (!fake_sleep && RSTC->RCAUSE.bit.BACKUP) {
69-
// This is checked during rtc_init to cache TAMPID if necessary
70-
if (pin_wake || RTC->MODE0.TAMPID.reg) {
71-
pin_wake = true;
72-
return SAMD_WAKEUP_GPIO;
73-
}
74-
return SAMD_WAKEUP_RTC;
75-
}
76-
return SAMD_WAKEUP_UNDEF;
57+
void alarm_get_wakeup_cause(void) {
58+
// Called from rtc_init, just before SWRST of RTC. It is called
59+
// at an early stage of main(), to save TAMPID from SWRST. Later,
60+
// common_hal_alarm_create_wake_alarm is called to make a wakeup
61+
// alarm from the deep sleep.
62+
63+
TAMPID = RTC->MODE0.TAMPID.reg;
7764
}
7865

7966
bool common_hal_alarm_woken_from_sleep(void) {
80-
return alarm_get_wakeup_cause() != SAMD_WAKEUP_UNDEF;
67+
return alarm_pin_pinalarm_woke_this_cycle() || alarm_time_timealarm_woke_this_cycle();
8168
}
8269

8370
mp_obj_t common_hal_alarm_create_wake_alarm(void) {
84-
// If woken from deep sleep, create a copy alarm similar to what would have
85-
// been passed in originally. Otherwise, just return none
86-
samd_sleep_source_t cause = alarm_get_wakeup_cause();
87-
switch (cause) {
88-
case SAMD_WAKEUP_RTC: {
89-
return alarm_time_timealarm_create_wakeup_alarm();
90-
}
91-
case SAMD_WAKEUP_GPIO: {
92-
return alarm_pin_pinalarm_create_wakeup_alarm();
93-
}
94-
case SAMD_WAKEUP_UNDEF:
95-
default:
96-
// Not a deep sleep reset.
97-
break;
71+
// Called from main.c on the first start up, just before alarm_reset.
72+
// Return a copy of wakeup alarm from deep sleep / fake deep sleep.
73+
// In case of fake sleep, status should be left in TimeAlarm/PinAlarm.
74+
bool true_deep = RSTC->RCAUSE.bit.BACKUP;
75+
76+
if (alarm_pin_pinalarm_woke_this_cycle()) {
77+
TAMPID = RTC->MODE0.TAMPID.reg;
78+
RTC->MODE0.TAMPID.reg = TAMPID; // clear register
79+
return alarm_pin_pinalarm_create_wakeup_alarm(TAMPID);
80+
}
81+
if (alarm_time_timealarm_woke_this_cycle() || (true_deep && TAMPID == 0)) {
82+
return alarm_time_timealarm_create_wakeup_alarm();
83+
}
84+
if (true_deep) {
85+
return alarm_pin_pinalarm_create_wakeup_alarm(TAMPID);
9886
}
9987
return mp_const_none;
10088
}
@@ -103,64 +91,54 @@ mp_obj_t common_hal_alarm_create_wake_alarm(void) {
10391
STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) {
10492
alarm_pin_pinalarm_set_alarms(deep_sleep, n_alarms, alarms);
10593
alarm_time_timealarm_set_alarms(deep_sleep, n_alarms, alarms);
106-
fake_sleep = false;
10794
}
10895

10996
mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) {
11097
_setup_sleep_alarms(false, n_alarms, alarms);
11198
mp_obj_t wake_alarm = mp_const_none;
11299

100+
// This works but achieves same power consumption as time.sleep()
101+
PM->SLEEPCFG.reg = PM_SLEEPCFG_SLEEPMODE_STANDBY;
102+
while (PM->SLEEPCFG.bit.SLEEPMODE != PM_SLEEPCFG_SLEEPMODE_STANDBY_Val) {
103+
}
104+
// STDBYCFG is left to be 0 to retain SYSRAM. Note that, even if
105+
// RAMCFG_OFF is set here, SYSRAM seems to be retained, probably
106+
// because RTC and/or USB keeps sleepwalking.
107+
113108
while (!mp_hal_is_interrupted()) {
114109
RUN_BACKGROUND_TASKS;
115110
// Detect if interrupt was alarm or ctrl-C interrupt.
116-
if (common_hal_alarm_woken_from_sleep()) {
117-
samd_sleep_source_t cause = alarm_get_wakeup_cause();
118-
switch (cause) {
119-
case SAMD_WAKEUP_RTC: {
120-
wake_alarm = alarm_time_timealarm_find_triggered_alarm(n_alarms,alarms);
121-
break;
122-
}
123-
case SAMD_WAKEUP_GPIO: {
124-
wake_alarm = alarm_pin_pinalarm_find_triggered_alarm(n_alarms,alarms);
125-
break;
126-
}
127-
default:
128-
// Should not reach this, if all light sleep types are covered correctly
129-
break;
130-
}
131-
shared_alarm_save_wake_alarm(wake_alarm);
111+
if (alarm_time_timealarm_woke_this_cycle()) {
112+
wake_alarm = alarm_time_timealarm_find_triggered_alarm(n_alarms,alarms);
113+
break;
114+
}
115+
if (alarm_pin_pinalarm_woke_this_cycle()) {
116+
wake_alarm = alarm_pin_pinalarm_find_triggered_alarm(n_alarms,alarms);
132117
break;
133118
}
134-
// ATTEMPT ------------------------------
135-
// This works but achieves same power consumption as time.sleep()
136119

137120
// Clear the FPU interrupt because it can prevent us from sleeping.
138121
if (__get_FPSCR() & ~(0x9f)) {
139122
__set_FPSCR(__get_FPSCR() & ~(0x9f));
140123
(void)__get_FPSCR();
141124
}
142125

143-
// Disable RTC interrupts
144-
NVIC_DisableIRQ(RTC_IRQn);
145-
// Set standby power domain stuff
146-
PM->STDBYCFG.reg = PM_STDBYCFG_RAMCFG_OFF;
147-
// Set-up Sleep Mode
148-
PM->SLEEPCFG.reg = PM_SLEEPCFG_SLEEPMODE_STANDBY;
149-
while (PM->SLEEPCFG.bit.SLEEPMODE != PM_SLEEPCFG_SLEEPMODE_STANDBY_Val) {
150-
;
151-
}
152-
126+
common_hal_mcu_disable_interrupts();
153127
__DSB(); // Data Synchronization Barrier
154128
__WFI(); // Wait For Interrupt
155-
// Enable RTC interrupts
156-
NVIC_EnableIRQ(RTC_IRQn);
157-
// END ATTEMPT ------------------------------
129+
common_hal_mcu_enable_interrupts();
158130
}
131+
// Restore SLEEPCFG or port_idle_until_interrupt sleeps in STANDBY mode.
132+
PM->SLEEPCFG.reg = PM_SLEEPCFG_SLEEPMODE_IDLE2;
133+
while (PM->SLEEPCFG.bit.SLEEPMODE != PM_SLEEPCFG_SLEEPMODE_IDLE2_Val) {
134+
}
135+
alarm_pin_pinalarm_deinit_alarms(n_alarms, alarms); // after care for alarm_pin_pinalarm_set_alarms
136+
alarm_reset();
137+
159138
if (mp_hal_is_interrupted()) {
160139
return mp_const_none; // Shouldn't be given to python code because exception handling should kick in.
161140
}
162141

163-
alarm_reset();
164142
return wake_alarm;
165143
}
166144

@@ -171,69 +149,49 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
171149
void NORETURN common_hal_alarm_enter_deep_sleep(void) {
172150
alarm_pin_pinalarm_prepare_for_deep_sleep();
173151
alarm_time_timealarm_prepare_for_deep_sleep();
174-
_target = RTC->MODE0.COMP[1].reg;
175-
port_disable_tick(); // TODO: Required for SAMD?
152+
// port_disable_tick(); // TODO: Required for SAMD?
176153

177-
// cache alarm flag since backup registers about to be reset
178-
uint32_t _SAMD_ALARM_FLAG = SAMD_ALARM_FLAG;
154+
// cache alarm flag and etc since RTC about to be reset
155+
uint32_t _flag = SAMD_ALARM_FLAG; // RTC->MODE0.BKUP[0].reg
156+
uint32_t _target = RTC->MODE0.COMP[1].reg;
157+
uint32_t _tampctrl = RTC->MODE0.TAMPCTRL.reg;
179158

180159
// Clear the FPU interrupt because it can prevent us from sleeping.
181160
if (__get_FPSCR() & ~(0x9f)) {
182161
__set_FPSCR(__get_FPSCR() & ~(0x9f));
183162
(void)__get_FPSCR();
184163
}
185164

186-
NVIC_DisableIRQ(RTC_IRQn);
165+
common_hal_mcu_disable_interrupts();
187166
// Must disable the RTC before writing to EVCTRL and TMPCTRL
188-
RTC->MODE0.CTRLA.bit.ENABLE = 0; // Disable the RTC
189-
while (RTC->MODE0.SYNCBUSY.bit.ENABLE) { // Wait for synchronization
190-
;
191-
}
192167
RTC->MODE0.CTRLA.bit.SWRST = 1; // Software reset the RTC
193168
while (RTC->MODE0.SYNCBUSY.bit.SWRST) { // Wait for synchronization
194-
;
195169
}
196170
RTC->MODE0.CTRLA.reg = RTC_MODE0_CTRLA_PRESCALER_DIV1024 | // Set prescaler to 1024
197171
RTC_MODE0_CTRLA_MODE_COUNT32; // Set RTC to mode 0, 32-bit timer
198172

173+
SAMD_ALARM_FLAG = _flag;
199174
// Check if we're setting TimeAlarm
200-
if (_SAMD_ALARM_FLAG & SAMD_ALARM_FLAG_TIME) {
201-
RTC->MODE0.COMP[1].reg = (_target / 1024) * 32;
175+
if (SAMD_ALARM_FLAG_TIME_CHK) {
176+
RTC->MODE0.COMP[1].reg = _target;
202177
while (RTC->MODE0.SYNCBUSY.reg) {
203-
;
204178
}
205-
}
206-
// Check if we're setting PinAlarm
207-
if (_SAMD_ALARM_FLAG & SAMD_ALARM_FLAG_PIN) {
208-
RTC->MODE0.TAMPCTRL.bit.DEBNC2 = 1; // Edge triggered when INn is stable for 4 CLK_RTC_DEB periods
209-
RTC->MODE0.TAMPCTRL.bit.TAMLVL2 = 1; // rising edge
210-
// PA02 = IN2
211-
RTC->MODE0.TAMPCTRL.bit.IN2ACT = 1; // WAKE on IN2 (doesn't save timestamp)
212-
}
213-
// Enable interrupts
214-
NVIC_SetPriority(RTC_IRQn, 0);
215-
NVIC_EnableIRQ(RTC_IRQn);
216-
if (_SAMD_ALARM_FLAG & SAMD_ALARM_FLAG_TIME) {
217-
// Set interrupts for COMPARE1
218179
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_CMP1;
219180
}
220-
if (_SAMD_ALARM_FLAG & SAMD_ALARM_FLAG_PIN) {
221-
// Set interrupts for TAMPER pins
181+
// Check if we're setting PinAlarm
182+
if (SAMD_ALARM_FLAG_PIN_CHK) {
183+
RTC->MODE0.TAMPCTRL.reg = _tampctrl;
222184
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_TAMPER;
223185
}
186+
// Enable interrupts
187+
common_hal_mcu_enable_interrupts();
224188

225-
// Set-up Deep Sleep Mode & RAM retention
226-
PM->BKUPCFG.reg = PM_BKUPCFG_BRAMCFG(0x2); // No RAM retention 0x2 partial:0x1
227-
while (PM->BKUPCFG.bit.BRAMCFG != 0x2) { // Wait for synchronization
228-
;
229-
}
189+
// Set-up Deep Sleep Mode with backup RAM retention
230190
PM->SLEEPCFG.reg = PM_SLEEPCFG_SLEEPMODE_BACKUP;
231191
while (PM->SLEEPCFG.bit.SLEEPMODE != PM_SLEEPCFG_SLEEPMODE_BACKUP_Val) {
232-
;
233192
}
234193
RTC->MODE0.CTRLA.bit.ENABLE = 1; // Enable the RTC
235194
while (RTC->MODE0.SYNCBUSY.bit.ENABLE) { // Wait for synchronization
236-
;
237195
}
238196

239197
__DSB(); // Data Synchronization Barrier
@@ -245,16 +203,9 @@ void NORETURN common_hal_alarm_enter_deep_sleep(void) {
245203
}
246204
}
247205

248-
void common_hal_alarm_pretending_deep_sleep(void) {
249-
// TODO:
250-
// If tamper detect interrupts cannot be used to wake from the Idle tier of sleep,
251-
// This section will need to re-initialize the pins to allow the PORT peripheral
252-
// to generate external interrupts again. See STM32 for reference.
253-
254-
if (!fake_sleep) {
255-
fake_sleep = true;
256-
}
257-
}
206+
// Default common_hal_alarm_pretending_deep_sleep is defined in
207+
// shared-bindings, which is used here. Note that "pretending" does
208+
// not work on REPL; it only works for main.py (or code.py, ...).
258209

259210
void common_hal_alarm_gc_collect(void) {
260211
gc_collect_ptr(shared_alarm_get_wake_alarm());

ports/atmel-samd/common-hal/alarm/__init__.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ extern const alarm_sleep_memory_obj_t alarm_sleep_memory_obj;
3737
#define SAMD_ALARM_FLAG (RTC->MODE0.BKUP[0].reg)
3838
#define SAMD_ALARM_FLAG_TIME (_U_(0x1) << 0)
3939
#define SAMD_ALARM_FLAG_PIN (_U_(0x1) << 1)
40+
41+
#define SAMD_ALARM_FLAG_TIME_SET (SAMD_ALARM_FLAG |= SAMD_ALARM_FLAG_TIME)
42+
#define SAMD_ALARM_FLAG_TIME_CLR (SAMD_ALARM_FLAG &= ~SAMD_ALARM_FLAG_TIME)
43+
#define SAMD_ALARM_FLAG_TIME_CHK (SAMD_ALARM_FLAG & SAMD_ALARM_FLAG_TIME)
44+
45+
#define SAMD_ALARM_FLAG_PIN_SET (SAMD_ALARM_FLAG |= SAMD_ALARM_FLAG_PIN)
46+
#define SAMD_ALARM_FLAG_PIN_CLR (SAMD_ALARM_FLAG &= ~SAMD_ALARM_FLAG_PIN)
47+
#define SAMD_ALARM_FLAG_PIN_CHK (SAMD_ALARM_FLAG & SAMD_ALARM_FLAG_PIN)
4048
#endif
4149

4250
typedef enum {
@@ -46,7 +54,7 @@ typedef enum {
4654
} samd_sleep_source_t;
4755

4856
extern void alarm_set_wakeup_reason(samd_sleep_source_t reason);
49-
samd_sleep_source_t alarm_get_wakeup_cause(void);
57+
void alarm_get_wakeup_cause(void);
5058
extern void alarm_reset(void);
5159

5260
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM__INIT__H

0 commit comments

Comments
 (0)