Hi all,
I am using nRF52832 with SDK 13.0.0. And I use the ble_perpheral -> ble_app_uart example.
I am trying to send data through BLE and. I'm using timer 2 to sampling SAADC data with sampling frequency is 500Hz. In timer 2 interrupt function, I have 2 event compare to do my work.
I get 6 byte each time sampling and I wait for in enough 60 byte and then send data through BLE by ble_nus_string_send function that mean every 20ms I send the data one time, like this code below:
void TIMER2_IRQHandler(void)
{
if ((NRF_TIMER2->EVENTS_COMPARE[0] != 0) && ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0))
{
nrf_gpio_pin_write(19, 1);
nrf_gpio_pin_write(18, 0);
NRF_TIMER2->EVENTS_COMPARE[0] = 0; //Clear compare register 0 event
saadc_start();
while(!NRF_SAADC->EVENTS_END){}
NRF_SAADC->EVENTS_END = 0;
if(((uint16_t)data_buffer)>>15)
{
data_buffer=data_buffer&0xffff0000;
}
if(data_buffer>>31)
{
data_buffer=data_buffer&0x0000ffff;
}
saadc_UartSend[count] = data_buffer&0x3f;
count++;
saadc_UartSend[count] = ((data_buffer >> 6UL)&0x3f)|0x40;
count++;
saadc_UartSend[count] = ((data_buffer >> 16UL)&0x3f)|0x80;
count++;
saadc_UartSend[count] = ((data_buffer >> 22UL)&0x3f)|0xc0;
count++;
}
if ((NRF_TIMER2->EVENTS_COMPARE[1] != 0) && ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE1_Msk) != 0))
{
NRF_TIMER2->EVENTS_COMPARE[1] = 0; //Clear compare register 1 event
saadc_start();
while(!NRF_SAADC->EVENTS_END){}
NRF_SAADC->EVENTS_END = 0;
if(((uint16_t)data_buffer)>>15)
{
data_buffer=data_buffer&0xffff0000;
}
if(data_buffer>>31)
{
data_buffer=data_buffer&0x0000ffff;
} // clear EVENTS_END
saadc_UartSend[count] = ((data_buffer>> 16UL)&0x3f)|0x80; // divide and add index bit
count++;
saadc_UartSend[count]= ((data_buffer>> 22UL)&0x3f)|0xc0;
count++;
uint32_t err_code;
if(count==60)
{
do
{
err_code = ble_nus_string_send(&m_nus, saadc_UartSend, 60);
if ( (err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) )
{
APP_ERROR_CHECK(err_code);
}
} while (err_code == NRF_ERROR_BUSY);
count=0;
}
nrf_gpio_pin_write(19, 0);
nrf_gpio_pin_write(18, 1);
}
}
The timer_init() function is like this:
void timer_init()
{
NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer; // Set the timer in Counter Mode // 0x4000A000
NRF_TIMER2->TASKS_CLEAR = 1; // clear the task first to be usable for later
NRF_TIMER2->PRESCALER = 7; //Set prescaler. Higher number gives slower timer. Prescaler = 0 gives 16MHz timer
NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_08Bit; //Set counter to 16 bit resolution
NRF_TIMER2->CC[0] = 0; //Set value for TIMER2 compare register 0
NRF_TIMER2->CC[1] = 60; //Set value for TIMER2 compare register 1
NRF_TIMER2->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos)|(TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos);
NVIC_SetPriority(TIMER2_IRQn, 6);
NVIC_EnableIRQ(TIMER2_IRQn);
NRF_TIMER2->TASKS_START = 1;
}
And in saadc_init() function I setup SAADC in scan_mode:
void saadc_init(void)
{
NRF_SAADC->RESOLUTION = 2; // 12 BIT MODE
NRF_SAADC->OVERSAMPLE = 0; //bypass oversample
NRF_SAADC->CH[0].CONFIG = 0x0 << 0UL | 0x0 << 4UL | 0x0 << 8UL | 0x0 << 12UL | 0x2 << 16UL;
NRF_SAADC->CH[0].PSELP = 1; // NRF_SAADC_INPUT_AIN0
NRF_SAADC->CH[0].PSELN = 0; // NRF_SAADC_INPUT_DISABLED
NRF_SAADC->CH[1].CONFIG = 0x0 << 0UL | 0x0 << 4UL | 0x0 << 8UL | 0x0 << 12UL | 0x2 << 16UL;
NRF_SAADC->CH[1].PSELP = 2; // NRF_SAADC_INPUT_AIN0
NRF_SAADC->CH[1].PSELN = 0; // NRF_SAADC_INPUT_DISABLED
NRF_SAADC->INTEN = 0x00000;
NRF_SAADC->RESULT.PTR = (uint32_t)(&data_buffer);
NRF_SAADC->RESULT.MAXCNT = 2; // number of samples
}
when I test with nRF52 Connect app. It can read the data is received in log file like. According to the time line show in log file, it seem like I got the data every 20ms without problem.

But when i try with nRF ToolBox UART the app is pathetic lag and then stopped working and I don't know why? My connection interval equal 7.5ms
Can anyone advise on how this problem can be solved?
Thanks in advance!