scripts: Support SDIO in flash-sdcard using split verification step

Signed-off-by: Donna Whisnant <dewhisna@dewtronics.com>
This commit is contained in:
Donna Whisnant 2022-08-22 10:23:49 -05:00 committed by KevinOConnor
parent f4440267e6
commit a0457f79e4
2 changed files with 48 additions and 10 deletions

View File

@ -10,6 +10,7 @@ KLIPPER_BIN_DEFAULT=$KLIPPER_BIN
KLIPPER_DICT_DEFAULT="${SRCDIR}/out/klipper.dict" KLIPPER_DICT_DEFAULT="${SRCDIR}/out/klipper.dict"
SPI_FLASH="${SRCDIR}/scripts/spi_flash/spi_flash.py" SPI_FLASH="${SRCDIR}/scripts/spi_flash/spi_flash.py"
BAUD_ARG="" BAUD_ARG=""
CHECK_ARG=""
# Force script to exit if an error occurs # Force script to exit if an error occurs
set -e set -e
@ -17,7 +18,7 @@ print_help_message()
{ {
echo "SD Card upload utility for Klipper" echo "SD Card upload utility for Klipper"
echo echo
echo "usage: flash_sdcard.sh [-h] [-l] [-b <baud>] [-f <firmware>] [-d <dictionary>]" echo "usage: flash_sdcard.sh [-h] [-l] [-c] [-b <baud>] [-f <firmware>] [-d <dictionary>]"
echo " <device> <board>" echo " <device> <board>"
echo echo
echo "positional arguments:" echo "positional arguments:"
@ -27,13 +28,14 @@ print_help_message()
echo "optional arguments:" echo "optional arguments:"
echo " -h show this message" echo " -h show this message"
echo " -l list available boards" echo " -l list available boards"
echo " -c run flash check/verify only (skip upload)"
echo " -b <baud> serial baud rate (default is 250000)" echo " -b <baud> serial baud rate (default is 250000)"
echo " -f <firmware> path to klipper.bin" echo " -f <firmware> path to klipper.bin"
echo " -d <dictionary> path to klipper.dict for firmware validation" echo " -d <dictionary> path to klipper.dict for firmware validation"
} }
# Parse command line "optional args" # Parse command line "optional args"
while getopts "hlb:f:d:" arg; do while getopts "hlcb:f:d:" arg; do
case $arg in case $arg in
h) h)
print_help_message print_help_message
@ -43,6 +45,7 @@ while getopts "hlb:f:d:" arg; do
${KLIPPY_ENV} ${SPI_FLASH} -l ${KLIPPY_ENV} ${SPI_FLASH} -l
exit 0 exit 0
;; ;;
c) CHECK_ARG="-c";;
b) BAUD_ARG="-b ${OPTARG}";; b) BAUD_ARG="-b ${OPTARG}";;
f) KLIPPER_BIN=$OPTARG;; f) KLIPPER_BIN=$OPTARG;;
d) KLIPPER_DICT=$OPTARG;; d) KLIPPER_DICT=$OPTARG;;
@ -82,4 +85,4 @@ fi
# Run Script # Run Script
echo "Flashing ${KLIPPER_BIN} to ${DEVICE}" echo "Flashing ${KLIPPER_BIN} to ${DEVICE}"
${KLIPPY_ENV} ${SPI_FLASH} ${BAUD_ARG} ${KLIPPER_DICT} ${DEVICE} ${BOARD} ${KLIPPER_BIN} ${KLIPPY_ENV} ${SPI_FLASH} ${CHECK_ARG} ${BAUD_ARG} ${KLIPPER_DICT} ${DEVICE} ${BOARD} ${KLIPPER_BIN}

View File

