// // Module: main.c // Version: V1.0 // Created: 7/1/97 // Programmer: Ralph Stickley // Environment: Borland 3.1 BGI graphics // // PIC_HEX.exe // // Program to serially poll Micro-controller and display current // values for variables located in the map file. // Micro-controller must respond with the data value of a given an // address. // // Limitations: // Currently only supports fixed baud rate (9600) COM1-4 // Map file must match MPLINK V1.2 format. // Variables shown as 1 or two bytes only. // Addressing ranges support PIC 16C74. // All numbers displayed in ASCII Hex // // Invocation: // PIC_HEX filename.map [1|2] // // Environment: Borland C 3.1, MS-DOS compatable // Created by: Ralph Stickley // Created: 2/26/98 // #include "stdio.h" // sscanf(), printf(), fopen(), fclose(), fgets() #include "stdlib.h" // strtol(), min(), max() #include "string.h" // strcpy(), strlen(), strstr() functions #include "conio.h" // kbhit() function #include "viewer.h" #include "dos.h" // delay() function #define MAIN_TITLE \ " PIC HEX - Symbolic Serial Monitor V1.0 " #define MAIN_MENU \ " ESC->QUIT PG_UP/PG_DN->SymbolPage ARROW->HEX Bank S->SORT ENTER->Stop " #define SHOW_TEXT() Display_Frame(1,1,79,24); \ Display_String( MAIN_MENU, 25, 3 ); \ Display_String( MAIN_TITLE, 1, 40 - (strlen( MAIN_TITLE ) / 2) ); #define MAIN_HELP \ " KEY: ACTION: \r" \ " Page Up - display next page of symbols \r" \ " Page Down - display previous page of symbols \r" \ " Arrow keys - next hex dump page (or remove) \r" \ " S - toggle sorted by name or address \r" \ " ENTER - toggles update on or off \r" \ " ESC - Quit the program \r" \ " F1 or H - This help screen \r" // // Symbol Table - structured array containing all data parsed from the map // Symbol Variables[ MAX_NUM_SYMBOLS ]; int SymbolNext; int OnChipData[ 256 ]; // // Command line arguments // char MapFilename[ MAX_FILENAME_LENGTH ]; int Port; // // load_arguments - loads command line arguments - fixed position // filename.map Port# // where filename.map is an MPLINK map file // Port# is the comm port to use 1 or 2 // // Parameters: argc, argv - command line argument variables // // returns: TRUE - if valid map filename is found (defaults to comm1) // FALSE otherwise // int load_arguments( int argc, char **argv ) { int argPort; strcpy( MapFilename, argv[1] ); if ( strlen ( MapFilename ) < 1 ) return( 0 ); Port = 1; if ( argc > 2 ) { argPort = atoi( argv[ 2 ] ); if (( 1 <= argPort ) && ( argPort <= 4 ) ) Port = argPort; } return( 1 ); } int comms_init( void ) { InitUSART( Port ); return( 1 ); } int NextPollAddr, EndPollAddr, StartPollAddr; void comms_start( int varStartIndex, int varEndIndex, int hexBank ) { int minAddr, maxAddr, i; // only poll data currently being displayed minAddr = 0x7F; maxAddr = 0; for ( i = varStartIndex; i < varEndIndex; i++ ) { minAddr = min( Variables[ i ].address, minAddr ); maxAddr = max( Variables[ i ].address, maxAddr ); } StartPollAddr = minAddr; EndPollAddr = maxAddr; if ( hexBank == 0 ) { if ( StartPollAddr > HEX_DUMP_BK0_START ) StartPollAddr = HEX_DUMP_BK0_START; // go back and get bk data if ( EndPollAddr < HEX_DUMP_BK0_END ) EndPollAddr= HEX_DUMP_BK0_END; // go up to end of bank } else if ( hexBank == 1 ) { if ( StartPollAddr > HEX_DUMP_BK1_START ) StartPollAddr = HEX_DUMP_BK1_START; // go back and get bk data if ( EndPollAddr < HEX_DUMP_BK1_END ) EndPollAddr= HEX_DUMP_BK1_END; // go up to end of bank } // else hex bank is not displayed, display variables only NextPollAddr = StartPollAddr; RxFlush(); // clear receive buffer to synchronize transfer } int comms_data( void ) { int dataIn; for ( NextPollAddr = StartPollAddr; NextPollAddr < EndPollAddr; NextPollAddr++ ) { // send request delay( 2 ); TxData( NextPollAddr ); delay( 2 ); // get response - returns on character in or keyboard hit dataIn = RxData(); if ( dataIn == -1 ) // key pressed return( 0 ); // store the data OnChipData[ NextPollAddr ] = dataIn; } return( 1 ); } void do_viewer( void ) { int varStart = 0; int varEnd = SymbolNext + 1; int chIn; int hexBank = 0; int sorted = 1; // already sorted by address // wait for exit key do { Display_Init(); // refresh screen SHOW_TEXT(); Show_Names( &varStart, &varEnd ); comms_start( varStart, varEnd, hexBank ); do { // poll and update until any key pressed if ( comms_data( ) != 0 ) { Show_Data( varStart, varEnd, hexBank ); } } while ( kbhit() == 0 ); // do keyboard commands chIn = Display_Get_Key("SsHh"); if (( chIn == 'S' ) || ( chIn == 's' ) ) // reload map sorted SymbolNext = MapLoadFile( MapFilename, sorted++ ); if ( chIn == ENTER ) { do { chIn = Display_Get_Key(""); if ( chIn == ESC ) break; } while ( chIn != ENTER ); } if ( ( chIn == 'H' ) || ( chIn == 'h' ) || ( chIn == KEY_F1 )) { Display_Message( MAIN_HELP ); } if ( chIn == PAGE_DN ) { varStart = varStart + SHOW_NAMES_PER_PAGE; } if ( chIn == PAGE_UP ) { varStart = varStart - SHOW_NAMES_PER_PAGE; } if (( chIn == LEFT_ARROW ) || ( chIn == UP_ARROW ) || ( chIn == RIGHT_ARROW ) || ( chIn == DOWN_ARROW ) ) hexBank = ( hexBank + 1) % 3; // bank 0, 1 or off if (( varStart > SymbolNext ) || ( varStart < 0 ) ) varStart = 0; varEnd = SymbolNext + 1; } while ( chIn != ESC ); Display_Reset(); } void main( int argc, char **argv ) { printf("%s\n", MAIN_TITLE ); if ( ( argc <= 1 ) || ( load_arguments( argc, argv ) == 0 )) { printf( " Usage: viewer [1|2|3|4]\n"); printf( " filename.map - MPLINK map file\n" ); printf( " 1|2|3|4 - use comm port given\n" ); } else { SymbolNext = MapLoadFile( MapFilename, 0 ); if ( SymbolNext <= 0 ) { printf ("\nMap file Load Error: %s", MapFilename ); } else { if ( comms_init() == 0 ) { printf( "\nPort Open Error: Comm %d", Port ); } else { do_viewer(); } } } }