chiplist_convert.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. import argparse
  3. import xml.etree.ElementTree as XML
  4. import sys
  5. def getArgs():
  6. parser = argparse.ArgumentParser(
  7. description="chiplist.xml to C array converter",
  8. )
  9. parser.add_argument("file", help="chiplist.xml file")
  10. return parser.parse_args()
  11. def getXML(file):
  12. tree = XML.parse(file)
  13. root = tree.getroot()
  14. return root
  15. def parseChip(cur, arr, vendor, vendorCodeArr):
  16. chip = {}
  17. chipAttr = cur.attrib
  18. if "page" not in chipAttr: # chip without page size not supported
  19. return
  20. if "id" not in chipAttr: # I2C not supported yet
  21. return
  22. if len(chipAttr["id"]) < 6: # ID wihout capacity id not supported yet
  23. return
  24. chip["modelName"] = cur.tag
  25. chip["vendorEnum"] = "SPIMemChipVendor" + vendor
  26. chip["vendorID"] = "0x" + chipAttr["id"][0] + chipAttr["id"][1]
  27. chip["typeID"] = chipAttr["id"][2] + chipAttr["id"][3]
  28. chip["capacityID"] = chipAttr["id"][4] + chipAttr["id"][5]
  29. chip["size"] = chipAttr["size"]
  30. if chipAttr["page"] == "SSTW":
  31. chip["writeMode"] = "SPIMemChipWriteModeAAIWord"
  32. chip["pageSize"] = "1"
  33. elif chipAttr["page"] == "SSTB":
  34. chip["writeMode"] = "SPIMemChipWriteModeAAIByte"
  35. chip["pageSize"] = "1"
  36. else:
  37. chip["writeMode"] = "SPIMemChipWriteModePage"
  38. chip["pageSize"] = chipAttr["page"]
  39. arr.append(chip)
  40. vendorCodeArr[vendor].add(chip["vendorID"])
  41. def cleanEmptyVendors(vendors):
  42. for cur in list(vendors):
  43. if not vendors[cur]:
  44. vendors.pop(cur)
  45. def getVendors(xml, interface):
  46. arr = {}
  47. for cur in xml.find(interface):
  48. arr[cur.tag] = set()
  49. return arr
  50. def parseXML(xml, interface, vendorCodeArr):
  51. arr = []
  52. for vendor in xml.find(interface):
  53. for cur in vendor:
  54. parseChip(cur, arr, vendor.tag, vendorCodeArr)
  55. return arr
  56. def getVendorNameEnum(vendorID):
  57. try:
  58. return vendors[vendorID]
  59. except:
  60. print("Unknown vendor: " + vendorID)
  61. sys.exit(1)
  62. def generateCArr(arr, filename):
  63. with open(filename, "w") as out:
  64. print('#include "spi_mem_chip_i.h"', file=out)
  65. print("const SPIMemChip SPIMemChips[] = {", file=out)
  66. for cur in arr:
  67. print(" {" + cur["vendorID"] + ",", file=out, end="")
  68. print(" 0x" + cur["typeID"] + ",", file=out, end="")
  69. print(" 0x" + cur["capacityID"] + ",", file=out, end="")
  70. print(' "' + cur["modelName"] + '",', file=out, end="")
  71. print(" " + cur["size"] + ",", file=out, end="")
  72. print(" " + cur["pageSize"] + ",", file=out, end="")
  73. print(" " + cur["vendorEnum"] + ",", file=out, end="")
  74. if cur == arr[-1]:
  75. print(" " + cur["writeMode"] + "}};", file=out)
  76. else:
  77. print(" " + cur["writeMode"] + "},", file=out)
  78. def main():
  79. filename = "spi_mem_chip_arr.c"
  80. args = getArgs()
  81. xml = getXML(args.file)
  82. vendors = getVendors(xml, "SPI")
  83. chipArr = parseXML(xml, "SPI", vendors)
  84. cleanEmptyVendors(vendors)
  85. for cur in vendors:
  86. print(' {"' + cur + '", SPIMemChipVendor' + cur + "},")
  87. generateCArr(chipArr, filename)
  88. if __name__ == "__main__":
  89. main()