[add] flash memory loader tool
authorFabien Le Mentec <texane@gmail.com>
Sun, 16 Oct 2011 20:36:11 +0000 (15:36 -0500)
committerFabien Le Mentec <texane@gmail.com>
Sun, 16 Oct 2011 20:36:11 +0000 (15:36 -0500)
flash/Makefile [new file with mode: 0644]
flash/main.c [new file with mode: 0644]

diff --git a/flash/Makefile b/flash/Makefile
new file mode 100644 (file)
index 0000000..fe0dff7
--- /dev/null
@@ -0,0 +1,28 @@
+CFLAGS+=-g
+CFLAGS+=-DCONFIG_USE_LIBUSB
+CFLAGS+=-DCONFIG_USE_LIBSG
+CFLAGS+=-DDEBUG
+CFLAGS+=-std=gnu99
+CFLAGS+=-Wall -Wextra
+CFLAGS+=-I../src
+
+LDFLAGS=-L.. -lstlink -lusb-1.0 -lsgutils2
+
+SRCS=main.c
+OBJS=$(SRCS:.c=.o)
+
+NAME=flash
+
+all: $(NAME)
+
+$(NAME): $(OBJS)
+       $(CC) $(CFLAGS) -o $(NAME) $(OBJS) $(LDFLAGS)
+
+%.o: %.c
+       $(CC) $(CFLAGS) -c $^ -o $@
+
+clean:
+       rm -f $(OBJS)
+       rm -f $(NAME)
+
+.PHONY: clean all
diff --git a/flash/main.c b/flash/main.c
new file mode 100644 (file)
index 0000000..c8b15e0
--- /dev/null
@@ -0,0 +1,51 @@
+/* simple wrapper around the stlink_flash_write function */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "stlink-common.h"
+
+
+int main(int ac, char** av)
+{
+  /* stlinkv1 command line: ./flash /dev/sgX path addr */
+  /* stlinkv2 command line: ./flash path addr */
+
+  stlink_t* sl = NULL;
+  stm32_addr_t addr;
+  const char* path;
+  int err;
+
+  if (ac == 4) /* stlinkv1 */
+  {
+    static const int scsi_verbose = 2;
+    sl = stlink_quirk_open(av[1], scsi_verbose);
+    path = av[2];
+    addr = strtoul(av[3], NULL, 16);
+  }
+  else if (ac == 3) /* stlinkv2 */
+  {
+    sl = stlink_open_usb(NULL, 10);
+    path = av[1];
+    addr = strtoul(av[2], NULL, 16);
+  }
+  else /* invalid */
+  {
+    printf("invalid command line\n");
+    goto on_error;
+  }
+
+  if (sl == NULL) goto on_error;
+
+  err = stlink_fwrite_flash(sl, path, addr);
+  if (err == -1)
+  {
+    printf("stlink_fwrite_flash() == -1\n");
+    goto on_error;
+  }
+
+ on_error:
+  if (sl != NULL) stlink_close(sl);
+
+  return err;
+}