This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

RTC Compare Interrupt Doesn't Happen After Debugger Halted

I am using the RTC1 compare event to generate a periodic interrupt. When I halt the bugger and and then run the interrupt doesn't occur. Looking at the RTC1 register set shows the counter value is greater than the compare register. See example pictures below. Is there are away to stop this or a work around?

image description image description image description image description

/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the license.txt file.
 */
#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"

#define BUTTON_0 (17)
#define BUTTON_1 (18)

#define LED_0 (21)
#define LED_1 (22)

#define LED_START (LED_0)
#define LED_STOP (LED_1)

#define BTN_PRESSED 0
#define BTN_RELEASED 1

int main(void)
{
    // Configure BUTTON0 as a regular input
    nrf_gpio_cfg_input(BUTTON_0, NRF_GPIO_PIN_NOPULL);
    
    // Configure BUTTON1 with SENSE enabled
    nrf_gpio_cfg_sense_input(BUTTON_1, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_LOW);
    
    // Configure the LED pins as outputs
    nrf_gpio_range_cfg_output(LED_START, LED_STOP);
    
    nrf_gpio_pin_set(LED_0);
    
    // Internal 32kHz RC
    NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos;
    
    // Start the 32 kHz clock, and wait for the start up to complete
    NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_LFCLKSTART = 1;
    while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
       
  	// Configure the RTC to generate the ISR tick 
	NRF_RTC1->PRESCALER = 0;
    
    NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk;
    NRF_RTC1->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
    NRF_RTC1->CC[0] = 256;  // interrupt at ~128Hz

    NVIC_ClearPendingIRQ(RTC1_IRQn);
    NVIC_EnableIRQ(RTC1_IRQn);

    NRF_RTC1->TASKS_START = 1;
            
    // Configure the RAM retention parameters
    NRF_POWER->RAMON = POWER_RAMON_ONRAM0_RAM0On   << POWER_RAMON_ONRAM0_Pos
                     | POWER_RAMON_ONRAM1_RAM1Off  << POWER_RAMON_ONRAM1_Pos
                     | POWER_RAMON_OFFRAM0_RAM0Off << POWER_RAMON_OFFRAM0_Pos
                     | POWER_RAMON_OFFRAM1_RAM1Off << POWER_RAMON_OFFRAM1_Pos;
    
    while(1)
    {     
      // Enter System ON sleep mode
      __WFE();  
      // Make sure any pending events are cleared
      __SEV();
      __WFE();                
    }
}

void POWER_CLOCK_IRQHandler(void)
{
  while(1);
}

void RADIO_IRQHandler(void)
{
  while(1);
}

void UART0_IRQHandler(void)
{
  while(1);
}

void RTC1_IRQHandler(void)
{
      // This handler will be run after wakeup from system ON (RTC wakeup)
    if(NRF_RTC1->EVENTS_COMPARE[0])
    {
      NRF_RTC1->EVENTS_COMPARE[0] = 0;
      NRF_RTC1->CC[0] += 256;
      nrf_gpio_pin_toggle(LED_1);
    }
}

Thanks, Darren

Parents
  • I don't think I'd call that useless. Point is RTC keeps running during debug mode (which is why processors with a SysTick function often have a flag to disable it in debug) and your code doesn't defend against being late servicing the interrupt by more than the next timer delay. 1/128 second is pretty long so the chances of missing one outside debugging is pretty low, not zero however, if you end up using the softdevice that can in worst case take about 6ms, if you have other pending interrupts, you could miss it.

    Many ways to fix it. If you need the interrupt, even late, then detect you aren't going to get it from the RTC because you missed it and just set the interrupt yourself to cause it to run again, you'll catch up soon enough. If you don't need every pulse then just ensure you add multiples of 256 until your next interrupt time is in the future (by at least 2 clock pulses).

Reply
  • I don't think I'd call that useless. Point is RTC keeps running during debug mode (which is why processors with a SysTick function often have a flag to disable it in debug) and your code doesn't defend against being late servicing the interrupt by more than the next timer delay. 1/128 second is pretty long so the chances of missing one outside debugging is pretty low, not zero however, if you end up using the softdevice that can in worst case take about 6ms, if you have other pending interrupts, you could miss it.

    Many ways to fix it. If you need the interrupt, even late, then detect you aren't going to get it from the RTC because you missed it and just set the interrupt yourself to cause it to run again, you'll catch up soon enough. If you don't need every pulse then just ensure you add multiples of 256 until your next interrupt time is in the future (by at least 2 clock pulses).

Children
Related