]> git.sur5r.net Git - openocd/blob - src/jtag/stlink/stlink_tcl.c
ST-LINK USB initial release
[openocd] / src / jtag / stlink / stlink_tcl.c
1 /***************************************************************************
2  *   Copyright (C) 2011 by Mathias Kuester                                 *
3  *   Mathias Kuester <kesmtp@freenet.de>                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 /* project specific includes */
26 #include <jtag/interface.h>
27 #include <transport/transport.h>
28 #include <helper/time_support.h>
29
30 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
31                                   struct jtag_tap *pTap)
32 {
33         jim_wide w;
34         int e = Jim_GetOpt_Wide(goi, &w);
35         if (e != JIM_OK) {
36                 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter",
37                                        n->name);
38                 return e;
39         }
40
41         unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
42         uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
43         if (new_expected_ids == NULL) {
44                 Jim_SetResultFormatted(goi->interp, "no memory");
45                 return JIM_ERR;
46         }
47
48         memcpy(new_expected_ids, pTap->expected_ids, expected_len);
49
50         new_expected_ids[pTap->expected_ids_cnt] = w;
51
52         free(pTap->expected_ids);
53         pTap->expected_ids = new_expected_ids;
54         pTap->expected_ids_cnt++;
55
56         return JIM_OK;
57 }
58
59 #define NTAP_OPT_EXPECTED_ID 0
60
61 static int jim_stlink_newtap_cmd(Jim_GetOptInfo *goi)
62 {
63         struct jtag_tap *pTap;
64         int x;
65         int e;
66         Jim_Nvp *n;
67         char *cp;
68         const Jim_Nvp opts[] = {
69                 {.name = "-expected-id", .value = NTAP_OPT_EXPECTED_ID},
70                 {.name = NULL, .value = -1},
71         };
72
73         pTap = calloc(1, sizeof(struct jtag_tap));
74         if (!pTap) {
75                 Jim_SetResultFormatted(goi->interp, "no memory");
76                 return JIM_ERR;
77         }
78
79         /*
80          * we expect CHIP + TAP + OPTIONS
81          * */
82         if (goi->argc < 3) {
83                 Jim_SetResultFormatted(goi->interp,
84                                        "Missing CHIP TAP OPTIONS ....");
85                 free(pTap);
86                 return JIM_ERR;
87         }
88         Jim_GetOpt_String(goi, &cp, NULL);
89         pTap->chip = strdup(cp);
90
91         Jim_GetOpt_String(goi, &cp, NULL);
92         pTap->tapname = strdup(cp);
93
94         /* name + dot + name + null */
95         x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
96         cp = malloc(x);
97         sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
98         pTap->dotted_name = cp;
99
100         LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
101                   pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
102
103         while (goi->argc) {
104                 e = Jim_GetOpt_Nvp(goi, opts, &n);
105                 if (e != JIM_OK) {
106                         Jim_GetOpt_NvpUnknown(goi, opts, 0);
107                         free((void *)pTap->dotted_name);
108                         free(pTap);
109                         return e;
110                 }
111                 LOG_DEBUG("Processing option: %s", n->name);
112                 switch (n->value) {
113                 case NTAP_OPT_EXPECTED_ID:
114                         e = jim_newtap_expected_id(n, goi, pTap);
115                         if (JIM_OK != e) {
116                                 free((void *)pTap->dotted_name);
117                                 free(pTap);
118                                 return e;
119                         }
120                         break;
121                 }               /* switch (n->value) */
122         }                       /* while (goi->argc) */
123
124         /* default is enabled-after-reset */
125         pTap->enabled = !pTap->disabled_after_reset;
126
127         jtag_tap_init(pTap);
128         return JIM_OK;
129 }
130
131 int jim_stlink_newtap(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
132 {
133         Jim_GetOptInfo goi;
134         Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
135         return jim_stlink_newtap_cmd(&goi);
136 }