Compare commits

...

5 Commits

8 changed files with 72 additions and 32 deletions

View File

@ -209,8 +209,8 @@ static int butterfly_get_memtype(struct multiboot * p_mboot,
/* *************************************************************************
* butterfly_get_memsize
* ************************************************************************* */
static int butterfly_get_memsize(struct multiboot * p_mboot,
int memtype)
static uint32_t butterfly_get_memsize(struct multiboot * p_mboot,
int memtype)
{
bfly_privdata_t * p_priv = (bfly_privdata_t *)p_mboot->privdata;

View File

@ -31,6 +31,7 @@ static avr_chipinfo_t chips[] =
{
{ { 0x1E, 0x93, 0x07 }, "ATmega8", 0x2000, 0x200 },
{ { 0x1E, 0x93, 0x0A }, "ATmega88", 0x2000, 0x200 },
{ { 0x1E, 0x93, 0x0B }, "ATtiny85", 0x2000, 0x200 },
{ { 0x1E, 0x94, 0x06 }, "ATmega168", 0x4000, 0x200 },
{ { 0x1E, 0x95, 0x02 }, "ATmega32", 0x8000, 0x400 },
{ { 0x1E, 0x95, 0x0F }, "ATmega328p", 0x8000, 0x400 },

View File

@ -267,8 +267,8 @@ static int eprog_get_memtype(struct multiboot *mboot,
/* *************************************************************************
* eprog_get_memsize
* ************************************************************************* */
static int eprog_get_memsize(struct multiboot *mboot,
int memtype)
static uint32_t eprog_get_memsize(struct multiboot *mboot,
int memtype)
{
struct eprog_privdata *p_prog = (struct eprog_privdata *)mboot->privdata;

10
funk.c
View File

@ -255,7 +255,7 @@ static void funk_free(struct multiboot *mboot)
/* *************************************************************************
* optarg_copy
* funk_get_memtype
* ************************************************************************* */
static int funk_get_memtype(struct multiboot *mboot,
const char *memname)
@ -277,10 +277,10 @@ static int funk_get_memtype(struct multiboot *mboot,
/* *************************************************************************
* optarg_copy
* funk_get_memsize
* ************************************************************************* */
static int funk_get_memsize(struct multiboot *mboot,
int memtype)
static uint32_t funk_get_memsize(struct multiboot *mboot,
int memtype)
{
struct funk_privdata *funk = (struct funk_privdata *)mboot->privdata;
@ -761,7 +761,7 @@ static int funk_read_version(struct funk_privdata *funk,
}
int i;
for (i = 0; i < packet.data_length -3; i++)
for (i = 0; i < MIN(length, packet.data_length -3); i++)
{
version[i] = packet.msg.p.version.data[i] & 0x7F;
}

2
mpm.c
View File

@ -197,7 +197,7 @@ static int mpm_get_memtype(struct multiboot *mboot,
/* *************************************************************************
* mpm_get_memsize
* ************************************************************************* */
static int mpm_get_memsize(struct multiboot *mboot, int memtype)
static uint32_t mpm_get_memsize(struct multiboot *mboot, int memtype)
{
struct mpm_privdata *mpm = (struct mpm_privdata *)mboot->privdata;

View File

@ -374,8 +374,8 @@ int main(int argc, char *argv[])
break;
}
int memsize = mboot->ops->get_memsize(mboot, action->memtype);
if (memsize == 0)
uint32_t memsize = mboot->ops->get_memsize(mboot, action->memtype);
if ((memsize == 0) || (memsize < dbuf->length))
{
fprintf(stderr, "invalid memsize: 0x%04x > 0x%04x\n", dbuf->length, memsize);
dbuf_free(dbuf);

View File

@ -21,7 +21,7 @@ struct multiboot_ops
void (* free)(struct multiboot *mboot);
int (* get_memtype)(struct multiboot *mboot, const char *memname);
int (* get_memsize)(struct multiboot *mboot, int memtype);
uint32_t (* get_memsize)(struct multiboot *mboot, int memtype);
int (* open)(struct multiboot *mboot);
int (* close)(struct multiboot *mboot);

77
twi.c
View File

@ -42,27 +42,29 @@
#define TWI_DEFAULT_DEVICE "/dev/i2c-0"
#define READ_BLOCK_SIZE 128 /* bytes in one flash/eeprom read request */
#define WRITE_BLOCK_SIZE 16 /* bytes in one eeprom write request */
#define READ_BLOCK_SIZE 128 /* bytes in one flash/eeprom read request */
#define WRITE_BLOCK_SIZE 16 /* bytes in one eeprom write request */
#define WRITE_RETRY_COUNT 50 /* write retries when slave does not not acknowledge */
#define WRITE_RETRY_DELAY_US 2000 /* delay in us per retry */
/* SLA+R */
#define CMD_WAIT 0x00
#define CMD_READ_VERSION 0x01
#define CMD_READ_MEMORY 0x02
#define CMD_WAIT 0x00
#define CMD_READ_VERSION 0x01
#define CMD_READ_MEMORY 0x02
/* SLA+W */
#define CMD_SWITCH_APPLICATION CMD_READ_VERSION
#define CMD_WRITE_MEMORY CMD_READ_MEMORY
#define CMD_SWITCH_APPLICATION CMD_READ_VERSION
#define CMD_WRITE_MEMORY CMD_READ_MEMORY
/* CMD_SWITCH_APPLICATION parameter */
#define BOOTTYPE_BOOTLOADER 0x00 /* only in APP */
#define BOOTTYPE_APPLICATION 0x80
#define BOOTTYPE_BOOTLOADER 0x00 /* only in APP */
#define BOOTTYPE_APPLICATION 0x80
/* CMD_{READ|WRITE}_* parameter */
#define MEMTYPE_CHIPINFO 0x00
#define MEMTYPE_FLASH 0x01
#define MEMTYPE_EEPROM 0x02
#define MEMTYPE_PARAMETERS 0x03 /* only in APP */
#define MEMTYPE_CHIPINFO 0x00
#define MEMTYPE_FLASH 0x01
#define MEMTYPE_EEPROM 0x02
#define MEMTYPE_PARAMETERS 0x03 /* only in APP */
struct multiboot_ops twi_ops;
@ -85,6 +87,43 @@ static struct option twi_optargs[] =
};
/* *************************************************************************
* twi_write_retries
* ************************************************************************* */
static int twi_write_retries(int fd, const void *buf, size_t count)
{
int result;
int retries = WRITE_RETRY_COUNT;
do {
result = write(fd, buf, count);
if (result >= 0)
{
break;
}
else
{
/*
* slave device does not acknowledge (eg. not able to clockstretch)
* actual error code may be driver specific
*/
if ((errno == ENXIO) || (errno == EREMOTEIO) || (errno == EIO))
{
usleep(WRITE_RETRY_DELAY_US);
}
else
{
fprintf(stderr, "twi_write_retries(): %s\n",
strerror(errno));
retries = 0;
}
}
} while (retries--);
return result;
} /* twi_write_retries */
/* *************************************************************************
* twi_switch_application
* ************************************************************************* */
@ -93,7 +132,7 @@ static int twi_switch_application(struct twi_privdata *twi,
{
uint8_t cmd[] = { CMD_SWITCH_APPLICATION, application };
return (write(twi->fd, cmd, sizeof(cmd)) != sizeof(cmd));
return (twi_write_retries(twi->fd, cmd, sizeof(cmd)) != sizeof(cmd));
} /* twi_switch_application */
@ -105,7 +144,7 @@ static int twi_read_version(struct twi_privdata *twi,
{
uint8_t cmd[] = { CMD_READ_VERSION };
if (write(twi->fd, cmd, sizeof(cmd)) != sizeof(cmd))
if (twi_write_retries(twi->fd, cmd, sizeof(cmd)) != sizeof(cmd))
{
return -1;
}
@ -135,7 +174,7 @@ static int twi_read_memory(struct twi_privdata *twi,
{
uint8_t cmd[] = { CMD_READ_MEMORY, memtype, (address >> 8) & 0xFF, (address & 0xFF) };
if (write(twi->fd, cmd, sizeof(cmd)) != sizeof(cmd))
if (twi_write_retries(twi->fd, cmd, sizeof(cmd)) != sizeof(cmd))
{
return -1;
}
@ -187,7 +226,7 @@ static int twi_write_memory(struct twi_privdata *twi,
memset(cmd +4 +size, 0xFF, twi->pagesize - size);
}
int result = write(twi->fd, cmd, bufsize);
int result = twi_write_retries(twi->fd, cmd, bufsize);
free(cmd);
return (result != bufsize);
@ -592,8 +631,8 @@ static int twi_get_memtype(struct multiboot *mboot,
/* *************************************************************************
* twi_get_memsize
* ************************************************************************* */
static int twi_get_memsize(struct multiboot *mboot,
int memtype)
static uint32_t twi_get_memsize(struct multiboot *mboot,
int memtype)
{
struct twi_privdata *twi = (struct twi_privdata *)mboot->privdata;