2015-10-26 20:46:36 +00:00
|
|
|
"""
|
|
|
|
This test need a set of pins which can be set as inputs and have no external
|
|
|
|
pull up or pull down connected.
|
|
|
|
GP12 and GP17 must be connected together
|
2015-09-03 10:25:44 +01:00
|
|
|
"""
|
2015-09-27 12:45:48 +01:00
|
|
|
from machine import Pin
|
2015-09-03 10:25:44 +01:00
|
|
|
import os
|
|
|
|
|
2015-09-27 12:45:48 +01:00
|
|
|
mch = os.uname().machine
|
|
|
|
if "LaunchPad" in mch:
|
2015-09-03 10:25:44 +01:00
|
|
|
pin_map = [
|
|
|
|
"GP24",
|
|
|
|
"GP12",
|
|
|
|
"GP14",
|
|
|
|
"GP15",
|
|
|
|
"GP16",
|
|
|
|
"GP17",
|
|
|
|
"GP28",
|
|
|
|
"GP8",
|
|
|
|
"GP6",
|
|
|
|
"GP30",
|
|
|
|
"GP31",
|
|
|
|
"GP3",
|
|
|
|
"GP0",
|
|
|
|
"GP4",
|
|
|
|
"GP5",
|
|
|
|
]
|
2015-09-07 20:19:11 +01:00
|
|
|
max_af_idx = 15
|
2015-09-27 12:45:48 +01:00
|
|
|
elif "WiPy" in mch:
|
2015-09-03 10:25:44 +01:00
|
|
|
pin_map = [
|
|
|
|
"GP23",
|
|
|
|
"GP24",
|
|
|
|
"GP12",
|
|
|
|
"GP13",
|
|
|
|
"GP14",
|
|
|
|
"GP9",
|
|
|
|
"GP17",
|
|
|
|
"GP28",
|
|
|
|
"GP22",
|
|
|
|
"GP8",
|
|
|
|
"GP30",
|
|
|
|
"GP31",
|
|
|
|
"GP0",
|
|
|
|
"GP4",
|
|
|
|
"GP5",
|
|
|
|
]
|
2015-09-07 20:19:11 +01:00
|
|
|
max_af_idx = 15
|
2015-09-03 10:25:44 +01:00
|
|
|
else:
|
|
|
|
raise Exception("Board not supported!")
|
|
|
|
|
2015-10-26 20:46:36 +00:00
|
|
|
# test initial value
|
|
|
|
p = Pin("GP12", Pin.IN)
|
|
|
|
Pin("GP17", Pin.OUT, value=1)
|
|
|
|
print(p() == 1)
|
|
|
|
Pin("GP17", Pin.OUT, value=0)
|
|
|
|
print(p() == 0)
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2015-09-03 10:25:44 +01:00
|
|
|
def test_noinit():
|
|
|
|
for p in pin_map:
|
|
|
|
pin = Pin(p)
|
|
|
|
pin.value()
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2015-09-03 10:25:44 +01:00
|
|
|
def test_pin_read(pull):
|
|
|
|
# enable the pull resistor on all pins, then read the value
|
|
|
|
for p in pin_map:
|
|
|
|
pin = Pin(p, mode=Pin.IN, pull=pull)
|
2015-09-07 08:23:46 +01:00
|
|
|
for p in pin_map:
|
2015-09-03 10:25:44 +01:00
|
|
|
print(pin())
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2015-09-03 10:25:44 +01:00
|
|
|
def test_pin_af():
|
|
|
|
for p in pin_map:
|
2015-09-07 20:19:11 +01:00
|
|
|
for af in Pin(p).alt_list():
|
|
|
|
if af[1] <= max_af_idx:
|
|
|
|
Pin(p, mode=Pin.ALT, alt=af[1])
|
|
|
|
Pin(p, mode=Pin.ALT_OPEN_DRAIN, alt=af[1])
|
2015-09-03 10:25:44 +01:00
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2015-09-03 10:25:44 +01:00
|
|
|
# test un-initialized pins
|
|
|
|
test_noinit()
|
|
|
|
# test with pull-up and pull-down
|
|
|
|
test_pin_read(Pin.PULL_UP)
|
|
|
|
test_pin_read(Pin.PULL_DOWN)
|
|
|
|
|
|
|
|
# test all constructor combinations
|
|
|
|
pin = Pin(pin_map[0])
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.IN)
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.OUT)
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.IN, pull=Pin.PULL_DOWN)
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.IN, pull=Pin.PULL_UP)
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.OPEN_DRAIN, pull=Pin.PULL_UP)
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_DOWN)
|
2015-09-13 14:31:17 +01:00
|
|
|
pin = Pin(pin_map[0], mode=Pin.OUT, pull=None)
|
2015-09-03 10:25:44 +01:00
|
|
|
pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP)
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.LOW_POWER)
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.MED_POWER)
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.HIGH_POWER)
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.OUT, drive=pin.LOW_POWER)
|
|
|
|
pin = Pin(pin_map[0], Pin.OUT, Pin.PULL_DOWN)
|
|
|
|
pin = Pin(pin_map[0], Pin.ALT, Pin.PULL_UP)
|
|
|
|
pin = Pin(pin_map[0], Pin.ALT_OPEN_DRAIN, Pin.PULL_UP)
|
|
|
|
test_pin_af() # try the entire af range on all pins
|
|
|
|
|
|
|
|
# test pin init and printing
|
|
|
|
pin = Pin(pin_map[0])
|
|
|
|
pin.init(mode=Pin.IN)
|
|
|
|
print(pin)
|
|
|
|
pin.init(Pin.IN, Pin.PULL_DOWN)
|
|
|
|
print(pin)
|
|
|
|
pin.init(mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.LOW_POWER)
|
|
|
|
print(pin)
|
|
|
|
pin.init(mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.HIGH_POWER)
|
|
|
|
print(pin)
|
|
|
|
|
|
|
|
# test value in OUT mode
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.OUT)
|
2015-09-04 13:36:52 +01:00
|
|
|
pin.value(0)
|
2015-09-03 10:25:44 +01:00
|
|
|
pin.toggle() # test toggle
|
|
|
|
print(pin())
|
|
|
|
pin.toggle() # test toggle again
|
|
|
|
print(pin())
|
|
|
|
# test different value settings
|
|
|
|
pin(1)
|
|
|
|
print(pin.value())
|
|
|
|
pin(0)
|
|
|
|
print(pin.value())
|
|
|
|
pin.value(1)
|
|
|
|
print(pin())
|
|
|
|
pin.value(0)
|
|
|
|
print(pin())
|
|
|
|
|
|
|
|
# test all getters and setters
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.OUT)
|
|
|
|
# mode
|
|
|
|
print(pin.mode() == Pin.OUT)
|
|
|
|
pin.mode(Pin.IN)
|
|
|
|
print(pin.mode() == Pin.IN)
|
|
|
|
# pull
|
2015-09-13 14:31:17 +01:00
|
|
|
pin.pull(None)
|
|
|
|
print(pin.pull() == None)
|
2015-09-03 10:25:44 +01:00
|
|
|
pin.pull(Pin.PULL_DOWN)
|
|
|
|
print(pin.pull() == Pin.PULL_DOWN)
|
|
|
|
# drive
|
|
|
|
pin.drive(Pin.MED_POWER)
|
|
|
|
print(pin.drive() == Pin.MED_POWER)
|
|
|
|
pin.drive(Pin.HIGH_POWER)
|
|
|
|
print(pin.drive() == Pin.HIGH_POWER)
|
|
|
|
# id
|
|
|
|
print(pin.id() == pin_map[0])
|
|
|
|
|
|
|
|
# all the next ones MUST raise
|
|
|
|
try:
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.IN) # incorrect drive value
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.LOW_POWER, pull=Pin.PULL_UP) # incorrect mode value
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
pin = Pin(pin_map[0], mode=Pin.IN, pull=Pin.HIGH_POWER) # incorrect pull value
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
pin = Pin("A0", Pin.OUT, Pin.PULL_DOWN) # incorrect pin id
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
pin = Pin(pin_map[0], Pin.IN, Pin.PULL_UP, alt=0) # af specified in GPIO mode
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
pin = Pin(pin_map[0], Pin.OUT, Pin.PULL_UP, alt=7) # af specified in GPIO mode
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
pin = Pin(pin_map[0], Pin.ALT, Pin.PULL_UP, alt=0) # incorrect af
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
pin = Pin(pin_map[0], Pin.ALT_OPEN_DRAIN, Pin.PULL_UP, alt=-1) # incorrect af
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
pin = Pin(pin_map[0], Pin.ALT_OPEN_DRAIN, Pin.PULL_UP, alt=16) # incorrect af
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
pin.mode(Pin.PULL_UP) # incorrect pin mode
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
pin.pull(Pin.OUT) # incorrect pull
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
pin.drive(Pin.IN) # incorrect drive strength
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
pin.id("ABC") # id cannot be set
|
|
|
|
except Exception:
|
|
|
|
print("Exception")
|