Skip to content
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

Fix mainloop to not lose interrupts #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <unistd.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/cm3/cortex.h>
#include <libopencm3/stm32/dbgmcu.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/stm32/flash.h>
Expand Down Expand Up @@ -61,6 +62,8 @@ static void gpio_setup(void)

void BUTTON_DISCO_USER_isr(void)
{
state.interrupted = true;

exti_reset_request(BUTTON_DISCO_USER_EXTI);
state.pressed = true;
if (state.falling) {
Expand All @@ -76,6 +79,8 @@ void BUTTON_DISCO_USER_isr(void)

static void setup_buttons(void)
{
state.interrupted = true;

/* Enable EXTI0 interrupt. */
nvic_enable_irq(BUTTON_DISCO_USER_NVIC);

Expand Down Expand Up @@ -227,6 +232,8 @@ static int setup_rtc_wakeup(int period)

void rtc_wkup_isr(void)
{
state.interrupted = true;

/* clear flag, not write protected */
RTC_ISR &= ~(RTC_ISR_WUTF);
exti_reset_request(EXTI20);
Expand All @@ -235,6 +242,12 @@ void rtc_wkup_isr(void)

static int process_state(volatile struct state_t *st)
{
st->interrupted = false;
/*
* TODO: protect against another interrupt arriving while we're in
* here, preferably not by disabling all interrupt processing.
*/

if (st->rtc_ticked) {
st->rtc_ticked = 0;
printf("Tick: %x\n", (unsigned int) RTC_TR);
Expand Down Expand Up @@ -294,12 +307,22 @@ int main(void)
setup_rtc();
setup_rtc_wakeup(1);

/*
* This example demonstrates how to safely handle interrupts in the
* main loop: we want to avoid a deadlock when the interrupt is
* serviced just before the WFI.
*/
while (1) {
PWR_CR |= PWR_CR_LPSDSR;
pwr_set_stop_mode();
__WFI();
reset_clocks();
process_state(&state);
cm_disable_interrupts();
if(state.interrupted) {
cm_enable_interrupts();
process_state(&state);
} else {
PWR_CR |= PWR_CR_LPSDSR;
pwr_set_stop_mode();
__WFI();
reset_clocks();
}
}

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extern "C" {
#define TIMER_BUTTON_PRESS_RST RST_TIM7

struct state_t {
bool interrupted;
bool falling;
bool pressed;
int rtc_ticked;
Expand Down