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

Using RTC2, PPI and GPIOTE to drive LED

SDK Version: 15.1
IDE: Keil
Softdevice: S132 v6
Example:  Blinky peripheral example slightly modified

Q1.  Does the Softdevice use RTC0?
Q2.  Does the App Timer use RTC1?  If yes, I am assuming for BLE connection events and other BLE stuff.  Is this correct?

My goal is to use RTC2, PPI and GPIOTE create a pseudo-PWM drive for a single LED.  Why not use the PWM driver, because it consumes ~ 500uA due to the fact that it uses HFCLK....Why not use an App Timer?...because from my investigation, it consumes more current than talking to the RTC directly without interrupts.  So, here's my code:

void GPIOTE6_init(void)
{
	// Configure GPIOTE to toggle pin
	NRF_GPIOTE->CONFIG[6] =	(GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) | 					//task mode using GPIOTE CH7
													(PIN_BLUE_LED << GPIOTE_CONFIG_PSEL_Pos) |											//BLUE LED
													(GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos) |	//set mode
													(GPIOTE_CONFIG_OUTINIT_High << GPIOTE_CONFIG_OUTINIT_Pos);				//initial state = high/LED off
}

void PPI17_init(void)
{
	NRF_PPI->CH[17].EEP = (uint32_t) &NRF_RTC2->EVENTS_COMPARE[0];	//trigger from this event
	NRF_PPI->CH[17].TEP = (uint32_t) &NRF_GPIOTE->TASKS_OUT[6];			//end point for task
	
	NRF_PPI->CH[16].EEP = (uint32_t) &NRF_RTC2->EVENTS_COMPARE[1];	//trigger from this event
	NRF_PPI->CH[16].TEP = (uint32_t) &NRF_GPIOTE->TASKS_OUT[6];			//end point for task
	NRF_PPI->FORK[16].TEP = (uint32_t) &NRF_RTC2->TASKS_CLEAR;			//clear RTC2 counter value
	
	NRF_PPI->CHENSET = ((PPI_CHENSET_CH17_Enabled << PPI_CHENSET_CH17_Pos) |	//enable PPI CH17
											(PPI_CHENSET_CH16_Enabled << PPI_CHENSET_CH16_Pos));	//enable PPI CH16
}



NRF_RTC2->PRESCALER = 0;
NRF_RTC2->TASKS_CLEAR = 0;
NRF_RTC2->CC[0] = 5;
NRF_RTC2->CC[1] = 20; 	//20 caused 150uA, 10 caused 172uA
NRF_RTC2->EVTEN = ((RTC_EVTEN_COMPARE0_Enabled << RTC_EVTEN_COMPARE0_Pos) |
				(RTC_EVTEN_COMPARE1_Enabled << RTC_EVTEN_COMPARE1_Pos));
GPIOTE6_init();
PPI17_init();
NRF_RTC2->TASKS_START = 1;

My goal here is to use RTC2 to generate 2 toggles of the LED.  One at CC[0] = 5, and the other one at CC[1] = 20 so that the LED is on (active low) for 5 RTC counts and off for 20 RTC counts.  So I've hooked up PPI CH 17 to trigger when RTC2 reaches CC[0], and toggle "PIN_NRF_BLUE_LED" without clearing/resetting the count value.  Then as the RTC2 keeps counting, when it reaches CC[1],  I setup PP1 CH 16 to toggle that same pin once more.  I have also configured a fork on PPI CH 16 so that the timer clears and starts all over.

The problem I'm having is that I'm getting a 50% duty cyle waveform at the rate of CC[1].  I am not sure what is happening here, please help.

Regards,

E

Parents Reply Children
Related