RTC_NXP_Arduino
1.1.2
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
void
PCF2131_base::alarm
(
alarm_setting
digit,
int
val )
69
{
70
alarm
( digit, val, 0 );
71
}
72
73
void
PCF2131_base::alarm
(
alarm_setting
digit,
int
val,
int
int_sel )
74
{
75
int
v = (val == 0x80) ? 0x80 :
dec2bcd
( val );
76
_reg_w
( Second_alarm + digit, v );
77
_bit_op8
( int_mask_reg[ int_sel ][ 0 ], ~0x04, 0x00 );
78
_bit_op8
( Control_2, ~0x02, 0x02 );
79
}
80
81
void
PCF2131_base::alarm_clear
(
void
)
82
{
83
_bit_op8
( Control_2, ~0x10, 0x00 );
84
}
85
86
void
PCF2131_base::alarm_disable
(
void
)
87
{
88
_bit_op8
( Control_2, ~0x02, 0x00 );
89
}
90
91
void
PCF2131_base::timestamp
(
int
num,
timestamp_setting
ts_setting,
int
int_sel )
92
{
93
const
int
r_ofst = 7;
94
const
int
fst = ts_setting ? 0x80 : 0x00;
95
96
num -=1;
97
98
uint8_t reg = Timestp_ctl1 + num * r_ofst;
99
100
_bit_op8
( reg, (uint8_t)(~0x80), fst );
101
_bit_op8
( int_mask_reg[ int_sel ][ 1 ], ~(0x1 << (3 - num)), (0x0 << (3 - num)) );
102
103
_bit_op8
( Control_5, ~(0x1 << (7 - num)), (0x1 << (7 - num)) );
104
}
105
106
time_t
PCF2131_base::timestamp
(
int
num )
107
{
108
const
int
r_ofst = 7;
109
110
num -=1;
111
112
uint8_t reg = Timestp_ctl1 + num * r_ofst;
113
uint8_t v[ 7 ];
114
115
_reg_r
( reg, v,
sizeof
( v ) );
116
117
struct
tm ts_tm;
118
119
ts_tm.tm_sec =
bcd2dec
( v[ 1 ] );
120
ts_tm.tm_min =
bcd2dec
( v[ 2 ] );
121
ts_tm.tm_hour =
bcd2dec
( v[ 3 ] );
122
ts_tm.tm_mday =
bcd2dec
( v[ 4 ] );
123
ts_tm.tm_mon =
bcd2dec
( v[ 5 ] ) - 1;
124
ts_tm.tm_year =
bcd2dec
( v[ 6 ] ) + 100;
125
ts_tm.tm_isdst = 0;
126
127
return
mktime(&ts_tm);
128
129
}
130
131
uint8_t
PCF2131_base::int_clear
(
void
)
132
{
133
uint8_t dummy[ 3 ];
134
int_clear
( dummy );
135
136
return
0;
// dummy
137
}
138
139
uint8_t
PCF2131_base::int_clear
( uint8_t* rv )
140
{
141
_reg_r
( Control_2, rv, 3 );
142
143
if
( rv[ 0 ] & 0x90 )
// if interrupt flag set in Control_2
144
_reg_w
( Control_2, rv[ 0 ] & ~((rv[ 0 ] & 0x90) | 0x49) );
// datasheet 7.11.5
145
146
if
( rv[ 1 ] & 0x08 )
// if interrupt flag set in Control_3
147
_reg_w
( Control_3, rv[ 1 ] & ~(0x08) );
148
149
if
( rv[ 2 ] & 0xF0 )
// if interrupt flag set in Control_4
150
_reg_w
( Control_4, rv[ 2 ] & ~(rv[ 2 ] & 0xF0) );
151
152
return
0;
// dummy
153
}
154
155
void
PCF2131_base::periodic_interrupt_enable
(
periodic_int_select
sel,
int
int_sel )
156
{
157
if
( !sel ) {
158
_bit_op8
( Control_1, ~0x03, 0x00 );
159
_bit_op8
( int_mask_reg[ int_sel ][ 0 ], ~0x30, 0x30 );
160
return
;
161
}
162
163
uint8_t v = (sel == EVERY_MINUTE) ? 0x02 : 0x01;
164
165
v=3;
166
_bit_op8
( Control_1, ~0x03, v );
167
_bit_op8
( int_mask_reg[ int_sel ][ 0 ], ~0x30, ~(v << 4) );
168
}
169
170
void
PCF2131_base::set_clock_out
(
clock_out_frequency
freq)
171
{
172
_bit_op8
(CLKOUT_ctl, ~0b00000111, freq);
173
}
174
175
void
PCF2131_base::reset
()
176
{
177
_reg_w
(SR_Reset, 0b00101100);
178
}
179
180
void
PCF2131_base::otp_refresh
()
181
{
182
_bit_op8
(CLKOUT_ctl, ~0b00100000, 0b00000000);
183
_bit_op8
(CLKOUT_ctl, ~0b00100000, 0b00100000);
184
}
PCF2131_base::timestamp_setting
timestamp_setting
Definition
RTC_NXP.h:148
PCF2131_base::otp_refresh
void otp_refresh()
Definition
PCF2131_base.cpp:180
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:131
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:81
PCF2131_base::reset
void reset()
Definition
PCF2131_base.cpp:175
PCF2131_base::set_clock_out
void set_clock_out(clock_out_frequency freq)
Definition
PCF2131_base.cpp:170
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:91
PCF2131_base::periodic_interrupt_enable
void periodic_interrupt_enable(periodic_int_select sel, int int_sel=0)
Definition
PCF2131_base.cpp:155
PCF2131_base::~PCF2131_base
virtual ~PCF2131_base()
Definition
PCF2131_base.cpp:7
PCF2131_base::alarm_disable
void alarm_disable(void)
Definition
PCF2131_base.cpp:86
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:68
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