Merge pull request #180 from nekromant/info
[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, int ac, char** av)
22 {
23         int ret = 0;
24         if (strcmp(av[1], "--flash") == 0) 
25                 printf("0x%lx\n", sl->flash_size);
26         else if (strcmp(av[1], "--sram") == 0)
27                 printf("0x%lx\n", sl->sram_size);
28         else if (strcmp(av[1], "--pagesize") == 0)
29                 printf("0x%lx\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
46
47
48 stlink_t* open_sl() 
49 {
50         stlink_t* sl;
51         sl = stlink_v1_open(0, 1);
52         if (sl == NULL)
53                 sl = stlink_open_usb(0, 1);
54         return sl;
55 }
56
57
58 int main(int ac, char** av)
59 {
60         stlink_t* sl = NULL;
61         int err = -1;
62         if (ac < 2) {   
63                 usage();
64                 return -1;
65         }
66
67         sl = open_sl(); 
68         
69         if (sl == NULL) {
70                 return -1;
71         }
72         sl->verbose=0;
73         if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
74                 stlink_exit_dfu_mode(sl);
75
76         if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
77                 stlink_enter_swd_mode(sl);
78
79         err = print_data(sl, ac, av);
80         
81         if (sl != NULL)
82         {
83                 stlink_exit_debug_mode(sl);
84                 stlink_close(sl);
85         }
86
87         return err;
88 }