Avoiding Boron timer conflicts

Which of the nrf52840 hardware timers does DeviceOS use?

I want to use a timer for a precisely timed interrupt, but I don’t want to conflict with the rest of the system.

I tried to do it anyways with Timer4, and it gives me a flashing red light that I think translates hard fault

The code I’m trying to use is copied almost directly from the nrf52 timer example from Nordic.

const nrf_drv_timer_t beeptimer = NRF_DRV_TIMER_INSTANCE(4);

void testtimer(nrf_timer_event_t event_type, void *p_context) {
    Serial.println("hi");
}


//TODO: FREQUENCY INPUT LIMITS
void testTimer()
{
   
    //set up timer
    ticks = 1000;
    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    int err_code = nrf_drv_timer_init(&beeptimer, &timer_cfg, testtimer);
    Log.error("Timer error %d", err_code);

    nrf_drv_timer_extended_compare(&beeptimer, NRF_TIMER_CC_CHANNEL0, ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
    nrf_drv_timer_enable(&beeptimer);
    
    
}

Any suggestions would be appreciated,

Thanks!

Timer 4 is normally used by NFC, so that is a good choice unless you need NFC.

  • 0: Softdevice - do not use
  • 1: Radio - Used by BLE (and mesh)
  • 2: Usart (Serial1) Can use if not using Serial1.
  • 3: Usart (Serial2 on Xenon) Cannot be used on Argon or Boron because it’s required by NCP.
  • 4: NFC - recommended (unless you need NFC).

Also I would not call Serial.print from a hardware timer callback. It’s not interrupt safe and will eventually cause a SOS hard fault.

So, I’m still getting a hard fault when I try to do this. Suggestions?

#include "nrf_timer.h"
#include "nrf_drv_timer.h"

const nrf_drv_timer_t beeptimer = NRF_DRV_TIMER_INSTANCE(4);

SYSTEM_MODE(MANUAL)

volatile bool pinswitch = 0;
void timerPinToggle(nrf_timer_event_t event_type, void *p_context)
{   
    if(pinswitch) {
        digitalWriteFast(A5, HIGH);
    } else {
        digitalWriteFast(A5, LOW);
    }
}

void testtimer()
{
    //calculate how often to fire the timer
    int ticks = 16000000 / 512;

    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    int err_code = nrf_drv_timer_init(&beeptimer, &timer_cfg, timerPinToggle);
    nrf_drv_timer_extended_compare(&beeptimer, NRF_TIMER_CC_CHANNEL0, ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
    nrf_drv_timer_enable(&beeptimer);
    
}

void setup() {
  testtimer();
}

void loop() {
}