I am trying to share ADC output to Bluetooth device. But issue is as I am getting SAADC output via SPI protocol.
Now i have added SAADC+SPI in BLE app uart example, So when i call SPI function in main program Bluetooth stops advertising again when i comment SPI function call then Bluetooth starts advertising. So please look after the code i have attached.
Why Bluetooth stops advertising when SPI function is called? Even different TIMER i have defined, TIMER 2 but still I am finding issue.
Please suggest as I am trying to resolve this issue from 1week and its urgent.
#define SPI_INSTANCE 1 /**< SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
static volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
//#define TEST_STRING "Nordic"
static uint8_t m_tx_buf[2]; /**< TX buffer. */
static uint8_t m_rx_buf[40]; /**< RX buffer. */
//static const uint8_t m_length = sizeof(m_tx_buf); /**< Transfer length. */
/**
* @brief SPI user event handler.
* @param event
*/
void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
{
spi_xfer_done = true;
NRF_LOG_INFO(" Sent %d bytes: ", p_event->data.done.tx_length);
for(int i=0; i< p_event->data.done.tx_length;i++)
{
NRF_LOG_INFO(" ;(0x%x); ",p_event->data.done.p_tx_buffer[i], p_event->data.done.p_tx_buffer[i]);
}
//NRF_LOG_INFO("\r\n");
NRF_LOG_INFO(" Received %d bytes: ", p_event->data.done.rx_length);
for(int i=0; i< p_event->data.done.rx_length;i++)
{
NRF_LOG_INFO(" ;(0x%x); ",p_event->data.done.p_rx_buffer[i], p_event->data.done.p_rx_buffer[i]);
nrf_delay_ms(5);
}
//NRF_LOG_INFO("\r\n\n");
}
void spi_con(void)
{
nrf_gpio_cfg_output(18); //Configure pin 18 as output
NRF_CLOCK->TASKS_HFCLKSTART = 1; //Start high frequency clock
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
{
//Wait for HFCLK to start
}
NRF_CLOCK->EVENTS_HFCLKSTARTED = 0; //Clear event
//Configure GPIOTE to toggle pin 18
NRF_GPIOTE->CONFIG[0] = GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos |
GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos |
18 << GPIOTE_CONFIG_PSEL_Pos |
GPIOTE_CONFIG_OUTINIT_Low << GPIOTE_CONFIG_OUTINIT_Pos;
//Configure timer
NRF_TIMER1->PRESCALER = 3;
NRF_TIMER1->CC[0] = 1; // Adjust the output frequency by adjusting the CC.
NRF_TIMER1->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos;
NRF_TIMER1->TASKS_START = 1;
//Configure PPI
NRF_PPI->CH[0].EEP = (uint32_t) &NRF_TIMER1->EVENTS_COMPARE[0];
NRF_PPI->CH[0].TEP = (uint32_t) &NRF_GPIOTE->TASKS_OUT[0];
NRF_PPI->CHENSET = PPI_CHENSET_CH0_Enabled << PPI_CHENSET_CH0_Pos;
LEDS_CONFIGURE(BSP_LED_0_MASK);
LEDS_OFF(BSP_LED_0_MASK);
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi_config.ss_pin = SPI_SS_PIN;
spi_config.miso_pin = SPI_MISO_PIN;
spi_config.mosi_pin = SPI_MOSI_PIN;
spi_config.sck_pin = SPI_SCK_PIN;
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
NRF_LOG_INFO("SPI example started.");
// Reset rx buffer and transfer done flag
memset(m_rx_buf, 0, 40);
spi_xfer_done = false;
m_tx_buf[0]=0x0d;
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 40));
while (!spi_xfer_done)
{
__WFE();
}
}
#define SAMPLES_IN_BUFFER 20
volatile uint8_t state = 1;
static int value;
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;
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 400ms */
uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 100);
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);
}
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;
err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
int i;
NRF_LOG_INFO("ADC event number: %d", (int)m_adc_evt_counter);
for (i = 0; i < SAMPLES_IN_BUFFER; i++)
{
value= p_event->data.done.p_buffer[i];
NRF_LOG_INFO("%d ",value);
}
//uint16_t length = (uint16_t)index;
ble_nus_data_send(&m_nus,(uint8_t *)value,NULL,m_conn_handle);
m_adc_evt_counter++;
}
}
void saadc_init(void)
{
ret_code_t err_code;
nrf_saadc_channel_config_t channel_config =
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
err_code = nrf_drv_saadc_init(NULL, saadc_callback);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_channel_init(0, &channel_config);
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);
}
I am not getting whats wrong in code please suggest.