machine.Pin.rst 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. .. currentmodule:: machine
  2. .. _machine.Pin:
  3. class Pin -- control I/O pins
  4. =============================
  5. A pin object is used to control I/O pins (also known as GPIO - general-purpose
  6. input/output). Pin objects are commonly associated with a physical pin that can
  7. drive an output voltage and read input voltages. The pin class has methods to set the mode of
  8. the pin (IN, OUT, etc) and methods to get and set the digital logic level.
  9. For analog control of a pin, see the :class:`ADC` class.
  10. A pin object is constructed by using an identifier which unambiguously
  11. specifies a certain I/O pin. The allowed forms of the identifier and the
  12. physical pin that the identifier maps to are port-specific. Possibilities
  13. for the identifier are an integer, a string or a tuple with port and pin
  14. number.
  15. Usage Model::
  16. from machine import Pin
  17. # create an output pin on pin #0
  18. p0 = Pin(0, Pin.OUT)
  19. # set the value low then high
  20. p0.value(0)
  21. p0.value(1)
  22. # create an input pin on pin #2, with a pull up resistor
  23. p2 = Pin(2, Pin.IN, Pin.PULL_UP)
  24. # read and print the pin value
  25. print(p2.value())
  26. # reconfigure pin #0 in input mode
  27. p0.mode(p0.IN)
  28. # configure an irq callback
  29. p0.irq(lambda p:print(p))
  30. Constructors
  31. ------------
  32. .. class:: Pin(id, mode=-1, pull=-1, \*, value, drive, alt)
  33. Access the pin peripheral (GPIO pin) associated with the given ``id``. If
  34. additional arguments are given in the constructor then they are used to initialise
  35. the pin. Any settings that are not specified will remain in their previous state.
  36. The arguments are:
  37. - ``id`` is mandatory and can be an arbitrary object. Among possible value
  38. types are: int (an internal Pin identifier), str (a Pin name), and tuple
  39. (pair of [port, pin]).
  40. - ``mode`` specifies the pin mode, which can be one of:
  41. - ``Pin.IN`` - Pin is configured for input. If viewed as an output the pin
  42. is in high-impedance state.
  43. - ``Pin.OUT`` - Pin is configured for (normal) output.
  44. - ``Pin.OPEN_DRAIN`` - Pin is configured for open-drain output. Open-drain
  45. output works in the following way: if the output value is set to 0 the pin
  46. is active at a low level; if the output value is 1 the pin is in a high-impedance
  47. state. Not all ports implement this mode, or some might only on certain pins.
  48. - ``Pin.ALT`` - Pin is configured to perform an alternative function, which is
  49. port specific. For a pin configured in such a way any other Pin methods
  50. (except :meth:`Pin.init`) are not applicable (calling them will lead to undefined,
  51. or a hardware-specific, result). Not all ports implement this mode.
  52. - ``Pin.ALT_OPEN_DRAIN`` - The Same as ``Pin.ALT``, but the pin is configured as
  53. open-drain. Not all ports implement this mode.
  54. - ``pull`` specifies if the pin has a (weak) pull resistor attached, and can be
  55. one of:
  56. - ``None`` - No pull up or down resistor.
  57. - ``Pin.PULL_UP`` - Pull up resistor enabled.
  58. - ``Pin.PULL_DOWN`` - Pull down resistor enabled.
  59. - ``value`` is valid only for Pin.OUT and Pin.OPEN_DRAIN modes and specifies initial
  60. output pin value if given, otherwise the state of the pin peripheral remains
  61. unchanged.
  62. - ``drive`` specifies the output power of the pin and can be one of: ``Pin.LOW_POWER``,
  63. ``Pin.MED_POWER`` or ``Pin.HIGH_POWER``. The actual current driving capabilities
  64. are port dependent. Not all ports implement this argument.
  65. - ``alt`` specifies an alternate function for the pin and the values it can take are
  66. port dependent. This argument is valid only for ``Pin.ALT`` and ``Pin.ALT_OPEN_DRAIN``
  67. modes. It may be used when a pin supports more than one alternate function. If only
  68. one pin alternate function is supported the this argument is not required. Not all
  69. ports implement this argument.
  70. As specified above, the Pin class allows to set an alternate function for a particular
  71. pin, but it does not specify any further operations on such a pin. Pins configured in
  72. alternate-function mode are usually not used as GPIO but are instead driven by other
  73. hardware peripherals. The only operation supported on such a pin is re-initialising,
  74. by calling the constructor or :meth:`Pin.init` method. If a pin that is configured in
  75. alternate-function mode is re-initialised with ``Pin.IN``, ``Pin.OUT``, or
  76. ``Pin.OPEN_DRAIN``, the alternate function will be removed from the pin.
  77. Methods
  78. -------
  79. .. method:: Pin.init(mode=-1, pull=-1, \*, value, drive, alt)
  80. Re-initialise the pin using the given parameters. Only those arguments that
  81. are specified will be set. The rest of the pin peripheral state will remain
  82. unchanged. See the constructor documentation for details of the arguments.
  83. Returns ``None``.
  84. .. method:: Pin.value([x])
  85. This method allows to set and get the value of the pin, depending on whether
  86. the argument ``x`` is supplied or not.
  87. If the argument is omitted then this method gets the digital logic level of
  88. the pin, returning 0 or 1 corresponding to low and high voltage signals
  89. respectively. The behaviour of this method depends on the mode of the pin:
  90. - ``Pin.IN`` - The method returns the actual input value currently present
  91. on the pin.
  92. - ``Pin.OUT`` - The behaviour and return value of the method is undefined.
  93. - ``Pin.OPEN_DRAIN`` - If the pin is in state '0' then the behaviour and
  94. return value of the method is undefined. Otherwise, if the pin is in
  95. state '1', the method returns the actual input value currently present
  96. on the pin.
  97. If the argument is supplied then this method sets the digital logic level of
  98. the pin. The argument ``x`` can be anything that converts to a boolean.
  99. If it converts to ``True``, the pin is set to state '1', otherwise it is set
  100. to state '0'. The behaviour of this method depends on the mode of the pin:
  101. - ``Pin.IN`` - The value is stored in the output buffer for the pin. The
  102. pin state does not change, it remains in the high-impedance state. The
  103. stored value will become active on the pin as soon as it is changed to
  104. ``Pin.OUT`` or ``Pin.OPEN_DRAIN`` mode.
  105. - ``Pin.OUT`` - The output buffer is set to the given value immediately.
  106. - ``Pin.OPEN_DRAIN`` - If the value is '0' the pin is set to a low voltage
  107. state. Otherwise the pin is set to high-impedance state.
  108. When setting the value this method returns ``None``.
  109. .. method:: Pin.__call__([x])
  110. Pin objects are callable. The call method provides a (fast) shortcut to set
  111. and get the value of the pin. It is equivalent to Pin.value([x]).
  112. See :meth:`Pin.value` for more details.
  113. .. method:: Pin.on()
  114. Set pin to "1" output level.
  115. .. method:: Pin.off()
  116. Set pin to "0" output level.
  117. .. method:: Pin.mode([mode])
  118. Get or set the pin mode.
  119. See the constructor documentation for details of the ``mode`` argument.
  120. .. method:: Pin.pull([pull])
  121. Get or set the pin pull state.
  122. See the constructor documentation for details of the ``pull`` argument.
  123. .. method:: Pin.drive([drive])
  124. Get or set the pin drive strength.
  125. See the constructor documentation for details of the ``drive`` argument.
  126. Not all ports implement this method.
  127. Availability: WiPy.
  128. .. method:: Pin.irq(handler=None, trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), \*, priority=1, wake=None)
  129. Configure an interrupt handler to be called when the trigger source of the
  130. pin is active. If the pin mode is ``Pin.IN`` then the trigger source is
  131. the external value on the pin. If the pin mode is ``Pin.OUT`` then the
  132. trigger source is the output buffer of the pin. Otherwise, if the pin mode
  133. is ``Pin.OPEN_DRAIN`` then the trigger source is the output buffer for
  134. state '0' and the external pin value for state '1'.
  135. The arguments are:
  136. - ``handler`` is an optional function to be called when the interrupt
  137. triggers.
  138. - ``trigger`` configures the event which can generate an interrupt.
  139. Possible values are:
  140. - ``Pin.IRQ_FALLING`` interrupt on falling edge.
  141. - ``Pin.IRQ_RISING`` interrupt on rising edge.
  142. - ``Pin.IRQ_LOW_LEVEL`` interrupt on low level.
  143. - ``Pin.IRQ_HIGH_LEVEL`` interrupt on high level.
  144. These values can be OR'ed together to trigger on multiple events.
  145. - ``priority`` sets the priority level of the interrupt. The values it
  146. can take are port-specific, but higher values always represent higher
  147. priorities.
  148. - ``wake`` selects the power mode in which this interrupt can wake up the
  149. system. It can be ``machine.IDLE``, ``machine.SLEEP`` or ``machine.DEEPSLEEP``.
  150. These values can also be OR'ed together to make a pin generate interrupts in
  151. more than one power mode.
  152. This method returns a callback object.
  153. Constants
  154. ---------
  155. The following constants are used to configure the pin objects. Note that
  156. not all constants are available on all ports.
  157. .. data:: Pin.IN
  158. Pin.OUT
  159. Pin.OPEN_DRAIN
  160. Pin.ALT
  161. Pin.ALT_OPEN_DRAIN
  162. Selects the pin mode.
  163. .. data:: Pin.PULL_UP
  164. Pin.PULL_DOWN
  165. Selects whether there is a pull up/down resistor. Use the value
  166. ``None`` for no pull.
  167. .. data:: Pin.LOW_POWER
  168. Pin.MED_POWER
  169. Pin.HIGH_POWER
  170. Selects the pin drive strength.
  171. .. data:: Pin.IRQ_FALLING
  172. Pin.IRQ_RISING
  173. Pin.IRQ_LOW_LEVEL
  174. Pin.IRQ_HIGH_LEVEL
  175. Selects the IRQ trigger type.