Skip to content

Commit

Permalink
Adding device driver for DS2482, 1-Wire Master Bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelpatel committed Oct 10, 2017
1 parent 60e6a0e commit 4491e11
Show file tree
Hide file tree
Showing 5 changed files with 609 additions and 7 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
The TWI library is an abstract interface for I2C device drivers. The
library includes a hardware and software bus manager, and example
device drivers for I2C Humidity and Temperature Sensor (Si70XX),
Remote 8-bit I/O expander (PCF8574/PCF8574A), and Digital Pressure
Sensor (BMP085).
Remote 8-bit I/O expander (PCF8574/PCF8574A), Digital Pressure
Sensor (BMP085), and Single/Multi-Channel 1-Wire Master (DS2482).

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

Version: 1.8
Version: 1.9

## Classes

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

## Example Sketches

* [Scanner](./examples/Scanner)
* [BMP085](./examples/BMP085)
* [DS2482](./examples/DS2482)
* [PCF8574](./examples/PCF8574)
* [Si7021](./examples/Si7021)

Expand Down
186 changes: 186 additions & 0 deletions examples/DS2482/DS2482.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#include "GPIO.h"
#include "TWI.h"
#include "Driver/DS2482.h"

// Configure: Software or Hardware TWI
// #define USE_SOFTWARE_TWI
#if defined(USE_SOFTWARE_TWI)
#include "Software/TWI.h"
#if defined(SAM)
Software::TWI<BOARD::D8,BOARD::D9> twi;
#else
Software::TWI<BOARD::D18,BOARD::D19> twi;
#endif
#else
#include "Hardware/TWI.h"
Hardware::TWI twi;
#endif

DS2482 owi(twi);

const size_t ROM_MAX = 8;
const uint8_t CHARBITS = 8;

// 1-Wire ROM and DS18B20 commands
enum {
SEARCH_ROM = 0xF0,
READ_ROM = 0x33,
MATCH_ROM = 0x55,
SKIP_ROM = 0xCC,
ALARM_SEARCH = 0xEC,
CONVERT_T = 0x44,
READ_SCRATCHPAD = 0xBE
};

// DS18B20 scratchpad
struct scratchpad_t {
int16_t temperature;
int8_t high_trigger;
int8_t low_trigger;
uint8_t configuration;
uint8_t reserved[3];
uint8_t crc;
};

// 1-Wire CRC calculation
uint8_t one_wire_crc_update(uint8_t crc, uint8_t data)
{
crc = crc ^ data;
for (uint8_t i = 0; i < 8; i++) {
if (crc & 0x01)
crc = (crc >> 1) ^ 0x8C;
else
crc >>= 1;
}
return (crc);
}

// Check assertion to be true, otherwise print line and expression
#define ASSERT(expr) \
do { \
if (!(expr)) { \
Serial.print(__LINE__); \
Serial.println(F(":assert:" #expr)); \
Serial.flush(); \
exit(0); \
} \
} while (0)


// Print and evaluate expression
#define TRACE(expr) \
do { \
Serial.print(#expr "="); \
Serial.println(expr); \
} while (0)


void setup()
{
Serial.begin(57600);
while (!Serial);

// Reset and configure the TWI 1-Wire Master
ASSERT(owi.device_reset());
ASSERT(owi.write_configuration());

// Read and print registers
uint8_t config;
ASSERT(owi.set_read_pointer(owi.CONFIGURATION_REGISTER, config));
TRACE(config);

uint8_t data;
ASSERT(owi.set_read_pointer(owi.READ_DATA_REGISTER, data));
TRACE(data);

uint8_t status;
ASSERT(owi.set_read_pointer(owi.STATUS_REGISTER, status));
TRACE(status);

#if defined(DS2482_800)
uint8_t channel;
ASSERT(owi.set_read_pointer(owi.CHANNEL_SELECTION_REGISTER, channel));
TRACE(channel);
#endif

// Read and print rom from device on 1-Wire bus
uint8_t rom[ROM_MAX] = { 0 };
uint8_t crc = 0;
ASSERT(owi.one_wire_reset());
ASSERT(owi.one_wire_write_byte(READ_ROM));
Serial.print(F("read_rom="));
for (size_t i = 0; i < sizeof(rom); i++) {
owi.one_wire_read_byte(rom[i]);
if (rom[i] < 0x10) Serial.print(0);
Serial.print(rom[i], HEX);
crc = one_wire_crc_update(crc, rom[i]);
}
Serial.println();
ASSERT(crc == 0);

// Search device on 1-Wire bus and print rom
uint8_t bits = 0;
uint8_t ix = 0;
uint8_t value = 0;
uint8_t dir = 0;
int8_t res = 0;
bool id;
bool nid;
crc = 0;
ASSERT(owi.one_wire_reset());
ASSERT(owi.one_wire_write_byte(SEARCH_ROM));
Serial.print(F("search_rom="));
do {
res = owi.one_wire_triplet(dir);
if (res < 0) break;
id = (res & 1) != 0;
nid = (res & 2) != 0;
value = (value >> 1);
if (dir) value |= 0x80;
bits += 1;
if (bits == CHARBITS) {
rom[ix] = value;
if (rom[ix] < 0x10) Serial.print(0);
Serial.print(rom[ix], HEX);
crc = one_wire_crc_update(crc, rom[ix]);
ix += 1;
bits = 0;
value = 0;
}
} while (id != nid);
Serial.println();
ASSERT(crc == 0);
}

void loop()
{
// Convert and read temperature
ASSERT(owi.one_wire_reset());
ASSERT(owi.one_wire_write_byte(SKIP_ROM));
ASSERT(owi.one_wire_write_byte(CONVERT_T));
delay(750);

ASSERT(owi.one_wire_reset());
ASSERT(owi.one_wire_write_byte(SKIP_ROM));
ASSERT(owi.one_wire_write_byte(READ_SCRATCHPAD));

// Print scatchpad and calculate check sum
scratchpad_t scratchpad;
uint8_t* p = (uint8_t*) &scratchpad;
uint8_t crc = 0;
Serial.print(F("read_scratchpad="));
for (size_t i = 0; i < sizeof(scratchpad); i++) {
ASSERT(owi.one_wire_read_byte(p[i]));
if (i == sizeof(scratchpad) - 1) Serial.print(F(",crc="));
if (p[i] < 0x10) Serial.print('0');
Serial.print(p[i], HEX);
crc = one_wire_crc_update(crc, p[i]);
}
ASSERT(crc == 0);

// Print temperature (convert from fixed to floating point number)
Serial.print(',');
float temperature = scratchpad.temperature * 0.0625;
TRACE(temperature);
delay(2000);
}
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=Arduino-TWI
version=1.8
version=1.9
author=Mikael Patel
maintainer=Mikael Patel <[email protected]>
sentence=Two-Wire Interface (TWI) library for Arduino.
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).
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).
category=Communication
url=https://github.com/mikaelpatel/Arduino-TWI
architectures=avr,sam
5 changes: 3 additions & 2 deletions mainpage.dox
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ The TWI library is an abstract interface for I2C device drivers. The
library includes a Hardware::TWI and Software::TWI bus manager
implementation, and example device drivers for I2C Humidity and
Temperature Sensor (Si70XX), Remote 8-bit I/O expander (PCF8574 and
PCF8574A), and and Digital Pressure Sensor (BMP085).
PCF8574A), Digital Pressure Sensor (BMP085), and Single/Multi-Channel
1-Wire Master (DS2482).

Version: 1.8
Version: 1.9
*/

/** @page License
Expand Down
Loading

0 comments on commit 4491e11

Please sign in to comment.