<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Consecutive ADC PPI triggered by RTC1 offset problem - ADC event trigger on the wrong sample</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/73643/consecutive-adc-ppi-triggered-by-rtc1-offset-problem---adc-event-trigger-on-the-wrong-sample</link><description>Hi All 
 I am using S130, nRF51822, SDK12, My RTC1 compare events is connected by PPI to ADC, I have set it to generate an adc_ppi_event_handler after 6 consecutive compare events, or after 2 events of 3 consecutive samples (so I get 2 adc_ppi_event_handler</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 08 Apr 2021 16:11:25 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/73643/consecutive-adc-ppi-triggered-by-rtc1-offset-problem---adc-event-trigger-on-the-wrong-sample" /><item><title>RE: Consecutive ADC PPI triggered by RTC1 offset problem - ADC event trigger on the wrong sample</title><link>https://devzone.nordicsemi.com/thread/303852?ContentTypeID=1</link><pubDate>Thu, 08 Apr 2021 16:11:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:200839e1-818d-4a4f-87b1-ceb4200996b5</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;PPI does not have any awareness of the state of the ADC buffers and sample counts, this is simply a hardware signal going from an EVENT generated by one peripheral to a TASK of another peripheral.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The ADC in nRF51 series ICs also does not have any buffering functionality for the ADC, every sample needs to be read out from the peripheral by the CPU, before the next sample can be started. Storing of multiple samples into a single buffer is handled by the ADC driver.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When you say &amp;quot;&lt;span&gt;resetting the ADC-PPI before every time I start sampling&amp;quot;, when exactly do plan to do this? Do you start the RTC at a given time to start sampling, i.e., it does not sample continuously into 6 sample buffers? There seems to be no APIs to stop an ongoing conversion&amp;nbsp;in the ADC driver, but you can check&amp;nbsp;nrf_drv_adc_is_busy() and then trigger nrf_drv_adc_sample() until you get the&amp;nbsp;NRF_DRV_ADC_EVT_DONE&amp;nbsp;event.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;br /&gt;Jørgen&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Consecutive ADC PPI triggered by RTC1 offset problem - ADC event trigger on the wrong sample</title><link>https://devzone.nordicsemi.com/thread/303294?ContentTypeID=1</link><pubDate>Tue, 06 Apr 2021 18:02:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:57321824-8283-403e-98b0-b7aa92da44cb</guid><dc:creator>Ron</dc:creator><description>&lt;p&gt;Hi J&amp;oslash;rgen&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you for your answer&lt;/p&gt;
&lt;p&gt;the sample interval is about 10msec.&lt;/p&gt;
&lt;p&gt;I am adding below some of the relevant code&lt;/p&gt;
&lt;p&gt;Aside for suggestion on possible cause, I would like to know how can I reset the process, buffer etc.., I am thinking about just resetting the ADC-PPI before every time I start sampling to make it more robust, I mean how do I reset the PPI to believe the buffer is empty and to assure starting from 0 every new 6 sample cycle.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks again and best regards&lt;/p&gt;
&lt;p&gt;Ron&lt;/p&gt;
&lt;p&gt;--------------------------&lt;/p&gt;
&lt;p&gt;This is the configuration code&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define ADC_BUFFER_SIZE 								6  
nrf_adc_value_t          		adc_buffer[ADC_BUFFER_SIZE];

static void adc_ppi_config(void)
{
    ret_code_t ret_code;
	
    //Initialize ADC
    nrf_drv_adc_config_t config = NRF_DRV_ADC_DEFAULT_CONFIG;
    ret_code = nrf_drv_adc_init(&amp;amp;config, adc_ppi_event_handler);
    APP_ERROR_CHECK(ret_code);
	
    m_channel_0_config.config.config.input = NRF_ADC_CONFIG_SCALING_INPUT_ONE_THIRD;
    m_channel_1_config.config.config.input = NRF_ADC_CONFIG_SCALING_INPUT_ONE_THIRD;	
    
    nrf_drv_adc_channel_enable(&amp;amp;m_channel_0_config);
    nrf_drv_adc_channel_enable(&amp;amp;m_channel_1_config);
    
}

void rtc1_cc_enable(void)
{
	
  NRF_RTC1-&amp;gt;EVTENSET = RTC_EVTENSET_COMPARE0_Msk | RTC_EVTENSET_COMPARE1_Msk | RTC_EVTENSET_COMPARE2_Msk;
  NRF_RTC1-&amp;gt;INTENSET = RTC_INTENSET_COMPARE0_Msk | RTC_EVTENSET_COMPARE1_Msk | RTC_EVTENSET_COMPARE2_Msk;
	
  NVIC_ClearPendingIRQ(RTC1_IRQn);
  NVIC_EnableIRQ(RTC1_IRQn);
  NRF_RTC1-&amp;gt;TASKS_START = 1;
	
}

void adc_ppi_sampling_event_init(void)
{
    ret_code_t err_code;
	
    uint32_t timer_compare_event_addr = (uint32_t)&amp;amp;NRF_RTC1-&amp;gt;EVENTS_COMPARE[2];	//connect adc ppi to rtc1 as clock source
    uint32_t adc_sample_event_addr = nrf_drv_adc_start_task_get();

    /* setup ppi channel so that timer compare event is triggering sample task in SAADC */
    err_code = nrf_drv_ppi_channel_alloc(&amp;amp;m_ppi_channel);
    APP_ERROR_CHECK(err_code);
    
    err_code = nrf_drv_ppi_channel_assign(m_ppi_channel, timer_compare_event_addr, adc_sample_event_addr);  //NRF_ADC-&amp;gt;TASKS_START);
    APP_ERROR_CHECK(err_code);
}

void adc_ppi_sampling_event_enable(void)
{
    ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);
    APP_ERROR_CHECK(err_code);
}

void adc_ppi_init(void)
{
	rtc1_cc_enable();
	
    adc_ppi_sampling_event_init();
	
    adc_ppi_config();
	
    adc_ppi_sampling_event_enable();
	
	APP_ERROR_CHECK(nrf_drv_adc_buffer_convert(adc_buffer,ADC_BUFFER_SIZE));
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And this is how I trigger the process&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define ADC_PPI_RTC_INTERVAL 30

void RTC1_IRQHandler(void)
{
	 NRF_RTC1-&amp;gt;EVENTS_COMPARE[0] = 0;
	 NRF_RTC1-&amp;gt;EVENTS_COMPARE[1] = 0;
	 NRF_RTC1-&amp;gt;EVENTS_COMPARE[3] = 0;

		if ((NRF_RTC1-&amp;gt;EVENTS_COMPARE[2] != 0) &amp;amp;&amp;amp; (rtc1_main_init_flag))
		{

				rtc1_c2_counter++;
	
				NRF_RTC1-&amp;gt;EVENTS_COMPARE[2] = 0;
			
				if (rtc1_c2_counter &amp;gt;= ADC_BUFFER_SIZE)
				{
					rtc1_c2_counter = 0;
				}			
				else
				{
					NRF_RTC1-&amp;gt;CC[2] = add_rtc_offset(0x20*10);
				}
		}
		else
		{
			NRF_RTC1-&amp;gt;EVENTS_COMPARE[2] = 0;
		}	

    NRF_RTC1-&amp;gt;EVENTS_TICK       = 0;
    NRF_RTC1-&amp;gt;EVENTS_OVRFLW     = 0;
    
    // Check for expired timers
    timer_timeouts_check();
}

uint32_t add_rtc_offset(uint32_t offset)	
{
		uint32_t rtc_current;
		uint32_t rtc_updated;

		rtc_current = NRF_RTC1-&amp;gt;COUNTER;
	
		if ((rtc_current + offset) &amp;gt; RTC1_MAX)
		{
			rtc_updated = offset - (RTC1_MAX - rtc_current) ;
		}
		else
		{
			rtc_updated = rtc_current + offset;
		}
					
		return(rtc_updated);
}

void rtc1_ChanOffset(uint8_t chan, uint32_t offset)	
{
	NRF_RTC1-&amp;gt;CC[chan] = add_rtc_offset(0x20*offset);
	rtc1_c2_counter = 0;
}

rtc1_ChanOffset(2,ADC_PPI_RTC_INTERVAL);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Consecutive ADC PPI triggered by RTC1 offset problem - ADC event trigger on the wrong sample</title><link>https://devzone.nordicsemi.com/thread/303290?ContentTypeID=1</link><pubDate>Tue, 06 Apr 2021 17:11:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0f3ed86e-cc72-4cd1-b883-96ee0f652f4b</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The only thing that comes to mind is that the ADC interrupt handler is not able to read out the result before the next sample is triggered by PPI, for instance if it is blocked by the softdevice.&lt;/p&gt;
&lt;p&gt;What is the sample interval? Can you post the code you used for configuring the ADC and PPI?&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Jørgen&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>