-
Notifications
You must be signed in to change notification settings - Fork 52
/
owb.c
745 lines (645 loc) · 19.6 KB
/
owb.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
* MIT License
*
* Copyright (c) 2017 David Antliff
* Copyright (c) 2017 Chris Morgan <chmorgan@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* @file
*/
#include <stddef.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "rom/gpio.h" // for gpio_pad_select_gpio()
#include "owb.h"
#include "owb_gpio.h"
static const char * TAG = "owb";
static bool _is_init(const OneWireBus * bus)
{
bool ok = false;
if (bus != NULL)
{
if (bus->driver)
{
// OK
ok = true;
}
else
{
ESP_LOGE(TAG, "bus is not initialised");
}
}
else
{
ESP_LOGE(TAG, "bus is NULL");
}
return ok;
}
/**
* @brief 1-Wire 8-bit CRC lookup.
* @param[in] crc Starting CRC value. Pass in prior CRC to accumulate.
* @param[in] data Byte to feed into CRC.
* @return Resultant CRC value.
*/
static uint8_t _calc_crc(uint8_t crc, uint8_t data)
{
// https://www.maximintegrated.com/en/app-notes/index.mvp/id/27
static const uint8_t table[256] = {
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
};
return table[crc ^ data];
}
static uint8_t _calc_crc_block(uint8_t crc, const uint8_t * buffer, size_t len)
{
do
{
crc = _calc_crc(crc, *buffer++);
ESP_LOGD(TAG, "buffer 0x%02x, crc 0x%02x, len %d", (uint8_t)*(buffer - 1), (int)crc, (int)len);
}
while (--len > 0);
return crc;
}
/**
* @param[out] is_found true if a device was found, false if not
* @return status
*/
static owb_status _search(const OneWireBus * bus, OneWireBus_SearchState * state, bool * is_found)
{
// Based on https://www.maximintegrated.com/en/app-notes/index.mvp/id/187
// initialize for search
int id_bit_number = 1;
int last_zero = 0;
int rom_byte_number = 0;
uint8_t id_bit = 0;
uint8_t cmp_id_bit = 0;
uint8_t rom_byte_mask = 1;
uint8_t search_direction = 0;
bool search_result = false;
uint8_t crc8 = 0;
owb_status status = OWB_STATUS_NOT_SET;
// if the last call was not the last one
if (!state->last_device_flag)
{
// 1-Wire reset
bool is_present;
bus->driver->reset(bus, &is_present);
if (!is_present)
{
// reset the search
state->last_discrepancy = 0;
state->last_device_flag = false;
state->last_family_discrepancy = 0;
*is_found = false;
return OWB_STATUS_OK;
}
// issue the search command
bus->driver->write_bits(bus, OWB_ROM_SEARCH, 8);
// loop to do the search
do
{
id_bit = cmp_id_bit = 0;
// read a bit and its complement
bus->driver->read_bits(bus, &id_bit, 1);
bus->driver->read_bits(bus, &cmp_id_bit, 1);
// check for no devices on 1-wire (signal level is high in both bit reads)
if (id_bit && cmp_id_bit)
{
break;
}
else
{
// all devices coupled have 0 or 1
if (id_bit != cmp_id_bit)
{
search_direction = (id_bit) ? 1 : 0; // bit write value for search
}
else
{
// if this discrepancy if before the Last Discrepancy
// on a previous next then pick the same as last time
if (id_bit_number < state->last_discrepancy)
{
search_direction = ((state->rom_code.bytes[rom_byte_number] & rom_byte_mask) > 0);
}
else
{
// if equal to last pick 1, if not then pick 0
search_direction = (id_bit_number == state->last_discrepancy);
}
// if 0 was picked then record its position in LastZero
if (search_direction == 0)
{
last_zero = id_bit_number;
// check for Last discrepancy in family
if (last_zero < 9)
{
state->last_family_discrepancy = last_zero;
}
}
}
// set or clear the bit in the ROM byte rom_byte_number
// with mask rom_byte_mask
if (search_direction == 1)
{
state->rom_code.bytes[rom_byte_number] |= rom_byte_mask;
}
else
{
state->rom_code.bytes[rom_byte_number] &= ~rom_byte_mask;
}
// serial number search direction write bit
bus->driver->write_bits(bus, search_direction, 1);
// increment the byte counter id_bit_number
// and shift the mask rom_byte_mask
id_bit_number++;
rom_byte_mask <<= 1;
// if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
if (rom_byte_mask == 0)
{
crc8 = owb_crc8_byte(crc8, state->rom_code.bytes[rom_byte_number]); // accumulate the CRC
rom_byte_number++;
rom_byte_mask = 1;
}
}
}
while (rom_byte_number < 8); // loop until through all ROM bytes 0-7
// if the search was successful then
if (!((id_bit_number < 65) || (crc8 != 0)))
{
// search successful so set LastDiscrepancy,LastDeviceFlag,search_result
state->last_discrepancy = last_zero;
// check for last device
if (state->last_discrepancy == 0)
{
state->last_device_flag = true;
}
search_result = true;
}
}
// if no device found then reset counters so next 'search' will be like a first
if (!search_result || !state->rom_code.bytes[0])
{
state->last_discrepancy = 0;
state->last_device_flag = false;
state->last_family_discrepancy = 0;
search_result = false;
}
status = OWB_STATUS_OK;
*is_found = search_result;
return status;
}
// Public API
owb_status owb_uninitialize(OneWireBus * bus)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
bus->driver->uninitialize(bus);
status = OWB_STATUS_OK;
}
return status;
}
owb_status owb_use_crc(OneWireBus * bus, bool use_crc)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!bus)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
bus->use_crc = use_crc;
ESP_LOGD(TAG, "use_crc %d", bus->use_crc);
status = OWB_STATUS_OK;
}
return status;
}
owb_status owb_use_parasitic_power(OneWireBus * bus, bool use_parasitic_power)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!bus)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
bus->use_parasitic_power = use_parasitic_power;
ESP_LOGD(TAG, "use_parasitic_power %d", bus->use_parasitic_power);
status = OWB_STATUS_OK;
}
return status;
}
owb_status owb_use_strong_pullup_gpio(OneWireBus * bus, gpio_num_t gpio)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!bus)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
if (gpio != GPIO_NUM_NC) {
// The strong GPIO pull-up is only activated if parasitic-power mode is enabled
if (!bus->use_parasitic_power) {
ESP_LOGW(TAG, "Strong pull-up GPIO set with parasitic-power disabled");
}
gpio_pad_select_gpio(gpio);
gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
}
else
{
gpio_reset_pin(bus->strong_pullup_gpio);
}
bus->strong_pullup_gpio = gpio;
ESP_LOGD(TAG, "use_strong_pullup_gpio %d", bus->strong_pullup_gpio);
status = OWB_STATUS_OK;
}
return status;
}
owb_status owb_read_rom(const OneWireBus * bus, OneWireBus_ROMCode *rom_code)
{
owb_status status = OWB_STATUS_NOT_SET;
memset(rom_code, 0, sizeof(OneWireBus_ROMCode));
if (!bus || !rom_code)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
bool is_present;
bus->driver->reset(bus, &is_present);
if (is_present)
{
uint8_t value = OWB_ROM_READ;
bus->driver->write_bits(bus, value, 8);
owb_read_bytes(bus, rom_code->bytes, sizeof(OneWireBus_ROMCode));
if (bus->use_crc)
{
if (owb_crc8_bytes(0, rom_code->bytes, sizeof(OneWireBus_ROMCode)) != 0)
{
ESP_LOGE(TAG, "CRC failed");
memset(rom_code->bytes, 0, sizeof(OneWireBus_ROMCode));
status = OWB_STATUS_CRC_FAILED;
}
else
{
status = OWB_STATUS_OK;
}
}
else
{
status = OWB_STATUS_OK;
}
char rom_code_s[OWB_ROM_CODE_STRING_LENGTH];
owb_string_from_rom_code(*rom_code, rom_code_s, sizeof(rom_code_s));
ESP_LOGD(TAG, "rom_code %s", rom_code_s);
}
else
{
status = OWB_STATUS_DEVICE_NOT_RESPONDING;
ESP_LOGE(TAG, "ds18b20 device not responding");
}
}
return status;
}
owb_status owb_verify_rom(const OneWireBus * bus, OneWireBus_ROMCode rom_code, bool * is_present)
{
owb_status status = OWB_STATUS_NOT_SET;
bool result = false;
if (!bus || !is_present)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
OneWireBus_SearchState state = {
.rom_code = rom_code,
.last_discrepancy = 64,
.last_device_flag = false,
};
bool is_found = false;
_search(bus, &state, &is_found);
if (is_found)
{
result = true;
for (int i = 0; i < sizeof(state.rom_code.bytes) && result; ++i)
{
result = rom_code.bytes[i] == state.rom_code.bytes[i];
ESP_LOGD(TAG, "%02x %02x", rom_code.bytes[i], state.rom_code.bytes[i]);
}
is_found = result;
}
ESP_LOGD(TAG, "state.last_discrepancy %d, state.last_device_flag %d, is_found %d",
state.last_discrepancy, state.last_device_flag, is_found);
ESP_LOGD(TAG, "rom code %sfound", result ? "" : "not ");
*is_present = result;
status = OWB_STATUS_OK;
}
return status;
}
owb_status owb_reset(const OneWireBus * bus, bool * a_device_present)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!bus || !a_device_present)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if(!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
status = bus->driver->reset(bus, a_device_present);
}
return status;
}
owb_status owb_read_bit(const OneWireBus * bus, uint8_t * out)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!bus || !out)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
bus->driver->read_bits(bus, out, 1);
ESP_LOGD(TAG, "owb_read_bit: %02x", *out);
status = OWB_STATUS_OK;
}
return status;
}
owb_status owb_read_byte(const OneWireBus * bus, uint8_t * out)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!bus || !out)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
bus->driver->read_bits(bus, out, 8);
ESP_LOGD(TAG, "owb_read_byte: %02x", *out);
status = OWB_STATUS_OK;
}
return status;
}
owb_status owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, unsigned int len)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!bus || !buffer)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
for (int i = 0; i < len; ++i)
{
uint8_t out;
bus->driver->read_bits(bus, &out, 8);
buffer[i] = out;
}
ESP_LOGD(TAG, "owb_read_bytes, len %d:", len);
ESP_LOG_BUFFER_HEX_LEVEL(TAG, buffer, len, ESP_LOG_DEBUG);
status = OWB_STATUS_OK;
}
return status;
}
owb_status owb_write_bit(const OneWireBus * bus, const uint8_t bit)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!bus)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
ESP_LOGD(TAG, "owb_write_bit: %02x", bit);
bus->driver->write_bits(bus, bit & 0x01u, 1);
status = OWB_STATUS_OK;
}
return status;
}
owb_status owb_write_byte(const OneWireBus * bus, uint8_t data)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!bus)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
ESP_LOGD(TAG, "owb_write_byte: %02x", data);
bus->driver->write_bits(bus, data, 8);
status = OWB_STATUS_OK;
}
return status;
}
owb_status owb_write_bytes(const OneWireBus * bus, const uint8_t * buffer, size_t len)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!bus || !buffer)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
ESP_LOGD(TAG, "owb_write_bytes, len %d:", len);
ESP_LOG_BUFFER_HEX_LEVEL(TAG, buffer, len, ESP_LOG_DEBUG);
for (int i = 0; i < len; i++)
{
bus->driver->write_bits(bus, buffer[i], 8);
}
status = OWB_STATUS_OK;
}
return status;
}
owb_status owb_write_rom_code(const OneWireBus * bus, OneWireBus_ROMCode rom_code)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!bus)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
owb_write_bytes(bus, (uint8_t*)&rom_code, sizeof(rom_code));
status = OWB_STATUS_OK;
}
return status;
}
uint8_t owb_crc8_byte(uint8_t crc, uint8_t data)
{
return _calc_crc(crc, data);
}
uint8_t owb_crc8_bytes(uint8_t crc, const uint8_t * data, size_t len)
{
return _calc_crc_block(crc, data, len);
}
owb_status owb_search_first(const OneWireBus * bus, OneWireBus_SearchState * state, bool * found_device)
{
bool result;
owb_status status = OWB_STATUS_NOT_SET;
if (!bus || !state || !found_device)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
memset(&state->rom_code, 0, sizeof(state->rom_code));
state->last_discrepancy = 0;
state->last_family_discrepancy = 0;
state->last_device_flag = false;
_search(bus, state, &result);
status = OWB_STATUS_OK;
*found_device = result;
}
return status;
}
owb_status owb_search_next(const OneWireBus * bus, OneWireBus_SearchState * state, bool * found_device)
{
owb_status status = OWB_STATUS_NOT_SET;
bool result = false;
if (!bus || !state || !found_device)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
_search(bus, state, &result);
status = OWB_STATUS_OK;
*found_device = result;
}
return status;
}
char * owb_string_from_rom_code(OneWireBus_ROMCode rom_code, char * buffer, size_t len)
{
for (int i = sizeof(rom_code.bytes) - 1; i >= 0; i--)
{
sprintf(buffer, "%02x", rom_code.bytes[i]);
buffer += 2;
}
return buffer;
}
owb_status owb_set_strong_pullup(const OneWireBus * bus, bool enable)
{
owb_status status = OWB_STATUS_NOT_SET;
if (!bus)
{
status = OWB_STATUS_PARAMETER_NULL;
}
else if (!_is_init(bus))
{
status = OWB_STATUS_NOT_INITIALIZED;
}
else
{
if (bus->use_parasitic_power && bus->strong_pullup_gpio != GPIO_NUM_NC)
{
gpio_set_level(bus->strong_pullup_gpio, enable ? 1 : 0);
ESP_LOGD(TAG, "strong pullup GPIO %d", enable);
} // else ignore
status = OWB_STATUS_OK;
}
return status;
}