AVR128DA48 CLK setup

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I have started working with the Curiosity nano eval board. I need to setup the RTC

for a 5 ms period. What is the best way to do this? I have the RTC periodic interrupt

setup code from the app notes using the 32768 external crystal. 

 

Do I also need to set the prescaler in the configuration? Div 32 gives a 1 ms time period.

Do I use this in combination with the RTC.PITCTRLA register? I appreciate some numeric

examples.

 

Edited to add, I also need to use the UART at 9600 baud rate. I have the F_CPU set up at 

3333333 for this. I am not clear on the difference between system clock and RTC.

 

Thanks,

Priya

 


 

 

Attachment(s): 

This topic has a solution.
Last Edited: Sun. Nov 22, 2020 - 06:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


I would set the RTC prescaler to 32, the RTC period to 5 (maybe it's 4, better test it) and use the overflow interrupt. Since you are using start, something like this:

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The RTC clock source does not depend on the system clock.
The code below uses the internal oscillator OSC32K to generate interrupts approximately every 5ms.

 

#include <avr/io.h>
#include <avr/interrupt.h>

int main(void){
    RTC.PER = 163;  /* 32768 / 164 = 199.8Hz(5.005ms) */
    RTC.CLKSEL = RTC_CLKSEL_OSC32K_gc;
    RTC.INTCTRL = RTC_OVF_bm;
    RTC.CTRLA = RTC_RTCEN_bm;

    sei();
    while (1);
}

ISR(RTC_CNT_vect){
    RTC.INTFLAGS = RTC_OVF_bm;
    
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


fwe wrote:

Edited to add, I also need to use the UART at 9600 baud rate. I have the F_CPU set up at 

3333333 for this. I am not clear on the difference between system clock and RTC.

 

F_CPU doesn't set CPU speed, it just serves to inform the program and libraries of the current CPU speed. As mentioned by kabasan, the RTC doesn't necessarily use the same clock as the system.

You setup the clock sources separately. For example to set a 3.33MHz system clock:

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your replies. I have the atmel start configurator setup as posted. I am just

calling RTC_0_init instead of using the code from the app note. I just have an ISR to

clear the RTC overflow interrupt flag. Is this OK?

 

Same way, CLKCTRL_init has the settings from atmel start. Does this take care of defining

F_CPU = 3333333? 

 

I have software timers I am trying to use. These expect an RTC interrupt every 5 ms. 

I have a 2 second time out that is not happening, a breakpoint doesn't halt here.

Not sure what is still missing from my setup.

 

Thanks,

Priya

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

fwe wrote:

Same way, CLKCTRL_init has the settings from atmel start. Does this take care of defining

F_CPU = 3333333? 

 

I think it's defined somewhere inside the Start mess (yeah, it's in config/clock_config.h). However, since you are picking specific init functions and not the whole thing, you should define F_CPU yourself.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am having best results if I have code to setup the RTC CLK, UART in main.c.

Configuring CLK and RTC in atmel start did not work for me, I don't know why.

Maybe I need specific include driver files.

 

I am able to halt at the overflow interrupt ISR. 

 

I am using software timers in the code. I think the problem is this function vTmrCfgFnct is not

being implemented the way it needs to be. Are there any special header files I need to

include for this? The callback function is not being called. The time out is 2 sec.

 

 

void  vTmrCfgFnct (int n, void (*fnct)(void *), void *arg)  
{
    TMR *ptmr;      

    if (n < TMR_MAX_TMR) {
        ptmr             = &TmrTbl[n];
        TS_ENTER_CRITICAL();
        ptmr->TmrFnct    = fnct;                 /* Store pointer to user function into timer          */
        ptmr->TmrFnctArg = arg;                  /* Store user's function arguments pointer            */
        TS_EXIT_CRITICAL();
    }
}

vTmrCfgFnct (WIEG_TIMER, (void *)vIOWiegTimeout, (void *)0);        // config WIEG timer

 

 

Certain other software functions are working OK:

 

void  vTmrStart (int n)
{
    if (n < TMR_MAX_TMR) {
        TS_ENTER_CRITICAL();
        TmrTbl[n].TmrEn = TRUE;
        TS_EXIT_CRITICAL();
    }
}

void  vTmrStop (int n)
{
    if (n < TMR_MAX_TMR) {
        TS_ENTER_CRITICAL();
        TmrTbl[n].TmrEn = FALSE;
        TS_EXIT_CRITICAL();
    }
}

 

I appreciate any pointers.

 

Thank you,

Priya

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

fwe wrote:
I am using software timers in the code.

 

"Software timers"? That's pretty vague. I have no idea how you are implementing them...

Seems you are registering callback functions to some kind of system tick interrupt, that will then call them at specific times? In that case, the registered callback functions need to be called somewhere in the timer ISR.

 

What can I say? Remember, all global variables used inside ISRs need to be volatile. In particular, the array

TmrTbl[]

where you seem to be registering callbacks, needs to be volatile. I can only guess, since you didn't show the definition of this array.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Attached are the software timer files being used. These timer libraries have been 

used for a variety of firmware in production, so they work OK. There is 

something missing when compiling these to the AVR128DA project. Appreciate

any pointers here. 

 

Thanks,

Priya

 

Attachment(s): 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So is this function being called regularly, as the library you are using expects? If so, from where? From the ISR or from main()?

 

/*
*********************************************************************************************************
												vTmrSignalTmr
*/

	/*! This function is called by the main program approx. every 10ms.  This function is thus
        responsible for counting the number of clock ticks per 1/10 second.  When 1/10 second
        elapses, this function will signal the timer manager task. TMR_DLY_TICKS must be set
	    to produce 1/10 second delays

      \param - nothing
      
      \return - nothing
    */ 

/*********************************************************************************************************
*/
void  vTmrSignalTmr (void)
{
    uTmrTickCtr++;
    if (uTmrTickCtr >= TMR_DLY_TICKS) {
        uTmrTickCtr = 0;
        vTmrTask();
    }
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

El--

I believe that's the issue. This function needs to be called. I need to add and verify it.

 

Thank you so much!

Priya'

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

fwe wrote:

This function needs to be called. I need to add and verify it.

 

Remember that, if you end up calling it from the RTS overflow ISR, the global variables used must be volatile.

 

So this part may need to be changed:

 

/*
*********************************************************************************************************
*                                          GLOBAL VARIABLES
*********************************************************************************************************
*/

//! Table of timers managed by this module
TMR       TmrTbl[TMR_MAX_TMR];   

to

/*
*********************************************************************************************************
*                                          GLOBAL VARIABLES
*********************************************************************************************************
*/

//! Table of timers managed by this module
volatile TMR       TmrTbl[TMR_MAX_TMR];   

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I also want to understand why configuring the project in atmel start doesn't work but have the code in

main.c does.

 

 

Attached is a picture of my setup in atmel start. The clock and RTC settings aren't being recognized in

main.c. Is there a way to configure GPIO  interrupt settings in atmel start?

 

Thanks,

Priya

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

fwe wrote:

Is there a way to configure GPIO  interrupt settings in atmel start?

 

I'm sure there is, however I don't really use Start except to sometimes generate configuration code for more complex peripherals.

There must be some kind of documentation for Start you can read to see how it is done.