This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

SAADC scan mode of 3 channels --> buffer order swap

Hi,

I already read all the forum posts about this buffer order swap problem of the SAADC. Especially this one: https://devzone.nordicsemi.com/f/nordic-q-a/20291/offset-in-saadc-samples-with-easy-dma-and-ble/79053#79053

I tried to implement the PPI solution, but I am not able to bring this to work. I have still a swap in the order every second call of the callback function. Can anyone help me?

This is pretty much my firmware (without BLE stuff at the moment):

I'm using a compare event of the RTC to trigger the sample task in scan mode. Another compare event is used to enable the ppi channel again after disabling it after 10 samples per channel. The reason for that is that I want to sample at a high frequency but only 10 samples per second. The second PPI channel is used for the fix from the link above.

PPI:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void saadc_sampling_event_init(void)
{
ret_code_t err_code;
err_code = nrf_drv_ppi_init();
APP_ERROR_CHECK(err_code);
uint32_t rtc_compare_event_addr = nrf_drv_rtc_event_address_get(&rtc, NRF_RTC_EVENT_COMPARE_0);//nrf_drv_timer_compare_event_address_get(&m_timer, NRF_TIMER_CC_CHANNEL0);
uint32_t saadc_sample_task_addr = nrf_drv_saadc_sample_task_get();
/* setup ppi channel so that timer compare event is triggering sample task in SAADC */
err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel_1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel_2);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_assign(m_ppi_channel_1,
rtc_compare_event_addr,
saadc_sample_task_addr);
APP_ERROR_CHECK(err_code);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

SAADC:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#define SAMPLES_IN_BUFFER 10
#define ADC_CHANNEL_COUNT 3
void saadc_sampling_event_enable(void)
{
ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel_1);
APP_ERROR_CHECK(err_code);
}
void saadc_sampling_event_disable(void)
{
ret_code_t err_code = nrf_drv_ppi_channel_disable(m_ppi_channel_1);
APP_ERROR_CHECK(err_code);
}
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
int value = 0;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

RTC:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
static int i = 2;
if (int_type == NRF_DRV_RTC_INT_COMPARE0)
{
//nrf_gpio_pin_toggle(LED);
//nrf_drv_rtc_counter_clear(&rtc);
//reactivate compare irq!
nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * i++,true);
}
else if (int_type == NRF_DRV_RTC_INT_COMPARE1)
{
nrf_gpio_pin_toggle(LED);
nrf_drv_rtc_counter_clear(&rtc);
//reactivate compare irq!
nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 1,true);
nrf_drv_rtc_cc_set(&rtc,1,COMPARE_COUNTERTIME * 1000,true);
i = 2;
saadc_sampling_event_enable();
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Main:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main(void)
{
/* Configure board. */
bsp_board_init(BSP_INIT_LEDS);
bsp_board_leds_on();
bsp_board_init(BSP_INIT_BUTTONS);
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
lfclk_config();
rtc_config();
saadc_init();
saadc_sampling_event_init();
saadc_sampling_event_enable();
while(1)
{
if(measDone == 1)
{
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Thank you all and best regards! 
Manuel