@echo off
:START

  REM *** Multiverter firmware update script 
  REM *** Copyright (c) 2015-2016 Appsys ProAudio

  cls  
  echo ==========
  echo  WELCOME!
  echo ==========
  echo.
  echo This will update the MVR-64 multiverter's firmware to    
  echo the latest version. Please execute the following steps:
  echo.
  echo   - Switch OFF multiverter                                
  echo   - Connect multiverter via USB cable to PC               
  echo   - Push and hold down the encoder knob while switching ON. All LEDs except Power should be OFF.
  echo   - Release encoder knob                                  
  echo   - When done, continue below by pressing "U":
  echo.
  choice /M "Press U to update or C to cancel" /c UC  
  if errorlevel 2 goto END
  cls
  echo ===========
  echo  IMPORTANT
  echo ===========
  echo.
  echo Please update also the Dante firmware (firmware\MVR-64-x.x.x.x.dnt) using the 
  echo Dante Firmware Update Manager (https://www.audinate.com/products/firmware-update-manager)
  echo after this updater has completed.
  echo.
  choice /M "Press D to confirm or C to cancel" /c DC
  if errorlevel 2 goto END
  
:COM_DETECT
  REM # Find all COM ports for FTDI in the registry and echo them to ports.tmp
  del /q %TEMP%\ports.tmp 2>NUL
  reg query "HKLM\SYSTEM\CurrentControlSet\Enum\FTDIBUS" /s /f PortName | win\grep -1 VID_0403.PID_6010.*\\0000 | win\grep PortName | win\cut -c 27- > %TEMP%\ports.tmp 

:FLASH_CONTROL_PROCESSOR
  setlocal enabledelayedexpansion
  for /F %%A in (%TEMP%\ports.tmp) do (
    cls
    echo ============================================================
    echo  Step 1/2: Flashing Control Processor, takes about 1 minute
    echo ============================================================
    echo Probing control processor on %%A
    win\stm32flash -v -w firmware\frontpanel.bin \\.\%%A && goto FLASH_FPGA
  )
  goto STM32FLASHERROR

:FLASH_FPGA 
  cls
  echo ================================================
  echo  Step 2/2: Flashing FPGA, takes about 2 minutes
  echo ================================================
  echo.
  REM *** Find USB device index of Port B, not always 1!
  timeout 2 >NUL
  del /q %TEMP%\usbdev.tmp 2>NUL
  win\fwloader -l | win\grep "Dual RS232 B" | win\cut -c 5 > %TEMP%\usbdev.tmp
  set /p USBDEV=<%TEMP%\usbdev.tmp 
  del /q %TEMP%\fwloader.log 2>NUL
  win\fwloader -m 1 -e 16 -i %USBDEV% -f firmware\multiverter.rpd | win\tee %TEMP%\fwloader.log
  grep Error %TEMP%\fwloader.log && goto FWLOADERERROR
  goto SUCCESS
  
:SUCCESS  
  cls
  echo ==========
  echo  SUCCESS!
  echo ==========
  echo.
  echo Your multiverter is now running the latest firmware.
  echo Don't forget to update the DANTE firmware also (you might need to disable windows firewall during this).
  echo. 
  goto END


:NOTFOUND
  echo
  echo ========
  echo  ERROR!
  echo ========
  echo.
  echo COM PORTS NOT FOUND 
  echo - MULTIVERTER CONNECTED?
  echo - FTDI DRIVER INSTALLED?
  echo.
  echo.
  echo Please double-check that the Multiverter is connected via USB, and has been powered up
  echo with OK button pressed during power-up.
  echo.
  echo.
  echo If the above is confirmed, very likely you need to install the COM drivers first:
  echo - Power off the multiverter
  echo - Run win\Driver\CDMxxxx_Setup.exe, proceed as indicated
  echo - Power on the multiverter, wait until drivers are installed
  echo - Restart this updater
  echo.
  echo If the error persists, please refer to the "Troubleshooting"  
  echo section of the README.TXT file.
  echo.
  goto END
  

:STM32FLASHERROR
  echo
  echo ========
  echo  ERROR!
  echo ========
  echo.
  echo CONTROL PROCESSOR not found on any of the probed COM ports.
  echo.
  echo Make sure that:                                           
  echo - The multiverter is connected via USB and power-cycled 
  echo   *while* the encoder has been pushed down (only power lights on).
  echo - The proper FTDI drivers have been installed
  echo - In Windows Device Manager, two subsequent "USB Serial Port" devices must show up.
  echo.
  goto END


:FWLOADERERROR
  cls
  echo ERROR: FAILED TO PROGRAM FPGA.
  echo.
  echo Make sure that:                                           
  echo - The multiverter is connected via USB and power-cycled while the 
  echo   encoder has been pushed down                           
  echo - The proper FTDI drivers have been installed
  echo.
  echo If the error persists, please refer to the "Troubleshooting"  
  echo section of the README.TXT file.
  echo For details, see also the file %TEMP%\fwloader.log.
  echo.
  goto END

  
:END
  pause

  
