Arduino-TWI
Two-Wire Interface (TWI) library for Arduino
DS2482.h
Go to the documentation of this file.
1 
19 #ifndef DS2482_H
20 #define DS2482_H
21 
22 #include "TWI.h"
23 
28 class DS2482 : protected TWI::Device {
29 public:
35  DS2482(TWI& twi, uint8_t subaddr = 0) :
36  TWI::Device(twi, 0x18 | (subaddr & 0x03))
37  {
38  }
39 
46  {
47  status_t status;
48  uint8_t cmd;
49  int count;
50  bool res = false;
51 
52  // Issue one wire reset command
53  cmd = ONE_WIRE_RESET;
54  if (!TWI::Device::acquire()) return (false);
55  count = TWI::Device::write(&cmd, sizeof(cmd));
56  if (count != sizeof(cmd)) goto error;
57  if (one_wire_await(status)) res = status.PPD;
58 
59  error:
60  if (!TWI::Device::release()) return (false);
61  return (res);
62  }
63 
70  bool one_wire_read_bit(bool& value)
71  {
72  status_t status;
73  uint8_t cmd[2];
74  int count;
75  bool res = false;
76 
77  // Issue one wire single bit command with read data time slot
78  cmd[0] = ONE_WIRE_SINGLE_BIT;
79  cmd[1] = 0x80;
80  if (!TWI::Device::acquire()) return (false);
81  count = TWI::Device::write(cmd, sizeof(cmd));
82  if (count != sizeof(cmd)) goto error;
83 
84  // Wait for one wire operation to complete
85  res = one_wire_await(status);
86  value = status.SBR;
87 
88  error:
89  if (!TWI::Device::release()) return (false);
90  return (res);
91  }
92 
99  bool one_wire_write_bit(bool value)
100  {
101  status_t status;
102  uint8_t cmd[2];
103  int count;
104  bool res = false;
105 
106  // Issue one wire single bit command with given data
107  cmd[0] = ONE_WIRE_SINGLE_BIT;
108  cmd[1] = (value ? 0x80 : 0x00);
109  if (!TWI::Device::acquire()) return (false);
110  count = TWI::Device::write(cmd, sizeof(cmd));
111  if (count != sizeof(cmd)) goto error;
112  res = one_wire_await(status);
113 
114  error:
115  if (!TWI::Device::release()) return (false);
116  return (res);
117  }
118 
125  virtual bool one_wire_read_byte(uint8_t& value)
126  {
127  status_t status;
128  uint8_t cmd;
129  int count;
130 
131  // Issue one wire read byte command
132  cmd = ONE_WIRE_READ_BYTE;
133  if (!TWI::Device::acquire()) return (false);
134  count = TWI::Device::write(&cmd, sizeof(cmd));
135  if (count != sizeof(cmd)) goto error;
136 
137  // Wait for one wire operation to complete
138  if (!one_wire_await(status)) goto error;
139  if (!TWI::Device::release()) return (false);
140 
141  // Read data register value
142  return (set_read_pointer(READ_DATA_REGISTER, value));
143 
144  error:
146  return (false);
147  }
148 
155  bool one_wire_write_byte(uint8_t value)
156  {
157  status_t status;
158  uint8_t cmd[2];
159  int count;
160  bool res = false;
161 
162  // Issue one wire write byte command with given data
163  cmd[0] = ONE_WIRE_WRITE_BYTE;
164  cmd[1] = value;
165  if (!TWI::Device::acquire()) return (res);
166  count = TWI::Device::write(cmd, sizeof(cmd));
167  if (count != sizeof(cmd)) goto error;
168  res = one_wire_await(status);
169 
170  error:
171  if (!TWI::Device::release()) return (false);
172  return (res);
173  }
174 
183  int8_t one_wire_triplet(uint8_t& dir)
184  {
185  status_t status;
186  uint8_t cmd[2];
187  int count;
188 
189  // Issue one wire single bit command with given data
190  cmd[0] = ONE_WIRE_TRIPLET;
191  cmd[1] = (dir ? 0x80 : 0x00);
192  if (!TWI::Device::acquire()) return (-1);
193  count = TWI::Device::write(cmd, sizeof(cmd));
194  if (count != sizeof(cmd)) goto error;
195 
196  // Wait for one wire operation to complete
197  if (!one_wire_await(status)) goto error;
198  if (!TWI::Device::release()) return (-1);
199  dir = status.DIR;
200  return ((status >> 5) & 0x3);
201 
202  error:
204  return (-1);
205  }
206 
213  {
214  status_t status;
215  uint8_t cmd;
216  int count;
217 
218  // Issue device reset command
219  cmd = DEVICE_RESET;
220  if (!TWI::Device::acquire()) return (false);
221  count = TWI::Device::write(&cmd, sizeof(cmd));
222  if (count != sizeof(cmd)) goto error;
223 
224  // Check status register for device reset
225  count = TWI::Device::read(&status, sizeof(status));
226  if (!TWI::Device::release()) return (false);
227  return ((count == sizeof(status)) && status.RST);
228 
229  error:
231  return (false);
232  }
233 
242  bool write_configuration(bool apu = true, bool spu = false, bool iws = false)
243  {
244  config_t config;
245  status_t status;
246  uint8_t cmd[2];
247  int count;
248 
249  // Set configuration bit-fields
250  config.APU = apu;
251  config.SPU = spu;
252  config.IWS = iws;
253  config.COMP = ~config;
254 
255  // Issue write configuration command with given setting
256  cmd[0] = WRITE_CONGIFURATION;
257  cmd[1] = config;
258  if (!TWI::Device::acquire()) return (false);
259  count = TWI::Device::write(cmd, sizeof(cmd));
260  if (count != sizeof(cmd)) goto error;
261 
262  // Read status and check configuration
263  count = TWI::Device::read(&status, sizeof(status));
264  if (!TWI::Device::release()) return (false);
265  return ((count == sizeof(status)) && !status.RST);
266 
267  error:
269  return (false);
270  }
271 
275  enum Register {
280  } __attribute__((packed));
281 
289  bool set_read_pointer(Register addr, uint8_t& value)
290  {
291  uint8_t cmd[2];
292  int count;
293  bool res = false;
294 
295  // Issue set read pointer command with given pointer
296  cmd[0] = SET_READ_POINTER;
297  cmd[1] = (uint8_t) addr;
298  if (!TWI::Device::acquire()) return (false);
299  count = TWI::Device::write(cmd, sizeof(cmd));
300  if (count != sizeof(cmd)) goto error;
301 
302  // Read register value
303  count = TWI::Device::read(&value, sizeof(value));
304  res = (count == sizeof(value));
305 
306  error:
307  if (!TWI::Device::release()) return (false);
308  return (res);
309  }
310 
317  bool channel_select(uint8_t chan)
318  {
319  uint8_t cmd[2];
320  int count;
321 
322  // Check channel number
323  if (chan > 7) return (false);
324 
325  // Issue channel select command with channel code
326  cmd[0] = CHANNEL_SELECT;
327  cmd[1] = (~chan << 4) | chan;
328  if (!TWI::Device::acquire()) return (false);
329  count = TWI::Device::write(cmd, sizeof(cmd));
330  if (!TWI::Device::release()) return (false);
331  return (count == sizeof(cmd));
332  }
333 
334 protected:
338  enum {
339  DEVICE_RESET = 0xf0,
342  CHANNEL_SELECT = 0xc3,
343  ONE_WIRE_RESET = 0xb4,
348  } __attribute__((packed));
349 
353  union status_t {
354  uint8_t as_uint8;
355  struct {
356  uint8_t IWB:1;
357  uint8_t PPD:1;
358  uint8_t SD:1;
359  uint8_t LL:1;
360  uint8_t RST:1;
361  uint8_t SBR:1;
362  uint8_t TSB:1;
363  uint8_t DIR:1;
364  };
365  operator uint8_t()
366  {
367  return (as_uint8);
368  }
369  };
370 
374  union config_t {
375  uint8_t as_uint8;
376  struct {
377  uint8_t APU:1;
378  uint8_t ZERO:1;
379  uint8_t SPU:1;
380  uint8_t IWS:1;
381  uint8_t COMP:4;
382  };
383  operator uint8_t()
384  {
385  return (as_uint8);
386  }
388  {
389  as_uint8 = 0;
390  }
391  };
392 
394  static const int POLL_MAX = 20;
395 
402  bool one_wire_await(status_t& status)
403  {
404  // Wait for one wire operation to complete
405  for (int i = 0; i < POLL_MAX; i++) {
406  int count = TWI::Device::read(&status, sizeof(status));
407  if (count == sizeof(status) && !status.IWB) return (true);
408  }
409  return (false);
410  }
411 
412 };
413 #endif
bool write_configuration(bool apu=true, bool spu=false, bool iws=false)
Definition: DS2482.h:242
Definition: TWI.h:28
bool one_wire_reset()
Definition: DS2482.h:45
bool one_wire_await(status_t &status)
Definition: DS2482.h:402
Channel Select.
Definition: DS2482.h:342
uint8_t TSB
Triplet Second Bit.
Definition: DS2482.h:362
uint8_t DIR
Branch Direction Taken.
Definition: DS2482.h:363
bool one_wire_write_bit(bool value)
Definition: DS2482.h:99
bool set_read_pointer(Register addr, uint8_t &value)
Definition: DS2482.h:289
uint8_t RST
Device Reset.
Definition: DS2482.h:360
bool release()
Definition: TWI.h:62
uint8_t APU
< Bitfield access (little endian).
Definition: DS2482.h:377
uint8_t as_uint8
Unsigned byte access.
Definition: DS2482.h:354
1-Wire Triplet.
Definition: DS2482.h:347
1-Wire Write Byte.
Definition: DS2482.h:345
uint8_t as_uint8
Unsigned byte access.
Definition: DS2482.h:375
DS2482(TWI &twi, uint8_t subaddr=0)
Definition: DS2482.h:35
bool device_reset()
Definition: DS2482.h:212
Definition: DS2482.h:28
bool one_wire_write_byte(uint8_t value)
Definition: DS2482.h:155
Hardware::TWI twi(100000UL)
uint8_t IWS
1-Wire Speed.
Definition: DS2482.h:380
bool channel_select(uint8_t chan)
Definition: DS2482.h:317
uint8_t IWB
< Bitfield access (little endian).
Definition: DS2482.h:356
1-Wire Read Byte.
Definition: DS2482.h:346
1-Wire Single Bit.
Definition: DS2482.h:344
Device Reset.
Definition: DS2482.h:339
uint8_t SPU
Strong Pullup.
Definition: DS2482.h:379
static const int POLL_MAX
Definition: DS2482.h:394
uint8_t SD
Short Detected.
Definition: DS2482.h:358
uint8_t PPD
Presence-Pulse Detect.
Definition: DS2482.h:357
Register
Definition: DS2482.h:275
bool one_wire_read_bit(bool &value)
Definition: DS2482.h:70
1-Wire Reset.
Definition: DS2482.h:343
uint8_t LL
Logic Level.
Definition: DS2482.h:359
uint8_t COMP
Complement of lower 4-bits.
Definition: DS2482.h:381
int write(const void *buf, size_t count)
Definition: TWI.h:84
Device(TWI &twi, uint8_t addr)
Definition: TWI.h:41
Set Read Pointer.
Definition: DS2482.h:340
int8_t one_wire_triplet(uint8_t &dir)
Definition: DS2482.h:183
bool acquire()
Definition: TWI.h:52
Write Configuration.
Definition: DS2482.h:341
int read(void *buf, size_t count)
Definition: TWI.h:73
uint8_t SBR
Single Bit Result.
Definition: DS2482.h:361
virtual bool one_wire_read_byte(uint8_t &value)
Definition: DS2482.h:125