2016-06-07 17:57:41 -04:00
|
|
|
.. currentmodule:: pyb
|
2014-11-04 13:25:20 -05:00
|
|
|
.. _pyb.Pin:
|
|
|
|
|
2014-10-31 18:21:37 -04:00
|
|
|
class Pin -- control I/O pins
|
|
|
|
=============================
|
2014-10-30 21:37:19 -04:00
|
|
|
|
|
|
|
A pin is the basic object to control I/O pins. It has methods to set
|
|
|
|
the mode of the pin (input, output, etc) and methods to get and set the
|
2015-06-10 17:29:56 -04:00
|
|
|
digital logic level. For analog control of a pin, see the ADC class.
|
2014-10-30 21:37:19 -04:00
|
|
|
|
|
|
|
Usage Model:
|
|
|
|
|
2018-07-18 01:47:44 -04:00
|
|
|
All Board Pins are predefined as pyb.Pin.board.Name::
|
|
|
|
|
|
|
|
x1_pin = pyb.Pin.board.X1
|
|
|
|
|
|
|
|
g = pyb.Pin(pyb.Pin.board.X1, pyb.Pin.IN)
|
|
|
|
|
|
|
|
CPU pins which correspond to the board pins are available
|
2018-10-18 11:58:16 -04:00
|
|
|
as ``pyb.Pin.cpu.Name``. For the CPU pins, the names are the port letter
|
2018-07-18 01:47:44 -04:00
|
|
|
followed by the pin number. On the PYBv1.0, ``pyb.Pin.board.X1`` and
|
|
|
|
``pyb.Pin.cpu.A0`` are the same pin.
|
|
|
|
|
|
|
|
You can also use strings::
|
|
|
|
|
|
|
|
g = pyb.Pin('X1', pyb.Pin.OUT_PP)
|
|
|
|
|
|
|
|
Users can add their own names::
|
|
|
|
|
|
|
|
MyMapperDict = { 'LeftMotorDir' : pyb.Pin.cpu.C12 }
|
|
|
|
pyb.Pin.dict(MyMapperDict)
|
|
|
|
g = pyb.Pin("LeftMotorDir", pyb.Pin.OUT_OD)
|
|
|
|
|
|
|
|
and can query mappings::
|
|
|
|
|
|
|
|
pin = pyb.Pin("LeftMotorDir")
|
|
|
|
|
|
|
|
Users can also add their own mapping function::
|
|
|
|
|
|
|
|
def MyMapper(pin_name):
|
|
|
|
if pin_name == "LeftMotorDir":
|
|
|
|
return pyb.Pin.cpu.A0
|
|
|
|
|
|
|
|
pyb.Pin.mapper(MyMapper)
|
|
|
|
|
|
|
|
So, if you were to call: ``pyb.Pin("LeftMotorDir", pyb.Pin.OUT_PP)``
|
|
|
|
then ``"LeftMotorDir"`` is passed directly to the mapper function.
|
|
|
|
|
|
|
|
To summarise, the following order determines how things get mapped into
|
|
|
|
an ordinal pin number:
|
|
|
|
|
|
|
|
1. Directly specify a pin object
|
|
|
|
2. User supplied mapping function
|
|
|
|
3. User supplied mapping (object must be usable as a dictionary key)
|
|
|
|
4. Supply a string which matches a board pin
|
|
|
|
5. Supply a string which matches a CPU port/pin
|
|
|
|
|
|
|
|
You can set ``pyb.Pin.debug(True)`` to get some debug information about
|
|
|
|
how a particular object gets mapped to a pin.
|
|
|
|
|
|
|
|
When a pin has the ``Pin.PULL_UP`` or ``Pin.PULL_DOWN`` pull-mode enabled,
|
|
|
|
that pin has an effective 40k Ohm resistor pulling it to 3V3 or GND
|
|
|
|
respectively (except pin Y5 which has 11k Ohm resistors).
|
|
|
|
|
|
|
|
Now every time a falling edge is seen on the gpio pin, the callback will be
|
|
|
|
executed. Caution: mechanical push buttons have "bounce" and pushing or
|
|
|
|
releasing a switch will often generate multiple edges.
|
|
|
|
See: http://www.eng.utah.edu/~cs5780/debouncing.pdf for a detailed
|
|
|
|
explanation, along with various techniques for debouncing.
|
|
|
|
|
|
|
|
All pin objects go through the pin mapper to come up with one of the
|
|
|
|
gpio pins.
|
2014-10-30 21:37:19 -04:00
|
|
|
|
2015-06-10 17:29:56 -04:00
|
|
|
Constructors
|
|
|
|
------------
|
2014-10-30 21:37:19 -04:00
|
|
|
|
2015-09-03 05:25:44 -04:00
|
|
|
.. class:: pyb.Pin(id, ...)
|
2014-10-30 21:37:19 -04:00
|
|
|
|
2015-09-03 05:25:44 -04:00
|
|
|
Create a new Pin object associated with the id. If additional arguments are given,
|
|
|
|
they are used to initialise the pin. See :meth:`pin.init`.
|
2014-10-30 21:37:19 -04:00
|
|
|
|
2018-07-18 01:47:44 -04:00
|
|
|
Class methods
|
|
|
|
-------------
|
|
|
|
|
|
|
|
.. classmethod:: Pin.debug([state])
|
|
|
|
|
|
|
|
Get or set the debugging state (``True`` or ``False`` for on or off).
|
2014-10-30 21:37:19 -04:00
|
|
|
|
2018-07-18 01:47:44 -04:00
|
|
|
.. classmethod:: Pin.dict([dict])
|
2016-06-08 08:57:46 -04:00
|
|
|
|
2018-07-18 01:47:44 -04:00
|
|
|
Get or set the pin mapper dictionary.
|
|
|
|
|
|
|
|
.. classmethod:: Pin.mapper([fun])
|
|
|
|
|
|
|
|
Get or set the pin mapper function.
|
2014-10-30 21:37:19 -04:00
|
|
|
|
|
|
|
|
2015-06-10 17:29:56 -04:00
|
|
|
Methods
|
|
|
|
-------
|
2014-10-30 21:37:19 -04:00
|
|
|
|
2021-07-12 12:04:56 -04:00
|
|
|
.. method:: Pin.init(mode, pull=Pin.PULL_NONE, *, value=None, alt=-1)
|
2018-07-18 01:47:44 -04:00
|
|
|
|
|
|
|
Initialise the pin:
|
2014-10-30 21:37:19 -04:00
|
|
|
|
2020-07-28 02:27:19 -04:00
|
|
|
- *mode* can be one of:
|
2014-10-30 21:37:19 -04:00
|
|
|
|
2018-07-18 01:47:44 -04:00
|
|
|
- ``Pin.IN`` - configure the pin for input;
|
|
|
|
- ``Pin.OUT_PP`` - configure the pin for output, with push-pull control;
|
|
|
|
- ``Pin.OUT_OD`` - configure the pin for output, with open-drain control;
|
|
|
|
- ``Pin.AF_PP`` - configure the pin for alternate function, pull-pull;
|
|
|
|
- ``Pin.AF_OD`` - configure the pin for alternate function, open-drain;
|
|
|
|
- ``Pin.ANALOG`` - configure the pin for analog.
|
2014-10-30 21:37:19 -04:00
|
|
|
|
2020-07-28 02:27:19 -04:00
|
|
|
- *pull* can be one of:
|
2014-10-30 21:37:19 -04:00
|
|
|
|
2018-07-18 01:47:44 -04:00
|
|
|
- ``Pin.PULL_NONE`` - no pull up or down resistors;
|
|
|
|
- ``Pin.PULL_UP`` - enable the pull-up resistor;
|
|
|
|
- ``Pin.PULL_DOWN`` - enable the pull-down resistor.
|
2014-10-30 21:37:19 -04:00
|
|
|
|
2020-07-28 02:27:19 -04:00
|
|
|
- *value* if not None will set the port output value before enabling the pin.
|
|
|
|
|
|
|
|
- *alt* can be used when mode is ``Pin.AF_PP`` or ``Pin.AF_OD`` to set the
|
2021-12-14 19:49:22 -05:00
|
|
|
index or name of one of the alternate functions associated with a pin.
|
2020-07-28 02:27:19 -04:00
|
|
|
This arg was previously called *af* which can still be used if needed.
|
2018-07-18 01:47:44 -04:00
|
|
|
|
|
|
|
Returns: ``None``.
|
2014-10-30 21:37:19 -04:00
|
|
|
|
2016-06-08 09:21:28 -04:00
|
|
|
.. method:: Pin.value([value])
|
2014-11-04 13:07:06 -05:00
|
|
|
|
|
|
|
Get or set the digital logic level of the pin:
|
|
|
|
|
|
|
|
- With no argument, return 0 or 1 depending on the logic level of the pin.
|
|
|
|
- With ``value`` given, set the logic level of the pin. ``value`` can be
|
|
|
|
anything that converts to a boolean. If it converts to ``True``, the pin
|
|
|
|
is set high, otherwise it is set low.
|
|
|
|
|
2018-07-18 01:47:44 -04:00
|
|
|
.. method:: Pin.__str__()
|
|
|
|
|
|
|
|
Return a string describing the pin object.
|
|
|
|
|
|
|
|
.. method:: Pin.af()
|
|
|
|
|
|
|
|
Returns the currently configured alternate-function of the pin. The
|
|
|
|
integer returned will match one of the allowed constants for the af
|
|
|
|
argument to the init function.
|
|
|
|
|
|
|
|
.. method:: Pin.af_list()
|
|
|
|
|
|
|
|
Returns an array of alternate functions available for this pin.
|
|
|
|
|
|
|
|
.. method:: Pin.gpio()
|
|
|
|
|
|
|
|
Returns the base address of the GPIO block associated with this pin.
|
|
|
|
|
|
|
|
.. method:: Pin.mode()
|
|
|
|
|
|
|
|
Returns the currently configured mode of the pin. The integer returned
|
|
|
|
will match one of the allowed constants for the mode argument to the init
|
|
|
|
function.
|
|
|
|
|
|
|
|
.. method:: Pin.name()
|
|
|
|
|
|
|
|
Get the pin name.
|
|
|
|
|
|
|
|
.. method:: Pin.names()
|
|
|
|
|
|
|
|
Returns the cpu and board names for this pin.
|
|
|
|
|
|
|
|
.. method:: Pin.pin()
|
|
|
|
|
|
|
|
Get the pin number.
|
|
|
|
|
|
|
|
.. method:: Pin.port()
|
|
|
|
|
|
|
|
Get the pin port.
|
2016-06-08 08:57:46 -04:00
|
|
|
|
2016-06-08 09:21:28 -04:00
|
|
|
.. method:: Pin.pull()
|
2015-09-03 05:25:44 -04:00
|
|
|
|
|
|
|
Returns the currently configured pull of the pin. The integer returned
|
|
|
|
will match one of the allowed constants for the pull argument to the init
|
|
|
|
function.
|
2015-06-10 17:29:56 -04:00
|
|
|
|
2014-10-30 21:37:19 -04:00
|
|
|
Constants
|
|
|
|
---------
|
|
|
|
|
2018-07-18 01:47:44 -04:00
|
|
|
.. data:: Pin.AF_OD
|
|
|
|
|
|
|
|
initialise the pin to alternate-function mode with an open-drain drive
|
|
|
|
|
|
|
|
.. data:: Pin.AF_PP
|
|
|
|
|
|
|
|
initialise the pin to alternate-function mode with a push-pull drive
|
|
|
|
|
|
|
|
.. data:: Pin.ANALOG
|
|
|
|
|
|
|
|
initialise the pin to analog mode
|
|
|
|
|
|
|
|
.. data:: Pin.IN
|
|
|
|
|
|
|
|
initialise the pin to input mode
|
|
|
|
|
|
|
|
.. data:: Pin.OUT_OD
|
|
|
|
|
|
|
|
initialise the pin to output mode with an open-drain drive
|
|
|
|
|
|
|
|
.. data:: Pin.OUT_PP
|
|
|
|
|
|
|
|
initialise the pin to output mode with a push-pull drive
|
|
|
|
|
|
|
|
.. data:: Pin.PULL_DOWN
|
|
|
|
|
|
|
|
enable the pull-down resistor on the pin
|
|
|
|
|
|
|
|
.. data:: Pin.PULL_NONE
|
|
|
|
|
|
|
|
don't enable any pull up or down resistors on the pin
|
|
|
|
|
|
|
|
.. data:: Pin.PULL_UP
|
|
|
|
|
|
|
|
enable the pull-up resistor on the pin
|
|
|
|
|
|
|
|
class PinAF -- Pin Alternate Functions
|
|
|
|
======================================
|
|
|
|
|
|
|
|
A Pin represents a physical pin on the microprocessor. Each pin
|
|
|
|
can have a variety of functions (GPIO, I2C SDA, etc). Each PinAF
|
|
|
|
object represents a particular function for a pin.
|
|
|
|
|
|
|
|
Usage Model::
|
|
|
|
|
|
|
|
x3 = pyb.Pin.board.X3
|
|
|
|
x3_af = x3.af_list()
|
|
|
|
|
|
|
|
x3_af will now contain an array of PinAF objects which are available on
|
|
|
|
pin X3.
|
|
|
|
|
|
|
|
For the pyboard, x3_af would contain:
|
|
|
|
[Pin.AF1_TIM2, Pin.AF2_TIM5, Pin.AF3_TIM9, Pin.AF7_USART2]
|
|
|
|
|
2022-01-27 00:36:08 -05:00
|
|
|
Normally, each peripheral would configure the alternate function automatically,
|
|
|
|
but sometimes the same function is available on multiple pins, and having more
|
|
|
|
control is desired.
|
2018-07-18 01:47:44 -04:00
|
|
|
|
|
|
|
To configure X3 to expose TIM2_CH3, you could use::
|
|
|
|
|
2022-01-27 00:36:08 -05:00
|
|
|
pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.AF_PP, alt=pyb.Pin.AF1_TIM2)
|
2018-07-18 01:47:44 -04:00
|
|
|
|
|
|
|
or::
|
|
|
|
|
2022-01-27 00:36:08 -05:00
|
|
|
pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.AF_PP, alt=1)
|
2018-07-18 01:47:44 -04:00
|
|
|
|
|
|
|
Methods
|
|
|
|
-------
|
|
|
|
|
|
|
|
.. method:: pinaf.__str__()
|
|
|
|
|
|
|
|
Return a string describing the alternate function.
|
|
|
|
|
|
|
|
.. method:: pinaf.index()
|
|
|
|
|
|
|
|
Return the alternate function index.
|
|
|
|
|
|
|
|
.. method:: pinaf.name()
|
|
|
|
|
|
|
|
Return the name of the alternate function.
|
|
|
|
|
|
|
|
.. method:: pinaf.reg()
|
|
|
|
|
|
|
|
Return the base register associated with the peripheral assigned to this
|
|
|
|
alternate function. For example, if the alternate function were TIM2_CH3
|
|
|
|
this would return stm.TIM2
|