⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lpc21isp.c

📁 LPC2000系列化的ISP下载源程序
💻 C
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************Project:           Portable command line ISP for Philips LPC2000 family                   and Analog Devices ADUC70xxFilename:          lpc21isp.cCompiler:          Microsoft VC 6/7, GCC Cygwin, GCC LinuxAutor:             Martin Maurer (Martin.Maurer@clibb.de)Copyright:         (c) Martin Maurer 2003, 2004, All rights reservedPortions Copyright (c) by Aeolus Development 2004 http://www.aeolusdevelopment.comVersion:           1.24Change-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)******************************************************************************/#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_LINUXtypedef unsigned char BINARY;   /**< data type used for microcontroller                                *  memory image.                        *//*debug levels0 - very quiet          - Nothing gets printed at this level1 - quiet               - Only error messages should be printed2 - indicate progress   - Add progress messages3 - first level debug   - Major level tracing4 - second level debug  - Add detailed debugging5 - 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_LPC21static void ClearSerialPortBuffers(ISP_ENVIRONMENT *IspEnvironment);static void ControlModemLines(ISP_ENVIRONMENT *IspEnvironment, unsigned char DTR, unsigned char RTS);static unsigned char Ascii2Hex(unsigned char c);#endifstatic 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 },  { 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 },  { 0x00000000, 2131,  32,  8,  8, 4096, SectorTable_213x },  { 0x00000000, 2132,  64, 16,  9, 4096, SectorTable_213x },  { 0x0002FF25, 2138, 512, 32, 27, 4096, SectorTable_213x },};/************* Portability layer. Serial and console I/O differences    *//* are taken care of here.                                              */#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWINstatic int kbhit( void);static int getch( void);#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN#if defined COMPILE_FOR_LINUXstatic void Sleep(unsigned long MilliSeconds);#endif // defined COMPILE_FOR_LINUX#if !defined COMPILE_FOR_LPC21static 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;    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 );    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);    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))    {        case 1152000: IspEnvironment->newtio.c_cflag |= B1152000; break;        case  576000: IspEnvironment->newtio.c_cflag |=  B576000; break;        case  230400: IspEnvironment->newtio.c_cflag |=  B230400; break;        case  115200: IspEnvironment->newtio.c_cflag |=  B115200; break;        case   57600: IspEnvironment->newtio.c_cflag |=   B57600; break;        case   38400: IspEnvironment->newtio.c_cflag |=   B38400; break;        case   19200: IspEnvironment->newtio.c_cflag |=   B19200; break;        case    9600: IspEnvironment->newtio.c_cflag |=    B9600; break;        default:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -