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

APP_timer & RTC

Hi,

I am using nrf52832 sdk11.0.0 s132 v2.0.1.

In my code app_timer uses RTC1.I want to use RTC2 for counting 1 minute without disturbing the ongoing functionality. Given below are the snippets of the code:

#define APP_TIMER_PRESCALER          0                                
#define APP_TIMER_MAX_TIMERS        (2+BSP_APP_TIMERS_NUMBER)          
#define APP_TIMER_OP_QUEUE_SIZE      2                                  
                                        
#define COMPARE_COUNTERTIME  (15UL << 2)
//#define SCHED_MAX_EVENT_DATA_SIZE       sizeof(app_timer_event_t)            
#define SCHED_QUEUE_SIZE                10                  
#define SCHED_MAX_EVENT_DATA_SIZE       MAX(APP_TIMER_SCHED_EVT_SIZE, sizeof(nrf_drv_gpiote_pin_t))


static void ppi_init(void)
{
    // When using the Timeslot advertiser-scanner library the only PPI channels available are 3 and 7
    
    // Configure PPI channel 0 to trigger COUNT on TIMER1 when COMPARE[1] matches on RTC1
	NRF_PPI->CH[7].EEP = (uint32_t)(&NRF_RTC2->EVENTS_COMPARE[1]);
	NRF_PPI->CH[7].TEP = (uint32_t)(&NRF_TIMER1->TASKS_COUNT);
	
	// Configure PPI channel 1 to trigger CLEAR on RTC1 when COMPARE[1] matches on RTC1
	NRF_PPI->CH[3].EEP = (uint32_t)(&NRF_RTC2->EVENTS_COMPARE[1]);
	NRF_PPI->CH[3].TEP = (uint32_t)(&NRF_RTC2->TASKS_CLEAR);
	
	// Enable only PPI channels 0 and 1.
	NRF_PPI->CHEN = (PPI_CHEN_CH3_Enabled << PPI_CHEN_CH3_Pos) | (PPI_CHEN_CH7_Enabled << PPI_CHEN_CH7_Pos);
}

static void timer1_init(void)
{
	// Configure TIMER1 in COUNT mode and 32 bit
	NRF_TIMER1->MODE = TIMER_MODE_MODE_Counter;
	NRF_TIMER1->BITMODE = TIMER_BITMODE_BITMODE_32Bit;  
	NRF_TIMER1->TASKS_CLEAR = 1;
	NRF_TIMER1->TASKS_START = 1;	
}


static void lfclk_config(void)
{
    NRF_CLOCK->LFCLKSRC             = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
    NRF_CLOCK->EVENTS_LFCLKSTARTED  = 0;
    NRF_CLOCK->TASKS_LFCLKSTART     = 1;
    while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
    {
        //Do nothing.
    }
    NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
}




static void appRTCHandler(nrf_drv_rtc_int_type_t int_type)
{     
	
    if (int_type == NRF_DRV_RTC_INT_COMPARE0)
    {
       	count_timer++;
		SEGGER_RTT_WriteString(0, "in_timer_handler\n");
		if(count_timer== (timer_minutes[3]-0x30))     //count_timer has to be 1 minute//
		{	
			nrf_drv_rtc_disable(&rtc);			
			nrf_drv_rtc_counter_clear(&rtc);
			app_sched_event_put(&p_pins,sizeof(p_pins),scheduler_event_handler);				
		}
		else
		{
			//nrf_drv_rtc_counter_clear(&rtc);
			nrf_drv_rtc_int_enable(&rtc, NRF_RTC_INT_COMPARE0_MASK);
			//SEGGER_RTT_WriteString(0, "TIMER_RUNNINGG...\n");
		}			
	}
}


void appRTCInit(void)
	
{
    uint32_t err_code;
		
	nrf_drv_rtc_config_t rtc_config;
	rtc_config.prescaler = 4095;
	//Initialize RTC instance
	err_code = nrf_drv_rtc_init(&rtc, &rtc_config, appRTCHandler);
	APP_ERROR_CHECK(err_code);

	//Enable tick event & interrupt
	nrf_drv_rtc_tick_enable(&rtc, true);

	//Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
	err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
	APP_ERROR_CHECK(err_code);
	
}
		


this is my main code:

int main(void)
{  
	
	ret_code_t err_code;
	bool  erase_bonds;


	NRF_POWER->RAMON = (POWER_RAMON_ONRAM0_RAM0On   << POWER_RAMON_ONRAM0_Pos)
						| (POWER_RAMON_ONRAM1_RAM1On   << POWER_RAMON_ONRAM1_Pos)
						| (POWER_RAMON_OFFRAM0_RAM0Off << POWER_RAMON_OFFRAM0_Pos)
						| (POWER_RAMON_OFFRAM1_RAM1Off << POWER_RAMON_OFFRAM1_Pos);

	//APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL);

	lfclk_config();
	timer1_init();
	ppi_init();
	//rtc1_init();
	appRTCInit();
	//APP_TIMER_INIT(APP_TIMER_PRESCALER,APP_TIMER_MAX_TIMERS ,APP_TIMER_OP_QUEUE_SIZE, NULL);

	init_gpiote();
	err_code = app_gpiote_user_enable(m_gpiote_user_id); // put in ARM switch case statement
	APP_ERROR_CHECK(err_code);
	//	   timers_init();	
	APP_TIMER_APPSH_INIT(APP_TIMER_PRESCALER,APP_TIMER_MAX_TIMERS,APP_TIMER_OP_QUEUE_SIZE,true);
	ble_stack_init();
	scheduler_init();
	device_manager_init(erase_bonds);
	gap_params_init();
	conn_params_init();
	services_init();
	scan_start();
}

I have given the functions only related to the timer and the main code is for the reference. So my questions are:

  1. what changes I should make to the above code?

  2. I am getting NRF_FAULT_ID_SDK_ERROR for SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL); how should I solve it?

  3. Can you provide modified app_timer.c and app_timer.h?

Thank you

Parents
  • Hi,

    Thank you for the answer.In my code, in the appRTC handler, I was always comparing with the NRF_DRV_RTC_INT_COMPARE0 and inside ppi_init() function it was &NRF_RTC2->EVENTS_COMPARE[1] which should be :

    &NRF_RTC2->EVENTS_COMPARE[0] for both the channels(3&7) and the count_timer variable was static which I made to global.Then in appRTCInit() function, I made initialization like below:

    nrf_drv_rtc_config_t m_default_config = NRF_DRV_RTC_DEFAULT_CONFIG(2);
    	m_default_config.prescaler = 4095;
    	m_default_config.interrupt_priority = 2;
    

    So everything is working fine now.

Reply
  • Hi,

    Thank you for the answer.In my code, in the appRTC handler, I was always comparing with the NRF_DRV_RTC_INT_COMPARE0 and inside ppi_init() function it was &NRF_RTC2->EVENTS_COMPARE[1] which should be :

    &NRF_RTC2->EVENTS_COMPARE[0] for both the channels(3&7) and the count_timer variable was static which I made to global.Then in appRTCInit() function, I made initialization like below:

    nrf_drv_rtc_config_t m_default_config = NRF_DRV_RTC_DEFAULT_CONFIG(2);
    	m_default_config.prescaler = 4095;
    	m_default_config.interrupt_priority = 2;
    

    So everything is working fine now.

Children
No Data
Related