# init the MCP23017
mcp=0x27
iodira=0x00
iodirb=0x10
gpioa=0x09
gpiob=0x19
gppua=0x06
gppub=0x16
olata=0x0a
olatb=0x1a
i2cset -y 2 $mcp $iodira 0x3f # portA 0-5 inputs, 6-7 outputs
i2cset -y 2 $mcp $iodira 0x3f # do it twice.
i2cset -y 2 $mcp $iodirb 0xff # portB 0-7 inputs
i2cset -y 2 $mcp $iodirb 0xff # do it twice.
i2cset -y 2 $mcp $gpioa 0x3f # 8-13 high, 14 & 15 low
i2cset -y 2 $mcp $gppua 0x3f # pullups 8-13
i2cset -y 2 $mcp $gppub 0xff # pullups 0-7
i2cset -y 2 $mcp $olata 0 # both FETs off
i2cset -y 2 $mcp $olata 64 # FET2 on = reset counter
sleep .1
i2cset -y 2 $mcp $olata 0 # both FETs off
error_count=0
COUNTER=0
COUNTER2=0
while [ $COUNTER -lt 100000 ]; do
((COUNTER2 +=1))
if [[ $COUNTER2 = 31 ]] ;
then # echo wink $COUNTER # wink the reset?
i2cset -y 2 $mcp $olata 64 # FET2 on = reset counter
sleep .01
i2cset -y 2 $mcp $olata 0 # both FETs off.
sleep .01
(( COUNTER2 = 0))
Expected=0x3ffe # expected read back of inputs.
else
#echo notwinking
i2cset -y 2 $mcp $olata 128 # FET1 on = pulse counter
sleep .01
i2cset -y 2 $mcp $olata 0 # both FETs off.
sleep .01
fi
((COUNTER++))
done
# run as unbuffered...
# python -u OMCB_IO_11.py
# same as version 5 but much longer loop.
# also added counter reset to eack loop so I can se the FET switching.
# try to read input and compare it to expected result.
# add error counter
# patch bash calls into it.
import subprocess
import time
# init the MCP23017
mcp = 0x27
iodira = 0x00
iodirb = 0x10
gpioa = 0x09
gpiob = 0x19
gppua = 0x06
gppub = 0x16
olata = 0x0a
olatb = 0x1a
error_count=0
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x00", "0x3f"]) # portA 0-5 inputs, 6-7 outputs
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x00", "0x3f"]) # do it twice.
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x10", "0xff"]) # portB 0-7 inputs
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x10", "0xff"]) # do it twice.
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x09", "0x3f"]) # 8-13 high, 14 & 15 low
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x06", "0x3f"]) # pullups 8-13
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x16", "0xff"]) # pullups 0-7
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x0a", "0"]) # both FETs off
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x0a", "64"]) # FET2 on = reset counter
subprocess.call([ "sleep", ".1"])
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x0a", "0" ]) # both FETs off
error_count ==0
for j in range(1000000): # a loop sounter
if (j&31) == 0: # wink the reset?
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x0a", "64"]) # FET2 on = reset counter
subprocess.call([ "sleep", ".1"])
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x0a", "0" ]) # both FETs off
Expected = 0x3ffe # expected read back of inputs.
print('\r'),
PA = (subprocess.call(["i2cget", "-y", "2", "0x27", "0x0a",])) # read portA
PB = (subprocess.call(["i2cget", "-y", "2", "0x27", "0x1a",])) # read portB
PX = PB + (PA * 256)
if (PX == Expected):
print("PortA="),
print'{:06b}'.format (PA), # print PortA inputs
print(" PortB="),
print'{:08b}'.format (PB), # printb PortB inputs
else:
error_count = error_count + 1
print
print("Expected="),
print'{:14b}'.format (Expected),
print(" Read="),
print'{:14b}'.format (PX),
print(" Errors="),
print error_count
if (j&1) == 1: # inc input sounter every 2 loops
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x0a", "128"]) # FET1 on = reset co$
subprocess.call([ "sleep", ".1"])
subprocess.call([ "i2cset", "-y", "2", "0x27", "0x0a", "0" ]) # both FETs off
Expected = (((Expected * 2) +1)& 0x3fff)
subprocess.call([ "sleep", ".1"])
sudo /opt/scripts/tools/version.sh
bootloader:[microSD-(push-button)]:[/dev/mmcblk0]:[U-Boot 2018.03-00002-gac9cce7c6a]:[location: dd MBR]
https://github.com/tdack/Adafruit-Raspberry-Pi-Python-Code/tree/abd95a03492e678ac3d0e49357f38bc395beb418/Adafruit_MCP230xx
https://github.com/tdack/Adafruit-Raspberry-Pi-Python-Code