RTC_NXP_Arduino
1.1.3
RTC driver device operation sample code for Arduino
Toggle main menu visibility
Loading...
Searching...
No Matches
src
PCF2131_base.cpp
1
#include "RTC_NXP.h"
2
3
PCF2131_base::PCF2131_base
()
4
{
5
}
6
7
PCF2131_base::~PCF2131_base
()
8
{
9
}
10
11
void
PCF2131_base::begin
(
void
)
12
{
13
int_clear
();
14
}
15
16
17
bool
PCF2131_base::oscillator_stop
(
void
)
18
{
19
return
_reg_r
( Seconds ) & 0x80;
20
}
21
22
time_t
PCF2131_base::rtc_time
()
23
{
24
struct
tm now_tm;
25
26
uint8_t bf[ 8 ];
27
28
_reg_r
( _100th_Seconds, bf,
sizeof
( bf ) );
29
30
now_tm.tm_sec =
bcd2dec
( bf[ 1 ] );
31
now_tm.tm_min =
bcd2dec
( bf[ 2 ] );
32
now_tm.tm_hour =
bcd2dec
( bf[ 3 ] );
33
now_tm.tm_mday =
bcd2dec
( bf[ 4 ] );
34
now_tm.tm_mon =
bcd2dec
( bf[ 6 ] ) - 1;
35
now_tm.tm_year =
bcd2dec
( bf[ 7 ] ) + 100;
36
now_tm.tm_isdst = 0;
37
38
return
mktime(&now_tm);
39
}
40
41
void
PCF2131_base::set
(
struct
tm* now_tmp )
42
{
43
time_t now_time;
44
struct
tm* cnv_tmp;
45
46
uint8_t bf[ 8 ];
47
48
bf[ 0 ] = 0;
49
bf[ 1 ] =
dec2bcd
( now_tmp->tm_sec );
50
bf[ 2 ] =
dec2bcd
( now_tmp->tm_min );
51
bf[ 3 ] =
dec2bcd
( now_tmp->tm_hour );
52
bf[ 4 ] =
dec2bcd
( now_tmp->tm_mday );
53
bf[ 6 ] =
dec2bcd
( now_tmp->tm_mon + 1 );
54
bf[ 7 ] =
dec2bcd
( now_tmp->tm_year - 100 );
55
56
now_time = mktime( now_tmp );
57
cnv_tmp = localtime( &now_time );
58
bf[ 5 ] =
dec2bcd
( cnv_tmp->tm_wday);
59
60
_bit_op8
( Control_1, ~0x28, 0x20 );
61
_bit_op8
( SR_Reset, (uint8_t)(~0x80), 0x80 );
62
63
_reg_w
( _100th_Seconds, bf,
sizeof
( bf ) );
64
65
_bit_op8
( Control_1, ~0x20, 0x00 );
66
67
68
// OTP refresh
69
70
otp_refresh
();
71
delay( 100 );
// OTP refresh will be completed less than 100 ms
72
}
73
74
void
PCF2131_base::alarm
(
alarm_setting
digit,
int
val )
75
{
76
alarm
( digit, val, 0 );
77
}
78
79
void
PCF2131_base::alarm
(
alarm_setting
digit,
int
val,
int
int_sel )
80
{
81
int
v = (val == 0x80) ? 0x80 :
dec2bcd
( val );
82
_reg_w
( Second_alarm + digit, v );
83
_bit_op8
( int_mask_reg[ int_sel ][ 0 ], ~0x04, 0x00 );
84
_bit_op8
( Control_2, ~0x02, 0x02 );
85
}
86
87
void
PCF2131_base::alarm_clear
(
void
)
88
{
89
_bit_op8
( Control_2, ~0x10, 0x00 );
90
}
91
92
void
PCF2131_base::alarm_disable
(
void
)
93
{
94
_bit_op8
( Control_2, ~0x02, 0x00 );
95
}
96
97
void
PCF2131_base::timestamp
(
int
num,
timestamp_setting
ts_setting,
int
int_sel )
98
{
99
const
int
r_ofst = 7;
100
const
int
fst = ts_setting ? 0x80 : 0x00;
101
102
num -=1;
103
104
uint8_t reg = Timestp_ctl1 + num * r_ofst;
105
106
_bit_op8
( reg, (uint8_t)(~0x80), fst );
107
_bit_op8
( int_mask_reg[ int_sel ][ 1 ], ~(0x1 << (3 - num)), (0x0 << (3 - num)) );
108
109
_bit_op8
( Control_5, ~(0x1 << (7 - num)), (0x1 << (7 - num)) );
110
}
111
112
time_t
PCF2131_base::timestamp
(
int
num )
113
{
114
const
int
r_ofst = 7;
115
116
num -=1;
117
118
uint8_t reg = Timestp_ctl1 + num * r_ofst;
119
uint8_t v[ 7 ];
120
121
_reg_r
( reg, v,
sizeof
( v ) );
122
123
struct
tm ts_tm;
124
125
ts_tm.tm_sec =
bcd2dec
( v[ 1 ] );
126
ts_tm.tm_min =
bcd2dec
( v[ 2 ] );
127
ts_tm.tm_hour =
bcd2dec
( v[ 3 ] );
128
ts_tm.tm_mday =
bcd2dec
( v[ 4 ] );
129
ts_tm.tm_mon =
bcd2dec
( v[ 5 ] ) - 1;
130
ts_tm.tm_year =
bcd2dec
( v[ 6 ] ) + 100;
131
ts_tm.tm_isdst = 0;
132
133
return
mktime(&ts_tm);
134
135
}
136
137
uint8_t
PCF2131_base::int_clear
(
void
)
138
{
139
uint8_t dummy[ 3 ];
140
int_clear
( dummy );
141
142
return
0;
// dummy
143
}
144
145
uint8_t
PCF2131_base::int_clear
( uint8_t* rv )
146
{
147
_reg_r
( Control_2, rv, 3 );
148
149
if
( rv[ 0 ] & 0x90 )
// if interrupt flag set in Control_2
150
_reg_w
( Control_2, rv[ 0 ] & ~((rv[ 0 ] & 0x90) | 0x49) );
// datasheet 7.11.5
151
152
if
( rv[ 1 ] & 0x08 )
// if interrupt flag set in Control_3
153
_reg_w
( Control_3, rv[ 1 ] & ~(0x08) );
154
155
if
( rv[ 2 ] & 0xF0 )
// if interrupt flag set in Control_4
156
_reg_w
( Control_4, rv[ 2 ] & ~(rv[ 2 ] & 0xF0) );
157
158
return
0;
// dummy
159
}
160
161
void
PCF2131_base::periodic_interrupt_enable
(
periodic_int_select
sel,
int
int_sel )
162
{
163
if
( !sel ) {
164
_bit_op8
( Control_1, ~0x03, 0x00 );
165
_bit_op8
( int_mask_reg[ int_sel ][ 0 ], ~0x30, 0x30 );
166
return
;
167
}
168
169
uint8_t v = (sel == EVERY_MINUTE) ? 0x02 : 0x01;
170
171
v=3;
172
_bit_op8
( Control_1, ~0x03, v );
173
_bit_op8
( int_mask_reg[ int_sel ][ 0 ], ~0x30, ~(v << 4) );
174
}
175
176
void
PCF2131_base::set_clock_out
(
clock_out_frequency
freq)
177
{
178
_bit_op8
(CLKOUT_ctl, ~0b00000111, freq);
179
}
180
181
void
PCF2131_base::reset
()
182
{
183
_reg_w
(SR_Reset, 0b00101100);
184
}
185
186
void
PCF2131_base::otp_refresh
()
187
{
188
_bit_op8
(CLKOUT_ctl, ~0b00100000, 0b00000000);
189
_bit_op8
(CLKOUT_ctl, ~0b00100000, 0b00100000);
190
}
PCF2131_base::timestamp_setting
timestamp_setting
Definition
RTC_NXP.h:148
PCF2131_base::otp_refresh
void otp_refresh()
Definition
PCF2131_base.cpp:186
PCF2131_base::begin
void begin(void)
Definition
PCF2131_base.cpp:11
PCF2131_base::periodic_int_select
periodic_int_select
Definition
RTC_NXP.h:142
PCF2131_base::PCF2131_base
PCF2131_base()
Definition
PCF2131_base.cpp:3
PCF2131_base::set
void set(struct tm *now_tm)
Definition
PCF2131_base.cpp:41
PCF2131_base::int_clear
uint8_t int_clear(void)
Definition
PCF2131_base.cpp:137
PCF2131_base::clock_out_frequency
clock_out_frequency
Definition
RTC_NXP.h:154
PCF2131_base::alarm_clear
void alarm_clear(void)
Definition
PCF2131_base.cpp:87
PCF2131_base::reset
void reset()
Definition
PCF2131_base.cpp:181
PCF2131_base::set_clock_out
void set_clock_out(clock_out_frequency freq)
Definition
PCF2131_base.cpp:176
PCF2131_base::rtc_time
time_t rtc_time(void)
Definition
PCF2131_base.cpp:22
PCF2131_base::timestamp
void timestamp(int num, timestamp_setting ts_setting, int int_sel=0)
Definition
PCF2131_base.cpp:97
PCF2131_base::periodic_interrupt_enable
void periodic_interrupt_enable(periodic_int_select sel, int int_sel=0)
Definition
PCF2131_base.cpp:161
PCF2131_base::~PCF2131_base
virtual ~PCF2131_base()
Definition
PCF2131_base.cpp:7
PCF2131_base::alarm_disable
void alarm_disable(void)
Definition
PCF2131_base.cpp:92
PCF2131_base::oscillator_stop
bool oscillator_stop(void)
Definition
PCF2131_base.cpp:17
PCF2131_base::alarm
void alarm(alarm_setting digit, int val)
Definition
PCF2131_base.cpp:74
PCF2131_base::_reg_r
virtual void _reg_r(uint8_t reg, uint8_t *vp, int len)=0
PCF2131_base::_bit_op8
virtual void _bit_op8(uint8_t reg, uint8_t mask, uint8_t val)=0
PCF2131_base::_reg_w
virtual void _reg_w(uint8_t reg, uint8_t *vp, int len)=0
RTC_NXP::alarm_setting
alarm_setting
Definition
RTC_NXP.h:35
RTC_NXP::dec2bcd
static uint8_t dec2bcd(uint8_t v)
Definition
RTC_NXP.cpp:24
RTC_NXP::bcd2dec
static uint8_t bcd2dec(uint8_t v)
Definition
RTC_NXP.cpp:19
Generated by
1.17.0