-
Notifications
You must be signed in to change notification settings - Fork 29
/
DS18B20Pi5V2.c
589 lines (433 loc) · 13 KB
/
DS18B20Pi5V2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// Read DS18B20 sensor using one DS18B20 sensor per GPIO PIN using
// parallel method to speed up process
// Copyright (c) 29 June 2014 Daniel Perron
//
// Need to install pull-up resistor
// It's need to be compiled like
// gcc -l gpiod -o DS18B20V2 DS18B20V2.c
// 3- Reset DS18B20
// 4- send SKIP_ROM command
// 5- send START_ACQUISITION
// 6- wait until acquisition is done
// 7- Reset DS18B20
// 8- Send SKIP_ROM command
// 9- Send read register command
// 10- Collect GPIO word into table
// 11- Decode individual bit to get sensor temperature
// 12- End
// November 2023
// use lib gpiod
// ---- obsolete ----
// modified version to check BCM physical address
// February 1, 2016
// check "/proc/device-tree/soc/ranges" for BCM address
//
// add timer delay for us resolution from Gladkikh Artem
// DelayMicrosecondsNoSleep
// --- obsolete ---
// August 3 , 2014
// Priority added
// code base on Adafruit DHT22 source code for
// set_max_priority and set_default_priority
// Copyright (c) 2014 Adafruit Industries
// Author: Tony DiCola
// August 5, 2014
// - Add retried on fail
// - Add bit resolution checking at the start to set the correct acquisition waiting time
// - Add loop scanning.
// copyrigth declaration
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sched.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <gpiod.h>
// define gpiod structure
struct gpiod_line_bulk gpiolines;
struct gpiod_chip *gpiochip;
struct gpiod_line *gpioline;
// define gpio access array
int intset[32]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int intclr[32]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int intvalue[32];
int DS18B20_Pins[32]= {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int BadSensors[32];
int NumberOfPin=0;
int intvalues[72*32];
int Idx=0;
unsigned int bitdatatable[72];
int bitdataCounter;
#define GPIO_READ(A) gpiod_line_get_value_bulk(&gpiolines,A);
#define GPIO_SET gpiod_line_set_value_bulk(&gpiolines,intset);
#define GPIO_CLR gpiod_line_set_value_bulk(&gpiolines,intclr);
#define DS18B20_SKIP_ROM 0xCC
#define DS18B20_CONVERT_T 0x44
#define DS18B20_MATCH_ROM 0x55
#define DS18B20_SEARCH_ROM 0XF0
#define DS18B20_READ_SCRATCHPAD 0xBE
#define DS18B20_WRITE_SCRATCHPAD 0x4E
#define DS18B20_COPY_SCRATCHPAD 0x48
unsigned char ScratchPad[9];
double temperature;
int resolution;
bool init_gpiod(void)
{
gpiochip = gpiod_chip_open_by_name("gpiochip4");
if(gpiochip == NULL)
gpiochip = gpiod_chip_open_by_name("gpiochip0");
if(gpiochip == NULL)
{
printf("unable to open GPIO\n");
return false;
}
gpiod_line_bulk_init(&gpiolines);
}
bool add_pin(int pin)
{
printf("add Pin %d =>",pin);
gpioline = gpiod_chip_get_line(gpiochip,pin);
if(gpioline == NULL)
{
printf(" NULL\n");
return false;
}
printf("Done\n");
// gpiod_line_request_output_flags(gpioline,"DS18B20",
// GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN|GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP,1);
gpiod_line_bulk_add (&gpiolines,gpioline);
return true;
}
typedef struct {
unsigned char valid;
unsigned char resolution;
double temperature;
}SensorInfoStruct;
SensorInfoStruct DS18B20_Data[32];
struct timespec mystart,myacqstart,myend;
double clock_diff(struct timespec start,struct timespec end)
{
double dtime;;
dtime = (double) end.tv_sec- start.tv_sec;
dtime += (double) ((end.tv_nsec - start.tv_nsec)/ 1.0e9);
return dtime;
}
#define DELAY1US DelayMicrosecondsNoSleep(1);
void DelayMicrosecondsNoSleep (int delay_us)
{
long int start_time;
long int time_difference;
struct timespec gettime_now;
clock_gettime(CLOCK_REALTIME, &gettime_now);
start_time = gettime_now.tv_nsec; //Get nS value
while (1)
{
clock_gettime(CLOCK_REALTIME, &gettime_now);
time_difference = gettime_now.tv_nsec - start_time;
if (time_difference < 0)
time_difference += 1000000000; //(Rolls over every 1 second)
if (time_difference > (delay_us * 1000)) //Delay for # nS
break;
}
}
// If everything is ok it will return 0
// otherwise BadSensor will have the bit corresponding to the bad sensor set
int DoReset(void)
{
unsigned int gpio_pin;
GPIO_SET
DelayMicrosecondsNoSleep(10);
GPIO_CLR
usleep(480);
GPIO_SET
DelayMicrosecondsNoSleep(60);
GPIO_READ(intvalue)
DelayMicrosecondsNoSleep(420);
int Flag=1;
for(int loop=0;loop<NumberOfPin;loop++)
{
BadSensors[loop]=intvalue[loop];
if(BadSensors[loop]==1)
Flag=0;
}
return Flag;
}
void WriteByte(unsigned char value)
{
unsigned char Mask=1;
int loop;
for(loop=0;loop<8;loop++)
{
GPIO_CLR
if((value & Mask)!=0)
{
DELAY1US
GPIO_SET
usleep(60);
}
else
{
DelayMicrosecondsNoSleep(60);
GPIO_SET
usleep(1);
}
Mask*=2;
DelayMicrosecondsNoSleep(60);
}
usleep(100);
}
void ReadByte(unsigned int *datatable)
{
int loop,loop2;
unsigned int _temp;
unsigned int mask;
for(loop=0;loop<8;loop++)
{
GPIO_CLR
DELAY1US
// set input
GPIO_SET
DelayMicrosecondsNoSleep(2);
GPIO_READ(datatable)
datatable+=32;
DelayMicrosecondsNoSleep(60);
}
}
// extract information by bit position from table of 72 unsigned long
void ExtractScratchPad( unsigned int bitmask, unsigned char *ScratchPad)
{
int loop,loopr,Idx;
unsigned char Mask=1;
unsigned char databyte=0;
unsigned int *pointer= &bitdatatable[0];
for(loopr=0;loopr<9;loopr++)
{
Mask=1;
databyte=0;
for(loop=0;loop<8;loop++)
{
if((*(pointer++) & bitmask)!=0)
databyte|=Mask;
Mask*=2;
}
*(ScratchPad++)=databyte;
}
}
unsigned char CalcCRC(unsigned char * data, unsigned char byteSize)
{
unsigned char shift_register = 0;
unsigned char loop,loop2;
char DataByte;
for(loop = 0; loop < byteSize; loop++)
{
DataByte = *(data + loop);
for(loop2 = 0; loop2 < 8; loop2++)
{
if((shift_register ^ DataByte)& 1)
{
shift_register = shift_register >> 1;
shift_register ^= 0x8C;
}
else
shift_register = shift_register >> 1;
DataByte = DataByte >> 1;
}
}
return shift_register;
}
// Adafruit set_max_priority and set_default priority add-on
void set_max_priority(void) {
struct sched_param sched;
memset(&sched, 0, sizeof(sched));
// Use FIFO scheduler with highest priority for the lowest chance of the kernel context switching.
sched.sched_priority = sched_get_priority_max(SCHED_FIFO);
sched_setscheduler(0, SCHED_FIFO, &sched);
}
void set_default_priority(void) {
struct sched_param sched;
memset(&sched, 0, sizeof(sched));
// Go back to default scheduler with default 0 priority.
sched.sched_priority = 0;
sched_setscheduler(0, SCHED_OTHER, &sched);
}
int ReadSensors(void)
{
int temp;
int loop;
int GotOneResult;
int GotAllResults;
unsigned char CRCByte;
union {
short SHORT;
unsigned char CHAR[2];
}IntTemp;
int retryloop;
// ok now read until we got a least one valid crc up to n times
#define RETRY_MAX 5
for(retryloop=0;retryloop<RETRY_MAX;retryloop++)
{
GotOneResult=0; // this will indicate if we have one reading with a good crc
GotAllResults=1; // this will indicate if we have all readins from all sensors
set_max_priority();
DoReset();
// Read scratch pad
WriteByte(DS18B20_SKIP_ROM);
WriteByte(DS18B20_READ_SCRATCHPAD);
// for(loop=0;loop<72;loop+=8)
// ReadByte(&bitdatatable[loop]);
for(loop=0;loop<72;loop+=8)
ReadByte(&intvalues[loop*32]);
// convert to bitdatatable
int mask=1;
for(loop=0;loop<72;loop++)
{
int _temp=0;
for(int loop2=0;loop2<32;loop2++)
if(intvalues[loop*32+loop2])
_temp|= 1 << loop2;
bitdatatable[loop]=_temp;
}
set_default_priority();
// extract bit info fro valid gpio pin
for(loop=0;loop<NumberOfPin;loop++)
{
// temp = DS18B20_Pins[loop];
temp = loop;
// if(temp<0) break;
// by default put data invalid
DS18B20_Data[loop].valid=0;
ExtractScratchPad(1UL<<temp,ScratchPad);
CRCByte= CalcCRC(ScratchPad,8);
if(CRCByte!=ScratchPad[8])
{
GotAllResults=0;
}
else
{
//Check Resolution
resolution=0;
if((ScratchPad[4] & 0x9F)== 0x1f)
{
GotOneResult=1;
DS18B20_Data[loop].valid=1;
switch(ScratchPad[4])
{
case 0x1f: resolution=9;break;
case 0x3f: resolution=10;break;
case 0x5f: resolution=11;break;
default: resolution=12;break;
}
DS18B20_Data[loop].resolution=resolution;
// Read Temperature
IntTemp.CHAR[0]=ScratchPad[0];
IntTemp.CHAR[1]=ScratchPad[1];
temperature = 0.0625 * (double) IntTemp.SHORT;
DS18B20_Data[loop].temperature= temperature;
}
else
GotAllResults=0;
}
}
// if(GotOneResult) return(1);
if(GotAllResults) return(1);
usleep(10000);
}
return 0;
}
int main(int argc, char **argv)
{
int temp;
int loop;
int Flag=0;
unsigned long AcqDelay; // Acquisition time delay needed
unsigned char Hresolution;
if(argc==1)
{
printf("Usage: DS18B20 GPIOFirst, GPIOSecond, ..., GPIOLast\n");
return -1;
}
init_gpiod();
for(loop=1;loop<argc;loop++)
{
DS18B20_Pins[loop-1]=atoi(argv[loop]);
add_pin(DS18B20_Pins[loop-1]);
NumberOfPin++;
}
gpiod_line_request_bulk_output_flags(&gpiolines,"DS18B20",
GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN|GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP,intset);
// first thing to do is to check all sensor to determine which is the highest resolution
//
Hresolution=9;
ReadSensors();
for(loop=0;;loop++)
{
if(DS18B20_Pins[loop]<0) break;
if(DS18B20_Data[loop].valid)
if(DS18B20_Data[loop].resolution > Hresolution)
Hresolution=DS18B20_Data[loop].resolution;
}
// now set timing according to the highesh resolution sensor.
switch(Hresolution)
{
case 9: AcqDelay= 94000;break;
case 10: AcqDelay= 188000;break;
case 11: AcqDelay= 375000;break;
default: AcqDelay= 750000;
}
printf("Highest resolution is %d bits. Acquisition delay will be %ldms.\n",Hresolution,AcqDelay/1000);fflush(stdout);
printf("Hit enter to continue. Ctrl-c to break.\n");
fflush(stdout);
getchar();
clock_gettime(CLOCK_MONOTONIC,&mystart);
// and now the real stuff
do
{
// Do Reset
clock_gettime(CLOCK_MONOTONIC,&myacqstart);
set_max_priority();
DoReset();
// Start Acquisition
WriteByte(DS18B20_SKIP_ROM);
WriteByte(DS18B20_CONVERT_T);
set_default_priority();
// wait for the highest resolution probe
usleep(AcqDelay);
ReadSensors();
// now let's print result
clock_gettime(CLOCK_MONOTONIC,&myend);
printf("====\n%.3f sec acquisition time = %.3f sec\n",clock_diff(mystart, myacqstart),clock_diff(myacqstart,myend));
for(loop=0;;loop++)
{
if(DS18B20_Pins[loop]<0) break;
printf("GPIO %d : ",DS18B20_Pins[loop]);
if(DS18B20_Data[loop].valid)
printf("%02d bits Temperature: %6.2f +/- %4.2f Celsius\n", DS18B20_Data[loop].resolution ,DS18B20_Data[loop].temperature, 0.0625 * (double) (1<<(12 - DS18B20_Data[loop].resolution)));
else
printf("Bad CRC!\n");
}
fflush(stdout);
}while(1);
gpiod_line_release_bulk(&gpiolines);
gpiod_chip_close(gpiochip);
return 0;
} // main