diff --git a/loadp2/Makefile b/loadp2/Makefile index ac6eb2e..1368d22 100644 --- a/loadp2/Makefile +++ b/loadp2/Makefile @@ -35,7 +35,7 @@ endif default: $(BUILD)/loadp2$(EXT) -$(BUILD)/loadp2$(EXT): $(BUILD) loadp2.c +$(BUILD)/loadp2$(EXT): $(BUILD) loadp2.c osint_linux.c osint_mingw.c $(CC) -Wall -O -o $@ loadp2.c $(OSFILE) $(BUILD): diff --git a/loadp2/loadp2.c b/loadp2/loadp2.c index 160ce00..4b696e4 100644 --- a/loadp2/loadp2.c +++ b/loadp2/loadp2.c @@ -33,8 +33,8 @@ #define LOAD_FPGA 1 #define LOAD_SINGLE 2 -#define LOADER_BAUD 2000000 - +#define LOADER_BAUD loader_baud +static int loader_baud = 2000000; static int clock_mode = -1; static int user_baud = -1; static int clock_freq = 80000000; @@ -58,6 +58,23 @@ char *MainLoader1 = static char buffer[1024]; static char binbuffer[101]; // Added for Prop2-v28 static int verbose = 0; +static int waitAtExit = 0; + +/* promptexit: print a prompt if waitAtExit is set, then exit */ +void +promptexit(int r) +{ + int c; + if (waitAtExit) { + fflush(stderr); + printf("Press enter to continue...\n"); + fflush(stdout); + do { + c = getchar(); + } while (c > 0 && c != '\n' && c != '\r'); + } + exit(r); +} /* Usage - display a usage message and exit */ static void Usage(void) @@ -73,12 +90,13 @@ usage: loadp2\n\ [ -t ] enter terminal mode after running the program\n\ [ -T ] enter PST-compatible terminal mode\n\ [ -v ] enable verbose mode\n\ + [ -k ] wait for user input before exit\n\ [ -? ] display a usage message and exit\n\ [ -CHIP ] set load mode for CHIP\n\ [ -FPGA ] set load mode for FPGA\n\ [ -SINGLE ] set load mode for single stage\n\ file file to load\n", user_baud, clock_freq, clock_mode); - exit(1); + promptexit(1); } void txval(int val) @@ -250,6 +268,7 @@ int main(int argc, char **argv) user_baud = atoi(argv[i]); else Usage(); + loader_baud = user_baud; } else if (argv[i][1] == 'X') { @@ -269,6 +288,10 @@ int main(int argc, char **argv) else Usage(); } + else if (argv[i][1] == 'k') + { + waitAtExit = 1; + } else if (argv[i][1] == 'm') { if(argv[i][2]) @@ -318,13 +341,13 @@ int main(int argc, char **argv) if (!findp2(PORT_PREFIX, LOADER_BAUD)) { printf("Could not find a P2\n"); - exit(1); + promptexit(1); } } else if (1 != serial_init(port, LOADER_BAUD)) { printf("Could not open port %s\n", argv[1]); - exit(1); + promptexit(1); } if (load_mode == LOAD_CHIP) @@ -361,16 +384,21 @@ int main(int argc, char **argv) if (loadfile(fname, address)) { serial_done(); - exit(1); + promptexit(1); } if (runterm) { - serial_baud(user_baud); + if (user_baud == -1) { + user_baud = LOADER_BAUD; + } + if (user_baud != LOADER_BAUD) { + serial_baud(user_baud); + } printf("[ Entering terminal mode. Press ESC to exit. ]\n"); terminal_mode(1,pstmode); } serial_done(); - return 0; + promptexit(0); } diff --git a/loadp2/osint_linux.c b/loadp2/osint_linux.c index 372a7a7..17e8941 100644 --- a/loadp2/osint_linux.c +++ b/loadp2/osint_linux.c @@ -48,6 +48,7 @@ typedef int HANDLE; static HANDLE hSerial = -1; static struct termios old_sparm; +extern void promptexit(int r); /* normally we use DTR for reset but setting this variable to non-zero will use RTS instead */ static int use_rts_for_reset = 0; @@ -90,7 +91,7 @@ int serial_find(const char* prefix, int (*check)(const char* port, void* data), static void sigint_handler(int signum) { serial_done(); - exit(1); + promptexit(1); } /** @@ -167,7 +168,9 @@ int serial_baud(unsigned long baud) { struct termios sparm; int tbaud = 0; - +#ifndef MACOS + int cmd = TIOCM_DTR; +#endif switch(baud) { case 0: // default tbaud = B115200; @@ -218,8 +221,8 @@ int serial_baud(unsigned long baud) tbaud = B9600; break; default: - printf("Unsupported baudrate. Use "); - tbaud = baud; break; + printf("Unsupported baudrate %lu. Use ", baud); + tbaud = baud; #ifdef B921600 printf("921600, "); #endif @@ -237,7 +240,7 @@ int serial_baud(unsigned long baud) #endif printf("115200, 57600, 38400, 19200, or 9600\n"); serial_done(); - exit(2); + promptexit(2); break; } @@ -255,7 +258,10 @@ int serial_baud(unsigned long baud) /* set the options */ chk("tcflush", tcflush(hSerial, TCIFLUSH)); chk("tcsetattr", tcsetattr(hSerial, TCSANOW, &sparm)); - +#ifndef MACOSX + // immediately clear DTR to prevent reset + ioctl(hSerial, TIOCMBIC, &cmd); +#endif return 1; } @@ -266,6 +272,11 @@ void serial_done(void) { if (hSerial != -1) { tcflush(hSerial, TCIOFLUSH); +#ifndef MACOSX + // "hang up" to allow program to continue running + cfsetispeed(&old_sparm, B0); + cfsetospeed(&old_sparm, B0); +#endif tcsetattr(hSerial, TCSANOW, &old_sparm); ioctl(hSerial, TIOCNXCL); close(hSerial); @@ -465,7 +476,7 @@ done: if (sawexit_valid) { - exit(exitcode); + promptexit(exitcode); } }