@ -785,6 +785,13 @@ class SDCardSPI:
if err_msgs: if err_msgs:
raise OSError("\n".join(err_msgs)) raise OSError("\n".join(err_msgs))
SDIO_WARNING = """
This board requires a manual reboot to complete the flash process.
If the board's bootloader uses SDIO mode for its SDCard, then a full
power cycle is required. Please perform the power cycle now and then
rerun this utility with the 'check' option to verify flash.
"""
class MCUConnection: class MCUConnection:
def __init__(self, k_reactor, device, baud, board_cfg): def __init__(self, k_reactor, device, baud, board_cfg):
self.reactor = k_reactor self.reactor = k_reactor
@ -989,6 +996,9 @@ class MCUConnection:
return sd_chksm return sd_chksm
def verify_flash(self, req_chksm, old_dictionary, req_dictionary): def verify_flash(self, req_chksm, old_dictionary, req_dictionary):
if bool(self.board_config.get('skip_verify', False)):
output_line(SDIO_WARNING)
return
output("Verifying Flash...") output("Verifying Flash...")
validation_passed = False validation_passed = False
msgparser = self._serial.get_msgparser() msgparser = self._serial.get_msgparser()
@ -1063,6 +1073,7 @@ class SPIFlash:
self.firmware_checksum = None self.firmware_checksum = None
self.task_complete = False self.task_complete = False
self.need_upload = True self.need_upload = True
self.need_verify = True
self.old_dictionary = None self.old_dictionary = None
self.new_dictionary = None self.new_dictionary = None
if args['klipper_dict_path'] is not None: if args['klipper_dict_path'] is not None:
@ -1092,7 +1103,7 @@ class SPIFlash:
raise SPIFlashError("Unable to reconnect") raise SPIFlashError("Unable to reconnect")
output_line("Done") output_line("Done")
def run_reset(self, eventtime): def run_reset_upload(self, eventtime):
# Reset MCU to default state if necessary # Reset MCU to default state if necessary
self.mcu_conn.connect() self.mcu_conn.connect()
if self.mcu_conn.check_need_restart(): if self.mcu_conn.check_need_restart():
@ -1102,6 +1113,16 @@ class SPIFlash:
self.need_upload = False self.need_upload = False
self.run_sdcard_upload(eventtime) self.run_sdcard_upload(eventtime)
def run_reset_verify(self, eventtime):
# Reset MCU to default state if necessary
self.mcu_conn.connect()
if self.mcu_conn.check_need_restart():
self.mcu_conn.reset()
self.task_complete = True
else:
self.need_verify = False
self.run_verify(eventtime)
def run_sdcard_upload(self, eventtime): def run_sdcard_upload(self, eventtime):
# Reconnect and upload # Reconnect and upload
if not self.mcu_conn.connected: if not self.mcu_conn.connected:
@ -1121,7 +1142,8 @@ class SPIFlash:
def run_verify(self, eventtime): def run_verify(self, eventtime):
# Reconnect and verify # Reconnect and verify
self.mcu_conn.connect() if not self.mcu_conn.connected:
self.mcu_conn.connect()
self.mcu_conn.configure_mcu() self.mcu_conn.configure_mcu()
self.mcu_conn.verify_flash(self.firmware_checksum, self.old_dictionary, self.mcu_conn.verify_flash(self.firmware_checksum, self.old_dictionary,
self.new_dictionary) self.new_dictionary)
@ -1148,12 +1170,18 @@ class SPIFlash:
self.mcu_conn = k_reactor = None self.mcu_conn = k_reactor = None
def run(self): def run(self):
self.run_reactor_task(self.run_reset) if not bool(self.board_config.get('verify_only', False)):
self._wait_for_reconnect() self.run_reactor_task(self.run_reset_upload)
if self.need_upload:
self.run_reactor_task(self.run_sdcard_upload)
self._wait_for_reconnect() self._wait_for_reconnect()
self.run_reactor_task(self.run_verify) if self.need_upload:
self.run_reactor_task(self.run_sdcard_upload)
self._wait_for_reconnect()
self.run_reactor_task(self.run_verify)
else:
self.run_reactor_task(self.run_reset_verify)
if self.need_verify:
self._wait_for_reconnect()
self.run_reactor_task(self.run_verify)
def main(): def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -1177,6 +1205,9 @@ def main():
parser.add_argument( parser.add_argument(
"-d", "--dict_path", metavar="<klipper.dict>", type=str, "-d", "--dict_path", metavar="<klipper.dict>", type=str,
default=None, help="Klipper firmware dictionary") default=None, help="Klipper firmware dictionary")
parser.add_argument(
"-c","--check", action="store_true",
help="Perform flash check/verify only")
parser.add_argument( parser.add_argument(
"device", metavar="<device>", help="Device Serial Port") "device", metavar="<device>", help="Device Serial Port")
parser.add_argument( parser.add_argument(
@ -1195,6 +1226,10 @@ def main():
flash_args['baud'] = args.baud flash_args['baud'] = args.baud
flash_args['klipper_bin_path'] = args.klipper_bin_path flash_args['klipper_bin_path'] = args.klipper_bin_path
flash_args['klipper_dict_path'] = args.dict_path flash_args['klipper_dict_path'] = args.dict_path
flash_args['verify_only'] = args.check
if args.check:
# override board_defs setting when doing verify-only:
flash_args['skip_verify'] = False
check_need_convert(args.board, flash_args) check_need_convert(args.board, flash_args)
fatfs_lib.check_fatfs_build(output) fatfs_lib.check_fatfs_build(output)
try: try: