RTC_NXP_Arduino 0.5.0
LED driver device operation sample code for Arduino
Loading...
Searching...
No Matches
PCF2131_base.cpp
1#include "RTC_NXP.h"
2
4{
5}
6
8{
9}
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
68void PCF2131_base::alarm( alarm_setting digit, int val )
69{
70 alarm( digit, val, 0 );
71}
72
73void 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
82{
83 _bit_op8( Control_2, ~0x10, 0x00 );
84}
85
87{
88 _bit_op8( Control_2, ~0x02, 0x00 );
89}
90
91void PCF2131_base::timestamp( int num, timestanp_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
106time_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
132{
133 uint8_t dummy[ 3 ];
134 int_clear( dummy );
135
136 return 0; // dummy
137}
138
139uint8_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
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}
void timestamp(int num, timestanp_setting ts_setting, int int_sel=0)
void begin(void)
periodic_int_select
Definition: RTC_NXP.h:142
void set(struct tm *now_tm)
uint8_t int_clear(void)
void alarm_clear(void)
time_t rtc_time(void)
void periodic_interrupt_enable(periodic_int_select sel, int int_sel=0)
virtual ~PCF2131_base()
Definition: PCF2131_base.cpp:7
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