COSA
An Object-Oriented Platform for Arduino Programming
DS1302.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_DS1302_HH
22 #define COSA_DS1302_HH
23 
24 #include "Cosa/OutputPin.hh"
25 #include "Cosa/IOPin.hh"
26 #include "Cosa/Time.hh"
27 
48 class DS1302 {
49 public:
51  static const uint8_t RTC_START = 0;
52 
54  static const size_t RAM_MAX = 31;
55 
57  static const uint8_t RAM_START = 32;
58 
60  static const uint8_t RAM_END = RAM_START + RAM_MAX;
61 
72  m_cs(cs, 0),
73  m_sda(sda, IOPin::OUTPUT_MODE),
74  m_clk(clk, 0)
75  {}
76 
77  /*
78  * Low level RTC access function. Read data from the clock register
79  * or static memory on device.
80  * @param[in] addr device address.
81  * @return data.
82  */
83  uint8_t read(uint8_t addr);
84 
85  /*
86  * Low level RTC access function. Write given data to the clock
87  * register or static memory on device.
88  * @param[in] addr device address.
89  * @param[in] data to write.
90  */
91  void write(uint8_t addr, uint8_t data);
92 
97  void write_protect(bool flag)
98  __attribute__((always_inline))
99  {
100  write(WP, flag ? 0x80 : 0x00);
101  }
102 
107  void get_time(time_t& now);
108 
113  void set_time(time_t& now);
114 
119  uint8_t read_ram(uint8_t addr)
120  __attribute__((always_inline))
121  {
122  return (read(RAM_START | (addr & ADDR_MASK)));
123  }
124 
131  void write_ram(uint8_t addr, uint8_t data)
132  __attribute__((always_inline))
133  {
134  write(RAM_START | (addr & ADDR_MASK), data);
135  }
136 
143  void read_ram(void* buf, size_t size);
144 
152  void write_ram(void* buf, size_t size);
153 
154 protected:
156  static const uint8_t WP = 0x07;
157 
159  enum {
160  WRITE = 0x80,
161  READ = 0x81,
162  RTC_BURST = 0xbe,
163  RAM_BURST = 0xfe,
164  ADDR_MASK = 0x3f
165  } __attribute__((packed));
166 
170 
178  uint8_t read();
179 
186  void write(uint8_t data);
187 };
188 
189 #endif
190 
191 
uint8_t read_ram(uint8_t addr)
Definition: DS1302.hh:119
RAM burst transfer.
Definition: DS1302.hh:163
static const uint8_t RTC_START
Definition: DS1302.hh:51
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
Mask address bits.
Definition: DS1302.hh:164
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
static const size_t RAM_MAX
Definition: DS1302.hh:54
Read/write bit in read mode.
Definition: DS1302.hh:161
DS1302(Board::DigitalPin cs=Board::D4, Board::DigitalPin sda=Board::D3, Board::DigitalPin clk=Board::D2)
Definition: DS1302.hh:69
static const uint8_t WP
Definition: DS1302.hh:156
Definition: Time.hh:102
IOPin m_sda
Serial data, bidirectional.
Definition: DS1302.hh:168
Definition: IOPin.hh:29
void get_time(time_t &now)
Definition: DS1302.cpp:79
static const uint8_t RAM_END
Definition: DS1302.hh:60
static const uint8_t RAM_START
Definition: DS1302.hh:57
Definition: DS1302.hh:48
uint8_t read()
Definition: DS1302.cpp:24
OutputPin m_clk
Clock for synchronized data.
Definition: DS1302.hh:169
void write_ram(uint8_t addr, uint8_t data)
Definition: DS1302.hh:131