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
rperkins
Posts: 1422
Joined: Sun Jan 08, 2012 12:51 pm
Country: -

generate Intel Hex file for debugging purposes with bash

Post by rperkins »

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

#!/bin/bash
pages=2
pagesize=256
#bytecount=16
bytecount=32
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
                        currentbyte="${msbhex}${lsbhex}"
                        linetotal="$linetotal 0x$currentbyte +"
                        echo -n "$currentbyte"
                        ((lsb++))
                        ((address++))
                        if [ $lsb -gt 15 ] ;then
                                lsb=0
                        fi
                done
                linetotal="$linetotal 0x0"
                #echo "this is linetotal $linetotal"
                linesum=$(($linetotal))
                padwidth=00 ;convert=$linesum; dec2hex; linesumhex=$converted
                truncatedhex=${linesumhex: -2}
                checksum=$((0x100 - 0x$truncatedhex))
                # this is real hackish
                if [ $checksum -eq 256 ] ;then
                        checksum=0
                fi
                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
                linetotal=""
        done
        ((msb++))
        if [ $msb -gt 15 ] ;then
                msb=0
        fi
done
# we are done so lets generate the closing line
echo "${startcode}00000001FF"

the resulting hex

Code: Select all

:20000000000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0FF0
:20002000000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0FD0
:20004000000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0FB0
:20006000000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F90
:20008000000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F70
:2000A000000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F50
:2000C000000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F30
:2000E000000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F10
:20010000101112131415161718191A1B1C1D1E1F101112131415161718191A1B1C1D1E1FEF
:20012000101112131415161718191A1B1C1D1E1F101112131415161718191A1B1C1D1E1FCF
:20014000101112131415161718191A1B1C1D1E1F101112131415161718191A1B1C1D1E1FAF
:20016000101112131415161718191A1B1C1D1E1F101112131415161718191A1B1C1D1E1F8F
:20018000101112131415161718191A1B1C1D1E1F101112131415161718191A1B1C1D1E1F6F
:2001A000101112131415161718191A1B1C1D1E1F101112131415161718191A1B1C1D1E1F4F
:2001C000101112131415161718191A1B1C1D1E1F101112131415161718191A1B1C1D1E1F2F
:2001E000101112131415161718191A1B1C1D1E1F101112131415161718191A1B1C1D1E1F0F
:00000001FF
256x2.hex
Intel hex file
(1.2 KiB) Downloaded 452 times
intel_hex_generator.txt
script to create intel hex file for debugging
(1.69 KiB) Downloaded 496 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”