]> rtime.felk.cvut.cz Git - sysless.git/blobdiff - arch/arm/mach-lpc21xx/tools/lpc21isp/lpc21isp.c
Added newer version (1.64) of lpc21isp
[sysless.git] / arch / arm / mach-lpc21xx / tools / lpc21isp / lpc21isp.c
index 154d98c4372c68e6686eda65eef2f7a8e6394c29..976836ebcf539c0cef5701a9766c7cf3e2d8568c 100644 (file)
-/******************************************************************************
-
-Project:           Portable command line ISP for Philips LPC2000 family
-                   and Analog Devices ADUC70xx
-
-Filename:          lpc21isp.c
-
-Compiler:          Microsoft VC 6/7, GCC Cygwin, GCC Linux
-
-Autor:             Martin Maurer (Martin.Maurer@clibb.de)
-
-Copyright:         (c) Martin Maurer 2003-2005, All rights reserved
-Portions Copyright (c) by Aeolus Development 2004 http://www.aeolusdevelopment.com
-
-Version:           1.27
-
-Change-History:
-
- 1.00  2004-01-08  Initial Version, tested for MSVC6/7 and GCC under Cygwin
- 1.01  2004-01-10  Porting to Linux (at least compiling must work)
- 1.02  2004-01-10  Implemented conversion intel hex format -> binary
- 1.03  2004-01-25  Preparation to upload to public website
- 1.04  2004-02-12  Merged in bugfixes by Soeren Gust
- 1.05  2004-03-14  Implement printing of error codes as text / strings
- 1.06  2004-03-09  Merged in bugfixes by Charles Manning:
-                   The '?' sychronisation does not reliably respond to the first '?'.
-                   I added some retries.
-                   The LPC2106 sometimes responds to the '?' by echoing them back.
-                   This sometimes causes an attempt to match "?Synchonized".
-                   Added code to strip off any leading '?'s.
-                   Timeouts were too long.
-                   Change from RTS/CTS to no flow control.
-                   Done because many/most people will use only 3-wire comms.
-                   Added some progress tracing.
- 1.07  2004-03-14  Implement handling of control lines for easier booting
- 1.08  2004-04-01  Bugfix for upload problem
- 1.09  2004-04-03  Redesign of upload routine
-                   Now always 180 byte blocks are uploaded, to prevent
-                   small junks in uuencoding
- 1.10  2004-04-03  Clear buffers before sending commands to LPC21xx,
-                   this prevents synchronizing errors when previously loaded
-                   program does a lot of output, so FIFO of PC runs full
- 1.11  2004-04-03  Small optimization for controlling reset line
-                   otherwise termonly starts LPC twice, free PC buffers
- 1.12  2004-04-04  Add switch to enable logging terminal output to lpc21isp.log
- 1.13  2004-05-19  Merged in improvement by Charles Manning:
-                   Instead of exiting the wrong hex file size is corrected
- 1.14  2004-07-07  Merged in improvement by Alex Holden:
-                   Remove little/big endian dependancy
- 1.15  2004-09-27  Temporary improvement by Cyril Holweck:
-                   Removed test (data echoed = data transmited) on the main
-                   data transfert, since this was the biggest failure
-                   reason and is covered by checksome anyway.
-                   Added COMPILE_FOR_LPC21, to have target dump it's own
-                   memory to stdout.
- 1.16  2004-10-09  Merged in bugfix / improvement by Sinelnikov Evgeny
-                   I found out that Linux and Windows serial port initialization
-                   are different with pinouts states. My board don't get
-                   reset signal at first cycle of DTR pinout moving.
-                   And I add this moving to initalization cycle.
- 1.17  2004-10-21  Changes by Cyril Holweck
-                   Divide main, take out the real programming function, that can
-                   also be used by a target to copy its own code to another.
- 1.18  2004-10-26  Changes by Cyril Holweck
-                   Added a "G 0 A\r\n" at end of programming to run code.
- 1.19  2004-11-03  Changes by Robert Adsett
-                   Add support for Analog Devices.
-                   Separate file load from programming.
-                   Change from a debug on/off flag to debug level
-                   Remove if(debug) tests and replace with DebugPrintf
-                   statements.
-                   Change serial I/O and timing so that the system
-                   dependancies are isolated to a few portability functions.
-                   Add support for binary serial I/O.
-                   Add doxygen support.
- 1.20  2004-11-07  Preparation for multiport booting (factory support)
- 1.21  2004-11-08  Bugfix from Robert Adsett
-                   BinaryLength was not initialized
- 1.22  2004-11-08  Changes from Cyril Holweck / Evgeny Sinelnikov
-                   Forgotten IspEnvironment-> and bugfixes if COMPILE_FOR_LINUX
-                   If COMPILE_FOR_LPC21, PhilipsDownload() 'acts as' main():
-                   - it should not be static and should return int.
-                   - no sub-function can use exit() but only return()
-                   Use 'char' instead of 'byte' ;)
- 1.23  2005-01-16  Build in automatic detection of LPC chiptype
-                   (needed for 256 KByte support)
- 1.24B 2005-06-02  Changes by Thiadmer Riemersma: completed support for other
-                   chip types (LPC213x series and others).
- 1.24C 2005-06-11  Changes by Thiadmer Riemersma: added the device ID codes for
-                   chip types LPC2131 and LPC2132.
- 1.25  2005-06-19  Martin Maurer: Setup more parameters in DCB,
-                   otherwise wrong code is downloaded (only Windows and Cygwin)
-                   when a previous program has changed these parameters
-                   Check exact string of "G 0 A\r\n0\r\n" instead of whole received buffer,
-                   to prevent checking of already received by program start
-                   (error on running program, but reports CMD_SUCCESS)
-                   Add ifdefs for all baudrates (needed only for high baudrate,
-                   which seem to be not available on Macs...)
- 1.26  2005-06-26  Martin Maurer:
-                   Correct check again: "G 0 A\r\n0\r\n" is cutted, because of reboot
-                   (error on running program, but reports CMD_SUCCESS)
- 1.27  2005-06-29  Martin Maurer:
-                   Add LPC chip ID's (thanks to Robert from Philips) for
-                   missing LPC213x and upcoming new LPC214x chips
-                   (currently untested, because i don't have access to these chips,
-                    please give me feedback !)
-
-******************************************************************************/
-
-#define VERSION_STR    "1.27"
-
-#if defined(_WIN32) && !defined(__CYGWIN__)
-    #define COMPILE_FOR_WINDOWS
-    #define COMPILED_FOR ("Windows")
-#elif defined(__CYGWIN__)
-    #define COMPILE_FOR_CYGWIN
-    #define COMPILED_FOR ("Cygwin")
-#elif defined(__arm__) || defined(__thumb__)
-    #define COMPILE_FOR_LPC21
-    #define COMPILED_FOR ("ARM")
-    #define printf iprintf
-#else
-    #define COMPILE_FOR_LINUX
-    #define COMPILED_FOR ("Linux")
-#endif
-
-#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-#include <windows.h>
-#include <io.h>
-#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-#if defined COMPILE_FOR_WINDOWS
-#include <conio.h>
-#endif // defined COMPILE_FOR_WINDOWS
-
-#if defined COMPILE_FOR_LINUX
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <stdlib.h>
-#include <string.h>
-#include <strings.h>
-#include <sys/ioctl.h>
-#endif // defined COMPILE_FOR_LINUX
-
-#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN
-#include <termios.h>
-#include <unistd.h>     // for read and return value of lseek
-#include <sys/time.h>   // for select_time
-#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN
-
-#include <ctype.h>      // isdigit()
-#include <stdio.h>      // stdout
-#include <stdarg.h>
-#include <time.h>
-
-#if defined COMPILE_FOR_LPC21
-#include <stdlib.h>
-#include <string.h>
-#include "lpc_ioctl.h"
-#endif
-
-#if !defined COMPILE_FOR_LPC21
-#include <fcntl.h>
-#endif
-
-#ifndef O_BINARY
-#define O_BINARY 0
-#endif // O_BINARY
-
-#ifndef DWORD
-#define DWORD unsigned long
-#endif // DWORD
-
-#if defined COMPILE_FOR_LINUX
-#define stricmp strcasecmp
-#endif // defined COMPILE_FOR_LINUX
-
-typedef unsigned char BINARY;   /**< data type used for microcontroller
-                                *  memory image.                        */
-
-/*
-debug levels
-0 - very quiet          - Nothing gets printed at this level
-1 - quiet               - Only error messages should be printed
-2 - indicate progress   - Add progress messages
-3 - first level debug   - Major level tracing
-4 - second level debug  - Add detailed debugging
-5 - log comm's          - log serial I/O
-*/
-
-typedef enum
-{
-    PHILIPS_ARM,
-    ANALOG_DEVICES_ARM
-} TARGET;
-
-typedef struct
-{
-    unsigned long id;
-    unsigned Product;
-    unsigned FlashSize;     /* in kiB, for informational purposes only */
-    unsigned RAMSize;       /* in kiB, for informational purposes only */
-    unsigned FlashSectors;  /* total number of sectors */
-    unsigned MaxCopySize;   /* maximum size that can be copied to Flash in a single command */
-    int *SectorTable;       /* pointer to a sector table with the sector sizes */
-} LPC_DEVICE_TYPE;
-
-typedef struct
-{
-
-#if !defined COMPILE_FOR_LPC21
-    TARGET micro;                      /**< The type of micro that will be
-                                         * programmed.                           */
-    int debug_level;
-    unsigned char TerminalAfterUpload;
-    unsigned char TerminalOnly;
-    unsigned char DetectOnly;
-    int           DetectedDevice;       /* index in LPCtypes[] array */
-    unsigned char FormatHex;
-    unsigned char ControlLines;
-    unsigned char LogFile;
-    char *input_file;                   /**< Name of the file to get input from. */
-    char *serial_port;                  /**< Name of the serial port to use to
-                                         * communicate with the microcontroller.
-                                         * Read from the command line.          */
-    char *baud_rate;                    /**< Baud rate to use on the serial
-                                         * port communicating with the
-                                         * microcontroller. Read from the
-                                         * command line.                        */
-#endif // !defined COMPILE_FOR_LPC21
-
-    char *StringOscillator;             /**< Holds representation of oscillator
-                                          * speed from the command line.         */
-    BINARY *BinaryContent;              /**< Binary image of the                 */
-                                        /* microcontroller's memory.             */
-    unsigned long BinaryLength;
-
-#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-    HANDLE hCom;
-#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21
-    int fdCom;
-#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21
-
-#if defined COMPILE_FOR_LINUX
-    struct termios oldtio, newtio;
-#endif // defined COMPILE_FOR_LINUX
-
-    unsigned serial_timeout_count;   /**< Local used to track
-                                      * timeouts on serial port read. */
-
-} ISP_ENVIRONMENT;
-
-static int debug_level = 2;
-
-static void DumpString(int level, const void *s, size_t size, const char *fmt, ...);
-static void SendComPort(ISP_ENVIRONMENT *IspEnvironment, const char *s);
-static void ReceiveComPort(ISP_ENVIRONMENT *IspEnvironment, void *Answer, unsigned long MaxSize, unsigned long *RealSize, unsigned long WantedNr0x0A,unsigned timeOutMilliseconds);
-static void DebugPrintf( int level, const char *fmt, ...);
-static void PhilipsOutputErrorMessage(unsigned char ErrorNumber);
-static unsigned char GetErrorNumber(const char *Answer);
-static void SerialTimeoutSet(ISP_ENVIRONMENT *IspEnvironment, unsigned timeout_milliseconds);
-static void SerialTimeoutTick(ISP_ENVIRONMENT *IspEnvironment);
-static int SerialTimeoutCheck(ISP_ENVIRONMENT *IspEnvironment);
-
-#if !defined COMPILE_FOR_LPC21
-static void ClearSerialPortBuffers(ISP_ENVIRONMENT *IspEnvironment);
-static void ControlModemLines(ISP_ENVIRONMENT *IspEnvironment, unsigned char DTR, unsigned char RTS);
-static unsigned char Ascii2Hex(unsigned char c);
-#endif
-
-static int SectorTable_210x[] = { 8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192,
-                                  8192, 8192, 8192, 8192, 8192, 8192, 8192 };
-static int SectorTable_211x[] = { 8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192,
-                                  8192, 8192, 8192, 8192, 8192, 8192, 8192, };
-static int SectorTable_212x[] = { 8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192,
-                                  65536, 65536, 8192, 8192, 8192, 8192, 8192, 8192,
-                                  8192 };
-static int SectorTable_213x[] = { 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096,
-                                  32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768,
-                                  32768, 32768, 32768, 32768, 32768, 32768, 4096, 4096,
-                                  4096, 4096, 4096 };
-static LPC_DEVICE_TYPE LPCtypes[] =
-{
-  { 0, 0, 0 },  /* unknown */
-  { 0xFFF0FF12, 2104, 128, 16, 15, 8192, SectorTable_210x },
-  { 0xFFF0FF22, 2105, 128, 32, 15, 8192, SectorTable_210x },
-  { 0xFFF0FF32, 2106, 128, 64, 15, 8192, SectorTable_210x },
-  { 0x0101FF12, 2114, 128, 16, 15, 8192, SectorTable_211x },
-  { 0x0201FF12, 2119, 128, 16, 15, 8192, SectorTable_211x },
-  { 0x0101FF13, 2124, 256, 16, 17, 8192, SectorTable_212x },
-  { 0x0201FF13, 2129, 256, 16, 17, 8192, SectorTable_212x },
-  { 0x0002FF01, 2131,  32,  8,  8, 4096, SectorTable_213x },
-  { 0x0002FF11, 2132,  64, 16,  9, 4096, SectorTable_213x },
-  { 0x0002FF12, 2134, 128, 16, 11, 4096, SectorTable_213x },
-  { 0x0002FF23, 2136, 256, 32, 15, 4096, SectorTable_213x },
-  { 0x0002FF25, 2138, 512, 32, 27, 4096, SectorTable_213x },
-  { 0x0402FF01, 2141,  32,  8,  8, 4096, SectorTable_213x },
-  { 0x0402FF11, 2142,  64, 16,  9, 4096, SectorTable_213x },
-  { 0x0402FF12, 2144, 128, 16, 11, 4096, SectorTable_213x },
-  { 0x0402FF23, 2146, 256, 40, 15, 4096, SectorTable_213x },
-  { 0x0402FF25, 2148, 512, 40, 27, 4096, SectorTable_213x },
-  { 0x0301FF13, 2194, 256, 16,  0, 8192, SectorTable_212x },
-  { 0x0301FF12, 2210,   0, 16,  0, 8192, SectorTable_211x }, /* table is a "don't care" */
-  { 0x0401FF12, 2212, 128, 16, 15, 8192, SectorTable_211x },
-  { 0x0601FF13, 2214, 256, 16, 17, 8192, SectorTable_212x },
-  /*            2290; same id as the LPC2210 */
-  { 0x0401FF13, 2292, 256, 16,  0, 8192, SectorTable_212x },
-  { 0x0501FF13, 2294, 256, 16,  0, 8192, SectorTable_212x },
-};
-
-/************* Portability layer. Serial and console I/O differences    */
-/* are taken care of here.                                              */
-
-#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN
-static int kbhit( void);
-static int getch( void);
-#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN
-
-#if defined COMPILE_FOR_LINUX
-static void Sleep(unsigned long MilliSeconds);
-#endif // defined COMPILE_FOR_LINUX
-
-
-
-#if !defined COMPILE_FOR_LPC21
-static void OpenSerialPort(ISP_ENVIRONMENT *IspEnvironment)
-{
-    // Open COM-Port (different between Windows and Linux)
-
-#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-    DCB    dcb;
-    COMMTIMEOUTS commtimeouts;
-
-    IspEnvironment->hCom = CreateFile(IspEnvironment->serial_port, GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
-
-    if(IspEnvironment->hCom == INVALID_HANDLE_VALUE)
-    {
-        DebugPrintf( 1, "Can't open COM-Port %s ! - Error: %ld\n", IspEnvironment->serial_port, GetLastError());
-        exit(2);
-    }
-
-    DebugPrintf( 3, "COM-Port %s opened...\n", IspEnvironment->serial_port);
-
-    GetCommState(IspEnvironment->hCom, &dcb);
-    dcb.BaudRate    = atol(IspEnvironment->baud_rate);
-    dcb.ByteSize    = 8;
-    dcb.StopBits    = ONESTOPBIT;
-    dcb.Parity      = NOPARITY;
-    dcb.fDtrControl = DTR_CONTROL_DISABLE;
-    dcb.fOutX       = FALSE;
-    dcb.fInX        = FALSE;
-    dcb.fNull       = FALSE;
-    dcb.fRtsControl = RTS_CONTROL_DISABLE;
-    if(SetCommState(IspEnvironment->hCom, &dcb) == 0)
-    {
-        DebugPrintf( 1, "Can't set baudrate %s ! - Error: %ld", IspEnvironment->baud_rate, GetLastError());
-        exit(3);
-    }
-
-    SetCommMask(IspEnvironment->hCom,EV_RXCHAR | EV_TXEMPTY);
-
-    commtimeouts.ReadIntervalTimeout         = MAXDWORD;
-    commtimeouts.ReadTotalTimeoutMultiplier  =    0;
-    commtimeouts.ReadTotalTimeoutConstant    =    1;
-    commtimeouts.WriteTotalTimeoutMultiplier =    0;
-    commtimeouts.WriteTotalTimeoutConstant   =    0;
-    SetCommTimeouts(IspEnvironment->hCom, &commtimeouts);
-
-#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-#if defined COMPILE_FOR_LINUX
-
-    IspEnvironment->fdCom = open(IspEnvironment->serial_port, O_RDWR | O_NOCTTY | O_NONBLOCK );
-
-    if(IspEnvironment->fdCom < 0)
-    {
-        DebugPrintf( 1, "Can't open COM-Port %s !\n", IspEnvironment->serial_port);
-        exit(2);
-    }
-
-    DebugPrintf( 3, "COM-Port %s opened...\n", IspEnvironment->serial_port);
-
-       /* clear input & output buffers, then switch to "blocking mode" */
-       tcflush(IspEnvironment->fdCom, TCOFLUSH);
-       tcflush(IspEnvironment->fdCom, TCIFLUSH);
-       fcntl(IspEnvironment->fdCom, F_SETFL, fcntl(IspEnvironment->fdCom, F_GETFL) & ~O_NONBLOCK);
-
-    tcgetattr(IspEnvironment->fdCom, &IspEnvironment->oldtio); /* save current port settings */
-
-    bzero(&IspEnvironment->newtio, sizeof(IspEnvironment->newtio));
-    IspEnvironment->newtio.c_cflag = CS8 | CLOCAL | CREAD;
-
-    switch(atol(IspEnvironment->baud_rate))
-    {
-#ifdef B1152000
-        case 1152000: IspEnvironment->newtio.c_cflag |= B1152000; break;
-#endif // B1152000
-#ifdef B576000
-        case  576000: IspEnvironment->newtio.c_cflag |=  B576000; break;
-#endif // B576000
-#ifdef B230400
-        case  230400: IspEnvironment->newtio.c_cflag |=  B230400; break;
-#endif // B230400
-#ifdef B115200
-        case  115200: IspEnvironment->newtio.c_cflag |=  B115200; break;
-#endif // B115200
-#ifdef B57600
-        case   57600: IspEnvironment->newtio.c_cflag |=   B57600; break;
-#endif // B57600
-#ifdef B38400
-        case   38400: IspEnvironment->newtio.c_cflag |=   B38400; break;
-#endif // B38400
-#ifdef B19200
-        case   19200: IspEnvironment->newtio.c_cflag |=   B19200; break;
-#endif // B19200
-#ifdef B9600
-        case    9600: IspEnvironment->newtio.c_cflag |=    B9600; break;
-#endif // B9600
-#ifdef B4800
-        case    4800: IspEnvironment->newtio.c_cflag |=    B4800; break;
-#endif // B4800
-#ifdef B2400
-        case    2400: IspEnvironment->newtio.c_cflag |=    B2400; break;
-#endif // B2400
-#ifdef B1200
-        case    1200: IspEnvironment->newtio.c_cflag |=    B1200; break;
-#endif // B1200
-#ifdef B600
-        case    600: IspEnvironment->newtio.c_cflag |=    B600; break;
-#endif // B600
-#ifdef B300
-        case    300: IspEnvironment->newtio.c_cflag |=    B300; break;
-#endif // B300
-        default:
-        {
-            DebugPrintf( 1, "unknown baudrate %s\n", IspEnvironment->baud_rate);
-            exit(3);
-        }
-    }
-
-    IspEnvironment->newtio.c_iflag = IGNPAR | IGNBRK | IXON | IXOFF;
-    IspEnvironment->newtio.c_oflag = 0;
-
-    /* set input mode (non-canonical, no echo,...) */
-    IspEnvironment->newtio.c_lflag = 0;
-
-    cfmakeraw(&IspEnvironment->newtio);
-    IspEnvironment->newtio.c_cc[VTIME]    = 1;   /* inter-character timer used */
-    IspEnvironment->newtio.c_cc[VMIN]     = 0;   /* blocking read until 0 chars received */
-
-    tcflush(IspEnvironment->fdCom, TCIFLUSH);
-    tcsetattr(IspEnvironment->fdCom, TCSANOW, &IspEnvironment->newtio);
-
-#endif // defined COMPILE_FOR_LINUX
-}
-
-static void CloseSerialPort(ISP_ENVIRONMENT *IspEnvironment)
-{
-#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-    CloseHandle(IspEnvironment->hCom);
-
-#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-#if defined COMPILE_FOR_LINUX
-
-       tcflush(IspEnvironment->fdCom, TCOFLUSH);
-       tcflush(IspEnvironment->fdCom, TCIFLUSH);
-    tcsetattr(IspEnvironment->fdCom, TCSANOW, &IspEnvironment->oldtio);
-
-    close(IspEnvironment->fdCom);
-
-#endif // defined COMPILE_FOR_LINUX
-}
-
-#endif // !defined COMPILE_FOR_LPC21
-
-/***************************** SendComPortBlock *************************/
-/**  Sends a block of bytes out the opened com port.
-\param [in] s block to send.
-\param [in] n size of the block.
-*/
-static void SendComPortBlock(ISP_ENVIRONMENT *IspEnvironment, const void *s, size_t n)
-{
-#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-    unsigned long realsize;
-
-#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-    DumpString( 4, s, n, "Sending ");
-
-#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-    WriteFile(IspEnvironment->hCom, s, n, &realsize, NULL);
-
-#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21
-
-    write(IspEnvironment->fdCom, s, n);
-
-#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21
-}
-
-/***************************** SendComPort ******************************/
-/**  Sends a string out the opened com port.
-\param [in] s string to send.
-*/
-static void SendComPort(ISP_ENVIRONMENT *IspEnvironment, const char *s)
-{
-    SendComPortBlock( IspEnvironment, s, strlen(s));
-}
-
-
-/***************************** ReceiveComPortBlock **********************/
-/**  Receives a buffer from the open com port. Returns all the characters
-ready (waits for up to 'n' milliseconds before accepting that no more
-characters are ready) or when the buffer is full. 'n' is system dependant,
-see SerialTimeout routines.
-\param [out] answer buffer to hold the bytes read from the serial port.
-\param [in] max_size the size of buffer pointed to by answer.
-\param [out] real_size pointer to a long that returns the amout of the
-buffer that is actually used.
-*/
-static void ReceiveComPortBlock( ISP_ENVIRONMENT *IspEnvironment,
-                                 void *answer, unsigned long max_size,
-                                 unsigned long *real_size)
-{
-
-#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-    ReadFile(IspEnvironment->hCom, answer, max_size, real_size, NULL);
-
-#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21
-
-    *real_size = read(IspEnvironment->fdCom, answer, max_size);
-
-#endif // defined COMPILE_FOR_LINUX
-
-
-    DumpString( 5, answer, (*real_size), "Read(Length=%ld): ", (*real_size));
-
-    if( *real_size == 0)
-    {
-        SerialTimeoutTick( IspEnvironment );
-    }
-}
-
-
-/***************************** SerialTimeoutSet *************************/
-/**  Sets (or resets) the timeout to the timout period requested.  Starts
-counting to this period.  This timeout support is a little odd in that the
-timeout specifies the accumulated deadtime waiting to read not the total
-time waiting to read. They should be close enought to the same for this
-use. Used by the serial input routines, the actual counting takes place in
-ReceiveComPortBlock.
-\param [in] timeout_milliseconds the time in milliseconds to use for
-timeout.  Note that just because it is set in milliseconds doesn't mean
-that the granularity is that fine.  In many cases (particularly Linux) it
-will be coarser.
-*/
-static void SerialTimeoutSet(ISP_ENVIRONMENT *IspEnvironment, unsigned timeout_milliseconds)
-{
-
-#if defined COMPILE_FOR_LINUX
-    IspEnvironment->serial_timeout_count = timeout_milliseconds/100;
-#elif defined COMPILE_FOR_LPC21
-    IspEnvironment->serial_timeout_count = timeout_milliseconds*200;
-#else
-    IspEnvironment->serial_timeout_count = timeout_milliseconds;
-#endif
-}
-
-/***************************** SerialTimeoutTick ************************/
-/**  Performs a timer tick.  In this simple case all we do is count down
-with protection against underflow and wrapping at the low end.
-*/
-static void SerialTimeoutTick(ISP_ENVIRONMENT *IspEnvironment)
-{
-    if( IspEnvironment->serial_timeout_count <= 1)
-    {
-        IspEnvironment->serial_timeout_count = 0;
-    }
-    else
-    {
-        IspEnvironment->serial_timeout_count--;
-    }
-}
-
-/***************************** SerialTimeoutCheck ***********************/
-/**  Check to see if the serial timeout timer has run down.
-\retval 1 if timer has run out.
-\retval 0 if timer still has time left.
-*/
-static int SerialTimeoutCheck(ISP_ENVIRONMENT *IspEnvironment)
-{
-    if( IspEnvironment->serial_timeout_count == 0)
-    {
-        return 1;
-    }
-    return 0;
-}
-
-
-#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN
-/***************************** getch ************************************/
-/** Replacement for the common dos function of the same name. Reads a
-single unbuffered character from the 'keyboard'.
-\return The character read from the keyboard.
-*/
-static int getch(void)
-{
-    char ch;
-    struct termios origtty, tty;
-
-    /* store the current tty settings */
-    tcgetattr(0, &origtty);
-
-    /* start with the current settings */
-    tty = origtty;
-    /* make modifications to put it in raw mode, turn off echo */
-    tty.c_lflag &= ~ICANON;
-    tty.c_lflag &= ~ECHO;
-    tty.c_lflag &= ~ISIG;
-    tty.c_cc[VMIN] = 1;
-    tty.c_cc[VTIME] = 0;
-
-    /* put the settings into effect */
-    tcsetattr(0, TCSADRAIN, &tty);
-
-    /* Read in one character */
-    read(0,&ch,1);
-
-    /* reset the tty to its original settings */
-    tcsetattr(0, TCSADRAIN, &origtty);
-
-    return ch;
-}
-#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN
-
-#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN
-/***************************** kbhit ************************************/
-/** Replacement for the common dos function of the same name. Indicates if
-there are characters to be read from the console.
-\retval 0 No characters ready.
-\retval 1 Characters from the console ready to be read.
-*/
-static int kbhit(void)
-{
-    /* return 0 for no key pressed, 1 for key pressed */
-    int return_value = 0;
-
-    /* variables to store the current tty state, create a new one */
-    struct termios origtty, tty;
-    /* time struct for the select() function, to only wait a little while */
-    struct timeval select_time;
-    /* file descriptor variable for the select() call */
-    fd_set readset;
-
-    /* we're only interested in STDIN */
-    FD_ZERO(&readset);
-    FD_SET(STDIN_FILENO, &readset);
-
-    /* store the current tty settings */
-    tcgetattr(0, &origtty);
-
-    /* start with the current settings */
-    tty = origtty;
-    /* make modifications to put it in raw mode, turn off echo */
-    tty.c_lflag &= ~ICANON;
-    tty.c_lflag &= ~ECHO;
-    tty.c_lflag &= ~ISIG;
-    tty.c_cc[VMIN] = 1;
-    tty.c_cc[VTIME] = 0;
-
-    /* put the settings into effect */
-    tcsetattr(0, TCSADRAIN, &tty);
-
-    /* how long to block for - this must be > 0.0, but could be changed
-       to some other setting. 10-18msec seems to work well and only
-       minimally load the system (0% CPU loading) */
-    select_time.tv_sec = 0;
-    select_time.tv_usec = 10;
-
-    /* is there a keystroke there? */
-    if (select(1, &readset, NULL, NULL, &select_time))
-    {
-        /* yes, remember it */
-        return_value = 1;
-    }
-
-    /* reset the tty to its original settings */
-    tcsetattr(0, TCSADRAIN, &origtty);
-
-    /* return with what we found out */
-    return return_value;
-}
-#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN
-
-
-#if !defined COMPILE_FOR_LPC21
-/***************************** ControlModemLines ************************/
-/**  Controls the modem lines to place the microcontroller into various
-states during the programming process.
-error rather abruptly terminates the program.
-\param [in] DTR the state to set the DTR line to.
-\param [in] RTS the state to set the RTS line to.
-*/
-static void ControlModemLines(ISP_ENVIRONMENT *IspEnvironment, unsigned char DTR, unsigned char RTS)
-{
-#if defined COMPILE_FOR_LINUX
-    int status;
-
-    if(ioctl(IspEnvironment->fdCom, TIOCMGET, &status) == 0)
-    {
-        DebugPrintf( 1, "ioctl get ok, status = %X\n",status);
-    }
-    else
-    {
-        DebugPrintf( 1, "ioctl get failed\n");
-    }
-
-    if(DTR) status |=  TIOCM_DTR;
-    else    status &= ~TIOCM_DTR;
-
-    if(RTS) status |=  TIOCM_RTS;
-    else    status &= ~TIOCM_RTS;
-
-    if(ioctl(IspEnvironment->fdCom, TIOCMSET, &status) == 0)
-    {
-        DebugPrintf( 1, "ioctl set ok, status = %X\n",status);
-    }
-    else
-    {
-        DebugPrintf( 1, "ioctl set failed\n");
-    }
-
-    if(ioctl(IspEnvironment->fdCom, TIOCMGET, &status) == 0)
-    {
-        DebugPrintf( 1, "ioctl get ok, status = %X\n",status);
-    }
-    else
-    {
-        DebugPrintf( 1, "ioctl get failed\n");
-    }
-
-#endif // defined COMPILE_FOR_LINUX
-#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-    if(DTR) EscapeCommFunction(IspEnvironment->hCom, SETDTR);
-    else    EscapeCommFunction(IspEnvironment->hCom, CLRDTR);
-
-    if(RTS) EscapeCommFunction(IspEnvironment->hCom, SETRTS);
-    else    EscapeCommFunction(IspEnvironment->hCom, CLRRTS);
-
-#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-
-    DebugPrintf( 3, "DTR (%d), RTS (%d)\n", DTR, RTS);
-}
-
-
-/***************************** ClearSerialPortBuffers********************/
-/**  Empty the serial port buffers.  Cleans things to a known state.
-*/
-static void ClearSerialPortBuffers(ISP_ENVIRONMENT *IspEnvironment)
-{
-#if defined COMPILE_FOR_LINUX
-    /* variables to store the current tty state, create a new one */
-    struct termios origtty, tty;
-
-    /* store the current tty settings */
-    tcgetattr(IspEnvironment->fdCom, &origtty);
-
-    // Flush input and output buffers
-    tcsetattr(IspEnvironment->fdCom, TCSAFLUSH, &tty);
-
-    /* reset the tty to its original settings */
-    tcsetattr(IspEnvironment->fdCom, TCSADRAIN, &origtty);
-#endif // defined COMPILE_FOR_LINUX
-#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-    PurgeComm(IspEnvironment->hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
-#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN
-}
-#endif // !defined COMPILE_FOR_LPC21
-
-
-#if defined COMPILE_FOR_LINUX
-/***************************** Sleep ************************************/
-/**  Provide linux replacement for windows function.
-\param [in] Milliseconds the time to wait for in milliseconds.
-*/
-static void Sleep(unsigned long MilliSeconds)
-{
-    usleep(MilliSeconds*1000); //convert to microseconds
-}
-#endif // defined COMPILE_FOR_LINUX
-
-
-/************* Applicationlayer.                                        */
-
-/***************************** DebugPrintf ******************************/
-/**  Prints a debug string depending the current debug level. The higher
-the debug level the more detail that will be printed.  Each print
-has an associated level, the higher the level the more detailed the
-debugging information being sent.
-\param [in] level the debug level of the print statement, if the level
- is less than or equal to the current debug level it will be printed.
-\param [in] fmt a standard printf style format string.
-\param [in] ... the usual printf parameters.
-*/
-static void DebugPrintf( int level, const char *fmt, ...)
-{
-    va_list ap;
-
-    if( level <= debug_level)
-    {
-        va_start( ap, fmt);
-        vprintf( fmt, ap);
-        va_end( ap);
-        fflush( stdout);
-    }
-}
-
-/***************************** ReceiveComPort ***************************/
-/**  Receives a buffer from the open com port. Returns when the buffer is
-filled, the numer of requested linefeeds has been received or the timeout
-period has passed
-\param [in] ISPEnvironment.
-\param [out] Answer buffer to hold the bytes read from the serial port.
-\param [in] MaxSize the size of buffer pointed to by Answer.
-\param [out] RealSize pointer to a long that returns the amout of the
-buffer that is actually used.
-\param [in] WantedNr0x0A the maximum number of linefeeds to accept before
-returning.
-\param [in] timeOutMilliseconds the maximum amount of time to wait before
-reading with an incomplete buffer.
-*/
-static void ReceiveComPort( ISP_ENVIRONMENT *IspEnvironment,
-                            void *Ans, unsigned long MaxSize,
-                            unsigned long *RealSize, unsigned long WantedNr0x0A,
-                            unsigned timeOutMilliseconds)
-{
-    unsigned long tmp_realsize;
-    unsigned long nr_of_0x0A = 0;
-    int eof = 0;
-    unsigned long p;
-    char *Answer;
-
-    Answer = Ans;
-
-    SerialTimeoutSet( IspEnvironment, timeOutMilliseconds);
-
-    (*RealSize) = 0;
-
-    do
-    {
-        ReceiveComPortBlock( IspEnvironment, Answer + (*RealSize), MaxSize - 1 - (*RealSize), &tmp_realsize);
-
-        if(tmp_realsize != 0)
-        {
-            for(p = (*RealSize); p < (*RealSize) + tmp_realsize; p++)
-            {
-                if(Answer[p] == 0x0a)
-                {
-                    nr_of_0x0A++;
-                }
-                else if(Answer[p] < 0)
-                {
-                    eof = 1;
-                }
-            }
-        }
-
-        (*RealSize) += tmp_realsize;
-
-    } while(((*RealSize) < MaxSize) && (SerialTimeoutCheck(IspEnvironment) == 0) && (nr_of_0x0A < WantedNr0x0A) && !eof);
-
-    Answer[(*RealSize)] = 0;
-
-    DumpString( 3, Answer, (*RealSize), "Answer(Length=%ld): ", (*RealSize));
-}
-
-
-#if !defined COMPILE_FOR_LPC21
-
-/***************************** ReceiveComPortBlockComplete **************/
-/**  Receives a fixed block from the open com port. Returns when the
-block is completely filled or the timeout period has passed
-\param [out] block buffer to hold the bytes read from the serial port.
-\param [in] size the size of the buffer pointed to by block.
-\param [in] timeOut the maximum amount of time to wait before guvung up on
-completing the read.
-\return 0 if successful, non-zero otherwise.
-*/
-static int ReceiveComPortBlockComplete( ISP_ENVIRONMENT *IspEnvironment,
-                                        void *block, size_t size, unsigned timeout)
-{
-    unsigned long realsize = 0, read;
-    char *result;
-
-    result = block;
-
-    SerialTimeoutSet( IspEnvironment, timeout);
-
-    do
-    {
-        ReceiveComPortBlock( IspEnvironment, result + realsize, size - realsize, &read);
-
-        realsize += read;
-
-    } while((realsize < size) && (SerialTimeoutCheck(IspEnvironment) == 0));
-
-    DumpString( 3, result, realsize, "Answer(Length=%ld): ", realsize);
-
-    if( realsize != size)
-    {
-        return 1;
-    }
-    return 0;
-}
-
-/***************************** ReadArguments ****************************/
-/**  Reads the command line arguments and parses it for the various
-options. Uses the same arguments as main.  Used to separate the command
-line parsing from main and improve its readability.  This should also make
-it easier to modify the command line parsing in the future.
-\param [in] argc the number of arguments.
-\param [in] argv an array of pointers to the arguments.
-*/
-static void ReadArguments(ISP_ENVIRONMENT *IspEnvironment, int argc, char *argv[])
-{
-    int i;
-
-    if(argc >= 5)
-    {
-        for(i = 1; i < argc - 4; i++)
-        {
-            if(stricmp(argv[i], "-bin") == 0)
-            {
-                 IspEnvironment->FormatHex = 0;
-                 DebugPrintf( 3, "Binary format file input.\n");
-            }
-            else if(stricmp(argv[i], "-hex") == 0)
-            {
-                 IspEnvironment->FormatHex = 1;
-                 DebugPrintf( 3, "Hex format file input.\n");
-            }
-            else if(stricmp(argv[i], "-term") == 0)
-            {
-                 IspEnvironment->TerminalAfterUpload = 1;
-                 DebugPrintf( 3, "Invoke terminal after upload.\n");
-            }
-            else if(stricmp(argv[i], "-termonly") == 0)
-            {
-                 IspEnvironment->TerminalOnly = 1;
-                 DebugPrintf( 3, "Only provide terminal.\n");
-            }
-            else if(stricmp(argv[i], "-detectonly") == 0)
-            {
-                 IspEnvironment->DetectOnly = 1;
-                 DebugPrintf( 3, "Only detect LPC chip part id.\n");
-            }
-            else if(stricmp(argv[i], "-debug") == 0)
-            {
-                 debug_level = 4;
-                 DebugPrintf( 3, "Turn on debug.\n");
-            }
-            else if(stricmp(argv[i], "-control") == 0)
-            {
-                 IspEnvironment->ControlLines = 1;
-                 DebugPrintf( 3, "Use RTS/DTS to control target state.\n");
-            }
-            else if(stricmp(argv[i], "-logfile") == 0)
-            {
-                 IspEnvironment->LogFile = 1;
-                 DebugPrintf( 3, "Log terminal output.\n");
-            }
-            else if( stricmp( argv[i], "-ADARM") == 0)
-            {
-                IspEnvironment->micro = ANALOG_DEVICES_ARM;
-                DebugPrintf( 2, "Target: Analog Devices.\n");
-            }
-            else if( stricmp( argv[i], "-PHILIPSARM") == 0)
-            {
-                IspEnvironment->micro = PHILIPS_ARM;
-                DebugPrintf( 2, "Target: Philips.\n");
-            }
-            else
-            {
-                DebugPrintf( 2, "Unknown command line option: \"%s\"\n", argv[i]);
-            }
-        }
-
-        IspEnvironment->input_file = argv[argc - 1];
-        IspEnvironment->StringOscillator = argv[argc - 2];
-        IspEnvironment->serial_port = argv[argc - 4];
-        IspEnvironment->baud_rate = argv[argc - 3];
-    }
-
-    if(argc < 5)
-    {
-        debug_level = (debug_level < 2) ? 2 : debug_level;
-    }
-
-    if(argc < 5)
-    {
-        DebugPrintf( 2, "\n");
-        DebugPrintf( 2, "Portable command line ISP for Philips LPC2000 family and \n");
-        DebugPrintf( 2, "Version " VERSION_STR "                 Analog Devices ADUC 70xx\n");
-        DebugPrintf( 2, "Compiled for %s: %s %s\n", COMPILED_FOR, __DATE__, __TIME__);
-        DebugPrintf( 2, "Copyright (c) by Martin Maurer, 2003-2005  Email: Martin.Maurer@clibb.de\n");
-        DebugPrintf( 2, "Portions Copyright (c) by Aeolus Development 2004\n");
-        DebugPrintf( 2, "                 http://www.aeolusdevelopment.com\n");
-        DebugPrintf( 2, "\n");
-
-        DebugPrintf( 1, "Syntax:  lpc21isp [Options] comport baudrate Oscillator_in_kHz file\n\n");
-        DebugPrintf( 1, "Example: lpc21isp test.hex com1 115200 14746\n\n");
-        DebugPrintf( 1, "Options: -bin           for uploading binary file\n");
-        DebugPrintf( 1, "         -hex           for uploading file in intel hex format (default)\n");
-        DebugPrintf( 1, "         -term          for starting terminal after upload\n");
-        DebugPrintf( 1, "         -termonly      for starting terminal without an upload\n");
-        DebugPrintf( 1, "         -detectonly    detect only used LPC chiptype (PHILIPSARM only)\n");
-        DebugPrintf( 1, "         -debug         for creating a lot of debug infos\n");
-        DebugPrintf( 1, "         -control       for controlling RS232 lines for easier booting\n");
-        DebugPrintf( 1, "                        (Reset = DTR, EnableBootLoader = RTS)\n");
-        DebugPrintf( 1, "         -logfile       for enabling logging of terminal output to lpc21isp.log\n");
-        DebugPrintf( 1, "         -ADARM         for downloading to an Analog Devices\n");
-        DebugPrintf( 1, "                        ARM microcontroller ADUC70xx\n");
-        DebugPrintf( 1, "         -PHILIPSARM    for downloading to a microcontroller from\n");
-        DebugPrintf( 1, "                        Philips LPC2000 family (default)\n");
-
-        exit(1);
-    }
-
-    if(IspEnvironment->micro == PHILIPS_ARM)
-    {
-        if (strlen(IspEnvironment->StringOscillator) > 5)
-        {
-            printf("Invalid crystal frequency %s",argv[argc - 1]);
-            exit(1);
-        }
-    }
-}
-
-typedef enum
-{
-    PROGRAM_MODE,
-    RUN_MODE
-} TARGET_MODE;
-
-/***************************** ResetTarget ******************************/
-/**  Resets the target leaving it in either download (program) mode or
-run mode.
-\param [in] mode the mode to leave the target in.
-*/
-static void ResetTarget( ISP_ENVIRONMENT *IspEnvironment, TARGET_MODE mode)
-{
-
-    if(IspEnvironment->ControlLines)
-    {
-
-        switch( mode)
-        {
-                /* Reset and jump to boot loader.                       */
-            case PROGRAM_MODE:
-                ControlModemLines(IspEnvironment, 1, 1);
-                Sleep(100);
-                ClearSerialPortBuffers(IspEnvironment);
-                Sleep(100);
-                ControlModemLines(IspEnvironment, 0, 1);
-                Sleep(100);
-                // Clear the RTS line after having reset the micro
-                // Needed for the "GO <Address> <Mode>" ISP command to work */
-                ControlModemLines(IspEnvironment, 0, 0);
-                break;
-
-                /* Reset and start uploaded program                     */
-            case RUN_MODE:
-                ControlModemLines(IspEnvironment, 1, 0);
-                Sleep(100);
-                ClearSerialPortBuffers(IspEnvironment);
-                Sleep(100);
-                ControlModemLines(IspEnvironment, 0, 0);
-                Sleep(100);
-                break;
-        }
-    }
-}
-
-/***************************** LoadFile *********************************/
-/**  Loads the requested file to download into memory.
-\param [in] IspEnvironment  structure containing input filename
-*/
-static void LoadFile(ISP_ENVIRONMENT *IspEnvironment)
-{
-    int fd;
-    int i;
-    unsigned long  Pos;
-    unsigned long  FileLength;
-    BINARY        *FileContent;              /**< Used to store the content of a hex */
-                                             /* file before converting to binary.    */
-    unsigned long BinaryMemSize;
-
-    fd = open(IspEnvironment->input_file, O_RDONLY | O_BINARY);
-    if(fd == -1)
-    {
-        DebugPrintf( 1, "Can't open file %s\n", IspEnvironment->input_file);
-        exit(1);
-    }
-
-    FileLength = lseek(fd, 0L, 2);      // Get file size
-
-    if(FileLength == (size_t)-1)
-    {
-        DebugPrintf( 1, "\nFileLength = -1 !?!\n");
-        exit(1);
-    }
-
-    lseek(fd, 0L, 0);
-
-    FileContent = malloc(FileLength);
-
-    BinaryMemSize = FileLength * 2;
-
-    IspEnvironment->BinaryLength = 0;   /* Increase length as needed.       */
-
-    IspEnvironment->BinaryContent = malloc(BinaryMemSize);
-
-    read(fd, FileContent, FileLength);
-
-    close(fd);
-
-    DebugPrintf( 2, "File %s:\n\tloaded...\n", IspEnvironment->input_file);
-
-        // Intel-Hex -> Binary Conversion
-
-    if(IspEnvironment->FormatHex != 0)
-    {
-        unsigned char  RecordLength;
-        unsigned short RecordAddress;
-        unsigned long  RealAddress = 0;
-        unsigned char  RecordType;
-        unsigned char  Hexvalue;
-
-        DebugPrintf( 3, "Converting file %s to binary format...\n", IspEnvironment->input_file);
-
-        Pos = 0;
-        while(Pos < FileLength)
-        {
-            if(FileContent[Pos] == '\r')
-            {
-                Pos++;
-                continue;
-            }
-
-            if(FileContent[Pos] == '\n')
-            {
-                Pos++;
-                continue;
-            }
-
-            if(FileContent[Pos] != ':')
-            {
-                DebugPrintf( 1, "Missing start of record (':') wrong byte %c / %02X\n", FileContent[Pos], FileContent[Pos]);
-                exit(1);
-            }
-
-            Pos++;
-
-            RecordLength   = Ascii2Hex(FileContent[Pos++]);
-            RecordLength <<= 4;
-            RecordLength  |= Ascii2Hex(FileContent[Pos++]);
-
-            DebugPrintf( 4, "RecordLength = %02X\n", RecordLength);
-
-            RecordAddress   = Ascii2Hex(FileContent[Pos++]);
-            RecordAddress <<= 4;
-            RecordAddress  |= Ascii2Hex(FileContent[Pos++]);
-            RecordAddress <<= 4;
-            RecordAddress  |= Ascii2Hex(FileContent[Pos++]);
-            RecordAddress <<= 4;
-            RecordAddress  |= Ascii2Hex(FileContent[Pos++]);
-
-            DebugPrintf( 4, "RecordAddress = %04X\n", RecordAddress);
-
-            RealAddress = RealAddress - (RealAddress & 0xffff) + RecordAddress;
-
-            DebugPrintf( 4, "RealAddress = %08lX\n", RealAddress);
-
-            RecordType      = Ascii2Hex(FileContent[Pos++]);
-            RecordType    <<= 4;
-            RecordType     |= Ascii2Hex(FileContent[Pos++]);
-
-            DebugPrintf( 4, "RecordType = %02X\n", RecordType);
-
-            if(RecordType == 0x00)          // 00 - Data record
-            {
-                    // Memory for binary file big enough ?
-                while(RealAddress + RecordLength > BinaryMemSize)
-                {
-                    BinaryMemSize <<= 1;
-                    IspEnvironment->BinaryContent = realloc(IspEnvironment->BinaryContent, BinaryMemSize);
-                }
-
-                    // We need to know, what the highest address is,
-                    // how many bytes / sectors we must flash
-                if(RealAddress + RecordLength > IspEnvironment->BinaryLength)
-                {
-                    IspEnvironment->BinaryLength = RealAddress + RecordLength;
-                    DebugPrintf( 3, "Image size now: %ld\n", IspEnvironment->BinaryLength);
-                }
-
-                for(i = 0; i < RecordLength; i++)
-                {
-                    Hexvalue        = Ascii2Hex(FileContent[Pos++]);
-                    Hexvalue      <<= 4;
-                    Hexvalue       |= Ascii2Hex(FileContent[Pos++]);
-                    IspEnvironment->BinaryContent[RealAddress + i] = Hexvalue;
-                }
-            }
-            else if(RecordType == 0x01)     // 01 - End of file record
-            {
-                break;
-            }
-            else if(RecordType == 0x02)     // 02 - Extended segment address record
-            {
-                for(i = 0; i < RecordLength * 2; i++)   // double amount of nibbles
-                {
-                    RealAddress <<= 4;
-                    if(i == 0)
-                    {
-                        RealAddress  = Ascii2Hex(FileContent[Pos++]);
-                    }
-                    else
-                    {
-                        RealAddress |= Ascii2Hex(FileContent[Pos++]);
-                    }
-                }
-                RealAddress <<= 4;
-            }
-            else if(RecordType == 0x03)     // 03 - Start segment address record
-            {
-                for(i = 0; i < RecordLength * 2; i++)   // double amount of nibbles
-                {
-                    RealAddress <<= 4;
-                    if(i == 0)
-                    {
-                        RealAddress  = Ascii2Hex(FileContent[Pos++]);
-                    }
-                    else
-                    {
-                        RealAddress |= Ascii2Hex(FileContent[Pos++]);
-                    }
-                }
-                RealAddress <<= 8;
-            }
-            else if(RecordType == 0x04)     // 04 - Extended linear address record, used by IAR
-            {
-                DebugPrintf( 1, "RecordType %02X not yet implemented - ignore ?\n", RecordType);
-                // exit(1);
-            }
-            else if(RecordType == 0x05)     // 05 - Start linear address record
-            {
-                DebugPrintf( 1, "RecordType %02X not yet implemented\n", RecordType);
-                exit(1);
-            }
-
-            while(FileContent[Pos++] != 0x0a)      // Search till line end
-            {
-            }
-        }
-
-        DebugPrintf( 2, "\tconverted to binary format...\n");
-
-            // When debugging is switched on, output result of conversion to file debugout.bin
-        if(debug_level >= 4)
-        {
-            int fdout;
-            fdout = open("debugout.bin", O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0777);
-            write(fdout, IspEnvironment->BinaryContent, IspEnvironment->BinaryLength);
-            close(fdout);
-        }
-    }
-    else
-    {
-        memcpy(IspEnvironment->BinaryContent, FileContent, FileLength);
-
-        IspEnvironment->BinaryLength = FileLength;
-    }
-
-    DebugPrintf( 2, "\timage size : %ld\n", IspEnvironment->BinaryLength);
-
-        // check length to flash for correct alignment, can happen with broken ld-scripts
-    if (IspEnvironment->BinaryLength % 4 != 0)
-    {
-        unsigned long NewBinaryLength = ((IspEnvironment->BinaryLength + 3)/4) * 4;
-
-        DebugPrintf( 2, "Warning:  data not aligned to 32 bits, padded (length was %lX, now %lX)\n", IspEnvironment->BinaryLength, NewBinaryLength);
-
-        IspEnvironment->BinaryLength = NewBinaryLength;
-    }
-
-}
-
-
-#define ANALOG_DEVICES_SYNC_CHAR        ((BINARY)0x08)
-#define ANALOG_DEVICES_SYNC_RESPONSE    ("ADuC")
-#define ANALOG_DEVICES_SYNC_SIZE        (strlen( ANALOG_DEVICES_SYNC_RESPONSE))
-
-typedef struct {
-    BINARY product_id[15];
-    BINARY version[3];
-    BINARY reserved[4];
-    BINARY terminator[2];
-    } AD_SYNC_RESPONSE;
-
-/***************************** AnalogDevicesSync ************************/
-/**  Attempt to synchronize with an Analog Device ARM micro.  Sends a
-backspace and reads back the microcontrollers response.  Performs
-multiple retries. Exits the program on error, returns to caller in the
-case of success.
-*/
-static void AnalogDevicesSync(ISP_ENVIRONMENT *IspEnvironment)
-{
-    BINARY sync;                        /* Holds sync command.          */
-    AD_SYNC_RESPONSE response;          /* Response from micro.         */
-    int sync_attempts;                  /* Number of retries.           */
-
-        /*  Make sure we don't read garbage later instead of the        */
-        /* response we expect from the micro.                           */
-    ClearSerialPortBuffers(IspEnvironment);
-
-    DebugPrintf( 2, "Synchronizing\n"); /* Progress report.             */
-
-    sync = ANALOG_DEVICES_SYNC_CHAR;    /* Build up sync command.       */
-
-        /*  Perform the actual sync attempt.  First send the sync       */
-        /* character, the attempt to read back the response.  For the   */
-        /* AD ARM micro this is a fixed length block.  If response is   */
-        /* received attempt to validate it by comparing the first       */
-        /* characters to those expected.  If the received block does    */
-        /* not validate or is incomplete empty the serial buffer and    */
-        /* retry.                                                       */
-    for(sync_attempts = 0; sync_attempts < 5; sync_attempts++)
-    {
-        SendComPortBlock( IspEnvironment, &sync, 1);
-
-        if( ReceiveComPortBlockComplete( IspEnvironment, &response, sizeof( response),
-            500) == 0)
-        {
-
-            if( memcmp( response.product_id, ANALOG_DEVICES_SYNC_RESPONSE,
-                ANALOG_DEVICES_SYNC_SIZE) == 0)
-            {
-                return;
-            }
-            else
-            {
-                DumpString( 3, &response, sizeof(response),
-                    "Unexpected response to sync attempt ");
-            }
-        }
-        else
-        {
-            DebugPrintf( 3, "No (or incomplete) answer on sync attempt\n");
-        }
-
-        ClearSerialPortBuffers(IspEnvironment);
-    }
-
-    DebugPrintf( 1, "No (or unacceptable) answer on sync attempt\n");
-    exit(4);
-}
-
-typedef struct {
-    char start1;
-    char start2;
-    BINARY bytes;
-    char cmd;
-    BINARY address_h;
-    BINARY address_u;
-    BINARY address_m;
-    BINARY address_l;
-    BINARY data[251];
-    } AD_PACKET;
-
-/***************************** AnalogDevicesFormPacket ******************/
-/**  Create an Analog Devices communication packet from the constituent
-elements.
-\param [in] cmd The command being sent, one of 'E' for erase, 'W' for
-write, 'V' for verify or 'R' for run..
-\param [in] no_bytes the number of data bytes to send with the command in
-the packet.
-\param [in] address the address to apply the command to.
-\param [in] data the data to send with the packet, may be null if no_bytes
-is zero.
-\param[out] packet that will be filled.
-*/
-static void AnalogDevicesFormPacket( ISP_ENVIRONMENT *IspEnvironment,
-                                     char cmd, int no_bytes, unsigned int address,
-                                     const void *data, AD_PACKET *packet)
-{
-    BINARY checksum;
-    const BINARY *data_in;
-    int i;
-
-    (void)IspEnvironment; /* never used in this function */
-
-        /*  Some sanity checking on the arguments.  These should only   */
-        /* fail if there is a bug in the caller.                        */
-        /*  Check 1) that the number of data bytes is in an acceptable  */
-        /* range, 2) that we have a non-null pointer if data is being   */
-        /* put in the packet and 3) that we have a non-null pointer to  */
-        /* the packet to be filled. We just exit with an error message  */
-        /* if any of these tests fail.                                  */
-    if( (no_bytes < 0) || (no_bytes > 250))
-    {
-        DebugPrintf( 1,
-            "The number of bytes (%d) passed to FormPacket is invalid.\n",
-            no_bytes);
-        exit( -1);
-    }
-    if( (data == 0) && (no_bytes != 0))
-    {
-        DebugPrintf( 1,
-            "A null pointer to data paased to FormPacket when data was expected.\n");
-        exit( -1);
-    }
-    if( packet == 0)
-    {
-        DebugPrintf( 1,
-            "A null packet pointer was passed to FormPacket.\n");
-        exit( -1);
-    }
-
-    checksum = 0;               /*  Checksum starts at zero.            */
-
-    data_in = data;             /*  Pointer pun so we can walk through  */
-                                /* the data.                            */
-
-    packet->start1 = 0x7;       /*  The start of the packet is constant.*/
-    packet->start2 = 0xE;
-
-        /*  Fill in the rest of the packet and calculate the checksum   */
-        /* as we go.                                                    */
-
-        /* The number of bytes is the number of data bytes + the        */
-        /* address bytes + the command byte.                            */
-    packet->bytes = (BINARY)(no_bytes + 5);
-
-    checksum += packet->bytes;
-
-        /*  The command for the packet being sent.  No error checking   */
-        /* done on this.                                                */
-    packet->cmd = cmd;
-
-    checksum += cmd;
-
-        /*  Now break up the address and place in the proper packet     */
-        /* locations.                                                   */
-    packet->address_l = (BINARY)(address & 0xFF);
-    packet->address_m = (BINARY)((address >> 8) & 0xFF);
-    packet->address_u = (BINARY)((address >> 16) & 0xFF);
-    packet->address_h = (BINARY)((address >> 24) & 0xFF);
-
-    checksum += packet->address_l;
-    checksum += packet->address_m;
-    checksum += packet->address_u;
-    checksum += packet->address_h;
-
-        /*  Copy the data bytes into the packet.  We could use memcpy   */
-        /* but we have to calculate the checksum anyway.                */
-    for( i = 0; i < no_bytes; i++)
-    {
-        packet->data[i] = data_in[i];
-        checksum += data_in[i];
-    }
-
-        /*  Finally, add the checksum to the end of the packet.         */
-    packet->data[i] = (BINARY)-checksum;
-}
-
-#define ANALOG_DEVICES_ACK      0x6
-#define ANALOG_DEVICES_NAK      0x7
-
-/***************************** AnalogDevicesSendPacket ******************/
-/**  Send a previously form Analog Devices communication.  Retry a
-couple of times if needed but fail by exiting the program if no ACK is
-forthcoming.
-\param [in] packet the packet to send.
-*/
-static void AnalogDevicesSendPacket( ISP_ENVIRONMENT *IspEnvironment,
-                                     const AD_PACKET * packet)
-{
-    BINARY response;
-    int retry = 0;
-
-    do {
-        retry++;
-
-                /*  Make sure we don't read garbage later instead of    */
-                /* the response we expect from the micro.               */
-        ClearSerialPortBuffers(IspEnvironment);
-
-                /*  Send the packet, the size is the number of data     */
-                /* bytes in the packet plus 3 bytes worth of header     */
-                /* plus checksum.                                       */
-        SendComPortBlock( IspEnvironment, packet, packet->bytes + 4);
-
-                /*  Receive the response and check, return to caller    */
-                /* if successful.                                       */
-        if( ReceiveComPortBlockComplete( IspEnvironment, &response, 1, 500) == 0)
-        {
-            if( response == ANALOG_DEVICES_ACK)
-            {
-                DebugPrintf( 3, "Packet Sent\n");
-                return;
-            }
-            if( response != ANALOG_DEVICES_NAK)
-            {
-                DebugPrintf( 3, "Unexpected response to packet (%x)\n", (int)response);
-            }
-            DebugPrintf( 2, "*");
-        }
-    } while( retry < 3);
-
-    DebugPrintf( 1, "Send packet failed\n");
-    exit( -1);
-}
-
-/***************************** AnalogDevicesErase ***********************/
-/**  Erase the Analog Devices micro.  We take the simple way out and
-just erase the whole thing.
-*/
-static void AnalogDevicesErase(ISP_ENVIRONMENT *IspEnvironment)
-{
-    BINARY pages;
-    AD_PACKET packet;
-
-    pages = 0;
-    DebugPrintf( 2, "Erasing .. ");
-    AnalogDevicesFormPacket( IspEnvironment, 'E', 1, 0, &pages, &packet);
-    AnalogDevicesSendPacket( IspEnvironment, &packet);
-    DebugPrintf( 2, "Erased\n");
-}
-
-#define AD_PACKET_SIZE (250)
-
-/***************************** AnalogDevicesWrite ***********************/
-/**  Write the program.
-\param [in] data the program to download to the micro.
-\param [in] address where to start placing the program.
-\param [in] bytes the size of the progrm to download.
-*/
-static void AnalogDevicesWrite( ISP_ENVIRONMENT *IspEnvironment,
-                                const void *data, long address, size_t bytes)
-{
-    AD_PACKET packet;
-    const BINARY *prog_data;
-
-    DebugPrintf( 2, "Writing %d bytes ", bytes);
-    prog_data = data;
-    while( bytes > AD_PACKET_SIZE)
-    {
-        AnalogDevicesFormPacket( IspEnvironment, 'W', AD_PACKET_SIZE, address, prog_data, &packet);
-        AnalogDevicesSendPacket( IspEnvironment, &packet);
-        address += AD_PACKET_SIZE;
-        prog_data += AD_PACKET_SIZE;
-        bytes -= AD_PACKET_SIZE;
-        DebugPrintf( 2, ".");
-    }
-    if( bytes > 0)
-    {
-        AnalogDevicesFormPacket( IspEnvironment, 'W', bytes, address, prog_data, &packet);
-        AnalogDevicesSendPacket( IspEnvironment, &packet);
-        DebugPrintf( 2, ".");
-    }
-}
-
-/***************************** AnalogDevicesDownload ********************/
-/**  Perform the download into an Analog Devices micro.  As a quick and
- * dirty hack against flash relocations at 0x80000
- * \return 0 if ok, error code else
- * \ToDo: possible to implement the return value instead of calling
- * exit() in sub-functions
- */
-static int AnalogDevicesDownload(ISP_ENVIRONMENT *IspEnvironment)
-{
-    AnalogDevicesSync(IspEnvironment);
-    AnalogDevicesErase(IspEnvironment);
-    if( IspEnvironment->BinaryLength > 0x80000)
-    {
-        DebugPrintf( 2, "Note:  Flash remapped 0x80000 to 0.\n");
-        AnalogDevicesWrite( IspEnvironment, IspEnvironment->BinaryContent + 0x80000, 0, IspEnvironment->BinaryLength-0x80000);
-    }
-    else
-    {
-        AnalogDevicesWrite( IspEnvironment, IspEnvironment->BinaryContent, 0, IspEnvironment->BinaryLength);
-    }
-    return(0);
-}
-
-/* note -- need to rationalize timeouts, use define?
-similarly for the number of sync attempts
-*/
-
-#endif // !defined COMPILE_FOR_LPC21
-
-
-static int SendAndVerify(ISP_ENVIRONMENT *IspEnvironment, const char *Command,
-                         char *AnswerBuffer, int AnswerLength)
-{
-    unsigned long realsize;
-    int cmdlen;
-
-    SendComPort( IspEnvironment, Command);
-    ReceiveComPort( IspEnvironment, AnswerBuffer, AnswerLength-1, &realsize, 2, 5000);
-    cmdlen = strlen(Command);
-    return (strncmp(AnswerBuffer, Command, cmdlen) == 0
-            && strcmp(AnswerBuffer + cmdlen, "0\r\n") == 0);
-}
-
-/***************************** Download *********************************/
-/**  Download the file from the internal memory image to the philips microcontroller.
- *   This function is visible from outside if COMPILE_FOR_LPC21
- */
-#define LPC_RAMBASE     0x40000200L
-#if !defined COMPILE_FOR_LPC21
-static
-#endif
-int PhilipsDownload(ISP_ENVIRONMENT *IspEnvironment)
-{
-    unsigned long realsize;
-    char Answer[128];
-    char temp[128];
-    char *strippedAnswer, *endPtr;
-    int  strippedsize;
-    int nQuestionMarks;
-    int found;
-    unsigned long Sector;
-    unsigned long SectorLength;
-    unsigned long SectorStart, SectorOffset, SectorChunk;
-    char tmpString[128];
-    char uuencode_table[64];
-    int Line;
-    unsigned long tmpStringPos;
-    unsigned long BlockOffset;
-    unsigned long Block;
-    unsigned long Pos;
-    unsigned long CopyLength;
-    int c,k=0,i;
-    unsigned long ivt_CRC;          // CRC over interrupt vector table
-    unsigned long block_CRC;
-    time_t tStartUpload, tDoneUpload;
-    long WatchDogSeconds = 0;
-    int WaitForWatchDog = 0;
-
-    if(!IspEnvironment->DetectOnly)
-    {
-            // Build up uuencode table
-        uuencode_table[0] = 0x60;           // 0x20 is translated to 0x60 !
-
-        for(i = 1; i <= 64; i++)
-        {
-            uuencode_table[i] = (char)(0x20 + i);
-        }
-
-            // Patch 0x14, otherwise it is not running and jumps to boot mode
-
-        ivt_CRC = 0;
-
-            /* Clear the vector at 0x14 so it doesn't affect the checksum: */
-        for(i = 0; i < 4; i++)
-        {
-            IspEnvironment->BinaryContent[i + 0x14] = 0;
-        }
-            /* Calculate a native checksum of the little endian vector table: */
-        for(i = 0; i < (4 * 8);) {
-            ivt_CRC += IspEnvironment->BinaryContent[i++];
-            ivt_CRC += IspEnvironment->BinaryContent[i++] << 8;
-            ivt_CRC += IspEnvironment->BinaryContent[i++] << 16;
-            ivt_CRC += IspEnvironment->BinaryContent[i++] << 24;
-        }
-
-            /* Negate the result and place in the vector at 0x14 as little endian
-             * again. The resulting vector table should checksum to 0. */
-        ivt_CRC = (unsigned long)-ivt_CRC;
-        for(i = 0; i < 4; i++)
-        {
-            IspEnvironment->BinaryContent[i + 0x14] = (unsigned char)(ivt_CRC >> (8 * i));
-        }
-
-        DebugPrintf( 3, "Position 0x14 patched: ivt_CRC = 0x%08lX\n", ivt_CRC);
-    }
-
-    DebugPrintf( 2, "Synchronizing");
-
-    for(nQuestionMarks = found = 0; !found && nQuestionMarks < 100; nQuestionMarks++)
-    {
-               DebugPrintf( 2, ".");
-        SendComPort( IspEnvironment, "?");
-
-        memset(Answer,0,sizeof(Answer));
-        ReceiveComPort( IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 1,100);
-
-        strippedAnswer = Answer;
-        strippedsize = realsize;
-        while((strippedsize > 0) && ((*strippedAnswer == '?') || (*strippedAnswer == 0)))
-        {
-            strippedAnswer++;
-            strippedsize--;
-        }
-
-        DumpString( 3, strippedAnswer, strippedsize, "StrippedAnswer(Length=%ld): '", strippedsize);
-
-        if(strcmp(strippedAnswer, "Bootloader\r\n") == 0 && IspEnvironment->TerminalOnly == 0)
-        {
-            long chars, xtal;
-            unsigned long ticks;
-            chars = (17 * IspEnvironment->BinaryLength + 1) / 10;
-            WatchDogSeconds = (10 * chars + 5) / atol(IspEnvironment->baud_rate) + 10;
-            xtal = atol(IspEnvironment->StringOscillator) * 1000;
-            ticks = (unsigned long)WatchDogSeconds * ((xtal + 15) / 16);
-            DebugPrintf( 2, "Entering ISP; re-synchronizing (watchdog = %ld seconds)\n", WatchDogSeconds);
-            sprintf(temp, "T %lu\r\n", ticks);
-            SendComPort( IspEnvironment, temp);
-            ReceiveComPort( IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 1,100);
-            if(strcmp(Answer, "OK\r\n") != 0)
-            {
-                printf("No answer on 'watchdog timer set'\n");
-                return(4);
-            }
-            SendComPort( IspEnvironment, "G 10356\r\n");
-            Sleep(200);
-            nQuestionMarks = 0;
-            WaitForWatchDog = 1;
-            continue;
-        }
-
-        tStartUpload = time(NULL);
-        if(strcmp(strippedAnswer, "Synchronized\r\n") == 0)
-        {
-            found = 1;
-        }
-#if !defined COMPILE_FOR_LPC21
-        else
-        {
-            ResetTarget(IspEnvironment, PROGRAM_MODE );
-        }
-#endif
-    }
-
-    if(!found)
-    {
-        DebugPrintf( 1, " no answer on '?'\n");
-        return(4);
-    }
-
-    DebugPrintf( 2, " OK\n");
-
-    SendComPort( IspEnvironment, "Synchronized\r\n");
-
-    ReceiveComPort( IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 2,1000);
-
-    if(strcmp(Answer, "Synchronized\r\nOK\r\n") != 0)
-    {
-        DebugPrintf( 1, "No answer on 'Synchronized'\n");
-        return(4);
-    }
-
-    DebugPrintf( 3, "Synchronized 1\n");
-
-    DebugPrintf( 3, "Setting oscillator\n");
-
-    sprintf( temp, "%s\r\n", IspEnvironment->StringOscillator);
-    SendComPort( IspEnvironment, temp);
-
-    ReceiveComPort( IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 2,1000);
-
-    sprintf( temp, "%s\r\nOK\r\n", IspEnvironment->StringOscillator);
-
-    if(strcmp(Answer, temp) != 0)
-    {
-        DebugPrintf( 1, "No answer on Oscillator-Command\n");
-        return(4);
-    }
-
-    DebugPrintf( 3, "Unlock\n");
-
-    if (!SendAndVerify(IspEnvironment, "U 23130\r\n", Answer, sizeof Answer))
-    {
-        DebugPrintf( 1, "Unlock-Command:\n");
-        PhilipsOutputErrorMessage(GetErrorNumber(Answer));
-        return(4);
-    }
-
-    DebugPrintf( 2, "Read bootcode version: ");
-
-    SendComPort( IspEnvironment, "K\r\n");
-
-    ReceiveComPort( IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 4,5000);
-
-    if(strncmp(Answer, "K\r\n", 3) != 0)
-    {
-        DebugPrintf( 1, "no answer on Read Boot Code Version\n");
-        return(4);
-    }
-
-    if(strncmp(Answer, "K\r\n0\r\n", 6) == 0)
-    {
-        int maj, min, build;
-        strippedAnswer = Answer + 6;
-        if (sscanf(strippedAnswer, "%d %d %d", &build, &min, &maj) == 2) {
-            maj = min;
-            min = build;
-            build = 0;
-        } /* if */
-        DebugPrintf( 2, "%d.%d.%d\n", maj, min, build);
-    }
-    else
-    {
-        DebugPrintf( 2, "unknown\n");
-    }
-
-    DebugPrintf( 2, "Read part ID: ");
-    SendComPort( IspEnvironment, "J\r\n");
-
-    ReceiveComPort( IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 3,5000);
-
-    if(strncmp(Answer, "J\r\n", 3) != 0)
-    {
-        DebugPrintf( 1, "no answer on Read Part Id\n");
-        return(4);
-    }
-
-    strippedAnswer = (strncmp(Answer, "J\r\n0\r\n", 6) == 0) ? Answer + 6 : Answer;
-    Pos = strtoul(strippedAnswer, &endPtr, 10);
-    *endPtr = '\0'; /* delete \r\n */
-    for (i = sizeof LPCtypes / sizeof LPCtypes[0] - 1; i > 0 && LPCtypes[i].id != Pos; i--)
-        /* nothing */;
-    IspEnvironment->DetectedDevice = i;
-    if (IspEnvironment->DetectedDevice == 0)
-        DebugPrintf( 2, "unknown");
-    else
-        DebugPrintf( 2, "LPC%d, %d kiB ROM / %d kiB SRAM",
-                        LPCtypes[IspEnvironment->DetectedDevice].Product,
-                        LPCtypes[IspEnvironment->DetectedDevice].FlashSize,
-                        LPCtypes[IspEnvironment->DetectedDevice].RAMSize);
-    DebugPrintf( 2, " (%s)\n", strippedAnswer);
-
-    if (IspEnvironment->DetectOnly)
-        return(0);
-
-    for(SectorStart = Sector = 0; ; SectorStart += LPCtypes[IspEnvironment->DetectedDevice].SectorTable[Sector++])
-    {
-        if (Sector >= LPCtypes[IspEnvironment->DetectedDevice].FlashSectors)
-        {
-            DebugPrintf( 1, "Programm too large; running out of Flash sectors.\n");
-            return(4);
-        }
-
-        DebugPrintf(2, "Sector %ld: ", Sector);
-        fflush(stdout);
-
-        sprintf(tmpString, "P %ld %ld\r\n", Sector, Sector);
-        if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
-        {
-            DebugPrintf( 1, "Wrong answer on Prepare-Command (1) (Sector %ld)\n", Sector);
-            PhilipsOutputErrorMessage(GetErrorNumber(Answer));
-            return(4);
-        }
-
-        DebugPrintf( 2, ".");
-        fflush(stdout);
-
-        sprintf(tmpString, "E %ld %ld\r\n", Sector, Sector);
-        if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
-        {
-            DebugPrintf( 1, "Wrong answer on Erase-Command (Sector %ld)\n", Sector);
-            PhilipsOutputErrorMessage(GetErrorNumber(Answer));
-            return(4);
-        }
-
-        DebugPrintf( 2, ".");
-        fflush(stdout);
-
-        SectorLength = LPCtypes[IspEnvironment->DetectedDevice].SectorTable[Sector];
-        if(SectorLength > IspEnvironment->BinaryLength - SectorStart)
-        {
-            SectorLength = IspEnvironment->BinaryLength - SectorStart;
-        }
-
-        for(SectorOffset = 0; SectorOffset < SectorLength; SectorOffset += SectorChunk)
-        {
-                       if (SectorOffset > 0)
-                       {
-                               // Add a visible marker between segments in a sector
-                DebugPrintf(2, "|");  /* means: partial segment copied */
-                fflush(stdout);
-                       }
-
-                // If the Flash ROM sector size is bigger than the number of bytes
-                // we can copy from RAM to Flash, we must "chop up" the sector and
-                // copy these individually.
-                // This is especially needed in the case where a Flash sector is
-                // bigger than the amount of SRAM.
-            SectorChunk = SectorLength - SectorOffset;
-            if(SectorChunk > (unsigned)LPCtypes[IspEnvironment->DetectedDevice].MaxCopySize)
-            {
-                SectorChunk = LPCtypes[IspEnvironment->DetectedDevice].MaxCopySize;
-            }
-
-                // Write multiple of 45 * 4 Byte blocks to RAM, but copy maximum of on sector to Flash
-                // In worst case we transfer up to 180 byte to much to RAM
-                // but then we can always use full 45 byte blocks and length is multiple of 4
-            CopyLength = SectorChunk;
-            if((CopyLength % (45 * 4)) != 0)
-            {
-                CopyLength += ((45 * 4) - (CopyLength % (45 * 4)));
-            }
-            sprintf(tmpString, "W %ld %ld\r\n", LPC_RAMBASE, CopyLength);
-            if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
-            {
-                DebugPrintf( 1, "Wrong answer on Write-Command\n");
-                PhilipsOutputErrorMessage(GetErrorNumber(Answer));
-                return(4);
-            }
-
-            DebugPrintf( 2, ".");
-            fflush(stdout);
-
-            block_CRC = 0;
-            Line = 0;
-
-                // Transfer blocks of 45 * 4 bytes to RAM
-            for(Pos = SectorStart + SectorOffset; (Pos < SectorStart + SectorOffset + CopyLength) && (Pos < IspEnvironment->BinaryLength); Pos += (45 * 4))
-            {
-                for(Block = 0; Block < 4; Block++)  // Each block 45 bytes
-                {
-                    DebugPrintf( 2, ".");
-                    fflush(stdout);
-
-                        // Uuencode one 45 byte block
-                    tmpStringPos = 0;
-
-                    tmpString[tmpStringPos++] = (char)(' ' + 45);  // Encode Length of block
-
-                    for(BlockOffset = 0; BlockOffset < 45; BlockOffset++)
-                    {
-                        c = IspEnvironment->BinaryContent[Pos + Block * 45 + BlockOffset];
-
-                        block_CRC += c;
-
-                        k = (k << 8) + (c & 255);
-
-                        if((BlockOffset % 3) == 2)   // Collecting always 3 Bytes, then do processing in 4 Bytes
-                        {
-                            tmpString[tmpStringPos++] = uuencode_table[(k >> 18) & 63];
-                            tmpString[tmpStringPos++] = uuencode_table[(k >> 12) & 63];
-                            tmpString[tmpStringPos++] = uuencode_table[(k >>  6) & 63];
-                            tmpString[tmpStringPos++] = uuencode_table[ k        & 63];
-                        }
-                    }
-
-                    tmpString[tmpStringPos++] = '\r';
-                    tmpString[tmpStringPos++] = '\n';
-                    tmpString[tmpStringPos++] = 0;
-
-                    SendComPort( IspEnvironment, tmpString);
-
-                    ReceiveComPort( IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 1,5000);
-
-                    if(strncmp(Answer, tmpString, tmpStringPos) != 0)
-                    {
-                        DebugPrintf( 1, "Error on writing data (1)\n");
-                        return(4);
-                    }
-
-                    Line++;
-
-                    DebugPrintf( 3, "Line = %d\n", Line);
-
-                    if(Line == 20)
-                    {
-                            // printf("block_CRC = %ld\n", block_CRC);
-
-                        sprintf(tmpString, "%ld\r\n", block_CRC);
-                        SendComPort( IspEnvironment, tmpString);
-
-                        ReceiveComPort( IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 2,5000);
-
-                        sprintf(tmpString, "%ld\r\nOK\r\n", block_CRC);
-                        if(strcmp(Answer, tmpString) != 0)
-                        {
-                            DebugPrintf( 1, "Error on writing block_CRC (1)\n");
-                            return(4);
-                        }
-
-                        Line = 0;
-                        block_CRC = 0;
-                    }
-                }
-            }
-
-            if(Line != 0)
-            {
-                sprintf(tmpString, "%ld\r\n", block_CRC);
-                SendComPort( IspEnvironment, tmpString);
-
-                ReceiveComPort( IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 2,5000);
-
-                sprintf(tmpString, "%ld\r\nOK\r\n", block_CRC);
-                if(strcmp(Answer, tmpString) != 0)
-                {
-                    DebugPrintf( 1, "Error on writing block_CRC (2)\n");
-                    return(4);
-                }
-            }
-
-                // Prepare command must be repeated before every write
-            sprintf(tmpString, "P %ld %ld\r\n", Sector, Sector);
-            if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
-            {
-                DebugPrintf( 1, "Wrong answer on Prepare-Command (2) (Sector %ld)\n", Sector);
-                PhilipsOutputErrorMessage(GetErrorNumber(Answer));
-                return(4);
-            }
-
-               // Round CopyLength up to one of the following values: 512, 1024,
-               // 4096, 8192; but do not exceed the maximum copy size (usually
-               // 8192, but chip-dependent)
-            if(CopyLength < 512)
-            {
-                CopyLength = 512;
-            }
-            else if(SectorLength < 1024)
-            {
-                CopyLength = 1024;
-            }
-            else if(SectorLength < 4096)
-            {
-                CopyLength = 4096;
-            }
-            else
-            {
-                CopyLength = 8192;
-            }
-            if(CopyLength > (unsigned)LPCtypes[IspEnvironment->DetectedDevice].MaxCopySize)
-            {
-                CopyLength = LPCtypes[IspEnvironment->DetectedDevice].MaxCopySize;
-            }
-
-            sprintf(tmpString, "C %ld %ld %ld\r\n", SectorStart + SectorOffset, LPC_RAMBASE, CopyLength);
-            if (!SendAndVerify(IspEnvironment, tmpString, Answer, sizeof Answer))
-            {
-                DebugPrintf( 1, "Wrong answer on Write-Command\n");
-                PhilipsOutputErrorMessage(GetErrorNumber(Answer));
-                return(4);
-            }
-
-        }
-
-        DebugPrintf(2, "\n");
-        fflush(stdout);
-
-        if((SectorStart + SectorLength) >= IspEnvironment->BinaryLength)
-        {
-            break;
-        }
-    }
-
-    tDoneUpload = time(NULL);
-    DebugPrintf( 2, "Download Finished... taking %d seconds\n", tDoneUpload - tStartUpload);
-
-    if (WaitForWatchDog)
-    {
-        DebugPrintf( 2, "Wait for restart, in %d seconds from now\n", WatchDogSeconds - (tDoneUpload - tStartUpload));
-    }
-    else
-    {
-        DebugPrintf( 2, "Now launching the brand new code\n" );
-        fflush(stdout);
-
-        SendComPort(IspEnvironment, "G 0 A\r\n"); //goto 0 : run this fresh new downloaded code code
-        ReceiveComPort(IspEnvironment, Answer, sizeof(Answer)-1, &realsize, 2, 5000);
-        /* the reply string is frequently terminated with a -1 (EOF) because the
-         * connection gets broken; zero-terminate the string ourselves
-         */
-        while (realsize > 0 && Answer[(int)realsize - 1] < 0)
-            realsize--;
-        Answer[(int)realsize] = '\0';
-        /* Better to check only the first 9 chars instead of complete receive buffer,
-         * because the answer can contain the output by the started programm
-         */
-        if(realsize == 0 || strncmp((const char *)Answer, "G 0 A\r\n0\r", 9) != 0)
-        {
-            DebugPrintf( 2, "Failed to run the new downloaded code: ");
-            PhilipsOutputErrorMessage(GetErrorNumber(Answer));
-            return(4);
-        }
-        fflush(stdout);
-    }
-    return(0);
-}
-
-
-#if !defined COMPILE_FOR_LPC21
-/***************************** Terminal *********************************/
-/**  Acts as a simple dumb terminal. Press 'ESC' to exit.
-*/
-static void Terminal( ISP_ENVIRONMENT *IspEnvironment)
-{
-    int           ch = 0;
-    char buffer[128];
-    int           fdlogfile = -1;
-    unsigned long realsize;
-
-        // When logging is switched on, output terminal output to lpc21isp.log
-    if(IspEnvironment->LogFile)
-    {
-        fdlogfile = open("lpc21isp.log", O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0777);
-    }
-
-    printf("Terminal started (press Escape to abort)\n\n");
-    fflush(stdout);
-
-    do
-    {
-            // Check for received characters
-
-        ReceiveComPort( IspEnvironment, buffer, sizeof(buffer) - 1, &realsize, 0,200);
-
-        if( realsize )
-        {
-            write(1, buffer, realsize);
-            fflush(stdout);
-            if(IspEnvironment->LogFile)     // When logging is turned on, then copy output to logfile
-            {
-                write(fdlogfile, buffer, realsize);
-            }
-        }
-
-            // check for keypress, and write any out the port.
-        if ( kbhit() )
-        {
-            ch = getch();
-            if(ch == 0x1b)
-            {
-                break;
-            }
-
-            buffer[0] = (unsigned char)ch;
-            buffer[1] = 0;
-
-            SendComPort( IspEnvironment, buffer);
-        }
-    } while(ch != 0x1b);
-
-    printf("\n\nTerminal stopped\n\n");
-    fflush(stdout);
-
-    if(IspEnvironment->LogFile)
-    {
-        close(fdlogfile);
-    }
-
-}
-
-#endif // !defined COMPILE_FOR_LPC21
-
-/***************************** main *************************************/
-/**  main. Everything starts from here.
-\param [in] argc the number of arguments.
-\param [in] argv an array of pointers to the arguments.
-*/
-
-#if !defined COMPILE_FOR_LPC21
-
-int main(int argc, char *argv[])
-{
-    ISP_ENVIRONMENT IspEnvironment;
-    int downloadResult = -1;
-
-        // Initialize debug level
-    debug_level = 2;
-
-        // Initialize ISP Environment
-    IspEnvironment.micro = PHILIPS_ARM;          /**< The type of micro that will be */
-                                                 /* programmed.                      */
-    IspEnvironment.TerminalAfterUpload = 0;
-    IspEnvironment.TerminalOnly = 0;
-    IspEnvironment.DetectOnly = 0;
-    IspEnvironment.DetectedDevice = 0;          /* index in LPCtypes[] */
-    IspEnvironment.FormatHex = 1;
-    IspEnvironment.ControlLines = 0;
-    IspEnvironment.LogFile = 0;
-
-        /* Read and parse the command line.                             */
-    ReadArguments( &IspEnvironment, argc, argv);
-       DebugPrintf( 2, "lpc21isp version " VERSION_STR "\n");
-
-        /* Download requested, read in the input file.                  */
-    if(!IspEnvironment.TerminalOnly && !IspEnvironment.DetectOnly)
-    {
-        LoadFile(&IspEnvironment);
-    }
-
-    OpenSerialPort(&IspEnvironment);   /* Open the serial port to the microcontroller. */
-
-    ResetTarget(&IspEnvironment, PROGRAM_MODE);
-
-    ClearSerialPortBuffers(&IspEnvironment);
-
-
-        /* Perform the requested download.                              */
-    if(!IspEnvironment.TerminalOnly)
-    {
-        switch( IspEnvironment.micro)
-        {
-            case PHILIPS_ARM:
-                downloadResult = PhilipsDownload(&IspEnvironment);
-                break;
-
-            case ANALOG_DEVICES_ARM:
-                downloadResult = AnalogDevicesDownload(&IspEnvironment);
-                break;
-        }
-        if( downloadResult != 0 )
-        {
-            CloseSerialPort(&IspEnvironment);
-            exit(downloadResult);
-        }
-    }
-
-    ResetTarget(&IspEnvironment, RUN_MODE);
-
-    IspEnvironment.debug_level = 1;    /* From now on there is no more debug output !! */
-                                       /* Therefore switch it off...                   */
-
-        /* User asked for terminal emulation, provide a really dumb     */
-        /* terminal.                                                    */
-    if(IspEnvironment.TerminalAfterUpload || IspEnvironment.TerminalOnly)
-    {
-        Terminal(&IspEnvironment);
-    }
-
-    CloseSerialPort(&IspEnvironment);  /*  All done, close the serial port to the      */
-    return(0);                         /* microcontroller and exit.                    */
-}
-
-#endif // !defined COMPILE_FOR_LPC21
-
-/***************************** DumpString ******************************/
-/**  Prints an area of memory to stdout. Converts non-printables to hex.
-\param [in] level the debug level of the block to be dumped.  If this is
-less than or equal to the current debug level than the dump will happen
-otherwise this just returns.
-\param [in] b pointer to an area of memory.
-\param [in] size the length of the memory block to print.
-\param [in] fmt a printf style format string used to produce a prefix to
-the block of dumped memory.
-\param [in] ... the arguments referenced by the fmt string.
-*/
-static void DumpString(int level, const void *b, size_t size, const char *fmt, ...)
-{
-    size_t i;
-    const char *s;
-    va_list ap;
-
-    if( level <= debug_level)
-    {
-        va_start( ap, fmt);
-        vprintf( fmt, ap);
-        va_end( ap);
-
-        s = b;
-
-        printf("'");
-        for( i = 0; i < size; i++)
-            {
-            if(s[i] >= 0x20 && s[i] <= 0x7e) /*isprint?*/
-            {
-                putchar(s[i]);
-            }
-            else
-            {
-                printf("(%02X)", s[i]);
-            }
-        }
-        printf("'\n");
-    }
-
-}
-
-
-#if !defined COMPILE_FOR_LPC21
-/***************************** Ascii2Hex ********************************/
-/**  Converts a hex character to its equivalent number value. In case of an
-error rather abruptly terminates the program.
-\param [in] c the hex digit to convert.
-\return the value of the hex digit.
-*/
-static unsigned char Ascii2Hex(unsigned char c)
-{
-    if(c >= '0' && c <= '9')
-    {
-        return(unsigned char)(c - '0');
-    }
-    if(c >= 'A' && c <= 'F')
-    {
-        return(unsigned char)(c - 'A' + 10);
-    }
-    if(c >= 'a' && c <= 'f')
-    {
-        return(unsigned char)(c - 'a' + 10);
-    }
-    printf("Wrong Hex-Nibble %c (%02X)\n", c, c);
-    exit(1);
-    return(0);  // this "return" will never be reached, but some compilers give a warning if it is not present
-}
-#endif // !defined COMPILE_FOR_LPC21
-
-
-/***************************** PhilipsOutputErrorMessage ***********************/
-/**  Given an error number find and print the appropriate error message.
-\param [in] ErrorNumber The number of the error.
-*/
-static void PhilipsOutputErrorMessage(unsigned char ErrorNumber)
-{
-//#if !defined COMPILE_FOR_LPC21
-    switch(ErrorNumber)
-    {
-        case   0:
-            DebugPrintf( 1, "CMD_SUCCESS");
-            break;
-
-        case   1:
-            DebugPrintf( 1, "INVALID_COMMAND");
-            break;
-
-        case   2:
-            DebugPrintf( 1, "SRC_ADDR_ERROR: Source address is not on word boundary.");
-            break;
-        case   3:
-            DebugPrintf( 1, "DST_ADDR_ERROR: Destination address is not on a correct boundary.");
-            break;
-
-        case   4:
-            DebugPrintf( 1, "SRC_ADDR_NOT_MAPPED: Source address is not mapped in the memory map.\n");
-            DebugPrintf( 1, "                     Count value is taken into consideration where applicable.");
-            break;
-
-        case   5:
-            DebugPrintf( 1, "DST_ADDR_NOT_MAPPED: Destination address is not mapped in the memory map.\n");
-            DebugPrintf( 1, "                     Count value is taken into consideration where applicable.");
-            break;
-
-        case   6:
-            DebugPrintf( 1, "COUNT_ERROR: Byte count is not multiple of 4 or is not a permitted value.");
-            break;
-
-        case   7:
-            DebugPrintf( 1, "INVALID_SECTOR: Sector number is invalid or end sector number is\n");
-            DebugPrintf( 1, "                greater than start sector number.");
-            break;
-
-        case   8:
-            DebugPrintf( 1, "SECTOR_NOT_BLANK");
-            break;
-
-        case   9:
-            DebugPrintf( 1, "SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION:\n");
-            DebugPrintf( 1, "Command to prepare sector for write operation was not executed.");
-            break;
-
-        case  10:
-            DebugPrintf( 1, "COMPARE_ERROR: Source and destination data not equal.");
-            break;
-
-        case  11:
-            DebugPrintf( 1, "BUSY: Flash programming hardware interface is busy.");
-            break;
-
-        case  12:
-            DebugPrintf( 1, "PARAM_ERROR: Insufficient number of parameters or invalid parameter.");
-            break;
-
-        case  13:
-            DebugPrintf( 1, "ADDR_ERROR: Address is not on word boundary.");
-            break;
-
-        case  14:
-            DebugPrintf( 1, "ADDR_NOT_MAPPED: Address is not mapped in the memory map.\n");
-            DebugPrintf( 1, "                 Count value is taken in to consideration where applicable.");
-            break;
-
-        case  15:
-            DebugPrintf( 1, "CMD_LOCKED");
-            break;
-
-        case  16:
-            DebugPrintf( 1, "INVALID_CODE: Unlock code is invalid.");
-            break;
-
-        case  17:
-            DebugPrintf( 1, "INVALID_BAUD_RATE: Invalid baud rate setting.");
-            break;
-
-        case  18:
-            DebugPrintf( 1, "INVALID_STOP_BIT: Invalid stop bit setting.");
-            break;
-
-        case 255:                                                                                            break;
-        default:
-            DebugPrintf( 1, "unknown error %u", ErrorNumber);
-            break;
-    }
-//#else // !defined COMPILE_FOR_LPC21
-//    printf("error %u", ErrorNumber);
-//#endif // !defined COMPILE_FOR_LPC21
-}
-
-
-/***************************** GetErrorNumber ***************************/
-/**  Find error number in string.  This will normally be the string
-returned from the microcontroller.
-\param [in] Answer the buffer to search for the error number.
-\return the error number found, if no linefeed found before the end of the
-string an error value of 255 is returned. If a non-numeric value is found
-then it is printed to stdout and an error value of 255 is returned.
-*/
-static unsigned char GetErrorNumber(const char *Answer)
-{
-    unsigned int i = 0;
-
-    while(1)
-    {
-        if(Answer[i] == 0x00) return(255);
-        if(Answer[i] == 0x0a) break;
-        i++;
-    }
-
-    i++;
-
-    if(Answer[i] < '0' || Answer[i] > '9')
-    {
-        DebugPrintf( 1, "ErrorString: %s", &Answer[i]);
-        return(255);
-    }
-
-    return (unsigned char)(atoi(&Answer[i]));
-}
+/******************************************************************************\r
+\r
+Project:           Portable command line ISP for Philips LPC2000 family\r
+                   and Analog Devices ADUC70xx\r
+\r
+Filename:          lpc21isp.c\r
+\r
+Compiler:          Microsoft VC 6/7, GCC Cygwin, GCC Linux, GCC ARM ELF\r
+\r
+Author:            Martin Maurer (Martin.Maurer@clibb.de)\r
+\r
+Copyright:         (c) Martin Maurer 2003-2008, All rights reserved\r
+Portions Copyright (c) by Aeolus Development 2004 http://www.aeolusdevelopment.com\r
+\r
+    This file is part of lpc21isp.\r
+\r
+    lpc21isp is free software: you can redistribute it and/or modify\r
+    it under the terms of the GNU Lesser General Public License as published by\r
+    the Free Software Foundation, either version 3 of the License, or\r
+    any later version.\r
+\r
+    lpc21isp is distributed in the hope that it will be useful,\r
+    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+    GNU Lesser General Public License for more details.\r
+\r
+    You should have received a copy of the GNU Lesser General Public License\r
+    and GNU General Public License along with lpc21isp.\r
+    If not, see <http://www.gnu.org/licenses/>.\r
+*/\r
+\r
+#if defined(_WIN32)\r
+#if !defined __BORLANDC__\r
+#include "StdAfx.h"        // Precompiled Header for WIN32\r
+#endif\r
+#endif // defined(_WIN32)\r
+#include "lpc21isp.h"   // if using propriatory serial port communication (customize attached lpc21isp.h)\r
+#include "adprog.h"\r
+#include "lpcprog.h"\r
+#include "lpcterm.h"\r
+\r
+/*\r
+Change-History:\r
+\r
+1.00  2004-01-08  Initial Version, tested for MSVC6/7 and GCC under Cygwin\r
+1.01  2004-01-10  Porting to Linux (at least compiling must work)\r
+1.02  2004-01-10  Implemented conversion intel hex format -> binary\r
+1.03  2004-01-25  Preparation to upload to public website\r
+1.04  2004-02-12  Merged in bugfixes by Soeren Gust\r
+1.05  2004-03-14  Implement printing of error codes as text / strings\r
+1.06  2004-03-09  Merged in bugfixes by Charles Manning:\r
+                  The '?' sychronisation does not reliably respond to the first '?'.\r
+                  I added some retries.\r
+                  The LPC2106 sometimes responds to the '?' by echoing them back.\r
+                  This sometimes causes an attempt to match "?Synchonized".\r
+                  Added code to strip off any leading '?'s.\r
+                  Timeouts were too long.\r
+                  Change from RTS/CTS to no flow control.\r
+                  Done because many/most people will use only 3-wire comms.\r
+                  Added some progress tracing.\r
+1.07  2004-03-14  Implement handling of control lines for easier booting\r
+1.08  2004-04-01  Bugfix for upload problem\r
+1.09  2004-04-03  Redesign of upload routine\r
+                  Now always 180 byte blocks are uploaded, to prevent\r
+                  small junks in uuencoding\r
+1.10  2004-04-03  Clear buffers before sending commands to LPC21xx,\r
+                  this prevents synchronizing errors when previously loaded\r
+                  program does a lot of output, so FIFO of PC runs full\r
+1.11  2004-04-03  Small optimization for controlling reset line\r
+                  otherwise termonly starts LPC twice, free PC buffers\r
+1.12  2004-04-04  Add switch to enable logging terminal output to lpc21isp.log\r
+1.13  2004-05-19  Merged in improvement by Charles Manning:\r
+                  Instead of exiting the wrong hex file size is corrected\r
+1.14  2004-07-07  Merged in improvement by Alex Holden:\r
+                  Remove little/big endian dependancy\r
+1.15  2004-09-27  Temporary improvement by Cyril Holweck:\r
+                  Removed test (data echoed = data transmited) on the main\r
+                  data transfert, since this was the biggest failure\r
+                  reason and is covered by checksome anyway.\r
+                  Added COMPILE_FOR_LPC21, to have target dump it's own\r
+                  memory to stdout.\r
+1.16  2004-10-09  Merged in bugfix / improvement by Sinelnikov Evgeny\r
+                  I found out that Linux and Windows serial port initialization\r
+                  are different with pinouts states. My board don't get\r
+                  reset signal at first cycle of DTR pinout moving.\r
+                  And I add this moving to initalization cycle.\r
+1.17  2004-10-21  Changes by Cyril Holweck\r
+                  Divide main, take out the real programming function, that can\r
+                  also be used by a target to copy its own code to another.\r
+1.18  2004-10-26  Changes by Cyril Holweck\r
+                  Added a "G 0 A\r\n" at end of programming to run code.\r
+1.19  2004-11-03  Changes by Robert Adsett\r
+                  Add support for Analog Devices.\r
+                  Separate file load from programming.\r
+                  Change from a debug on/off flag to debug level\r
+                  Remove if (debug) tests and replace with DebugPrintf\r
+                  statements.\r
+                  Change serial I/O and timing so that the system\r
+                  dependancies are isolated to a few portability functions.\r
+                  Add support for binary serial I/O.\r
+                  Add doxygen support.\r
+1.20  2004-11-07  Preparation for multiport booting (factory support)\r
+1.21  2004-11-08  Bugfix from Robert Adsett\r
+                  BinaryLength was not initialized\r
+1.22  2004-11-08  Changes from Cyril Holweck / Evgeny Sinelnikov\r
+                  Forgotten IspEnvironment-> and bugfixes if COMPILE_FOR_LINUX\r
+                  If COMPILE_FOR_LPC21, PhilipsDownload() 'acts as' main():\r
+                  - it should not be static and should return int.\r
+                  - no sub-function can use exit() but only return ()\r
+                  Use 'char' instead of 'byte' ;)\r
+1.23  2005-01-16  Build in automatic detection of LPC chiptype\r
+                  (needed for 256 KByte support)\r
+1.24B 2005-06-02  Changes by Thiadmer Riemersma: completed support for other\r
+                  chip types (LPC213x series and others).\r
+1.24C 2005-06-11  Changes by Thiadmer Riemersma: added the device ID codes for\r
+                  chip types LPC2131 and LPC2132.\r
+1.25  2005-06-19  Martin Maurer: Setup more parameters in DCB,\r
+                  otherwise wrong code is downloaded (only Windows and Cygwin)\r
+                  when a previous program has changed these parameters\r
+                  Check exact string of "G 0 A\r\n0\r\n" instead of whole received buffer,\r
+                  to prevent checking of already received by program start\r
+                  (error on running program, but reports CMD_SUCCESS)\r
+                  Add ifdefs for all baudrates (needed only for high baudrate,\r
+                  which seem to be not available on Macs...)\r
+1.26  2005-06-26  Martin Maurer:\r
+                  Correct check again: "G 0 A\r\n0\r\n" is cutted, because of reboot\r
+                  (error on running program, but reports CMD_SUCCESS)\r
+1.27  2005-06-29  Martin Maurer:\r
+                  Add LPC chip ID's (thanks to Robert from Philips) for\r
+                  missing LPC213x and upcoming new LPC214x chips\r
+                  (currently untested, because i don't have access to these chips,\r
+                  please give me feedback !)\r
+1.28  2005-07-27  Anders Rosvall / Embedded Artists AB:\r
+                  Changed the reset timeout to 500 ms when entering the bootloader.\r
+                  Some external reset controllers have quite long timeout periods,\r
+                  so extening the timeout delay would be a good thing.\r
+1.29  2005-09-14  Rob Jansen:\r
+                  Added functionality to download to RAM and run from there.\r
+                  In LoadFile() added record types 04 (Extended Linear Address Record)\r
+                  and 05 (Start Linear Address Record), added address offset\r
+                  (IspEnvironment->BinaryOffset) and start address (...->StartAddress).\r
+                  Changed PhilipsDownload to skip all Flash prepare/erase/copy commands.\r
+                  Note: Tested with VC7 only\r
+1.30   2005-10-04 Rob Jansen:\r
+                  - forgot to change the version string in 1.29\r
+                  - Wrong text in LoadFile corrected (printed text mentions record type 05,\r
+                    this should be 04\r
+                  - Changed LoadFile to accept multiple record types 04\r
+                  - Changed LoadFile to check on memory size, will not load more than x MB\r
+                    if linear extended address records are used\r
+1.31   2005-11-13 Martin Maurer: Thanks to Frank Gutmann\r
+                  Updated number of sectors in device table\r
+                  for LPC2194, LPC2292 and LPC2294\r
+1.32   2005-12-02 Martin Maurer: Corrected missing control of RTS/DTR\r
+                  in case user selected -termonly and -control\r
+                  Small correction (typo in debug)\r
+1.33   2006-10-01 Jean-Marc Koller:\r
+                  Added support for MacOS X (difference on how to set termios baudrate).\r
+1.34   2006-10-01  Cyril Holweck:\r
+                  Made it compile again for lpc21isp\r
+                  Added const keyword to constant variables to make it better\r
+                  code for embeded target. (decrease RAM usage)\r
+                  Replaced all regular call to printf() by DebugPrintf()\r
+                  Removed call to scanf() (not much usefull and cost a lot to my target)\r
+1.35   2006-22-01 Cyril Holweck\r
+                  Added feature for LPC21: will start downloading at Sector 1 and upward,\r
+                  to finish with Sector 0, the one containing the checksum controling BSL entry\r
+1.36   2006-25-01 Cyril Holweck\r
+                  PhilipsDownload() will now return a unique error code for each error\r
+1.37   2006-10-03 Jeroen Domburg\r
+                  Added LPC2103 (and only the 2103, I can't find the IDs for 2101/2102)\r
+                  Corrected a loop which occured if the program completely fits in sector 0\r
+1.38   2007-01-05 Ray Molenkamp\r
+                  Added feature for LPC21: Wipe entire device before programming to enable\r
+                  reflashing of chips with the lpc codeprotection feature enabled.\r
+1.39   2007-01-12 Martin Maurer\r
+                  Added initial support for new processors LPC23xx and LPC24xx\r
+1.40   2007-01-22 Martin Maurer\r
+                  Correction of chip id of LPC2458\r
+1.41   2007-01-28 Jean-Marc Koller\r
+                  Modified Terminal() to disable ECHO with termios only once, instead of\r
+                  modifying and restoring termios in each getch and kbhit call (which caused\r
+                  a strange echo behaviour in MacOS X).\r
+1.42   2007-01-28 Rob Probin\r
+                  Added -localecho command to allow local echoing in terminal mode for use\r
+                  where target does not echo back keystrokes.\r
+1.43   2007-01-29 Martin Maurer\r
+                  Moved keyboard handling routines to own subroutines,\r
+                  so they can be used during aborting synchronisation.\r
+                  Newest cygwin made problems, StringOscillator always contained '\0x0d'\r
+                  at the end, when calling lpc21isp from batch file\r
+1.44   2007-02-23 Yang Yang\r
+                  Added feature for LPC21: Verify the data in Flash after every writes\r
+                  to sector. To detect errors in writing to Flash ROM.\r
+1.45   2007-02-25 Martin Maurer\r
+                  Replace printf syntax of DumpString by a simple pointer to a string\r
+                  printf syntax is a nice thing, but it is not working :-(\r
+                  and therefore makes debugging much more difficult...\r
+                  Moved VERSION_STR to top of file to avoid possible cosmetical errors\r
+1.46   2007-02-25 Martin Maurer\r
+                  Again corrected debug output: should solve output of\r
+                  (FFFFFFB5) instead of (B5)\r
+1.47   2007-02-27 Robert Adsett\r
+                  Raised timeout on AD send packet function.\r
+1.48   2007-04-20 Martin Maurer\r
+                  Thanks to Josef Wolf for preventing to overwrite over end of array\r
+1.49   2007-10-16 New Option -halfduplex allow single wire using.\r
+                  Implemented and tested only for Windows. Data Resend implemented.\r
+1.50   2007-10-31 Changes by Simon Ellwood\r
+                  Formated the code for readablity\r
+                  Fixed some c++ compiler issues\r
+1.51   2007-11-20 Changes by Simon Ellwood\r
+                  Split into seperate files\r
+                  Made more modular so when used in an embedded mode only the required code is built\r
+1.52   2008-01-22 Changes by Manuel Koeppen\r
+                  Made compileable again for linux and windows\r
+                  Fixed bug in ClearSerialPortBuffers (linux)\r
+1.53   2008-02-25 Changes by Michael Roth\r
+                  Get priority of debug messages wih -control right\r
+1.54   2008-03-03 Martin Maurer\r
+                  Try to bring lpc21isp back to a useable state in Windows, Cygwin, Linux and Mac OS.\r
+                  Merged in changes by Erika Stefanini, which were done only for old version 1.49:\r
+                  Added device ids for revision B chips\r
+1.55   2008-03-03 Martin Maurer\r
+                  Thanks to Fausto Marzoli, bugfix for compiling latest version under Linux\r
+1.56   2008-04-01 Steve Franks\r
+                  Integrate FreeBSD patch.\r
+                  Add support for swapping and/or inverting RTS & DTR\r
+1.57   2008-04-06 Mauricio Scaff\r
+                  Changed OpenSerialPort to work with MacOS\r
+                  Corrected the number of sectors in some 512K devices (28 instead of 27)\r
+                  Added support for LPC2387 and LPC2388\r
+                  Defined BL error 19 (Code Protected)\r
+1.58   2008-05-10 Herbert Demmel dh2@demmel.com\r
+                  I had the special requirement to integrate the program into my own Windows\r
+                  software compiled with Borland C++ Builder 5. I had to do some minor changes\r
+                  for Borland (see defined __BORLANDC__) and modified to code slightly to have\r
+                  some simple callbacks for screen i/o (see define INTEGRATED_IN_WIN_APP).\r
+                  Please notet that I don *not* check / modify the part for AnalogDevices !!\r
+                  Besides that I fixed some minor issues:\r
+                  added dcb.fOutxCtsFlow = FALSE and dcb.fOutxDsrFlow = FALSE (sometimes required)\r
+                  Now comparing one character less of answer to "Now launching ... code" command\r
+1.59   2008-07-07 Peter Hayward\r
+                  Fixed freeze under Windows XP SP2 by removing redundant call to SetCommMask.\r
+1.60   2008-07-21 Martin Maurer\r
+                  Added uptodate part ids for LPC2458, LPC2468 and LPC2478\r
+                  Add comment "obsolete" for older part ids for LPC2458 and LPC2468\r
+                  Add ", " between compile date and time\r
+1.61   2008-10-21 Fausto Marzoli (thanks to Geoffrey Wossum for the patches)\r
+                  Fix for compiling latest version under Linux and "ControlLinesSwapped" issue\r
+1.62   2008-11-19 Martin Maurer\r
+                  Added (untested) support for LPC2109\r
+                  Added (untested) support for LPC2361 / LPC2362\r
+                  Heavy update of part identification number of LPC23xx and LPC24xx\r
+                  Correct bug, that hex file must exist, when "-detectonly" is used\r
+                  Correct Makefile.vc: use /Fe instead of -o\r
+1.63   2008-11-23 Martin Maurer\r
+                  Changed to GNU Lesser General Public License\r
+1.64   2009-01-19 Steve Franks\r
+               __FREEBSD__ changed to __FreeBSD__ at some point, plus other com port fixes\r
+*/\r
+\r
+// Don't forget to update the version string that is on the next line\r
+#define VERSION_STR "1.64"\r
+\r
+#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+static char RxTmpBuf[256];        // save received data to this buffer for half-duplex\r
+char * pRxTmpBuf = RxTmpBuf;\r
+#endif\r
+\r
+#if !defined COMPILE_FOR_LPC21\r
+int debug_level = 2;\r
+#endif\r
+\r
+/************* Portability layer. Serial and console I/O differences    */\r
+/* are taken care of here.                                              */\r
+\r
+#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+static void OpenSerialPort(ISP_ENVIRONMENT *IspEnvironment)\r
+{\r
+    DCB    dcb;\r
+    COMMTIMEOUTS commtimeouts;\r
+\r
+    IspEnvironment->hCom = CreateFile(IspEnvironment->serial_port, GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);\r
+\r
+    if (IspEnvironment->hCom == INVALID_HANDLE_VALUE)\r
+    {\r
+        DebugPrintf(1, "Can't open COM-Port %s ! - Error: %ld\n", IspEnvironment->serial_port, GetLastError());\r
+        exit(2);\r
+    }\r
+\r
+    DebugPrintf(3, "COM-Port %s opened...\n", IspEnvironment->serial_port);\r
+\r
+    GetCommState(IspEnvironment->hCom, &dcb);\r
+    dcb.BaudRate    = atol(IspEnvironment->baud_rate);\r
+    dcb.ByteSize    = 8;\r
+    dcb.StopBits    = ONESTOPBIT;\r
+    dcb.Parity      = NOPARITY;\r
+    dcb.fDtrControl = DTR_CONTROL_DISABLE;\r
+    dcb.fOutX       = FALSE;\r
+    dcb.fInX        = FALSE;\r
+    dcb.fNull       = FALSE;\r
+    dcb.fRtsControl = RTS_CONTROL_DISABLE;\r
+\r
+    // added by Herbert Demmel - iF CTS line has the wrong state, we would never send anything!\r
+    dcb.fOutxCtsFlow = FALSE;\r
+    dcb.fOutxDsrFlow = FALSE;\r
+\r
+    if (SetCommState(IspEnvironment->hCom, &dcb) == 0)\r
+    {\r
+        DebugPrintf(1, "Can't set baudrate %s ! - Error: %ld", IspEnvironment->baud_rate, GetLastError());\r
+        exit(3);\r
+    }\r
+\r
+   /*\r
+    *  Peter Hayward 02 July 2008\r
+    *\r
+    *  The following call is only needed if the WaitCommEvent\r
+    *  or possibly the GetCommMask functions are used.  They are\r
+    *  *not* in this implimentation.  However, under Windows XP SP2\r
+    *  on my laptop the use of this call causes XP to freeze (crash) while\r
+    *  this program is running, e.g. in section 5/6/7 ... of a largish\r
+    *  download.  Removing this *unnecessary* call fixed the problem.\r
+    *  At the same time I've added a call to SetupComm to request\r
+    *  (not necessarity honoured) the operating system to provide\r
+    *  large I/O buffers for high speed I/O without handshaking.\r
+    *\r
+    *   SetCommMask(IspEnvironment->hCom,EV_RXCHAR | EV_TXEMPTY);\r
+    */\r
+    SetupComm(IspEnvironment->hCom, 32000, 32000);\r
+\r
+    SetCommMask(IspEnvironment->hCom, EV_RXCHAR | EV_TXEMPTY);\r
+\r
+    commtimeouts.ReadIntervalTimeout         = MAXDWORD;\r
+    commtimeouts.ReadTotalTimeoutMultiplier  =    0;\r
+    commtimeouts.ReadTotalTimeoutConstant    =    1;\r
+    commtimeouts.WriteTotalTimeoutMultiplier =    0;\r
+    commtimeouts.WriteTotalTimeoutConstant   =    0;\r
+    SetCommTimeouts(IspEnvironment->hCom, &commtimeouts);\r
+}\r
+#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+\r
+#if defined COMPILE_FOR_LINUX\r
+static void OpenSerialPort(ISP_ENVIRONMENT *IspEnvironment)\r
+{\r
+    IspEnvironment->fdCom = open(IspEnvironment->serial_port, O_RDWR | O_NOCTTY | O_NONBLOCK);\r
+\r
+    if (IspEnvironment->fdCom < 0)\r
+    {\r
+        int err = errno;\r
+        DebugPrintf(1, "Can't open COM-Port %s ! (Error: %dd (0x%X))\n", IspEnvironment->serial_port, err, err);\r
+        exit(2);\r
+    }\r
+\r
+    DebugPrintf(3, "COM-Port %s opened...\n", IspEnvironment->serial_port);\r
+\r
+    /* clear input & output buffers, then switch to "blocking mode" */\r
+    tcflush(IspEnvironment->fdCom, TCOFLUSH);\r
+    tcflush(IspEnvironment->fdCom, TCIFLUSH);\r
+    fcntl(IspEnvironment->fdCom, F_SETFL, fcntl(IspEnvironment->fdCom, F_GETFL) & ~O_NONBLOCK);\r
+\r
+    tcgetattr(IspEnvironment->fdCom, &IspEnvironment->oldtio); /* save current port settings */\r
+\r
+    bzero(&IspEnvironment->newtio, sizeof(IspEnvironment->newtio));\r
+    IspEnvironment->newtio.c_cflag = CS8 | CLOCAL | CREAD;\r
+\r
+#ifdef __APPLE__\r
+#define NEWTERMIOS_SETBAUDARTE(bps) IspEnvironment->newtio.c_ispeed = IspEnvironment->newtio.c_ospeed = bps;\r
+\r
+#elif __FreeBSD__\r
+\r
+       if(cfsetspeed(&IspEnvironment->newtio,(speed_t) strtol(IspEnvironment->baud_rate,NULL,10))) {\r
+                  DebugPrintf(1, "baudrate %s not supported\n", IspEnvironment->baud_rate);\r
+                  exit(3);\r
+              };\r
+#else\r
+#define NEWTERMIOS_SETBAUDARTE(bps) IspEnvironment->newtio.c_cflag |= bps;\r
+\r
+    switch (atol(IspEnvironment->baud_rate))\r
+    {\r
+#ifdef B1152000\r
+          case 1152000: NEWTERMIOS_SETBAUDARTE(B1152000); break;\r
+#endif // B1152000\r
+#ifdef B576000\r
+          case  576000: NEWTERMIOS_SETBAUDARTE(B576000); break;\r
+#endif // B576000\r
+#ifdef B230400\r
+          case  230400: NEWTERMIOS_SETBAUDARTE(B230400); break;\r
+#endif // B230400\r
+#ifdef B115200\r
+          case  115200: NEWTERMIOS_SETBAUDARTE(B115200); break;\r
+#endif // B115200\r
+#ifdef B57600\r
+          case   57600: NEWTERMIOS_SETBAUDARTE(B57600); break;\r
+#endif // B57600\r
+#ifdef B38400\r
+          case   38400: NEWTERMIOS_SETBAUDARTE(B38400); break;\r
+#endif // B38400\r
+#ifdef B19200\r
+          case   19200: NEWTERMIOS_SETBAUDARTE(B19200); break;\r
+#endif // B19200\r
+#ifdef B9600\r
+          case    9600: NEWTERMIOS_SETBAUDARTE(B9600); break;\r
+#endif // B9600\r
+          default:\r
+              {\r
+                  DebugPrintf(1, "unknown baudrate %s\n", IspEnvironment->baud_rate);\r
+                  exit(3);\r
+              }\r
+    }\r
+\r
+#endif\r
+\r
+    IspEnvironment->newtio.c_iflag = IGNPAR | IGNBRK | IXON | IXOFF;\r
+    IspEnvironment->newtio.c_oflag = 0;\r
+\r
+    /* set input mode (non-canonical, no echo,...) */\r
+    IspEnvironment->newtio.c_lflag = 0;\r
+\r
+    cfmakeraw(&IspEnvironment->newtio);\r
+    IspEnvironment->newtio.c_cc[VTIME]    = 1;   /* inter-character timer used */\r
+    IspEnvironment->newtio.c_cc[VMIN]     = 0;   /* blocking read until 0 chars received */\r
+\r
+    tcflush(IspEnvironment->fdCom, TCIFLUSH);\r
+    if(tcsetattr(IspEnvironment->fdCom, TCSANOW, &IspEnvironment->newtio))\r
+    {\r
+       DebugPrintf(1, "Could not change serial port behaviour (wrong baudrate?)\n");\r
+       exit(3);\r
+    }\r
+\r
+}\r
+#endif // defined COMPILE_FOR_LINUX\r
+\r
+#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+static void CloseSerialPort(ISP_ENVIRONMENT *IspEnvironment)\r
+{\r
+    CloseHandle(IspEnvironment->hCom);\r
+}\r
+\r
+#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+\r
+#if defined COMPILE_FOR_LINUX\r
+static void CloseSerialPort(ISP_ENVIRONMENT *IspEnvironment)\r
+{\r
+    tcflush(IspEnvironment->fdCom, TCOFLUSH);\r
+    tcflush(IspEnvironment->fdCom, TCIFLUSH);\r
+    tcsetattr(IspEnvironment->fdCom, TCSANOW, &IspEnvironment->oldtio);\r
+\r
+    close(IspEnvironment->fdCom);\r
+}\r
+#endif // defined COMPILE_FOR_LINUX\r
+\r
+\r
+/***************************** SendComPortBlock *************************/\r
+/**  Sends a block of bytes out the opened com port.\r
+\param [in] s block to send.\r
+\param [in] n size of the block.\r
+*/\r
+void SendComPortBlock(ISP_ENVIRONMENT *IspEnvironment, const void *s, size_t n)\r
+{\r
+#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+\r
+    unsigned long realsize;\r
+    size_t m;\r
+    unsigned long rxsize;\r
+    char * pch;\r
+    char * rxpch;\r
+#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+\r
+    DumpString(4, s, n, "Sending ");\r
+\r
+#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+\r
+    if (IspEnvironment->HalfDuplex == 0)\r
+        WriteFile(IspEnvironment->hCom, s, n, &realsize, NULL);\r
+    else\r
+    {\r
+        pch = (char *)s;\r
+        rxpch = RxTmpBuf;\r
+        pRxTmpBuf = RxTmpBuf;\r
+\r
+        // avoid buffer otherflow\r
+        if (n > sizeof (RxTmpBuf))\r
+            n = sizeof (RxTmpBuf);\r
+\r
+        for (m = 0; m < n; m++)\r
+        {\r
+            WriteFile(IspEnvironment->hCom, pch, 1, &realsize, NULL);\r
+\r
+            if ((*pch != '?') || (n != 1))\r
+            {\r
+                do\r
+                {\r
+                    ReadFile(IspEnvironment->hCom, rxpch, 1, &rxsize, NULL);\r
+                }while (rxsize == 0);\r
+            }\r
+            pch++;\r
+            rxpch++;\r
+        }\r
+        *rxpch = 0;        // terminate echo string\r
+    }\r
+#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+\r
+#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21\r
+\r
+    write(IspEnvironment->fdCom, s, n);\r
+\r
+#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21\r
+}\r
+\r
+/***************************** SendComPort ******************************/\r
+/**  Sends a string out the opened com port.\r
+\param [in] s string to send.\r
+*/\r
+void SendComPort(ISP_ENVIRONMENT *IspEnvironment, const char *s)\r
+{\r
+    SendComPortBlock(IspEnvironment, s, strlen(s));\r
+}\r
+\r
+/***************************** SerialTimeoutTick ************************/\r
+/**  Performs a timer tick.  In this simple case all we do is count down\r
+with protection against underflow and wrapping at the low end.\r
+*/\r
+static void SerialTimeoutTick(ISP_ENVIRONMENT *IspEnvironment)\r
+{\r
+    if (IspEnvironment->serial_timeout_count <= 1)\r
+    {\r
+        IspEnvironment->serial_timeout_count = 0;\r
+    }\r
+    else\r
+    {\r
+        IspEnvironment->serial_timeout_count--;\r
+    }\r
+}\r
+\r
+\r
+/***************************** ReceiveComPortBlock **********************/\r
+/**  Receives a buffer from the open com port. Returns all the characters\r
+ready (waits for up to 'n' milliseconds before accepting that no more\r
+characters are ready) or when the buffer is full. 'n' is system dependant,\r
+see SerialTimeout routines.\r
+\param [out] answer buffer to hold the bytes read from the serial port.\r
+\param [in] max_size the size of buffer pointed to by answer.\r
+\param [out] real_size pointer to a long that returns the amout of the\r
+buffer that is actually used.\r
+*/\r
+static void ReceiveComPortBlock(ISP_ENVIRONMENT *IspEnvironment,\r
+                                          void *answer, unsigned long max_size,\r
+                                          unsigned long *real_size)\r
+{\r
+    char tmp_string[32];\r
+\r
+#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+\r
+    if (IspEnvironment->HalfDuplex == 0)\r
+        ReadFile(IspEnvironment->hCom, answer, max_size, real_size, NULL);\r
+    else\r
+    {\r
+        *real_size = strlen (pRxTmpBuf);\r
+        if (*real_size)\r
+        {\r
+            if (max_size >= *real_size)\r
+            {\r
+                strncpy((char*) answer, pRxTmpBuf, *real_size);\r
+                RxTmpBuf[0] = 0;\r
+                pRxTmpBuf = RxTmpBuf;\r
+            }\r
+            else\r
+            {\r
+                strncpy((char*) answer, pRxTmpBuf, max_size);\r
+                *real_size = max_size;\r
+                pRxTmpBuf += max_size;\r
+            }\r
+        }\r
+        else\r
+            ReadFile(IspEnvironment->hCom, answer, max_size, real_size, NULL);\r
+    }\r
+\r
+#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+\r
+#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21\r
+\r
+    *real_size = read(IspEnvironment->fdCom, answer, max_size);\r
+\r
+#endif // defined COMPILE_FOR_LINUX\r
+\r
+    sprintf(tmp_string, "Read(Length=%ld): ", (*real_size));\r
+    DumpString(5, answer, (*real_size), tmp_string);\r
+\r
+    if (*real_size == 0)\r
+    {\r
+        SerialTimeoutTick(IspEnvironment);\r
+    }\r
+}\r
+\r
+\r
+/***************************** SerialTimeoutSet *************************/\r
+/**  Sets (or resets) the timeout to the timout period requested.  Starts\r
+counting to this period.  This timeout support is a little odd in that the\r
+timeout specifies the accumulated deadtime waiting to read not the total\r
+time waiting to read. They should be close enought to the same for this\r
+use. Used by the serial input routines, the actual counting takes place in\r
+ReceiveComPortBlock.\r
+\param [in] timeout_milliseconds the time in milliseconds to use for\r
+timeout.  Note that just because it is set in milliseconds doesn't mean\r
+that the granularity is that fine.  In many cases (particularly Linux) it\r
+will be coarser.\r
+*/\r
+static void SerialTimeoutSet(ISP_ENVIRONMENT *IspEnvironment, unsigned timeout_milliseconds)\r
+{\r
+#if defined COMPILE_FOR_LINUX\r
+    IspEnvironment->serial_timeout_count = timeout_milliseconds / 100;\r
+#elif defined COMPILE_FOR_LPC21\r
+    IspEnvironment->serial_timeout_count = timeout_milliseconds * 200;\r
+#else\r
+    IspEnvironment->serial_timeout_count = timeout_milliseconds;\r
+#endif\r
+}\r
+\r
+\r
+\r
+/***************************** SerialTimeoutCheck ***********************/\r
+/**  Check to see if the serial timeout timer has run down.\r
+\retval 1 if timer has run out.\r
+\retval 0 if timer still has time left.\r
+*/\r
+static int SerialTimeoutCheck(ISP_ENVIRONMENT *IspEnvironment)\r
+{\r
+    if (IspEnvironment->serial_timeout_count == 0)\r
+    {\r
+        return 1;\r
+    }\r
+    return 0;\r
+}\r
+\r
+\r
+#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN\r
+/***************************** getch ************************************/\r
+/** Replacement for the common dos function of the same name. Reads a\r
+single unbuffered character from the 'keyboard'.\r
+\return The character read from the keyboard.\r
+*/\r
+int getch(void)\r
+{\r
+    char ch;\r
+\r
+    /* Read in one character */\r
+    read(0,&ch,1);\r
+\r
+    return ch;\r
+}\r
+#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN\r
+\r
+#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN\r
+/***************************** kbhit ************************************/\r
+/** Replacement for the common dos function of the same name. Indicates if\r
+there are characters to be read from the console.\r
+\retval 0 No characters ready.\r
+\retval 1 Characters from the console ready to be read.\r
+*/\r
+int kbhit(void)\r
+{\r
+    /* return 0 for no key pressed, 1 for key pressed */\r
+    int return_value = 0;\r
+\r
+    /* time struct for the select() function, to only wait a little while */\r
+    struct timeval select_time;\r
+    /* file descriptor variable for the select() call */\r
+    fd_set readset;\r
+\r
+    /* we're only interested in STDIN */\r
+    FD_ZERO(&readset);\r
+    FD_SET(STDIN_FILENO, &readset);\r
+\r
+    /* how long to block for - this must be > 0.0, but could be changed\r
+    to some other setting. 10-18msec seems to work well and only\r
+    minimally load the system (0% CPU loading) */\r
+    select_time.tv_sec = 0;\r
+    select_time.tv_usec = 10;\r
+\r
+    /* is there a keystroke there? */\r
+    if (select(1, &readset, NULL, NULL, &select_time))\r
+    {\r
+        /* yes, remember it */\r
+        return_value = 1;\r
+    }\r
+\r
+\r
+    /* return with what we found out */\r
+    return return_value;\r
+}\r
+struct termios keyboard_origtty;\r
+#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN\r
+\r
+\r
+/***************************** PrepareKeyboardTtySettings ***************/\r
+/** Set the keyboard tty to be able to check for new characters via kbhit\r
+getting them via getch\r
+*/\r
+\r
+void PrepareKeyboardTtySettings(void)\r
+{\r
+#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN\r
+    /* store the current tty settings */\r
+    if (!tcgetattr(0, &keyboard_origtty))\r
+    {\r
+        struct termios tty;\r
+        /* start with the current settings */\r
+        tty = keyboard_origtty;\r
+        /* make modifications to put it in raw mode, turn off echo */\r
+        tty.c_lflag &= ~ICANON;\r
+        tty.c_lflag &= ~ECHO;\r
+        tty.c_lflag &= ~ISIG;\r
+        tty.c_cc[VMIN] = 1;\r
+        tty.c_cc[VTIME] = 0;\r
+\r
+        /* put the settings into effect */\r
+        tcsetattr(0, TCSADRAIN, &tty);\r
+    }\r
+#endif\r
+}\r
+\r
+\r
+/***************************** ResetKeyboardTtySettings *****************/\r
+/** Reset the keyboard tty to original settings\r
+*/\r
+void ResetKeyboardTtySettings(void)\r
+{\r
+#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN\r
+    /* reset the tty to its original settings */\r
+    tcsetattr(0, TCSADRAIN, &keyboard_origtty);\r
+#endif\r
+}\r
+\r
+\r
+#if !defined COMPILE_FOR_LPC21\r
+/***************************** ControlModemLines ************************/\r
+/**  Controls the modem lines to place the microcontroller into various\r
+states during the programming process.\r
+error rather abruptly terminates the program.\r
+\param [in] DTR the state to set the DTR line to.\r
+\param [in] RTS the state to set the RTS line to.\r
+*/\r
+static void ControlModemLines(ISP_ENVIRONMENT *IspEnvironment, unsigned char DTR, unsigned char RTS)\r
+{\r
+    //handle wether to invert the control lines:\r
+    DTR ^= IspEnvironment->ControlLinesInverted;\r
+    RTS ^= IspEnvironment->ControlLinesInverted;\r
+\r
+    //handle wether to swap the control lines\r
+    if (IspEnvironment->ControlLinesSwapped)\r
+    {\r
+        unsigned char tempRTS;\r
+        tempRTS = RTS;\r
+        RTS = DTR;\r
+        DTR = tempRTS;\r
+    }\r
+\r
+#if defined COMPILE_FOR_LINUX\r
+    int status;\r
+\r
+    if (ioctl(IspEnvironment->fdCom, TIOCMGET, &status) == 0)\r
+    {\r
+        DebugPrintf(3, "ioctl get ok, status = %X\n",status);\r
+    }\r
+    else\r
+    {\r
+        DebugPrintf(1, "ioctl get failed\n");\r
+    }\r
+\r
+    if (DTR) status |=  TIOCM_DTR;\r
+    else    status &= ~TIOCM_DTR;\r
+\r
+    if (RTS) status |=  TIOCM_RTS;\r
+    else    status &= ~TIOCM_RTS;\r
+\r
+    if (ioctl(IspEnvironment->fdCom, TIOCMSET, &status) == 0)\r
+    {\r
+        DebugPrintf(3, "ioctl set ok, status = %X\n",status);\r
+    }\r
+    else\r
+    {\r
+        DebugPrintf(1, "ioctl set failed\n");\r
+    }\r
+\r
+    if (ioctl(IspEnvironment->fdCom, TIOCMGET, &status) == 0)\r
+    {\r
+        DebugPrintf(3, "ioctl get ok, status = %X\n",status);\r
+    }\r
+    else\r
+    {\r
+        DebugPrintf(1, "ioctl get failed\n");\r
+    }\r
+\r
+#endif // defined COMPILE_FOR_LINUX\r
+#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+\r
+    if (DTR) EscapeCommFunction(IspEnvironment->hCom, SETDTR);\r
+    else    EscapeCommFunction(IspEnvironment->hCom, CLRDTR);\r
+\r
+    if (RTS) EscapeCommFunction(IspEnvironment->hCom, SETRTS);\r
+    else    EscapeCommFunction(IspEnvironment->hCom, CLRRTS);\r
+\r
+#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+\r
+#if defined COMPILE_FOR_LPC21\r
+    LPC_RESET(DTR);\r
+    LPC_BSL(RTS);\r
+#endif\r
+\r
+    DebugPrintf(3, "DTR (%d), RTS (%d)\n", DTR, RTS);\r
+}\r
+\r
+\r
+/***************************** ClearSerialPortBuffers********************/\r
+/**  Empty the serial port buffers.  Cleans things to a known state.\r
+*/\r
+void ClearSerialPortBuffers(ISP_ENVIRONMENT *IspEnvironment)\r
+{\r
+#if defined COMPILE_FOR_LINUX\r
+    /* variables to store the current tty state, create a new one */\r
+    struct termios origtty, tty;\r
+\r
+    /* store the current tty settings */\r
+    tcgetattr(IspEnvironment->fdCom, &origtty);\r
+\r
+    // Flush input and output buffers\r
+    tty=origtty;\r
+    tcsetattr(IspEnvironment->fdCom, TCSAFLUSH, &tty);\r
+\r
+    /* reset the tty to its original settings */\r
+    tcsetattr(IspEnvironment->fdCom, TCSADRAIN, &origtty);\r
+#endif // defined COMPILE_FOR_LINUX\r
+#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+    PurgeComm(IspEnvironment->hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);\r
+#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN\r
+}\r
+#endif // !defined COMPILE_FOR_LPC21\r
+\r
+\r
+#if defined COMPILE_FOR_LINUX\r
+/***************************** Sleep ************************************/\r
+/**  Provide linux replacement for windows function.\r
+\param [in] Milliseconds the time to wait for in milliseconds.\r
+*/\r
+void Sleep(unsigned long MilliSeconds)\r
+{\r
+    usleep(MilliSeconds*1000); //convert to microseconds\r
+}\r
+#endif // defined COMPILE_FOR_LINUX\r
+\r
+\r
+#if defined COMPILE_FOR_LPC21\r
+/**  Provide linux replacement for windows function.\r
+\note I implement that one in my private header file today...\r
+\param [in] Milliseconds the time to wait for in milliseconds.\r
+*/\r
+/*static void Sleep(unsigned long MilliSeconds)\r
+{\r
+#   warning Sleep function not implemented\r
+}\r
+*/\r
+#endif // defined COMPILE_FOR_LPC21\r
+\r
+\r
+\r
+/************* Applicationlayer.                                        */\r
+\r
+#if !defined COMPILE_FOR_LPC21\r
+/***************************** DebugPrintf ******************************/\r
+/**  Prints a debug string depending the current debug level. The higher\r
+the debug level the more detail that will be printed.  Each print\r
+has an associated level, the higher the level the more detailed the\r
+debugging information being sent.\r
+\param [in] level the debug level of the print statement, if the level\r
+is less than or equal to the current debug level it will be printed.\r
+\param [in] fmt a standard printf style format string.\r
+\param [in] ... the usual printf parameters.\r
+*/\r
+#if !defined INTEGRATED_IN_WIN_APP\r
+void DebugPrintf(int level, const char *fmt, ...)\r
+{\r
+    va_list ap;\r
+\r
+    if (level <= debug_level)\r
+    {\r
+        char pTemp[2000];\r
+        va_start(ap, fmt);\r
+        //vprintf(fmt, ap);\r
+        vsprintf(pTemp, fmt, ap);\r
+        TRACE(pTemp);\r
+        va_end(ap);\r
+        fflush(stdout);\r
+    }\r
+}\r
+#endif\r
+#endif // !defined COMPILE_FOR_LPC21\r
+\r
+\r
+/***************************** ReceiveComPort ***************************/\r
+/**  Receives a buffer from the open com port. Returns when the buffer is\r
+filled, the numer of requested linefeeds has been received or the timeout\r
+period has passed\r
+\param [in] ISPEnvironment.\r
+\param [out] Answer buffer to hold the bytes read from the serial port.\r
+\param [in] MaxSize the size of buffer pointed to by Answer.\r
+\param [out] RealSize pointer to a long that returns the amout of the\r
+buffer that is actually used.\r
+\param [in] WantedNr0x0A the maximum number of linefeeds to accept before\r
+returning.\r
+\param [in] timeOutMilliseconds the maximum amount of time to wait before\r
+reading with an incomplete buffer.\r
+*/\r
+void ReceiveComPort(ISP_ENVIRONMENT *IspEnvironment,\r
+                                    const char *Ans, unsigned long MaxSize,\r
+                                    unsigned long *RealSize, unsigned long WantedNr0x0A,\r
+                                    unsigned timeOutMilliseconds)\r
+{\r
+    unsigned long tmp_realsize;\r
+    unsigned long nr_of_0x0A = 0;\r
+    int eof = 0;\r
+    unsigned long p;\r
+    unsigned char *Answer;\r
+    char tmp_string[32];\r
+\r
+    Answer = (unsigned char*) Ans;\r
+\r
+    SerialTimeoutSet(IspEnvironment, timeOutMilliseconds);\r
+\r
+    (*RealSize) = 0;\r
+\r
+    do\r
+    {\r
+        ReceiveComPortBlock(IspEnvironment, Answer + (*RealSize), MaxSize - 1 - (*RealSize), &tmp_realsize);\r
+\r
+        if (tmp_realsize != 0)\r
+        {\r
+            for (p = (*RealSize); p < (*RealSize) + tmp_realsize; p++)\r
+            {\r
+                if (Answer[p] == 0x0a)\r
+                {\r
+                    nr_of_0x0A++;\r
+                }\r
+                else if (((signed char) Answer[p]) < 0)\r
+                {\r
+                    eof = 1;\r
+                }\r
+            }\r
+        }\r
+\r
+        (*RealSize) += tmp_realsize;\r
+\r
+    } while (((*RealSize) < MaxSize) && (SerialTimeoutCheck(IspEnvironment) == 0) && (nr_of_0x0A < WantedNr0x0A) && !eof);\r
+\r
+    Answer[(*RealSize)] = 0;\r
+\r
+    sprintf(tmp_string, "Answer(Length=%ld): ", (*RealSize));\r
+    DumpString(3, Answer, (*RealSize), tmp_string);\r
+}\r
+\r
+\r
+#if !defined COMPILE_FOR_LPC21\r
+\r
+/***************************** ReceiveComPortBlockComplete **************/\r
+/**  Receives a fixed block from the open com port. Returns when the\r
+block is completely filled or the timeout period has passed\r
+\param [out] block buffer to hold the bytes read from the serial port.\r
+\param [in] size the size of the buffer pointed to by block.\r
+\param [in] timeOut the maximum amount of time to wait before guvung up on\r
+completing the read.\r
+\return 0 if successful, non-zero otherwise.\r
+*/\r
+int ReceiveComPortBlockComplete(ISP_ENVIRONMENT *IspEnvironment,\r
+                                                    void *block, size_t size, unsigned timeout)\r
+{\r
+    unsigned long realsize = 0, read;\r
+    char *result;\r
+    char tmp_string[32];\r
+\r
+    result = (char*) block;\r
+\r
+    SerialTimeoutSet(IspEnvironment, timeout);\r
+\r
+    do\r
+    {\r
+        ReceiveComPortBlock(IspEnvironment, result + realsize, size - realsize, &read);\r
+\r
+        realsize += read;\r
+\r
+    } while ((realsize < size) && (SerialTimeoutCheck(IspEnvironment) == 0));\r
+\r
+    sprintf(tmp_string, "Answer(Length=%ld): ", realsize);\r
+    DumpString(3, result, realsize, tmp_string);\r
+\r
+    if (realsize != size)\r
+    {\r
+        return 1;\r
+    }\r
+    return 0;\r
+}\r
+\r
+/***************************** ReadArguments ****************************/\r
+/**  Reads the command line arguments and parses it for the various\r
+options. Uses the same arguments as main.  Used to separate the command\r
+line parsing from main and improve its readability.  This should also make\r
+it easier to modify the command line parsing in the future.\r
+\param [in] argc the number of arguments.\r
+\param [in] argv an array of pointers to the arguments.\r
+*/\r
+static void ReadArguments(ISP_ENVIRONMENT *IspEnvironment, unsigned int argc, char *argv[])\r
+{\r
+    unsigned int i;\r
+\r
+    if (argc >= 5)\r
+    {\r
+        for (i = 1; i < argc - 4; i++)\r
+        {\r
+            if (stricmp(argv[i], "-wipe") == 0)\r
+            {\r
+                IspEnvironment->WipeDevice = 1;\r
+                DebugPrintf(3, "Wipe entire device before writing.\n");\r
+                continue;\r
+            }\r
+\r
+            if (stricmp(argv[i], "-bin") == 0)\r
+            {\r
+                IspEnvironment->FileFormat = FORMAT_BINARY;\r
+                DebugPrintf(3, "Binary format file input.\n");\r
+                continue;\r
+            }\r
+\r
+            if (stricmp(argv[i], "-hex") == 0)\r
+            {\r
+                IspEnvironment->FileFormat = FORMAT_HEX;\r
+                DebugPrintf(3, "Hex format file input.\n");\r
+                continue;\r
+            }\r
+\r
+            if (stricmp(argv[i], "-logfile") == 0)\r
+            {\r
+                IspEnvironment->LogFile = 1;\r
+                DebugPrintf(3, "Log terminal output.\n");\r
+                continue;\r
+            }\r
+\r
+            if (stricmp(argv[i], "-detectonly") == 0)\r
+            {\r
+                IspEnvironment->DetectOnly  = 1;\r
+                IspEnvironment->ProgramChip = 0;\r
+                DebugPrintf(3, "Only detect LPC chip part id.\n");\r
+                continue;\r
+            }\r
+\r
+            if (stricmp(argv[i], "-debug") == 0)\r
+            {\r
+                debug_level = 4;\r
+                DebugPrintf(3, "Turn on debug.\n");\r
+                continue;\r
+            }\r
+\r
+            if (stricmp(argv[i], "-control") == 0)\r
+            {\r
+                IspEnvironment->ControlLines = 1;\r
+                DebugPrintf(3, "Use RTS/DTR to control target state.\n");\r
+                continue;\r
+            }\r
+\r
+            if (stricmp(argv[i], "-controlswap") == 0)\r
+            {\r
+                IspEnvironment->ControlLinesSwapped = 1;\r
+                DebugPrintf(3, "Use RTS to control reset, and DTR to control P0.14(ISP).\n");\r
+                continue;\r
+            }\r
+\r
+            if (stricmp(argv[i], "-controlinv") == 0)\r
+            {\r
+                IspEnvironment->ControlLinesInverted = 1;\r
+                DebugPrintf(3, "Invert state of RTS & DTR (0=true/assert/set, 1=false/deassert/clear).\n");\r
+                continue;\r
+            }\r
+\r
+            if (stricmp(argv[i], "-halfduplex") == 0)\r
+            {\r
+                IspEnvironment->HalfDuplex = 1;\r
+                DebugPrintf(3, "halfduplex serial communication.\n");\r
+                continue;\r
+            }\r
+\r
+            if (stricmp(argv[i], "-ADARM") == 0)\r
+            {\r
+                IspEnvironment->micro = ANALOG_DEVICES_ARM;\r
+                DebugPrintf(2, "Target: Analog Devices.\n");\r
+                continue;\r
+            }\r
+\r
+            if (stricmp(argv[i], "-PHILIPSARM") == 0)\r
+            {\r
+                IspEnvironment->micro = PHILIPS_ARM;\r
+                DebugPrintf(2, "Target: Philips.\n");\r
+                continue;\r
+            }\r
+\r
+            if (stricmp(argv[i], "-Verify") == 0)\r
+            {\r
+                IspEnvironment->Verify = 1;\r
+                DebugPrintf(2, "Verify after copy RAM to Flash.\n");\r
+                continue;\r
+            }\r
+\r
+#ifdef INTEGRATED_IN_WIN_APP\r
+            if (stricmp(argv[i], "-nosync") == 0)\r
+            {\r
+                IspEnvironment->NoSync = 1;\r
+                DebugPrintf(2, "Performing no syncing, already done.\n");\r
+                continue;\r
+            }\r
+#endif\r
+\r
+#ifdef TERMINAL_SUPPORT\r
+            if (CheckTerminalParameters(IspEnvironment, argv[i]))\r
+            {\r
+                continue;\r
+            }\r
+#endif\r
+\r
+            DebugPrintf(2, "Unknown command line option: \"%s\"\n", argv[i]);\r
+        }\r
+\r
+        IspEnvironment->input_file = argv[argc - 4];\r
+\r
+        // Newest cygwin delivers a '\x0d' at the end of argument\r
+        // when calling lpc21isp from batch file\r
+        for (i = 0; i < strlen(argv[argc - 1]) && i < (sizeof(IspEnvironment->StringOscillator) - 1) &&\r
+            argv[argc - 1][i] >= '0' && argv[argc - 1][i] <= '9'; i++)\r
+        {\r
+            IspEnvironment->StringOscillator[i] = argv[argc - 1][i];\r
+        }\r
+        IspEnvironment->StringOscillator[i] = 0;\r
+\r
+        IspEnvironment->serial_port = argv[argc - 3];\r
+        IspEnvironment->baud_rate = argv[argc - 2];\r
+    }\r
+\r
+    if (argc < 5)\r
+    {\r
+        debug_level = (debug_level < 2) ? 2 : debug_level;\r
+    }\r
+\r
+    if (argc < 5)\r
+    {\r
+        DebugPrintf(2, "\n"\r
+                       "Portable command line ISP for NXP LPC2000 family and Analog Devices ADUC 70xx\n"\r
+                       "Version " VERSION_STR " compiled for " COMPILED_FOR ": " __DATE__ ", " __TIME__ "\n"\r
+                       "Copyright (c) by Martin Maurer, 2003-2008, Email: Martin.Maurer@clibb.de\n"\r
+                       "Portions Copyright (c) by Aeolus Development 2004, www.aeolusdevelopment.com\n"\r
+                       "\n");\r
+\r
+        DebugPrintf(1, "Syntax:  lpc21isp [Options] file comport baudrate Oscillator_in_kHz\n\n"\r
+                       "Example: lpc21isp test.hex com1 115200 14746\n\n"\r
+                       "Options: -bin         for uploading binary file\n"\r
+                       "         -hex         for uploading file in intel hex format (default)\n"\r
+                       "         -term        for starting terminal after upload\n"\r
+                       "         -termonly    for starting terminal without an upload\n"\r
+                       "         -localecho   for local echo in terminal\n"\r
+                       "         -detectonly  detect only used LPC chiptype (PHILIPSARM only)\n"\r
+                       "         -debug       for creating a lot of debug infos\n"\r
+                       "         -wipe        Erase entire device before upload\n"\r
+                       "         -control     for controlling RS232 lines for easier booting\n"\r
+                       "                      (Reset = DTR, EnableBootLoader = RTS)\n"\r
+#ifdef INTEGRATED_IN_WIN_APP\r
+                       "         -nosync      Do not synchronize device via '?'\n"\r
+#endif\r
+                       "         -controlswap swap RS232 control lines\n"\r
+                       "                      (Reset = RTS, EnableBootLoader = DTR)\n"\r
+                       "         -controlinv  Invert state of RTS & DTR \n"\r
+                       "                      (0=true/assert/set, 1=false/deassert/clear).\n"\r
+                       "         -verify      Verify the data in Flash after every writes to\n"\r
+                       "                      sector. To detect errors in writing to Flash ROM\n"\r
+                       "         -logfile     for enabling logging of terminal output to lpc21isp.log\n"\r
+                       "         -halfduplex  use halfduplex serial communication (i.e. with K-Line)\n"\r
+                       "         -ADARM       for downloading to an Analog Devices\n"\r
+                       "                      ARM microcontroller ADUC70xx\n"\r
+                       "         -PHILIPSARM  for downloading to a microcontroller from\n"\r
+                       "                      Philips LPC2000 family (default)\n");\r
+\r
+        exit(1);\r
+    }\r
+\r
+    if (IspEnvironment->micro == PHILIPS_ARM)\r
+    {\r
+        // If StringOscillator is bigger than 100 MHz, there seems to be something wrong\r
+        if (strlen(IspEnvironment->StringOscillator) > 5)\r
+        {\r
+            DebugPrintf(1, "Invalid crystal frequency %s\n", IspEnvironment->StringOscillator);\r
+            exit(1);\r
+        }\r
+    }\r
+}\r
+\r
+/***************************** ResetTarget ******************************/\r
+/**  Resets the target leaving it in either download (program) mode or\r
+run mode.\r
+\param [in] mode the mode to leave the target in.\r
+*/\r
+void ResetTarget(ISP_ENVIRONMENT *IspEnvironment, TARGET_MODE mode)\r
+{\r
+    if (IspEnvironment->ControlLines)\r
+    {\r
+        switch (mode)\r
+        {\r
+        /* Reset and jump to boot loader.                       */\r
+        case PROGRAM_MODE:\r
+            ControlModemLines(IspEnvironment, 1, 1);\r
+            Sleep(100);\r
+            ClearSerialPortBuffers(IspEnvironment);\r
+            Sleep(100);\r
+            ControlModemLines(IspEnvironment, 0, 1);\r
+            //Longer delay is the Reset signal is conected to an external rest controller\r
+            Sleep(500);\r
+            // Clear the RTS line after having reset the micro\r
+            // Needed for the "GO <Address> <Mode>" ISP command to work */\r
+            ControlModemLines(IspEnvironment, 0, 0);\r
+            break;\r
+\r
+        /* Reset and start uploaded program                     */\r
+        case RUN_MODE:\r
+            ControlModemLines(IspEnvironment, 1, 0);\r
+            Sleep(100);\r
+            ClearSerialPortBuffers(IspEnvironment);\r
+            Sleep(100);\r
+            ControlModemLines(IspEnvironment, 0, 0);\r
+            Sleep(100);\r
+            break;\r
+        }\r
+    }\r
+}\r
+\r
+\r
+/***************************** Ascii2Hex ********************************/\r
+/**  Converts a hex character to its equivalent number value. In case of an\r
+error rather abruptly terminates the program.\r
+\param [in] c the hex digit to convert.\r
+\return the value of the hex digit.\r
+*/\r
+static unsigned char Ascii2Hex(unsigned char c)\r
+{\r
+    if (c >= '0' && c <= '9')\r
+    {\r
+        return (unsigned char)(c - '0');\r
+    }\r
+\r
+    if (c >= 'A' && c <= 'F')\r
+    {\r
+        return (unsigned char)(c - 'A' + 10);\r
+    }\r
+\r
+    if (c >= 'a' && c <= 'f')\r
+    {\r
+        return (unsigned char)(c - 'a' + 10);\r
+    }\r
+\r
+    DebugPrintf(1, "Wrong Hex-Nibble %c (%02X)\n", c, c);\r
+    exit(1);\r
+\r
+    return 0;  // this "return" will never be reached, but some compilers give a warning if it is not present\r
+}\r
+\r
+void ReadHexFile(ISP_ENVIRONMENT *IspEnvironment)\r
+{\r
+    LoadFile(IspEnvironment);\r
+\r
+    if (IspEnvironment->BinaryLength)\r
+    {\r
+        BINARY* FileContent = IspEnvironment->FileContent;\r
+\r
+        unsigned long  Pos;\r
+        unsigned char  RecordLength;\r
+        unsigned short RecordAddress;\r
+        unsigned long  RealAddress = 0;\r
+        unsigned char  RecordType;\r
+        unsigned char  Hexvalue;\r
+        unsigned long  StartAddress;\r
+        int            BinaryOffsetDefined = 0;\r
+        unsigned char  i;\r
+\r
+\r
+        DebugPrintf(3, "Converting file %s to binary format...\n", IspEnvironment->input_file);\r
+\r
+        Pos = 0;\r
+        while (Pos < IspEnvironment->BinaryLength)\r
+        {\r
+            if (FileContent[Pos] == '\r')\r
+            {\r
+                Pos++;\r
+                continue;\r
+            }\r
+\r
+            if (FileContent[Pos] == '\n')\r
+            {\r
+                Pos++;\r
+                continue;\r
+            }\r
+\r
+            if (FileContent[Pos] != ':')\r
+            {\r
+                DebugPrintf(1, "Missing start of record (':') wrong byte %c / %02X\n", FileContent[Pos], FileContent[Pos]);\r
+                exit(1);\r
+            }\r
+\r
+            Pos++;\r
+\r
+            RecordLength   = Ascii2Hex(FileContent[Pos++]);\r
+            RecordLength <<= 4;\r
+            RecordLength  |= Ascii2Hex(FileContent[Pos++]);\r
+\r
+            DebugPrintf(4, "RecordLength = %02X\n", RecordLength);\r
+\r
+            RecordAddress   = Ascii2Hex(FileContent[Pos++]);\r
+            RecordAddress <<= 4;\r
+            RecordAddress  |= Ascii2Hex(FileContent[Pos++]);\r
+            RecordAddress <<= 4;\r
+            RecordAddress  |= Ascii2Hex(FileContent[Pos++]);\r
+            RecordAddress <<= 4;\r
+            RecordAddress  |= Ascii2Hex(FileContent[Pos++]);\r
+\r
+            DebugPrintf(4, "RecordAddress = %04X\n", RecordAddress);\r
+\r
+            RealAddress = RealAddress - (RealAddress & 0xffff) + RecordAddress;\r
+\r
+            DebugPrintf(4, "RealAddress = %08lX\n", RealAddress);\r
+\r
+            RecordType      = Ascii2Hex(FileContent[Pos++]);\r
+            RecordType    <<= 4;\r
+            RecordType     |= Ascii2Hex(FileContent[Pos++]);\r
+\r
+            DebugPrintf(4, "RecordType = %02X\n", RecordType);\r
+\r
+            if (RecordType == 0x00)          // 00 - Data record\r
+            {\r
+                /*\r
+                * Binary Offset is defined as soon as first data record read\r
+                */\r
+\r
+                //BinaryOffsetDefined = 1;\r
+\r
+                // Memory for binary file big enough ?\r
+                while ((RealAddress + RecordLength - IspEnvironment->BinaryOffset) > IspEnvironment->BinaryMemSize)\r
+                {\r
+                    IspEnvironment->BinaryMemSize <<= 1;    // Double the size allocated !!!\r
+                    IspEnvironment->BinaryContent = (BINARY*) realloc(IspEnvironment->BinaryContent, IspEnvironment->BinaryMemSize);\r
+                }\r
+\r
+                // We need to know, what the highest address is,\r
+                // how many bytes / sectors we must flash\r
+                if (RealAddress + RecordLength - IspEnvironment->BinaryOffset > IspEnvironment->BinaryLength)\r
+                {\r
+                    IspEnvironment->BinaryLength = RealAddress + RecordLength - IspEnvironment->BinaryOffset;\r
+                    DebugPrintf(3, "Image size now: %ld\n", IspEnvironment->BinaryLength);\r
+                }\r
+\r
+                for (i = 0; i < RecordLength; i++)\r
+                {\r
+                    Hexvalue        = Ascii2Hex(FileContent[Pos++]);\r
+                    Hexvalue      <<= 4;\r
+                    Hexvalue       |= Ascii2Hex(FileContent[Pos++]);\r
+                    IspEnvironment->BinaryContent[RealAddress + i - IspEnvironment->BinaryOffset] = Hexvalue;\r
+                }\r
+            }\r
+            else if (RecordType == 0x01)     // 01 - End of file record\r
+            {\r
+                break;\r
+            }\r
+            else if (RecordType == 0x02)     // 02 - Extended segment address record\r
+            {\r
+                for (i = 0; i < RecordLength * 2; i++)   // double amount of nibbles\r
+                {\r
+                    RealAddress <<= 4;\r
+                    if (i == 0)\r
+                    {\r
+                        RealAddress  = Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                    else\r
+                    {\r
+                        RealAddress |= Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                }\r
+                RealAddress <<= 4;\r
+            }\r
+            else if (RecordType == 0x03)     // 03 - Start segment address record\r
+            {\r
+                for (i = 0; i < RecordLength * 2; i++)   // double amount of nibbles\r
+                {\r
+                    RealAddress <<= 4;\r
+                    if (i == 0)\r
+                    {\r
+                        RealAddress  = Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                    else\r
+                    {\r
+                        RealAddress |= Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                }\r
+                RealAddress <<= 8;\r
+            }\r
+            else if (RecordType == 0x04)     // 04 - Extended linear address record, used by IAR\r
+            {\r
+                for (i = 0; i < RecordLength * 2; i++)   // double amount of nibbles\r
+                {\r
+                    RealAddress <<= 4;\r
+                    if (i == 0)\r
+                    {\r
+                        RealAddress  = Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                    else\r
+                    {\r
+                        RealAddress |= Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                }\r
+                RealAddress <<= 16;\r
+                if (!BinaryOffsetDefined)\r
+                {\r
+                    // set startaddress of BinaryContent\r
+                    // use of LPC_FLASHMASK to allow a memory range, not taking the first\r
+                    // [04] record as actual start-address.\r
+                    IspEnvironment->BinaryOffset = RealAddress & LPC_FLASHMASK;\r
+                }\r
+                else\r
+                {\r
+                    if ((RealAddress & LPC_FLASHMASK) != IspEnvironment->BinaryOffset)\r
+                    {\r
+                        DebugPrintf(1, "New Extended Linear Address Record [04] out of memory range\n"\r
+                                       "Current Memory starts at: 0x%08X, new Address is: 0x%08X",\r
+                                       IspEnvironment->BinaryOffset, RealAddress);\r
+                        exit(1);\r
+                    }\r
+                }\r
+            }\r
+            else if (RecordType == 0x05)     // 05 - Start linear address record\r
+            {\r
+                StartAddress = 0;\r
+                for (i = 0; i < RecordLength * 2; i++)   // double amount of nibbles\r
+                {\r
+                    StartAddress <<= 4;\r
+                    if (i == 0)\r
+                    {\r
+                        StartAddress  = Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                    else\r
+                    {\r
+                        StartAddress |= Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                }\r
+                DebugPrintf(1,"Start Address = 0x%8X\n", StartAddress);\r
+                IspEnvironment->StartAddress = StartAddress;\r
+            }\r
+\r
+            while (FileContent[Pos++] != 0x0a)      // Search till line end\r
+            {\r
+            }\r
+        }\r
+\r
+        DebugPrintf(2, "\tconverted to binary format...\n");\r
+\r
+        // When debugging is switched on, output result of conversion to file debugout.bin\r
+        if (debug_level >= 4)\r
+        {\r
+            int fdout;\r
+            fdout = open("debugout.bin", O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0777);\r
+            write(fdout, IspEnvironment->BinaryContent, IspEnvironment->BinaryLength);\r
+            close(fdout);\r
+        }\r
+    }\r
+}\r
+\r
+\r
+/***************************** LoadFile *********************************/\r
+/**  Loads the requested file to download into memory.\r
+\param [in] IspEnvironment  structure containing input filename\r
+*/\r
+static void LoadFile(ISP_ENVIRONMENT *IspEnvironment)\r
+{\r
+    int            fd;\r
+    int            i;\r
+    int            BinaryOffsetDefined;\r
+    unsigned long  Pos;\r
+    unsigned long  FileLength;\r
+    BINARY        *FileContent;              /**< Used to store the content of a hex */\r
+                                             /*   file before converting to binary.  */\r
+    unsigned long  BinaryMemSize;\r
+\r
+    fd = open(IspEnvironment->input_file, O_RDONLY | O_BINARY);\r
+    if (fd == -1)\r
+    {\r
+        DebugPrintf(1, "Can't open file %s\n", IspEnvironment->input_file);\r
+        exit(1);\r
+    }\r
+\r
+    FileLength = lseek(fd, 0L, 2);      // Get file size\r
+\r
+    if (FileLength == (size_t)-1)\r
+    {\r
+        DebugPrintf(1, "\nFileLength = -1 !?!\n");\r
+        exit(1);\r
+    }\r
+\r
+    lseek(fd, 0L, 0);\r
+\r
+    FileContent = (BINARY*) malloc(FileLength);\r
+\r
+    BinaryMemSize = FileLength * 2;\r
+\r
+    IspEnvironment->BinaryLength = 0;   /* Increase length as needed.       */\r
+    IspEnvironment->BinaryOffset = 0;\r
+    IspEnvironment->StartAddress = 0;\r
+    BinaryOffsetDefined = 0;\r
+\r
+    IspEnvironment->BinaryContent = (BINARY*) malloc(BinaryMemSize);\r
+\r
+    read(fd, FileContent, FileLength);\r
+\r
+    close(fd);\r
+\r
+    DebugPrintf(2, "File %s:\n\tloaded...\n", IspEnvironment->input_file);\r
+\r
+    // Intel-Hex -> Binary Conversion\r
+\r
+    if (IspEnvironment->FileFormat == FORMAT_HEX)\r
+    {\r
+        unsigned char  RecordLength;\r
+        unsigned short RecordAddress;\r
+        unsigned long  RealAddress = 0;\r
+        unsigned char  RecordType;\r
+        unsigned char  Hexvalue;\r
+        unsigned long  StartAddress;\r
+\r
+        DebugPrintf(3, "Converting file %s to binary format...\n", IspEnvironment->input_file);\r
+\r
+        Pos = 0;\r
+        while (Pos < FileLength)\r
+        {\r
+            if (FileContent[Pos] == '\r')\r
+            {\r
+                Pos++;\r
+                continue;\r
+            }\r
+\r
+            if (FileContent[Pos] == '\n')\r
+            {\r
+                Pos++;\r
+                continue;\r
+            }\r
+\r
+            if (FileContent[Pos] != ':')\r
+            {\r
+                DebugPrintf(1, "Missing start of record (':') wrong byte %c / %02X\n", FileContent[Pos], FileContent[Pos]);\r
+                exit(1);\r
+            }\r
+\r
+            Pos++;\r
+\r
+            RecordLength   = Ascii2Hex(FileContent[Pos++]);\r
+            RecordLength <<= 4;\r
+            RecordLength  |= Ascii2Hex(FileContent[Pos++]);\r
+\r
+            DebugPrintf(4, "RecordLength = %02X\n", RecordLength);\r
+\r
+            RecordAddress   = Ascii2Hex(FileContent[Pos++]);\r
+            RecordAddress <<= 4;\r
+            RecordAddress  |= Ascii2Hex(FileContent[Pos++]);\r
+            RecordAddress <<= 4;\r
+            RecordAddress  |= Ascii2Hex(FileContent[Pos++]);\r
+            RecordAddress <<= 4;\r
+            RecordAddress  |= Ascii2Hex(FileContent[Pos++]);\r
+\r
+            DebugPrintf(4, "RecordAddress = %04X\n", RecordAddress);\r
+\r
+            RealAddress = RealAddress - (RealAddress & 0xffff) + RecordAddress;\r
+\r
+            DebugPrintf(4, "RealAddress = %08lX\n", RealAddress);\r
+\r
+            RecordType      = Ascii2Hex(FileContent[Pos++]);\r
+            RecordType    <<= 4;\r
+            RecordType     |= Ascii2Hex(FileContent[Pos++]);\r
+\r
+            DebugPrintf(4, "RecordType = %02X\n", RecordType);\r
+\r
+            if (RecordType == 0x00)          // 00 - Data record\r
+            {\r
+                /*\r
+                * Binary Offset is defined as soon as first data record read\r
+                */\r
+                BinaryOffsetDefined = 1;\r
+                // Memory for binary file big enough ?\r
+                while (RealAddress + RecordLength - IspEnvironment->BinaryOffset > BinaryMemSize)\r
+                {\r
+                    BinaryMemSize <<= 1;\r
+                    IspEnvironment->BinaryContent = (BINARY*) realloc(IspEnvironment->BinaryContent, BinaryMemSize);\r
+                }\r
+\r
+                // We need to know, what the highest address is,\r
+                // how many bytes / sectors we must flash\r
+                if (RealAddress + RecordLength - IspEnvironment->BinaryOffset > IspEnvironment->BinaryLength)\r
+                {\r
+                    IspEnvironment->BinaryLength = RealAddress + RecordLength - IspEnvironment->BinaryOffset;\r
+                    DebugPrintf(3, "Image size now: %ld\n", IspEnvironment->BinaryLength);\r
+                }\r
+\r
+                for (i = 0; i < RecordLength; i++)\r
+                {\r
+                    Hexvalue        = Ascii2Hex(FileContent[Pos++]);\r
+                    Hexvalue      <<= 4;\r
+                    Hexvalue       |= Ascii2Hex(FileContent[Pos++]);\r
+                    IspEnvironment->BinaryContent[RealAddress + i - IspEnvironment->BinaryOffset] = Hexvalue;\r
+                }\r
+            }\r
+            else if (RecordType == 0x01)     // 01 - End of file record\r
+            {\r
+                break;\r
+            }\r
+            else if (RecordType == 0x02)     // 02 - Extended segment address record\r
+            {\r
+                for (i = 0; i < RecordLength * 2; i++)   // double amount of nibbles\r
+                {\r
+                    RealAddress <<= 4;\r
+                    if (i == 0)\r
+                    {\r
+                        RealAddress  = Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                    else\r
+                    {\r
+                        RealAddress |= Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                }\r
+                RealAddress <<= 4;\r
+            }\r
+            else if (RecordType == 0x03)     // 03 - Start segment address record\r
+            {\r
+                for (i = 0; i < RecordLength * 2; i++)   // double amount of nibbles\r
+                {\r
+                    RealAddress <<= 4;\r
+                    if (i == 0)\r
+                    {\r
+                        RealAddress  = Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                    else\r
+                    {\r
+                        RealAddress |= Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                }\r
+                RealAddress <<= 8;\r
+            }\r
+            else if (RecordType == 0x04)     // 04 - Extended linear address record, used by IAR\r
+            {\r
+                for (i = 0; i < RecordLength * 2; i++)   // double amount of nibbles\r
+                {\r
+                    RealAddress <<= 4;\r
+                    if (i == 0)\r
+                    {\r
+                        RealAddress  = Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                    else\r
+                    {\r
+                        RealAddress |= Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                }\r
+                RealAddress <<= 16;\r
+                if (!BinaryOffsetDefined)\r
+                {\r
+                    // set startaddress of BinaryContent\r
+                    // use of LPC_FLASHMASK to allow a memory range, not taking the first\r
+                    // [04] record as actual start-address.\r
+                    IspEnvironment->BinaryOffset = RealAddress & LPC_FLASHMASK;\r
+                }\r
+                else\r
+                {\r
+                    if ((RealAddress & LPC_FLASHMASK) != IspEnvironment->BinaryOffset)\r
+                    {\r
+                        DebugPrintf(1, "New Extended Linear Address Record [04] out of memory range\n");\r
+                        DebugPrintf(1, "Current Memory starts at: 0x%08X, new Address is: 0x%08X",\r
+                            IspEnvironment->BinaryOffset, RealAddress);\r
+                        exit(1);\r
+                    }\r
+                }\r
+            }\r
+            else if (RecordType == 0x05)     // 05 - Start linear address record\r
+            {\r
+                StartAddress = 0;\r
+                for (i = 0; i < RecordLength * 2; i++)   // double amount of nibbles\r
+                {\r
+                    StartAddress <<= 4;\r
+                    if (i == 0)\r
+                    {\r
+                        StartAddress  = Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                    else\r
+                    {\r
+                        StartAddress |= Ascii2Hex(FileContent[Pos++]);\r
+                    }\r
+                }\r
+                DebugPrintf(1,"Start Address = 0x%8X\n", StartAddress);\r
+                IspEnvironment->StartAddress = StartAddress;\r
+            }\r
+\r
+            while (FileContent[Pos++] != 0x0a)      // Search till line end\r
+            {\r
+            }\r
+        }\r
+\r
+        DebugPrintf(2, "\tconverted to binary format...\n");\r
+\r
+        // When debugging is switched on, output result of conversion to file debugout.bin\r
+        if (debug_level >= 4)\r
+        {\r
+            int fdout;\r
+            fdout = open("debugout.bin", O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0777);\r
+            write(fdout, IspEnvironment->BinaryContent, IspEnvironment->BinaryLength);\r
+            close(fdout);\r
+        }\r
+    }\r
+    else\r
+    {\r
+        memcpy(IspEnvironment->BinaryContent, FileContent, FileLength);\r
+\r
+        IspEnvironment->BinaryLength = FileLength;\r
+    }\r
+\r
+    DebugPrintf(2, "\timage size : %ld\n", IspEnvironment->BinaryLength);\r
+\r
+    // check length to flash for correct alignment, can happen with broken ld-scripts\r
+    if (IspEnvironment->BinaryLength % 4 != 0)\r
+    {\r
+        unsigned long NewBinaryLength = ((IspEnvironment->BinaryLength + 3)/4) * 4;\r
+\r
+        DebugPrintf(2, "Warning:  data not aligned to 32 bits, padded (length was %lX, now %lX)\n", IspEnvironment->BinaryLength, NewBinaryLength);\r
+\r
+        IspEnvironment->BinaryLength = NewBinaryLength;\r
+    }\r
+}\r
+#endif // !defined COMPILE_FOR_LPC21\r
+\r
+#ifndef COMPILE_FOR_LPC21\r
+int PerformActions(ISP_ENVIRONMENT *IspEnvironment)\r
+{\r
+    int downloadResult = -1;\r
+\r
+    DebugPrintf(2, "lpc21isp version " VERSION_STR "\n");\r
+\r
+    /* Download requested, read in the input file.                  */\r
+    if (IspEnvironment->ProgramChip)\r
+    {\r
+        LoadFile(IspEnvironment);\r
+    }\r
+\r
+    OpenSerialPort(IspEnvironment);   /* Open the serial port to the microcontroller. */\r
+\r
+    ResetTarget(IspEnvironment, PROGRAM_MODE);\r
+\r
+    ClearSerialPortBuffers(IspEnvironment);\r
+\r
+    /* Perform the requested download.                              */\r
+    if (IspEnvironment->ProgramChip || IspEnvironment->DetectOnly)\r
+    {\r
+        switch (IspEnvironment->micro)\r
+        {\r
+#ifdef LPC_SUPPORT\r
+        case PHILIPS_ARM:\r
+            downloadResult = PhilipsDownload(IspEnvironment);\r
+            break;\r
+#endif\r
+\r
+#ifdef AD_SUPPORT\r
+        case ANALOG_DEVICES_ARM:\r
+            downloadResult = AnalogDevicesDownload(IspEnvironment);\r
+            break;\r
+#endif\r
+        }\r
+\r
+        if (downloadResult != 0)\r
+        {\r
+            CloseSerialPort(IspEnvironment);\r
+            exit(downloadResult);\r
+        }\r
+    }\r
+\r
+    if (IspEnvironment->StartAddress == 0 || IspEnvironment->TerminalOnly)\r
+    {\r
+        /* Only reset target if startaddress = 0\r
+        * Otherwise stay with the running program as started in Download()\r
+        */\r
+        ResetTarget(IspEnvironment, RUN_MODE);\r
+    }\r
+\r
+    IspEnvironment->debug_level = 1;    /* From now on there is no more debug output !! */\r
+                                        /* Therefore switch it off...                   */\r
+\r
+#ifdef TERMINAL_SUPPORT\r
+    // Pass control to Terminal which will provide a terminal if one was asked for\r
+    // User asked for terminal emulation, provide a really dumb terminal.\r
+    Terminal(IspEnvironment);\r
+#endif\r
+\r
+    CloseSerialPort(IspEnvironment);  /*  All done, close the serial port to the      */\r
+\r
+    return 0;\r
+}\r
+#endif\r
+\r
+/***************************** main *************************************/\r
+/**  main. Everything starts from here.\r
+\param [in] argc the number of arguments.\r
+\param [in] argv an array of pointers to the arguments.\r
+*/\r
+\r
+#if !defined COMPILE_FOR_LPC21\r
+\r
+#if defined INTEGRATED_IN_WIN_APP\r
+int AppDoProgram(int argc, char *argv[])\r
+#else\r
+int main(int argc, char *argv[])\r
+#endif\r
+{\r
+    ISP_ENVIRONMENT IspEnvironment;\r
+\r
+    // Initialize debug level\r
+    debug_level = 2;\r
+\r
+    // Initialize ISP Environment\r
+    memset(&IspEnvironment, 0, sizeof(IspEnvironment));       // Clear the IspEnviroment to a known value\r
+    IspEnvironment.micro       = PHILIPS_ARM;                 // Default Micro\r
+    IspEnvironment.FileFormat  = FORMAT_HEX;                  // Default File Format\r
+    IspEnvironment.ProgramChip = TRUE;                        // Default to Programming the chip\r
+    ReadArguments(&IspEnvironment, argc, argv);               // Read and parse the command line\r
+\r
+    return PerformActions(&IspEnvironment);                   // Do as requested !\r
+}\r
+\r
+#endif // !defined COMPILE_FOR_LPC21\r
+\r
+/***************************** DumpString ******************************/\r
+/**  Prints an area of memory to stdout. Converts non-printables to hex.\r
+\param [in] level the debug level of the block to be dumped.  If this is\r
+less than or equal to the current debug level than the dump will happen\r
+otherwise this just returns.\r
+\param [in] b pointer to an area of memory.\r
+\param [in] size the length of the memory block to print.\r
+\param [in] prefix string is a pointer to a prefix string.\r
+*/\r
+void DumpString(int level, const void *b, size_t size, const char *prefix_string)\r
+{\r
+    size_t i;\r
+    const char * s = (const char*) b;\r
+    unsigned char c;\r
+\r
+    DebugPrintf(level, prefix_string);\r
+\r
+    DebugPrintf(level, "'");\r
+    for (i = 0; i < size; i++)\r
+    {\r
+        c = s[i];\r
+        if (c >= 0x20 && c <= 0x7e) /*isprint?*/\r
+        {\r
+            DebugPrintf(level, "%c", c);\r
+        }\r
+        else\r
+        {\r
+            DebugPrintf(level, "(%02X)", c);\r
+        }\r
+    }\r
+    DebugPrintf(level, "'\n");\r
+}\r
+\r
+#if !defined COMPILE_FOR_LPC21\r
+int lpctest(char* FileName)\r
+{\r
+    ISP_ENVIRONMENT IspEnvironment;\r
+\r
+    // Initialize debug level\r
+    debug_level = 2;\r
+\r
+    // Initialize ISP Environment\r
+    memset(&IspEnvironment, 0, sizeof(IspEnvironment));        // Clear the IspEnviroment to a known value\r
+    IspEnvironment.micro        = PHILIPS_ARM;                 // Default Micro\r
+    IspEnvironment.FileFormat   = FORMAT_HEX;                  // Default File Format\r
+    IspEnvironment.ProgramChip  = TRUE;                        // Default to Programming the chip\r
+    IspEnvironment.input_file   = FileName;\r
+    IspEnvironment.ControlLines = TRUE;\r
+    IspEnvironment.serial_port  = "COM2";\r
+    IspEnvironment.baud_rate    = "19200";\r
+    strcpy(IspEnvironment.StringOscillator, "25000");\r
+\r
+    return PerformActions(&IspEnvironment);                    // Do as requested !\r
+}\r
+#endif\r