energia / Energia

Fork of Arduino for the Texas Instruments LaunchPad's
http://energia.nu
Other
793 stars 673 forks source link

One-Wire Library #65

Closed rei-vilo closed 11 years ago

rei-vilo commented 12 years ago

Add one-wire library

Diecore commented 12 years ago

Should someone port this code? http://www.43oh.com/forum/viewtopic.php?f=9&t=1616

rei-vilo commented 12 years ago

Problem is, I don't own any of those Maxim DS1820, DS18S20, DS18B20 or DS1822 temperature sensors.

rhass commented 12 years ago

Maxim ships free samples of most of their 1-wire products if you request them.

pbrier commented 12 years ago

Anyone working one this issue? I have some 1wire devices here, so I can test (or implement)

rei-vilo commented 12 years ago

I managed to get one-wire working with a DHT22 temperature and humidity sensor, thanks to the help for 43oh forum.

See http://www.43oh.com/forum/viewtopic.php?f=38&t=2795

VenomXNL commented 11 years ago

Is this lib/function also usable for the DS1820 series or will it be in the (near) future? because I've tried almost every mode/setting and pinout (1 wire, 2 wires, 3 wires [Vcc, Data and GND separate] with and without Pull-down resistors) but it seems that there isn't any data coming from the DS, (i know for sure that they are not broken ;-), i have about 20 in stock here and also several versions of the DS, none of them seem respond to this protocol.)

I hope that I'm just doing something wrong, i'm just getting started with microcontrollers like this, but I've been a professional (certified) programmer for over 10 years now (full .NET developer in C#, VB etc) so the logics are no problem for me i only need a little guidance to accomplish this. I'm just working on the microcontrollers for two days now and already developed sketches with Rotary Encoders, Serial Connections (PC/MAX232), Graphical display control etc. so if i'm doing something wrong with this lib and the DS serie.

Thanks in advance

rei-vilo commented 11 years ago

See

JohnRDOrazio commented 9 years ago

Sorry if I continue the topic even though it's been closed... I have successfully used the OneWire library found here http://www.pjrc.com/teensy/td_libs_OneWire.html in the Arduino IDE. I would now like to use it in Energia so that I can port my code fairly easily. I am trying to include the library among the Energia libraries, but there is hardware abstraction layer with I/O definitions / register types that needs to be extended to include the boards that Energia supports. Specifically lines 51-109 in OneWire.h:

// Platform specific I/O definitions

#if defined(__AVR__)
#define PIN_TO_BASEREG(pin)             (portInputRegister(digitalPinToPort(pin)))
#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
#define IO_REG_TYPE uint8_t
#define IO_REG_ASM asm("r30")
#define DIRECT_READ(base, mask)         (((*(base)) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask)   ((*((base)+1)) &= ~(mask))
#define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+1)) |= (mask))
#define DIRECT_WRITE_LOW(base, mask)    ((*((base)+2)) &= ~(mask))
#define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+2)) |= (mask))

#elif defined(__MK20DX128__)
#define PIN_TO_BASEREG(pin)             (portOutputRegister(pin))
#define PIN_TO_BITMASK(pin)             (1)
#define IO_REG_TYPE uint8_t
#define IO_REG_ASM
#define DIRECT_READ(base, mask)         (*((base)+512))
#define DIRECT_MODE_INPUT(base, mask)   (*((base)+640) = 0)
#define DIRECT_MODE_OUTPUT(base, mask)  (*((base)+640) = 1)
#define DIRECT_WRITE_LOW(base, mask)    (*((base)+256) = 1)
#define DIRECT_WRITE_HIGH(base, mask)   (*((base)+128) = 1)

#elif defined(__SAM3X8E__)
// Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due.
// http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268
// If you have trouble with OneWire on Arduino Due, please check the
// status of delayMicroseconds() before reporting a bug in OneWire!
#define PIN_TO_BASEREG(pin)             (&(digitalPinToPort(pin)->PIO_PER))
#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
#define IO_REG_TYPE uint32_t
#define IO_REG_ASM
#define DIRECT_READ(base, mask)         (((*((base)+15)) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask)   ((*((base)+5)) = (mask))
#define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+4)) = (mask))
#define DIRECT_WRITE_LOW(base, mask)    ((*((base)+13)) = (mask))
#define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+12)) = (mask))
#ifndef PROGMEM
#define PROGMEM
#endif
#ifndef pgm_read_byte
#define pgm_read_byte(addr) (*(const uint8_t *)(addr))
#endif

