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