forked from smurfix/owslave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonewire.c
More file actions
692 lines (623 loc) · 13.2 KB
/
onewire.c
File metadata and controls
692 lines (623 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
* Copyright © 2010, Matthias Urlichs <matthias@urlichs.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License (included; see the file LICENSE)
* for more details.
*/
/* Based on work published at http://www.mikrocontroller.net/topic/44100 */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <setjmp.h>
#define MAIN
#include "onewire.h"
#include "features.h"
#define PRESCALE 64
#define USEC2TICKS(x) (x/(F_CPU/PRESCALE))
#undef EMU_18b20 // temperature
#define EMU_2423 // counter
// 1wire interface
static uint8_t bitcount, transbyte;
#define vbitcount *(volatile uint8_t *)&bitcount
static uint8_t xmitlen;
static unsigned char addr[8];
static jmp_buf end_out;
#ifdef __AVR_ATtiny13__
#define OWPIN PINB
#define OWPORT PORTB
#define OWDDR DDRB
#define ONEWIREPIN 1 // INT0
#elif defined(__AVR_ATtiny84__)
#define OWPIN PINB
#define OWPORT PORTB
#define OWDDR DDRB
#define ONEWIREPIN 2 // INT0
#elif defined (__AVR_ATmega8__)
#define OWPIN PIND
#define OWPORT PORTD
#define OWDDR DDRD
#define ONEWIREPIN 2 // INT0
#elif defined (__AVR_ATmega168__)
#define OWPIN PIND
#define OWPORT PORTD
#define OWDDR DDRD
#define ONEWIREPIN 2 // INT0
//#define DBGPIN 3 // debug output
#else
#error Pinout for your CPU undefined
#endif
#define T_(c) ((F_CPU/64)/(1000000/c))
#define T_PRESENCE T_(120)-5
#define T_PRESENCEWAIT T_(20)
#define T_SAMPLE T_(15)-2 // overhead
#define T_XMIT T_(60)-5 // overhead (measured w/ scope on ATmega168)
#define T_RESET_ T_(400) // timestamp for sampling
#define T_RESET (T_RESET_-T_SAMPLE)
#if (T_RESET>200)
#error Reset slot is too wide, fix timing
#endif
#define BAUDRATE 57600
#ifdef DBGPIN
static char interest = 0;
#define DBG_IN() interest=1
#define DBG_OUT() interest=0
#define DBG_ON() if(interest) OWPORT |= (1<<DBGPIN)
#define DBG_OFF() if(interest) OWPORT &= ~(1<<DBGPIN)
#else
#define DBG_IN() do { } while(0)
#define DBG_OUT() do { } while(0)
#define DBG_ON() do { } while(0)
#define DBG_OFF() do { } while(0)
#endif
// stupidity
#ifndef TIMER0_OVF_vect
# define TIMER0_OVF_vect TIM0_OVF_vect
#endif
// Initialisierung der Hardware
void setup(void)
{
unsigned char i;
#ifdef __AVR_ATtiny13__
CLKPR = 0x80; // Prepare to ...
CLKPR = 0x00; // ... set to 9.6 MHz
TCCR0A = 0;
TCCR0B = 0x03; // Prescaler 1/64
MCUCR |= (1 << ISC00); // Interrupt on both level changes
#elif defined (__AVR_ATtiny84__)
CLKPR = 0x80; // Prepare to ...
CLKPR = 0x00; // ... set to 8.0 MHz
MCUCR |= (1 << ISC00); // Interrupt on both level changes
#elif defined (__AVR_ATmega8__)
// Clock is set via fuse
// CKSEL = 0100; Fuse Low Byte Bits 3:0
TCCR0 = 0x03; // Prescaler 1/64
MCUCR |= (1 << ISC00); // Interrupt on both level changes
#elif defined (__AVR_ATmega168__)
// Clock is set via fuse
TCCR0A = 0;
TCCR0B = 0x03; // Prescaler 1/64
EICRA = (1<<ISC00); // interrupt of INT0 (pin D2) on both level changes
#else
#error Not yet implemented
#endif
OWPORT &= ~(1 << ONEWIREPIN);
OWDDR &= ~(1 << ONEWIREPIN);
#ifdef DBGPIN
OWPORT &= ~(1 << DBGPIN);
OWDDR |= (1 << DBGPIN);
#endif
#ifdef HAVE_TIMESTAMP
TCCR1A = 0;
TCCR1B = (1<<ICES1) | (1<<CS10);
TIMSK1 &= ~(1<<ICIE1);
TCNT1 = 0;
#endif
// Get 64bit address from EEPROM
while(EECR & (1<<EEPE)); // Wait for EPROM circuitry to be ready
for (i=8; i;) {
i--;
/* Set up address register */
EEARL = 7-i; // set EPROM Address
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE);
/* Return data from data register */
addr[i] = EEDR;
}
// init application-specific code
init_state();
IFR |= (1 << INTF0);
IMSK |= (1 << INT0);
#ifdef HAVE_UART
uart_init(UART_BAUD_SELECT(BAUDRATE,F_CPU));
#endif
}
static inline void set_timer(int timeout)
{
//DBG_C('T');
//DBG_X(timeout);
//DBG_C(',');
TCNT0 = ~timeout;
TIFR0 |= (1 << TOV0);
TIMSK0 |= (1 << TOIE0);
}
static inline void clear_timer(void)
{
//DBG_C('t');
TCNT0 = 0;
TIMSK0 &= ~(1 << TOIE0); // turn off the timer IRQ
}
void next_idle(void)
{
if(state != S_IDLE)
DBG_P("\nj_out\n");
set_idle();
longjmp(end_out,1);
}
void next_command(void)
{
if ((state & S_MASK) != S_CMD_IDLE) {
DBG_P("\nc_out\n");
set_idle();
} else {
bitcount = 8;
state = S_RECEIVE_OPCODE | (state & S_XMIT2);
}
longjmp(end_out,1);
}
static void
xmit_any(uint8_t val, uint8_t len)
{
while(state & (S_RECV|S_XMIT))
update_idle(vbitcount);
cli();
if(!(state & 0x20)) {
sei();
if(state != S_IDLE) {
if (state < 0x10)
longjmp(end_out,1);
DBG_P("\nState error xmit! ");
DBG_X(state);
DBG_C('\n');
}
next_idle();
}
if(xmitlen) {
sei();
DBG_P("\nXbuflen error xmit! ");
DBG_X(xmitlen);
DBG_C('\n');
next_idle();
}
if(bitcount) {
sei();
DBG_P("\nBitcount error xmit! ");
DBG_X(state);
DBG_C(',');
DBG_X(bitcount);
DBG_C('\n');
next_idle();
}
transbyte = val;
bitcount = 8;
state = S_CMD_XMIT | (state & S_XMIT2);
DBG_X(val);
sei();
DBG_OFF();
}
void xmit_bit(uint8_t val)
{
DBG_C('<');
DBG_C('_');
xmit_any(!!val,1);
}
void xmit_byte(uint8_t val)
{
DBG_C('<');
xmit_any(val,8);
}
uint8_t rx_ready(void)
{
return !(state & (S_RECV|S_XMIT));
}
static void
recv_any(uint8_t len)
{
while(state & (S_RECV|S_XMIT))
update_idle(vbitcount);
cli();
if(!(state & 0x20)) {
sei();
if (state != S_IDLE) {
if (state < 0x10)
longjmp(end_out,1);
DBG_P("\nState error recv! ");
DBG_X(state);
DBG_C('\n');
}
next_idle();
}
if(xmitlen) {
sei();
DBG_P("\nXbuflen error recv! ");
DBG_X(xmitlen);
DBG_C('\n');
next_idle();
}
if(bitcount) {
sei();
DBG_P("\nBitcount error recv! ");
DBG_X(bitcount);
DBG_C('\n');
next_idle();
}
bitcount = len;
state = S_CMD_RECV | (state & S_XMIT2);
sei();
DBG_OFF();
DBG_C('>');
}
static uint8_t recv_any_in(void)
{
while(state & S_RECV)
update_idle(vbitcount);
if ((state & S_MASK) != S_CMD_IDLE)
longjmp(end_out,1);
return transbyte;
}
void recv_bit(void)
{
recv_any(1);
DBG_C('_');
}
void recv_byte(void)
{
recv_any(8);
}
uint8_t recv_bit_in(void)
{
uint8_t byte;
byte = ((recv_any_in() & 0x80) != 0);
DBG_X(byte);
return byte;
}
uint8_t recv_byte_in(void)
{
uint8_t byte;
byte = recv_any_in();
DBG_X(byte);
return byte;
}
// this code is from owfs
static uint8_t parity_table[16] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
uint16_t crc16(uint16_t r, uint8_t x)
{
uint16_t c = (x ^ (r & 0xFF));
r >>= 8;
if (parity_table[c & 0x0F] ^ parity_table[(c >> 4) & 0x0F])
r ^= 0xC001;
r ^= (c <<= 6);
r ^= (c << 1);
return r;
}
// Main program
int main(void)
{
#ifdef HAVE_TIMESTAMP
tbpos = sizeof(tsbuf)/sizeof(tsbuf[0]);
uint16_t last_tb = 0;
#endif
state = S_IDLE;
setup();
set_idle();
// now go
sei();
DBG_P("\nInit done!\n");
setjmp(end_out);
while (1) {
#ifdef HAVE_UART
volatile unsigned long long int x; // for 'worse' timing
DBG_C('/');
for(x=0;x<100000ULL;x++)
#endif
{
if((state & S_MASK) == S_HAS_OPCODE)
do_command(transbyte);
// RESET processing takes > 12 bit times, plus one byte.
update_idle((state == S_IDLE) ? 20 : (state < 0x10) ? 8 : vbitcount);
#ifdef HAVE_TIMESTAMP
unsigned char n = sizeof(tsbuf)/sizeof(tsbuf[0]);
while(tbpos < n && n > 0) {
uint16_t this_tb = tsbuf[--n];
DBG_Y(this_tb-last_tb);
last_tb=this_tb;
DBG_C(lev ? '^' : '_');
lev = 1-lev;
cli();
if(tbpos == n) tbpos = sizeof(tsbuf)/sizeof(tsbuf[0]);
sei();
}
if (n == 0) {
DBG_P("<?>");
tbpos = sizeof(tsbuf)/sizeof(tsbuf[0]);
}
#endif
}
}
}
void set_idle(void)
{
if(state != S_IDLE) {
DBG_P(">idle:");
DBG_X(state);
DBG_P(" b");
DBG_N(xmitlen);
DBG_N(bitcount);
DBG_NL();
state = S_IDLE;
}
DBG_OFF();
DBG_OUT();
bitcount = 0;
xmitlen = 0;
clear_timer();
IFR |= (1 << INTF0); // ack+enable level-change interrupt, just to be safe
IMSK |= (1 << INT0);
OWDDR &= ~(1 << ONEWIREPIN); // set to input
}
static void set_reset(void) {
state = S_RESET;
bitcount = 8;
xmitlen = 0;
DBG_C('\n');
DBG_C('R');
DBG_OUT();
}
/* ISRs cannot be interrupted, so this saves 80 bytes */
#define state *(uint8_t *)&state
// Timer interrupt routine
ISR (TIMER0_OVF_vect)
{
uint8_t pin, st = state & S_MASK;
pin = OWPIN & (1 << ONEWIREPIN);
clear_timer();
//DBG_C(pin ? '!' : ':');
if (state & S_XMIT2) {
state &= ~S_XMIT2;
IFR |= (1 << INTF0);
IMSK |= (1 << INT0);
OWDDR &= ~(1 << ONEWIREPIN); // set to input
//DBG_C('x');
goto end;
}
if (st == S_RESET) { // send a presence pulse
OWDDR |= (1 << ONEWIREPIN);
state = S_PRESENCEPULSE;
set_timer(T_PRESENCE);
DBG_C('P');
goto end;
}
if (st == S_PRESENCEPULSE) {
OWDDR &= ~(1 << ONEWIREPIN); // Presence pulse done
state = S_RECEIVE_ROMCODE; // wait for command
DBG_C('O');
goto end;
}
if (!(st & S_RECV)) { // any other non-receive situation is a NO-OP;
DBG_C('T'); // this really should not happen anyway
set_idle();
goto end;
}
#ifndef SKIP_SEARCH
if (st == S_SEARCHROM_R) {
//DBG_C((pin != 0) + '0');
if (((transbyte & 0x01) == 0) == (pin == 0)) { // non-match => exit
// remember that S_SEARCHMEM_I left the bit inverted
DBG_C('-');
//DBG_X(transbyte);
set_idle();
goto end;
}
//DBG_C(' ');
transbyte >>= 1;
state = S_SEARCHROM;
if(!--bitcount) {
bitcount = 8;
if(xmitlen) {
transbyte = addr[--xmitlen];
//DBG_C('?');
//DBG_X(transbyte);
} else {
state = S_RECEIVE_OPCODE;
DBG_C('C');
}
}
goto end;
}
#endif
transbyte >>= 1;
if (pin)
transbyte |= 0x80;
if(--bitcount)
goto end;
bitcount = 8;
if (st == S_RECEIVE_ROMCODE) {
DBG_X(transbyte);
DBG_C(':');
if (transbyte == 0x55) {
state = S_MATCHROM;
xmitlen = 8;
}
else if (transbyte == 0xCC) {
// skip ROM; nothing to do, just wait for next command
state = S_RECEIVE_OPCODE;
DBG_C('C');
}
else if (transbyte == 0x33) {
state = S_READROM;
xmitlen = 8;
transbyte = addr[7];
}
else
#ifndef SKIP_SEARCH
if (transbyte == 0xF0) {
state = S_SEARCHROM;
xmitlen = 7;
transbyte = addr[7];
//DBG_C('?');
//DBG_X(transbyte);
//DBG_C(' ');
}
else
#endif
{
DBG_P("::Unknown ");
DBG_X(transbyte);
set_idle();
}
goto end;
}
if (st == S_RECEIVE_OPCODE) {
DBG_IN();
DBG_X(transbyte);
bitcount = 0;
state = S_HAS_OPCODE | (state & S_XMIT2);
goto end;
}
#ifndef SKIP_SEARCH
if (st == S_SEARCHROM) {
if (xmitlen) {
transbyte = addr[--xmitlen];
//DBG_C('?');
//DBG_X(transbyte);
}
else {
state = S_RECEIVE_OPCODE;
DBG_C('C');
}
goto end;
}
#endif
if (st == S_MATCHROM) {
if (transbyte != addr[--xmitlen]) {
DBG_C('M');
set_idle();
goto end;
}
if (xmitlen)
goto end;
state = S_RECEIVE_OPCODE;
DBG_C('C');
goto end;
}
if (st == S_CMD_RECV) {
bitcount = 0;
state = S_CMD_IDLE;
DBG_ON();
goto end;
}
// no idea what to do here, probably the main program was too slow
{
DBG_P("? rcv s");
DBG_X(state);
set_idle();
}
end:;
DBG_OFF();
}
// 1wire level change
ISR (INT0_vect)
{
uint8_t st = state & S_MASK;
if (OWPIN & (1 << ONEWIREPIN)) { // low => high transition
DBG_TS();
//DBG_C('^');
#ifdef HAVE_TIMESTAMP
TCCR1B &=~ (1<<ICES1);
#endif
if (((TCNT0 < 0xF0)||(st == S_IDLE)) && (TCNT0 > T_RESET)) { // Reset pulse seen
set_timer(T_PRESENCEWAIT);
set_reset();
} // else do nothing special; the timer will read the state
goto end;
}
//TIFR0 |= (1 << TOV0); // clear timer IRQ
DBG_TS();
//DBG_C('_');
#ifdef HAVE_TIMESTAMP
TCCR1B |= (1<<ICES1);
#endif
if (st & S_XMIT) {
if ((transbyte & 0x01) == 0) {
IMSK &= ~(1 << INT0);
OWDDR |= (1 << ONEWIREPIN); // send zero
set_timer(T_XMIT);
state |= S_XMIT2;
} else
clear_timer();
#ifndef SKIP_SEARCH
if (st == S_SEARCHROM) {
//DBG_C((transbyte & 0x01) + '0');
transbyte ^= 0x01;
state = S_SEARCHROM_I | (state & S_XMIT2);
goto end;
} else if (st == S_SEARCHROM_I) {
//DBG_C((transbyte & 0x01) + '0');
// note: we leave the bit flipped here
// and check for equality later
state = S_SEARCHROM_R | (state & S_XMIT2);
goto end;
}
#endif
transbyte >>= 1;
if (--bitcount)
goto end;
if (st == S_READROM) {
if (xmitlen) {
bitcount = 8;
transbyte = addr[--xmitlen];
DBG_C('<');
DBG_X(transbyte);
} else {
state = S_RECEIVE_OPCODE | (state & S_XMIT2);
DBG_C('C');
}
}
else if (st == S_CMD_XMIT) {
state = S_CMD_IDLE | (state & S_XMIT2);
DBG_ON();
}
else {
DBG_P("\nxmit: bad state\n");
set_idle();
}
goto end;
}
else if (st == S_CMD_IDLE) {
DBG_C('x');
set_idle();
}
else if (st == S_IDLE) { // first 1-0 transition
clear_timer();
}
else if (state & S_RECV) {
set_timer(T_SAMPLE);
}
else if (st == S_RESET) { // somebody else sends their Presence pulse
state = S_RECEIVE_ROMCODE;
clear_timer();
}
else if (st == S_PRESENCEPULSE)
; // do nothing, this is our own presence pulse
else {
DBG_C('c');
set_idle();
}
end:;
}