Skip to content

Instantly share code, notes, and snippets.

@justinmassiot
Last active July 31, 2024 12:26
Show Gist options
  • Save justinmassiot/ecdb38857427598b909aaabf28de1179 to your computer and use it in GitHub Desktop.
Save justinmassiot/ecdb38857427598b909aaabf28de1179 to your computer and use it in GitHub Desktop.
Multiple useful scripts to flash/erase Altera FPGA/CPLD circuits
@echo off
REM Path to the POF binary file
set pof_file="my_bitstream_file.pof"
REM Index of the target device in the JTAG chain
set device_index=1
REM Device name (for "Examine" process) ; example: EPM1270T144
set device_name=THE_DEVICE_NAME
REM Timeout before retry (in seconds)
set timeout=5
REM Time between each JTAG chain scan (in seconds) - works only if the "choice" program is available on the system
set refresh_time=1
REM Force programming of the already up-to-date devices ; 0=False (default) , 1=True
set force_pgm=0
REM Default text color in the console (see "color /?" for a list of colors)
set default_color=7
REM Debugging option: "nul" (debug Off) OR "con" (debug On)
set output=nul
REM Please do not edit the script below this line! (unless you're absolutely sure of what you're doing)
REM ################################################################################
REM Set the window title
title Cyclic JTAG programming
REM Silently check if the "choice" command is available on the system
set cmd_choice=0
choice /c abcdef /d f /t 0 > %output% 2>&1
if %ErrorLevel% equ 6 set cmd_choice=1
REM Silently check if the "quartus_pgm" program is reachable
%QUARTUS_ROOTDIR%\bin\quartus_pgm --version > %output%
if %ErrorLevel% neq 0 goto error_quartus_pgm
:check_file
if exist %pof_file% goto check_cable
REM Clear screen, and switch console color to Red (C)
cls
color C
echo.
echo ERROR 1: FILE NOT FOUND!
echo The source binary file was not found. (%pof_file%)
echo Please provide a file with the correct filename in the appropriate location.
echo.
set /p anyvar=Press Enter when it is done.
goto check_file
:check_cable
REM Clear screen, and switch console color to default color
cls
color %default_color%
echo.
echo Checking if cable is properly connected ...
REM Try to list connected JTAG devices
%QUARTUS_ROOTDIR%\bin\quartus_pgm --auto > %output%
REM If cable not detected, the command returns an error because the programming adapter is not connected
if %ErrorLevel% neq 0 goto error_cable
:check_device
REM Clear screen, and switch console color to default color
cls
color %default_color%
echo.
echo Trying to communicate with the onboard device ...
REM Try to execute an "Examine" + "Clamp" (E + C) command on the target device %device_name% at index %device_index%
REM "Clamp" is optional but allow to avoid some errors "writing to file..." in the Examine operation
%QUARTUS_ROOTDIR%\bin\quartus_pgm --mode JTAG --operation=CE;nul;%device_name%@%device_index% > %output%
REM If Examine fails, the command returns an error because device has not been found
if %ErrorLevel% neq 0 goto error_device
echo Device found!
REM Check that the target device really needs to be programmed by applying a "Verify" (V) command
if %force_pgm% neq 0 goto program_device
echo.
echo Checking current version ...
%QUARTUS_ROOTDIR%\bin\quartus_pgm --mode JTAG --operation=V;%pof_file%@%device_index% > %output%
if %ErrorLevel% equ 0 goto already_programmed
:program_device
REM Clear screen, and switch console color to Cyan (B)
cls
color B
echo.
REM Blank-check (B) + Program (P) + Verify (V) device at index %device_index%
REM Then initialize the target with its boot configuration (--initcfg)
echo Programming your target device at index %device_index% with file %pof_file% ...
%QUARTUS_ROOTDIR%\bin\quartus_pgm --mode JTAG --operation=BPV;%pof_file%@%device_index% --initcfg > %output%
if %ErrorLevel% neq 0 goto error_programming
:programming_ok
REM Clear screen, and switch console color to Light Green (A)
cls
color A
echo.
echo SUCCESS: Your device has been successfully programmed.
echo You can now disconnect your board and plug an other one.
if %cmd_choice% neq 1 goto prog_loop
echo.
echo.
echo.
echo Tip: Hit and hold 'f' key to program again.
REM Wait until the cable is removed from the programmed board (operation "Verify" (V) gives an error when the device is unreachable, and also when the device is programmed with an outdated binary file - i.e. not the board that has just been programmed!).
:prog_loop
REM If user presses 'f' key, then force the script to program again
if %cmd_choice% neq 1 goto no_choice1
choice /c fz /t %refresh_time% /d z /n > %output%
if %ErrorLevel% equ 1 goto program_device
:no_choice1
REM Else, wait until the board is unplugged
%QUARTUS_ROOTDIR%\bin\quartus_pgm --mode JTAG --operation=V;%pof_file%@%device_index% > %output%
if %ErrorLevel% equ 0 goto prog_loop
REM When cable removed, let's program the next board
goto check_device
:already_programmed
REM Clear screen, and switch console color to Pink (D)
cls
color D
echo.
echo INFO: Your device is already up-to-date.
echo You can now disconnect your board and plug an other one.
if %cmd_choice% neq 1 goto no_choice2
echo.
echo.
echo.
echo Tip: Hit and hold 'f' key to force programming the device.
:no_choice2
goto prog_loop
:error_cable
REM Clear screen, and switch console color to Red (C)
cls
color C
echo.
echo ERROR 2: NO CABLE DETECTED!
echo.
echo The USB-JTAG adapter seems not to be connected to your computer. Please check the following:
echo * Is the cable connected properly?
echo * Is the cable driver installed properly?
echo * Is your USB or Serial port working correctly?
call sleep.bat %timeout%
goto check_cable
:error_device
REM Clear screen, and switch console color to Red (C)
cls
color C
echo.
echo ERROR 3: TARGET DEVICE NOT FOUND!
echo.
echo Device %device_name% at index %device_index% is not reachable. Please check the following:
echo * Is your adapter connected to the target board?
echo * Is your target board powered on?
echo * Is the device index equal to %device_index%?
echo * Is the device name equal to %device_name%?
call sleep.bat %timeout%
REM Switch back to cable verification, just to be sure
goto check_cable
:error_programming
if not exist %pof_file% goto check_file
REM Clear screen, and switch console color to Red (C)
cls
color C
echo.
echo ERROR 4: PROGRAMMING HAS FAILED!
echo Programming operation has failed.
echo Please check the following:
echo * Is your adapter still connected to the target board?
echo * Is your power supply stable?
echo.
set /p anyvar=*** Correct the problem(s) and press Enter to retry. ***
REM Switch back to device verification, just to be sure
goto check_device
:error_quartus_pgm
REM Clear screen, and switch console color to Red (C)
cls
color C
echo.
echo ERROR: PROGRAMMER IS NOT AVAILABLE!
echo The file "quartus_pgm.exe" has not been found on your computer.
echo As a consequence, none of the programming operations are available.
echo.
echo This script will exit.
echo.
REM Exit the script
pause
goto eof
:eof
@echo off
REM Path to the POF binary file
set pof_file="my_bitstream_file.pof"
REM Index of the target device in the JTAG chain
set device_index=1
REM Set the window title
title JTAG erasing
REM Silently check if the "quartus_pgm" program is reachable
%QUARTUS_ROOTDIR%\bin\quartus_pgm --version > nul
if %ErrorLevel% neq 0 goto error_quartus_pgm
echo.
echo Erasing and blank-checking the device...
echo.
REM Erase (R) + Blank-check (B) device at index %device_index%
%QUARTUS_ROOTDIR%\bin\quartus_pgm --mode JTAG --operation=R;%pof_file%@%device_index%
%QUARTUS_ROOTDIR%\bin\quartus_pgm --mode JTAG --operation=B;%pof_file%@%device_index%
echo.
if %ErrorLevel% neq 0 goto error
:pass
echo Operation is terminated.
echo Please reboot the board.
echo.
pause
goto eof
:error
echo Erase operation has FAILED.
echo.
pause
goto eof
:error_quartus_pgm
echo ERROR: PROGRAMMER IS NOT AVAILABLE!
echo The file "quartus_pgm.exe" has not been found on your computer.
echo As a consequence, none of the programming/erasing operations are available.
echo This script will exit.
echo.
pause
goto eof
:eof
@echo off
REM Path to the POF binary file
set pof_file="my_bitstream_file.pof"
REM Index of the target device in the JTAG chain
set device_index=1
REM Set the window title
title JTAG programming
REM Silently check if the "quartus_pgm" program is reachable
%QUARTUS_ROOTDIR%\bin\quartus_pgm --version > nul
if %ErrorLevel% neq 0 goto error_quartus_pgm
echo.
echo Blank-checking, programming and verifying the device...
echo.
REM Blank-check (B) + Program (P) + Verify (V) device at index %device_index%
REM Then initialize CPLD with its boot configuration (--initcfg)
%QUARTUS_ROOTDIR%\bin\quartus_pgm --mode JTAG --operation=BPV;%pof_file%@%device_index% --initcfg
echo.
if %ErrorLevel% neq 0 goto error
:pass
echo Operation is terminated.
echo Please reboot the board.
echo.
pause
goto eof
:error
echo Program operation has FAILED.
echo.
pause
goto eof
:error_quartus_pgm
echo ERROR: PROGRAMMER IS NOT AVAILABLE!
echo The file "quartus_pgm.exe" has not been found on your computer.
echo As a consequence, none of the programming/erasing operations are available.
echo This script will exit.
echo.
pause
goto eof
:eof
@echo off
%QUARTUS_ROOTDIR%\bin\quartus_pgm --auto
pause
REM Windows sleep script by Justin MASSIOT
REM Example use to sleep for 15 seconds: "call sleep.bat 15"
@echo off
REM Check if the sleep time is defined
if "%1"=="" ( goto usage )
REM Check if the time is greater than 0
if %1 leq 0 ( goto usage )
REM When using the "ping" hack, we must add 1 sec to the time because there is an offset
set /a time_ping=%1+1
:sleep
REM Extract the numeric version of Windows from "ver.exe": "... [version X.X ...]"
for /f "tokens=2 delims=[" %%a in ('ver ^| findstr /v "linux"') do @set winver=%%a
REM Then we get the X.X version by removing the 8 first characters
REM Extract the version number (X.X), and do different actions if greater or equal than 6.0 (Vista and newer) or less than 6.0 (XP and older)
if %winver:~8,3% geq 6.0 (
REM Windows Vista, 7 and newer
timeout %1
) else (
REM Windows XP, 2000 (and older?)
echo.
REM The following line is a HACK to sleep in Win XP and older
echo Waiting for %1 second^(s^)...
ping -n %time_ping% 127.0.0.1 > nul
)
goto eof
:usage
echo Usage:
echo SLEEP time_in_seconds
sleep 2
:eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment