BayEOS-Arduino  1.8.0_0.0.4
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 #ifndef FALSE
49 #define FALSE 0
50 #endif
51 #ifndef TRUE
52 #define TRUE 1
53 #endif
54 
55 // Platform specific I/O definitions
56 
57 #if defined(__AVR__)
58 #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
59 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
60 #define IO_REG_TYPE uint8_t
61 #define IO_REG_ASM asm("r30")
62 #define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
63 #define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask))
64 #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask))
65 #define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask))
66 #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask))
67 
68 #elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__)
69 #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
70 #define PIN_TO_BITMASK(pin) (1)
71 #define IO_REG_TYPE uint8_t
72 #define IO_REG_ASM
73 #define DIRECT_READ(base, mask) (*((base)+512))
74 #define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0)
75 #define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1)
76 #define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1)
77 #define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1)
78 
79 #elif defined(__MKL26Z64__)
80 #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
81 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
82 #define IO_REG_TYPE uint8_t
83 #define IO_REG_ASM
84 #define DIRECT_READ(base, mask) ((*((base)+16) & (mask)) ? 1 : 0)
85 #define DIRECT_MODE_INPUT(base, mask) (*((base)+20) &= ~(mask))
86 #define DIRECT_MODE_OUTPUT(base, mask) (*((base)+20) |= (mask))
87 #define DIRECT_WRITE_LOW(base, mask) (*((base)+8) = (mask))
88 #define DIRECT_WRITE_HIGH(base, mask) (*((base)+4) = (mask))
89 
90 #elif defined(__SAM3X8E__)
91 // Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due.
92 // http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268
93 // If you have trouble with OneWire on Arduino Due, please check the
94 // status of delayMicroseconds() before reporting a bug in OneWire!
95 #define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER))
96 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
97 #define IO_REG_TYPE uint32_t
98 #define IO_REG_ASM
99 #define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0)
100 #define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask))
101 #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask))
102 #define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask))
103 #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask))
104 #ifndef PROGMEM
105 #define PROGMEM
106 #endif
107 #ifndef pgm_read_byte
108 #define pgm_read_byte(addr) (*(const uint8_t *)(addr))
109 #endif
110 
111 #elif defined(__PIC32MX__)
112 #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin)))
113 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
114 #define IO_REG_TYPE uint32_t
115 #define IO_REG_ASM
116 #define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10
117 #define DIRECT_MODE_INPUT(base, mask) ((*(base+2)) = (mask)) //TRISXSET + 0x08
118 #define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) = (mask)) //TRISXCLR + 0x04
119 #define DIRECT_WRITE_LOW(base, mask) ((*(base+8+1)) = (mask)) //LATXCLR + 0x24
120 #define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28
121 
122 #elif defined(ARDUINO_ARCH_ESP8266)
123 #define PIN_TO_BASEREG(pin) ((volatile uint32_t*) GPO)
124 #define PIN_TO_BITMASK(pin) (1 << pin)
125 #define IO_REG_TYPE uint32_t
126 #define IO_REG_ASM
127 #define DIRECT_READ(base, mask) ((GPI & (mask)) ? 1 : 0) //GPIO_IN_ADDRESS
128 #define DIRECT_MODE_INPUT(base, mask) (GPE &= ~(mask)) //GPIO_ENABLE_W1TC_ADDRESS
129 #define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS
130 #define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS
131 #define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS
132 
133 #elif defined(__SAMD21G18A__)
134 #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin))
135 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
136 #define IO_REG_TYPE uint32_t
137 #define IO_REG_ASM
138 #define DIRECT_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0)
139 #define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) = (mask))
140 #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask))
141 #define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask))
142 #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask))
143 
144 #elif defined(RBL_NRF51822)
145 #define PIN_TO_BASEREG(pin) (0)
146 #define PIN_TO_BITMASK(pin) (pin)
147 #define IO_REG_TYPE uint32_t
148 #define IO_REG_ASM
149 #define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin)
150 #define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin)
151 #define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin)
152 #define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL)
153 #define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin)
154 
155 #elif defined(__arc__) /* Arduino101/Genuino101 specifics */
156 
157 #include "scss_registers.h"
158 #include "portable.h"
159 #include "avr/pgmspace.h"
160 
161 #define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId)
162 #define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType)
163 #define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase)
164 #define DIR_OFFSET_SS 0x01
165 #define DIR_OFFSET_SOC 0x04
166 #define EXT_PORT_OFFSET_SS 0x0A
167 #define EXT_PORT_OFFSET_SOC 0x50
168 
169 /* GPIO registers base address */
170 #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase)
171 #define PIN_TO_BITMASK(pin) pin
172 #define IO_REG_TYPE uint32_t
173 #define IO_REG_ASM
174 
175 static inline __attribute__((always_inline))
176 IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
177 {
178  IO_REG_TYPE ret;
179  if (SS_GPIO == GPIO_TYPE(pin)) {
180  ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS));
181  } else {
182  ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC);
183  }
184  return ((ret >> GPIO_ID(pin)) & 0x01);
185 }
186 
187 static inline __attribute__((always_inline))
188 void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
189 {
190  if (SS_GPIO == GPIO_TYPE(pin)) {
191  WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)),
192  ((IO_REG_TYPE)(base) + DIR_OFFSET_SS));
193  } else {
194  MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin));
195  }
196 }
197 
198 static inline __attribute__((always_inline))
199 void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
200 {
201  if (SS_GPIO == GPIO_TYPE(pin)) {
202  WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)),
203  ((IO_REG_TYPE)(base) + DIR_OFFSET_SS));
204  } else {
205  MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin));
206  }
207 }
208 
209 static inline __attribute__((always_inline))
210 void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
211 {
212  if (SS_GPIO == GPIO_TYPE(pin)) {
213  WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base);
214  } else {
215  MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin));
216  }
217 }
218 
219 static inline __attribute__((always_inline))
220 void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
221 {
222  if (SS_GPIO == GPIO_TYPE(pin)) {
223  WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base);
224  } else {
225  MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin));
226  }
227 }
228 
229 #define DIRECT_READ(base, pin) directRead(base, pin)
230 #define DIRECT_MODE_INPUT(base, pin) directModeInput(base, pin)
231 #define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(base, pin)
232 #define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin)
233 #define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(base, pin)
234 
235 #else
236 #define PIN_TO_BASEREG(pin) (0)
237 #define PIN_TO_BITMASK(pin) (pin)
238 #define IO_REG_TYPE unsigned int
239 #define IO_REG_ASM
240 #define DIRECT_READ(base, pin) digitalRead(pin)
241 #define DIRECT_WRITE_LOW(base, pin) digitalWrite(pin, LOW)
242 #define DIRECT_WRITE_HIGH(base, pin) digitalWrite(pin, HIGH)
243 #define DIRECT_MODE_INPUT(base, pin) pinMode(pin,INPUT)
244 #define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT)
245 #warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture."
246 
247 #endif
248 
249 
250 class OneWire
251 {
252  private:
253  IO_REG_TYPE bitmask;
254  volatile IO_REG_TYPE *baseReg;
255 
256 #if ONEWIRE_SEARCH
257  // global search state
258  unsigned char ROM_NO[8];
259  uint8_t LastDiscrepancy;
260  uint8_t LastFamilyDiscrepancy;
261  uint8_t LastDeviceFlag;
262 #endif
263 
264  public:
265  OneWire( uint8_t pin);
266 
267  // Perform a 1-Wire reset cycle. Returns 1 if a device responds
268  // with a presence pulse. Returns 0 if there is no device or the
269  // bus is shorted or otherwise held low for more than 250uS
270  uint8_t reset(void);
271 
272  // Issue a 1-Wire rom select command, you do the reset first.
273  void select(const uint8_t rom[8]);
274 
275  // Issue a 1-Wire rom skip command, to address all on bus.
276  void skip(void);
277 
278  // Write a byte. If 'power' is one then the wire is held high at
279  // the end for parasitically powered devices. You are responsible
280  // for eventually depowering it by calling depower() or doing
281  // another read or write.
282  void write(uint8_t v, uint8_t power = 0);
283 
284  void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0);
285 
286  // Read a byte.
287  uint8_t read(void);
288 
289  void read_bytes(uint8_t *buf, uint16_t count);
290 
291  // Write a bit. The bus is always left powered at the end, see
292  // note in write() about that.
293  void write_bit(uint8_t v);
294 
295  // Read a bit.
296  uint8_t read_bit(void);
297 
298  // Stop forcing power onto the bus. You only need to do this if
299  // you used the 'power' flag to write() or used a write_bit() call
300  // and aren't about to do another read or write. You would rather
301  // not leave this powered if you don't have to, just in case
302  // someone shorts your bus.
303  void depower(void);
304 
305 #if ONEWIRE_SEARCH
306  // Clear the search state so that if will start from the beginning again.
307  void reset_search();
308 
309  // Setup the search to find the device type 'family_code' on the next call
310  // to search(*newAddr) if it is present.
311  void target_search(uint8_t family_code);
312 
313  // Look for the next device. Returns 1 if a new address has been
314  // returned. A zero might mean that the bus is shorted, there are
315  // no devices, or you have already retrieved all of them. It
316  // might be a good idea to check the CRC to make sure you didn't
317  // get garbage. The order is deterministic. You will always get
318  // the same devices in the same order.
319  uint8_t search(uint8_t *newAddr, bool search_mode = true);
320 #endif
321 
322 #if ONEWIRE_CRC
323  // Compute a Dallas Semiconductor 8 bit CRC, these are used in the
324  // ROM and scratchpad registers.
325  static uint8_t crc8(const uint8_t *addr, uint8_t len);
326 
327 #if ONEWIRE_CRC16
328  // Compute the 1-Wire CRC16 and compare it against the received CRC.
329  // Example usage (reading a DS2408):
330  // // Put everything in a buffer so we can compute the CRC easily.
331  // uint8_t buf[13];
332  // buf[0] = 0xF0; // Read PIO Registers
333  // buf[1] = 0x88; // LSB address
334  // buf[2] = 0x00; // MSB address
335  // WriteBytes(net, buf, 3); // Write 3 cmd bytes
336  // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
337  // if (!CheckCRC16(buf, 11, &buf[11])) {
338  // // Handle error.
339  // }
340  //
341  // @param input - Array of bytes to checksum.
342  // @param len - How many bytes to use.
343  // @param inverted_crc - The two CRC16 bytes in the received data.
344  // This should just point into the received data,
345  // *not* at a 16-bit integer.
346  // @param crc - The crc starting value (optional)
347  // @return True, iff the CRC matches.
348  static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0);
349 
350  // Compute a Dallas Semiconductor 16 bit CRC. This is required to check
351  // the integrity of data received from many 1-Wire devices. Note that the
352  // CRC computed here is *not* what you'll get from the 1-Wire network,
353  // for two reasons:
354  // 1) The CRC is transmitted bitwise inverted.
355  // 2) Depending on the endian-ness of your processor, the binary
356  // representation of the two-byte return value may have a different
357  // byte order than the two bytes you get from 1-Wire.
358  // @param input - Array of bytes to checksum.
359  // @param len - How many bytes to use.
360  // @param crc - The crc starting value (optional)
361  // @return The CRC16, as defined by Dallas Semiconductor.
362  static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0);
363 #endif
364 #endif
365 };
366 
367 #endif