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

app_scheduler and TIMER1 hardfault

I have the code below initializing TIMER1 and and IRQ every 1ms. I call app_sched_event_put within the timer and I get a Hardfault.

Any hints on what is going on ?

Thanks.

void high_res_timer_init(){
    // Stop timers
    NRF_TIMER1->TASKS_STOP  = 1;
    // Clear timers
    NRF_TIMER1->TASKS_CLEAR = 1;
    // Set the timer1 in Timer Mode, and timer 2 in counter mode
    NRF_TIMER1->MODE        = TIMER_MODE_MODE_Timer;
    // Prescaler 4 produces 1MHz for timer1 frequency
    NRF_TIMER1->PRESCALER      = 4;
    // Ensure 16 bit mode on both timers
    NRF_TIMER1->BITMODE        = TIMER_BITMODE_BITMODE_16Bit;
    // 1ms interrupt
    NRF_TIMER1->CC[0] = 1000;
		NRF_TIMER1->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos);
		NVIC_EnableIRQ(TIMER1_IRQn);
	
    // Start timers
    NRF_TIMER1->TASKS_START = 1;
}

void TIMER1_IRQHandler(void) {
	
	
	if ((NRF_TIMER1->EVENTS_COMPARE[0] != 0) && ((NRF_TIMER1->INTENSET & TIMER_INTENSET_COMPARE0_Msk) !=0)) {
		NRF_TIMER1->EVENTS_COMPARE[0] = 0;
		NRF_TIMER1->TASKS_CLEAR = 1;
		nt_time++;
		if (count++ > 20) {
			app_sched_event_put(&nt_time, sizeof(nt_time), mag_scheduler_event_handler);
			count = 0;
		}
		dpin_toggle();
	}
}
Parents
  • EricS is right, ill explain little more of what he said. It must be happening when calling CRITICAL_REGION_ENTER(); inside app_sched_event_put. This happens when your timer interrupt handler priority is higher than SVC priority (2) and you have definedSOFTDEVICE_PRESENT in your project.

    Setting below when initializing the timer interrupt should do the trick.

    NVIC_SetPriority(TIMER1_IRQn, APP_IRQ_PRIORITY_LOW);
    
  • nRF51 has Cortex M0 processor which allows only four programmable priority levels (0-3). Setting any thing else than that will obviously hardfault immediately on the chip level. Please use only APP_PRIORITY_HIGH or APP_PRIORITY_LOW macro for application specific priority levels. If you use APP_PRIORITY_HIGH, then it will be higher priority than SVC calls and hence your application wont be able to make any softdevice calls in this interrupt priority context.

Reply
  • nRF51 has Cortex M0 processor which allows only four programmable priority levels (0-3). Setting any thing else than that will obviously hardfault immediately on the chip level. Please use only APP_PRIORITY_HIGH or APP_PRIORITY_LOW macro for application specific priority levels. If you use APP_PRIORITY_HIGH, then it will be higher priority than SVC calls and hence your application wont be able to make any softdevice calls in this interrupt priority context.

Children
No Data
Related