creating a pin in c

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
User avatar
gcarver
Posts: 34
Joined: Sun Oct 26, 2014 4:07 am
Location: New Market, Maryland
Contact:

creating a pin in c

Post by gcarver » Mon Apr 09, 2018 5:08 pm

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.
End of line...

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: creating a pin in c

Post by dhylands » Mon Apr 09, 2018 5:37 pm

The Pin class does that already. For example, in python when you do:

Code: Select all

p = Pin('A5')
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.

User avatar
gcarver
Posts: 34
Joined: Sun Oct 26, 2014 4:07 am
Location: New Market, Maryland
Contact:

Re: creating a pin in c

Post by gcarver » Fri Apr 13, 2018 6:08 pm

Does that work on esp32? I thought that was only pyboard.
End of line...

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: creating a pin in c

Post by dhylands » Fri Apr 13, 2018 9:42 pm

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()

User avatar
gcarver
Posts: 34
Joined: Sun Oct 26, 2014 4:07 am
Location: New Market, Maryland
Contact:

Re: creating a pin in c

Post by gcarver » Sun Apr 15, 2018 7:51 pm

Thank you, this worked.
End of line...

Post Reply