Page 1 of 1
creating a pin in c
Posted: Mon Apr 09, 2018 5:08 pm
by gcarver
I have a python module to communicate with a wireless PS2 controller I want to port to C. The init function starts with this.
Code: Select all
def __init__( self, aCmd, aData, aClk, aAtt, aCallback = None ):
self._cmd = Pin(aCmd, Pin.OUT, Pin.PULL_UP)
self._data = Pin(aData, Pin.IN, Pin.PULL_UP)
self._clk = Pin(aClk, Pin.OUT, Pin.PULL_UP)
self._att = Pin(aAtt, Pin.OUT, Pin.PULL_UP)
I'd like to know the best way to create a Pin object from c code when given a GPIO Pin number.
Thank you.
Re: creating a pin in c
Posted: Mon Apr 09, 2018 5:37 pm
by dhylands
The Pin class does that already. For example, in python when you do:
then it the pin class will look for GPIO pin 5 on port A.
This is the pin code that is called:
https://github.com/micropython/micropyt ... #L248-L262
The pin_find function is exposed, and if you search through the code you'll see several other modules call it.
https://github.com/micropython/micropyt ... pin.c#L104
If you know at compile time which pin you want, then you can refer to the pin directly. For example, the A5 pin will be generated and have a name of pin_A5.
For the PYBV11, for example, you can look in build-PYBV11/genhdr/pins.h and see all of the generated pins.
Re: creating a pin in c
Posted: Fri Apr 13, 2018 6:08 pm
by gcarver
Does that work on esp32? I thought that was only pyboard.
Re: creating a pin in c
Posted: Fri Apr 13, 2018 9:42 pm
by dhylands
Sorry - I missed that this was in the "ESP32 boards" (I normally read unread messages not by board).
It looks like you'd need to call the make_new method from the machine.Pin class.
Something like the following (untested):
Code: Select all
mp_obj_t args[] = {
MP_OBJ_NEW_SMALL_INT(gpio_num),
other Pin arguments
};
my_pin = machine_pin_type.make_new(&machine_pin_type, n_args, 0, &args);
I'd probably create a C wrapper function that takes the arguments you want and packs them into mp_obj_t's and then calls the make_new()
Re: creating a pin in c
Posted: Sun Apr 15, 2018 7:51 pm
by gcarver
Thank you, this worked.