-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding device driver for DS2482, 1-Wire Master Bridge
- Loading branch information
1 parent
60e6a0e
commit 4491e11
Showing
5 changed files
with
609 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.