Adafruit MAX31850 OneWire Library
OneWire.h
Go to the documentation of this file.
1 
4 #ifndef OneWire_h
5 #define OneWire_h
6 
7 #include <inttypes.h>
8 
9 #if ARDUINO >= 100
10 #include "Arduino.h" // for delayMicroseconds, digitalPinToBitMask, etc
11 #else
12 #include "WProgram.h" // for delayMicroseconds
13 #include "pins_arduino.h" // for digitalPinToBitMask, etc
14 #endif
15 
16 // You can exclude certain features from OneWire. In theory, this
17 // might save some space. In practice, the compiler automatically
18 // removes unused code (technically, the linker, using -fdata-sections
19 // and -ffunction-sections when compiling, and Wl,--gc-sections
20 // when linking), so most of these will not result in any code size
21 // reduction. Well, unless you try to use the missing features
22 // and redesign your program to not need them! ONEWIRE_CRC8_TABLE
23 // is the exception, because it selects a fast but large algorithm
24 // or a small but slow algorithm.
25 
26 // you can exclude onewire_search by defining that to 0
27 #ifndef ONEWIRE_SEARCH
28 #define ONEWIRE_SEARCH 1
29 #endif
30 
31 // You can exclude CRC checks altogether by defining this to 0
32 #ifndef ONEWIRE_CRC
33 #define ONEWIRE_CRC 1
34 #endif
35 
36 // Select the table-lookup method of computing the 8-bit CRC
37 // by setting this to 1. The lookup table enlarges code size by
38 // about 250 bytes. It does NOT consume RAM (but did in very
39 // old versions of OneWire). If you disable this, a slower
40 // but very compact algorithm is used.
41 #ifndef ONEWIRE_CRC8_TABLE
42 #define ONEWIRE_CRC8_TABLE \
43  1
44 #endif
46 
47 // You can allow 16-bit CRC checks by defining this to 1
48 // (Note that ONEWIRE_CRC must also be 1.)
49 #ifndef ONEWIRE_CRC16
50 #define ONEWIRE_CRC16 \
51  1
52 #endif
53 
54 #define FALSE 0
55 #define TRUE 1
56 
57 // Platform specific I/O definitions
58 
59 #if defined(__AVR__)
60 #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
61 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
62 #define IO_REG_TYPE uint8_t
63 #define IO_REG_ASM asm("r30")
64 #define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
65 #define DIRECT_MODE_INPUT(base, mask) ((*((base) + 1)) &= ~(mask))
66 #define DIRECT_MODE_OUTPUT(base, mask) ((*((base) + 1)) |= (mask))
67 #define DIRECT_WRITE_LOW(base, mask) ((*((base) + 2)) &= ~(mask))
68 #define DIRECT_WRITE_HIGH(base, mask) ((*((base) + 2)) |= (mask))
69 
70 #elif defined(__MK20DX128__)
71 #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
72 #define PIN_TO_BITMASK(pin) (1)
73 #define IO_REG_TYPE uint8_t
74 #define IO_REG_ASM
75 #define DIRECT_READ(base, mask) (*((base) + 512))
76 #define DIRECT_MODE_INPUT(base, mask) (*((base) + 640) = 0)
77 #define DIRECT_MODE_OUTPUT(base, mask) (*((base) + 640) = 1)
78 #define DIRECT_WRITE_LOW(base, mask) (*((base) + 256) = 1)
79 #define DIRECT_WRITE_HIGH(base, mask) (*((base) + 128) = 1)
80 
81 #elif defined(__SAM3X8E__)
82 // Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due.
83 // http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268
84 // If you have trouble with OneWire on Arduino Due, please check the
85 // status of delayMicroseconds() before reporting a bug in OneWire!
86 #define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER))
87 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
88 #define IO_REG_TYPE uint32_t
89 #define IO_REG_ASM
90 #define DIRECT_READ(base, mask) (((*((base) + 15)) & (mask)) ? 1 : 0)
91 #define DIRECT_MODE_INPUT(base, mask) ((*((base) + 5)) = (mask))
92 #define DIRECT_MODE_OUTPUT(base, mask) ((*((base) + 4)) = (mask))
93 #define DIRECT_WRITE_LOW(base, mask) ((*((base) + 13)) = (mask))
94 #define DIRECT_WRITE_HIGH(base, mask) ((*((base) + 12)) = (mask))
95 #ifndef PROGMEM
96 #define PROGMEM
97 #endif
98 #ifndef pgm_read_byte
99 #define pgm_read_byte(addr) (*(const uint8_t *)(addr))
100 #endif
101 
102 #elif defined(__PIC32MX__)
103 #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin)))
104 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
105 #define IO_REG_TYPE uint32_t
106 #define IO_REG_ASM
107 #define DIRECT_READ(base, mask) \
108  (((*(base + 4)) & (mask)) ? 1 : 0) // PORTX + 0x10
109 #define DIRECT_MODE_INPUT(base, mask) \
110  ((*(base + 2)) = (mask)) // TRISXSET +
111  // 0x08
112 #define DIRECT_MODE_OUTPUT(base, mask) \
113  ((*(base + 1)) = (mask)) // TRISXCLR + 0x04
114 #define DIRECT_WRITE_LOW(base, mask) \
115  ((*(base + 8 + 1)) = (mask)) // LATXCLR + 0x24
116 #define DIRECT_WRITE_HIGH(base, mask) \
117  ((*(base + 8 + 2)) = (mask)) // LATXSET + 0x28
118 
119 #elif defined(ARDUINO_ARCH_ESP8266)
120 #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)GPO)
121 #define PIN_TO_BITMASK(pin) (1 << pin)
122 #define IO_REG_TYPE uint32_t
123 #define IO_REG_ASM
124 #define DIRECT_READ(base, mask) ((GPI & (mask)) ? 1 : 0) // GPIO_IN_ADDRESS
125 #define DIRECT_MODE_INPUT(base, mask) \
126  (GPE &= ~(mask)) // GPIO_ENABLE_W1TC_ADDRESS
127 #define DIRECT_MODE_OUTPUT(base, mask) \
128  (GPE |= (mask)) // GPIO_ENABLE_W1TS_ADDRESS
129 #define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) // GPIO_OUT_W1TC_ADDRESS
130 #define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) // GPIO_OUT_W1TS_ADDRESS
131 
132 #elif defined(__SAMD21G18A__) || defined(__SAMD21E18A__)
133 #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin))
134 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
135 #define IO_REG_TYPE uint32_t
136 #define IO_REG_ASM
137 #define DIRECT_READ(base, mask) (((*((base) + 8)) & (mask)) ? 1 : 0)
138 #define DIRECT_MODE_INPUT(base, mask) ((*((base) + 1)) = (mask))
139 #define DIRECT_MODE_OUTPUT(base, mask) ((*((base) + 2)) = (mask))
140 #define DIRECT_WRITE_LOW(base, mask) ((*((base) + 5)) = (mask))
141 #define DIRECT_WRITE_HIGH(base, mask) ((*((base) + 6)) = (mask))
142 
143 #elif defined(__SAMD51__)
144 #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin))
145 #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
146 #define IO_REG_TYPE uint32_t
147 #define IO_REG_ASM
148 #define DIRECT_READ(base, mask) (((*((base) + 8)) & (mask)) ? 1 : 0) // IN
149 #define DIRECT_MODE_INPUT(base, mask) ((*((base) + 1)) = (mask)) // DIRCLR
150 #define DIRECT_MODE_OUTPUT(base, mask) ((*((base) + 2)) = (mask)) // DIRSET
151 #define DIRECT_WRITE_LOW(base, mask) ((*((base) + 5)) = (mask)) // OUTCLR
152 #define DIRECT_WRITE_HIGH(base, mask) ((*((base) + 6)) = (mask))
153 
154 #elif defined(ARDUINO_NRF52_ADAFRUIT)
155 #define PIN_TO_BASEREG(pin) (0)
156 #define PIN_TO_BITMASK(pin) digitalPinToPinName(pin)
157 #define IO_REG_TYPE uint32_t
158 #define IO_REG_ASM
159 #define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin)
160 #define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin)
161 #define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin)
162 #define DIRECT_MODE_INPUT(base, pin) \
163  nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL)
164 #define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin)
165 
166 #else
167 #error "Please define I/O register types here"
168 #endif
169 
173 class OneWire {
174 private:
175  IO_REG_TYPE bitmask;
176  volatile IO_REG_TYPE *baseReg;
177 
178 #if ONEWIRE_SEARCH
179  // global search state
180  unsigned char ROM_NO[8];
181  uint8_t LastDiscrepancy;
182  uint8_t LastFamilyDiscrepancy;
183  uint8_t LastDeviceFlag;
184 #endif
185 
186 public:
191  OneWire(uint8_t pin);
192 
199  uint8_t reset(void);
200 
205  void select(const uint8_t rom[8]);
206 
210  void skip(void);
211 
220  void write(uint8_t v, uint8_t power = 0);
221 
228  void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0);
229 
234  uint8_t read(void);
235 
241  void read_bytes(uint8_t *buf, uint16_t count);
242 
248  void write_bit(uint8_t v);
249 
254  uint8_t read_bit(void);
255 
263  void depower(void);
264 
265 #if ONEWIRE_SEARCH
266 
270  void reset_search();
271 
277  void target_search(uint8_t family_code);
278 
289  uint8_t search(uint8_t *newAddr);
290 #endif
291 
292 #if ONEWIRE_CRC
293 
300  static uint8_t crc8(const uint8_t *addr, uint8_t len);
301 
302 #if ONEWIRE_CRC16
303 
325  static bool check_crc16(const uint8_t *input, uint16_t len,
326  const uint8_t *inverted_crc, uint16_t crc = 0);
327 
341  static uint16_t crc16(const uint8_t *input, uint16_t len, uint16_t crc = 0);
342 #endif
343 #endif
344 };
345 
346 #endif
void select(const uint8_t rom[8])
Issue a 1-Wire rom select command, you do the reset first.
Definition: OneWire.cpp:271
static bool check_crc16(const uint8_t *input, uint16_t len, const uint8_t *inverted_crc, uint16_t crc=0)
Compute the 1-Wire CRC16 and compare it against the received CRC. Example usage (reading a DS2408): /...
Definition: OneWire.cpp:519
void write_bit(uint8_t v)
Write a bit. The bus is always left powered at the end, see note in write() about that...
Definition: OneWire.cpp:172
void write_bytes(const uint8_t *buf, uint16_t count, bool power=0)
Writes bytes.
Definition: OneWire.cpp:237
static uint8_t crc8(const uint8_t *addr, uint8_t len)
Compute a Dallas Semiconductor 8 bit CRC, these are used in the ROM and scratchpad registers...
Definition: OneWire.cpp:488
uint8_t read_bit(void)
Read a bit.
Definition: OneWire.cpp:199
uint8_t reset(void)
Perform a 1-Wire reset cycle. Returns 1 if a device responds with a presence pulse. Returns 0 if there is no device or the bus is shorted or otherwise held low for more than 250uS.
Definition: OneWire.cpp:138
void skip(void)
Issue a 1-Wire rom skip command, to address all on bus.
Definition: OneWire.cpp:283
uint8_t read(void)
Read a byte.
Definition: OneWire.cpp:252
void depower(void)
Stop forcing power onto the bus. You only need to do this if you used the &#39;power&#39; flag to write() or ...
Definition: OneWire.cpp:287
void reset_search()
Clear the search state so that if will start from the beginning again.
Definition: OneWire.cpp:299
Stores the state and functions for Max31850 OneWire.
Definition: OneWire.h:173
void target_search(uint8_t family_code)
Setup the search to find the device type &#39;family_code&#39; on the next call to search(*newAddr) if it is ...
Definition: OneWire.cpp:314
uint8_t search(uint8_t *newAddr)
Look for the next device. Returns 1 if a new address has been returned. A zero might mean that the bu...
Definition: OneWire.cpp:340
OneWire(uint8_t pin)
OneWire constructor.
Definition: OneWire.cpp:123
static uint16_t crc16(const uint8_t *input, uint16_t len, uint16_t crc=0)
Compute a Dallas Semiconductor 16 bit CRC. This is required to check the integrity of data received f...
Definition: OneWire.cpp:525
void read_bytes(uint8_t *buf, uint16_t count)
Reads bytes.
Definition: OneWire.cpp:263
void write(uint8_t v, uint8_t power=0)
Write a byte. If &#39;power&#39; is one then the wire is held high at the end for parasitically powered devic...
Definition: OneWire.cpp:223