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

📄 lpc21isp.c

📁 NXP公司的arm系列芯片LPCXXXX的ISP烧写程序。
💻 C
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************

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-2007, All rights reserved
Portions Copyright (c) by Aeolus Development 2004 http://www.aeolusdevelopment.com
*/

#define VERSION_STR "1.48"

/*

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 !)
 1.28  2005-07-27  Anders Rosvall / Embedded Artists AB:
                   Changed the reset timeout to 500 ms when entering the bootloader.
                   Some external reset controllers have quite long timeout periods,
                   so extening the timeout delay would be a good thing.
 1.29  2005-09-14  Rob Jansen:
                   Added functionality to download to RAM and run from there.
                   In LoadFile() added record types 04 (Extended Linear Address Record)
                   and 05 (Start Linear Address Record), added address offset
                   (IspEnvironment->BinaryOffset) and start address (...->StartAddress).
                   Changed PhilipsDownload to skip all Flash prepare/erase/copy commands.
                   Note: Tested with VC7 only
1.30   2005-10-04  Rob Jansen:
                   - forgot to change the version string in 1.29
                   - Wrong text in LoadFile corrected (printed text mentions record type 05,
                     this should be 04
                   - Changed LoadFile to accept multiple record types 04
                   - Changed LoadFile to check on memory size, will not load more than x MB
                     if linear extended address records are used
1.31   2005-11-13  Martin Maurer: Thanks to Frank Gutmann
                   Updated number of sectors in device table
                   for LPC2194, LPC2292 and LPC2294
1.32   2005-12-02  Martin Maurer: Corrected missing control of RTS/DTR
                   in case user selected -termonly and -control
                   Small correction (typo in debug)
1.33   2006-10-01  Jean-Marc Koller:
                   Added support for MacOS X (difference on how to set termios baudrate).
1.34   2006-10-01  Cyril Holweck:
                   Made it compile again for lpc21isp
                   Added const keyword to constant variables to make it better
                   code for embeded target. (decrease RAM usage)
                   Replaced all regular call to printf() by DebugPrintf()
                   Removed call to scanf() (not much usefull and cost a lot to my target)
1.35   2006-22-01  Cyril Holweck
                   Added feature for LPC21: will start downloading at Sector 1 and upward,
                   to finish with Sector 0, the one containing the checksum controling BSL entry
1.36   2006-25-01  Cyril Holweck
                   PhilipsDownload() will now return a unique error code for each error
1.37   2006-10-03  Jeroen Domburg
                   Added LPC2103 (and only the 2103, I can't find the IDs for 2101/2102)
                   Corrected a loop which occured if the program completely fits in sector 0
1.38   2007-01-05  Ray Molenkamp
                   Added feature for LPC21: Wipe entire device before programming to enable
                   reflashing of chips with the lpc codeprotection feature enabled.
1.39   2007-01-12  Martin Maurer
                   Added initial support for new processors LPC23xx and LPC24xx
1.40   2007-01-22  Martin Maurer
                   Correction of chip id of LPC2458
1.41   2007-01-28  Jean-Marc Koller
                   Modified Terminal() to disable ECHO with termios only once, instead of
                   modifying and restoring termios in each getch and kbhit call (which caused
                   a strange echo behaviour in MacOS X).
1.42   2007-01-28  Rob Probin
                   Added -localecho command to allow local echoing in terminal mode for use
                   where target does not echo back keystrokes.
1.43   2007-01-29  Martin Maurer
                   Moved keyboard handling routines to own subroutines,
                   so they can be used during aborting synchronisation.
                   Newest cygwin made problems, StringOscillator always contained '\0x0d'
                   at the end, when calling lpc21isp from batch file
1.44   2007-02-23  Yang Yang
                   Added feature for LPC21: Verify the data in Flash after every writes
                   to sector. To detect errors in writing to Flash ROM.
1.45   2007-02-25  Martin Maurer
                   Replace printf syntax of DumpString by a simple pointer to a string
                   printf syntax is a nice thing, but it is not working :-(
                   and therefore makes debugging much more difficult...
                   Moved VERSION_STR to top of file to avoid possible cosmetical errors
1.46   2007-02-25  Martin Maurer
                   Again corrected debug output: should solve output of
                   (FFFFFFB5) instead of (B5)
1.47   2007-02-27  Robert Adsett
                   Raised timeout on AD send packet function.
1.48   2007-04-20  Martin Maurer
                   Thanks to Josef Wolf for preventing to overwrite over end of array

******************************************************************************
******* Don't forget to update the version string on top of file *************
******************************************************************************/

