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

RTC jittering and lacking control of frequency

I am using the RTC to generate a square wave signal.

I have two issues. One is that nothing I do on the prescaler or the Count Compare is having an influence on the frequency produced. The other is that I have significant jitter on the time axis (see image below).

Here is my code that currently yields a frequency of 3.3 kHz:

NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
NRF_CLOCK->TASKS_LFCLKSTART = 1;
while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0){}
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
NRF_RTC1->PRESCALER = 4; 
NRF_RTC1->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
NRF_RTC1->CC[0] = 10;
NRF_PPI->CH[1].EEP = (uint32_t)&NRF_RTC1->EVENTS_COMPARE[0];
NRF_PPI->CH[1].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[1];
NRF_PPI->CH[2].EEP = (uint32_t)&NRF_RTC1->EVENTS_COMPARE[0];
NRF_PPI->CH[2].TEP = (uint32_t)&NRF_RTC1->TASKS_CLEAR;
NRF_GPIOTE->CONFIG[1] =((GPIOTE_CONFIG_MODE_Task       << GPIOTE_CONFIG_MODE_Pos)
                        |(CONV                         << GPIOTE_CONFIG_PSEL_Pos)
                        |(GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos)
                        |(GPIOTE_CONFIG_OUTINIT_Low     << GPIOTE_CONFIG_OUTINIT_Pos));

NRF_RTC1->TASKS_START = 1;

Is the PPI channel resetting the counters necessary / is there a better way to do it?

What am I doing wrong for setting the frequency? Is the jitter something to be expected? I cannot resort to using TIMERX as they are already occupied (SD, SPI and another square wave at 4 MHz).

RTC Jitter

Parents
  • @RK: To be fair, he mentions nRF51 in the tags in his question.

    @stephbur: Another thing that is missing in the code in your question is the enabling of your PPI channels. Try to add this:

    NRF_PPI->CHEN = (PPI_CHEN_CH1_Enabled << PPI_CHEN_CH1_Pos) | (PPI_CHEN_CH2_Enabled << PPI_CHEN_CH2_Pos);
    

    I tested your code on an nRF51 DK and toggled LED_4. It worked fine. Output frequency was a stable 321.3Hz. The random square waves are pretty odd though. Are you sure you are not measuring on your SPI bus or something? It could be some sort of serial signals.

Reply
  • @RK: To be fair, he mentions nRF51 in the tags in his question.

    @stephbur: Another thing that is missing in the code in your question is the enabling of your PPI channels. Try to add this:

    NRF_PPI->CHEN = (PPI_CHEN_CH1_Enabled << PPI_CHEN_CH1_Pos) | (PPI_CHEN_CH2_Enabled << PPI_CHEN_CH2_Pos);
    

    I tested your code on an nRF51 DK and toggled LED_4. It worked fine. Output frequency was a stable 321.3Hz. The random square waves are pretty odd though. Are you sure you are not measuring on your SPI bus or something? It could be some sort of serial signals.

Children
No Data
Related