2012-12-29 02:00:28 +02:00
|
|
|
/*
|
|
|
|
* lpc111x-isp/lpc111x.c - LPC111x/LPC11Cxx ISP programmer
|
|
|
|
*
|
|
|
|
* Written 2012 by Werner Almesberger
|
|
|
|
* Copyright 2012 Werner Almesberger
|
|
|
|
*
|
|
|
|
* 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-12-29 04:26:13 +02:00
|
|
|
#define _GNU_SOURCE /* for vasprintf */
|
|
|
|
#include <stdarg.h>
|
2012-12-29 02:00:28 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2013-01-02 15:33:28 +02:00
|
|
|
#include <strings.h> /* for strcasecmp, strncasecmp */
|
2012-12-29 03:45:26 +02:00
|
|
|
#include <alloca.h>
|
2012-12-31 04:29:24 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/types.h>
|
2012-12-29 02:00:28 +02:00
|
|
|
|
|
|
|
#include <ubb/ubb.h>
|
|
|
|
#include <ubb/swuart.h>
|
|
|
|
|
|
|
|
|
2012-12-31 04:29:24 +02:00
|
|
|
#define BPS 115200
|
|
|
|
|
2012-12-30 03:52:33 +02:00
|
|
|
#define MAX_BUF 10000 /* receive buffer */
|
2012-12-31 04:29:24 +02:00
|
|
|
|
2012-12-29 03:45:26 +02:00
|
|
|
#define AUTOBAUD_TRIES 10
|
|
|
|
#define SYNC "Synchronized"
|
2012-12-29 02:00:28 +02:00
|
|
|
|
2012-12-31 04:29:24 +02:00
|
|
|
#define MAX_RECORD 45 /* data bytes per uuencoded record */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* UM 26.4.8 pg 416 says 0x1000017C to 0x1000025B and the last 256+32 bytes
|
|
|
|
* of the RAM are taken.
|
|
|
|
*
|
|
|
|
* UM 26.5.4 pg 418 hints 0x10000300 is a good place.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define RAM_BUFFER 0x10000300
|
|
|
|
#define SECTOR 4096
|
|
|
|
#define PAGE 256
|
|
|
|
|
2013-01-02 14:58:45 +02:00
|
|
|
|
|
|
|
enum {
|
|
|
|
pin_rxd, /* RXD pin on the device (PIO1_6) */
|
|
|
|
pin_txd, /* TXD pin on the device (PIO1_7) */
|
|
|
|
pin_nisp, /* nISP: ISP mode selection (PIO0_1) */
|
|
|
|
pin_nreset, /* nRESET pin (PIO0_0) */
|
|
|
|
pin_end /* last value */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct pin {
|
|
|
|
const char *name;
|
|
|
|
const char *alt_name;
|
|
|
|
uint32_t pin;
|
2013-01-02 15:33:28 +02:00
|
|
|
} pins[] = {
|
2013-01-02 14:58:45 +02:00
|
|
|
[pin_rxd] = { "RXD", "P1_6", UBB_DAT2 },
|
|
|
|
[pin_txd] = { "TXD", "P1_7", UBB_DAT3 },
|
|
|
|
[pin_nisp] = { "nISP", "P0_1", UBB_DAT1 },
|
|
|
|
[pin_nreset] = { "nRESET", "P0_0", UBB_CMD },
|
|
|
|
[pin_end] = { NULL }
|
|
|
|
};
|
|
|
|
|
2013-01-02 15:33:28 +02:00
|
|
|
static const struct signal {
|
|
|
|
const char *name;
|
|
|
|
uint32_t pin;
|
|
|
|
} signals[] = {
|
|
|
|
{ "CMD", UBB_CMD },
|
|
|
|
{ "CLK", UBB_CLK },
|
|
|
|
{ "DAT0", UBB_DAT0 },
|
|
|
|
{ "DAT1", UBB_DAT1 },
|
|
|
|
{ "DAT2", UBB_DAT2 },
|
|
|
|
{ "DAT3", UBB_DAT3 },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2012-12-29 04:12:37 +02:00
|
|
|
static int verbose = 0;
|
2012-12-29 11:27:04 +02:00
|
|
|
static int quiet = 0;
|
2012-12-29 04:12:37 +02:00
|
|
|
|
2012-12-29 03:45:26 +02:00
|
|
|
|
|
|
|
/* ----- Debugging and tracing --------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static void trace(const char *label, const uint8_t *s, int len)
|
2012-12-29 02:00:28 +02:00
|
|
|
{
|
|
|
|
const uint8_t *end = s+len;
|
|
|
|
|
2012-12-29 03:45:26 +02:00
|
|
|
if (label)
|
2012-12-30 03:52:33 +02:00
|
|
|
fprintf(stderr, "%s ", label);
|
2012-12-29 02:00:28 +02:00
|
|
|
for (end = s+len; s != end; s++) {
|
|
|
|
if (*s >= ' ' && *s <= '~')
|
2012-12-30 03:52:33 +02:00
|
|
|
fprintf(stderr, "%c", *s);
|
2012-12-29 02:00:28 +02:00
|
|
|
else if (*s == 10)
|
2012-12-30 03:52:33 +02:00
|
|
|
fprintf(stderr, "\\n");
|
2012-12-29 02:00:28 +02:00
|
|
|
else if (*s == 13)
|
2012-12-30 03:52:33 +02:00
|
|
|
fprintf(stderr, "\\r");
|
2012-12-29 02:00:28 +02:00
|
|
|
else
|
2012-12-30 03:52:33 +02:00
|
|
|
fprintf(stderr, "\\%02o", *s);
|
2012-12-29 02:00:28 +02:00
|
|
|
}
|
2012-12-30 03:52:33 +02:00
|
|
|
fprintf(stderr, "\n");
|
2012-12-29 02:00:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-29 03:45:26 +02:00
|
|
|
static void trace_out(const void *s, int len)
|
|
|
|
{
|
|
|
|
trace(">>>", s, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-29 11:27:04 +02:00
|
|
|
static void trace_in(const void *s, int len)
|
2012-12-29 03:45:26 +02:00
|
|
|
{
|
|
|
|
trace("<<<", s, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Dialog functions -------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
static const char *dialog_fixed(int idle, const char *msg)
|
2012-12-29 03:45:26 +02:00
|
|
|
{
|
2012-12-30 01:54:24 +02:00
|
|
|
static char *res = NULL;
|
2012-12-29 04:26:13 +02:00
|
|
|
int msg_len = strlen(msg);
|
|
|
|
char *tx_buf = alloca(msg_len+3);
|
2012-12-30 01:54:24 +02:00
|
|
|
char *rx_buf = alloca(MAX_BUF);
|
|
|
|
char *s, *t;
|
2012-12-29 03:45:26 +02:00
|
|
|
int got;
|
|
|
|
|
2012-12-29 04:26:13 +02:00
|
|
|
memcpy(tx_buf, msg, msg_len);
|
|
|
|
memcpy(tx_buf+msg_len, "\r\n", 2);
|
2012-12-29 03:45:26 +02:00
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
if (verbose)
|
2012-12-29 04:26:13 +02:00
|
|
|
trace_out(tx_buf, msg_len+2);
|
2012-12-30 01:54:24 +02:00
|
|
|
got = swuart_trx(tx_buf, msg_len+2, rx_buf, MAX_BUF, idle, idle);
|
|
|
|
if (verbose)
|
|
|
|
trace_in(rx_buf, got);
|
2012-12-29 03:45:26 +02:00
|
|
|
|
2012-12-29 04:26:13 +02:00
|
|
|
if (got < msg_len+2) {
|
2012-12-29 03:45:26 +02:00
|
|
|
fprintf(stderr, "response too short for echo\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-12-29 04:26:13 +02:00
|
|
|
if (memcmp(rx_buf, msg, msg_len) ||
|
|
|
|
rx_buf[msg_len] != '\r' || rx_buf[msg_len+1] != '\n') {
|
2012-12-29 03:45:26 +02:00
|
|
|
fprintf(stderr, "echo mismatch\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
if (memchr(rx_buf, 0, got)) {
|
|
|
|
fprintf(stderr, "NUL in response\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-12-29 03:45:26 +02:00
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
rx_buf += msg_len+2;
|
2012-12-29 04:26:13 +02:00
|
|
|
got -= msg_len+2;
|
2012-12-29 03:45:26 +02:00
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
res = realloc(res, got+1);
|
|
|
|
if (!res) {
|
|
|
|
perror("realloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
t = res;
|
|
|
|
for (s = rx_buf; s != rx_buf+got; s++) {
|
|
|
|
if (*s == '\r') {
|
|
|
|
if (s+1 != rx_buf+got && s[1] == '\n')
|
|
|
|
continue;
|
|
|
|
fprintf(stderr, "\\r without \\n\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-12-30 03:52:33 +02:00
|
|
|
if (*s == '\n' && s+1 == rx_buf+got)
|
2012-12-30 01:54:24 +02:00
|
|
|
break;
|
|
|
|
*t++ = *s;
|
|
|
|
}
|
|
|
|
*t = 0;
|
|
|
|
|
|
|
|
return res;
|
2012-12-29 03:45:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
static const char *vdialog(int idle, const char *cmd, va_list ap)
|
2012-12-29 04:26:13 +02:00
|
|
|
{
|
|
|
|
char *msg;
|
|
|
|
|
|
|
|
vasprintf(&msg, cmd, ap);
|
2012-12-30 01:54:24 +02:00
|
|
|
return dialog_fixed(idle, msg);
|
2012-12-29 04:26:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
static const char *dialog(int idle, const char *cmd, ...)
|
2012-12-29 04:26:13 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
2012-12-30 01:54:24 +02:00
|
|
|
const char *res;
|
2012-12-29 04:26:13 +02:00
|
|
|
|
|
|
|
va_start(ap, cmd);
|
2012-12-30 01:54:24 +02:00
|
|
|
res = vdialog(idle, cmd, ap);
|
2012-12-29 04:26:13 +02:00
|
|
|
va_end(ap);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
static const char *dialog_rc(int idle, const char *cmd, ...)
|
2012-12-29 11:27:04 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
2012-12-30 01:54:24 +02:00
|
|
|
const char *res, *p;
|
2012-12-29 11:27:04 +02:00
|
|
|
unsigned rc;
|
|
|
|
|
|
|
|
va_start(ap, cmd);
|
2012-12-30 01:54:24 +02:00
|
|
|
res = vdialog(idle, cmd, ap);
|
2012-12-29 11:27:04 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
p = strchr(res, '\n');
|
2012-12-31 04:29:24 +02:00
|
|
|
if (!p)
|
|
|
|
p = strchr(res, 0);
|
|
|
|
if (sscanf(res, "%u", &rc) != 1) {
|
2012-12-29 11:27:04 +02:00
|
|
|
fprintf(stderr, "invalid status\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
if (rc != 0) {
|
|
|
|
fprintf(stderr, "rc %d\n", rc);
|
|
|
|
exit(1);
|
2012-12-29 11:27:04 +02:00
|
|
|
}
|
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
return p+1;
|
2012-12-29 11:27:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-29 03:45:26 +02:00
|
|
|
static int autobaud(void)
|
|
|
|
{
|
|
|
|
uint8_t reply[100];
|
|
|
|
int i, got;
|
2012-12-30 01:54:24 +02:00
|
|
|
const char *res;
|
2012-12-29 03:45:26 +02:00
|
|
|
|
|
|
|
for (i = 0; i != AUTOBAUD_TRIES; i++) {
|
2013-01-02 15:33:28 +02:00
|
|
|
CLR(pins[pin_nreset].pin);
|
2012-12-29 03:45:26 +02:00
|
|
|
usleep(10); /* DS Table 9 pg 29 says min 50 ns */
|
2013-01-02 15:33:28 +02:00
|
|
|
SET(pins[pin_nreset].pin);
|
2012-12-29 03:45:26 +02:00
|
|
|
|
|
|
|
usleep(5*1000); /* UM 26.3.1 pg 408 says max 3 ms */
|
|
|
|
|
|
|
|
got = swuart_trx("?", 1, reply, sizeof(reply), 1000, 100);
|
2012-12-30 01:54:24 +02:00
|
|
|
if (got != strlen(SYNC)+2 || memcmp(reply, SYNC "\r\n", got))
|
2012-12-29 03:45:26 +02:00
|
|
|
continue;
|
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
res = dialog(100, SYNC);
|
2012-12-29 03:45:26 +02:00
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
if (!strcmp(res, "OK"))
|
2012-12-29 03:45:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-29 11:27:04 +02:00
|
|
|
/* ----- Devices database -------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static const struct device {
|
|
|
|
const char *name;
|
|
|
|
unsigned id;
|
|
|
|
int flash_kb;
|
|
|
|
} devices[] = {
|
|
|
|
{ "LPC1112FHxxx/202", 0x2524902b, 16 },
|
|
|
|
{ NULL, }
|
|
|
|
}, *device = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
static void identify(void)
|
|
|
|
{
|
2012-12-30 01:54:24 +02:00
|
|
|
const char *res;
|
2012-12-30 02:12:09 +02:00
|
|
|
unsigned id, serial[4];
|
2012-12-29 11:27:04 +02:00
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
res = dialog_rc(100, "J");
|
|
|
|
if (sscanf(res, "%u", &id) != 1) {
|
|
|
|
fprintf(stderr, "J: cannot parse ID \"%s\"\n", res);
|
2012-12-29 11:27:04 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-12-30 02:12:09 +02:00
|
|
|
res = dialog_rc(100, "N");
|
|
|
|
if (sscanf(res, "%u %u %u %u",
|
|
|
|
serial, serial+1, serial+2, serial+3) != 4) {
|
|
|
|
fprintf(stderr, "N: cannot parse serial number\"%s\"\n", res);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-12-29 11:27:04 +02:00
|
|
|
for (device = devices; device->name; device++)
|
|
|
|
if (device->id == id) {
|
|
|
|
if (!quiet)
|
2012-12-30 02:12:09 +02:00
|
|
|
fprintf(stderr, "%s (0x%04x %04x) %d kB "
|
|
|
|
"serial %08x.%08x.%08x.%08x\n",
|
2012-12-29 11:27:04 +02:00
|
|
|
device->name, id >> 16, id & 0xffff,
|
2012-12-30 02:12:09 +02:00
|
|
|
device->flash_kb,
|
|
|
|
serial[0], serial[1], serial[2], serial[3]);
|
2012-12-29 11:27:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "unknown device 0x%04x %04x\n", id >> 16, id & 0xffff);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-31 04:29:24 +02:00
|
|
|
/* ----- Flash programming ------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static int ms_to_bits(int ms)
|
|
|
|
{
|
|
|
|
return (BPS*ms+999)/1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char uuechar(uint8_t b)
|
|
|
|
{
|
|
|
|
b &= 0x3f;
|
|
|
|
return b ? b+32 : 0x60;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char *uuencode(const uint8_t *p, int len)
|
|
|
|
{
|
|
|
|
static char buf[80];
|
|
|
|
char *t = buf;
|
|
|
|
const uint8_t *end = p+len;
|
|
|
|
unsigned tmp = 0; /* initialize to prevent compiler complaints */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
*t++ = len+32;
|
|
|
|
while (p != end) {
|
|
|
|
for (i = 0; i != 3; i++)
|
|
|
|
if (p == end)
|
|
|
|
tmp <<= 8;
|
|
|
|
else
|
|
|
|
tmp = tmp << 8 | *p++;
|
|
|
|
*t++ = uuechar(tmp >> 18);
|
|
|
|
*t++ = uuechar(tmp >> 12);
|
|
|
|
*t++ = uuechar(tmp >> 6);
|
|
|
|
*t++ = uuechar(tmp);
|
|
|
|
}
|
|
|
|
*t = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void flash_erase_sectors(unsigned addr, unsigned len)
|
|
|
|
{
|
|
|
|
unsigned from = addr/SECTOR;
|
|
|
|
unsigned to = (addr+len-1)/SECTOR;
|
|
|
|
|
|
|
|
assert(!(addr & (SECTOR-1)));
|
|
|
|
assert(!(len & (SECTOR-1)));
|
|
|
|
|
|
|
|
dialog_rc(100, "P %d %d", from, to);
|
|
|
|
|
|
|
|
/* DS 10.2 pg 77: t_er(max) = 105 ms */
|
|
|
|
dialog_rc(ms_to_bits(200), "E %d %d", from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void flash_write_page(unsigned addr, const uint8_t *buf)
|
|
|
|
{
|
|
|
|
unsigned sector = addr/SECTOR;
|
|
|
|
unsigned sum;
|
|
|
|
int chunk, i;
|
|
|
|
const char *res;
|
|
|
|
|
|
|
|
assert(!(addr & (PAGE-1)));
|
|
|
|
|
|
|
|
dialog_rc(100, "W %u %u", RAM_BUFFER, PAGE);
|
|
|
|
for (i = 0; i != PAGE; i += chunk) {
|
|
|
|
chunk = PAGE-i > MAX_RECORD ? MAX_RECORD : PAGE-i;
|
|
|
|
dialog(100, "%s", uuencode(buf+i, chunk));
|
|
|
|
}
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
for (i = 0; i != PAGE; i++)
|
|
|
|
sum += buf[i];
|
|
|
|
|
|
|
|
res = dialog(100, "%u", sum);
|
|
|
|
if (strcmp(res, "OK")) {
|
|
|
|
fprintf(stderr, "non-OK response: \"%s\"\n", res);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog_rc(100, "P %d %d", sector, sector);
|
|
|
|
|
|
|
|
/* DS 10.2 pg 77: t_prog(max) = 1.05 ms */
|
|
|
|
dialog_rc(ms_to_bits(2), "C %d %u %d", addr, RAM_BUFFER, PAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void flash_write(unsigned addr, const uint8_t *p, int len,
|
|
|
|
int erase_all)
|
|
|
|
{
|
|
|
|
uint8_t page[PAGE];
|
|
|
|
|
|
|
|
assert(!(addr & (PAGE-1)));
|
|
|
|
|
|
|
|
dialog_rc(100, "U 23130");
|
|
|
|
if (erase_all)
|
|
|
|
flash_erase_sectors(0, device->flash_kb << 10);
|
|
|
|
else
|
|
|
|
flash_erase_sectors(addr & ~(SECTOR-1),
|
|
|
|
(addr+len+SECTOR-1) & ~(SECTOR-1));
|
|
|
|
while (len > 0) {
|
|
|
|
if (len < PAGE) {
|
|
|
|
memcpy(page, p, len);
|
|
|
|
memset(page+len, 0xff, PAGE-len);
|
|
|
|
} else {
|
|
|
|
memcpy(page, p, PAGE);
|
|
|
|
p += PAGE;
|
|
|
|
}
|
|
|
|
flash_write_page(addr, page);
|
|
|
|
addr += PAGE;
|
|
|
|
len -= PAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void flash_file(FILE *file)
|
|
|
|
{
|
|
|
|
uint8_t buf[(device->flash_kb << 10)+1];
|
|
|
|
size_t got;
|
|
|
|
|
|
|
|
got = fread(buf, 1, sizeof(buf), file);
|
|
|
|
if (!got) {
|
|
|
|
if (ferror(file))
|
|
|
|
perror("fread");
|
|
|
|
else
|
|
|
|
fprintf(stderr, "file is empty\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (got > device->flash_kb << 10) {
|
|
|
|
fprintf(stderr, "file is larger than flash (%d kB)\n",
|
|
|
|
device->flash_kb);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
flash_write(0, buf, got, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-30 03:52:33 +02:00
|
|
|
/* ----- Flash dump -------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2012-12-31 04:29:24 +02:00
|
|
|
/*
|
|
|
|
* The uuencoding algorithm is described in AN11229.
|
|
|
|
* It's the same as used by uuencode.
|
|
|
|
*/
|
2012-12-30 03:52:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
static uint8_t uudchar(char c)
|
|
|
|
{
|
|
|
|
if (c <= 0x20 && c > 0x60) {
|
|
|
|
fprintf(stderr, "invalid UU character '%c'\n", c);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return c == 0x60 ? 0 : c-32;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int uudecode(const char *s, uint8_t *buf)
|
|
|
|
{
|
|
|
|
int len, uu_len;
|
|
|
|
const char *nl;
|
|
|
|
const uint8_t *end;
|
|
|
|
|
|
|
|
len = *s-32;
|
|
|
|
if (len < 0 || len > MAX_RECORD) {
|
|
|
|
fprintf(stderr, "invalid UU length (%d)\n", len);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
nl = strchr(++s, '\n');
|
|
|
|
if (!nl) {
|
|
|
|
fprintf(stderr, "no \\n at end of UU record\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
uu_len = nl-s;
|
|
|
|
if (uu_len & 3) {
|
|
|
|
fprintf(stderr, "UU length %d not a multiple of 4\n", uu_len);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if ((len+2)/3 != uu_len/4) {
|
|
|
|
fprintf(stderr, "UU length %d vs. %d bytes (\"%.*s\")\n",
|
|
|
|
uu_len, len, nl-s+1, s-1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
end = buf+len;
|
|
|
|
while (buf != end) {
|
|
|
|
unsigned tmp = uudchar(s[0]) << 18 | uudchar(s[1]) << 12 |
|
|
|
|
uudchar(s[2]) << 6 | uudchar(s[3]);
|
|
|
|
|
|
|
|
*buf++ = tmp >> 16;
|
|
|
|
if (buf != end)
|
|
|
|
*buf++ = tmp >> 8;
|
|
|
|
if (buf != end)
|
|
|
|
*buf++ = tmp;
|
|
|
|
s += 4;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void dump(void)
|
|
|
|
{
|
|
|
|
int addr = 0, end;
|
|
|
|
unsigned sum = 0, check;
|
|
|
|
const char *res, *nl;
|
|
|
|
int got, wrote, i;
|
|
|
|
|
|
|
|
end = device->flash_kb << 10;
|
|
|
|
res = dialog_rc(100, "R 0 %u", end);
|
|
|
|
while (addr != end) {
|
|
|
|
while (1) {
|
|
|
|
uint8_t buf[MAX_RECORD];
|
|
|
|
|
|
|
|
nl = strchr(res, '\n');
|
|
|
|
if (!nl)
|
|
|
|
break;
|
|
|
|
got = uudecode(res, buf);
|
|
|
|
for (i = 0; i != got; i++)
|
|
|
|
sum += buf[i];
|
|
|
|
wrote = fwrite(buf, 1, got, stdout);
|
|
|
|
if (wrote != got) {
|
|
|
|
perror("fwrite");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
addr += got;
|
|
|
|
res = nl+1;
|
|
|
|
}
|
|
|
|
if (sscanf(res, "%u", &check) != 1) {
|
|
|
|
fprintf(stderr, "can't parse checksum \"%s\"\n", res);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (check != sum) {
|
|
|
|
fprintf(stderr, "checksum error: got %u received %u\n",
|
|
|
|
sum, check);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
res = dialog(100, "OK");
|
|
|
|
sum = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-29 03:45:26 +02:00
|
|
|
/* ----- ISP session ------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2012-12-29 02:00:28 +02:00
|
|
|
static void at_exit(void)
|
|
|
|
{
|
|
|
|
ubb_close(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-30 02:18:32 +02:00
|
|
|
static void start_isp(int power)
|
2012-12-29 02:00:28 +02:00
|
|
|
{
|
2012-12-30 01:54:24 +02:00
|
|
|
const char *res;
|
2012-12-29 02:00:28 +02:00
|
|
|
|
|
|
|
if (ubb_open(0) < 0) {
|
|
|
|
perror("ubb_open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
atexit(at_exit);
|
|
|
|
|
2012-12-30 02:18:32 +02:00
|
|
|
if (power)
|
|
|
|
ubb_power(1);
|
2012-12-29 02:00:28 +02:00
|
|
|
|
|
|
|
usleep(100*1000);
|
|
|
|
|
2013-01-02 15:33:28 +02:00
|
|
|
SET(pins[pin_nreset].pin);
|
|
|
|
OUT(pins[pin_nreset].pin);
|
2012-12-29 02:00:28 +02:00
|
|
|
|
2013-01-02 15:33:28 +02:00
|
|
|
CLR(pins[pin_nisp].pin);
|
|
|
|
OUT(pins[pin_nisp].pin);
|
2012-12-29 02:00:28 +02:00
|
|
|
|
2013-01-02 15:33:28 +02:00
|
|
|
if (swuart_open(pins[pin_rxd].pin, pins[pin_txd].pin, BPS) < 0) {
|
2013-01-01 00:29:35 +02:00
|
|
|
perror("swuart_open");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-12-29 02:00:28 +02:00
|
|
|
|
2012-12-29 03:45:26 +02:00
|
|
|
if (!autobaud()) {
|
|
|
|
fprintf(stderr, "target is not responding\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-12-29 02:00:28 +02:00
|
|
|
|
2012-12-30 01:54:24 +02:00
|
|
|
res = dialog(100, "12000");
|
|
|
|
if (strcmp(res, "OK")) {
|
2012-12-29 03:45:26 +02:00
|
|
|
fprintf(stderr, "cannot set clock rate\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2012-12-29 02:00:28 +02:00
|
|
|
|
|
|
|
|
2012-12-31 06:54:36 +02:00
|
|
|
/* ----- Reset and run target ---------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static void run_target(int power)
|
|
|
|
{
|
|
|
|
if (ubb_open(0) < 0) {
|
|
|
|
perror("ubb_open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (power)
|
|
|
|
ubb_power(1);
|
|
|
|
|
2013-01-02 15:33:28 +02:00
|
|
|
SET(pins[pin_nreset].pin);
|
|
|
|
OUT(pins[pin_nreset].pin);
|
2012-12-31 06:54:36 +02:00
|
|
|
|
2013-01-02 15:33:28 +02:00
|
|
|
IN(pins[pin_nisp].pin);
|
2012-12-31 06:54:36 +02:00
|
|
|
|
2013-01-02 15:33:28 +02:00
|
|
|
CLR(pins[pin_nreset].pin);
|
2012-12-31 06:54:36 +02:00
|
|
|
usleep(10); /* DS Table 9 pg 29 says min 50 ns */
|
2013-01-02 15:33:28 +02:00
|
|
|
SET(pins[pin_nreset].pin);
|
2012-12-31 06:54:36 +02:00
|
|
|
|
2013-01-02 15:33:28 +02:00
|
|
|
ubb_close(UBB_nPWR | pins[pin_nreset].pin | pins[pin_nisp].pin);
|
2012-12-31 06:54:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-29 03:45:26 +02:00
|
|
|
/* ----- Command-line processing ------------------------------------------- */
|
|
|
|
|
|
|
|
|
2012-12-29 04:12:37 +02:00
|
|
|
static void usage(const char *name)
|
|
|
|
{
|
2013-01-02 15:33:28 +02:00
|
|
|
const struct signal *signal;
|
|
|
|
int i;
|
|
|
|
|
2012-12-29 04:12:37 +02:00
|
|
|
fprintf(stderr,
|
2013-01-02 15:33:28 +02:00
|
|
|
"usage: %s [-P function=signal ...] [-n] [-q] [-v ...] [file.bin]\n"
|
|
|
|
" %s [-P function=signal ...] [-n] -r\n\n"
|
|
|
|
" -n don't power the device\n"
|
|
|
|
" -q suppress basic progress messages\n"
|
|
|
|
" -P function=signal assign a 8:10 interface signal to a function "
|
|
|
|
"(see below)\n"
|
|
|
|
" -r reset the target and let it run\n"
|
|
|
|
" -v increase verbosity level\n\n"
|
|
|
|
"Functions: RXD (P1_6), TXD (P1_7), nISP (P0_1), nRESET (P0_0)\n"
|
|
|
|
"Signals: CMD, CLK, DAT0, DAT1, DAT2, DAT3\n"
|
2012-12-31 06:54:36 +02:00
|
|
|
, name, name);
|
2013-01-02 15:33:28 +02:00
|
|
|
|
|
|
|
fprintf(stderr, "Default mapping:");
|
|
|
|
for (i = 0; i != pin_end; i++) {
|
|
|
|
for (signal = signals; signal->pin != pins[i].pin; signal++);
|
|
|
|
fprintf(stderr, " -P %s=%s", pins[i].name, signal->name);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
2012-12-29 04:12:37 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-02 15:33:28 +02:00
|
|
|
static void assign_pin(const char *name, const char *s)
|
|
|
|
{
|
|
|
|
const char *eq;
|
|
|
|
struct pin *pin;
|
|
|
|
const struct signal *signal;
|
|
|
|
|
|
|
|
eq = strchr(s, '=');
|
|
|
|
if (!eq)
|
|
|
|
usage(name);
|
|
|
|
|
|
|
|
for (pin = pins; pin->name; pin++) {
|
|
|
|
if (strlen(pin->name) == eq-s &&
|
|
|
|
!strncasecmp(pin->name, s, eq-s))
|
|
|
|
break;
|
|
|
|
if (strlen(pin->alt_name) == eq-s &&
|
|
|
|
!strncasecmp(pin->alt_name, s, eq-s))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!pin->name) {
|
|
|
|
fprintf(stderr, "unknown function \"%.*s\"\n", eq-s, s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (signal = signals; signal->name; signal++)
|
|
|
|
if (!strcasecmp(signal->name, eq+1))
|
|
|
|
break;
|
|
|
|
if (!signal->name) {
|
|
|
|
fprintf(stderr, "unknown signal \"%s\"\n", eq+1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pin->pin = signal->pin;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-29 03:45:26 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-12-31 04:29:24 +02:00
|
|
|
FILE *file = NULL;
|
2012-12-30 02:18:32 +02:00
|
|
|
int power = 1;
|
2012-12-31 06:54:36 +02:00
|
|
|
int run = 0;
|
2012-12-29 11:27:04 +02:00
|
|
|
int c;
|
2012-12-29 04:12:37 +02:00
|
|
|
|
2013-01-02 15:33:28 +02:00
|
|
|
while ((c = getopt(argc, argv, "nP:qrv")) != EOF)
|
2012-12-29 04:12:37 +02:00
|
|
|
switch (c) {
|
2012-12-30 02:18:32 +02:00
|
|
|
case 'n':
|
|
|
|
power = 0;
|
|
|
|
break;
|
2013-01-02 15:33:28 +02:00
|
|
|
case 'P':
|
|
|
|
assign_pin(*argv, optarg);
|
|
|
|
break;
|
2012-12-29 11:27:04 +02:00
|
|
|
case 'q':
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
2012-12-31 06:54:36 +02:00
|
|
|
case 'r':
|
|
|
|
run = 1;
|
|
|
|
break;
|
2012-12-29 04:12:37 +02:00
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
|
|
|
|
2012-12-31 06:54:36 +02:00
|
|
|
if (run) {
|
|
|
|
if (quiet || verbose || argc != optind)
|
|
|
|
usage(*argv);
|
|
|
|
run_target(power);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-29 04:12:37 +02:00
|
|
|
switch (argc-optind) {
|
|
|
|
case 0:
|
|
|
|
break;
|
2012-12-31 04:29:24 +02:00
|
|
|
case 1:
|
|
|
|
if (!strcmp(argv[optind], "-"))
|
|
|
|
file = stdin;
|
|
|
|
else {
|
|
|
|
file = fopen(argv[optind], "r");
|
|
|
|
if (!file) {
|
|
|
|
perror(argv[optind]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-12-29 04:12:37 +02:00
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
2012-12-29 02:00:28 +02:00
|
|
|
|
2012-12-30 02:18:32 +02:00
|
|
|
start_isp(power);
|
2012-12-29 02:00:28 +02:00
|
|
|
|
2012-12-29 11:27:04 +02:00
|
|
|
identify();
|
2012-12-29 02:00:28 +02:00
|
|
|
|
2012-12-31 04:29:24 +02:00
|
|
|
if (file)
|
|
|
|
flash_file(file);
|
|
|
|
else
|
|
|
|
dump();
|
2012-12-30 03:52:33 +02:00
|
|
|
|
2012-12-29 02:00:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|