Hi all,
i am getting values properly, if i set 250 samples per second. but if i set 1000 samples (1ms), i can not get values after some moments. it is showing time out error (0*08) in nrf connect application.
i am enabling one channel, taking samples in 1 milli second, making buffer of 6 samples and sending over NUS. well, this is working during 4 or above milli second sampling rate. Also, connection minimum and maximum interval have been set to 10 and 100 respectively. still, unable to fix the same.
it is highly appreciated if anyone help to figure it out the same to fix...
Here i am attaching the code snippet..
#define MIN_CONN_INTERVAL MSEC_TO_UNITS(8, UNIT_1_25_MS) /**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
#define MAX_CONN_INTERVAL MSEC_TO_UNITS(75, UNIT_1_25_MS) /**< Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */
#define SLAVE_LATENCY 0 /**< Slave latency. */
#define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds), Supervision Timeout uses 10 ms units. */
#define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
#define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
#define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */
#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
#define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
#define SAADC_SAMPLES_IN_BUFFER 6
#define SAADC_SAMPLE_RATE 1
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_config = NRF_DRV_TIMER_DEFAULT_CONFIG;
timer_config.frequency = NRF_TIMER_FREQ_31250Hz;
err_code = nrf_drv_timer_init(&m_timer, &timer_config, timer_handler);
APP_ERROR_CHECK(err_code);
/* setup m_timer for compare event */
uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer,SAADC_SAMPLE_RATE);
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_event_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_event_addr);
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);
}
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
ret_code_t err_code;
uint16_t adc_value;
uint16_t bytes_to_send;
uint8_t value[SAADC_SAMPLES_IN_BUFFER*2];
// set buffers
err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAADC_SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
// print samples on hardware UART and parse data for BLE transmission
//printf("ADC event number: %d\r\n",(int)m_adc_evt_counter);
for (int i = 0; i < SAADC_SAMPLES_IN_BUFFER; i++)
{
printf("E: %d\r\n", p_event->data.done.p_buffer[i]);
adc_value = p_event->data.done.p_buffer[i];
value[i*2] = adc_value;
value[(i*2)+1] = adc_value >> 8;
}
// Send data over BLE via NUS service. Create string from samples and send string with correct length.
uint8_t nus_string[50];
bytes_to_send = sprintf(nus_string,
"E1:%d E2:%d E3:%d E4:%d E5:%d E6:%d",
p_event->data.done.p_buffer[0],p_event->data.done.p_buffer[1],p_event->data.done.p_buffer[2],p_event->data.done.p_buffer[3],p_event->data.done.p_buffer[4],
p_event->data.done.p_buffer[5]);
err_code = ble_nus_data_send(&m_nus, nus_string, &bytes_to_send, m_conn_handle);
if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_NOT_FOUND))
{
APP_ERROR_CHECK(err_code);
}
m_adc_evt_counter++;
}
}
void saadc_init(void)
{
ret_code_t err_code;
nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;
nrf_saadc_channel_config_t channel_0_config =
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN3);
channel_0_config.gain = NRF_SAADC_GAIN1_4;
channel_0_config.reference = NRF_SAADC_REFERENCE_VDD4;
err_code = nrf_drv_saadc_init(&saadc_config, saadc_callback);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_channel_init(0, &channel_0_config);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0],SAADC_SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1],SAADC_SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
}
/**@brief Application main function.
*/
int main(void)
{
bool erase_bonds;
// Initialize.
uart_init();
log_init();
timers_init();
buttons_leds_init(&erase_bonds);
power_management_init();
ble_stack_init();
gap_params_init();
gatt_init();
services_init();
advertising_init();
conn_params_init();
nrf_gpio_cfg_output(6);
nrf_gpio_pin_set(6);
saadc_sampling_event_init();
saadc_init();
saadc_sampling_event_enable();
// Start execution.
printf("\r\nUART started.\r\n");
NRF_LOG_INFO("Debug logging for UART over RTT started.");
advertising_start();
// Enter main loop.
for (;;)
{
idle_state_handle();
}
}