COSA
An Object-Oriented Platform for Arduino Programming
DS2482.cpp
Go to the documentation of this file.
1 
21 #include "DS2482.hh"
22 
23 bool
25 {
26  status_t status;
27  uint8_t cmd;
28  int count;
29 
30  // Issue device reset command
31  cmd = DEVICE_RESET;
32  twi.acquire(this);
33  count = twi.write(cmd);
34  if (count != sizeof(cmd)) goto error;
35 
36  // Check status register for device reset
37  count = twi.read(&status, sizeof(status));
38  twi.release();
39  return (count == sizeof(status) && status.RST);
40 
41  error:
42  twi.release();
43  return (false);
44 }
45 
46 bool
47 DS2482::device_config(bool apu, bool spu, bool iws)
48 {
49  config_t config;
50  status_t status;
51  uint8_t cmd[2];
52  int count;
53 
54  // Set configuration bit-fields
55  config.APU = apu;
56  config.SPU = spu;
57  config.IWS = iws;
58  config.COMP = ~config;
59 
60  // Issue write configuration command with given setting
61  cmd[0] = WRITE_CONGIFURATION;
62  cmd[1] = config;
63  twi.acquire(this);
64  count = twi.write(cmd, sizeof(cmd));
65  if (count != sizeof(cmd)) goto error;
66 
67  // Read status and check configuration
68  count = twi.read(&status, sizeof(status));
69  twi.release();
70  return (count == sizeof(status) && !status.RST);
71 
72  error:
73  twi.release();
74  return (false);
75 }
76 
77 int
79 {
80  uint8_t cmd[2];
81  uint8_t reg;
82  int count;
83 
84  // Issue set read pointer command with given pointer
85  cmd[0] = SET_READ_POINTER;
86  cmd[1] = (uint8_t) addr;
87  twi.acquire(this);
88  count = twi.write(cmd, sizeof(cmd));
89  if (count != sizeof(cmd)) goto error;
90 
91  // Read register value
92  count = twi.read(&reg, sizeof(reg));
93  twi.release();
94  return ((count == sizeof(reg)) ? reg : -1);
95 
96  error:
97  twi.release();
98  return (false);
99 }
100 
101 bool
103 {
104  uint8_t cmd[2];
105  int count;
106 
107  // Check channel number
108  if (chan > 7) return (false);
109 
110  // Issue channel select command with channel code
111  cmd[0] = CHANNEL_SELECT;
112  cmd[1] = (~chan << 4) | chan;
113  twi.acquire(this);
114  count = twi.write(cmd, sizeof(cmd));
115  twi.release();
116  return (count == sizeof(cmd));
117 }
118 
119 bool
121 {
122  status_t status;
123  uint8_t cmd;
124  int count;
125 
126  // Issue one wire reset command
127  cmd = ONE_WIRE_RESET;
128  twi.acquire(this);
129  count = twi.write(cmd);
130  if (count != sizeof(cmd)) goto error;
131 
132  // Wait for one wire operation to complete
133  for (int i = 0; i < POLL_MAX; i++) {
134  count = twi.read(&status, sizeof(status));
135  if (count == sizeof(status) && !status.IWB) break;
136  }
137  twi.release();
138  return (count == sizeof(status) && status.PPD);
139 
140  error:
141  twi.release();
142  return (false);
143 }
144 
145 int
147 {
148  status_t status;
149  uint8_t cmd[2];
150  int count;
151 
152  // Issue one wire single bit command with read data time slot
153  cmd[0] = ONE_WIRE_SINGLE_BIT;
154  cmd[1] = 0x80;
155  twi.acquire(this);
156  count = twi.write(cmd, sizeof(cmd));
157  if (count != sizeof(cmd)) goto error;
158 
159  // Wait for one wire operation to complete
160  for (int i = 0; i < POLL_MAX; i++) {
161  count = twi.read(&status, sizeof(status));
162  if (count == sizeof(status) && !status.IWB) break;
163  }
164  twi.release();
165  if (count != sizeof(status) || status.IWB) return (-1);
166  return (status.SBR);
167 
168  error:
169  twi.release();
170  return (-1);
171 }
172 
173 bool
175 {
176  status_t status;
177  uint8_t cmd[2];
178  int count;
179 
180  // Issue one wire single bit command with given data
181  cmd[0] = ONE_WIRE_SINGLE_BIT;
182  cmd[1] = (value ? 0x80 : 0x00);
183  twi.acquire(this);
184  count = twi.write(cmd, sizeof(cmd));
185  if (count != sizeof(cmd)) goto error;
186 
187  // Wait for one wire operation to complete
188  for (int i = 0; i < POLL_MAX; i++) {
189  count = twi.read(&status, sizeof(status));
190  if (count == sizeof(status) && !status.IWB) break;
191  }
192  twi.release();
193  return (count == sizeof(status) && !status.IWB);
194 
195  error:
196  twi.release();
197  return (false);
198 }
199 
200 bool
202 {
203  status_t status;
204  uint8_t cmd[2];
205  int count;
206 
207  // Issue one wire write byte command with given data
208  cmd[0] = ONE_WIRE_WRITE_BYTE;
209  cmd[1] = value;
210  twi.acquire(this);
211  count = twi.write(cmd, sizeof(cmd));
212  if (count != sizeof(cmd)) goto error;
213 
214  // Wait for one wire operation to complete
215  for (int i = 0; i < POLL_MAX; i++) {
216  count = twi.read(&status, sizeof(status));
217  if (count == sizeof(status) && !status.IWB) break;
218  }
219  twi.release();
220  return (count == sizeof(status) && !status.IWB);
221 
222  error:
223  twi.release();
224  return (false);
225 }
226 
227 int
229 {
230  status_t status;
231  uint8_t cmd;
232  int count;
233 
234  // Issue one wire read byte command
235  cmd = ONE_WIRE_READ_BYTE;
236  twi.acquire(this);
237  count = twi.write(cmd);
238  if (count != sizeof(cmd)) goto error;
239 
240  // Wait for one wire operation to complete
241  for (int i = 0; i < POLL_MAX; i++) {
242  count = twi.read(&status, sizeof(status));
243  if (count == sizeof(status) && !status.IWB) break;
244  }
245  twi.release();
246  if (count != sizeof(status) || status.IWB) return (false);
247 
248  // Read data register value
250 
251  error:
252  twi.release();
253  return (false);
254 }
255 
256 int
258 {
259  status_t status;
260  uint8_t cmd[2];
261  int count;
262 
263  // Issue one wire single bit command with given data
264  cmd[0] = ONE_WIRE_TRIPLET;
265  cmd[1] = (direction ? 0x80 : 0x00);
266  twi.acquire(this);
267  count = twi.write(cmd, sizeof(cmd));
268  if (count != sizeof(cmd)) goto error;
269 
270  // Wait for one wire operation to complete
271  for (int i = 0; i < POLL_MAX; i++) {
272  count = twi.read(&status, sizeof(status));
273  if (count == sizeof(status) && !status.IWB) break;
274  }
275  twi.release();
276  if (count != sizeof(status) && status.IWB) return (-1);
277 
278  // Return (DIR, NID, ID)
279  return (status >> 5);
280 
281  error:
282  twi.release();
283  return (-1);
284 }
285 
TWI twi
Definition: TWI.cpp:27
bool one_wire_reset()
Definition: DS2482.cpp:120
Channel Select.
Definition: DS2482.hh:139
Set Read Pointer.
Definition: DS2482.hh:137
bool device_config(bool apu=true, bool spu=false, bool iws=false)
Definition: DS2482.cpp:47
bool one_wire_write_bit(bool value)
Definition: DS2482.cpp:174
uint8_t RST
Device Reset.
Definition: DS2482.hh:157
uint8_t APU
< Bitfield access (little endian).
Definition: DS2482.hh:174
int set_read_pointer(Register addr)
Definition: DS2482.cpp:78
1-Wire Write Byte.
Definition: DS2482.hh:142
bool device_reset()
Definition: DS2482.cpp:24
1-Wire Single Bit.
Definition: DS2482.hh:141
bool one_wire_write_byte(uint8_t value)
Definition: DS2482.cpp:201
void release()
Definition: TWI.cpp:58
uint8_t IWS
1-Wire Speed.
Definition: DS2482.hh:177
bool channel_select(uint8_t chan)
Definition: DS2482.cpp:102
int read(void *buf, size_t size)
Definition: TWI.hh:326
uint8_t IWB
< Bitfield access (little endian).
Definition: DS2482.hh:153
uint8_t SPU
Strong Pullup.
Definition: DS2482.hh:176
int one_wire_triplet(bool direction=false)
Definition: DS2482.cpp:257
int write(void *buf, size_t size)
Definition: TWI.hh:282
static const int POLL_MAX
Definition: DS2482.hh:191
int one_wire_read_byte()
Definition: DS2482.cpp:228
uint8_t PPD
Presence-Pulse Detect.
Definition: DS2482.hh:154
int one_wire_read_bit()
Definition: DS2482.cpp:146
Register
Definition: DS2482.hh:62
void acquire(TWI::Driver *dev)
Definition: TWI.cpp:36
uint8_t COMP
Complement of lower 4-bits.
Definition: DS2482.hh:178
1-Wire Read Byte.
Definition: DS2482.hh:143
Device Reset.
Definition: DS2482.hh:136
1-Wire Reset.
Definition: DS2482.hh:140
uint8_t SBR
Single Bit Result.
Definition: DS2482.hh:158
1-Wire Triplet.
Definition: DS2482.hh:144
Write Configuration.
Definition: DS2482.hh:138