Enabling 8 channels of SAADC and sending data via BLE in the most efficient way

Hi, 

I want to enable 8 channels of saadc in SE mode with 10 bits of resolution. the number of samples in every packet is 32, which means every channel have 4 samples in every packet. then I will put 5 buffers of saadc in 1 buffer and then I transmit it via BLE to the PC. for transmitting in the most efficient way, I put every 10 bits sample in 1.5 bytes as follow 

for ( i = 0; i < SAMPLES_IN_BUFFER; i+=2)
	{
		tx_payload.data[k] =   (uint8_t)(p_event->data.done.p_buffer[i]);
		tx_payload.data[k+1] = (uint8_t)(p_event->data.done.p_buffer[i] >> 8) | (uint8_t)(p_event->data.done.p_buffer[i+1] << 4);
		tx_payload.data[k+2] = (uint8_t)(p_event->data.done.p_buffer[i+1] >> 4);
		k+=3;
	}

I am getting the right value of adc but the frequency is not okay. for example, when I am sampling a 10 Hz signal, I receive a 20 Hz signal. 

Can you help me find the problem?

my whole code is as follow:

/**
 * Copyright (c) 2014 - 2020, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "sdk_common.h"
#include "nrf.h"
#include "nrf_esb.h"
#include "nrf_error.h"
#include "nrf_esb_error_codes.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf_clock.h"
#include "boards.h"
#include "nrf_delay.h"
#include "app_util.h"

/** user's code begins 1. 
*/
#include <stdio.h>
#include "nrf_drv_saadc.h"
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
#include "app_error.h"
#include "app_util_platform.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#include "bsp.h"

#include "nrf_drv_gpiote.h"
/** user's code ends 1. 
*/

/** user's code begins 2. 
*/

int Sleep    = 0;
int Command  = 1;
int Record   = 2;
int nrf_mode = 0;

#define E1 	NRF_SAADC_INPUT_AIN0  // Open //
#define E2 	NRF_SAADC_INPUT_AIN1  // BAT //
#define E3 	NRF_SAADC_INPUT_AIN2  // Open //
#define E4 	NRF_SAADC_INPUT_AIN3  // Ref //
#define E5 	NRF_SAADC_INPUT_AIN4  // E3 //
#define E6 	NRF_SAADC_INPUT_AIN5  // E4 //
#define E7 	NRF_SAADC_INPUT_AIN6  // E1 //
#define E8 	NRF_SAADC_INPUT_AIN7  // E2 //
#define Status_LED	10
#define Pin_CTRL		26
#define PIN_IN			25

int count=0;
uint32_t Record_time=0;
bool nrf_record_config = false;
#define SAMPLES_IN_BUFFER 32
volatile uint8_t state = 1;

static const nrf_drv_timer_t m_timer = NRF_DRV_TIMER_INSTANCE(0);
static nrf_saadc_value_t     m_buffer_pool[2][SAMPLES_IN_BUFFER];
static nrf_ppi_channel_t     m_ppi_channel;
static nrf_ppi_channel_t     m_ppi_channel0;
static nrf_esb_payload_t     tx_payload = NRF_ESB_CREATE_PAYLOAD (0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
																																		0x00,0x00);

static nrf_esb_payload_t     rx_payload;

static uint32_t              Sending_packet_passed;
static uint32_t              Sending_packet_failed;
int i,k;
uint8_t flag=0;
uint8_t iteration=1;
uint32_t time_count_m  = 0;
uint32_t packet=0;
const nrf_drv_timer_t TIMER_Sleep = NRF_DRV_TIMER_INSTANCE(1);

/* Functions */

void timer_handler(nrf_timer_event_t event_type, void * p_context);

void saadc_sampling_event_init(void);


void saadc_sampling_event_enable(void);

void saadc_callback(nrf_drv_saadc_evt_t const * p_event);


void saadc_init(void);

void nrf_esb_event_handler(nrf_esb_evt_t const * p_event);

void clocks_start( void );

uint32_t esb_init_ptx( void );
uint32_t esb_init_prx( void );

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action);

