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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
|
/* -*- c++ -*-
This is Picprog, Microchip PIC programmer software for the serial port device.
Copyright © 1997,2002,2003,2004,2006,2007,2008,2010 Jaakko Hyvätti
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/ .
The author may be contacted at:
Email: Jaakko.Hyvatti@iki.fi
URL: http://www.iki.fi/hyvatti/
Phone: +358 40 5011222
Please send any suggestions, bug reports, success stories etc. to the
Email address above. Include word 'picprog' in the subject line to
make sure your email passes my spam filtering.
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cerrno>
#include <ctime>
#include <sys/ioctl.h>
#if defined(__i386__) || defined(__x86_64__)
#include <sys/io.h>
#define HAVE_IOPL
#endif
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include <termios.h>
#include <sysexits.h>
#include <sched.h>
#include "picport.h"
using namespace std;
#ifndef O_NONBLOCK
#define O_NONBLOCK O_NDELAY
#endif
unsigned int picport::tsc_1000ns = 0;
// Data bitrate delay, ns.
// Cable delay is tested and this value adjusted when port is opened.
int picport::cable_delay = 0;
// Set this to -1 if you want to use nanosleep when running as root.
// This only works with 2.4 kernels, 2.6 series kernels no more work.
int picport::use_nanosleep = 0;
//
static bool disable_interrupts = 1;
void
picport::set_clock_data (int rts, int dtr)
{
if (hardware == k8048) {
rts = !rts;
dtr = !dtr;
}
// Before first call to set_clock_data, read the modem status like this:
// ioctl (fd, TIOCMGET, &modembits);
if (rts)
modembits |= TIOCM_RTS;
else
modembits &= ~TIOCM_RTS;
if (dtr)
modembits |= TIOCM_DTR;
else
modembits &= ~TIOCM_DTR;
if (0 > ioctl (fd, TIOCMSET, &modembits)) {
int e = errno;
tcsetattr (fd, TCSANOW, &saved);
cerr << "Unable to set RTS/DTR on tty " << portname << ":" << strerror (e) << endl;
exit (EX_IOERR);
}
}
void
picport::set_vpp (int vpp)
{
if (hardware == k8048)
vpp = !vpp;
if (0 > ioctl (fd, vpp ? TIOCSBRK : TIOCCBRK, 0)) {
int e = errno;
if (vpp)
ioctl (fd, TIOCCBRK, 0);
tcsetattr (fd, TCSANOW, &saved);
cerr << "Unable to " << (vpp ? "start" : "stop") << " break on tty "
<< portname << ":" << strerror (e) << endl;
exit (EX_IOERR);
}
}
picport::picport (const char *tty, bool nordtsc, bool slow, bool reboot,
hardware_types h)
: addr (0), debug_on (0), hardware (h)
{
for (int i = 0; i < 16; ++i)
W [i] = 0;
portname = new char [strlen (tty) + 1];
strcpy (portname, tty);
if (0 > (fd = open (tty, O_RDWR|O_NOCTTY|O_NONBLOCK))) {
int e = errno;
cerr << "Unable to open tty " << tty << ":" << strerror (e) << endl;
exit (EX_IOERR);
}
tcgetattr (fd, &saved);
termstate = saved;
termstate.c_iflag = IGNBRK | IGNPAR;
termstate.c_oflag = 0;
termstate.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
termstate.c_lflag = 0;
tcsetattr (fd, TCSANOW, &termstate);
// Initialize lines for programming
// RTS/DTR low, sleep and then TxD high (program voltage)
ioctl (fd, TIOCCBRK, 0);
// Before first call to set_clock_data, read the modem status.
ioctl (fd, TIOCMGET, &modembits);
modembits &= ~(TIOCM_RTS | TIOCM_DTR);
ioctl (fd, TIOCMSET, &modembits);
usleep (1000);
// Check the CTS. If it is up, even when we just lowered DTR,
// we probably are not talking to a JDM type programmer.
int i = 0;
ioctl (fd, TIOCMGET, &i);
if (i & TIOCM_CTS) {
tcsetattr (fd, TCSANOW, &saved);
cerr << tty << ":CTS is high, probably we are not connected to a programmer but a modem or terminal." << endl;
exit (EX_IOERR);
}
struct sched_param scp;
scp.sched_priority = sched_get_priority_min (SCHED_FIFO);
// sched_get_priority_max()
// scp.sched_priority = 50;
cerr << "Trying realtime priority " << scp.sched_priority << endl;
if (sched_setscheduler (0, SCHED_FIFO, &scp)) {
cerr << "Cannot use real time scheduling: " << strerror(errno) << endl;
// Not root. Cannot use realtime scheduling.
use_nanosleep = 0;
}
#ifdef HAVE_IOPL
if (iopl (3))
disable_interrupts = 0;
#else
disable_interrupts = 0;
#endif
#ifdef CPU_SETSIZE
// When computing the delay loops, we do not want the cpu's to change.
// Pick the first cpu in the current allowed set.
cpu_set_t new_mask;
cpu_set_t cur_mask;
CPU_ZERO(&cur_mask);
CPU_ZERO(&new_mask);
if (sched_getaffinity(0, sizeof (cur_mask), &cur_mask) < 0) {
tcsetattr (fd, TCSANOW, &saved);
cerr << "Getting affinity functions do not work." << endl;
exit (EX_IOERR);
}
for (int cpuno = 0; cpuno < CPU_SETSIZE; ++cpuno)
if (CPU_ISSET (cpuno, &cur_mask)) {
CPU_SET (cpuno, &new_mask);
sched_getaffinity (0, sizeof (new_mask), &new_mask);
// We want to print informative line only if we really
// have multiple cpu's.
for (int cpuno1 = cpuno+1; cpuno1 < CPU_SETSIZE; ++cpuno1)
if (CPU_ISSET (cpuno1, &cur_mask)) {
cerr << "Bound to CPU " << cpuno << endl;
break;
}
}
#endif // CPU_SETSIZE
#ifdef RDTSC_WORKS
if (!nordtsc && !use_nanosleep && !tsc_1000ns) {
// Read /proc/cpuinfo for clock speed, or if that fails, try our
// own test.
ifstream cpui ("/proc/cpuinfo");
string w;
unsigned int mhz = 1; // 1MHz is just a flag that tsc was found
while (cpui) {
cpui >> w;
if ("MHz" == w) {
unsigned int tmp;
// Read just the integer part. No need for the rest.
cpui >> w >> tmp;
// Update this when we have 100 GHz CPUs.
if (tmp >= 4 && tmp <= 100000)
mhz = tmp;
}
if ("tsc" == w && mhz) {
// tsc capability found. Use it.
tsc_1000ns = mhz;
break;
}
}
// If the /proc fs did not contain clock speed but indicated
// tsc capability, test the approximate clock speed.
if (1 == tsc_1000ns) {
// Loop the test 20 times, and select the smallest count.
for (int recount = 0; recount < 20; ++recount) {
struct timeval tv1, tv2;
unsigned long a1, d1, a2, d2;
// Wait for when a microsecond changes
gettimeofday (&tv2, 0);
do {
gettimeofday (&tv1, 0);
} while (tv2.tv_usec == tv1.tv_usec);
asm volatile("rdtsc":"=a" (a1), "=d" (d1));
tv1.tv_usec += 1000;
if (tv1.tv_usec >= 1000000) {
tv1.tv_usec -= 1000000;
tv1.tv_sec ++;
}
do {
gettimeofday (&tv2, 0);
} while (tv2.tv_sec < tv1.tv_sec
|| (tv2.tv_sec == tv1.tv_sec && tv2.tv_usec < tv1.tv_usec));
asm volatile("rdtsc":"=a" (a2), "=d" (d2));
if (a2 < a1)
d2 --;
a2 -= a1;
a2 &= 0xffffffff; // for x86_64
d2 -= d1;
if (d2)
continue;
a2 /= 1000;
if (tsc_1000ns <= 1 || tsc_1000ns > a2)
tsc_1000ns = a2;
}
if (tsc_1000ns <= 1) {
tcsetattr (fd, TCSANOW, &saved);
cerr << "Unable to determine CPU clock speed for delay loops." << endl;
exit (EX_IOERR);
}
}
if (tsc_1000ns >= 1) {
cout << "CPU clock speed: " << tsc_1000ns << " MHz" << endl;
}
}
#endif
if (tsc_1000ns > 1 && disable_interrupts)
cerr << "Disable interrupts during delays." << endl;
if (reboot) {
// Power off any microcontroller that may be running a program.
cerr << "Power off." << endl;
set_clock_data (1, 1);
set_vpp (1);
usleep (500000); // 0.5s delay should discharge Vdd.
}
// /MCLR must go down for a while first
set_vpp (0);
usleep (10);
// Power up
set_clock_data (0, 0);
set_vpp (1);
// Charge Vdd
usleep (25000);
// Detect delays that are needed for cable lenght
bool successful_echo = false;
for (int i = 1; i <= 100; ++i) {
int data_out = i & 1;
// toggle data, keep clock off / power on
if (data_out)
modembits |= TIOCM_DTR;
else
modembits &= ~TIOCM_DTR;
ioctl (fd, TIOCMSET, &modembits);
delay (cable_delay);
int data_in;
ioctl (fd, TIOCMGET, &data_in);
data_in = 0 != (data_in & TIOCM_CTS);
delay (1000000);
successful_echo = data_out == data_in;
if (!successful_echo)
cable_delay += 100; // Another 100 ns added for cable length
ioctl (fd, TIOCMGET, &data_in);
data_in = 0 != (data_in & TIOCM_CTS);
if (data_out != data_in) {
ioctl (fd, TIOCCBRK, 0);
tcsetattr (fd, TCSANOW, &saved);
cerr << "No JDM compatible hardware detected." << endl;
exit (EX_IOERR);
}
}
set_clock_data (0, 0);
if (!successful_echo) {
ioctl (fd, TIOCCBRK, 0);
tcsetattr (fd, TCSANOW, &saved);
cerr << "Cable too long." << endl;
exit (EX_IOERR);
}
cable_delay += 100; // Another 100 ns added just for safety
if (slow && cable_delay < 10000)
cable_delay = 10000;
if (1 == use_nanosleep)
cerr << "Using " << cable_delay << " ns delays with real time priority." << endl;
else if (tsc_1000ns > 1)
cerr << "Using " << cable_delay << " ns delays." << endl;
else if (cable_delay > 1000)
cerr << "Using " << ((cable_delay + 999) / 1000) << " µs delays." << endl;
else
cerr << "Using >1 µs delays. --rdtsc may work for faster timings." << endl;
}
picport::~picport ()
{
set_vpp (0);
usleep (1);
tcsetattr (fd, TCSANOW, &saved);
close (fd);
delete [] portname;
}
void picport::reset (unsigned long reset_address)
{
set_clock_data (0, 0);
usleep (100); // Make sure we have the power there.
set_vpp (0);
usleep (50);
set_vpp (1);
usleep (10);
addr = reset_address;
}
void picport::delay (long ns)
{
if (1 == use_nanosleep) {
timespec ts = {ns / 1000000000, ns % 1000000000}, ts2;
while (nanosleep (&ts, &ts2) && EINTR == errno)
ts = ts2;
return;
}
#ifdef RDTSC_WORKS
if (tsc_1000ns > 1) {
unsigned long a1, d1, a2, d2;
if (ns <= 10000 && disable_interrupts)
asm volatile("pushf; cli");
asm volatile("rdtsc":"=a" (a1), "=d" (d1));
d2 = d1;
if (ns > 10000)
// This is not as accurate but does not overflow
a2 = 0xffffffff & (a1 + 1 + (ns+999)/1000*tsc_1000ns);
else
a2 = 0xffffffff & (a1 + 1 + (ns*tsc_1000ns + 999) / 1000);
if (a2 < a1)
d2++;
do {
asm volatile("rdtsc":"=a" (a1), "=d" (d1));
} while (d1 < d2 || (d1 == d2 && a1 < a2));
if (ns <= 10000 && disable_interrupts)
asm volatile("popf");
return;
}
// Fall back to gettimeofday() if tsc is not available.
#endif // RDTSC_WORKS
// Delay loop that should take more than a microsecond to execute.
// Check the real time clock and break out if at least the specified
// number of microseconds has gone.
struct timeval tv1, tv2;
gettimeofday (&tv2, 0);
tv2.tv_usec += 1 + (ns + 999)/1000;
if (tv2.tv_usec >= 1000000) {
tv2.tv_usec -= 1000000;
tv2.tv_sec++;
}
do {
gettimeofday (&tv1, 0);
} while (tv1.tv_sec < tv2.tv_sec
|| (tv1.tv_sec == tv2.tv_sec && tv1.tv_usec < tv2.tv_usec));
}
void picport::p_out (int b)
{
struct timeval tv1, tv2;
gettimeofday (&tv1, 0);
#if defined(__i386__) or defined(__x86_64__)
if (tsc_1000ns > 1 && disable_interrupts)
asm volatile("pushf; cli");
#endif
set_clock_data (1, b); // set data, clock up
delay (cable_delay);
set_clock_data (0, b); // clock down
#if defined(__i386__) or defined(__x86_64__)
if (tsc_1000ns > 1 && disable_interrupts)
asm volatile("popf");
#endif
gettimeofday (&tv2, 0);
// We may have spent a long time in an interrupt or in another task
// with clock down, so the pic may be pretty drained for power now.
// Recharge at least as long as we were without power.
long matching_delay = (tv2.tv_sec-tv1.tv_sec) * 1000000 + tv2.tv_usec - tv1.tv_usec;
if (matching_delay > 10) // 10 µs should be enough to charge up
matching_delay = 10;
matching_delay *= 1000;
if (matching_delay < cable_delay)
matching_delay = cable_delay;
delay (matching_delay);
}
int picport::p_in ()
{
struct timeval tv1, tv2;
gettimeofday (&tv1, 0);
#if defined(__i386__) or defined(__x86_64__)
if (tsc_1000ns > 1 && disable_interrupts)
asm volatile("pushf; cli");
#endif
set_clock_data (1, 1); // clock up
delay (cable_delay);
set_clock_data (0, 1); // set data up, clock down
#if defined(__i386__) or defined(__x86_64__)
if (tsc_1000ns > 1 && disable_interrupts)
asm volatile("popf");
#endif
gettimeofday (&tv2, 0);
// We may have spent a long time in an interrupt or in another task
// with clock down, so the pic may be pretty drained for power now.
// Recharge at least as long as we were without power.
long matching_delay = (tv2.tv_sec-tv1.tv_sec) * 1000000 + tv2.tv_usec - tv1.tv_usec;
if (matching_delay > 10) // 10 µs should be enough to charge up
matching_delay = 10;
matching_delay *= 1000;
if (matching_delay < cable_delay)
matching_delay = cable_delay;
delay (matching_delay);
int i;
ioctl (fd, TIOCMGET, &i);
i = 0 != (i & TIOCM_CTS);
if (hardware == k8048)
return !i;
return i;
}
int picport::command18 (enum commands18 comm, int data)
{
int i, shift = comm;
if (nop_prog == comm) {
// A programming command must leave the last bit clock pulse up
p_out (0);
p_out (0);
p_out (0);
set_clock_data (1, 0); // clock up
delay (1000*1000); // P9 >1 ms programming time
set_clock_data (0, 0); // clock down
// P10 >5 µs high voltage discharge time
// Later models listed as > 100 µs
delay (100*1000);
} else {
for (i = 0; i < 4; i++)
p_out ((shift >> i) & 1);
set_clock_data (0, 0); // set data down
delay (1000);
}
shift = 0; // default return value
switch (comm) {
case nop_erase:
// Erase cycle has delay between command and data
usleep (10000); // P11 5 ms + P10 5 µs erase time
// FALLTHROUGH
case instr:
case nop_prog:
if (0x0e00 == (data & 0xff00))
W[0] = data & 0x00ff;
else if (0x6ef8 == data)
addr = (addr & 0x00ffff) | (W[0] << 16);
else if (0x6ef7 == data)
addr = (addr & 0xff00ff) | (W[0] << 8);
else if (0x6ef6 == data)
addr = (addr & 0xffff00) | W[0];
goto sw;
case twrite_dec2:
addr -= 2;
goto sw;
case twrite_inc2:
addr += 2;
// FALLTHROUGH
case twrite:
case twrite_prog:
sw:
for (i = 0; i < 16; i++)
p_out ((data >> i) & 1);
set_clock_data (0, 0); // set data down
break;
case tread_dec:
--addr;
goto sr;
case tread_inc:
case inc_tread:
++addr;
// FALLTHROUGH
case shift_out:
case tread:
sr:
for (i = 0; i < 8; i++)
p_out(0);
delay (1000);
for (i = 0; i < 8; i++)
shift |= p_in () << i;
set_clock_data (0, 0); // set data down
}
delay (1000);
return shift;
}
int picport::command30 (enum commands30 comm, int data)
{
int i, shift = comm;
for (i = 0; i < 4; i++)
p_out ((shift >> i) & 1);
shift = 0; // default return value
switch (comm) {
case SIX:
if (0x200000 == (data & 0xff0000))
W[data & 15] = (data & 0x0ffff0) >> 4;
else if (0xEB0000 == (data & 0xfff87f))
W[(data >> 7) & 15] = 0;
else if (0xBA1830 == (data & 0xfff870)) {
// TBLRDL
++W[(data >> 7) & 15];
++W[data & 15];
} else if (0xBA0830 == (data & 0xfff870)) {
// TBLRDL
++W[(data >> 7) & 15];
++W[data & 15];
} else if (0x880190 == (data & 0xfffff0))
addr = (W[data & 15] << 16) & 0xff0000;
addr = (addr & 0xff0000) | W[6];
for (i = 0; i < 24; i++)
p_out ((data >> i) & 1);
break;
case REGOUT:
for (i = 0; i < 8; i++)
p_out(0);
for (i = 0; i < 16; i++)
shift |= p_in () << i;
}
set_clock_data (0, 0); // set data down
return shift;
}
void picport::setaddress (unsigned long a)
{
if (0 != a && addr == a)
return;
command18 (instr, 0x0e00 | ((a & 0xff0000) >> 16));
command18 (instr, 0x6ef8);
command18 (instr, 0x0e00 | ((a & 0x00ff00) >> 8));
command18 (instr, 0x6ef7);
command18 (instr, 0x0e00 | (a & 0x0000ff));
command18 (instr, 0x6ef6);
}
void picport::setaddress30 (unsigned long a)
{
if (0 != a && addr == a)
return;
command30 (SIX, 0x200000 | ((a & 0xff0000) >> 12)); // MOV #, W0
command30 (SIX, 0x880190); // MOV W0, TBLPAG
command30 (SIX, 0x200006 | ((a & 0x00ffff) << 4)); // MOV #, W6
}
// -1 == error, no programmer present
int picport::command (enum commands comm, int data)
{
int tmp1, tmp2;
// first, send out the command, 6 bits
int i, shift = comm;
for (i = 0; i < 6; i++)
p_out ((shift >> i) & 1);
set_clock_data (0, 0); // set data down
shift = 0; // default return value
switch (comm) {
case inc_addr:
++addr;
if (data != 0) { // 12f508 and 12f509
if (addr >= (unsigned long)(data))
addr = 0;
break;
}
if (addr >= 0x4000)
addr = 0x2000;
break;
case data_from_prog:
case data_from_data:
delay (1000);
tmp1 = p_in ();
for (i = 0; i < 14; i++)
shift |= p_in () << i;
tmp2 = p_in ();
set_clock_data (0, 0); // set data down
#ifdef CHECK_START_STOP
// Start and stop bits must be 1. Most of the old chips at least
// conform to this test, but apparently pic12f635 and some other
// later chips do not.
if (!tmp1 || !tmp2) {
cerr << portname << ":PIC programmer missing or chip fault" << endl;
return -1;
}
#endif
if (data_from_data == comm) {
// Check that the leftover bits were valid, all 1's.
// This detects if the programmer is not connected to the port.
// Unfortunately later chips clear these bits, so we must
// accept both all 1's and all 0's.
if ((shift & 0x3f00) != 0x3f00
&& (shift & 0x3f00) != 0x0000) {
cerr << portname << ": read value "
<< hex << setfill('0') << setw(4) << shift << dec
<< ": PIC programmer or chip fault\n"
"Is code protection enabled? "
"Use --erase option to disable code protection." << endl;
return -1;
}
shift &= 0xff;
}
break;
case load_conf:
addr = 0x2000;
// FALLTHROUGH
case data_for_prog:
case data_for_data:
delay (1000);
p_out (0);
for (i = 0; i < 14; i++)
p_out ((data >> i) & 1);
p_out (0);
set_clock_data (0, 0); // set data down
break;
default:
;
}
delay (1000);
return shift;
}
|