From 1d14e05ca83e6be2cd36ab6216f99f742f0c8174 Mon Sep 17 00:00:00 2001 From: Roman3349 Date: Tue, 27 Sep 2016 10:31:36 +0200 Subject: [PATCH 01/71] Add information about examples, homepage and version to @PlatformIO Library Registry manifest file Signed-off-by: Roman3349 --- library.json | 115 ++++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 56 deletions(-) diff --git a/library.json b/library.json index ed23250..1e95e2e 100644 --- a/library.json +++ b/library.json @@ -1,58 +1,61 @@ { -"name": "OneWire", -"frameworks": "Arduino", -"keywords": "onewire, 1-wire, bus, sensor, temperature, ibutton", -"description": "Control 1-Wire protocol (DS18S20, DS18B20, DS2408 and etc)", -"authors": -[ - { - "name": "Paul Stoffregen", - "email": "paul@pjrc.com", - "url": "http://www.pjrc.com", - "maintainer": true - }, - { - "name": "Jim Studt" - }, - { - "name": "Tom Pollard", - "email": "pollard@alum.mit.edu" - }, - { - "name": "Derek Yerger" - }, - { - "name": "Josh Larios" - }, - { - "name": "Robin James" - }, - { - "name": "Glenn Trewitt" - }, - { - "name": "Jason Dangel", - "email": "dangel.jason AT gmail.com" - }, - { - "name": "Guillermo Lovato" - }, - { - "name": "Ken Butcher" - }, - { - "name": "Mark Tillotson" - }, - { - "name": "Bertrik Sikken" - }, - { - "name": "Scott Roberts" - } -], -"repository": -{ - "type": "git", - "url": "https://github.com/PaulStoffregen/OneWire" -} + "name": "OneWire", + "description": "Control 1-Wire protocol (DS18S20, DS18B20, DS2408 and etc)", + "keywords": "onewire, 1-wire, bus, sensor, temperature, ibutton", + "authors": [ + { + "name": "Paul Stoffregen", + "email": "paul@pjrc.com", + "url": "http://www.pjrc.com", + "maintainer": true + }, + { + "name": "Jim Studt" + }, + { + "name": "Tom Pollard", + "email": "pollard@alum.mit.edu" + }, + { + "name": "Derek Yerger" + }, + { + "name": "Josh Larios" + }, + { + "name": "Robin James" + }, + { + "name": "Glenn Trewitt" + }, + { + "name": "Jason Dangel", + "email": "dangel.jason AT gmail.com" + }, + { + "name": "Guillermo Lovato" + }, + { + "name": "Ken Butcher" + }, + { + "name": "Mark Tillotson" + }, + { + "name": "Bertrik Sikken" + }, + { + "name": "Scott Roberts" + } + ], + "repository": { + "type": "git", + "url": "https://github.com/PaulStoffregen/OneWire" + }, + "version": "2.3.2", + "homepage": "https://www.pjrc.com/teensy/td_libs_OneWire.html", + "frameworks": "Arduino", + "examples": [ + "examples/*/*.pde" + ] } From 1ab78dd8858bc5b44a80fe4a664710800117d771 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Mon, 6 Feb 2017 15:57:28 -0800 Subject: [PATCH 02/71] Add comments about tech support --- OneWire.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/OneWire.cpp b/OneWire.cpp index efd6949..3b6fa82 100644 --- a/OneWire.cpp +++ b/OneWire.cpp @@ -5,15 +5,35 @@ 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 +January 2010. + +DO NOT EMAIL for technical support, especially not for ESP chips! +All project support questions must be posted on public forums +relevant to the board or chips used. If using Arduino, post on +Arduino's forum. If using ESP, post on the ESP community forums. +There is ABSOLUTELY NO TECH SUPPORT BY PRIVATE EMAIL! + +Github's issue tracker for OneWire should be used only to report +specific bugs. DO NOT request project support via Github. All +project and tech support questions must be posted on forums, not +github issues. If you experience a problem and you are not +absolutely sure it's an issue with the library, ask on a forum +first. Only use github to report issues after experts have +confirmed the issue is with OneWire rather than your project. + +Back in 2010, OneWire 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. +please contact Paul (this is pretty much the only reason to use +private email about OneWire). + +OneWire is now very mature code. No changes other than adding +definitions for newer hardware support are anticipated. Version 2.3: - Unknonw chip fallback mode, Roger Clark + Unknown chip fallback mode, Roger Clark Teensy-LC compatibility, Paul Stoffregen Search bug fix, Love Nystrom From 1b7a56813a918f6b8f8f5c085bb1c0cd11b5d7a4 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Mon, 6 Feb 2017 16:01:37 -0800 Subject: [PATCH 03/71] Add comment about ESP defs support --- OneWire.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/OneWire.h b/OneWire.h index 50bae6a..189855c 100644 --- a/OneWire.h +++ b/OneWire.h @@ -124,6 +124,11 @@ #define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28 #elif defined(ARDUINO_ARCH_ESP8266) +// Special note: I depend on the ESP community to maintain these definitions and +// submit good pull requests. I can not answer any ESP questions or help you +// resolve any problems related to ESP chips. Please do not contact me and please +// DO NOT CREATE GITHUB ISSUES for ESP support. All ESP questions must be asked +// on ESP community forums. #define PIN_TO_BASEREG(pin) ((volatile uint32_t*) GPO) #define PIN_TO_BITMASK(pin) (1 << pin) #define IO_REG_TYPE uint32_t From 15b2fe5cf230bcf14c3a6c3aacbdaf7a61124197 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Mon, 6 Feb 2017 16:19:06 -0800 Subject: [PATCH 04/71] Bump version to 2.3.3 --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 0946c9a..cd8fc05 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=OneWire -version=2.3.2 +version=2.3.3 author=Jim Studt, Tom Pollard, Robin James, Glenn Trewitt, Jason Dangel, Guillermo Lovato, Paul Stoffregen, Scott Roberts, Bertrik Sikken, Mark Tillotson, Ken Butcher, Roger Clark, Love Nystrom maintainer=Paul Stoffregen sentence=Access 1-wire temperature sensors, memory and other chips. From 6521f58d7a805d23126d7e989dff5623de119a84 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 15 Jul 2017 19:22:18 +0200 Subject: [PATCH 05/71] ignore build-env --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d3fc9b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.xml +*.iml +cmake-build-debug/ From 5f1d67470ee9774ee4e62e2f6b54d8dc58e879a7 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 15 Jul 2017 19:22:38 +0200 Subject: [PATCH 06/71] remove some define-crust from old c --- OneWire.cpp | 45 ++++++++++-------------- OneWire.h | 99 ++++++++++++++++------------------------------------- 2 files changed, 48 insertions(+), 96 deletions(-) diff --git a/OneWire.cpp b/OneWire.cpp index 3b6fa82..b98a848 100644 --- a/OneWire.cpp +++ b/OneWire.cpp @@ -147,9 +147,9 @@ OneWire::OneWire(uint8_t pin) pinMode(pin, INPUT); bitmask = PIN_TO_BITMASK(pin); baseReg = PIN_TO_BASEREG(pin); -#if ONEWIRE_SEARCH - reset_search(); -#endif + + reset_search(); // really needed? + } @@ -161,8 +161,8 @@ OneWire::OneWire(uint8_t pin) // uint8_t OneWire::reset(void) { - IO_REG_TYPE mask = bitmask; - volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; + io_reg_t mask = bitmask; + volatile io_reg_t *reg = baseReg; uint8_t r; uint8_t retries = 125; @@ -195,8 +195,8 @@ uint8_t OneWire::reset(void) // void OneWire::write_bit(uint8_t v) { - IO_REG_TYPE mask=bitmask; - volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; + io_reg_t mask=bitmask; + volatile io_reg_t *reg = baseReg; if (v & 1) { noInterrupts(); @@ -223,8 +223,8 @@ void OneWire::write_bit(uint8_t v) // uint8_t OneWire::read_bit(void) { - IO_REG_TYPE mask=bitmask; - volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; + io_reg_t mask=bitmask; + volatile io_reg_t *reg = baseReg; uint8_t r; noInterrupts(); @@ -316,8 +316,6 @@ void OneWire::depower() 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. @@ -326,7 +324,7 @@ void OneWire::reset_search() { // reset the search state LastDiscrepancy = 0; - LastDeviceFlag = FALSE; + LastDeviceFlag = false; LastFamilyDiscrepancy = 0; for(int i = 7; ; i--) { ROM_NO[i] = 0; @@ -345,7 +343,7 @@ void OneWire::target_search(uint8_t family_code) ROM_NO[i] = 0; LastDiscrepancy = 64; LastFamilyDiscrepancy = 0; - LastDeviceFlag = FALSE; + LastDeviceFlag = 0; } // @@ -387,13 +385,13 @@ uint8_t OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) { // reset the search LastDiscrepancy = 0; - LastDeviceFlag = FALSE; + LastDeviceFlag = 0; LastFamilyDiscrepancy = 0; - return FALSE; + return 0; } // issue the search command - if (search_mode == true) { + if (search_mode) { write(0xF0); // NORMAL SEARCH } else { write(0xEC); // CONDITIONAL SEARCH @@ -468,9 +466,9 @@ uint8_t OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) // check for last device if (LastDiscrepancy == 0) - LastDeviceFlag = TRUE; + LastDeviceFlag = true; - search_result = TRUE; + search_result = true; } } @@ -478,18 +476,17 @@ uint8_t OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) if (!search_result || !ROM_NO[0]) { LastDiscrepancy = 0; - LastDeviceFlag = FALSE; + LastDeviceFlag = 0; LastFamilyDiscrepancy = 0; - search_result = FALSE; + 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" // @@ -557,7 +554,6 @@ uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) } #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); @@ -592,6 +588,3 @@ uint16_t OneWire::crc16(const uint8_t* input, uint16_t len, uint16_t crc) #endif return crc; } -#endif - -#endif diff --git a/OneWire.h b/OneWire.h index 189855c..1be6744 100644 --- a/OneWire.h +++ b/OneWire.h @@ -1,37 +1,14 @@ #ifndef OneWire_h #define OneWire_h -#include - #if defined(__AVR__) #include #endif -#if ARDUINO >= 100 -#include "Arduino.h" // for delayMicroseconds, digitalPinToBitMask, etc +#if defined(ARDUINO) && ARDUINO >= 100 +#include // 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 +#include #endif // Select the table-lookup method of computing the 8-bit CRC @@ -43,53 +20,37 @@ #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 - -#ifndef FALSE -#define FALSE 0 -#endif -#ifndef TRUE -#define TRUE 1 -#endif - -// Platform specific I/O definitions +// Platform specific I/O definitions TODO: outsource #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)) +using io_reg_t = uint8_t; // define special datatype for register-access #elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) #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) +using io_reg_t = uint8_t; // define special datatype for register-access #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)) +using io_reg_t = uint8_t; // define special datatype for register-access #elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__) // Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due. @@ -98,8 +59,6 @@ // 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)) @@ -111,17 +70,17 @@ #ifndef pgm_read_byte #define pgm_read_byte(addr) (*(const uint8_t *)(addr)) #endif +using io_reg_t = uint32_t; // define special datatype for register-access #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 +using io_reg_t = uint32_t; // define special datatype for register-access #elif defined(ARDUINO_ARCH_ESP8266) // Special note: I depend on the ESP community to maintain these definitions and @@ -131,35 +90,32 @@ // on ESP community forums. #define PIN_TO_BASEREG(pin) ((volatile uint32_t*) GPO) #define PIN_TO_BITMASK(pin) (1 << pin) -#define IO_REG_TYPE uint32_t -#define IO_REG_ASM #define DIRECT_READ(base, mask) ((GPI & (mask)) ? 1 : 0) //GPIO_IN_ADDRESS #define DIRECT_MODE_INPUT(base, mask) (GPE &= ~(mask)) //GPIO_ENABLE_W1TC_ADDRESS #define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS #define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS #define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS +using io_reg_t = uint32_t; // define special datatype for register-access #elif defined(__SAMD21G18A__) #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)+8)) & (mask)) ? 1 : 0) #define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) = (mask)) #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask)) #define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask)) #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask)) +using io_reg_t = uint32_t; // define special datatype for register-access #elif defined(RBL_NRF51822) #define PIN_TO_BASEREG(pin) (0) #define PIN_TO_BITMASK(pin) (pin) -#define IO_REG_TYPE uint32_t -#define IO_REG_ASM #define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin) #define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin) #define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) #define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) #define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) +using io_reg_t = uint32_t; // define special datatype for register-access #elif defined(__arc__) /* Arduino101/Genuino101 specifics */ @@ -178,8 +134,7 @@ /* GPIO registers base address */ #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) #define PIN_TO_BITMASK(pin) pin -#define IO_REG_TYPE uint32_t -#define IO_REG_ASM +using io_reg_t = uint32_t; // define special datatype for register-access static inline __attribute__((always_inline)) IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) @@ -244,33 +199,38 @@ void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) #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) +using io_reg_t = uint32_t; // define special datatype for register-access + #warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture." +#define INPUT 1 +#define OUTPUT 0 +#define HIGH 1 +#define LOW 0 + #endif class OneWire { private: - IO_REG_TYPE bitmask; - volatile IO_REG_TYPE *baseReg; -#if ONEWIRE_SEARCH + io_reg_t bitmask; + volatile io_reg_t *baseReg; + // 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 @@ -311,7 +271,7 @@ class OneWire // 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(); @@ -326,14 +286,14 @@ class OneWire // get garbage. The order is deterministic. You will always get // the same devices in the same order. uint8_t search(uint8_t *newAddr, bool search_mode = true); -#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. @@ -369,8 +329,7 @@ class OneWire // @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 From e0900365edd26035b1a890b0b0c08a2447fa665e Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 15 Jul 2017 19:23:03 +0200 Subject: [PATCH 07/71] bump ver --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index cd8fc05..dfcc56b 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=OneWire -version=2.3.3 +version=2.4.0 author=Jim Studt, Tom Pollard, Robin James, Glenn Trewitt, Jason Dangel, Guillermo Lovato, Paul Stoffregen, Scott Roberts, Bertrik Sikken, Mark Tillotson, Ken Butcher, Roger Clark, Love Nystrom maintainer=Paul Stoffregen sentence=Access 1-wire temperature sensors, memory and other chips. From a72a53574cd8e493718dfea05fe62f3bdce045f6 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 15 Jul 2017 19:25:12 +0200 Subject: [PATCH 08/71] move source --- OneWire.cpp => src/OneWire.cpp | 0 OneWire.h => src/OneWire.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename OneWire.cpp => src/OneWire.cpp (100%) rename OneWire.h => src/OneWire.h (100%) diff --git a/OneWire.cpp b/src/OneWire.cpp similarity index 100% rename from OneWire.cpp rename to src/OneWire.cpp diff --git a/OneWire.h b/src/OneWire.h similarity index 100% rename from OneWire.h rename to src/OneWire.h From 6b7abb293efad6a4d4db2f204ded5c2ea7bfe3f4 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 15 Jul 2017 19:51:17 +0200 Subject: [PATCH 09/71] reduce datatypes, const correctness and lot more explicit coding --- src/OneWire.cpp | 120 ++++++++++++++++++++++++++---------------------- src/OneWire.h | 28 +++++++---- 2 files changed, 84 insertions(+), 64 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index b98a848..6e20055 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -141,15 +141,13 @@ sample code bearing this copyright. #include "OneWire.h" - -OneWire::OneWire(uint8_t pin) +OneWire::OneWire(const uint8_t pin) { pinMode(pin, INPUT); bitmask = PIN_TO_BITMASK(pin); baseReg = PIN_TO_BASEREG(pin); reset_search(); // really needed? - } @@ -159,11 +157,11 @@ OneWire::OneWire(uint8_t pin) // // Returns 1 if a device asserted a presence pulse, 0 otherwise. // -uint8_t OneWire::reset(void) +bool OneWire::reset() { io_reg_t mask = bitmask; volatile io_reg_t *reg = baseReg; - uint8_t r; + bool success; uint8_t retries = 125; noInterrupts(); @@ -183,22 +181,22 @@ uint8_t OneWire::reset(void) noInterrupts(); DIRECT_MODE_INPUT(reg, mask); // allow it to float delayMicroseconds(70); - r = !DIRECT_READ(reg, mask); + success = !DIRECT_READ(reg, mask); interrupts(); delayMicroseconds(410); - return r; + return success; } // // Write a bit. Port and bit is used to cut lookup time and provide // more certain timing. // -void OneWire::write_bit(uint8_t v) +void OneWire::write_bit(const bool value) { io_reg_t mask=bitmask; volatile io_reg_t *reg = baseReg; - if (v & 1) { + if (value) { noInterrupts(); DIRECT_WRITE_LOW(reg, mask); DIRECT_MODE_OUTPUT(reg, mask); // drive output low @@ -221,11 +219,11 @@ void OneWire::write_bit(uint8_t v) // Read a bit. Port and bit is used to cut lookup time and provide // more certain timing. // -uint8_t OneWire::read_bit(void) +bool OneWire::read_bit() { io_reg_t mask=bitmask; volatile io_reg_t *reg = baseReg; - uint8_t r; + bool value; noInterrupts(); DIRECT_MODE_OUTPUT(reg, mask); @@ -233,10 +231,10 @@ uint8_t OneWire::read_bit(void) delayMicroseconds(3); DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise delayMicroseconds(10); - r = DIRECT_READ(reg, mask); + value = DIRECT_READ(reg, mask); interrupts(); delayMicroseconds(53); - return r; + return value; } // @@ -246,13 +244,15 @@ uint8_t OneWire::read_bit(void) // 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; +void OneWire::write(const uint8_t value, const bool power) +{ - for (bitMask = 0x01; bitMask; bitMask <<= 1) { - OneWire::write_bit( (bitMask & v)?1:0); + for (uint8_t bitMask = 0x01; bitMask != 0; bitMask <<= 1) + { + OneWire::write_bit( (bitMask & value)?1:0); } - if ( !power) { + if (!power) + { noInterrupts(); DIRECT_MODE_INPUT(baseReg, bitmask); DIRECT_WRITE_LOW(baseReg, bitmask); @@ -260,10 +260,15 @@ void OneWire::write(uint8_t v, uint8_t power /* = 0 */) { } } -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) { +void OneWire::write_bytes(const uint8_t *buf, const uint16_t count, const bool power) +{ + for (uint16_t index = 0 ; index < count ; index++) + { + write(buf[index]); + } + + if (!power) + { noInterrupts(); DIRECT_MODE_INPUT(baseReg, bitmask); DIRECT_WRITE_LOW(baseReg, bitmask); @@ -274,19 +279,24 @@ void OneWire::write_bytes(const uint8_t *buf, uint16_t count, bool power /* = 0 // // Read a byte // -uint8_t OneWire::read() { - uint8_t bitMask; - uint8_t r = 0; +uint8_t OneWire::read() +{ + uint8_t value = 0; - for (bitMask = 0x01; bitMask; bitMask <<= 1) { - if ( OneWire::read_bit()) r |= bitMask; + for (uint8_t bitMask = 0x01; bitMask != 0; bitMask <<= 1) + { + if ( OneWire::read_bit()) value |= bitMask; } - return r; + return value; } -void OneWire::read_bytes(uint8_t *buf, uint16_t count) { - for (uint16_t i = 0 ; i < count ; i++) - buf[i] = read(); +void OneWire::read_bytes(uint8_t * const buf, const uint16_t count) +{ + for (uint16_t index = 0 ; index < count ; index++) + { + buf[index] = read(); + } + } // @@ -294,11 +304,12 @@ void OneWire::read_bytes(uint8_t *buf, uint16_t count) { // void OneWire::select(const uint8_t rom[8]) { - uint8_t i; - write(0x55); // Choose ROM - for (i = 0; i < 8; i++) write(rom[i]); + for (uint8_t index = 0; index < 8; index++) + { + write(rom[index]); + } } // @@ -324,23 +335,26 @@ void OneWire::reset_search() { // reset the search state LastDiscrepancy = 0; - LastDeviceFlag = false; + LastDeviceFlag = 0; LastFamilyDiscrepancy = 0; - for(int i = 7; ; i--) { - ROM_NO[i] = 0; - if ( i == 0) break; + for(int index = 7; ; index--) + { + ROM_NO[index] = 0; + if ( index == 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) +void OneWire::target_search(const 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; + for (uint8_t index = 1; index < 8; index++) + { + ROM_NO[index] = 0; + } LastDiscrepancy = 64; LastFamilyDiscrepancy = 0; LastDeviceFlag = 0; @@ -362,20 +376,16 @@ void OneWire::target_search(uint8_t family_code) // Return TRUE : device found, ROM number in ROM_NO buffer // FALSE : device not found, end of search // -uint8_t OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) +uint8_t OneWire::search(uint8_t * const newAddr, const bool search_mode) { - uint8_t id_bit_number; - uint8_t last_zero, rom_byte_number, search_result; - uint8_t id_bit, cmp_id_bit; + uint8_t id_bit_number = 1; + uint8_t last_zero = 0; + uint8_t rom_byte_number = 0; + uint8_t search_result = 0; - unsigned char rom_byte_mask, search_direction; + bool id_bit, cmp_id_bit; - // initialize for search - id_bit_number = 1; - last_zero = 0; - rom_byte_number = 0; - rom_byte_mask = 1; - search_result = 0; + unsigned char rom_byte_mask = 1, search_direction; // if the last call was not the last one if (!LastDeviceFlag) @@ -385,7 +395,7 @@ uint8_t OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) { // reset the search LastDiscrepancy = 0; - LastDeviceFlag = 0; + LastDeviceFlag = false; LastFamilyDiscrepancy = 0; return 0; } @@ -406,7 +416,9 @@ uint8_t OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) // check for no devices on 1-wire if ((id_bit == 1) && (cmp_id_bit == 1)) - break; + { + break; + } else { // all devices coupled have 0 or 1 diff --git a/src/OneWire.h b/src/OneWire.h index 1be6744..5c312c7 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -227,49 +227,57 @@ class OneWire unsigned char ROM_NO[8]; uint8_t LastDiscrepancy; uint8_t LastFamilyDiscrepancy; - uint8_t LastDeviceFlag; + bool LastDeviceFlag; public: - OneWire( uint8_t pin); + explicit OneWire(uint8_t pin); + + ~OneWire() = default; // nothing special to do here + + OneWire(const OneWire& ow) = delete; // disallow copy constructor + OneWire(OneWire&& ow) = default; // default move constructor + OneWire& operator=(OneWire& ow) = delete; // disallow copy assignment + OneWire& operator=(const OneWire& ow) = delete; // disallow copy assignment + OneWire& operator=(OneWire&& ow) = delete; // disallow move assignment // 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); + bool reset(); // 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); + void skip(); // 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(uint8_t value, bool power = 0); - void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0); + void write_bytes(const uint8_t *buf, uint16_t count, bool power = false); // Read a byte. - uint8_t read(void); + uint8_t read(); 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); + void write_bit(bool v); // Read a bit. - uint8_t read_bit(void); + bool read_bit(); // 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); + void depower(); // Clear the search state so that if will start from the beginning again. From 4360fc5b9610433605955e48bb5080577044ec76 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 15 Jul 2017 20:02:12 +0200 Subject: [PATCH 10/71] simplify pin access --- src/OneWire.cpp | 95 ++++++++++++++++++++++++------------------------- src/OneWire.h | 6 ++-- 2 files changed, 50 insertions(+), 51 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 6e20055..4b8b787 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -143,9 +143,11 @@ sample code bearing this copyright. OneWire::OneWire(const uint8_t pin) { - pinMode(pin, INPUT); - bitmask = PIN_TO_BITMASK(pin); - baseReg = PIN_TO_BASEREG(pin); + // prepare pin + pinMode(pin, INPUT); + pin_bitMask = PIN_TO_BITMASK(pin); + pin_baseReg = PIN_TO_BASEREG(pin); + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); reset_search(); // really needed? } @@ -159,29 +161,27 @@ OneWire::OneWire(const uint8_t pin) // bool OneWire::reset() { - io_reg_t mask = bitmask; - volatile io_reg_t *reg = baseReg; bool success; uint8_t retries = 125; noInterrupts(); - DIRECT_MODE_INPUT(reg, mask); + DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); interrupts(); // wait until the wire is high... just in case do { if (--retries == 0) return 0; delayMicroseconds(2); - } while ( !DIRECT_READ(reg, mask)); + } while ( !DIRECT_READ(pin_baseReg, pin_bitMask)); noInterrupts(); - DIRECT_WRITE_LOW(reg, mask); - DIRECT_MODE_OUTPUT(reg, mask); // drive output low + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low interrupts(); delayMicroseconds(480); noInterrupts(); - DIRECT_MODE_INPUT(reg, mask); // allow it to float + DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); // allow it to float delayMicroseconds(70); - success = !DIRECT_READ(reg, mask); + success = !DIRECT_READ(pin_baseReg, pin_bitMask); interrupts(); delayMicroseconds(410); return success; @@ -193,23 +193,20 @@ bool OneWire::reset() // void OneWire::write_bit(const bool value) { - io_reg_t mask=bitmask; - volatile io_reg_t *reg = baseReg; - if (value) { noInterrupts(); - DIRECT_WRITE_LOW(reg, mask); - DIRECT_MODE_OUTPUT(reg, mask); // drive output low + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low delayMicroseconds(10); - DIRECT_WRITE_HIGH(reg, mask); // drive output high + DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high interrupts(); delayMicroseconds(55); } else { noInterrupts(); - DIRECT_WRITE_LOW(reg, mask); - DIRECT_MODE_OUTPUT(reg, mask); // drive output low + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low delayMicroseconds(65); - DIRECT_WRITE_HIGH(reg, mask); // drive output high + DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high interrupts(); delayMicroseconds(5); } @@ -221,17 +218,13 @@ void OneWire::write_bit(const bool value) // bool OneWire::read_bit() { - io_reg_t mask=bitmask; - volatile io_reg_t *reg = baseReg; - bool value; - noInterrupts(); - DIRECT_MODE_OUTPUT(reg, mask); - DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); delayMicroseconds(3); - DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise + DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); // let pin float, pull up will raise delayMicroseconds(10); - value = DIRECT_READ(reg, mask); + const bool value = DIRECT_READ(pin_baseReg, pin_bitMask); interrupts(); delayMicroseconds(53); return value; @@ -249,13 +242,13 @@ void OneWire::write(const uint8_t value, const bool power) for (uint8_t bitMask = 0x01; bitMask != 0; bitMask <<= 1) { - OneWire::write_bit( (bitMask & value)?1:0); + OneWire::write_bit((bitMask & value) != 0); } if (!power) { noInterrupts(); - DIRECT_MODE_INPUT(baseReg, bitmask); - DIRECT_WRITE_LOW(baseReg, bitmask); + DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); interrupts(); } } @@ -270,8 +263,8 @@ void OneWire::write_bytes(const uint8_t *buf, const uint16_t count, const bool p if (!power) { noInterrupts(); - DIRECT_MODE_INPUT(baseReg, bitmask); - DIRECT_WRITE_LOW(baseReg, bitmask); + DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); interrupts(); } } @@ -323,7 +316,7 @@ void OneWire::skip() void OneWire::depower() { noInterrupts(); - DIRECT_MODE_INPUT(baseReg, bitmask); + DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); interrupts(); } @@ -335,9 +328,9 @@ void OneWire::reset_search() { // reset the search state LastDiscrepancy = 0; - LastDeviceFlag = 0; + LastDeviceFlag = false; LastFamilyDiscrepancy = 0; - for(int index = 7; ; index--) + for(uint8_t index = 7; ; index--) { ROM_NO[index] = 0; if ( index == 0) break; @@ -357,7 +350,7 @@ void OneWire::target_search(const uint8_t family_code) } LastDiscrepancy = 64; LastFamilyDiscrepancy = 0; - LastDeviceFlag = 0; + LastDeviceFlag = false; } // @@ -376,16 +369,17 @@ void OneWire::target_search(const uint8_t family_code) // Return TRUE : device found, ROM number in ROM_NO buffer // FALSE : device not found, end of search // -uint8_t OneWire::search(uint8_t * const newAddr, const bool search_mode) +bool OneWire::search(uint8_t * const newAddr, const bool search_mode) { uint8_t id_bit_number = 1; uint8_t last_zero = 0; uint8_t rom_byte_number = 0; - uint8_t search_result = 0; + bool search_result = false; bool id_bit, cmp_id_bit; - unsigned char rom_byte_mask = 1, search_direction; + uint8_t rom_byte_mask = 1; + bool search_direction; // if the last call was not the last one if (!LastDeviceFlag) @@ -415,7 +409,7 @@ uint8_t OneWire::search(uint8_t * const newAddr, const bool search_mode) cmp_id_bit = read_bit(); // check for no devices on 1-wire - if ((id_bit == 1) && (cmp_id_bit == 1)) + if ((id_bit) && (cmp_id_bit)) { break; } @@ -435,7 +429,7 @@ uint8_t OneWire::search(uint8_t * const newAddr, const bool search_mode) search_direction = (id_bit_number == LastDiscrepancy); // if 0 was picked then record its position in LastZero - if (search_direction == 0) + if (!search_direction) { last_zero = id_bit_number; @@ -447,7 +441,7 @@ uint8_t OneWire::search(uint8_t * const newAddr, const bool search_mode) // set or clear the bit in the ROM byte rom_byte_number // with mask rom_byte_mask - if (search_direction == 1) + if (search_direction) ROM_NO[rom_byte_number] |= rom_byte_mask; else ROM_NO[rom_byte_number] &= ~rom_byte_mask; @@ -471,7 +465,7 @@ uint8_t OneWire::search(uint8_t * const newAddr, const bool search_mode) while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 // if the search was successful then - if (!(id_bit_number < 65)) + if (id_bit_number >= 65) { // search successful so set LastDiscrepancy,LastDeviceFlag,search_result LastDiscrepancy = last_zero; @@ -485,14 +479,19 @@ uint8_t OneWire::search(uint8_t * const newAddr, const bool search_mode) } // if no device found then reset counters so next 'search' will be like a first - if (!search_result || !ROM_NO[0]) + if (!search_result || (ROM_NO[0] == 0)) { LastDiscrepancy = 0; - LastDeviceFlag = 0; + LastDeviceFlag = false; LastFamilyDiscrepancy = 0; search_result = false; - } else { - for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i]; + } + else + { + for (int index = 0; index < 8; index++) + { + newAddr[index] = ROM_NO[index]; + } } return search_result; } diff --git a/src/OneWire.h b/src/OneWire.h index 5c312c7..fc2a107 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -220,8 +220,8 @@ class OneWire { private: - io_reg_t bitmask; - volatile io_reg_t *baseReg; + io_reg_t pin_bitMask; + volatile io_reg_t *pin_baseReg; // global search state unsigned char ROM_NO[8]; @@ -293,7 +293,7 @@ class OneWire // 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, bool search_mode = true); + bool search(uint8_t *newAddr, bool search_mode = true); From c241c3041958dac6cb1798172e34d2dbe106339e Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 15 Jul 2017 20:20:42 +0200 Subject: [PATCH 11/71] extend crc interface --- src/OneWire.cpp | 32 +++++++++++++++++++------------- src/OneWire.h | 4 ++-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 4b8b787..95a022b 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -391,7 +391,7 @@ bool OneWire::search(uint8_t * const newAddr, const bool search_mode) LastDiscrepancy = 0; LastDeviceFlag = false; LastFamilyDiscrepancy = 0; - return 0; + return false; } // issue the search command @@ -530,12 +530,12 @@ static const uint8_t PROGMEM dscrc_table[] = { // 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 OneWire::crc8(const uint8_t address[], uint8_t length, const uint8_t crc_init) { - uint8_t crc = 0; + uint8_t crc = crc_init; - while (len--) { - crc = pgm_read_byte(dscrc_table + (crc ^ *addr++)); + while (length-- > 0) { + crc = pgm_read_byte(dscrc_table + (crc ^ *address++)); } return crc; } @@ -571,25 +571,31 @@ bool OneWire::check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inv 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) +uint16_t OneWire::crc16(const uint8_t address[], const uint16_t length, const uint16_t crc_init) { + uint16_t crc = crc_init; // init value + #if defined(__AVR__) - for (uint16_t i = 0 ; i < len ; i++) { - crc = _crc16_update(crc, input[i]); + for (uint16_t i = 0 ; i < length ; i++) + { + crc = _crc16_update(crc_init, address[i]); } #else 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, + for (uint16_t index = 0; index < length ; index++) + { + // Even though we're just copying a byte from the address, // we'll be doing 16-bit computation with it. - uint16_t cdata = input[i]; - cdata = (cdata ^ crc) & 0xff; + uint16_t cdata = address[index]; + cdata = (cdata ^ crc) & static_cast(0xff); crc >>= 8; - if (oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4]) + if ((oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4]) != 0) + { crc ^= 0xC001; + } cdata <<= 6; crc ^= cdata; diff --git a/src/OneWire.h b/src/OneWire.h index fc2a107..d90ae27 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -299,7 +299,7 @@ class OneWire // 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); + static uint8_t crc8(const uint8_t *address, uint8_t length, uint8_t crc_init = 0); // Compute the 1-Wire CRC16 and compare it against the received CRC. @@ -336,7 +336,7 @@ class OneWire // @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); + static uint16_t crc16(const uint8_t* address, uint16_t length, uint16_t crc_init = 0); }; From f83fb7c473e50ceb8898c81013b8e46a9f516c4b Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 12:19:50 +0200 Subject: [PATCH 12/71] separate hardware dependend code and optimize variable names --- src/OneWire.cpp | 94 +++++++------- src/OneWire.h | 217 ++----------------------------- src/platform.cpp | 22 ++++ src/platform.h | 328 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 408 insertions(+), 253 deletions(-) create mode 100644 src/platform.cpp create mode 100644 src/platform.h diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 95a022b..7d2357d 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -161,7 +161,6 @@ OneWire::OneWire(const uint8_t pin) // bool OneWire::reset() { - bool success; uint8_t retries = 125; noInterrupts(); @@ -169,7 +168,7 @@ bool OneWire::reset() interrupts(); // wait until the wire is high... just in case do { - if (--retries == 0) return 0; + if (--retries == 0) return false; delayMicroseconds(2); } while ( !DIRECT_READ(pin_baseReg, pin_bitMask)); @@ -181,7 +180,7 @@ bool OneWire::reset() noInterrupts(); DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); // allow it to float delayMicroseconds(70); - success = !DIRECT_READ(pin_baseReg, pin_bitMask); + const bool success = !DIRECT_READ(pin_baseReg, pin_bitMask); interrupts(); delayMicroseconds(410); return success; @@ -242,7 +241,7 @@ void OneWire::write(const uint8_t value, const bool power) for (uint8_t bitMask = 0x01; bitMask != 0; bitMask <<= 1) { - OneWire::write_bit((bitMask & value) != 0); + write_bit((bitMask & value) != 0); // TODO: shifting value could be me faster } if (!power) { @@ -253,9 +252,9 @@ void OneWire::write(const uint8_t value, const bool power) } } -void OneWire::write_bytes(const uint8_t *buf, const uint16_t count, const bool power) +void OneWire::write_bytes(const uint8_t *buf, const uint16_t data_size, const bool power) { - for (uint16_t index = 0 ; index < count ; index++) + for (uint16_t index = 0 ; index < data_size ; index++) // TODO: can be tuned { write(buf[index]); } @@ -278,30 +277,36 @@ uint8_t OneWire::read() for (uint8_t bitMask = 0x01; bitMask != 0; bitMask <<= 1) { - if ( OneWire::read_bit()) value |= bitMask; + if (read_bit()) value |= bitMask; } return value; } -void OneWire::read_bytes(uint8_t * const buf, const uint16_t count) +void OneWire::read_bytes(uint8_t data_array[], uint16_t data_size) { - for (uint16_t index = 0 ; index < count ; index++) + for (uint16_t index = 0 ; index < data_size ; index++) + // while (data_size-- > 0) { - buf[index] = read(); - } + data_array[index] = read(); + + //*data_array = read(); // TODO: test if it can be combined in one line + //*data_array = read(); // TODO: test if it can be combined in one line + //data_array++; + } } // // Do a ROM select // -void OneWire::select(const uint8_t rom[8]) +void OneWire::select(const uint8_t rom_array[8]) { write(0x55); // Choose ROM - for (uint8_t index = 0; index < 8; index++) + uint8_t rom_size = 8; // clean solution with for(index) seems to be 6 byte larger + while (rom_size-- > 0) { - write(rom[index]); + write(*rom_array++); } } @@ -330,11 +335,8 @@ void OneWire::reset_search() LastDiscrepancy = 0; LastDeviceFlag = false; LastFamilyDiscrepancy = 0; - for(uint8_t index = 7; ; index--) - { - ROM_NO[index] = 0; - if ( index == 0) break; - } + + memset(ROM_NO, 0, 8); } // Setup the search to find the device type 'family_code' on the next call @@ -344,10 +346,7 @@ void OneWire::target_search(const uint8_t family_code) { // set the search state to find SearchFamily type devices ROM_NO[0] = family_code; - for (uint8_t index = 1; index < 8; index++) - { - ROM_NO[index] = 0; - } + memset(&ROM_NO[1], 0, 7); LastDiscrepancy = 64; LastFamilyDiscrepancy = 0; LastDeviceFlag = false; @@ -505,7 +504,7 @@ bool OneWire::search(uint8_t * const newAddr, const bool search_mode) #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[] = { +static const uint8_t PROGMEM crc_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, @@ -530,12 +529,13 @@ static const uint8_t PROGMEM dscrc_table[] = { // 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 address[], uint8_t length, const uint8_t crc_init) +uint8_t OneWire::crc8(const uint8_t data_array[], uint8_t data_size, const uint8_t crc_init) { uint8_t crc = crc_init; - while (length-- > 0) { - crc = pgm_read_byte(dscrc_table + (crc ^ *address++)); + for (uint8_t index = 0; index < data_size; ++index) + { + crc = pgm_read_byte(crc_table + (crc ^ data_array[index])); } return crc; } @@ -544,20 +544,22 @@ uint8_t OneWire::crc8(const uint8_t address[], uint8_t length, const uint8_t crc // 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 OneWire::crc8(const uint8_t data_array[], uint8_t data_size, const uint8_t crc_init) { - uint8_t crc = 0; + uint8_t crc = crc_init; - while (len--) { + while (data_size-- > 0) // clean solution with for(index) seems to be 6 byte larger + { #if defined(__AVR__) - crc = _crc_ibutton_update(crc, *addr++); + crc = _crc_ibutton_update(crc, *data_array++); #else - uint8_t inbyte = *addr++; - for (uint8_t i = 8; i; i--) { - uint8_t mix = (crc ^ inbyte) & 0x01; + uint8_t inByte = *data_array++; + for (uint8_t bitPosition = 0; bitPosition < 8; bitPosition++) + { + const uint8_t mix = (crc ^ inByte) & static_cast(0x01); crc >>= 1; - if (mix) crc ^= 0x8C; - inbyte >>= 1; + if (mix != 0) crc ^= 0x8C; + inByte >>= 1; } #endif } @@ -565,34 +567,34 @@ uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) } #endif -bool OneWire::check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc) +bool OneWire::check_crc16(const uint8_t data_array[], const uint16_t data_size, const uint8_t* inverted_crc, uint16_t crc) { - crc = ~crc16(input, len, crc); + crc = ~crc16(data_array, data_size, crc); return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1]; } -uint16_t OneWire::crc16(const uint8_t address[], const uint16_t length, const uint16_t crc_init) +uint16_t OneWire::crc16(const uint8_t data_array[], uint16_t data_size, const uint16_t crc_init) { uint16_t crc = crc_init; // init value #if defined(__AVR__) - for (uint16_t i = 0 ; i < length ; i++) + while (data_size-- > 0) { - crc = _crc16_update(crc_init, address[i]); + crc = _crc16_update(crc, *data_array++); } #else - static const uint8_t oddparity[16] = + 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 index = 0; index < length ; index++) + while (data_size-- > 0) // clean solution with for(index) seems to be 6 byte larger { - // Even though we're just copying a byte from the address, + // Even though we're just copying a byte from the data_array, // we'll be doing 16-bit computation with it. - uint16_t cdata = address[index]; + uint16_t cdata = *data_array++; cdata = (cdata ^ crc) & static_cast(0xff); crc >>= 8; - if ((oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4]) != 0) + if ((oddParity[cdata & 0x0F] ^ oddParity[cdata >> 4]) != 0) { crc ^= 0xC001; } diff --git a/src/OneWire.h b/src/OneWire.h index d90ae27..a537f7d 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -1,16 +1,12 @@ #ifndef OneWire_h #define OneWire_h -#if defined(__AVR__) -#include -#endif - #if defined(ARDUINO) && ARDUINO >= 100 #include // for delayMicroseconds, digitalPinToBitMask, etc -#else -#include #endif +#include "platform.h" + // 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 @@ -20,200 +16,7 @@ #define ONEWIRE_CRC8_TABLE 1 #endif -// Platform specific I/O definitions TODO: outsource - -#if defined(__AVR__) -#define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) -#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) -#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)) -using io_reg_t = uint8_t; // define special datatype for register-access - -#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) -#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) -#define PIN_TO_BITMASK(pin) (1) -#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) -using io_reg_t = uint8_t; // define special datatype for register-access - -#elif defined(__MKL26Z64__) -#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) -#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) -#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)) -using io_reg_t = uint8_t; // define special datatype for register-access - -#elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__) -// 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 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 -using io_reg_t = uint32_t; // define special datatype for register-access - -#elif defined(__PIC32MX__) -#define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) -#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) -#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 -using io_reg_t = uint32_t; // define special datatype for register-access - -#elif defined(ARDUINO_ARCH_ESP8266) -// Special note: I depend on the ESP community to maintain these definitions and -// submit good pull requests. I can not answer any ESP questions or help you -// resolve any problems related to ESP chips. Please do not contact me and please -// DO NOT CREATE GITHUB ISSUES for ESP support. All ESP questions must be asked -// on ESP community forums. -#define PIN_TO_BASEREG(pin) ((volatile uint32_t*) GPO) -#define PIN_TO_BITMASK(pin) (1 << pin) -#define DIRECT_READ(base, mask) ((GPI & (mask)) ? 1 : 0) //GPIO_IN_ADDRESS -#define DIRECT_MODE_INPUT(base, mask) (GPE &= ~(mask)) //GPIO_ENABLE_W1TC_ADDRESS -#define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS -#define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS -#define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS -using io_reg_t = uint32_t; // define special datatype for register-access - -#elif defined(__SAMD21G18A__) -#define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) -#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) -#define DIRECT_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0) -#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) = (mask)) -#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask)) -#define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask)) -#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask)) -using io_reg_t = uint32_t; // define special datatype for register-access - -#elif defined(RBL_NRF51822) -#define PIN_TO_BASEREG(pin) (0) -#define PIN_TO_BITMASK(pin) (pin) -#define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin) -#define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin) -#define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) -#define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) -#define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) -using io_reg_t = uint32_t; // define special datatype for register-access - -#elif defined(__arc__) /* Arduino101/Genuino101 specifics */ - -#include "scss_registers.h" -#include "portable.h" -#include "avr/pgmspace.h" - -#define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId) -#define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType) -#define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase) -#define DIR_OFFSET_SS 0x01 -#define DIR_OFFSET_SOC 0x04 -#define EXT_PORT_OFFSET_SS 0x0A -#define EXT_PORT_OFFSET_SOC 0x50 - -/* GPIO registers base address */ -#define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) -#define PIN_TO_BITMASK(pin) pin -using io_reg_t = uint32_t; // define special datatype for register-access - -static inline __attribute__((always_inline)) -IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) -{ - IO_REG_TYPE ret; - if (SS_GPIO == GPIO_TYPE(pin)) { - ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS)); - } else { - ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC); - } - return ((ret >> GPIO_ID(pin)) & 0x01); -} - -static inline __attribute__((always_inline)) -void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) -{ - if (SS_GPIO == GPIO_TYPE(pin)) { - WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), - ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); - } else { - MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); - } -} - -static inline __attribute__((always_inline)) -void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) -{ - if (SS_GPIO == GPIO_TYPE(pin)) { - WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), - ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); - } else { - MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); - } -} - -static inline __attribute__((always_inline)) -void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) -{ - if (SS_GPIO == GPIO_TYPE(pin)) { - WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base); - } else { - MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin)); - } -} - -static inline __attribute__((always_inline)) -void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) -{ - if (SS_GPIO == GPIO_TYPE(pin)) { - WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base); - } else { - MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin)); - } -} - -#define DIRECT_READ(base, pin) directRead(base, pin) -#define DIRECT_MODE_INPUT(base, pin) directModeInput(base, pin) -#define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(base, pin) -#define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin) -#define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(base, pin) - -#else -#define PIN_TO_BASEREG(pin) (0) -#define PIN_TO_BITMASK(pin) (pin) -#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) -using io_reg_t = uint32_t; // define special datatype for register-access - -#warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture." - -#define INPUT 1 -#define OUTPUT 0 -#define HIGH 1 -#define LOW 0 -#endif class OneWire @@ -224,7 +27,7 @@ class OneWire volatile io_reg_t *pin_baseReg; // global search state - unsigned char ROM_NO[8]; + uint8_t ROM_NO[8]; uint8_t LastDiscrepancy; uint8_t LastFamilyDiscrepancy; bool LastDeviceFlag; @@ -247,7 +50,7 @@ class OneWire bool reset(); // Issue a 1-Wire rom select command, you do the reset first. - void select(const uint8_t rom[8]); + void select(const uint8_t rom_array[8]); // Issue a 1-Wire rom skip command, to address all on bus. void skip(); @@ -256,14 +59,14 @@ class OneWire // 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 value, bool power = 0); + void write(uint8_t value, bool power = false); - void write_bytes(const uint8_t *buf, uint16_t count, bool power = false); + void write_bytes(const uint8_t data_array[], uint16_t data_size, bool power = false); // Read a byte. uint8_t read(); - void read_bytes(uint8_t *buf, uint16_t count); + void read_bytes(uint8_t data_array[], uint16_t data_size); // Write a bit. The bus is always left powered at the end, see // note in write() about that. @@ -299,7 +102,7 @@ class OneWire // Compute a Dallas Semiconductor 8 bit CRC, these are used in the // ROM and scratchpad registers. - static uint8_t crc8(const uint8_t *address, uint8_t length, uint8_t crc_init = 0); + static uint8_t crc8(const uint8_t data_array[], uint8_t data_size, uint8_t crc_init = 0); // Compute the 1-Wire CRC16 and compare it against the received CRC. @@ -322,7 +125,7 @@ class OneWire // *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); + static bool check_crc16(const uint8_t data_array[], uint16_t data_size, 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 @@ -336,7 +139,7 @@ class OneWire // @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* address, uint16_t length, uint16_t crc_init = 0); + static uint16_t crc16(const uint8_t data_array[], uint16_t data_size, uint16_t crc_init = 0); }; diff --git a/src/platform.cpp b/src/platform.cpp new file mode 100644 index 0000000..6d315c1 --- /dev/null +++ b/src/platform.cpp @@ -0,0 +1,22 @@ +#include "platform.h" + +#ifdef FALLBACK_BASIC_FNs + +uint32_t micros() {return 0; }; // original arduino-fn takes about 3 µs to process @ 16 MHz + +void cli() { }; +void sei() { }; + +void noInterrupts() { }; + +void interrupts() { }; + +uint8_t pgm_read_byte(const uint8_t * const address) +{ + return *address; +} + +#endif // FALLBACK_BASIC_FNs + + + diff --git a/src/platform.h b/src/platform.h new file mode 100644 index 0000000..a6941f1 --- /dev/null +++ b/src/platform.h @@ -0,0 +1,328 @@ +// Platform specific I/O definitions + +#ifndef ONEWIRE_PLATFORM_H +#define ONEWIRE_PLATFORM_H + +#if defined(ARDUINO) && ARDUINO >= 100 +#include +#endif + +#if defined(__AVR__) +#include +#define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#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)) +using io_reg_t = uint8_t; // define special datatype for register-access + +#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) +#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) +#define PIN_TO_BITMASK(pin) (1) +#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) +using io_reg_t = uint8_t; // define special datatype for register-access + +#elif defined(__MKL26Z64__) +#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#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)) +using io_reg_t = uint8_t; // define special datatype for register-access + +#elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__) +// 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 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 +using io_reg_t = uint32_t; // define special datatype for register-access + +#elif defined(__PIC32MX__) +#define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#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 +using io_reg_t = uint32_t; // define special datatype for register-access + +#elif defined(ARDUINO_ARCH_ESP8266) +// Special note: I depend on the ESP community to maintain these definitions and +// submit good pull requests. I can not answer any ESP questions or help you +// resolve any problems related to ESP chips. Please do not contact me and please +// DO NOT CREATE GITHUB ISSUES for ESP support. All ESP questions must be asked +// on ESP community forums. +#define PIN_TO_BASEREG(pin) ((volatile uint32_t*) GPO) +#define PIN_TO_BITMASK(pin) (1 << pin) +#define DIRECT_READ(base, mask) ((GPI & (mask)) ? 1 : 0) //GPIO_IN_ADDRESS +#define DIRECT_MODE_INPUT(base, mask) (GPE &= ~(mask)) //GPIO_ENABLE_W1TC_ADDRESS +#define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS +#define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS +#define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS +using io_reg_t = uint32_t; // define special datatype for register-access + +#elif defined(__SAMD21G18A__) +#define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define DIRECT_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) = (mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask)) +using io_reg_t = uint32_t; // define special datatype for register-access + +#elif defined(RBL_NRF51822) +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin) +#define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin) +#define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) +#define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) +#define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) +using io_reg_t = uint32_t; // define special datatype for register-access + +#elif defined(__arc__) /* Arduino101/Genuino101 specifics */ + +#include "scss_registers.h" +#include "portable.h" +#include "avr/pgmspace.h" + +#define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId) +#define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType) +#define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase) +#define DIR_OFFSET_SS 0x01 +#define DIR_OFFSET_SOC 0x04 +#define EXT_PORT_OFFSET_SS 0x0A +#define EXT_PORT_OFFSET_SOC 0x50 + +/* GPIO registers base address */ +#define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) +#define PIN_TO_BITMASK(pin) pin +using io_reg_t = uint32_t; // define special datatype for register-access + +static inline __attribute__((always_inline)) +IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + IO_REG_TYPE ret; + if (SS_GPIO == GPIO_TYPE(pin)) { + ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS)); + } else { + ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC); + } + return ((ret >> GPIO_ID(pin)) & 0x01); +} + +static inline __attribute__((always_inline)) +void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + } else { + MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + } else { + MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base); + } else { + MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base); + } else { + MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin)); + } +} + +#define DIRECT_READ(base, pin) directRead(base, pin) +#define DIRECT_MODE_INPUT(base, pin) directModeInput(base, pin) +#define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(base, pin) +#define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin) +#define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(base, pin) + +#else // any unknown architecture, including PC + +#include + +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#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) +using io_reg_t = uint32_t; // define special datatype for register-access + +#warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture." + +/////////////////////////////////////////// EXTRA PART ///////////////////////////////////////// +// this part is loaded if no proper arduino-environment is found (good for external testing) +// these used functions are mockups +//////////////////////////////////////////////////////////////////////////////////////////////// + +#define INPUT 1 +#define OUTPUT 0 +#define HIGH 1 +#define LOW 0 + +#define FALLBACK_BASIC_FNs +#define FALLBACK_ADDITIONAL_FNs // to load up Serial below + +static bool mockup_pin_value[256]; + +template +bool digitalRead(const T1 pin) { return (mockup_pin_value[pin & 0xFF] != 0); }; // mock up outputs + +template +void digitalWrite(const T1 pin, const T2 value) { mockup_pin_value[pin & 0xFF] = value; }; + +template +void pinMode(const T1 pin, const T2 value) { mockup_pin_value[pin & 0xFF] = value; }; + +template +T1 digitalPinToPort(const T1 pin) { return pin; }; + +template +T1 * portInputRegister(const T1 port) { return port; }; + +template +T1 digitalPinToBitMask(const T1 pin) { return pin; }; + +constexpr uint32_t microsecondsToClockCycles(const uint32_t micros) {return (100*micros);}; // mockup, emulate 100 MHz CPU + +template +void delayMicroseconds(const T1 micros) { }; + +/// the following fn are no templates and need to be defined in platform.cpp + +uint32_t micros(); // takes about 3 µs to process @ 16 MHz + +void cli(); +void sei(); + +void noInterrupts(); + +void interrupts(); + +#endif + +#if defined(ARDUINO_attiny) +#define FALLBACK_ADDITIONAL_FNs // to load up Serial below +#endif + + +#ifdef FALLBACK_ADDITIONAL_FNs // Test to make it work on aTtiny85, 8MHz +/// README: use pin2 or pin3 for Attiny, source: https://github.com/gioblu/PJON/wiki/ATtiny-interfacing + +#ifndef HEX +#define HEX 1 +#endif + +static class serial +{ +private: + + static uint32_t speed; + +public: + + void print(...) { }; + + void println(...) { }; + + void flush() { }; + void begin(const uint32_t speed_baud) { speed = speed_baud; }; + +} Serial; + + +template +void memset(T1 * const address, const T1 initValue, const T2 bytes) +{ + const T2 iterations = bytes / sizeof(T1); + for (T2 counter = 0; counter < iterations; ++counter) + { + address[counter] = (initValue); + } +} + + +template +void memcpy(T1 * const destination, const T1 * const source, const T2 bytes) +{ + const T2 iterations = bytes / sizeof(T1); + for (T2 counter = 0; counter < iterations; ++counter) + { + destination[counter] = source[counter]; + } +} + + +template +bool memcmp(const T1* const source_A, const T1* const source_B, const T2 bytes) // return true if string is different +{ + const T2 iterations = bytes / sizeof(T1); + for (T2 counter = 0; counter < iterations; ++counter) + { + if (source_A[counter] != source_B[counter]) return true; + }; + return false; +} + +void delay(uint32_t time_millis); +uint32_t millis(); + +void wdt_reset(); +void wdt_enable(...); + +#ifndef PROGMEM +#define PROGMEM +#endif + +uint8_t pgm_read_byte(const uint8_t * address); + +#endif + +#endif //ONEWIRE_PLATFORM_H From 6c1f52e96e90769dfecaadc099606553fbd6aaf2 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 12:20:02 +0200 Subject: [PATCH 13/71] add readme --- readme.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 readme.md diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..5dc4da1 --- /dev/null +++ b/readme.md @@ -0,0 +1,6 @@ +TODO: fill + +## Features +## History +## How to use +## Help \ No newline at end of file From db5b13b085cbd91a77f38db0d0267c6bd03796c5 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 12:51:18 +0200 Subject: [PATCH 14/71] make examples more easy to understand --- .../DS18x20_Temperature.pde | 104 ++++++++++-------- examples/DS250x_PROM/DS250x_PROM.pde | 18 +-- 2 files changed, 66 insertions(+), 56 deletions(-) diff --git a/examples/DS18x20_Temperature/DS18x20_Temperature.pde b/examples/DS18x20_Temperature/DS18x20_Temperature.pde index 68ca194..05fe9f8 100644 --- a/examples/DS18x20_Temperature/DS18x20_Temperature.pde +++ b/examples/DS18x20_Temperature/DS18x20_Temperature.pde @@ -1,4 +1,6 @@ #include +//#include "../../src/platform.h" +//#include "../../src/OneWire.h" // OneWire DS18S20, DS18B20, DS1822 Temperature Example // @@ -7,103 +9,111 @@ // The DallasTemperature library can do all this work for you! // http://milesburton.com/Dallas_Temperature_Control_Library -OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary) +OneWire oneWire(10); // on pin 10 (a 4.7K resistor is necessary) -void setup(void) { - Serial.begin(9600); +void setup() +{ + Serial.begin(115200); } -void loop(void) { - byte i; - byte present = 0; - byte type_s; - byte data[12]; - byte addr[8]; - float celsius, fahrenheit; - - if ( !ds.search(addr)) { +void loop() +{ + bool device_is_present = false; + uint8_t device_type; + uint8_t device_data[12]; + uint8_t device_address[8]; + + if (!oneWire.search(device_address)) + { Serial.println("No more addresses."); Serial.println(); - ds.reset_search(); + oneWire.reset_search(); delay(250); return; } Serial.print("ROM ="); - for( i = 0; i < 8; i++) { - Serial.write(' '); - Serial.print(addr[i], HEX); + for(uint8_t index = 0; index < 8; index++) + { + Serial.print(" "); + Serial.print(device_address[index], HEX); } - if (OneWire::crc8(addr, 7) != addr[7]) { - Serial.println("CRC is not valid!"); + if (OneWire::crc8(device_address, 7) != device_address[7]) + { + Serial.println(" ERROR: CRC is not valid!"); return; } Serial.println(); // the first ROM byte indicates which chip - switch (addr[0]) { + switch (device_address[0]) + { case 0x10: - Serial.println(" Chip = DS18S20"); // or old DS1820 - type_s = 1; + Serial.println(" Chip = DS18S20"); // or old DS1820 + device_type = 1; break; case 0x28: - Serial.println(" Chip = DS18B20"); - type_s = 0; + Serial.println(" Chip = DS18B20"); + device_type = 0; break; case 0x22: - Serial.println(" Chip = DS1822"); - type_s = 0; + Serial.println(" Chip = DS1822"); + device_type = 0; break; default: - Serial.println("Device is not a DS18x20 family device."); + Serial.println(" ERROR: Device is not a DS18x20 family device."); return; } - ds.reset(); - ds.select(addr); - ds.write(0x44, 1); // start conversion, with parasite power on at the end + oneWire.reset(); + oneWire.select(device_address); + oneWire.write(0x44, 1); // start conversion, with parasite power on at the end delay(1000); // maybe 750ms is enough, maybe not - // we might do a ds.depower() here, but the reset will take care of it. + // we might do a oneWire.depower() here, but the reset will take care of it. - present = ds.reset(); - ds.select(addr); - ds.write(0xBE); // Read Scratchpad + device_is_present = oneWire.reset(); + oneWire.select(device_address); + oneWire.write(0xBE); // Read Scratchpad Serial.print(" Data = "); - Serial.print(present, HEX); + Serial.print(device_is_present, HEX); Serial.print(" "); - for ( i = 0; i < 9; i++) { // we need 9 bytes - data[i] = ds.read(); - Serial.print(data[i], HEX); + for (uint8_t index = 0; index < 9; index++) // we need 9 bytes + { + device_data[index] = oneWire.read(); + Serial.print(device_data[index], HEX); Serial.print(" "); } Serial.print(" CRC="); - Serial.print(OneWire::crc8(data, 8), HEX); + Serial.print(OneWire::crc8(device_data, 8), HEX); Serial.println(); - // Convert the data to actual temperature + // Convert the device_data to actual temperature // because the result is a 16 bit signed integer, it should // be stored to an "int16_t" type, which is always 16 bits // even when compiled on a 32 bit processor. - int16_t raw = (data[1] << 8) | data[0]; - if (type_s) { + int16_t raw = (device_data[1] << 8) | device_data[0]; + if (device_type) + { raw = raw << 3; // 9 bit resolution default - if (data[7] == 0x10) { + if (device_data[7] == 0x10) + { // "count remain" gives full 12 bit resolution - raw = (raw & 0xFFF0) + 12 - data[6]; + raw = (raw & 0xFFF0) + 12 - device_data[6]; } - } else { - byte cfg = (data[4] & 0x60); + } else + { + const uint8_t cfg = (device_data[4] & 0x60); // at lower res, the low bits are undefined, so let's zero them if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms //// default is 12 bit resolution, 750 ms conversion time } - celsius = (float)raw / 16.0; - fahrenheit = celsius * 1.8 + 32.0; + const float celsius = (float)raw / 16.0f; + const float fahrenheit = celsius * 1.8f + 32.0f; Serial.print(" Temperature = "); Serial.print(celsius); Serial.print(" Celsius, "); diff --git a/examples/DS250x_PROM/DS250x_PROM.pde b/examples/DS250x_PROM/DS250x_PROM.pde index baa51c8..204c38a 100644 --- a/examples/DS250x_PROM/DS250x_PROM.pde +++ b/examples/DS250x_PROM/DS250x_PROM.pde @@ -23,7 +23,7 @@ DS250x add-only programmable memory reader w/SKIP ROM. */ #include -OneWire ds(6); // OneWire bus on digital pin 6 +OneWire oneWire(6); // OneWire bus on digital pin 6 void setup() { Serial.begin (9600); } @@ -37,16 +37,16 @@ void loop() { byte ccrc; // Variable to store the command CRC byte ccrc_calc; - present = ds.reset(); // OneWire bus reset, always needed to start operation on the bus, returns a 1/TRUE if there's a device present. - ds.skip(); // Skip ROM search + present = oneWire.reset(); // OneWire bus reset, always needed to start operation on the bus, returns a 1/TRUE if there's a device present. + oneWire.skip(); // Skip ROM search if (present == TRUE){ // We only try to read the data if there's a device present Serial.println("DS250x device present"); - ds.write(leemem[0],1); // Read data command, leave ghost power on - ds.write(leemem[1],1); // LSB starting address, leave ghost power on - ds.write(leemem[2],1); // MSB starting address, leave ghost power on + oneWire.write(leemem[0],1); // Read data command, leave ghost power on + oneWire.write(leemem[1],1); // LSB starting address, leave ghost power on + oneWire.write(leemem[2],1); // MSB starting address, leave ghost power on - ccrc = ds.read(); // DS250x generates a CRC for the command we sent, we assign a read slot and store it's value + ccrc = oneWire.read(); // DS250x generates a CRC for the command we sent, we assign a read slot and store it's value ccrc_calc = OneWire::crc8(leemem, 3); // We calculate the CRC of the commands we sent using the library function and store it if ( ccrc_calc != ccrc) { // Then we compare it to the value the ds250x calculated, if it fails, we print debug messages and abort @@ -59,7 +59,7 @@ void loop() { } Serial.println("Data is: "); // For the printout of the data for ( i = 0; i < 32; i++) { // Now it's time to read the PROM data itself, each page is 32 bytes so we need 32 read commands - data[i] = ds.read(); // we store each read byte to a different position in the data array + data[i] = oneWire.read(); // we store each read byte to a different position in the data array Serial.print(data[i]); // printout in ASCII Serial.print(" "); // blank space } @@ -87,4 +87,4 @@ void loop() { - + From 475c7c0b1e5178dc0c1c62610618ebb370cc9fce Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 12:58:07 +0200 Subject: [PATCH 15/71] better naming --- src/OneWire.cpp | 60 ++++++++++++++++++++++++------------------------- src/OneWire.h | 17 ++++++-------- 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 7d2357d..a86106c 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -332,11 +332,11 @@ void OneWire::depower() void OneWire::reset_search() { // reset the search state - LastDiscrepancy = 0; - LastDeviceFlag = false; - LastFamilyDiscrepancy = 0; + search_last_discrepancy = 0; + search_last_device_flag = false; + search_last_family_discrepancy = 0; - memset(ROM_NO, 0, 8); + memset(search_rom_array, 0, 8); } // Setup the search to find the device type 'family_code' on the next call @@ -345,11 +345,11 @@ void OneWire::reset_search() void OneWire::target_search(const uint8_t family_code) { // set the search state to find SearchFamily type devices - ROM_NO[0] = family_code; - memset(&ROM_NO[1], 0, 7); - LastDiscrepancy = 64; - LastFamilyDiscrepancy = 0; - LastDeviceFlag = false; + search_rom_array[0] = family_code; + memset(&search_rom_array[1], 0, 7); + search_last_discrepancy = 64; + search_last_family_discrepancy = 0; + search_last_device_flag = false; } // @@ -365,10 +365,10 @@ void OneWire::target_search(const uint8_t family_code) //-------------------------------------------------------------------------- // 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 +// Return TRUE : device found, ROM number in search_rom_array buffer // FALSE : device not found, end of search // -bool OneWire::search(uint8_t * const newAddr, const bool search_mode) +bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) { uint8_t id_bit_number = 1; uint8_t last_zero = 0; @@ -381,15 +381,15 @@ bool OneWire::search(uint8_t * const newAddr, const bool search_mode) bool search_direction; // if the last call was not the last one - if (!LastDeviceFlag) + if (!search_last_device_flag) { // 1-Wire reset if (!reset()) { // reset the search - LastDiscrepancy = 0; - LastDeviceFlag = false; - LastFamilyDiscrepancy = 0; + search_last_discrepancy = 0; + search_last_device_flag = false; + search_last_family_discrepancy = 0; return false; } @@ -421,11 +421,11 @@ bool OneWire::search(uint8_t * const newAddr, const bool search_mode) { // 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); + if (id_bit_number < search_last_discrepancy) + search_direction = ((search_rom_array[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); + search_direction = (id_bit_number == search_last_discrepancy); // if 0 was picked then record its position in LastZero if (!search_direction) @@ -434,16 +434,16 @@ bool OneWire::search(uint8_t * const newAddr, const bool search_mode) // check for Last discrepancy in family if (last_zero < 9) - LastFamilyDiscrepancy = last_zero; + search_last_family_discrepancy = last_zero; } } // set or clear the bit in the ROM byte rom_byte_number // with mask rom_byte_mask if (search_direction) - ROM_NO[rom_byte_number] |= rom_byte_mask; + search_rom_array[rom_byte_number] |= rom_byte_mask; else - ROM_NO[rom_byte_number] &= ~rom_byte_mask; + search_rom_array[rom_byte_number] &= ~rom_byte_mask; // serial number search direction write bit write_bit(search_direction); @@ -466,30 +466,30 @@ bool OneWire::search(uint8_t * const newAddr, const bool search_mode) // if the search was successful then if (id_bit_number >= 65) { - // search successful so set LastDiscrepancy,LastDeviceFlag,search_result - LastDiscrepancy = last_zero; + // search successful so set search_last_discrepancy,search_last_device_flag,search_result + search_last_discrepancy = last_zero; // check for last device - if (LastDiscrepancy == 0) - LastDeviceFlag = true; + if (search_last_discrepancy == 0) + search_last_device_flag = 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] == 0)) + if (!search_result || (search_rom_array[0] == 0)) { - LastDiscrepancy = 0; - LastDeviceFlag = false; - LastFamilyDiscrepancy = 0; + search_last_discrepancy = 0; + search_last_device_flag = false; + search_last_family_discrepancy = 0; search_result = false; } else { for (int index = 0; index < 8; index++) { - newAddr[index] = ROM_NO[index]; + new_rom_array[index] = search_rom_array[index]; } } return search_result; diff --git a/src/OneWire.h b/src/OneWire.h index a537f7d..dbcedc3 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -16,9 +16,6 @@ #define ONEWIRE_CRC8_TABLE 1 #endif - - - class OneWire { private: @@ -26,11 +23,11 @@ class OneWire io_reg_t pin_bitMask; volatile io_reg_t *pin_baseReg; - // global search state - uint8_t ROM_NO[8]; - uint8_t LastDiscrepancy; - uint8_t LastFamilyDiscrepancy; - bool LastDeviceFlag; + // search state + uint8_t search_rom_array[8]; + uint8_t search_last_discrepancy; + uint8_t search_last_family_discrepancy; + bool search_last_device_flag; public: @@ -70,7 +67,7 @@ class OneWire // Write a bit. The bus is always left powered at the end, see // note in write() about that. - void write_bit(bool v); + void write_bit(bool value); // Read a bit. bool read_bit(); @@ -96,7 +93,7 @@ class OneWire // 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. - bool search(uint8_t *newAddr, bool search_mode = true); + bool search(uint8_t new_rom_array[8], bool search_mode = true); From 48546ea704ac84dbcbd56adfdeb4b567745645ac Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 12:59:58 +0200 Subject: [PATCH 16/71] adapt to "new" .ino extension --- .../{DS18x20_Temperature.pde => DS18x20_Temperature.ino} | 0 examples/DS2408_Switch/{DS2408_Switch.pde => DS2408_Switch.ino} | 0 examples/DS250x_PROM/{DS250x_PROM.pde => DS250x_PROM.ino} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename examples/DS18x20_Temperature/{DS18x20_Temperature.pde => DS18x20_Temperature.ino} (100%) rename examples/DS2408_Switch/{DS2408_Switch.pde => DS2408_Switch.ino} (100%) rename examples/DS250x_PROM/{DS250x_PROM.pde => DS250x_PROM.ino} (100%) diff --git a/examples/DS18x20_Temperature/DS18x20_Temperature.pde b/examples/DS18x20_Temperature/DS18x20_Temperature.ino similarity index 100% rename from examples/DS18x20_Temperature/DS18x20_Temperature.pde rename to examples/DS18x20_Temperature/DS18x20_Temperature.ino diff --git a/examples/DS2408_Switch/DS2408_Switch.pde b/examples/DS2408_Switch/DS2408_Switch.ino similarity index 100% rename from examples/DS2408_Switch/DS2408_Switch.pde rename to examples/DS2408_Switch/DS2408_Switch.ino diff --git a/examples/DS250x_PROM/DS250x_PROM.pde b/examples/DS250x_PROM/DS250x_PROM.ino similarity index 100% rename from examples/DS250x_PROM/DS250x_PROM.pde rename to examples/DS250x_PROM/DS250x_PROM.ino From 269664f22f9d00a52fc7ac46b8711b3e34cef0ea Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 14:01:29 +0200 Subject: [PATCH 17/71] reformat code --- src/OneWire.cpp | 506 ++++++++++++++++++++++++------------------------ src/OneWire.h | 23 ++- src/platform.h | 83 +++++--- 3 files changed, 318 insertions(+), 294 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index a86106c..6047ecc 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -143,13 +143,13 @@ sample code bearing this copyright. OneWire::OneWire(const uint8_t pin) { - // prepare pin + // prepare pin pinMode(pin, INPUT); - pin_bitMask = PIN_TO_BITMASK(pin); - pin_baseReg = PIN_TO_BASEREG(pin); + pin_bitMask = PIN_TO_BITMASK(pin); + pin_baseReg = PIN_TO_BASEREG(pin); DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - reset_search(); // really needed? + reset_search(); // really needed? } @@ -161,29 +161,30 @@ OneWire::OneWire(const uint8_t pin) // bool OneWire::reset() { - uint8_t retries = 125; - - noInterrupts(); - DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); - interrupts(); - // wait until the wire is high... just in case - do { - if (--retries == 0) return false; - delayMicroseconds(2); - } while ( !DIRECT_READ(pin_baseReg, pin_bitMask)); - - noInterrupts(); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low - interrupts(); - delayMicroseconds(480); - noInterrupts(); - DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); // allow it to float - delayMicroseconds(70); + uint8_t retries = 125; + + noInterrupts(); + DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); + interrupts(); + // wait until the wire is high... just in case + do + { + if (--retries == 0) return false; + delayMicroseconds(2); + } while (!DIRECT_READ(pin_baseReg, pin_bitMask)); + + noInterrupts(); + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low + interrupts(); + delayMicroseconds(480); + noInterrupts(); + DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); // allow it to float + delayMicroseconds(70); const bool success = !DIRECT_READ(pin_baseReg, pin_bitMask); - interrupts(); - delayMicroseconds(410); - return success; + interrupts(); + delayMicroseconds(410); + return success; } // @@ -192,23 +193,25 @@ bool OneWire::reset() // void OneWire::write_bit(const bool value) { - if (value) { - noInterrupts(); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low - delayMicroseconds(10); - DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high - interrupts(); - delayMicroseconds(55); - } else { - noInterrupts(); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low - delayMicroseconds(65); - DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high - interrupts(); - delayMicroseconds(5); - } + if (value) + { + noInterrupts(); + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low + delayMicroseconds(10); + DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high + interrupts(); + delayMicroseconds(55); + } else + { + noInterrupts(); + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low + delayMicroseconds(65); + DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high + interrupts(); + delayMicroseconds(5); + } } // @@ -217,16 +220,16 @@ void OneWire::write_bit(const bool value) // bool OneWire::read_bit() { - noInterrupts(); - DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - delayMicroseconds(3); - DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); // let pin float, pull up will raise - delayMicroseconds(10); - const bool value = DIRECT_READ(pin_baseReg, pin_bitMask); - interrupts(); - delayMicroseconds(53); - return value; + noInterrupts(); + DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + delayMicroseconds(3); + DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); // let pin float, pull up will raise + delayMicroseconds(10); + const bool value = DIRECT_READ(pin_baseReg, pin_bitMask); + interrupts(); + delayMicroseconds(53); + return value; } // @@ -241,31 +244,31 @@ void OneWire::write(const uint8_t value, const bool power) for (uint8_t bitMask = 0x01; bitMask != 0; bitMask <<= 1) { - write_bit((bitMask & value) != 0); // TODO: shifting value could be me faster + write_bit((bitMask & value) != 0); // TODO: shifting value could be me faster } if (!power) { - noInterrupts(); - DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - interrupts(); + noInterrupts(); + DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + interrupts(); } } void OneWire::write_bytes(const uint8_t *buf, const uint16_t data_size, const bool power) { - for (uint16_t index = 0 ; index < data_size ; index++) // TODO: can be tuned - { - write(buf[index]); - } + for (uint16_t index = 0; index < data_size; index++) // TODO: can be tuned + { + write(buf[index]); + } - if (!power) - { - noInterrupts(); - DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - interrupts(); - } + if (!power) + { + noInterrupts(); + DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + interrupts(); + } } // @@ -277,23 +280,23 @@ uint8_t OneWire::read() for (uint8_t bitMask = 0x01; bitMask != 0; bitMask <<= 1) { - if (read_bit()) value |= bitMask; + if (read_bit()) value |= bitMask; } return value; } void OneWire::read_bytes(uint8_t data_array[], uint16_t data_size) { - for (uint16_t index = 0 ; index < data_size ; index++) - // while (data_size-- > 0) - { - data_array[index] = read(); + for (uint16_t index = 0; index < data_size; index++) + // while (data_size-- > 0) + { + data_array[index] = read(); - //*data_array = read(); // TODO: test if it can be combined in one line + //*data_array = read(); // TODO: test if it can be combined in one line - //*data_array = read(); // TODO: test if it can be combined in one line - //data_array++; - } + //*data_array = read(); // TODO: test if it can be combined in one line + //data_array++; + } } // @@ -320,9 +323,9 @@ void OneWire::skip() void OneWire::depower() { - noInterrupts(); - DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); - interrupts(); + noInterrupts(); + DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); + interrupts(); } // @@ -331,10 +334,10 @@ void OneWire::depower() // void OneWire::reset_search() { - // reset the search state - search_last_discrepancy = 0; - search_last_device_flag = false; - search_last_family_discrepancy = 0; + // reset the search state + search_last_discrepancy = 0; + search_last_device_flag = false; + search_last_family_discrepancy = 0; memset(search_rom_array, 0, 8); } @@ -344,12 +347,12 @@ void OneWire::reset_search() // void OneWire::target_search(const uint8_t family_code) { - // set the search state to find SearchFamily type devices - search_rom_array[0] = family_code; + // set the search state to find SearchFamily type devices + search_rom_array[0] = family_code; memset(&search_rom_array[1], 0, 7); - search_last_discrepancy = 64; - search_last_family_discrepancy = 0; - search_last_device_flag = false; + search_last_discrepancy = 64; + search_last_family_discrepancy = 0; + search_last_device_flag = false; } // @@ -370,130 +373,130 @@ void OneWire::target_search(const uint8_t family_code) // bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) { - uint8_t id_bit_number = 1; - uint8_t last_zero = 0; - uint8_t rom_byte_number = 0; - bool search_result = false; + uint8_t id_bit_number = 1; + uint8_t last_zero = 0; + uint8_t rom_byte_number = 0; + bool search_result = false; - bool id_bit, cmp_id_bit; + bool id_bit, cmp_id_bit; - uint8_t rom_byte_mask = 1; + uint8_t rom_byte_mask = 1; bool search_direction; - // if the last call was not the last one - if (!search_last_device_flag) - { - // 1-Wire reset - if (!reset()) - { - // reset the search - search_last_discrepancy = 0; - search_last_device_flag = false; - search_last_family_discrepancy = 0; - return false; - } - - // issue the search command - if (search_mode) { - write(0xF0); // NORMAL SEARCH - } else { - write(0xEC); // CONDITIONAL SEARCH - } - - // 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) && (cmp_id_bit)) - { - break; - } - else - { - // all devices coupled have 0 or 1 - if (id_bit != cmp_id_bit) - search_direction = id_bit; // bit write value for search + // if the last call was not the last one + if (!search_last_device_flag) + { + // 1-Wire reset + if (!reset()) + { + // reset the search + search_last_discrepancy = 0; + search_last_device_flag = false; + search_last_family_discrepancy = 0; + return false; + } + + // issue the search command + if (search_mode) + { + write(0xF0); // NORMAL SEARCH + } else + { + write(0xEC); // CONDITIONAL SEARCH + } + + // 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) && (cmp_id_bit)) + { + break; + } else { - // if this discrepancy if before the Last Discrepancy - // on a previous next then pick the same as last time - if (id_bit_number < search_last_discrepancy) - search_direction = ((search_rom_array[rom_byte_number] & rom_byte_mask) > 0); - else - // if equal to last pick 1, if not then pick 0 - search_direction = (id_bit_number == search_last_discrepancy); - - // if 0 was picked then record its position in LastZero - if (!search_direction) - { - last_zero = id_bit_number; - - // check for Last discrepancy in family - if (last_zero < 9) - search_last_family_discrepancy = last_zero; - } + // 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 < search_last_discrepancy) + search_direction = ((search_rom_array[rom_byte_number] & rom_byte_mask) > 0); + else + // if equal to last pick 1, if not then pick 0 + search_direction = (id_bit_number == search_last_discrepancy); + + // if 0 was picked then record its position in LastZero + if (!search_direction) + { + last_zero = id_bit_number; + + // check for Last discrepancy in family + if (last_zero < 9) + search_last_family_discrepancy = last_zero; + } + } + + // set or clear the bit in the ROM byte rom_byte_number + // with mask rom_byte_mask + if (search_direction) + search_rom_array[rom_byte_number] |= rom_byte_mask; + else + search_rom_array[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 - // set or clear the bit in the ROM byte rom_byte_number - // with mask rom_byte_mask - if (search_direction) - search_rom_array[rom_byte_number] |= rom_byte_mask; - else - search_rom_array[rom_byte_number] &= ~rom_byte_mask; + // if the search was successful then + if (id_bit_number >= 65) + { + // search successful so set search_last_discrepancy,search_last_device_flag,search_result + search_last_discrepancy = last_zero; - // serial number search direction write bit - write_bit(search_direction); + // check for last device + if (search_last_discrepancy == 0) + search_last_device_flag = true; - // increment the byte counter id_bit_number - // and shift the mask rom_byte_mask - id_bit_number++; - rom_byte_mask <<= 1; + search_result = true; + } + } - // 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 search_last_discrepancy,search_last_device_flag,search_result - search_last_discrepancy = last_zero; - - // check for last device - if (search_last_discrepancy == 0) - search_last_device_flag = true; - - search_result = true; - } - } - - // if no device found then reset counters so next 'search' will be like a first - if (!search_result || (search_rom_array[0] == 0)) - { - search_last_discrepancy = 0; - search_last_device_flag = false; - search_last_family_discrepancy = 0; - search_result = false; - } - else - { - for (int index = 0; index < 8; index++) - { - new_rom_array[index] = search_rom_array[index]; - } - } - return search_result; - } + // if no device found then reset counters so next 'search' will be like a first + if (!search_result || (search_rom_array[0] == 0)) + { + search_last_discrepancy = 0; + search_last_device_flag = false; + search_last_family_discrepancy = 0; + search_result = false; + } else + { + for (int index = 0; index < 8; index++) + { + new_rom_array[index] = search_rom_array[index]; + } + } + return search_result; +} @@ -505,22 +508,22 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) // This table comes from Dallas sample code where it is freely reusable, // though Copyright (C) 2000 Dallas Semiconductor Corporation static const uint8_t PROGMEM crc_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}; + 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 @@ -531,14 +534,15 @@ static const uint8_t PROGMEM crc_table[] = { // uint8_t OneWire::crc8(const uint8_t data_array[], uint8_t data_size, const uint8_t crc_init) { - uint8_t crc = crc_init; + uint8_t crc = crc_init; for (uint8_t index = 0; index < data_size; ++index) { - crc = pgm_read_byte(crc_table + (crc ^ data_array[index])); - } - return crc; + crc = pgm_read_byte(crc_table + (crc ^ data_array[index])); + } + return crc; } + #else // // Compute a Dallas Semiconductor 8 bit CRC directly. @@ -546,28 +550,28 @@ uint8_t OneWire::crc8(const uint8_t data_array[], uint8_t data_size, const uint8 // uint8_t OneWire::crc8(const uint8_t data_array[], uint8_t data_size, const uint8_t crc_init) { - uint8_t crc = crc_init; + uint8_t crc = crc_init; while (data_size-- > 0) // clean solution with for(index) seems to be 6 byte larger { #if defined(__AVR__) - crc = _crc_ibutton_update(crc, *data_array++); + crc = _crc_ibutton_update(crc, *data_array++); #else - uint8_t inByte = *data_array++; - for (uint8_t bitPosition = 0; bitPosition < 8; bitPosition++) + uint8_t inByte = *data_array++; + for (uint8_t bitPosition = 0; bitPosition < 8; bitPosition++) { - const uint8_t mix = (crc ^ inByte) & static_cast(0x01); - crc >>= 1; - if (mix != 0) crc ^= 0x8C; - inByte >>= 1; - } + const uint8_t mix = (crc ^ inByte) & static_cast(0x01); + crc >>= 1; + if (mix != 0) crc ^= 0x8C; + inByte >>= 1; + } #endif - } - return crc; + } + return crc; } #endif -bool OneWire::check_crc16(const uint8_t data_array[], const uint16_t data_size, const uint8_t* inverted_crc, uint16_t crc) +bool OneWire::check_crc16(const uint8_t data_array[], const uint16_t data_size, const uint8_t *inverted_crc, uint16_t crc) { crc = ~crc16(data_array, data_size, crc); return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1]; @@ -584,25 +588,25 @@ uint16_t OneWire::crc16(const uint8_t data_array[], uint16_t data_size, const ui } #else static const uint8_t oddParity[16] = - { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 }; + {0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0}; while (data_size-- > 0) // clean solution with for(index) seems to be 6 byte larger { - // Even though we're just copying a byte from the data_array, - // we'll be doing 16-bit computation with it. - uint16_t cdata = *data_array++; - cdata = (cdata ^ crc) & static_cast(0xff); - crc >>= 8; - - if ((oddParity[cdata & 0x0F] ^ oddParity[cdata >> 4]) != 0) - { - crc ^= 0xC001; - } - - cdata <<= 6; - crc ^= cdata; - cdata <<= 1; - crc ^= cdata; + // Even though we're just copying a byte from the data_array, + // we'll be doing 16-bit computation with it. + uint16_t cdata = *data_array++; + cdata = (cdata ^ crc) & static_cast(0xff); + crc >>= 8; + + if ((oddParity[cdata & 0x0F] ^ oddParity[cdata >> 4]) != 0) + { + crc ^= 0xC001; + } + + cdata <<= 6; + crc ^= cdata; + cdata <<= 1; + crc ^= cdata; } #endif return crc; diff --git a/src/OneWire.h b/src/OneWire.h index dbcedc3..607d247 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -18,7 +18,7 @@ class OneWire { - private: +private: io_reg_t pin_bitMask; volatile io_reg_t *pin_baseReg; @@ -27,19 +27,19 @@ class OneWire uint8_t search_rom_array[8]; uint8_t search_last_discrepancy; uint8_t search_last_family_discrepancy; - bool search_last_device_flag; + bool search_last_device_flag; - public: +public: explicit OneWire(uint8_t pin); ~OneWire() = default; // nothing special to do here - OneWire(const OneWire& ow) = delete; // disallow copy constructor - OneWire(OneWire&& ow) = default; // default move constructor - OneWire& operator=(OneWire& ow) = delete; // disallow copy assignment - OneWire& operator=(const OneWire& ow) = delete; // disallow copy assignment - OneWire& operator=(OneWire&& ow) = delete; // disallow move assignment + OneWire(const OneWire &ow) = delete; // disallow copy constructor + OneWire(OneWire &&ow) = default; // default move constructor + OneWire &operator=(OneWire &ow) = delete; // disallow copy assignment + OneWire &operator=(const OneWire &ow) = delete; // disallow copy assignment + OneWire &operator=(OneWire &&ow) = delete; // disallow move assignment // 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 @@ -96,7 +96,6 @@ class OneWire bool search(uint8_t new_rom_array[8], bool search_mode = true); - // Compute a Dallas Semiconductor 8 bit CRC, these are used in the // ROM and scratchpad registers. static uint8_t crc8(const uint8_t data_array[], uint8_t data_size, uint8_t crc_init = 0); @@ -109,8 +108,8 @@ class OneWire // 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 + // WriteBytes(onewire, buf, 3); // Write 3 cmd bytes + // ReadBytes(onewire, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16 // if (!CheckCRC16(buf, 11, &buf[11])) { // // Handle error. // } @@ -122,7 +121,7 @@ class OneWire // *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 data_array[], uint16_t data_size, const uint8_t* inverted_crc, uint16_t crc = 0); + static bool check_crc16(const uint8_t data_array[], uint16_t data_size, 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 diff --git a/src/platform.h b/src/platform.h index a6941f1..16abbf3 100644 --- a/src/platform.h +++ b/src/platform.h @@ -139,7 +139,7 @@ void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { if (SS_GPIO == GPIO_TYPE(pin)) { WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), - ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); } else { MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); } @@ -150,7 +150,7 @@ void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { if (SS_GPIO == GPIO_TYPE(pin)) { WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), - ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); } else { MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); } @@ -212,34 +212,43 @@ using io_reg_t = uint32_t; // define special datatype for register-access static bool mockup_pin_value[256]; -template -bool digitalRead(const T1 pin) { return (mockup_pin_value[pin & 0xFF] != 0); }; // mock up outputs +template +bool digitalRead(const T1 pin) +{ return (mockup_pin_value[pin & 0xFF] != 0); }; // mock up outputs -template -void digitalWrite(const T1 pin, const T2 value) { mockup_pin_value[pin & 0xFF] = value; }; +template +void digitalWrite(const T1 pin, const T2 value) +{ mockup_pin_value[pin & 0xFF] = value; }; -template -void pinMode(const T1 pin, const T2 value) { mockup_pin_value[pin & 0xFF] = value; }; +template +void pinMode(const T1 pin, const T2 value) +{ mockup_pin_value[pin & 0xFF] = value; }; -template -T1 digitalPinToPort(const T1 pin) { return pin; }; +template +T1 digitalPinToPort(const T1 pin) +{ return pin; }; -template -T1 * portInputRegister(const T1 port) { return port; }; +template +T1 *portInputRegister(const T1 port) +{ return port; }; -template -T1 digitalPinToBitMask(const T1 pin) { return pin; }; +template +T1 digitalPinToBitMask(const T1 pin) +{ return pin; }; -constexpr uint32_t microsecondsToClockCycles(const uint32_t micros) {return (100*micros);}; // mockup, emulate 100 MHz CPU +constexpr uint32_t microsecondsToClockCycles(const uint32_t micros) +{ return (100 * micros); }; // mockup, emulate 100 MHz CPU -template -void delayMicroseconds(const T1 micros) { }; +template +void delayMicroseconds(const T1 micros) +{}; /// the following fn are no templates and need to be defined in platform.cpp uint32_t micros(); // takes about 3 µs to process @ 16 MHz void cli(); + void sei(); void noInterrupts(); @@ -256,10 +265,15 @@ void interrupts(); #ifdef FALLBACK_ADDITIONAL_FNs // Test to make it work on aTtiny85, 8MHz /// README: use pin2 or pin3 for Attiny, source: https://github.com/gioblu/PJON/wiki/ATtiny-interfacing +#ifndef BIN +#define BIN 1 +#endif + #ifndef HEX -#define HEX 1 +#define HEX 2 #endif + static class serial { private: @@ -268,18 +282,23 @@ static class serial public: - void print(...) { }; + void print(...) + {}; + + void println(...) + {}; - void println(...) { }; + void flush() + {}; - void flush() { }; - void begin(const uint32_t speed_baud) { speed = speed_baud; }; + void begin(const uint32_t speed_baud) + { speed = speed_baud; }; } Serial; template -void memset(T1 * const address, const T1 initValue, const T2 bytes) +void memset(T1 *const address, const T1 initValue, const T2 bytes) { const T2 iterations = bytes / sizeof(T1); for (T2 counter = 0; counter < iterations; ++counter) @@ -290,7 +309,7 @@ void memset(T1 * const address, const T1 initValue, const T2 bytes) template -void memcpy(T1 * const destination, const T1 * const source, const T2 bytes) +void memcpy(T1 *const destination, const T1 *const source, const T2 bytes) { const T2 iterations = bytes / sizeof(T1); for (T2 counter = 0; counter < iterations; ++counter) @@ -300,8 +319,8 @@ void memcpy(T1 * const destination, const T1 * const source, const T2 bytes) } -template -bool memcmp(const T1* const source_A, const T1* const source_B, const T2 bytes) // return true if string is different +template +bool memcmp(const T1 *const source_A, const T1 *const source_B, const T2 bytes) // return true if string is different { const T2 iterations = bytes / sizeof(T1); for (T2 counter = 0; counter < iterations; ++counter) @@ -311,17 +330,19 @@ bool memcmp(const T1* const source_A, const T1* const source_B, const T2 bytes) return false; } -void delay(uint32_t time_millis); -uint32_t millis(); +void delay(uint32_t time_millis); + +uint32_t millis(); + +void wdt_reset(); -void wdt_reset(); -void wdt_enable(...); +void wdt_enable(...); #ifndef PROGMEM #define PROGMEM #endif -uint8_t pgm_read_byte(const uint8_t * address); +uint8_t pgm_read_byte(const uint8_t *address); #endif From 12e53f80db246f63cc6584d87b266c6d485143f2 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 14:03:30 +0200 Subject: [PATCH 18/71] reformat examples --- .../DS18x20_Temperature.ino | 194 +++++++++--------- examples/DS2408_Switch/DS2408_Switch.ino | 128 ++++++------ examples/DS250x_PROM/DS250x_PROM.ino | 92 +++++---- 3 files changed, 214 insertions(+), 200 deletions(-) diff --git a/examples/DS18x20_Temperature/DS18x20_Temperature.ino b/examples/DS18x20_Temperature/DS18x20_Temperature.ino index 05fe9f8..d35350d 100644 --- a/examples/DS18x20_Temperature/DS18x20_Temperature.ino +++ b/examples/DS18x20_Temperature/DS18x20_Temperature.ino @@ -1,7 +1,3 @@ -#include -//#include "../../src/platform.h" -//#include "../../src/OneWire.h" - // OneWire DS18S20, DS18B20, DS1822 Temperature Example // // http://www.pjrc.com/teensy/td_libs_OneWire.html @@ -9,114 +5,114 @@ // The DallasTemperature library can do all this work for you! // http://milesburton.com/Dallas_Temperature_Control_Library -OneWire oneWire(10); // on pin 10 (a 4.7K resistor is necessary) +#include + +OneWire oneWire(10); // on pin 10 (a 4.7K resistor is necessary) void setup() { - Serial.begin(115200); + Serial.begin(115200); } void loop() { - bool device_is_present = false; - uint8_t device_type; - uint8_t device_data[12]; - uint8_t device_address[8]; + uint8_t device_address[8]; - if (!oneWire.search(device_address)) - { - Serial.println("No more addresses."); - Serial.println(); - oneWire.reset_search(); - delay(250); - return; - } - - Serial.print("ROM ="); - for(uint8_t index = 0; index < 8; index++) - { - Serial.print(" "); - Serial.print(device_address[index], HEX); - } + if (!oneWire.search(device_address)) + { + Serial.println("No more addresses."); + oneWire.reset_search(); + delay(250); + return; + } + + Serial.print("ROM ="); + for (uint8_t index = 0; index < 8; index++) + { + Serial.print(" "); + Serial.print(device_address[index], HEX); + } + + if (OneWire::crc8(device_address, 7) != device_address[7]) + { + Serial.println(" ERROR: CRC is not valid!"); + return; + } + + // start of device dependent code + + // the first ROM byte indicates which chip + uint8_t device_type = 0; // zero for all but 18S20 + switch (device_address[0]) + { + case 0x10: + Serial.println(" Chip = DS18S20"); // or old DS1820 + device_type = 1; + break; + case 0x28: + Serial.println(" Chip = DS18B20"); + break; + case 0x22: + Serial.println(" Chip = DS1822"); + break; + default: + Serial.println(" ERROR: not a DS18x20 family device."); + return; + } - if (OneWire::crc8(device_address, 7) != device_address[7]) - { - Serial.println(" ERROR: CRC is not valid!"); - return; - } - Serial.println(); - - // the first ROM byte indicates which chip - switch (device_address[0]) - { - case 0x10: - Serial.println(" Chip = DS18S20"); // or old DS1820 - device_type = 1; - break; - case 0x28: - Serial.println(" Chip = DS18B20"); - device_type = 0; - break; - case 0x22: - Serial.println(" Chip = DS1822"); - device_type = 0; - break; - default: - Serial.println(" ERROR: Device is not a DS18x20 family device."); - return; - } + oneWire.reset(); + oneWire.select(device_address); + oneWire.write(0x44, 1); // start conversion, with parasite power on at the end - oneWire.reset(); - oneWire.select(device_address); - oneWire.write(0x44, 1); // start conversion, with parasite power on at the end - - delay(1000); // maybe 750ms is enough, maybe not - // we might do a oneWire.depower() here, but the reset will take care of it. - - device_is_present = oneWire.reset(); - oneWire.select(device_address); - oneWire.write(0xBE); // Read Scratchpad + delay(1000); // maybe 750ms is enough, maybe not + // we might do a oneWire.depower() here, but the reset will take care of it. - Serial.print(" Data = "); - Serial.print(device_is_present, HEX); - Serial.print(" "); - for (uint8_t index = 0; index < 9; index++) // we need 9 bytes - { - device_data[index] = oneWire.read(); - Serial.print(device_data[index], HEX); + const bool device_is_present = oneWire.reset(); + oneWire.select(device_address); + oneWire.write(0xBE); // Read Scratchpad + + Serial.print(" Data = "); + Serial.print(device_is_present, HEX); Serial.print(" "); - } - Serial.print(" CRC="); - Serial.print(OneWire::crc8(device_data, 8), HEX); - Serial.println(); - // Convert the device_data to actual temperature - // because the result is a 16 bit signed integer, it should - // be stored to an "int16_t" type, which is always 16 bits - // even when compiled on a 32 bit processor. - int16_t raw = (device_data[1] << 8) | device_data[0]; - if (device_type) - { - raw = raw << 3; // 9 bit resolution default - if (device_data[7] == 0x10) + uint8_t device_data[12]; + for (uint8_t index = 0; index < 9; index++) // we need 9 bytes + { + device_data[index] = oneWire.read(); + Serial.print(device_data[index], HEX); + Serial.print(" "); + } + Serial.print(" CRC="); + Serial.print(OneWire::crc8(device_data, 8), HEX); + Serial.println(); + + // Convert the device_data to actual temperature + // because the result is a 16 bit signed integer, it should + // be stored to an "int16_t" type, which is always 16 bits + // even when compiled on a 32 bit processor. + int16_t raw = (device_data[1] << 8) | device_data[0]; + if (device_type) + { + raw = raw << 3; // 9 bit resolution default + if (device_data[7] == 0x10) + { + // "count remain" gives full 12 bit resolution + raw = (raw & 0xFFF0) + 12 - device_data[6]; + } + } else { - // "count remain" gives full 12 bit resolution - raw = (raw & 0xFFF0) + 12 - device_data[6]; + const uint8_t cfg = (device_data[4] & 0x60); + // at lower res, the low bits are undefined, so let's zero them + if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms + else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms + else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms + //// default is 12 bit resolution, 750 ms conversion time } - } else - { - const uint8_t cfg = (device_data[4] & 0x60); - // at lower res, the low bits are undefined, so let's zero them - if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms - else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms - else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms - //// default is 12 bit resolution, 750 ms conversion time - } - const float celsius = (float)raw / 16.0f; - const float fahrenheit = celsius * 1.8f + 32.0f; - Serial.print(" Temperature = "); - Serial.print(celsius); - Serial.print(" Celsius, "); - Serial.print(fahrenheit); - Serial.println(" Fahrenheit"); + const float celsius = (float) raw / 16.0f; + const float fahrenheit = celsius * 1.8f + 32.0f; + Serial.print(" Temperature = "); + Serial.print(celsius); + Serial.print(" Celsius, "); + Serial.print(fahrenheit); + Serial.println(" Fahrenheit"); } diff --git a/examples/DS2408_Switch/DS2408_Switch.ino b/examples/DS2408_Switch/DS2408_Switch.ino index d171f9b..cbb3ca0 100644 --- a/examples/DS2408_Switch/DS2408_Switch.ino +++ b/examples/DS2408_Switch/DS2408_Switch.ino @@ -1,5 +1,3 @@ -#include - /* * DS2408 8-Channel Addressable Switch * @@ -9,69 +7,83 @@ * - Unlike most input/output ports, the DS2408 doesn't have mode bits to * set whether the pins are input or output. If you issue a read command, * they're inputs. If you write to them, they're outputs. - * - For reading from a switch, you should use 10K pull-up resisters. + * - For reading from a switch, you should use 10K pull-up resistors. */ -void PrintBytes(uint8_t* addr, uint8_t count, bool newline=0) { - for (uint8_t i = 0; i < count; i++) { - Serial.print(addr[i]>>4, HEX); - Serial.print(addr[i]&0x0f, HEX); - } - if (newline) - Serial.println(); -} +#include +#include "../../src/platform.h" +#include "../../src/OneWire.h" -void ReadAndReport(OneWire* net, uint8_t* addr) { - Serial.print(" Reading DS2408 "); - PrintBytes(addr, 8); - Serial.println(); - - uint8_t buf[13]; // Put everything in the buffer so we can compute CRC easily. - buf[0] = 0xF0; // Read PIO Registers - buf[1] = 0x88; // LSB address - buf[2] = 0x00; // MSB address - net->write_bytes(buf, 3); - net->read_bytes(buf+3, 10); // 3 cmd bytes, 6 data bytes, 2 0xFF, 2 CRC16 - net->reset(); +OneWire onewire(10); // on pin 10 - if (!OneWire::check_crc16(buf, 11, &buf[11])) { - Serial.print("CRC failure in DS2408 at "); - PrintBytes(addr, 8, true); - return; - } - Serial.print(" DS2408 data = "); - // First 3 bytes contain command, register address. - Serial.println(buf[3], BIN); +void setup() +{ + Serial.begin(115200); } -OneWire net(10); // on pin 10 +void loop() +{ + uint8_t device_address[8]; -void setup(void) { - Serial.begin(9600); -} + delay(50); + + if (!onewire.search(device_address)) + { + Serial.println("No more addresses."); + onewire.reset_search(); + delay(250); + return; + } + + Serial.print("ROM ="); + for(uint8_t index = 0; index < 8; index++) + { + Serial.print(" "); + Serial.print(device_address[index], HEX); + } + + if (OneWire::crc8(device_address, 7) == device_address[7]) + { + Serial.print(" - CRC valid"); + } + else + { + Serial.println(" - ERROR: CRC is not valid!"); + return; + } + + // start of device dependent code + + if (device_address[0] == 0x29) + { + Serial.println(" - Device is a DS2408"); + } + else + { + Serial.println(" - ERROR: not a DS2408"); + return; + } + + const bool device_is_present = onewire.reset(); + onewire.select(device_address); + + uint8_t device_data[13]; // Put everything in the buffer so we can compute CRC easily. + device_data[0] = 0xF0; // Read PIO Registers + device_data[1] = 0x88; // LSB address + device_data[2] = 0x00; // MSB address + onewire.write_bytes(device_data, 3); + onewire.read_bytes(device_data + 3, 10); // 3 cmd bytes, 6 data bytes, 2 0xFF, 2 CRC16 + onewire.reset(); -void loop(void) { - byte i; - byte present = 0; - byte addr[8]; - - if (!net.search(addr)) { - Serial.print("No more addresses.\n"); - net.reset_search(); - delay(1000); - return; - } - - if (OneWire::crc8(addr, 7) != addr[7]) { - Serial.print("CRC is not valid!\n"); - return; - } - - if (addr[0] != 0x29) { - PrintBytes(addr, 8); - Serial.print(" is not a DS2408.\n"); - return; - } + Serial.print(" DS2408 data = "); + for(uint8_t index = 0; index < 11; index++) + { + Serial.print(" "); + Serial.print(device_data[index], HEX); + } - ReadAndReport(&net, addr); + if (!OneWire::check_crc16(device_data, 11, &device_data[11])) + { + Serial.print(" - ERROR: CRC failure in data"); + } } diff --git a/examples/DS250x_PROM/DS250x_PROM.ino b/examples/DS250x_PROM/DS250x_PROM.ino index 204c38a..884ef86 100644 --- a/examples/DS250x_PROM/DS250x_PROM.ino +++ b/examples/DS250x_PROM/DS250x_PROM.ino @@ -23,53 +23,59 @@ DS250x add-only programmable memory reader w/SKIP ROM. */ #include + OneWire oneWire(6); // OneWire bus on digital pin 6 -void setup() { - Serial.begin (9600); +void setup() +{ + Serial.begin(9600); } -void loop() { - byte i; // This is for the for loops - boolean present; // device present var - byte data[32]; // container for the data from device - byte leemem[3] = { // array with the commands to initiate a read, DS250x devices expect 3 bytes to start a read: command,LSB&MSB adresses - 0xF0 , 0x00 , 0x00 }; // 0xF0 is the Read Data command, followed by 00h 00h as starting address(the beginning, 0000h) - byte ccrc; // Variable to store the command CRC - byte ccrc_calc; - - present = oneWire.reset(); // OneWire bus reset, always needed to start operation on the bus, returns a 1/TRUE if there's a device present. - oneWire.skip(); // Skip ROM search - - if (present == TRUE){ // We only try to read the data if there's a device present - Serial.println("DS250x device present"); - oneWire.write(leemem[0],1); // Read data command, leave ghost power on - oneWire.write(leemem[1],1); // LSB starting address, leave ghost power on - oneWire.write(leemem[2],1); // MSB starting address, leave ghost power on - - ccrc = oneWire.read(); // DS250x generates a CRC for the command we sent, we assign a read slot and store it's value - ccrc_calc = OneWire::crc8(leemem, 3); // We calculate the CRC of the commands we sent using the library function and store it - - if ( ccrc_calc != ccrc) { // Then we compare it to the value the ds250x calculated, if it fails, we print debug messages and abort - Serial.println("Invalid command CRC!"); - Serial.print("Calculated CRC:"); - Serial.println(ccrc_calc,HEX); // HEX makes it easier to observe and compare - Serial.print("DS250x readback CRC:"); - Serial.println(ccrc,HEX); - return; // Since CRC failed, we abort the rest of the loop and start over - } - Serial.println("Data is: "); // For the printout of the data - for ( i = 0; i < 32; i++) { // Now it's time to read the PROM data itself, each page is 32 bytes so we need 32 read commands - data[i] = oneWire.read(); // we store each read byte to a different position in the data array - Serial.print(data[i]); // printout in ASCII - Serial.print(" "); // blank space +void loop() +{ + byte i; // This is for the for loops + boolean present; // device present var + byte data[32]; // container for the data from device + byte leemem[3] = { // array with the commands to initiate a read, DS250x devices expect 3 bytes to start a read: command,LSB&MSB adresses + 0xF0, 0x00, 0x00}; // 0xF0 is the Read Data command, followed by 00h 00h as starting address(the beginning, 0000h) + byte ccrc; // Variable to store the command CRC + byte ccrc_calc; + + present = oneWire.reset(); // OneWire bus reset, always needed to start operation on the bus, returns a 1/TRUE if there's a device present. + oneWire.skip(); // Skip ROM search + + if (present == TRUE) + { // We only try to read the data if there's a device present + Serial.println("DS250x device present"); + oneWire.write(leemem[0], 1); // Read data command, leave ghost power on + oneWire.write(leemem[1], 1); // LSB starting address, leave ghost power on + oneWire.write(leemem[2], 1); // MSB starting address, leave ghost power on + + ccrc = oneWire.read(); // DS250x generates a CRC for the command we sent, we assign a read slot and store it's value + ccrc_calc = OneWire::crc8(leemem, 3); // We calculate the CRC of the commands we sent using the library function and store it + + if (ccrc_calc != ccrc) + { // Then we compare it to the value the ds250x calculated, if it fails, we print debug messages and abort + Serial.println("Invalid command CRC!"); + Serial.print("Calculated CRC:"); + Serial.println(ccrc_calc, HEX); // HEX makes it easier to observe and compare + Serial.print("DS250x readback CRC:"); + Serial.println(ccrc, HEX); + return; // Since CRC failed, we abort the rest of the loop and start over + } + Serial.println("Data is: "); // For the printout of the data + for (i = 0; i < 32; i++) + { // Now it's time to read the PROM data itself, each page is 32 bytes so we need 32 read commands + data[i] = oneWire.read(); // we store each read byte to a different position in the data array + Serial.print(data[i]); // printout in ASCII + Serial.print(" "); // blank space + } + Serial.println(); + delay(5000); // Delay so we don't saturate the serial output + } else + { // Nothing is connected in the bus + Serial.println("Nothing connected"); + delay(3000); } - Serial.println(); - delay(5000); // Delay so we don't saturate the serial output - } - else { // Nothing is connected in the bus - Serial.println("Nothing connected"); - delay(3000); - } } From 8d0e99a8a56469cafb85aa5d66c0e744ad325713 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 14:57:55 +0200 Subject: [PATCH 19/71] rework examples to make them coherent --- .../DS18x20_Temperature.ino | 26 ++-- examples/DS2408_Switch/DS2408_Switch.ino | 21 +-- examples/DS250x_PROM/DS250x_PROM.ino | 131 ++++++++++++------ 3 files changed, 114 insertions(+), 64 deletions(-) diff --git a/examples/DS18x20_Temperature/DS18x20_Temperature.ino b/examples/DS18x20_Temperature/DS18x20_Temperature.ino index d35350d..082f9a7 100644 --- a/examples/DS18x20_Temperature/DS18x20_Temperature.ino +++ b/examples/DS18x20_Temperature/DS18x20_Temperature.ino @@ -16,9 +16,13 @@ void setup() void loop() { - uint8_t device_address[8]; + /// start general bus search: safer for unknown bus-devices + // alternative 1: directly address device (onewire.select()) if you know the ROM + // alternative 2: if only ONE device is present: issue skip-rom-cmd skip() after reset() - if (!oneWire.search(device_address)) + uint8_t device_rom[8]; + + if (!oneWire.search(device_rom)) { Serial.println("No more addresses."); oneWire.reset_search(); @@ -30,20 +34,20 @@ void loop() for (uint8_t index = 0; index < 8; index++) { Serial.print(" "); - Serial.print(device_address[index], HEX); + Serial.print(device_rom[index], HEX); } - if (OneWire::crc8(device_address, 7) != device_address[7]) + if (OneWire::crc8(device_rom, 7) != device_rom[7]) { Serial.println(" ERROR: CRC is not valid!"); return; } - // start of device dependent code + /// start of device dependent code // the first ROM byte indicates which chip uint8_t device_type = 0; // zero for all but 18S20 - switch (device_address[0]) + switch (device_rom[0]) { case 0x10: Serial.println(" Chip = DS18S20"); // or old DS1820 @@ -60,15 +64,17 @@ void loop() return; } - oneWire.reset(); - oneWire.select(device_address); + bool device_is_present = oneWire.reset(); + if (!device_is_present) return; + oneWire.select(device_rom); oneWire.write(0x44, 1); // start conversion, with parasite power on at the end delay(1000); // maybe 750ms is enough, maybe not // we might do a oneWire.depower() here, but the reset will take care of it. - const bool device_is_present = oneWire.reset(); - oneWire.select(device_address); + device_is_present = oneWire.reset(); + if (!device_is_present) return; + oneWire.select(device_rom); oneWire.write(0xBE); // Read Scratchpad Serial.print(" Data = "); diff --git a/examples/DS2408_Switch/DS2408_Switch.ino b/examples/DS2408_Switch/DS2408_Switch.ino index cbb3ca0..9e9c40e 100644 --- a/examples/DS2408_Switch/DS2408_Switch.ino +++ b/examples/DS2408_Switch/DS2408_Switch.ino @@ -11,10 +11,8 @@ */ #include -#include "../../src/platform.h" -#include "../../src/OneWire.h" -OneWire onewire(10); // on pin 10 +OneWire onewire(10); // on pin 10 (a 4.7K resistor is necessary) void setup() { @@ -23,11 +21,15 @@ void setup() void loop() { - uint8_t device_address[8]; + /// start general bus search: safer for unknown bus-devices + // alternative 1: directly address device (onewire.select()) if you know the ROM + // alternative 2: if only ONE device is present: issue skip-rom-cmd skip() after reset() + + uint8_t device_rom[8]; delay(50); - if (!onewire.search(device_address)) + if (!onewire.search(device_rom)) { Serial.println("No more addresses."); onewire.reset_search(); @@ -39,10 +41,10 @@ void loop() for(uint8_t index = 0; index < 8; index++) { Serial.print(" "); - Serial.print(device_address[index], HEX); + Serial.print(device_rom[index], HEX); } - if (OneWire::crc8(device_address, 7) == device_address[7]) + if (OneWire::crc8(device_rom, 7) == device_rom[7]) { Serial.print(" - CRC valid"); } @@ -54,7 +56,7 @@ void loop() // start of device dependent code - if (device_address[0] == 0x29) + if (device_rom[0] == 0x29) { Serial.println(" - Device is a DS2408"); } @@ -65,7 +67,8 @@ void loop() } const bool device_is_present = onewire.reset(); - onewire.select(device_address); + if (!device_is_present) return; + onewire.select(device_rom); uint8_t device_data[13]; // Put everything in the buffer so we can compute CRC easily. device_data[0] = 0xF0; // Read PIO Registers diff --git a/examples/DS250x_PROM/DS250x_PROM.ino b/examples/DS250x_PROM/DS250x_PROM.ino index 884ef86..c78ffdd 100644 --- a/examples/DS250x_PROM/DS250x_PROM.ino +++ b/examples/DS250x_PROM/DS250x_PROM.ino @@ -24,58 +24,99 @@ DS250x add-only programmable memory reader w/SKIP ROM. #include -OneWire oneWire(6); // OneWire bus on digital pin 6 +OneWire oneWire(10); // on pin 10 (a 4.7K resistor is necessary) + void setup() { - Serial.begin(9600); + Serial.begin(115200); } void loop() { - byte i; // This is for the for loops - boolean present; // device present var - byte data[32]; // container for the data from device - byte leemem[3] = { // array with the commands to initiate a read, DS250x devices expect 3 bytes to start a read: command,LSB&MSB adresses - 0xF0, 0x00, 0x00}; // 0xF0 is the Read Data command, followed by 00h 00h as starting address(the beginning, 0000h) - byte ccrc; // Variable to store the command CRC - byte ccrc_calc; - - present = oneWire.reset(); // OneWire bus reset, always needed to start operation on the bus, returns a 1/TRUE if there's a device present. - oneWire.skip(); // Skip ROM search - - if (present == TRUE) - { // We only try to read the data if there's a device present - Serial.println("DS250x device present"); - oneWire.write(leemem[0], 1); // Read data command, leave ghost power on - oneWire.write(leemem[1], 1); // LSB starting address, leave ghost power on - oneWire.write(leemem[2], 1); // MSB starting address, leave ghost power on - - ccrc = oneWire.read(); // DS250x generates a CRC for the command we sent, we assign a read slot and store it's value - ccrc_calc = OneWire::crc8(leemem, 3); // We calculate the CRC of the commands we sent using the library function and store it - - if (ccrc_calc != ccrc) - { // Then we compare it to the value the ds250x calculated, if it fails, we print debug messages and abort - Serial.println("Invalid command CRC!"); - Serial.print("Calculated CRC:"); - Serial.println(ccrc_calc, HEX); // HEX makes it easier to observe and compare - Serial.print("DS250x readback CRC:"); - Serial.println(ccrc, HEX); - return; // Since CRC failed, we abort the rest of the loop and start over - } - Serial.println("Data is: "); // For the printout of the data - for (i = 0; i < 32; i++) - { // Now it's time to read the PROM data itself, each page is 32 bytes so we need 32 read commands - data[i] = oneWire.read(); // we store each read byte to a different position in the data array - Serial.print(data[i]); // printout in ASCII - Serial.print(" "); // blank space - } - Serial.println(); - delay(5000); // Delay so we don't saturate the serial output - } else - { // Nothing is connected in the bus - Serial.println("Nothing connected"); - delay(3000); + /// start general bus search: safer for unknown bus-devices + // alternative 1: directly address device (onewire.select()) if you know the ROM + // alternative 2: if only ONE device is present: issue skip-rom-cmd skip() after reset() + + uint8_t device_rom[8]; + + if (!oneWire.search(device_rom)) + { + Serial.println("No more addresses."); + oneWire.reset_search(); + delay(250); + return; + } + + Serial.print("ROM ="); + for (uint8_t index = 0; index < 8; index++) + { + Serial.print(" "); + Serial.print(device_rom[index], HEX); + } + + if (OneWire::crc8(device_rom, 7) != device_rom[7]) + { + Serial.println(" ERROR: CRC is not valid!"); + return; + } + + // start of device dependent code + + switch (device_rom[0]) + { + case 0x11: + case 0x91: + Serial.println(" Chip = DS2501"); // 64 byte + break; + case 0x09: + Serial.println(" Chip = DS2502"); // 512 byte + break; + case 0x13: + Serial.println(" Chip = DS2503"); // 512 byte + break; + case 0x28: + Serial.println(" Chip = DS2505"); // 2048 byte + break; + case 0x22: + Serial.println(" Chip = DS2506"); // 8192 byte + break; + default: + Serial.println(" ERROR: not a DS18x20 family device."); + return; + } + + const bool device_is_present = oneWire.reset(); + if (!device_is_present) return; + oneWire.select(device_rom); + + // array with the commands to initiate a read, DS250x devices expect 3 bytes to start a read: command,LSB&MSB addresses + uint8_t cmd_read[3] = { 0xF0, 0x00, 0x00 }; // 0xF0 is the Read Data command, followed by 00h 00h as starting address(the beginning, 0000h) + oneWire.write_bytes(cmd_read, 3, true); // Read data command, leave ghost power on + + const uint8_t crc_read = oneWire.read(); // DS250x generates a CRC for the command we sent, we assign a read slot and store it's value + const uint8_t crc_calc = OneWire::crc8(cmd_read, 3); // We calculate the CRC of the commands we sent using the library function and store it + + if (crc_calc != crc_read) + { // Then we compare it to the value the ds250x calculated, if it fails, we print debug messages and abort + Serial.println("Invalid command CRC!"); + Serial.print("Calculated CRC:"); + Serial.println(crc_calc, HEX); // HEX makes it easier to observe and compare + Serial.print("DS250x readback CRC:"); + Serial.println(crc_read, HEX); + return; // Since CRC failed, we abort the rest of the loop and start over } + + Serial.println("Data is: "); // For the printout of the data + uint8_t device_data[32]; // container for the data from device + for (uint8_t index = 0; index < 32; index++) + { // Now it's time to read the PROM data itself, each page is 32 bytes so we need 32 read commands + device_data[index] = oneWire.read(); // we store each read byte to a different position in the data array + Serial.print(device_data[index], HEX); // printout in ASCII + Serial.print(" "); // blank space + } + Serial.println(); + delay(5000); // Delay so we don't saturate the serial output + } From fedcbee99af827529037e5c7ee2600b7c87c2a38 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 16:43:25 +0200 Subject: [PATCH 20/71] add note for proper resistor --- examples/DS18x20_Temperature/DS18x20_Temperature.ino | 2 +- examples/DS2408_Switch/DS2408_Switch.ino | 2 +- examples/DS250x_PROM/DS250x_PROM.ino | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/DS18x20_Temperature/DS18x20_Temperature.ino b/examples/DS18x20_Temperature/DS18x20_Temperature.ino index 082f9a7..13c5ddc 100644 --- a/examples/DS18x20_Temperature/DS18x20_Temperature.ino +++ b/examples/DS18x20_Temperature/DS18x20_Temperature.ino @@ -7,7 +7,7 @@ #include -OneWire oneWire(10); // on pin 10 (a 4.7K resistor is necessary) +OneWire oneWire(10); // on pin 10 (a 1k to 4.7K resistor is necessary) void setup() { diff --git a/examples/DS2408_Switch/DS2408_Switch.ino b/examples/DS2408_Switch/DS2408_Switch.ino index 9e9c40e..c66b8f2 100644 --- a/examples/DS2408_Switch/DS2408_Switch.ino +++ b/examples/DS2408_Switch/DS2408_Switch.ino @@ -12,7 +12,7 @@ #include -OneWire onewire(10); // on pin 10 (a 4.7K resistor is necessary) +OneWire onewire(10); // on pin 10 (a 1k to 4.7K resistor is necessary) void setup() { diff --git a/examples/DS250x_PROM/DS250x_PROM.ino b/examples/DS250x_PROM/DS250x_PROM.ino index c78ffdd..9b5ba68 100644 --- a/examples/DS250x_PROM/DS250x_PROM.ino +++ b/examples/DS250x_PROM/DS250x_PROM.ino @@ -24,7 +24,7 @@ DS250x add-only programmable memory reader w/SKIP ROM. #include -OneWire oneWire(10); // on pin 10 (a 4.7K resistor is necessary) +OneWire oneWire(10); // on pin 10 (a 1k to 4.7K resistor is necessary) void setup() { From 8c7bf6b68a6382851b9678b478b3d24e2da6a436 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 16:43:53 +0200 Subject: [PATCH 21/71] optimize naming vars --- src/OneWire.cpp | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 6047ecc..71041da 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -202,7 +202,8 @@ void OneWire::write_bit(const bool value) DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high interrupts(); delayMicroseconds(55); - } else + } + else { noInterrupts(); DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); @@ -239,13 +240,15 @@ bool OneWire::read_bit() // go tri-state at the end of the write to avoid heating in a short or // other mishap. // -void OneWire::write(const uint8_t value, const bool power) +void OneWire::write(uint8_t value, const bool power) { - for (uint8_t bitMask = 0x01; bitMask != 0; bitMask <<= 1) + for (uint8_t index = 0; index < 8; index++) { - write_bit((bitMask & value) != 0); // TODO: shifting value could be me faster + write_bit((value & 0x01) != 0); // shifting value is faster than clean solution with bitmask (~18 byte on arduino) + value >>= 1; } + if (!power) { noInterrupts(); @@ -255,11 +258,11 @@ void OneWire::write(const uint8_t value, const bool power) } } -void OneWire::write_bytes(const uint8_t *buf, const uint16_t data_size, const bool power) +void OneWire::write_bytes(const uint8_t data_array[], const uint16_t data_size, const bool power) { - for (uint16_t index = 0; index < data_size; index++) // TODO: can be tuned + for (uint16_t index = 0; index < data_size; index++) // not slower than solution without index { - write(buf[index]); + write(data_array[index]); } if (!power) @@ -285,17 +288,11 @@ uint8_t OneWire::read() return value; } -void OneWire::read_bytes(uint8_t data_array[], uint16_t data_size) +void OneWire::read_bytes(uint8_t data_array[], const uint16_t data_size) { - for (uint16_t index = 0; index < data_size; index++) - // while (data_size-- > 0) + for (uint16_t index = 0; index < data_size; index++) // not slower than solution without index { data_array[index] = read(); - - //*data_array = read(); // TODO: test if it can be combined in one line - - //*data_array = read(); // TODO: test if it can be combined in one line - //data_array++; } } @@ -400,7 +397,8 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) if (search_mode) { write(0xF0); // NORMAL SEARCH - } else + } + else { write(0xEC); // CONDITIONAL SEARCH } From 8d034430e07aa9a4b46bf4d570257b8751136ef5 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 16:56:02 +0200 Subject: [PATCH 22/71] ease up on interrupt handling --- src/OneWire.cpp | 31 +++---------------------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 71041da..5654b00 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -163,9 +163,7 @@ bool OneWire::reset() { uint8_t retries = 125; - noInterrupts(); DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); - interrupts(); // wait until the wire is high... just in case do { @@ -173,10 +171,8 @@ bool OneWire::reset() delayMicroseconds(2); } while (!DIRECT_READ(pin_baseReg, pin_bitMask)); - noInterrupts(); DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low - interrupts(); delayMicroseconds(480); noInterrupts(); DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); // allow it to float @@ -193,26 +189,13 @@ bool OneWire::reset() // void OneWire::write_bit(const bool value) { - if (value) - { - noInterrupts(); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low - delayMicroseconds(10); - DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high - interrupts(); - delayMicroseconds(55); - } - else - { noInterrupts(); DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low - delayMicroseconds(65); + delayMicroseconds(value ? 10 : 60); DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high interrupts(); - delayMicroseconds(5); - } + delayMicroseconds(value ? 55 : 5); } // @@ -222,8 +205,8 @@ void OneWire::write_bit(const bool value) bool OneWire::read_bit() { noInterrupts(); - DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); delayMicroseconds(3); DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); // let pin float, pull up will raise delayMicroseconds(10); @@ -251,10 +234,7 @@ void OneWire::write(uint8_t value, const bool power) if (!power) { - noInterrupts(); DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - interrupts(); } } @@ -267,10 +247,7 @@ void OneWire::write_bytes(const uint8_t data_array[], const uint16_t data_size, if (!power) { - noInterrupts(); DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - interrupts(); } } @@ -320,9 +297,7 @@ void OneWire::skip() void OneWire::depower() { - noInterrupts(); DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); - interrupts(); } // From 0a14df1afd41937730dd65d2bc08da5d49fdf50e Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 17:14:45 +0200 Subject: [PATCH 23/71] clean up search() --- src/OneWire.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 5654b00..ffb17ab 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -350,10 +350,8 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) uint8_t rom_byte_number = 0; bool search_result = false; - bool id_bit, cmp_id_bit; - uint8_t rom_byte_mask = 1; - bool search_direction; + // if the last call was not the last one if (!search_last_device_flag) @@ -382,47 +380,55 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) do { // read a bit and its complement - id_bit = read_bit(); - cmp_id_bit = read_bit(); + const bool id_bit = read_bit(); + const bool cmp_id_bit = read_bit(); // check for no devices on 1-wire - if ((id_bit) && (cmp_id_bit)) + if (id_bit && cmp_id_bit) { break; } else { + bool search_direction; // 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 < search_last_discrepancy) + { search_direction = ((search_rom_array[rom_byte_number] & rom_byte_mask) > 0); + } else + { // if equal to last pick 1, if not then pick 0 search_direction = (id_bit_number == search_last_discrepancy); - + } // if 0 was picked then record its position in LastZero if (!search_direction) { last_zero = id_bit_number; // check for Last discrepancy in family - if (last_zero < 9) - search_last_family_discrepancy = last_zero; + if (last_zero < 9) search_last_family_discrepancy = last_zero; } } // set or clear the bit in the ROM byte rom_byte_number // with mask rom_byte_mask if (search_direction) + { search_rom_array[rom_byte_number] |= rom_byte_mask; + } else + { search_rom_array[rom_byte_number] &= ~rom_byte_mask; - + } // serial number search direction write bit write_bit(search_direction); From 384926fef2295a6acae341121521ca831f2dc4ef Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 17:46:28 +0200 Subject: [PATCH 24/71] optimize write-routine --- src/OneWire.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index ffb17ab..25b0f6f 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -189,13 +189,16 @@ bool OneWire::reset() // void OneWire::write_bit(const bool value) { - noInterrupts(); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low - delayMicroseconds(value ? 10 : 60); - DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high - interrupts(); - delayMicroseconds(value ? 55 : 5); + const uint8_t time_high = (value ? 10 : 60); + const uint8_t time_low = (value ? 55 : 5); + + noInterrupts(); + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low + delayMicroseconds(time_high); + DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high + interrupts(); + delayMicroseconds(time_low); } // @@ -467,12 +470,10 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) search_last_device_flag = false; search_last_family_discrepancy = 0; search_result = false; - } else + } + else { - for (int index = 0; index < 8; index++) - { - new_rom_array[index] = search_rom_array[index]; - } + memcpy(new_rom_array, search_rom_array, 8); } return search_result; } From bed077172a27ef5a09a5d008813a13b1fe96e43e Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 17:59:57 +0200 Subject: [PATCH 25/71] allow local copies of pins to save pgm-space --- src/OneWire.cpp | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 25b0f6f..967e0c4 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -144,10 +144,13 @@ sample code bearing this copyright. OneWire::OneWire(const uint8_t pin) { // prepare pin + io_reg_t _bitMask=pin_bitMask; // local copies save pgm-space when called >1 + volatile io_reg_t *_baseReg = pin_baseReg; + pinMode(pin, INPUT); - pin_bitMask = PIN_TO_BITMASK(pin); - pin_baseReg = PIN_TO_BASEREG(pin); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + _bitMask = PIN_TO_BITMASK(pin); + _baseReg = PIN_TO_BASEREG(pin); + DIRECT_WRITE_LOW(_baseReg, _bitMask); reset_search(); // really needed? } @@ -161,23 +164,26 @@ OneWire::OneWire(const uint8_t pin) // bool OneWire::reset() { + const io_reg_t _bitMask=pin_bitMask; // local copies save pgm-space when called >1 + volatile io_reg_t *_baseReg = pin_baseReg; + uint8_t retries = 125; - DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); + DIRECT_MODE_INPUT(_baseReg, _bitMask); // wait until the wire is high... just in case do { if (--retries == 0) return false; delayMicroseconds(2); - } while (!DIRECT_READ(pin_baseReg, pin_bitMask)); + } while (!DIRECT_READ(_baseReg, _bitMask)); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low + DIRECT_WRITE_LOW(_baseReg, _bitMask); + DIRECT_MODE_OUTPUT(_baseReg, _bitMask); // drive output low delayMicroseconds(480); noInterrupts(); - DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); // allow it to float + DIRECT_MODE_INPUT(_baseReg, _bitMask); // allow it to float delayMicroseconds(70); - const bool success = !DIRECT_READ(pin_baseReg, pin_bitMask); + const bool success = !DIRECT_READ(_baseReg, _bitMask); interrupts(); delayMicroseconds(410); return success; @@ -189,14 +195,16 @@ bool OneWire::reset() // void OneWire::write_bit(const bool value) { - const uint8_t time_high = (value ? 10 : 60); - const uint8_t time_low = (value ? 55 : 5); + const uint8_t time_high = uint8_t(value ? 10 : 60); + const uint8_t time_low = uint8_t(value ? 55 : 5); + const io_reg_t _bitMask=pin_bitMask; // local copies save pgm-space when called >1 + volatile io_reg_t *_baseReg = pin_baseReg; noInterrupts(); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low + DIRECT_WRITE_LOW(_baseReg, _bitMask); + DIRECT_MODE_OUTPUT(_baseReg, _bitMask); // drive output low delayMicroseconds(time_high); - DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high + DIRECT_WRITE_HIGH(_baseReg, _bitMask); // drive output high interrupts(); delayMicroseconds(time_low); } @@ -207,13 +215,16 @@ void OneWire::write_bit(const bool value) // bool OneWire::read_bit() { + const io_reg_t _bitMask=pin_bitMask; // local copies save pgm-space when called >1 + volatile io_reg_t *_baseReg = pin_baseReg; + noInterrupts(); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); - DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); + DIRECT_WRITE_LOW(_baseReg, _bitMask); + DIRECT_MODE_OUTPUT(_baseReg, _bitMask); delayMicroseconds(3); - DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); // let pin float, pull up will raise + DIRECT_MODE_INPUT(_baseReg, _bitMask); // let pin float, pull up will raise delayMicroseconds(10); - const bool value = DIRECT_READ(pin_baseReg, pin_bitMask); + const bool value = DIRECT_READ(_baseReg, _bitMask); interrupts(); delayMicroseconds(53); return value; From ba375708f76fafc069a97cf25b0b3d0cdf3201bd Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 18:05:52 +0200 Subject: [PATCH 26/71] bugfix of latest commit --- src/OneWire.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 967e0c4..1915ad8 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -144,13 +144,10 @@ sample code bearing this copyright. OneWire::OneWire(const uint8_t pin) { // prepare pin - io_reg_t _bitMask=pin_bitMask; // local copies save pgm-space when called >1 - volatile io_reg_t *_baseReg = pin_baseReg; - pinMode(pin, INPUT); - _bitMask = PIN_TO_BITMASK(pin); - _baseReg = PIN_TO_BASEREG(pin); - DIRECT_WRITE_LOW(_baseReg, _bitMask); + pin_bitMask = PIN_TO_BITMASK(pin); + pin_baseReg = PIN_TO_BASEREG(pin); + DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); reset_search(); // really needed? } From 861947b318b8463e2b28042c37d7ffc4ad38bae2 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 18:38:18 +0200 Subject: [PATCH 27/71] add cmake --- CMakeLists.txt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7ad144c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.6) +project(OneWire) + +set(CMAKE_CXX_STANDARD 14) + +set(SOURCE_FILES + examples/DS18x20_Temperature/DS18x20_Temperature.ino + examples/DS2408_Switch/DS2408_Switch.ino + examples/DS250x_PROM/DS250x_PROM.ino + src/platform.cpp + src/OneWire.cpp + ) + +add_executable(OneWire ${SOURCE_FILES}) \ No newline at end of file From b0059ea735db3f31faf4b2a73c1bdd67f8f72a5d Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 23:23:41 +0200 Subject: [PATCH 28/71] add new bus discovery --- examples/bus_discovery/bus_discovery.ino | 122 +++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 examples/bus_discovery/bus_discovery.ino diff --git a/examples/bus_discovery/bus_discovery.ino b/examples/bus_discovery/bus_discovery.ino new file mode 100644 index 0000000..6c9d622 --- /dev/null +++ b/examples/bus_discovery/bus_discovery.ino @@ -0,0 +1,122 @@ +// OneWire Bus Discovery +// +// http://www.pjrc.com/teensy/td_libs_OneWire.html +// + +#include + +OneWire oneWire(10); // on pin 10 (a 1k to 4.7K resistor is necessary) + +void setup() +{ + Serial.begin(115200); +} + +void loop() +{ + /// start general bus search: safer for unknown bus-devices + + uint8_t device_rom[8]; + + delay(50); + + if (!oneWire.search(device_rom)) + { + Serial.println("No more addresses."); + oneWire.reset_search(); + delay(250); + return; + } + + Serial.print("ROM ="); + for (uint8_t index = 0; index < 8; index++) + { + Serial.print(" "); + Serial.print(device_rom[index], HEX); + } + + if (device_rom[7] != oneWire.crc8(device_rom, 7)) + { + Serial.println(" - ERROR: CRC not valid!"); + return; + } + + /// start of device dependent code + + // the first ROM byte indicates which chip + Serial.print(" - Chip = "); + switch (device_rom[0]) + { + case 0x01: + Serial.println("DS1990, DS2401 or DS2411"); + break; + case 0x05: + Serial.println("DS2405"); + break; + case 0x09: + Serial.println("DS2502"); // 512 byte + break; + case 0x0B: + Serial.println("DS2505"); + break; + case 0x0F: + Serial.println("DS2506"); + break; + case 0x10: + Serial.println("DS18S20"); // or old DS1820 + break; + case 0x11: + Serial.println("DS2501"); // 64 byte + break; + case 0x13: + Serial.println("DS2503"); // 512 byte + break; + case 0x1D: + Serial.println("DS2423"); + break; + case 0x20: + Serial.println("DS2450"); + break; + case 0x22: + Serial.println("DS1822"); + break; + case 0x26: + Serial.println("DS2438"); + break; + case 0x28: + Serial.println("DS18B20"); + break; + case 0x29: + Serial.println("DS2408"); + break; + case 0x2C: + Serial.println("DS2890"); + break; + case 0x2D: + Serial.println("DS2431"); + break; + case 0x33: + Serial.println("DS2432"); + break; + case 0x3A: + Serial.println("DS2413"); + break; + case 0x81: + Serial.println("DS1990A"); + break; + case 0x89: + Serial.println("DS2502"); + break; + case 0x91: + Serial.println("DS2501"); // 64 byte, alternative ID + break; + case 0xFC: + Serial.println("BAE0910"); + break; + default: + Serial.println("unknown"); + return; + } + + +} From 0f37385271f54030c1153a9dc363c7fc5f3a3ed2 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 23:25:16 +0200 Subject: [PATCH 29/71] add new example for ds2438 battery monitor --- examples/DS2438_BatMon/DS2438_BatMon.ino | 103 +++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 examples/DS2438_BatMon/DS2438_BatMon.ino diff --git a/examples/DS2438_BatMon/DS2438_BatMon.ino b/examples/DS2438_BatMon/DS2438_BatMon.ino new file mode 100644 index 0000000..e4679d3 --- /dev/null +++ b/examples/DS2438_BatMon/DS2438_BatMon.ino @@ -0,0 +1,103 @@ +// OneWire DS2438 Example +// +// You need : https://github.com/PaulStoffregen/OneWire + +#include + +OneWire oneWire(10); // on pin 10 (a 1k to 4.7K resistor is necessary) + +void setup() +{ + Serial.begin(115200); +} + +void loop() +{ + /// start general bus search: safer for unknown bus-devices + // alternative 1: directly address device (onewire.select()) if you know the ROM + // alternative 2: if only ONE device is present: issue skip-rom-cmd skip() after reset() + + delay(50); + + uint8_t device_rom[8]; + + if (!oneWire.search(device_rom)) + { + Serial.println("No more addresses."); + oneWire.reset_search(); + delay(250); + return; + } + + Serial.print("ROM ="); + for(uint8_t index = 0; index < 8; index++) + { + Serial.print(" "); + Serial.print(device_rom[index], HEX); + } + + if (device_rom[7] != oneWire.crc8(device_rom, 7)) + { + Serial.println(" - ERROR: CRC not valid!"); + return; + } + + // start of device dependent code + + Serial.print(" - Chip = "); + switch (device_rom[0]) + { + case 0x26: + Serial.println("DS2438"); + break; + default: + Serial.println("not a DS2438"); + return; + } + + bool device_is_present = oneWire.reset(); + if (!device_is_present) return; + oneWire.select(device_rom); + oneWire.write(0x44, 1); // start T_conversion, with parasite power on at the end + //ds.write(0xB4, 1); // start V_conversion, with parasite power on at the end + + delay(1000); // maybe 750ms is enough, maybe not + + device_is_present = oneWire.reset(); + if (!device_is_present) return; + oneWire.select(device_rom); + oneWire.write(0xBE); // Read Scratchpad + oneWire.write(0x00); // request Page 0, measurements + uint8_t device_data[9]; + oneWire.read_bytes(device_data,9); + + Serial.print(" data ="); + for(uint8_t index = 0; index < 9; index++) + { + Serial.print(" "); + Serial.print(device_data[index], HEX); + } + + if (device_data[8] != oneWire.crc8(device_data, 8)) + { + Serial.println(" - ERROR: CRC not valid!"); + return; + } + + // Convert the data to actual values + int16_t raw = (device_data[2] << 8) | device_data[1]; + const float temperature = raw / 256.0f; // degC + raw = ((device_data[4] << 8) & uint16_t(0x03)) | device_data[3]; + const float voltage = raw / 100.0f; // V + raw = (device_data[6] << 8) | (device_data[5]); + const float current = raw / 32.0f; // A + + // display data + Serial.print(" Temp = "); + Serial.print(temperature); + Serial.print(" degC, Voltage = "); + Serial.print(voltage); + Serial.print(" V, Current = "); + Serial.print(current); + Serial.println(" A"); +} From a747f8a84db8b6c55ba6f92a1e0d66952ce553d2 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 23:25:48 +0200 Subject: [PATCH 30/71] make examples more similar --- CMakeLists.txt | 2 + .../DS18x20_Temperature.ino | 35 +++++++------ examples/DS2408_Switch/DS2408_Switch.ino | 48 +++++++++-------- examples/DS250x_PROM/DS250x_PROM.ino | 51 +++++++++---------- 4 files changed, 70 insertions(+), 66 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ad144c..359700b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,11 @@ project(OneWire) set(CMAKE_CXX_STANDARD 14) set(SOURCE_FILES + examples/bus_discovery/bus_discovery.ino examples/DS18x20_Temperature/DS18x20_Temperature.ino examples/DS2408_Switch/DS2408_Switch.ino examples/DS250x_PROM/DS250x_PROM.ino + examples/DS2438_BatMon/DS2438_BatMon.ino src/platform.cpp src/OneWire.cpp ) diff --git a/examples/DS18x20_Temperature/DS18x20_Temperature.ino b/examples/DS18x20_Temperature/DS18x20_Temperature.ino index 13c5ddc..82d040c 100644 --- a/examples/DS18x20_Temperature/DS18x20_Temperature.ino +++ b/examples/DS18x20_Temperature/DS18x20_Temperature.ino @@ -20,6 +20,8 @@ void loop() // alternative 1: directly address device (onewire.select()) if you know the ROM // alternative 2: if only ONE device is present: issue skip-rom-cmd skip() after reset() + delay(50); + uint8_t device_rom[8]; if (!oneWire.search(device_rom)) @@ -37,30 +39,32 @@ void loop() Serial.print(device_rom[index], HEX); } - if (OneWire::crc8(device_rom, 7) != device_rom[7]) + if (oneWire.crc8(device_rom, 7) != device_rom[7]) { - Serial.println(" ERROR: CRC is not valid!"); + Serial.println(" - ERROR: CRC is not valid!"); return; } /// start of device dependent code // the first ROM byte indicates which chip + Serial.print(" - Chip = "); uint8_t device_type = 0; // zero for all but 18S20 + switch (device_rom[0]) { case 0x10: - Serial.println(" Chip = DS18S20"); // or old DS1820 + Serial.println("DS18S20"); // or old DS1820 device_type = 1; break; case 0x28: - Serial.println(" Chip = DS18B20"); + Serial.println("DS18B20"); break; case 0x22: - Serial.println(" Chip = DS1822"); + Serial.println("DS1822"); break; default: - Serial.println(" ERROR: not a DS18x20 family device."); + Serial.println("not in DS18x20 family"); return; } @@ -77,20 +81,21 @@ void loop() oneWire.select(device_rom); oneWire.write(0xBE); // Read Scratchpad - Serial.print(" Data = "); - Serial.print(device_is_present, HEX); - Serial.print(" "); - uint8_t device_data[12]; + oneWire.read_bytes(device_data,9); + + Serial.print(" Data ="); for (uint8_t index = 0; index < 9; index++) // we need 9 bytes { - device_data[index] = oneWire.read(); - Serial.print(device_data[index], HEX); Serial.print(" "); + Serial.print(device_data[index], HEX); + } + + if (device_rom[9] != oneWire.crc8(device_data, 8)) + { + Serial.println(" - ERROR: CRC not valid!"); + return; } - Serial.print(" CRC="); - Serial.print(OneWire::crc8(device_data, 8), HEX); - Serial.println(); // Convert the device_data to actual temperature // because the result is a 16 bit signed integer, it should diff --git a/examples/DS2408_Switch/DS2408_Switch.ino b/examples/DS2408_Switch/DS2408_Switch.ino index c66b8f2..57f6f7c 100644 --- a/examples/DS2408_Switch/DS2408_Switch.ino +++ b/examples/DS2408_Switch/DS2408_Switch.ino @@ -12,7 +12,7 @@ #include -OneWire onewire(10); // on pin 10 (a 1k to 4.7K resistor is necessary) +OneWire oneWire(10); // on pin 10 (a 1k to 4.7K resistor is necessary) void setup() { @@ -25,14 +25,14 @@ void loop() // alternative 1: directly address device (onewire.select()) if you know the ROM // alternative 2: if only ONE device is present: issue skip-rom-cmd skip() after reset() - uint8_t device_rom[8]; - delay(50); - if (!onewire.search(device_rom)) + uint8_t device_rom[8]; + + if (!oneWire.search(device_rom)) { Serial.println("No more addresses."); - onewire.reset_search(); + oneWire.reset_search(); delay(250); return; } @@ -44,11 +44,7 @@ void loop() Serial.print(device_rom[index], HEX); } - if (OneWire::crc8(device_rom, 7) == device_rom[7]) - { - Serial.print(" - CRC valid"); - } - else + if (device_rom[7] != oneWire.crc8(device_rom, 7)) { Serial.println(" - ERROR: CRC is not valid!"); return; @@ -56,37 +52,39 @@ void loop() // start of device dependent code - if (device_rom[0] == 0x29) - { - Serial.println(" - Device is a DS2408"); - } - else + Serial.print(" - Chip = "); + switch (device_rom[0]) { - Serial.println(" - ERROR: not a DS2408"); - return; + case 0x29: + Serial.println("DS2408"); + break; + default: + Serial.println("not a DS2408"); + return; } - const bool device_is_present = onewire.reset(); + const bool device_is_present = oneWire.reset(); if (!device_is_present) return; - onewire.select(device_rom); + oneWire.select(device_rom); uint8_t device_data[13]; // Put everything in the buffer so we can compute CRC easily. device_data[0] = 0xF0; // Read PIO Registers device_data[1] = 0x88; // LSB address device_data[2] = 0x00; // MSB address - onewire.write_bytes(device_data, 3); - onewire.read_bytes(device_data + 3, 10); // 3 cmd bytes, 6 data bytes, 2 0xFF, 2 CRC16 - onewire.reset(); + oneWire.write_bytes(device_data, 3); + oneWire.read_bytes(device_data + 3, 10); // 3 cmd bytes, 6 data bytes, 2 0xFF, 2 CRC16 + oneWire.reset(); - Serial.print(" DS2408 data = "); + Serial.print(" data ="); for(uint8_t index = 0; index < 11; index++) { Serial.print(" "); Serial.print(device_data[index], HEX); } - if (!OneWire::check_crc16(device_data, 11, &device_data[11])) + if (!oneWire.check_crc16(device_data, 11, &device_data[11])) { - Serial.print(" - ERROR: CRC failure in data"); + Serial.print(" - ERROR: CRC not valid"); } + Serial.println(); } diff --git a/examples/DS250x_PROM/DS250x_PROM.ino b/examples/DS250x_PROM/DS250x_PROM.ino index 9b5ba68..4103b3b 100644 --- a/examples/DS250x_PROM/DS250x_PROM.ino +++ b/examples/DS250x_PROM/DS250x_PROM.ino @@ -39,6 +39,8 @@ void loop() uint8_t device_rom[8]; + delay(50); + if (!oneWire.search(device_rom)) { Serial.println("No more addresses."); @@ -54,34 +56,34 @@ void loop() Serial.print(device_rom[index], HEX); } - if (OneWire::crc8(device_rom, 7) != device_rom[7]) + if (device_rom[7] != oneWire.crc8(device_rom, 7)) { - Serial.println(" ERROR: CRC is not valid!"); + Serial.println(" - ERROR: CRC not valid!"); return; } // start of device dependent code - + Serial.print(" - Chip = "); switch (device_rom[0]) { case 0x11: case 0x91: - Serial.println(" Chip = DS2501"); // 64 byte + Serial.println("DS2501"); // 64 byte break; case 0x09: - Serial.println(" Chip = DS2502"); // 512 byte + Serial.println("DS2502"); // 512 byte break; case 0x13: - Serial.println(" Chip = DS2503"); // 512 byte + Serial.println("DS2503"); // 512 byte break; - case 0x28: - Serial.println(" Chip = DS2505"); // 2048 byte + case 0x0B: + Serial.println("DS2505"); // 2048 byte break; - case 0x22: - Serial.println(" Chip = DS2506"); // 8192 byte + case 0x0F: + Serial.println("DS2506"); // 8192 byte break; default: - Serial.println(" ERROR: not a DS18x20 family device."); + Serial.println("not a DS250x"); return; } @@ -94,28 +96,25 @@ void loop() oneWire.write_bytes(cmd_read, 3, true); // Read data command, leave ghost power on const uint8_t crc_read = oneWire.read(); // DS250x generates a CRC for the command we sent, we assign a read slot and store it's value - const uint8_t crc_calc = OneWire::crc8(cmd_read, 3); // We calculate the CRC of the commands we sent using the library function and store it - - if (crc_calc != crc_read) - { // Then we compare it to the value the ds250x calculated, if it fails, we print debug messages and abort - Serial.println("Invalid command CRC!"); - Serial.print("Calculated CRC:"); - Serial.println(crc_calc, HEX); // HEX makes it easier to observe and compare - Serial.print("DS250x readback CRC:"); - Serial.println(crc_read, HEX); + + if (crc_read != oneWire.crc8(cmd_read, 3)) + { + Serial.println(" - ERROR: CRC not valid!"); return; // Since CRC failed, we abort the rest of the loop and start over } - Serial.println("Data is: "); // For the printout of the data - uint8_t device_data[32]; // container for the data from device + uint8_t device_data[32]; // container for the data from device + oneWire.read_bytes(device_data,32); // we store each read byte to a different position in the data array + + Serial.print(" data ="); // For the printout of the data + for (uint8_t index = 0; index < 32; index++) - { // Now it's time to read the PROM data itself, each page is 32 bytes so we need 32 read commands - device_data[index] = oneWire.read(); // we store each read byte to a different position in the data array - Serial.print(device_data[index], HEX); // printout in ASCII + { Serial.print(" "); // blank space + Serial.print(device_data[index], HEX); // printout } Serial.println(); - delay(5000); // Delay so we don't saturate the serial output + delay(1000); // Delay so we don't saturate the serial output } From bd5e7bbdf493282c6430a013cee1fac7c54c02d2 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 23:32:58 +0200 Subject: [PATCH 31/71] 4 byte overhead to allow pullups on avr ... do it --- src/OneWire.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 1915ad8..0d4a7b1 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -147,7 +147,7 @@ OneWire::OneWire(const uint8_t pin) pinMode(pin, INPUT); pin_bitMask = PIN_TO_BITMASK(pin); pin_baseReg = PIN_TO_BASEREG(pin); - DIRECT_WRITE_LOW(pin_baseReg, pin_bitMask); + DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // on avr: act as pullup in input-mode reset_search(); // really needed? } @@ -179,6 +179,7 @@ bool OneWire::reset() delayMicroseconds(480); noInterrupts(); DIRECT_MODE_INPUT(_baseReg, _bitMask); // allow it to float + DIRECT_WRITE_HIGH(_baseReg, _bitMask); // on avr: act as pullup in input-mode delayMicroseconds(70); const bool success = !DIRECT_READ(_baseReg, _bitMask); interrupts(); @@ -220,6 +221,7 @@ bool OneWire::read_bit() DIRECT_MODE_OUTPUT(_baseReg, _bitMask); delayMicroseconds(3); DIRECT_MODE_INPUT(_baseReg, _bitMask); // let pin float, pull up will raise + DIRECT_WRITE_HIGH(_baseReg, _bitMask); // on avr: act as pullup in input-mode delayMicroseconds(10); const bool value = DIRECT_READ(_baseReg, _bitMask); interrupts(); From fbb5bcb6f4e9c5185b9757b9356a914b9633132e Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 16 Jul 2017 23:45:21 +0200 Subject: [PATCH 32/71] fix latest two commits --- examples/DS18x20_Temperature/DS18x20_Temperature.ino | 3 ++- src/OneWire.cpp | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/DS18x20_Temperature/DS18x20_Temperature.ino b/examples/DS18x20_Temperature/DS18x20_Temperature.ino index 82d040c..333c28a 100644 --- a/examples/DS18x20_Temperature/DS18x20_Temperature.ino +++ b/examples/DS18x20_Temperature/DS18x20_Temperature.ino @@ -91,11 +91,12 @@ void loop() Serial.print(device_data[index], HEX); } - if (device_rom[9] != oneWire.crc8(device_data, 8)) + if (device_data[8] != oneWire.crc8(device_data, 8)) { Serial.println(" - ERROR: CRC not valid!"); return; } + Serial.println(); // Convert the device_data to actual temperature // because the result is a 16 bit signed integer, it should diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 0d4a7b1..abc0708 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -167,6 +167,7 @@ bool OneWire::reset() uint8_t retries = 125; DIRECT_MODE_INPUT(_baseReg, _bitMask); + DIRECT_WRITE_HIGH(_baseReg, _bitMask); // on avr: act as pullup in input-mode // wait until the wire is high... just in case do { From ddd1276c1368ea2baee038eecb47807b335b235e Mon Sep 17 00:00:00 2001 From: orgua Date: Mon, 17 Jul 2017 00:30:47 +0200 Subject: [PATCH 33/71] first try to make bus open drain compatible --- src/OneWire.cpp | 9 +++++++++ src/OneWire.h | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index abc0708..f07b7cf 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -203,6 +203,9 @@ void OneWire::write_bit(const bool value) DIRECT_WRITE_LOW(_baseReg, _bitMask); DIRECT_MODE_OUTPUT(_baseReg, _bitMask); // drive output low delayMicroseconds(time_high); +#if ONEWIRE_OPEN_DRAIN_ONLY + DIRECT_MODE_INPUT(_baseReg, _bitMask); // allow it to float +#endif DIRECT_WRITE_HIGH(_baseReg, _bitMask); // drive output high interrupts(); delayMicroseconds(time_low); @@ -246,10 +249,12 @@ void OneWire::write(uint8_t value, const bool power) value >>= 1; } +#if !ONEWIRE_OPEN_DRAIN_ONLY if (!power) { DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); } +#endif } void OneWire::write_bytes(const uint8_t data_array[], const uint16_t data_size, const bool power) @@ -259,10 +264,12 @@ void OneWire::write_bytes(const uint8_t data_array[], const uint16_t data_size, write(data_array[index]); } +#if !ONEWIRE_OPEN_DRAIN_ONLY if (!power) { DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); } +#endif } // @@ -311,7 +318,9 @@ void OneWire::skip() void OneWire::depower() { +#if !ONEWIRE_OPEN_DRAIN_ONLY DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); +#endif } // diff --git a/src/OneWire.h b/src/OneWire.h index 607d247..aca4ff9 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -16,6 +16,14 @@ #define ONEWIRE_CRC8_TABLE 1 #endif +// to make the bus more secure you can disable parasitic bus powering +// and switch to open drain only. +// NOTE: you need to power the devices yourself, because the PU-resistor +// might not do it completely +#ifndef ONEWIRE_OPEN_DRAIN_ONLY +#define ONEWIRE_OPEN_DRAIN_ONLY 1 +#endif + class OneWire { private: From a46469c60a0748b863729d4d723f7f3f71bba10d Mon Sep 17 00:00:00 2001 From: orgua Date: Mon, 17 Jul 2017 00:40:38 +0200 Subject: [PATCH 34/71] even better implementation of open-drain only! change on runtime if wanted --- src/OneWire.cpp | 27 ++++----------------------- src/OneWire.h | 13 ++++++------- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index f07b7cf..4b6e46f 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -192,7 +192,7 @@ bool OneWire::reset() // Write a bit. Port and bit is used to cut lookup time and provide // more certain timing. // -void OneWire::write_bit(const bool value) +void OneWire::write_bit(const bool value, const bool power) { const uint8_t time_high = uint8_t(value ? 10 : 60); const uint8_t time_low = uint8_t(value ? 55 : 5); @@ -203,9 +203,7 @@ void OneWire::write_bit(const bool value) DIRECT_WRITE_LOW(_baseReg, _bitMask); DIRECT_MODE_OUTPUT(_baseReg, _bitMask); // drive output low delayMicroseconds(time_high); -#if ONEWIRE_OPEN_DRAIN_ONLY - DIRECT_MODE_INPUT(_baseReg, _bitMask); // allow it to float -#endif + if (!power) DIRECT_MODE_INPUT(_baseReg, _bitMask); // allow it to float DIRECT_WRITE_HIGH(_baseReg, _bitMask); // drive output high interrupts(); delayMicroseconds(time_low); @@ -242,34 +240,19 @@ bool OneWire::read_bit() // void OneWire::write(uint8_t value, const bool power) { - for (uint8_t index = 0; index < 8; index++) { - write_bit((value & 0x01) != 0); // shifting value is faster than clean solution with bitmask (~18 byte on arduino) + write_bit((value & 0x01) != 0, power); // shifting value is faster than clean solution with bitmask (~18 byte on arduino) value >>= 1; } - -#if !ONEWIRE_OPEN_DRAIN_ONLY - if (!power) - { - DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); - } -#endif } void OneWire::write_bytes(const uint8_t data_array[], const uint16_t data_size, const bool power) { for (uint16_t index = 0; index < data_size; index++) // not slower than solution without index { - write(data_array[index]); - } - -#if !ONEWIRE_OPEN_DRAIN_ONLY - if (!power) - { - DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); + write(data_array[index], power); } -#endif } // @@ -318,9 +301,7 @@ void OneWire::skip() void OneWire::depower() { -#if !ONEWIRE_OPEN_DRAIN_ONLY DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); -#endif } // diff --git a/src/OneWire.h b/src/OneWire.h index aca4ff9..c82421b 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -17,12 +17,11 @@ #endif // to make the bus more secure you can disable parasitic bus powering -// and switch to open drain only. -// NOTE: you need to power the devices yourself, because the PU-resistor -// might not do it completely -#ifndef ONEWIRE_OPEN_DRAIN_ONLY -#define ONEWIRE_OPEN_DRAIN_ONLY 1 -#endif +// and switch to open drain only, just keep in mind to use the +// send-routines without power=true +// NOTE: you need to power the devices yourself, because the external +// PU-resistor might not do it completely + class OneWire { @@ -75,7 +74,7 @@ class OneWire // Write a bit. The bus is always left powered at the end, see // note in write() about that. - void write_bit(bool value); + void write_bit(bool value, bool power = false); // Read a bit. bool read_bit(); From d2d1527cea5cbea7702b9620bbf39f3f1422e6bd Mon Sep 17 00:00:00 2001 From: orgua Date: Mon, 17 Jul 2017 00:57:41 +0200 Subject: [PATCH 35/71] begin to write readme --- readme.md | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 5dc4da1..54a4d9b 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,37 @@ TODO: fill -## Features -## History +### Features +- cleaner, faster code with c++11 features (requires arduino sw 1.6.x or higher, >=1.6.10 recommended) +- hardware-dependencies are combined in "platform.h" + - supported: arduino zero, teensy, sam3x, pic32, [ATtiny](https://github.com/damellis/attiny), esp8266, nrf51822, raspberry (...) + - tested architectures: atmega328 @ 16 MHz / arduino Uno, teensy3.2 + - for portability and tests the hub can be compiled on a PC with the supplied mock-up functions in platform.h +### History +- better implementation to allow parasitic power to bus, otherwise master will act as open-drain-only! -> this was totally wrong before as the bus was powered for a short amount of time even if powering is disabled +- some µC enable pin-pullups when writing pin=high when pin is in input-mode -> use it, does not harm other +- extend examples with ds2438 and a bus-discovery feature +- clean up examples, easier to understand, more similar to each other +- put hardware dependant code into platform.h +- better naming of variables and a lot of code cleanup +- simplify pin-access and interrupt-handling +- improve onewire class-interface (it was possible to copy the object, ...) +- reduce size of datatypes if possible, more const correctness and more explicit code +- get rid of plain old c and decide on one code style + ## How to use -## Help \ No newline at end of file + +### Help + +### Connecting the master with the bus: + +![Onewire-Schematic](http://wiki.lvl1.org/images/1/15/Onewire.gif) + +[read more](http://wiki.lvl1.org/DS1820_Temp_sensor) + +### Parasite Power with two wires + +![Parasite-Power-Schematic](http://i.stack.imgur.com/0MeGL.jpg) + +**Note:** you will need to activate power-argument on the send-routines or lower the pullup-resistor. + +[read more](http://electronics.stackexchange.com/questions/193300/digital-ic-that-draws-power-from-data-pins) From c1db0fad0ac7e438103b01fecd663d95a4bf488f Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 22 Jul 2017 12:04:09 +0200 Subject: [PATCH 36/71] =?UTF-8?q?cleanup=20compiler-switches=20and=20add?= =?UTF-8?q?=20cleaner=20way=20to=20use=20pull=20ups=20of=20=C2=B5Cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/OneWire.cpp | 15 +++++--- src/OneWire.h | 9 +++-- src/platform.cpp | 4 +-- src/platform.h | 91 ++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 91 insertions(+), 28 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 4b6e46f..48136a7 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -147,7 +147,10 @@ OneWire::OneWire(const uint8_t pin) pinMode(pin, INPUT); pin_bitMask = PIN_TO_BITMASK(pin); pin_baseReg = PIN_TO_BASEREG(pin); - DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // on avr: act as pullup in input-mode + +#if ONEWIRE_USE_PULL_UP + DIRECT_ACTIVATE_PU(pin_baseReg, pin_bitMask); +#endif reset_search(); // really needed? } @@ -167,7 +170,7 @@ bool OneWire::reset() uint8_t retries = 125; DIRECT_MODE_INPUT(_baseReg, _bitMask); - DIRECT_WRITE_HIGH(_baseReg, _bitMask); // on avr: act as pullup in input-mode + // wait until the wire is high... just in case do { @@ -178,13 +181,16 @@ bool OneWire::reset() DIRECT_WRITE_LOW(_baseReg, _bitMask); DIRECT_MODE_OUTPUT(_baseReg, _bitMask); // drive output low delayMicroseconds(480); + noInterrupts(); DIRECT_MODE_INPUT(_baseReg, _bitMask); // allow it to float - DIRECT_WRITE_HIGH(_baseReg, _bitMask); // on avr: act as pullup in input-mode + delayMicroseconds(70); const bool success = !DIRECT_READ(_baseReg, _bitMask); interrupts(); delayMicroseconds(410); + + // TODO: after presence detection we could power the hub if wanted return success; } @@ -223,7 +229,6 @@ bool OneWire::read_bit() DIRECT_MODE_OUTPUT(_baseReg, _bitMask); delayMicroseconds(3); DIRECT_MODE_INPUT(_baseReg, _bitMask); // let pin float, pull up will raise - DIRECT_WRITE_HIGH(_baseReg, _bitMask); // on avr: act as pullup in input-mode delayMicroseconds(10); const bool value = DIRECT_READ(_baseReg, _bitMask); interrupts(); @@ -485,7 +490,7 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) // "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products" // -#if ONEWIRE_CRC8_TABLE +#if ONEWIRE_USE_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 crc_table[] = { diff --git a/src/OneWire.h b/src/OneWire.h index c82421b..c184179 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -12,15 +12,18 @@ // 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 +#define ONEWIRE_USE_CRC8_TABLE 1 // to make the bus more secure you can disable parasitic bus powering // and switch to open drain only, just keep in mind to use the // send-routines without power=true // NOTE: you need to power the devices yourself, because the external // PU-resistor might not do it completely +// TODO + +// use micro controller feature to use internal pull-ups +// NOTE: will throw compiler-error if feature is not yet implemented for your controller +#define ONEWIRE_USE_PULL_UP 0 class OneWire diff --git a/src/platform.cpp b/src/platform.cpp index 6d315c1..5782581 100644 --- a/src/platform.cpp +++ b/src/platform.cpp @@ -1,6 +1,6 @@ #include "platform.h" -#ifdef FALLBACK_BASIC_FNs +#ifdef ONEWIRE_FALLBACK_BASIC_FNs uint32_t micros() {return 0; }; // original arduino-fn takes about 3 µs to process @ 16 MHz @@ -16,7 +16,7 @@ uint8_t pgm_read_byte(const uint8_t * const address) return *address; } -#endif // FALLBACK_BASIC_FNs +#endif // ONEWIRE_FALLBACK_BASIC_FNs diff --git a/src/platform.h b/src/platform.h index 16abbf3..38b7dfa 100644 --- a/src/platform.h +++ b/src/platform.h @@ -12,11 +12,18 @@ #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) #define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) + +#if ONEWIRE_USE_PULL_UP // also set pin high, after changing it to input -> activate PU +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask));((*((base)+2)) |= (mask)) +#else #define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask)) +#endif + #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)) -using io_reg_t = uint8_t; // define special datatype for register-access +#define DIRECT_ACTIVATE_PU(base, mask) DIRECT_MODE_INPUT(base, mask) +using io_reg_t = uint8_t; // define special data type for register-access #elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) #define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) @@ -26,7 +33,12 @@ using io_reg_t = uint8_t; // define special datatype for register-access #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) -using io_reg_t = uint8_t; // define special datatype for register-access +using io_reg_t = uint8_t; // define special data type for register-access + +#if ONEWIRE_USE_PULL_UP +#error "PULL UP feature is not yet implemented for your microcontroller" +#endif +#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here #elif defined(__MKL26Z64__) #define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) @@ -36,7 +48,12 @@ using io_reg_t = uint8_t; // define special datatype for register-access #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)) -using io_reg_t = uint8_t; // define special datatype for register-access +using io_reg_t = uint8_t; // define special data type for register-access + +#if ONEWIRE_USE_PULL_UP +#error "PULL UP feature is not yet implemented for your microcontroller" +#endif +#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here #elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__) // Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due. @@ -56,7 +73,12 @@ using io_reg_t = uint8_t; // define special datatype for register-access #ifndef pgm_read_byte #define pgm_read_byte(addr) (*(const uint8_t *)(addr)) #endif -using io_reg_t = uint32_t; // define special datatype for register-access +using io_reg_t = uint32_t; // define special data type for register-access + +#if ONEWIRE_USE_PULL_UP +#error "PULL UP feature is not yet implemented for your microcontroller" +#endif +#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here #elif defined(__PIC32MX__) #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) @@ -66,7 +88,12 @@ using io_reg_t = uint32_t; // define special datatype for register-access #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 -using io_reg_t = uint32_t; // define special datatype for register-access +using io_reg_t = uint32_t; // define special data type for register-access + +#if ONEWIRE_USE_PULL_UP +#error "PULL UP feature is not yet implemented for your microcontroller" +#endif +#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here #elif defined(ARDUINO_ARCH_ESP8266) // Special note: I depend on the ESP community to maintain these definitions and @@ -81,7 +108,12 @@ using io_reg_t = uint32_t; // define special datatype for register-access #define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS #define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS #define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS -using io_reg_t = uint32_t; // define special datatype for register-access +using io_reg_t = uint32_t; // define special data type for register-access + +#if ONEWIRE_USE_PULL_UP +#error "PULL UP feature is not yet implemented for your microcontroller" +#endif +#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here #elif defined(__SAMD21G18A__) #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) @@ -91,7 +123,12 @@ using io_reg_t = uint32_t; // define special datatype for register-access #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask)) #define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask)) #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask)) -using io_reg_t = uint32_t; // define special datatype for register-access +using io_reg_t = uint32_t; // define special data type for register-access + +#if ONEWIRE_USE_PULL_UP +#error "PULL UP feature is not yet implemented for your microcontroller" +#endif +#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here #elif defined(RBL_NRF51822) #define PIN_TO_BASEREG(pin) (0) @@ -101,7 +138,12 @@ using io_reg_t = uint32_t; // define special datatype for register-access #define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) #define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) #define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) -using io_reg_t = uint32_t; // define special datatype for register-access +using io_reg_t = uint32_t; // define special data type for register-access + +#if ONEWIRE_USE_PULL_UP +#error "PULL UP feature is not yet implemented for your microcontroller" +#endif +#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here #elif defined(__arc__) /* Arduino101/Genuino101 specifics */ @@ -120,7 +162,12 @@ using io_reg_t = uint32_t; // define special datatype for register-access /* GPIO registers base address */ #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) #define PIN_TO_BITMASK(pin) pin -using io_reg_t = uint32_t; // define special datatype for register-access +using io_reg_t = uint32_t; // define special data type for register-access + +#if ONEWIRE_USE_PULL_UP +#error "PULL UP feature is not yet implemented for your microcontroller" +#endif +#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here static inline __attribute__((always_inline)) IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) @@ -193,10 +240,25 @@ void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) #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) -using io_reg_t = uint32_t; // define special datatype for register-access +using io_reg_t = uint32_t; // define special data type for register-access + +#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here #warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture." +#endif + +#ifndef ARDUINO +#define ONEWIRE_FALLBACK_BASIC_FNs +#define ONEWIRE_FALLBACK_ADDITIONAL_FNs // to load up Serial below +#endif + +#ifdef ARDUINO_attiny +#define ONEWIRE_FALLBACK_ADDITIONAL_FNs // to load up Serial below +#endif + +#ifdef ONEWIRE_FALLBACK_BASIC_FNs + /////////////////////////////////////////// EXTRA PART ///////////////////////////////////////// // this part is loaded if no proper arduino-environment is found (good for external testing) // these used functions are mockups @@ -207,9 +269,6 @@ using io_reg_t = uint32_t; // define special datatype for register-access #define HIGH 1 #define LOW 0 -#define FALLBACK_BASIC_FNs -#define FALLBACK_ADDITIONAL_FNs // to load up Serial below - static bool mockup_pin_value[256]; template @@ -257,12 +316,8 @@ void interrupts(); #endif -#if defined(ARDUINO_attiny) -#define FALLBACK_ADDITIONAL_FNs // to load up Serial below -#endif - -#ifdef FALLBACK_ADDITIONAL_FNs // Test to make it work on aTtiny85, 8MHz +#ifdef ONEWIRE_FALLBACK_ADDITIONAL_FNs // Test to make it work on aTtiny85, 8MHz /// README: use pin2 or pin3 for Attiny, source: https://github.com/gioblu/PJON/wiki/ATtiny-interfacing #ifndef BIN From cb3921c85a7757d5ef0284c377cbe2d080d5d3dc Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 22 Jul 2017 12:07:04 +0200 Subject: [PATCH 37/71] remove compilerwarnings --- src/OneWire.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 48136a7..bb1467a 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -320,7 +320,7 @@ void OneWire::reset_search() search_last_device_flag = false; search_last_family_discrepancy = 0; - memset(search_rom_array, 0, 8); + memset(search_rom_array, uint8_t(0), 8); } // Setup the search to find the device type 'family_code' on the next call @@ -330,7 +330,7 @@ void OneWire::target_search(const uint8_t family_code) { // set the search state to find SearchFamily type devices search_rom_array[0] = family_code; - memset(&search_rom_array[1], 0, 7); + memset(&search_rom_array[1], uint8_t(0), 7); search_last_discrepancy = 64; search_last_family_discrepancy = 0; search_last_device_flag = false; From 06ec89bae9569690c209bf71bdb136c651961b62 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 22 Jul 2017 12:19:51 +0200 Subject: [PATCH 38/71] add more universal way to allow pullups --- src/OneWire.cpp | 10 ++++++---- src/platform.h | 35 +++++++---------------------------- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index bb1467a..d870af7 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -144,14 +144,16 @@ sample code bearing this copyright. OneWire::OneWire(const uint8_t pin) { // prepare pin - pinMode(pin, INPUT); - pin_bitMask = PIN_TO_BITMASK(pin); - pin_baseReg = PIN_TO_BASEREG(pin); #if ONEWIRE_USE_PULL_UP - DIRECT_ACTIVATE_PU(pin_baseReg, pin_bitMask); + pinMode(pin, INPUT_PULLUP); +#else + pinMode(pin, INPUT); #endif + pin_bitMask = PIN_TO_BITMASK(pin); + pin_baseReg = PIN_TO_BASEREG(pin); + reset_search(); // really needed? } diff --git a/src/platform.h b/src/platform.h index 38b7dfa..a0d9b4e 100644 --- a/src/platform.h +++ b/src/platform.h @@ -22,7 +22,6 @@ #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)) -#define DIRECT_ACTIVATE_PU(base, mask) DIRECT_MODE_INPUT(base, mask) using io_reg_t = uint8_t; // define special data type for register-access #elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) @@ -35,11 +34,6 @@ using io_reg_t = uint8_t; // define special data type for register-access #define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1) using io_reg_t = uint8_t; // define special data type for register-access -#if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented for your microcontroller" -#endif -#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here - #elif defined(__MKL26Z64__) #define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) @@ -50,11 +44,6 @@ using io_reg_t = uint8_t; // define special data type for register-access #define DIRECT_WRITE_HIGH(base, mask) (*((base)+4) = (mask)) using io_reg_t = uint8_t; // define special data type for register-access -#if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented for your microcontroller" -#endif -#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here - #elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__) // Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due. // http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268 @@ -76,9 +65,8 @@ using io_reg_t = uint8_t; // define special data type for register-access using io_reg_t = uint32_t; // define special data type for register-access #if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented for your microcontroller" +#error "PULL UP feature is not yet implemented or tested for your microcontroller" #endif -#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here #elif defined(__PIC32MX__) #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) @@ -91,9 +79,8 @@ using io_reg_t = uint32_t; // define special data type for register-access using io_reg_t = uint32_t; // define special data type for register-access #if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented for your microcontroller" +#error "PULL UP feature is not yet implemented or tested for your microcontroller" #endif -#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here #elif defined(ARDUINO_ARCH_ESP8266) // Special note: I depend on the ESP community to maintain these definitions and @@ -110,11 +97,6 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS using io_reg_t = uint32_t; // define special data type for register-access -#if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented for your microcontroller" -#endif -#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here - #elif defined(__SAMD21G18A__) #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) @@ -126,9 +108,8 @@ using io_reg_t = uint32_t; // define special data type for register-access using io_reg_t = uint32_t; // define special data type for register-access #if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented for your microcontroller" +#error "PULL UP feature is not yet implemented or tested for your microcontroller" #endif -#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here #elif defined(RBL_NRF51822) #define PIN_TO_BASEREG(pin) (0) @@ -141,9 +122,9 @@ using io_reg_t = uint32_t; // define special data type for register-access using io_reg_t = uint32_t; // define special data type for register-access #if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented for your microcontroller" +#error "PULL UP feature is not yet implemented or tested for your microcontroller" #endif -#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here + #elif defined(__arc__) /* Arduino101/Genuino101 specifics */ @@ -165,9 +146,8 @@ using io_reg_t = uint32_t; // define special data type for register-access using io_reg_t = uint32_t; // define special data type for register-access #if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented for your microcontroller" +#error "PULL UP feature is not yet implemented or tested for your microcontroller" #endif -#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here static inline __attribute__((always_inline)) IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) @@ -242,8 +222,6 @@ void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) #define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT) using io_reg_t = uint32_t; // define special data type for register-access -#define DIRECT_ACTIVATE_PU(base, mask) // note: add function for PU activation here - #warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture." #endif @@ -265,6 +243,7 @@ using io_reg_t = uint32_t; // define special data type for register-access //////////////////////////////////////////////////////////////////////////////////////////////// #define INPUT 1 +#define INPUT_PULLUP 1 #define OUTPUT 0 #define HIGH 1 #define LOW 0 From 07cf55ffe2c8687584c493fa94536862d51bdda6 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 22 Jul 2017 14:05:20 +0200 Subject: [PATCH 39/71] test lib with different compilers --- library.properties | 2 +- readme.md | 23 ++++++++++++++++++++++- src/OneWire.cpp | 3 ++- src/OneWire.h | 5 ++++- src/platform.cpp | 5 ----- src/platform.h | 46 +++++++++++++++++++++++----------------------- 6 files changed, 52 insertions(+), 32 deletions(-) diff --git a/library.properties b/library.properties index dfcc56b..810ff38 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=OneWire -version=2.4.0 +version=3.0.0 author=Jim Studt, Tom Pollard, Robin James, Glenn Trewitt, Jason Dangel, Guillermo Lovato, Paul Stoffregen, Scott Roberts, Bertrik Sikken, Mark Tillotson, Ken Butcher, Roger Clark, Love Nystrom maintainer=Paul Stoffregen sentence=Access 1-wire temperature sensors, memory and other chips. diff --git a/readme.md b/readme.md index 54a4d9b..fe45a40 100644 --- a/readme.md +++ b/readme.md @@ -1,12 +1,17 @@ TODO: fill ### Features -- cleaner, faster code with c++11 features (requires arduino sw 1.6.x or higher, >=1.6.10 recommended) +- cleaner, smaller, faster code with c++11 features (requires arduino sw 1.6.x or higher, >=1.6.10 recommended) - hardware-dependencies are combined in "platform.h" - supported: arduino zero, teensy, sam3x, pic32, [ATtiny](https://github.com/damellis/attiny), esp8266, nrf51822, raspberry (...) - tested architectures: atmega328 @ 16 MHz / arduino Uno, teensy3.2 - for portability and tests the hub can be compiled on a PC with the supplied mock-up functions in platform.h +- use command to enable or disable parasitic bus powering by the master (was implemented wrong before) +- use compiler-switch to use pull-ups of micro controller + ### History +- allow to use pull-up if micro controller has support for it -> most support INPUT_PULLUP used on pinMode(), others will get an errormessage if feature is enables +- clean up compiler-switches - better implementation to allow parasitic power to bus, otherwise master will act as open-drain-only! -> this was totally wrong before as the bus was powered for a short amount of time even if powering is disabled - some µC enable pin-pullups when writing pin=high when pin is in input-mode -> use it, does not harm other - extend examples with ds2438 and a bus-discovery feature @@ -18,6 +23,22 @@ TODO: fill - reduce size of datatypes if possible, more const correctness and more explicit code - get rid of plain old c and decide on one code style +### tested hardware +- run as master: + - Arduino Uno (Arduino AVR Boards) + - Teensy 3.2 (teensyduino) +- compile process: + - Teensy LC (teensyduino) + - Teensy 3.6 (teensyduino) + - Arduino Zero native USB (Arduino SAMD Boards (32-bits ARM Cortex-M0+)) + - Arduino Primo (Arduino nRF52 Boards) + - Arduino Due native USB (Arduino SAMD Boards (32-bits ARM Cortex-M3)) + - ATtiny 84 (attiny) + - generic ESP8266 (esp8266) + +### todo +- https://github.com/sandeepmistry/arduino-nRF5 + ## How to use ### Help diff --git a/src/OneWire.cpp b/src/OneWire.cpp index d870af7..4852a5a 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -526,7 +526,8 @@ uint8_t OneWire::crc8(const uint8_t data_array[], uint8_t data_size, const uint8 for (uint8_t index = 0; index < data_size; ++index) { - crc = pgm_read_byte(crc_table + (crc ^ data_array[index])); + const uint8_t* address = crc_table + (crc ^ data_array[index]); + crc = pgm_read_byte(address); } return crc; } diff --git a/src/OneWire.h b/src/OneWire.h index c184179..778314d 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -46,10 +46,13 @@ class OneWire ~OneWire() = default; // nothing special to do here OneWire(const OneWire &ow) = delete; // disallow copy constructor - OneWire(OneWire &&ow) = default; // default move constructor OneWire &operator=(OneWire &ow) = delete; // disallow copy assignment OneWire &operator=(const OneWire &ow) = delete; // disallow copy assignment + +#if (ONEWIRE_GCC_VERSION > 40900) + OneWire(OneWire &&ow) = default; // default move constructor OneWire &operator=(OneWire &&ow) = delete; // disallow move assignment +#endif // 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 diff --git a/src/platform.cpp b/src/platform.cpp index 5782581..1949fc4 100644 --- a/src/platform.cpp +++ b/src/platform.cpp @@ -11,11 +11,6 @@ void noInterrupts() { }; void interrupts() { }; -uint8_t pgm_read_byte(const uint8_t * const address) -{ - return *address; -} - #endif // ONEWIRE_FALLBACK_BASIC_FNs diff --git a/src/platform.h b/src/platform.h index a0d9b4e..2915fc7 100644 --- a/src/platform.h +++ b/src/platform.h @@ -3,11 +3,18 @@ #ifndef ONEWIRE_PLATFORM_H #define ONEWIRE_PLATFORM_H +// determine gcc version, will produce number like 40803 for gcc 4.8.3 +#if defined(__GNUC__) +#define ONEWIRE_GCC_VERSION ( (__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__) +#else +#define ONEWIRE_GCC_VERSION 0 +#endif + #if defined(ARDUINO) && ARDUINO >= 100 #include #endif -#if defined(__AVR__) +#if defined(__AVR__) /* arduino (all with atmega, atiny) */ #include #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) @@ -24,7 +31,7 @@ #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask)) using io_reg_t = uint8_t; // define special data type for register-access -#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) +#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) /* teensy 3.2 to 3.6 */ #define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) #define PIN_TO_BITMASK(pin) (1) #define DIRECT_READ(base, mask) (*((base)+512)) @@ -34,7 +41,7 @@ using io_reg_t = uint8_t; // define special data type for register-access #define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1) using io_reg_t = uint8_t; // define special data type for register-access -#elif defined(__MKL26Z64__) +#elif defined(__MKL26Z64__) /* teensy LC */ #define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) #define DIRECT_READ(base, mask) ((*((base)+16) & (mask)) ? 1 : 0) @@ -44,7 +51,7 @@ using io_reg_t = uint8_t; // define special data type for register-access #define DIRECT_WRITE_HIGH(base, mask) (*((base)+4) = (mask)) using io_reg_t = uint8_t; // define special data type for register-access -#elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__) +#elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__) /* arduino due */ // 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 @@ -64,10 +71,6 @@ using io_reg_t = uint8_t; // define special data type for register-access #endif using io_reg_t = uint32_t; // define special data type for register-access -#if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented or tested for your microcontroller" -#endif - #elif defined(__PIC32MX__) #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) @@ -82,7 +85,7 @@ using io_reg_t = uint32_t; // define special data type for register-access #error "PULL UP feature is not yet implemented or tested for your microcontroller" #endif -#elif defined(ARDUINO_ARCH_ESP8266) +#elif defined(ARDUINO_ARCH_ESP8266) /* nodeMCU, ESPduino, ... */ // Special note: I depend on the ESP community to maintain these definitions and // submit good pull requests. I can not answer any ESP questions or help you // resolve any problems related to ESP chips. Please do not contact me and please @@ -97,7 +100,7 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS using io_reg_t = uint32_t; // define special data type for register-access -#elif defined(__SAMD21G18A__) +#elif defined(__SAMD21G18A__) /* arduino zero */ #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) #define DIRECT_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0) @@ -107,11 +110,7 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask)) using io_reg_t = uint32_t; // define special data type for register-access -#if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented or tested for your microcontroller" -#endif - -#elif defined(RBL_NRF51822) +#elif defined(RBL_NRF51822) || defined(NRF5) /* arduino primo, red bear blend, should be good for all nrf5x chips */ #define PIN_TO_BASEREG(pin) (0) #define PIN_TO_BITMASK(pin) (pin) #define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin) @@ -119,12 +118,9 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) #define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) #define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) -using io_reg_t = uint32_t; // define special data type for register-access - -#if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented or tested for your microcontroller" -#endif +#define io_reg_t uint32_t /* the tool chain is old .... */ +// using io_reg_t = uint32_t; // define special data type for register-access #elif defined(__arc__) /* Arduino101/Genuino101 specifics */ @@ -293,6 +289,12 @@ void noInterrupts(); void interrupts(); +template +T1 pgm_read_byte(const T1 *address) +{ + return *address; +} + #endif @@ -312,7 +314,7 @@ static class serial { private: - static uint32_t speed; + uint32_t speed; public: @@ -376,8 +378,6 @@ void wdt_enable(...); #define PROGMEM #endif -uint8_t pgm_read_byte(const uint8_t *address); - #endif #endif //ONEWIRE_PLATFORM_H From 4ee1c640c5f11843d0b75c892bb7e12d79fba9ac Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 22 Jul 2017 15:15:18 +0200 Subject: [PATCH 40/71] big documentation update --- readme.md | 133 ++++++++++++++++++++++++++++++++++++++---------- src/OneWire.cpp | 57 +++------------------ src/OneWire.h | 17 ++++--- 3 files changed, 123 insertions(+), 84 deletions(-) diff --git a/readme.md b/readme.md index fe45a40..b5ee36e 100644 --- a/readme.md +++ b/readme.md @@ -1,16 +1,58 @@ -TODO: fill +OneWire Master Library +====================== -### Features +### Features compared to lib 2.3.3 - cleaner, smaller, faster code with c++11 features (requires arduino sw 1.6.x or higher, >=1.6.10 recommended) +- proper naming of variables make it easier to read the code - hardware-dependencies are combined in "platform.h" - supported: arduino zero, teensy, sam3x, pic32, [ATtiny](https://github.com/damellis/attiny), esp8266, nrf51822, raspberry (...) - - tested architectures: atmega328 @ 16 MHz / arduino Uno, teensy3.2 - for portability and tests the hub can be compiled on a PC with the supplied mock-up functions in platform.h +- more and better understandable examples - use command to enable or disable parasitic bus powering by the master (was implemented wrong before) - use compiler-switch to use pull-ups of micro controller +- improved documentation -### History -- allow to use pull-up if micro controller has support for it -> most support INPUT_PULLUP used on pinMode(), others will get an errormessage if feature is enables +### Tested hardware +- run as ds18x20-master: + - Arduino Uno (Arduino AVR Boards) + - Teensy 3.2 (teensyduino) +- compile process passes: + - Teensy LC (teensyduino) + - Teensy 3.6 (teensyduino) + - Arduino Zero native USB (Arduino SAMD Boards (32-bits ARM Cortex-M0+)) + - Arduino Primo (Arduino nRF52 Boards) + - Arduino Due native USB (Arduino SAMD Boards (32-bits ARM Cortex-M3)) + - ATtiny 84 (attiny) + - generic ESP8266 (esp8266) + +### How to use +- understanding [onewire protocol](https://en.wikipedia.org/wiki/1-Wire) help a lot +- connect your microcontroller (onewire-master) with the onewire-bus +- the onewire-bus should be set up as described below +- the examples are mostly self explanatory + - the microcontroller acts as a master + - it will search and select devices on the bus + - master will read data from devices + - data is processed and send via uart to the pc +- onewire.h has the following compiler-switches to enable special features + - USE_CRC8_TABLE: will trade compilation size for speed + - USE_PULL_UP: activate internal pull up of microcontroller (external bus PU most likely not needed then) + +### HELP - What to do if things don't work as expected? +- check if your arduino software up to date (>v1.6.8) +- update this lib to the latest release (v3.0.0) +- check if clock-speed of the µC is set correctly (if possible) - test with simple blink example, 1sec ON should really need 1sec. timing is critical +- begin with a simple example like the ds18x20 (if possible) or the bus_discovery example +- check if your setup is right: see sections below +- is there more than one master on the bus? It won't work! +- has any other master ever worked with the slave device? +- if you can provide a recording via logic-analyzer (logic 8 or similar) there should be chance we can help you +- if you checked all these points feel free to open an issue at [Github](https://github.com/orgua/OneWire) and describe your troubleshooting process + - please provide the following basic info: which µC do you use, software versions, what device do you try to work with, what works, what doesn't + +### Development history +Version 3.0 +- allow to use internal pull-up if micro controller has support for it -> most support INPUT_PULLUP used on pinMode(), others will get an errormessage if feature is enables - clean up compiler-switches - better implementation to allow parasitic power to bus, otherwise master will act as open-drain-only! -> this was totally wrong before as the bus was powered for a short amount of time even if powering is disabled - some µC enable pin-pullups when writing pin=high when pin is in input-mode -> use it, does not harm other @@ -20,39 +62,78 @@ TODO: fill - better naming of variables and a lot of code cleanup - simplify pin-access and interrupt-handling - improve onewire class-interface (it was possible to copy the object, ...) -- reduce size of datatypes if possible, more const correctness and more explicit code +- reduce size of data types if possible, more const correctness and more explicit code - get rid of plain old c and decide on one code style -### tested hardware -- run as master: - - Arduino Uno (Arduino AVR Boards) - - Teensy 3.2 (teensyduino) -- compile process: - - Teensy LC (teensyduino) - - Teensy 3.6 (teensyduino) - - Arduino Zero native USB (Arduino SAMD Boards (32-bits ARM Cortex-M0+)) - - Arduino Primo (Arduino nRF52 Boards) - - Arduino Due native USB (Arduino SAMD Boards (32-bits ARM Cortex-M3)) - - ATtiny 84 (attiny) - - generic ESP8266 (esp8266) - -### todo -- https://github.com/sandeepmistry/arduino-nRF5 +Version 2.3 +- Unknown chip fallback mode, Roger Clark +- Teensy-LC compatibility, Paul Stoffregen +- Search bug fix, Love Nystrom -## How to use +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 -### Help +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 + +Version before 2.0 +- Modified to work with larger numbers of devices - avoids loop. Tested in Arduino 11 alpha with 12 sensors. 26 Sept 2008 -- Robin James +- 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 + +### Plans for the future +- update documentation, maybe doxygen +- update the interface, NOTE: these changes can be made without breaking old code, just use "deprecated" on old interface + - use overloaded functions read and write_bytes(), write(), write_bit() become just write() + - simplify bus powering as extra argument for reset and read as well, OR we set it on bus constructor + - predefine standard onewire commands and use send(), skip() is not needed then +- unit tests +- CI ### Connecting the master with the bus: +- the shown picture does not show the power line (assumes parasitic powering) +- by activating the internal pull up of the microcontroller you most likely do not need external PU + ![Onewire-Schematic](http://wiki.lvl1.org/images/1/15/Onewire.gif) [read more](http://wiki.lvl1.org/DS1820_Temp_sensor) -### Parasite Power with two wires +### Parasitic Power for the bus slave devices -![Parasite-Power-Schematic](http://i.stack.imgur.com/0MeGL.jpg) +advantages: +- reduce number of wires to the slave device + +how to do it: +- you will need to activate power-argument on the send-routines or use power()-function of the master +- for low power devices like the ds2401 it should be possible to just lower the resistance of the pullup-resistor of the bus and +- most slave devices have the circuit shown in the lower picture already included (consult datasheet) -**Note:** you will need to activate power-argument on the send-routines or lower the pullup-resistor. +![Parasite-Power-Schematic](http://i.stack.imgur.com/0MeGL.jpg) [read more](http://electronics.stackexchange.com/questions/193300/digital-ic-that-draws-power-from-data-pins) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 4852a5a..72dfc44 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -32,57 +32,6 @@ private email about OneWire). OneWire is now very mature code. No changes other than adding definitions for newer hardware support are anticipated. -Version 2.3: - Unknown 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 @@ -306,6 +255,12 @@ void OneWire::skip() write(0xCC); // Skip ROM } +void OneWire::power() +{ + DIRECT_MODE_OUTPUT(pin_baseReg, pin_bitMask); // drive output low + DIRECT_WRITE_HIGH(pin_baseReg, pin_bitMask); // drive output high +} + void OneWire::depower() { DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); diff --git a/src/OneWire.h b/src/OneWire.h index 778314d..1ff587a 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -14,17 +14,15 @@ // but very compact algorithm is used. #define ONEWIRE_USE_CRC8_TABLE 1 +// micro controller feature to use internal pull-ups +// NOTE: will throw compiler-error if feature is not yet implemented or tested for your controller +#define ONEWIRE_USE_PULL_UP 0 + // to make the bus more secure you can disable parasitic bus powering // and switch to open drain only, just keep in mind to use the // send-routines without power=true // NOTE: you need to power the devices yourself, because the external // PU-resistor might not do it completely -// TODO - -// use micro controller feature to use internal pull-ups -// NOTE: will throw compiler-error if feature is not yet implemented for your controller -#define ONEWIRE_USE_PULL_UP 0 - class OneWire { @@ -49,7 +47,7 @@ class OneWire OneWire &operator=(OneWire &ow) = delete; // disallow copy assignment OneWire &operator=(const OneWire &ow) = delete; // disallow copy assignment -#if (ONEWIRE_GCC_VERSION > 40900) +#if (ONEWIRE_GCC_VERSION > 40900) // is needed because of old tool chain for arduino primo OneWire(OneWire &&ow) = default; // default move constructor OneWire &operator=(OneWire &&ow) = delete; // disallow move assignment #endif @@ -85,6 +83,11 @@ class OneWire // Read a bit. bool read_bit(); + // activate parasitic bus powering by master + // write_bytes() , write() and write_bit() have these features already build in, + // but reset() and read() do not --> use this after issuing + void power(); + // 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 From d8f9680bbbf1e4eff56eb9dd35358f5bbe9a8ca8 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 22 Jul 2017 19:33:37 +0200 Subject: [PATCH 41/71] documentation, doxygen, and proper defined code style --- readme.md | 45 ++++++----- src/OneWire.cpp | 127 ++++++++---------------------- src/OneWire.h | 197 +++++++++++++++++++++++++++-------------------- src/platform.cpp | 2 +- src/platform.h | 69 ++++++----------- 5 files changed, 199 insertions(+), 241 deletions(-) diff --git a/readme.md b/readme.md index b5ee36e..0853aec 100644 --- a/readme.md +++ b/readme.md @@ -1,18 +1,21 @@ OneWire Master Library ====================== -### Features compared to lib 2.3.3 -- cleaner, smaller, faster code with c++11 features (requires arduino sw 1.6.x or higher, >=1.6.10 recommended) +### Features +- emulate a bus master in a generic way, the lib is handling most of the timing-constrains and the hardware-access +- clean, small, fast code with c++11 features (requires arduino sw 1.6.x or higher, >=1.6.10 recommended) - proper naming of variables make it easier to read the code - hardware-dependencies are combined in "platform.h" - supported: arduino zero, teensy, sam3x, pic32, [ATtiny](https://github.com/damellis/attiny), esp8266, nrf51822, raspberry (...) - for portability and tests the hub can be compiled on a PC with the supplied mock-up functions in platform.h -- more and better understandable examples -- use command to enable or disable parasitic bus powering by the master (was implemented wrong before) -- use compiler-switch to use pull-ups of micro controller -- improved documentation +- extended and better understandable examples +- command to enable or disable parasitic bus powering by the master (was implemented wrong before) +- compiler-switch to use pull-ups of micro controller +- improved documentation in doxygen style +- decided code style - Allman braces - was closest to main style in original ### Tested hardware +- tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets - run as ds18x20-master: - Arduino Uno (Arduino AVR Boards) - Teensy 3.2 (teensyduino) @@ -50,12 +53,22 @@ OneWire Master Library - if you checked all these points feel free to open an issue at [Github](https://github.com/orgua/OneWire) and describe your troubleshooting process - please provide the following basic info: which µC do you use, software versions, what device do you try to work with, what works, what doesn't +### Plans for the future + +- update the interface, NOTE: these changes can be made without breaking old code, just use "deprecated" on old interface + - use overloaded functions read and write_bytes(), write(), write_bit() become just write() + - simplify bus powering as extra argument for reset and read as well, OR we set it on bus constructor + - predefine standard onewire commands and use send(), skip() is not needed then +- another hardware abstraction to allow proper esp-use +- unit tests +- CI + ### Development history Version 3.0 -- allow to use internal pull-up if micro controller has support for it -> most support INPUT_PULLUP used on pinMode(), others will get an errormessage if feature is enables +- update documentation, doxygen style +- allow to use internal pull-up if micro controller has support for it -> most µC support INPUT_PULLUP in combination with pinMode(), others will get an errormessage if feature is enabled but not supported / tested - clean up compiler-switches - better implementation to allow parasitic power to bus, otherwise master will act as open-drain-only! -> this was totally wrong before as the bus was powered for a short amount of time even if powering is disabled -- some µC enable pin-pullups when writing pin=high when pin is in input-mode -> use it, does not harm other - extend examples with ds2438 and a bus-discovery feature - clean up examples, easier to understand, more similar to each other - put hardware dependant code into platform.h @@ -63,7 +76,8 @@ Version 3.0 - simplify pin-access and interrupt-handling - improve onewire class-interface (it was possible to copy the object, ...) - reduce size of data types if possible, more const correctness and more explicit code -- get rid of plain old c and decide on one code style +- get rid of most ancient c and cpp code and reduce language mix +- decide on one code style - Allman braces - was closest to main style in original Version 2.3 - Unknown chip fallback mode, Roger Clark @@ -106,15 +120,6 @@ Version before 2.0 - 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 -### Plans for the future -- update documentation, maybe doxygen -- update the interface, NOTE: these changes can be made without breaking old code, just use "deprecated" on old interface - - use overloaded functions read and write_bytes(), write(), write_bit() become just write() - - simplify bus powering as extra argument for reset and read as well, OR we set it on bus constructor - - predefine standard onewire commands and use send(), skip() is not needed then -- unit tests -- CI - ### Connecting the master with the bus: - the shown picture does not show the power line (assumes parasitic powering) @@ -131,8 +136,10 @@ advantages: how to do it: - you will need to activate power-argument on the send-routines or use power()-function of the master -- for low power devices like the ds2401 it should be possible to just lower the resistance of the pullup-resistor of the bus and +- for low power devices like the ds2401 it should be possible to just lower the resistance of the pullup-resistor of the bus (~1k) - most slave devices have the circuit shown in the lower picture already included (consult datasheet) +- note: you should only activate / use this feature if you know what you are doing +- possible safety-precaution: do not directly connect the master-pin to the bus, instead add ~200 Ohm between -> in case of shortage or any other bus failure there will only be ~16mA of current flowing ![Parasite-Power-Schematic](http://i.stack.imgur.com/0MeGL.jpg) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 72dfc44..90b1ff2 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -2,7 +2,7 @@ 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 + https://github.com/PaulStoffregen/OneWire OneWire has been maintained by Paul Stoffregen (paul@pjrc.com) since January 2010. @@ -106,17 +106,10 @@ OneWire::OneWire(const uint8_t pin) reset_search(); // really needed? } - -// 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. -// bool OneWire::reset() { - const io_reg_t _bitMask=pin_bitMask; // local copies save pgm-space when called >1 - volatile io_reg_t *_baseReg = pin_baseReg; + const io_reg_t _bitMask = pin_bitMask; // local copies save pgm-space when called >1 + volatile io_reg_t* _baseReg = pin_baseReg; uint8_t retries = 125; @@ -125,9 +118,10 @@ bool OneWire::reset() // wait until the wire is high... just in case do { - if (--retries == 0) return false; + if (--retries==0) return false; delayMicroseconds(2); - } while (!DIRECT_READ(_baseReg, _bitMask)); + } + while (!DIRECT_READ(_baseReg, _bitMask)); DIRECT_WRITE_LOW(_baseReg, _bitMask); DIRECT_MODE_OUTPUT(_baseReg, _bitMask); // drive output low @@ -145,16 +139,12 @@ bool OneWire::reset() return success; } -// -// Write a bit. Port and bit is used to cut lookup time and provide -// more certain timing. -// void OneWire::write_bit(const bool value, const bool power) { const uint8_t time_high = uint8_t(value ? 10 : 60); - const uint8_t time_low = uint8_t(value ? 55 : 5); - const io_reg_t _bitMask=pin_bitMask; // local copies save pgm-space when called >1 - volatile io_reg_t *_baseReg = pin_baseReg; + const uint8_t time_low = uint8_t(value ? 55 : 5); + const io_reg_t _bitMask = pin_bitMask; // local copies save pgm-space when called >1 + volatile io_reg_t* _baseReg = pin_baseReg; noInterrupts(); DIRECT_WRITE_LOW(_baseReg, _bitMask); @@ -166,14 +156,10 @@ void OneWire::write_bit(const bool value, const bool power) delayMicroseconds(time_low); } -// -// Read a bit. Port and bit is used to cut lookup time and provide -// more certain timing. -// bool OneWire::read_bit() { - const io_reg_t _bitMask=pin_bitMask; // local copies save pgm-space when called >1 - volatile io_reg_t *_baseReg = pin_baseReg; + const io_reg_t _bitMask = pin_bitMask; // local copies save pgm-space when called >1 + volatile io_reg_t* _baseReg = pin_baseReg; noInterrupts(); DIRECT_WRITE_LOW(_baseReg, _bitMask); @@ -187,38 +173,28 @@ bool OneWire::read_bit() return value; } -// -// 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 value, const bool power) { - for (uint8_t index = 0; index < 8; index++) + for (uint8_t index = 0; index<8; index++) { - write_bit((value & 0x01) != 0, power); // shifting value is faster than clean solution with bitmask (~18 byte on arduino) + write_bit((value & 0x01)!=0, power); // shifting value is faster than clean solution with bitmask (~18 byte on arduino) value >>= 1; } } void OneWire::write_bytes(const uint8_t data_array[], const uint16_t data_size, const bool power) { - for (uint16_t index = 0; index < data_size; index++) // not slower than solution without index + for (uint16_t index = 0; index 0) + while (rom_size-->0) { write(*rom_array++); } } -// -// Do a ROM skip -// void OneWire::skip() { write(0xCC); // Skip ROM @@ -266,10 +236,6 @@ void OneWire::depower() DIRECT_MODE_INPUT(pin_baseReg, pin_bitMask); } -// -// 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 @@ -280,9 +246,6 @@ void OneWire::reset_search() memset(search_rom_array, uint8_t(0), 8); } -// 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(const uint8_t family_code) { // set the search state to find SearchFamily type devices @@ -293,22 +256,6 @@ void OneWire::target_search(const uint8_t family_code) search_last_device_flag = 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 search_rom_array buffer -// FALSE : device not found, end of search -// bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) { uint8_t id_bit_number = 1; @@ -358,7 +305,7 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) { bool search_direction; // all devices coupled have 0 or 1 - if (id_bit != cmp_id_bit) + if (id_bit!=cmp_id_bit) { search_direction = id_bit; // bit write value for search } @@ -366,14 +313,14 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) { // if this discrepancy if before the Last Discrepancy // on a previous next then pick the same as last time - if (id_bit_number < search_last_discrepancy) + if (id_bit_number 0); + search_direction = ((search_rom_array[rom_byte_number] & rom_byte_mask)>0); } else { // if equal to last pick 1, if not then pick 0 - search_direction = (id_bit_number == search_last_discrepancy); + search_direction = (id_bit_number==search_last_discrepancy); } // if 0 was picked then record its position in LastZero if (!search_direction) @@ -381,7 +328,7 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) last_zero = id_bit_number; // check for Last discrepancy in family - if (last_zero < 9) search_last_family_discrepancy = last_zero; + if (last_zero<9) search_last_family_discrepancy = last_zero; } } @@ -404,22 +351,23 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) 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) + 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 + } + while (rom_byte_number<8); // loop until through all ROM bytes 0-7 // if the search was successful then - if (id_bit_number >= 65) + if (id_bit_number>=65) { // search successful so set search_last_discrepancy,search_last_device_flag,search_result search_last_discrepancy = last_zero; // check for last device - if (search_last_discrepancy == 0) + if (search_last_discrepancy==0) search_last_device_flag = true; search_result = true; @@ -427,7 +375,7 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) } // if no device found then reset counters so next 'search' will be like a first - if (!search_result || (search_rom_array[0] == 0)) + if (!search_result || (search_rom_array[0]==0)) { search_last_discrepancy = 0; search_last_device_flag = false; @@ -468,20 +416,13 @@ static const uint8_t PROGMEM crc_table[] = { 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 data_array[], uint8_t data_size, const uint8_t crc_init) { uint8_t crc = crc_init; - for (uint8_t index = 0; index < data_size; ++index) + for (uint8_t index = 0; index> 8) == inverted_crc[1]; + return (crc & 0xFF)==inverted_crc[0] && (crc >> 8)==inverted_crc[1]; } uint16_t OneWire::crc16(const uint8_t data_array[], uint16_t data_size, const uint16_t crc_init) @@ -534,7 +475,7 @@ uint16_t OneWire::crc16(const uint8_t data_array[], uint16_t data_size, const ui static const uint8_t oddParity[16] = {0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0}; - while (data_size-- > 0) // clean solution with for(index) seems to be 6 byte larger + while (data_size-->0) // clean solution with for(index) seems to be 6 byte larger { // Even though we're just copying a byte from the data_array, // we'll be doing 16-bit computation with it. @@ -542,7 +483,7 @@ uint16_t OneWire::crc16(const uint8_t data_array[], uint16_t data_size, const ui cdata = (cdata ^ crc) & static_cast(0xff); crc >>= 8; - if ((oddParity[cdata & 0x0F] ^ oddParity[cdata >> 4]) != 0) + if ((oddParity[cdata & 0x0F] ^ oddParity[cdata >> 4])!=0) { crc ^= 0xC001; } diff --git a/src/OneWire.h b/src/OneWire.h index 1ff587a..4449d86 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -1,7 +1,7 @@ #ifndef OneWire_h #define OneWire_h -#if defined(ARDUINO) && ARDUINO >= 100 +#if defined(ARDUINO) && ARDUINO>=100 #include // for delayMicroseconds, digitalPinToBitMask, etc #endif @@ -24,14 +24,13 @@ // NOTE: you need to power the devices yourself, because the external // PU-resistor might not do it completely -class OneWire -{ +class OneWire { private: io_reg_t pin_bitMask; - volatile io_reg_t *pin_baseReg; + volatile io_reg_t* pin_baseReg; - // search state + /// search state uint8_t search_rom_array[8]; uint8_t search_last_discrepancy; uint8_t search_last_family_discrepancy; @@ -39,118 +38,148 @@ class OneWire public: + /// constructor for the bus master + /// + /// \param pin number of the pin as stated by controller-framework explicit OneWire(uint8_t pin); + /// instantiate a default destructor ~OneWire() = default; // nothing special to do here - OneWire(const OneWire &ow) = delete; // disallow copy constructor - OneWire &operator=(OneWire &ow) = delete; // disallow copy assignment - OneWire &operator=(const OneWire &ow) = delete; // disallow copy assignment + OneWire(const OneWire& ow) = delete; // disallow copy constructor + OneWire& operator=(OneWire& ow) = delete; // disallow copy assignment + OneWire& operator=(const OneWire& ow) = delete; // disallow copy assignment -#if (ONEWIRE_GCC_VERSION > 40900) // is needed because of old tool chain for arduino primo - OneWire(OneWire &&ow) = default; // default move constructor - OneWire &operator=(OneWire &&ow) = delete; // disallow move assignment +#if (ONEWIRE_GCC_VERSION>40900) // is needed because of an old tool chain for arduino primo + OneWire(OneWire&& ow) = default; // default move constructor + OneWire& operator=(OneWire&& ow) = delete; // disallow move assignment #endif - // 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 + /// Perform a 1-Wire reset cycle. Will wait up to 250uS for the bus to come high, + /// if it doesn't then it is broken or shorted + /// + /// \return true = a device responds with a presence pulse. + /// false = there is no device or the bus is shorted or otherwise held low for more than 250uS bool reset(); - // Issue a 1-Wire rom select command, you do the reset first. + /// Issue a 1-Wire rom select command. Will need a bus reset first. + /// + /// \param rom_array provide pointer to ROM of slave device void select(const uint8_t rom_array[8]); - // Issue a 1-Wire rom skip command, to address all on bus. + /// Issue a 1-Wire rom skip command, to address all on bus. normaly this should only be used if ONE slave device + /// is on the bus. otherwise all present device would respond + /// void skip(); - // 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. + /// Write a bit. + /// + /// \param value bit value to write + /// \param power true = 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. + /// false = high impedance mode after writing + void write_bit(bool value, bool power = false); + + /// Write a byte. + /// + /// \param value byte to write + /// \param power true = 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. + /// false = high impedance mode after writing void write(uint8_t value, bool power = false); + /// Write an array of bytes + /// + /// \param data_array pointer to array + /// \param data_size size of array + /// \param power true = 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. + /// false = high impedance mode after writing void write_bytes(const uint8_t data_array[], uint16_t data_size, bool power = false); - // Read a byte. + /// Read a bit. + /// + /// \return bit that has been read + bool read_bit(); + + /// Read a byte. + /// + /// \return byte that has been read uint8_t read(); + /// Read an array of bytes + /// + /// \param data_array pointer to array + /// \param data_size size of array void read_bytes(uint8_t data_array[], uint16_t data_size); - // Write a bit. The bus is always left powered at the end, see - // note in write() about that. - void write_bit(bool value, bool power = false); - - // Read a bit. - bool read_bit(); - - // activate parasitic bus powering by master - // write_bytes() , write() and write_bit() have these features already build in, - // but reset() and read() do not --> use this after issuing + /// activate parasitic bus powering by master + /// - write_bytes() , write() and write_bit() have these features already build in, + /// - but reset() and read() do not --> use this after issuing + /// void power(); - // 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. + /// 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(); - - // Clear the search state so that if will start from the beginning again. + /// Clear the search state so that it will start from the beginning again. + /// Not needed to call prior the first search, though it does not matter. + /// void reset_search(); - // Setup the search to find the device type 'family_code' on the next call - // to search(*newAddr) if it is present. + /// Setup the search to find the device type 'family_code' on the next call + /// to search(*newAddr) if it is present. + /// + /// \param family_code device type to search for on next search() run 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. + /// Look for the next device. 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. + /// + /// \param new_rom_array pointer to array of data, will be filled with found ROM + /// \param search_mode true = normal search mode + /// false = conditional search + /// \return true = a new address has been returned. + /// false = might mean that the bus is shorted, there are no devices, or you have already retrieved all of them. bool search(uint8_t new_rom_array[8], bool search_mode = true); - - // Compute a Dallas Semiconductor 8 bit CRC, these are used in the - // ROM and scratchpad registers. + /// Compute a Dallas Semiconductor 8 bit CRC, these are used in the + /// ROM and scratchpad registers. + /// + /// \param data_array pointer to array of data to check + /// \param data_size size of data to check (usually one byte smaller than array size, because last byte is crc itself) + /// \param crc_init initial crc-value, normally left blank (set to zero) + /// \return calculated crc static uint8_t crc8(const uint8_t data_array[], uint8_t data_size, uint8_t crc_init = 0); - - // 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(onewire, buf, 3); // Write 3 cmd bytes - // ReadBytes(onewire, 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 data_array[], uint16_t data_size, 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. + /// Compute the 1-Wire CRC16 and compare it against the received CRC. + /// + /// \param data_array Array of bytes to checksum. + /// \param data_size size of data to check (usually one byte smaller than array size, because last byte is crc itself) + /// \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 initial crc-value, normally left blank (set to zero) + /// \return true = crc matches / is correct + static bool check_crc16(const uint8_t data_array[], uint16_t data_size, 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 data_array Array of bytes to checksum. + /// \param data_size size of data to check (usually one byte smaller than array size, because last byte is crc itself) + /// \param crc_init initial crc-value, normally left blank (set to zero) + /// \return calculated crc static uint16_t crc16(const uint8_t data_array[], uint16_t data_size, uint16_t crc_init = 0); }; diff --git a/src/platform.cpp b/src/platform.cpp index 1949fc4..339ced9 100644 --- a/src/platform.cpp +++ b/src/platform.cpp @@ -2,7 +2,7 @@ #ifdef ONEWIRE_FALLBACK_BASIC_FNs -uint32_t micros() {return 0; }; // original arduino-fn takes about 3 µs to process @ 16 MHz +uint32_t micros() { return 0; }; // original arduino-fn takes about 3 µs to process @ 16 MHz void cli() { }; void sei() { }; diff --git a/src/platform.h b/src/platform.h index 2915fc7..3855832 100644 --- a/src/platform.h +++ b/src/platform.h @@ -10,7 +10,6 @@ #define ONEWIRE_GCC_VERSION 0 #endif -#if defined(ARDUINO) && ARDUINO >= 100 #include #endif @@ -247,35 +246,27 @@ using io_reg_t = uint32_t; // define special data type for register-access static bool mockup_pin_value[256]; template -bool digitalRead(const T1 pin) -{ return (mockup_pin_value[pin & 0xFF] != 0); }; // mock up outputs +bool digitalRead(const T1 pin) { return (mockup_pin_value[pin & 0xFF]!=0); }; // mock up outputs template -void digitalWrite(const T1 pin, const T2 value) -{ mockup_pin_value[pin & 0xFF] = value; }; +void digitalWrite(const T1 pin, const T2 value) { mockup_pin_value[pin & 0xFF] = value; }; template -void pinMode(const T1 pin, const T2 value) -{ mockup_pin_value[pin & 0xFF] = value; }; +void pinMode(const T1 pin, const T2 value) { mockup_pin_value[pin & 0xFF] = value; }; template -T1 digitalPinToPort(const T1 pin) -{ return pin; }; +T1 digitalPinToPort(const T1 pin) { return pin; }; template -T1 *portInputRegister(const T1 port) -{ return port; }; +T1* portInputRegister(const T1 port) { return port; }; template -T1 digitalPinToBitMask(const T1 pin) -{ return pin; }; +T1 digitalPinToBitMask(const T1 pin) { return pin; }; -constexpr uint32_t microsecondsToClockCycles(const uint32_t micros) -{ return (100 * micros); }; // mockup, emulate 100 MHz CPU +constexpr uint32_t microsecondsToClockCycles(const uint32_t micros) { return (100*micros); }; // mockup, emulate 100 MHz CPU template -void delayMicroseconds(const T1 micros) -{}; +void delayMicroseconds(const T1 micros) { }; /// the following fn are no templates and need to be defined in platform.cpp @@ -290,14 +281,13 @@ void noInterrupts(); void interrupts(); template -T1 pgm_read_byte(const T1 *address) +T1 pgm_read_byte(const T1* address) { return *address; } #endif - #ifdef ONEWIRE_FALLBACK_ADDITIONAL_FNs // Test to make it work on aTtiny85, 8MHz /// README: use pin2 or pin3 for Attiny, source: https://github.com/gioblu/PJON/wiki/ATtiny-interfacing @@ -309,59 +299,50 @@ T1 pgm_read_byte(const T1 *address) #define HEX 2 #endif - -static class serial -{ +static class serial { private: - uint32_t speed; + uint32_t speed; public: - void print(...) - {}; + void print(...) { }; - void println(...) - {}; + void println(...) { }; - void flush() - {}; + void flush() { }; - void begin(const uint32_t speed_baud) - { speed = speed_baud; }; + void begin(const uint32_t speed_baud) { speed = speed_baud; }; } Serial; - template -void memset(T1 *const address, const T1 initValue, const T2 bytes) +void memset(T1* const address, const T1 initValue, const T2 bytes) { - const T2 iterations = bytes / sizeof(T1); - for (T2 counter = 0; counter < iterations; ++counter) + const T2 iterations = bytes/sizeof(T1); + for (T2 counter = 0; counter -void memcpy(T1 *const destination, const T1 *const source, const T2 bytes) +void memcpy(T1* const destination, const T1* const source, const T2 bytes) { - const T2 iterations = bytes / sizeof(T1); - for (T2 counter = 0; counter < iterations; ++counter) + const T2 iterations = bytes/sizeof(T1); + for (T2 counter = 0; counter -bool memcmp(const T1 *const source_A, const T1 *const source_B, const T2 bytes) // return true if string is different +bool memcmp(const T1* const source_A, const T1* const source_B, const T2 bytes) // return true if string is different { - const T2 iterations = bytes / sizeof(T1); - for (T2 counter = 0; counter < iterations; ++counter) + const T2 iterations = bytes/sizeof(T1); + for (T2 counter = 0; counter Date: Sat, 22 Jul 2017 19:33:47 +0200 Subject: [PATCH 42/71] missed line --- src/platform.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/platform.h b/src/platform.h index 3855832..c20d0fc 100644 --- a/src/platform.h +++ b/src/platform.h @@ -10,6 +10,7 @@ #define ONEWIRE_GCC_VERSION 0 #endif +#if defined(ARDUINO) && (ARDUINO>=100) #include #endif From 29789b7d0f3839b7d893ce13f8758f343ad61ae9 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 22 Jul 2017 20:24:53 +0200 Subject: [PATCH 43/71] introduce and use onewire-cmds and overload write() and read() --- .../DS18x20_Temperature.ino | 2 +- examples/DS2408_Switch/DS2408_Switch.ino | 4 +-- examples/DS2438_BatMon/DS2438_BatMon.ino | 2 +- examples/DS250x_PROM/DS250x_PROM.ino | 4 +-- readme.md | 4 +-- src/OneWire.cpp | 12 ++++----- src/OneWire.h | 27 +++++++++++++++++-- 7 files changed, 39 insertions(+), 16 deletions(-) diff --git a/examples/DS18x20_Temperature/DS18x20_Temperature.ino b/examples/DS18x20_Temperature/DS18x20_Temperature.ino index 333c28a..b21514a 100644 --- a/examples/DS18x20_Temperature/DS18x20_Temperature.ino +++ b/examples/DS18x20_Temperature/DS18x20_Temperature.ino @@ -82,7 +82,7 @@ void loop() oneWire.write(0xBE); // Read Scratchpad uint8_t device_data[12]; - oneWire.read_bytes(device_data,9); + oneWire.read(device_data,9); Serial.print(" Data ="); for (uint8_t index = 0; index < 9; index++) // we need 9 bytes diff --git a/examples/DS2408_Switch/DS2408_Switch.ino b/examples/DS2408_Switch/DS2408_Switch.ino index 57f6f7c..892f1ad 100644 --- a/examples/DS2408_Switch/DS2408_Switch.ino +++ b/examples/DS2408_Switch/DS2408_Switch.ino @@ -71,8 +71,8 @@ void loop() device_data[0] = 0xF0; // Read PIO Registers device_data[1] = 0x88; // LSB address device_data[2] = 0x00; // MSB address - oneWire.write_bytes(device_data, 3); - oneWire.read_bytes(device_data + 3, 10); // 3 cmd bytes, 6 data bytes, 2 0xFF, 2 CRC16 + oneWire.write(device_data, 3); + oneWire.read(device_data + 3, 10); // 3 cmd bytes, 6 data bytes, 2 0xFF, 2 CRC16 oneWire.reset(); Serial.print(" data ="); diff --git a/examples/DS2438_BatMon/DS2438_BatMon.ino b/examples/DS2438_BatMon/DS2438_BatMon.ino index e4679d3..aee0421 100644 --- a/examples/DS2438_BatMon/DS2438_BatMon.ino +++ b/examples/DS2438_BatMon/DS2438_BatMon.ino @@ -69,7 +69,7 @@ void loop() oneWire.write(0xBE); // Read Scratchpad oneWire.write(0x00); // request Page 0, measurements uint8_t device_data[9]; - oneWire.read_bytes(device_data,9); + oneWire.read(device_data,9); Serial.print(" data ="); for(uint8_t index = 0; index < 9; index++) diff --git a/examples/DS250x_PROM/DS250x_PROM.ino b/examples/DS250x_PROM/DS250x_PROM.ino index 4103b3b..1c3728f 100644 --- a/examples/DS250x_PROM/DS250x_PROM.ino +++ b/examples/DS250x_PROM/DS250x_PROM.ino @@ -93,7 +93,7 @@ void loop() // array with the commands to initiate a read, DS250x devices expect 3 bytes to start a read: command,LSB&MSB addresses uint8_t cmd_read[3] = { 0xF0, 0x00, 0x00 }; // 0xF0 is the Read Data command, followed by 00h 00h as starting address(the beginning, 0000h) - oneWire.write_bytes(cmd_read, 3, true); // Read data command, leave ghost power on + oneWire.write(cmd_read, 3, true); // Read data command, leave ghost power on const uint8_t crc_read = oneWire.read(); // DS250x generates a CRC for the command we sent, we assign a read slot and store it's value @@ -104,7 +104,7 @@ void loop() } uint8_t device_data[32]; // container for the data from device - oneWire.read_bytes(device_data,32); // we store each read byte to a different position in the data array + oneWire.read(device_data,32); // we store each read byte to a different position in the data array Serial.print(" data ="); // For the printout of the data diff --git a/readme.md b/readme.md index 0853aec..521e42f 100644 --- a/readme.md +++ b/readme.md @@ -56,15 +56,15 @@ OneWire Master Library ### Plans for the future - update the interface, NOTE: these changes can be made without breaking old code, just use "deprecated" on old interface - - use overloaded functions read and write_bytes(), write(), write_bit() become just write() - simplify bus powering as extra argument for reset and read as well, OR we set it on bus constructor - predefine standard onewire commands and use send(), skip() is not needed then -- another hardware abstraction to allow proper esp-use +- delay() hardware abstraction to allow proper esp-use - unit tests - CI ### Development history Version 3.0 +- overload write() to replace write_bytes() and read() to replace read_byte() - update documentation, doxygen style - allow to use internal pull-up if micro controller has support for it -> most µC support INPUT_PULLUP in combination with pinMode(), others will get an errormessage if feature is enabled but not supported / tested - clean up compiler-switches diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 90b1ff2..95d3b17 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -182,7 +182,7 @@ void OneWire::write(uint8_t value, const bool power) } } -void OneWire::write_bytes(const uint8_t data_array[], const uint16_t data_size, const bool power) +void OneWire::write(const uint8_t data_array[], const uint16_t data_size, const bool power) { for (uint16_t index = 0; index0) @@ -222,7 +222,7 @@ void OneWire::select(const uint8_t rom_array[8]) void OneWire::skip() { - write(0xCC); // Skip ROM + write(CMD_SKIP_ROM); // Skip ROM } void OneWire::power() @@ -282,11 +282,11 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) // issue the search command if (search_mode) { - write(0xF0); // NORMAL SEARCH + write(CMD_SEARCH_ROM); // NORMAL SEARCH } else { - write(0xEC); // CONDITIONAL SEARCH + write(CMD_CONDITIONAL_SEARCH); // CONDITIONAL SEARCH } // loop to do the search diff --git a/src/OneWire.h b/src/OneWire.h index 4449d86..f269d78 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -24,6 +24,9 @@ // NOTE: you need to power the devices yourself, because the external // PU-resistor might not do it completely + + + class OneWire { private: @@ -38,6 +41,18 @@ class OneWire { public: + /// ONEWIRE COMMANDS + static const uint8_t CMD_SEARCH_ROM { 0xF0 }; + static const uint8_t CMD_CONDITIONAL_SEARCH { 0xEC }; + static const uint8_t CMD_SKIP_ROM { 0xCC }; + static const uint8_t CMD_SKIP_ROM_OVERDRIVE { 0x3C }; + static const uint8_t CMD_MATCH_ROM { 0x55 }; + static const uint8_t CMD_MATCH_ROM_OVERDRIVE { 0x69 }; + static const uint8_t CMD_READ_ROM { 0x0F }; + static const uint8_t CMD_READ_ROM_OLD { 0x33 }; + static const uint8_t CMD_ALARM_SEARCH { 0xEC }; + static const uint8_t CMD_RESUME { 0xA5 }; + /// constructor for the bus master /// /// \param pin number of the pin as stated by controller-framework @@ -95,7 +110,11 @@ class OneWire { /// \param power true = 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. /// false = high impedance mode after writing - void write_bytes(const uint8_t data_array[], uint16_t data_size, bool power = false); + void write(const uint8_t data_array[], uint16_t data_size, bool power = false); + + /// deprecated "got replaced by overloaded version of write(), will be deleted in future version" + void write_bytes(const uint8_t data_array[], uint16_t data_size, bool power = false) + { write(data_array, data_size, power); }; /// Read a bit. /// @@ -111,7 +130,11 @@ class OneWire { /// /// \param data_array pointer to array /// \param data_size size of array - void read_bytes(uint8_t data_array[], uint16_t data_size); + void read(uint8_t data_array[], uint16_t data_size); + + /// deprecated "got replaced by overloaded version of read(), will be deleted in future version" + void read_bytes(uint8_t data_array[], uint16_t data_size) + { read(data_array, data_size); }; /// activate parasitic bus powering by master /// - write_bytes() , write() and write_bit() have these features already build in, From 1063008fb4c5e5bd928f60286b48719cfbc3b309 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 22 Jul 2017 22:11:18 +0200 Subject: [PATCH 44/71] temporary fix for problems with redbear-modules --- readme.md | 8 +++++--- src/OneWire.cpp | 18 +++++++++--------- src/OneWire.h | 3 ++- src/platform.h | 42 ++++++++++++++++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/readme.md b/readme.md index 521e42f..981445a 100644 --- a/readme.md +++ b/readme.md @@ -29,9 +29,9 @@ OneWire Master Library - generic ESP8266 (esp8266) ### How to use -- understanding [onewire protocol](https://en.wikipedia.org/wiki/1-Wire) help a lot +- understanding [onewire protocol](https://en.wikipedia.org/wiki/1-Wire) helps a lot during "how to" - connect your microcontroller (onewire-master) with the onewire-bus -- the onewire-bus should be set up as described below + - the onewire-bus should be set up as described in separate section below - the examples are mostly self explanatory - the microcontroller acts as a master - it will search and select devices on the bus @@ -58,12 +58,14 @@ OneWire Master Library - update the interface, NOTE: these changes can be made without breaking old code, just use "deprecated" on old interface - simplify bus powering as extra argument for reset and read as well, OR we set it on bus constructor - predefine standard onewire commands and use send(), skip() is not needed then -- delay() hardware abstraction to allow proper esp-use - unit tests - CI +- maybe add stm32 support: https://github.com/arduino-org/arduino-core-stm32f4 ### Development history Version 3.0 +- delayMicroseconds() hardware abstraction to allow proper nRF51-use (redbear messed up the delaymicroseconds()-implementation) + - will be removed as soon redbear fixes its code - overload write() to replace write_bytes() and read() to replace read_byte() - update documentation, doxygen style - allow to use internal pull-up if micro controller has support for it -> most µC support INPUT_PULLUP in combination with pinMode(), others will get an errormessage if feature is enabled but not supported / tested diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 95d3b17..94bddeb 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -119,21 +119,21 @@ bool OneWire::reset() do { if (--retries==0) return false; - delayMicroseconds(2); + DELAY_MICROSECONDS(2); } while (!DIRECT_READ(_baseReg, _bitMask)); DIRECT_WRITE_LOW(_baseReg, _bitMask); DIRECT_MODE_OUTPUT(_baseReg, _bitMask); // drive output low - delayMicroseconds(480); + DELAY_MICROSECONDS(480); noInterrupts(); DIRECT_MODE_INPUT(_baseReg, _bitMask); // allow it to float - delayMicroseconds(70); + DELAY_MICROSECONDS(70); const bool success = !DIRECT_READ(_baseReg, _bitMask); interrupts(); - delayMicroseconds(410); + DELAY_MICROSECONDS(410); // TODO: after presence detection we could power the hub if wanted return success; @@ -149,11 +149,11 @@ void OneWire::write_bit(const bool value, const bool power) noInterrupts(); DIRECT_WRITE_LOW(_baseReg, _bitMask); DIRECT_MODE_OUTPUT(_baseReg, _bitMask); // drive output low - delayMicroseconds(time_high); + DELAY_MICROSECONDS(time_high); if (!power) DIRECT_MODE_INPUT(_baseReg, _bitMask); // allow it to float DIRECT_WRITE_HIGH(_baseReg, _bitMask); // drive output high interrupts(); - delayMicroseconds(time_low); + DELAY_MICROSECONDS(time_low); } bool OneWire::read_bit() @@ -164,12 +164,12 @@ bool OneWire::read_bit() noInterrupts(); DIRECT_WRITE_LOW(_baseReg, _bitMask); DIRECT_MODE_OUTPUT(_baseReg, _bitMask); - delayMicroseconds(3); + DELAY_MICROSECONDS(3); DIRECT_MODE_INPUT(_baseReg, _bitMask); // let pin float, pull up will raise - delayMicroseconds(10); + DELAY_MICROSECONDS(10); const bool value = DIRECT_READ(_baseReg, _bitMask); interrupts(); - delayMicroseconds(53); + DELAY_MICROSECONDS(53); return value; } diff --git a/src/OneWire.h b/src/OneWire.h index f269d78..c622567 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -52,6 +52,7 @@ class OneWire { static const uint8_t CMD_READ_ROM_OLD { 0x33 }; static const uint8_t CMD_ALARM_SEARCH { 0xEC }; static const uint8_t CMD_RESUME { 0xA5 }; + // TODO: should be constexpr, but NRF5x (redbear, ardu primo) have old toolchain /// constructor for the bus master /// @@ -65,7 +66,7 @@ class OneWire { OneWire& operator=(OneWire& ow) = delete; // disallow copy assignment OneWire& operator=(const OneWire& ow) = delete; // disallow copy assignment -#if (ONEWIRE_GCC_VERSION>40900) // is needed because of an old tool chain for arduino primo +#if (ONEWIRE_GCC_VERSION>40900) // is needed because of an old tool chain for nrf5x OneWire(OneWire&& ow) = default; // default move constructor OneWire& operator=(OneWire&& ow) = delete; // disallow move assignment #endif diff --git a/src/platform.h b/src/platform.h index c20d0fc..0af81b2 100644 --- a/src/platform.h +++ b/src/platform.h @@ -29,6 +29,7 @@ #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)) +#define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint8_t; // define special data type for register-access #elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) /* teensy 3.2 to 3.6 */ @@ -39,6 +40,7 @@ using io_reg_t = uint8_t; // define special data type for register-access #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) +#define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint8_t; // define special data type for register-access #elif defined(__MKL26Z64__) /* teensy LC */ @@ -49,9 +51,11 @@ using io_reg_t = uint8_t; // define special data type for register-access #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)) +#define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint8_t; // define special data type for register-access #elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__) /* arduino due */ +// https://github.com/arduino/ArduinoCore-sam // 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 @@ -63,6 +67,7 @@ using io_reg_t = uint8_t; // define special data type for register-access #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)) +#define DELAY_MICROSECONDS(us) delayMicroseconds(us) #ifndef PROGMEM #define PROGMEM #endif @@ -72,6 +77,8 @@ using io_reg_t = uint8_t; // define special data type for register-access using io_reg_t = uint32_t; // define special data type for register-access #elif defined(__PIC32MX__) +// https://github.com/chipKIT32/chipKIT-core + #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) #define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10 @@ -79,6 +86,7 @@ using io_reg_t = uint32_t; // define special data type for register-access #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 +#define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint32_t; // define special data type for register-access #if ONEWIRE_USE_PULL_UP @@ -98,9 +106,12 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS #define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS #define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS +#define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint32_t; // define special data type for register-access #elif defined(__SAMD21G18A__) /* arduino zero */ +// https://github.com/arduino/ArduinoCore-samd + #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) #define DIRECT_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0) @@ -108,9 +119,12 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask)) #define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask)) #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask)) +#define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint32_t; // define special data type for register-access -#elif defined(RBL_NRF51822) || defined(NRF5) /* arduino primo, red bear blend, should be good for all nrf5x chips */ +#elif defined(NRF52) /* arduino primo */ +// https://github.com/arduino-org/arduino-core-nrf52 + #define PIN_TO_BASEREG(pin) (0) #define PIN_TO_BITMASK(pin) (pin) #define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin) @@ -118,8 +132,26 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) #define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) #define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) +#define DELAY_MICROSECONDS(us) delayMicroseconds(us) +#define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ +// using io_reg_t = uint32_t; // define special data type for register-access + +#elif defined(NRF51) /* red bear blend, should be good for all nrf51x chips */ +// https://github.com/RedBearLab/nRF51822-Arduino + +#if defined(TARGET_NRF51822) +#include +#endif -#define io_reg_t uint32_t /* the tool chain is old .... */ +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin) +#define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin) +#define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) +#define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) +#define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) +#define DELAY_MICROSECONDS(us) nrf_delay_us(us) // TODO: only needed because of faulty redbear-delay()-implementation +#define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ // using io_reg_t = uint32_t; // define special data type for register-access #elif defined(__arc__) /* Arduino101/Genuino101 specifics */ @@ -199,11 +231,12 @@ void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) } } -#define DIRECT_READ(base, pin) directRead(base, pin) +#define DIRECT_READ(base, pin) directRead(base, pin) #define DIRECT_MODE_INPUT(base, pin) directModeInput(base, pin) #define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(base, pin) -#define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin) +#define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin) #define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(base, pin) +#define DELAY_MICROSECONDS(us) delayMicroseconds(us) #else // any unknown architecture, including PC @@ -216,6 +249,7 @@ void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) #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) +#define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint32_t; // define special data type for register-access #warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture." From 0baab6799d5b1de2701c3aaaeb1c1c9c7f8fdea0 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 23 Jul 2017 10:40:18 +0200 Subject: [PATCH 45/71] move information about board libraries to readme --- readme.md | 27 +++++++++++++++------------ src/platform.h | 7 ++----- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/readme.md b/readme.md index 981445a..349ac52 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ OneWire Master Library - clean, small, fast code with c++11 features (requires arduino sw 1.6.x or higher, >=1.6.10 recommended) - proper naming of variables make it easier to read the code - hardware-dependencies are combined in "platform.h" - - supported: arduino zero, teensy, sam3x, pic32, [ATtiny](https://github.com/damellis/attiny), esp8266, nrf51822, raspberry (...) + - supported: arduino uno / zero / due / primo, teensy, sam3x, pic32, ATtiny, esp8266, nrf51822, raspberry (...) - for portability and tests the hub can be compiled on a PC with the supplied mock-up functions in platform.h - extended and better understandable examples - command to enable or disable parasitic bus powering by the master (was implemented wrong before) @@ -17,16 +17,20 @@ OneWire Master Library ### Tested hardware - tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets - run as ds18x20-master: - - Arduino Uno (Arduino AVR Boards) - - Teensy 3.2 (teensyduino) + - Arduino Uno ([Arduino AVR Boards](https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr)) + - Teensy 3.2 ([teensyduino](https://github.com/PaulStoffregen/cores)) - compile process passes: - - Teensy LC (teensyduino) - - Teensy 3.6 (teensyduino) - - Arduino Zero native USB (Arduino SAMD Boards (32-bits ARM Cortex-M0+)) - - Arduino Primo (Arduino nRF52 Boards) - - Arduino Due native USB (Arduino SAMD Boards (32-bits ARM Cortex-M3)) - - ATtiny 84 (attiny) - - generic ESP8266 (esp8266) + - Teensy LC ([teensyduino](https://github.com/PaulStoffregen/cores)) + - Teensy 3.6 ([teensyduino](https://github.com/PaulStoffregen/cores)) + - Arduino Zero native USB ([Arduino SAMD Boards (32-bits ARM Cortex-M0+)](https://github.com/arduino/ArduinoCore-samd)) + - Arduino Primo ([Arduino nRF52 Boards](https://github.com/arduino-org/arduino-core-nrf52)) + - Arduino Due native USB ([Arduino SAMD Boards (32-bits ARM Cortex-M3)](https://github.com/arduino/ArduinoCore-sam)) + - ATtiny 84 ([attiny](https://github.com/damellis/attiny)) + - generic ESP8266 ([esp8266](https://github.com/esp8266/Arduino)) +- untested + - [pic32](https://github.com/chipKIT32/chipKIT-core) + - [nRF51](https://github.com/RedBearLab/nRF51822-Arduino) + - Arduino 101 ([Intel Curie Boards](https://github.com/01org/corelibs-arduino101)) ### How to use - understanding [onewire protocol](https://en.wikipedia.org/wiki/1-Wire) helps a lot during "how to" @@ -54,13 +58,12 @@ OneWire Master Library - please provide the following basic info: which µC do you use, software versions, what device do you try to work with, what works, what doesn't ### Plans for the future - - update the interface, NOTE: these changes can be made without breaking old code, just use "deprecated" on old interface - simplify bus powering as extra argument for reset and read as well, OR we set it on bus constructor - predefine standard onewire commands and use send(), skip() is not needed then - unit tests - CI -- maybe add stm32 support: https://github.com/arduino-org/arduino-core-stm32f4 +- add stm32f4 support: https://github.com/arduino-org/arduino-core-stm32f4 ### Development history Version 3.0 diff --git a/src/platform.h b/src/platform.h index 0af81b2..a108e9a 100644 --- a/src/platform.h +++ b/src/platform.h @@ -33,6 +33,7 @@ using io_reg_t = uint8_t; // define special data type for register-access #elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) /* teensy 3.2 to 3.6 */ + #define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) #define PIN_TO_BITMASK(pin) (1) #define DIRECT_READ(base, mask) (*((base)+512)) @@ -44,6 +45,7 @@ using io_reg_t = uint8_t; // define special data type for register-access using io_reg_t = uint8_t; // define special data type for register-access #elif defined(__MKL26Z64__) /* teensy LC */ + #define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) #define DIRECT_READ(base, mask) ((*((base)+16) & (mask)) ? 1 : 0) @@ -55,7 +57,6 @@ using io_reg_t = uint8_t; // define special data type for register-access using io_reg_t = uint8_t; // define special data type for register-access #elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__) /* arduino due */ -// https://github.com/arduino/ArduinoCore-sam // 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 @@ -77,7 +78,6 @@ using io_reg_t = uint8_t; // define special data type for register-access using io_reg_t = uint32_t; // define special data type for register-access #elif defined(__PIC32MX__) -// https://github.com/chipKIT32/chipKIT-core #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) @@ -110,7 +110,6 @@ using io_reg_t = uint32_t; // define special data type for register-access using io_reg_t = uint32_t; // define special data type for register-access #elif defined(__SAMD21G18A__) /* arduino zero */ -// https://github.com/arduino/ArduinoCore-samd #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) @@ -123,7 +122,6 @@ using io_reg_t = uint32_t; // define special data type for register-access using io_reg_t = uint32_t; // define special data type for register-access #elif defined(NRF52) /* arduino primo */ -// https://github.com/arduino-org/arduino-core-nrf52 #define PIN_TO_BASEREG(pin) (0) #define PIN_TO_BITMASK(pin) (pin) @@ -137,7 +135,6 @@ using io_reg_t = uint32_t; // define special data type for register-access // using io_reg_t = uint32_t; // define special data type for register-access #elif defined(NRF51) /* red bear blend, should be good for all nrf51x chips */ -// https://github.com/RedBearLab/nRF51822-Arduino #if defined(TARGET_NRF51822) #include From d5651369f72db926117c58e1351ec605f329d1b2 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 10:45:41 +0200 Subject: [PATCH 46/71] test travis CI --- .travis.yml | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..84bdcd3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,37 @@ +# source: https://github.com/udoklein/dcf77/blob/master/.travis.yml + +language: cpp + +env: + global: + - ARDUINO_PACKAGE_VERSION=1.8.3 + - DISPLAY=:1.0 + + matrix: + - TARGET_BOARD=diecimila + - TARGET_BOARD=leonardo + - TARGET_BOARD=due + +before_script: + - /sbin/start-stop-daemon --start --quiet --pidfile /tmp/xvfb_$TRAVIS_JOB_NUMBER.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16 + - wget -q -O- http://downloads.arduino.cc/arduino-$ARDUINO_PACKAGE_VERSION-linux64.tar.xz | unxz -c | tar -xf - + + - mkdir -p ~/Arduino/libraries/ + - ln -s $TRAVIS_BUILD_DIR ~/Arduino/libraries/ + - $TRAVIS_BUILD_DIR/arduino-$ARDUINO_PACKAGE_VERSION/arduino --install-boards arduino:sam + + - function log() { RCODE="$1"; MSG="$2"; if [ "$RCODE" -ne 0 ]; then echo "failure for $MSG" >> build_log.txt; return 1; else echo "success for $MSG" >> build_log.txt; fi; } + - function build_sketch() { BOARD="$1"; SPEC=$2 EXAMPLE="$3"; $TRAVIS_BUILD_DIR/arduino-$ARDUINO_PACKAGE_VERSION/arduino --verbose --verify --board "$SPEC" $TRAVIS_BUILD_DIR/examples/$EXAMPLE/$EXAMPLE.INO; log "$?" "$BOARD $EXAMPLE"; } + - function build_on_match() { BOARD="$1"; SPEC=$2 PATTERN="$3"; EXAMPLE="$4"; if [[ "$BOARD" =~ $PATTERN ]]; then build_sketch "$BOARD" "$SPEC" "$EXAMPLE"; else echo "excluded $BOARD $EXAMPLE" >> build_log.txt; fi; } + - function diecimila() { build_on_match "diecimila" arduino:avr:diecimila:cpu=atmega328 "$1" "$2"; } + - function leonardo() { build_on_match "leonardo" arduino:avr:leonardo "$1" "$2"; } + - function due() { build_on_match "due" arduino:sam:arduino_due_x "$1" "$2"; } + +script: + - $TARGET_BOARD "diecimila|leonardo|due" "bus_discovery" + - $TARGET_BOARD "diecimila|leonardo|due" "DS18x20_Temperature" + - $TARGET_BOARD "diecimila|leonardo|due" "DS2408_Switch" + - $TARGET_BOARD "diecimila|leonardo|due" "DS250x_PROM" + - $TARGET_BOARD "diecimila|leonardo|due" "DS2438_BatMon" + - echo; echo "finished script for $TARGET_BOARD" + - cat build_log.txt \ No newline at end of file From c7476cbabddc53b9a406218f2f25e1b8d98ec9bb Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 10:52:37 +0200 Subject: [PATCH 47/71] add gitattributes to handle various OS-standards --- .gitattributes | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..8094d99 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,28 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp + +# sources +*.ino text +*.cpp text +*.c text +*.hpp text +*.h text +*.md text +*.yml text +*.json text +*.properties text + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain From 4f1d50dc50bbf103872f166d3ee554709189d348 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 12:04:32 +0200 Subject: [PATCH 48/71] try platform io as a CI --- .travis.yml | 48 ++++++++++++++++-------------------------------- readme.md | 2 ++ 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/.travis.yml b/.travis.yml index 84bdcd3..9beffe9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,37 +1,21 @@ -# source: https://github.com/udoklein/dcf77/blob/master/.travis.yml +language: python -language: cpp +python: + - "2.7" -env: - global: - - ARDUINO_PACKAGE_VERSION=1.8.3 - - DISPLAY=:1.0 +install: + - python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)" - matrix: - - TARGET_BOARD=diecimila - - TARGET_BOARD=leonardo - - TARGET_BOARD=due - -before_script: - - /sbin/start-stop-daemon --start --quiet --pidfile /tmp/xvfb_$TRAVIS_JOB_NUMBER.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16 - - wget -q -O- http://downloads.arduino.cc/arduino-$ARDUINO_PACKAGE_VERSION-linux64.tar.xz | unxz -c | tar -xf - - - - mkdir -p ~/Arduino/libraries/ - - ln -s $TRAVIS_BUILD_DIR ~/Arduino/libraries/ - - $TRAVIS_BUILD_DIR/arduino-$ARDUINO_PACKAGE_VERSION/arduino --install-boards arduino:sam +script: +- platformio ci examples/DS18x20_Temperature --board=uno --board=due --board=zero --board=teensy30 --board=teensy31 --board=teensy35 --board=teensy36 --board=esp01 - - function log() { RCODE="$1"; MSG="$2"; if [ "$RCODE" -ne 0 ]; then echo "failure for $MSG" >> build_log.txt; return 1; else echo "success for $MSG" >> build_log.txt; fi; } - - function build_sketch() { BOARD="$1"; SPEC=$2 EXAMPLE="$3"; $TRAVIS_BUILD_DIR/arduino-$ARDUINO_PACKAGE_VERSION/arduino --verbose --verify --board "$SPEC" $TRAVIS_BUILD_DIR/examples/$EXAMPLE/$EXAMPLE.INO; log "$?" "$BOARD $EXAMPLE"; } - - function build_on_match() { BOARD="$1"; SPEC=$2 PATTERN="$3"; EXAMPLE="$4"; if [[ "$BOARD" =~ $PATTERN ]]; then build_sketch "$BOARD" "$SPEC" "$EXAMPLE"; else echo "excluded $BOARD $EXAMPLE" >> build_log.txt; fi; } - - function diecimila() { build_on_match "diecimila" arduino:avr:diecimila:cpu=atmega328 "$1" "$2"; } - - function leonardo() { build_on_match "leonardo" arduino:avr:leonardo "$1" "$2"; } - - function due() { build_on_match "due" arduino:sam:arduino_due_x "$1" "$2"; } +# examples/DS18x20_Temperature/DS18x20_Temperature.ino +# examples/DS2408_Switch/DS2408_Switch.ino +# examples/DS250x_PROM/DS250x_PROM.ino +# examples/DS2438_BatMon/DS2438_BatMon.ino -script: - - $TARGET_BOARD "diecimila|leonardo|due" "bus_discovery" - - $TARGET_BOARD "diecimila|leonardo|due" "DS18x20_Temperature" - - $TARGET_BOARD "diecimila|leonardo|due" "DS2408_Switch" - - $TARGET_BOARD "diecimila|leonardo|due" "DS250x_PROM" - - $TARGET_BOARD "diecimila|leonardo|due" "DS2438_BatMon" - - echo; echo "finished script for $TARGET_BOARD" - - cat build_log.txt \ No newline at end of file +#elif defined(__PIC32MX__) +#elif defined(ARDUINO_ARCH_ESP8266) /* nodeMCU, ESPduino, ... */ +#elif defined(NRF52) /* arduino primo */ +#elif defined(NRF51) /* red bear blend, should be good for all nrf51x chips */ +#elif defined(__arc__) /* Arduino101/Genuino101 specifics */ diff --git a/readme.md b/readme.md index 349ac52..7712f15 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,8 @@ OneWire Master Library ====================== +[![Build Status](https://travis-ci.org/orgua/OneWire.svg?branch=master)](https://travis-ci.org/orgua/OneWire) + ### Features - emulate a bus master in a generic way, the lib is handling most of the timing-constrains and the hardware-access - clean, small, fast code with c++11 features (requires arduino sw 1.6.x or higher, >=1.6.10 recommended) From e71d52d28dc9e440a9afbbcbd5f81f88509a0c42 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 12:18:55 +0200 Subject: [PATCH 49/71] try bugfix --- .travis.yml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9beffe9..206c9bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,18 @@ language: python python: - - "2.7" + - "2.7" install: - - python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)" + - python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)" + +before_script: + - platformio init --board uno --board due script: -- platformio ci examples/DS18x20_Temperature --board=uno --board=due --board=zero --board=teensy30 --board=teensy31 --board=teensy35 --board=teensy36 --board=esp01 + - platformio ci --lib="." --board=uno --board=due + +#--board=zero --board=teensy30 --board=teensy31 --board=teensy35 --board=teensy36 --board=esp01 # examples/DS18x20_Temperature/DS18x20_Temperature.ino # examples/DS2408_Switch/DS2408_Switch.ino From f7f9e55aa7511b719aaf06d3fda9a18ff49da156 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 12:20:51 +0200 Subject: [PATCH 50/71] next try --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 206c9bc..8c8322f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,8 @@ python: - "2.7" install: - - python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)" +# - python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)" + - pip install -U platformio before_script: - platformio init --board uno --board due From a5396beb4f9255b1311ef7e99cf44974960aed52 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 12:25:48 +0200 Subject: [PATCH 51/71] extend CI --- .travis.yml | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8c8322f..e056fd5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,19 @@ language: python python: - "2.7" +sudo: false + +cache: + directories: + - "~/.platformio" + +env: + - PLATFORMIO_CI_SRC=examples/DS18x20_Temperature + - PLATFORMIO_CI_SRC=examples/DS2408_Switch + - PLATFORMIO_CI_SRC=examples/DS250x_PROM + - PLATFORMIO_CI_SRC=examples/DS2438_BatMon + install: -# - python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)" - pip install -U platformio before_script: @@ -15,11 +26,6 @@ script: #--board=zero --board=teensy30 --board=teensy31 --board=teensy35 --board=teensy36 --board=esp01 -# examples/DS18x20_Temperature/DS18x20_Temperature.ino -# examples/DS2408_Switch/DS2408_Switch.ino -# examples/DS250x_PROM/DS250x_PROM.ino -# examples/DS2438_BatMon/DS2438_BatMon.ino - #elif defined(__PIC32MX__) #elif defined(ARDUINO_ARCH_ESP8266) /* nodeMCU, ESPduino, ... */ #elif defined(NRF52) /* arduino primo */ From d5e598707d5c3106d920593cae4eccbbe5246fc2 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 12:37:38 +0200 Subject: [PATCH 52/71] jobs by board --- .travis.yml | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index e056fd5..31d5dd5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,20 +9,25 @@ cache: directories: - "~/.platformio" -env: - - PLATFORMIO_CI_SRC=examples/DS18x20_Temperature - - PLATFORMIO_CI_SRC=examples/DS2408_Switch - - PLATFORMIO_CI_SRC=examples/DS250x_PROM - - PLATFORMIO_CI_SRC=examples/DS2438_BatMon +#env: +# - PLATFORMIO_CI_SRC=examples/DS18x20_Temperature +# - PLATFORMIO_CI_SRC=examples/DS2408_Switch +# - PLATFORMIO_CI_SRC=examples/DS250x_PROM +# - PLATFORMIO_CI_SRC=examples/DS2438_BatMon install: - pip install -U platformio +env: + - PIO_BRD=uno + - PIO_BRD=due + - PIO_BRD=zero + before_script: - - platformio init --board uno --board due + - platformio init --board $PIO_BRD script: - - platformio ci --lib="." --board=uno --board=due + - platformio ci --lib="." --board $PIO_BRD ./examples/DS18x20_Temperature/DS18x20_Temperature.ino #--board=zero --board=teensy30 --board=teensy31 --board=teensy35 --board=teensy36 --board=esp01 From 01c33eff29394918090ece12fd9ffe47dca99fe0 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 13:07:46 +0200 Subject: [PATCH 53/71] extend tested boards --- .travis.yml | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 31d5dd5..bbabf0f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,30 +9,27 @@ cache: directories: - "~/.platformio" -#env: -# - PLATFORMIO_CI_SRC=examples/DS18x20_Temperature -# - PLATFORMIO_CI_SRC=examples/DS2408_Switch -# - PLATFORMIO_CI_SRC=examples/DS250x_PROM -# - PLATFORMIO_CI_SRC=examples/DS2438_BatMon - install: - pip install -U platformio env: - - PIO_BRD=uno - - PIO_BRD=due - - PIO_BRD=zero + - PIO_BRD: "'-b=uno'" # arduino uno + - PIO_BRD: "'-b=due'" # arduino due + - PIO_BRD: "'-b=zero'" # arduino zero + - PIO_BRD: "'-b=teensy30 -b=teensy31 -b=teensy35 -b=teensy36'" + - PIO_BRD: "'-b=esp01 -b=nodemcuv2 -b=espduino'" + - PIO_BRD: "'-b=rfduino'" # nrf51 + - PIO_BRD: "'-b=lenny'" # pic32mx + - PIO_BRD: "'-b=genuino101'" # intel curie before_script: - - platformio init --board $PIO_BRD + - platformio init $PIO_BRD script: - - platformio ci --lib="." --board $PIO_BRD ./examples/DS18x20_Temperature/DS18x20_Temperature.ino - -#--board=zero --board=teensy30 --board=teensy31 --board=teensy35 --board=teensy36 --board=esp01 + - platformio ci --lib="." $PIO_BRD ./examples/DS18x20_Temperature/DS18x20_Temperature.ino + - platformio ci --lib="." $PIO_BRD ./examples/DS2408_Switch/DS2408_Switch.ino + - platformio ci --lib="." $PIO_BRD ./examples/DS250x_PROM/DS250x_PROM.ino + - platformio ci --lib="." $PIO_BRD ./examples/DS2438_BatMon/DS2438_BatMon.ino -#elif defined(__PIC32MX__) -#elif defined(ARDUINO_ARCH_ESP8266) /* nodeMCU, ESPduino, ... */ #elif defined(NRF52) /* arduino primo */ #elif defined(NRF51) /* red bear blend, should be good for all nrf51x chips */ -#elif defined(__arc__) /* Arduino101/Genuino101 specifics */ From 6692ba6eb77acaa38af484c8919328d820bc6c0c Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 13:11:38 +0200 Subject: [PATCH 54/71] try to fix bug --- .travis.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index bbabf0f..2fa1bf9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,14 +13,14 @@ install: - pip install -U platformio env: - - PIO_BRD: "'-b=uno'" # arduino uno - - PIO_BRD: "'-b=due'" # arduino due - - PIO_BRD: "'-b=zero'" # arduino zero - - PIO_BRD: "'-b=teensy30 -b=teensy31 -b=teensy35 -b=teensy36'" - - PIO_BRD: "'-b=esp01 -b=nodemcuv2 -b=espduino'" - - PIO_BRD: "'-b=rfduino'" # nrf51 - - PIO_BRD: "'-b=lenny'" # pic32mx - - PIO_BRD: "'-b=genuino101'" # intel curie + - PIO_BRD: "'-b uno'" # arduino uno + - PIO_BRD: "'-b due'" # arduino due + - PIO_BRD: "'-b zero'" # arduino zero + - PIO_BRD: "'-b teensy30 -b teensy31 -b teensy35 -b teensy36'" # teensy + - PIO_BRD: "'-b esp01 -b nodemcuv2 -b espduino'" # esp8266 + - PIO_BRD: "'-b rfduino'" # nrf51 + - PIO_BRD: "'-b lenny'" # pic32mx + - PIO_BRD: "'-b genuino101'" # intel curie before_script: - platformio init $PIO_BRD From 8f0f28d2682560b5fe8521d4503e1f33a00c1a0d Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 13:29:07 +0200 Subject: [PATCH 55/71] try to make failing board work --- .travis.yml | 2 +- src/platform.h | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2fa1bf9..d463c17 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,7 +19,7 @@ env: - PIO_BRD: "'-b teensy30 -b teensy31 -b teensy35 -b teensy36'" # teensy - PIO_BRD: "'-b esp01 -b nodemcuv2 -b espduino'" # esp8266 - PIO_BRD: "'-b rfduino'" # nrf51 - - PIO_BRD: "'-b lenny'" # pic32mx +# - PIO_BRD: "'-b lenny'" # pic32mx - PIO_BRD: "'-b genuino101'" # intel curie before_script: diff --git a/src/platform.h b/src/platform.h index a108e9a..f4c5b7c 100644 --- a/src/platform.h +++ b/src/platform.h @@ -134,7 +134,7 @@ using io_reg_t = uint32_t; // define special data type for register-access #define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ // using io_reg_t = uint32_t; // define special data type for register-access -#elif defined(NRF51) /* red bear blend, should be good for all nrf51x chips */ +#elif defined(NRF51) || defined(RFduino) /* red bear blend, should be good for all nrf51x chips */ #if defined(TARGET_NRF51822) #include @@ -168,7 +168,8 @@ using io_reg_t = uint32_t; // define special data type for register-access /* GPIO registers base address */ #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) #define PIN_TO_BITMASK(pin) pin -using io_reg_t = uint32_t; // define special data type for register-access +#define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ +//using io_reg_t = uint32_t; // define special data type for register-access #if ONEWIRE_USE_PULL_UP #error "PULL UP feature is not yet implemented or tested for your microcontroller" From e5a4738390743cdaff190faf80e68f3e8e8d493e Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 13:38:40 +0200 Subject: [PATCH 56/71] add more boardfixes --- .travis.yml | 3 +++ src/platform.h | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index d463c17..df5b21e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,3 +33,6 @@ script: #elif defined(NRF52) /* arduino primo */ #elif defined(NRF51) /* red bear blend, should be good for all nrf51x chips */ +#esp32 +#stm32 +#msp430 diff --git a/src/platform.h b/src/platform.h index f4c5b7c..cddd842 100644 --- a/src/platform.h +++ b/src/platform.h @@ -134,7 +134,7 @@ using io_reg_t = uint32_t; // define special data type for register-access #define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ // using io_reg_t = uint32_t; // define special data type for register-access -#elif defined(NRF51) || defined(RFduino) /* red bear blend, should be good for all nrf51x chips */ +#elif defined(NRF51) || defined(__RFduino__) /* red bear blend, should be good for all nrf51x chips */ #if defined(TARGET_NRF51822) #include @@ -151,7 +151,7 @@ using io_reg_t = uint32_t; // define special data type for register-access #define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ // using io_reg_t = uint32_t; // define special data type for register-access -#elif defined(__arc__) /* Arduino101/Genuino101 specifics */ +#elif defined(__arc__) || defined(__ARDUINO_ARC__) /* Arduino101/Genuino101 specifics */ #include "scss_registers.h" #include "portable.h" From 64ff43c01736a47b97e71a54aa4d55d0102a1d5f Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 14:12:00 +0200 Subject: [PATCH 57/71] fix and document travis --- .travis.yml | 5 ++++- readme.md | 34 ++++++++++++++++++++++------------ src/platform.h | 34 +++++++++++++++++----------------- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/.travis.yml b/.travis.yml index df5b21e..e40a230 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,11 +16,14 @@ env: - PIO_BRD: "'-b uno'" # arduino uno - PIO_BRD: "'-b due'" # arduino due - PIO_BRD: "'-b zero'" # arduino zero - - PIO_BRD: "'-b teensy30 -b teensy31 -b teensy35 -b teensy36'" # teensy + - PIO_BRD: "'-b teensy30 -b teensy31 -b teensy35 -b teensy36'" # teensy 3 + - PIO_BRD: "'-b teensy20 -b teensy20pp'" # teensy 2 - PIO_BRD: "'-b esp01 -b nodemcuv2 -b espduino'" # esp8266 - PIO_BRD: "'-b rfduino'" # nrf51 # - PIO_BRD: "'-b lenny'" # pic32mx - PIO_BRD: "'-b genuino101'" # intel curie + - PIO_BRD: "'-b attiny88 -b attiny84'" # attiny + - PIO_BRD: "'-b digispark-tiny'" # attiny before_script: - platformio init $PIO_BRD diff --git a/readme.md b/readme.md index 7712f15..7cf997a 100644 --- a/readme.md +++ b/readme.md @@ -16,23 +16,32 @@ OneWire Master Library - improved documentation in doxygen style - decided code style - Allman braces - was closest to main style in original -### Tested hardware -- tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets -- run as ds18x20-master: +### Continues Integration and additional tests +- Travis CI for different platforms - Arduino Uno ([Arduino AVR Boards](https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr)) - - Teensy 3.2 ([teensyduino](https://github.com/PaulStoffregen/cores)) -- compile process passes: + - Arduino Due ([Arduino SAMD Boards (32-bits ARM Cortex-M3)](https://github.com/arduino/ArduinoCore-sam)) + - Arduino Zero ([Arduino SAMD Boards (32-bits ARM Cortex-M0+)](https://github.com/arduino/ArduinoCore-samd)) + - Arduino 101 ([Intel Curie Boards](https://github.com/01org/corelibs-arduino101)) + - Teensy 3.0 ([teensyduino](https://github.com/PaulStoffregen/cores)) + - Teensy 3.1 / 3.2 ([teensyduino](https://github.com/PaulStoffregen/cores)) + - Teensy 3.5 ([teensyduino](https://github.com/PaulStoffregen/cores)) - Teensy LC ([teensyduino](https://github.com/PaulStoffregen/cores)) - Teensy 3.6 ([teensyduino](https://github.com/PaulStoffregen/cores)) - - Arduino Zero native USB ([Arduino SAMD Boards (32-bits ARM Cortex-M0+)](https://github.com/arduino/ArduinoCore-samd)) - - Arduino Primo ([Arduino nRF52 Boards](https://github.com/arduino-org/arduino-core-nrf52)) - - Arduino Due native USB ([Arduino SAMD Boards (32-bits ARM Cortex-M3)](https://github.com/arduino/ArduinoCore-sam)) - - ATtiny 84 ([attiny](https://github.com/damellis/attiny)) - generic ESP8266 ([esp8266](https://github.com/esp8266/Arduino)) + - nodeMCU V2 ([esp8266](https://github.com/esp8266/Arduino)) + - espduino ([esp8266](https://github.com/esp8266/Arduino)) + - RFduino nrf51 ([RFduino](https://github.com/RFduino/RFduino)) + - ATtiny 84 / 88 ([attiny](https://github.com/damellis/attiny)) + - Digispark tiny ([DigistumpArduino](https://github.com/digistump/DigistumpArduino)) +- run as ds18x20-master: + - Arduino Uno ([Arduino AVR Boards](https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr)) + - Teensy 3.2 ([teensyduino](https://github.com/PaulStoffregen/cores)) + - tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets +- compile process passes: + - Arduino Primo ([Arduino nRF52 Boards](https://github.com/arduino-org/arduino-core-nrf52)) - untested - [pic32](https://github.com/chipKIT32/chipKIT-core) - - [nRF51](https://github.com/RedBearLab/nRF51822-Arduino) - - Arduino 101 ([Intel Curie Boards](https://github.com/01org/corelibs-arduino101)) + - RedBear [nRF51](https://github.com/RedBearLab/nRF51822-Arduino) ### How to use - understanding [onewire protocol](https://en.wikipedia.org/wiki/1-Wire) helps a lot during "how to" @@ -64,11 +73,12 @@ OneWire Master Library - simplify bus powering as extra argument for reset and read as well, OR we set it on bus constructor - predefine standard onewire commands and use send(), skip() is not needed then - unit tests -- CI +- overdrive and other features (alarm search) - add stm32f4 support: https://github.com/arduino-org/arduino-core-stm32f4 ### Development history Version 3.0 +- added travis CI - delayMicroseconds() hardware abstraction to allow proper nRF51-use (redbear messed up the delaymicroseconds()-implementation) - will be removed as soon redbear fixes its code - overload write() to replace write_bytes() and read() to replace read_byte() diff --git a/src/platform.h b/src/platform.h index cddd842..4e6e4dd 100644 --- a/src/platform.h +++ b/src/platform.h @@ -136,7 +136,7 @@ using io_reg_t = uint32_t; // define special data type for register-access #elif defined(NRF51) || defined(__RFduino__) /* red bear blend, should be good for all nrf51x chips */ -#if defined(TARGET_NRF51822) +#if defined(TARGET_NRF51822) || defined(__RFduino__) #include #endif @@ -168,49 +168,49 @@ using io_reg_t = uint32_t; // define special data type for register-access /* GPIO registers base address */ #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) #define PIN_TO_BITMASK(pin) pin -#define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ -//using io_reg_t = uint32_t; // define special data type for register-access +//#define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ +using io_reg_t = uint32_t; // define special data type for register-access #if ONEWIRE_USE_PULL_UP #error "PULL UP feature is not yet implemented or tested for your microcontroller" #endif static inline __attribute__((always_inline)) -IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +io_reg_t directRead(volatile io_reg_t *base, io_reg_t pin) { - IO_REG_TYPE ret; + io_reg_t ret; if (SS_GPIO == GPIO_TYPE(pin)) { - ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS)); + ret = READ_ARC_REG(((io_reg_t)base + EXT_PORT_OFFSET_SS)); } else { - ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC); + ret = MMIO_REG_VAL_FROM_BASE((io_reg_t)base, EXT_PORT_OFFSET_SOC); } return ((ret >> GPIO_ID(pin)) & 0x01); } static inline __attribute__((always_inline)) -void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +void directModeInput(volatile io_reg_t *base, io_reg_t pin) { if (SS_GPIO == GPIO_TYPE(pin)) { - WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), - ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + WRITE_ARC_REG(READ_ARC_REG((((io_reg_t)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), + ((io_reg_t)(base) + DIR_OFFSET_SS)); } else { - MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); + MMIO_REG_VAL_FROM_BASE((io_reg_t)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); } } static inline __attribute__((always_inline)) -void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +void directModeOutput(volatile io_reg_t *base, io_reg_t pin) { if (SS_GPIO == GPIO_TYPE(pin)) { - WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), - ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + WRITE_ARC_REG(READ_ARC_REG(((io_reg_t)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), + ((io_reg_t)(base) + DIR_OFFSET_SS)); } else { - MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); + MMIO_REG_VAL_FROM_BASE((io_reg_t)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); } } static inline __attribute__((always_inline)) -void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +void directWriteLow(volatile io_reg_t *base, io_reg_t pin) { if (SS_GPIO == GPIO_TYPE(pin)) { WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base); @@ -220,7 +220,7 @@ void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) } static inline __attribute__((always_inline)) -void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +void directWriteHigh(volatile io_reg_t *base, io_reg_t pin) { if (SS_GPIO == GPIO_TYPE(pin)) { WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base); From 9fa00e44e8ef3b8766e817f0f9b390d31028e007 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 14:41:11 +0200 Subject: [PATCH 58/71] further extend travis tests --- .travis.yml | 4 ++-- readme.md | 5 +++-- src/OneWire.cpp | 4 ++-- src/platform.h | 17 ++++++++++++++--- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index e40a230..e9e17da 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,6 +36,6 @@ script: #elif defined(NRF52) /* arduino primo */ #elif defined(NRF51) /* red bear blend, should be good for all nrf51x chips */ -#esp32 -#stm32 +#esp32 https://github.com/espressif/arduino-esp32 +#stm32 https://github.com/rogerclarkmelbourne/Arduino_STM32 #msp430 diff --git a/readme.md b/readme.md index 7cf997a..3442cc7 100644 --- a/readme.md +++ b/readme.md @@ -31,7 +31,7 @@ OneWire Master Library - nodeMCU V2 ([esp8266](https://github.com/esp8266/Arduino)) - espduino ([esp8266](https://github.com/esp8266/Arduino)) - RFduino nrf51 ([RFduino](https://github.com/RFduino/RFduino)) - - ATtiny 84 / 88 ([attiny](https://github.com/damellis/attiny)) + - ATtiny 84 / 88 ([attiny](https://github.com/damellis/attiny)??) - Digispark tiny ([DigistumpArduino](https://github.com/digistump/DigistumpArduino)) - run as ds18x20-master: - Arduino Uno ([Arduino AVR Boards](https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr)) @@ -40,7 +40,7 @@ OneWire Master Library - compile process passes: - Arduino Primo ([Arduino nRF52 Boards](https://github.com/arduino-org/arduino-core-nrf52)) - untested - - [pic32](https://github.com/chipKIT32/chipKIT-core) + - [pic32](https://github.com/chipKIT32/chipKIT-core) -> platformIO has an error - RedBear [nRF51](https://github.com/RedBearLab/nRF51822-Arduino) ### How to use @@ -78,6 +78,7 @@ OneWire Master Library ### Development history Version 3.0 +- extend and fix hardware support (special esp-boards, ...) - added travis CI - delayMicroseconds() hardware abstraction to allow proper nRF51-use (redbear messed up the delaymicroseconds()-implementation) - will be removed as soon redbear fixes its code diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 94bddeb..fab78f1 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -439,7 +439,7 @@ uint8_t OneWire::crc8(const uint8_t data_array[], uint8_t data_size, const uint8 while (data_size-- > 0) // clean solution with for(index) seems to be 6 byte larger { -#if defined(__AVR__) +#if defined(__AVR__) && !defined(TEENSYDUINO) crc = _crc_ibutton_update(crc, *data_array++); #else uint8_t inByte = *data_array++; @@ -466,7 +466,7 @@ uint16_t OneWire::crc16(const uint8_t data_array[], uint16_t data_size, const ui { uint16_t crc = crc_init; // init value -#if defined(__AVR__) +#if defined(__AVR__) && !defined(TEENSYDUINO) while (data_size-- > 0) { crc = _crc16_update(crc, *data_array++); diff --git a/src/platform.h b/src/platform.h index 4e6e4dd..ec8ef24 100644 --- a/src/platform.h +++ b/src/platform.h @@ -134,9 +134,9 @@ using io_reg_t = uint32_t; // define special data type for register-access #define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ // using io_reg_t = uint32_t; // define special data type for register-access -#elif defined(NRF51) || defined(__RFduino__) /* red bear blend, should be good for all nrf51x chips */ +#elif defined(NRF51) /* red bear blend, should be good for all nrf51x chips */ -#if defined(TARGET_NRF51822) || defined(__RFduino__) +#if defined(TARGET_NRF51822) #include #endif @@ -151,6 +151,18 @@ using io_reg_t = uint32_t; // define special data type for register-access #define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ // using io_reg_t = uint32_t; // define special data type for register-access +#elif defined(__RFduino__) /* rf51 chip with special implementation */ + +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#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) +#define DELAY_MICROSECONDS(us) delayMicroseconds(us) +using io_reg_t = uint32_t; // define special data type for register-access + #elif defined(__arc__) || defined(__ARDUINO_ARC__) /* Arduino101/Genuino101 specifics */ #include "scss_registers.h" @@ -168,7 +180,6 @@ using io_reg_t = uint32_t; // define special data type for register-access /* GPIO registers base address */ #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) #define PIN_TO_BITMASK(pin) pin -//#define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ using io_reg_t = uint32_t; // define special data type for register-access #if ONEWIRE_USE_PULL_UP From 0348cf2aa984f8a245ff86c54360a75e749cda66 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 15:19:48 +0200 Subject: [PATCH 59/71] teensy2 does not compile OW in platformIO --- .travis.yml | 9 ++------- readme.md | 16 ++++++++++++++-- src/OneWire.cpp | 4 ++-- src/platform.h | 3 ++- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index e9e17da..d8f4cb2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,10 +17,11 @@ env: - PIO_BRD: "'-b due'" # arduino due - PIO_BRD: "'-b zero'" # arduino zero - PIO_BRD: "'-b teensy30 -b teensy31 -b teensy35 -b teensy36'" # teensy 3 - - PIO_BRD: "'-b teensy20 -b teensy20pp'" # teensy 2 +# - PIO_BRD: "'-b teensy20 -b teensy20pp'" # teensy 2 - PIO_BRD: "'-b esp01 -b nodemcuv2 -b espduino'" # esp8266 - PIO_BRD: "'-b rfduino'" # nrf51 # - PIO_BRD: "'-b lenny'" # pic32mx + - PIO_BRD: "'-b uno_pic32'" # pic32mx - PIO_BRD: "'-b genuino101'" # intel curie - PIO_BRD: "'-b attiny88 -b attiny84'" # attiny - PIO_BRD: "'-b digispark-tiny'" # attiny @@ -33,9 +34,3 @@ script: - platformio ci --lib="." $PIO_BRD ./examples/DS2408_Switch/DS2408_Switch.ino - platformio ci --lib="." $PIO_BRD ./examples/DS250x_PROM/DS250x_PROM.ino - platformio ci --lib="." $PIO_BRD ./examples/DS2438_BatMon/DS2438_BatMon.ino - -#elif defined(NRF52) /* arduino primo */ -#elif defined(NRF51) /* red bear blend, should be good for all nrf51x chips */ -#esp32 https://github.com/espressif/arduino-esp32 -#stm32 https://github.com/rogerclarkmelbourne/Arduino_STM32 -#msp430 diff --git a/readme.md b/readme.md index 3442cc7..5aec163 100644 --- a/readme.md +++ b/readme.md @@ -38,7 +38,10 @@ OneWire Master Library - Teensy 3.2 ([teensyduino](https://github.com/PaulStoffregen/cores)) - tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets - compile process passes: - - Arduino Primo ([Arduino nRF52 Boards](https://github.com/arduino-org/arduino-core-nrf52)) + - Arduino Primo ([Arduino nRF52 Boards](https://github.com/arduino-org/arduino-core-nrf52)) + - Teensy 2.0 ([teensyduino](https://github.com/PaulStoffregen/cores)) + - Teensy 2.0++ ([teensyduino](https://github.com/PaulStoffregen/cores)) + - tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets - untested - [pic32](https://github.com/chipKIT32/chipKIT-core) -> platformIO has an error - RedBear [nRF51](https://github.com/RedBearLab/nRF51822-Arduino) @@ -74,7 +77,16 @@ OneWire Master Library - predefine standard onewire commands and use send(), skip() is not needed then - unit tests - overdrive and other features (alarm search) -- add stm32f4 support: https://github.com/arduino-org/arduino-core-stm32f4 +- add or extend hardware support + - stm32f4 https://github.com/arduino-org/arduino-core-stm32f4 + - esp32 https://github.com/espressif/arduino-esp32 + - stm32 https://github.com/rogerclarkmelbourne/Arduino_STM32 + - msp430 E https://github.com/energia/Energia + - msp430 arduino, platformIO speaks of a different framework +- add travis tests, currently not supported + - NRF52 /* arduino primo */ + - NRF51 /* red bear blend, should be good for all nrf51x chips */ + - pic32 -> problem with toolchain in platformIO ### Development history Version 3.0 diff --git a/src/OneWire.cpp b/src/OneWire.cpp index fab78f1..94bddeb 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -439,7 +439,7 @@ uint8_t OneWire::crc8(const uint8_t data_array[], uint8_t data_size, const uint8 while (data_size-- > 0) // clean solution with for(index) seems to be 6 byte larger { -#if defined(__AVR__) && !defined(TEENSYDUINO) +#if defined(__AVR__) crc = _crc_ibutton_update(crc, *data_array++); #else uint8_t inByte = *data_array++; @@ -466,7 +466,7 @@ uint16_t OneWire::crc16(const uint8_t data_array[], uint16_t data_size, const ui { uint16_t crc = crc_init; // init value -#if defined(__AVR__) && !defined(TEENSYDUINO) +#if defined(__AVR__) while (data_size-- > 0) { crc = _crc16_update(crc, *data_array++); diff --git a/src/platform.h b/src/platform.h index ec8ef24..12691df 100644 --- a/src/platform.h +++ b/src/platform.h @@ -161,7 +161,8 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DIRECT_MODE_INPUT(base, pin) pinMode(pin,INPUT) #define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT) #define DELAY_MICROSECONDS(us) delayMicroseconds(us) -using io_reg_t = uint32_t; // define special data type for register-access +#define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ +//using io_reg_t = uint32_t; // define special data type for register-access #elif defined(__arc__) || defined(__ARDUINO_ARC__) /* Arduino101/Genuino101 specifics */ From fbbf75c68d68b08844012b16ba2f89ae3a54a7e8 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 15:56:41 +0200 Subject: [PATCH 60/71] disable pic32 travis run --- .travis.yml | 3 +-- readme.md | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index d8f4cb2..ad4a980 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,8 +20,7 @@ env: # - PIO_BRD: "'-b teensy20 -b teensy20pp'" # teensy 2 - PIO_BRD: "'-b esp01 -b nodemcuv2 -b espduino'" # esp8266 - PIO_BRD: "'-b rfduino'" # nrf51 -# - PIO_BRD: "'-b lenny'" # pic32mx - - PIO_BRD: "'-b uno_pic32'" # pic32mx +# - PIO_BRD: "'-b lenny -b uno_pic32'" # pic32mx -> error: pic32-g++ not found - PIO_BRD: "'-b genuino101'" # intel curie - PIO_BRD: "'-b attiny88 -b attiny84'" # attiny - PIO_BRD: "'-b digispark-tiny'" # attiny diff --git a/readme.md b/readme.md index 5aec163..63eec04 100644 --- a/readme.md +++ b/readme.md @@ -16,7 +16,7 @@ OneWire Master Library - improved documentation in doxygen style - decided code style - Allman braces - was closest to main style in original -### Continues Integration and additional tests +### Supported Hardware: Continuous Integration and additional tests - Travis CI for different platforms - Arduino Uno ([Arduino AVR Boards](https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr)) - Arduino Due ([Arduino SAMD Boards (32-bits ARM Cortex-M3)](https://github.com/arduino/ArduinoCore-sam)) @@ -33,18 +33,17 @@ OneWire Master Library - RFduino nrf51 ([RFduino](https://github.com/RFduino/RFduino)) - ATtiny 84 / 88 ([attiny](https://github.com/damellis/attiny)??) - Digispark tiny ([DigistumpArduino](https://github.com/digistump/DigistumpArduino)) -- run as ds18x20-master: +- embedded test: run as ds18x20-master - Arduino Uno ([Arduino AVR Boards](https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr)) - Teensy 3.2 ([teensyduino](https://github.com/PaulStoffregen/cores)) - tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets -- compile process passes: +- compile test: process passes - Arduino Primo ([Arduino nRF52 Boards](https://github.com/arduino-org/arduino-core-nrf52)) - Teensy 2.0 ([teensyduino](https://github.com/PaulStoffregen/cores)) - Teensy 2.0++ ([teensyduino](https://github.com/PaulStoffregen/cores)) - - tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets -- untested - - [pic32](https://github.com/chipKIT32/chipKIT-core) -> platformIO has an error + - Digilent chipKIT UNO32 [pic32](https://github.com/chipKIT32/chipKIT-core) - RedBear [nRF51](https://github.com/RedBearLab/nRF51822-Arduino) + - tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets ### How to use - understanding [onewire protocol](https://en.wikipedia.org/wiki/1-Wire) helps a lot during "how to" @@ -73,24 +72,25 @@ OneWire Master Library ### Plans for the future - update the interface, NOTE: these changes can be made without breaking old code, just use "deprecated" on old interface - - simplify bus powering as extra argument for reset and read as well, OR we set it on bus constructor - - predefine standard onewire commands and use send(), skip() is not needed then + - simplify bus powering as extra argument for reset and read as well, OR we set it on bus constructor + - predefine standard onewire commands and use send(), skip() is not needed then - unit tests - overdrive and other features (alarm search) - add or extend hardware support - - stm32f4 https://github.com/arduino-org/arduino-core-stm32f4 - - esp32 https://github.com/espressif/arduino-esp32 - - stm32 https://github.com/rogerclarkmelbourne/Arduino_STM32 - - msp430 E https://github.com/energia/Energia - - msp430 arduino, platformIO speaks of a different framework + - stm32f4 https://github.com/arduino-org/arduino-core-stm32f4 + - esp32 https://github.com/espressif/arduino-esp32 + - stm32 https://github.com/rogerclarkmelbourne/Arduino_STM32 + - msp430 E https://github.com/energia/Energia + - msp430 arduino, platformIO speaks of a different framework - add travis tests, currently not supported - NRF52 /* arduino primo */ - NRF51 /* red bear blend, should be good for all nrf51x chips */ - pic32 -> problem with toolchain in platformIO + - teensy 2 -> crc-lib has linker problem in platformIO ### Development history Version 3.0 -- extend and fix hardware support (special esp-boards, ...) +- extend and fix hardware support (specially esp-boards, ...) - added travis CI - delayMicroseconds() hardware abstraction to allow proper nRF51-use (redbear messed up the delaymicroseconds()-implementation) - will be removed as soon redbear fixes its code From 41bc883fc8e60adc01ada3ca7770278782c04a02 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 29 Jul 2017 21:38:44 +0200 Subject: [PATCH 61/71] improve doc --- readme.md | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/readme.md b/readme.md index 63eec04..f3047a3 100644 --- a/readme.md +++ b/readme.md @@ -16,22 +16,18 @@ OneWire Master Library - improved documentation in doxygen style - decided code style - Allman braces - was closest to main style in original -### Supported Hardware: Continuous Integration and additional tests -- Travis CI for different platforms +### Supported Hardware: +- Travis CI (Continuous Integration) for different platforms - Arduino Uno ([Arduino AVR Boards](https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr)) - Arduino Due ([Arduino SAMD Boards (32-bits ARM Cortex-M3)](https://github.com/arduino/ArduinoCore-sam)) - Arduino Zero ([Arduino SAMD Boards (32-bits ARM Cortex-M0+)](https://github.com/arduino/ArduinoCore-samd)) - Arduino 101 ([Intel Curie Boards](https://github.com/01org/corelibs-arduino101)) - - Teensy 3.0 ([teensyduino](https://github.com/PaulStoffregen/cores)) - - Teensy 3.1 / 3.2 ([teensyduino](https://github.com/PaulStoffregen/cores)) - - Teensy 3.5 ([teensyduino](https://github.com/PaulStoffregen/cores)) - - Teensy LC ([teensyduino](https://github.com/PaulStoffregen/cores)) - - Teensy 3.6 ([teensyduino](https://github.com/PaulStoffregen/cores)) + - Teensy 3.0, 3.1, 3.2, LC, 3.5, 3.6 ([teensyduino](https://github.com/PaulStoffregen/cores)) - generic ESP8266 ([esp8266](https://github.com/esp8266/Arduino)) - nodeMCU V2 ([esp8266](https://github.com/esp8266/Arduino)) - espduino ([esp8266](https://github.com/esp8266/Arduino)) - RFduino nrf51 ([RFduino](https://github.com/RFduino/RFduino)) - - ATtiny 84 / 88 ([attiny](https://github.com/damellis/attiny)??) + - ATtiny 84, 88 ([attiny](https://github.com/damellis/attiny)??) - Digispark tiny ([DigistumpArduino](https://github.com/digistump/DigistumpArduino)) - embedded test: run as ds18x20-master - Arduino Uno ([Arduino AVR Boards](https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr)) @@ -39,8 +35,7 @@ OneWire Master Library - tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets - compile test: process passes - Arduino Primo ([Arduino nRF52 Boards](https://github.com/arduino-org/arduino-core-nrf52)) - - Teensy 2.0 ([teensyduino](https://github.com/PaulStoffregen/cores)) - - Teensy 2.0++ ([teensyduino](https://github.com/PaulStoffregen/cores)) + - Teensy 2.0, 2.0++ ([teensyduino](https://github.com/PaulStoffregen/cores)) - Digilent chipKIT UNO32 [pic32](https://github.com/chipKIT32/chipKIT-core) - RedBear [nRF51](https://github.com/RedBearLab/nRF51822-Arduino) - tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets From 330e8bdcb77d3a2a2456ab5fc2d94b56abc0947a Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 19 Aug 2017 19:07:29 +0200 Subject: [PATCH 62/71] reflect tested hardware in documentation --- .travis.yml | 9 +++++---- readme.md | 50 ++++++++++++++++++++++++++-------------------- src/OneWire.h | 6 ++---- src/platform.h | 54 ++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 79 insertions(+), 40 deletions(-) diff --git a/.travis.yml b/.travis.yml index ad4a980..3fd5750 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,11 +19,12 @@ env: - PIO_BRD: "'-b teensy30 -b teensy31 -b teensy35 -b teensy36'" # teensy 3 # - PIO_BRD: "'-b teensy20 -b teensy20pp'" # teensy 2 - PIO_BRD: "'-b esp01 -b nodemcuv2 -b espduino'" # esp8266 - - PIO_BRD: "'-b rfduino'" # nrf51 -# - PIO_BRD: "'-b lenny -b uno_pic32'" # pic32mx -> error: pic32-g++ not found - - PIO_BRD: "'-b genuino101'" # intel curie + - PIO_BRD: "'-b espino32 -b esp32dev'" # esp8266 + - PIO_BRD: "'-b rfduino'" # nrf51 +# - PIO_BRD: "'-b lenny -b uno_pic32'" # pic32mx -> error: pic32-g++ not found + - PIO_BRD: "'-b genuino101'" # intel curie - PIO_BRD: "'-b attiny88 -b attiny84'" # attiny - - PIO_BRD: "'-b digispark-tiny'" # attiny + - PIO_BRD: "'-b digispark-tiny'" # attiny before_script: - platformio init $PIO_BRD diff --git a/readme.md b/readme.md index f3047a3..7b3a202 100644 --- a/readme.md +++ b/readme.md @@ -4,20 +4,29 @@ OneWire Master Library [![Build Status](https://travis-ci.org/orgua/OneWire.svg?branch=master)](https://travis-ci.org/orgua/OneWire) ### Features -- emulate a bus master in a generic way, the lib is handling most of the timing-constrains and the hardware-access -- clean, small, fast code with c++11 features (requires arduino sw 1.6.x or higher, >=1.6.10 recommended) +- emulates a bus master in a generic way, the lib is handling most of the timing-constrains and the hardware-access +- clean, small, fast code with c++11 features (requires arduino sw 1.6.x or higher) - proper naming of variables make it easier to read the code -- hardware-dependencies are combined in "platform.h" - - supported: arduino uno / zero / due / primo, teensy, sam3x, pic32, ATtiny, esp8266, nrf51822, raspberry (...) - - for portability and tests the hub can be compiled on a PC with the supplied mock-up functions in platform.h -- extended and better understandable examples +- hardware-dependencies are combined in "platform.h" (see next section) + - for portability and tests the lib can be compiled on a PC with the supplied mock-up functions in platform.h +- extended and easy understandable examples - command to enable or disable parasitic bus powering by the master (was implemented wrong before) - compiler-switch to use pull-ups of micro controller - improved documentation in doxygen style -- decided code style - Allman braces - was closest to main style in original +- decided on one code style - Allman braces - was closest to main style in original -### Supported Hardware: -- Travis CI (Continuous Integration) for different platforms +### Supported and tested Hardware +- embedded real life test + - setup: run ds18x20-master, enabled internal pull up (if possible), arduino 1.8.3, Windows 10 and the board-library named in the brackets + - Arduino Uno ([Arduino AVR Boards](https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr)) + - Arduino Due ([Arduino SAMD Boards (32-bits ARM Cortex-M3)](https://github.com/arduino/ArduinoCore-sam)) -> NOTE: internal PU is not strong enough to power bus + - Arduino MKRZero ([Arduino SAMD Boards (32-bits ARM Cortex-M0+)](https://github.com/arduino/ArduinoCore-samd)) -> NOTE: internal PU is not strong enough to power bus + - Teensy 3.2 ([teensyduino](https://github.com/PaulStoffregen/cores)) + - Wemos D1 Mini ESP32S ([esp32](https://github.com/espressif/arduino-esp32)) -> NOTE: internal PU is not strong enough to power bus + - Wemos Wifi & BT ESP32 ([esp32](https://github.com/espressif/arduino-esp32)) -> NOTE: internal PU is not strong enough to power bus + - Wemos D1 R2 ([esp8266](https://github.com/esp8266/Arduino)) + - nodeMCU 1.0 ESP-12E ([esp8266](https://github.com/esp8266/Arduino)) +- Travis CI (automated Continuous Integration) for different platforms - Arduino Uno ([Arduino AVR Boards](https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr)) - Arduino Due ([Arduino SAMD Boards (32-bits ARM Cortex-M3)](https://github.com/arduino/ArduinoCore-sam)) - Arduino Zero ([Arduino SAMD Boards (32-bits ARM Cortex-M0+)](https://github.com/arduino/ArduinoCore-samd)) @@ -26,19 +35,18 @@ OneWire Master Library - generic ESP8266 ([esp8266](https://github.com/esp8266/Arduino)) - nodeMCU V2 ([esp8266](https://github.com/esp8266/Arduino)) - espduino ([esp8266](https://github.com/esp8266/Arduino)) + - ESP32 dev module ([esp32](https://github.com/espressif/arduino-esp32)) - RFduino nrf51 ([RFduino](https://github.com/RFduino/RFduino)) - ATtiny 84, 88 ([attiny](https://github.com/damellis/attiny)??) - Digispark tiny ([DigistumpArduino](https://github.com/digistump/DigistumpArduino)) -- embedded test: run as ds18x20-master - - Arduino Uno ([Arduino AVR Boards](https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr)) - - Teensy 3.2 ([teensyduino](https://github.com/PaulStoffregen/cores)) - - tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets -- compile test: process passes +- manual compile test: process passes + - setup: run ds18x20-master, arduino 1.8.3, Windows 10 and the board-library named in the brackets - Arduino Primo ([Arduino nRF52 Boards](https://github.com/arduino-org/arduino-core-nrf52)) - Teensy 2.0, 2.0++ ([teensyduino](https://github.com/PaulStoffregen/cores)) - Digilent chipKIT UNO32 [pic32](https://github.com/chipKIT32/chipKIT-core) - RedBear [nRF51](https://github.com/RedBearLab/nRF51822-Arduino) - - tested with arduino 1.8.3, Windows 10 and the board-library named in the brackets +- if you successfully tested a new controller don't hesitate to open an issue on github to document it +- if you modified / extended the lib to support new hardware don't hesitate to open a pull request on github ### How to use - understanding [onewire protocol](https://en.wikipedia.org/wiki/1-Wire) helps a lot during "how to" @@ -73,7 +81,6 @@ OneWire Master Library - overdrive and other features (alarm search) - add or extend hardware support - stm32f4 https://github.com/arduino-org/arduino-core-stm32f4 - - esp32 https://github.com/espressif/arduino-esp32 - stm32 https://github.com/rogerclarkmelbourne/Arduino_STM32 - msp430 E https://github.com/energia/Energia - msp430 arduino, platformIO speaks of a different framework @@ -85,13 +92,13 @@ OneWire Master Library ### Development history Version 3.0 -- extend and fix hardware support (specially esp-boards, ...) +- extend and fix hardware support (specially esp-boards, esp32, ...) - added travis CI - delayMicroseconds() hardware abstraction to allow proper nRF51-use (redbear messed up the delaymicroseconds()-implementation) - will be removed as soon redbear fixes its code - overload write() to replace write_bytes() and read() to replace read_byte() -- update documentation, doxygen style -- allow to use internal pull-up if micro controller has support for it -> most µC support INPUT_PULLUP in combination with pinMode(), others will get an errormessage if feature is enabled but not supported / tested +- update code documentation, doxygen style +- allow to use internal pull-up if micro controller has support for it -> most µC support INPUT_PULLUP in combination with pinMode(), others will get an error message or warning if feature is enabled but not supported / tested - clean up compiler-switches - better implementation to allow parasitic power to bus, otherwise master will act as open-drain-only! -> this was totally wrong before as the bus was powered for a short amount of time even if powering is disabled - extend examples with ds2438 and a bus-discovery feature @@ -100,9 +107,10 @@ Version 3.0 - better naming of variables and a lot of code cleanup - simplify pin-access and interrupt-handling - improve onewire class-interface (it was possible to copy the object, ...) -- reduce size of data types if possible, more const correctness and more explicit code +- reduce size of data types if possible, more const correctness and much more explicit code - get rid of most ancient c and cpp code and reduce language mix - decide on one code style - Allman braces - was closest to main style in original +- a lot of how-to documentation for this lib inside this readme Version 2.3 - Unknown chip fallback mode, Roger Clark @@ -147,7 +155,7 @@ Version before 2.0 ### Connecting the master with the bus: -- the shown picture does not show the power line (assumes parasitic powering) +- the picture does not show the power line (assumes parasitic powering) - by activating the internal pull up of the microcontroller you most likely do not need external PU ![Onewire-Schematic](http://wiki.lvl1.org/images/1/15/Onewire.gif) diff --git a/src/OneWire.h b/src/OneWire.h index c622567..d0cf85d 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -5,8 +5,6 @@ #include // for delayMicroseconds, digitalPinToBitMask, etc #endif -#include "platform.h" - // 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 @@ -15,7 +13,7 @@ #define ONEWIRE_USE_CRC8_TABLE 1 // micro controller feature to use internal pull-ups -// NOTE: will throw compiler-error if feature is not yet implemented or tested for your controller +// NOTE: may work, depends on controler and bus-load -> analyse signal traces with scope for reassurance #define ONEWIRE_USE_PULL_UP 0 // to make the bus more secure you can disable parasitic bus powering @@ -24,7 +22,7 @@ // NOTE: you need to power the devices yourself, because the external // PU-resistor might not do it completely - +#include "platform.h" class OneWire { diff --git a/src/platform.h b/src/platform.h index 12691df..2a864dd 100644 --- a/src/platform.h +++ b/src/platform.h @@ -77,6 +77,10 @@ using io_reg_t = uint8_t; // define special data type for register-access #endif using io_reg_t = uint32_t; // define special data type for register-access +#if ONEWIRE_USE_PULL_UP +#warning "Internal pull up of controller is NOT strong enough to power the OW-Bus!" +#endif + #elif defined(__PIC32MX__) #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) @@ -89,10 +93,6 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint32_t; // define special data type for register-access -#if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented or tested for your microcontroller" -#endif - #elif defined(ARDUINO_ARCH_ESP8266) /* nodeMCU, ESPduino, ... */ // Special note: I depend on the ESP community to maintain these definitions and // submit good pull requests. I can not answer any ESP questions or help you @@ -109,6 +109,22 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint32_t; // define special data type for register-access +#elif defined(ARDUINO_ARCH_ESP32) || defined(ESP32) + +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#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) +#define DELAY_MICROSECONDS(us) delayMicroseconds(us) +using io_reg_t = uint32_t; // define special data type for register-access + +#if ONEWIRE_USE_PULL_UP +#warning "Internal pull up of controller is NOT strong enough to power the OW-Bus!" +#endif + #elif defined(__SAMD21G18A__) /* arduino zero */ #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) @@ -121,6 +137,10 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint32_t; // define special data type for register-access +#if ONEWIRE_USE_PULL_UP +#warning "Internal pull up of controller is NOT strong enough to power the OW-Bus!" +#endif + #elif defined(NRF52) /* arduino primo */ #define PIN_TO_BASEREG(pin) (0) @@ -128,11 +148,17 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin) #define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin) #define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) + +#if ONEWIRE_USE_PULL_UP +#define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_PULLUP) +#else #define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) +#endif + #define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) #define DELAY_MICROSECONDS(us) delayMicroseconds(us) +//using io_reg_t = uint32_t; // define special data type for register-access #define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ -// using io_reg_t = uint32_t; // define special data type for register-access #elif defined(NRF51) /* red bear blend, should be good for all nrf51x chips */ @@ -145,11 +171,17 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin) #define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin) #define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) + +#if ONEWIRE_USE_PULL_UP +#define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_PULLUP) +#else #define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) +#endif + #define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) #define DELAY_MICROSECONDS(us) nrf_delay_us(us) // TODO: only needed because of faulty redbear-delay()-implementation +//using io_reg_t = uint32_t; // define special data type for register-access #define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ -// using io_reg_t = uint32_t; // define special data type for register-access #elif defined(__RFduino__) /* rf51 chip with special implementation */ @@ -161,8 +193,8 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DIRECT_MODE_INPUT(base, pin) pinMode(pin,INPUT) #define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT) #define DELAY_MICROSECONDS(us) delayMicroseconds(us) -#define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ //using io_reg_t = uint32_t; // define special data type for register-access +#define io_reg_t uint32_t /* TODO: the tool chain is old .... check for updates, last 2017-07 */ #elif defined(__arc__) || defined(__ARDUINO_ARC__) /* Arduino101/Genuino101 specifics */ @@ -183,10 +215,6 @@ using io_reg_t = uint32_t; // define special data type for register-access #define PIN_TO_BITMASK(pin) pin using io_reg_t = uint32_t; // define special data type for register-access -#if ONEWIRE_USE_PULL_UP -#error "PULL UP feature is not yet implemented or tested for your microcontroller" -#endif - static inline __attribute__((always_inline)) io_reg_t directRead(volatile io_reg_t *base, io_reg_t pin) { @@ -264,6 +292,10 @@ using io_reg_t = uint32_t; // define special data type for register-access #warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture." +#if ONEWIRE_USE_PULL_UP +#warning "Internal pull up of controller is NOT strong enough to power the OW-Bus!" +#endif + #endif #ifndef ARDUINO From 1b77038c330fc8c07332cc7778123d27064e88bb Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 19 Aug 2017 19:16:12 +0200 Subject: [PATCH 63/71] prepare release --- library.json | 2 +- library.properties | 2 +- readme.md | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/library.json b/library.json index 1e95e2e..231d487 100644 --- a/library.json +++ b/library.json @@ -52,7 +52,7 @@ "type": "git", "url": "https://github.com/PaulStoffregen/OneWire" }, - "version": "2.3.2", + "version": "3.0.1", "homepage": "https://www.pjrc.com/teensy/td_libs_OneWire.html", "frameworks": "Arduino", "examples": [ diff --git a/library.properties b/library.properties index 810ff38..06493db 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=OneWire -version=3.0.0 +version=3.0.1 author=Jim Studt, Tom Pollard, Robin James, Glenn Trewitt, Jason Dangel, Guillermo Lovato, Paul Stoffregen, Scott Roberts, Bertrik Sikken, Mark Tillotson, Ken Butcher, Roger Clark, Love Nystrom maintainer=Paul Stoffregen sentence=Access 1-wire temperature sensors, memory and other chips. diff --git a/readme.md b/readme.md index 7b3a202..4ac24dd 100644 --- a/readme.md +++ b/readme.md @@ -92,8 +92,9 @@ OneWire Master Library ### Development history Version 3.0 +- real life tested with several platforms (atmega, atsamD21, Atsam3x, esp8266, esp32, teensy) - extend and fix hardware support (specially esp-boards, esp32, ...) -- added travis CI +- added travis CI for a big bunch of controllers - delayMicroseconds() hardware abstraction to allow proper nRF51-use (redbear messed up the delaymicroseconds()-implementation) - will be removed as soon redbear fixes its code - overload write() to replace write_bytes() and read() to replace read_byte() From 561d2f537db35227fce54651785570554d536483 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 19 Aug 2017 19:22:02 +0200 Subject: [PATCH 64/71] add todo --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index 4ac24dd..fdd6196 100644 --- a/readme.md +++ b/readme.md @@ -78,6 +78,7 @@ OneWire Master Library - simplify bus powering as extra argument for reset and read as well, OR we set it on bus constructor - predefine standard onewire commands and use send(), skip() is not needed then - unit tests +- collect timings in variables - overdrive and other features (alarm search) - add or extend hardware support - stm32f4 https://github.com/arduino-org/arduino-core-stm32f4 From d2e4da1c510c69b08dabf819b716213f9934e3f3 Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 19 Aug 2017 20:24:14 +0200 Subject: [PATCH 65/71] proper handle pullup-wish if platform-code touches this setting --- src/platform.h | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/platform.h b/src/platform.h index 2a864dd..9b71fa6 100644 --- a/src/platform.h +++ b/src/platform.h @@ -116,7 +116,13 @@ using io_reg_t = uint32_t; // define special data type for register-access #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) + +#if ONEWIRE_USE_PULL_UP +#define DIRECT_MODE_INPUT(base, pin) pinMode(pin, INPUT_PULLUP) +#else +#define DIRECT_MODE_INPUT(base, pin) pinMode(pin, INPUT) +#endif + #define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT) #define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint32_t; // define special data type for register-access @@ -190,7 +196,13 @@ using io_reg_t = uint32_t; // define special data type for register-access #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) + +#if ONEWIRE_USE_PULL_UP +#define DIRECT_MODE_INPUT(base, pin) pinMode(pin, INPUT_PULLUP) +#else +#define DIRECT_MODE_INPUT(base, pin) pinMode(pin, INPUT) +#endif + #define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT) #define DELAY_MICROSECONDS(us) delayMicroseconds(us) //using io_reg_t = uint32_t; // define special data type for register-access @@ -285,7 +297,13 @@ void directWriteHigh(volatile io_reg_t *base, io_reg_t pin) #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) + +#if ONEWIRE_USE_PULL_UP +#define DIRECT_MODE_INPUT(base, pin) pinMode(pin, INPUT_PULLUP) +#else +#define DIRECT_MODE_INPUT(base, pin) pinMode(pin, INPUT) +#endif + #define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT) #define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint32_t; // define special data type for register-access @@ -293,7 +311,7 @@ using io_reg_t = uint32_t; // define special data type for register-access #warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture." #if ONEWIRE_USE_PULL_UP -#warning "Internal pull up of controller is NOT strong enough to power the OW-Bus!" +#warning "Internal pull up of controller is probably not strong enough to power the OW-Bus!" #endif #endif From 78dae78d8d56edee110e517340f290738619996c Mon Sep 17 00:00:00 2001 From: orgua Date: Sat, 19 Aug 2017 20:28:43 +0200 Subject: [PATCH 66/71] remove unused "search_last_family_discrepancy" from 5186 / 396 byte to 5160 / 395 byte on atmega with ds18x20 example --- src/OneWire.cpp | 7 ------- src/OneWire.h | 3 +-- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/OneWire.cpp b/src/OneWire.cpp index 94bddeb..9f14505 100644 --- a/src/OneWire.cpp +++ b/src/OneWire.cpp @@ -241,7 +241,6 @@ void OneWire::reset_search() // reset the search state search_last_discrepancy = 0; search_last_device_flag = false; - search_last_family_discrepancy = 0; memset(search_rom_array, uint8_t(0), 8); } @@ -252,7 +251,6 @@ void OneWire::target_search(const uint8_t family_code) search_rom_array[0] = family_code; memset(&search_rom_array[1], uint8_t(0), 7); search_last_discrepancy = 64; - search_last_family_discrepancy = 0; search_last_device_flag = false; } @@ -275,7 +273,6 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) // reset the search search_last_discrepancy = 0; search_last_device_flag = false; - search_last_family_discrepancy = 0; return false; } @@ -326,9 +323,6 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) if (!search_direction) { last_zero = id_bit_number; - - // check for Last discrepancy in family - if (last_zero<9) search_last_family_discrepancy = last_zero; } } @@ -379,7 +373,6 @@ bool OneWire::search(uint8_t new_rom_array[], const bool search_mode) { search_last_discrepancy = 0; search_last_device_flag = false; - search_last_family_discrepancy = 0; search_result = false; } else diff --git a/src/OneWire.h b/src/OneWire.h index d0cf85d..8e52acc 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -34,8 +34,7 @@ class OneWire { /// search state uint8_t search_rom_array[8]; uint8_t search_last_discrepancy; - uint8_t search_last_family_discrepancy; - bool search_last_device_flag; + bool search_last_device_flag; public: From 48e978e725da0311dc38f5b3009279c9dea6745a Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 20 Aug 2017 16:09:34 +0200 Subject: [PATCH 67/71] add a todo --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index fdd6196..62d1421 100644 --- a/readme.md +++ b/readme.md @@ -79,6 +79,7 @@ OneWire Master Library - predefine standard onewire commands and use send(), skip() is not needed then - unit tests - collect timings in variables +- add pull up support directly to constructor - overdrive and other features (alarm search) - add or extend hardware support - stm32f4 https://github.com/arduino-org/arduino-core-stm32f4 From 80ccf602dc3267c47db90f1e8227d2c4c90fdd66 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 20 Aug 2017 16:12:05 +0200 Subject: [PATCH 68/71] restructure platform-files --- src/platform.h | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/platform.h b/src/platform.h index 9b71fa6..509f6b1 100644 --- a/src/platform.h +++ b/src/platform.h @@ -3,6 +3,10 @@ #ifndef ONEWIRE_PLATFORM_H #define ONEWIRE_PLATFORM_H +#if defined(ARDUINO) && (ARDUINO>=100) +#include +#endif + // determine gcc version, will produce number like 40803 for gcc 4.8.3 #if defined(__GNUC__) #define ONEWIRE_GCC_VERSION ( (__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__) @@ -10,12 +14,8 @@ #define ONEWIRE_GCC_VERSION 0 #endif -#if defined(ARDUINO) && (ARDUINO>=100) -#include -#endif - #if defined(__AVR__) /* arduino (all with atmega, atiny) */ -#include +#include // TODO tryout if PIO compiles with this next to actual FN, not outsourced #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) #define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) @@ -57,10 +57,7 @@ using io_reg_t = uint8_t; // define special data type for register-access using io_reg_t = uint8_t; // define special data type for register-access #elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__) /* arduino due */ -// 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 DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0) @@ -109,7 +106,7 @@ using io_reg_t = uint32_t; // define special data type for register-access #define DELAY_MICROSECONDS(us) delayMicroseconds(us) using io_reg_t = uint32_t; // define special data type for register-access -#elif defined(ARDUINO_ARCH_ESP32) || defined(ESP32) +#elif defined(ARDUINO_ARCH_ESP32) || defined(ESP32) /* ESP32 Family */ #define PIN_TO_BASEREG(pin) (0) #define PIN_TO_BITMASK(pin) (pin) @@ -316,6 +313,13 @@ using io_reg_t = uint32_t; // define special data type for register-access #endif + + +/////////////////////////////////////////// EXTRA PART ///////////////////////////////////////// +// this part is loaded if no proper arduino-environment is found (good for external testing) +// these used functions are mockups +//////////////////////////////////////////////////////////////////////////////////////////////// + #ifndef ARDUINO #define ONEWIRE_FALLBACK_BASIC_FNs #define ONEWIRE_FALLBACK_ADDITIONAL_FNs // to load up Serial below @@ -327,11 +331,6 @@ using io_reg_t = uint32_t; // define special data type for register-access #ifdef ONEWIRE_FALLBACK_BASIC_FNs -/////////////////////////////////////////// EXTRA PART ///////////////////////////////////////// -// this part is loaded if no proper arduino-environment is found (good for external testing) -// these used functions are mockups -//////////////////////////////////////////////////////////////////////////////////////////////// - #define INPUT 1 #define INPUT_PULLUP 1 #define OUTPUT 0 @@ -383,6 +382,7 @@ T1 pgm_read_byte(const T1* address) #endif + #ifdef ONEWIRE_FALLBACK_ADDITIONAL_FNs // Test to make it work on aTtiny85, 8MHz /// README: use pin2 or pin3 for Attiny, source: https://github.com/gioblu/PJON/wiki/ATtiny-interfacing From 9ec5d851035ee88de1f73bc1291dc884db1f8ff6 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 20 Aug 2017 16:12:21 +0200 Subject: [PATCH 69/71] optimize travis scripts --- .travis.yml | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3fd5750..6ab63a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,23 @@ +os: linux +dist: trusty +group: edge +sudo: false + language: python python: - "2.7" -sudo: false - cache: + pip: true + apt: true directories: - "~/.platformio" -install: +before_cache: + - rm -f $HOME/.cache/pip/log/debug.log + +before_install: - pip install -U platformio env: @@ -17,7 +25,7 @@ env: - PIO_BRD: "'-b due'" # arduino due - PIO_BRD: "'-b zero'" # arduino zero - PIO_BRD: "'-b teensy30 -b teensy31 -b teensy35 -b teensy36'" # teensy 3 -# - PIO_BRD: "'-b teensy20 -b teensy20pp'" # teensy 2 + - PIO_BRD: "'-b teensy20 -b teensy20pp'" # teensy 2 -> crc-lib has linker problem in platformIO - PIO_BRD: "'-b esp01 -b nodemcuv2 -b espduino'" # esp8266 - PIO_BRD: "'-b espino32 -b esp32dev'" # esp8266 - PIO_BRD: "'-b rfduino'" # nrf51 From 0eab87887afffd01ec5041eac9be032e1aeb21e2 Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 20 Aug 2017 16:12:45 +0200 Subject: [PATCH 70/71] try to fix linker error with teensy2 --- src/OneWire.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/OneWire.h b/src/OneWire.h index 8e52acc..28f41e6 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -24,6 +24,9 @@ #include "platform.h" +#if defined(__AVR__) /* arduino (all with atmega, atiny) */ +#include // TODO tryout if PIO compiles with this next to actual FN, not outsourced +#endif class OneWire { private: From bf4e56a9bc8cc6f2054217e0a30fec67d8e403dc Mon Sep 17 00:00:00 2001 From: orgua Date: Sun, 20 Aug 2017 16:40:13 +0200 Subject: [PATCH 71/71] revert not working fix to make teensy2 compile in pio --- .travis.yml | 2 +- src/OneWire.h | 8 -------- src/platform.h | 20 ++++++++++---------- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6ab63a9..24ac428 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,7 +25,7 @@ env: - PIO_BRD: "'-b due'" # arduino due - PIO_BRD: "'-b zero'" # arduino zero - PIO_BRD: "'-b teensy30 -b teensy31 -b teensy35 -b teensy36'" # teensy 3 - - PIO_BRD: "'-b teensy20 -b teensy20pp'" # teensy 2 -> crc-lib has linker problem in platformIO +# - PIO_BRD: "'-b teensy20 -b teensy20pp'" # teensy 2 -> crc-lib has linker problem in platformIO but works in arduino-sw - PIO_BRD: "'-b esp01 -b nodemcuv2 -b espduino'" # esp8266 - PIO_BRD: "'-b espino32 -b esp32dev'" # esp8266 - PIO_BRD: "'-b rfduino'" # nrf51 diff --git a/src/OneWire.h b/src/OneWire.h index 28f41e6..ae17bbc 100644 --- a/src/OneWire.h +++ b/src/OneWire.h @@ -1,10 +1,6 @@ #ifndef OneWire_h #define OneWire_h -#if defined(ARDUINO) && ARDUINO>=100 -#include // for delayMicroseconds, digitalPinToBitMask, etc -#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 @@ -24,10 +20,6 @@ #include "platform.h" -#if defined(__AVR__) /* arduino (all with atmega, atiny) */ -#include // TODO tryout if PIO compiles with this next to actual FN, not outsourced -#endif - class OneWire { private: diff --git a/src/platform.h b/src/platform.h index 509f6b1..870abce 100644 --- a/src/platform.h +++ b/src/platform.h @@ -70,7 +70,7 @@ using io_reg_t = uint8_t; // define special data type for register-access #define PROGMEM #endif #ifndef pgm_read_byte -#define pgm_read_byte(addr) (*(const uint8_t *)(addr)) +#define pgm_read_byte(address) (*(const uint8_t *)(address)) #endif using io_reg_t = uint32_t; // define special data type for register-access @@ -414,8 +414,8 @@ static class serial { template void memset(T1* const address, const T1 initValue, const T2 bytes) { - const T2 iterations = bytes/sizeof(T1); - for (T2 counter = 0; counter void memcpy(T1* const destination, const T1* const source, const T2 bytes) { - const T2 iterations = bytes/sizeof(T1); - for (T2 counter = 0; counter bool memcmp(const T1* const source_A, const T1* const source_B, const T2 bytes) // return true if string is different { - const T2 iterations = bytes/sizeof(T1); - for (T2 counter = 0; counter