From ea810bc26ae36f9db352662961154d842a777630 Mon Sep 17 00:00:00 2001 From: sommersoft Date: Mon, 2 Jul 2018 19:03:08 -0500 Subject: [PATCH 1/2] added EIC->EXTINT handling; disabled PRIMASK instructions --- src/mymodule.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/mymodule.c b/src/mymodule.c index bd84a10..c285b57 100644 --- a/src/mymodule.c +++ b/src/mymodule.c @@ -1,6 +1,7 @@ #include "py/obj.h" #include "py/runtime.h" #include "samd/external_interrupts.h" +#include "samd/pins.h" #define SET_BIT(REG, BIT) ((REG) |= (BIT)) @@ -8,6 +9,7 @@ #define READ_BIT(REG, BIT) ((REG) & (BIT)) STATIC mp_obj_t mymodule_deep_sleep(void) { + /* // store the original value for global interrupt enabling uint32_t original_PRIMASK_value; original_PRIMASK_value = __get_PRIMASK(); @@ -19,8 +21,30 @@ STATIC mp_obj_t mymodule_deep_sleep(void) { NVIC_EnableIRQ(PIN_PA19); // TBD: Interrupt priority level???? __set_PRIMASK(original_PRIMASK_value); + */ // Now ... EIC...rev your engine... turn_on_external_interrupt_controller(); + + // Set the PMUX pin funciton to "EIC" + gpio_set_pin_function(&PIN_PA19->pin, GPIO_PIN_FUNCTION_A); + + // Enable the EIC IRQ on the NVIC + turn_on_cpu_interrupt(&PIN_PA19->extint_channel); + + // Configure the EIC to trigger an interrupt on HIGH. I hand-rolled this vs using + // turn_on_eic_channel() since we're setting an extra config (WAKEUP) and are not + // interested in setting the channel_handler (EIC_Handler). + uint8_t config_index = &PIN_PA19->extint_channel / 8; + uint8_t position = (&PIN_PA19->extint_channel % 8) * 4; + uint32_t sense_setting = EIC_CONFIG_SENSE0_HIGH; + // The following instructions will clear any existing register bits. + // Use a mask if you don't want to clear them. + // See the SAMD21 datasheet on Sleep Mode Operation (paragraph 21.6.8, page 310); + // recommends to use both WAKEUPEN and INTENSET. + EIC->CONFIG[config_index].reg = sense_setting << position; + EIC->WAKEUPEN.reg = &PIN_PA19->extint_channel << EIC_WAKEUP_WAKEUPEN_Pos; + EIC->INTENSET.reg = &PIN_PA19->extint_channel << EIC_INTENSET_EXTINT_Pos; + // Go to sleep. SET_BIT(SCB->SCR,2); __WFI(); From cdc4d1e731e77fbd8eac9fd3f474e22b46fcd519 Mon Sep 17 00:00:00 2001 From: sommersoft Date: Mon, 2 Jul 2018 23:46:25 -0500 Subject: [PATCH 2/2] forgot to include a header --- src/mymodule.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mymodule.c b/src/mymodule.c index c285b57..9fc7293 100644 --- a/src/mymodule.c +++ b/src/mymodule.c @@ -2,6 +2,7 @@ #include "py/runtime.h" #include "samd/external_interrupts.h" #include "samd/pins.h" +#include "hal/include/hal_gpio.h" #define SET_BIT(REG, BIT) ((REG) |= (BIT))