Skip to content

Commit

Permalink
Add -E option to enable erasing only used flash
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Utzig committed Jun 11, 2013
1 parent dbd6d7f commit 0e55c4f
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lm4flash/lm4flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static const uint8_t ENDPOINT_OUT = 0x02;
#define END_LEN (strlen(END) + 2)

#define FLASH_BLOCK_SIZE 512
#define FLASH_ERASE_SIZE 1024

/* Prefix + potentially every flash byte escaped */
#define BUF_SIZE 64 + 2*FLASH_BLOCK_SIZE
Expand All @@ -91,7 +92,7 @@ static union {
void show_version(void)
{
printf("%s",
"LM4Flash version 0.1 - Flasher for Stellaris Launchpad ICDI boards\n"
"LM4Flash version 0.1.1 - Flasher for Stellaris Launchpad ICDI boards\n"
"Copyright (C) 2012 Fabio Utzig <[email protected]>\n"
"Copyright (C) 2012 Peter Stuge <[email protected]>\n"
"This is free software; see the source for copying conditions. There is NO\n"
Expand All @@ -114,6 +115,7 @@ static uint32_t le32_to_cpu(const uint32_t x)
}

static int do_verify = 0;
static int erase_used = 0;

#define cpu_to_le32 le32_to_cpu

Expand Down Expand Up @@ -473,6 +475,7 @@ static int write_firmware(libusb_device_handle *handle, FILE *f)
uint32_t addr;
size_t rdbytes;
int retval = 0;
uint32_t size;

print_icdi_version(handle);

Expand All @@ -498,7 +501,17 @@ static int write_firmware(libusb_device_handle *handle, FILE *f)

MEM_WRITE(FMA, 0x0);
MEM_READ(DHCSR, &val);
FLASH_ERASE(0, 0);

if (erase_used) {
fseek(f, 0, SEEK_END);
size = ftell(f);
for (addr = 0; addr < size; addr += FLASH_ERASE_SIZE)
FLASH_ERASE(addr, FLASH_ERASE_SIZE);
fseek(f, 0, SEEK_SET);
} else {
FLASH_ERASE(0, 0);
}

SEND_COMMAND("debug creset");
MEM_READ(DHCSR, &val);

Expand Down Expand Up @@ -686,6 +699,8 @@ static void flasher_usage()
printf("\t\tPrint usage information\n");
printf("\t-v\n");
printf("\t\tEnables verification after write\n");
printf("\t-E\n");
printf("\t\tOnly erase blocks where binary file will be written\n");
printf("\t-s SERIAL\n");
printf("\t\tFlash device with the following serial\n");
}
Expand Down Expand Up @@ -769,14 +784,17 @@ int main(int argc, char *argv[])
const char *rom_name = NULL;
int opt;

while ((opt = getopt(argc, argv, "Vhvs:")) != -1) {
while ((opt = getopt(argc, argv, "VEhvs:")) != -1) {
switch (opt) {
case 'V':
show_version();
return 0;
case 'h':
flasher_usage();
return 0;
case 'E':
erase_used = 1;
break;
case 'v':
do_verify = 1;
break;
Expand Down

0 comments on commit 0e55c4f

Please sign in to comment.