Skip to content

Crash on ISR #52

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

Closed
d00616 opened this issue Nov 5, 2016 · 7 comments
Closed

Crash on ISR #52

d00616 opened this issue Nov 5, 2016 · 7 comments

Comments

@d00616
Copy link
Contributor

d00616 commented Nov 5, 2016

This code is hanging after first timer/rtc event on NRF51 and NRF52. I think this code is correct.

//#define TIMER
#define RTC

#include "nrf.h"

uint16_t counter = 0;

void setup() {
  Serial.begin(38400);

  NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  NRF_CLOCK->TASKS_HFCLKSTART = 1;

  while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {}

  NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;
  NRF_TIMER2->PRESCALER = 2;

  #ifdef TIMER
    NVIC_SetPriority(TIMER2_IRQn, 15);
    NVIC_ClearPendingIRQ(TIMER2_IRQn);
    NVIC_EnableIRQ(TIMER2_IRQn);
  #endif
  NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
  NRF_TIMER2->CC[0] = 4000;
  NRF_TIMER2->INTENSET = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos;
  NRF_TIMER2->TASKS_START = 1;

  #ifdef RTC
    NVIC_SetPriority(RTC0_IRQn, 15);
    NVIC_ClearPendingIRQ(RTC0_IRQn);
    NVIC_EnableIRQ(RTC0_IRQn);
  #endif
  NRF_RTC0->PRESCALER = 32;
  NRF_RTC0->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
  NRF_RTC0->INTENSET = RTC_INTENSET_COMPARE0_Msk;
  NRF_RTC0->CC[0] = 2000;
  NRF_RTC0->TASKS_START = 1;

}

void loop() {
  Serial.print("Counter: ");
  Serial.println(counter);
  delay(300);
}

void TIMER_2_ISR(void)
{
  counter++;
  NRF_TIMER2->EVENTS_COMPARE[0] = 0;
#if __CORTEX_M == 0x04
    volatile uint32_t dummy = NRF_TIMER2->EVENTS_COMPARE[0];
    (void)dummy;
#endif
}

void RTC0_IRQHandler(void)
{
  counter++;
  NRF_RTC0->EVENTS_COMPARE[0] = 0;

#if __CORTEX_M == 0x04
    volatile uint32_t dummy = NRF_RTC0->EVENTS_COMPARE[0];
    (void)dummy;
#endif
}
@sandeepmistry
Copy link
Owner

@d00616

Are you sure it's crashing?

I think you are missing the extern "C" clauses around the ISR's:

extern "C" {

void TIMER_2_ISR(void)
{
  counter++;
  NRF_TIMER2->EVENTS_COMPARE[0] = 0;
#if __CORTEX_M == 0x04
    volatile uint32_t dummy = NRF_TIMER2->EVENTS_COMPARE[0];
    (void)dummy;
#endif
}

void RTC0_IRQHandler(void)
{
  counter++;
  NRF_RTC0->EVENTS_COMPARE[0] = 0;

#if __CORTEX_M == 0x04
    volatile uint32_t dummy = NRF_RTC0->EVENTS_COMPARE[0];
    (void)dummy;
#endif
}

}

@d00616
Copy link
Contributor Author

d00616 commented Nov 7, 2016

Hello @sandeepmistry,

thank you for the hint. At the moment there is a bug whith 'extern "C"' -> arduino/arduino-builder#128

'Extern "C"' is only working with all code in one line.

Thank you.

@d00616 d00616 closed this as completed Nov 7, 2016
@cheeta1
Copy link

cheeta1 commented May 10, 2017

hi @d00616 have you been able to run the above code for timer 2 interrupt on nrf51822? if yes then can you share the code?

@d00616
Copy link
Contributor Author

d00616 commented May 10, 2017

@cheeta1 You have to put all interrupt code into one line:

// This code is public domain

#include <nrf.h>

#define RTC NRF_RTC0
#define RTC_IRQ RTC0_IRQn

int interrupt = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Start");

  // Configure RTC
  RTC->TASKS_STOP = 1;
  RTC->PRESCALER = 31; //1024Hz frequency
  RTC->CC[0] = RTC->COUNTER + (3 * 1024);
  RTC->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
  RTC->INTENSET = RTC_INTENSET_COMPARE0_Msk;
  RTC->TASKS_START = 1;
  RTC->EVENTS_COMPARE[0] = 0;

  // Enable interrupt
  NVIC_SetPriority(RTC_IRQ, 15);
  NVIC_ClearPendingIRQ(RTC_IRQ);
  NVIC_EnableIRQ(RTC_IRQ);
}

void loop() {
  Serial.print(millis());
  Serial.print(" ");
  Serial.print(RTC->COUNTER);
  Serial.print(" ");
  Serial.println(interrupt);
  delay(1000);
}

/**
 * Reset events and read back on nRF52
 * http://infocenter.nordicsemi.com/pdf/nRF52_Series_Migration_v1.0.pdf
 */
#if __CORTEX_M == 0x04
#define NRF5_RESET_EVENT(event)                                                 \
        event = 0;                                                                   \
        (void)event
#else
#define NRF5_RESET_EVENT(event) event = 0
#endif

// This must be in one line
extern "C" { void RTC0_IRQHandler(void) { NRF5_RESET_EVENT(RTC->EVENTS_COMPARE[0]); interrupt++; RTC->TASKS_CLEAR = 1; }}

@cheeta1
Copy link

cheeta1 commented May 11, 2017

image

@cheeta1
Copy link

cheeta1 commented May 11, 2017

hi @d00616 i got the above output at serial which shows that ISR is not called. can you tell mw why the ISR is not being called?

@d00616
Copy link
Contributor Author

d00616 commented May 13, 2017

I have updated the example code. On NRF51 NVIC_EnableIRQ is required. The NRF5_RESET_EVENT helps to handle resetting the event on NRF51 and NRF52.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants