Merge pull request #223 from troth/troth/fix-compiler-warning-32bit
[fw/stlink] / src / st-info.c
1 /* simple wrapper around the stlink_flash_write function */
2
3 // TODO - this should be done as just a simple flag to the st-util command line...
4
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include "stlink-common.h"
11
12 static void usage(void)
13 {
14         puts("st-info --flash");
15         puts("st-info --sram");
16         puts("st-info --descr");
17         puts("st-info --pagesize");
18         puts("st-info --chipid");
19 }
20
21 static int print_data(stlink_t* sl, char** av)
22 {
23         int ret = 0;
24         if (strcmp(av[1], "--flash") == 0) 
25                 printf("0x%zx\n", sl->flash_size);
26         else if (strcmp(av[1], "--sram") == 0)
27                 printf("0x%zx\n", sl->sram_size);
28         else if (strcmp(av[1], "--pagesize") == 0)
29                 printf("0x%zx\n", sl->flash_pgsz);
30         else if (strcmp(av[1], "--chipid") == 0)
31                 printf("0x%.4x\n", sl->chip_id);
32         else if (strcmp(av[1], "--descr")==0) {
33                 const chip_params_t *params = NULL;
34                 for (size_t i = 0; i < sizeof(devices) / sizeof(devices[0]); i++) {
35                         if(devices[i].chip_id == sl->chip_id) {
36                                 params = &devices[i];
37                                 break;
38                         }
39                 }
40                 if (params == NULL) {
41                         return -1;
42                 }
43                 printf("%s\n", params->description);
44         }
45         return ret;
46
47
48
49 stlink_t* open_sl(void) 
50 {
51         stlink_t* sl;
52         sl = stlink_v1_open(0, 1);
53         if (sl == NULL)
54                 sl = stlink_open_usb(0, 1);
55         return sl;
56 }
57
58
59 int main(int ac, char** av)
60 {
61         stlink_t* sl = NULL;
62         int err = -1;
63         if (ac < 2) {   
64                 usage();
65                 return -1;
66         }
67
68         sl = open_sl(); 
69         
70         if (sl == NULL) {
71                 return -1;
72         }
73         sl->verbose=0;
74         if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
75                 stlink_exit_dfu_mode(sl);
76
77         if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
78                 stlink_enter_swd_mode(sl);
79
80         err = print_data(sl, av);
81         
82         if (sl != NULL)
83         {
84                 stlink_exit_debug_mode(sl);
85                 stlink_close(sl);
86         }
87
88         return err;
89 }