'Use serial access to detect the chip type.
We use the UART\'s datecode register for this, it\'s mapped at
the same address on ESP8266 & ESP32 so we can use one
memory read and compare to the datecode register for each chip
type.
This routine automatically performs ESPLoader.connect() (passing
connect_mode parameter) as part of querying the chip.'

@staticmethod
def detect_chip(port=DEFAULT_PORT, baud=ESP_ROM_BAUD, connect_mode='default_reset'):

    detect_port = ESPLoader(port, baud)
    detect_port.connect(connect_mode)
    print('Detecting    chip    type...', end='')
    sys.stdout.flush()
    date_reg = detect_port.read_reg(ESPLoader.UART_DATA_REG_ADDR)
    for cls in [ESP8266ROM, ESP32ROM]:
        if (date_reg == cls.DATE_REG_VALUE):
            inst = cls(detect_port._port, baud)
            print(('    %s' % inst.CHIP_NAME))
            return inst
    print('')
    raise FatalError(('Unexpected    UART    datecode    value    0x%08x.    Failed    to    autodetect    chip    type.' % date_reg))
