@echo off
:START

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

  cls  
  echo This will update the SRC-128 Samplerate converter's firmware to    
  echo the latest version. Please execute the following steps:
  echo.
  echo   - Make sure the ASRC module is correctly installed in the multiverter
  echo   - Switch off multiverter
  echo   - *** REMOVE MAINS INPUT POWER TO AVOID ELECTRICAL SHOCK !!! ***
  echo   - Remove the cover from the multiverter
  echo   - Plug the USB cable into the multiverter
  echo   - *** CAREFULLY RE-CONNECT MAINS POWER. DON'T TOUCH ANYTHING INSIDE !!! ***
  echo   - *** 230 V LIVE PARTS COULD BE TOUCHED !!!                             ***
  echo   - Switch ON multiverter 
  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


  cls
  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\src-128.rpd | win\tee %TMP%\fwloader.log
  grep Error %TMP%\fwloader.log && goto FWLOADERERROR

:SUCCESS  
  cls
  echo SUCCESS!
  echo.
  echo Please switch off your multiverter.
  echo *** CAUTION: REMOVE MAINS POWER BEFORE RE-MOUNTING THE COVER!!! ***
  goto END


:NOTFOUND
  cls
  echo ERROR: COM PORTS NOT FOUND - FTDI DRIVER INSTALLED?
  echo.
  echo 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
  


:FWLOADERERROR
  cls
  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

  
