Skip to content

Instantly share code, notes, and snippets.

@teddokano
Created January 22, 2023 06:38
Show Gist options
  • Save teddokano/5a6c7f0d0e86e51cb47304b7ca177eb3 to your computer and use it in GitHub Desktop.
Save teddokano/5a6c7f0d0e86e51cb47304b7ca177eb3 to your computer and use it in GitHub Desktop.
from machine import Pin, I2C, Timer
from utime import sleep
import machine
ADR = 0x7A >>1
class PTN5150A:
def __init__( self, i2c, address = ADR ):
self.__i2c = i2c
self.__addr = ADR
self.intr = Pin( 2, Pin.IN )
self.intr.irq( trigger = Pin.IRQ_FALLING, handler = self.callback )
self.int_flag = False
self.read( 0x03 ) # read register and clear INT output
def write( self, r, v ):
self.__i2c.writeto( self.__addr, bytearray( [ r, v ] ) )
def read( self, r ):
self.__i2c.writeto( self.__addr, bytearray( [ r ] ) )
return self.__i2c.readfrom( self.__addr, 1 )[ 0 ]
def show_reg( self, r ):
print( " reg:0x{:02X} = 0x{:02X}".format( r, self.read( r ) ) )
def show_dump( self ):
reg_list = [ 0x01, 0x02, 0x04, 0x09, 0x0A, 0x18 ]
for r in reg_list:
self.show_reg( r )
def callback( self, pin_obj ):
self.int_flag = True
def check_int( self ):
r = self.int_flag
self.int_flag = False
return r
class HW_trigger:
def __init__( self, num ):
self.pin = Pin( num, Pin.OUT )
self.pin.value( 1 )
def kick( self ):
self.pin.value( 0 )
self.pin.value( 1 )
def main():
i2c = I2C( 0, sda = Pin( 0 ), scl = Pin( 1 ), freq = 400_000 )
cc_logic = PTN5150A( i2c )
trig = HW_trigger( 3 )
print( "switch to DRP (as initial state)" )
cc_logic.write( 0x02, 0x04 )
cc_logic.write( 0x18, 0x1F )
sleep( 1 )
cc_logic.show_dump()
print( "--- TEST STARTED ---" )
state = None
tim_flag = False
def tim_cb( tim_obj ):
nonlocal tim_flag
tim_flag = True
tim0 = Timer()
role_msg = [ "Not Connected", "DFP attached", "UFP attached", "Analog Audio Accessory attached", "Debug Accessory attached", "", "", "" ]
while True:
if tim_flag:
tim_flag = False
print( "INT by timer" )
status04 = cc_logic.read( 0x04 )
dev_role = (status04 >> 2) & 0x7
print( " reg:0x{:02X} = 0x{:02X}, {}".format( 0x04, status04, role_msg[ dev_role ] ) )
if dev_role is 0x00:
cc_logic.write( 0x02, 0x02 ) # Set the port to DFP
state = "DFP"
print( " port is set to DFP" )
else:
print( " nothing done for timer INT" )
if cc_logic.check_int():
print( "INT by PTN5150A" )
cc_logic.show_reg( 0x03 ) # read register and clear INT output
status19 = cc_logic.read( 0x19 )
print( " reg:0x{:02X} = 0x{:02X}".format( 0x19, status19 ) )
if status19:
status04 = cc_logic.read( 0x04 )
dev_role = (status04 >> 2) & 0x7
print( " reg:0x{:02X} = 0x{:02X}, {}".format( 0x04, status04, role_msg[ dev_role ] ) )
if (dev_role is 0x1):
state = "UFP"
elif (dev_role is 0x2) and (state is None):
trig.kick()
print( " ********** forcing the port as UFP (act as a device)" )
state = "Try_SNK"
cc_logic.write( 0x02, 0x00 ) # Set the port to UFP
tim0.init( mode = Timer.ONE_SHOT, period = 500, callback = tim_cb)
elif (dev_role is 0x00) and (state is "Try_SNK"):
pass
elif (dev_role is 0x00) and (state is not "Try_SNK"):
state = None
print( " configure the port as DRP (because cable detach detected)" )
cc_logic.write( 0x02, 0x04 ) # Set the port to DRP
else:
print( " else: dev_role=0x{:01X}, {}".format( dev_role, state ) )
pass
print( " New status --> {}".format( state ) )
if not state:
print( "" )
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment