2010-08-13 14:46:38 +03:00
|
|
|
/*
|
|
|
|
* f32x/f32x.c - Simple C8051F326/7 Flash programmer
|
|
|
|
*
|
2010-10-22 20:09:35 +03:00
|
|
|
* Written 2008-2010 by Werner Almesberger
|
|
|
|
* Copyright 2008-2010 Werner Almesberger
|
2010-08-13 14:46:38 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2010-10-22 20:09:35 +03:00
|
|
|
#include <unistd.h>
|
2010-08-13 14:46:38 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "c2.h"
|
|
|
|
#include "flash.h"
|
|
|
|
#include "boundary.h"
|
|
|
|
|
|
|
|
|
2010-11-07 06:52:19 +02:00
|
|
|
struct chip {
|
|
|
|
const char *name; /* NULL for end of list */
|
|
|
|
uint8_t dev_id;
|
|
|
|
int max_flash_kb; /* maximum flash size, in kB */
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mem_layout {
|
|
|
|
int flash_kb; /* real flash size, in kB. 0 for end of list */
|
|
|
|
uint16_t lock_byte; /* location of lock byte */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct chip chips[] = {
|
|
|
|
{ "C8051F320/1", 0x09, 16 },
|
|
|
|
{ "C8051F326/7", 0x0d, 16 },
|
|
|
|
{ "C8051F34x", 0x0f, 64 },
|
|
|
|
{ NULL, }
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct mem_layout mem_layouts[] = {
|
|
|
|
{ 16, 0x3dff },
|
|
|
|
{ 32, 0x7fff },
|
|
|
|
{ 64, 0xfbff },
|
|
|
|
{ 0, }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct chip *chip = NULL;
|
|
|
|
static const struct mem_layout *layout = NULL;
|
2010-08-13 14:46:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
static size_t file_size;
|
|
|
|
|
|
|
|
|
2010-11-07 06:52:19 +02:00
|
|
|
/* ----- flash ------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2010-08-13 14:46:38 +03:00
|
|
|
static void dump(const char *title, void *data, size_t size)
|
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
fprintf(stderr, "%s:\n", title);
|
|
|
|
for (i = 0; i < size; i += 16) {
|
|
|
|
fprintf(stderr, " %04x", i);
|
|
|
|
for (j = 0; j != 16 && i+j < size; j++)
|
|
|
|
fprintf(stderr, " %02x", ((uint8_t *) data)[i+j]);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void flash_device(void *data, size_t size)
|
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
int i;
|
|
|
|
size_t len;
|
|
|
|
uint8_t buf[256];
|
|
|
|
|
|
|
|
for (i = 0; i < size; i += 256)
|
|
|
|
fputc('-', stderr);
|
|
|
|
fputc('\r', stderr);
|
|
|
|
|
|
|
|
flash_init();
|
|
|
|
flash_device_erase();
|
|
|
|
|
|
|
|
for (i = 0; i < size; i += 256) {
|
|
|
|
fputc('*', stderr);
|
|
|
|
fflush(stderr);
|
|
|
|
len = size-i <= 256 ? size-i : 256;
|
|
|
|
flash_block_write(i, data+i, len);
|
|
|
|
}
|
|
|
|
fputc('\r', stderr);
|
|
|
|
|
|
|
|
for (i = 0; i < size; i += 256) {
|
|
|
|
fputc('#', stderr);
|
|
|
|
fflush(stderr);
|
|
|
|
len = size-i <= 256 ? size-i : 256;
|
|
|
|
flash_block_read(i, buf, len);
|
|
|
|
if (memcmp(buf, data+i, len)) {
|
|
|
|
fprintf(stderr, "compare error at 0x%04x\n", i);
|
|
|
|
dump("Expected", data+i, len);
|
|
|
|
dump("Read", buf, len);
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
2010-10-21 16:05:26 +03:00
|
|
|
fputc('\n', stderr);
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-07 06:52:19 +02:00
|
|
|
static void do_flash(const char *name)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
uint8_t code[16384];
|
|
|
|
|
|
|
|
file = fopen(name, "r");
|
|
|
|
if (!file) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
file_size = fread(code, 1, sizeof(code), file);
|
|
|
|
(void) fclose(file);
|
|
|
|
flash_device(code, file_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- protect ----------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static void protect(void)
|
|
|
|
{
|
|
|
|
uint8_t pages, lock_byte;
|
|
|
|
|
|
|
|
pages = (file_size+511) >> 9;
|
|
|
|
printf("Protecting %d page%s\n", pages, pages == 1 ? "" : "s");
|
|
|
|
lock_byte = ~pages;
|
|
|
|
flash_block_write(layout->lock_byte, &lock_byte, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- erase ------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2010-08-13 14:46:38 +03:00
|
|
|
static void erase_flash(void)
|
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
flash_init();
|
|
|
|
flash_device_erase();
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-07 06:52:19 +02:00
|
|
|
/* ----- dump -------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2010-08-13 14:46:38 +03:00
|
|
|
static void dump_flash(size_t size)
|
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
int i, j;
|
|
|
|
size_t len;
|
|
|
|
uint8_t buf[256], last[256];
|
|
|
|
int skipping = 0;
|
|
|
|
|
|
|
|
flash_init();
|
|
|
|
for (i = 0; i < size; i += 16) {
|
|
|
|
len = size-i <= 16 ? size-i : 16;
|
|
|
|
flash_block_read(i, buf, len);
|
|
|
|
if (i && !memcmp(last, buf, len)) {
|
|
|
|
printf("%04x: *%c", i, skipping ? '\r' : '\n');
|
|
|
|
fflush(stdout);
|
|
|
|
skipping = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
skipping = 0;
|
|
|
|
memcpy(last, buf, len);
|
|
|
|
printf("%04x:", i);
|
|
|
|
for (j = 0; j != len; j++)
|
|
|
|
printf(" %02x", buf[j]);
|
|
|
|
printf(" ");
|
|
|
|
for (j = 0; j != len; j++)
|
|
|
|
printf("%c",
|
|
|
|
buf[j] >= ' ' && buf[j] <= '~' ? buf[j] : '.');
|
|
|
|
putchar('\n');
|
|
|
|
fflush(stdout);
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-07 06:52:19 +02:00
|
|
|
/* ----- identify ---------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static void identify_chip(uint8_t dev)
|
|
|
|
{
|
|
|
|
const struct chip *p;
|
|
|
|
|
|
|
|
for (p = chips; p->name; p++)
|
|
|
|
if (p->dev_id == dev) {
|
|
|
|
chip = p;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-13 14:46:38 +03:00
|
|
|
static void identify(void)
|
|
|
|
{
|
2010-11-07 06:52:19 +02:00
|
|
|
int i, same = 1;
|
|
|
|
uint8_t dev, same_dev;
|
2010-10-21 16:05:26 +03:00
|
|
|
|
|
|
|
c2_addr_write(0);
|
2010-11-07 06:52:19 +02:00
|
|
|
fprintf(stderr, "Dev");
|
|
|
|
for (i = 0; i != 10; i++) {
|
|
|
|
dev = c2_data_read(1);
|
|
|
|
fprintf(stderr, " 0x%02x", dev);
|
|
|
|
if (!i)
|
|
|
|
same_dev = dev;
|
|
|
|
else {
|
|
|
|
if (same_dev != dev)
|
|
|
|
same = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!same) {
|
|
|
|
fprintf(stderr, "\ncommunication error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
identify_chip(same_dev);
|
|
|
|
|
2010-10-21 16:05:26 +03:00
|
|
|
c2_addr_write(1);
|
2010-11-07 06:52:19 +02:00
|
|
|
fprintf(stderr, "\nRev");
|
2010-10-21 16:05:26 +03:00
|
|
|
for (i = 0; i != 10; i++)
|
2010-11-07 06:52:19 +02:00
|
|
|
fprintf(stderr, " 0x%02x", c2_data_read(1));
|
|
|
|
fprintf(stderr, "\n");
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-07 06:52:19 +02:00
|
|
|
static void determine_layout(int kb)
|
2010-08-13 14:46:38 +03:00
|
|
|
{
|
2010-11-07 06:52:19 +02:00
|
|
|
if (kb) {
|
|
|
|
if (chip && kb > chip->max_flash_kb) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"chip has only %d kB of flash\n",
|
|
|
|
chip->max_flash_kb);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (chip)
|
|
|
|
kb = chip->max_flash_kb;
|
|
|
|
}
|
2010-08-13 14:46:38 +03:00
|
|
|
|
2010-11-07 06:52:19 +02:00
|
|
|
if (!kb)
|
|
|
|
return;
|
|
|
|
for (layout = mem_layouts; layout->flash_kb; layout++)
|
|
|
|
if (layout->flash_kb == kb)
|
|
|
|
break;
|
|
|
|
if (!layout->flash_kb) {
|
|
|
|
fprintf(stderr, "no memory layout found for %d kB\n", kb);
|
2010-10-21 16:05:26 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-07 06:52:19 +02:00
|
|
|
/* ----- main -------------------------------------------------------------- */
|
2010-08-13 14:46:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
static void usage(const char *name)
|
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
fprintf(stderr,
|
2010-11-07 06:52:19 +02:00
|
|
|
"usage: %s [-n] [-p] [-k flash_kb] file\n"
|
|
|
|
" %s [-n] [-k flash_kb] -d\n"
|
2010-10-22 20:09:35 +03:00
|
|
|
" %s [-n] -e\n"
|
|
|
|
" %s [-n] -b pin_setup\n"
|
|
|
|
" %s [-n]\n\n"
|
2010-08-13 14:46:38 +03:00
|
|
|
" -b pin_setup\n"
|
|
|
|
" Perform a boundary scan. pin_setup sets all 14 pins in this order:\n"
|
|
|
|
" P0_0, P0_1, ..., P0_7, P2_0, ..., P2_5.\n"
|
|
|
|
" Pins can be set to 0, 1, or R (pull-up). Dots can be used to structure\n"
|
|
|
|
" the bit string. Prints what the pins read back (0 or 1) in the same\n"
|
|
|
|
" order, with a dot between P0 and P2.\n"
|
2010-11-07 06:52:19 +02:00
|
|
|
" -d dump Flash content\n"
|
|
|
|
" -e erase whole Flash\n"
|
|
|
|
" -k flash_kb set flash size (16/32/64; default: auto-select)\n"
|
|
|
|
" -n do not provide power to the target (default: do provide power)\n"
|
|
|
|
" -p protect the data after writing\n"
|
2010-08-13 14:46:38 +03:00
|
|
|
"Invocation without argument resets the F32x.\n"
|
|
|
|
, name, name, name, name, name);
|
2010-10-21 16:05:26 +03:00
|
|
|
exit(1);
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-07 06:52:19 +02:00
|
|
|
static void need_layout(void)
|
|
|
|
{
|
|
|
|
if (layout)
|
|
|
|
return;
|
|
|
|
fprintf(stderr, "please specify flash size with -k flash_kB\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-13 14:46:38 +03:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2010-10-22 20:09:35 +03:00
|
|
|
enum {
|
|
|
|
mode_default = 0,
|
|
|
|
mode_flash,
|
|
|
|
mode_dump,
|
|
|
|
mode_erase,
|
|
|
|
mode_scan,
|
|
|
|
} mode = mode_default;
|
2010-11-07 06:52:19 +02:00
|
|
|
int do_protect = 0, power = 1, kb = 0;
|
2010-10-22 20:09:35 +03:00
|
|
|
int c;
|
|
|
|
|
2010-11-07 06:52:19 +02:00
|
|
|
while ((c = getopt(argc, argv, "bdek:np")) != EOF)
|
2010-10-22 20:09:35 +03:00
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
|
|
|
if (mode)
|
|
|
|
usage(*argv);
|
|
|
|
mode = mode_dump;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
if (mode)
|
|
|
|
usage(*argv);
|
|
|
|
mode = mode_erase;;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
if (mode)
|
|
|
|
usage(*argv);
|
|
|
|
mode = mode_scan;;
|
|
|
|
break;
|
2010-11-07 06:52:19 +02:00
|
|
|
case 'k':
|
|
|
|
kb = atoi(optarg);
|
|
|
|
break;
|
2010-10-22 20:09:35 +03:00
|
|
|
case 'n':
|
|
|
|
power = 0;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
do_protect = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
2010-10-21 16:05:26 +03:00
|
|
|
|
2010-10-22 20:09:35 +03:00
|
|
|
switch (mode) {
|
|
|
|
case mode_default:
|
|
|
|
switch (argc-optind) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
mode = mode_flash;
|
|
|
|
break;
|
|
|
|
default:
|
2010-10-21 16:05:26 +03:00
|
|
|
usage(*argv);
|
|
|
|
}
|
|
|
|
break;
|
2010-10-22 20:09:35 +03:00
|
|
|
case mode_scan:
|
|
|
|
if (argc != optind+1)
|
|
|
|
usage(*argv);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (argc != optind)
|
2010-10-21 16:05:26 +03:00
|
|
|
usage(*argv);
|
2010-10-22 20:09:35 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-11-07 06:52:19 +02:00
|
|
|
if (do_protect && mode != mode_flash)
|
|
|
|
usage(*argv);
|
2010-10-22 20:09:35 +03:00
|
|
|
|
|
|
|
c2_init(power);
|
|
|
|
identify();
|
2010-11-07 06:52:19 +02:00
|
|
|
determine_layout(kb);
|
|
|
|
|
|
|
|
if (chip)
|
|
|
|
fprintf(stderr, "%s, ", chip->name);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "unknown chip, ");
|
|
|
|
if (layout)
|
|
|
|
fprintf(stderr, "flash size: %d kB\n", layout->flash_kb);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "flash size unknown\n");
|
2010-10-22 20:09:35 +03:00
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case mode_default:
|
|
|
|
/* just reset */
|
|
|
|
break;
|
|
|
|
case mode_dump:
|
2010-11-07 06:52:19 +02:00
|
|
|
need_layout();
|
|
|
|
dump_flash(layout->lock_byte+2);
|
2010-10-22 20:09:35 +03:00
|
|
|
break;
|
|
|
|
case mode_erase:
|
|
|
|
erase_flash();
|
|
|
|
break;
|
|
|
|
case mode_flash:
|
2010-11-07 06:52:19 +02:00
|
|
|
if (do_protect)
|
|
|
|
need_layout();
|
2010-10-22 20:09:35 +03:00
|
|
|
do_flash(argv[optind]);
|
|
|
|
if (do_protect)
|
|
|
|
protect();
|
|
|
|
identify();
|
|
|
|
break;
|
|
|
|
case mode_scan:
|
|
|
|
boundary(argv[optind]);
|
2010-10-21 16:05:26 +03:00
|
|
|
break;
|
|
|
|
default:
|
2010-10-22 20:09:35 +03:00
|
|
|
abort();
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
2010-10-22 20:09:35 +03:00
|
|
|
|
2010-10-21 16:05:26 +03:00
|
|
|
c2_reset();
|
|
|
|
|
|
|
|
return 0;
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|