Align flash writes to word boundaries.

The FET silently drops flash memory write requests which are not
address or length aligned to a word boundary.

Based on a patch by Sören Höckner <soeren.hoeckner@gmx.de>.
This commit is contained in:
Daniel Beer 2010-09-10 12:52:47 +12:00
parent 40fa5de592
commit 479b0b2a9f
6 changed files with 159 additions and 74 deletions

View File

@ -32,3 +32,6 @@ Robert Spanton <rspanton@zepler.net>:
Stephen Kench <stephen@pmssystems.com>
* Support for MSP430F47197.
Sören Höckner <soeren.hoeckner@gmx.de>:
* Testing and debugging of flash memory word-alignment bug.

View File

@ -58,7 +58,7 @@ mspdebug: main.o fet.o rf2500.o dis.o uif.o olimex.o ihex.o elf32.o stab.o \
util.o bsl.o sim.o symmap.o gdb.o btree.o rtools.o sym.o devcmd.o \
reader.o vector.o output_util.o expr.o fet_error.o binfile.o \
fet_db.o usbutil.o titext.o srec.o device.o coff.o opdb.o output.o \
cmddb.o stdcmd.o
cmddb.o stdcmd.o prog.o
$(CC) $(LDFLAGS) $(MACPORTS_LDFLAGS) -o $@ $^ -lusb $(READLINE_LIBS)
.c.o:

View File

@ -29,6 +29,7 @@
#include "reader.h"
#include "output_util.h"
#include "util.h"
#include "prog.h"
#include "dis.h"
int cmd_regs(char **arg)
@ -450,75 +451,10 @@ fail:
return -1;
}
struct prog_data {
uint8_t buf[128];
address_t addr;
int len;
int have_erased;
};
static int prog_flush(struct prog_data *prog)
static int cmd_prog_feed(void *user_data, address_t addr,
const uint8_t *data, int len)
{
while (prog->len) {
int wlen = prog->len;
if (!prog->have_erased) {
printc("Erasing...\n");
if (device_default->ctl(device_default,
DEVICE_CTL_ERASE) < 0)
return -1;
printc("Programming...\n");
prog->have_erased = 1;
}
printc_dbg("Writing %3d bytes to %04x...\n", wlen, prog->addr);
if (device_default->writemem(device_default, prog->addr,
prog->buf, wlen) < 0)
return -1;
memmove(prog->buf, prog->buf + wlen, prog->len - wlen);
prog->len -= wlen;
prog->addr += wlen;
}
return 0;
}
static int prog_feed(void *user_data,
address_t addr, const uint8_t *data, int len)
{
struct prog_data *prog = (struct prog_data *)user_data;
/* Flush if this section is discontiguous */
if (prog->len && prog->addr + prog->len != addr &&
prog_flush(prog) < 0)
return -1;
if (!prog->len)
prog->addr = addr;
/* Add the buffer in piece by piece, flushing when it gets
* full.
*/
while (len) {
int count = sizeof(prog->buf) - prog->len;
if (count > len)
count = len;
if (!count) {
if (prog_flush(prog) < 0)
return -1;
} else {
memcpy(prog->buf + prog->len, data, count);
prog->len += count;
data += count;
len -= count;
}
}
return 0;
return prog_feed((struct prog_data *)user_data, addr, data, len);
}
int cmd_prog(char **arg)
@ -540,9 +476,9 @@ int cmd_prog(char **arg)
return -1;
}
memset(&prog, 0, sizeof(prog));
prog_init(&prog, PROG_WANT_ERASE);
if (binfile_extract(in, prog_feed, &prog) < 0) {
if (binfile_extract(in, cmd_prog_feed, &prog) < 0) {
fclose(in);
return -1;
}

8
gdb.c
View File

@ -28,6 +28,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include "device.h"
#include "prog.h"
#include "util.h"
#include "opdb.h"
#include "gdb.h"
@ -341,6 +342,7 @@ static int write_memory(struct gdb_data *data, char *text)
address_t length, addr;
uint8_t buf[MAX_MEM_XFER];
int buflen = 0;
struct prog_data prog;
if (!(data_text && length_text)) {
printc_err("gdb: malformed memory write request\n");
@ -364,9 +366,9 @@ static int write_memory(struct gdb_data *data, char *text)
return gdb_send(data, "E00");
}
printc("Writing %d bytes to 0x%04x\n", buflen, addr);
if (device_default->writemem(device_default, addr, buf, buflen) < 0)
prog_init(&prog, 0);
if (prog_feed(&prog, addr, buf, buflen) < 0 ||
prog_flush(&prog) < 0)
return gdb_send(data, "E00");
return gdb_send(data, "OK");

105
prog.c Normal file
View File

@ -0,0 +1,105 @@
/* MSPDebug - debugging tool for the eZ430
* Copyright (C) 2009, 2010 Daniel Beer
*
* 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.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include "device.h"
#include "prog.h"
#include "output.h"
void prog_init(struct prog_data *prog, int flags)
{
memset(prog, 0, sizeof(*prog));
prog->flags = flags;
}
int prog_flush(struct prog_data *prog)
{
if (!prog->len)
return 0;
if (!prog->have_erased && (prog->flags & PROG_WANT_ERASE)) {
printc("Erasing...\n");
if (device_default->ctl(device_default,
DEVICE_CTL_ERASE) < 0)
return -1;
prog->have_erased = 1;
}
/* If writing an odd number of bytes to flash memory, add a
* trailing pad byte.
*/
if (prog->addr + prog->len >= device_default->code_start &&
(prog->len & 1)) {
printc_dbg("prog: adding trailing pad byte\n");
prog->buf[prog->len++] = 0xff;
}
printc_dbg("Writing %3d bytes to %04x...\n", prog->len, prog->addr);
if (device_default->writemem(device_default, prog->addr,
prog->buf, prog->len) < 0)
return -1;
prog->addr += prog->len;
prog->len = 0;
return 0;
}
int prog_feed(struct prog_data *prog, address_t addr,
const uint8_t *data, int len)
{
/* Flush if this section is discontiguous */
if (prog->len && prog->addr + prog->len != addr &&
prog_flush(prog) < 0)
return -1;
if (!prog->len) {
/* If starting at an odd address, add a leading pad byte. */
if (prog->addr + len >= device_default->code_start &&
(addr & 1)) {
printc_dbg("prog: adding initial pad byte\n");
prog->addr = addr - 1;
prog->len = 1;
prog->buf[0] = 0xff;
} else {
prog->addr = addr;
}
}
/* Add the buffer in piece by piece, flushing when it gets
* full.
*/
while (len) {
int count = sizeof(prog->buf) - prog->len;
if (count > len)
count = len;
if (!count) {
if (prog_flush(prog) < 0)
return -1;
} else {
memcpy(prog->buf + prog->len, data, count);
prog->len += count;
data += count;
len -= count;
}
}
return 0;
}

39
prog.h Normal file
View File

@ -0,0 +1,39 @@
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009, 2010 Daniel Beer
*
* 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.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PROG_H_
#define PROG_H_
#define PROG_BUFSIZE 128
struct prog_data {
uint8_t buf[PROG_BUFSIZE];
address_t addr;
int len;
int flags;
int have_erased;
};
#define PROG_WANT_ERASE 0x01
void prog_init(struct prog_data *data, int flags);
int prog_feed(struct prog_data *data, address_t addr,
const uint8_t *buffer, int count);
int prog_flush(struct prog_data *data);
#endif