Skip to content

Disable SAMD21 SysTick interrupt during sleep #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

robdmob
Copy link

@robdmob robdmob commented Feb 16, 2022

This addresses a problem I experienced using an Adafruit Feather M0 where the board would suddenly refuse to wake up after a random time (4 - 12 hours). I found the solution in a discussion on the Atmel community forum: https://community.atmel.com/comment/2625116#comment-2625116.

In summary, due to a hardware bug on the SAMD21 platform it is possible for the SysTick interrupt to fire during wakeup before the RAM has had time to reinitialise which results in a hard fault. This can be prevented by disabling the SysTick interrupt before sleep and then reenabling it immediately after wakeup.

SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;  // Disable SysTick interrupt
__DSB();   // Data sync to ensure outgoing memory accesses complete
__WFI();   // Wait for interrupt (places device in sleep mode)
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;   // Enable SysTick interrupt

After making this change the board has now been running for over two weeks with no problems.

Disabling the systick timer interrupt during sleep has previously been discussed on the Microchip/Atmel community forum: https://community.atmel.com/comment/2625116#comment-2625116.

In summary, due to a hardware bug on the SAMD21, the SysTick interrupts become active before the flash has powered up from sleep, causing a hard fault after a random time. To prevent this the SysTick interrupts are disabled before entering sleep mode and enabled oncemore after the processor has woken up:

SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;  // Disable SysTick interrupts
__DSB();   // Data sync to ensure outgoing memory accesses complete
__WFI();   // Wait for interrupt (places device in sleep mode)
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;   // Enable SysTick interrupts
@CLAassistant
Copy link

CLAassistant commented Feb 16, 2022

CLA assistant check
All committers have signed the CLA.

@github-actions
Copy link

Memory usage change @ 42b760b

Board flash % RAM for global variables %
arduino:samd:arduino_zero_edbg 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkr1000 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrfox1200 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrgsm1400 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrnb1500 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrvidor4000 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1300 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1310 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrwifi1010 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrzero 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:nano_33_iot 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
Click for full report table
Board examples/Epoch
flash
% examples/Epoch
RAM for global variables
% examples/SimpleRTC
flash
% examples/SimpleRTC
RAM for global variables
% examples/SimpleRTCAlarm
flash
% examples/SimpleRTCAlarm
RAM for global variables
% examples/SleepRTCAlarm
flash
% examples/SleepRTCAlarm
RAM for global variables
%
arduino:samd:arduino_zero_edbg 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkr1000 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrfox1200 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrgsm1400 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrnb1500 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrvidor4000 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrwan1300 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrwan1310 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrwifi1010 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrzero 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:nano_33_iot 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
Click for full report CSV
Board,examples/Epoch<br>flash,%,examples/Epoch<br>RAM for global variables,%,examples/SimpleRTC<br>flash,%,examples/SimpleRTC<br>RAM for global variables,%,examples/SimpleRTCAlarm<br>flash,%,examples/SimpleRTCAlarm<br>RAM for global variables,%,examples/SleepRTCAlarm<br>flash,%,examples/SleepRTCAlarm<br>RAM for global variables,%
arduino:samd:arduino_zero_edbg,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkr1000,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrfox1200,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrgsm1400,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrnb1500,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrvidor4000,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrwan1300,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrwan1310,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrwifi1010,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrzero,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:nano_33_iot,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0

@per1234 per1234 added topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project labels Feb 17, 2022
@github-actions
Copy link

github-actions bot commented Jul 2, 2022

Memory usage change @ 89695db

Board flash % RAM for global variables %
arduino:samd:arduino_zero_edbg 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkr1000 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrfox1200 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrgsm1400 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrnb1500 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrvidor4000 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1300 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1310 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrwifi1010 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:mkrzero 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
arduino:samd:nano_33_iot 🔺 0 - +20 0.0 - +0.01 0 - 0 0.0 - 0.0
Click for full report table
Board examples/Epoch
flash
% examples/Epoch
RAM for global variables
% examples/SimpleRTC
flash
% examples/SimpleRTC
RAM for global variables
% examples/SimpleRTCAlarm
flash
% examples/SimpleRTCAlarm
RAM for global variables
% examples/SleepRTCAlarm
flash
% examples/SleepRTCAlarm
RAM for global variables
%
arduino:samd:arduino_zero_edbg 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkr1000 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrfox1200 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrgsm1400 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrnb1500 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrvidor4000 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrwan1300 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrwan1310 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrwifi1010 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:mkrzero 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
arduino:samd:nano_33_iot 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 20 0.01 0 0.0
Click for full report CSV
Board,examples/Epoch<br>flash,%,examples/Epoch<br>RAM for global variables,%,examples/SimpleRTC<br>flash,%,examples/SimpleRTC<br>RAM for global variables,%,examples/SimpleRTCAlarm<br>flash,%,examples/SimpleRTCAlarm<br>RAM for global variables,%,examples/SleepRTCAlarm<br>flash,%,examples/SleepRTCAlarm<br>RAM for global variables,%
arduino:samd:arduino_zero_edbg,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkr1000,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrfox1200,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrgsm1400,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrnb1500,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrvidor4000,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrwan1300,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrwan1310,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrwifi1010,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:mkrzero,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0
arduino:samd:nano_33_iot,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,20,0.01,0,0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants