forked from danjperron/BitBangingDS18B20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigDS18B20.c
568 lines (408 loc) · 11.3 KB
/
configDS18B20.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
// 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
// modified version to read DS18B20 in bit banging
//
// 24 May 2014
// Daniel Perron
//
// Use At your own risk
// 7 August 2014
// Add arg parameter to select the GPIO pin
// Add the priority function from Adafruit DHT22 c code
// 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
// 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 DEALI
//
// How to access GPIO registers from C-code on the Raspberry-Pi
// Example program
// 15-January-2012
// Dom and Gert
// Revised: 15-Feb-2013
// Access from ARM Running Linux
//#define BCM2708_PERI_BASE 0x20000000
unsigned long BCM2708_PERI_BASE=0x20000000;
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <time.h>
#include <sched.h>
#include <string.h>
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)
int mem_fd;
void *gpio_map;
// I/O access
volatile unsigned *gpio;
// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
#define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
#define GPIO_READ(g) (*(gpio + 13) &= (1<<(g)))
#define DS18B20_SKIP_ROM 0xCC
#define DS18B20_CONVERT_T 0x44
#define DS18B20_READ_SCRATCHPAD 0xBE
#define DS18B20_WRITE_SCRATCHPAD 0x4E
#define DS18B20_COPY_SCRATCHPAD 0x48
unsigned char ScratchPad[9];
double temperature;
int resolution;
void setup_io();
unsigned int DS_PIN=4;
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);
}
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;
}
}
int DoReset(void)
{
int loop;
INP_GPIO(DS_PIN);
DelayMicrosecondsNoSleep(1000);
INP_GPIO(DS_PIN);
OUT_GPIO(DS_PIN);
// pin low for 480 us
GPIO_CLR=1<<DS_PIN;
DelayMicrosecondsNoSleep(480);
INP_GPIO(DS_PIN);
DelayMicrosecondsNoSleep(60);
if(GPIO_READ(DS_PIN)==0)
{
DelayMicrosecondsNoSleep(380);
return 1;
}
return 0;
}
void smalldelay(void)
{
int loop2;
for(loop2=0;loop2<100;loop2++);
}
void WriteByte(unsigned char value)
{
unsigned char Mask=1;
int loop;
for(loop=0;loop<8;loop++)
{
INP_GPIO(DS_PIN);
OUT_GPIO(DS_PIN);
GPIO_CLR= 1 <<DS_PIN;
if((value & Mask)!=0)
{
DelayMicrosecondsNoSleep(1);
INP_GPIO(DS_PIN);
DelayMicrosecondsNoSleep(60);
}
else
{
DelayMicrosecondsNoSleep(60);
INP_GPIO(DS_PIN);
DelayMicrosecondsNoSleep(1);
}
Mask*=2;
DelayMicrosecondsNoSleep(60);
}
DelayMicrosecondsNoSleep(100);
}
unsigned char ReadBit(void)
{
INP_GPIO(DS_PIN);
OUT_GPIO(DS_PIN);
// PIN LOW
GPIO_CLR= 1 << DS_PIN;
DelayMicrosecondsNoSleep(1);
// set INPUT
INP_GPIO(DS_PIN);
DelayMicrosecondsNoSleep(2);
if(GPIO_READ(DS_PIN)!=0)
return 1;
return 0;
}
unsigned char ReadByte(void)
{
unsigned char Mask=1;
int loop;
unsigned char data=0;
int loop2;
for(loop=0;loop<8;loop++)
{
// set output
INP_GPIO(DS_PIN);
OUT_GPIO(DS_PIN);
// PIN LOW
GPIO_CLR= 1<<DS_PIN;
DelayMicrosecondsNoSleep(1);
// set input
INP_GPIO(DS_PIN);
// Wait 2 us
DelayMicrosecondsNoSleep(2);
if(GPIO_READ(DS_PIN)!=0)
data |= Mask;
Mask*=2;
DelayMicrosecondsNoSleep(60);
}
return data;
}
int ReadScratchPad(void)
{
int loop;
if(DoReset())
{
WriteByte(DS18B20_SKIP_ROM);
WriteByte(DS18B20_READ_SCRATCHPAD);
for(loop=0;loop<9;loop++)
{
ScratchPad[loop]=ReadByte();
}
return 1;
}
return 0;
}
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;
}
int ReadSensor(void)
{
int maxloop;
int RetryCount;
int loop;
unsigned char CRCByte;
union {
short SHORT;
unsigned char CHAR[2];
}IntTemp;
time_t t = time(NULL);
struct tm tm = *localtime(&t);
temperature=-9999.9;
for(RetryCount=0;RetryCount<5;RetryCount++)
{
if(!DoReset()) continue;
// start a conversion
WriteByte(DS18B20_SKIP_ROM);
WriteByte(DS18B20_CONVERT_T);
maxloop=0;
// wait until ready
while(!ReadBit())
{
putchar('.');
maxloop++;
if(maxloop>100000) break;
}
if(maxloop>100000) continue;
if(!ReadScratchPad()) continue;
for(loop=0;loop<9;loop++)
printf("%02X ",ScratchPad[loop]);
printf("\n");fflush(stdout);
// OK Check sum Check;
CRCByte= CalcCRC(ScratchPad,8);
if(CRCByte!=ScratchPad[8]) continue;
//Check Resolution
resolution=0;
switch(ScratchPad[4])
{
case 0x1f: resolution=9;break;
case 0x3f: resolution=10;break;
case 0x5f: resolution=11;break;
case 0x7f: resolution=12;break;
}
if(resolution==0) continue;
// Read Temperature
IntTemp.CHAR[0]=ScratchPad[0];
IntTemp.CHAR[1]=ScratchPad[1];
temperature = 0.0625 * (double) IntTemp.SHORT;
printf("%02d bits Temperature: %6.2f +/- %f Celsius\n", resolution ,temperature, 0.0625 * (double) (1<<(12 - resolution)));
return 1;
}
return 0;
}
void WriteScratchPad(unsigned char TH, unsigned char TL, unsigned char config)
{
int loop;
// First reset device
DoReset();
DelayMicrosecondsNoSleep(1000);
// Skip ROM command
WriteByte(DS18B20_SKIP_ROM);
// Write Scratch pad
WriteByte(DS18B20_WRITE_SCRATCHPAD);
// Write TH
WriteByte(TH);
// Write TL
WriteByte(TL);
// Write config
WriteByte(config);
}
void CopyScratchPad(void)
{
// Reset device
DoReset();
DelayMicrosecondsNoSleep(1000);
// Skip ROM Command
WriteByte(DS18B20_SKIP_ROM);
// copy scratch pad
WriteByte(DS18B20_COPY_SCRATCHPAD);
usleep(100000);
}
int main(int argc, char **argv)
{
int loop;
int config;
// Set up gpi pointer for direct register access
if(argc==2)
{
DS_PIN = atoi(argv[1]);
}
printf("GPIO %d\n",DS_PIN);
if((DS_PIN < 1) || (DS_PIN>32))
{
printf("Invalid GPIO PIN\n");
return -1;
}
setup_io();
set_max_priority();
if(ReadSensor())
{
printf("DS18B20 Resolution (9,10,11 or 12) ?");fflush(stdout);
config=0;
set_default_priority();
if(scanf("%d",&resolution)==1)
{
switch(resolution)
{
case 9: config=0x1f;break;
case 10: config=0x3f;break;
case 11: config=0x5f;break;
case 12: config=0x7f;break;
}
}
if(config==0)
printf("Invalid Value! Nothing done.\n");
else
{
printf("Try to set %d bits config=%2X\n",resolution,config);
usleep(1000);
set_max_priority();
WriteScratchPad(ScratchPad[2],ScratchPad[3],config);
usleep(1000);
CopyScratchPad();
}
}
set_default_priority();
return 0;
} // main
//
// Set up a memory regions to access GPIO
//
void setup_io()
{
int handle;
int count;
struct{
unsigned long V1,V2,V3;
}ranges;
/* open /dev/mem */
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem \n");
exit(-1);
}
// read /proc/device-tree/soc/ranges
// to check if we have the GPIO at 0x20000000 or 0x3F000000
#define Swap4Bytes(val) \
( (((val) >> 24) & 0x000000FF) | (((val) >> 8) & 0x0000FF00) | \
(((val) << 8) & 0x00FF0000) | (((val) << 24) & 0xFF000000) )
handle = open("/proc/device-tree/soc/ranges" , O_RDONLY);
if(handle >=0)
{
count = read(handle,&ranges,12);
if(count == 12)
BCM2708_PERI_BASE=Swap4Bytes(ranges.V2);
close(handle);
}
printf("BCM GPIO BASE= %lx\n",BCM2708_PERI_BASE);
/* mmap GPIO */
gpio_map = mmap(
NULL, //Any adddress in our space will do
BLOCK_SIZE, //Map length
PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
MAP_SHARED, //Shared with other processes
mem_fd, //File to map
GPIO_BASE //Offset to GPIO peripheral
);
close(mem_fd); //No need to keep mem_fd open after mmap
if (gpio_map == MAP_FAILED) {
printf("mmap error %d\n", (int)gpio_map);//errno also set!
exit(-1);
}
// Always use volatile pointer!
gpio = (volatile unsigned *)gpio_map;
} // setup_io