COSA
An Object-Oriented Platform for Arduino Programming
DS3231.cpp
Go to the documentation of this file.
1 
21 #include "DS3231.hh"
22 #include "Cosa/BCD.h"
23 
24 int
25 DS3231::read(void* regs, uint8_t size, uint8_t pos)
26 {
27  twi.acquire(this);
28  twi.write(pos);
29  int res = twi.read(regs, size);
30  twi.release();
31  return (res);
32 }
33 
34 int
35 DS3231::write(void* regs, uint8_t size, uint8_t pos)
36 {
37  twi.acquire(this);
38  int res = twi.write(pos, regs, size);
39  twi.release();
40  return (res < 0 ? res : res - 1);
41 }
42 
43 bool
44 DS3231::get(void* alarm, uint8_t size, uint8_t offset, uint8_t& mask)
45 {
46  int res = read(alarm, size, offset);
47  if (UNLIKELY(res != size)) return (false);
48  mask = 0;
49  uint8_t* buf = (uint8_t*) alarm;
50  for (uint8_t i = 0; i < size; i++) {
51  if (buf[i] & 0x80) {
52  buf[i] &= 0x78;
53  mask |= (1 << i);
54  }
55  }
56  return (true);
57 }
58 
59 bool
60 DS3231::set(void* alarm, uint8_t size, uint8_t offset, uint8_t mask)
61 {
62  uint8_t* buf = (uint8_t*) alarm;
63  for (uint8_t i = 0; i < size; i++) {
64  if (mask & (1 << i)) {
65  buf[i] |= 0x80;
66  }
67  }
68  int res = write(alarm, size, offset);
69  return (res == size);
70 }
71 
72 int16_t
74 {
75  int16_t temp = 0;
76  read(&temp, sizeof(temp), offsetof(timekeeper_t, temp));
77  return (swap(temp) >> 6);
78 }
79 
80 bool
82 {
83  control_t control;
84  int res;
85  res = read(&control, sizeof(control), offsetof(timekeeper_t, control));
86  if (res != sizeof(control)) return (false);
87  control.bbsqw = flag;
88  control.intcn = !flag;
89  control.rs = RS_1_HZ;
90  res = write(&control, sizeof(control), offsetof(timekeeper_t, control));
91  return (res == sizeof(control));
92 }
93 
95 {
96  outs << bcd << t.date << ' '
97  << bcd << t.hours << ':'
98  << bcd << t.minutes << ':'
99  << bcd << t.seconds;
100  return (outs);
101 }
102 
104 {
105  outs << bcd << t.date << ' '
106  << bcd << t.hours << ':'
107  << bcd << t.minutes;
108  return (outs);
109 }
110 
112 {
113  int16_t temp = swap(t.temp) >> 6;
114  t.clock.to_binary();
115  outs << t.clock << ' '
116  << t.alarm1 << ' '
117  << t.alarm2 << ' '
118  << bin << t.control.as_uint8 << ' '
119  << bin << t.status.as_uint8 << ' '
120  << t.aging << ' '
121  << (temp >> 2) << '.' << (25 * (temp & 0x3));
122  t.clock.to_bcd();
123  return (outs);
124 }
int write(void *regs, uint8_t size, uint8_t pos=0)
Definition: DS3231.cpp:35
TWI twi
Definition: TWI.cpp:27
IOStream & bcd(IOStream &outs)
Definition: IOStream.hh:745
time_t clock
Current time.
Definition: DS3231.hh:177
uint8_t minutes
Definition: DS3231.hh:104
uint8_t as_uint8
Definition: DS3231.hh:136
bool square_wave(bool flag)
Definition: DS3231.cpp:81
status_t status
Device status register.
Definition: DS3231.hh:181
uint8_t hours
Definition: DS3231.hh:66
uint8_t date
Definition: DS3231.hh:108
control_t control
Device control register.
Definition: DS3231.hh:180
IOStream & bin(IOStream &outs)
Definition: IOStream.hh:757
uint8_t hours
Definition: DS3231.hh:105
void release()
Definition: TWI.cpp:58
int read(void *regs, uint8_t size, uint8_t pos=0)
Definition: DS3231.cpp:25
int16_t temp
Device temperature.
Definition: DS3231.hh:183
int8_t aging
Crystal adjustment.
Definition: DS3231.hh:182
IOStream & operator<<(IOStream &outs, DS3231::alarm1_t &t)
Definition: DS3231.cpp:94
int read(void *buf, size_t size)
Definition: TWI.hh:326
alarm2_t alarm2
Alarm 2 setting.
Definition: DS3231.hh:179
#define swap(a, b)
Definition: Canvas.cpp:164
int write(void *buf, size_t size)
Definition: TWI.hh:282
void acquire(TWI::Driver *dev)
Definition: TWI.cpp:36
uint8_t as_uint8
Definition: DS3231.hh:162
int16_t temperature()
Definition: DS3231.cpp:73
alarm1_t alarm1
Alarm 1 setting.
Definition: DS3231.hh:178
uint8_t intcn
Interrupt Control.
Definition: DS3231.hh:140
uint8_t bbsqw
Battery-Backup Square-Wave Enable.
Definition: DS3231.hh:143
#define UNLIKELY(x)
Definition: Types.h:153
void to_binary()
Definition: Time.hh:115
uint8_t seconds
Definition: DS3231.hh:64
void to_bcd()
Definition: Time.hh:125
uint8_t minutes
Definition: DS3231.hh:65
uint8_t rs
Rate Select.
Definition: DS3231.hh:141
uint8_t date
Definition: DS3231.hh:69