| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
#define VERSION_STR "1.39" |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
#define LPC_RAMSTART 0x40000000L |
|---|
| 171 |
#define LPC_RAMBASE 0x40000200L |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
#define LPC_FLASHMASK 0xFFC00000 |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
|---|
| 185 |
#define COMPILE_FOR_WINDOWS |
|---|
| 186 |
#define COMPILED_FOR ("Windows") |
|---|
| 187 |
#elif defined(__CYGWIN__) |
|---|
| 188 |
#define COMPILE_FOR_CYGWIN |
|---|
| 189 |
#define COMPILED_FOR ("Cygwin") |
|---|
| 190 |
#elif defined(__arm__) || defined(__thumb__) |
|---|
| 191 |
#define COMPILE_FOR_LPC21 |
|---|
| 192 |
#define COMPILED_FOR ("ARM") |
|---|
| 193 |
#define printf iprintf |
|---|
| 194 |
#elif defined(__APPLE__) |
|---|
| 195 |
#define COMPILE_FOR_LINUX |
|---|
| 196 |
#define COMPILED_FOR ("Apple MacOS X") |
|---|
| 197 |
#else |
|---|
| 198 |
#define COMPILE_FOR_LINUX |
|---|
| 199 |
#define COMPILED_FOR ("Linux") |
|---|
| 200 |
#endif |
|---|
| 201 |
|
|---|
| 202 |
#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 203 |
#include <windows.h> |
|---|
| 204 |
#include <io.h> |
|---|
| 205 |
#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 206 |
|
|---|
| 207 |
#if defined COMPILE_FOR_WINDOWS |
|---|
| 208 |
#include <conio.h> |
|---|
| 209 |
#endif // defined COMPILE_FOR_WINDOWS |
|---|
| 210 |
|
|---|
| 211 |
#if defined COMPILE_FOR_LINUX |
|---|
| 212 |
#include <sys/types.h> |
|---|
| 213 |
#include <sys/stat.h> |
|---|
| 214 |
#include <stdlib.h> |
|---|
| 215 |
#include <string.h> |
|---|
| 216 |
#include <strings.h> |
|---|
| 217 |
#include <sys/ioctl.h> |
|---|
| 218 |
#endif // defined COMPILE_FOR_LINUX |
|---|
| 219 |
|
|---|
| 220 |
#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN |
|---|
| 221 |
#include <termios.h> |
|---|
| 222 |
#include <unistd.h> // for read and return value of lseek |
|---|
| 223 |
#include <sys/time.h> // for select_time |
|---|
| 224 |
#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN |
|---|
| 225 |
|
|---|
| 226 |
#include <ctype.h> // isdigit() |
|---|
| 227 |
#include <stdio.h> // stdout |
|---|
| 228 |
#include <stdarg.h> |
|---|
| 229 |
#include <time.h> |
|---|
| 230 |
|
|---|
| 231 |
#if defined COMPILE_FOR_LPC21 |
|---|
| 232 |
#include <stdlib.h> |
|---|
| 233 |
#include <string.h> |
|---|
| 234 |
|
|---|
| 235 |
#include "lpc21isp.h" // if using propriatory serial port communication (customize attached lpc21isp.h) |
|---|
| 236 |
#endif |
|---|
| 237 |
|
|---|
| 238 |
#if !defined COMPILE_FOR_LPC21 |
|---|
| 239 |
#include <fcntl.h> |
|---|
| 240 |
#endif |
|---|
| 241 |
|
|---|
| 242 |
#ifndef O_BINARY |
|---|
| 243 |
#define O_BINARY 0 |
|---|
| 244 |
#endif // O_BINARY |
|---|
| 245 |
|
|---|
| 246 |
#ifndef DWORD |
|---|
| 247 |
#define DWORD unsigned long |
|---|
| 248 |
#endif // DWORD |
|---|
| 249 |
|
|---|
| 250 |
#if defined COMPILE_FOR_LINUX |
|---|
| 251 |
#define stricmp strcasecmp |
|---|
| 252 |
#endif // defined COMPILE_FOR_LINUX |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
#define NO_ANSWER_WDT 0x1000 |
|---|
| 258 |
#define NO_ANSWER_QM 0x1001 |
|---|
| 259 |
#define NO_ANSWER_SYNC 0x1002 |
|---|
| 260 |
#define NO_ANSWER_OSC 0x1003 |
|---|
| 261 |
#define NO_ANSWER_RBV 0x1004 |
|---|
| 262 |
#define NO_ANSWER_RPID 0x1005 |
|---|
| 263 |
#define ERROR_WRITE_DATA 0x1006 |
|---|
| 264 |
#define ERROR_WRITE_CRC 0x1007 |
|---|
| 265 |
#define ERROR_WRITE_CRC2 0x1008 |
|---|
| 266 |
#define PROGRAM_TOO_LARGE 0x1009 |
|---|
| 267 |
|
|---|
| 268 |
#define UNLOCK_ERROR 0x1100 |
|---|
| 269 |
#define WRONG_ANSWER_PREP 0x1200 |
|---|
| 270 |
#define WRONG_ANSWER_ERAS 0x1300 |
|---|
| 271 |
#define WRONG_ANSWER_WRIT 0x1400 |
|---|
| 272 |
#define WRONG_ANSWER_PREP2 0x1500 |
|---|
| 273 |
#define WRONG_ANSWER_COPY 0x1600 |
|---|
| 274 |
#define FAILED_RUN 0x1700 |
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
typedef unsigned char BINARY; |
|---|
| 281 |
* memory image. */ |
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
typedef enum |
|---|
| 294 |
{ |
|---|
| 295 |
PHILIPS_ARM, |
|---|
| 296 |
ANALOG_DEVICES_ARM |
|---|
| 297 |
} TARGET; |
|---|
| 298 |
|
|---|
| 299 |
typedef struct |
|---|
| 300 |
{ |
|---|
| 301 |
unsigned long id; |
|---|
| 302 |
unsigned Product; |
|---|
| 303 |
unsigned FlashSize; |
|---|
| 304 |
unsigned RAMSize; |
|---|
| 305 |
unsigned FlashSectors; |
|---|
| 306 |
unsigned MaxCopySize; |
|---|
| 307 |
const int *SectorTable; |
|---|
| 308 |
} LPC_DEVICE_TYPE; |
|---|
| 309 |
|
|---|
| 310 |
typedef struct |
|---|
| 311 |
{ |
|---|
| 312 |
|
|---|
| 313 |
#if !defined COMPILE_FOR_LPC21 |
|---|
| 314 |
TARGET micro; |
|---|
| 315 |
|
|---|
| 316 |
int debug_level; |
|---|
| 317 |
unsigned char TerminalAfterUpload; |
|---|
| 318 |
unsigned char FormatHex; |
|---|
| 319 |
unsigned char ControlLines; |
|---|
| 320 |
unsigned char LogFile; |
|---|
| 321 |
char *input_file; |
|---|
| 322 |
char *serial_port; |
|---|
| 323 |
* communicate with the microcontroller. |
|---|
| 324 |
* Read from the command line. */ |
|---|
| 325 |
|
|---|
| 326 |
#endif // !defined COMPILE_FOR_LPC21 |
|---|
| 327 |
unsigned char TerminalOnly; |
|---|
| 328 |
unsigned char DetectOnly; |
|---|
| 329 |
unsigned char WipeDevice; |
|---|
| 330 |
int DetectedDevice; |
|---|
| 331 |
char *baud_rate; |
|---|
| 332 |
* port communicating with the |
|---|
| 333 |
* microcontroller. Read from the |
|---|
| 334 |
* command line. */ |
|---|
| 335 |
|
|---|
| 336 |
char *StringOscillator; |
|---|
| 337 |
* speed from the command line. */ |
|---|
| 338 |
BINARY *BinaryContent; |
|---|
| 339 |
|
|---|
| 340 |
unsigned long BinaryLength; |
|---|
| 341 |
unsigned long BinaryOffset; |
|---|
| 342 |
unsigned long StartAddress; |
|---|
| 343 |
|
|---|
| 344 |
#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 345 |
HANDLE hCom; |
|---|
| 346 |
#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 347 |
|
|---|
| 348 |
#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21 |
|---|
| 349 |
int fdCom; |
|---|
| 350 |
#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21 |
|---|
| 351 |
|
|---|
| 352 |
#if defined COMPILE_FOR_LINUX |
|---|
| 353 |
struct termios oldtio, newtio; |
|---|
| 354 |
#endif // defined COMPILE_FOR_LINUX |
|---|
| 355 |
|
|---|
| 356 |
unsigned serial_timeout_count; |
|---|
| 357 |
* timeouts on serial port read. */ |
|---|
| 358 |
|
|---|
| 359 |
} ISP_ENVIRONMENT; |
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
static void DumpString(int level, const void *s, size_t size, const char *fmt, ...); |
|---|
| 363 |
static void SendComPort(ISP_ENVIRONMENT *IspEnvironment, const char *s); |
|---|
| 364 |
static void ReceiveComPort(ISP_ENVIRONMENT *IspEnvironment, void *Answer, unsigned long MaxSize, unsigned long *RealSize, unsigned long WantedNr0x0A,unsigned timeOutMilliseconds); |
|---|
| 365 |
static void PhilipsOutputErrorMessage(unsigned char ErrorNumber); |
|---|
| 366 |
static unsigned char GetErrorNumber(const char *Answer); |
|---|
| 367 |
static void SerialTimeoutSet(ISP_ENVIRONMENT *IspEnvironment, unsigned timeout_milliseconds); |
|---|
| 368 |
static void SerialTimeoutTick(ISP_ENVIRONMENT *IspEnvironment); |
|---|
| 369 |
static int SerialTimeoutCheck(ISP_ENVIRONMENT *IspEnvironment); |
|---|
| 370 |
|
|---|
| 371 |
#if !defined COMPILE_FOR_LPC21 |
|---|
| 372 |
|
|---|
| 373 |
static int debug_level = 2; |
|---|
| 374 |
|
|---|
| 375 |
static void DebugPrintf( int level, const char *fmt, ...); |
|---|
| 376 |
static void ClearSerialPortBuffers(ISP_ENVIRONMENT *IspEnvironment); |
|---|
| 377 |
static void ControlModemLines(ISP_ENVIRONMENT *IspEnvironment, unsigned char DTR, unsigned char RTS); |
|---|
| 378 |
static unsigned char Ascii2Hex(unsigned char c); |
|---|
| 379 |
#endif |
|---|
| 380 |
|
|---|
| 381 |
static const int SectorTable_210x[] = { 8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192, |
|---|
| 382 |
8192, 8192, 8192, 8192, 8192, 8192, 8192 }; |
|---|
| 383 |
static const int SectorTable_2103[] = { 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096 }; |
|---|
| 384 |
static const int SectorTable_211x[] = { 8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192, |
|---|
| 385 |
8192, 8192, 8192, 8192, 8192, 8192, 8192, }; |
|---|
| 386 |
static const int SectorTable_212x[] = { 8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192, |
|---|
| 387 |
65536, 65536, 8192, 8192, 8192, 8192, 8192, 8192, |
|---|
| 388 |
8192 }; |
|---|
| 389 |
static const int SectorTable_213x[] = { 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, |
|---|
| 390 |
32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, |
|---|
| 391 |
32768, 32768, 32768, 32768, 32768, 32768, 4096, 4096, |
|---|
| 392 |
4096, 4096, 4096 }; |
|---|
| 393 |
static int SectorTable_RAM[] = { 65000 }; |
|---|
| 394 |
|
|---|
| 395 |
static LPC_DEVICE_TYPE LPCtypes[] = |
|---|
| 396 |
{ |
|---|
| 397 |
{ 0, 0, 0 }, |
|---|
| 398 |
{ 0x0004FF11, 2103, 32, 8, 8, 4096, SectorTable_2103 }, |
|---|
| 399 |
{ 0xFFF0FF12, 2104, 128, 16, 15, 8192, SectorTable_210x }, |
|---|
| 400 |
{ 0xFFF0FF22, 2105, 128, 32, 15, 8192, SectorTable_210x }, |
|---|
| 401 |
{ 0xFFF0FF32, 2106, 128, 64, 15, 8192, SectorTable_210x }, |
|---|
| 402 |
{ 0x0101FF12, 2114, 128, 16, 15, 8192, SectorTable_211x }, |
|---|
| 403 |
{ 0x0201FF12, 2119, 128, 16, 15, 8192, SectorTable_211x }, |
|---|
| 404 |
{ 0x0101FF13, 2124, 256, 16, 17, 8192, SectorTable_212x }, |
|---|
| 405 |
{ 0x0201FF13, 2129, 256, 16, 17, 8192, SectorTable_212x }, |
|---|
| 406 |
{ 0x0002FF01, 2131, 32, 8, 8, 4096, SectorTable_213x }, |
|---|
| 407 |
{ 0x0002FF11, 2132, 64, 16, 9, 4096, SectorTable_213x }, |
|---|
| 408 |
{ 0x0002FF12, 2134, 128, 16, 11, 4096, SectorTable_213x }, |
|---|
| 409 |
{ 0x0002FF23, 2136, 256, 32, 15, 4096, SectorTable_213x }, |
|---|
| 410 |
{ 0x0002FF25, 2138, 512, 32, 27, 4096, SectorTable_213x }, |
|---|
| 411 |
{ 0x0402FF01, 2141, 32, 8, 8, 4096, SectorTable_213x }, |
|---|
| 412 |
{ 0x0402FF11, 2142, 64, 16, 9, 4096, SectorTable_213x }, |
|---|
| 413 |
{ 0x0402FF12, 2144, 128, 16, 11, 4096, SectorTable_213x }, |
|---|
| 414 |
{ 0x0402FF23, 2146, 256, 40, 15, 4096, SectorTable_213x }, |
|---|
| 415 |
{ 0x0402FF25, 2148, 512, 40, 27, 4096, SectorTable_213x }, |
|---|
| 416 |
{ 0x0301FF13, 2194, 256, 16, 17, 8192, SectorTable_212x }, |
|---|
| 417 |
{ 0x0301FF12, 2210, 0, 16, 0, 8192, SectorTable_211x }, |
|---|
| 418 |
{ 0x0401FF12, 2212, 128, 16, 15, 8192, SectorTable_211x }, |
|---|
| 419 |
{ 0x0601FF13, 2214, 256, 16, 17, 8192, SectorTable_212x }, |
|---|
| 420 |
|
|---|
| 421 |
{ 0x0401FF13, 2292, 256, 16, 17, 8192, SectorTable_212x }, |
|---|
| 422 |
{ 0x0501FF13, 2294, 256, 16, 17, 8192, SectorTable_212x }, |
|---|
| 423 |
{ 0x0603FB02, 2364, 128, 34, 11, 4096, SectorTable_213x }, |
|---|
| 424 |
{ 0x0603FB23, 2366, 256, 58, 15, 4096, SectorTable_213x }, |
|---|
| 425 |
{ 0x0603FB25, 2368, 512, 58, 27, 4096, SectorTable_213x }, |
|---|
| 426 |
{ 0x0703FF25, 2378, 512, 58, 27, 4096, SectorTable_213x }, |
|---|
| 427 |
|
|---|
| 428 |
{ 0x0603FF35, 2468, 512, 98, 27, 4096, SectorTable_213x } |
|---|
| 429 |
}; |
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN |
|---|
| 435 |
static int kbhit( void); |
|---|
| 436 |
static int getch( void); |
|---|
| 437 |
#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN |
|---|
| 438 |
|
|---|
| 439 |
#if defined COMPILE_FOR_LINUX |
|---|
| 440 |
static void Sleep(unsigned long MilliSeconds); |
|---|
| 441 |
#endif // defined COMPILE_FOR_LINUX |
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
#if !defined COMPILE_FOR_LPC21 |
|---|
| 446 |
static void OpenSerialPort(ISP_ENVIRONMENT *IspEnvironment) |
|---|
| 447 |
{ |
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 451 |
DCB dcb; |
|---|
| 452 |
COMMTIMEOUTS commtimeouts; |
|---|
| 453 |
|
|---|
| 454 |
IspEnvironment->hCom = CreateFile(IspEnvironment->serial_port, GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); |
|---|
| 455 |
|
|---|
| 456 |
if(IspEnvironment->hCom == INVALID_HANDLE_VALUE) |
|---|
| 457 |
{ |
|---|
| 458 |
DebugPrintf( 1, "Can't open COM-Port %s ! - Error: %ld\n", IspEnvironment->serial_port, GetLastError()); |
|---|
| 459 |
exit(2); |
|---|
| 460 |
} |
|---|
| 461 |
|
|---|
| 462 |
DebugPrintf( 3, "COM-Port %s opened...\n", IspEnvironment->serial_port); |
|---|
| 463 |
|
|---|
| 464 |
GetCommState(IspEnvironment->hCom, &dcb); |
|---|
| 465 |
dcb.BaudRate = atol(IspEnvironment->baud_rate); |
|---|
| 466 |
dcb.ByteSize = 8; |
|---|
| 467 |
dcb.StopBits = ONESTOPBIT; |
|---|
| 468 |
dcb.Parity = NOPARITY; |
|---|
| 469 |
dcb.fDtrControl = DTR_CONTROL_DISABLE; |
|---|
| 470 |
dcb.fOutX = FALSE; |
|---|
| 471 |
dcb.fInX = FALSE; |
|---|
| 472 |
dcb.fNull = FALSE; |
|---|
| 473 |
dcb.fRtsControl = RTS_CONTROL_DISABLE; |
|---|
| 474 |
if(SetCommState(IspEnvironment->hCom, &dcb) == 0) |
|---|
| 475 |
{ |
|---|
| 476 |
DebugPrintf( 1, "Can't set baudrate %s ! - Error: %ld", IspEnvironment->baud_rate, GetLastError()); |
|---|
| 477 |
exit(3); |
|---|
| 478 |
} |
|---|
| 479 |
|
|---|
| 480 |
SetCommMask(IspEnvironment->hCom,EV_RXCHAR | EV_TXEMPTY); |
|---|
| 481 |
|
|---|
| 482 |
commtimeouts.ReadIntervalTimeout = MAXDWORD; |
|---|
| 483 |
commtimeouts.ReadTotalTimeoutMultiplier = 0; |
|---|
| 484 |
commtimeouts.ReadTotalTimeoutConstant = 1; |
|---|
| 485 |
commtimeouts.WriteTotalTimeoutMultiplier = 0; |
|---|
| 486 |
commtimeouts.WriteTotalTimeoutConstant = 0; |
|---|
| 487 |
SetCommTimeouts(IspEnvironment->hCom, &commtimeouts); |
|---|
| 488 |
|
|---|
| 489 |
#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 490 |
|
|---|
| 491 |
#if defined COMPILE_FOR_LINUX |
|---|
| 492 |
|
|---|
| 493 |
IspEnvironment->fdCom = open(IspEnvironment->serial_port, O_RDWR | O_NOCTTY | O_NONBLOCK ); |
|---|
| 494 |
|
|---|
| 495 |
if(IspEnvironment->fdCom < 0) |
|---|
| 496 |
{ |
|---|
| 497 |
DebugPrintf( 1, "Can't open COM-Port %s !\n", IspEnvironment->serial_port); |
|---|
| 498 |
exit(2); |
|---|
| 499 |
} |
|---|
| 500 |
|
|---|
| 501 |
DebugPrintf( 3, "COM-Port %s opened...\n", IspEnvironment->serial_port); |
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
tcflush(IspEnvironment->fdCom, TCOFLUSH); |
|---|
| 505 |
tcflush(IspEnvironment->fdCom, TCIFLUSH); |
|---|
| 506 |
fcntl(IspEnvironment->fdCom, F_SETFL, fcntl(IspEnvironment->fdCom, F_GETFL) & ~O_NONBLOCK); |
|---|
| 507 |
|
|---|
| 508 |
tcgetattr(IspEnvironment->fdCom, &IspEnvironment->oldtio); |
|---|
| 509 |
|
|---|
| 510 |
bzero(&IspEnvironment->newtio, sizeof(IspEnvironment->newtio)); |
|---|
| 511 |
IspEnvironment->newtio.c_cflag = CS8 | CLOCAL | CREAD; |
|---|
| 512 |
|
|---|
| 513 |
#ifdef __APPLE__ |
|---|
| 514 |
#define NEWTERMIOS_SETBAUDARTE(bps) IspEnvironment->newtio.c_ispeed = IspEnvironment->newtio.c_ospeed = bps; |
|---|
| 515 |
#else |
|---|
| 516 |
#define NEWTERMIOS_SETBAUDARTE(bps) IspEnvironment->newtio.c_cflag |= bps; |
|---|
| 517 |
#endif |
|---|
| 518 |
|
|---|
| 519 |
switch(atol(IspEnvironment->baud_rate)) |
|---|
| 520 |
{ |
|---|
| 521 |
#ifdef B1152000 |
|---|
| 522 |
case 1152000: NEWTERMIOS_SETBAUDARTE( B1152000 ); break; |
|---|
| 523 |
#endif // B1152000 |
|---|
| 524 |
#ifdef B576000 |
|---|
| 525 |
case 576000: NEWTERMIOS_SETBAUDARTE( B576000 ); break; |
|---|
| 526 |
#endif // B576000 |
|---|
| 527 |
#ifdef B230400 |
|---|
| 528 |
case 230400: NEWTERMIOS_SETBAUDARTE( B230400 ); break; |
|---|
| 529 |
#endif // B230400 |
|---|
| 530 |
#ifdef B115200 |
|---|
| 531 |
case 115200: NEWTERMIOS_SETBAUDARTE( B115200 ); break; |
|---|
| 532 |
#endif // B115200 |
|---|
| 533 |
#ifdef B57600 |
|---|
| 534 |
case 57600: NEWTERMIOS_SETBAUDARTE( B57600 ); break; |
|---|
| 535 |
#endif // B57600 |
|---|
| 536 |
#ifdef B38400 |
|---|
| 537 |
case 38400: NEWTERMIOS_SETBAUDARTE( B38400 ); break; |
|---|
| 538 |
#endif // B38400 |
|---|
| 539 |
#ifdef B19200 |
|---|
| 540 |
case 19200: NEWTERMIOS_SETBAUDARTE( B19200 ); break; |
|---|
| 541 |
#endif // B19200 |
|---|
| 542 |
#ifdef B9600 |
|---|
| 543 |
case 9600: NEWTERMIOS_SETBAUDARTE( B9600 ); break; |
|---|
| 544 |
#endif // B9600 |
|---|
| 545 |
default: |
|---|
| 546 |
{ |
|---|
| 547 |
DebugPrintf( 1, "unknown baudrate %s\n", IspEnvironment->baud_rate); |
|---|
| 548 |
exit(3); |
|---|
| 549 |
} |
|---|
| 550 |
} |
|---|
| 551 |
|
|---|
| 552 |
IspEnvironment->newtio.c_iflag = IGNPAR | IGNBRK | IXON | IXOFF; |
|---|
| 553 |
IspEnvironment->newtio.c_oflag = 0; |
|---|
| 554 |
|
|---|
| 555 |
|
|---|
| 556 |
IspEnvironment->newtio.c_lflag = 0; |
|---|
| 557 |
|
|---|
| 558 |
cfmakeraw(&IspEnvironment->newtio); |
|---|
| 559 |
IspEnvironment->newtio.c_cc[VTIME] = 1; |
|---|
| 560 |
IspEnvironment->newtio.c_cc[VMIN] = 0; |
|---|
| 561 |
|
|---|
| 562 |
tcflush(IspEnvironment->fdCom, TCIFLUSH); |
|---|
| 563 |
tcsetattr(IspEnvironment->fdCom, TCSANOW, &IspEnvironment->newtio); |
|---|
| 564 |
|
|---|
| 565 |
#endif // defined COMPILE_FOR_LINUX |
|---|
| 566 |
} |
|---|
| 567 |
|
|---|
| 568 |
static void CloseSerialPort(ISP_ENVIRONMENT *IspEnvironment) |
|---|
| 569 |
{ |
|---|
| 570 |
#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 571 |
|
|---|
| 572 |
CloseHandle(IspEnvironment->hCom); |
|---|
| 573 |
|
|---|
| 574 |
#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 575 |
|
|---|
| 576 |
#if defined COMPILE_FOR_LINUX |
|---|
| 577 |
|
|---|
| 578 |
tcflush(IspEnvironment->fdCom, TCOFLUSH); |
|---|
| 579 |
tcflush(IspEnvironment->fdCom, TCIFLUSH); |
|---|
| 580 |
tcsetattr(IspEnvironment->fdCom, TCSANOW, &IspEnvironment->oldtio); |
|---|
| 581 |
|
|---|
| 582 |
close(IspEnvironment->fdCom); |
|---|
| 583 |
|
|---|
| 584 |
#endif // defined COMPILE_FOR_LINUX |
|---|
| 585 |
} |
|---|
| 586 |
|
|---|
| 587 |
#endif // !defined COMPILE_FOR_LPC21 |
|---|
| 588 |
|
|---|
| 589 |
|
|---|
| 590 |
|
|---|
| 591 |
|
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
static void SendComPortBlock(ISP_ENVIRONMENT *IspEnvironment, const void *s, size_t n) |
|---|
| 595 |
{ |
|---|
| 596 |
#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 597 |
|
|---|
| 598 |
unsigned long realsize; |
|---|
| 599 |
|
|---|
| 600 |
#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 601 |
|
|---|
| 602 |
DumpString( 4, s, n, "Sending "); |
|---|
| 603 |
|
|---|
| 604 |
#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 605 |
|
|---|
| 606 |
WriteFile(IspEnvironment->hCom, s, n, &realsize, NULL); |
|---|
| 607 |
|
|---|
| 608 |
#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 609 |
|
|---|
| 610 |
#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21 |
|---|
| 611 |
|
|---|
| 612 |
write(IspEnvironment->fdCom, s, n); |
|---|
| 613 |
|
|---|
| 614 |
#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21 |
|---|
| 615 |
} |
|---|
| 616 |
|
|---|
| 617 |
|
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
|
|---|
| 621 |
static void SendComPort(ISP_ENVIRONMENT *IspEnvironment, const char *s) |
|---|
| 622 |
{ |
|---|
| 623 |
SendComPortBlock( IspEnvironment, s, strlen(s)); |
|---|
| 624 |
} |
|---|
| 625 |
|
|---|
| 626 |
|
|---|
| 627 |
|
|---|
| 628 |
|
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 |
|
|---|
| 632 |
|
|---|
| 633 |
|
|---|
| 634 |
|
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
static void ReceiveComPortBlock( ISP_ENVIRONMENT *IspEnvironment, |
|---|
| 638 |
void *answer, unsigned long max_size, |
|---|
| 639 |
unsigned long *real_size) |
|---|
| 640 |
{ |
|---|
| 641 |
|
|---|
| 642 |
#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 643 |
|
|---|
| 644 |
ReadFile(IspEnvironment->hCom, answer, max_size, real_size, NULL); |
|---|
| 645 |
|
|---|
| 646 |
#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN |
|---|
| 647 |
|
|---|
| 648 |
#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_LPC21 |
|---|
| 649 |
|
|---|
| 650 |
*real_size = read(IspEnvironment->fdCom, answer, max_size); |
|---|
| 651 |
|
|---|
| 652 |
#endif // defined COMPILE_FOR_LINUX |
|---|
| 653 |
|
|---|
| 654 |
|
|---|
| 655 |
DumpString( 5, answer, (*real_size), "Read(Length=%ld): ", (*real_size)); |
|---|
| 656 |
|
|---|
| 657 |
if( *real_size == 0) |
|---|
| 658 |
{ |
|---|
| 659 |
SerialTimeoutTick( IspEnvironment ); |
|---|
| 660 |
} |
|---|
| 661 |
} |
|---|
| 662 |
|
|---|
| 663 |
|
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 |
|
|---|
| 667 |
|
|---|
| 668 |
|
|---|
| 669 |
|
|---|
| 670 |
|
|---|
| 671 |
|
|---|
| 672 |
|
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
static void SerialTimeoutSet(ISP_ENVIRONMENT *IspEnvironment, unsigned timeout_milliseconds) |
|---|
| 677 |
{ |
|---|
| 678 |
|
|---|
| 679 |
#if defined COMPILE_FOR_LINUX |
|---|
| 680 |
IspEnvironment->serial_timeout_count = timeout_milliseconds/100; |
|---|
| 681 |
#elif defined COMPILE_FOR_LPC21 |
|---|
| 682 |
IspEnvironment->serial_timeout_count = timeout_milliseconds*200; |
|---|
| 683 |
#else |
|---|
| 684 |
IspEnvironment->serial_timeout_count = timeout_milliseconds; |
|---|
| 685 |
#endif |
|---|
| 686 |
} |
|---|
| 687 |
|
|---|
| 688 |
|
|---|
| 689 |
|
|---|
| 690 |
|
|---|
| 691 |
|
|---|
| 692 |
static void SerialTimeoutTick(ISP_ENVIRONMENT *IspEnvironment) |
|---|
| 693 |
{ |
|---|
| 694 |
if( IspEnvironment->serial_timeout_count <= 1) |
|---|
| 695 |
{ |
|---|
| 696 |
IspEnvironment->serial_timeout_count = 0; |
|---|
| 697 |
} |
|---|
| 698 |
else |
|---|
| 699 |
{ |
|---|
| 700 |
IspEnvironment->serial_timeout_count--; |
|---|
| 701 |
} |
|---|
| 702 |
} |
|---|
| 703 |
|
|---|
| 704 |
|
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| 707 |
|
|---|
| 708 |
|
|---|
| 709 |
static int SerialTimeoutCheck(ISP_ENVIRONMENT *IspEnvironment) |
|---|
| 710 |
{ |
|---|
| 711 |
if( IspEnvironment->serial_timeout_count == 0) |
|---|
| 712 |
{ |
|---|
| 713 |
return 1; |
|---|
| 714 |
} |
|---|
| 715 |
return 0; |
|---|
| 716 |
} |
|---|
| 717 |
|
|---|
| 718 |
|
|---|
| 719 |
#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN |
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 |
|
|---|
| 723 |
|
|---|
| 724 |
|
|---|
| 725 |
static int getch(void) |
|---|
| 726 |
{ |
|---|
| 727 |
char ch; |
|---|
| 728 |
struct termios origtty, tty; |
|---|
| 729 |
|
|---|
| 730 |
|
|---|
| 731 |
tcgetattr(0, &origtty); |
|---|
| 732 |
|
|---|
| 733 |
|
|---|
| 734 |
tty = origtty; |
|---|
| 735 |
|
|---|
| 736 |
tty.c_lflag &= ~ICANON; |
|---|
| 737 |
tty.c_lflag &= ~ECHO; |
|---|
| 738 |
tty.c_lflag &= ~ISIG; |
|---|
| 739 |
tty.c_cc[VMIN] = 1; |
|---|
| 740 |
tty.c_cc[VTIME] = 0; |
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| 743 |
tcsetattr(0, TCSADRAIN, &tty); |
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
read(0,&ch,1); |
|---|
| 747 |
|
|---|
| 748 |
|
|---|
| 749 |
tcsetattr(0, TCSADRAIN, &origtty); |
|---|
| 750 |
|
|---|
| 751 |
return ch; |
|---|
| 752 |
} |
|---|
| 753 |
#endif // defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN |
|---|
| 754 |
|
|---|
| 755 |
#if defined COMPILE_FOR_LINUX || defined COMPILE_FOR_CYGWIN |
|---|
| 756 |
|
|---|
| 757 |
|
|---|
| 758 |
|
|---|
| 759 |
|
|---|
| 760 |
|
|---|
| 761 |
|
|---|
| 762 |
static int kbhit(void) |
|---|
| 763 |
{ |
|---|
| 764 |
|
|---|
| 765 |
int return_value = 0; |
|---|
| 766 |
|
|---|
| 767 |
|
|---|
| 768 |
struct termios origtty, tty; |
|---|
| 769 |
|
|---|
| 770 |
struct timeval select_time; |
|---|
| 771 |
|
|---|
| 772 |
fd_set readset; |
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
FD_ZERO(&readset); |
|---|
| 776 |
FD_SET(STDIN_FILENO, &readset); |
|---|
|