mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-08 12:10:59 +00:00
efuse: Fix python coding style
Set python's scripts attribute chmod Add compatibility with Python3 for efuse_table_gen.py
This commit is contained in:

committed by
bot

parent
a5fa3b6965
commit
cc094ba789
130
components/efuse/efuse_table_gen.py
Normal file → Executable file
130
components/efuse/efuse_table_gen.py
Normal file → Executable file
@@ -21,11 +21,8 @@ from __future__ import print_function, division
|
||||
import argparse
|
||||
import os
|
||||
import re
|
||||
import struct
|
||||
import sys
|
||||
import hashlib
|
||||
import binascii
|
||||
import ntpath
|
||||
|
||||
__version__ = '1.0'
|
||||
|
||||
@@ -33,9 +30,9 @@ quiet = False
|
||||
coding_scheme = 0
|
||||
|
||||
CODE_SCHEME = {
|
||||
"NONE" : 0,
|
||||
"3/4" : 1,
|
||||
"REPEAT" : 2,
|
||||
"NONE": 0,
|
||||
"3/4": 1,
|
||||
"REPEAT": 2,
|
||||
}
|
||||
|
||||
copyright = '''// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
|
||||
@@ -52,7 +49,8 @@ copyright = '''// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def status(msg):
|
||||
""" Print status message to stderr """
|
||||
if not quiet:
|
||||
@@ -74,6 +72,7 @@ class FuseTable(list):
|
||||
def from_csv(cls, csv_contents):
|
||||
res = FuseTable()
|
||||
lines = csv_contents.splitlines()
|
||||
|
||||
def expand_vars(f):
|
||||
f = os.path.expandvars(f)
|
||||
m = re.match(r'(?<!\\)\$([A-Za-z_][A-Za-z0-9_]*)', f)
|
||||
@@ -94,18 +93,17 @@ class FuseTable(list):
|
||||
raise
|
||||
|
||||
# fix up missing bit_start
|
||||
last_enfl_error = 0
|
||||
last_efuse_block = None
|
||||
for e in res:
|
||||
if last_efuse_block != e.efuse_block:
|
||||
if last_efuse_block != e.efuse_block:
|
||||
last_end = 0
|
||||
if e.bit_start is None:
|
||||
e.bit_start = last_end
|
||||
last_end = e.bit_start + e.bit_count
|
||||
last_efuse_block = e.efuse_block
|
||||
|
||||
|
||||
res.verify_duplicate_name()
|
||||
|
||||
|
||||
# fix up missing field_name
|
||||
last_field = None
|
||||
for e in res:
|
||||
@@ -114,7 +112,7 @@ class FuseTable(list):
|
||||
elif e.field_name == "" and last_field is not None:
|
||||
e.field_name = last_field.field_name
|
||||
last_field = e
|
||||
|
||||
|
||||
# fill group
|
||||
names = [p.field_name for p in res]
|
||||
duplicates = set(n for n in names if names.count(n) > 1)
|
||||
@@ -127,17 +125,17 @@ class FuseTable(list):
|
||||
else:
|
||||
i_count = 0
|
||||
res.verify_duplicate_name()
|
||||
|
||||
|
||||
# clac md5 for table
|
||||
res.calc_md5()
|
||||
|
||||
|
||||
return res
|
||||
|
||||
def verify_duplicate_name(self):
|
||||
# check on duplicate name
|
||||
names = [p.field_name for p in self]
|
||||
duplicates = set(n for n in names if names.count(n) > 1)
|
||||
|
||||
|
||||
# print sorted duplicate partitions by name
|
||||
if len(duplicates) != 0:
|
||||
fl_error = False
|
||||
@@ -145,17 +143,17 @@ class FuseTable(list):
|
||||
field_name = p.field_name + p.group
|
||||
if field_name != "" and len(duplicates.intersection([field_name])) != 0:
|
||||
fl_error = True
|
||||
print ("Field at %s, %s, %s, %s have dublicate field_name" %
|
||||
(p.field_name, p.efuse_block, p.bit_start, p.bit_count))
|
||||
if fl_error == True:
|
||||
print("Field at %s, %s, %s, %s have dublicate field_name" %
|
||||
(p.field_name, p.efuse_block, p.bit_start, p.bit_count))
|
||||
if fl_error is True:
|
||||
raise InputError("Field names must be unique")
|
||||
|
||||
def verify(self, type_table = None):
|
||||
|
||||
def verify(self, type_table=None):
|
||||
for p in self:
|
||||
p.verify(type_table)
|
||||
|
||||
self.verify_duplicate_name()
|
||||
|
||||
|
||||
# check for overlaps
|
||||
last = None
|
||||
for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
|
||||
@@ -164,28 +162,14 @@ class FuseTable(list):
|
||||
(p.field_name, p.efuse_block, p.bit_start, p.bit_count,
|
||||
last.field_name, last.efuse_block, last.bit_start, last.bit_count))
|
||||
last = p
|
||||
|
||||
def fix_size_fields_from_blk1_blk2(self):
|
||||
for p in self:
|
||||
if (p.efuse_block == "EFUSE_BLK1" and custom_table_use_BLK1 == False) or (p.efuse_block == "EFUSE_BLK2" and custom_table_use_BLK2 == False):
|
||||
max_bits = p.get_max_bits_of_block()
|
||||
if p.bit_start == 0 and p.bit_count > max_bits:
|
||||
print("Fix size `%s` field from %d to %d" % (p.field_name, p.bit_count, max_bits))
|
||||
p.bit_count = max_bits
|
||||
|
||||
def keys_from_blk1_blk2_make_empty(self):
|
||||
for p in self:
|
||||
if (p.efuse_block == "EFUSE_BLK1" and custom_table_use_BLK1 == True) or (p.efuse_block == "EFUSE_BLK2" and custom_table_use_BLK2 == True):
|
||||
p.bit_count = 0
|
||||
print("efuse: `%s` field was changed from %d to 0" % (p.field_name, p.bit_count))
|
||||
|
||||
|
||||
def calc_md5(self):
|
||||
txt_table = ''
|
||||
for p in self:
|
||||
txt_table += "%s %s %d %d %s" % (p.field_name, p.efuse_block, p.bit_start, p.bit_count, p.comment) + "\n"
|
||||
self.md5_digest_table = hashlib.md5(txt_table).hexdigest()
|
||||
|
||||
def show_range_used_bits(self):
|
||||
self.md5_digest_table = hashlib.md5(txt_table.encode('utf-8')).hexdigest()
|
||||
|
||||
def show_range_used_bits(self):
|
||||
# print used and free bits
|
||||
rows = ''
|
||||
rows += 'Sorted efuse table:\n'
|
||||
@@ -194,13 +178,13 @@ class FuseTable(list):
|
||||
for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
|
||||
rows += "{0} \t{1:<30} \t{2} \t{3:^8} \t{4:^8}".format(num, p.field_name, p.efuse_block, p.bit_start, p.bit_count) + "\n"
|
||||
num += 1
|
||||
|
||||
|
||||
rows += '\nUsed bits in efuse table:\n'
|
||||
last = None
|
||||
for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
|
||||
if last is None:
|
||||
rows += '%s \n[%d ' % (p.efuse_block, p.bit_start)
|
||||
if last is not None:
|
||||
if last is not None:
|
||||
if last.efuse_block != p.efuse_block:
|
||||
rows += '%d] \n\n%s \n[%d ' % (last.bit_start + last.bit_count - 1, p.efuse_block, p.bit_start)
|
||||
elif last.bit_start + last.bit_count != p.bit_start:
|
||||
@@ -209,7 +193,7 @@ class FuseTable(list):
|
||||
rows += '%d] \n' % (last.bit_start + last.bit_count - 1)
|
||||
rows += '\nNote: Not printed ranges are free for using. (bits in EFUSE_BLK0 are reserved for Espressif)\n'
|
||||
return rows
|
||||
|
||||
|
||||
def to_header(self, file_name):
|
||||
rows = [copyright]
|
||||
rows += ["#ifdef __cplusplus",
|
||||
@@ -223,13 +207,13 @@ class FuseTable(list):
|
||||
"// To show efuse_table run the command 'make show_efuse_table'.",
|
||||
"",
|
||||
""]
|
||||
|
||||
|
||||
last_field_name = ''
|
||||
for p in self:
|
||||
if (p.field_name != last_field_name):
|
||||
rows += ["extern const esp_efuse_desc_t* " + "ESP_EFUSE_" + p.field_name + "[];"]
|
||||
last_field_name = p.field_name
|
||||
|
||||
|
||||
rows += ["",
|
||||
"#ifdef __cplusplus",
|
||||
"}",
|
||||
@@ -248,7 +232,7 @@ class FuseTable(list):
|
||||
"// To show efuse_table run the command 'make show_efuse_table'.",
|
||||
"",
|
||||
""]
|
||||
|
||||
|
||||
last_name = ''
|
||||
for p in self:
|
||||
if (p.field_name != last_name):
|
||||
@@ -258,9 +242,8 @@ class FuseTable(list):
|
||||
last_name = p.field_name
|
||||
rows += [p.to_struct(debug) + ","]
|
||||
rows += ["};\n"]
|
||||
|
||||
rows += ["\n\n\n"]
|
||||
|
||||
|
||||
last_name = ''
|
||||
for p in self:
|
||||
if (p.field_name != last_name):
|
||||
@@ -272,8 +255,8 @@ class FuseTable(list):
|
||||
index = str(0) if str(p.group) == "" else str(p.group)
|
||||
rows += [" &" + p.field_name + "[" + index + "], \t\t// " + p.comment]
|
||||
rows += [" NULL",
|
||||
"};\n" ]
|
||||
|
||||
"};\n"]
|
||||
|
||||
return '\n'.join(rows) + "\n"
|
||||
|
||||
|
||||
@@ -306,13 +289,13 @@ class FuseDefinition(object):
|
||||
if strval == "":
|
||||
return None # Field will fill in default
|
||||
return self.parse_int(strval)
|
||||
|
||||
|
||||
def parse_int(self, v):
|
||||
try:
|
||||
return int(v, 0)
|
||||
except ValueError:
|
||||
raise InputError("Invalid field value %s" % v)
|
||||
|
||||
|
||||
def parse_block(self, strval):
|
||||
if strval == "":
|
||||
raise InputError("Field 'efuse_block' can't be left empty.")
|
||||
@@ -334,35 +317,35 @@ class FuseDefinition(object):
|
||||
else:
|
||||
raise ValidationError(self, "Unknown coding scheme")
|
||||
return max_bits
|
||||
|
||||
|
||||
def verify(self, type_table):
|
||||
if self.efuse_block is None:
|
||||
raise ValidationError(self, "efuse_block field is not set")
|
||||
if self.bit_count is None:
|
||||
raise ValidationError(self, "bit_count field is not set")
|
||||
|
||||
|
||||
if type_table is not None:
|
||||
if type_table == "custom_table":
|
||||
if self.efuse_block != "EFUSE_BLK3":
|
||||
raise ValidationError(self, "custom_table should use only EFUSE_BLK3")
|
||||
|
||||
|
||||
max_bits = self.get_max_bits_of_block()
|
||||
|
||||
if self.bit_start + self.bit_count > max_bits:
|
||||
raise ValidationError(self, "The field is outside the boundaries(max_bits = %d) of the %s block" % (max_bits, self.efuse_block))
|
||||
|
||||
|
||||
def get_full_name(self):
|
||||
def get_postfix(group):
|
||||
postfix = ""
|
||||
if group != "":
|
||||
postfix = "_PART_" + group
|
||||
return postfix
|
||||
|
||||
|
||||
return self.field_name + get_postfix(self.group)
|
||||
|
||||
|
||||
def to_struct(self, debug):
|
||||
start = " {"
|
||||
if (debug == True):
|
||||
if debug is True:
|
||||
start = " {" + '"' + self.field_name + '" ,'
|
||||
return ", ".join([start + self.efuse_block,
|
||||
str(self.bit_start),
|
||||
@@ -390,24 +373,24 @@ def ckeck_md5_in_file(md5, filename):
|
||||
def create_output_files(name, output_table, debug):
|
||||
file_name = os.path.splitext(os.path.basename(name))[0]
|
||||
gen_dir = os.path.dirname(name)
|
||||
|
||||
|
||||
dir_for_file_h = gen_dir + "/include"
|
||||
try:
|
||||
os.stat(dir_for_file_h)
|
||||
except:
|
||||
except Exception:
|
||||
os.mkdir(dir_for_file_h)
|
||||
|
||||
|
||||
file_h_path = os.path.join(dir_for_file_h, file_name + ".h")
|
||||
file_c_path = os.path.join(gen_dir, file_name + ".c")
|
||||
|
||||
|
||||
# src files are the same
|
||||
if ckeck_md5_in_file(output_table.md5_digest_table, file_c_path) == False:
|
||||
if ckeck_md5_in_file(output_table.md5_digest_table, file_c_path) is False:
|
||||
status("Creating efuse *.h file " + file_h_path + " ...")
|
||||
output = output_table.to_header(file_name)
|
||||
with open(file_h_path, 'w') as f:
|
||||
f.write(output)
|
||||
f.close()
|
||||
|
||||
|
||||
status("Creating efuse *.c file " + file_c_path + " ...")
|
||||
output = output_table.to_c_file(file_name, debug)
|
||||
with open(file_c_path, 'w') as f:
|
||||
@@ -420,15 +403,14 @@ def create_output_files(name, output_table, debug):
|
||||
def main():
|
||||
global quiet
|
||||
global coding_scheme
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser(description='ESP32 eFuse Manager')
|
||||
|
||||
parser.add_argument('--quiet', '-q', help="Don't print non-critical status messages to stderr", action='store_true')
|
||||
parser.add_argument('--debug', help='Create header file with debug info', default=False, action="store_false")
|
||||
parser.add_argument('--info', help='Print info about range of used bits', default=False, action="store_true")
|
||||
parser.add_argument('--coding_scheme', help='Coding scheme', type=int, default=0)
|
||||
parser.add_argument('common_input', help='Path to common CSV file to parse.', type=argparse.FileType('rb'))
|
||||
parser.add_argument('custom_input', help='Path to custom CSV file to parse.', type=argparse.FileType('rb'), nargs='?', default=None)
|
||||
parser.add_argument('common_input', help='Path to common CSV file to parse.', type=argparse.FileType('r'))
|
||||
parser.add_argument('custom_input', help='Path to custom CSV file to parse.', type=argparse.FileType('r'), nargs='?', default=None)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
@@ -441,37 +423,39 @@ def main():
|
||||
print("eFuse coding scheme: REPEAT")
|
||||
else:
|
||||
raise InputError("unknown CODE_SCHEME = %s" % (coding_scheme))
|
||||
|
||||
|
||||
quiet = args.quiet
|
||||
debug = args.debug
|
||||
info = args.info
|
||||
|
||||
|
||||
common_table = process_input_file(args.common_input, "common_table")
|
||||
two_table = common_table
|
||||
if args.custom_input is not None:
|
||||
custom_table = process_input_file(args.custom_input, "custom_table")
|
||||
two_table += custom_table
|
||||
two_table.verify()
|
||||
|
||||
|
||||
# save files.
|
||||
if info == False:
|
||||
if info is False:
|
||||
if args.custom_input is None:
|
||||
create_output_files(args.common_input.name, common_table, debug)
|
||||
else:
|
||||
create_output_files(args.custom_input.name, custom_table, debug)
|
||||
else:
|
||||
print(two_table.show_range_used_bits())
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
class InputError(RuntimeError):
|
||||
def __init__(self, e):
|
||||
super(InputError, self).__init__(e)
|
||||
|
||||
|
||||
class ValidationError(InputError):
|
||||
def __init__(self, p, message):
|
||||
super(ValidationError, self).__init__("Entry %s invalid: %s" % (p.field_name, message))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
|
Reference in New Issue
Block a user