void gpio_init( void );
void delay_s(uint32_t s_time);
void delay_m(uint32_t m_time);
void delay_h(uint32_t h_time);
void stop_saadc(void);
void TIMER_Sleep_event_handler(nrf_timer_event_t event_type, void* p_context);
void timer_init(void);
/** user's code ends 2. 
*/

int main(void)
{
		ret_code_t err_code;
    gpio_init();
		
    err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();
	
	  clocks_start();

		/** user's code begins 3. 
		*/
		NRF_POWER->DCDCEN=1;
	
		ret_code_t ret_code = nrf_pwr_mgmt_init();
    APP_ERROR_CHECK(ret_code);
		
		/** usr's code ends 3. 
		*/ 
	
    while (true)
    {
			count=0;		
			if (nrf_mode == Sleep)
			{	
				nrf_gpio_pin_clear(Pin_CTRL);
				nrf_gpio_pin_clear(Status_LED);
				
				timer_init();
				
				nrf_pwr_mgmt_run();
				
				nrf_mode = Command;
				nrf_drv_timer_disable(&TIMER_Sleep);
				nrf_drv_timer_uninit(&TIMER_Sleep);
				
			}
			if (nrf_mode == Command)
			{	
				err_code = esb_init_prx();
				APP_ERROR_CHECK(err_code);
				
				err_code = nrf_esb_start_rx();
				APP_ERROR_CHECK(err_code);
				
				while(nrf_mode == Command)
				{
					nrf_gpio_pin_toggle(Status_LED);
					nrf_delay_ms(200);
					count++;
					if (count==150)
					{
						nrf_mode = Sleep;
						err_code = nrf_esb_stop_rx();
						APP_ERROR_CHECK(err_code);
						nrf_esb_disable();
						nrf_saadc_disable();
						count = 0;
					}
				}
			}
			if (nrf_mode == Record)
			{
				if(nrf_record_config == true)
				{

					nrf_gpio_pin_set(Pin_CTRL);
				
					err_code = nrf_esb_stop_rx();
					APP_ERROR_CHECK(err_code); 
					
					err_code = esb_init_ptx();
					APP_ERROR_CHECK(err_code);
					
					saadc_init();
					saadc_sampling_event_init();
					saadc_sampling_event_enable();
					nrf_record_config = false;
				}
				else
					nrf_pwr_mgmt_run();
			}	
    }	
}


/**
 * @brief Handler for timer events.
 */
void TIMER_Sleep_event_handler(nrf_timer_event_t event_type, void* p_context)
{
    switch (event_type)
    {
        case NRF_TIMER_EVENT_COMPARE1:
					nrf_drv_timer_disable(&TIMER_Sleep);
				  nrf_drv_timer_uninit(&TIMER_Sleep);
					for (int i=0; i<10; i++)
					{
						nrf_gpio_pin_toggle(Status_LED);
						nrf_delay_ms(1000);
					}
					break;
        default:
          //Do nothing.
          break;
    }
}
void timer_init(void)
{
//		uint32_t time_ms = (4*60+50)*1000; //Time(in miliseconds) between consecutive compare events.
		uint32_t time_ms = 0.5*60*1000;  
		uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;

    //Configure TIMER_Sleep for generating simple light effect - leds on board will invert his state one after the other.
    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    err_code = nrf_drv_timer_init(&TIMER_Sleep, &timer_cfg, TIMER_Sleep_event_handler);
    APP_ERROR_CHECK(err_code);

    time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_Sleep, time_ms);

    nrf_drv_timer_extended_compare(
         &TIMER_Sleep, NRF_TIMER_CC_CHANNEL1, time_ticks, NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);

    nrf_drv_timer_enable(&TIMER_Sleep);
}

void timer_handler(nrf_timer_event_t event_type, void * p_context)
{
	
}

