mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-02 04:04:31 +02: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
58
components/efuse/efuse_table_gen.py
Normal file → Executable file
58
components/efuse/efuse_table_gen.py
Normal file → Executable file
@@ -21,11 +21,8 @@ from __future__ import print_function, division
|
|||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import struct
|
|
||||||
import sys
|
import sys
|
||||||
import hashlib
|
import hashlib
|
||||||
import binascii
|
|
||||||
import ntpath
|
|
||||||
|
|
||||||
__version__ = '1.0'
|
__version__ = '1.0'
|
||||||
|
|
||||||
@@ -33,9 +30,9 @@ quiet = False
|
|||||||
coding_scheme = 0
|
coding_scheme = 0
|
||||||
|
|
||||||
CODE_SCHEME = {
|
CODE_SCHEME = {
|
||||||
"NONE" : 0,
|
"NONE": 0,
|
||||||
"3/4" : 1,
|
"3/4": 1,
|
||||||
"REPEAT" : 2,
|
"REPEAT": 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
copyright = '''// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
|
copyright = '''// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
|
||||||
@@ -53,6 +50,7 @@ copyright = '''// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
|
|||||||
// limitations under the License
|
// limitations under the License
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
def status(msg):
|
def status(msg):
|
||||||
""" Print status message to stderr """
|
""" Print status message to stderr """
|
||||||
if not quiet:
|
if not quiet:
|
||||||
@@ -74,6 +72,7 @@ class FuseTable(list):
|
|||||||
def from_csv(cls, csv_contents):
|
def from_csv(cls, csv_contents):
|
||||||
res = FuseTable()
|
res = FuseTable()
|
||||||
lines = csv_contents.splitlines()
|
lines = csv_contents.splitlines()
|
||||||
|
|
||||||
def expand_vars(f):
|
def expand_vars(f):
|
||||||
f = os.path.expandvars(f)
|
f = os.path.expandvars(f)
|
||||||
m = re.match(r'(?<!\\)\$([A-Za-z_][A-Za-z0-9_]*)', f)
|
m = re.match(r'(?<!\\)\$([A-Za-z_][A-Za-z0-9_]*)', f)
|
||||||
@@ -94,7 +93,6 @@ class FuseTable(list):
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
# fix up missing bit_start
|
# fix up missing bit_start
|
||||||
last_enfl_error = 0
|
|
||||||
last_efuse_block = None
|
last_efuse_block = None
|
||||||
for e in res:
|
for e in res:
|
||||||
if last_efuse_block != e.efuse_block:
|
if last_efuse_block != e.efuse_block:
|
||||||
@@ -145,12 +143,12 @@ class FuseTable(list):
|
|||||||
field_name = p.field_name + p.group
|
field_name = p.field_name + p.group
|
||||||
if field_name != "" and len(duplicates.intersection([field_name])) != 0:
|
if field_name != "" and len(duplicates.intersection([field_name])) != 0:
|
||||||
fl_error = True
|
fl_error = True
|
||||||
print ("Field at %s, %s, %s, %s have dublicate field_name" %
|
print("Field at %s, %s, %s, %s have dublicate field_name" %
|
||||||
(p.field_name, p.efuse_block, p.bit_start, p.bit_count))
|
(p.field_name, p.efuse_block, p.bit_start, p.bit_count))
|
||||||
if fl_error == True:
|
if fl_error is True:
|
||||||
raise InputError("Field names must be unique")
|
raise InputError("Field names must be unique")
|
||||||
|
|
||||||
def verify(self, type_table = None):
|
def verify(self, type_table=None):
|
||||||
for p in self:
|
for p in self:
|
||||||
p.verify(type_table)
|
p.verify(type_table)
|
||||||
|
|
||||||
@@ -165,25 +163,11 @@ class FuseTable(list):
|
|||||||
last.field_name, last.efuse_block, last.bit_start, last.bit_count))
|
last.field_name, last.efuse_block, last.bit_start, last.bit_count))
|
||||||
last = p
|
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):
|
def calc_md5(self):
|
||||||
txt_table = ''
|
txt_table = ''
|
||||||
for p in self:
|
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"
|
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()
|
self.md5_digest_table = hashlib.md5(txt_table.encode('utf-8')).hexdigest()
|
||||||
|
|
||||||
def show_range_used_bits(self):
|
def show_range_used_bits(self):
|
||||||
# print used and free bits
|
# print used and free bits
|
||||||
@@ -200,7 +184,7 @@ class FuseTable(list):
|
|||||||
for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
|
for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
|
||||||
if last is None:
|
if last is None:
|
||||||
rows += '%s \n[%d ' % (p.efuse_block, p.bit_start)
|
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:
|
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)
|
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:
|
elif last.bit_start + last.bit_count != p.bit_start:
|
||||||
@@ -258,7 +242,6 @@ class FuseTable(list):
|
|||||||
last_name = p.field_name
|
last_name = p.field_name
|
||||||
rows += [p.to_struct(debug) + ","]
|
rows += [p.to_struct(debug) + ","]
|
||||||
rows += ["};\n"]
|
rows += ["};\n"]
|
||||||
|
|
||||||
rows += ["\n\n\n"]
|
rows += ["\n\n\n"]
|
||||||
|
|
||||||
last_name = ''
|
last_name = ''
|
||||||
@@ -272,7 +255,7 @@ class FuseTable(list):
|
|||||||
index = str(0) if str(p.group) == "" else str(p.group)
|
index = str(0) if str(p.group) == "" else str(p.group)
|
||||||
rows += [" &" + p.field_name + "[" + index + "], \t\t// " + p.comment]
|
rows += [" &" + p.field_name + "[" + index + "], \t\t// " + p.comment]
|
||||||
rows += [" NULL",
|
rows += [" NULL",
|
||||||
"};\n" ]
|
"};\n"]
|
||||||
|
|
||||||
return '\n'.join(rows) + "\n"
|
return '\n'.join(rows) + "\n"
|
||||||
|
|
||||||
@@ -362,7 +345,7 @@ class FuseDefinition(object):
|
|||||||
|
|
||||||
def to_struct(self, debug):
|
def to_struct(self, debug):
|
||||||
start = " {"
|
start = " {"
|
||||||
if (debug == True):
|
if debug is True:
|
||||||
start = " {" + '"' + self.field_name + '" ,'
|
start = " {" + '"' + self.field_name + '" ,'
|
||||||
return ", ".join([start + self.efuse_block,
|
return ", ".join([start + self.efuse_block,
|
||||||
str(self.bit_start),
|
str(self.bit_start),
|
||||||
@@ -394,14 +377,14 @@ def create_output_files(name, output_table, debug):
|
|||||||
dir_for_file_h = gen_dir + "/include"
|
dir_for_file_h = gen_dir + "/include"
|
||||||
try:
|
try:
|
||||||
os.stat(dir_for_file_h)
|
os.stat(dir_for_file_h)
|
||||||
except:
|
except Exception:
|
||||||
os.mkdir(dir_for_file_h)
|
os.mkdir(dir_for_file_h)
|
||||||
|
|
||||||
file_h_path = os.path.join(dir_for_file_h, file_name + ".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")
|
file_c_path = os.path.join(gen_dir, file_name + ".c")
|
||||||
|
|
||||||
# src files are the same
|
# 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 + " ...")
|
status("Creating efuse *.h file " + file_h_path + " ...")
|
||||||
output = output_table.to_header(file_name)
|
output = output_table.to_header(file_name)
|
||||||
with open(file_h_path, 'w') as f:
|
with open(file_h_path, 'w') as f:
|
||||||
@@ -422,13 +405,12 @@ def main():
|
|||||||
global coding_scheme
|
global coding_scheme
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='ESP32 eFuse Manager')
|
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('--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('--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('--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('--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('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('rb'), nargs='?', default=None)
|
parser.add_argument('custom_input', help='Path to custom CSV file to parse.', type=argparse.FileType('r'), nargs='?', default=None)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@@ -454,24 +436,26 @@ def main():
|
|||||||
two_table.verify()
|
two_table.verify()
|
||||||
|
|
||||||
# save files.
|
# save files.
|
||||||
if info == False:
|
if info is False:
|
||||||
if args.custom_input is None:
|
if args.custom_input is None:
|
||||||
create_output_files(args.common_input.name, common_table, debug)
|
create_output_files(args.common_input.name, common_table, debug)
|
||||||
else:
|
else:
|
||||||
create_output_files(args.custom_input.name, custom_table, debug)
|
create_output_files(args.custom_input.name, custom_table, debug)
|
||||||
else:
|
else:
|
||||||
print(two_table.show_range_used_bits())
|
print(two_table.show_range_used_bits())
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
class InputError(RuntimeError):
|
class InputError(RuntimeError):
|
||||||
def __init__(self, e):
|
def __init__(self, e):
|
||||||
super(InputError, self).__init__(e)
|
super(InputError, self).__init__(e)
|
||||||
|
|
||||||
|
|
||||||
class ValidationError(InputError):
|
class ValidationError(InputError):
|
||||||
def __init__(self, p, message):
|
def __init__(self, p, message):
|
||||||
super(ValidationError, self).__init__("Entry %s invalid: %s" % (p.field_name, message))
|
super(ValidationError, self).__init__("Entry %s invalid: %s" % (p.field_name, message))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
main()
|
main()
|
||||||
|
23
components/efuse/test_efuse_host/efuse_tests.py
Normal file → Executable file
23
components/efuse/test_efuse_host/efuse_tests.py
Normal file → Executable file
@@ -1,13 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
from __future__ import print_function, division
|
from __future__ import print_function, division
|
||||||
import unittest
|
import unittest
|
||||||
import struct
|
|
||||||
import csv
|
|
||||||
import sys
|
import sys
|
||||||
import subprocess
|
|
||||||
import tempfile
|
|
||||||
import os
|
|
||||||
import StringIO
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import efuse_table_gen
|
import efuse_table_gen
|
||||||
@@ -112,7 +106,7 @@ name2, EFUSE_BLK2, ,
|
|||||||
name1, EFUSE_BLK3, , 5,
|
name1, EFUSE_BLK3, , 5,
|
||||||
"""
|
"""
|
||||||
with self.assertRaisesRegexp(efuse_table_gen.InputError, "Field names must be unique"):
|
with self.assertRaisesRegexp(efuse_table_gen.InputError, "Field names must be unique"):
|
||||||
t = efuse_table_gen.FuseTable.from_csv(csv)
|
efuse_table_gen.FuseTable.from_csv(csv)
|
||||||
|
|
||||||
def test_seq_bit_start5_fill(self):
|
def test_seq_bit_start5_fill(self):
|
||||||
csv = """
|
csv = """
|
||||||
@@ -158,7 +152,7 @@ name2, EFUSE_BLK3, 5,
|
|||||||
name2, EFUSE_BLK2, , 4,
|
name2, EFUSE_BLK2, , 4,
|
||||||
"""
|
"""
|
||||||
with self.assertRaisesRegexp(efuse_table_gen.InputError, "missing field name"):
|
with self.assertRaisesRegexp(efuse_table_gen.InputError, "missing field name"):
|
||||||
t = efuse_table_gen.FuseTable.from_csv(csv)
|
efuse_table_gen.FuseTable.from_csv(csv)
|
||||||
|
|
||||||
def test_unique_field_name_fail(self):
|
def test_unique_field_name_fail(self):
|
||||||
csv = """
|
csv = """
|
||||||
@@ -167,7 +161,7 @@ name1, EFUSE_BLK3, 0,
|
|||||||
name1, EFUSE_BLK3, 5, 4, Use for test name 2
|
name1, EFUSE_BLK3, 5, 4, Use for test name 2
|
||||||
"""
|
"""
|
||||||
with self.assertRaisesRegexp(efuse_table_gen.InputError, "Field names must be unique"):
|
with self.assertRaisesRegexp(efuse_table_gen.InputError, "Field names must be unique"):
|
||||||
t = efuse_table_gen.FuseTable.from_csv(csv)
|
efuse_table_gen.FuseTable.from_csv(csv)
|
||||||
|
|
||||||
def test_bit_count_empty_fail(self):
|
def test_bit_count_empty_fail(self):
|
||||||
csv = """
|
csv = """
|
||||||
@@ -176,7 +170,7 @@ name1, EFUSE_BLK3, 0,
|
|||||||
name2, EFUSE_BLK3, 5, 4, Use for test name 2
|
name2, EFUSE_BLK3, 5, 4, Use for test name 2
|
||||||
"""
|
"""
|
||||||
with self.assertRaisesRegexp(efuse_table_gen.InputError, "empty"):
|
with self.assertRaisesRegexp(efuse_table_gen.InputError, "empty"):
|
||||||
t = efuse_table_gen.FuseTable.from_csv(csv)
|
efuse_table_gen.FuseTable.from_csv(csv)
|
||||||
|
|
||||||
def test_bit_start_num_fail(self):
|
def test_bit_start_num_fail(self):
|
||||||
csv = """
|
csv = """
|
||||||
@@ -185,7 +179,7 @@ name1, EFUSE_BLK3, k,
|
|||||||
name2, EFUSE_BLK3, 5, 4, Use for test name 2
|
name2, EFUSE_BLK3, 5, 4, Use for test name 2
|
||||||
"""
|
"""
|
||||||
with self.assertRaisesRegexp(efuse_table_gen.InputError, "Invalid field value"):
|
with self.assertRaisesRegexp(efuse_table_gen.InputError, "Invalid field value"):
|
||||||
t = efuse_table_gen.FuseTable.from_csv(csv)
|
efuse_table_gen.FuseTable.from_csv(csv)
|
||||||
|
|
||||||
def test_join_entry(self):
|
def test_join_entry(self):
|
||||||
csv = """
|
csv = """
|
||||||
@@ -231,7 +225,7 @@ name1, EFUSE_BLK5, 0,
|
|||||||
name2, EFUSE_BLK3, 5, 4, Use for test name 2
|
name2, EFUSE_BLK3, 5, 4, Use for test name 2
|
||||||
"""
|
"""
|
||||||
with self.assertRaisesRegexp(efuse_table_gen.InputError, "'efuse_block' should consist from EFUSE_BLK0..EFUSE_BLK3"):
|
with self.assertRaisesRegexp(efuse_table_gen.InputError, "'efuse_block' should consist from EFUSE_BLK0..EFUSE_BLK3"):
|
||||||
t = efuse_table_gen.FuseTable.from_csv(csv)
|
efuse_table_gen.FuseTable.from_csv(csv)
|
||||||
|
|
||||||
def test_field_size_is_ok(self):
|
def test_field_size_is_ok(self):
|
||||||
csv = """
|
csv = """
|
||||||
@@ -239,7 +233,7 @@ name2, EFUSE_BLK3, 5,
|
|||||||
name1, EFUSE_BLK0, 0, 224, Use for test name 1
|
name1, EFUSE_BLK0, 0, 224, Use for test name 1
|
||||||
name2, EFUSE_BLK1, 0, 256, Use for test name 2
|
name2, EFUSE_BLK1, 0, 256, Use for test name 2
|
||||||
"""
|
"""
|
||||||
efuse_table_gen.coding_scheme = 0 # NONE
|
efuse_table_gen.coding_scheme = 0 # NONE
|
||||||
t = efuse_table_gen.FuseTable.from_csv(csv)
|
t = efuse_table_gen.FuseTable.from_csv(csv)
|
||||||
t.verify()
|
t.verify()
|
||||||
|
|
||||||
@@ -249,7 +243,7 @@ name2, EFUSE_BLK1, 0,
|
|||||||
name1, EFUSE_BLK3, 190, 1, Use for test name 1
|
name1, EFUSE_BLK3, 190, 1, Use for test name 1
|
||||||
name2, EFUSE_BLK3, 191, 5, Use for test name 2
|
name2, EFUSE_BLK3, 191, 5, Use for test name 2
|
||||||
"""
|
"""
|
||||||
efuse_table_gen.coding_scheme = 1 #3/4 coding
|
efuse_table_gen.coding_scheme = 1 # 3/4 coding
|
||||||
t = efuse_table_gen.FuseTable.from_csv(csv)
|
t = efuse_table_gen.FuseTable.from_csv(csv)
|
||||||
with self.assertRaisesRegexp(efuse_table_gen.InputError, "The field is outside the boundaries"):
|
with self.assertRaisesRegexp(efuse_table_gen.InputError, "The field is outside the boundaries"):
|
||||||
t.verify()
|
t.verify()
|
||||||
@@ -330,5 +324,6 @@ name4, EFUSE_BLK3, 4,
|
|||||||
with self.assertRaisesRegexp(efuse_table_gen.InputError, "overlaps"):
|
with self.assertRaisesRegexp(efuse_table_gen.InputError, "overlaps"):
|
||||||
two_tables.verify()
|
two_tables.verify()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Reference in New Issue
Block a user