Go to the documentation of this file.00001
00002
00003
00004 import sys, getopt
00005 import csv
00006 import re
00007
00008 def getSize(arg):
00009 if("uint16_t" in arg or "int16_t" in arg):
00010 return 16
00011 elif("uint32_t" in arg or "int32_t" in arg):
00012 return 32
00013 elif("float" in arg):
00014 return 64
00015
00016 def parseRegisterDescription(desc):
00017 print('parsing ' + desc)
00018 regex = re.compile("([0-9a-zA-Z_]+)\(([0-9]+)\:([0-9]+)\)")
00019 registers = desc.split(' ')
00020 print('splint into ' + str(len(registers)) + ' pieces')
00021 output = []
00022 for reg in registers:
00023 print('finding regex match for ' + reg)
00024 try:
00025 match = regex.match(reg)
00026 mydict = {"id": match.group(1),
00027 "size": int(match.group(2)) - int(match.group(3)) +1,
00028 "index": int(match.group(3))}
00029 output.append(mydict)
00030 except AttributeError:
00031 print('no match found')
00032 pass
00033 return output
00034
00035 def main(argv):
00036 csvFile = 'ICD_PowCon_MLVDS_Oct_28_2013.csv'
00037 xmlFile = 'PowConRegisters.xml'
00038
00039 try:
00040 opts, args = getopt.getopt(argv, "hi:o:",["input=","output="])
00041 except getopt.GetoptError:
00042 print("PowCon_MLVDS_register_map_to_token.py -i <inputfile> -o <outputfile>")
00043 sys.exit(2)
00044 for opt, arg in opts:
00045 if opt == '-h':
00046 print 'PowCon_MLVDS_register_map_to_token.py -i <inputfile> -o <outputfile>'
00047 sys.exit()
00048 elif opt in ("-i", "--input"):
00049 csvFile = arg
00050 elif opt in ("-o", "--output"):
00051 xmlFile = arg
00052
00053 print("Opening " + csvFile)
00054 data = csv.reader(open(csvFile))
00055
00056 print("Writing output to " + xmlFile)
00057 xmlData = open(xmlFile, 'w')
00058
00059 xmlData.write('<?xml version="1.0" encoding="ISO-8859-1"?>' + "\n")
00060 xmlData.write('<RegisterData> ' + "\n")
00061 xmlData.write(' <Properties>' + "\n")
00062 xmlData.write(' <FPGAVersion>0x0001</FPGAVersion>' + "\n")
00063 xmlData.write(' <ControllerVersion>0x0001</ControllerVersion>' + "\n")
00064 xmlData.write(' <NodeType>PowCon</NodeType>' + "\n")
00065 xmlData.write(' </Properties>' + "\n")
00066 xmlData.write(' <Registers>' + "\n")
00067 rowNum = 0
00068 for row in data:
00069 if rowNum == 0:
00070 tags = row
00071
00072 for i in range(len(tags)):
00073 tags[i] = tags[i].strip().replace(' ', '_')
00074 if("name" in tags):
00075 iName = tags.index("name")
00076 print("found name tag at index: " + str(iName))
00077 else:
00078 print("can not find name tag")
00079 break
00080 if("data_type" in tags):
00081 iDataType = tags.index("data_type")
00082 print("found datatype tag at index: " + str(iDataType))
00083 else:
00084 print("can not find datatype tag")
00085 break
00086 if("ROBONET_type" in tags):
00087 iDirection = tags.index("ROBONET_type")
00088 print("found ROBONET_type tag at index: " + str(iDirection))
00089 else:
00090 print("can not find ROBONET_type tag")
00091 break
00092 if("registers" in tags):
00093 iRegisters = tags.index("registers")
00094 print("found registers tag at index: " + str(iRegisters))
00095 else:
00096 print("can not find registers tag")
00097 break
00098 if("description" in tags):
00099 iDescription = tags.index("description")
00100 print("found description tag at index: " + str(iDescription))
00101 else:
00102 print("can not find description tag")
00103 iDescription = -1
00104 else:
00105 if(row[iName].strip().replace(' ', '_')):
00106 registers = parseRegisterDescription(row[iRegisters])
00107 size = getSize(row[iDataType])
00108
00109 if(len(registers)>0):
00110 xmlData.write(' <Register size="' + str(getSize(row[iDataType])) + '" type="' + str(row[iDirection]) + '" id="' + str(row[iName].strip().replace(' ', '_')) + '">\n')
00111 xmlData.write(' <Certificate mask="0x0" value="0x0"/> \n')
00112 xmlData.write(' <Entries> \n')
00113 for reg in registers:
00114
00115 xmlData.write(' <Entry size="' + str(reg['size']) + '" id="' + str(reg['id']) + '" index="' + str(reg['index']) + '">')
00116 if(iDescription == -1):
00117 xmlData.write('<Description></Description></Entry>\n')
00118 else:
00119 xmlData.write('<Description>' + row[iDescription] + '</Description></Entry> \n')
00120 xmlData.write(' </Entries> \n')
00121 xmlData.write(' </Register> \n')
00122 rowNum +=1
00123 xmlData.write(' </Registers>' + "\n")
00124 xmlData.write('</RegisterData>')
00125
00126 xmlData.close()
00127
00128 if __name__ == "__main__":
00129 main(sys.argv[1:])