annotate service/arduinoNode/arduino-libraries/OneWire/OneWire.h @ 1754:92999dfbf321 default tip

add shelly support
author drewp@bigasterisk.com
date Tue, 04 Jun 2024 13:03:43 -0700
parents af4e9d9f0bd8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
165
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
1 #ifndef OneWire_h
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
2 #define OneWire_h
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
3
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
4 #include <inttypes.h>
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
5
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
6 #if ARDUINO >= 100
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
7 #include "Arduino.h" // for delayMicroseconds, digitalPinToBitMask, etc
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
8 #else
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
9 #include "WProgram.h" // for delayMicroseconds
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
10 #include "pins_arduino.h" // for digitalPinToBitMask, etc
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
11 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
12
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
13 // You can exclude certain features from OneWire. In theory, this
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
14 // might save some space. In practice, the compiler automatically
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
15 // removes unused code (technically, the linker, using -fdata-sections
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
16 // and -ffunction-sections when compiling, and Wl,--gc-sections
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
17 // when linking), so most of these will not result in any code size
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
18 // reduction. Well, unless you try to use the missing features
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
19 // and redesign your program to not need them! ONEWIRE_CRC8_TABLE
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
20 // is the exception, because it selects a fast but large algorithm
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
21 // or a small but slow algorithm.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
22
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
23 // you can exclude onewire_search by defining that to 0
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
24 #ifndef ONEWIRE_SEARCH
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
25 #define ONEWIRE_SEARCH 1
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
26 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
27
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
28 // You can exclude CRC checks altogether by defining this to 0
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
29 #ifndef ONEWIRE_CRC
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
30 #define ONEWIRE_CRC 1
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
31 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
32
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
33 // Select the table-lookup method of computing the 8-bit CRC
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
34 // by setting this to 1. The lookup table enlarges code size by
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
35 // about 250 bytes. It does NOT consume RAM (but did in very
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
36 // old versions of OneWire). If you disable this, a slower
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
37 // but very compact algorithm is used.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
38 #ifndef ONEWIRE_CRC8_TABLE
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
39 #define ONEWIRE_CRC8_TABLE 1
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
40 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
41
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
42 // You can allow 16-bit CRC checks by defining this to 1
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
43 // (Note that ONEWIRE_CRC must also be 1.)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
44 #ifndef ONEWIRE_CRC16
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
45 #define ONEWIRE_CRC16 1
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
46 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
47
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
48 #define FALSE 0
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
49 #define TRUE 1
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
50
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
51 // Platform specific I/O definitions
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
52
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
53 #if defined(__AVR__)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
54 #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
55 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
56 #define IO_REG_TYPE uint8_t
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
57 #define IO_REG_ASM asm("r30")
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
58 #define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
59 #define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
60 #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
61 #define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
62 #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
63
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
64 #elif defined(__MK20DX128__)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
65 #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
66 #define PIN_TO_BITMASK(pin) (1)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
67 #define IO_REG_TYPE uint8_t
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
68 #define IO_REG_ASM
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
69 #define DIRECT_READ(base, mask) (*((base)+512))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
70 #define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
71 #define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
72 #define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
73 #define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
74
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
75 #elif defined(__SAM3X8E__)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
76 // Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
77 // http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
78 // If you have trouble with OneWire on Arduino Due, please check the
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
79 // status of delayMicroseconds() before reporting a bug in OneWire!
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
80 #define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
81 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
82 #define IO_REG_TYPE uint32_t
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
83 #define IO_REG_ASM
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
84 #define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
85 #define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
86 #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
87 #define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
88 #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
89 #ifndef PROGMEM
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
90 #define PROGMEM
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
91 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
92 #ifndef pgm_read_byte
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
93 #define pgm_read_byte(addr) (*(const uint8_t *)(addr))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
94 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
95
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
96 #elif defined(__PIC32MX__)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
97 #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin)))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
98 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
99 #define IO_REG_TYPE uint32_t
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
100 #define IO_REG_ASM
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
101 #define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
102 #define DIRECT_MODE_INPUT(base, mask) ((*(base+2)) = (mask)) //TRISXSET + 0x08
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
103 #define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) = (mask)) //TRISXCLR + 0x04
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
104 #define DIRECT_WRITE_LOW(base, mask) ((*(base+8+1)) = (mask)) //LATXCLR + 0x24
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
105 #define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
106
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
107 #else
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
108 #error "Please define I/O register types here"
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
109 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
110
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
111
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
112 class OneWire
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
113 {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
114 private:
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
115 IO_REG_TYPE bitmask;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
116 volatile IO_REG_TYPE *baseReg;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
117
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
118 #if ONEWIRE_SEARCH
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
119 // global search state
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
120 unsigned char ROM_NO[8];
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
121 uint8_t LastDiscrepancy;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
122 uint8_t LastFamilyDiscrepancy;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
123 uint8_t LastDeviceFlag;
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
124 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
125
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
126 public:
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
127 OneWire( uint8_t pin);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
128
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
129 // Perform a 1-Wire reset cycle. Returns 1 if a device responds
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
130 // with a presence pulse. Returns 0 if there is no device or the
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
131 // bus is shorted or otherwise held low for more than 250uS
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
132 uint8_t reset(void);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
133
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
134 // Issue a 1-Wire rom select command, you do the reset first.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
135 void select(const uint8_t rom[8]);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
136
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
137 // Issue a 1-Wire rom skip command, to address all on bus.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
138 void skip(void);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
139
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
140 // Write a byte. If 'power' is one then the wire is held high at
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
141 // the end for parasitically powered devices. You are responsible
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
142 // for eventually depowering it by calling depower() or doing
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
143 // another read or write.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
144 void write(uint8_t v, uint8_t power = 0);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
145
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
146 void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
147
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
148 // Read a byte.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
149 uint8_t read(void);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
150
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
151 void read_bytes(uint8_t *buf, uint16_t count);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
152
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
153 // Write a bit. The bus is always left powered at the end, see
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
154 // note in write() about that.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
155 void write_bit(uint8_t v);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
156
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
157 // Read a bit.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
158 uint8_t read_bit(void);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
159
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
160 // Stop forcing power onto the bus. You only need to do this if
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
161 // you used the 'power' flag to write() or used a write_bit() call
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
162 // and aren't about to do another read or write. You would rather
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
163 // not leave this powered if you don't have to, just in case
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
164 // someone shorts your bus.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
165 void depower(void);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
166
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
167 #if ONEWIRE_SEARCH
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
168 // Clear the search state so that if will start from the beginning again.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
169 void reset_search();
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
170
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
171 // Setup the search to find the device type 'family_code' on the next call
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
172 // to search(*newAddr) if it is present.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
173 void target_search(uint8_t family_code);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
174
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
175 // Look for the next device. Returns 1 if a new address has been
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
176 // returned. A zero might mean that the bus is shorted, there are
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
177 // no devices, or you have already retrieved all of them. It
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
178 // might be a good idea to check the CRC to make sure you didn't
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
179 // get garbage. The order is deterministic. You will always get
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
180 // the same devices in the same order.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
181 uint8_t search(uint8_t *newAddr);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
182 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
183
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
184 #if ONEWIRE_CRC
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
185 // Compute a Dallas Semiconductor 8 bit CRC, these are used in the
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
186 // ROM and scratchpad registers.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
187 static uint8_t crc8(const uint8_t *addr, uint8_t len);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
188
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
189 #if ONEWIRE_CRC16
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
190 // Compute the 1-Wire CRC16 and compare it against the received CRC.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
191 // Example usage (reading a DS2408):
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
192 // // Put everything in a buffer so we can compute the CRC easily.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
193 // uint8_t buf[13];
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
194 // buf[0] = 0xF0; // Read PIO Registers
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
195 // buf[1] = 0x88; // LSB address
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
196 // buf[2] = 0x00; // MSB address
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
197 // WriteBytes(net, buf, 3); // Write 3 cmd bytes
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
198 // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
199 // if (!CheckCRC16(buf, 11, &buf[11])) {
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
200 // // Handle error.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
201 // }
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
202 //
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
203 // @param input - Array of bytes to checksum.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
204 // @param len - How many bytes to use.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
205 // @param inverted_crc - The two CRC16 bytes in the received data.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
206 // This should just point into the received data,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
207 // *not* at a 16-bit integer.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
208 // @param crc - The crc starting value (optional)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
209 // @return True, iff the CRC matches.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
210 static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
211
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
212 // Compute a Dallas Semiconductor 16 bit CRC. This is required to check
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
213 // the integrity of data received from many 1-Wire devices. Note that the
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
214 // CRC computed here is *not* what you'll get from the 1-Wire network,
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
215 // for two reasons:
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
216 // 1) The CRC is transmitted bitwise inverted.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
217 // 2) Depending on the endian-ness of your processor, the binary
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
218 // representation of the two-byte return value may have a different
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
219 // byte order than the two bytes you get from 1-Wire.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
220 // @param input - Array of bytes to checksum.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
221 // @param len - How many bytes to use.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
222 // @param crc - The crc starting value (optional)
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
223 // @return The CRC16, as defined by Dallas Semiconductor.
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
224 static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0);
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
225 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
226 #endif
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
227 };
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
228
af4e9d9f0bd8 some external arduino libs, minus examples and docs
drewp@bigasterisk.com
parents:
diff changeset
229 #endif