pin.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. """
  2. This test need a set of pins which can be set as inputs and have no external
  3. pull up or pull down connected.
  4. GP12 and GP17 must be connected together
  5. """
  6. from machine import Pin
  7. import os
  8. mch = os.uname().machine
  9. if 'LaunchPad' in mch:
  10. pin_map = ['GP24', 'GP12', 'GP14', 'GP15', 'GP16', 'GP17', 'GP28', 'GP8', 'GP6', 'GP30', 'GP31', 'GP3', 'GP0', 'GP4', 'GP5']
  11. max_af_idx = 15
  12. elif 'WiPy' in mch:
  13. pin_map = ['GP23', 'GP24', 'GP12', 'GP13', 'GP14', 'GP9', 'GP17', 'GP28', 'GP22', 'GP8', 'GP30', 'GP31', 'GP0', 'GP4', 'GP5']
  14. max_af_idx = 15
  15. else:
  16. raise Exception('Board not supported!')
  17. # test initial value
  18. p = Pin('GP12', Pin.IN)
  19. Pin('GP17', Pin.OUT, value=1)
  20. print(p() == 1)
  21. Pin('GP17', Pin.OUT, value=0)
  22. print(p() == 0)
  23. def test_noinit():
  24. for p in pin_map:
  25. pin = Pin(p)
  26. pin.value()
  27. def test_pin_read(pull):
  28. # enable the pull resistor on all pins, then read the value
  29. for p in pin_map:
  30. pin = Pin(p, mode=Pin.IN, pull=pull)
  31. for p in pin_map:
  32. print(pin())
  33. def test_pin_af():
  34. for p in pin_map:
  35. for af in Pin(p).alt_list():
  36. if af[1] <= max_af_idx:
  37. Pin(p, mode=Pin.ALT, alt=af[1])
  38. Pin(p, mode=Pin.ALT_OPEN_DRAIN, alt=af[1])
  39. # test un-initialized pins
  40. test_noinit()
  41. # test with pull-up and pull-down
  42. test_pin_read(Pin.PULL_UP)
  43. test_pin_read(Pin.PULL_DOWN)
  44. # test all constructor combinations
  45. pin = Pin(pin_map[0])
  46. pin = Pin(pin_map[0], mode=Pin.IN)
  47. pin = Pin(pin_map[0], mode=Pin.OUT)
  48. pin = Pin(pin_map[0], mode=Pin.IN, pull=Pin.PULL_DOWN)
  49. pin = Pin(pin_map[0], mode=Pin.IN, pull=Pin.PULL_UP)
  50. pin = Pin(pin_map[0], mode=Pin.OPEN_DRAIN, pull=Pin.PULL_UP)
  51. pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_DOWN)
  52. pin = Pin(pin_map[0], mode=Pin.OUT, pull=None)
  53. pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP)
  54. pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.LOW_POWER)
  55. pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.MED_POWER)
  56. pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.HIGH_POWER)
  57. pin = Pin(pin_map[0], mode=Pin.OUT, drive=pin.LOW_POWER)
  58. pin = Pin(pin_map[0], Pin.OUT, Pin.PULL_DOWN)
  59. pin = Pin(pin_map[0], Pin.ALT, Pin.PULL_UP)
  60. pin = Pin(pin_map[0], Pin.ALT_OPEN_DRAIN, Pin.PULL_UP)
  61. test_pin_af() # try the entire af range on all pins
  62. # test pin init and printing
  63. pin = Pin(pin_map[0])
  64. pin.init(mode=Pin.IN)
  65. print(pin)
  66. pin.init(Pin.IN, Pin.PULL_DOWN)
  67. print(pin)
  68. pin.init(mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.LOW_POWER)
  69. print(pin)
  70. pin.init(mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.HIGH_POWER)
  71. print(pin)
  72. # test value in OUT mode
  73. pin = Pin(pin_map[0], mode=Pin.OUT)
  74. pin.value(0)
  75. pin.toggle() # test toggle
  76. print(pin())
  77. pin.toggle() # test toggle again
  78. print(pin())
  79. # test different value settings
  80. pin(1)
  81. print(pin.value())
  82. pin(0)
  83. print(pin.value())
  84. pin.value(1)
  85. print(pin())
  86. pin.value(0)
  87. print(pin())
  88. # test all getters and setters
  89. pin = Pin(pin_map[0], mode=Pin.OUT)
  90. # mode
  91. print(pin.mode() == Pin.OUT)
  92. pin.mode(Pin.IN)
  93. print(pin.mode() == Pin.IN)
  94. # pull
  95. pin.pull(None)
  96. print(pin.pull() == None)
  97. pin.pull(Pin.PULL_DOWN)
  98. print(pin.pull() == Pin.PULL_DOWN)
  99. # drive
  100. pin.drive(Pin.MED_POWER)
  101. print(pin.drive() == Pin.MED_POWER)
  102. pin.drive(Pin.HIGH_POWER)
  103. print(pin.drive() == Pin.HIGH_POWER)
  104. # id
  105. print(pin.id() == pin_map[0])
  106. # all the next ones MUST raise
  107. try:
  108. pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.IN) # incorrect drive value
  109. except Exception:
  110. print('Exception')
  111. try:
  112. pin = Pin(pin_map[0], mode=Pin.LOW_POWER, pull=Pin.PULL_UP) # incorrect mode value
  113. except Exception:
  114. print('Exception')
  115. try:
  116. pin = Pin(pin_map[0], mode=Pin.IN, pull=Pin.HIGH_POWER) # incorrect pull value
  117. except Exception:
  118. print('Exception')
  119. try:
  120. pin = Pin('A0', Pin.OUT, Pin.PULL_DOWN) # incorrect pin id
  121. except Exception:
  122. print('Exception')
  123. try:
  124. pin = Pin(pin_map[0], Pin.IN, Pin.PULL_UP, alt=0) # af specified in GPIO mode
  125. except Exception:
  126. print('Exception')
  127. try:
  128. pin = Pin(pin_map[0], Pin.OUT, Pin.PULL_UP, alt=7) # af specified in GPIO mode
  129. except Exception:
  130. print('Exception')
  131. try:
  132. pin = Pin(pin_map[0], Pin.ALT, Pin.PULL_UP, alt=0) # incorrect af
  133. except Exception:
  134. print('Exception')
  135. try:
  136. pin = Pin(pin_map[0], Pin.ALT_OPEN_DRAIN, Pin.PULL_UP, alt=-1) # incorrect af
  137. except Exception:
  138. print('Exception')
  139. try:
  140. pin = Pin(pin_map[0], Pin.ALT_OPEN_DRAIN, Pin.PULL_UP, alt=16) # incorrect af
  141. except Exception:
  142. print('Exception')
  143. try:
  144. pin.mode(Pin.PULL_UP) # incorrect pin mode
  145. except Exception:
  146. print('Exception')
  147. try:
  148. pin.pull(Pin.OUT) # incorrect pull
  149. except Exception:
  150. print('Exception')
  151. try:
  152. pin.drive(Pin.IN) # incorrect drive strength
  153. except Exception:
  154. print('Exception')
  155. try:
  156. pin.id('ABC') # id cannot be set
  157. except Exception:
  158. print('Exception')