86838b0ca8ad0311d18a98df49ca3d5196b40401
[openocd.git] / src / jtag / hla / hla_tcl.c
blob86838b0ca8ad0311d18a98df49ca3d5196b40401
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, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 /* project specific includes */
29 #include <jtag/interface.h>
30 #include <transport/transport.h>
31 #include <helper/time_support.h>
33 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
34 struct jtag_tap *pTap)
36 jim_wide w;
37 int e = Jim_GetOpt_Wide(goi, &w);
38 if (e != JIM_OK) {
39 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter",
40 n->name);
41 return e;
44 unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
45 uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
46 if (new_expected_ids == NULL) {
47 Jim_SetResultFormatted(goi->interp, "no memory");
48 return JIM_ERR;
51 memcpy(new_expected_ids, pTap->expected_ids, expected_len);
53 new_expected_ids[pTap->expected_ids_cnt] = w;
55 free(pTap->expected_ids);
56 pTap->expected_ids = new_expected_ids;
57 pTap->expected_ids_cnt++;
59 return JIM_OK;
62 #define NTAP_OPT_EXPECTED_ID 0
64 static int jim_hl_newtap_cmd(Jim_GetOptInfo *goi)
66 struct jtag_tap *pTap;
67 int x;
68 int e;
69 Jim_Nvp *n;
70 char *cp;
71 const Jim_Nvp opts[] = {
72 {.name = "-expected-id", .value = NTAP_OPT_EXPECTED_ID},
73 {.name = NULL, .value = -1},
76 pTap = calloc(1, sizeof(struct jtag_tap));
77 if (!pTap) {
78 Jim_SetResultFormatted(goi->interp, "no memory");
79 return JIM_ERR;
83 * we expect CHIP + TAP + OPTIONS
84 * */
85 if (goi->argc < 3) {
86 Jim_SetResultFormatted(goi->interp,
87 "Missing CHIP TAP OPTIONS ....");
88 free(pTap);
89 return JIM_ERR;
91 Jim_GetOpt_String(goi, &cp, NULL);
92 pTap->chip = strdup(cp);
94 Jim_GetOpt_String(goi, &cp, NULL);
95 pTap->tapname = strdup(cp);
97 /* name + dot + name + null */
98 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
99 cp = malloc(x);
100 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
101 pTap->dotted_name = cp;
103 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
104 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
106 while (goi->argc) {
107 e = Jim_GetOpt_Nvp(goi, opts, &n);
108 if (e != JIM_OK) {
109 Jim_GetOpt_NvpUnknown(goi, opts, 0);
110 free((void *)pTap->dotted_name);
111 free(pTap);
112 return e;
114 LOG_DEBUG("Processing option: %s", n->name);
115 switch (n->value) {
116 case NTAP_OPT_EXPECTED_ID:
117 e = jim_newtap_expected_id(n, goi, pTap);
118 if (JIM_OK != e) {
119 free((void *)pTap->dotted_name);
120 free(pTap);
121 return e;
123 break;
124 } /* switch (n->value) */
125 } /* while (goi->argc) */
127 /* default is enabled-after-reset */
128 pTap->enabled = !pTap->disabled_after_reset;
130 jtag_tap_init(pTap);
131 return JIM_OK;
134 int jim_hl_newtap(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
136 Jim_GetOptInfo goi;
137 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
138 return jim_hl_newtap_cmd(&goi);