void saadc_sampling_event_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);

    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(&m_timer, &timer_cfg, timer_handler);
    APP_ERROR_CHECK(err_code);

    /* setup m_timer for compare event every 50us */
    uint32_t ticks = nrf_drv_timer_us_to_ticks(&m_timer, 50);
    nrf_drv_timer_extended_compare(&m_timer,
                                   NRF_TIMER_CC_CHANNEL0,
                                   ticks,
                                   NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
                                   false);
    nrf_drv_timer_enable(&m_timer);

    uint32_t timer_compare_event_addr = 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);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_ppi_channel_assign(m_ppi_channel,
                                          timer_compare_event_addr,
                                          saadc_sample_task_addr);
    APP_ERROR_CHECK(err_code);
		
		err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel0);
    APP_ERROR_CHECK(err_code);
		
		err_code = 		nrf_drv_ppi_channel_assign(m_ppi_channel0, 
																						 nrf_saadc_event_address_get(NRF_SAADC_EVENT_END),
																						 nrf_saadc_task_address_get(NRF_SAADC_TASK_START));
    APP_ERROR_CHECK(err_code);
}



void saadc_sampling_event_enable(void)
{
    ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);

    APP_ERROR_CHECK(err_code);
	
	  err_code = nrf_drv_ppi_channel_enable(m_ppi_channel0);

    APP_ERROR_CHECK(err_code);
}


void saadc_callback(nrf_drv_saadc_evt_t const * p_event) //ok
{
    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_ERROR_CHECK(err_code);

        if (flag == 0)
				{
					if (iteration == 5)
					{
						for ( i = 0; i < SAMPLES_IN_BUFFER; i+=2)
						{
							tx_payload.data[k] =   (uint8_t)(p_event->data.done.p_buffer[i]);
							tx_payload.data[k+1] = (uint8_t)(p_event->data.done.p_buffer[i] >> 8) | (uint8_t)(p_event->data.done.p_buffer[i+1] << 4);
							tx_payload.data[k+2] = (uint8_t)(p_event->data.done.p_buffer[i+1] >> 4);
							k+=3;
						}
						iteration = 1;
						flag=1;
					}
					if (iteration == 4)
					{
						for ( i = 0; i < SAMPLES_IN_BUFFER; i+=2)
						{
							tx_payload.data[k] =   (uint8_t)(p_event->data.done.p_buffer[i]);
							tx_payload.data[k+1] = (uint8_t)(p_event->data.done.p_buffer[i] >> 8) | (uint8_t)(p_event->data.done.p_buffer[i+1] << 4);
							tx_payload.data[k+2] = (uint8_t)(p_event->data.done.p_buffer[i+1] >> 4);
							k+=3;
						}
						iteration = 5;
					}
					if (iteration == 3)
					{
						for ( i = 0; i < SAMPLES_IN_BUFFER; i+=2)
						{
							tx_payload.data[k] =   (uint8_t)(p_event->data.done.p_buffer[i]);
							tx_payload.data[k+1] = (uint8_t)(p_event->data.done.p_buffer[i] >> 8) | (uint8_t)(p_event->data.done.p_buffer[i+1] << 4);
							tx_payload.data[k+2] = (uint8_t)(p_event->data.done.p_buffer[i+1] >> 4);
							k+=3;
						}
						iteration = 4;
					}
					if (iteration == 2)
					{
						for ( i = 0; i < SAMPLES_IN_BUFFER; i+=2)
						{
							tx_payload.data[k] =   (uint8_t)(p_event->data.done.p_buffer[i]);
							tx_payload.data[k+1] = (uint8_t)(p_event->data.done.p_buffer[i] >> 8) | (uint8_t)(p_event->data.done.p_buffer[i+1] << 4);
							tx_payload.data[k+2] = (uint8_t)(p_event->data.done.p_buffer[i+1] >> 4);
							k+=3;
						}
						iteration = 3;

					}
					
					if (iteration == 1 && flag ==0)
					{
					  k=1;
						for ( i = 0; i < SAMPLES_IN_BUFFER; i+=2)
						{
							tx_payload.data[k] =   (uint8_t)(p_event->data.done.p_buffer[i]);
							tx_payload.data[k+1] = (uint8_t)(p_event->data.done.p_buffer[i] >> 8) | (uint8_t)(p_event->data.done.p_buffer[i+1] << 4);
							tx_payload.data[k+2] = (uint8_t)(p_event->data.done.p_buffer[i+1] >> 4);
							k+=3;
						}
						iteration = 2;
					}	
				}
				
				if (flag == 1)
				{
					tx_payload.data[0]=Sending_packet_passed;
					tx_payload.data[241]=Sending_packet_failed;
					if (nrf_esb_write_payload(&tx_payload) == NRF_SUCCESS)
					{
						Sending_packet_passed++;
					}
					else
					{
						Sending_packet_failed++;
					}
					flag=0;
					
				}
    }
}
void stop_saadc(void)
{
    nrf_drv_timer_disable(&m_timer);
    nrf_drv_timer_uninit(&m_timer);
    nrf_drv_ppi_channel_disable(m_ppi_channel);
		nrf_drv_ppi_channel_disable(m_ppi_channel0);
    nrf_drv_ppi_uninit();
    nrf_drv_saadc_abort();
    nrf_drv_saadc_uninit();
	  while(nrf_drv_saadc_is_busy());   
}

