Fixed flash utility for STM32F4
[fw/stlink] / flash / main.c
index 5fb6f1c97f902687250d4f238258749fd373d893..278d63d1878b5100985300867967f9a082d67c80 100644 (file)
@@ -17,6 +17,11 @@ struct opts
   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>");
+}
 
 static int get_opts(struct opts* o, int ac, char** av)
 {
@@ -75,29 +80,38 @@ int main(int ac, char** av)
   if (get_opts(&o, ac - 1, av + 1) == -1)
   {
     printf("invalid command line\n");
+    usage();
     goto on_error;
   }
 
   if (o.devname != NULL) /* stlinkv1 */
   {
+#if CONFIG_USE_LIBSG
     static const int scsi_verbose = 2;
     sl = stlink_quirk_open(o.devname, scsi_verbose);
     if (sl == NULL) goto on_error;
-
-    if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
-      stlink_exit_dfu_mode(sl);
+#else
+    printf("not compiled for use with STLink/V1");
+    goto on_error;
+#endif
   }
   else /* stlinkv2 */
   {
-    sl = stlink_open_usb(NULL, 10);
+    sl = stlink_open_usb(1);
     if (sl == NULL) goto on_error;
   }
 
-  stlink_enter_swd_mode(sl);
+  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)
     {