lpcterm.c 4.0 KB

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