SAMD21 RTC is reset

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

Hello!

 

I have SAMD21 RTC watches powered by generator 2 (GCLK2 -  SYSTEM_CLOCK_SOURCE_XOSC32K )
32768 external quartz resonator

I have a problem - resetting the clock when executing the code - void app (int adr)
(go to another address in the bootloader and back to the main program)

How to make the clock not reset ???

 

void app(int adr){
    /* Pointer to the Application Section */
    void (*application_code_entry)(void);

    /* Rebase the Stack Pointer */
    __set_MSP(*(uint32_t *)adr);

    /* Rebase the vector table base address */
    SCB->VTOR = ((uint32_t)adr & SCB_VTOR_TBLOFF_Msk);

    /* Load the Reset Handler address of the application */
    application_code_entry = (void (*)(void))(unsigned *)(*(unsigned *)(adr+4)   );
    
    /* Jump to user Reset Handler in the application */
    application_code_entry();
    
    };\
    
struct rtc_module rtc_instance;
void InitCalendar (int year,int month,int day,int hour,int minute,int second){
    struct rtc_calendar_config config_rtc_calendar;
    struct rtc_calendar_time time;
    
    rtc_calendar_get_config_defaults(&config_rtc_calendar);
    //config_rtc_calendar.prescaler=RTC_CALENDAR_PRESCALER_DIV_1024;
    config_rtc_calendar.clock_24h=true;
    //config_rtc_calendar.continuously_update=true;    
    rtc_calendar_init(&rtc_instance,RTC,&config_rtc_calendar);
    rtc_calendar_frequency_correction(&rtc_instance,127);
    rtc_calendar_enable(&rtc_instance);

    time.year=year;
    time.month=month;
    time.day=day;
    
    if (hour>12){    time.pm=true;    }else{time.pm=false;}
    
    time.hour=hour;
    time.minute=minute;
    time.second=second;    
    rtc_calendar_set_time(&rtc_instance,&time);
}
--------------------- rtc_calendar_init ---------------------
/* Turn on the digital interface clock */
    system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBA, PM_APBAMASK_RTC);

    /* Set up GCLK */
    struct system_gclk_chan_config gclk_chan_conf;
    system_gclk_chan_get_config_defaults(&gclk_chan_conf);
    gclk_chan_conf.source_generator = GCLK_GENERATOR_2;
    system_gclk_chan_set_config(RTC_GCLK_ID, &gclk_chan_conf);
    system_gclk_chan_enable(RTC_GCLK_ID);

------------------ conf_clock.c ------------------------------------sampling major



#  define CONF_CLOCK_CPU_CLOCK_FAILURE_DETECT     false 
#  define CONF_CLOCK_FLASH_WAIT_STATES            2

#  define CONF_CLOCK_CPU_DIVIDER                  SYSTEM_MAIN_CLOCK_DIV_4 
#  define CONF_CLOCK_APBA_DIVIDER                 SYSTEM_MAIN_CLOCK_DIV_1
#  define CONF_CLOCK_APBB_DIVIDER                 SYSTEM_MAIN_CLOCK_DIV_1
#  define CONF_CLOCK_APBC_DIVIDER                 SYSTEM_MAIN_CLOCK_DIV_1

/* SYSTEM_CLOCK_SOURCE_OSC8M configuration - Internal 8MHz oscillator */
#  define CONF_CLOCK_OSC8M_PRESCALER              SYSTEM_OSC8M_DIV_1
#  define CONF_CLOCK_OSC8M_ON_DEMAND              true
#  define CONF_CLOCK_OSC8M_RUN_IN_STANDBY         false

#  define CONF_CLOCK_XOSC32K_ENABLE               true

#  define CONF_CLOCK_XOSC32K_EXTERNAL_CRYSTAL     SYSTEM_CLOCK_EXTERNAL_CRYSTAL
#  define CONF_CLOCK_XOSC32K_STARTUP_TIME         SYSTEM_XOSC32K_STARTUP_65536
#  define CONF_CLOCK_XOSC32K_AUTO_AMPLITUDE_CONTROL  false
#  define CONF_CLOCK_XOSC32K_ENABLE_1KHZ_OUPUT    false
#  define CONF_CLOCK_XOSC32K_ENABLE_32KHZ_OUTPUT  true
#  define CONF_CLOCK_XOSC32K_ON_DEMAND            true
#  define CONF_CLOCK_XOSC32K_RUN_IN_STANDBY       true

/* Set this to true to configure the GCLK when running clocks_init. If set to
 * false, none of the GCLK generators will be configured in clocks_init(). */
#  define CONF_CLOCK_CONFIGURE_GCLK               true

/* Configure GCLK generator 0 (Main Clock) */
#  define CONF_CLOCK_GCLK_0_ENABLE                true

#  define CONF_CLOCK_GCLK_0_RUN_IN_STANDBY        true 
#  define CONF_CLOCK_GCLK_0_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_DFLL 

#  define CONF_CLOCK_GCLK_0_PRESCALER             1
#  define CONF_CLOCK_GCLK_0_OUTPUT_ENABLE         false

/* Configure GCLK generator 1 */
#  define CONF_CLOCK_GCLK_1_ENABLE                true
#  define CONF_CLOCK_GCLK_1_RUN_IN_STANDBY        false
#  define CONF_CLOCK_GCLK_1_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_XOSC32K
#  define CONF_CLOCK_GCLK_1_PRESCALER             1
#  define CONF_CLOCK_GCLK_1_OUTPUT_ENABLE         false

/* Configure GCLK generator 2       (RTC) */ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#  define CONF_CLOCK_GCLK_2_ENABLE                true
#  define CONF_CLOCK_GCLK_2_RUN_IN_STANDBY        true
#  define CONF_CLOCK_GCLK_2_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_XOSC32K
#  define CONF_CLOCK_GCLK_2_PRESCALER             32
#  define CONF_CLOCK_GCLK_2_OUTPUT_ENABLE         false

/* Configure GCLK generator 3 */
#  define CONF_CLOCK_GCLK_3_ENABLE                true
#  define CONF_CLOCK_GCLK_3_RUN_IN_STANDBY        false
#  define CONF_CLOCK_GCLK_3_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
#  define CONF_CLOCK_GCLK_3_PRESCALER             1
#  define CONF_CLOCK_GCLK_3_OUTPUT_ENABLE         false

 

Last Edited: Sat. Dec 15, 2018 - 08:59 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi dav1977,

Are you got solution for this problem?

I am also facing same issue if you got solution please help me

Thanks in Advance

 

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

Ashok_textra0410 wrote:

Hi dav1977,

Are you got solution for this problem?

I am also facing same issue if you got solution please help me

Thanks in Advance

 

Hi

There is no solution yet.

While I keep in RAM,
after the transition, again initialize the clock from the data from the RAM

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

I am unsure of the questions but check and make sure the SWRST bit in the RTC is not being set when initialized again. 

 

I ran into a problem with the RTC where reading the count was taking a long time. The RTC was running at 1024Hz and thus reading the count caused a bus stall between clock domains, this caused ~2ms bus stall each time I read the RTC.  I ended up keeping the epoch seconds in RAM and just incrementing this epoch counter in the RTC interrupt handler. For my application waking from sleep every second did not significantly change the power consumption so it worked much better.   

 

I have also ran into problems with external RTCs where you need to write the calendar data in specific order or write twice.  This is often needed to handle setting time near leap years correctly. Therefore I try to only use real time counters (ie epoch seconds) and do the calendar time calculations in firmware. 

 

Trampas