pyb.Pin.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. .. currentmodule:: pyb
  2. .. _pyb.Pin:
  3. class Pin -- control I/O pins
  4. =============================
  5. A pin is the basic object to control I/O pins. It has methods to set
  6. the mode of the pin (input, output, etc) and methods to get and set the
  7. digital logic level. For analog control of a pin, see the ADC class.
  8. Usage Model:
  9. All Board Pins are predefined as pyb.Pin.board.Name::
  10. x1_pin = pyb.Pin.board.X1
  11. g = pyb.Pin(pyb.Pin.board.X1, pyb.Pin.IN)
  12. CPU pins which correspond to the board pins are available
  13. as ``pyb.cpu.Name``. For the CPU pins, the names are the port letter
  14. followed by the pin number. On the PYBv1.0, ``pyb.Pin.board.X1`` and
  15. ``pyb.Pin.cpu.A0`` are the same pin.
  16. You can also use strings::
  17. g = pyb.Pin('X1', pyb.Pin.OUT_PP)
  18. Users can add their own names::
  19. MyMapperDict = { 'LeftMotorDir' : pyb.Pin.cpu.C12 }
  20. pyb.Pin.dict(MyMapperDict)
  21. g = pyb.Pin("LeftMotorDir", pyb.Pin.OUT_OD)
  22. and can query mappings::
  23. pin = pyb.Pin("LeftMotorDir")
  24. Users can also add their own mapping function::
  25. def MyMapper(pin_name):
  26. if pin_name == "LeftMotorDir":
  27. return pyb.Pin.cpu.A0
  28. pyb.Pin.mapper(MyMapper)
  29. So, if you were to call: ``pyb.Pin("LeftMotorDir", pyb.Pin.OUT_PP)``
  30. then ``"LeftMotorDir"`` is passed directly to the mapper function.
  31. To summarise, the following order determines how things get mapped into
  32. an ordinal pin number:
  33. 1. Directly specify a pin object
  34. 2. User supplied mapping function
  35. 3. User supplied mapping (object must be usable as a dictionary key)
  36. 4. Supply a string which matches a board pin
  37. 5. Supply a string which matches a CPU port/pin
  38. You can set ``pyb.Pin.debug(True)`` to get some debug information about
  39. how a particular object gets mapped to a pin.
  40. When a pin has the ``Pin.PULL_UP`` or ``Pin.PULL_DOWN`` pull-mode enabled,
  41. that pin has an effective 40k Ohm resistor pulling it to 3V3 or GND
  42. respectively (except pin Y5 which has 11k Ohm resistors).
  43. Now every time a falling edge is seen on the gpio pin, the callback will be
  44. executed. Caution: mechanical push buttons have "bounce" and pushing or
  45. releasing a switch will often generate multiple edges.
  46. See: http://www.eng.utah.edu/~cs5780/debouncing.pdf for a detailed
  47. explanation, along with various techniques for debouncing.
  48. All pin objects go through the pin mapper to come up with one of the
  49. gpio pins.
  50. Constructors
  51. ------------
  52. .. class:: pyb.Pin(id, ...)
  53. Create a new Pin object associated with the id. If additional arguments are given,
  54. they are used to initialise the pin. See :meth:`pin.init`.
  55. Class methods
  56. -------------
  57. .. classmethod:: Pin.debug([state])
  58. Get or set the debugging state (``True`` or ``False`` for on or off).
  59. .. classmethod:: Pin.dict([dict])
  60. Get or set the pin mapper dictionary.
  61. .. classmethod:: Pin.mapper([fun])
  62. Get or set the pin mapper function.
  63. Methods
  64. -------
  65. .. method:: Pin.init(mode, pull=Pin.PULL_NONE, af=-1)
  66. Initialise the pin:
  67. - ``mode`` can be one of:
  68. - ``Pin.IN`` - configure the pin for input;
  69. - ``Pin.OUT_PP`` - configure the pin for output, with push-pull control;
  70. - ``Pin.OUT_OD`` - configure the pin for output, with open-drain control;
  71. - ``Pin.AF_PP`` - configure the pin for alternate function, pull-pull;
  72. - ``Pin.AF_OD`` - configure the pin for alternate function, open-drain;
  73. - ``Pin.ANALOG`` - configure the pin for analog.
  74. - ``pull`` can be one of:
  75. - ``Pin.PULL_NONE`` - no pull up or down resistors;
  76. - ``Pin.PULL_UP`` - enable the pull-up resistor;
  77. - ``Pin.PULL_DOWN`` - enable the pull-down resistor.
  78. - when mode is ``Pin.AF_PP`` or ``Pin.AF_OD``, then af can be the index or name
  79. of one of the alternate functions associated with a pin.
  80. Returns: ``None``.
  81. .. method:: Pin.value([value])
  82. Get or set the digital logic level of the pin:
  83. - With no argument, return 0 or 1 depending on the logic level of the pin.
  84. - With ``value`` given, set the logic level of the pin. ``value`` can be
  85. anything that converts to a boolean. If it converts to ``True``, the pin
  86. is set high, otherwise it is set low.
  87. .. method:: Pin.__str__()
  88. Return a string describing the pin object.
  89. .. method:: Pin.af()
  90. Returns the currently configured alternate-function of the pin. The
  91. integer returned will match one of the allowed constants for the af
  92. argument to the init function.
  93. .. method:: Pin.af_list()
  94. Returns an array of alternate functions available for this pin.
  95. .. method:: Pin.gpio()
  96. Returns the base address of the GPIO block associated with this pin.
  97. .. method:: Pin.mode()
  98. Returns the currently configured mode of the pin. The integer returned
  99. will match one of the allowed constants for the mode argument to the init
  100. function.
  101. .. method:: Pin.name()
  102. Get the pin name.
  103. .. method:: Pin.names()
  104. Returns the cpu and board names for this pin.
  105. .. method:: Pin.pin()
  106. Get the pin number.
  107. .. method:: Pin.port()
  108. Get the pin port.
  109. .. method:: Pin.pull()
  110. Returns the currently configured pull of the pin. The integer returned
  111. will match one of the allowed constants for the pull argument to the init
  112. function.
  113. Constants
  114. ---------
  115. .. data:: Pin.AF_OD
  116. initialise the pin to alternate-function mode with an open-drain drive
  117. .. data:: Pin.AF_PP
  118. initialise the pin to alternate-function mode with a push-pull drive
  119. .. data:: Pin.ANALOG
  120. initialise the pin to analog mode
  121. .. data:: Pin.IN
  122. initialise the pin to input mode
  123. .. data:: Pin.OUT_OD
  124. initialise the pin to output mode with an open-drain drive
  125. .. data:: Pin.OUT_PP
  126. initialise the pin to output mode with a push-pull drive
  127. .. data:: Pin.PULL_DOWN
  128. enable the pull-down resistor on the pin
  129. .. data:: Pin.PULL_NONE
  130. don't enable any pull up or down resistors on the pin
  131. .. data:: Pin.PULL_UP
  132. enable the pull-up resistor on the pin
  133. class PinAF -- Pin Alternate Functions
  134. ======================================
  135. A Pin represents a physical pin on the microprocessor. Each pin
  136. can have a variety of functions (GPIO, I2C SDA, etc). Each PinAF
  137. object represents a particular function for a pin.
  138. Usage Model::
  139. x3 = pyb.Pin.board.X3
  140. x3_af = x3.af_list()
  141. x3_af will now contain an array of PinAF objects which are available on
  142. pin X3.
  143. For the pyboard, x3_af would contain:
  144. [Pin.AF1_TIM2, Pin.AF2_TIM5, Pin.AF3_TIM9, Pin.AF7_USART2]
  145. Normally, each peripheral would configure the af automatically, but sometimes
  146. the same function is available on multiple pins, and having more control
  147. is desired.
  148. To configure X3 to expose TIM2_CH3, you could use::
  149. pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.AF_PP, af=pyb.Pin.AF1_TIM2)
  150. or::
  151. pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.AF_PP, af=1)
  152. Methods
  153. -------
  154. .. method:: pinaf.__str__()
  155. Return a string describing the alternate function.
  156. .. method:: pinaf.index()
  157. Return the alternate function index.
  158. .. method:: pinaf.name()
  159. Return the name of the alternate function.
  160. .. method:: pinaf.reg()
  161. Return the base register associated with the peripheral assigned to this
  162. alternate function. For example, if the alternate function were TIM2_CH3
  163. this would return stm.TIM2