stlink/flash/main.c

144 wiersze
2.5 KiB
C
Czysty Zwykły widok Historia

2011-10-16 20:36:11 +00:00
/* simple wrapper around the stlink_flash_write function */
// TODO - this should be done as just a simple flag to the st-util command line...
2011-10-16 20:36:11 +00:00
#include <stdio.h>
#include <stdlib.h>
2011-10-22 02:11:04 +00:00
#include <string.h>
#include <sys/types.h>
2011-10-16 20:36:11 +00:00
#include "stlink-common.h"
2011-10-22 02:11:04 +00:00
struct opts
2011-10-16 20:36:11 +00:00
{
2011-10-22 02:11:04 +00:00
unsigned int do_read;
const char* devname;
const char* filename;
2011-10-16 20:36:11 +00:00
stm32_addr_t addr;
2011-10-22 02:11:04 +00:00
size_t size;
};
static void usage(void)
{
puts("stlinkv1 command line: ./flash {read|write} /dev/sgX path addr <size>");
puts("stlinkv2 command line: ./flash {read|write} path addr <size>");
puts(" use hex format for addr and <size>");
2011-10-22 02:11:04 +00:00
}
static int get_opts(struct opts* o, int ac, char** av)
{
/* stlinkv1 command line: ./flash {read|write} /dev/sgX path addr <size> */
/* stlinkv2 command line: ./flash {read|write} path addr <size> */
unsigned int i = 0;
if (ac < 3) return -1;
2011-10-16 20:36:11 +00:00
2011-10-22 02:11:04 +00:00
/* stlinkv2 */
o->devname = NULL;
if (strcmp(av[0], "read") == 0)
2011-10-16 20:36:11 +00:00
{
2011-10-22 02:11:04 +00:00
o->do_read = 1;
/* stlinkv1 mode */
if (ac == 5)
{
o->devname = av[1];
i = 1;
}
if (ac > 3)
o->size = strtoul(av[i + 3], NULL, 16);
2011-10-16 20:36:11 +00:00
}
2011-10-22 02:11:04 +00:00
else if (strcmp(av[0], "write") == 0)
2011-10-16 20:36:11 +00:00
{
2011-10-22 02:11:04 +00:00
o->do_read = 0;
/* stlinkv1 mode */
if (ac == 4)
{
o->devname = av[1];
i = 1;
}
2011-10-16 20:36:11 +00:00
}
2011-10-22 02:11:04 +00:00
else
{
return -1;
}
o->filename = av[i + 1];
o->addr = strtoul(av[i + 2], NULL, 16);
return 0;
}
int main(int ac, char** av)
{
stlink_t* sl = NULL;
struct opts o;
int err = -1;
o.size = 0;
2011-10-22 02:11:04 +00:00
if (get_opts(&o, ac - 1, av + 1) == -1)
2011-10-16 20:36:11 +00:00
{
printf("invalid command line\n");
2011-10-22 02:11:04 +00:00
usage();
2011-10-16 20:36:11 +00:00
goto on_error;
}
2011-10-22 02:11:04 +00:00
if (o.devname != NULL) /* stlinkv1 */
2011-10-16 20:36:11 +00:00
{
sl = stlink_v1_open(50);
sl->verbose = 50;
2011-10-22 02:11:04 +00:00
if (sl == NULL) goto on_error;
}
else /* stlinkv2 */
{
sl = stlink_open_usb(50);
sl->verbose = 50;
2011-10-22 02:11:04 +00:00
if (sl == NULL) goto on_error;
2011-10-16 20:36:11 +00:00
}
2011-10-22 02:11:04 +00:00
if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
stlink_exit_dfu_mode(sl);
if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
stlink_enter_swd_mode(sl);
stlink_reset(sl);
if (o.do_read == 0) /* write */
{
err = stlink_fwrite_flash(sl, o.filename, o.addr);
if (err == -1)
{
printf("stlink_fwrite_flash() == -1\n");
goto on_error;
}
}
else /* read */
{
err = stlink_fread(sl, o.filename, o.addr, o.size);
if (err == -1)
{
printf("stlink_fread() == -1\n");
goto on_error;
}
}
/* success */
err = 0;
2011-10-16 20:36:11 +00:00
on_error:
if (sl != NULL)
{
stlink_reset(sl);
stlink_run(sl);
stlink_close(sl);
}
2011-10-16 20:36:11 +00:00
return err;
}