| 1 | /****************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Project: Portable command line ISP for Philips LPC2000 family |
|---|
| 4 | and Analog Devices ADUC70xx |
|---|
| 5 | |
|---|
| 6 | Filename: lpcterm.c |
|---|
| 7 | |
|---|
| 8 | Compiler: Microsoft VC 6/7, GCC Cygwin, GCC Linux, GCC ARM ELF |
|---|
| 9 | |
|---|
| 10 | Author: Martin Maurer (Martin.Maurer@clibb.de) |
|---|
| 11 | |
|---|
| 12 | Copyright: (c) Martin Maurer 2003-2008, All rights reserved |
|---|
| 13 | Portions Copyright (c) by Aeolus Development 2004 http://www.aeolusdevelopment.com |
|---|
| 14 | |
|---|
| 15 | This file is part of lpc21isp. |
|---|
| 16 | |
|---|
| 17 | lpc21isp is free software: you can redistribute it and/or modify |
|---|
| 18 | it under the terms of the GNU Lesser General Public License as published by |
|---|
| 19 | the Free Software Foundation, either version 3 of the License, or |
|---|
| 20 | any later version. |
|---|
| 21 | |
|---|
| 22 | lpc21isp is distributed in the hope that it will be useful, |
|---|
| 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 25 | GNU Lesser General Public License for more details. |
|---|
| 26 | |
|---|
| 27 | You should have received a copy of the GNU Lesser General Public License |
|---|
| 28 | and GNU General Public License along with lpc21isp. |
|---|
| 29 | If not, see <http://www.gnu.org/licenses/>. |
|---|
| 30 | */ |
|---|
| 31 | |
|---|
| 32 | #if defined(_WIN32) |
|---|
| 33 | #if !defined __BORLANDC__ |
|---|
| 34 | #include "StdAfx.h" |
|---|
| 35 | #endif |
|---|
| 36 | #endif // defined(_WIN32) |
|---|
| 37 | #include "lpc21isp.h" |
|---|
| 38 | #include "lpcterm.h" |
|---|
| 39 | |
|---|
| 40 | #ifdef TERMINAL_SUPPORT |
|---|
| 41 | /***************************** Terminal *********************************/ |
|---|
| 42 | /** Acts as a simple dumb terminal. Press 'ESC' to exit. |
|---|
| 43 | */ |
|---|
| 44 | BOOL CheckTerminalParameters(ISP_ENVIRONMENT *IspEnvironment, char* pstr) |
|---|
| 45 | { |
|---|
| 46 | if (stricmp(pstr, "-localecho") == 0) |
|---|
| 47 | { |
|---|
| 48 | IspEnvironment->LocalEcho = 1; |
|---|
| 49 | DebugPrintf(3, "Local echo in terminal mode.\n"); |
|---|
| 50 | return TRUE; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | if (stricmp(pstr, "-term") == 0) |
|---|
| 54 | { |
|---|
| 55 | IspEnvironment->TerminalAfterUpload = 1; |
|---|
| 56 | DebugPrintf(3, "Invoke terminal after upload.\n"); |
|---|
| 57 | return TRUE; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | if (stricmp(pstr, "-termonly") == 0) |
|---|
| 61 | { |
|---|
| 62 | IspEnvironment->TerminalOnly = 1; |
|---|
| 63 | IspEnvironment->ProgramChip = 0; |
|---|
| 64 | DebugPrintf(3, "Only provide terminal.\n"); |
|---|
| 65 | return TRUE; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | return FALSE; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void Terminal(ISP_ENVIRONMENT *IspEnvironment) |
|---|
| 72 | { |
|---|
| 73 | if (IspEnvironment->TerminalAfterUpload || IspEnvironment->TerminalOnly) |
|---|
| 74 | { |
|---|
| 75 | int ch = 0; |
|---|
| 76 | char buffer[128]; |
|---|
| 77 | int fdlogfile = -1; |
|---|
| 78 | unsigned long realsize; |
|---|
| 79 | |
|---|
| 80 | // When logging is switched on, output terminal output to lpc21isp.log |
|---|
| 81 | if (IspEnvironment->LogFile) |
|---|
| 82 | { |
|---|
| 83 | fdlogfile = open("lpc21isp.log", O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0777); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | PrepareKeyboardTtySettings(); |
|---|
| 87 | |
|---|
| 88 | DebugPrintf(1, "Terminal started (press Escape to abort)\n\n"); |
|---|
| 89 | fflush(stdout); |
|---|
| 90 | |
|---|
| 91 | do |
|---|
| 92 | { |
|---|
| 93 | ReceiveComPort(IspEnvironment, buffer, sizeof(buffer) - 1, &realsize, 0,200); // Check for received characters |
|---|
| 94 | |
|---|
| 95 | if (realsize) |
|---|
| 96 | { |
|---|
| 97 | write(1, buffer, realsize); |
|---|
| 98 | fflush(stdout); |
|---|
| 99 | if (IspEnvironment->LogFile) // When logging is turned on, then copy output to logfile |
|---|
| 100 | { |
|---|
| 101 | write(fdlogfile, buffer, realsize); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | // check for keypress, and write any out the port. |
|---|
| 106 | if (kbhit()) |
|---|
| 107 | { |
|---|
| 108 | ch = getch(); |
|---|
| 109 | if (ch == 0x1b) |
|---|
| 110 | { |
|---|
| 111 | break; |
|---|
| 112 | } |
|---|
| 113 | buffer[0] = (unsigned char)ch; |
|---|
| 114 | buffer[1] = 0; |
|---|
| 115 | |
|---|
| 116 | if (IspEnvironment->LocalEcho) |
|---|
| 117 | { |
|---|
| 118 | write(1, buffer, 1); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | SendComPort(IspEnvironment, buffer); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | while (ch != 0x1b); |
|---|
| 125 | |
|---|
| 126 | DebugPrintf(1, "\n\nTerminal stopped\n\n"); |
|---|
| 127 | fflush(stdout); |
|---|
| 128 | |
|---|
| 129 | ResetKeyboardTtySettings(); |
|---|
| 130 | |
|---|
| 131 | if (IspEnvironment->LogFile) |
|---|
| 132 | { |
|---|
| 133 | close(fdlogfile); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | #endif |
|---|