<?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>ADC sampling @ 8kHz</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/50649/adc-sampling-8khz</link><description>Hello, 
 I want to use the ADC for a burst @ 8khz. 
 I use this sample &amp;quot; test_adc_sample_with_interval &amp;quot; from https://github.com/zephyrproject-rtos/zephyr/blob/master/tests/drivers/adc/adc_api/src/test_adc.c 
 I set the interval regarding to 8khz to 125us</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 20 Aug 2019 14:18:27 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/50649/adc-sampling-8khz" /><item><title>RE: ADC sampling @ 8kHz</title><link>https://devzone.nordicsemi.com/thread/205083?ContentTypeID=1</link><pubDate>Tue, 20 Aug 2019 14:18:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0d40a037-253b-4082-8994-28cf131904ac</guid><dc:creator>squick</dc:creator><description>&lt;p&gt;Yes, i have but it doesn&amp;#39;t work either :(&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC sampling @ 8kHz</title><link>https://devzone.nordicsemi.com/thread/203988?ContentTypeID=1</link><pubDate>Wed, 14 Aug 2019 08:44:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cdf6d718-e7ae-41ad-a8c9-fe0bce993b4b</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;One obvious thing came to mind - have you increased&amp;nbsp;SYS_CLOCK_TICKS_PER_SEC? If you are using 100 (default),&amp;nbsp;then that matches your 100 Hz sample rate.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC sampling @ 8kHz</title><link>https://devzone.nordicsemi.com/thread/203749?ContentTypeID=1</link><pubDate>Tue, 13 Aug 2019 08:20:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4086c3d8-15bb-4950-82d2-903e3e09df45</guid><dc:creator>squick</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/*
 * Copyright (c) 2018 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
 */

#include &amp;lt;net/socket.h&amp;gt;
#include &amp;lt;nrf9160.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;uart.h&amp;gt;
#include &amp;lt;adc.h&amp;gt;
#include &amp;lt;gpio.h&amp;gt;
#include &amp;lt;zephyr.h&amp;gt;

#define CONFIG_BOARD_NRF9160_PCA10090

static int i = 0;

struct device *adc_dev;
struct device *gpio_dev;

#if defined(CONFIG_BOARD_NRF52_PCA10040) ||                                    \
	defined(CONFIG_BOARD_NRF52840_PCA10056) ||                             \
	defined(CONFIG_BOARD_NRF9160_PCA10090) ||                              \
	defined(CONFIG_BOARD_NRF52840_BLIP)

#include &amp;lt;hal/nrf_saadc.h&amp;gt;
#define ADC_DEVICE_NAME DT_ADC_0_NAME
#define ADC_RESOLUTION 12
#define ADC_GAIN ADC_GAIN_1_4
#define ADC_REFERENCE ADC_REF_INTERNAL
#define ADC_ACQUISITION_TIME ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 40)
#define ADC_1ST_CHANNEL_ID 0
#define ADC_1ST_CHANNEL_INPUT NRF_SAADC_INPUT_AIN0
#define ADC_2ND_CHANNEL_ID 2
#define ADC_2ND_CHANNEL_INPUT NRF_SAADC_INPUT_AIN2
#endif

#define PIN SW0_GPIO_PIN 
static struct gpio_callback button_cb;
static const u8_t led_pins[] = { LED0_GPIO_PIN, LED1_GPIO_PIN, LED2_GPIO_PIN, LED3_GPIO_PIN };

static const struct adc_channel_cfg m_1st_channel_cfg = {
	.gain = ADC_GAIN,
	.reference = ADC_REFERENCE,
	.acquisition_time = ADC_ACQUISITION_TIME,
	.channel_id = ADC_1ST_CHANNEL_ID,
        .differential = 0,
#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS)
	.input_positive = ADC_1ST_CHANNEL_INPUT,
#endif
};


#define BUFFER_SIZE 600
static int16_t m_sample_buffer[BUFFER_SIZE];

enum adc_action adc_finished(struct device *dev,const struct adc_sequence *sequence, u16_t sampling_index){
 
        if(sampling_index == BUFFER_SIZE-1){
                // Print the AIN0 values
                gpio_pin_write(gpio_dev, led_pins[0], 0);                   // Led off when burst is finished
                for (int i = 0; i &amp;lt; BUFFER_SIZE/10; i++) {
                        printk(&amp;quot;%4d\n%4d\n%4d\n%4d\n%4d\n%4d\n%4d\n%4d\n%4d\n%4d\n&amp;quot;, // send the results to the uart...
                        m_sample_buffer[i*10+0],
                        m_sample_buffer[i*10+1],
                        m_sample_buffer[i*10+2],
                        m_sample_buffer[i*10+3],
                        m_sample_buffer[i*10+4],
                        m_sample_buffer[i*10+5],
                        m_sample_buffer[i*10+6],
                        m_sample_buffer[i*10+7],
                        m_sample_buffer[i*10+8],
                        m_sample_buffer[i*10+9]);
                }  
                gpio_pin_configure(gpio_dev, PIN, GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | GPIO_INT_ACTIVE_HIGH | GPIO_PUD_PULL_UP);  // activate the interrupt pin
        }
        
       
       k_busy_wait(67);     // workaround for the 8khz samplerate - determined by hand :/

