' "hpicterm" 12/17/12 WTN ' Can be used as a simple serial terminal but designed for HLLPIC. ' Default is first valid comm port (COM1-COM4), 9600 baud, 20ms delay ' Command line: hpicterm [COMx [baud [msdelay [compiler]]]] ' Example: hpicterm COM3 9600 15 ' Default compiler name is "hllcomp", add as parm 4 to specify. ' ' Compile with FreeBASIC (no options), for Linux: fbc hpicterm.bas ' For Windows: "c:\program files\freebasic\fbc" hpicterm.bas ' ' Under Linux can copy/paste to the terminal to upload hex code and scroll ' back to highlight and copy/paste extracted data, Load/Dump options can be ' used if desired but not necessary. Under Windows the "dos" command window ' may not scroll fast enough and characters lost when extracting, and it is ' not possible to copy/paste to the terminal, so the option menu must be used ' to upload code and download hex from the HLLPIC firmware. ' ' The option menu shown when Esc pressed when at the HLLPIC menu.. if the last ' two characters received aren't "> " then the only option is Quit. Pressing ' any key besides the menu options resumes the terminal. The Load/Save options ' are specific to the HLLPIC firmware - 1.12 or later provided the protocols ' haven't changed. Load supports both hex bytes files and HLL files, if a ' file starts with "0000:" it sends the file as-is other than converting ' newlines into CR's. Otherwise shells "hllcomp" to compile the (assumed) HLL ' source file, if no error then sends the resulting hex file. After prompting ' for the save filename, Dump sends "Y" to start an external eeprom extract, ' ignores the first 3 bytes sent back (HLLPIC echoes the Y plus crlf) then ' saves the rest to the specified file. Any received data that isn't a valid ' hex bytes file stops the dump and closes the file. The progress bar assumes ' 8KB of hex data will be returned with a 4-digit hex address every 16 bytes. ' ' History... ' 11/28/12 - 1.00 - initial test versions. ' 11/29/12 - 1.01 - comments, configurable dump progress, text tweaks, ' [[[ Quit ]]] restores display when not quitting. ' 12/17/12 - 1.02 - added command line options to override defaults. ' ' This is public domain code, do whatever you want. ' dim shared openerr as integer dim shared inbyte as byte dim shared keychar as string dim shared outchar as string dim shared exitprog as integer dim shared i as integer dim shared astr as string dim shared cursorpos as integer dim port as string dim baud as string dim chardelay as integer dim shared filename as string dim shared dofileupload as integer dim shared dofiledump as integer dim shared portoptions as string dim shared lastbyte as byte dim shared prevbyte as byte dim shared dumpcounter as integer dim shared menulen as integer dim shared hllcompiler as string dim shared tempbytesfile as string dim shared progress_size as integer dim shared progress_mod as integer port = Command(1) '12/17/12 baud = Command(2) chardelay = Val(Command(3)) hllcompiler = Command(4) if baud = "" then baud = "9600" 'defaults if chardelay = 0 then chardelay = 20 if hllcompiler = "" then hllcompiler = "hllcomp" portoptions = ",CD0,DS,CS,RS" tempbytesfile = "hpicterm.tmpbytes" progress_size = 32 'size of the progress bar progress_mod = int(28676/progress_size) '1st # is dump size in bytes print print "***** HLLPIC TERMINAL 1.02 *****" print "Press reset switch for HLLPIC menu" print "Press Esc to show options or quit" print exitprog = 0 dofileupload = 0 lastbyte = 0 prevbyte = 0 if port <> "" then openerr = open com (port+":"+baud+",N,8,1"+portoptions for binary as #1) else openerr = 1 for i = 1 to 4 if openerr <> 0 then port = "COM"+ltrim(rtrim(str(i))) openerr = open com (port+":"+baud+",N,8,1"+portoptions for binary as #1) end if next i end if if openerr = 0 then while exitprog = 0 while loc(1) get #1,0,inbyte if inbyte <> 0 then prevbyte = lastbyte lastbyte = inbyte print chr(inbyte); end if wend outchar = inkey if outchar = chr(27) then if dofileupload then 'a file upload in progress print print "Cancelling upload." close #2 dofileupload = 0 print #1, chr(13); 'cancel upload in progress print #1, chr(13); print #1, chr(13); else if lastbyte <> 32 or prevbyte <> 62 then 'not at menu cursorpos = pos print astr = "[[[ Quit ]]] " print astr; menulen = len(astr) inbyte = getkey keychar = ucase(chr(inbyte)) if keychar = "Q" then print keychar exitprog = 1 else locate ,1,1 astr = space(menulen) print astr; locate CsrLin-1,cursorpos,1 end if else 'coast is clear.. show option menu cursorpos = pos astr = "[[[ Load Dump Quit ]]] " print astr; menulen = len(astr) inbyte = getkey keychar = ucase(chr(inbyte)) select case keychar case "Q" print keychar exitprog = 1 case "L" print keychar print "Hex or HLL file to load: "; line input filename filename = ltrim(rtrim(filename)) if len(filename) > 0 then openerr = open (filename, for input as #2) if openerr then print "File not found.": print "> "; else 'make sure it's a valid hex bytes file input #2, astr close #2 if left(astr,5) <> "0000:" then i = instr(filename," ") if i > 0 then print "Spaces are not allowed when compiling.": print "> "; else print "Running ";hllcompiler;" to compile..." kill tempbytesfile openerr = shell (hllcompiler+" "+filename+" "+tempbytesfile) if openerr = -1 then print "Can't execute compiler.": print "> "; else openerr = open (tempbytesfile, for input as #2) if openerr then print "Compile failed.": print "> "; else dofileupload = 1 end if end if 'shell error check end if 'filename spaces check else 'hex bytes filename entered openerr = open (filename, for input as #2) if openerr then print "Can't reopen file, that's weird...": print "> "; else dofileupload = 1 end if end if 'valid bytes file check end if 'first open error check else 'no filename entered print "> "; 'fake the serial prompt end if case "D" print keychar print "File to write hex bytes: "; line input filename filename = ltrim(rtrim(filename)) if len(filename) > 0 then openerr = open (filename, for output as #2) if openerr then print "Can't open that file for output.": print "> "; else print "Extracting and saving..." print #1, "Y"; 'initiate dump print "|";spc(progress_size);"|"; locate ,2,1 dofiledump = 1 dumpcounter = 0 'counts bytes for indicator and skipping while dofiledump inbyte = 0 while loc(1) > 0 and dofiledump <> 0 get #1,0,inbyte if dumpcounter < 3 then dumpcounter = dumpcounter + 1 else dofiledump = 0 'only allow valid dump chars... if inbyte = 32 or inbyte = 58 then dofiledump = 1 if inbyte = 13 or inbyte = 10 then dofiledump = 1 if inbyte >= 48 and inbyte <= 57 then dofiledump = 1 if inbyte >= 65 and inbyte <= 70 then dofiledump = 1 if dofiledump then print #2, chr(inbyte); dumpcounter = dumpcounter + 1 if dumpcounter mod progress_mod = 0 then print "*"; else print #2, "END" end if end if 'skip first 3 chars check wend 'serial byte available check sleep 10 'give the OS a bit of time keychar = inkey if keychar = chr(27) then print print "Cancelling dump, displaying remaining output." dofiledump = 0 lastbyte = 0 end if wend 'dofiledump loop close #2 print if inbyte <> 0 and keychar <> chr(27) then print chr(inbyte); 'fix terminal output end if end if 'open write file error check else 'no dump filename entered print "> "; 'fake prompt end if case else '(try to) restore the display locate ,cursorpos,1 astr = space(menulen) print astr; locate ,cursorpos,1 end select end if 'menu check end if 'upload check else 'esc wasn't pressed if dofileupload = 0 then print #1, outchar; end if 'key check if dofileupload then 'doing a file upload if not eof(2) then get #2,,inbyte if inbyte <> 13 then 'ignore cr's in file if inbyte = 10 then inbyte = 13 'change lf's to cr print #1, chr(inbyte); end if else 'end of file close #2 dofileupload = 0 end if 'eof check end if 'file upload check sleep chardelay 'delay between each character sent wend 'main loop close #1 'close comm port else 'comm error... print "Can't open serial port. Press any key..." sleep end if kill tempbytesfile 'if it exists system