correct pinout for protoboard
[Arduino] / libraries / OneWire / OneWire.h
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(__MK20DX128__)
65 #define PIN_TO_BASEREG(pin)             (portOutputRegister(pin))
66 #define PIN_TO_BITMASK(pin)             (1)
67 #define IO_REG_TYPE uint8_t
68 #define IO_REG_ASM
69 #define DIRECT_READ(base, mask)         (*((base)+512))
70 #define DIRECT_MODE_INPUT(base, mask)   (*((base)+640) = 0)
71 #define DIRECT_MODE_OUTPUT(base, mask)  (*((base)+640) = 1)
72 #define DIRECT_WRITE_LOW(base, mask)    (*((base)+256) = 1)
73 #define DIRECT_WRITE_HIGH(base, mask)   (*((base)+128) = 1)
74
75 #elif defined(__SAM3X8E__)
76 // Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due.
77 // http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268
78 // If you have trouble with OneWire on Arduino Due, please check the
79 // status of delayMicroseconds() before reporting a bug in OneWire!
80 #define PIN_TO_BASEREG(pin)             (&(digitalPinToPort(pin)->PIO_PER))
81 #define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
82 #define IO_REG_TYPE uint32_t
83 #define IO_REG_ASM
84 #define DIRECT_READ(base, mask)         (((*((base)+15)) & (mask)) ? 1 : 0)
85 #define DIRECT_MODE_INPUT(base, mask)   ((*((base)+5)) = (mask))
86 #define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+4)) = (mask))
87 #define DIRECT_WRITE_LOW(base, mask)    ((*((base)+13)) = (mask))
88 #define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+12)) = (mask))
89 #ifndef PROGMEM
90 #define PROGMEM
91 #endif
92 #ifndef pgm_read_byte
93 #define pgm_read_byte(addr) (*(const uint8_t *)(addr))
94 #endif
95
96 #elif defined(__PIC32MX__)
97 #define PIN_TO_BASEREG(pin)             (portModeRegister(digitalPinToPort(pin)))
98 #define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
99 #define IO_REG_TYPE uint32_t
100 #define IO_REG_ASM
101 #define DIRECT_READ(base, mask)         (((*(base+4)) & (mask)) ? 1 : 0)  //PORTX + 0x10
102 #define DIRECT_MODE_INPUT(base, mask)   ((*(base+2)) = (mask))            //TRISXSET + 0x08
103 #define DIRECT_MODE_OUTPUT(base, mask)  ((*(base+1)) = (mask))            //TRISXCLR + 0x04
104 #define DIRECT_WRITE_LOW(base, mask)    ((*(base+8+1)) = (mask))          //LATXCLR  + 0x24
105 #define DIRECT_WRITE_HIGH(base, mask)   ((*(base+8+2)) = (mask))          //LATXSET + 0x28
106
107 #else
108 #error "Please define I/O register types here"
109 #endif
110
111
112 class OneWire
113 {
114   private:
115     IO_REG_TYPE bitmask;
116     volatile IO_REG_TYPE *baseReg;
117
118 #if ONEWIRE_SEARCH
119     // global search state
120     unsigned char ROM_NO[8];
121     uint8_t LastDiscrepancy;
122     uint8_t LastFamilyDiscrepancy;
123     uint8_t LastDeviceFlag;
124 #endif
125
126   public:
127     OneWire( uint8_t pin);
128
129     // Perform a 1-Wire reset cycle. Returns 1 if a device responds
130     // with a presence pulse.  Returns 0 if there is no device or the
131     // bus is shorted or otherwise held low for more than 250uS
132     uint8_t reset(void);
133
134     // Issue a 1-Wire rom select command, you do the reset first.
135     void select(const uint8_t rom[8]);
136
137     // Issue a 1-Wire rom skip command, to address all on bus.
138     void skip(void);
139
140     // Write a byte. If 'power' is one then the wire is held high at
141     // the end for parasitically powered devices. You are responsible
142     // for eventually depowering it by calling depower() or doing
143     // another read or write.
144     void write(uint8_t v, uint8_t power = 0);
145
146     void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0);
147
148     // Read a byte.
149     uint8_t read(void);
150
151     void read_bytes(uint8_t *buf, uint16_t count);
152
153     // Write a bit. The bus is always left powered at the end, see
154     // note in write() about that.
155     void write_bit(uint8_t v);
156
157     // Read a bit.
158     uint8_t read_bit(void);
159
160     // Stop forcing power onto the bus. You only need to do this if
161     // you used the 'power' flag to write() or used a write_bit() call
162     // and aren't about to do another read or write. You would rather
163     // not leave this powered if you don't have to, just in case
164     // someone shorts your bus.
165     void depower(void);
166
167 #if ONEWIRE_SEARCH
168     // Clear the search state so that if will start from the beginning again.
169     void reset_search();
170
171     // Setup the search to find the device type 'family_code' on the next call
172     // to search(*newAddr) if it is present.
173     void target_search(uint8_t family_code);
174
175     // Look for the next device. Returns 1 if a new address has been
176     // returned. A zero might mean that the bus is shorted, there are
177     // no devices, or you have already retrieved all of them.  It
178     // might be a good idea to check the CRC to make sure you didn't
179     // get garbage.  The order is deterministic. You will always get
180     // the same devices in the same order.
181     uint8_t search(uint8_t *newAddr);
182 #endif
183
184 #if ONEWIRE_CRC
185     // Compute a Dallas Semiconductor 8 bit CRC, these are used in the
186     // ROM and scratchpad registers.
187     static uint8_t crc8(const uint8_t *addr, uint8_t len);
188
189 #if ONEWIRE_CRC16
190     // Compute the 1-Wire CRC16 and compare it against the received CRC.
191     // Example usage (reading a DS2408):
192     //    // Put everything in a buffer so we can compute the CRC easily.
193     //    uint8_t buf[13];
194     //    buf[0] = 0xF0;    // Read PIO Registers
195     //    buf[1] = 0x88;    // LSB address
196     //    buf[2] = 0x00;    // MSB address
197     //    WriteBytes(net, buf, 3);    // Write 3 cmd bytes
198     //    ReadBytes(net, buf+3, 10);  // Read 6 data bytes, 2 0xFF, 2 CRC16
199     //    if (!CheckCRC16(buf, 11, &buf[11])) {
200     //        // Handle error.
201     //    }     
202     //          
203     // @param input - Array of bytes to checksum.
204     // @param len - How many bytes to use.
205     // @param inverted_crc - The two CRC16 bytes in the received data.
206     //                       This should just point into the received data,
207     //                       *not* at a 16-bit integer.
208     // @param crc - The crc starting value (optional)
209     // @return True, iff the CRC matches.
210     static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0);
211
212     // Compute a Dallas Semiconductor 16 bit CRC.  This is required to check
213     // the integrity of data received from many 1-Wire devices.  Note that the
214     // CRC computed here is *not* what you'll get from the 1-Wire network,
215     // for two reasons:
216     //   1) The CRC is transmitted bitwise inverted.
217     //   2) Depending on the endian-ness of your processor, the binary
218     //      representation of the two-byte return value may have a different
219     //      byte order than the two bytes you get from 1-Wire.
220     // @param input - Array of bytes to checksum.
221     // @param len - How many bytes to use.
222     // @param crc - The crc starting value (optional)
223     // @return The CRC16, as defined by Dallas Semiconductor.
224     static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0);
225 #endif
226 #endif
227 };
228
229 #endif