summaryrefslogtreecommitdiff
path: root/lib/onewire.h
diff options
context:
space:
mode:
authorBernhard Tittelbach <xro@realraum.at>2013-08-03 04:45:35 +0000
committerBernhard Tittelbach <xro@realraum.at>2013-08-03 04:45:35 +0000
commit8afa3e3f9de66d6ae11fc1a0ca839be31c327631 (patch)
tree7467814cef8c18276b4005635f87058b86a3f234 /lib/onewire.h
parentlower speed - hardware issue?? (diff)
ds18x20 onewire temp sensor example
git-svn-id: https://svn.spreadspace.org/avr/trunk@210 aa12f405-d877-488e-9caf-2d797e2a1cc7
Diffstat (limited to 'lib/onewire.h')
-rw-r--r--lib/onewire.h229
1 files changed, 229 insertions, 0 deletions
diff --git a/lib/onewire.h b/lib/onewire.h
new file mode 100644
index 0000000..21be0af
--- /dev/null
+++ b/lib/onewire.h
@@ -0,0 +1,229 @@
+#ifndef OneWire_h
+#define OneWire_h
+
+#include <inttypes.h>
+#include <stdbool.h>
+#include <avr/pgmspace.h>
+
+// You can exclude certain features from OneWire. In theory, this
+// might save some space. In practice, the compiler automatically
+// removes unused code (technically, the linker, using -fdata-sections
+// and -ffunction-sections when compiling, and Wl,--gc-sections
+// when linking), so most of these will not result in any code size
+// reduction. Well, unless you try to use the missing features
+// and redesign your program to not need them! ONEWIRE_CRC8_TABLE
+// is the exception, because it selects a fast but large algorithm
+// or a small but slow algorithm.
+
+// you can exclude onewire_search by defining that to 0
+#ifndef ONEWIRE_SEARCH
+#define ONEWIRE_SEARCH 1
+#endif
+
+// You can exclude CRC checks altogether by defining this to 0
+#ifndef ONEWIRE_CRC
+#define ONEWIRE_CRC 1
+#endif
+
+// Select the table-lookup method of computing the 8-bit CRC
+// by setting this to 1. The lookup table enlarges code size by
+// about 250 bytes. It does NOT consume RAM (but did in very
+// old versions of OneWire). If you disable this, a slower
+// but very compact algorithm is used.
+#ifndef ONEWIRE_CRC8_TABLE
+#define ONEWIRE_CRC8_TABLE 1
+#endif
+
+// You can allow 16-bit CRC checks by defining this to 1
+// (Note that ONEWIRE_CRC must also be 1.)
+#ifndef ONEWIRE_CRC16
+#define ONEWIRE_CRC16 1
+#endif
+
+#define FALSE 0
+#define TRUE 1
+
+// Platform specific I/O definitions
+
+#if defined(__AVR__)
+#define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
+#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
+#define IO_REG_TYPE uint8_t
+#define IO_REG_ASM __asm__("r30")
+#define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
+#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask))
+#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask))
+#define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask))
+#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask))
+
+#elif defined(__MK20DX128__)
+#define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
+#define PIN_TO_BITMASK(pin) (1)
+#define IO_REG_TYPE uint8_t
+#define IO_REG_ASM
+#define DIRECT_READ(base, mask) (*((base)+512))
+#define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0)
+#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1)
+#define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1)
+#define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1)
+
+#elif defined(__SAM3X8E__)
+// Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due.
+// http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268
+// If you have trouble with OneWire on Arduino Due, please check the
+// status of delayMicroseconds() before reporting a bug in OneWire!
+#define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER))
+#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
+#define IO_REG_TYPE uint32_t
+#define IO_REG_ASM
+#define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0)
+#define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask))
+#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask))
+#define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask))
+#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask))
+#ifndef PROGMEM
+#define PROGMEM
+#endif
+#ifndef pgm_read_byte
+#define pgm_read_byte(addr) (*(const uint8_t *)(addr))
+#endif
+
+#elif defined(__PIC32MX__)
+#define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin)))
+#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
+#define IO_REG_TYPE uint32_t
+#define IO_REG_ASM
+#define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10
+#define DIRECT_MODE_INPUT(base, mask) ((*(base+2)) = (mask)) //TRISXSET + 0x08
+#define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) = (mask)) //TRISXCLR + 0x04
+#define DIRECT_WRITE_LOW(base, mask) ((*(base+8+1)) = (mask)) //LATXCLR + 0x24
+#define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28
+
+#else
+#error "Please define I/O register types here"
+#endif
+
+#define DS1820_FAMILY_ID 0x10
+#define DS1820_START_CONVERSION 0x44
+#define DS1820_READ_SCRATCHPAD 0xbe
+#define DS1820_WRITE_SCRATCHPAD 0x4e
+#define DS1820_ERROR -1000 // Return code. Outside temperature range.
+#define DS1820_RESOLUTION_9BITS 0x00
+#define DS1820_RESOLUTION_10BITS 0x20
+#define DS1820_RESOLUTION_11BITS 0x40
+#define DS1820_RESOLUTION_12BITS 0x60
+#define DS1820_TCONV_MS_9BITS 96
+#define DS1820_TCONV_MS_10BITS 195
+#define DS1820_TCONV_MS_11BITS 390
+#define DS1820_TCONV_MS_12BITS 800
+
+
+
+
+
+#define DS2890_FAMILY_ID 0x2c
+#define DS2890_WRITE_CONTROL_REGISTER 0X55
+#define DS2890_RELEASE_CODE 0x96
+#define DS2890_WRITE_POSITION 0x0f
+
+#ifndef _BV
+#define _BV(bit) (1 << (bit))
+#endif
+
+void owi_init(uint8_t pin, volatile IO_REG_TYPE *pin_read_port);
+
+uint8_t owi_reset(void);
+void owi_select(const uint8_t rom[8]);
+
+// Issue a 1-Wire rom skip command, to address all on bus.
+void owi_skip(void);
+
+// Write a byte. If 'power' is one then the wire is held high at
+// the end for parasitically powered devices. You are responsible
+// for eventually depowering it by calling depower() or doing
+// another read or write.
+void owi_write(uint8_t v, uint8_t power);
+
+void owi_write_bytes(const uint8_t *buf, uint16_t count, bool power);
+
+// Read a byte.
+uint8_t owi_read(void);
+
+void owi_read_bytes(uint8_t *buf, uint16_t count);
+
+// Write a bit. The bus is always left powered at the end, see
+// note in write() about that.
+void owi_write_bit(uint8_t v);
+
+// Read a bit.
+uint8_t owi_read_bit(void);
+
+// Stop forcing power onto the bus. You only need to do this if
+// you used the 'power' flag to write() or used a write_bit() call
+// and aren't about to do another read or write. You would rather
+// not leave this powered if you don't have to, just in case
+// someone shorts your bus.
+void owi_depower(void);
+
+#if ONEWIRE_SEARCH
+// Clear the search state so that if will start from the beginning again.
+void owi_reset_search(void);
+
+// Setup the search to find the device type 'family_code' on the next call
+// to search(*newAddr) if it is present.
+void owi_target_search(uint8_t family_code);
+
+// Look for the next device. Returns 1 if a new address has been
+// returned. A zero might mean that the bus is shorted, there are
+// no devices, or you have already retrieved all of them. It
+// might be a good idea to check the CRC to make sure you didn't
+// get garbage. The order is deterministic. You will always get
+// the same devices in the same order.
+uint8_t owi_search(uint8_t *newAddr);
+#endif
+
+#if ONEWIRE_CRC
+// Compute a Dallas Semiconductor 8 bit CRC, these are used in the
+// ROM and scratchpad registers.
+uint8_t owi_crc8(const uint8_t *addr, uint8_t len);
+
+#if ONEWIRE_CRC16
+// Compute the 1-Wire CRC16 and compare it against the received CRC.
+// Example usage (reading a DS2408):
+// // Put everything in a buffer so we can compute the CRC easily.
+// uint8_t buf[13];
+// buf[0] = 0xF0; // Read PIO Registers
+// buf[1] = 0x88; // LSB address
+// buf[2] = 0x00; // MSB address
+// WriteBytes(net, buf, 3); // Write 3 cmd bytes
+// ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
+// if (!CheckCRC16(buf, 11, &buf[11])) {
+// // Handle error.
+// }
+//
+// @param input - Array of bytes to checksum.
+// @param len - How many bytes to use.
+// @param inverted_crc - The two CRC16 bytes in the received data.
+// This should just point into the received data,
+// *not* at a 16-bit integer.
+// @param crc - The crc starting value (optional)
+// @return True, iff the CRC matches.
+bool owi_check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc);
+
+// Compute a Dallas Semiconductor 16 bit CRC. This is required to check
+// the integrity of data received from many 1-Wire devices. Note that the
+// CRC computed here is *not* what you'll get from the 1-Wire network,
+// for two reasons:
+// 1) The CRC is transmitted bitwise inverted.
+// 2) Depending on the endian-ness of your processor, the binary
+// representation of the two-byte return value may have a different
+// byte order than the two bytes you get from 1-Wire.
+// @param input - Array of bytes to checksum.
+// @param len - How many bytes to use.
+// @param crc - The crc starting value (optional)
+// @return The CRC16, as defined by Dallas Semiconductor.
+uint16_t owi_crc16(const uint8_t* input, uint16_t len, uint16_t crc);
+#endif
+#endif
+
+#endif