Skip to content

Commit 4491e11

Browse files
committed
Adding device driver for DS2482, 1-Wire Master Bridge
1 parent 60e6a0e commit 4491e11

File tree

5 files changed

+609
-7
lines changed

5 files changed

+609
-7
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
The TWI library is an abstract interface for I2C device drivers. The
44
library includes a hardware and software bus manager, and example
55
device drivers for I2C Humidity and Temperature Sensor (Si70XX),
6-
Remote 8-bit I/O expander (PCF8574/PCF8574A), and Digital Pressure
7-
Sensor (BMP085).
6+
Remote 8-bit I/O expander (PCF8574/PCF8574A), Digital Pressure
7+
Sensor (BMP085), and Single/Multi-Channel 1-Wire Master (DS2482).
88

99
The software bus manager implementation of the TWI interface uses the
1010
[Arduino-GPIO](https://github.com/mikaelpatel/Arduino-GPIO)
@@ -20,7 +20,7 @@ Device driver mutex allows a task to complete a device driver function
2020
in a synchronized manner when using the
2121
[Arduino-Scheduler](https://github.com/mikaelpatel/Arduino-Scheduler).
2222

23-
Version: 1.8
23+
Version: 1.9
2424

2525
## Classes
2626

@@ -31,11 +31,13 @@ Version: 1.8
3131
* [Digital Pressure Sensor, BMP085](./src/Driver/BMP085.h)
3232
* [Humidity and Temperature Sensor, Si70XX](./src/Driver/Si70XX.h)
3333
* [Remote 8-bit I/O expander, PCF8574](./src/Driver/PCF8574.h)
34+
* [Single/Multi-Channel 1-Wire Master, DS2482-100/800](./src/Driver/DS2482.h)
3435

3536
## Example Sketches
3637

3738
* [Scanner](./examples/Scanner)
3839
* [BMP085](./examples/BMP085)
40+
* [DS2482](./examples/DS2482)
3941
* [PCF8574](./examples/PCF8574)
4042
* [Si7021](./examples/Si7021)
4143

examples/DS2482/DS2482.ino

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
#include "GPIO.h"
2+
#include "TWI.h"
3+
#include "Driver/DS2482.h"
4+
5+
// Configure: Software or Hardware TWI
6+
// #define USE_SOFTWARE_TWI
7+
#if defined(USE_SOFTWARE_TWI)
8+
#include "Software/TWI.h"
9+
#if defined(SAM)
10+
Software::TWI<BOARD::D8,BOARD::D9> twi;
11+
#else
12+
Software::TWI<BOARD::D18,BOARD::D19> twi;
13+
#endif
14+
#else
15+
#include "Hardware/TWI.h"
16+
Hardware::TWI twi;
17+
#endif
18+
19+
DS2482 owi(twi);
20+
21+
const size_t ROM_MAX = 8;
22+
const uint8_t CHARBITS = 8;
23+
24+
// 1-Wire ROM and DS18B20 commands
25+
enum {
26+
SEARCH_ROM = 0xF0,
27+
READ_ROM = 0x33,
28+
MATCH_ROM = 0x55,
29+
SKIP_ROM = 0xCC,
30+
ALARM_SEARCH = 0xEC,
31+
CONVERT_T = 0x44,
32+
READ_SCRATCHPAD = 0xBE
33+
};
34+
35+
// DS18B20 scratchpad
36+
struct scratchpad_t {
37+
int16_t temperature;
38+
int8_t high_trigger;
39+
int8_t low_trigger;
40+
uint8_t configuration;
41+
uint8_t reserved[3];
42+
uint8_t crc;
43+
};
44+
45+
// 1-Wire CRC calculation
46+
uint8_t one_wire_crc_update(uint8_t crc, uint8_t data)
47+
{
48+
crc = crc ^ data;
49+
for (uint8_t i = 0; i < 8; i++) {
50+
if (crc & 0x01)
51+
crc = (crc >> 1) ^ 0x8C;
52+
else
53+
crc >>= 1;
54+
}
55+
return (crc);
56+
}
57+
58+
// Check assertion to be true, otherwise print line and expression
59+
#define ASSERT(expr) \
60+
do { \
61+
if (!(expr)) { \
62+
Serial.print(__LINE__); \
63+
Serial.println(F(":assert:" #expr)); \
64+
Serial.flush(); \
65+
exit(0); \
66+
} \
67+
} while (0)
68+
69+
70+
// Print and evaluate expression
71+
#define TRACE(expr) \
72+
do { \
73+
Serial.print(#expr "="); \
74+
Serial.println(expr); \
75+
} while (0)
76+
77+
78+
void setup()
79+
{
80+
Serial.begin(57600);
81+
while (!Serial);
82+
83+
// Reset and configure the TWI 1-Wire Master
84+
ASSERT(owi.device_reset());
85+
ASSERT(owi.write_configuration());
86+
87+
// Read and print registers
88+
uint8_t config;
89+
ASSERT(owi.set_read_pointer(owi.CONFIGURATION_REGISTER, config));
90+
TRACE(config);
91+
92+
uint8_t data;
93+
ASSERT(owi.set_read_pointer(owi.READ_DATA_REGISTER, data));
94+
TRACE(data);
95+
96+
uint8_t status;
97+
ASSERT(owi.set_read_pointer(owi.STATUS_REGISTER, status));
98+
TRACE(status);
99+
100+
#if defined(DS2482_800)
101+
uint8_t channel;
102+
ASSERT(owi.set_read_pointer(owi.CHANNEL_SELECTION_REGISTER, channel));
103+
TRACE(channel);
104+
#endif
105+
106+
// Read and print rom from device on 1-Wire bus
107+
uint8_t rom[ROM_MAX] = { 0 };
108+
uint8_t crc = 0;
109+
ASSERT(owi.one_wire_reset());
110+
ASSERT(owi.one_wire_write_byte(READ_ROM));
111+
Serial.print(F("read_rom="));
112+
for (size_t i = 0; i < sizeof(rom); i++) {
113+
owi.one_wire_read_byte(rom[i]);
114+
if (rom[i] < 0x10) Serial.print(0);
115+
Serial.print(rom[i], HEX);
116+
crc = one_wire_crc_update(crc, rom[i]);
117+
}
118+
Serial.println();
119+
ASSERT(crc == 0);
120+
121+
// Search device on 1-Wire bus and print rom
122+
uint8_t bits = 0;
123+
uint8_t ix = 0;
124+
uint8_t value = 0;
125+
uint8_t dir = 0;
126+
int8_t res = 0;
127+
bool id;
128+
bool nid;
129+
crc = 0;
130+
ASSERT(owi.one_wire_reset());
131+
ASSERT(owi.one_wire_write_byte(SEARCH_ROM));
132+
Serial.print(F("search_rom="));
133+
do {
134+
res = owi.one_wire_triplet(dir);
135+
if (res < 0) break;
136+
id = (res & 1) != 0;
137+
nid = (res & 2) != 0;
138+
value = (value >> 1);
139+
if (dir) value |= 0x80;
140+
bits += 1;
141+
if (bits == CHARBITS) {
142+
rom[ix] = value;
143+
if (rom[ix] < 0x10) Serial.print(0);
144+
Serial.print(rom[ix], HEX);
145+
crc = one_wire_crc_update(crc, rom[ix]);
146+
ix += 1;
147+
bits = 0;
148+
value = 0;
149+
}
150+
} while (id != nid);
151+
Serial.println();
152+
ASSERT(crc == 0);
153+
}
154+
155+
void loop()
156+
{
157+
// Convert and read temperature
158+
ASSERT(owi.one_wire_reset());
159+
ASSERT(owi.one_wire_write_byte(SKIP_ROM));
160+
ASSERT(owi.one_wire_write_byte(CONVERT_T));
161+
delay(750);
162+
163+
ASSERT(owi.one_wire_reset());
164+
ASSERT(owi.one_wire_write_byte(SKIP_ROM));
165+
ASSERT(owi.one_wire_write_byte(READ_SCRATCHPAD));
166+
167+
// Print scatchpad and calculate check sum
168+
scratchpad_t scratchpad;
169+
uint8_t* p = (uint8_t*) &scratchpad;
170+
uint8_t crc = 0;
171+
Serial.print(F("read_scratchpad="));
172+
for (size_t i = 0; i < sizeof(scratchpad); i++) {
173+
ASSERT(owi.one_wire_read_byte(p[i]));
174+
if (i == sizeof(scratchpad) - 1) Serial.print(F(",crc="));
175+
if (p[i] < 0x10) Serial.print('0');
176+
Serial.print(p[i], HEX);
177+
crc = one_wire_crc_update(crc, p[i]);
178+
}
179+
ASSERT(crc == 0);
180+
181+
// Print temperature (convert from fixed to floating point number)
182+
Serial.print(',');
183+
float temperature = scratchpad.temperature * 0.0625;
184+
TRACE(temperature);
185+
delay(2000);
186+
}

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=Arduino-TWI
2-
version=1.8
2+
version=1.9
33
author=Mikael Patel
44
maintainer=Mikael Patel <[email protected]>
55
sentence=Two-Wire Interface (TWI) library for Arduino.
6-
paragraph=The TWI library is an abstract interface for I2C device drivers. The library includes a hardware and software implementation, and example device drivers for I2C Humidity and Temperature Sensor (Si70XX), Remote 8-bit I/O expander (PCF8574/PCF8574A), and Digital Pressure Sensor (BMP085).
6+
paragraph=The TWI library is an abstract interface for I2C device drivers. The library includes a hardware and software implementation, and example device drivers for I2C Humidity and Temperature Sensor (Si70XX), Remote 8-bit I/O expander (PCF8574/PCF8574A), Digital Pressure Sensor (BMP085), and Single/Multi-Channel 1-Wire Master (DS2482).
77
category=Communication
88
url=https://github.com/mikaelpatel/Arduino-TWI
99
architectures=avr,sam

mainpage.dox

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ The TWI library is an abstract interface for I2C device drivers. The
44
library includes a Hardware::TWI and Software::TWI bus manager
55
implementation, and example device drivers for I2C Humidity and
66
Temperature Sensor (Si70XX), Remote 8-bit I/O expander (PCF8574 and
7-
PCF8574A), and and Digital Pressure Sensor (BMP085).
7+
PCF8574A), Digital Pressure Sensor (BMP085), and Single/Multi-Channel
8+
1-Wire Master (DS2482).
89

9-
Version: 1.8
10+
Version: 1.9
1011
*/
1112

1213
/** @page License

0 commit comments

Comments
 (0)