dtc.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #
  2. # Copyright (c) 2006-2023, RT-Thread Development Team
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. #
  6. # Change Logs:
  7. # Date Author Notes
  8. # 2023-05-10 GuEe-GUI the first version
  9. #
  10. import os, re
  11. from building import *
  12. __dtc_install_tip = """
  13. You should install dtc (devicetree compiler) in your system:
  14. Linux:
  15. Debian/Ubuntu: apt-get install device-tree-compiler
  16. Arch/Manjaro: pacman -Sy dtc
  17. MacOS:
  18. brew install dtc
  19. Windows (MinGW):
  20. msys2: pacman -S dtc
  21. """
  22. def __check_dtc(value):
  23. if value != 0 and os.system("dtc -v") != 0:
  24. print(__dtc_install_tip)
  25. def dts_to_dtb(RTT_ROOT, dts_list, options = "", include_paths = [], ignore_warning = []):
  26. path = GetCurrentDir() + '/'
  27. warning_ops = ""
  28. for warning in ignore_warning:
  29. warning_ops += " -W no-" + warning
  30. for dts in dts_list:
  31. dtb = dts.replace('.dts', '.dtb')
  32. if not os.path.exists(path + dtb) or os.path.getmtime(path + dtb) < os.path.getmtime(path + dts):
  33. tmp_dts = dts + '.tmp'
  34. Preprocessing(dts, None, output = tmp_dts, CPPPATH=[RTT_ROOT + '/components/drivers/include'] + include_paths)
  35. ret = os.system("dtc -I dts -O dtb -@ -A {} {} {} -o {}".format(warning_ops, options, path + tmp_dts, path + dtb))
  36. __check_dtc(ret)
  37. if os.path.exists(path + tmp_dts):
  38. os.remove(path + tmp_dts)
  39. def dtb_to_dts(RTT_ROOT, dtb_name, dts_name = None, options = ""):
  40. path = GetCurrentDir() + '/'
  41. if dts_name == None:
  42. dts_name = re.sub(r'\.dtb[o]*$', '.dts', dtb_name)
  43. ret = os.system("dtc -I dtb -O dts {} {} -o {}".format(options, path + dtb_name, path + dts_name))
  44. __check_dtc(ret)