1 /******************************************************************************
\r
3 Project: Portable command line ISP for NXP LPC1000 / LPC2000 family
\r
4 and Analog Devices ADUC70xx
\r
8 Compiler: Microsoft VC 6/7, Microsoft VS2008, Microsoft VS2010,
\r
9 GCC Cygwin, GCC Linux, GCC ARM ELF
\r
11 Author: Martin Maurer (Martin.Maurer@clibb.de)
\r
13 Copyright: (c) Martin Maurer 2003-2011, All rights reserved
\r
14 Portions Copyright (c) by Aeolus Development 2004 http://www.aeolusdevelopment.com
\r
16 This file is part of lpc21isp.
\r
18 lpc21isp is free software: you can redistribute it and/or modify
\r
19 it under the terms of the GNU Lesser General Public License as published by
\r
20 the Free Software Foundation, either version 3 of the License, or
\r
23 lpc21isp is distributed in the hope that it will be useful,
\r
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
26 GNU Lesser General Public License for more details.
\r
28 You should have received a copy of the GNU Lesser General Public License
\r
29 and GNU General Public License along with lpc21isp.
\r
30 If not, see <http://www.gnu.org/licenses/>.
\r
33 // #define INTEGRATED_IN_WIN_APP
\r
35 #if defined(_WIN32) && !defined(__CYGWIN__)
\r
36 #define COMPILE_FOR_WINDOWS
\r
37 #define COMPILED_FOR "Windows"
\r
38 #elif defined(__CYGWIN__)
\r
39 #define COMPILE_FOR_CYGWIN
\r
40 #define COMPILED_FOR "Cygwin"
\r
41 #elif (defined(__arm__) || defined(__thumb__)) && !defined(__linux__)
\r
42 #define COMPILE_FOR_LPC21
\r
43 #define COMPILED_FOR "ARM"
\r
44 #define printf iprintf
\r
45 #elif defined(__APPLE__)
\r
46 #define COMPILE_FOR_LINUX
\r
47 #define COMPILED_FOR "Apple MacOS X"
\r
48 #elif defined(__FreeBSD__)
\r
49 #define COMPILE_FOR_LINUX
\r
50 #define COMPILED_FOR "FreeBSD"
\r
51 #elif defined(__OpenBSD__)
\r
52 #define COMPILE_FOR_LINUX
\r
53 #define COMPILED_FOR "OpenBSD"
\r
55 #define COMPILE_FOR_LINUX
\r
56 #define COMPILED_FOR "Linux"
\r
59 // The Required features can be enabled / disabled here
\r
62 #ifndef COMPILE_FOR_LPC21
\r
64 #define TERMINAL_SUPPORT
\r
67 #if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
\r
68 #include <windows.h>
\r
70 #endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
\r
72 #if defined COMPILE_FOR_WINDOWS
\r
74 //#define TRACE(x) OutputDebugString(x)
\r
75 #define TRACE(x) printf("%s",x)
\r
76 #endif // defined COMPILE_FOR_WINDOWS
\r
78 #if defined COMPILE_FOR_CYGWIN
\r
79 //#define TRACE(x) OutputDebugString(x)
\r
80 #define TRACE(x) printf("%s",x)
\r
81 #endif // defined COMPILE_FOR_WINDOWS
\r
83 #if defined COMPILE_FOR_LINUX
\r
84 #include <sys/types.h>
\r
85 #include <sys/stat.h>
\r
88 #include <strings.h>
\r
89 #include <sys/ioctl.h>
\r
90 extern void Sleep(unsigned long MilliSeconds);
\r
91 #define TRACE(x) printf("%s",x)
\r
92 #endif // defined COMPILE_FOR_LINUX
\r
94 #if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN
\r
95 #include <termios.h>
\r
96 #include <unistd.h> // for read and return value of lseek
\r
97 #include <sys/time.h> // for select_time
\r
98 extern int kbhit(void);
\r
99 extern int getch(void);
\r
100 extern struct termios keyboard_origtty;
\r
101 #endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN
\r
103 #include <ctype.h> // isdigit()
\r
104 #include <stdio.h> // stdout
\r
105 #include <stdarg.h>
\r
107 #if defined (COMPILE_FOR_LINUX)
\r
108 #if defined(__OpenBSD__)
\r
111 #include <sys/errno.h>
\r
115 #if defined COMPILE_FOR_LPC21
\r
116 #include <stdlib.h>
\r
117 #include <string.h>
\r
118 //#include <lpc_ioctl.h> // if using libc serial port communication
\r
139 } FILE_FORMAT_TYPE;
\r
141 typedef unsigned char BINARY; // Data type used for microcontroller
\r
143 /** Used to create list of files to read in. */
\r
144 typedef struct file_list FILE_LIST;
\r
146 #define ERR_RECORD_TYPE_LOADFILE 55 /**< File record type not yet implemented. */
\r
147 #define ERR_ALLOC_FILE_LIST 60 /**< Error allocation file list. */
\r
148 #define ERR_FILE_OPEN_HEX 61 /**< Couldn't open hex file. */
\r
149 #define ERR_FILE_SIZE_HEX 62 /**< Unexpected hex file size. */
\r
150 #define ERR_FILE_ALLOC_HEX 63 /**< Couldn't allocate enough memory for hex file. */
\r
151 #define ERR_MEMORY_RANGE 69 /**< Out of memory range. */
\r
153 /** Structure used to build list of input files. */
\r
156 const char *name; /**< The name of the input file. */
\r
157 FILE_LIST *prev; /**< The previous file name in the list.*/
\r
158 char hex_flag; /**< True if the input file is hex. */
\r
163 #if !defined COMPILE_FOR_LPC21
\r
164 TARGET micro; // The type of micro that will be programmed.
\r
165 FILE_FORMAT_TYPE FileFormat;
\r
166 unsigned char ProgramChip; // Normally set
\r
168 unsigned char ControlLines;
\r
169 unsigned char ControlLinesSwapped;
\r
170 unsigned char ControlLinesInverted;
\r
171 unsigned char LogFile;
\r
172 FILE_LIST *f_list; // List of files to read in.
\r
173 int nQuestionMarks; // how many times to try to synchronise
\r
176 char *serial_port; // Name of the serial port to use to
\r
177 // communicate with the microcontroller.
\r
178 // Read from the command line.
\r
179 #endif // !defined COMPILE_FOR_LPC21
\r
181 unsigned char TerminalOnly; // Declared here for lazyness saves ifdef's
\r
182 #ifdef TERMINAL_SUPPORT
\r
183 unsigned char TerminalAfterUpload;
\r
184 unsigned char LocalEcho;
\r
187 unsigned char HalfDuplex; // Only used for LPC Programming
\r
188 unsigned char DetectOnly;
\r
189 unsigned char WipeDevice;
\r
190 unsigned char Verify;
\r
191 int DetectedDevice; /* index in LPCtypes[] array */
\r
192 char *baud_rate; /**< Baud rate to use on the serial
\r
193 * port communicating with the
\r
194 * microcontroller. Read from the
\r
197 char StringOscillator[6]; /**< Holds representation of oscillator
\r
198 * speed from the command line. */
\r
200 BINARY *FileContent;
\r
201 BINARY *BinaryContent; /**< Binary image of the */
\r
202 /* microcontroller's memory. */
\r
203 unsigned long BinaryLength;
\r
204 unsigned long BinaryOffset;
\r
205 unsigned long StartAddress;
\r
206 unsigned long BinaryMemSize;
\r
208 #if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
\r
210 #endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
\r
212 #if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21
\r
214 #endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21
\r
216 #if defined COMPILE_FOR_LINUX
\r
217 struct termios oldtio, newtio;
\r
218 #endif // defined COMPILE_FOR_LINUX
\r
220 #ifdef INTEGRATED_IN_WIN_APP
\r
221 unsigned char NoSync;
\r
224 #if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
\r
225 unsigned long serial_timeout_count; /**< Local used to track timeouts on serial port read. */
\r
227 unsigned serial_timeout_count; /**< Local used to track timeouts on serial port read. */
\r
232 #if defined COMPILE_FOR_LPC21
\r
234 #define DebugPrintf(in, ...)
\r
237 extern int debug_level;
\r
239 #if defined INTEGRATED_IN_WIN_APP
\r
241 #define DebugPrintf AppDebugPrintf
\r
242 void AppDebugPrintf(int level, const char *fmt, ...);
\r
244 #define exit(val) AppException(val)
\r
245 void AppException(int exception_level);
\r
247 int AppDoProgram(int argc, char *argv[]);
\r
249 #define Exclude_kbhit 1
\r
250 int AppSyncing(int trials);
\r
251 void AppWritten(int size);
\r
254 void DebugPrintf(int level, const char *fmt, ...);
\r
255 //#define DebugPrintf(level, ...) if (level <= debug_level) { TRACE( __VA_ARGS__ ); }
\r
258 void ClearSerialPortBuffers(ISP_ENVIRONMENT *IspEnvironment);
\r
259 void ControlXonXoffSerialPort(ISP_ENVIRONMENT *IspEnvironment, unsigned char XonXoff);
\r
264 #if defined COMPILE_FOR_LINUX
\r
265 #define stricmp strcasecmp
\r
266 #define strnicmp strncasecmp
\r
267 #endif // defined COMPILE_FOR_LINUX
\r
274 #define DWORD unsigned long
\r
279 0 - very quiet - Nothing gets printed at this level
\r
280 1 - quiet - Only error messages should be printed
\r
281 2 - indicate progress - Add progress messages
\r
282 3 - first level debug - Major level tracing
\r
283 4 - second level debug - Add detailed debugging
\r
284 5 - log comm's - log serial I/O
\r
288 void ReceiveComPort(ISP_ENVIRONMENT *IspEnvironment,
\r
289 const char *Ans, unsigned long MaxSize,
\r
290 unsigned long *RealSize, unsigned long WantedNr0x0A,
\r
291 unsigned timeOutMilliseconds);
\r
292 void PrepareKeyboardTtySettings(void);
\r
293 void ResetKeyboardTtySettings(void);
\r
294 void ResetTarget(ISP_ENVIRONMENT *IspEnvironment, TARGET_MODE mode);
\r
296 void DumpString(int level, const void *s, size_t size, const char *prefix_string);
\r
297 void SendComPort(ISP_ENVIRONMENT *IspEnvironment, const char *s);
\r
298 void SendComPortBlock(ISP_ENVIRONMENT *IspEnvironment, const void *s, size_t n);
\r
299 int ReceiveComPortBlockComplete(ISP_ENVIRONMENT *IspEnvironment, void *block, size_t size, unsigned timeout);
\r
300 void ClearSerialPortBuffers(ISP_ENVIRONMENT *IspEnvironment);
\r
301 void ControlXonXoffSerialPort(ISP_ENVIRONMENT *IspEnvironment, unsigned char XonXoff);
\r