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

Advertising and Scanning BLE packet using Timer

Hi,

I am trying to create a BLE node which can perform dual role(advertising/scanning) to create mesh of multiple nodes. I am planning to do it with the help of timer. It will take data from one node and pass it on to other kinda like the ble_app_hrs_rscs_relay example but instead of connecting to any node I am just advertising my data. So the the other node simply has to scan it, copy it and advertise further. Till now I am able to advertise my custom data and scan it on the other device. I am also able to run simple timer code but when i integrate the timer code in my main function it simply stops the timer(timer wont work...stays in the while loop). Now as far I know BLE stack uses timer0 for the ble event handler routine calls but timer1 and timer2 are free(I am using timer 2) then why isn't the timer handler getting called?

Can anybody guide and tell me what I a doing wrong?

int main(void){
LED_init();
SEGGER_RTT_printf(0,"Inside the main\n"); //for debugging 
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL);
ble_stack_init();
start_timer(); 	//Configure and start timer
//NRF_TIMER2->TASKS_START = 1;               // Start TIMER2
        	
while(true) 
{	
        // Enter System ON sleep mode
        __WFE();
        // Make sure any pending events are cleared
        __SEV();
        __WFE();	
}
}

the timer code

void start_timer(void)
{		
  NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;  // Set the timer in Counter Mode
  NRF_TIMER2->TASKS_CLEAR = 1;               // clear the task first to be usable for later
	NRF_TIMER2->PRESCALER = 15;                             //Set prescaler. Higher number gives slower timer. Prescaler = 0 gives 16MHz timer
	NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;		 //Set counter to 16 bit resolution
	NRF_TIMER2->CC[0] = 500000;                             //Set value for TIMER2 compare register 0
	NRF_TIMER2->CC[1] = 500;                                 //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
	
  
}

and timer handler

void TIMER2_IRQHandler(void)
{
    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	
        	    		
    //-----------------------the scanning function--------------------------------------//
    sd_ble_gap_scan_stop();
    SEGGER_RTT_printf(0,"Inside the 1st timer loop\n");
    sd_ble_gap_scan_start(&m_scan_param);
    LEDS_ON(BSP_LED_1_MASK);
    }
        	
//-----------------------the advertising function will go here-------------------//
    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
    LEDS_OFF(BSP_LED_1_MASK);
    sd_ble_gap_scan_stop();
    SEGGER_RTT_printf(0,"Inside the 2nd timer loop\n");
    }
}
Parents Reply Children
No Data
Related