Skip to content

Commit 09e3df4

Browse files
committed
Adding support for SAM
1 parent f77b7af commit 09e3df4

File tree

22 files changed

+286
-118
lines changed

22 files changed

+286
-118
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The RTC library has been developed to allow a common interface for RTC
44
devices. The library includes a software based RTC, and device drivers
55
for DS1302 and DS1307 Real-Time Clock devices.
66

7-
Version: 1.0
7+
Version: 1.1
88

99
## Classes
1010

examples/RTC/RTC.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ DS1302<BOARD::D11, BOARD::D12, BOARD::D13> rtc;
2323
Software::TWI<BOARD::D18, BOARD::D19> twi;
2424
#else
2525
// Configure: Hardware TWI bus clock frequency (100 or 400 kHz)
26-
#define FREQ 100000UL
27-
// #define FREQ 400000UL
2826
#include "Hardware/TWI.h"
29-
Hardware::TWI twi(FREQ);
27+
Hardware::TWI twi(100000UL);
28+
// Hardware::TWI twi(400000UL);
3029
#endif
3130
DS1307 rtc(twi);
3231

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=Arduino-RTC
2-
version=1.0
2+
version=1.1
33
author=Mikael Patel
44
maintainer=Mikael Patel <mikael.patel@gmail.com>
55
sentence=Real-Time Clock (RTC) library for Arduino.
66
paragraph=The RTC library has been developed to allow a common interface for RTC devices. The library also includes a simple software RTC based on the Arduino milliseconds function, and device drivers for DS1302 and DS1307.
77
category=Communication
88
url=https://github.com/mikaelpatel/Arduino-RTC
9-
architectures=avr
9+
architectures=avr,sam

mainpage.dox

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ devices. The library also includes a simple software RTC based on the
55
Arduino milliseconds function, and device drivers for DS1302 and
66
DS1307.
77

8-
Version: 1.0
8+
Version: 1.1
99
*/
1010

1111
/** @page License

src/Driver/DS1302.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
#define DS1302_H
2121

2222
#include "GPIO.h"
23-
#include "bcd.h"
24-
#include "time.h"
23+
#include "RTC.h"
2524

2625
#ifndef CHARBITS
2726
#define CHARBITS 8

src/Driver/DS1307.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
#ifndef DS1307_H
2020
#define DS1307_H
2121

22-
#include "bcd.h"
23-
#include "time.h"
22+
#include "RTC.h"
2423
#include "TWI.h"
2524

2625
/**

src/Hardware/AVR/RTC.h

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @file Hardware/AVR/RTC.h
3+
* @version 1.0
4+
*
5+
* @section License
6+
* Copyright (C) 2017, Mikael Patel
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*/
18+
19+
#ifndef HARDWARE_AVR_RTC_H
20+
#define HARDWARE_AVR_RTC_H
21+
22+
/**
23+
* Software Real-Time Clock.
24+
*/
25+
class RTC {
26+
public:
27+
/**
28+
* Construct software real-time clock based on millis().
29+
*/
30+
RTC() :
31+
m_millis(0),
32+
m_time(0)
33+
{}
34+
35+
/**
36+
* Increment seconds counter when time has elapsed.
37+
* Return true(1) if an increment occured, otherwise false(0).
38+
*/
39+
bool tick()
40+
{
41+
uint16_t now = millis();
42+
if (now - m_millis < 1000) return (false);
43+
uint8_t sreg = SREG;
44+
__asm__ __volatile__("cli" ::: "memory");
45+
m_time += 1;
46+
m_millis = now;
47+
SREG = sreg;
48+
__asm__ __volatile__("" ::: "memory");
49+
return (true);
50+
}
51+
52+
/**
53+
* Return the current time in seconds from epoch.
54+
*/
55+
time_t get_time()
56+
{
57+
uint8_t sreg = SREG;
58+
__asm__ __volatile__("cli" ::: "memory");
59+
time_t res = m_time;
60+
SREG = sreg;
61+
__asm__ __volatile__("" ::: "memory");
62+
return (res);
63+
}
64+
65+
/**
66+
* Set the current time (seconds) from epoch.
67+
*/
68+
void set_time(time_t time)
69+
{
70+
uint8_t sreg = SREG;
71+
__asm__ __volatile__("cli" ::: "memory");
72+
m_time = time;
73+
SREG = sreg;
74+
__asm__ __volatile__("" ::: "memory");
75+
}
76+
77+
/**
78+
* Return the current time as a time structure.
79+
*/
80+
void get_time(struct tm& now)
81+
{
82+
time_t time = get_time();
83+
gmtime_r(&time, &now);
84+
}
85+
86+
/**
87+
* Set the current time based on the given time structure.
88+
*/
89+
void set_time(struct tm& now)
90+
{
91+
extern long __utc_offset;
92+
set_time(mktime(&now) + __utc_offset);
93+
}
94+
95+
protected:
96+
/** Timestamp for previous tick call. */
97+
volatile uint16_t m_millis;
98+
99+
/** Current time from epoch. */
100+
volatile time_t m_time;
101+
};
102+
103+
#endif

src/time/difftime.cpp renamed to src/Hardware/AVR/difftime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* POSSIBILITY OF SUCH DAMAGE.
2727
*/
2828

29-
#include <time.h>
29+
#include "time.h"
3030

3131
int32_t
3232
difftime(time_t t1, time_t t2)

src/eu_dst.h renamed to src/Hardware/AVR/eu_dst.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@
5151
#ifndef EU_DST_H
5252
#define EU_DST_H
5353

54-
#include <time.h>
55-
#include <inttypes.h>
54+
#include "time.h"
5655

5756
int eu_dst(const time_t* timer, int32_t* z)
5857
{

src/time/gmtime_r.cpp renamed to src/Hardware/AVR/gmtime_r.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
* POSSIBILITY OF SUCH DAMAGE.
2727
*/
2828

29-
#include <time.h>
29+
#include "time.h"
3030
#include <stdlib.h>
31-
#include <inttypes.h>
3231

3332
struct tm*
3433
gmtime_r(const time_t * timer, struct tm * timeptr)

0 commit comments

Comments
 (0)