#elif defined(__PIC32MX__)
#define PIN_TO_BASEREG(pin)             (portModeRegister(digitalPinToPort(pin)))
#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
#define IO_REG_TYPE uint32_t
#define IO_REG_ASM
#define DIRECT_READ(base, mask)         (((*(base+4)) & (mask)) ? 1 : 0)  //PORTX + 0x10
#define DIRECT_MODE_INPUT(base, mask)   ((*(base+2)) = (mask))            //TRISXSET + 0x08
#define DIRECT_MODE_OUTPUT(base, mask)  ((*(base+1)) = (mask))            //TRISXCLR + 0x04
#define DIRECT_WRITE_LOW(base, mask)    ((*(base+8+1)) = (mask))          //LATXCLR  + 0x24
#define DIRECT_WRITE_HIGH(base, mask)   ((*(base+8+2)) = (mask))          //LATXSET + 0x28

#else
#error "Please define I/O register types here"
#endif

If there is a reference somewhere I suppose I could try to tackle it, and it could be included here in the repository among the available libraries... If anyone can point me to an example definition of the register types for the boards supported by Energia.

spirilis commented 9 years ago

Best to look for a OneWire lib already ported.

JohnRDOrazio commented 9 years ago

do you know of one? I have found some suggestions on some forums and I can now get the code to compile but I'll have to test it with my MSP430 tomorrow to see if it'll work ok...

#elif defined(__TM4C129XNCZAD__) || defined(__TM4C1294NCPDT__) || defined(__LM4F120H5QR__) || defined(__TM4C123GH6PM__)
#define portMaskedOutputRegister(base, mask) \ ((volatile uint8_t *) (base + (GPIO_O_DATA + (((uint32_t)mask) << 2))))
#define PIN_TO_BASEREG(pin)             (&((uint32_t)portBASERegister(digitalPinToPort(pin))))
#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
#define IO_REG_TYPE uint8_t
#define IO_REG_ASM
#define DIRECT_READ(base, mask)
#define DIRECT_MODE_INPUT(base, mask)
#define DIRECT_MODE_OUTPUT(base, mask)
#define DIRECT_WRITE_LOW(base, mask)  (portMaskedOutputRegister(base, mask) = 0)
#define DIRECT_WRITE_HIGH(base, mask) (portMaskedOutputRegister(base, mask) = 0xFF)

#else
//#error "Please define I/O register types here"
#define PIN_TO_BASEREG(pin)            (0) // dummy not used in fallback mode
#define PIN_TO_BITMASK(pin)            ( pin ) // use for pin # fallback mode
#define IO_REG_TYPE uint32_t
#define IO_REG_ASM                    // not used
#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)
#endif
watkinrt commented 8 years ago

Like Lwangaman, I was unable to find the officially ported version of OneWire for MSP430 in Energia. The above modification to the original OneWire Arduino code seems to work; however, the digital I/O can be optimized a little bit to speed things up:

// Platform specific I/O definitions

#if defined(__AVR__)
#define PIN_TO_BASEREG(pin)             (portInputRegister(digitalPinToPort(pin)))
#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
#define IO_REG_TYPE uint8_t
#define IO_REG_ASM asm("r30")
#define DIRECT_READ(base, mask)         (((*(base)) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask)   ((*((base)+1)) &= ~(mask))
#define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+1)) |= (mask))
#define DIRECT_WRITE_LOW(base, mask)    ((*((base)+2)) &= ~(mask))
#define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+2)) |= (mask))

...

#elif defined(__MSP430__)
#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 
#define DIRECT_READ(base, mask)         (((*(base)) & (mask)) ? 1 : 0)  // PXIN
#define DIRECT_MODE_INPUT(base, mask)   ((*((base)+4)) &= ~(mask))      // PXDIR
#define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+4)) |= (mask))       // PXDIR
#define DIRECT_WRITE_LOW(base, mask)    ((*((base)+2)) &= ~(mask))      // PXOUT
#define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+2)) |= (mask))       // PXOUT

#else
#error "Please define I/O register types here"
#endif

Note that this is very similar to the Arduino (AVR) definition, except for the DIRECT_MODE_INPUT and DIRECT_MODE_OUTPUT definitions as the base address registries (such as P1DIR, P1OUT, P1IN, etc) are ordered differently than for Arduinos.