PIN questions

  • A couple of PIN questions:

    1. In the docs they say alt is the alternate fn for p = Pin('P10', mode=Pin.OUT, pull=None, alt=-1) I don't understand what that means? Could someone give me an example of how you might use alt?
    2. I want to change multiple output pins simultaneously. There doesn't seem to be a way to group pins? Any suggestions?

  • tnx Rob I'll try my luck

  • @kjm
    About pt1: You have to look into the ESP32 technical reference manual, section "IO_MUX Pad List". There is a table with the suitable functions of each Pin.
    About pt 2: You use machine.mem32 to write directely to the GPIO output register. You can set the Pins to respective mode before. You will find the addresses of the output register in the ESP32 technical reference, chapter 4.12. Writing for instance a 1 to the respective position in register GPIO_OUT_W1TS_REG will set that bit to 1 without changing the other pins, wrting a 1 to GPIO_OUT_W1TS_REG will set that bit to 0. Writing a pattern to GPIO_OUT_REG will write that pattern to the set output pints. You can get the actual value. And you have to refer to the GPIO numbers, not the Pxx numbers.
    Below is a sample script, not very efficient, but it works. The interesting line is line 22.

    from machine import Pin, mem32
    def write_multi(data, pin_list, init=False):
        if init:
            for pin in pin_list:
                Pin(pin[0], Pin.OUT)
        mask = 0
        value = 0
        if type(data) is int:
            for i in range(len(pin_list) - 1, -1, -1):
                pin = pin_list[i][1]
                mask |= 1 <<  pin
                value |= ((data & 1) << pin)
                data >>= 1
            for i in range(len(pin_list)):
                pin = pin_list[i][1]
                mask |= 1 << pin
                value |= ((data[i] & 1) << pin)
        mask = 0xffffffff - mask
        mem32[0x3FF44004] = (mem32[0x3FF44004] & mask) | value
    def run():
        init = True
        for n in range(8):
            write_multi(n, (("P9", 12), ("P10", 13), ("P11", 22)), init)
            init = False
        for n in range(8):
            write_multi((n >> 2, n >> 1, n), (("P9", 12), ("P10", 13), ("P11", 22)), init)

Log in to reply

Pycom on Twitter