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

Using timer interrupt in uart_ble application

Hi,

I am using timer interrupt in my code. In that I will be generating interrupt after certain time interval, after the interrupt it should process the incoming data from the UART. For every interrupt ble_nus will send 20 bytes of data from the buffer. The buffer is FIFO circular buffer, so once all the data are processed the pointer location changed to 0'th place if there is no new data within the next interrupt.

In detail, my buffer size is 2048. If the data is filled upto 1500,after processing the every data it is jumping to 0'th location. So the same data is being sent again and again. So I want the solution to avoid this by managing the timer in correct manner, so that data won't be sent again and again.

Here is my code snippet.

   void start_timer(void)
{		
  NRF_TIMER2->MODE = TIMER_MODE_MODE_Counter;              // Set the timer in Counter Mode
  NRF_TIMER2->TASKS_CLEAR = 1;                             // clear the task first to be usable for later
	NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;		   //Set counter to 16 bit resolution
	NRF_TIMER2->CC[0] = 5;                               //Set value for TIMER2 compare register 0
	NRF_TIMER2->CC[1] = 0;                                   //Set value for TIMER2 compare register 1
		
  // Enable interrupt on Timer 2, both for CC[0] and CC[1] compare match events
	NRF_TIMER2->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos) | (TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos);
  NVIC_EnableIRQ(TIMER2_IRQn);
		
  NRF_TIMER2->TASKS_START = 1;               // Start TIMER2
}

/** TIMTER2 peripheral interrupt handler. This interrupt handler is called whenever there it a TIMER2 interrupt
 */
void TIMER2_IRQHandler(void)
{
	//for(counter=0; counter<=var; counter++)
	//{
  if ((NRF_TIMER2->EVENTS_COMPARE[0] != 0) && ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0))
  {
		NRF_TIMER2->EVENTS_COMPARE[0] = 0;	       //Clear compare register 0 event	
		flag=1;         //Set flag
		
  }
	//counter++;
//}
	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
		
  }

}

Handler function:

       NRF_TIMER2->TASKS_COUNT = 1;        //Increment the timer
       nrf_delay_us(1);                   //Create 1us delay before incrementing the timer again
       if( flag ==1 && i<=102)  //i- pointer to move the position of buffer
   {
			
 err_code = ble_nus_string_send(&m_nus, data_array+(i*20),20);
     if (err_code != NRF_ERROR_INVALID_STATE)
    {
       APP_ERROR_CHECK(err_code);
     }
i++;
      flag=0;
		 
	 }
if(i==102)   // once the maximum size is reached it will go to 0th place
{
    i=0;    
}
		 
  }

Waiting for the quick response... Thanks...

Related