PaulStoffregen / OneWire

Library for Dallas/Maxim 1-Wire Chips
http://www.pjrc.com/teensy/td_libs_OneWire.html
579 stars 382 forks source link

Conditional compile for ESP32 Critical Sections #47

Closed stickbreaker closed 6 years ago

stickbreaker commented 6 years ago

conditional compiles for ESP32 compatibility, prior to this revision, library was unusable with WiFi enabled on the ESP32, the scheduler was causing havoc with the strict timing requirements of this protocol. This is a quick and dirty substitution of portENTER_CRITICAL(&mux) for noInterrupts(). It has only been tested with DS18b20's (2 with 4.7k Pullups to 3.3v)

Chuck.

PaulStoffregen commented 6 years ago

I will merge this, but not as so many ifdefs throughout the code. It needs to be worked into the existing scheme, with a macro to abstract/replace noInterrupts()

stickbreaker commented 6 years ago

@PaulStoffregen How can I code a macro with a limited scope?

// in OneWire.h
#ifdef ARDUINO_ARCH_ESP32
#define noInterrupts() portMUX_TYPE mux=portMUX_INITIALIZER_UNLOCKED;portENTER_CRITICAL(&mux)
#define interrupts() portEXIT_CRITICAL(&mux)
#endif

Do you want mux defined as a local variable instead of a private Object variable?

The mux variable still needs to conditionally compiled in the Object and Initialized.

I don't know of any coding to keep the #define local to this library? Do you know of any?

Chuck.

stickbreaker commented 6 years ago

@PaulStoffregen to get it to compile with a local mux I had to code the macro with unbalanced '{' '}', scope the mux local.

and My limited scope was solved with #undef. I need to think before I speak.


#ifdef ARDUINO_ARCH_ESP32
#define noInterrupts() {portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;portENTER_CRITICAL(&mux)
#define interrupts() portEXIT_CRITICAL(&mux);}
#endif

Chuck.

PaulStoffregen commented 6 years ago

I have attempted to merge this. https://github.com/PaulStoffregen/OneWire/commit/ac0ea9a7fc266b2d709532fcb1269ffe5fcbbb46

I also moved the platform specific GPIO stuff to a separate header and added undefs, to keep all this stuff from leaking into Arduino sketches.

Please give this version a try and let me know if it works for ESP32?

PaulStoffregen commented 6 years ago

I believe https://github.com/PaulStoffregen/OneWire/commit/9e08f4e43eadae975a321c323b85b8d1ddc7aa4a should fix all remaining ESP32 problems.

Closing this.