summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/w1.c
blob: 99b92d1a88947c82a0a0fb9f7fafbdfb86f44dc3 (plain)
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
/**************************************************************************//**
  \file  w1.c

  \brief The implementation of the 1-wire interface.

  \author
      Atmel Corporation: http://www.atmel.com \n
      Support email: avr@atmel.com

    Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
    Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
    History:
      10/12/07 A. Khromykh - Created
 ******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/

/******************************************************************************
                   Includes section
******************************************************************************/
#include <halW1.h>
#include <w1.h>
#include <atomic.h>

/******************************************************************************
                   Define(s) section
******************************************************************************/
#define HAL_ROM_BYTE_LEN 8
#define HAL_ROM_BIT_LEN 64
#define HAL_W1_CRC_POLINOM 0x8C
#define HAL_SEARCH_ROM_CMD 0xF0
#define HAL_SEARCH_ALRM_CMD 0xEC

/******************************************************************************
                   Prototypes section
******************************************************************************/
/*******************************************************************************
 1-Wire search procedure for all devices discovering
 Parameters:
   cmd      - ROM function command.
   family   - 8-bit family code.
   data     - pointer of SRAM where are stored the 8 bytes ROM
              codes returned by the devices.
   count    - number of devices wish to find.
   actCount - number of devices have been found.
 Returns:
   W1_SUCCESS_STATUS   - if at least one device has been found.
   W1_NO_DEVICE_STATUS - if there are no any devices presented
                         on the bus with specified family code.
   W1_INVALID_CRC      - if during searching invalid CRC has
                         been read and no devices with
                         spicified family code has been found.
*******************************************************************************/
W1Status_t halCommSearchW1(uint8_t cmd,
                           uint8_t family,
                           uint8_t *data,
                           uint8_t count,
                           uint8_t *actCount);

/*******************************************************************************
 1-Wire search procedure for one device discovering
 Parameters:
   cmd - ROM function command.
 Returns:
   W1_SUCCESS_STATUS   - if device has been found, ROM number
                         in ROM_NO buffer.
   W1_NO_DEVICE_STATUS - if device has not been found.
   W1_INVALID_CRC      - if during searching invalid CRC has
                         been read.
*******************************************************************************/
W1Status_t halSearchW1(uint8_t cmd);

/*******************************************************************************
 Calculating 1-Wire 8-bit CRC
 Parameters:
   data   - data buffer pointer.
   length - data length.
 Returns:
   CRC value based on polynomial x^8 + x^5 + x^4 + 1
*******************************************************************************/
uint8_t halW1CRC(uint8_t *data, uint8_t length);

/******************************************************************************
                   Global variables section
******************************************************************************/
// 8-byte buffer that contains the current ROM registration
// number discovered
static uint8_t ROM_NO[HAL_ROM_BYTE_LEN];
// Bit index that identifies from which bit the next search
// discrepancy check should start
static uint8_t LastDiscrepancy;
// Flag to indicate previos search was the last device
static uint8_t LastDeviceFlag;

/******************************************************************************
                   Implementations section
******************************************************************************/
/*******************************************************************************
 1-Wire search procedure with search ROM command only
 Parameters:
   family   - 8-bit family code.
   data     - pointer of SRAM where are stored the 8 bytes ROM
              codes returned by the devices.
   count    - number of devices wish to find.
   actCount - number of devices have been found.
 Returns:
   W1_SUCCESS_STATUS   - if at least one device has been found.
   W1_NO_DEVICE_STATUS - if there are no any devices presented
                         on the bus with specified family code.
   W1_INVALID_CRC      - if during searching invalid CRC has
                         been read and no devices with
                         spicified family code has been found.
*******************************************************************************/
W1Status_t HAL_SearchW1Device(uint8_t family,
                              uint8_t *data,
                              uint8_t count,
                              uint8_t *actCount)
{
  return halCommSearchW1(HAL_SEARCH_ROM_CMD, family, data, count, actCount);
}

/*******************************************************************************
 1-Wire search procedure with alarm search command only
 Parameters:
   family   - 8-bit family code.
   data     - pointer of SRAM where are stored the 8 bytes ROM
              codes returned by the devices.
   count    - number of devices wish to find.
   actCount - number of devices have been found.
 Returns:
   W1_SUCCESS_STATUS   - if at least one device has been found.
   W1_NO_DEVICE_STATUS - if there are no any devices presented
                         on the bus with specified family code.
   W1_INVALID_CRC      - if during searching invalid CRC has
                         been read and no devices with
                         spicified family code has been found.
*******************************************************************************/
W1Status_t HAL_AlarmSearchW1Device(uint8_t family,
                                   uint8_t *data,
                                   uint8_t count,
                                   uint8_t *actCount)
{
  return halCommSearchW1(HAL_SEARCH_ALRM_CMD, family, data, count, actCount);
}

/*******************************************************************************
 1-Wire search procedure for all devices discovering
 Parameters:
   cmd      - ROM function command.
   family   - 8-bit family code.
   data     - pointer of SRAM where are stored the 8 bytes ROM
              codes returned by the devices.
   count    - number of devices wish to find.
   actCount - number of devices have been found.
 Returns:
   W1_SUCCESS_STATUS   - if at least one device has been found.
   W1_NO_DEVICE_STATUS - if there are no any devices presented
                         on the bus with specified family code.
   W1_INVALID_CRC      - if during searching invalid CRC has
                         been read and no devices with
                         spicified family code has been found.
*******************************************************************************/
W1Status_t halCommSearchW1(uint8_t cmd,
                       uint8_t family,
                       uint8_t *data,
                       uint8_t count,
                       uint8_t *actCount)
{
  W1Status_t w1_result;
  uint8_t i;

  // Reset the searh state
  LastDiscrepancy = 0;
  LastDeviceFlag = 0;
  // Setup family code
  if (W1_ANY_FAMILY != family)
  {
    ROM_NO[0] = family;
    LastDiscrepancy = HAL_ROM_BIT_LEN + 1;
    for (i = 1; i < HAL_ROM_BYTE_LEN; i++) ROM_NO[i] = 0;
  }
  i = 0;
  // Discovering
  do
  {
    w1_result = halSearchW1(cmd);
    if (W1_SUCCESS_STATUS != w1_result)
      break;
    // There is some device with specified family code
    if ((ROM_NO[0] == family) || (W1_ANY_FAMILY == family))
    {
      *(uint64_t *)(data + 8 * i) = *(uint64_t *)ROM_NO;
      i++;
    }
    else // There are not any devices with specified family code
    {
      w1_result = W1_NO_DEVICE_STATUS;
      break;
    }
  }
  while (!LastDeviceFlag && (i < count));

  if (NULL != actCount)
    *actCount = i;

  if (i != 0)
    return W1_SUCCESS_STATUS;
  else
    return w1_result;
}

/*******************************************************************************
 1-Wire search procedure for one device discovering
 Parameters:
   cmd - ROM function command.
 Returns:
   W1_SUCCESS_STATUS   - if device has been found, ROM number
                         in ROM_NO buffer.
   W1_NO_DEVICE_STATUS - if device has not been found.
   W1_INVALID_CRC      - if during searching invalid CRC has
                         been read.
*******************************************************************************/
W1Status_t halSearchW1(uint8_t cmd)
{
  uint8_t id_bit_number;
  uint8_t last_zero, rom_byte_number;
  uint8_t id_bit, cmp_id_bit;
  uint8_t rom_byte_mask, search_direction;

  // initialize for search
  id_bit_number = 1;
  last_zero = 0;
  rom_byte_number = 0;
  rom_byte_mask = 1;

  // 1-Wire reset
  ATOMIC_SECTION_ENTER
    id_bit = halResetW1();
  ATOMIC_SECTION_LEAVE
  if (W1_NO_DEVICE_STATUS == id_bit)
  {
    LastDiscrepancy = 0;
    LastDeviceFlag = 0;
    return W1_NO_DEVICE_STATUS;
  }

  // issue the search command
  ATOMIC_SECTION_ENTER
    halWriteW1(cmd);
  ATOMIC_SECTION_LEAVE
  // search 64-bit uniqued registration number
  do
  {
    // read a bit and its complement
    ATOMIC_SECTION_ENTER
      id_bit = halReadW1Bit();
      cmp_id_bit = halReadW1Bit();
    ATOMIC_SECTION_LEAVE
    // check for no devices on 1-wire
    if ((1 == id_bit) && (1 == cmp_id_bit))
      return W1_NO_DEVICE_STATUS;

    // all devices coupled have 0 or 1
    if (id_bit != cmp_id_bit)
    {
      search_direction = id_bit;
      // there is descepancy
    }
    else
    {
      // if this discrepancy if before the Last Discrepancy
      // on a previous next then pick the same as last time (old branch)
      if (id_bit_number < LastDiscrepancy)
        search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
      // if equal to last pick 1, if not then pick 0 (new branch)
      else
        search_direction = (id_bit_number == LastDiscrepancy);
      // if 0 was picked then record its position in LastZero
      if (0 == search_direction)
        last_zero = id_bit_number;
    }

    if (1 == search_direction)
      ROM_NO[rom_byte_number] |= rom_byte_mask;
    else
      ROM_NO[rom_byte_number] &= ~rom_byte_mask;
    ATOMIC_SECTION_ENTER
      halWriteW1bit(search_direction);
    ATOMIC_SECTION_LEAVE
    id_bit_number++;
    rom_byte_mask <<= 1;

    if (rom_byte_mask == 0)
    {
      rom_byte_number++;
      rom_byte_mask = 1;
    }
  } while (rom_byte_number < HAL_ROM_BYTE_LEN);

  // Invalid CRC
  if (halW1CRC(ROM_NO, HAL_ROM_BYTE_LEN))
    return W1_INVALID_CRC;

  LastDiscrepancy = last_zero;
  // check for last device
  if (0 == LastDiscrepancy)
    LastDeviceFlag = 1;

  return W1_SUCCESS_STATUS;
}

/*******************************************************************************
 Calculating 1-Wire 8-bit CRC
 Parameters:
   data   - data buffer pointer.
   length - data length.
 Returns:
   CRC value based on polynomial x^8 + x^5 + x^4 + 1
*******************************************************************************/
uint8_t halW1CRC(uint8_t *data, uint8_t length)
{
  uint8_t reg;
  uint8_t b;

  for (reg = 0; length > 0; length--, data++)
  {
    for (b = 0; b < 8; b++)
    {
      if (((*data >> b) & 1) ^ (reg & 1))
      {
        reg >>= 1;
        reg ^= HAL_W1_CRC_POLINOM;
      }
      else
        reg >>= 1;
    }
  }
  return reg;
}

/*******************************************************************************
Resets all devices connected to the bus.
Parameters:
  none.
Returns:
  0 - there are some devices at the bus.
  1 - there are not any devices at the bus.
*******************************************************************************/
uint8_t HAL_ResetW1(void)
{
  return halResetW1();
};

/*******************************************************************************
Writes byte to the bus
Parameters:
    value - byte to write.
Returns:
    none.
*******************************************************************************/
void HAL_WriteW1(uint8_t value)
{
  halWriteW1(value);
}

/*******************************************************************************
Reads byte from the bus.
Parameters:
    none.
Returns:
  byte read from the bus.
*******************************************************************************/
uint8_t HAL_ReadW1(void)
{
  return halReadW1();
}

// eof halW1.c