| 1 | /****************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Project: Portable command line ISP for Philips LPC2000 family |
|---|
| 4 | and Analog Devices ADUC70xx |
|---|
| 5 | |
|---|
| 6 | Filename: lsp21isp.h |
|---|
| 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 | // #define INTEGRATED_IN_WIN_APP |
|---|
| 33 | |
|---|
| 34 | #if defined(_WIN32) && !defined(__CYGWIN__) |
|---|
| 35 | #define COMPILE_FOR_WINDOWS |
|---|
| 36 | #define COMPILED_FOR "Windows" |
|---|
| 37 | #elif defined(__CYGWIN__) |
|---|
| 38 | #define COMPILE_FOR_CYGWIN |
|---|
| 39 | #define COMPILED_FOR "Cygwin" |
|---|
| 40 | #elif defined(__arm__) || defined(__thumb__) |
|---|
| 41 | #define COMPILE_FOR_LPC21 |
|---|
| 42 | #define COMPILED_FOR "ARM" |
|---|
| 43 | #define printf iprintf |
|---|
| 44 | #elif defined(__APPLE__) |
|---|
| 45 | #define COMPILE_FOR_LINUX |
|---|
| 46 | #define COMPILED_FOR "Apple MacOS X" |
|---|
| 47 | #elif defined(__FreeBSD__) |
|---|
| 48 | #define COMPILE_FOR_LINUX |
|---|
| 49 | #define COMPILED_FOR "FreeBSD" |
|---|
| 50 | #else |
|---|
| 51 | #define COMPILE_FOR_LINUX |
|---|
| 52 | #define COMPILED_FOR "Linux" |
|---|
| 53 | #endif |
|---|
| 54 | |
|---|
| 55 | // The Required features can be enabled / disabled here |
|---|
| 56 | #define LPC_SUPPORT |
|---|
| 57 | |
|---|
| 58 | #ifndef COMPILE_FOR_LPC21 |
|---|
| 59 | #define AD_SUPPORT |
|---|
| 60 | #define TERMINAL_SUPPORT |
|---|
| 61 | #endif |
|---|
| 62 | |
|---|
| 63 | #if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 64 | #include <windows.h> |
|---|
| 65 | #include <io.h> |
|---|
| 66 | #endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 67 | |
|---|
| 68 | #if defined COMPILE_FOR_WINDOWS |
|---|
| 69 | #include <conio.h> |
|---|
| 70 | //#define TRACE(x) OutputDebugString(x) |
|---|
| 71 | #define TRACE(x) printf("%s",x) |
|---|
| 72 | #endif // defined COMPILE_FOR_WINDOWS |
|---|
| 73 | |
|---|
| 74 | #if defined COMPILE_FOR_CYGWIN |
|---|
| 75 | //#define TRACE(x) OutputDebugString(x) |
|---|
| 76 | #define TRACE(x) printf("%s",x) |
|---|
| 77 | #endif // defined COMPILE_FOR_WINDOWS |
|---|
| 78 | |
|---|
| 79 | #if defined COMPILE_FOR_LINUX |
|---|
| 80 | #include <sys/types.h> |
|---|
| 81 | #include <sys/stat.h> |
|---|
| 82 | #include <stdlib.h> |
|---|
| 83 | #include <string.h> |
|---|
| 84 | #include <strings.h> |
|---|
| 85 | #include <sys/ioctl.h> |
|---|
| 86 | extern void Sleep(unsigned long MilliSeconds); |
|---|
| 87 | #define TRACE(x) printf("%s",x) |
|---|
| 88 | #endif // defined COMPILE_FOR_LINUX |
|---|
| 89 | |
|---|
| 90 | #if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN |
|---|
| 91 | #include <termios.h> |
|---|
| 92 | #include <unistd.h> // for read and return value of lseek |
|---|
| 93 | #include <sys/time.h> // for select_time |
|---|
| 94 | extern int kbhit(void); |
|---|
| 95 | extern int getch(void); |
|---|
| 96 | extern struct termios keyboard_origtty; |
|---|
| 97 | #endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN |
|---|
| 98 | |
|---|
| 99 | #include <ctype.h> // isdigit() |
|---|
| 100 | #include <stdio.h> // stdout |
|---|
| 101 | #include <stdarg.h> |
|---|
| 102 | #include <time.h> |
|---|
| 103 | #if defined (COMPILE_FOR_LINUX) |
|---|
| 104 | #include <sys/errno.h> |
|---|
| 105 | #endif |
|---|
| 106 | |
|---|
| 107 | #if defined COMPILE_FOR_LPC21 |
|---|
| 108 | #include <stdlib.h> |
|---|
| 109 | #include <string.h> |
|---|
| 110 | //#include <lpc_ioctl.h> // if using libc serial port communication |
|---|
| 111 | #else |
|---|
| 112 | #include <fcntl.h> |
|---|
| 113 | #endif |
|---|
| 114 | |
|---|
| 115 | typedef enum |
|---|
| 116 | { |
|---|
| 117 | PHILIPS_ARM, |
|---|
| 118 | ANALOG_DEVICES_ARM |
|---|
| 119 | } TARGET; |
|---|
| 120 | |
|---|
| 121 | typedef enum |
|---|
| 122 | { |
|---|
| 123 | PROGRAM_MODE, |
|---|
| 124 | RUN_MODE |
|---|
| 125 | } TARGET_MODE; |
|---|
| 126 | |
|---|
| 127 | typedef enum |
|---|
| 128 | { |
|---|
| 129 | FORMAT_BINARY, |
|---|
| 130 | FORMAT_HEX |
|---|
| 131 | } FILE_FORMAT_TYPE; |
|---|
| 132 | |
|---|
| 133 | typedef unsigned char BINARY; // Data type used for microcontroller |
|---|
| 134 | |
|---|
| 135 | typedef struct |
|---|
| 136 | { |
|---|
| 137 | #if !defined COMPILE_FOR_LPC21 |
|---|
| 138 | TARGET micro; // The type of micro that will be programmed. |
|---|
| 139 | FILE_FORMAT_TYPE FileFormat; |
|---|
| 140 | unsigned char ProgramChip; // Normally set |
|---|
| 141 | |
|---|
| 142 | int debug_level; |
|---|
| 143 | unsigned char ControlLines; |
|---|
| 144 | unsigned char ControlLinesSwapped; |
|---|
| 145 | unsigned char ControlLinesInverted; |
|---|
| 146 | unsigned char LogFile; |
|---|
| 147 | char *input_file; // The name of the file to get input from. |
|---|
| 148 | char *serial_port; // Name of the serial port to use to |
|---|
| 149 | // communicate with the microcontroller. |
|---|
| 150 | // Read from the command line. |
|---|
| 151 | #endif // !defined COMPILE_FOR_LPC21 |
|---|
| 152 | |
|---|
| 153 | unsigned char TerminalOnly; // Declared here for lazyness saves ifdef's |
|---|
| 154 | #ifdef TERMINAL_SUPPORT |
|---|
| 155 | unsigned char TerminalAfterUpload; |
|---|
| 156 | unsigned char LocalEcho; |
|---|
| 157 | #endif |
|---|
| 158 | |
|---|
| 159 | unsigned char HalfDuplex; // Only used for LPC Programming |
|---|
| 160 | unsigned char DetectOnly; |
|---|
| 161 | unsigned char WipeDevice; |
|---|
| 162 | unsigned char Verify; |
|---|
| 163 | int DetectedDevice; /* index in LPCtypes[] array */ |
|---|
| 164 | char *baud_rate; /**< Baud rate to use on the serial |
|---|
| 165 | * port communicating with the |
|---|
| 166 | * microcontroller. Read from the |
|---|
| 167 | * command line. */ |
|---|
| 168 | |
|---|
| 169 | char StringOscillator[6]; /**< Holds representation of oscillator |
|---|
| 170 | * speed from the command line. */ |
|---|
| 171 | |
|---|
| 172 | BINARY *FileContent; |
|---|
| 173 | BINARY *BinaryContent; /**< Binary image of the */ |
|---|
| 174 | /* microcontroller's memory. */ |
|---|
| 175 | unsigned long BinaryLength; |
|---|
| 176 | unsigned long BinaryOffset; |
|---|
| 177 | unsigned long StartAddress; |
|---|
| 178 | unsigned long BinaryMemSize; |
|---|
| 179 | |
|---|
| 180 | #if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 181 | HANDLE hCom; |
|---|
| 182 | #endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 183 | |
|---|
| 184 | #if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21 |
|---|
| 185 | int fdCom; |
|---|
| 186 | #endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21 |
|---|
| 187 | |
|---|
| 188 | #if defined COMPILE_FOR_LINUX |
|---|
| 189 | struct termios oldtio, newtio; |
|---|
| 190 | #endif // defined COMPILE_FOR_LINUX |
|---|
| 191 | |
|---|
| 192 | #ifdef INTEGRATED_IN_WIN_APP |
|---|
| 193 | unsigned char NoSync; |
|---|
| 194 | #endif |
|---|
| 195 | |
|---|
| 196 | unsigned serial_timeout_count; /**< Local used to track timeouts on serial port read. */ |
|---|
| 197 | |
|---|
| 198 | } ISP_ENVIRONMENT; |
|---|
| 199 | |
|---|
| 200 | #if defined COMPILE_FOR_LPC21 |
|---|
| 201 | |
|---|
| 202 | #define DebugPrintf(in, ...) |
|---|
| 203 | |
|---|
| 204 | #else |
|---|
| 205 | extern int debug_level; |
|---|
| 206 | |
|---|
| 207 | #if defined INTEGRATED_IN_WIN_APP |
|---|
| 208 | |
|---|
| 209 | #define DebugPrintf AppDebugPrintf |
|---|
| 210 | void AppDebugPrintf(int level, const char *fmt, ...); |
|---|
| 211 | |
|---|
| 212 | #define exit(val) AppException(val) |
|---|
| 213 | void AppException(int exception_level); |
|---|
| 214 | |
|---|
| 215 | int AppDoProgram(int argc, char *argv[]); |
|---|
| 216 | |
|---|
| 217 | #define Exclude_kbhit 1 |
|---|
| 218 | int AppSyncing(int trials); |
|---|
| 219 | void AppWritten(int size); |
|---|
| 220 | |
|---|
| 221 | #else |
|---|
| 222 | void DebugPrintf(int level, const char *fmt, ...); |
|---|
| 223 | //#define DebugPrintf(level, ...) if (level <= debug_level) { TRACE( __VA_ARGS__ ); } |
|---|
| 224 | #endif |
|---|
| 225 | |
|---|
| 226 | void ClearSerialPortBuffers(ISP_ENVIRONMENT *IspEnvironment); |
|---|
| 227 | static void ControlModemLines(ISP_ENVIRONMENT *IspEnvironment, unsigned char DTR, unsigned char RTS); |
|---|
| 228 | static unsigned char Ascii2Hex(unsigned char c); |
|---|
| 229 | |
|---|
| 230 | #endif |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | #if defined COMPILE_FOR_LINUX |
|---|
| 234 | #define stricmp strcasecmp |
|---|
| 235 | #endif // defined COMPILE_FOR_LINUX |
|---|
| 236 | |
|---|
| 237 | #ifndef O_BINARY |
|---|
| 238 | #define O_BINARY 0 |
|---|
| 239 | #endif // O_BINARY |
|---|
| 240 | |
|---|
| 241 | #ifndef DWORD |
|---|
| 242 | #define DWORD unsigned long |
|---|
| 243 | #endif // DWORD |
|---|
| 244 | |
|---|
| 245 | /* |
|---|
| 246 | debug levels |
|---|
| 247 | 0 - very quiet - Nothing gets printed at this level |
|---|
| 248 | 1 - quiet - Only error messages should be printed |
|---|
| 249 | 2 - indicate progress - Add progress messages |
|---|
| 250 | 3 - first level debug - Major level tracing |
|---|
| 251 | 4 - second level debug - Add detailed debugging |
|---|
| 252 | 5 - log comm's - log serial I/O |
|---|
| 253 | */ |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | void ReceiveComPort(ISP_ENVIRONMENT *IspEnvironment, |
|---|
| 257 | const char *Ans, unsigned long MaxSize, |
|---|
| 258 | unsigned long *RealSize, unsigned long WantedNr0x0A, |
|---|
| 259 | unsigned timeOutMilliseconds); |
|---|
| 260 | void PrepareKeyboardTtySettings(void); |
|---|
| 261 | void ResetKeyboardTtySettings(void); |
|---|
| 262 | void ResetTarget(ISP_ENVIRONMENT *IspEnvironment, TARGET_MODE mode); |
|---|
| 263 | |
|---|
| 264 | void DumpString(int level, const void *s, size_t size, const char *prefix_string); |
|---|
| 265 | void SendComPort(ISP_ENVIRONMENT *IspEnvironment, const char *s); |
|---|
| 266 | void SendComPortBlock(ISP_ENVIRONMENT *IspEnvironment, const void *s, size_t n); |
|---|
| 267 | int ReceiveComPortBlockComplete(ISP_ENVIRONMENT *IspEnvironment, void *block, size_t size, unsigned timeout); |
|---|
| 268 | void ClearSerialPortBuffers(ISP_ENVIRONMENT *IspEnvironment); |
|---|
| 269 | |
|---|
| 270 | #ifdef COMPILE_FOR_WINDOWS |
|---|
| 271 | static void SerialTimeoutSet(ISP_ENVIRONMENT *IspEnvironment, unsigned timeout_milliseconds); |
|---|
| 272 | static int SerialTimeoutCheck(ISP_ENVIRONMENT *IspEnvironment); |
|---|
| 273 | #endif // COMPILE_FOR_WINDOWS |
|---|
| 274 | |
|---|
| 275 | static void LoadFile(ISP_ENVIRONMENT *IspEnvironment); |
|---|
| 276 | |
|---|
| 277 | int lpctest(char* FileName); |
|---|