COSA
An Object-Oriented Platform for Arduino Programming
DS1302.cpp
Go to the documentation of this file.
1 
21 #include "DS1302.hh"
22 
23 uint8_t
25 {
26  uint8_t bits = CHARBITS;
27  uint8_t res = 0;
28  synchronized {
29  for (;;) {
30  if (m_sda.is_set()) res |= 0x80;
31  m_clk._toggle();
32  m_clk._toggle();
33  if (UNLIKELY(--bits == 0)) break;
34  res >>= 1;
35  }
36  }
37  return (res);
38 }
39 
40 void
41 DS1302::write(uint8_t data)
42 {
43  uint8_t bits = CHARBITS;
44  synchronized {
45  do {
46  m_sda._write(data & 0x01);
47  m_clk._toggle();
48  data >>= 1;
49  m_clk._toggle();
50  } while (--bits);
51  }
52 }
53 
54 uint8_t
55 DS1302::read(uint8_t addr)
56 {
57  addr = ((addr << 1) | READ);
58  uint8_t res = 0;
59  asserted(m_cs) {
60  write(addr);
62  res = read();
64  }
65  return (res);
66 }
67 
68 void
69 DS1302::write(uint8_t addr, uint8_t data)
70 {
71  addr = ((addr << 1) | WRITE);
72  asserted(m_cs) {
73  write(addr);
74  write(data);
75  }
76 }
77 
78 void
80 {
81  asserted(m_cs) {
82  write(RTC_BURST | READ);
84  now.seconds = read();
85  now.minutes = read();
86  now.hours = read();
87  now.date = read();
88  now.month = read();
89  now.day = read();
90  now.year = read();
92  }
93 }
94 
95 void
97 {
98  write_protect(false);
99  asserted(m_cs) {
100  write(RTC_BURST | WRITE);
101  write(now.seconds);
102  write(now.minutes);
103  write(now.hours);
104  write(now.date);
105  write(now.month);
106  write(now.day);
107  write(now.year);
108  }
109  write_protect(true);
110 }
111 
112 void
113 DS1302::read_ram(void* buf, size_t size)
114 {
115  if (UNLIKELY(size == 0)) return;
116  uint8_t* bp = (uint8_t*) buf;
117  if (UNLIKELY(size > RAM_MAX)) size = RAM_MAX;
118  asserted(m_cs) {
119  write(RAM_BURST | READ);
121  do *bp++ = read(); while (--size);
123  }
124 }
125 
126 void
127 DS1302::write_ram(void* buf, size_t size)
128 {
129  if (UNLIKELY(size == 0)) return;
130  uint8_t* bp = (uint8_t*) buf;
131  if (UNLIKELY(size > RAM_MAX)) size = RAM_MAX;
132  write_protect(false);
133  asserted(m_cs) {
134  write(RAM_BURST | WRITE);
135  do write(*bp++); while (--size);
136  }
137  write_protect(true);
138 }
uint8_t read_ram(uint8_t addr)
Definition: DS1302.hh:119
RAM burst transfer.
Definition: DS1302.hh:163
uint8_t minutes
00-59 Minutes.
Definition: Time.hh:104
void mode(Mode mode)
Definition: IOPin.hh:56
#define asserted(pin)
Definition: Pin.hh:275
uint8_t day
01-07 Day.
Definition: Time.hh:106
Read/write bit in write mode.
Definition: DS1302.hh:160
void write(uint8_t addr, uint8_t data)
Definition: DS1302.cpp:69
void set_time(time_t &now)
Definition: DS1302.cpp:96
RTC register burst transfer.
Definition: DS1302.hh:162
OutputPin m_cs
Chip select, asserted high.
Definition: DS1302.hh:167
void write_protect(bool flag)
Definition: DS1302.hh:97
uint8_t month
01-12 Month.
Definition: Time.hh:108
static const size_t RAM_MAX
Definition: DS1302.hh:54
Read/write bit in read mode.
Definition: DS1302.hh:161
#define CHARBITS
Definition: Types.h:57
bool is_set() const
Definition: Pin.hh:112
void _toggle() const
Definition: OutputPin.hh:153
uint8_t year
00-99 Year.
Definition: Time.hh:109
uint8_t seconds
00-59 Seconds.
Definition: Time.hh:103
Definition: Time.hh:102
IOPin m_sda
Serial data, bidirectional.
Definition: DS1302.hh:168
void _write(int value) const
Definition: OutputPin.hh:219
void get_time(time_t &now)
Definition: DS1302.cpp:79
uint8_t hours
00-23 Hours.
Definition: Time.hh:105
uint8_t read()
Definition: DS1302.cpp:24
uint8_t date
01-31 Date.
Definition: Time.hh:107
#define UNLIKELY(x)
Definition: Types.h:153
OutputPin m_clk
Clock for synchronized data.
Definition: DS1302.hh:169
void write_ram(uint8_t addr, uint8_t data)
Definition: DS1302.hh:131