COSA
An Object-Oriented Platform for Arduino Programming
DS3231.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_DS3231_HH
22 #define COSA_DS3231_HH
23 
24 #include "Cosa/TWI.hh"
25 #include "Cosa/Time.hh"
26 #include "Cosa/IOStream.hh"
27 
50 class DS3231 : private TWI::Driver {
51 public:
55  struct alarm1_t {
56  enum {
57  ONCE_PER_SEC = 0x0f,
63  } __attribute__ ((packed));
64  uint8_t seconds;
65  uint8_t minutes;
66  uint8_t hours;
67  union {
68  uint8_t day;
69  uint8_t date;
70  };
71 
77  void to_binary()
78  {
79  ::to_binary(&seconds, sizeof(alarm1_t));
80  }
81 
87  void to_bcd()
88  {
89  ::to_bcd(&seconds, sizeof(alarm1_t));
90  }
91  };
92 
96  struct alarm2_t {
97  enum {
98  ONCE_PER_MIN = 0x07,
99  WHEN_MIN_MATCH = 0x06,
103  } __attribute__ ((packed));
104  uint8_t minutes;
105  uint8_t hours;
106  union {
107  uint8_t day;
108  uint8_t date;
109  };
110 
116  void to_binary()
117  {
118  ::to_binary(&minutes, sizeof(alarm2_t));
119  }
120 
126  void to_bcd()
127  {
128  ::to_bcd(&minutes, sizeof(alarm2_t));
129  }
130  };
131 
135  union control_t {
136  uint8_t as_uint8;
137  struct {
138  uint8_t a1ie:1;
139  uint8_t a2ie:1;
140  uint8_t intcn:1;
141  uint8_t rs:2;
142  uint8_t conv:1;
143  uint8_t bbsqw:1;
144  uint8_t eosc:1;
145  };
146  };
147 
151  enum {
152  RS_1_HZ = 0,
156  };
157 
161  union status_t {
162  uint8_t as_uint8;
163  struct {
164  uint8_t a1f:1;
165  uint8_t a2f:1;
166  uint8_t bsy:1;
167  uint8_t en32khz:1;
168  uint8_t reserved:3;
169  uint8_t osf:1;
170  };
171  };
172 
176  struct timekeeper_t {
182  int8_t aging;
183  int16_t temp;
184  };
185 
189  DS3231() : TWI::Driver(0x68) {}
190 
199  int read(void* regs, uint8_t size, uint8_t pos = 0);
200 
209  int write(void* regs, uint8_t size, uint8_t pos = 0);
210 
217  bool get_time(time_t& now)
218  __attribute__((always_inline))
219  {
220  return (read(&now, sizeof(now)) == sizeof(now));
221  }
222 
229  bool set_time(time_t& now)
230  __attribute__((always_inline))
231  {
232  return (write(&now, sizeof(now)) == sizeof(now));
233  }
234 
242  bool get_alarm1(alarm1_t& alarm, uint8_t& mask)
243  __attribute__((always_inline))
244  {
245  return (get(&alarm, sizeof(alarm), offsetof(timekeeper_t, alarm1), mask));
246  }
247 
255  bool set_alarm1(alarm1_t& alarm, uint8_t mask)
256  __attribute__((always_inline))
257  {
258  return (set(&alarm, sizeof(alarm), offsetof(timekeeper_t, alarm1), mask));
259  }
260 
268  bool get_alarm2(alarm2_t& alarm, uint8_t& mask)
269  __attribute__((always_inline))
270  {
271  return (get(&alarm, sizeof(alarm), offsetof(timekeeper_t, alarm2), mask));
272  }
273 
281  bool set_alarm2(alarm2_t& alarm, uint8_t mask)
282  __attribute__((always_inline))
283  {
284  return (set(&alarm, sizeof(alarm), offsetof(timekeeper_t, alarm2), mask));
285  }
286 
291  int16_t temperature();
292 
299  bool square_wave(bool flag);
300 
301 private:
311  bool get(void* alarm, uint8_t size, uint8_t offset, uint8_t& mask);
312 
322  bool set(void* alarm, uint8_t size, uint8_t offset, uint8_t mask);
323 };
324 
333 
342 
351 
352 #endif
int write(void *regs, uint8_t size, uint8_t pos=0)
Definition: DS3231.cpp:35
Definition: TWI.hh:51
bool set_alarm1(alarm1_t &alarm, uint8_t mask)
Definition: DS3231.hh:255
time_t clock
Current time.
Definition: DS3231.hh:177
uint8_t minutes
Definition: DS3231.hh:104
uint8_t day
Definition: DS3231.hh:107
uint8_t as_uint8
Definition: DS3231.hh:136
bool set_alarm2(alarm2_t &alarm, uint8_t mask)
Definition: DS3231.hh:281
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
Definition: DS3231.hh:50
control_t control
Device control register.
Definition: DS3231.hh:180
bool get_time(time_t &now)
Definition: DS3231.hh:217
void to_binary()
Definition: DS3231.hh:116
uint8_t day
Definition: DS3231.hh:68
uint8_t hours
Definition: DS3231.hh:105
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
void to_bcd()
Definition: DS3231.hh:126
bool set_time(time_t &now)
Definition: DS3231.hh:229
alarm2_t alarm2
Alarm 2 setting.
Definition: DS3231.hh:179
bool get_alarm1(alarm1_t &alarm, uint8_t &mask)
Definition: DS3231.hh:242
Definition: Time.hh:102
Driver(uint8_t addr)
Definition: TWI.hh:70
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
void to_binary()
Definition: DS3231.hh:77
IOStream & operator<<(IOStream &outs, DS3231::alarm1_t &t)
Definition: DS3231.cpp:94
bool get_alarm2(alarm2_t &alarm, uint8_t &mask)
Definition: DS3231.hh:268
DS3231()
Definition: DS3231.hh:189
uint8_t seconds
Definition: DS3231.hh:64
uint8_t minutes
Definition: DS3231.hh:65
uint8_t date
Definition: DS3231.hh:69
void to_bcd()
Definition: DS3231.hh:87