RTC_NXP_Arduino 1.1.3
RTC driver device operation sample code for Arduino
Loading...
Searching...
No Matches
PCF2131_base.cpp
1#include "RTC_NXP.h"
2
6
10
12{
13 int_clear();
14}
15
16
18{
19 return _reg_r( Seconds ) & 0x80;
20}
21
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
41void 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
71 delay( 100 ); // OTP refresh will be completed less than 100 ms
72}
73
74void PCF2131_base::alarm( alarm_setting digit, int val )
75{
76 alarm( digit, val, 0 );
77}
78
79void 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
88{
89 _bit_op8( Control_2, ~0x10, 0x00 );
90}
91
93{
94 _bit_op8( Control_2, ~0x02, 0x00 );
95}
96
97void 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
112time_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
138{
139 uint8_t dummy[ 3 ];
140 int_clear( dummy );
141
142 return 0; // dummy
143}
144
145uint8_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
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
177{
178 _bit_op8(CLKOUT_ctl, ~0b00000111, freq);
179}
180
182{
183 _reg_w(SR_Reset, 0b00101100);
184}
185
187{
188 _bit_op8(CLKOUT_ctl, ~0b00100000, 0b00000000);
189 _bit_op8(CLKOUT_ctl, ~0b00100000, 0b00100000);
190}
void begin(void)
void set(struct tm *now_tm)
uint8_t int_clear(void)
void alarm_clear(void)
void set_clock_out(clock_out_frequency freq)
time_t rtc_time(void)
void timestamp(int num, timestamp_setting ts_setting, int int_sel=0)
void periodic_interrupt_enable(periodic_int_select sel, int int_sel=0)
virtual ~PCF2131_base()
void alarm_disable(void)
bool oscillator_stop(void)
void alarm(alarm_setting digit, int val)
virtual void _reg_r(uint8_t reg, uint8_t *vp, int len)=0
virtual void _bit_op8(uint8_t reg, uint8_t mask, uint8_t val)=0
virtual void _reg_w(uint8_t reg, uint8_t *vp, int len)=0
alarm_setting
Definition RTC_NXP.h:35
static uint8_t dec2bcd(uint8_t v)
Definition RTC_NXP.cpp:24
static uint8_t bcd2dec(uint8_t v)
Definition RTC_NXP.cpp:19