| 1 | | /***************************************************************************** |
| 2 | | ** This data and information is proprietary to, and a valuable trade |
| 3 | | ** secret off, Q-Free Products A/S. It is given in confidence by |
| 4 | | ** Q-Free Products A/S and may only be used as permitted under the |
| 5 | | ** license agreement under which it has been distributed, and in no |
| 6 | | ** other way. |
| 7 | | ** |
| 8 | | ** Copyright (C) 2004 Q-Free Products A/S. All rights reserved. |
| 9 | | ****************************************************************************/ |
| 10 | | |
| 11 | | /** \file |
| 12 | | * |
| 13 | | * \brief define a function to Upload Binary according to LPC2114 bootloader |
| 14 | | * protocol. |
| 15 | | * |
| 16 | | * $Author: cyrilh $ |
| 17 | | * $Date: 2006/02/20 14:02:29 $ |
| 18 | | * $Revision: 1.18 $ |
| 19 | | */ |
| 20 | | #ifndef lpc21isp_h |
| 21 | | #define lpc21isp_h |
| 22 | | |
| 23 | | |
| 24 | | |
| 25 | | #include "Types.h" |
| 26 | | #include "Uart.h" |
| 27 | | #include "QFBCNP.h" |
| 28 | | #include "lpc210x.h" |
| 29 | | #include "buff.h" |
| 30 | | #include "app.h" |
| 31 | | |
| | 1 | /****************************************************************************** |
| | 2 | |
| | 3 | Project: Portable command line ISP for Philips LPC2000 family |
| | 4 | and Analog Devices ADUC70xx |
| | 5 | |
| | 6 | Filename: lsp21isp.h |
| | 7 | |
| | 8 | Compiler: Microsoft VC 6/7, GCC Cygwin, GCC Linux, GCC ARM ELF |
| | 9 | |
| | 10 | Author: Martin Maurer (Martin.Maurer@clibb.de) |
| | 11 | |
| | 12 | Copyright: (c) Martin Maurer 2003-2008, All rights reserved |
| | 13 | Portions Copyright (c) by Aeolus Development 2004 http://www.aeolusdevelopment.com |
| | 14 | |
| | 15 | This file is part of lpc21isp. |
| | 16 | |
| | 17 | lpc21isp is free software: you can redistribute it and/or modify |
| | 18 | it under the terms of the GNU Lesser General Public License as published by |
| | 19 | the Free Software Foundation, either version 3 of the License, or |
| | 20 | any later version. |
| | 21 | |
| | 22 | lpc21isp is distributed in the hope that it will be useful, |
| | 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| | 25 | GNU Lesser General Public License for more details. |
| | 26 | |
| | 27 | You should have received a copy of the GNU Lesser General Public License |
| | 28 | and GNU General Public License along with lpc21isp. |
| | 29 | If not, see <http://www.gnu.org/licenses/>. |
| | 30 | */ |
| | 31 | |
| | 32 | // #define INTEGRATED_IN_WIN_APP |
| | 33 | |
| | 34 | #if defined(_WIN32) && !defined(__CYGWIN__) |
| | 35 | #define COMPILE_FOR_WINDOWS |
| | 36 | #define COMPILED_FOR "Windows" |
| | 37 | #elif defined(__CYGWIN__) |
| | 38 | #define COMPILE_FOR_CYGWIN |
| | 39 | #define COMPILED_FOR "Cygwin" |
| | 40 | #elif defined(__arm__) || defined(__thumb__) |
| | 41 | #define COMPILE_FOR_LPC21 |
| | 42 | #define COMPILED_FOR "ARM" |
| | 43 | #define printf iprintf |
| | 44 | #elif defined(__APPLE__) |
| | 45 | #define COMPILE_FOR_LINUX |
| | 46 | #define COMPILED_FOR "Apple MacOS X" |
| | 47 | #elif defined(__FreeBSD__) |
| | 48 | #define COMPILE_FOR_LINUX |
| | 49 | #define COMPILED_FOR "FreeBSD" |
| | 50 | #else |
| | 51 | #define COMPILE_FOR_LINUX |
| | 52 | #define COMPILED_FOR "Linux" |
| | 53 | #endif |
| | 54 | |
| | 55 | // The Required features can be enabled / disabled here |
| | 56 | #define LPC_SUPPORT |
| | 57 | |
| | 58 | #ifndef COMPILE_FOR_LPC21 |
| | 59 | #define AD_SUPPORT |
| | 60 | #define TERMINAL_SUPPORT |
| | 61 | #endif |
| | 62 | |
| | 63 | #if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
| | 64 | #include <windows.h> |
| | 65 | #include <io.h> |
| | 66 | #endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
| | 67 | |
| | 68 | #if defined COMPILE_FOR_WINDOWS |
| | 69 | #include <conio.h> |
| | 70 | //#define TRACE(x) OutputDebugString(x) |
| | 71 | #define TRACE(x) printf("%s",x) |
| | 72 | #endif // defined COMPILE_FOR_WINDOWS |
| | 73 | |
| | 74 | #if defined COMPILE_FOR_CYGWIN |
| | 75 | //#define TRACE(x) OutputDebugString(x) |
| | 76 | #define TRACE(x) printf("%s",x) |
| | 77 | #endif // defined COMPILE_FOR_WINDOWS |
| | 78 | |
| | 79 | #if defined COMPILE_FOR_LINUX |
| | 80 | #include <sys/types.h> |
| | 81 | #include <sys/stat.h> |
| | 82 | #include <stdlib.h> |
| | 83 | #include <string.h> |
| | 84 | #include <strings.h> |
| | 85 | #include <sys/ioctl.h> |
| | 86 | extern void Sleep(unsigned long MilliSeconds); |
| | 87 | #define TRACE(x) printf("%s",x) |
| | 88 | #endif // defined COMPILE_FOR_LINUX |
| | 89 | |
| | 90 | #if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN |
| | 91 | #include <termios.h> |
| | 92 | #include <unistd.h> // for read and return value of lseek |
| | 93 | #include <sys/time.h> // for select_time |
| | 94 | extern int kbhit(void); |
| | 95 | extern int getch(void); |
| | 96 | extern struct termios keyboard_origtty; |
| | 97 | #endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN |
| | 98 | |
| | 99 | #include <ctype.h> // isdigit() |
| | 100 | #include <stdio.h> // stdout |
| | 101 | #include <stdarg.h> |
| | 102 | #include <time.h> |
| | 103 | #if defined (COMPILE_FOR_LINUX) |
| | 104 | #include <sys/errno.h> |
| | 105 | #endif |
| | 106 | |
| | 107 | #if defined COMPILE_FOR_LPC21 |
| | 108 | #include <stdlib.h> |
| | 109 | #include <string.h> |
| | 110 | //#include <lpc_ioctl.h> // if using libc serial port communication |
| | 111 | #else |
| | 112 | #include <fcntl.h> |
| | 113 | #endif |
| | 114 | |
| | 115 | typedef enum |
| | 116 | { |
| | 117 | PHILIPS_ARM, |
| | 118 | ANALOG_DEVICES_ARM |
| | 119 | } TARGET; |
| | 120 | |
| | 121 | typedef enum |
| | 122 | { |
| | 123 | PROGRAM_MODE, |
| | 124 | RUN_MODE |
| | 125 | } TARGET_MODE; |
| | 126 | |
| | 127 | typedef enum |
| | 128 | { |
| | 129 | FORMAT_BINARY, |
| | 130 | FORMAT_HEX |
| | 131 | } FILE_FORMAT_TYPE; |
| | 132 | |
| | 133 | typedef unsigned char BINARY; // Data type used for microcontroller |
| | 134 | |
| | 135 | typedef struct |
| | 136 | { |
| | 137 | #if !defined COMPILE_FOR_LPC21 |
| | 138 | TARGET micro; // The type of micro that will be programmed. |
| | 139 | FILE_FORMAT_TYPE FileFormat; |
| | 140 | unsigned char ProgramChip; // Normally set |
| | 141 | |
| | 142 | int debug_level; |
| | 143 | unsigned char ControlLines; |
| | 144 | unsigned char ControlLinesSwapped; |
| | 145 | unsigned char ControlLinesInverted; |
| | 146 | unsigned char LogFile; |
| | 147 | char *input_file; // The name of the file to get input from. |
| | 148 | char *serial_port; // Name of the serial port to use to |
| | 149 | // communicate with the microcontroller. |
| | 150 | // Read from the command line. |
| | 151 | #endif // !defined COMPILE_FOR_LPC21 |
| | 152 | |
| | 153 | unsigned char TerminalOnly; // Declared here for lazyness saves ifdef's |
| | 154 | #ifdef TERMINAL_SUPPORT |
| | 155 | unsigned char TerminalAfterUpload; |
| | 156 | unsigned char LocalEcho; |
| | 157 | #endif |
| | 158 | |
| | 159 | unsigned char HalfDuplex; // Only used for LPC Programming |
| | 160 | unsigned char DetectOnly; |
| | 161 | unsigned char WipeDevice; |
| | 162 | unsigned char Verify; |
| | 163 | int DetectedDevice; /* index in LPCtypes[] array */ |
| | 164 | char *baud_rate; /**< Baud rate to use on the serial |
| | 165 | * port communicating with the |
| | 166 | * microcontroller. Read from the |
| | 167 | * command line. */ |
| | 168 | |
| | 169 | char StringOscillator[6]; /**< Holds representation of oscillator |
| | 170 | * speed from the command line. */ |
| | 171 | |
| | 172 | BINARY *FileContent; |
| | 173 | BINARY *BinaryContent; /**< Binary image of the */ |
| | 174 | /* microcontroller's memory. */ |
| | 175 | unsigned long BinaryLength; |
| | 176 | unsigned long BinaryOffset; |
| | 177 | unsigned long StartAddress; |
| | 178 | unsigned long BinaryMemSize; |
| | 179 | |
| | 180 | #if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
| | 181 | HANDLE hCom; |
| | 182 | #endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
| | 183 | |
| | 184 | #if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21 |
| | 185 | int fdCom; |
| | 186 | #endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21 |
| | 187 | |
| | 188 | #if defined COMPILE_FOR_LINUX |
| | 189 | struct termios oldtio, newtio; |
| | 190 | #endif // defined COMPILE_FOR_LINUX |
| | 191 | |
| | 192 | #ifdef INTEGRATED_IN_WIN_APP |
| | 193 | unsigned char NoSync; |
| | 194 | #endif |
| | 195 | |
| | 196 | unsigned serial_timeout_count; /**< Local used to track timeouts on serial port read. */ |
| | 197 | |
| | 198 | } ISP_ENVIRONMENT; |
| | 199 | |
| | 200 | #if defined COMPILE_FOR_LPC21 |
| | 201 | |
| | 202 | #define DebugPrintf(in, ...) |
| | 203 | |
| | 204 | #else |
| | 205 | extern int debug_level; |
| | 206 | |
| | 207 | #if defined INTEGRATED_IN_WIN_APP |
| | 208 | |
| | 209 | #define DebugPrintf AppDebugPrintf |
| | 210 | void AppDebugPrintf(int level, const char *fmt, ...); |
| | 211 | |
| | 212 | #define exit(val) AppException(val) |
| | 213 | void AppException(int exception_level); |
| | 214 | |
| | 215 | int AppDoProgram(int argc, char *argv[]); |
| | 216 | |
| | 217 | #define Exclude_kbhit 1 |
| | 218 | int AppSyncing(int trials); |
| | 219 | void AppWritten(int size); |
| | 220 | |
| | 221 | #else |
| | 222 | void DebugPrintf(int level, const char *fmt, ...); |
| | 223 | //#define DebugPrintf(level, ...) if (level <= debug_level) { TRACE( __VA_ARGS__ ); } |
| | 224 | #endif |
| | 225 | |
| | 226 | void ClearSerialPortBuffers(ISP_ENVIRONMENT *IspEnvironment); |
| | 227 | static void ControlModemLines(ISP_ENVIRONMENT *IspEnvironment, unsigned char DTR, unsigned char RTS); |
| | 228 | static unsigned char Ascii2Hex(unsigned char c); |
| | 229 | |
| | 230 | #endif |
| | 231 | |
| | 232 | |
| | 233 | #if defined COMPILE_FOR_LINUX |
| | 234 | #define stricmp strcasecmp |
| | 235 | #endif // defined COMPILE_FOR_LINUX |
| | 236 | |
| | 237 | #ifndef O_BINARY |
| | 238 | #define O_BINARY 0 |
| | 239 | #endif // O_BINARY |
| | 240 | |
| | 241 | #ifndef DWORD |
| | 242 | #define DWORD unsigned long |
| | 243 | #endif // DWORD |
| 44 | | |
| 45 | | #ifdef COMPILE_FOR_LPC21 |
| 46 | | |
| 47 | | # define my_level( level ) ( \ |
| 48 | | (level == 0)? 0: \ |
| 49 | | (level == 1)? DFLT_LEVEL/2: \ |
| 50 | | (level == 2)? DFLT_LEVEL: \ |
| 51 | | (level == 3)? 0xA0: \ |
| 52 | | (level == 4)? 0xB0: \ |
| 53 | | 0xC0 \ |
| 54 | | ) |
| 55 | | |
| 56 | | |
| 57 | | # define DebugPrintf( level, args... ) { appDebugPrintf( my_level( level ), ##args ); } |
| 58 | | # ifdef fflush |
| 59 | | # undef fflush |
| 60 | | # endif |
| 61 | | # define fflush(a) {} |
| 62 | | # ifdef time |
| 63 | | # undef time |
| 64 | | # endif |
| 65 | | # define time(a) 0 |
| 66 | | |
| 67 | | # include "timer.h" |
| 68 | | # define Sleep sleep |
| 69 | | |
| 70 | | static inline void write(int UNUSED fd, const unsigned char *s, int n) |
| 71 | | { |
| 72 | | buffSaveData( app_map.nw2serial, s, n ); |
| 73 | | } |
| 74 | | |
| 75 | | static inline int read(int UNUSED fd, char* r, int count) |
| 76 | | { |
| 77 | | resetWatchDogIfCritical(); |
| 78 | | return buffDelete( app_map.serial2nw, r, count ); |
| 79 | | } |
| 80 | | |
| 81 | | #else |
| 82 | | |
| 83 | | typedef unsigned char BINARY; /**< data type used for microcontroller |
| 84 | | * memory image. */ |
| 85 | | |
| 86 | | typedef struct |
| 87 | | { |
| 88 | | unsigned char TerminalOnly; |
| 89 | | unsigned char DetectOnly; |
| 90 | | int DetectedDevice; /** index in LPCtypes[] array */ |
| 91 | | char *baud_rate; /**< Baud rate to use on the serial |
| 92 | | * port communicating with the |
| 93 | | * microcontroller. Read from the |
| 94 | | * command line. */ |
| 95 | | |
| 96 | | char *StringOscillator; /**< Holds representation of oscillator |
| 97 | | * speed from the command line. */ |
| 98 | | BINARY *BinaryContent; /**< Binary image of the |
| 99 | | * microcontroller's memory. */ |
| 100 | | unsigned long BinaryLength; |
| 101 | | unsigned long BinaryOffset; |
| 102 | | unsigned long StartAddress; |
| 103 | | |
| 104 | | int fdCom; |
| 105 | | |
| 106 | | unsigned serial_timeout_count; /**< Local used to track |
| 107 | | * timeouts on serial port read. */ |
| 108 | | |
| 109 | | } ISP_ENVIRONMENT; |
| 110 | | |
| 111 | | |
| 112 | | extern int PhilipsDownload(ISP_ENVIRONMENT *IspEnvironment); |
| 113 | | |
| 114 | | static inline int UploadBinary( unsigned char *BinaryContent, unsigned long BinaryLength, |
| 115 | | unsigned char* StringOscillator ) |
| 116 | | { |
| 117 | | ISP_ENVIRONMENT environment = { |
| 118 | | .StringOscillator = StringOscillator, |
| 119 | | .BinaryContent = BinaryContent, |
| 120 | | .BinaryLength = BinaryLength |
| 121 | | }; |
| 122 | | return PhilipsDownload( &environment ); |
| 123 | | } |
| 124 | | #endif |
| 125 | | |
| 126 | | |
| 127 | | |
| 128 | | #endif /* !defined(lpc21isp_h) */ |
| 129 | | |
| 130 | | /* EOF lpc21isp.h */ |
| | 256 | void ReceiveComPort(ISP_ENVIRONMENT *IspEnvironment, |
| | 257 | const char *Ans, unsigned long MaxSize, |
| | 258 | unsigned long *RealSize, unsigned long WantedNr0x0A, |
| | 259 | unsigned timeOutMilliseconds); |
| | 260 | void PrepareKeyboardTtySettings(void); |
| | 261 | void ResetKeyboardTtySettings(void); |
| | 262 | void ResetTarget(ISP_ENVIRONMENT *IspEnvironment, TARGET_MODE mode); |
| | 263 | |
| | 264 | void DumpString(int level, const void *s, size_t size, const char *prefix_string); |
| | 265 | void SendComPort(ISP_ENVIRONMENT *IspEnvironment, const char *s); |
| | 266 | void SendComPortBlock(ISP_ENVIRONMENT *IspEnvironment, const void *s, size_t n); |
| | 267 | int ReceiveComPortBlockComplete(ISP_ENVIRONMENT *IspEnvironment, void *block, size_t size, unsigned timeout); |
| | 268 | void ClearSerialPortBuffers(ISP_ENVIRONMENT *IspEnvironment); |
| | 269 | |
| | 270 | #ifdef COMPILE_FOR_WINDOWS |
| | 271 | static void SerialTimeoutSet(ISP_ENVIRONMENT *IspEnvironment, unsigned timeout_milliseconds); |
| | 272 | static int SerialTimeoutCheck(ISP_ENVIRONMENT *IspEnvironment); |
| | 273 | #endif // COMPILE_FOR_WINDOWS |
| | 274 | |
| | 275 | static void LoadFile(ISP_ENVIRONMENT *IspEnvironment); |
| | 276 | |
| | 277 | int lpctest(char* FileName); |