docs: include static members in doxygen output
[openocd/ntfreak.git] / src / jtag / commands.c
blob546b12f8bfef0d935018b806895f3c0e9c34c8bc
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2009 SoftPLC Corporation *
9 * http://softplc.com *
10 * dick@softplc.com *
11 * *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
14 * *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
19 * *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
24 * *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
29 ***************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #include <jtag/jtag.h>
36 #include "commands.h"
38 struct cmd_queue_page {
39 void *address;
40 size_t used;
41 struct cmd_queue_page *next;
44 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
45 static struct cmd_queue_page *cmd_queue_pages;
47 struct jtag_command *jtag_command_queue;
48 static struct jtag_command **next_command_pointer = &jtag_command_queue;
50 void jtag_queue_command(struct jtag_command *cmd)
52 /* this command goes on the end, so ensure the queue terminates */
53 cmd->next = NULL;
55 struct jtag_command **last_cmd = next_command_pointer;
56 assert(NULL != last_cmd);
57 assert(NULL == *last_cmd);
58 *last_cmd = cmd;
60 /* store location where the next command pointer will be stored */
61 next_command_pointer = &cmd->next;
64 void *cmd_queue_alloc(size_t size)
66 struct cmd_queue_page **p_page = &cmd_queue_pages;
67 int offset;
68 uint8_t *t;
71 * WARNING:
72 * We align/round the *SIZE* per below
73 * so that all pointers returned by
74 * this function are reasonably well
75 * aligned.
77 * If we did not, then an "odd-length" request would cause the
78 * *next* allocation to be at an *odd* address, and because
79 * this function has the same type of api as malloc() - we
80 * must also return pointers that have the same type of
81 * alignment.
83 * What I do not/have is a reasonable portable means
84 * to align by...
86 * The solution here, is based on these suggestions.
87 * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
90 union worse_case_align {
91 int i;
92 long l;
93 float f;
94 void *v;
96 #define ALIGN_SIZE (sizeof(union worse_case_align))
98 /* The alignment process. */
99 size = (size + ALIGN_SIZE - 1) & (~(ALIGN_SIZE - 1));
100 /* Done... */
102 if (*p_page) {
103 while ((*p_page)->next)
104 p_page = &((*p_page)->next);
105 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
106 p_page = &((*p_page)->next);
109 if (!*p_page) {
110 *p_page = malloc(sizeof(struct cmd_queue_page));
111 (*p_page)->used = 0;
112 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
113 (*p_page)->next = NULL;
116 offset = (*p_page)->used;
117 (*p_page)->used += size;
119 t = (uint8_t *)((*p_page)->address);
120 return t + offset;
123 static void cmd_queue_free(void)
125 struct cmd_queue_page *page = cmd_queue_pages;
127 while (page) {
128 struct cmd_queue_page *last = page;
129 free(page->address);
130 page = page->next;
131 free(last);
134 cmd_queue_pages = NULL;
137 void jtag_command_queue_reset(void)
139 cmd_queue_free();
141 jtag_command_queue = NULL;
142 next_command_pointer = &jtag_command_queue;
145 enum scan_type jtag_scan_type(const struct scan_command *cmd)
147 int i;
148 int type = 0;
150 for (i = 0; i < cmd->num_fields; i++) {
151 if (cmd->fields[i].in_value)
152 type |= SCAN_IN;
153 if (cmd->fields[i].out_value)
154 type |= SCAN_OUT;
157 return type;
160 int jtag_scan_size(const struct scan_command *cmd)
162 int bit_count = 0;
163 int i;
165 /* count bits in scan command */
166 for (i = 0; i < cmd->num_fields; i++)
167 bit_count += cmd->fields[i].num_bits;
169 return bit_count;
172 int jtag_build_buffer(const struct scan_command *cmd, uint8_t **buffer)
174 int bit_count = 0;
175 int i;
177 bit_count = jtag_scan_size(cmd);
178 *buffer = calloc(1, DIV_ROUND_UP(bit_count, 8));
180 bit_count = 0;
182 DEBUG_JTAG_IO("%s num_fields: %i",
183 cmd->ir_scan ? "IRSCAN" : "DRSCAN",
184 cmd->num_fields);
186 for (i = 0; i < cmd->num_fields; i++) {
187 if (cmd->fields[i].out_value) {
188 #ifdef _DEBUG_JTAG_IO_
189 char *char_buf = buf_to_str(cmd->fields[i].out_value,
190 (cmd->fields[i].num_bits > DEBUG_JTAG_IOZ)
191 ? DEBUG_JTAG_IOZ
192 : cmd->fields[i].num_bits, 16);
194 LOG_DEBUG("fields[%i].out_value[%i]: 0x%s", i,
195 cmd->fields[i].num_bits, char_buf);
196 free(char_buf);
197 #endif
198 buf_set_buf(cmd->fields[i].out_value, 0, *buffer,
199 bit_count, cmd->fields[i].num_bits);
200 } else {
201 DEBUG_JTAG_IO("fields[%i].out_value[%i]: NULL",
202 i, cmd->fields[i].num_bits);
205 bit_count += cmd->fields[i].num_bits;
208 /*DEBUG_JTAG_IO("bit_count totalling: %i", bit_count); */
210 return bit_count;
213 int jtag_read_buffer(uint8_t *buffer, const struct scan_command *cmd)
215 int i;
216 int bit_count = 0;
217 int retval;
219 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
220 retval = ERROR_OK;
222 for (i = 0; i < cmd->num_fields; i++) {
223 /* if neither in_value nor in_handler
224 * are specified we don't have to examine this field
226 if (cmd->fields[i].in_value) {
227 int num_bits = cmd->fields[i].num_bits;
228 uint8_t *captured = buf_set_buf(buffer, bit_count,
229 malloc(DIV_ROUND_UP(num_bits, 8)), 0, num_bits);
231 #ifdef _DEBUG_JTAG_IO_
232 char *char_buf = buf_to_str(captured,
233 (num_bits > DEBUG_JTAG_IOZ)
234 ? DEBUG_JTAG_IOZ
235 : num_bits, 16);
237 LOG_DEBUG("fields[%i].in_value[%i]: 0x%s",
238 i, num_bits, char_buf);
239 free(char_buf);
240 #endif
242 if (cmd->fields[i].in_value)
243 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
245 free(captured);
247 bit_count += cmd->fields[i].num_bits;
250 return retval;