diff --git a/examples/osd/arduino-dallaseprom/DallasEPROM.cpp b/examples/osd/arduino-dallaseprom/DallasEPROM.cpp new file mode 100644 index 000000000..e9ac5ab8a --- /dev/null +++ b/examples/osd/arduino-dallaseprom/DallasEPROM.cpp @@ -0,0 +1,383 @@ +// Maxim/Dallas 1-Wire EPROM & EEPROM library for Arduino +// Copyright (C) 2011-2014 Eric Hokanson +// https://github.com/pceric + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +#include "DallasEPROM.h" + +/** Supported chips. */ +model_type _chip_model_list[] = { + // EPROMs + { 0x09, "DS2502", 4, true }, + { 0x0B, "DS2505", 64, true }, + // EEPROMs + { 0x14, "DS2430", 1, false }, + { 0x2D, "DS2431", 4, false }, + { 0x23, "DS2433", 16, false }, + { 0, 0, 0, 0 } +}; + +DallasEPROM::DallasEPROM(OneWire* rWire) { + _wire = rWire; + _progPin = -1; +} + +DallasEPROM::DallasEPROM(OneWire* rWire, int progPin) { + _wire = rWire; + _progPin = progPin; + pinMode(progPin, OUTPUT); + digitalWrite(progPin, LOW); +} + +/******************* + * Static methods + *******************/ + +bool DallasEPROM::validAddress(uint8_t* deviceAddress) { + return (OneWire::crc8(deviceAddress, 7) == deviceAddress[7]); +} + +bool DallasEPROM::isSupported(uint8_t* deviceAddress) { + int i = 0; + while (_chip_model_list[i].id) { + if (deviceAddress[0] == _chip_model_list[i].id) + return true; + ++i; + } + return false; +} + +/******************* + * Public methods + *******************/ + +bool DallasEPROM::search() { + int i; + _curModelIndex = -1; + if (!_wire->reset()) + return false; + _wire->reset_search(); + while (_wire->search(_addr)) { + i = 0; + while (_chip_model_list[i].id) { + if (_addr[0] == _chip_model_list[i].id) { + _curModelIndex = i; + return true; + } + ++i; + } + } + return false; +} + +uint8_t* DallasEPROM::getAddress() { + return _addr; +} + +void DallasEPROM::setAddress(uint8_t* pAddress) { + int i = 0; + _curModelIndex = -1; + + memcpy(_addr, pAddress, 8); + + while (_chip_model_list[i].id) { + if (_addr[0] == _chip_model_list[i].id) { + _curModelIndex = i; + return; + } + ++i; + } +} + +const char* DallasEPROM::getDeviceName() { + if (_curModelIndex >= 0) + return _chip_model_list[_curModelIndex].name; + else + return NULL; +} + +bool DallasEPROM::isConnected() { + uint8_t tmpAddress[8]; + + if (!_wire->reset()) + return false; + _wire->reset_search(); + while (_wire->search(tmpAddress)) { + if (memcmp(_addr, tmpAddress, 8)==0) + return true; + } + return false; +} + +int DallasEPROM::readPage(uint8_t* data, int page) { + unsigned int address = page * 32; + + if (!isPageValid(page)) + return INVALID_PAGE; + if (!isConnected()) + return DEVICE_DISCONNECTED; + + // check for page redirection + if (isEPROMDevice()) { + byte command[] = { READSTATUS, (byte)(page+1), 0x00 }; + byte new_addr; + + _wire->reset(); + _wire->select(_addr); + _wire->write(command[0]); + _wire->write(command[1]); + _wire->write(command[2]); + + if (OneWire::crc8(command, 3) != _wire->read()) + return CRC_MISMATCH; + + if ((new_addr = _wire->read()) != 0xFF) + address = new_addr; + } + + byte command[] = { READMEMORY, (byte) address, (byte)(address >> 8) }; + + // send the command and starting address + _wire->reset(); + _wire->select(_addr); + _wire->write(command[0]); + _wire->write(command[1]); + _wire->write(command[2]); + + // Check CRC on EPROM devices + if (isEPROMDevice() && OneWire::crc8(command, 3) != _wire->read()) + return CRC_MISMATCH; + + // Read the entire page + for (int i = 0; i < 32; i++) { + data[i] = _wire->read(); + } + + // TODO: On EPROM device you can check the CRC post read + + return 0; +} + +int DallasEPROM::writePage(uint8_t* data, int page) { + unsigned int address = page * 32; + + if (!isPageValid(page)) + return INVALID_PAGE; + if (!isConnected()) + return DEVICE_DISCONNECTED; + + // EEPROMS have a difference write method than EPROMS + if (!isEPROMDevice()) { + int status; + + // a page is 4 8-byte scratch writes + for (int i = 0; i < 32; i += 8) { + if ((status = scratchWrite(&data[i], 8, address + i))) + return status; + } + return 0; + } + + byte command[] = { WRITEMEMORY, (byte) address, (byte)(address >> 8), + data[0] }; + + // send the command, address, and the first byte + _wire->reset(); + _wire->select(_addr); + _wire->write(command[0]); + _wire->write(command[1]); + _wire->write(command[2]); + _wire->write(command[3]); + + // Check CRC + if (OneWire::crc8(command, 4) != _wire->read()) + return CRC_MISMATCH; + + // Issue programming pulse for the first byte + if (_progPin >= 0) { + digitalWrite(_progPin, HIGH); + delayMicroseconds(500); + digitalWrite(_progPin, LOW); + } + delayMicroseconds(500); + + // Check the first byte for proper burn + if (command[3] != _wire->read()) + return COPY_FAILURE; + + // write out the rest of the page + for (int i = 1; i < 32; i++) { + // Write byte + _wire->write(data[i]); + // Check CRC + _wire->read(); // FIXME: The EPROM calculates a CRC based on 9 bits, we can't do that with OneWire + //byte crc[] = { (byte)((address+i) & 0x01), data[i] }; + //if (OneWire::crc8(crc, 2) != _wire->read()) + // return CRC_MISMATCH; + // Issue programming pulse + if (_progPin >= 0) { + digitalWrite(_progPin, HIGH); + delayMicroseconds(500); + digitalWrite(_progPin, LOW); + } + delayMicroseconds(500); + // Check for proper burn + if (data[i] != _wire->read()) + return COPY_FAILURE; + } + + return 0; +} + +int DallasEPROM::lockPage(int page) { + if (!isPageValid(page)) + return INVALID_PAGE; + if (!isConnected()) + return DEVICE_DISCONNECTED; + + _wire->reset(); + _wire->select(_addr); + + if (isEPROMDevice()) { + byte command[] = { WRITESTATUS, 0x00, 0x00, (1 << page) }; + + _wire->write(command[0]); + _wire->write(command[1]); + _wire->write(command[2]); + _wire->write(command[3]); + + // Check CRC + if (OneWire::crc8(command, 4) != _wire->read()) + return CRC_MISMATCH; + + // Issue programming pulse + if (_progPin >= 0) { + digitalWrite(_progPin, HIGH); + delayMicroseconds(500); + digitalWrite(_progPin, LOW); + } + delayMicroseconds(500); + + // TODO: Verify data + } else { + unsigned int start; + byte data[] = { 0x55 }; // write protect + + start = _chip_model_list[_curModelIndex].pages * 32 + page; + scratchWrite(data, 1, start); + } + + return 0; +} + +bool DallasEPROM::isPageLocked(int page) { + byte status; + + if (!isPageValid(page)) + return INVALID_PAGE; + if (!isConnected()) + return DEVICE_DISCONNECTED; + + _wire->reset(); + _wire->select(_addr); + + if (isEPROMDevice()) { + byte command[] = { READSTATUS, 0x00, 0x00 }; + _wire->write(command[0]); + _wire->write(command[1]); + _wire->write(command[2]); + + // Check CRC on EPROM devices + if (OneWire::crc8(command, 3) != _wire->read()) + return CRC_MISMATCH; + + status = _wire->read(); + + _wire->reset(); + + return 1 & (status >> page); + } else { + unsigned int start; + + start = _chip_model_list[_curModelIndex].pages * 32 + page; + + _wire->write(READMEMORY); + _wire->write((byte)start); + _wire->write((byte)(start >> 8)); + + if (_wire->read() == 0x55) + return true; + else + return false; + } +} + +/******************* + * Private methods + *******************/ + +int DallasEPROM::scratchWrite(uint8_t* data, int length, unsigned int address) { + byte auth[3]; + + // send the command and address + _wire->reset(); + _wire->select(_addr); + _wire->write(WRITEMEMORY); + _wire->write((byte) address); + _wire->write((byte)(address >> 8)); + + // write "length" bytes to the scratchpad + for (int i = 0; i < length; i++) + _wire->write(data[i]); + + // Read the auth code from the scratchpad and verify integrity + _wire->reset(); + _wire->select(_addr); + _wire->write(READSTATUS); + _wire->read_bytes(auth, 3); + for (int i = 0; i < length; i++) { + if (_wire->read() != data[i]) + return BAD_INTEGRITY; + } + + // Issue copy scratchpad with auth bytes + _wire->reset(); + _wire->select(_addr); + _wire->write(WRITESTATUS); + _wire->write(auth[0]); + _wire->write(auth[1]); + _wire->write(auth[2], 1); + + // Need 10ms prog delay + delay(10); + _wire->depower(); + + // Check for success + if (_wire->read() != 0xAA) + return COPY_FAILURE; + + return 0; +} + +bool DallasEPROM::isPageValid(int page) { + if (_curModelIndex >= 0 && page < _chip_model_list[_curModelIndex].pages) + return true; + return false; +} + +bool DallasEPROM::isEPROMDevice() { + if (_curModelIndex >= 0 && _chip_model_list[_curModelIndex].isEPROM == true) + return true; + return false; +} + +/** @file */ \ No newline at end of file diff --git a/examples/osd/arduino-dallaseprom/DallasEPROM.h b/examples/osd/arduino-dallaseprom/DallasEPROM.h new file mode 100644 index 000000000..a009c0e78 --- /dev/null +++ b/examples/osd/arduino-dallaseprom/DallasEPROM.h @@ -0,0 +1,209 @@ +// Maxim/Dallas 1-Wire EPROM & EEPROM library for Arduino +// Copyright (C) 2011-2014 Eric Hokanson +// https://github.com/pceric + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +/** @mainpage Quick Start Guide + * + * @section req_sec Requirements + * Arduino v1.0.0+ and OneWire Library v2.2 + * + * @section install_sec Installation + * Extract the DallasEPROM directory into the arduino/libraries directory. + * + * @section usage_sec Usage + * Click here to see a + * simple example of how to use this library. + * + * You can also find this example by selecting File->Examples->DallasEPROM + * from the Arduino software menu. + */ + +#ifndef DallasEPROM_h +#define DallasEPROM_h + +#define DALLASEPROMVERSION "1.2.0" + +#include +#include + +// OneWire commands +#define READSTATUS 0xAA // Read the status fields [EPROM] or the Scratchpad [EEPROM] +#define WRITESTATUS 0x55 // Write to the status fields [EPROM] or commit Scratchpad [EEPROM] +#define READMEMORY 0xF0 // Read memory +#define READMEMORYCRC 0xC3 // Read memory w CRC +#define WRITEMEMORY 0x0F // Write to EPROM or the Scratchpad +/** + * @defgroup ERROR_GROUP Returned Error Codes + * + * @{ + */ +#define CRC_MISMATCH -1 //!< CRC mismatch +#define INVALID_PAGE -2 //!< Requested page is invalid +#define PAGE_LOCKED -3 //!< Page is currently locked +#define BAD_INTEGRITY -4 //!< Failed scratchpad integrity check +#define COPY_FAILURE -5 //!< Copy scratchpad to memory has failed +#define UNSUPPORTED_DEVICE -64 //!< Chip is unsupported +#define DEVICE_DISCONNECTED -127 //!< Device has disconnected +/** @} */ + +/** + * Stores our supported chip types. + * + * @param id 1 byte chip id. + * @param name Name/model number of chip. + * @param pages Total number of 32 byte pages supported by chip. + * @param isEPROM Is this device an EPROM and not an EEPROM. + */ +typedef struct { + const uint8_t id; + const char* name; + const int pages; + const bool isEPROM; +} model_type; + +/** + * A class that reads and writes to Dallas/Maxim EPROM and EEPROM devices. + * + * @author Eric Hokanson + */ +class DallasEPROM { +public: + /** + * Creates a new DallasEPROM instance using the first EPROM/EEPROM + * device found on the bus. + * + * @param rWire Reference to a OneWire v2.2 instance. + */ + DallasEPROM(OneWire* rWire); + + /** + * Creates a new DallasEPROM instance using the first EPROM/EEPROM + * device found on the bus. In addition it will trigger a 500us + * pulse on the provided Arduino pin for EPROM programming. + * + * @param rWire Reference to a OneWire v2.2 instance. + * @param progPin Arduino pin number to pulse if writing EPROMs + */ + DallasEPROM(OneWire* rWire, int progPin); + + /** + * Static helper function to check if an address has a valid checksum. + * + * @param pAddress Pointer to an 8 byte 1-Wire address. + * @return True if the address has a valid checksum. + */ + static bool validAddress(uint8_t* pAddress); + + /** + * Static helper function to check if the supplied address is from + * a chip that the library supports. + * + * @param pAddress Pointer to an 8 byte 1-Wire address. + * @return True if the chip is supported. + */ + static bool isSupported(uint8_t* pAddress); + + /** + * Finds the first supported device on the bus and returns true on success + */ + bool search(); + + /** + * Gets the device address of the current instance. + * + * @return Pointer to the currently configured address. + */ + uint8_t* getAddress(); + + /** + * Sets the address of the current instance. + * + * @param pAddress Pointer to an 8 byte 1-Wire address. + */ + void setAddress(uint8_t* pAddress); + + /** + * Gets the device name based on the current address. + * + * @return Pointer to the current device string. + */ + const char* getDeviceName(); + + /** + * Scans the bus and checks if the device is still connected. + * + * @return True if the device is still connected. + */ + bool isConnected(); + + /** + * Reads a page from the device's memory. + * + * @param pData Pointer to a 32 byte buffer to store the data. + * @param page Page number to read (0-indexed). + * @return 0 on success or @ref ERROR_GROUP. + */ + int readPage(uint8_t* pData, int page); + + /** + * Writes a page to the device's memory. + * + * @param pData Pointer to a 32 byte buffer containing the data to store. + * @param page Page number to write (0-indexed). + * @return 0 on success or @ref ERROR_GROUP. + */ + int writePage(uint8_t* pData, int page); + + /** + * Lock a page and prevent further writes. + * + * @param page Page to lock (0-indexed). + * @return 0 on success or @ref ERROR_GROUP. + */ + int lockPage(int page); + + /** + * Checks to see if a page is locked. + * + * @param page Page to lock (0-indexed). + * @return True if locked. + */ + bool isPageLocked(int page); + +private: + OneWire* _wire; // Pointer to OneWire v2.2 instance + + uint8_t _addr[8]; // 1-Wire address of memory device stored LSB first + + int _progPin; // Arduino pin number to pulse when programming EPROMs + + char _curModelIndex; // Currently selected device from device table + + /** + * EEPROMs must use a scratch space to write data + */ + int scratchWrite(uint8_t* pdata, int length, unsigned int address); + + /** + * Checks to see if the provided page is valid. + */ + bool isPageValid(int page); + + /** + * Returns true if the current device is an EPROM and not an EEPROM. + */ + bool isEPROMDevice(); +}; +#endif + +/** @file */ \ No newline at end of file diff --git a/examples/osd/arduino-dallaseprom/Makefile b/examples/osd/arduino-dallaseprom/Makefile new file mode 100644 index 000000000..31ed18ed2 --- /dev/null +++ b/examples/osd/arduino-dallaseprom/Makefile @@ -0,0 +1,71 @@ +# Set this to the name of your sketch (without extension .pde) +SKETCH=sketch +EXE=arduino-example + +all: $(EXE) + +CONTIKI=../../.. + +# Contiki IPv6 configuration +CONTIKI_WITH_IPV6 = 1 + +CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\" +LFLAGS += -lm + +PROJECT_SOURCEFILES += ${SKETCH}.cpp OneWire.cpp DallasEPROM.cpp + +# automatically build RESTful resources +REST_RESOURCES_DIR = ./resources +REST_RESOURCES_DIR_COMMON = ../resources-common +REST_RESOURCES_FILES= $(notdir \ + $(shell find $(REST_RESOURCES_DIR) -name '*.c') \ + $(shell find $(REST_RESOURCES_DIR_COMMON) -name '*.c') \ + ) + +PROJECTDIRS += $(REST_RESOURCES_DIR) $(REST_RESOURCES_DIR_COMMON) +PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES) + +# variable for Makefile.include +ifneq ($(TARGET), minimal-net) +CFLAGS += -DUIP_CONF_IPV6_RPL=1 +else +# minimal-net does not support RPL under Linux and is mostly used to test CoAP only +${info INFO: compiling without RPL} +CFLAGS += -DUIP_CONF_IPV6_RPL=0 +CFLAGS += -DHARD_CODED_ADDRESS=\"fdfd::10\" +${info INFO: compiling with large buffers} +CFLAGS += -DUIP_CONF_BUFFER_SIZE=2048 +CFLAGS += -DREST_MAX_CHUNK_SIZE=1024 +CFLAGS += -DCOAP_MAX_HEADER_SIZE=640 +endif + +# linker optimizations +SMALL=1 + + +# REST Engine shall use Erbium CoAP implementation +APPS += er-coap +APPS += rest-engine +APPS += arduino + +include $(CONTIKI)/Makefile.include +include $(CONTIKI)/apps/arduino/Makefile.include + +$(CONTIKI)/tools/tunslip6: $(CONTIKI)/tools/tunslip6.c + (cd $(CONTIKI)/tools && $(MAKE) tunslip6) + +connect-router: $(CONTIKI)/tools/tunslip6 + sudo $(CONTIKI)/tools/tunslip6 aaaa::1/64 + +connect-router-cooja: $(CONTIKI)/tools/tunslip6 + sudo $(CONTIKI)/tools/tunslip6 -a 127.0.0.1 aaaa::1/64 + +connect-minimal: + sudo ip address add fdfd::1/64 dev tap0 + +avr-size: $(EXE).$(TARGET).sz + +flash: $(EXE).$(TARGET).u $(EXE).$(TARGET).eu + +.PHONY: flash avr-size +.PRECIOUS: $(EXE).$(TARGET).hex $(EXE).$(TARGET).eep diff --git a/examples/osd/arduino-dallaseprom/OneWire.cpp b/examples/osd/arduino-dallaseprom/OneWire.cpp new file mode 100644 index 000000000..6d55de0bf --- /dev/null +++ b/examples/osd/arduino-dallaseprom/OneWire.cpp @@ -0,0 +1,563 @@ +/* +Copyright (c) 2007, Jim Studt (original old version - many contributors since) + +The latest version of this library may be found at: + http://www.pjrc.com/teensy/td_libs_OneWire.html + +OneWire has been maintained by Paul Stoffregen (paul@pjrc.com) since +January 2010. At the time, it was in need of many bug fixes, but had +been abandoned the original author (Jim Studt). None of the known +contributors were interested in maintaining OneWire. Paul typically +works on OneWire every 6 to 12 months. Patches usually wait that +long. If anyone is interested in more actively maintaining OneWire, +please contact Paul. + +Version 2.3: + Unknonw chip fallback mode, Roger Clark + Teensy-LC compatibility, Paul Stoffregen + Search bug fix, Love Nystrom + +Version 2.2: + Teensy 3.0 compatibility, Paul Stoffregen, paul@pjrc.com + Arduino Due compatibility, http://arduino.cc/forum/index.php?topic=141030 + Fix DS18B20 example negative temperature + Fix DS18B20 example's low res modes, Ken Butcher + Improve reset timing, Mark Tillotson + Add const qualifiers, Bertrik Sikken + Add initial value input to crc16, Bertrik Sikken + Add target_search() function, Scott Roberts + +Version 2.1: + Arduino 1.0 compatibility, Paul Stoffregen + Improve temperature example, Paul Stoffregen + DS250x_PROM example, Guillermo Lovato + PIC32 (chipKit) compatibility, Jason Dangel, dangel.jason AT gmail.com + Improvements from Glenn Trewitt: + - crc16() now works + - check_crc16() does all of calculation/checking work. + - Added read_bytes() and write_bytes(), to reduce tedious loops. + - Added ds2408 example. + Delete very old, out-of-date readme file (info is here) + +Version 2.0: Modifications by Paul Stoffregen, January 2010: +http://www.pjrc.com/teensy/td_libs_OneWire.html + Search fix from Robin James + http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27 + Use direct optimized I/O in all cases + Disable interrupts during timing critical sections + (this solves many random communication errors) + Disable interrupts during read-modify-write I/O + Reduce RAM consumption by eliminating unnecessary + variables and trimming many to 8 bits + Optimize both crc8 - table version moved to flash + +Modified to work with larger numbers of devices - avoids loop. +Tested in Arduino 11 alpha with 12 sensors. +26 Sept 2008 -- Robin James +http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27 + +Updated to work with arduino-0008 and to include skip() as of +2007/07/06. --RJL20 + +Modified to calculate the 8-bit CRC directly, avoiding the need for +the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010 +-- Tom Pollard, Jan 23, 2008 + +Jim Studt's original library was modified by Josh Larios. + +Tom Pollard, pollard@alum.mit.edu, contributed around May 20, 2008 + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Much of the code was inspired by Derek Yerger's code, though I don't +think much of that remains. In any event that was.. + (copyleft) 2006 by Derek Yerger - Free to distribute freely. + +The CRC code was excerpted and inspired by the Dallas Semiconductor +sample code bearing this copyright. +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//-------------------------------------------------------------------------- +*/ + +#include "OneWire.h" + + +OneWire::OneWire(uint8_t pin) +{ + pinMode(pin, INPUT); + bitmask = PIN_TO_BITMASK(pin); + baseReg = PIN_TO_BASEREG(pin); +#if ONEWIRE_SEARCH + reset_search(); +#endif +} + + +// Perform the onewire reset function. We will wait up to 250uS for +// the bus to come high, if it doesn't then it is broken or shorted +// and we return a 0; +// +// Returns 1 if a device asserted a presence pulse, 0 otherwise. +// +uint8_t OneWire::reset(void) +{ + IO_REG_TYPE mask = bitmask; + volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; + uint8_t r; + uint8_t retries = 125; + + noInterrupts(); + DIRECT_MODE_INPUT(reg, mask); + interrupts(); + // wait until the wire is high... just in case + do { + if (--retries == 0) return 0; + delayMicroseconds(2); + } while ( !DIRECT_READ(reg, mask)); + + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + interrupts(); + delayMicroseconds(480); + noInterrupts(); + DIRECT_MODE_INPUT(reg, mask); // allow it to float + delayMicroseconds(70); + r = !DIRECT_READ(reg, mask); + interrupts(); + delayMicroseconds(410); + return r; +} + +// +// Write a bit. Port and bit is used to cut lookup time and provide +// more certain timing. +// +void OneWire::write_bit(uint8_t v) +{ + IO_REG_TYPE mask=bitmask; + volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; + + if (v & 1) { + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + delayMicroseconds(10); + DIRECT_WRITE_HIGH(reg, mask); // drive output high + interrupts(); + delayMicroseconds(55); + } else { + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + delayMicroseconds(65); + DIRECT_WRITE_HIGH(reg, mask); // drive output high + interrupts(); + delayMicroseconds(5); + } +} + +// +// Read a bit. Port and bit is used to cut lookup time and provide +// more certain timing. +// +uint8_t OneWire::read_bit(void) +{ + IO_REG_TYPE mask=bitmask; + volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; + uint8_t r; + + noInterrupts(); + DIRECT_MODE_OUTPUT(reg, mask); + DIRECT_WRITE_LOW(reg, mask); + delayMicroseconds(3); + DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise + delayMicroseconds(10); + r = DIRECT_READ(reg, mask); + interrupts(); + delayMicroseconds(53); + return r; +} + +// +// Write a byte. The writing code uses the active drivers to raise the +// pin high, if you need power after the write (e.g. DS18S20 in +// parasite power mode) then set 'power' to 1, otherwise the pin will +// go tri-state at the end of the write to avoid heating in a short or +// other mishap. +// +void OneWire::write(uint8_t v, uint8_t power /* = 0 */) { + uint8_t bitMask; + + for (bitMask = 0x01; bitMask; bitMask <<= 1) { + OneWire::write_bit( (bitMask & v)?1:0); + } + if ( !power) { + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + DIRECT_WRITE_LOW(baseReg, bitmask); + interrupts(); + } +} + +void OneWire::write_bytes(const uint8_t *buf, uint16_t count, bool power /* = 0 */) { + for (uint16_t i = 0 ; i < count ; i++) + write(buf[i]); + if (!power) { + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + DIRECT_WRITE_LOW(baseReg, bitmask); + interrupts(); + } +} + +// +// Read a byte +// +uint8_t OneWire::read() { + uint8_t bitMask; + uint8_t r = 0; + + for (bitMask = 0x01; bitMask; bitMask <<= 1) { + if ( OneWire::read_bit()) r |= bitMask; + } + return r; +} + +void OneWire::read_bytes(uint8_t *buf, uint16_t count) { + for (uint16_t i = 0 ; i < count ; i++) + buf[i] = read(); +} + +// +// Do a ROM select +// +void OneWire::select(const uint8_t rom[8]) +{ + uint8_t i; + + write(0x55); // Choose ROM + + for (i = 0; i < 8; i++) write(rom[i]); +} + +// +// Do a ROM skip +// +void OneWire::skip() +{ + write(0xCC); // Skip ROM +} + +void OneWire::depower() +{ + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + interrupts(); +} + +#if ONEWIRE_SEARCH + +// +// You need to use this function to start a search again from the beginning. +// You do not need to do it for the first search, though you could. +// +void OneWire::reset_search() +{ + // reset the search state + LastDiscrepancy = 0; + LastDeviceFlag = FALSE; + LastFamilyDiscrepancy = 0; + for(int i = 7; ; i--) { + ROM_NO[i] = 0; + if ( i == 0) break; + } +} + +// Setup the search to find the device type 'family_code' on the next call +// to search(*newAddr) if it is present. +// +void OneWire::target_search(uint8_t family_code) +{ + // set the search state to find SearchFamily type devices + ROM_NO[0] = family_code; + for (uint8_t i = 1; i < 8; i++) + ROM_NO[i] = 0; + LastDiscrepancy = 64; + LastFamilyDiscrepancy = 0; + LastDeviceFlag = FALSE; +} + +// +// Perform a search. If this function returns a '1' then it has +// enumerated the next device and you may retrieve the ROM from the +// OneWire::address variable. If there are no devices, no further +// devices, or something horrible happens in the middle of the +// enumeration then a 0 is returned. If a new device is found then +// its address is copied to newAddr. Use OneWire::reset_search() to +// start over. +// +// --- Replaced by the one from the Dallas Semiconductor web site --- +//-------------------------------------------------------------------------- +// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing +// search state. +// Return TRUE : device found, ROM number in ROM_NO buffer +// FALSE : device not found, end of search +// +uint8_t OneWire::search(uint8_t *newAddr) +{ + uint8_t id_bit_number; + uint8_t last_zero, rom_byte_number, search_result; + uint8_t id_bit, cmp_id_bit; + + unsigned char rom_byte_mask, search_direction; + + // initialize for search + id_bit_number = 1; + last_zero = 0; + rom_byte_number = 0; + rom_byte_mask = 1; + search_result = 0; + + // if the last call was not the last one + if (!LastDeviceFlag) + { + // 1-Wire reset + if (!reset()) + { + // reset the search + LastDiscrepancy = 0; + LastDeviceFlag = FALSE; + LastFamilyDiscrepancy = 0; + return FALSE; + } + + // issue the search command + write(0xF0); + + // loop to do the search + do + { + // read a bit and its complement + id_bit = read_bit(); + cmp_id_bit = read_bit(); + + // check for no devices on 1-wire + if ((id_bit == 1) && (cmp_id_bit == 1)) + break; + else + { + // all devices coupled have 0 or 1 + if (id_bit != cmp_id_bit) + search_direction = id_bit; // bit write value for search + else + { + // if this discrepancy if before the Last Discrepancy + // on a previous next then pick the same as last time + if (id_bit_number < LastDiscrepancy) + search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0); + else + // if equal to last pick 1, if not then pick 0 + search_direction = (id_bit_number == LastDiscrepancy); + + // if 0 was picked then record its position in LastZero + if (search_direction == 0) + { + last_zero = id_bit_number; + + // check for Last discrepancy in family + if (last_zero < 9) + LastFamilyDiscrepancy = last_zero; + } + } + + // set or clear the bit in the ROM byte rom_byte_number + // with mask rom_byte_mask + if (search_direction == 1) + ROM_NO[rom_byte_number] |= rom_byte_mask; + else + ROM_NO[rom_byte_number] &= ~rom_byte_mask; + + // serial number search direction write bit + write_bit(search_direction); + + // increment the byte counter id_bit_number + // and shift the mask rom_byte_mask + id_bit_number++; + rom_byte_mask <<= 1; + + // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask + if (rom_byte_mask == 0) + { + rom_byte_number++; + rom_byte_mask = 1; + } + } + } + while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 + + // if the search was successful then + if (!(id_bit_number < 65)) + { + // search successful so set LastDiscrepancy,LastDeviceFlag,search_result + LastDiscrepancy = last_zero; + + // check for last device + if (LastDiscrepancy == 0) + LastDeviceFlag = TRUE; + + search_result = TRUE; + } + } + + // if no device found then reset counters so next 'search' will be like a first + if (!search_result || !ROM_NO[0]) + { + LastDiscrepancy = 0; + LastDeviceFlag = FALSE; + LastFamilyDiscrepancy = 0; + search_result = FALSE; + } else { + for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i]; + } + return search_result; + } + +#endif + +#if ONEWIRE_CRC +// The 1-Wire CRC scheme is described in Maxim Application Note 27: +// "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products" +// + +#if ONEWIRE_CRC8_TABLE +// This table comes from Dallas sample code where it is freely reusable, +// though Copyright (C) 2000 Dallas Semiconductor Corporation +static const uint8_t PROGMEM dscrc_table[] = { + 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65, + 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220, + 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98, + 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255, + 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7, + 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154, + 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36, + 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185, + 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205, + 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80, + 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238, + 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115, + 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139, + 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22, + 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168, + 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53}; + +// +// Compute a Dallas Semiconductor 8 bit CRC. These show up in the ROM +// and the registers. (note: this might better be done without to +// table, it would probably be smaller and certainly fast enough +// compared to all those delayMicrosecond() calls. But I got +// confused, so I use this table from the examples.) +// +uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) +{ + uint8_t crc = 0; + + while (len--) { + crc = pgm_read_byte(dscrc_table + (crc ^ *addr++)); + } + return crc; +} +#else +// +// Compute a Dallas Semiconductor 8 bit CRC directly. +// this is much slower, but much smaller, than the lookup table. +// +uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) +{ + uint8_t crc = 0; + + while (len--) { + uint8_t inbyte = *addr++; + for (uint8_t i = 8; i; i--) { + uint8_t mix = (crc ^ inbyte) & 0x01; + crc >>= 1; + if (mix) crc ^= 0x8C; + inbyte >>= 1; + } + } + return crc; +} +#endif + +#if ONEWIRE_CRC16 +bool OneWire::check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc) +{ + crc = ~crc16(input, len, crc); + return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1]; +} + +uint16_t OneWire::crc16(const uint8_t* input, uint16_t len, uint16_t crc) +{ + static const uint8_t oddparity[16] = + { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 }; + + for (uint16_t i = 0 ; i < len ; i++) { + // Even though we're just copying a byte from the input, + // we'll be doing 16-bit computation with it. + uint16_t cdata = input[i]; + cdata = (cdata ^ crc) & 0xff; + crc >>= 8; + + if (oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4]) + crc ^= 0xC001; + + cdata <<= 6; + crc ^= cdata; + cdata <<= 1; + crc ^= cdata; + } + return crc; +} +#endif + +#endif diff --git a/examples/osd/arduino-dallaseprom/OneWire.h b/examples/osd/arduino-dallaseprom/OneWire.h new file mode 100644 index 000000000..3aac70636 --- /dev/null +++ b/examples/osd/arduino-dallaseprom/OneWire.h @@ -0,0 +1,250 @@ +#ifndef OneWire_h +#define OneWire_h + +#include + +//#if ARDUINO >= 100 +#include "Arduino.h" // for delayMicroseconds, digitalPinToBitMask, etc +//#else +//#include "WProgram.h" // for delayMicroseconds +//#include "pins_arduino.h" // for digitalPinToBitMask, etc +//#endif + +// 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__) || defined(__MK20DX256__) +#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(__MKL26Z64__) +#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint8_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) ((*((base)+16) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) (*((base)+20) &= ~(mask)) +#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+20) |= (mask)) +#define DIRECT_WRITE_LOW(base, mask) (*((base)+8) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) (*((base)+4) = (mask)) + +#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 +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#define IO_REG_TYPE unsigned int +#define IO_REG_ASM +#define DIRECT_READ(base, pin) digitalRead(pin) +#define DIRECT_WRITE_LOW(base, pin) digitalWrite(pin, LOW) +#define DIRECT_WRITE_HIGH(base, pin) digitalWrite(pin, HIGH) +#define DIRECT_MODE_INPUT(base, pin) pinMode(pin,INPUT) +#define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT) +#warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture." + +#endif + + +class OneWire +{ + private: + IO_REG_TYPE bitmask; + volatile IO_REG_TYPE *baseReg; + +#if ONEWIRE_SEARCH + // global search state + unsigned char ROM_NO[8]; + uint8_t LastDiscrepancy; + uint8_t LastFamilyDiscrepancy; + uint8_t LastDeviceFlag; +#endif + + public: + OneWire( uint8_t pin); + + // 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 + uint8_t reset(void); + + // Issue a 1-Wire rom select command, you do the reset first. + void select(const uint8_t rom[8]); + + // Issue a 1-Wire rom skip command, to address all on bus. + void 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 write(uint8_t v, uint8_t power = 0); + + void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0); + + // Read a byte. + uint8_t read(void); + + void 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 write_bit(uint8_t v); + + // Read a bit. + uint8_t 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 depower(void); + +#if ONEWIRE_SEARCH + // Clear the search state so that if will start from the beginning again. + void reset_search(); + + // Setup the search to find the device type 'family_code' on the next call + // to search(*newAddr) if it is present. + void 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 search(uint8_t *newAddr); +#endif + +#if ONEWIRE_CRC + // Compute a Dallas Semiconductor 8 bit CRC, these are used in the + // ROM and scratchpad registers. + static uint8_t 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. + static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0); + + // 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. + static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0); +#endif +#endif +}; + +#endif diff --git a/examples/osd/arduino-dallaseprom/README.md b/examples/osd/arduino-dallaseprom/README.md new file mode 100644 index 000000000..46a6a509d --- /dev/null +++ b/examples/osd/arduino-dallaseprom/README.md @@ -0,0 +1,24 @@ +Arduino compatibility example +============================= + +This example shows that it is now possible to re-use arduino sketches in +Contiki. This example documents the necessary magic. Arduino specifies +two routines, `setup` and `loop`. Before `setup` is called, the +framework initializes hardware. In original Arduino, all this is done in +a `main` function (in C). For contiki we define a process that does the +same. + +DallasEPROM +=========== + +Arduino library for Dallas 1-Wire (E)EPROMs +https://github.com/pceric/DallasEPROM + + +See the documentation file in apps/contiki-compat/README.md + +Build and Flash Merkurboard 256 +=============================== + +make clean TARGET=osd-merkur-256 flash + diff --git a/examples/osd/arduino-dallaseprom/arduino-example.c b/examples/osd/arduino-dallaseprom/arduino-example.c new file mode 100644 index 000000000..ea74dd8b8 --- /dev/null +++ b/examples/osd/arduino-dallaseprom/arduino-example.c @@ -0,0 +1,2 @@ +#include +AUTOSTART_PROCESSES(&arduino_sketch); diff --git a/examples/osd/arduino-dallaseprom/flash.sh b/examples/osd/arduino-dallaseprom/flash.sh new file mode 100755 index 000000000..e82962073 --- /dev/null +++ b/examples/osd/arduino-dallaseprom/flash.sh @@ -0,0 +1,2 @@ +#!/bin/bash +make TARGET=osd-merkur-128 flash diff --git a/examples/osd/arduino-dallaseprom/project-conf.h b/examples/osd/arduino-dallaseprom/project-conf.h new file mode 100644 index 000000000..e25aed53c --- /dev/null +++ b/examples/osd/arduino-dallaseprom/project-conf.h @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * + */ + +#ifndef PROJECT_RPL_WEB_CONF_H_ +#define PROJECT_RPL_WEB_CONF_H_ + +#define PLATFORM_HAS_LEDS 1 +//#define PLATFORM_HAS_BUTTON 1 +#define PLATFORM_HAS_BATTERY 1 + +#define SICSLOWPAN_CONF_FRAG 1 + +#define LOOP_INTERVAL (10 * CLOCK_SECOND) + +/* Save energy */ +//#define RDC_CONF_PT_YIELD_OFF + +/* For Debug: Dont allow MCU sleeping between channel checks */ +//#undef RDC_CONF_MCU_SLEEP +//#define RDC_CONF_MCU_SLEEP 0 + +/* Disabling RDC for demo purposes. Core updates often require more memory. */ +/* For projects, optimize memory and enable RDC again. */ +// #undef NETSTACK_CONF_RDC +//#define NETSTACK_CONF_RDC nullrdc_driver + +/* Increase rpl-border-router IP-buffer when using more than 64. */ +#undef REST_MAX_CHUNK_SIZE +#define REST_MAX_CHUNK_SIZE 64 + +/* Estimate your header size, especially when using Proxy-Uri. */ +/* +#undef COAP_MAX_HEADER_SIZE +#define COAP_MAX_HEADER_SIZE 70 +*/ + +/* The IP buffer size must fit all other hops, in particular the border router. */ + +#undef UIP_CONF_BUFFER_SIZE +#define UIP_CONF_BUFFER_SIZE 256 + + +/* Multiplies with chunk size, be aware of memory constraints. */ +#undef COAP_MAX_OPEN_TRANSACTIONS +#define COAP_MAX_OPEN_TRANSACTIONS 4 + +/* Must be <= open transaction number, default is COAP_MAX_OPEN_TRANSACTIONS-1. */ +/* +#undef COAP_MAX_OBSERVERS +#define COAP_MAX_OBSERVERS 2 +*/ + +/* Filtering .well-known/core per query can be disabled to save space. */ +/* +#undef COAP_LINK_FORMAT_FILTERING +#define COAP_LINK_FORMAT_FILTERING 0 +*/ + +/* Save some memory for the sky platform. */ +/* +#undef NBR_TABLE_CONF_MAX_NEIGHBORS +#define NBR_TABLE_CONF_MAX_NEIGHBORS 10 +#undef UIP_CONF_MAX_ROUTES +#define UIP_CONF_MAX_ROUTES 10 +*/ + +/* Reduce 802.15.4 frame queue to save RAM. */ +/* +#undef QUEUEBUF_CONF_NUM +#define QUEUEBUF_CONF_NUM 4 +*/ + +/* +#undef SICSLOWPAN_CONF_FRAG +#define SICSLOWPAN_CONF_FRAG 1 +*/ + +#endif /* PROJECT_RPL_WEB_CONF_H_ */ diff --git a/examples/osd/arduino-dallaseprom/run.sh b/examples/osd/arduino-dallaseprom/run.sh new file mode 100755 index 000000000..5d5cbbbb4 --- /dev/null +++ b/examples/osd/arduino-dallaseprom/run.sh @@ -0,0 +1,5 @@ +#!/bin/bash +# For the ages-old bootloader (before 2014) you want to use +# BOOTLOADER_GET_MAC=0x0001f3a0 as parameter to make below. +make clean TARGET=osd-merkur-128 +make TARGET=osd-merkur-128 diff --git a/examples/osd/arduino-dallaseprom/sketch.h b/examples/osd/arduino-dallaseprom/sketch.h new file mode 100644 index 000000000..b48e13237 --- /dev/null +++ b/examples/osd/arduino-dallaseprom/sketch.h @@ -0,0 +1,10 @@ +#ifndef Sketch_h +#define Sketch_h + +struct dstemp{ + float ftemp; + char stemp[8]; +}; +extern struct dstemp ds1820[7]; + +#endif diff --git a/examples/osd/arduino-dallaseprom/sketch.pde b/examples/osd/arduino-dallaseprom/sketch.pde new file mode 100644 index 000000000..2d9101229 --- /dev/null +++ b/examples/osd/arduino-dallaseprom/sketch.pde @@ -0,0 +1,99 @@ +/* + * Sample arduino sketch using contiki features. + * We turn the LED off + * We allow read the moisture sensor + * Unfortunately sleeping for long times in loop() isn't currently + * possible, something turns off the CPU (including PWM outputs) if a + * Proto-Thread is taking too long. We need to find out how to sleep in + * a Contiki-compatible way. + * Note that for a normal arduino sketch you won't have to include any + * of the contiki-specific files here, the sketch should just work. + */ + +#include +#include + +extern "C" { + +#include "arduino-process.h" +#include "rest-engine.h" +#include "sketch.h" + +extern volatile uint8_t mcusleepcycle; // default 16 + +// Data wire is plugged into port 2 on the Arduino +#define ONE_WIRE_BUS 3 + +// Setup a oneWire instance to communicate with any OneWire devices +OneWire oneWire(ONE_WIRE_BUS); +DallasEPROM de(&oneWire); + +extern resource_t res_dtemp1, res_dtemp2, res_battery; + +#define LED_PIN 4 +} + + +void setup (void) +{ + // switch off the led + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, HIGH); + + printf("Dallas Eprom Control Library Demo"); + + Serial1.begin(38400); + // init coap resourcen + rest_init_engine (); + rest_activate_resource (&res_battery, "s/batter"); +} + +// at project-conf.h +// LOOP_INTERVAL (10 * CLOCK_SECOND) +void loop (void) +{ + byte buffer[32]; // Holds one page of data + int status; + + mcu_sleep_off(); + + // Search for the first compatible EPROM/EEPROM on the bus. + // If you have multiple devices you can use de.setAddress() + de.search(); + + // Print out the 1-wire device's 64-bit address + Serial1.print("Address="); + for(int i = 0; i < 8; i++) { + Serial1.print(de.getAddress()[i], HEX); + Serial1.print(" "); + } + Serial1.println(""); + + if (de.getAddress()[0] == 0x00) { + Serial1.println("No device was found!"); + } else { + if (de.validAddress(de.getAddress())) { + Serial1.println("Address CRC is correct."); + + // Uncomment to write to the first page of memory + //strcpy((char*)buffer, "allthingsgeek.com"); + //if ((status = de.writePage(buffer, 0)) != 0) { + //sprintf((char*)buffer, "Error writing page! Code: %d", status); + //Serial1.println((char*)buffer); + //} + + // Read the first page of memory into buffer + if ((status = de.readPage(buffer, 0)) == 0) { + Serial1.println((char*)buffer); + } else { + sprintf((char*)buffer, "Error reading page! Code: %d", status); + Serial1.println((char*)buffer); + } + } else { + Serial1.println("Address CRC is wrong."); + } + } + Serial1.println(""); + + mcu_sleep_on(); +}