COSA
An Object-Oriented Platform for Arduino Programming
MCP7940N.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_MCP7940N_HH
22 #define COSA_MCP7940N_HH
23 
24 #include "Cosa/TWI.hh"
25 #include "Cosa/Time.hh"
26 #include "Cosa/IOStream.hh"
28 
37 class MCP7940N : private TWI::Driver {
38 public:
42  union config_t {
43  uint8_t as_uint8;
44  struct {
45  uint8_t day:3;
46  uint8_t vbaten:1;
47  uint8_t vbat:1;
48  uint8_t oscon:1;
49  uint8_t reserved:2;
50  };
51  config_t(uint8_t value = 0)
52  {
53  as_uint8 = value;
54  }
55  };
56 
60  union control_t {
61  uint8_t as_uint8;
62  struct {
63  uint8_t sqwfs:2;
64  uint8_t crstrim:1;
65  uint8_t extosc:1;
66  uint8_t alm0en:1;
67  uint8_t alm1en:1;
68  uint8_t sqwen:1;
69  uint8_t out:1;
70  };
71  control_t(uint8_t value = 0)
72  {
73  as_uint8 = value;
74  }
75  };
76 
80  enum {
85  } __attribute__((packed));
86 
90  struct alarm_t {
91  uint8_t seconds;
92  uint8_t minutes;
93  uint8_t hours;
94  uint8_t day;
95  uint8_t date;
96  uint8_t month;
97 
102  void to_binary()
103  __attribute__((always_inline))
104  {
105  ::to_binary(&seconds, sizeof(alarm_t));
106  }
107 
112  void to_bcd()
113  __attribute__((always_inline))
114  {
115  ::to_bcd(&seconds, sizeof(alarm_t));
116  }
117 
121  union config_t {
122  uint8_t as_uint8;
123  struct {
124  uint8_t day:3;
125  uint8_t triggered:1;
126  uint8_t when:3;
127  uint8_t polarity:1;
128  };
129  config_t(uint8_t value = 0)
130  {
131  as_uint8 = value;
132  }
133  };
134  };
135 
139  enum {
140  WHEN_SEC_MATCH = 0x00,
141  WHEN_MIN_MATCH = 0x01,
143  WHEN_DAY_MATCH = 0x03,
146  } __attribute__ ((packed));
147 
151  struct timestamp_t {
152  uint8_t seconds;
153  uint8_t minutes;
154  uint8_t hours;
155  uint8_t day;
156  uint8_t date;
157  uint8_t month;
158 
163  void to_binary()
164  __attribute__((always_inline))
165  {
166  ::to_binary(&seconds, sizeof(timestamp_t));
167  }
168 
173  void to_bcd()
174  __attribute__((always_inline))
175  {
176  ::to_bcd(&seconds, sizeof(time_t));
177  }
178  };
179 
183  struct rtcc_t {
186  int8_t calibration;
187  uint8_t reserved1;
189  uint8_t reserved2;
191  uint8_t reserved3;
194  };
195 
197  const static uint8_t RAM_START = sizeof(rtcc_t);
198 
200  const static uint8_t RAM_END = 0x5f;
201 
203  const static uint8_t RAM_MAX = RAM_END - RAM_START + 1;
204 
209 #if !defined(BOARD_ATTINY)
211  TWI::Driver(0x6f),
212  m_alarm_irq(pin)
213  {}
214 #else
216  TWI::Driver(0x6f),
217  m_alarm_irq(pin)
218  {}
219 #endif
220 
227  bool get_time(time_t& now);
228 
235  bool set_time(time_t& now);
236 
245  bool set_alarm(uint8_t nr, time_t& alarm, uint8_t when);
246 
255  bool get_alarm(uint8_t nr, time_t& alarm, uint8_t& when);
256 
263  bool clear_alarm(uint8_t nr);
264 
271  uint8_t pending_alarm();
272 
277  bool square_wave(bool flag);
278 
279 protected:
288  int read(void* regs, uint8_t size, uint8_t pos = 0);
289 
298  int write(void* regs, uint8_t size, uint8_t pos = 0);
299 
304  public:
306  virtual void on_interrupt(uint16_t arg = 0);
307  protected:
308  friend class MCP7940N;
310  };
311 
314 };
315 
324 
333 #endif
uint8_t pending_alarm()
Definition: MCP7940N.cpp:124
uint8_t day
01-7 Day.
Definition: MCP7940N.hh:94
Definition: TWI.hh:51
uint8_t month
01-12 Month.
Definition: MCP7940N.hh:157
IOStream & operator<<(IOStream &outs, MCP7940N::alarm_t &t)
Definition: MCP7940N.cpp:174
uint8_t day
< Bitfield access.
Definition: MCP7940N.hh:45
uint8_t to_binary(uint8_t value)
Definition: BCD.h:42
uint8_t as_uint8
Unsigned byte access.
Definition: MCP7940N.hh:61
uint8_t month
01-12 Month.
Definition: MCP7940N.hh:96
uint8_t reserved2
Reserved - do not use.
Definition: MCP7940N.hh:189
int write(void *regs, uint8_t size, uint8_t pos=0)
Definition: MCP7940N.cpp:47
static const uint8_t RAM_END
Definition: MCP7940N.hh:200
uint8_t seconds
00-59 Seconds.
Definition: MCP7940N.hh:152
uint8_t minutes
00-59 Minutes.
Definition: MCP7940N.hh:153
uint8_t minutes
00-59 Minutes.
Definition: MCP7940N.hh:92
bool get_time(time_t &now)
Definition: MCP7940N.cpp:56
static const uint8_t RAM_START
Definition: MCP7940N.hh:197
uint8_t reserved1
Reserved - do not use.
Definition: MCP7940N.hh:187
Sec, Min, Hour, Day, Date, Month.
Definition: MCP7940N.hh:145
bool square_wave(bool flag)
Definition: MCP7940N.cpp:165
uint8_t seconds
00-59 Seconds.
Definition: MCP7940N.hh:91
config_t(uint8_t value=0)
Definition: MCP7940N.hh:51
AlarmInterrupt m_alarm_irq
Definition: MCP7940N.hh:313
Matches the current Day.
Definition: MCP7940N.hh:143
control_t(uint8_t value=0)
Definition: MCP7940N.hh:71
bool get_alarm(uint8_t nr, time_t &alarm, uint8_t &when)
Definition: MCP7940N.cpp:75
alarm_t alarm1
Alarm 1 setting (time/configuration).
Definition: MCP7940N.hh:190
static const uint8_t RAM_MAX
Definition: MCP7940N.hh:203
uint8_t hours
00-23 Hours.
Definition: MCP7940N.hh:154
time_t clock
Current time.
Definition: MCP7940N.hh:184
uint8_t reserved
Reserved.
Definition: MCP7940N.hh:49
bool set_alarm(uint8_t nr, time_t &alarm, uint8_t when)
Definition: MCP7940N.cpp:93
uint8_t as_uint8
Unsigned byte access.
Definition: MCP7940N.hh:43
uint8_t hours
00-23 Hours.
Definition: MCP7940N.hh:93
Minutes match.
Definition: MCP7940N.hh:141
control_t control
Device control register.
Definition: MCP7940N.hh:185
Seconds match.
Definition: MCP7940N.hh:140
uint8_t date
01-31 Date.
Definition: MCP7940N.hh:156
int8_t calibration
Time calibration (in clock cycles).
Definition: MCP7940N.hh:186
ExternalInterruptPin
Definition: ATmega1284P.hh:190
Hours match (takes into accout 12/24 hour).
Definition: MCP7940N.hh:142
uint8_t as_uint8
< Day, alarm configuration and status.
Definition: MCP7940N.hh:122
Definition: Time.hh:102
uint8_t oscon
Oscillator Running Status.
Definition: MCP7940N.hh:48
uint8_t to_bcd(uint8_t value)
Definition: BCD.h:56
Driver(uint8_t addr)
Definition: TWI.hh:70
uint8_t vbaten
External Battery Enable.
Definition: MCP7940N.hh:46
uint8_t date
01-31 Date.
Definition: MCP7940N.hh:95
uint8_t day
01-07 Day.
Definition: MCP7940N.hh:155
alarm_t alarm0
Alarm 0 setting (time/configuration).
Definition: MCP7940N.hh:188
int read(void *regs, uint8_t size, uint8_t pos=0)
Definition: MCP7940N.cpp:37
uint8_t reserved3
Reserved - do not use.
Definition: MCP7940N.hh:191
timestamp_t vcc_restored
Timestamp when Vcc restored.
Definition: MCP7940N.hh:193
config_t(uint8_t value=0)
Definition: MCP7940N.hh:129
timestamp_t vcc_failed
Timestamp when Vcc fails.
Definition: MCP7940N.hh:192
uint8_t vbat
Battey Used Status.
Definition: MCP7940N.hh:47
bool clear_alarm(uint8_t nr)
Definition: MCP7940N.cpp:146
bool set_time(time_t &now)
Definition: MCP7940N.cpp:66
MCP7940N(Board::ExternalInterruptPin pin=Board::EXT1)
Definition: MCP7940N.hh:210