Created
January 17, 2023 20:43
-
-
Save teddokano/d9b46600560bd38444683d5fe4d2908c to your computer and use it in GitHub Desktop.
PTN5150A forcing the port as UFP when it become DFP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_state( self ): | |
r = self.int_flag | |
self.int_flag = False | |
return r | |
def main(): | |
i2c = I2C( 0, sda = Pin( 0 ), scl = Pin( 1 ), freq = 400_000 ) | |
#print( i2c.scan() ) | |
cc_logic = PTN5150A( i2c ) | |
print( "switch to DRP (as initial state)" ) | |
cc_logic.write( 0x02, 0x04 ) | |
cc_logic.write( 0x18, 0x1F ) | |
sleep( 1 ) | |
print( "--- TEST START ---" ) | |
cc_logic.show_dump() | |
while True: | |
if cc_logic.check_state(): | |
print( "INT" ) | |
cc_logic.show_reg( 0x03 ) # read register and clear INT output | |
status = cc_logic.read( 0x19 ) | |
print( "reg:0x{:02X} = 0x{:02X}".format( 0x19, status ) ) | |
if status: | |
cc_status = cc_logic.read( 0x04 ) | |
print( "reg:0x{:02X} = 0x{:02X}".format( 0x04, cc_status ) ) | |
msg = [ "Not Connected", "DFP attached", "UFP attached", "Analog Audio Accessory attached", "Debug Accessory attached", "", "", "" ] | |
print( msg[ (cc_status >> 2) & 0x7 ] ) | |
if ( cc_status & 0x1C ) is 0x08: | |
print( "********** forcing the port as UFP (act as a device)" ) | |
cc_logic.write( 0x02, 0x00 ) | |
if ( cc_status & 0x03 ) is 0x00: | |
print( "configure the port as DRP (because cable detach detected)" ) | |
cc_logic.write( 0x02, 0x04 ) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment