2001-01-19 02:46:50 +00:00
|
|
|
/*
|
2002-01-12 01:51:35 +00:00
|
|
|
* Copyright (c) 2000, 2001, 2002 Brian S. Dean <bsd@bsdhome.com>
|
2001-01-19 02:46:50 +00:00
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY BRIAN S. DEAN ``AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BRIAN S. DEAN BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
|
|
|
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2001-10-13 03:12:52 +00:00
|
|
|
#include <string.h>
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "avr.h"
|
2001-10-14 23:17:26 +00:00
|
|
|
#include "config.h"
|
2001-11-21 02:46:55 +00:00
|
|
|
#include "lists.h"
|
2001-01-24 19:10:34 +00:00
|
|
|
#include "pindefs.h"
|
2001-01-19 02:46:50 +00:00
|
|
|
#include "ppi.h"
|
|
|
|
|
|
|
|
|
2002-01-12 01:26:09 +00:00
|
|
|
#define DEBUG 0
|
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
extern char * progname;
|
|
|
|
extern char progbuf[];
|
|
|
|
extern PROGRAMMER * pgm;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
|
2001-01-26 20:34:08 +00:00
|
|
|
char * avr_version = "$Id$";
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
AVRPART * avr_new_part(void)
|
|
|
|
{
|
|
|
|
AVRPART * p;
|
|
|
|
|
|
|
|
p = (AVRPART *)malloc(sizeof(AVRPART));
|
|
|
|
if (p == NULL) {
|
|
|
|
fprintf(stderr, "new_part(): out of memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(p, 0, sizeof(*p));
|
|
|
|
|
|
|
|
p->id[0] = 0;
|
|
|
|
p->desc[0] = 0;
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
p->mem = lcreat(NULL, 0);
|
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
OPCODE * avr_new_opcode(void)
|
2001-10-14 23:17:26 +00:00
|
|
|
{
|
2001-11-21 02:46:55 +00:00
|
|
|
OPCODE * m;
|
2001-10-14 23:17:26 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
m = (OPCODE *)malloc(sizeof(*m));
|
|
|
|
if (m == NULL) {
|
|
|
|
fprintf(stderr, "avr_new_opcode(): out of memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(m, 0, sizeof(*m));
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AVRMEM * avr_new_memtype(void)
|
|
|
|
{
|
|
|
|
AVRMEM * m;
|
|
|
|
|
|
|
|
m = (AVRMEM *)malloc(sizeof(*m));
|
|
|
|
if (m == NULL) {
|
|
|
|
fprintf(stderr, "avr_new_memtype(): out of memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(m, 0, sizeof(*m));
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AVRMEM * avr_dup_mem(AVRMEM * m)
|
|
|
|
{
|
|
|
|
AVRMEM * n;
|
|
|
|
|
|
|
|
n = avr_new_memtype();
|
|
|
|
|
|
|
|
*n = *m;
|
|
|
|
|
|
|
|
n->buf = (unsigned char *)malloc(n->size);
|
|
|
|
if (n->buf == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"avr_dup_mem(): out of memory (memsize=%d)\n",
|
|
|
|
n->size);
|
2001-10-14 23:17:26 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2001-11-21 02:46:55 +00:00
|
|
|
memset(n->buf, 0, n->size);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AVRPART * avr_dup_part(AVRPART * d)
|
|
|
|
{
|
|
|
|
AVRPART * p;
|
|
|
|
LISTID save;
|
|
|
|
LNODEID ln;
|
|
|
|
|
|
|
|
p = avr_new_part();
|
|
|
|
save = p->mem;
|
2001-10-14 23:17:26 +00:00
|
|
|
|
|
|
|
*p = *d;
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
p->mem = save;
|
|
|
|
|
|
|
|
for (ln=lfirst(d->mem); ln; ln=lnext(ln)) {
|
|
|
|
ladd(p->mem, avr_dup_mem(ldata(ln)));
|
2001-10-14 23:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-02-08 01:08:30 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
AVRMEM * avr_locate_mem(AVRPART * p, char * desc)
|
|
|
|
{
|
|
|
|
AVRMEM * m, * match;
|
|
|
|
LNODEID ln;
|
|
|
|
int matches;
|
|
|
|
int l;
|
|
|
|
|
|
|
|
l = strlen(desc);
|
|
|
|
matches = 0;
|
|
|
|
match = NULL;
|
|
|
|
for (ln=lfirst(p->mem); ln; ln=lnext(ln)) {
|
|
|
|
m = ldata(ln);
|
|
|
|
if (strncmp(desc, m->desc, l) == 0) {
|
|
|
|
match = m;
|
|
|
|
matches++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matches == 1)
|
|
|
|
return match;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
/*
|
|
|
|
* transmit and receive a byte of data to/from the AVR device
|
|
|
|
*/
|
2001-10-13 03:13:13 +00:00
|
|
|
unsigned char avr_txrx(int fd, unsigned char byte)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char r, b, rbyte;
|
|
|
|
|
|
|
|
rbyte = 0;
|
|
|
|
for (i=0; i<8; i++) {
|
|
|
|
b = (byte >> (7-i)) & 0x01;
|
2001-11-21 18:54:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* read the result bit (it is either valid from a previous clock
|
|
|
|
* pulse or it is ignored in the current context)
|
|
|
|
*/
|
|
|
|
r = ppi_getpin(fd, pgm->pinno[PIN_AVR_MISO]);
|
|
|
|
|
|
|
|
/* set the data input line as desired */
|
|
|
|
ppi_setpin(fd, pgm->pinno[PIN_AVR_MOSI], b);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pulse the clock line, clocking in the MOSI data, and clocking out
|
|
|
|
* the next result bit
|
|
|
|
*/
|
|
|
|
ppi_pulsepin(fd, pgm->pinno[PIN_AVR_SCK]);
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
rbyte = rbyte | (r << (7-i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return rbyte;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* transmit an AVR device command and return the results; 'cmd' and
|
|
|
|
* 'res' must point to at least a 4 byte data buffer
|
|
|
|
*/
|
2001-10-13 03:13:13 +00:00
|
|
|
int avr_cmd(int fd, unsigned char cmd[4], unsigned char res[4])
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<4; i++) {
|
|
|
|
res[i] = avr_txrx(fd, cmd[i]);
|
|
|
|
}
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "avr_cmd(): [ ");
|
|
|
|
for (i=0; i<4; i++)
|
|
|
|
fprintf(stderr, "%02x ", cmd[i]);
|
|
|
|
fprintf(stderr, "] [ ");
|
|
|
|
for (i=0; i<4; i++)
|
|
|
|
fprintf(stderr, "%02x ", res[i]);
|
|
|
|
fprintf(stderr, "]\n");
|
|
|
|
#endif
|
2001-11-19 17:44:24 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
return 0;
|
2001-11-19 17:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-21 18:54:11 +00:00
|
|
|
/*
|
|
|
|
* avr_set_bits()
|
|
|
|
*
|
|
|
|
* Set instruction bits in the specified command based on the opcode.
|
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
int avr_set_bits(OPCODE * op, unsigned char * cmd)
|
2001-11-19 17:44:24 +00:00
|
|
|
{
|
2001-11-21 02:46:55 +00:00
|
|
|
int i, j, bit;
|
|
|
|
unsigned char mask;
|
|
|
|
|
|
|
|
for (i=0; i<32; i++) {
|
|
|
|
if (op->bit[i].type == AVR_CMDBIT_VALUE) {
|
|
|
|
j = 3 - i / 8;
|
|
|
|
bit = i % 8;
|
|
|
|
mask = 1 << bit;
|
|
|
|
if (op->bit[i].value)
|
|
|
|
cmd[j] = cmd[j] | mask;
|
|
|
|
else
|
|
|
|
cmd[j] = cmd[j] & ~mask;
|
|
|
|
}
|
|
|
|
}
|
2001-11-19 17:44:24 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
return 0;
|
2001-11-19 17:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-21 18:54:11 +00:00
|
|
|
/*
|
|
|
|
* avr_set_addr()
|
|
|
|
*
|
|
|
|
* Set address bits in the specified command based on the opcode, and
|
|
|
|
* the address.
|
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
int avr_set_addr(OPCODE * op, unsigned char * cmd, unsigned long addr)
|
2001-11-19 17:44:24 +00:00
|
|
|
{
|
2001-11-21 02:46:55 +00:00
|
|
|
int i, j, bit;
|
|
|
|
unsigned long value;
|
|
|
|
unsigned char mask;
|
|
|
|
|
|
|
|
for (i=0; i<32; i++) {
|
|
|
|
if (op->bit[i].type == AVR_CMDBIT_ADDRESS) {
|
|
|
|
j = 3 - i / 8;
|
|
|
|
bit = i % 8;
|
|
|
|
mask = 1 << bit;
|
|
|
|
value = addr >> op->bit[i].bitno & 0x01;
|
|
|
|
if (value)
|
|
|
|
cmd[j] = cmd[j] | mask;
|
|
|
|
else
|
|
|
|
cmd[j] = cmd[j] & ~mask;
|
|
|
|
}
|
|
|
|
}
|
2001-11-19 17:44:24 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-21 18:54:11 +00:00
|
|
|
/*
|
|
|
|
* avr_set_input()
|
|
|
|
*
|
|
|
|
* Set input data bits in the specified command based on the opcode,
|
|
|
|
* and the data byte.
|
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
int avr_set_input(OPCODE * op, unsigned char * cmd, unsigned char data)
|
2001-11-19 17:44:24 +00:00
|
|
|
{
|
2001-11-21 02:46:55 +00:00
|
|
|
int i, j, bit;
|
|
|
|
unsigned char value;
|
|
|
|
unsigned char mask;
|
|
|
|
|
|
|
|
for (i=0; i<32; i++) {
|
|
|
|
if (op->bit[i].type == AVR_CMDBIT_INPUT) {
|
|
|
|
j = 3 - i / 8;
|
|
|
|
bit = i % 8;
|
|
|
|
mask = 1 << bit;
|
|
|
|
value = data >> op->bit[i].bitno & 0x01;
|
|
|
|
if (value)
|
|
|
|
cmd[j] = cmd[j] | mask;
|
|
|
|
else
|
|
|
|
cmd[j] = cmd[j] & ~mask;
|
|
|
|
}
|
|
|
|
}
|
2001-11-19 17:44:24 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
return 0;
|
2001-11-19 17:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-21 18:54:11 +00:00
|
|
|
/*
|
|
|
|
* avr_get_output()
|
|
|
|
*
|
|
|
|
* Retreive output data bits from the command results based on the
|
|
|
|
* opcode data.
|
|
|
|
*/
|
|
|
|
int avr_get_output(OPCODE * op, unsigned char * res, unsigned char * data)
|
2001-11-19 17:44:24 +00:00
|
|
|
{
|
2001-11-21 02:46:55 +00:00
|
|
|
int i, j, bit;
|
|
|
|
unsigned char value;
|
|
|
|
unsigned char mask;
|
|
|
|
|
|
|
|
for (i=0; i<32; i++) {
|
|
|
|
if (op->bit[i].type == AVR_CMDBIT_OUTPUT) {
|
|
|
|
j = 3 - i / 8;
|
|
|
|
bit = i % 8;
|
|
|
|
mask = 1 << bit;
|
2001-11-21 18:54:11 +00:00
|
|
|
value = ((res[j] & mask) >> bit) & 0x01;
|
2001-11-21 02:46:55 +00:00
|
|
|
value = value << op->bit[i].bitno;
|
|
|
|
if (value)
|
|
|
|
*data = *data | value;
|
|
|
|
else
|
|
|
|
*data = *data & ~value;
|
|
|
|
}
|
|
|
|
}
|
2001-11-19 17:44:24 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
/*
|
|
|
|
* read a byte of data from the indicated memory region
|
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
int avr_read_byte(int fd, AVRPART * p, AVRMEM * mem, unsigned long addr,
|
|
|
|
unsigned char * value)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
unsigned char cmd[4];
|
|
|
|
unsigned char res[4];
|
2001-11-21 02:46:55 +00:00
|
|
|
unsigned char data;
|
|
|
|
OPCODE * readop;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_ON(fd, pgm->pinno[PIN_LED_PGM]);
|
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_ERR]);
|
2001-01-24 19:10:34 +00:00
|
|
|
|
2001-11-21 18:54:11 +00:00
|
|
|
/*
|
|
|
|
* figure out what opcode to use
|
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
if (mem->op[AVR_OP_READ_LO]) {
|
|
|
|
if (addr & 0x00000001)
|
|
|
|
readop = mem->op[AVR_OP_READ_HI];
|
|
|
|
else
|
|
|
|
readop = mem->op[AVR_OP_READ_LO];
|
|
|
|
addr = addr / 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
readop = mem->op[AVR_OP_READ];
|
|
|
|
}
|
2001-01-22 01:59:47 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
if (readop == NULL) {
|
2002-01-12 01:26:09 +00:00
|
|
|
#if DEBUG
|
2001-11-21 02:46:55 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"avr_read_byte(): operation not supported on memory type \"%s\"\n",
|
|
|
|
p->desc);
|
2002-01-12 01:26:09 +00:00
|
|
|
#endif
|
2001-11-21 02:46:55 +00:00
|
|
|
return -1;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
memset(cmd, 0, sizeof(cmd));
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
avr_set_bits(readop, cmd);
|
|
|
|
avr_set_addr(readop, cmd, addr);
|
2001-01-19 02:46:50 +00:00
|
|
|
avr_cmd(fd, cmd, res);
|
2001-11-21 02:46:55 +00:00
|
|
|
data = 0;
|
|
|
|
avr_get_output(readop, res, &data);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_PGM]);
|
2001-01-24 19:10:34 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
*value = data;
|
|
|
|
|
|
|
|
return 0;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2001-01-20 16:34:28 +00:00
|
|
|
* Read the entirety of the specified memory type into the
|
2001-11-19 17:44:24 +00:00
|
|
|
* corresponding buffer of the avrpart pointed to by 'p'. If size =
|
2001-11-21 02:46:55 +00:00
|
|
|
* 0, read the entire contents, otherwise, read 'size' bytes.
|
2001-01-20 16:34:28 +00:00
|
|
|
*
|
2001-11-21 18:54:11 +00:00
|
|
|
* Return the number of bytes read, or < 0 if an error occurs.
|
2001-11-21 02:46:55 +00:00
|
|
|
*/
|
|
|
|
int avr_read(int fd, AVRPART * p, char * memtype, int size, int verbose)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2001-01-22 01:59:47 +00:00
|
|
|
unsigned char rbyte;
|
2001-10-14 02:53:21 +00:00
|
|
|
unsigned long i;
|
2001-01-22 01:59:47 +00:00
|
|
|
unsigned char * buf;
|
2001-11-21 02:46:55 +00:00
|
|
|
AVRMEM * mem;
|
|
|
|
int rc;
|
2002-08-01 01:00:03 +00:00
|
|
|
int printed;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
mem = avr_locate_mem(p, memtype);
|
|
|
|
if (mem == NULL) {
|
|
|
|
fprintf(stderr, "No \"%s\" memory for part %s\n",
|
|
|
|
memtype, p->desc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = mem->buf;
|
2001-11-19 17:44:24 +00:00
|
|
|
if (size == 0) {
|
2001-11-21 02:46:55 +00:00
|
|
|
size = mem->size;
|
2001-11-19 17:44:24 +00:00
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2002-08-01 01:00:03 +00:00
|
|
|
printed = 0;
|
|
|
|
|
2001-01-22 01:59:47 +00:00
|
|
|
for (i=0; i<size; i++) {
|
2001-11-21 02:46:55 +00:00
|
|
|
rc = avr_read_byte(fd, p, mem, i, &rbyte);
|
|
|
|
if (rc != 0) {
|
|
|
|
fprintf(stderr, "avr_read(): error reading address 0x%04lx\n", i);
|
2002-01-12 01:26:09 +00:00
|
|
|
if (rc == -1)
|
|
|
|
fprintf(stderr,
|
|
|
|
" read operation not supported for memory \"%s\"\n",
|
|
|
|
memtype);
|
2001-11-21 02:46:55 +00:00
|
|
|
return -2;
|
|
|
|
}
|
2001-01-22 01:59:47 +00:00
|
|
|
buf[i] = rbyte;
|
2001-11-21 02:46:55 +00:00
|
|
|
if (verbose) {
|
2002-02-14 02:48:07 +00:00
|
|
|
if ((i % 16 == 0)||(i == (size-1))) {
|
2002-08-01 01:00:03 +00:00
|
|
|
printed = 1;
|
2002-02-14 02:48:07 +00:00
|
|
|
fprintf(stderr, "\r \r%6lu", i);
|
|
|
|
}
|
2001-11-21 02:46:55 +00:00
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2002-08-01 01:00:03 +00:00
|
|
|
if (printed) {
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-01-22 01:59:47 +00:00
|
|
|
return i;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-14 02:53:21 +00:00
|
|
|
/*
|
2001-11-21 18:54:11 +00:00
|
|
|
* write a page data at the specified address
|
2001-10-14 02:53:21 +00:00
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
int avr_write_page(int fd, AVRPART * p, AVRMEM * mem,
|
|
|
|
unsigned long addr)
|
2001-10-14 02:53:21 +00:00
|
|
|
{
|
|
|
|
unsigned char cmd[4];
|
|
|
|
unsigned char res[4];
|
2001-11-21 02:46:55 +00:00
|
|
|
OPCODE * wp;
|
|
|
|
|
|
|
|
wp = mem->op[AVR_OP_WRITEPAGE];
|
|
|
|
if (wp == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"avr_write_page(): memory \"%s\" not configured for page writes\n",
|
|
|
|
mem->desc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-11-21 18:54:11 +00:00
|
|
|
/*
|
|
|
|
* if this memory is word-addressable, adjust the address
|
|
|
|
* accordingly
|
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
if (mem->op[AVR_OP_LOADPAGE_LO])
|
|
|
|
addr = addr / 2;
|
2001-10-14 02:53:21 +00:00
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_ON(fd, pgm->pinno[PIN_LED_PGM]);
|
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_ERR]);
|
2001-10-14 02:53:21 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
memset(cmd, 0, sizeof(cmd));
|
2001-10-14 02:53:21 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
avr_set_bits(wp, cmd);
|
|
|
|
avr_set_addr(wp, cmd, addr);
|
2001-10-14 02:53:21 +00:00
|
|
|
avr_cmd(fd, cmd, res);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* since we don't know what voltage the target AVR is powered by, be
|
|
|
|
* conservative and delay the max amount the spec says to wait
|
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
usleep(mem->max_write_delay);
|
2001-10-14 02:53:21 +00:00
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_PGM]);
|
2001-10-14 02:53:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
/*
|
2001-11-21 18:54:11 +00:00
|
|
|
* write a byte of data at the specified address
|
2001-01-19 02:46:50 +00:00
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
int avr_write_byte(int fd, AVRPART * p, AVRMEM * mem,
|
2001-10-14 23:17:26 +00:00
|
|
|
unsigned long addr, unsigned char data)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2001-01-22 01:59:47 +00:00
|
|
|
unsigned char cmd[4];
|
|
|
|
unsigned char res[4];
|
2001-01-19 02:46:50 +00:00
|
|
|
unsigned char r;
|
|
|
|
int ready;
|
|
|
|
int tries;
|
|
|
|
unsigned char b;
|
2001-01-22 01:59:47 +00:00
|
|
|
unsigned short caddr;
|
2001-11-21 02:46:55 +00:00
|
|
|
OPCODE * writeop;
|
|
|
|
int rc;
|
2002-01-12 01:26:09 +00:00
|
|
|
int readok=0;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
if (!mem->paged) {
|
2001-10-14 02:53:21 +00:00
|
|
|
/*
|
|
|
|
* check to see if the write is necessary by reading the existing
|
|
|
|
* value and only write if we are changing the value; we can't
|
2001-10-16 23:32:30 +00:00
|
|
|
* use this optimization for paged addressing.
|
2001-10-14 02:53:21 +00:00
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
rc = avr_read_byte(fd, p, mem, addr, &b);
|
2002-01-12 01:26:09 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
if (rc != -1) {
|
2002-02-14 02:48:07 +00:00
|
|
|
return -2;
|
2002-01-12 01:26:09 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* the read operation is not support on this memory type
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
readok = 1;
|
|
|
|
if (b == data) {
|
|
|
|
return 0;
|
|
|
|
}
|
2001-10-14 02:53:21 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-21 02:46:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* determine which memory opcode to use
|
|
|
|
*/
|
|
|
|
if (mem->op[AVR_OP_WRITE_LO]) {
|
|
|
|
if (addr & 0x01)
|
|
|
|
writeop = mem->op[AVR_OP_WRITE_HI];
|
|
|
|
else
|
|
|
|
writeop = mem->op[AVR_OP_WRITE_LO];
|
|
|
|
caddr = addr / 2;
|
|
|
|
}
|
|
|
|
else if (mem->op[AVR_OP_LOADPAGE_LO]) {
|
|
|
|
if (addr & 0x01)
|
|
|
|
writeop = mem->op[AVR_OP_LOADPAGE_HI];
|
|
|
|
else
|
|
|
|
writeop = mem->op[AVR_OP_LOADPAGE_LO];
|
|
|
|
caddr = addr / 2;
|
|
|
|
}
|
2001-10-14 02:53:21 +00:00
|
|
|
else {
|
2001-11-21 02:46:55 +00:00
|
|
|
writeop = mem->op[AVR_OP_WRITE];
|
|
|
|
caddr = addr;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
if (writeop == NULL) {
|
2002-01-12 01:26:09 +00:00
|
|
|
#if DEBUG
|
2001-11-21 02:46:55 +00:00
|
|
|
fprintf(stderr,
|
2002-01-12 01:26:09 +00:00
|
|
|
"avr_write_byte(): write not supported for memory type \"%s\"\n",
|
2001-11-21 02:46:55 +00:00
|
|
|
mem->desc);
|
2002-01-12 01:26:09 +00:00
|
|
|
#endif
|
2001-11-21 02:46:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2001-01-24 19:10:34 +00:00
|
|
|
|
2001-01-22 01:59:47 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
LED_ON(fd, pgm->pinno[PIN_LED_PGM]);
|
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_ERR]);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
memset(cmd, 0, sizeof(cmd));
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
avr_set_bits(writeop, cmd);
|
|
|
|
avr_set_addr(writeop, cmd, caddr);
|
|
|
|
avr_set_input(writeop, cmd, data);
|
2001-01-19 02:46:50 +00:00
|
|
|
avr_cmd(fd, cmd, res);
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
if (mem->paged) {
|
2001-10-14 02:53:21 +00:00
|
|
|
/*
|
2002-07-27 20:55:01 +00:00
|
|
|
* in paged addressing, single bytes to be written to the memory
|
2001-10-14 02:53:21 +00:00
|
|
|
* page complete immediately, we only need to delay when we commit
|
2001-10-16 23:32:30 +00:00
|
|
|
* the whole page via the avr_write_page() routine.
|
2001-10-14 02:53:21 +00:00
|
|
|
*/
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_PGM]);
|
2001-10-14 02:53:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-01-12 01:26:09 +00:00
|
|
|
if (readok == 0) {
|
|
|
|
/*
|
|
|
|
* read operation not supported for this memory type, just wait
|
|
|
|
* the max programming time and then return
|
|
|
|
*/
|
|
|
|
usleep(mem->max_write_delay); /* maximum write delay */
|
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_PGM]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
tries = 0;
|
|
|
|
ready = 0;
|
|
|
|
while (!ready) {
|
2002-02-14 02:48:07 +00:00
|
|
|
usleep(mem->min_write_delay);
|
2001-11-21 02:46:55 +00:00
|
|
|
rc = avr_read_byte(fd, p, mem, addr, &r);
|
|
|
|
if (rc != 0) {
|
2002-02-14 02:48:07 +00:00
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_PGM]);
|
|
|
|
LED_ON(fd, pgm->pinno[PIN_LED_ERR]);
|
|
|
|
return -4;
|
2001-11-21 02:46:55 +00:00
|
|
|
}
|
2002-02-14 02:48:07 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
if ((data == mem->readback[0]) ||
|
|
|
|
(data == mem->readback[1])) {
|
2001-01-19 02:46:50 +00:00
|
|
|
/*
|
|
|
|
* use an extra long delay when we happen to be writing values
|
|
|
|
* used for polled data read-back. In this case, polling
|
|
|
|
* doesn't work, and we need to delay the worst case write time
|
|
|
|
* specified for the chip.
|
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
usleep(mem->max_write_delay);
|
|
|
|
rc = avr_read_byte(fd, p, mem, addr, &r);
|
|
|
|
if (rc != 0) {
|
2002-02-14 02:48:07 +00:00
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_PGM]);
|
|
|
|
LED_ON(fd, pgm->pinno[PIN_LED_ERR]);
|
|
|
|
return -5;
|
2001-11-21 02:46:55 +00:00
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
2002-01-12 01:26:09 +00:00
|
|
|
|
2001-10-14 02:53:21 +00:00
|
|
|
if (r == data) {
|
2001-01-19 02:46:50 +00:00
|
|
|
ready = 1;
|
|
|
|
}
|
2002-02-14 02:48:07 +00:00
|
|
|
else if (mem->pwroff_after_write) {
|
|
|
|
/*
|
|
|
|
* The device has been flagged as power-off after write to this
|
|
|
|
* memory type. The reason we don't just blindly follow the
|
|
|
|
* flag is that the power-off advice may only apply to some
|
|
|
|
* memory bits but not all. We only actually power-off the
|
|
|
|
* device if the data read back does not match what we wrote.
|
|
|
|
*/
|
|
|
|
usleep(mem->max_write_delay); /* maximum write delay */
|
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_PGM]);
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: this device must be powered off and back on to continue\n",
|
|
|
|
progname);
|
|
|
|
if (pgm->pinno[PPI_AVR_VCC]) {
|
|
|
|
fprintf(stderr, "%s: attempting to do this now ...\n", progname);
|
|
|
|
avr_powerdown(fd);
|
|
|
|
usleep(250000);
|
|
|
|
rc = avr_initialize(fd, p);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: initialization failed, rc=%d\n", progname, rc);
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: can't re-initialize device after programming the "
|
|
|
|
"%s bits\n", progname, mem->desc);
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: you must manually power-down the device and restart\n"
|
|
|
|
"%s: avrprog to continue.\n",
|
|
|
|
progname, progname);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: device was successfully re-initialized\n",
|
|
|
|
progname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
tries++;
|
2001-10-14 02:53:21 +00:00
|
|
|
if (!ready && tries > 5) {
|
2001-01-19 02:46:50 +00:00
|
|
|
/*
|
2002-02-14 02:48:07 +00:00
|
|
|
* we wrote the data, but after waiting for what should have
|
|
|
|
* been plenty of time, the memory cell still doesn't match what
|
|
|
|
* we wrote. Indicate a write error.
|
2001-01-19 02:46:50 +00:00
|
|
|
*/
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_PGM]);
|
|
|
|
LED_ON(fd, pgm->pinno[PIN_LED_ERR]);
|
2002-01-12 01:26:09 +00:00
|
|
|
|
2002-02-14 02:48:07 +00:00
|
|
|
return -6;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_PGM]);
|
2001-01-19 02:46:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2001-11-21 18:54:11 +00:00
|
|
|
* Write the whole memory region of the specified memory from the
|
|
|
|
* corresponding buffer of the avrpart pointed to by 'p'. Write up to
|
|
|
|
* 'size' bytes from the buffer. Data is only written if the new data
|
|
|
|
* value is different from the existing data value. Data beyond
|
|
|
|
* 'size' bytes is not affected.
|
2001-01-20 16:34:28 +00:00
|
|
|
*
|
|
|
|
* Return the number of bytes written, or -1 if an error occurs.
|
2001-01-19 02:46:50 +00:00
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
int avr_write(int fd, AVRPART * p, char * memtype, int size, int verbose)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2001-01-22 01:59:47 +00:00
|
|
|
int rc;
|
|
|
|
int wsize;
|
2001-10-14 02:53:21 +00:00
|
|
|
unsigned long i;
|
2001-01-22 01:59:47 +00:00
|
|
|
unsigned char data;
|
2001-02-08 01:05:05 +00:00
|
|
|
int werror;
|
2001-11-21 02:46:55 +00:00
|
|
|
AVRMEM * m;
|
2002-08-01 01:00:03 +00:00
|
|
|
int printed;
|
2001-11-21 02:46:55 +00:00
|
|
|
|
|
|
|
m = avr_locate_mem(p, memtype);
|
|
|
|
if (m == NULL) {
|
|
|
|
fprintf(stderr, "No \"%s\" memory for part %s\n",
|
|
|
|
memtype, p->desc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_ERR]);
|
2001-01-24 19:10:34 +00:00
|
|
|
|
2002-08-01 01:00:03 +00:00
|
|
|
printed = 0;
|
|
|
|
werror = 0;
|
2001-02-08 01:05:05 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
wsize = m->size;
|
2001-01-22 01:59:47 +00:00
|
|
|
if (size < wsize) {
|
|
|
|
wsize = size;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
2001-01-22 01:59:47 +00:00
|
|
|
else if (size > wsize) {
|
2001-01-20 16:34:28 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: WARNING: %d bytes requested, but memory region is only %d bytes\n"
|
|
|
|
"%sOnly %d bytes will actually be written\n",
|
2001-01-22 01:59:47 +00:00
|
|
|
progname, size, wsize,
|
|
|
|
progbuf, wsize);
|
2001-01-20 16:34:28 +00:00
|
|
|
}
|
|
|
|
|
2001-01-22 01:59:47 +00:00
|
|
|
for (i=0; i<wsize; i++) {
|
2001-11-21 02:46:55 +00:00
|
|
|
data = m->buf[i];
|
|
|
|
if (verbose) {
|
2002-02-14 02:48:07 +00:00
|
|
|
if ((i % 16 == 0)||(i == (wsize-1))) {
|
|
|
|
fprintf(stderr, "\r \r%6lu", i);
|
2002-08-01 01:00:03 +00:00
|
|
|
printed = 1;
|
2002-02-14 02:48:07 +00:00
|
|
|
}
|
2001-11-21 02:46:55 +00:00
|
|
|
}
|
|
|
|
rc = avr_write_byte(fd, p, m, i, data);
|
2001-01-19 02:46:50 +00:00
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, " ***failed; ");
|
|
|
|
fprintf(stderr, "\n");
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_ON(fd, pgm->pinno[PIN_LED_ERR]);
|
2001-02-08 01:05:05 +00:00
|
|
|
werror = 1;
|
|
|
|
}
|
2001-10-14 02:53:21 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
if (m->paged) {
|
2001-11-21 18:54:11 +00:00
|
|
|
/*
|
|
|
|
* check to see if it is time to flush the page with a page
|
|
|
|
* write
|
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
if (((i % m->page_size) == m->page_size-1) ||
|
2001-10-14 02:53:21 +00:00
|
|
|
(i == wsize-1)) {
|
2001-11-21 02:46:55 +00:00
|
|
|
rc = avr_write_page(fd, p, m, i);
|
2001-10-14 02:53:21 +00:00
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr,
|
2001-11-21 02:46:55 +00:00
|
|
|
" *** page %ld (addresses 0x%04lx - 0x%04lx) failed "
|
|
|
|
"to write\n",
|
|
|
|
i % m->page_size,
|
|
|
|
i - m->page_size + 1, i);
|
2001-10-14 02:53:21 +00:00
|
|
|
fprintf(stderr, "\n");
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_ON(fd, pgm->pinno[PIN_LED_ERR]);
|
2001-10-14 02:53:21 +00:00
|
|
|
werror = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-08 01:05:05 +00:00
|
|
|
if (werror) {
|
|
|
|
/*
|
|
|
|
* make sure the error led stay on if there was a previous write
|
|
|
|
* error, otherwise it gets cleared in avr_write_byte()
|
|
|
|
*/
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_ON(fd, pgm->pinno[PIN_LED_ERR]);
|
2001-01-22 01:59:47 +00:00
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2002-08-01 01:00:03 +00:00
|
|
|
if (printed)
|
|
|
|
fprintf(stderr, "\n");
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-01-22 01:59:47 +00:00
|
|
|
return i;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* issue the 'program enable' command to the AVR device
|
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
int avr_program_enable(int fd, AVRPART * p)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2001-11-21 02:46:55 +00:00
|
|
|
unsigned char cmd[4];
|
2001-01-19 02:46:50 +00:00
|
|
|
unsigned char res[4];
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
if (p->op[AVR_OP_PGM_ENABLE] == NULL) {
|
|
|
|
fprintf(stderr, "program enable instruction not defined for part \"%s\"\n",
|
|
|
|
p->desc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(cmd, 0, sizeof(cmd));
|
|
|
|
avr_set_bits(p->op[AVR_OP_PGM_ENABLE], cmd);
|
2001-01-19 02:46:50 +00:00
|
|
|
avr_cmd(fd, cmd, res);
|
|
|
|
|
|
|
|
if (res[2] != cmd[1])
|
2001-11-21 02:46:55 +00:00
|
|
|
return -2;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* issue the 'chip erase' command to the AVR device
|
|
|
|
*/
|
2001-10-14 23:17:26 +00:00
|
|
|
int avr_chip_erase(int fd, AVRPART * p)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2001-11-21 02:46:55 +00:00
|
|
|
unsigned char cmd[4];
|
2001-01-19 02:46:50 +00:00
|
|
|
unsigned char res[4];
|
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
if (p->op[AVR_OP_CHIP_ERASE] == NULL) {
|
|
|
|
fprintf(stderr, "chip erase instruction not defined for part \"%s\"\n",
|
|
|
|
p->desc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_ON(fd, pgm->pinno[PIN_LED_PGM]);
|
2001-01-24 19:10:34 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
memset(cmd, 0, sizeof(cmd));
|
|
|
|
|
|
|
|
avr_set_bits(p->op[AVR_OP_CHIP_ERASE], cmd);
|
|
|
|
avr_cmd(fd, cmd, res);
|
2001-01-19 02:46:50 +00:00
|
|
|
usleep(p->chip_erase_delay);
|
|
|
|
avr_initialize(fd, p);
|
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
LED_OFF(fd, pgm->pinno[PIN_LED_PGM]);
|
2001-01-24 19:10:34 +00:00
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read the AVR device's signature bytes
|
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
int avr_signature(int fd, AVRPART * p)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2001-11-21 02:46:55 +00:00
|
|
|
int rc;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
rc = avr_read(fd, p, "signature", 0, 0);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: error reading signature data for part \"%s\", rc=%d\n",
|
|
|
|
progname, p->desc, rc);
|
|
|
|
return -1;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* apply power to the AVR processor
|
|
|
|
*/
|
2001-10-13 03:13:13 +00:00
|
|
|
void avr_powerup(int fd)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2001-12-29 21:37:20 +00:00
|
|
|
ppi_set(fd, PPIDATA, pgm->pinno[PPI_AVR_VCC]); /* power up */
|
2001-01-19 02:46:50 +00:00
|
|
|
usleep(100000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remove power from the AVR processor
|
|
|
|
*/
|
2001-10-13 03:13:13 +00:00
|
|
|
void avr_powerdown(int fd)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2001-12-29 21:37:20 +00:00
|
|
|
ppi_clr(fd, PPIDATA, pgm->pinno[PPI_AVR_VCC]); /* power down */
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize the AVR device and prepare it to accept commands
|
|
|
|
*/
|
2001-10-14 23:17:26 +00:00
|
|
|
int avr_initialize(int fd, AVRPART * p)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int tries;
|
|
|
|
|
|
|
|
avr_powerup(fd);
|
2001-12-29 21:37:20 +00:00
|
|
|
usleep(20000);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
ppi_setpin(fd, pgm->pinno[PIN_AVR_SCK], 0);
|
|
|
|
ppi_setpin(fd, pgm->pinno[PIN_AVR_RESET], 0);
|
2001-12-29 21:37:20 +00:00
|
|
|
usleep(20000);
|
|
|
|
|
2001-10-14 23:17:26 +00:00
|
|
|
ppi_pulsepin(fd, pgm->pinno[PIN_AVR_RESET]);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
usleep(20000); /* 20 ms XXX should be a per-chip parameter */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable programming mode. If we are programming an AT90S1200, we
|
|
|
|
* can only issue the command and hope it worked. If we are using
|
|
|
|
* one of the other chips, the chip will echo 0x53 when issuing the
|
|
|
|
* third byte of the command. In this case, try up to 32 times in
|
|
|
|
* order to possibly get back into sync with the chip if we are out
|
|
|
|
* of sync.
|
|
|
|
*/
|
2001-10-14 23:17:26 +00:00
|
|
|
if (strcmp(p->desc, "AT90S1200")==0) {
|
2001-11-21 02:46:55 +00:00
|
|
|
avr_program_enable(fd, p);
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
tries = 0;
|
|
|
|
do {
|
2001-11-21 02:46:55 +00:00
|
|
|
rc = avr_program_enable(fd, p);
|
2001-12-29 21:37:20 +00:00
|
|
|
if ((rc == 0)||(rc == -1))
|
2001-01-19 02:46:50 +00:00
|
|
|
break;
|
2001-10-14 23:17:26 +00:00
|
|
|
ppi_pulsepin(fd, pgm->pinno[PIN_AVR_SCK]);
|
2001-01-19 02:46:50 +00:00
|
|
|
tries++;
|
2001-12-29 21:37:20 +00:00
|
|
|
} while (tries < 65);
|
2001-01-19 02:46:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* can't sync with the device, maybe it's not attached?
|
|
|
|
*/
|
2001-12-29 21:37:20 +00:00
|
|
|
if (rc) {
|
2001-10-13 03:13:13 +00:00
|
|
|
fprintf(stderr, "%s: AVR device not responding\n", progname);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-21 18:54:11 +00:00
|
|
|
/*
|
|
|
|
* Allocate and initialize memory buffers for each of the device's
|
|
|
|
* defined memory regions.
|
|
|
|
*/
|
2001-10-14 23:17:26 +00:00
|
|
|
int avr_initmem(AVRPART * p)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2001-11-21 02:46:55 +00:00
|
|
|
LNODEID ln;
|
|
|
|
AVRMEM * m;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
for (ln=lfirst(p->mem); ln; ln=lnext(ln)) {
|
|
|
|
m = ldata(ln);
|
|
|
|
m->buf = (unsigned char *) malloc(m->size);
|
|
|
|
if (m->buf == NULL) {
|
2001-01-22 01:59:47 +00:00
|
|
|
fprintf(stderr, "%s: can't alloc buffer for %s size of %d bytes\n",
|
2001-11-21 02:46:55 +00:00
|
|
|
progname, m->desc, m->size);
|
2001-01-22 01:59:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-20 16:34:28 +00:00
|
|
|
/*
|
2001-01-26 17:22:40 +00:00
|
|
|
* Verify the memory buffer of p with that of v. The byte range of v,
|
|
|
|
* may be a subset of p. The byte range of p should cover the whole
|
|
|
|
* chip's memory size.
|
|
|
|
*
|
|
|
|
* Return the number of bytes verified, or -1 if they don't match.
|
2001-01-20 16:34:28 +00:00
|
|
|
*/
|
2001-11-21 02:46:55 +00:00
|
|
|
int avr_verify(AVRPART * p, AVRPART * v, char * memtype, int size)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char * buf1, * buf2;
|
2001-01-20 16:34:28 +00:00
|
|
|
int vsize;
|
2001-11-21 02:46:55 +00:00
|
|
|
AVRMEM * a, * b;
|
|
|
|
|
|
|
|
a = avr_locate_mem(p, memtype);
|
|
|
|
if (a == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"avr_verify(): memory type \"%s\" not defined for part %s\n",
|
|
|
|
memtype, p->desc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
b = avr_locate_mem(v, memtype);
|
|
|
|
if (b == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"avr_verify(): memory type \"%s\" not defined for part %s\n",
|
|
|
|
memtype, v->desc);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-11-21 02:46:55 +00:00
|
|
|
buf1 = a->buf;
|
|
|
|
buf2 = b->buf;
|
|
|
|
vsize = a->size;
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2001-01-20 16:34:28 +00:00
|
|
|
if (vsize < size) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: WARNING: requested verification for %d bytes\n"
|
|
|
|
"%s%s memory region only contains %d bytes\n"
|
|
|
|
"%sOnly %d bytes will be verified.\n",
|
|
|
|
progname, size,
|
2001-11-21 02:46:55 +00:00
|
|
|
progbuf, memtype, vsize,
|
2001-01-20 16:34:28 +00:00
|
|
|
progbuf, vsize);
|
|
|
|
size = vsize;
|
|
|
|
}
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
for (i=0; i<size; i++) {
|
|
|
|
if (buf1[i] != buf2[i]) {
|
|
|
|
fprintf(stderr,
|
2002-02-14 02:48:07 +00:00
|
|
|
"%s: verification error, first mismatch at byte 0x%04x\n"
|
2001-01-19 02:46:50 +00:00
|
|
|
"%s0x%02x != 0x%02x\n",
|
|
|
|
progname, i,
|
|
|
|
progbuf, buf1[i], buf2[i]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-20 16:34:28 +00:00
|
|
|
return size;
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-01 01:00:03 +00:00
|
|
|
int avr_get_cycle_count(int fd, AVRPART * p)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
AVRMEM * a;
|
|
|
|
int cycle_count;
|
|
|
|
unsigned char v1, v2;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
a = avr_locate_mem(p, "eeprom");
|
|
|
|
if (a == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = avr_read_byte(fd, p, a, a->size-2, &v1);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: WARNING: can't read memory for cycle count, rc=%d\n",
|
|
|
|
progname, rc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rc = avr_read_byte(fd, p, a, a->size-1, &v2);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: WARNING: can't read memory for cycle count, rc=%d\n",
|
|
|
|
progname, rc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cycle_count = ((unsigned int)v1) << 8 | v2;
|
|
|
|
|
|
|
|
return cycle_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int avr_put_cycle_count(int fd, AVRPART * p, int cycles)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
AVRMEM * a;
|
|
|
|
unsigned char v1, v2;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
a = avr_locate_mem(p, "eeprom");
|
|
|
|
if (a == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
v2 = cycles & 0x0ff;
|
|
|
|
v1 = (cycles & 0x0ff00) >> 8;
|
|
|
|
|
|
|
|
rc = avr_write_byte(fd, p, a, a->size-2, v1);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: WARNING: can't write memory for cycle count, rc=%d\n",
|
|
|
|
progname, rc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rc = avr_write_byte(fd, p, a, a->size-1, v2);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: WARNING: can't write memory for cycle count, rc=%d\n",
|
|
|
|
progname, rc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-13 03:12:52 +00:00
|
|
|
|
2002-02-14 02:48:07 +00:00
|
|
|
char * avr_op_str(int op)
|
2001-10-13 03:12:52 +00:00
|
|
|
{
|
2002-02-14 02:48:07 +00:00
|
|
|
switch (op) {
|
|
|
|
case AVR_OP_READ : return "READ"; break;
|
|
|
|
case AVR_OP_WRITE : return "WRITE"; break;
|
|
|
|
case AVR_OP_READ_LO : return "READ_LO"; break;
|
|
|
|
case AVR_OP_READ_HI : return "READ_HI"; break;
|
|
|
|
case AVR_OP_WRITE_LO : return "WRITE_LO"; break;
|
|
|
|
case AVR_OP_WRITE_HI : return "WRITE_HI"; break;
|
|
|
|
case AVR_OP_LOADPAGE_LO : return "LOADPAGE_LO"; break;
|
|
|
|
case AVR_OP_LOADPAGE_HI : return "LOADPAGE_HI"; break;
|
|
|
|
case AVR_OP_WRITEPAGE : return "WRITEPAGE"; break;
|
|
|
|
case AVR_OP_CHIP_ERASE : return "CHIP_ERASE"; break;
|
|
|
|
case AVR_OP_PGM_ENABLE : return "PGM_ENABLE"; break;
|
|
|
|
default : return "<unknown opcode>"; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char * bittype(int type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case AVR_CMDBIT_IGNORE : return "IGNORE"; break;
|
|
|
|
case AVR_CMDBIT_VALUE : return "VALUE"; break;
|
|
|
|
case AVR_CMDBIT_ADDRESS : return "ADDRESS"; break;
|
|
|
|
case AVR_CMDBIT_INPUT : return "INPUT"; break;
|
|
|
|
case AVR_CMDBIT_OUTPUT : return "OUTPUT"; break;
|
|
|
|
default : return "<unknown bit type>"; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void avr_mem_display(char * prefix, FILE * f, AVRMEM * m, int type,
|
|
|
|
int verbose)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
char * optr;
|
|
|
|
|
2001-10-13 03:12:52 +00:00
|
|
|
if (m == NULL) {
|
|
|
|
fprintf(f,
|
2001-11-21 18:54:11 +00:00
|
|
|
"%s Page Polled\n"
|
|
|
|
"%sMemory Type Paged Size Size #Pages MinW MaxW ReadBack\n"
|
2001-11-21 02:46:55 +00:00
|
|
|
"%s----------- ------ ------ ---- ------ ----- ----- ---------\n",
|
2001-10-13 03:12:52 +00:00
|
|
|
prefix, prefix, prefix);
|
|
|
|
}
|
|
|
|
else {
|
2002-02-14 02:50:58 +00:00
|
|
|
if (verbose > 2) {
|
2002-02-14 02:48:07 +00:00
|
|
|
fprintf(f,
|
|
|
|
"%s Page Polled\n"
|
|
|
|
"%sMemory Type Paged Size Size #Pages MinW MaxW ReadBack\n"
|
|
|
|
"%s----------- ------ ------ ---- ------ ----- ----- ---------\n",
|
|
|
|
prefix, prefix, prefix);
|
|
|
|
}
|
2001-10-13 03:12:52 +00:00
|
|
|
fprintf(f,
|
2001-11-21 02:46:55 +00:00
|
|
|
"%s%-11s %-6s %6d %4d %5d %5d %5d 0x%02x 0x%02x\n",
|
|
|
|
prefix, m->desc,
|
2001-10-16 23:32:30 +00:00
|
|
|
m->paged ? "yes" : "no",
|
|
|
|
m->size,
|
|
|
|
m->page_size,
|
|
|
|
m->num_pages,
|
|
|
|
m->min_write_delay,
|
|
|
|
m->max_write_delay,
|
|
|
|
m->readback[0],
|
|
|
|
m->readback[1]);
|
2002-02-14 02:50:58 +00:00
|
|
|
if (verbose > 2) {
|
2002-02-14 02:48:07 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s Memory Ops:\n"
|
|
|
|
"%s Oeration Inst Bit Bit Type Bitno Value\n"
|
|
|
|
"%s ----------- -------- -------- ----- -----\n",
|
|
|
|
prefix, prefix, prefix);
|
|
|
|
for (i=0; i<AVR_OP_MAX; i++) {
|
|
|
|
if (m->op[i]) {
|
|
|
|
for (j=31; j>=0; j--) {
|
|
|
|
if (j==31)
|
|
|
|
optr = avr_op_str(i);
|
|
|
|
else
|
|
|
|
optr = " ";
|
|
|
|
fprintf(f,
|
|
|
|
"%s %-11s %8d %8s %5d %5d\n",
|
|
|
|
prefix, optr, j,
|
|
|
|
bittype(m->op[i]->bit[j].type),
|
|
|
|
m->op[i]->bit[j].bitno,
|
|
|
|
m->op[i]->bit[j].value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-10-13 03:12:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-14 02:48:07 +00:00
|
|
|
void avr_display(FILE * f, AVRPART * p, char * prefix, int verbose)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2001-10-13 03:12:52 +00:00
|
|
|
int i;
|
|
|
|
char * buf;
|
|
|
|
char * px;
|
2001-11-21 02:46:55 +00:00
|
|
|
LNODEID ln;
|
|
|
|
AVRMEM * m;
|
2001-10-13 03:12:52 +00:00
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
fprintf(f,
|
2001-10-13 03:12:52 +00:00
|
|
|
"%sAVR Part : %s\n"
|
|
|
|
"%sChip Erase delay : %d us\n"
|
|
|
|
"%sMemory Detail :\n\n",
|
2001-10-14 23:17:26 +00:00
|
|
|
prefix, p->desc,
|
2001-01-19 02:46:50 +00:00
|
|
|
prefix, p->chip_erase_delay,
|
2001-10-13 03:12:52 +00:00
|
|
|
prefix);
|
|
|
|
|
|
|
|
px = prefix;
|
|
|
|
i = strlen(prefix) + 5;
|
|
|
|
buf = (char *)malloc(i);
|
|
|
|
if (buf == NULL) {
|
|
|
|
/* ugh, this is not important enough to bail, just ignore it */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcpy(buf, prefix);
|
|
|
|
strcat(buf, " ");
|
|
|
|
px = buf;
|
|
|
|
}
|
|
|
|
|
2002-02-14 02:50:58 +00:00
|
|
|
if (verbose <= 2) {
|
2002-02-14 02:48:07 +00:00
|
|
|
avr_mem_display(px, f, NULL, 0, verbose);
|
|
|
|
}
|
2001-11-21 02:46:55 +00:00
|
|
|
for (ln=lfirst(p->mem); ln; ln=lnext(ln)) {
|
|
|
|
m = ldata(ln);
|
2002-02-14 02:48:07 +00:00
|
|
|
avr_mem_display(px, f, m, i, verbose);
|
2001-10-13 03:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buf)
|
|
|
|
free(buf);
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
2001-10-13 03:12:52 +00:00
|
|
|
|