[update] flash tool, missing SWD mode entering and core reset
[fw/stlink] / flash / main.c
1 /* simple wrapper around the stlink_flash_write function */
2
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include "stlink-common.h"
7
8
9 int main(int ac, char** av)
10 {
11   /* stlinkv1 command line: ./flash /dev/sgX path addr */
12   /* stlinkv2 command line: ./flash path addr */
13
14   stlink_t* sl = NULL;
15   stm32_addr_t addr;
16   const char* path;
17   int err = -1;
18
19   if (ac == 4) /* stlinkv1 */
20   {
21     static const int scsi_verbose = 2;
22     sl = stlink_quirk_open(av[1], scsi_verbose);
23     path = av[2];
24     addr = strtoul(av[3], NULL, 16);
25   }
26   else if (ac == 3) /* stlinkv2 */
27   {
28     sl = stlink_open_usb(NULL, 10);
29     path = av[1];
30     addr = strtoul(av[2], NULL, 16);
31   }
32   else /* invalid */
33   {
34     printf("invalid command line\n");
35     goto on_error;
36   }
37
38   if (sl == NULL) goto on_error;
39
40   if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
41     stlink_exit_dfu_mode(sl);
42   stlink_enter_swd_mode(sl);
43   stlink_reset(sl);
44
45   err = stlink_fwrite_flash(sl, path, addr);
46   if (err == -1)
47   {
48     printf("stlink_fwrite_flash() == -1\n");
49     goto on_error;
50   }
51
52   /* success */
53   err = 0;
54
55  on_error:
56   if (sl != NULL) stlink_close(sl);
57
58   return err;
59 }