silver2row

from bone_pinmux import set_pinmux_state

Jun 29th, 2020
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # from #beagle on Freenode
  2.  
  3. # use
  4.  
  5. # from bone_pinmux import set_pinmux_state
  6. # https://pastebin.com/MKtWJ8G8
  7. # set_pinmux_state( 'P8_37', 'uart' )
  8. # set_pinmux_state( 'P8_38', 'uart' )
  9.  
  10. from glob import glob
  11. from pathlib import Path
  12. import errno
  13.  
  14. def pinmux_path( name ):
  15.     paths = glob( '/sys/bus/platform/drivers/bone-pinmux-helper/*%s*/state' % name )
  16.     if len(paths) == 0:
  17.         raise KeyError( 'Pinmux node not found: ' + name )
  18.     if len(paths) > 1:
  19.         raise KeyError( 'Pinmux node ambiguous: ' + name )
  20.     return Path( paths[0] )
  21.  
  22. def get_pinmux_state( name ):
  23.     return pinmux_path( name ).read_text().rstrip()
  24.  
  25. def set_pinmux_state( name, state ):
  26.     try:
  27.         pinmux_path( name ).write_text( state )
  28.     except OSError as err:
  29.         if err.errno == errno.ENODEV:
  30.             raise ValueError( 'Invalid state for ' + name + ': ' + state ) from None
  31.         else:
  32.             raise
Add Comment
Please, Sign In to add comment