components/esp32-owb/owb.c

Tue, 26 Sep 2023 14:57:18 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 26 Sep 2023 14:57:18 +0200
changeset 72
acc1904cd70d
parent 0
88d965579617
permissions
-rw-r--r--

Migrated to isp-idf v5.1

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

mercurial