-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOneWireESP32.cpp
333 lines (281 loc) · 7.72 KB
/
OneWireESP32.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
https://github.com/junkfix/esp32-ds18b20
*/
#include <Arduino.h>
#include "OneWireESP32.h"
#define OWR_OK 0
#define OWR_CRC 1
#define OWR_BAD_DATA 2
#define OWR_TIMEOUT 3
#define OWR_DRIVER 4
#define OW_RESET_PULSE 500
#define OW_RESET_WAIT 200
#define OW_RESET_PRESENCE_WAIT_MIN 15
#define OW_RESET_PRESENCE_MIN 60
#define OW_SLOT_BIT_SAMPLE_TIME 15
#define OW_SLOT_START 2
#define OW_SLOT_BIT 60
#define OW_SLOT_RECOVERY 5
#define OW_TIMEOUT 50
static rmt_symbol_word_t ow_bit0 = {
.duration0 = OW_SLOT_START + OW_SLOT_BIT,
.level0 = 0,
.duration1 = OW_SLOT_RECOVERY,
.level1 = 1
};
static rmt_symbol_word_t ow_bit1 = {
.duration0 = OW_SLOT_START,
.level0 = 0,
.duration1 = OW_SLOT_BIT + OW_SLOT_RECOVERY,
.level1 = 1
};
const rmt_transmit_config_t owtxconf = {
.loop_count = 0,
.flags = {
.eot_level = 1
}
};
const rmt_receive_config_t owrxconf = {
.signal_range_min_ns = 1000,
.signal_range_max_ns = (OW_RESET_PULSE + OW_RESET_WAIT) * 1000,
};
OneWire32::OneWire32(uint8_t pin){
owpin = static_cast<gpio_num_t>(pin);
rmt_bytes_encoder_config_t bnc = {
.bit0 = ow_bit0,
.bit1 = ow_bit1,
.flags = {
.msb_first = 0
}
};
if(rmt_new_bytes_encoder(&bnc, &(owbenc)) != ESP_OK) {
return;
}
rmt_copy_encoder_config_t cnc = {};
if(rmt_new_copy_encoder(&cnc, &(owcenc)) != ESP_OK) {
return;
}
const rmt_rx_channel_config_t rxconf = {
.gpio_num = owpin,
.clk_src = RMT_CLK_SRC_DEFAULT,
.resolution_hz = 1000000,
.mem_block_symbols = MAX_BLOCKS
};
if(rmt_new_rx_channel(&rxconf, &(owrx)) != ESP_OK) {
return;
}
const rmt_tx_channel_config_t txconf = {
.gpio_num = owpin,
.clk_src = RMT_CLK_SRC_DEFAULT,
.resolution_hz = 1000000,
.mem_block_symbols = MAX_BLOCKS,
.trans_queue_depth = 4,
.flags = {
.io_loop_back = 1,
.io_od_mode = 1
}
};
if(rmt_new_tx_channel(&txconf, &owtx) != ESP_OK) {
return;
}
owqueue = xQueueCreate(1, sizeof(rmt_rx_done_event_data_t));
if(owqueue == NULL) {
return;
}
rmt_rx_event_callbacks_t rx_callbacks = {
.on_recv_done = owrxdone
};
if(rmt_rx_register_event_callbacks(owrx, &rx_callbacks, owqueue) != ESP_OK) {
return;
}
if(rmt_enable(owrx) != ESP_OK) {
return;
}
if(rmt_enable(owtx) != ESP_OK) {
return;
}
static rmt_symbol_word_t release_symbol = {
.duration0 = 1,
.level0 = 1,
.duration1 = 0,
.level1 = 1,
};
rmt_transmit(owtx, owcenc, &release_symbol, sizeof(rmt_symbol_word_t), &owtxconf);
drv = 1;
}
OneWire32::~OneWire32(){
if(owbenc) {
rmt_del_encoder(owbenc);
}
if(owcenc) {
rmt_del_encoder(owcenc);
}
if(owrx) {
rmt_disable(owrx);
rmt_del_channel(owrx);
}
if(owtx) {
rmt_disable(owtx);
rmt_del_channel(owtx);
}
if(owqueue) {
vQueueDelete(owqueue);
}
drv = 0;
}
bool owrxdone(rmt_channel_handle_t ch, const rmt_rx_done_event_data_t *edata, void *udata) {
BaseType_t h = pdFALSE;
xQueueSendFromISR((QueueHandle_t)udata, edata, &h);
return (h == pdTRUE);
}
bool OneWire32::reset(){
rmt_symbol_word_t symbol_reset = {
.duration0 = OW_RESET_PULSE,
.level0 = 0,
.duration1 = OW_RESET_WAIT,
.level1 = 1
};
rmt_rx_done_event_data_t evt;
rmt_receive(owrx, owbuf, sizeof(owbuf), &owrxconf);
rmt_transmit(owtx, owcenc, &symbol_reset, sizeof(rmt_symbol_word_t), &owtxconf);
bool found = false;
if(xQueueReceive(owqueue, &evt, pdMS_TO_TICKS(OW_TIMEOUT)) == pdTRUE) {
size_t symbol_num = evt.num_symbols;
rmt_symbol_word_t *symbols = evt.received_symbols;
if (symbol_num > 1) {
if (symbols[0].level1 == 1) {
if (symbols[0].duration1 > OW_RESET_PRESENCE_WAIT_MIN && symbols[1].duration0 > OW_RESET_PRESENCE_MIN) {
found = true;
}
} else {
if (symbols[0].duration0 > OW_RESET_PRESENCE_WAIT_MIN && symbols[1].duration1 > OW_RESET_PRESENCE_MIN) {
found = true;
}
}
}
if(rmt_tx_wait_all_done(owtx, OW_TIMEOUT) != ESP_OK) {
found = false;
}
}
return found;
}
bool OneWire32::read(uint8_t &data, uint8_t len){
rmt_rx_done_event_data_t evt;
rmt_receive(owrx, owbuf, sizeof(owbuf), &owrxconf);
if(!write((len > 1)? 0xff : 1, len) || xQueueReceive(owqueue, &evt, pdMS_TO_TICKS(OW_TIMEOUT)) != pdTRUE) {
return false;
}
size_t symbol_num = evt.num_symbols;
rmt_symbol_word_t *symbol = evt.received_symbols;
data = 0;
for (uint8_t i = 0; i < symbol_num && i < 8; i++) {
if(!(symbol[i].duration0 > OW_SLOT_BIT_SAMPLE_TIME)){
data |= 1 << i;
}
}
if(len != 8){ data = data & 0x01; }
return true;
}
bool OneWire32::write(const uint8_t data, uint8_t len){
if(len < 8){
const rmt_symbol_word_t *sb;
for(uint8_t i = 0; i < len; i++){
sb = &ow_bit0;
if((data & (1 << i)) != 0) {
sb = &ow_bit1;
}
if(rmt_transmit(owtx, owcenc, sb, sizeof(rmt_symbol_word_t), &owtxconf) != ESP_OK){
return false;
}
}
}else{
if(rmt_transmit(owtx, owbenc, &data, 1, &owtxconf) != ESP_OK){
return false;
}
}
return (rmt_tx_wait_all_done(owtx, OW_TIMEOUT) == ESP_OK);
}
void OneWire32::request(){
if(drv && reset()){
write(0xCC);
write(0x44);
}
}
const uint8_t crc_table[] = {0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, 219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205, 17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80, 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238, 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53};
uint8_t OneWire32::getTemp(uint64_t &addr, float &temp){
uint8_t error = OWR_OK;
if(!drv){return OWR_DRIVER;}
if(reset()){ //connected
write(0x55);
uint8_t *a = (uint8_t *)&addr;
for(uint8_t i = 0; i < 8; i++){
write(a[i]);
}
write(0xBE); // Read
uint8_t data[9]; uint16_t zero = 0; uint8_t crc = 0;
for(uint8_t j = 0; j < 9; j++){
if(!read(data[j],8)){data[j] = 0;}
zero += data[j];
if(j < 8 ){crc = crc_table[crc ^ data[j]];}
}
if(zero != 0x8f7 && zero){
if(data[8] == crc ){ //CRC OK
int16_t t = (data[1] << 8) | data[0];
temp = ((float)t / 16.0);
}else{
error = OWR_CRC;
}
}else{
error = OWR_BAD_DATA;
}
}else{
error = OWR_TIMEOUT;
}
return error;
}
uint8_t OneWire32::search(uint64_t *addresses, uint8_t total) {
int8_t last_src;
int8_t last_dev = -1;
uint8_t found = 0;
uint8_t loop = 1;
if(!drv){return found;}
uint64_t addr = 0;
while(loop && found < total){
loop = 0;
last_src = last_dev;
if(!reset()){
found = 0;
break;
}
write(0xF0,8);
for(uint8_t i = 0; i < 64; i += 1){
uint8_t bitA, bitB; uint64_t m = 1ULL << i;
if(!read(bitA, 1) || !read(bitB, 1) || (bitA && bitB)){
addr = found = loop = 0;
break;
}else if(!bitA && !bitB){
if(i == last_src){
write(1, 1); addr |= m;
}else{
if((addr & m) == 0 || i > last_src){
write(0, 1); loop = 1; addr &= ~m;
last_dev = i;
}else{
write(1, 1);
}
}
}else{
if(bitA){
write(1, 1); addr |= m;
}else{
write(0, 1); addr &= ~m;
}
}
}
if(addr){
addresses[found] = addr;
found++;
}
}
return found;
}