8 years, 11 months ago.

Advice for OneWire library with I2C and RTOS


I ported the Arduino OneWire and Dallas Temperature Control Library, then discovered a hard fact of life...

1-wire using a digital IO pin suffers with longer cable lengths when you're also trying to get the mbed to do other things at the same time (because of timing tolerances).

So I then did some object orientation on the OneWire library to allow the use of either a DigitalInOut or an I2C interface via the DS2482 1-wire/I2C bridge.

Having done that, I needed to include some RTOS stuff in the I2C part of the library, as I need mutex on a shared I2C port (because I have separate threads for temperature and other port expander devices).

If anybody else wanted to use the OneWire library even with I2C, they might not want the RTOS junk compiled in if they had a dedicated I2C port (or could just put everything on one thread).

So my question is, how should I elegantly allow the exclusion of the RTOS references in my library.

A code snip is as follows:

    if (_mutex != NULL) _mutex->lock();
    _i2c->write(_addr, cmd, 1);
    _i2c->read(_addr, cmd, 1);
    if (_mutex != NULL) _mutex->unlock();


Hi Daniel, I would be very interested in a STM32 lib for the DS2482 1wire/I2C bridge. I'm using DS2482-800 chips which handle several 1-wire networks. This is the only way I found to interface with long/star networks. As a test, I've put 20 temperature sensors at the end of a 100m network cable actionned by one of the 8 bus masters, with no problem. Tens of sensors on each bus master runs well too, some in parasite mode. I'm using it with a Raspberry Pi / OWFS, but I would like to port this to STM32 for Instrumentation/Control application. Thus, I think using I2C to interface with 1wire network is a good idea, as I guess it has less timing constraints in a real time application. Do you think you could share the DS2482 library ? Thank you in advance. NB

posted by Nat Bon 01 Dec 2014

2 Answers

8 years, 11 months ago.

I happened to have the same problem recently, see: http://mbed.org/users/Sissors/code/SimpleDMA/file/c3a84c6c432c/SimpleDMA.h.

Put it in an ifdef RTOS_H block, that should only be included if RTOS is included. Not 100% sure, but it probably only works if you include RTOS before your library.

Thanks Erik, I'll have a play. I've been slightly side-tracked on discovering that my 1-wire longer cable length problems may be down to a power supply issue.

posted by Daniel Peter 01 Jan 2014
8 years, 10 months ago.

How long was your 1-wire bus? I have successfully used a multi-threaded programme with five DS18B20 temperature probes on a bus at least 30 metres long. The cable was nothing special - just four-core unshielded security wire. I used the driver that I modified from Michael Hagberg.

Import libraryDS1820

Dallas / Maxim DS1820 1-Wire library. For communication with multiple DS1820 on a single 1-Wire bus. Also supports DS18S20 and DS18B20. This fork fixes a timing problem I had with latest mbed library; allows immediate return from convert_temperature(); tidies up a few other things, including some documentation errors.

Hi David

As I mentioned in my comment to Erik, this was actually a cheap switched mode power supply injecting noise into the system. The noise only became an issue on longer cable runs, and it was resolved by switching to a better quality supply. However, I do have a nice DS2482 library for my trouble, and I've offloaded some timing to that chip rather than having to bit bang the mbed.

posted by Daniel Peter 12 Jan 2014