Where is PinAF?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
hlovatt
Posts: 68
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

Where is PinAF?

Post by hlovatt » Tue Jun 30, 2020 8:10 am

Hi,

I'm writing a typeshed for the PyBoard and have hit a problem, I can't find where PinAF is defined. The documentation implies that it is in `pyb` but if I `dir(pyb)` on the REPL it doesn't show up!

Anyone know where it is?

Thanks for any help,

Howard.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Where is PinAF?

Post by jimmo » Tue Jun 30, 2020 12:52 pm

hlovatt wrote:
Tue Jun 30, 2020 8:10 am
I'm writing a typeshed for the PyBoard and have hit a problem
What's a typeshed?
hlovatt wrote:
Tue Jun 30, 2020 8:10 am
I can't find where PinAF is defined. The documentation implies that it is in `pyb` but if I `dir(pyb)` on the REPL it doesn't show up!
You can't construct a PinAF directly, but there are instances of PinAF defined on pyb.Pin (and returned by the af_list() method on Pin).

https://docs.micropython.org/en/latest/ ... -functions

(This applies to both machine.Pin and pyb.Pin, they're actually the same type, unlike machine.I2C vs pyb.I2C which are different)

hlovatt
Posts: 68
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

Re: Where is PinAF?

Post by hlovatt » Tue Jun 30, 2020 11:00 pm

jimmo wrote:
Tue Jun 30, 2020 12:52 pm
What's a typeshed?
A typeshed or `.pyi` file is adding optional type hinting to Python in a seperate file, e.g. the typesheds for the standard Python library. Whilst a typeshed isn't useful for MicroPython itself, it is for IDEs like PyCharm (i.e. in this case for the MicroPython plugin).

The problem I have is that the REPL tells me that there are PinAF objects, as you say. EG `type(pyb.Pin.board.X3.af_list()[0])` gives `<class 'PinAF'>`. But I can't tell if `PinAF` is in `pyb`, i.e. `pyb.PinAF`, or somewhere else since you cannot extract module names from MicroPython.

Do you know its module?

I guess the fact that you can't instantiate the class makes this moot, but I might as well put it in the correct place. I can make the class either abstract or a Protocol to tell PyCharm that it can't be instantiated.

Thanks for any help.

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

Re: Where is PinAF?

Post by dhylands » Wed Jul 01, 2020 6:33 pm

The Pin and PinAF (for the pyboard) are both found in the pin.c file:
https://github.com/micropython/micropyt ... pin.c#L592
https://github.com/micropython/micropyt ... pin.c#L669

Currently there isn't anyway to construct a PinAF object from Python. They only exist as objects in the flash. The source files for creating the PinAF objects is generated, for example: in ports/stm32/build-PYBV11/pins_PYBV11.c. Each line with the AF macro is generating one PinAF object.

Here's a snippet from the pins_PYBV11.c file:

Code: Select all

const pin_af_obj_t pin_A0_af[] = {
  AF( 1, TIM     ,  2, CH1       , TIM2    ), // TIM2_CH1
  AF( 1, TIM     ,  2, ETR       , TIM2    ), // TIM2_ETR
  AF( 2, TIM     ,  5, CH1       , TIM5    ), // TIM5_CH1
  AF( 3, TIM     ,  8, ETR       , TIM8    ), // TIM8_ETR
#if defined(MICROPY_HW_UART2_TX)
  AF( 7, USART   ,  2, CTS       , USART2  ), // USART2_CTS
#endif
#if defined(MICROPY_HW_UART4_TX)
  AF( 8, UART    ,  4, TX        , UART4   ), // UART4_TX
#endif
  //(11, ETH     ,  0, MII_CRS   , ETH     ), // ETH_MII_CRS
  //(15, EVENTOUT,  0,           , EVENTOUT), // EVENTOUT
};

Post Reply