void saadc_init(void) //ok
{
		ret_code_t err_code;

		nrf_saadc_channel_config_t channel_config0 =
					NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(E1);
		nrf_saadc_channel_config_t channel_config1 =
					NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(E2);	
    nrf_saadc_channel_config_t channel_config2 =
					NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(E3);
		nrf_saadc_channel_config_t channel_config3 =
					NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(E4);
		nrf_saadc_channel_config_t channel_config4 =
					NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(E5);
		nrf_saadc_channel_config_t channel_config5 =
					NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(E6);	
    nrf_saadc_channel_config_t channel_config6 =
					NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(E7);
		nrf_saadc_channel_config_t channel_config7 =
					NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(E8);	

    err_code = nrf_drv_saadc_init(NULL, saadc_callback);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_channel_init(0, &channel_config0);
    APP_ERROR_CHECK(err_code);
		err_code = nrf_drv_saadc_channel_init(1, &channel_config1);
    APP_ERROR_CHECK(err_code);
	  err_code = nrf_drv_saadc_channel_init(2, &channel_config2);
    APP_ERROR_CHECK(err_code);
		err_code = nrf_drv_saadc_channel_init(3, &channel_config3);
    APP_ERROR_CHECK(err_code);
		err_code = nrf_drv_saadc_channel_init(4, &channel_config4);
    APP_ERROR_CHECK(err_code);
		err_code = nrf_drv_saadc_channel_init(5, &channel_config5);
    APP_ERROR_CHECK(err_code);
	  err_code = nrf_drv_saadc_channel_init(6, &channel_config6);
    APP_ERROR_CHECK(err_code);
		err_code = nrf_drv_saadc_channel_init(7, &channel_config7);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER);
    APP_ERROR_CHECK(err_code);
}

/** user's code ends 2. 
*/

void nrf_esb_event_handler(nrf_esb_evt_t const * p_event)
{

    switch (p_event->evt_id)
    {
        case NRF_ESB_EVENT_TX_SUCCESS:
            NRF_LOG_DEBUG("TX SUCCESS EVENT");
						nrf_gpio_pin_set(Status_LED);
						packet++;
						if(packet==60000)
						{
							time_count_m++;
							packet=0;
						}
						if(time_count_m == Record_time)
						{
							stop_saadc();
							nrf_mode = Sleep;
							packet=0;
							time_count_m=0;
							nrf_gpio_pin_clear(Status_LED);
						}
            break;
        case NRF_ESB_EVENT_TX_FAILED:
            NRF_LOG_DEBUG("TX FAILED EVENT");
            (void) nrf_esb_flush_tx();
            (void) nrf_esb_start_tx();
						nrf_gpio_pin_clear(Status_LED);
            break;
        case NRF_ESB_EVENT_RX_RECEIVED:
            NRF_LOG_DEBUG("RX RECEIVED EVENT");
            while (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS)
            {
							 Record_time = rx_payload.data[1];
							
		           bsp_board_leds_off();							  
							 nrf_mode = Record;
							 nrf_record_config = true;
            }
            break;
    }
}
 

void clocks_start( void )
{
    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_HFCLKSTART = 1;

    while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
}


