Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pulseIn() function of Arch/Esp8266/Core/Digital.cpp can't work with pins >= 8 #2336

Closed
alex18-d opened this issue Jun 2, 2021 · 1 comment
Labels

Comments

@alex18-d
Copy link
Contributor

alex18-d commented Jun 2, 2021

It was found that Ultrasonic HC-SR04 set eith Echo pin = 13 always returns 0 cm disatance.
The problem is in the old code of pulseIn(), see Arch/Esp8266/Core/Digital.cpp.

Now GPIO_IN register is read as a whole 32bit value, *portInputRegister(port) returns 32bit value.
But pin's respective variables bit and stateMask are still defined as uint8_t, as they were when GPIO_IN was processed by 8bit parts. Therefore bit and stateMask will be 0 always for pins >=8 and will never match the value read from the register.

So, the variable needs a correction:

--- a/Sming/Arch/Esp8266/Core/Digital.cpp
+++ b/Sming/Arch/Esp8266/Core/Digital.cpp
@@ -141,9 +141,9 @@ unsigned long pulseIn(uint16_t pin, uint8_t state, unsigned long timeout)
        // cache the port and bit of the pin in order to speed up the
        // pulse width measuring loop and achieve finer resolution.  calling
        // digitalRead() instead yields much coarser resolution.
-       uint8_t bit = digitalPinToBitMask(pin);
+       uint32_t bit = digitalPinToBitMask(pin);
        // uint8_t port = digitalPinToPort(pin); // Does nothing in Sming, comment-out to prevent compiler warning
-       uint8_t stateMask = (state ? bit : 0);
+       uint32_t stateMask = (state ? bit : 0);
        unsigned long width = 0; // keep initialization out of time critical area
 
        // convert the timeout from microseconds to a number of times through
@slaff
Copy link
Contributor

slaff commented Jun 7, 2021

@alexdz18 Thanks for pointing this out. Can you please open a PR with the change to receive the deserved credit for it? Take a look at the contributing guide if you need some guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants