lancaster-university / codal-microbit-v2

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

`TouchButton::setPinValue(1)` results in a pin voltage lower than Vin #346

Open microbit-carlos opened 1 year ago

microbit-carlos commented 1 year ago

To replicate on USB power:

MICROBIT.hex.zip

#include "MicroBit.h"

MicroBit uBit;

int main() {
    uBit.init();

    while (!uBit.buttonB.isPressed()) {
        if (uBit.buttonA.isPressed()) {
            uBit.display.print("A");
            uBit.io.P16.setDigitalValue(1);
            // Voltage at this point in P16 is around ~3.3V
        }
        uBit.sleep(250);
    }

    TouchButton *pin16Touch = new TouchButton(uBit.io.P16, uBit.touchSensor, CAPTOUCH_DEFAULT_CALIBRATION);

    uBit.sleep(1000);
    uBit.display.clear();
    while (true) {
        if (uBit.buttonA.isPressed()) {
            uBit.display.print("A");
            pin16Touch->setPinValue(1);
            // Voltage at this point in P16 is around ~2.2V
        } else if (uBit.buttonB.isPressed()) {
            uBit.display.print("B");
            pin16Touch->setPinValue(0);
        }
        uBit.sleep(250);
    }
}
JohnVidler commented 11 months ago

S'cuse the bad photo, but having tested this on P2 (for ease of connection to the scope), I see this waveform when touch mode is enabled and setValue(1) is called - this looks like the peripherals are fighting each other:

image

The PWMing effect of this would account for the lower voltage you're seeing, I guess @microbit-carlos

Edit I just noticed that the average the scope reports is 2.27v, which is right where you see it...

microbit-carlos commented 11 months ago

Yeah, that would explain the voltage value.

Btw, how is capacitance touch done on the digital pins with CODAL? Isn't setting the pin high and then measuring how long it takes to go low? Do you get the same waveform when setValue() is not called and is checking "isTouched" in a loop?

finneyj commented 11 months ago

Now fixed according to my VW Passat GTE scope, as per #345

passat-scope

microbit-carlos commented 11 months ago

But Joe, we were testing pins not the g-force events! 🤣

I've given this a quick test with the latest commits in all codal repos:

$ python build.py -s

***/Users/microbit-carlos/workspace/mbef/codal/microbit-v2-samples/libraries/codal-core
Branch: master, Nearest Tag: streamracefix-v0 (32cc87c3298a49663c3b3b7b1881ec7aeb3d73a5)
## master...origin/master

***/Users/microbit-carlos/workspace/mbef/codal/microbit-v2-samples/libraries/codal-nrf52
Branch: master, Nearest Tag: ~none~ (2dbf5aa214c7432c0b1b8a19e92a3f9a4fc4b9da)
## master...origin/master

***/Users/microbit-carlos/workspace/mbef/codal/microbit-v2-samples/libraries/codal-microbit-nrf5sdk
Branch: master, Nearest Tag: ~none~ (ef4662e13875a7b03e7296d7ac24a2b4d231f323)
## master...origin/master

***/Users/microbit-carlos/workspace/mbef/codal/microbit-v2-samples/libraries/codal-microbit-v2
Branch: , Nearest Tag: v0.2.57 (018864dddb64ebf4e2cfbf13cbba1998e8b55d62)
## HEAD (no branch)

***/Users/microbit-carlos/workspace/mbef/codal/microbit-v2-samples
Branch: master, Nearest Tag: v0.2.11 (82691961f838e997593759538cca7f3999c3704d)
## master...origin/master
 M codal.json
 M source/main.cpp

https://github.com/lancaster-university/codal-microbit-v2/commit/018864dddb64ebf4e2cfbf13cbba1998e8b55d62 https://github.com/lancaster-university/codal-nrf52/commit/2dbf5aa214c7432c0b1b8a19e92a3f9a4fc4b9da https://github.com/lancaster-university/codal-core/commit/32cc87c3298a49663c3b3b7b1881ec7aeb3d73a5

And with a simple test programme like this, I'm still seeing ~2.2-2.6V in pin16 and the face logo with a multimeter:

#include "MicroBit.h"

MicroBit uBit;

int main() {
    uBit.init();

    TouchButton *pin16Touch = new TouchButton(uBit.io.P16, uBit.touchSensor, CAPTOUCH_DEFAULT_CALIBRATION);

    pin16Touch->setPinValue(1);
    uBit.logo.setPinValue(1);

    while (true) {
        uBit.sleep(1000);
    }
}
microbit-carlos commented 11 months ago

Okay, with this other test I can confirm that using uBit.io.xxxx.setDigitalValue(1); with a pin used in a TouchButton does work. But using the TouchButton:: setPinValue() does not.

With this programme:

#include "MicroBit.h"

MicroBit uBit;

int main() {
    uBit.init();

    TouchButton *pin16Touch = new TouchButton(uBit.io.P16, uBit.touchSensor, CAPTOUCH_DEFAULT_CALIBRATION);

    pin16Touch->setPinValue(1);
    uBit.logo.setPinValue(1);

    while (true) {
        if (uBit.buttonA.isPressed()) {
             uBit.io.logo.setDigitalValue(1);
             uBit.io.P16.setDigitalValue(1);
             uBit.display.print("A");
        } 
        uBit.sleep(200);
    }
}
microbit-carlos commented 11 months ago

Ah, that'll likely be because TouchButton::setPinValue() locks the pin before running pin.setDigitalValue():

https://github.com/lancaster-university/codal-core/blob/32cc87c3298a49663c3b3b7b1881ec7aeb3d73a5/source/drivers/TouchButton.cpp#L133-L138

void TouchButton::setPinValue(int v)
{
    setPinLock(true);
    _pin.setDigitalValue(v);
    setPinLock(false);
}