<?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>Timer/Counter example using Zephyr drivers for nRF52832</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/103994/timer-counter-example-using-zephyr-drivers-for-nrf52832</link><description>Hi, 
 I&amp;#39;m trying to do the following with my nRF52832: 
 
 on a trigger from the LPCOMP start taking a series of ADC measurements 
 each measurement needs to occur 312usec (equivalent to 64 samples on a 50Hz sinewave) 
 
 So, I want to use a timer or</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 02 Oct 2023 13:27:33 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/103994/timer-counter-example-using-zephyr-drivers-for-nrf52832" /><item><title>RE: Timer/Counter example using Zephyr drivers for nRF52832</title><link>https://devzone.nordicsemi.com/thread/448531?ContentTypeID=1</link><pubDate>Mon, 02 Oct 2023 13:27:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cea613a8-faf2-42ef-827a-7001dbe5ce8b</guid><dc:creator>Maria Gilje</dc:creator><description>&lt;p&gt;Hi Mike!&lt;/p&gt;
&lt;p&gt;Great that you found a solution which meets your requirements. Thank you for sharing!&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Maria&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Timer/Counter example using Zephyr drivers for nRF52832</title><link>https://devzone.nordicsemi.com/thread/447858?ContentTypeID=1</link><pubDate>Wed, 27 Sep 2023 06:17:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e415901d-fff4-4409-8aab-71c907749d43</guid><dc:creator>Mike Austin (LPI)</dc:creator><description>&lt;p&gt;Hi Maria,&lt;/p&gt;
&lt;p&gt;Got this sorted now.&lt;/p&gt;
&lt;p&gt;Just needed to change:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;while (!sampling_complete) {
	k_usleep(1);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;to:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;while (!sampling_complete) {
	k_yield();
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m now able to get my sampling speed up to ~ 40kHz, which is more than sufficient&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Mike&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Timer/Counter example using Zephyr drivers for nRF52832</title><link>https://devzone.nordicsemi.com/thread/447121?ContentTypeID=1</link><pubDate>Thu, 21 Sep 2023 14:45:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:79311880-2f56-4726-8091-d878e13d9547</guid><dc:creator>Maria Gilje</dc:creator><description>&lt;p&gt;Hi Mike, &lt;/p&gt;
&lt;p&gt;I will look into this soon. Thank you for your patience.&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Maria&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Timer/Counter example using Zephyr drivers for nRF52832</title><link>https://devzone.nordicsemi.com/thread/446985?ContentTypeID=1</link><pubDate>Thu, 21 Sep 2023 07:23:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:576c8227-c893-4d42-9403-1f3c8cb0e202</guid><dc:creator>Mike Austin (LPI)</dc:creator><description>&lt;p&gt;OK, have made a bit of progress on this, but still floundering along not really sure what I&amp;#39;m doing.&lt;/p&gt;
&lt;p&gt;I solved the initial problem with this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define TIMER DT_NODELABEL(timer0)
const struct device *counter_dev = DEVICE_DT_GET(TIMER);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;So, now I have a counter that seems to work.&amp;nbsp; When I say &amp;quot;work&amp;quot;, I mean that I can get it to trigger a callback at the value of ticks that I specify in the counter struct:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;struct counter_top_cfg counter_config = {
	.ticks = 32000, // Equivalent to 2msec
	.callback = counter_callback,
	.user_data = NULL,
	.flags = COUNTER_TOP_CFG_RESET_WHEN_LATE | COUNTER_CONFIG_INFO_COUNT_UP,
};&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I am using a button on the DK to trigger the start of the sampling, and my counter_callback function, that is getting triggered every 32,000 ticks looks like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void counter_callback(const struct device *dev, void *user_data)
{
	printk(&amp;quot;Getting sample %d\n&amp;quot;, sample_count);
	gpio_pin_set_dt(&amp;amp;sample_adc, HIGH);
	if (sample_count &amp;lt; MAX_ADC_SAMPLES) {
		err = adc_read(dev_adc, &amp;amp;sequence);
		gpio_pin_set_dt(&amp;amp;sample_adc, LOW);
		if (err != 0) {
			printk(&amp;quot;ADC reading failed with error %d.\n&amp;quot;, err);
			return;
		}
		adc_samples[sample_count] = sample_buffer;
		sample_count++;
	}
	else {
		sampling_complete = true;
	}
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m basically aiming to take up to MAX_ADC_SAMPLES (currently set to 32) and when this has been achieved, I set sample_complete to true.&amp;nbsp; In my main loop, I am doing this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;	while (1) {
		if (measure_adc) {

			sample_count = 0;
			sampling_complete = false;
			counter_start(counter_dev);
			while (!sampling_complete) {
				k_usleep(1);
				//printk(&amp;quot;sampling_complete = %d\n&amp;quot;, sampling_complete);
			}
			printk(&amp;quot;Sampling completed\n&amp;quot;);
			err = counter_stop(counter_dev);
			if (err) {
				printk(&amp;quot;Unable to stop counter (err %d)\n&amp;quot;, err);
			}
			printk(&amp;quot;Counter stopped\n&amp;quot;);

			measure_adc = false;
			sample_count=0;
			sampling_complete = false;

			for (i=0;i&amp;lt;MAX_ADC_SAMPLES;i++) {
				//printk(&amp;quot;sample: %d	value: %d	sample time: %d\n&amp;quot;, i, adc_samples[i], adc_sample_time[i]);
				printk(&amp;quot;sample: %d	value: %d\n&amp;quot;, i, adc_samples[i]);
			}
		}
		k_usleep(100);
	}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;So the button press asserts measure_adc.&amp;nbsp; That triggers the start of the counter, which triggers the sampling.&amp;nbsp; There are a few problems I am noticing though:&lt;/p&gt;
&lt;p&gt;1. If I reduce the value of .ticks in my counter_config structure (ultimately I need the sampling period to be 312usec), then the check on the value of sample_complete in my main loop doesn&amp;#39;t seem to get tested, so I never stop the counter and hence stop the sampling.&amp;nbsp; It just goes on sampling forever&lt;/p&gt;
&lt;p&gt;2. The same thing seems to occur if I remove the printk(&amp;quot;Getting sample %d\n&amp;quot;, sample_count); in my counter_callback function.&amp;nbsp; I&amp;#39;m presuming this is somehow adding a delay that, in the same way as not reducing the ticks value too much, allows the check on sample_complete to be done&lt;/p&gt;
&lt;p&gt;3. Again, in a similar vain, if I don&amp;#39;t put a suitable delay in the while(!sampling_complete) loop, this doesn&amp;#39;t seem to work correctly.&lt;/p&gt;
&lt;p&gt;Can anyone see what I&amp;#39;m doing wrong?&amp;nbsp; As I said, ultimately I need to be able to trigger a sampling sequence of 32 samples taken at 312usec intervals, and store this into an array that I can then use in my RMS and FFT calcs later on in my firmware.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Mike&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>