mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04: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:
parent
a5fa3b6965
commit
cc094ba789
54
components/efuse/efuse_table_gen.py
Normal file → Executable file
54
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
|
||||
@ -53,6 +50,7 @@ copyright = '''// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
|
||||
// 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,7 +93,6 @@ 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:
|
||||
@ -145,12 +143,12 @@ 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" %
|
||||
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:
|
||||
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)
|
||||
|
||||
@ -165,25 +163,11 @@ class FuseTable(list):
|
||||
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()
|
||||
self.md5_digest_table = hashlib.md5(txt_table.encode('utf-8')).hexdigest()
|
||||
|
||||
def show_range_used_bits(self):
|
||||
# print used and free bits
|
||||
@ -258,7 +242,6 @@ class FuseTable(list):
|
||||
last_name = p.field_name
|
||||
rows += [p.to_struct(debug) + ","]
|
||||
rows += ["};\n"]
|
||||
|
||||
rows += ["\n\n\n"]
|
||||
|
||||
last_name = ''
|
||||
@ -272,7 +255,7 @@ 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"
|
||||
|
||||
@ -362,7 +345,7 @@ class FuseDefinition(object):
|
||||
|
||||
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),
|
||||
@ -394,14 +377,14 @@ def create_output_files(name, output_table, debug):
|
||||
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:
|
||||
@ -422,13 +405,12 @@ def main():
|
||||
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()
|
||||
|
||||
@ -454,24 +436,26 @@ def main():
|
||||
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()
|
||||
|
21
components/efuse/test_efuse_host/efuse_tests.py
Normal file → Executable file
21
components/efuse/test_efuse_host/efuse_tests.py
Normal file → Executable file
@ -1,13 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function, division
|
||||
import unittest
|
||||
import struct
|
||||
import csv
|
||||
import sys
|
||||
import subprocess
|
||||
import tempfile
|
||||
import os
|
||||
import StringIO
|
||||
|
||||
try:
|
||||
import efuse_table_gen
|
||||
@ -112,7 +106,7 @@ name2, EFUSE_BLK2, ,
|
||||
name1, EFUSE_BLK3, , 5,
|
||||
"""
|
||||
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):
|
||||
csv = """
|
||||
@ -158,7 +152,7 @@ name2, EFUSE_BLK3, 5,
|
||||
name2, EFUSE_BLK2, , 4,
|
||||
"""
|
||||
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):
|
||||
csv = """
|
||||
@ -167,7 +161,7 @@ name1, EFUSE_BLK3, 0,
|
||||
name1, EFUSE_BLK3, 5, 4, Use for test name 2
|
||||
"""
|
||||
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):
|
||||
csv = """
|
||||
@ -176,7 +170,7 @@ name1, EFUSE_BLK3, 0,
|
||||
name2, EFUSE_BLK3, 5, 4, Use for test name 2
|
||||
"""
|
||||
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):
|
||||
csv = """
|
||||
@ -185,7 +179,7 @@ name1, EFUSE_BLK3, k,
|
||||
name2, EFUSE_BLK3, 5, 4, Use for test name 2
|
||||
"""
|
||||
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):
|
||||
csv = """
|
||||
@ -231,7 +225,7 @@ name1, EFUSE_BLK5, 0,
|
||||
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"):
|
||||
t = efuse_table_gen.FuseTable.from_csv(csv)
|
||||
efuse_table_gen.FuseTable.from_csv(csv)
|
||||
|
||||
def test_field_size_is_ok(self):
|
||||
csv = """
|
||||
@ -249,7 +243,7 @@ name2, EFUSE_BLK1, 0,
|
||||
name1, EFUSE_BLK3, 190, 1, Use for test name 1
|
||||
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)
|
||||
with self.assertRaisesRegexp(efuse_table_gen.InputError, "The field is outside the boundaries"):
|
||||
t.verify()
|
||||
@ -330,5 +324,6 @@ name4, EFUSE_BLK3, 4,
|
||||
with self.assertRaisesRegexp(efuse_table_gen.InputError, "overlaps"):
|
||||
two_tables.verify()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
Loading…
Reference in New Issue
Block a user