-
Notifications
You must be signed in to change notification settings - Fork 137
SAADC Advanced mode with IRQs
Karol Lasończyk edited this page Dec 6, 2019
·
1 revision
#include <stdbool.h>
#include <stdint.h>
#include <nrfx_saadc.h>
#define SAMPLES_NUMBER 1000
static volatile bool is_ready;
static nrf_saadc_value_t sample0[SAMPLES_NUMBER];
static nrf_saadc_value_t sample1[SAMPLES_NUMBER];
static nrf_saadc_value_t * p_bufs[2] = {sample0, sample1};
static uint32_t buf_to_load;
static nrf_saadc_value_t avg;
nrfx_saadc_channel_t channel = NRFX_SAADC_DEFAULT_CHANNEL_SE(NRF_SAADC_INPUT_AIN1, 0);
static void handle_error(nrfx_err_t error_code)
{
if (error_code!= NRFX_SUCCESS)
{
// error
while(1){};
}
}
void event_handler(nrfx_saadc_evt_t const * p_event)
{
switch (p_event->type)
{
case NRFX_SAADC_EVT_DONE:
// Buffer with data is available here: p_event->data.done.p_buffer
avg = p_event->data.done.p_buffer[0];
for (int i = 1; i < p_event->data.done.size; ++i)
{
avg += p_event->data.done.p_buffer[i];
avg /= 2;
}
is_ready = true;
break;
case NRFX_SAADC_EVT_BUF_REQ:
nrfx_saadc_buffer_set(p_bufs[buf_to_load % 2], SAMPLES_NUMBER);
buf_to_load++;
break;
default:
break;
}
}
int main(void)
{
nrfx_err_t err_code;
nrfx_saadc_adv_config_t config = {
.oversampling = NRF_SAADC_OVERSAMPLE_DISABLED, // Disable oversample.
.burst = false, // Disable burst.
.internal_timer_cc = 1600, // Sample at 10kHz.
.start_on_end = true, // Latch next buffer at END event in interrupt.
};
err_code = nrfx_saadc_init(NRFX_SAADC_DEFAULT_CONFIG_IRQ_PRIORITY);
handle_error(err_code);
err_code = nrfx_saadc_channels_config(&channel, 1);
handle_error(err_code);
err_code = nrfx_saadc_advanced_mode_set((1<<0),
NRF_SAADC_RESOLUTION_10BIT,
&config,
event_handler);
handle_error(err_code);
err_code = nrfx_saadc_buffer_set(p_bufs[buf_to_load % 2], SAMPLES_NUMBER);
handle_error(err_code);
buf_to_load++;
is_ready = false;
err_code = nrfx_saadc_mode_trigger();
handle_error(err_code);
while (1)
{
while(!is_ready){
__SEV();
__WFE();
};
is_ready = false;
// some_process_func(avg);
}
}