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