Skip to content

Commit

Permalink
fix: rewrite flash
Browse files Browse the repository at this point in the history
  • Loading branch information
horw committed Dec 24, 2023
1 parent 585b884 commit e573fe2
Showing 1 changed file with 20 additions and 57 deletions.
77 changes: 20 additions & 57 deletions pytest-embedded-idf/pytest_embedded_idf/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import logging
import os
import tempfile
from typing import Optional, TextIO, Union

import esptool
Expand Down Expand Up @@ -121,71 +120,35 @@ def flash(self) -> None:
logging.error('No flash settings detected. Skipping auto flash...')
return

addition_kwargs = self.app.flash_args.get('extra_esptool_args', {}).copy()
addition_kwargs['no-stub'] = not addition_kwargs.pop('stub')

write_flash_args = {
'erase-all': False,
'no-progress': False,
'verify': False,
'encrypt': False,
'encrypt-files': False,
'ignore-flash-encryption-efuse-setting': False,
'force': False,
'compress': True,
'no-compress': False,
}

for k, v in list(addition_kwargs.items()):
rk = k.replace('_', '-')
if rk in write_flash_args:
write_flash_args[rk] = addition_kwargs.pop(k)

_args = []

def _kwargs_extend_list(_items):
for _k, _v in _items:
if not _v:
continue
_args.append(f'--{_k}')

if isinstance(_v, bool):
continue
if isinstance(_v, list):
_args.extend(_v)
continue
_args.append(_v.strip())

current_wk = os.getcwd()
os.chdir(self.app.binary_path)

_kwargs_extend_list(addition_kwargs.items())
if os.path.isfile('flash_args'):
_args.extend(['write_flash', '@flash_args'])
else:
if os.path.isfile('flasher_args.json'):
flash_data = json.load(open('flasher_args.json'))
_args.append('write_flash')
for item in flash_data['flash_files'].items():
_args.extend(item)
_args.extend(flash_data['write_flash_args'])

if self.erase_nvs:
address = self.app.partition_table['nvs']['offset']
size = self.app.partition_table['nvs']['size']
nvs_file = tempfile.NamedTemporaryFile(delete=False)
nvs_file.write(b'\xff' * size)
if not isinstance(address, int):
address = int(address, 0)
ind = _args.index('write_flash')
_args.insert(ind + 1, address)
_args.insert(ind + 2, nvs_file.name)
esptool.main(['erase_region', str(address), str(size)], esp=self.esp)

_kwargs_extend_list(write_flash_args.items())
data = json.load(open('flasher_args.json'))
_args = []
for k, v in data['extra_esptool_args'].items():
if type(v, bool):
if k == 'stub':
if v is False:
_args.append('--no-stub')
else:
if v:
_args.append(f'--{k}')
else:
_args.append(f'--{k}')
_args.append(str(v))

_args = map(str, _args)
_args.append('write_flash')
for k, v in data['flash_files'].items():
_args.append(str(k))
_args.append(str(v))
_args.extend(data['write_flash_args'])

esptool.main(['-b', str(self.esptool_baud), *_args], esp=self.esp)
esptool.main(_args, esp=self.esp)

os.chdir(current_wk)

Expand Down

0 comments on commit e573fe2

Please sign in to comment.