lancaster-university / codal-microbit-v2

CODAL target for the micro:bit v2.x series of devices
MIT License
41 stars 50 forks source link

`TouchSensor::removeTouchButton()` doesn't always work #345

Closed microbit-carlos closed 11 months ago

microbit-carlos commented 1 year ago

When a pin is registered as a TouchButton and it is set as digital output high, the voltage on the pin is less than Vin. For example, when USB connected and the MCU is running at 3.3V, setting uBit.io.logo.setDigitalValue(1) produces ~2.6V on the pin instead of 3.3V. This is lower than expected voltage issue is reported in:

Using TouchSensor::removeTouchButton() should technically remove the touch functionality from the pin. By doing so, it should be back to normal, so when setting it as digital output high, the pin should reach the 3.3V again. However, with this example programme we can see that is not always the case.

Button A sets the pin logo high, and button B runs removeTouchButton(). To run this test we need a voltmeter to measure the voltage in the face logo.

MICROBIT.hex.zip

#include "MicroBit.h"

MicroBit uBit;

int main() {
    uBit.init();

    while (true) {
        if (uBit.buttonA.isPressed()) {
            uBit.io.logo.setDigitalValue(1);
            uBit.display.print("A");
        } else if (uBit.buttonB.isPressed()) {
            if (uBit.touchSensor.removeTouchButton(&uBit.logo) != DEVICE_OK) {
                uBit.display.print("X");
                while (true) uBit.sleep(1000);
            }
            uBit.display.print("B");
        } 
        uBit.sleep(250);
    }
}
JohnVidler commented 1 year ago

Just to clarify - this is that the HIGH voltage is not reaching 3v3.

I've not been able to test this yet, but does this also happen if we use an edge connector pin in capacitive sensing mode, or is it just the logo that is affected?

microbit-carlos commented 1 year ago

No sorry, that will be covered in:

This is that running uBit.touchSensor.removeTouchButton() doesn't always have the same result. Together with setDigitalValue(1) then pin voltage either goes to the expected 3.3V ✅, or it goes down to V1.7 ❌

JohnVidler commented 11 months ago

Should this actually be the case? Technically as we don't track the pin state on anything other than the pin peripheral itself, by removing the touch sensor we're getting into an undefined state.

We could proactively set the pin high or low, but we can't possibly know what the application requriements are for this, so is it better to leave this as it is, and require that if applications need a pin to be in a specific state after removing touch sensing they should actively drive high or low?

microbit-carlos commented 11 months ago

Removing the touch sensor should leave the pin in whatever is the default state for any other digital pin, no? How are the "normal" digital pins configured on startup?

finneyj commented 11 months ago

Thanks folks. I've looked into this - the issue was coming from the NRF52TouchSensor not letting go of the NRF52 GPIOTE channels when a button is disconnected.

I've run a fair few tests, including the ones listed above. This should now be fixed in main branches of the codal-core and codal-nrf52.

microbit-carlos commented 11 months ago

Okay, we can close this one as fixed! 🎉