generate Intel Hex file for debugging purposes with bash

Projects that are not for RC, but are cool and worthy of development.
Post Reply
User avatar
Posts: 1422
Joined: Sun Jan 08, 2012 12:51 pm
Country: -

generate Intel Hex file for debugging purposes with bash

Post by rperkins » Tue Nov 18, 2014 2:24 am

This script will generate a valid Intel Hex file that can be uploaded to an Atmel MCU. The hex file can be generated so the the even bytes of data are set to the page size of the MCU you are debugging. The odd bytes are sequential. Of course this code doesnt run, but the MCU will accept it and read it back to you. The page size, number of generated pages and the record length per line(byte count) can be configured from within the script. It is currently configured to generate 2 pages of 256 bytes with a record length (byte count) of 32 bytes. an ATmega 64 uses 256 byte pages.

usbasp does paged writes and i wrote this to investigate some writes that dont appear to be happening on page boundaries. Not sure if it will be helpful for anyone else but thought I would throw it out there

the code

Code: Select all

address=0 ;msb=0 ;lsb=0 ;startcode=":" ;recordtype="00"

function dec2hex () { converted=$(printf "%${padwidth}X\n" $convert) ;}

for ((t=1; t<=$pages; t++)) ;do
        #echo this is page number $t
        for ((p=1; p<=(($pagesize/$bytecount)); p++)) ;do
                #echo this is line number $p of page  number $t
                padwidth=00 ;convert=$bytecount; dec2hex; bytecounthex=$converted
                padwidth=04 ;convert=$address; dec2hex; addresshex=$converted
                echo -n "${startcode}${bytecounthex}${addresshex}${recordtype}"
                linetotal="0x${bytecounthex} + 0x${addresshex:0:2} + 0x${addresshex:2:2} + 0x${recordtype} +"
                for ((b=0; b<$bytecount; b++)) ;do
                padwidth=00 ;convert=$lsb; dec2hex; lsbhex=$converted
                padwidth=00 ;convert=$msb; dec2hex; msbhex=$converted
                        linetotal="$linetotal 0x$currentbyte +"
                        echo -n "$currentbyte"
                        if [ $lsb -gt 15 ] ;then
                linetotal="$linetotal 0x0"
                #echo "this is linetotal $linetotal"
                padwidth=00 ;convert=$linesum; dec2hex; linesumhex=$converted
                truncatedhex=${linesumhex: -2}
                checksum=$((0x100 - 0x$truncatedhex))
                # this is real hackish
                if [ $checksum -eq 256 ] ;then
                padwidth=02 ;convert=$checksum; dec2hex; checksumhex=$converted
                hexcheck=$((0x$truncatedhex + 0x$checksumhex))
                # this prints out the total, hextotal, truncatedhex, CHECKSUM, verification
                #echo "- $linesum $linesumhex $truncatedhex $checksumhex $hexcheck"
                echo $checksumhex
        if [ $msb -gt 15 ] ;then
# we are done so lets generate the closing line
echo "${startcode}00000001FF"

the resulting hex

Code: Select all

Intel hex file
(1.2 KiB) Downloaded 171 times
script to create intel hex file for debugging
(1.69 KiB) Downloaded 184 times
This is a bash script but the forum didnt like my extension so I changed it to .txt

Post Reply

Return to “Other Electronic Projects”