mem_test.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. #
  6. import argparse
  7. import json
  8. import os
  9. import re
  10. from typing import Dict
  11. IDF_PATH = os.environ['IDF_PATH']
  12. MAX_SIZE_DIFF = 50
  13. def mem_test(size_json: dict, esptool_output: list) -> None:
  14. seg_len = {} # type: Dict[str, int]
  15. for i in esptool_output:
  16. tmp = i.split(' ')
  17. if tmp[0] == 'Segment':
  18. # tmp look like ['Segment', '2:', 'len', '0x02780', 'load', '0x3fc90610', 'file_offs', '0x00007ab0', '[BYTE_ACCESSIBLE,MEM_INTERNAL,DRAM]']
  19. # tmp[3] contains the size of the segment and tmp[8] contains the name of the memory segment
  20. esptool_mem = {'mem_type':tmp[8], 'size':tmp[3]}
  21. seg = re.sub(r'MEM_INTERNAL|,|BYTE_ACCESSIBLE|\n|\[|\]', '', esptool_mem['mem_type'])
  22. # If there are two IRAMs in esptool output it will compute these two IRAM lengths in a seg_len['IRAM']
  23. seg_len[seg] = int(esptool_mem['size'], 16) if seg not in seg_len else seg_len[seg] + int(esptool_mem['size'], 16)
  24. # including flash_other to DROM because flash_other contain .flash.appdesc that includes in DROM that produced by esptool
  25. size_from_map = [('IROM', size_json['flash_code']), ('IRAM', size_json['iram_text'] + size_json['iram_vectors'] + size_json['diram_text']
  26. + size_json['diram_vectors']), ('DROM', size_json['flash_rodata'] + size_json['flash_other']), ('DRAM', size_json
  27. ['dram_data'] + size_json['diram_data'])]
  28. for mem_type, size in size_from_map:
  29. if abs(size - seg_len[mem_type]) > MAX_SIZE_DIFF:
  30. raise RuntimeError(mem_type + " segment in idf_size isn't correct regarding esptool")
  31. print('Test complete without errors')
  32. def main() -> None:
  33. parser = argparse.ArgumentParser(description='mem_test.py - a tool to test accuracy of the sizes of the memory segments regarding idf.py size by esptool')
  34. parser.add_argument(
  35. 'size_json', help='JSON file with the output of the idf.py size',
  36. type=argparse.FileType('r'))
  37. parser.add_argument(
  38. 'esptool_output', help='File with the output of the esptool',
  39. type=argparse.FileType('r'))
  40. args = parser.parse_args()
  41. mem_test(json.loads(args.size_json.read()), args.esptool_output.read().split('\n'))
  42. if __name__ == '__main__':
  43. main()