esp32: Use esptool.py to flash with 'make flash'

This flashes Zephyr at 0x1000: that's where the first stage bootloader,
part of the ESP32 ROM, expects to find an "image header".

The second-stage bootloader, part of ESP-IDF, isn't used by the Zephyr
port.  However, the bootloader can be used if desired; please refer to
the ESP-IDF documentation on how to set up partitions tables and use
the bootloader.

The following environment variables will affect the ESP32 flashing
process:

  Variable              Default value
  ESP_DEVICE            /dev/ttyUSB0
  ESP_BAUD_RATE         921600
  ESP_FLASH_SIZE        detect
  ESP_FLASH_FREQ        40m
  ESP_FLASH_MODE        dio
  ESP_TOOL              espidf

It's impossible to determine which serial port the ESP32 board is
connected to, as it uses a generic RS232-USB converter.  The default of
/dev/ttyUSB0 is provided as that's often the assigned name on a Linux
machine without any other such converters.

The baud rate of 921600bps is recommended.  If experiencing issues when
flashing, try halving the value a few times (460800, 230400, 115200,
etc).  It might be necessary to change the flash frequency or the flash
mode; please refer to the esptool documentation for guidance on these
settings.

If ${ESP_TOOL} is set to "espidf", the esptool.py script found within
ESP-IDF will be used.  Otherwise, this variable is handled as a path to
the tool.

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
diff --git a/boards/xtensa/esp32/Makefile.board b/boards/xtensa/esp32/Makefile.board
index 8fb989e..1cc4f83 100644
--- a/boards/xtensa/esp32/Makefile.board
+++ b/boards/xtensa/esp32/Makefile.board
@@ -1,2 +1,3 @@
 EMU_PLATFORM ?=
 DEBUG_SCRIPT :=
+FLASH_SCRIPT := esp32.sh
diff --git a/scripts/support/esp32.sh b/scripts/support/esp32.sh
new file mode 100755
index 0000000..f6b37e1
--- /dev/null
+++ b/scripts/support/esp32.sh
@@ -0,0 +1,50 @@
+#!/bin/bash
+
+ESP_DEVICE=${ESP_DEVICE:-/dev/ttyUSB0}
+ESP_BAUD_RATE=${ESP_BAUD_RATE:-921600}
+ESP_FLASH_SIZE=${ESP_FLASH_SIZE:-detect}
+ESP_FLASH_FREQ=${ESP_FLASH_FREQ:-40m}
+ESP_FLASH_MODE=${ESP_FLASH_MODE:-dio}
+ESP_TOOL=${ESP_TOOL:-espidf}
+
+cmd_flash() {
+	local esptool
+	local elf_name=${O}/${KERNEL_ELF_NAME}
+
+	if [ "x${ESP_TOOL}" = "xespidf" ]; then
+		esptool=${ESP_IDF_PATH}/components/esptool_py/esptool/esptool.py
+	else
+		esptool=${ESP_TOOL}
+	fi
+	if [ ! -x ${esptool} ]; then
+		echo "esptool could not be found at ${esptool}"
+		exit 1
+	fi
+
+	echo "Converting ELF to BIN"
+	${esptool} --chip esp32 elf2image ${elf_name}
+
+	echo "Flashing ESP32 on ${ESP_DEVICE} (${ESP_BAUD_RATE}bps)"
+	${esptool} --chip esp32 \
+		--port ${ESP_DEVICE} \
+		--baud ${ESP_BAUD_RATE} \
+		--before default_reset \
+		--after hard_reset \
+		write_flash \
+		-u \
+		--flash_mode ${ESP_FLASH_MODE} \
+		--flash_freq ${ESP_FLASH_FREQ} \
+		--flash_size ${ESP_FLASH_SIZE} \
+		0x1000 ${elf_name/.elf/.bin}
+}
+
+CMD="$1"; shift
+case "${CMD}" in
+   flash)
+   	cmd_flash "$@"
+   	;;
+   *)
+   	echo "${CMD} not supported"
+   	exit 1
+   	;;
+esac