       return ADC_ACTION_CONTINUE;    
}

static int adc_sample(void)
{
	int ret;

        const struct adc_sequence_options sequence_opt = {
                .interval_us = 0,
                .callback = adc_finished,
                .extra_samplings = BUFFER_SIZE-1,
        };

	const struct adc_sequence sequence = {
                .options = &amp;amp;sequence_opt,
		.channels = BIT(ADC_1ST_CHANNEL_ID),
		.buffer = m_sample_buffer,
		.buffer_size = sizeof(m_sample_buffer),
		.resolution = ADC_RESOLUTION,
                .oversampling = 0,
	};

	if (!adc_dev) {
		return -1;
	}

	ret = adc_read(adc_dev, &amp;amp;sequence);
	//printk(&amp;quot;ADC read err: %d\n&amp;quot;, ret);

	return ret;
}

void button_pressed(struct device *dev, struct gpio_callback *cb, u32_t pin)
{
          int err = 0;
          err = adc_sample();
                    if (err) {
		printk(&amp;quot;Error in adc sampling: %d\n&amp;quot;, err);
          }
          gpio_pin_configure(gpio_dev, PIN, GPIO_DIR_IN | GPIO_INT_ACTIVE_HIGH );   //deactivate the interrupt pin.
          gpio_pin_write(gpio_dev, led_pins[0], 1);                                 //led on for the read process
}

int dk_leds_init(void)
{
	int err = 0;

	gpio_dev = device_get_binding(DT_GPIO_P0_DEV_NAME);
	if (!gpio_dev) {
		printk(&amp;quot;Cannot bind gpio device&amp;quot;);
		return -ENODEV;
	}

	for (size_t i = 0; i &amp;lt; ARRAY_SIZE(led_pins); i++) {
		err = gpio_pin_configure(gpio_dev, led_pins[i], GPIO_DIR_OUT);

		if (err) {
			printk(&amp;quot;Cannot configure LED gpio&amp;quot;);
			return err;
		}
	}
	gpio_pin_configure(gpio_dev, PIN, GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | GPIO_INT_ACTIVE_HIGH | GPIO_PUD_PULL_UP );
	gpio_init_callback(&amp;amp;button_cb, button_pressed, BIT(PIN));
	gpio_add_callback(gpio_dev, &amp;amp;button_cb);
	gpio_pin_enable_callback(gpio_dev, PIN);	

	return err;
}

int dk_adc_init(void)
{
        int err = 0;

	adc_dev = device_get_binding(&amp;quot;ADC_0&amp;quot;);
	if (!adc_dev) {
		printk(&amp;quot;device_get_binding ADC_0 failed\n&amp;quot;);
	}


	err = adc_channel_setup(adc_dev, &amp;amp;m_1st_channel_cfg);
	if (err) {
		printk(&amp;quot;Error in adc setup: %d\n&amp;quot;, err);
	}

        return err;
}


int main(void)
{
	int err;

	printk(&amp;quot;nrf91 saadc sampling AIN0 (P0.13)\n&amp;quot;);
        
        dk_adc_init();
        dk_leds_init();

	/* Trigger offset calibration
	 * As this generates a _DONE and _RESULT event
	 * the first result will be incorrect.
	 */
	NRF_SAADC_NS-&amp;gt;TASKS_CALIBRATEOFFSET = 1;

	while (1){
                      k_cpu_idle();
                 }
	
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;above the full Code. If the switch 0 is pressed the cpu wake up and make a burst with 600 samples.&lt;/p&gt;
&lt;p&gt;Then the values will be sent via uart.&lt;/p&gt;
&lt;p&gt;I have implemented a work around to reach the 8khz. I use the k_busy_wait() function to reach the 8Khz.&lt;/p&gt;
&lt;p&gt;But this is not really professional.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;I hope we can find a solution. :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC sampling @ 8kHz</title><link>https://devzone.nordicsemi.com/thread/203093?ContentTypeID=1</link><pubDate>Thu, 08 Aug 2019 13:24:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4d482069-82c6-4785-ab6c-8181a6e083d4</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Any progress on this? If not, can you upload the complete code with any modifications you have made to the test/example? (I assume you made more modifications since nRF91 is not&amp;nbsp;supported out of the box as it is not in the list of boards on line 33-38).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>