PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/generate_overlays.py

http://github.com/alexanderhiam/PyBBIO
Python | 87 lines | 55 code | 15 blank | 17 comment | 10 complexity | 16fb7ddff850df1aabf1b49d31f3fc1f MD5 | raw file
  1. """
  2. generate_overlays.py
  3. Part of PyBBIO
  4. github.com/alexanderhiam/PyBBIO
  5. MIT license
  6. Generates and installs device tree overlays used for pinmuxing on
  7. BeagleBones running a 3.8 or newer kernel.
  8. """
  9. import sys, os, glob, shutil
  10. cwd = os.path.dirname(os.path.realpath(__file__))
  11. config_path = os.path.realpath('%s/../bbio/platform/beaglebone' % cwd)
  12. dts_path = '%s/overlays' % cwd
  13. dtbo_path = '%s/overlays/compiled' % cwd
  14. dtc_compile = ' dtc -O dtb -o %s.dtbo -b 0 -@ %s.dts'
  15. sys.path = [config_path] + sys.path
  16. from config import GPIO, ADC
  17. with open('%s/overlays/gpio-template.txt' % cwd, 'rb') as f:
  18. gpio_template = f.read()
  19. with open('%s/overlays/adc-template.txt' % cwd, 'rb') as f:
  20. adc_template = f.read()
  21. header = \
  22. """
  23. /* This file was generated as part of PyBBIO
  24. * github.com/alexanderhiam/PyBBIO
  25. *
  26. * This file is in the Public Domain.
  27. */
  28. """
  29. def compileOverlays():
  30. print "Compiling all overlays..."
  31. overlays = glob.glob('%s/*.dts' % dts_path)
  32. for overlay in overlays:
  33. name = os.path.splitext(os.path.basename(overlay))[0]
  34. os.system(dtc_compile % ('%s/%s' % (dtbo_path, name),
  35. '%s/%s' % (dts_path, name)))
  36. def generateOverlays():
  37. print "Generating GPIO overlays..."
  38. version = '00A0'
  39. for pin, config in GPIO.items():
  40. gpio_pin = pin.lower()
  41. register_name = config['signal']
  42. offset = str(config['offset'])
  43. overlay_name = 'PyBBIO-%s' % gpio_pin
  44. dts = gpio_template.replace('{gpio_pin}', gpio_pin)\
  45. .replace('{name}', register_name)\
  46. .replace('{overlay_name}', overlay_name)\
  47. .replace('{version}', version)\
  48. .replace('{offset}', offset)
  49. with open('%s/%s-%s.dts' % (dts_path, overlay_name, version), 'wb') as f:
  50. f.write(dts)
  51. print "Generating ADC overlays..."
  52. version = '00A0'
  53. adc_scale = '100'
  54. for adc_ch, config in ADC.items():
  55. overlay_name = 'PyBBIO-%s' % adc_ch
  56. header_pin = config[2]
  57. dts = adc_template.replace('{adc_ch}', adc_ch)\
  58. .replace('{header_pin}', header_pin)\
  59. .replace('{overlay_name}', overlay_name)\
  60. .replace('{adc_scale}', adc_scale)\
  61. .replace('{version}', version)
  62. with open('%s/%s-%s.dts' % (dts_path, overlay_name, version), 'wb') as f:
  63. f.write(dts)
  64. if __name__ == '__main__':
  65. if not os.path.exists(dts_path):
  66. os.makedirs(dts_path)
  67. if not os.path.exists(dtbo_path):
  68. os.makedirs(dtbo_path)
  69. generateOverlays()
  70. compileOverlays()