COSA
An Object-Oriented Platform for Arduino Programming
MCP7940N.cpp
Go to the documentation of this file.
1 
21 #include "MCP7940N.hh"
22 
24  ExternalInterrupt(pin, ExternalInterrupt::ON_RISING_MODE),
25  m_triggered(false)
26 {
27 }
28 
29 void
31 {
32  UNUSED(arg);
33  m_triggered = true;
34 }
35 
36 int
37 MCP7940N::read(void* regs, uint8_t size, uint8_t pos)
38 {
39  twi.acquire(this);
40  twi.write(pos);
41  int count = twi.read(regs, size);
42  twi.release();
43  return (count);
44 }
45 
46 int
47 MCP7940N::write(void* regs, uint8_t size, uint8_t pos)
48 {
49  twi.acquire(this);
50  int count = twi.write(pos, regs, size);
51  twi.release();
52  return (count - 1);
53 }
54 
55 bool
57 {
58  if (read(&now, sizeof(now)) != sizeof(now)) return (false);
59  now.seconds &= 0x7f;
60  now.day &= 0x07;
61  now.month &= 0x1f;
62  return (true);
63 }
64 
65 bool
67 {
68  now.seconds |= 0x80;
69  int res = write(&now, sizeof(now));
70  now.seconds &= 0x7f;
71  return (res == sizeof(now));
72 }
73 
74 bool
75 MCP7940N::get_alarm(uint8_t nr, time_t& alarm, uint8_t& when)
76 {
77  // Map alarm number to offset
78  uint8_t pos;
79  if (nr == 0)
80  pos = offsetof(rtcc_t,alarm0);
81  else if (nr == 1)
82  pos = offsetof(rtcc_t,alarm1);
83  else return (false);
84 
85  // Read alarm information and copy configuration
86  if (read(&alarm, sizeof(alarm_t), pos) != sizeof(alarm_t)) return (false);
87  when = alarm_t::config_t(alarm.day).when;
88  alarm.day &= 0x7;
89  return (true);
90 }
91 
92 bool
93 MCP7940N::set_alarm(uint8_t nr, time_t& alarm, uint8_t when)
94 {
95  // Map alarm number to offset
96  uint8_t pos;
97  if (nr == 0)
98  pos = offsetof(rtcc_t,alarm0);
99  else if (nr == 1)
100  pos = offsetof(rtcc_t,alarm1);
101  else return (false);
102 
103  // Create configuration and write alarm information
104  alarm_t::config_t config(alarm.day);
105  config.polarity = 1;
106  config.when = when;
107  alarm.day = config.as_uint8;
108  if (write(&alarm, sizeof(alarm_t), pos) != sizeof(alarm_t)) return (false);
109 
110  // Update control flags and enable alarm interrupt and handler
111  control_t cntrl;
112  pos = offsetof(rtcc_t,control);
113  if (read(&cntrl, sizeof(cntrl), pos) != sizeof(cntrl)) return (false);
114  if (nr == 0)
115  cntrl.alm0en = 1;
116  else if (nr == 1)
117  cntrl.alm1en = 1;
118  if (write(&cntrl, sizeof(cntrl), pos) != sizeof(cntrl)) return (false);
120  return (true);
121 }
122 
123 uint8_t
125 {
126  // Check if an interrupt has been received
127  alarm_t::config_t config;
128  uint8_t pos;
129  int res = 0;
130  if (!m_alarm_irq.m_triggered) return (0);
131  m_alarm_irq.m_triggered = false;
132 
133  // Read alarm 0 configuration. Check if alarm is triggered
134  pos = offsetof(rtcc_t,alarm0.day);
135  if (read(&config, sizeof(config), pos) != sizeof(config)) return (0);
136  if (config.triggered) res |= 0x01;
137 
138  // Read alarm 1 configuration. Check if alarm is triggered
139  pos = offsetof(rtcc_t,alarm1.day);
140  if (read(&config, sizeof(config), pos) != sizeof(config)) return (0);
141  if (config.triggered) res |= 0x02;
142  return (res);
143 }
144 
145 bool
147 {
148  // Update control flags; read, clear flag and write back
149  control_t cntrl;
150  uint8_t pos = offsetof(rtcc_t,control);
151  if (read(&cntrl, sizeof(cntrl), pos) != sizeof(cntrl)) return (false);
152  if (nr == 0)
153  cntrl.alm0en = 0;
154  else if (nr == 1)
155  cntrl.alm1en = 0;
156  else return (false);
157  if (write(&cntrl, sizeof(cntrl), pos) != sizeof(cntrl)) return (false);
158 
159  // Check if interrupt handler should be disabled
160  if (!cntrl.alm0en && !cntrl.alm1en) m_alarm_irq.disable();
161  return (true);
162 }
163 
164 bool
166 {
167  control_t cntrl;
168  uint8_t pos = offsetof(rtcc_t,control);
169  if (read(&cntrl, sizeof(cntrl), pos) != sizeof(cntrl)) return (false);
170  cntrl.sqwen = flag;
171  return (write(&cntrl, sizeof(cntrl), pos) == sizeof(cntrl));
172 }
173 
175 {
176  outs << bcd << t.month << '-'
177  << bcd << t.date << ' '
178  << bcd << t.hours << ':'
179  << bcd << t.minutes << ':'
180  << bcd << t.seconds;
181  return (outs);
182 }
183 
185 {
186  t.clock.to_binary();
187  outs << t.clock << ' '
188  << bin << t.control.as_uint8 << ' '
189  << t.calibration << ' '
190  << t.alarm0 << ' '
191  << t.alarm1 << ' ';
192  t.clock.to_bcd();
193  return (outs);
194 }
uint8_t pending_alarm()
Definition: MCP7940N.cpp:124
TWI twi
Definition: TWI.cpp:27
IOStream & bcd(IOStream &outs)
Definition: IOStream.hh:745
uint8_t as_uint8
Unsigned byte access.
Definition: MCP7940N.hh:61
uint8_t day
01-07 Day.
Definition: Time.hh:106
uint8_t month
01-12 Month.
Definition: MCP7940N.hh:96
int write(void *regs, uint8_t size, uint8_t pos=0)
Definition: MCP7940N.cpp:47
uint8_t minutes
00-59 Minutes.
Definition: MCP7940N.hh:92
virtual void on_interrupt(uint16_t arg=0)
Definition: MCP7940N.cpp:30
bool get_time(time_t &now)
Definition: MCP7940N.cpp:56
uint8_t when
Alarm Match.
Definition: MCP7940N.hh:126
virtual void disable()
uint8_t triggered
Alarm Interrupt Flag.
Definition: MCP7940N.hh:125
bool square_wave(bool flag)
Definition: MCP7940N.cpp:165
uint8_t month
01-12 Month.
Definition: Time.hh:108
uint8_t seconds
00-59 Seconds.
Definition: MCP7940N.hh:91
IOStream & bin(IOStream &outs)
Definition: IOStream.hh:757
AlarmInterrupt m_alarm_irq
Definition: MCP7940N.hh:313
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
void release()
Definition: TWI.cpp:58
void write(int value) const
Definition: OutputPin.hh:236
time_t clock
Current time.
Definition: MCP7940N.hh:184
bool set_alarm(uint8_t nr, time_t &alarm, uint8_t when)
Definition: MCP7940N.cpp:93
uint8_t hours
00-23 Hours.
Definition: MCP7940N.hh:93
control_t control
Device control register.
Definition: MCP7940N.hh:185
int read(void *buf, size_t size)
Definition: TWI.hh:326
uint8_t alm1en
Alarm 1 Module Enable.
Definition: MCP7940N.hh:67
int8_t calibration
Time calibration (in clock cycles).
Definition: MCP7940N.hh:186
ExternalInterruptPin
Definition: ATmega1284P.hh:190
uint8_t seconds
00-59 Seconds.
Definition: Time.hh:103
bool read() const
Definition: Pin.hh:172
int write(void *buf, size_t size)
Definition: TWI.hh:282
uint8_t as_uint8
< Day, alarm configuration and status.
Definition: MCP7940N.hh:122
#define UNUSED(x)
Definition: ATmega328P.hh:31
Definition: Time.hh:102
void acquire(TWI::Driver *dev)
Definition: TWI.cpp:36
uint8_t date
01-31 Date.
Definition: MCP7940N.hh:95
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 polarity
Alarm Pin (MFP) Polarity.
Definition: MCP7940N.hh:127
OutputPin & operator<<(int value)
Definition: OutputPin.hh:364
uint8_t sqwen
Square-Ware Enable.
Definition: MCP7940N.hh:68
void to_binary()
Definition: Time.hh:115
AlarmInterrupt(Board::ExternalInterruptPin pin)
Definition: MCP7940N.cpp:23
void to_bcd()
Definition: Time.hh:125
bool clear_alarm(uint8_t nr)
Definition: MCP7940N.cpp:146
bool set_time(time_t &now)
Definition: MCP7940N.cpp:66
uint8_t alm0en
Alarm 0 Module Enable.
Definition: MCP7940N.hh:66