@echo off
:START

  REM *** FLX firmware update script 
  REM *** Copyright (c) 2015-2021 Appsys ProAudio

  cls  
  echo This will update the Flexiverter's firmware to    
  echo the latest version. Please follow the steps below:
  echo.
  echo   - connect flexiverter via USB to your computer
  echo   - turn flexiverter ON
  echo   - continue below by pressing "U":
  echo.
  choice /M "Press U to update or C to cancel" /c UC
  if errorlevel 2 goto END

  REM # Find complete name of the COM ports from the registry, set %PORTA% and %PORTB% to them
  set PORTA=
  set PORTB=
  del /q %TMP%\port?.tmp 2>NUL
  reg query "HKLM\SYSTEM\CurrentControlSet\Enum\FTDIBUS" /s /f PortName | win\grep -1 VID_0403.PID_6010.*1\\0000 | win\tail -1 | win\cut -c 27- > %TMP%\porta.tmp 
  reg query "HKLM\SYSTEM\CurrentControlSet\Enum\FTDIBUS" /s /f PortName | win\grep -1 VID_0403.PID_6010.*2\\0000 | win\tail -1 | win\cut -c 27- > %TMP%\portb.tmp 
  set /p PORTA=<%TMP%\porta.tmp 
  set /p PORTB=<%TMP%\portb.tmp 
  if [%PORTA%] == [] goto NOTFOUND
  if [%PORTB%] == [] goto NOTFOUND

  echo.
  echo Flashing FPGA (takes about 1 minute)...
  echo.
  REM *** Find USB device index of Port B (not always 1!)
  timeout 2 >NUL
  del /q %TMP%\usbdev.tmp 2>NUL
  win\fwloader -l | win\grep "Dual RS232 B" | win\cut -c 5 > %TMP%\usbdev.tmp
  set /p USBDEV=<%TMP%\usbdev.tmp 
  del /q %TMP%\fwloader.log 2>NUL
  win\fwloader -m 1 -e 16 -i %USBDEV% -f firmware\flx_dante.rpd | win\tee %TMP%\fwloader.log
  win\grep Error %TMP%\fwloader.log && goto FWLOADERERROR

:SUCCESS  
  echo.
  echo *** Flashing firmware was successful.                                    ***
  echo *** REQUIRED: PLEASE UPDATE ALSO DANTE FIRMWARE (.dnt file) OVER NETWORK ***
  echo.
  echo    - Use Dante Controller or Dante Firmware Update Manager from 
  echo          https://my.audinate.com/latest-firmware-update-manager
  echo      to flash the FLX-DANTE_x_x_x_x.dnt file from the "firmware" directory onto the Dante module

  echo    - Power cycle the flexiverter afterwards
  echo. 
  goto END


:NOTFOUND
  echo.
  echo *** ERROR: COM PORTS NOT FOUND - FTDI DRIVER INSTALLED? ***
  echo.
  echo Very likely you need to install the COM drivers first. 
  echo Get the latest from FTDI:  https://ftdichip.com/drivers/vcp-drivers/
  echo and restart this updater.
  echo.
  echo If the error persists, please refer to the "Troubleshooting"  
  echo section of the README.TXT file.
  echo.
  goto END
  


:FWLOADERERROR
  echo.
  echo *** ERROR: FAILED TO PROGRAM FPGA ***
  echo.
  echo Make sure that:                                           
  echo - The ASRC 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 %TMP%\fwloader.log.
  echo.
  goto END

  
:END
  pause

  
