/
OWIHighLevelFunctions.c
419 lines (381 loc) · 11 KB
/
OWIHighLevelFunctions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// This file has been prepared for Doxygen automatic documentation generation.
/*! \file ********************************************************************
*
* Atmel Corporation
*
* \li File: OWIHighLevelFunctions.c
* \li Compiler: IAR EWAAVR 3.20a
* \li Support mail: avr@atmel.com
*
* \li Supported devices: All AVRs.
*
* \li Application Note: AVR318 - Dallas 1-Wire(R) master.
*
*
* \li Description: High level functions for transmission of full bytes
* on the 1-Wire(R) bus and implementations of ROM
* commands.
*
* $Revision: 1.4 $
* $Date: 2011-12-05 15:43:06 $
****************************************************************************/
#include <avr/io.h>
#include "OWIHighLevelFunctions.h"
#include "OWIBitFunctions.h"
#include "OWIPolled.h"
#include <string.h>
#include <stdio.h>
/*! \brief Sends one byte of data on the 1-Wire(R) bus(es).
*
* This function automates the task of sending a complete byte
* of data on the 1-Wire bus(es).
*
* \param data The data to send on the bus(es).
*
* \param pins A bitmask of the buses to send the data to.
*/
void OWI_SendByte( unsigned char data, unsigned char pins )
{
unsigned char temp;
unsigned char i;
// Do once for each bit
for ( i = 0; i < 8 ; i++ )
{
// Determine if lsb is '0' or '1' and transmit corresponding
// waveform on the bus.
temp = data & 0x01;
if ( temp )
{
OWI_WriteBit1(pins);
}
else
{
OWI_WriteBit0(pins);
}
// Right shift the data to get next bit.
data >>= 1;
}
}
/*! \brief Sends one byte of data on the 1-Wire(R) bus(es).
*
* This function automates the task of sending a complete word (2 byte)
* starting from lsb
* of data on the 1-Wire bus(es).
*
* \param data The data word to send on the bus(es).
*
* \param pins A bitmask of the buses to send the data to.
*/
void OWI_SendWord( uint16_t data, unsigned char pins )
{
uint16_t temp;
unsigned char i;
// Do once for each bit
for ( i = 0; i < 16 ; i++ )
{
// Determine if lsb is '0' or '1' and transmit corresponding
// waveform on the bus.
temp = data & 0x0001;
if ( temp )
{
OWI_WriteBit1(pins);
}
else
{
OWI_WriteBit0(pins);
}
// Right shift the data to get next bit.
data >>= 1;
}
}
/*! \brief Receives one word (2 bytes) of data from the 1-Wire(R) bus.
*
* This function automates the task of receiving a complete word (2 bytes)
* of data from the 1-Wire bus.
*
* \param pin A bitmask of the bus to read from.
*
* \return The word read from the bus.
*/
uint16_t OWI_ReceiveWord( unsigned char pin )
{
uint16_t data;
unsigned char i;
// Clear the temporary input variable.
data = 0x0000;
// Do once for each bit
for ( i = 0; i < 16 ; i++ )
{
// Shift temporary input variable right.
data >>= 1;
// Set the msb if a '1' value is read from the bus.
// Leave as it is ('0') else.
if ( OWI_ReadBit(pin) )
{
// Set msb
data |= 0x8000;
}
}
return data;
}
/*! \brief Receives one byte of data from the 1-Wire(R) bus.
*
* This function automates the task of receiving a complete byte
* of data from the 1-Wire bus.
*
* \param pin A bitmask of the bus to read from.
*
* \return The byte read from the bus.
*/
unsigned char OWI_ReceiveByte( unsigned char pin )
{
unsigned char data;
unsigned char i;
// Clear the temporary input variable.
data = 0x00;
// Do once for each bit
for ( i = 0; i < 8 ; i++ )
{
// Shift temporary input variable right.
data >>= 1;
// Set the msb if a '1' value is read from the bus.
// Leave as it is ('0') else.
if ( OWI_ReadBit(pin) )
{
// Set msb
data |= 0x80;
}
}
return data;
}
/*! \brief Sends the SKIP ROM command to the 1-Wire bus(es).
*
* \param pins A bitmask of the buses to send the SKIP ROM command to.
*/
void OWI_SkipRom( unsigned char pins )
{
// Send the SKIP ROM command on the bus.
OWI_SendByte(OWI_ROM_SKIP, pins);
}
/*! \brief Sends the READ ROM command and reads back the ROM id.
*
* \param romValue A pointer where the id will be placed.
*
* \param pin A bitmask of the bus to read from.
*/
void OWI_ReadRom( unsigned char * romValue, unsigned char pin )
{
unsigned char bytesLeft = 8;
// Send the READ ROM command on the bus.
OWI_SendByte(OWI_ROM_READ, pin);
// Do 8 times.
while ( bytesLeft > 0 )
{
// Place the received data in memory.
*romValue++ = OWI_ReceiveByte(pin);
bytesLeft--;
}
}
/*! \brief Sends the MATCH ROM command and the ROM id to match against.
*
* \param romValue A pointer to the ID to match against.
*
* \param pins A bitmask of the buses to perform the MATCH ROM command on.
*/
void OWI_MatchRom( unsigned char * romValue, unsigned char pins )
{
unsigned char bytesLeft = 8;
// Send the MATCH ROM command.
OWI_SendByte(OWI_ROM_MATCH, pins);
// Do once for each byte.
while ( bytesLeft > 0 )
{
// Transmit 1 byte of the ID to match.
OWI_SendByte(*romValue++, pins);
bytesLeft--;
}
}
/*! \brief Sends the SEARCH ROM command and returns 1 id found on the
* 1-Wire(R) bus.
*
* \param bitPattern A pointer to an 8 byte char array where the
* discovered identifier will be placed. When
* searching for several slaves, a copy of the
* last found identifier should be supplied in
* the array, or the search will fail.
*
* \param lastDeviation The bit position where the algorithm made a
* choice the last time it was run. This argument
* should be 0 when a search is initiated. Supplying
* the return argument of this function when calling
* repeatedly will go through the complete slave
* search.
*
* \param pin A bit-mask of the bus to perform a ROM search on.
*
* \return The last bit position where there was a discrepancy between slave addresses the last time this function was run. Returns OWI_ROM_SEARCH_FAILED if an error was detected (e.g. a device was connected to the bus during the search), or OWI_ROM_SEARCH_FINISHED when there are no more devices to be discovered.
*
* \note See main.c for an example of how to utilize this function.
*/
unsigned char OWI_SearchRom( unsigned char * bitPattern, unsigned char lastDeviation, unsigned char pin )
{
unsigned char currentBit = 1;
unsigned char newDeviation = 0;
unsigned char bitMask = 0x01;
unsigned char bitA;
unsigned char bitB;
// Send SEARCH ROM command on the bus.
OWI_SendByte(OWI_ROM_SEARCH, pin);
// Walk through all 64 bits.
while ( currentBit <= 64 )
{
// Read bit from bus twice.
bitA = OWI_ReadBit(pin);
bitB = OWI_ReadBit(pin);
if ( bitA && bitB )
{
// Both bits 1 (Error).
newDeviation = OWI_ROM_SEARCH_FAILED;
return newDeviation;
}
else if ( bitA ^ bitB )
{
// Bits A and B are different. All devices have the same bit here.
// Set the bit in bitPattern to this value.
if ( bitA )
{
( *bitPattern ) |= bitMask;
}
else
{
( *bitPattern ) &= ~bitMask;
}
}
else // Both bits 0
{
// If this is where a choice was made the last time,
// a '1' bit is selected this time.
if ( currentBit == lastDeviation )
{
( *bitPattern ) |= bitMask;
}
// For the rest of the id, '0' bits are selected when
// discrepancies occur.
else if ( currentBit > lastDeviation )
{
( *bitPattern ) &= ~bitMask;
newDeviation = currentBit;
}
// If current bit in bit pattern = 0, then this is
// out new deviation.
else if ( !( *bitPattern & bitMask ) )
{
newDeviation = currentBit;
}
// IF the bit is already 1, do nothing.
else
{
}
}
// Send the selected bit to the bus.
if ( ( *bitPattern ) & bitMask )
{
OWI_WriteBit1(pin);
}
else
{
OWI_WriteBit0(pin);
}
// Increment current bit.
currentBit++;
// Adjust bitMask and bitPattern pointer.
bitMask <<= 1;
if ( !bitMask )
{
bitMask = 0x01;
bitPattern++;
}
}
return newDeviation;
}
unsigned char OWI_SearchAlarm( unsigned char * bitPattern, unsigned char lastDeviation, unsigned char pin )
{
unsigned char currentBit = 1;
unsigned char newDeviation = 0;
unsigned char bitMask = 0x01;
unsigned char bitA;
unsigned char bitB;
// Send SEARCH ROM command on the bus.
OWI_SendByte(OWI_ROM_ALARM, pin);
// Walk through all 64 bits.
while ( currentBit <= 64 )
{
// Read bit from bus twice.
bitA = OWI_ReadBit(pin);
bitB = OWI_ReadBit(pin);
if ( bitA && bitB )
{
// Both bits 1 (Error).
newDeviation = OWI_ROM_SEARCH_FAILED;
return newDeviation;
}
else if ( bitA ^ bitB )
{
// Bits A and B are different. All devices have the same bit here.
// Set the bit in bitPattern to this value.
if ( bitA )
{
( *bitPattern ) |= bitMask;
}
else
{
( *bitPattern ) &= ~bitMask;
}
}
else // Both bits 0
{
// If this is where a choice was made the last time,
// a '1' bit is selected this time.
if ( currentBit == lastDeviation )
{
( *bitPattern ) |= bitMask;
}
// For the rest of the id, '0' bits are selected when
// discrepancies occur.
else if ( currentBit > lastDeviation )
{
( *bitPattern ) &= ~bitMask;
newDeviation = currentBit;
}
// If current bit in bit pattern = 0, then this is
// out new deviation.
else if ( !( *bitPattern & bitMask ) )
{
newDeviation = currentBit;
}
// IF the bit is already 1, do nothing.
else
{
}
}
// Send the selected bit to the bus.
if ( ( *bitPattern ) & bitMask )
{
OWI_WriteBit1(pin);
}
else
{
OWI_WriteBit0(pin);
}
// Increment current bit.
currentBit++;
// Adjust bitMask and bitPattern pointer.
bitMask <<= 1;
if ( !bitMask )
{
bitMask = 0x01;
bitPattern++;
}
}
return newDeviation;
}