Hello,
this is my first post on DevZone:-)
I'm working with DWM1001-DEV board. I know that this board contains an nRF52832.
I'm configuring the SAADC to measure the battery level. The board has available two pins for them, the P0.31 and P0.30.
I compile the SAADC example project from the Nordic Web page on board and this work.
Now I want to compile the code with the user thread available for developers but the
saadc_callback never is called. I'm using timer 2.
When I configure them to NRF_TIMER_EVENT_COMPARE0 event this works because the timer2_event_handler is always called.
I'm debugging both projects and comparing and all functions, return NRF_SUCCESS.
#include "dwm.h"
#include <stdio.h>
#include "nrf.h"
#include "nrf_drv_timer.h"
#include "nrf_drv_saadc.h"
#include "nrf_drv_ppi.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_delay.h"
/* Thread priority */
#ifndef THREAD_APP_PRIO
#define THREAD_APP_PRIO 20
#endif /* THREAD_APP_PRIO */
/* Thread stack size */
#ifndef THREAD_APP_STACK_SIZE
#define THREAD_APP_STACK_SIZE (3 * 1024)
#endif /* THREAD_APP_STACK_SIZE */
#define SAMPLES_IN_BUFFER 5
#define APP_ERR_CHECK(err_code) \
do { \
if ((err_code) != DWM_OK) \
printf("err: line(%u) code(%u)", __LINE__, (err_code));\
} while (0)
volatile uint8_t state = 1;
static const nrf_drv_timer_t m_timer = NRF_DRV_TIMER_INSTANCE(2);
static nrf_saadc_value_t m_buffer_pool[2][SAMPLES_IN_BUFFER];
static nrf_ppi_channel_t m_ppi_channel;
static uint32_t m_adc_evt_counter;
const nrf_drv_timer_t timer2 = NRF_DRV_TIMER_INSTANCE(2);
void timer2_event_handler(nrf_timer_event_t event_type, void * p_context)
{
ret_code_t ret_value;
nrf_saadc_value_t adc_value;
static int cnt = 0;
switch (event_type) {
case NRF_TIMER_EVENT_COMPARE0:
break;
default:
/*Do nothing.*/
break;
}
}
/* interrupt handled from nordic driver, see nrf_drv_timer.c */
extern void TIMER2_IRQHandler(void);
void dwm_irq_hndlr(void *p_context)
{
TIMER2_IRQHandler();
}
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
int vec[5];
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
ret_code_t err_code;
err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
APP_ERR_CHECK(err_code);
int i;
for (i = 0; i < SAMPLES_IN_BUFFER; i++)
{
vec[i]=p_event->data.done.p_buffer[i];
}
m_adc_evt_counter++;
}
}
void app_thread_entry(uint32_t data)
{
int count=0;
dwm_irq_t irq;
int rv;
uint32_t err_code = NRF_SUCCESS;
uint32_t time_ticks;
saadc_init();
saadc_sampling_event_init();
saadc_sampling_event_enable();
while (1)
{
count++;
if(count==30000)
count=0;
}
}
void saadc_sampling_event_init()
{
ret_code_t err_code;
err_code = nrf_drv_ppi_init();
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
err_code = nrf_drv_timer_init(&timer2, &timer_cfg, timer2_event_handler);
/* setup m_timer for compare event every 400ms */
uint32_t ticks = nrf_drv_timer_ms_to_ticks(&timer2, 4000);
nrf_drv_timer_extended_compare(&timer2, NRF_TIMER_CC_CHANNEL0,ticks,NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,false);
nrf_drv_timer_enable(&timer2);
uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&timer2,NRF_TIMER_CC_CHANNEL0);
uint32_t saadc_sample_task_addr = nrf_drv_saadc_sample_task_get();
err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel);
err_code = nrf_drv_ppi_channel_assign(m_ppi_channel,timer_compare_event_addr,saadc_sample_task_addr);
}
void saadc_sampling_event_enable(void)
{
ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);
}
void saadc_init(void)
{
ret_code_t err_code;
nrf_saadc_channel_config_t channel_config =NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(nrf_drv_saadc_gpio_to_ain(31));
err_code = nrf_drv_saadc_init(NULL, saadc_callback);
err_code = nrf_drv_saadc_channel_init(0, &channel_config);
err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);
err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER);
}
void dwm_user_start(void)
{
uint8_t hndl;
int rv;
//dwm_shell_compile();
//Disabling ble by default as softdevice prevents debugging with breakpoints (due to priority)
//dwm_ble_compile();
//dwm_le_compile();
rv = dwm_thread_create(THREAD_APP_PRIO, app_thread_entry, (void*)NULL,"app", THREAD_APP_STACK_SIZE, &hndl);
APP_ERR_CHECK(rv);
dwm_thread_resume(hndl);
}
Will there be any incompatibility with the use of SAADC?
Best Regards
CostaT