- Fixes '>=' whitespace
[openocd.git] / src / jtag / bitq.c
blob1af1be5424ef92fcfc6422bed7902e7fa9f72f6e
1 /***************************************************************************
2 * Copyright (C) 2007 by Pavel Chromy *
3 * chromy@asix.cz *
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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "bitq.h"
25 #include "interface.h"
28 bitq_interface_t* bitq_interface; /* low level bit queue interface */
30 static bitq_state_t bitq_in_state; /* state of input queue */
32 static uint8_t* bitq_in_buffer; /* buffer dynamically reallocated as needed */
33 static int bitq_in_bufsize = 32; /* min. buffer size */
36 * input queue processing does not use jtag_read_buffer() to avoid unnecessary overhead
37 * also the buffer for incomming data is reallocated only if necessary
38 * no parameters, makes use of stored state information
40 void bitq_in_proc(void)
42 /* static information preserved between calls to increase performance */
43 static uint8_t* in_buff; /* pointer to buffer for scanned data */
44 static int in_idx; /* index of byte being scanned */
45 static uint8_t in_mask; /* mask of next bit to be scanned */
47 scan_field_t* field;
48 int tdo;
50 /* loop through the queue */
51 while (bitq_in_state.cmd)
53 /* only JTAG_SCAN command may return data */
54 if (bitq_in_state.cmd->type==JTAG_SCAN)
56 /* loop through the fields */
57 while (bitq_in_state.field_idx<bitq_in_state.cmd->cmd.scan->num_fields)
59 field = &bitq_in_state.cmd->cmd.scan->fields[bitq_in_state.field_idx];
60 if (field->in_value)
62 if (bitq_in_state.bit_pos==0)
64 /* initialize field scanning */
65 in_mask = 0x01;
66 in_idx = 0;
67 if (field->in_value)
68 in_buff = field->in_value;
69 else
71 /* buffer reallocation needed? */
72 if (field->num_bits>bitq_in_bufsize * 8)
74 /* buffer previously allocated? */
75 if (bitq_in_buffer != NULL)
77 /* free it */
78 free(bitq_in_buffer);
79 bitq_in_buffer = NULL;
81 /* double the buffer size until it fits */
82 while (field->num_bits>bitq_in_bufsize * 8)
83 bitq_in_bufsize *= 2;
85 /* if necessary, allocate buffer and check for malloc error */
86 if (bitq_in_buffer==NULL && ( bitq_in_buffer = malloc(bitq_in_bufsize) )==NULL)
88 LOG_ERROR("malloc error");
89 exit(-1);
91 in_buff = (void*) bitq_in_buffer;
95 /* field scanning */
96 while (bitq_in_state.bit_pos<field->num_bits)
98 if ( ( tdo = bitq_interface->in() )<0 )
100 #ifdef _DEBUG_JTAG_IO_
101 LOG_DEBUG("bitq in EOF");
102 #endif
103 return;
105 if (in_mask==0x01)
106 in_buff[in_idx] = 0;
107 if (tdo)
108 in_buff[in_idx] |= in_mask;
109 if (in_mask==0x80)
111 in_mask = 0x01;
112 in_idx++;
114 else
115 in_mask <<= 1;
116 bitq_in_state.bit_pos++;
120 bitq_in_state.field_idx++; /* advance to next field */
121 bitq_in_state.bit_pos = 0; /* start next field from the first bit */
124 bitq_in_state.cmd = bitq_in_state.cmd->next; /* advance to next command */
125 bitq_in_state.field_idx = 0; /* preselect first field */
130 void bitq_io(int tms, int tdi, int tdo_req)
132 bitq_interface->out(tms, tdi, tdo_req);
133 /* check and process the input queue */
134 if ( bitq_interface->in_rdy() )
135 bitq_in_proc();
139 void bitq_end_state(tap_state_t state)
141 if (!tap_is_state_stable(state))
143 LOG_ERROR("BUG: %i is not a valid end state", state);
144 exit(-1);
146 tap_set_end_state(state);
150 void bitq_state_move(tap_state_t new_state)
152 int i = 0;
153 uint8_t tms_scan;
155 if (!tap_is_state_stable(tap_get_state()) || !tap_is_state_stable(new_state))
157 LOG_ERROR("TAP move from or to unstable state");
158 exit(-1);
161 tms_scan = tap_get_tms_path(tap_get_state(), new_state);
162 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
164 for (i = 0; i<tms_count; i++)
166 bitq_io(tms_scan & 1, 0, 0);
167 tms_scan >>= 1;
170 tap_set_state(new_state);
174 void bitq_path_move(pathmove_command_t* cmd)
176 int i;
178 for (i = 0; i<=cmd->num_states; i++)
180 if (tap_state_transition(tap_get_state(), false) == cmd->path[i])
181 bitq_io(0, 0, 0);
182 else if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
183 bitq_io(1, 0, 0);
184 else
186 LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
187 tap_get_state() ), tap_state_name(cmd->path[i]) );
188 exit(-1);
191 tap_set_state(cmd->path[i]);
194 tap_set_end_state( tap_get_state() );
198 void bitq_runtest(int num_cycles)
200 int i;
202 /* only do a state_move when we're not already in IDLE */
203 if (tap_get_state() != TAP_IDLE)
204 bitq_state_move(TAP_IDLE);
206 /* execute num_cycles */
207 for (i = 0; i < num_cycles; i++)
208 bitq_io(0, 0, 0);
210 /* finish in end_state */
211 if ( tap_get_state() != tap_get_end_state() )
212 bitq_state_move( tap_get_end_state() );
216 void bitq_scan_field(scan_field_t* field, int pause)
218 int bit_cnt;
219 int tdo_req;
221 uint8_t* out_ptr;
222 uint8_t out_mask;
224 if (field->in_value)
225 tdo_req = 1;
226 else
227 tdo_req = 0;
229 if (field->out_value==NULL)
231 /* just send zeros and request data from TDO */
232 for (bit_cnt = field->num_bits; bit_cnt>1; bit_cnt--)
233 bitq_io(0, 0, tdo_req);
235 bitq_io(pause, 0, tdo_req);
237 else
239 /* send data, and optionally request TDO */
240 out_mask = 0x01;
241 out_ptr = field->out_value;
242 for (bit_cnt = field->num_bits; bit_cnt>1; bit_cnt--)
244 bitq_io(0, ( (*out_ptr) & out_mask ) != 0, tdo_req);
245 if (out_mask==0x80)
247 out_mask = 0x01;
248 out_ptr++;
250 else
251 out_mask <<= 1;
254 bitq_io(pause, ( (*out_ptr) & out_mask ) != 0, tdo_req);
257 if (pause)
259 bitq_io(0, 0, 0);
260 if (tap_get_state()==TAP_IRSHIFT)
261 tap_set_state(TAP_IRPAUSE);
262 else if (tap_get_state()==TAP_DRSHIFT)
263 tap_set_state(TAP_DRPAUSE);
268 void bitq_scan(scan_command_t* cmd)
270 int i;
272 if (cmd->ir_scan)
273 bitq_state_move(TAP_IRSHIFT);
274 else
275 bitq_state_move(TAP_DRSHIFT);
277 for (i = 0; i < cmd->num_fields - 1; i++)
278 bitq_scan_field(&cmd->fields[i], 0);
280 bitq_scan_field(&cmd->fields[i], 1);
284 int bitq_execute_queue(void)
286 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
288 bitq_in_state.cmd = jtag_command_queue;
289 bitq_in_state.field_idx = 0;
290 bitq_in_state.bit_pos = 0;
291 bitq_in_state.status = ERROR_OK;
293 while (cmd)
295 switch (cmd->type)
297 case JTAG_RESET:
298 #ifdef _DEBUG_JTAG_IO_
299 LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
300 #endif
301 if ( (cmd->cmd.reset->trst == 1) || ( cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST) ) )
303 tap_set_state(TAP_RESET);
305 bitq_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
306 if ( bitq_interface->in_rdy() )
307 bitq_in_proc();
308 break;
310 case JTAG_RUNTEST:
311 #ifdef _DEBUG_JTAG_IO_
312 LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
313 #endif
314 bitq_end_state(cmd->cmd.runtest->end_state);
315 bitq_runtest(cmd->cmd.runtest->num_cycles);
316 break;
318 case JTAG_STATEMOVE:
319 #ifdef _DEBUG_JTAG_IO_
320 LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
321 #endif
322 bitq_end_state(cmd->cmd.statemove->end_state);
323 bitq_state_move( tap_get_end_state() ); /* uncoditional TAP move */
324 break;
326 case JTAG_PATHMOVE:
327 #ifdef _DEBUG_JTAG_IO_
328 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states,
329 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
330 #endif
331 bitq_path_move(cmd->cmd.pathmove);
332 break;
334 case JTAG_SCAN:
335 #ifdef _DEBUG_JTAG_IO_
336 LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
337 if (cmd->cmd.scan->ir_scan)
338 LOG_DEBUG("scan ir");
339 else
340 LOG_DEBUG("scan dr");
341 #endif
342 bitq_end_state(cmd->cmd.scan->end_state);
343 bitq_scan(cmd->cmd.scan);
344 if ( tap_get_state() != tap_get_end_state() )
345 bitq_state_move( tap_get_end_state() );
346 break;
348 case JTAG_SLEEP:
349 #ifdef _DEBUG_JTAG_IO_
350 LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
351 #endif
352 bitq_interface->sleep(cmd->cmd.sleep->us);
353 if ( bitq_interface->in_rdy() )
354 bitq_in_proc();
355 break;
357 default:
358 LOG_ERROR("BUG: unknown JTAG command type encountered");
359 exit(-1);
362 cmd = cmd->next;
365 bitq_interface->flush();
366 bitq_in_proc();
368 if (bitq_in_state.cmd)
370 LOG_ERROR("missing data from bitq interface");
371 return ERROR_JTAG_QUEUE_FAILED;
373 if (bitq_interface->in() >= 0)
375 LOG_ERROR("extra data from bitq interface");
376 return ERROR_JTAG_QUEUE_FAILED;
379 return bitq_in_state.status;
383 void bitq_cleanup(void)
385 if (bitq_in_buffer != NULL)
387 free(bitq_in_buffer);
388 bitq_in_buffer = NULL;