2019-01-23 08:31:06 -07:00
|
|
|
# Copyright (c) 2017 Linaro Limited.
|
2021-03-05 15:58:06 +05:30
|
|
|
# Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
|
2019-01-23 08:31:06 -07:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
'''Runner for flashing ESP32 devices with esptool/espidf.'''
|
|
|
|
|
|
|
|
from os import path
|
|
|
|
|
|
|
|
from runners.core import ZephyrBinaryRunner, RunnerCaps
|
|
|
|
|
2019-09-24 10:06:55 -07:00
|
|
|
import sys
|
|
|
|
|
2019-01-23 08:31:06 -07:00
|
|
|
|
|
|
|
class Esp32BinaryRunner(ZephyrBinaryRunner):
|
|
|
|
'''Runner front-end for espidf.'''
|
|
|
|
|
2021-04-12 19:39:04 -03:00
|
|
|
def __init__(self, cfg, device, boot_address, part_table_address,
|
|
|
|
app_address, baud=921600, flash_size='detect',
|
2019-01-23 08:31:06 -07:00
|
|
|
flash_freq='40m', flash_mode='dio', espidf='espidf',
|
|
|
|
bootloader_bin=None, partition_table_bin=None):
|
2020-06-23 13:27:11 -07:00
|
|
|
super().__init__(cfg)
|
2019-01-23 08:31:06 -07:00
|
|
|
self.elf = cfg.elf_file
|
|
|
|
self.device = device
|
2021-04-12 19:39:04 -03:00
|
|
|
self.boot_address = boot_address
|
|
|
|
self.part_table_address = part_table_address
|
2021-06-14 11:19:33 -03:00
|
|
|
self.app_address = app_address
|
2019-01-23 08:31:06 -07:00
|
|
|
self.baud = baud
|
|
|
|
self.flash_size = flash_size
|
|
|
|
self.flash_freq = flash_freq
|
|
|
|
self.flash_mode = flash_mode
|
|
|
|
self.espidf = espidf
|
|
|
|
self.bootloader_bin = bootloader_bin
|
|
|
|
self.partition_table_bin = partition_table_bin
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def name(cls):
|
|
|
|
return 'esp32'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def capabilities(cls):
|
|
|
|
return RunnerCaps(commands={'flash'})
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def do_add_parser(cls, parser):
|
|
|
|
# Required
|
|
|
|
parser.add_argument('--esp-idf-path', required=True,
|
|
|
|
help='path to ESP-IDF')
|
|
|
|
# Optional
|
2021-04-12 19:39:04 -03:00
|
|
|
parser.add_argument('--esp-boot-address', default='0x1000',
|
|
|
|
help='bootloader load address')
|
|
|
|
parser.add_argument('--esp-partition-table-address', default='0x8000',
|
|
|
|
help='partition table load address')
|
|
|
|
parser.add_argument('--esp-app-address', default='0x10000',
|
|
|
|
help='application load address')
|
2019-01-23 08:31:06 -07:00
|
|
|
parser.add_argument('--esp-device', default='/dev/ttyUSB0',
|
|
|
|
help='serial port to flash, default /dev/ttyUSB0')
|
|
|
|
parser.add_argument('--esp-baud-rate', default='921600',
|
|
|
|
help='serial baud rate, default 921600')
|
|
|
|
parser.add_argument('--esp-flash-size', default='detect',
|
|
|
|
help='flash size, default "detect"')
|
|
|
|
parser.add_argument('--esp-flash-freq', default='40m',
|
|
|
|
help='flash frequency, default "40m"')
|
|
|
|
parser.add_argument('--esp-flash-mode', default='dio',
|
|
|
|
help='flash mode, default "dio"')
|
|
|
|
parser.add_argument(
|
|
|
|
'--esp-tool',
|
|
|
|
help='''if given, complete path to espidf. default is to search for
|
|
|
|
it in [ESP_IDF_PATH]/components/esptool_py/esptool/esptool.py''')
|
|
|
|
parser.add_argument('--esp-flash-bootloader',
|
|
|
|
help='Bootloader image to flash')
|
|
|
|
parser.add_argument('--esp-flash-partition_table',
|
|
|
|
help='Partition table to flash')
|
|
|
|
|
|
|
|
@classmethod
|
2020-06-23 13:35:52 -07:00
|
|
|
def do_create(cls, cfg, args):
|
2019-01-23 08:31:06 -07:00
|
|
|
if args.esp_tool:
|
|
|
|
espidf = args.esp_tool
|
|
|
|
else:
|
|
|
|
espidf = path.join(args.esp_idf_path, 'components', 'esptool_py',
|
|
|
|
'esptool', 'esptool.py')
|
|
|
|
|
|
|
|
return Esp32BinaryRunner(
|
2021-06-14 11:19:33 -03:00
|
|
|
cfg, args.esp_device, boot_address=args.esp_boot_address,
|
2021-04-12 19:39:04 -03:00
|
|
|
part_table_address=args.esp_partition_table_address,
|
2021-06-14 11:19:33 -03:00
|
|
|
app_address=args.esp_app_address,baud=args.esp_baud_rate,
|
2019-01-23 08:31:06 -07:00
|
|
|
flash_size=args.esp_flash_size, flash_freq=args.esp_flash_freq,
|
|
|
|
flash_mode=args.esp_flash_mode, espidf=espidf,
|
|
|
|
bootloader_bin=args.esp_flash_bootloader,
|
|
|
|
partition_table_bin=args.esp_flash_partition_table)
|
|
|
|
|
|
|
|
def do_run(self, command, **kwargs):
|
2019-06-02 16:18:39 -06:00
|
|
|
self.require(self.espidf)
|
2019-01-23 08:31:06 -07:00
|
|
|
bin_name = path.splitext(self.elf)[0] + path.extsep + 'bin'
|
2021-04-12 19:39:04 -03:00
|
|
|
cmd_flash = [self.espidf, '--chip', 'auto', '--port', self.device,
|
2019-01-23 08:31:06 -07:00
|
|
|
'--baud', self.baud, '--before', 'default_reset',
|
|
|
|
'--after', 'hard_reset', 'write_flash', '-u',
|
|
|
|
'--flash_mode', self.flash_mode,
|
|
|
|
'--flash_freq', self.flash_freq,
|
|
|
|
'--flash_size', self.flash_size]
|
|
|
|
|
2019-09-24 10:06:55 -07:00
|
|
|
# Execute Python interpreter if calling a Python script
|
|
|
|
if self.espidf.lower().endswith(".py") and sys.executable:
|
|
|
|
cmd_flash.insert(0, sys.executable)
|
|
|
|
|
2021-04-12 19:39:04 -03:00
|
|
|
if self.bootloader_bin :
|
|
|
|
cmd_flash.extend([self.boot_address, self.bootloader_bin])
|
|
|
|
cmd_flash.extend([self.part_table_address, self.partition_table_bin])
|
|
|
|
cmd_flash.extend([self.app_address, bin_name])
|
2021-06-14 11:19:33 -03:00
|
|
|
else :
|
2021-04-12 19:39:04 -03:00
|
|
|
cmd_flash.extend([self.boot_address, bin_name])
|
2019-01-23 08:31:06 -07:00
|
|
|
|
2021-04-12 19:39:04 -03:00
|
|
|
self.logger.info("Flashing esp32 chip on {} ({}bps)".
|
2019-06-02 21:33:41 -06:00
|
|
|
format(self.device, self.baud))
|
2019-01-23 08:31:06 -07:00
|
|
|
self.check_call(cmd_flash)
|