BPIO2 - how to override BPIO outputs when using SPI?

:tada: Aha! Thank you Ian … I think I see it now.

My issue was how I interpreted the second paramter to send to spi.set_io_value(). I had incorrectly understood that parameter to be a simple True/False value that is used to set all the masked pins to H/L, respectively.

I now understand that the first parameter simply indicates which pins are to be set, and the value is also a bitmask indicating the values of the pins (ignored when not in the bitmask).

My code was thus executing correctly...

In the if True branch, the mask limited the effect of the value to bitmask 0b100, but the value sent was 0b001. Thus, it set BPIO2 low.

Similarly, the else branch had a mask of 0b1101 in the first call, with value 0b0001, and thus was setting BPIO0 high, and BPIO2 and BPIO3 low. The second call (0b0010 / 0b0001 ) was setting BPIO1 low.

if True:
    print(f"Setting BPIO2 as output/high")
    BPIO2_MASK = 0b00000100
    spi.set_io_direction(BPIO2_MASK, 1)
    spi.set_io_value(BPIO2_MASK, 1)
else:
    print(f"Setting BPIO3..0 as HHLH")
    BPIO_HIGH_MASK = 0b00001101
    spi.set_io_direction(BPIO_HIGH_MASK, 1)
    spi.set_io_value(BPIO_HIGH_MASK, 1)
    BPIO_LOW_MASK  = 0b00000010
    spi.set_io_direction(BPIO_LOW_MASK, 1)
    spi.set_io_value(BPIO_LOW_MASK, 0)

I’ll have to think on this; maybe I can find a way to clarify the parameter names. (in a backwards-compatible manner, of course!)

Or maybe add a debug output warning when a bit is NOT included in the mask, but IS set in the value? (only the low ten bits, of course, and adjust for negative values to check for cleared bits not in the mask.) Yes, it could be noisy, but it could also highlight errors in the caller’s code (always good).

Either way, I think this will unblock me. Thank you again!

1 Like