/* LPC_RAMSTART, LPC_RAMBASE
 *
 * Used in PhilipsDownload() to decide whether to Flash code or just place in in RAM
 * (works for .hex files only)
 *
 * LPC_RAMSTART - the Physical start address of the SRAM
 * LPC_RAMBASE  - the base address where downloading starts.
 *                Note that any code in the .hex file that resides in 0x4000,0000 ~ 0x4000,0200
 *                will _not_ be written to the LPCs SRAM.
 *                This is due to the fact that 0x4000,0040 - 0x4000,0200 is used by the bootrom.
 *                Any interrupt vectors must be copied to 0x4000,0000 and remapped to 0x0000,0000
 *                by the startup code.
 */
#define LPC_RAMSTART    0x40000000L
#define LPC_RAMBASE     0x40000200L

/* LPC_FLASHMASK
 *
 * LPC_FLASHMASK - bitmask to define the maximum size of the Filesize to download.
 *                 LoadFile() will check any new segment address record (03) or extended linear
 *                 address record (04) to see if the addressed 64 kByte data block still falls
 *                 in the max. flash size.
 *                 LoadFile() will not load any files that are larger than this size.
 */
#define LPC_FLASHMASK  0xFFC00000 /* 22 bits = 4 MB */


#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
#elif defined(__APPLE__)
    #define COMPILE_FOR_LINUX
    #define COMPILED_FOR ("Apple MacOS X")
#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>  // if using libc serial port communication
#include "lpc21isp.h"   // if using propriatory serial port communication (customize attached lpc21isp.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


/* Return values used by PhilipsDownload(): reserving all values from 0x1000 to 0x1FFF */

#define NO_ANSWER_WDT       0x1000
#define NO_ANSWER_QM        0x1001
#define NO_ANSWER_SYNC      0x1002
#define NO_ANSWER_OSC       0x1003
#define NO_ANSWER_RBV       0x1004
#define NO_ANSWER_RPID      0x1005
#define ERROR_WRITE_DATA    0x1006
#define ERROR_WRITE_CRC     0x1007
#define ERROR_WRITE_CRC2    0x1008
#define PROGRAM_TOO_LARGE   0x1009

#define USER_ABORT_SYNC     0x100A   /* User aborted synchronisation process */

#define UNLOCK_ERROR        0x1100   /* return value is 0x1100 + philips ISP returned value (0 to 255) */
#define WRONG_ANSWER_PREP   0x1200   /* return value is 0x1200 + philips ISP returned value (0 to 255) */
#define WRONG_ANSWER_ERAS   0x1300   /* return value is 0x1300 + philips ISP returned value (0 to 255) */
#define WRONG_ANSWER_WRIT   0x1400   /* return value is 0x1400 + philips ISP returned value (0 to 255) */
#define WRONG_ANSWER_PREP2  0x1500   /* return value is 0x1500 + philips ISP returned value (0 to 255) */
#define WRONG_ANSWER_COPY   0x1600   /* return value is 0x1600 + philips ISP returned value (0 to 255) */
#define FAILED_RUN          0x1700   /* return value is 0x1700 + philips ISP returned value (0 to 255) */





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

⌨️ 快捷键说明

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