male - 2006-02-06: sorry to ask for, but ... what is a s-record file format?
LM - The Motorola S-Record is used to upload code in ASCII format into devices, e.g. PROM programmers (burners), that use a simple ASCII terminal as input device.
# bin2srec.tcl
# convert a file into s-record format
# (with a bare bone GUI)
#
# Lino Monaco - Jan 2006
# _________________________________________
package require Tk
# Some parameters with fixed values
set recordlen 0x15
set startaddr 0x2000000
proc checksum {record} {
#
# Calculate the record checksum
#______________________________
set chksum [expr [join [regsub -all {[0-9a-fA-f]{2}} $record "0x& "] +]]
set chksum [expr {~($chksum) & 0x000000FF}]
return [format "%02X" $chksum]
}
proc createS0 {record} {
#
# Format the S0 record type
# S0<len><0000><data><checksum>
#________________________________
set rec 0000
binary scan $record H* record
append rec $record
set len [format "%02X" [expr {[string length $rec] / 2 + 1}]]
append srec $len $rec
append srec [checksum $srec]
return "S0[string toupper $srec]"
}
proc createS3 {addr record} {
#
# Format the S3 record type
# S3<len><addr><data><checksum>
#________________________________
set rec [format "%08X" $addr]
binary scan $record H* record
append rec $record
set len [format "%02X" [expr {[string length $rec] / 2 + 1}]]
append srec $len $rec
append srec [checksum $srec]
return "S3[string toupper $srec]"
}
proc createS7 {addr} {
#
# Format the S7 record type
# S7<len><addr><checksum>
#__________________________
set rec [format "%08X" $addr]
set len [format "%02X" [expr {[string length $rec] / 2 + 1}]]
append srec $len $rec
append srec [checksum $srec]
return "S7[string toupper $srec]"
}
proc bin2srec {} {
#
# Open input and output files and
# executes the conversion
#________________________________
# Open the input file in binary mode
set binfname [tk_getOpenFile]
if {$binfname == ""} {
return
}
if {[catch {open $binfname r} binfd]} {
tk_messageBox -message "I cannot open $binfname"
return
}
fconfigure $binfd -translation binary
# Create the output file adding extension .srec
set srecfname $binfname.srec
if {[catch {open $srecfname w} hexfd]} {
tk_messageBox -message "I cannot open $srecfname"
close $binfd
return
}
# Create a srec file with the following parameters:
# initial address = 0
# s-rec title = <binfname>
# record len = <recordlen>
# loading address = <startaddr>
#
puts $hexfd [createS0 [file tail $binfname]]
set count 0
while {! [eof $binfd]} {
set record [read $binfd $::recordlen]
puts $hexfd [createS3 $count $record]
incr count $::recordlen
}
puts $hexfd [createS7 $::startaddr]
# Close files
close $binfd
close $hexfd
}
proc mainGUI {} {
#
# Bare-bone GUI
# todo: srec parameters configuration
#
# windows title ________________________________________________________
wm title . "Bin to Srec"
# main frame construction
frame .bframe
label .bframe.lbl -text "Convert a file in srec format" -width 30
button .bframe.btn -text "Convert" -width 10 -pady 5 -command {bin2srec}
eval pack [winfo children .bframe] -side top
pack .bframe
# Menu configuration____________________________________________________
menu .menubar -type menubar
.menubar add cascade -label File -menu .menubar.file
#file menu
menu .menubar.file -tearoff 0
.menubar.file add command -label Open -underline 1 -command {bin2srec}
.menubar.file add separator
.menubar.file add command -label Exit -underline 1 -command {exit}
#end file menu
. configure -menu .menubar
# Various_______________________________________________________________
focus .bframe
# Bind console ____________
# for windows only
bind . <F1> {console show}
}
mainGUI
