Go Down

Topic: internal RTC (Read 3614 times) previous topic - next topic


According to the datasheet, the SAM3X has an internal RTC. There is also a 32.768Khz oscillator attached to the XOUT32 and XIN32 pins.
Is there a possibility to use the internal RTC with the Arduino libraries?


Yes there is a possibility, as to whether or not anyone has done that work yet I don't know but I very much doubt it because some other basic stuff has not been implemented yet.

They may just leave it for a bright young lad to port an existing RTC library.

Rob Gray aka the GRAYnomad www.robgray.com


Ok, but what about the RTC battery?
When we turn off the DUE also the RTC will go off, sounds like whe need to syncronize it every time we turn on the DUE.


Yes, there's no provision for a backup battery so having the RTC is of limited value I think.

It's a shame because the SAM has really good features for shutting itself and the entire PCB down.

Rob Gray aka the GRAYnomad www.robgray.com


OK, the DUE RTC seems to be ticking, but i'm trying to enable the "second" interrupt.  See the following sketch
Code: [Select]

// due rtc   32khz
//  pps interrupt
//   RTC        (0x400E1A60U)  rtc registers

#define BOARD_LED_PIN 13
volatile static unsigned long ticks,us;
static unsigned long us0;
static uint8_t hr,min,sec,day,m,wk;
static uint16_t yr;

void RTC_Handler() {
    if (us0 == 0) us0 = micros();
     else {
        us = micros() - us0;

void setup() {

// pmc_set_writeprotect(false);
// pmc_enable_periph_clk(RTC_IRQn);
hr=1; min =2; sec=3; m=4; day=5; wk=6; yr=2012;
RTC_EnableIt(RTC,(1<<2));     // pps
// NVIC_EnableIRQ(RTC_IRQn);

void loop() {
    // compare micros to pps
    int ppm;
    char str[32];

    while(1) {
        ppm = ticks*1000000 - us;
        ppm = 1.e6 * ppm/ (float)us;
        sprintf(str,"%ld %ld %d",ticks,us,ppm);

void print_time() {
char str[64];

sprintf(str,"%d/%d/%d %02d:%02d:%02d",m,day,yr,hr,min,sec);

I get no ticks in the RTC_Hander() as the code is.  If i uncomment the NVIC_Enable, it hangs. 

my first try the DUE interrupts etc. 



Dec 06, 2012, 11:37 pm Last Edit: Dec 07, 2012, 12:05 am by mantoui Reason: 1
Now seeing the other RTC thread, I incorporated some of that code, and now I'm getting into the RTC_Handler working.  Code reports that RTC is within 3ppm of system clock -- makes me wonder if code is really using the 32Khz crystal ?

Here are some frequency offsets for other MCU crystals, resonators, RTC ...
Code: [Select]

  processor    oscillator       spec       measured(ppm)
  avr 328p    16Mhz resonator  0.5%     -1330        uno
               16MHz resonator  0.5%     -1422        breadboard 328p
                16MHz crystal    50ppm    -42          breadboard 328p
  avr 328p    11Mhz crystal     ?             -23          3.74v batteries
  avr 328p    11Mhz crystal     ?       -16          5v
  avr 128      14Mhz             ?       1738         no schematic, resonator?

  DUE         12MHz*x crystal    ?       -4
  DUE RTC     32Khz   crystal    ?       3           work in progress
  maple       8MHz*9 crystal    ?        -5
  propeller   5Mhz*16 crystal   ?        -40         board1 (quickstart)
  propeller   5Mhz*16 crystal   ?        -98         board2 (quickstart)

  ds1307 RTC  32khz   cyrstal  20ppm    23           external "watch" crystal
  ds3132 RTC  32khz   cyrstal   2ppm     1           chronodot TCXO i2c
  ds3134 RTC  32khz   cyrstal   2ppm     1           TCXO SPI

  maple RTC   32khz   cyrstal   20ppm     -41         breadboard 512hz ticks
  bad         32khz   crystal   20ppm   -891019      breadboard 512hz ticks

  avr 328  RC  8Mhz     RC      1%       2100         OSCCAL units 3000 ppm
  avr 328p RC  8Mhz     RC      1%       2700         OSCCAL units 5000 ppm
  avr 328p RC  8Mhz     RC      1%       4061         OSCCAL units 4000 ppm
  maple LSI    40KHz    RC     50%     -19659         calibrate with prescale

  beagle      24MHz*x                    -5          ntp drift
  raspberry   19MHz*x                   -16          ntp drift
  dell desk                             -26          ntp drift
  dell laptop                           -14          ntp drift


Dec 07, 2012, 06:41 am Last Edit: Dec 07, 2012, 06:45 am by M4nu Reason: 1

Why du you use the RTC creating ticks? The SAM3X has a 24 bit system tick timer. It creates 1 ms ticks. In the arduino software it is already implemented --> millis() // returns the current system time.


ticks from the RTC were only used to measure frequency accuracy of the 32khz crystal


Oh sorry I understand you wrong. Back to your problem. I think you have to clear the second clear bit (RTC_SCCR_SECCLR). This is a small example which i did before:

Code: [Select]

void RTC_Handler(void)
  uint32_t ul_status = rtc.rtc_get_status(RTC);
  rtc.rtc_get_time(RTC, &hour, &minute, &second);
  /* Second increment interrupt */
  if ((ul_status & RTC_SR_SEC) == RTC_SR_SEC)
    rtc_time = "Time: ";
    rtc_time += String(hour);
    rtc_time += ":";
    rtc_time += String(minute);
    rtc_time += ":";
    rtc_time += String(second);
    rtc.rtc_clear_status(RTC, RTC_SCCR_SECCLR);
  /* Time or date alarm */
  if ((ul_status & RTC_SR_ALARM) == RTC_SR_ALARM)
    rtc.rtc_clear_status(RTC, RTC_SCCR_ALRCLR);



I need a source of such a 32kHz crystal that cooperates with our Sam3x8e.
(Low power design. not that trivial as it seems, I read... One seems to have to count Capacitances, of the PCB strands as well. )

Can you give me a link to the product in the shop you bought it?
Or some detailes like manufacturer and part number?

What is the size of the capacitors you used?

Thank you!



Jun 23, 2015, 11:51 pm Last Edit: Jun 23, 2015, 11:58 pm by aigarslv
CM315D32768DZCT is used in Due


32kHz crystal is used as an accurate slow clock source, SAM3X8E has an oscillator built-in but it is not accurate... You can try running your project without external oscillator.

Go Up