uint32_t esb_init_ptx( void )
{
    uint32_t err_code;
    uint8_t base_addr_0[4] = {0xE7, 0xE7, 0xE7, 0xE7};
    uint8_t base_addr_1[4] = {0xC2, 0xC2, 0xC2, 0xC2};
    uint8_t addr_prefix[8] = {0xE7, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8 };

    nrf_esb_config_t nrf_esb_config         = NRF_ESB_DEFAULT_CONFIG;
    nrf_esb_config.protocol                 = NRF_ESB_PROTOCOL_ESB_DPL;
		nrf_esb_config.tx_output_power					= NRF_ESB_TX_POWER_0DBM;
    nrf_esb_config.retransmit_delay         = 600;
    nrf_esb_config.bitrate                  = NRF_ESB_BITRATE_2MBPS_BLE;
		nrf_esb_config.payload_length						= 242;
    nrf_esb_config.event_handler            = nrf_esb_event_handler;
    nrf_esb_config.mode                     = NRF_ESB_MODE_PTX;
    nrf_esb_config.selective_auto_ack       = false;

    err_code = nrf_esb_init(&nrf_esb_config);

    VERIFY_SUCCESS(err_code);

    err_code = nrf_esb_set_base_address_0(base_addr_0);
    VERIFY_SUCCESS(err_code);

    err_code = nrf_esb_set_base_address_1(base_addr_1);
    VERIFY_SUCCESS(err_code);

    err_code = nrf_esb_set_prefixes(addr_prefix, NRF_ESB_PIPE_COUNT);
    VERIFY_SUCCESS(err_code);

    return err_code;
}

uint32_t esb_init_prx( void )
{
    uint32_t err_code;
    uint8_t base_addr_0[4] = {0xE7, 0xE7, 0xE7, 0xE7};
    uint8_t base_addr_1[4] = {0xC2, 0xC2, 0xC2, 0xC2};
    uint8_t addr_prefix[8] = {0xE7, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8 };

    nrf_esb_config_t nrf_esb_config         = NRF_ESB_DEFAULT_CONFIG;
    nrf_esb_config.protocol                 = NRF_ESB_PROTOCOL_ESB_DPL;
		nrf_esb_config.tx_output_power					= NRF_ESB_TX_POWER_0DBM;
    nrf_esb_config.retransmit_delay         = 600;
    nrf_esb_config.bitrate                  = NRF_ESB_BITRATE_2MBPS_BLE;
		nrf_esb_config.payload_length						= 242;
    nrf_esb_config.event_handler            = nrf_esb_event_handler;
    nrf_esb_config.mode                     = NRF_ESB_MODE_PRX;
    nrf_esb_config.selective_auto_ack       = false;

    err_code = nrf_esb_init(&nrf_esb_config);

    VERIFY_SUCCESS(err_code);

    err_code = nrf_esb_set_base_address_0(base_addr_0);
    VERIFY_SUCCESS(err_code);

    err_code = nrf_esb_set_base_address_1(base_addr_1);
    VERIFY_SUCCESS(err_code);

    err_code = nrf_esb_set_prefixes(addr_prefix, NRF_ESB_PIPE_COUNT);
    VERIFY_SUCCESS(err_code);

    return err_code;
}

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{

}

void gpio_init( void )
{
    ret_code_t err_code;
	
		nrf_gpio_cfg_output(Status_LED);
		nrf_gpio_cfg_output(Pin_CTRL);
	
	  err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
    in_config.pull = NRF_GPIO_PIN_PULLDOWN;

    err_code = nrf_drv_gpiote_in_init(PIN_IN, &in_config, in_pin_handler);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_event_enable(PIN_IN, true);
}


void delay_s(uint32_t s_time)
{
	if (s_time == 0)
    {
        return;
    }
  do 
		{
			nrf_delay_ms(1000);
    } while (--s_time);
}

void delay_m(uint32_t m_time)
{
	if (m_time == 0)
    {
        return;
    }
  do 
		{
			delay_s(60);
    } while (--m_time);
}
void delay_h(uint32_t h_time)
{
	if (h_time == 0)
    {
        return;
    }
  do 
		{
			delay_m(60);
    } while (--h_time);
}

Parents Reply
  • No, the problem is the order of samples in the buffer swap. it means that I set the channel as [1 2 3 .. 8], somehow sometimes I get [3 4 5 ...1 2], or sth like that. I understand that others have the same problem and it can be solved using a PPI as described here. I added this solution but it seems the problem still exists. 

Children
No Data
Related