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