RTC + PPI in nrf connect sdk using NRFX drivers.

Hi, I am trying intialize the RTC in ncs using nrfx drivers and then connect it tick event to a saadc sampling event using a ppi channel.
I learnt that RTC0 and RTC1 are used internally and only option available is RTC 2 though this ticket.

const nrfx_rtc_t rtc = NRFX_RTC_INSTANCE(2);

static void rtc_handler(nrfx_rtc_int_type_t int_type)
{
	switch (int_type)
	{
		case NRFX_RTC_INT_TICK:
			printk("tick interrupt received\n");
			break;
		case NRFX_RTC_INT_COMPARE0:
		{
			printk("compare 0 event received\n");
			err_code = nrfx_rtc_cc_set(&rtc, 0, 1, true);
			if(err_code!=NRFX_SUCCESS)
			{
				printk(" compare channel initialization error %d",err_code);
			}
		}
		default:
			printk("rtc interrupt %d\n", int_type);
			break;
	}
}

void rtc_init()
{
    uint32_t err_code;

    //Initialize RTC instance
    nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
    config.prescaler = RTC_FREQ_TO_PRESCALER(8);

	IRQ_CONNECT(DT_IRQN(DT_NODELABEL(rtc2)),
	            DT_IRQ(DT_NODELABEL(rtc2), priority),
	            rtc_handler, NULL, 0);

    err_code = nrfx_rtc_init(&rtc, &config, rtc_handler);
    if(err_code!=NRFX_SUCCESS)
	{
		printk(" rtc initialization error %d",err_code);
	}

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

    //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
	err_code = nrfx_rtc_cc_set(&rtc, 0, 1, true);
	if(err_code!=NRFX_SUCCESS)
	{
		printk(" compare channel initialization error %d",err_code);
	}
	nrfx_rtc_enable(&rtc);
}

 void ppi_init(void)
{
	// Trigger task sample from timer
	nrfx_err_t err_code = nrfx_ppi_channel_alloc(&m_timer_saadc_ppi_channel);
	if(err_code!=NRFX_SUCCESS)
	{
	printk(" PPI channel allocation error %d",err_code);
	}

	err_code = nrfx_ppi_channel_assign(m_timer_saadc_ppi_channel, 
	                                   nrfx_rtc_event_address_get(&rtc, NRF_RTC_EVENT_COMPARE_0),
	                                   nrf_saadc_task_address_get((NRF_SAADC_Type *)0x40007000,NRF_SAADC_TASK_SAMPLE));
	
	if(err_code!=NRFX_SUCCESS)
	{
		printk(" PPI channel assignment error %d",err_code);
	}

	err_code = nrfx_ppi_channel_enable(m_timer_saadc_ppi_channel);

	if(err_code!=NRFX_SUCCESS)
	{
		printk(" PPI channel enable error %d",err_code);
	}
}

main()
{
..
..
rtc_init();
ppi_inti();
..
..
}
 

This is my implementation. what am i doing wrong here?
Is there any drivers for using PPI through Zephyr drivers ? other than PPI TRACE. PPI is completely absent in zephyr drivers. I wonder why. If Nordic develops a way to integrate PPI like device drivers model, it would be a lot easier.
Thanks in advance.

Parents Reply
  • Hi, sorry for the delay. I have solved the issue. The PPI was working well on RTC events with previous implementation only. But the thing that confused me so much and ate all my time was IRQ_CONNECT. Instead of rtc_handler from my code , it should be nrfx_rtc_2_irq_handler. Which is declared in rtc.h and defined in rtc.c

    The above code works only with this change 

    IRQ_CONNECT(DT_IRQN(DT_NODELABEL(rtc2)),
    	            DT_IRQ(DT_NODELABEL(rtc2), priority),
    	            nrfx_rtc_2_irq_handler, NULL, 0);

Children
Related