OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [or_debug_proxy/] [src/] [usb_functions.c] - Blame information for rev 1779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1779 julius
/*$$HEADER*/
2
/******************************************************************************/
3
/*                                                                            */
4
/*                    H E A D E R   I N F O R M A T I O N                     */
5
/*                                                                            */
6
/******************************************************************************/
7
 
8
// Project Name                   : OpenRISC Debug Proxy
9
// File Name                      : usb_functions.c
10
// Prepared By                    : jb
11
// Project Start                  : 2008-10-01
12
 
13
/*$$COPYRIGHT NOTICE*/
14
/******************************************************************************/
15
/*                                                                            */
16
/*                      C O P Y R I G H T   N O T I C E                       */
17
/*                                                                            */
18
/******************************************************************************/
19
/*
20
  This library is free software; you can redistribute it and/or
21
  modify it under the terms of the GNU Lesser General Public
22
  License as published by the Free Software Foundation;
23
  version 2.1 of the License, a copy of which is available from
24
  http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
25
 
26
  This library is distributed in the hope that it will be useful,
27
  but WITHOUT ANY WARRANTY; without even the implied warranty of
28
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29
  Lesser General Public License for more details.
30
 
31
  You should have received a copy of the GNU Lesser General Public
32
  License along with this library; if not, write to the Free Software
33
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
34
*/
35
 
36
/*$$DESCRIPTION*/
37
/******************************************************************************/
38
/*                                                                            */
39
/*                           D E S C R I P T I O N                            */
40
/*                                                                            */
41
/******************************************************************************/
42
//
43
// Code calling the FT2232 JTAG MPSSE driver functions. Does a majority of the 
44
// fiddly work when interfacing to the system via its debug module.
45
//
46
 
47
 
48
/*$$CHANGE HISTORY*/
49
/******************************************************************************/
50
/*                                                                            */
51
/*                         C H A N G E  H I S T O R Y                         */
52
/*                                                                            */
53
/******************************************************************************/
54
// Date         Version Description
55
//------------------------------------------------------------------------
56
// 081101               First revision                                  jb
57
 
58
#include <assert.h>
59
#include <stdio.h>
60
#include <ctype.h>
61
#include <string.h>
62
#include <stdlib.h>
63
#include <unistd.h>
64
#include <stdarg.h>
65
#include <sys/stat.h>
66
#include <sys/types.h>
67
 
68
#include <netinet/in.h>
69
 
70
#ifdef CYGWIN_COMPILE
71
#include <windows.h>
72
#include "win_FTCJTAG.h"
73
#else // We're on Linux or Mac OS X
74
#include "WinTypes.h" // We still use things from here in this file - TODO: remove this dependency
75
#include "FT2232cMpsseJtag.h"
76
#endif
77
 
78
#include "gdb.h"
79
 
80
#include "usb_functions.h"
81
 
82
#include "or_debug_proxy.h"
83
 
84
#include "usb_driver_calls.h"
85
 
86
// Global JTAG signals for reading/writing
87
DWORD dwNumBytesReturned = 0;
88
 
89
/* Crc of current read or written data.  */
90
uint32_t crc_r, crc_w = 0;
91
 
92
/* Generates new crc, sending in new bit input_bit */
93
uint32_t crc_calc(uint32_t crc, uint32_t input_bit) {
94
        uint32_t d = (input_bit) ? 0xfffffff : 0x0000000;
95
        uint32_t crc_32 = ((crc >> 31)&1) ? 0xfffffff : 0x0000000;
96
        crc <<= 1;
97
        return crc ^ ((d ^ crc_32) & DBG_CRC_POLY);
98
}
99
 
100
// Speedy bit reversing algorithms for base2 lengths
101
// Thanks to: http://aggregate.org/MAGIC/#Bit%20Reversal
102
uint32_t bit_reverse_swar_2(uint32_t x)
103
{
104
  x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
105
  return x;
106
}
107
uint32_t bit_reverse_swar_4(uint32_t x)
108
{
109
  x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
110
  x=(((x&0xcccccccc)>>2)|((x&0x33333333)<<2));
111
  return x;
112
}
113
uint32_t bit_reverse_swar_8(uint32_t x)
114
{
115
  x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
116
  x=(((x&0xcccccccc)>>2)|((x&0x33333333)<<2));
117
  x=(((x&0xf0f0f0f0)>>4)|((x&0x0f0f0f0f)<<4));
118
  return x;
119
}
120
uint32_t bit_reverse_swar_16(uint32_t x)
121
{
122
  x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
123
  x=(((x&0xcccccccc)>>2)|((x&0x33333333)<<2));
124
  x=(((x&0xf0f0f0f0)>>4)|((x&0x0f0f0f0f)<<4));
125
  x=(((x&0xff00ff00)>>8)|((x&0x00ff00ff)<<8));
126
  return x;
127
}
128
uint32_t bit_reverse_swar_32(uint32_t x)
129
{
130
  x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
131
  x=(((x&0xcccccccc)>>2)|((x&0x33333333)<<2));
132
  x=(((x&0xf0f0f0f0)>>4)|((x&0x0f0f0f0f)<<4));
133
  x=(((x&0xff00ff00)>>8)|((x&0x00ff00ff)<<8));
134
  x=(((x&0xffff0000)>>16)|((x&0x0000ffff)<<16)); // We could be on 64-bit arch!
135
  return x;
136
}
137
 
138
uint32_t bit_reverse_data(uint32_t data, uint32_t length){
139
  if (length == 2) return bit_reverse_swar_2(data);
140
  if (length == 4) return bit_reverse_swar_4(data);
141
  if (length == 8) return bit_reverse_swar_8(data);
142
  if (length == 16) return bit_reverse_swar_16(data);
143
  if (length == 32) return bit_reverse_swar_32(data);
144
  // Long and laborious way - hopefully never gets called anymore!
145
  uint32_t i,reverse=0;
146
  for (i=0;i<length;i++) reverse |= (((data>>i)&1)<<(length-1-i));
147
  return reverse;
148
}
149
// Constants that are used a lot, and were 5 bits, so might as well precalculate them
150
// These are from or_debug_proxy.h, so if the original values change these
151
// should be recalculated!!
152
const uint8_t DI_GO_5BITREVERSED = 0x00;
153
const uint8_t DI_READ_CMD_5BITREVERSED = 0x10;
154
const uint8_t DI_WRITE_CMD_5BITREVERSED = 0x08;
155
const uint8_t DI_READ_CTRL_5BITREVERSED = 0x18;
156
const uint8_t DI_WRITE_CTRL_5BITREVERSED = 0x04;
157
 
158
 
159
void usb_dbg_test() {
160
 
161
  uint32_t npc, ppc, r1;
162
  unsigned char stalled;
163
 
164
 
165
 
166
  printf("Stalling or1k\n");
167
  err = dbg_cpu0_write_ctrl(0, 0x01);      // stall or1k
168
 
169
  err = dbg_cpu0_read_ctrl(0, &stalled);
170
  if (!(stalled & 0x1)) {
171
    printf("or1k should be stalled\n");   // check stall or1k
172
    exit(1);
173
  }
174
 
175
  /* Clear Debug Reason Register (DRR) 0x3015 */
176
  err = dbg_cpu0_write((6 << 11) + 21, 0);
177
  err = dbg_cpu0_read((0 << 11) + 16, &npc);  /* Read NPC */
178
  err = dbg_cpu0_read((0 << 11) + 18, &ppc);  /* Read PPC */
179
  err = dbg_cpu0_read(0x401, &r1);  /* Read R1 */
180
 
181
  if (err)
182
    {
183
      printf("Jtag error %d occured; exiting.", err);
184
      FT2232_USB_JTAG_CloseDevice();
185
      exit(1);
186
    }
187
  printf("Read      npc = %.8x ppc = %.8x r1 = %.8x\n", npc, ppc, r1);
188
 
189
  return;
190
}
191
 
192
/*
193
void ensure_or1k_stalled();
194
 
195
// Function to check if the processor is stalled, if not, stall it.
196
// this is useful in the event that GDB thinks the processor is stalled, but has, in fact
197
// been hard reset on the board and is running.
198
void ensure_or1k_stalled()
199
{
200
  unsigned char stalled;
201
  dbg_cpu0_read_ctrl(0, &stalled);
202
  if ((stalled & 0x1) != 0x1)
203
    {
204
      if (DEBUG_CMDS)
205
        printf("Processor not stalled, like we thought\n");
206
 
207
      // Set the TAP controller to its OR1k chain
208
      usb_set_tap_ir(JI_DEBUG);
209
      current_chain = -1;
210
 
211
      // Processor isn't stalled, contrary to what we though, so stall it
212
      printf("Stalling or1k\n");
213
      dbg_cpu0_write_ctrl(0, 0x01);      // stall or1k
214
 
215
    }
216
}
217
*/
218
/*---------------------------------------------------------------------------*/
219
/*!Write up to 32-bits to the JTAG bus via the USB device
220
 
221
Write up to 32-bits to the JTAG bus.
222
 
223
@param[in] stream  This should store the data to be written to the bus
224
@param[in] num_bits  Number of bits to write on the JTAG bus
225
@param[in] dwTapControllerState  State to leave the JTAG TAP in when done    */
226
/*---------------------------------------------------------------------------*/
227
void usb_write_stream (uint32_t stream, uint32_t num_bits, DWORD dwTapControllerState){
228
  FTC_STATUS Status = FTC_SUCCESS;
229
  uint32_t i,num_bytes;
230
  WriteDataByteBuffer WriteDataBuffer;
231
 
232
  if ((num_bits/8)>0)num_bytes=(num_bits/8);
233
  else num_bytes=1;
234
  // First clear the buffer for the amount of data we're shifting in
235
  // Bytewise copy the stream into WriteDataBuffer, LSB first
236
 
237
  // This means if we want to send things MSb first, we need to pass them
238
  // to this function wrapped with a call to bit_reverse_data(data,length)
239
 
240
  for (i=0; i<(num_bytes)+1;i++)
241
    WriteDataBuffer[i] = ((stream >>(8*i)) & (0xff));
242
 
243
  // Now generate the CRC for what we're sending
244
  // data should be presented MSb first (at bit0)
245
  for (i=0;i<num_bits;i++)
246
    crc_w = crc_calc(crc_w,((stream>>i)&1));
247
 
248
 
249
  if (DEBUG_USB_DRVR_FUNCS)
250
    {
251
      printf("\nusb_write_stream: num_bytes=%d, WriteDataBuffer contents for %d bits:",num_bytes,num_bits);
252
      for (i=0;i<num_bytes+1;i++)
253
        printf("%x",WriteDataBuffer[num_bytes-i]);
254
      printf("\n");
255
    }
256
 
257
  //Status = pFT2232cMpsseJtag->JTAG_WriteDataToExternalDevice(ftHandle, false, num_bits, 
258
  //                   &WriteDataBuffer, num_bytes, dwTapControllerState);
259
  // Platform independant function call
260
  Status = FT2232_USB_JTAG_WriteDataToExternalDevice(false, num_bits,
261
                       &WriteDataBuffer, num_bytes, dwTapControllerState);
262
 
263
  if (Status != FTC_SUCCESS)
264
    printf("Write to USB device failed: status code: %ld\n",Status);
265
 
266
}
267
 
268
/*---------------------------------------------------------------------------*/
269
/*!Read up to 32-bits off the JTAG bus via the USB device
270
 
271
The return value of this should remain uint32_t as we're returning all the
272
data in a signal variable. We never need more than 32-bits in a single read
273
when using this function.
274
 
275
@param[in] num_bits  Number of bits to read off from the USB
276
@param[in] dwTapControllerState  State to leave the JTAG TAP in when done
277
@return: The data read from the USB device                                   */
278
/*---------------------------------------------------------------------------*/
279
uint32_t usb_read_stream(uint32_t num_bits, DWORD dwTapControllerState){
280
  ReadDataByteBuffer ReadDataBuffer;
281
  FTC_STATUS Status = FTC_SUCCESS;
282
  uint32_t returnVal =0;
283
  uint32_t i;
284
 
285
  // Platform independant driver call
286
  Status = FT2232_USB_JTAG_ReadDataFromExternalDevice(false, num_bits, &ReadDataBuffer, &dwNumBytesReturned, dwTapControllerState);
287
 
288
  if (DEBUG_USB_DRVR_FUNCS)
289
    printf("usb_read_stream: returned Status: 0x%lx from reading %u bits, \n",
290
           Status,num_bits);
291
 
292
  if (Status == FTC_SUCCESS){
293
 
294
    for(i=0;i<num_bits;i++){
295
      returnVal |= ((ReadDataBuffer[i/8]>>(i%8))&0x1)<<(num_bits-1-i);
296
    }
297
  }
298
  // Generate the CRC for read
299
  for (i=0;i<num_bits;i++)
300
      crc_r = crc_calc(crc_r, ((returnVal>>(num_bits-1-i))&1));
301
 
302
  return returnVal;
303
}
304
 
305
/* Sets TAP instruction register */
306
void usb_set_tap_ir(uint32_t ir) {
307
 
308
  WriteDataByteBuffer WriteDataBuffer;
309
  FTC_STATUS Status = FTC_SUCCESS;
310
 
311
  WriteDataBuffer[0] = ir;
312
 
313
  // Using global ftHandle, writing to TAP instruction register 4 bits, 
314
  //Status = pFT2232cMpsseJtag->JTAG_WriteDataToExternalDevice(ftHandle, true, 
315
  //JI_SIZE, &WriteDataBuffer, 1, RUN_TEST_IDLE_STATE);
316
  // Platform independant driver call
317
 
318
  Status = FT2232_USB_JTAG_WriteDataToExternalDevice(true, JI_SIZE, &WriteDataBuffer, 1, RUN_TEST_IDLE_STATE);
319
 
320
  if (DEBUG_USB_DRVR_FUNCS)
321
    printf("USB JTAG write of %x to instruction register returned Status: 0x%lx\n",
322
           ir, Status);
323
  current_chain = -1;
324
}
325
 
326
 
327
/* Resets JTAG, and sets DEBUG scan chain */
328
int usb_dbg_reset() {
329
 
330
  // uint32_t err;
331
  uint32_t id;
332
  uint32_t reinit_count=0;
333
 retry_jtag_init:
334
  if (init_usb_jtag() > 0)
335
    return DBG_ERR_CRC;
336
 
337
  // Set ID code instruction in IR
338
  usb_set_tap_ir(JI_IDCODE);
339
 
340
  // Now read out the IDCODE for the device
341
  id = usb_read_stream(32, RUN_TEST_IDLE_STATE);
342
 
343
  // if read ID was rubbish retry init - this is probably NOT the best way to do this...
344
  if ((id == 0xffffffff) | (id == 0x00000002) | (id == 0x00000000)) {
345
    //pFT2232cMpsseJtag->JTAG_CloseDevice(gFtHandle);
346
    // Platform independant driver call
347
    FT2232_USB_JTAG_CloseDevice();
348
    if (reinit_count++ > 4){
349
      printf("JTAG TAP ID read error. Unable to detect TAP controller. \nPlease ensure debugger is connected to target correctly.\n");
350
      exit(1);
351
    }
352
    goto retry_jtag_init;
353
  }
354
 
355
  printf("JTAG ID = %08x\n", id & 0xffffffff);
356
 
357
  /* select debug scan chain and stay in it forever */
358
  usb_set_tap_ir(JI_DEBUG);
359
  current_chain = -1;
360
  return DBG_ERR_OK;
361
}
362
 
363
/* counts retries and returns zero if we should abort */
364
static int retry_no = 0;
365
int retry_do() {
366
 
367
  unsigned char stalled;
368
 
369
  //printf("RETRY\n");
370
  if (retry_no == 0)
371
    printf("Communication error. Retrying\n");
372
 
373
  // Odds are if we're having a communication problem, we should 
374
  // just reconnect to the processor. It's probably just been reset.
375
  /*
376
    FT2232_USB_JTAG_CloseDevice();// Close the device
377
 
378
    if (init_usb_jtag() > 0)
379
    {
380
    if (retry_no >= NUM_HARD_RETRIES)
381
    return 1;
382
    else
383
    return 0;
384
    }
385
  */
386
    usb_set_tap_ir(JI_DEBUG);
387
 
388
    current_chain = -1;
389
 
390
    if (retry_no == 1)
391
      sleep(1);
392
 
393
    else if ( retry_no < NUM_SOFT_RETRIES)
394
      return 1; // Just attempt again    
395
    if ((retry_no >= NUM_SOFT_RETRIES) && (retry_no < NUM_SOFT_RETRIES + NUM_HARD_RETRIES) )
396
      {
397
        // Attempt a stall of the processor and then we'll try again
398
        //usb_dbg_test();
399
        printf("Stalling or1k\n");
400
        err = dbg_cpu0_write_ctrl(0, 0x01);      // stall or1k
401
 
402
        err = dbg_cpu0_read_ctrl(0, &stalled);
403
        if (!(stalled & 0x1)) {
404
          printf("or1k should be stalled\n");   // check stall or1k
405
          FT2232_USB_JTAG_CloseDevice();// Close the device
406
          exit(1);
407
        }
408
 
409
        retry_no++;
410
        return 1;
411
      }
412
    else // Unable to get the processor going again, return 0
413
      return 0;
414
 
415
}
416
 
417
/* resets retry counter */
418
void retry_ok() {
419
        retry_no = 0;
420
}
421
 
422
/* Sets scan chain.  */
423
int usb_dbg_set_chain(int chain) {
424
  uint32_t status, crc_generated, crc_read;
425
  dbg_chain = chain;
426
 
427
 try_again:
428
  if (current_chain == chain) return DBG_ERR_OK;
429
  current_chain = -1;
430
  if (DEBUG_CMDS) printf("\n");
431
  if (DEBUG_CMDS) printf("set_chain %i\n", chain);
432
 
433
  crc_w = 0xffffffff; // reset crc write variable
434
 
435
  // CRC generated in usb_read/write_stream functions
436
 
437
  usb_write_stream((((bit_reverse_data(chain,DBGCHAIN_SIZE) & 0xf) << 1) | 1),
438
                   DBGCHAIN_SIZE+1 , PAUSE_TEST_DATA_REGISTER_STATE);
439
 
440
  usb_write_stream(bit_reverse_data(crc_w, DBG_CRC_SIZE),DBG_CRC_SIZE,
441
                   PAUSE_TEST_DATA_REGISTER_STATE);
442
 
443
  crc_r = 0xffffffff; // reset the crc_r variable
444
 
445
  status = (usb_read_stream(DC_STATUS_SIZE,
446
                            PAUSE_TEST_DATA_REGISTER_STATE) & 0xf);
447
 
448
  crc_generated = crc_r;
449
 
450
  crc_read = usb_read_stream(DBG_CRC_SIZE, RUN_TEST_IDLE_STATE);
451
  //printf("%x %x %x\n",status, crc_generated, crc_read);
452
 
453
  /* CRCs must match, otherwise retry */
454
  if (crc_read != crc_generated) {
455
     if (retry_do()) goto try_again;
456
    else return DBG_ERR_CRC;
457
  }
458
  /* we should read expected status value, otherwise retry */
459
  if (status != 0) {
460
    if (retry_do()) goto try_again;
461
    else return status;
462
  }
463
 
464
  /* reset retry counter */
465
  retry_ok();
466
 
467
  current_chain = chain;
468
  return DBG_ERR_OK;
469
}
470
 
471
 
472
 
473
/* sends out a command with 32bit address and 16bit length, if len >= 0 */
474
int usb_dbg_command(uint32_t type, uint32_t adr, uint32_t len) {
475
  uint32_t i,status, crc_generated, crc_read;
476
 
477
   // JTAG driver things
478
  FTC_STATUS Status = FTC_SUCCESS;
479
  WriteDataByteBuffer WriteDataBuffer;
480
  ReadDataByteBuffer ReadDataBuffer;
481
 
482
 try_again:
483
  usb_dbg_set_chain(dbg_chain);
484
  if (DEBUG_CMDS) printf("\n");
485
  if (DEBUG_CMDS) printf("comm %d %d %8x \n", type,len,adr);
486
 
487
  //Calculate CRCs first
488
  crc_w = 0xffffffff;
489
 
490
  for (i=0;i<DBGCHAIN_SIZE+1;i++)
491
    crc_w = crc_calc(crc_w, (DI_WRITE_CMD_5BITREVERSED >> i)  & 1);
492
    //crc_w = crc_calc(crc_w,
493
    //       ((bit_reverse_data((DI_WRITE_CMD & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
494
 
495
  for (i=0;i<4;i++)
496
    crc_w = crc_calc(crc_w,((bit_reverse_data(type,4))>>i)&1);
497
 
498
  for (i=0;i<32;i++)
499
    crc_w = crc_calc(crc_w,((bit_reverse_data(adr,32))>>i)&1);
500
 
501
  assert(len > 0);
502
  for (i=0;i<16;i++)
503
    crc_w = crc_calc(crc_w,((bit_reverse_data(len-1,16))>>i)&1);
504
 
505
 
506
 
507
  // Now pack the write data buffer
508
  // 1-bit 0, 4-bits cmd, 4-bit type, 32-bit adr, 32-bit CRC
509
  // [0]
510
  //bits 0-4
511
  WriteDataBuffer[0]=(DI_WRITE_CMD_5BITREVERSED);
512
  //bits 5-7 
513
  WriteDataBuffer[0] |= ((bit_reverse_data(type,4)&0x7)<<5);
514
  // [1]
515
  // bit 0 - last bit of type
516
  WriteDataBuffer[1] = ((bit_reverse_data(type,4)&0x08)>>3);
517
  //bits 1-7 - first 7 bits of adr
518
  WriteDataBuffer[1] |= ((bit_reverse_data(adr,32)&0x07f)<<1);
519
  //[2-4] 24 bits of adr
520
  for(i=0;i<3;i++)
521
    WriteDataBuffer[2+i] = (bit_reverse_data(adr,32)>>(7+(i*8)))&0xff;
522
  // [5] last bit of adr in bit 0, first 7 bits of len-1 follow
523
  WriteDataBuffer[5] = (bit_reverse_data(adr,32)>>31)&1;
524
  WriteDataBuffer[5] |= (bit_reverse_data(len-1,16)&0x7f)<<1;
525
  // [6] bits 7-14 of (len-1)
526
  WriteDataBuffer[6] = (bit_reverse_data(len-1,16)>>7)&0xff;
527
  // [7] - last bit of len-1 and first 7 bits of the CRC
528
  WriteDataBuffer[7] = (bit_reverse_data(len-1,16)>>15)&1;
529
  //Reverse the CRC first
530
  crc_w = bit_reverse_data(crc_w, DBG_CRC_SIZE);
531
  WriteDataBuffer[7] |= (crc_w&0x7f)<<1;
532
  //[8-10] next 24 bits (7-30) of crc_w
533
  WriteDataBuffer[8] = (crc_w>>7)&0xff;
534
  WriteDataBuffer[9] = (crc_w>>15)&0xff;
535
  WriteDataBuffer[10] = (crc_w>>23)&0xff;
536
  //[11] final bit of crc_w
537
  WriteDataBuffer[11] = (crc_w>>31)&1;
538
 
539
  //  Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
540
  // Platform independant driver call
541
  Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
542
 
543
  if (Status != FTC_SUCCESS)
544
    printf("USB write fail - code %ld\b",Status);
545
 
546
  // Now look through the read data
547
 
548
  // From bit1 of ReadDataBuffer[11] we should have our 4-bit status
549
  status = (ReadDataBuffer[11] >> 1) & 0xf;
550
 
551
  // Now extract the received CRC
552
  crc_read = 0;
553
  //first 3 bits (0-2)
554
  crc_read |= (ReadDataBuffer[11] >> 5) & 0x7;
555
  // middle 3 bytes (bits 3 to 26)
556
  for (i=0;i<3;i++)
557
    crc_read |= ((ReadDataBuffer[12+i]&0xff) << ((i*8)+3));
558
  // last 5 bits from ReadDataBuffer[15]
559
  crc_read |= (ReadDataBuffer[15]&0x1f)<<27;
560
 
561
  // Now calculate CRC on status
562
  crc_r = 0xffffffff;
563
  for(i=0;i<DC_STATUS_SIZE;i++)
564
    crc_r = crc_calc(crc_r, (status>>i)&1);
565
 
566
  crc_generated = crc_r;
567
  // Now bit reverse status and crc_read as we unpacked them
568
  // with the MSb going to the LSb
569
  status = bit_reverse_data(status, DC_STATUS_SIZE);
570
  crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
571
 
572
  //printf("%x %x %x\n", status, crc_read, crc_generated);
573
  /* CRCs must match, otherwise retry */
574
  if (crc_read != crc_generated) {
575
    //exit(1);//remove later
576
    if (retry_do()) goto try_again;
577
    else return DBG_ERR_CRC;
578
  }
579
  /* we should read expected status value, otherwise retry */
580
  if (status != 0) {
581
    //exit(1);//remove later
582
    if (retry_do()) goto try_again;
583
    else return status;
584
  }
585
  /* reset retry counter */
586
  retry_ok();
587
 
588
  return DBG_ERR_OK;
589
}
590
 
591
 
592
/* writes a ctrl reg */
593
int usb_dbg_ctrl(uint32_t reset, uint32_t stall) {
594
  uint32_t i,status, crc_generated, crc_read;
595
 
596
  // JTAG driver things
597
  FTC_STATUS Status = FTC_SUCCESS;
598
  WriteDataByteBuffer WriteDataBuffer;
599
  ReadDataByteBuffer ReadDataBuffer;
600
 
601
try_again:
602
  usb_dbg_set_chain(dbg_chain);
603
        if (DEBUG_CMDS) printf("\n");
604
        if (DEBUG_CMDS) printf("ctrl\n");
605
        if (DEBUG_CMDS) printf("reset %x stall %x\n", reset, stall);
606
 
607
        crc_w = 0xffffffff;
608
        // Try packing everyhing we want to send into one write buffer
609
        //Calculate CRCs first
610
        for (i=0;i<DBGCHAIN_SIZE+1;i++)
611
          crc_w = crc_calc(crc_w, (DI_WRITE_CTRL_5BITREVERSED>>i)&1);
612
          //crc_w = crc_calc(crc_w, 
613
          //       ((bit_reverse_data((DI_WRITE_CTRL & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
614
        crc_w = crc_calc(crc_w,(reset&1));
615
        crc_w = crc_calc(crc_w,(stall&1));
616
        for (i=0;i<50;i++)
617
          crc_w = crc_calc(crc_w,0);
618
 
619
 
620
 
621
        // Now pack the write data buffer
622
        // 1-bit 0, 4-bits cmd, 52-bits CPU control register data (only first 2 matter)
623
        //bits 0-4
624
        WriteDataBuffer[0]=(DI_WRITE_CTRL_5BITREVERSED);
625
        // bit 5
626
        WriteDataBuffer[0] |= (reset)<<(DBGCHAIN_SIZE+1);
627
        // bit 6
628
        WriteDataBuffer[0] |= (stall)<<(DBGCHAIN_SIZE+2);
629
        // Clear the next 48 bits
630
        for (i=1; i<16;i++)   //actually clear more than just the next 50 bits
631
          WriteDataBuffer[i] = 0;
632
 
633
        //Reverse the CRC first
634
        crc_w = bit_reverse_data(crc_w, DBG_CRC_SIZE);
635
        //Now load in the CRC, but take note of fact 
636
        // that bit 0 of buffer[7] is last 0 from cmd register data
637
        // fill up from WriteDataBuffer[7-11]
638
        for (i=0;i<4;i++)
639
          WriteDataBuffer[7+i] = ((crc_w<<1)>>(i*8))&0xff;
640
        //Final bit, shift in and make sure is the only thing int he buffer
641
        WriteDataBuffer[11]=0|((crc_w>>31)&1);
642
 
643
 
644
        //Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
645
        // Platform independant driver call
646
        Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
647
 
648
 
649
        if (Status != FTC_SUCCESS)
650
          printf("USB write fail - code %ld\b",Status);
651
 
652
        // Now look through the read data
653
 
654
        // From bit1 of ReadDataBuffer[11] we should have our 4-bit status
655
        status = (ReadDataBuffer[11] >> 1) & 0xf;
656
 
657
        // Now extract the received CRC
658
        crc_read = 0;
659
        //first 3 bits (0-2)
660
        crc_read |= (ReadDataBuffer[11] >> 5) & 0x7;
661
        // middle 3 bytes (bits 3 to 26)
662
        for (i=0;i<3;i++)
663
          crc_read |= ((ReadDataBuffer[12+i]&0xff) << ((i*8)+3));
664
        // last 5 bits from ReadDataBuffer[15]
665
        crc_read |= (ReadDataBuffer[15]&0x1f)<<27;
666
 
667
 
668
        // Now calculate CRC on status and crc_read
669
        crc_r = 0xffffffff;
670
        for(i=0;i<DC_STATUS_SIZE;i++)
671
          crc_r = crc_calc(crc_r, (status>>i)&1);
672
 
673
        crc_generated = crc_r;
674
        // Now bit reverse status and crc_read as we unpacked them
675
        // with the MSb going to the LSb
676
        status = bit_reverse_data(status, DC_STATUS_SIZE);
677
        crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
678
 
679
        /* CRCs must match, otherwise retry */
680
        //printf("%x %x %x\n", status, crc_read, crc_generated);
681
        if (crc_read != crc_generated) {
682
          //exit(1);//Remove later!!
683
          if (retry_do()) goto try_again;
684
          else return DBG_ERR_CRC;
685
        }
686
        /* we should read expected status value, otherwise retry */
687
        if (status != 0) {
688
          //exit(1);//Remove later!!
689
          if (retry_do()) goto try_again;
690
          else return status;
691
        }
692
 
693
        /* reset retry counter */
694
        retry_ok();
695
        return DBG_ERR_OK;
696
}
697
 
698
 
699
/* reads control register */
700
int usb_dbg_ctrl_read(uint32_t *reset, uint32_t *stall) {
701
  uint32_t i, status, crc_generated, crc_read;
702
 
703
  // JTAG driver things
704
  FTC_STATUS Status = FTC_SUCCESS;
705
  WriteDataByteBuffer WriteDataBuffer;
706
  ReadDataByteBuffer ReadDataBuffer;
707
 
708
 
709
 try_again:
710
  usb_dbg_set_chain(dbg_chain);
711
  if (DEBUG_CMDS) printf("\n");
712
  if (DEBUG_CMDS) printf("ctrl_read\n");
713
 
714
  crc_w = 0xffffffff;
715
  // Try packing everyhing we want to send into one write buffer
716
  //Calculate CRCs first
717
  for (i=0;i<DBGCHAIN_SIZE+1;i++)
718
    crc_w = crc_calc(crc_w, (DI_READ_CTRL_5BITREVERSED>>i)&1);
719
    //crc_w = crc_calc(crc_w, 
720
    //((bit_reverse_data((DI_READ_CTRL & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
721
 
722
 
723
  // Now pack the write data buffer
724
  // 1-bit 0, 4-bits cmd, 32-bit CRC
725
  //bits 0-4
726
  WriteDataBuffer[0]=(DI_READ_CTRL_5BITREVERSED);
727
  // Clear the next 48 bits
728
  for (i=1; i<16;i++)   //actually clear more than just the next 50 bits
729
    WriteDataBuffer[i] = 0;
730
 
731
  //Reverse the CRC first
732
  crc_w = bit_reverse_data(crc_w, DBG_CRC_SIZE);
733
  //Now load in the CRC
734
  //First 3 bits go in last 3 bits of buffer[0]
735
  WriteDataBuffer[0] |= (crc_w & 0x7)<<5;
736
  // The rest of crc_w goes in buffer[1-4]
737
  for (i=0;i<3;i++)
738
    WriteDataBuffer[1+i] = ((crc_w>>3)>>(i*8))&0xff;
739
  //Final bit of write buffer with CRC
740
  WriteDataBuffer[4] = ((crc_w>>3)>>(24))&0x1f;
741
 
742
 
743
 
744
  //Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,5+32+52+4+32, &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
745
  // Platform independant driver call
746
  Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,5+32+52+4+32, &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
747
  if (Status != FTC_SUCCESS)
748
    printf("USB read fail - code %ld\b",Status);
749
 
750
  // Now look through the read data
751
  //0 - 1+4-bit status, 3-bits CRC
752
  //1,2,3 - CRC
753
  //4 - first 5 bits CRC, last 3, control reg bits (first 3)
754
  //5,6,7,8,9,10 - control reg data (48 bits)
755
  //11 - bit0 - control reg data, bit 1-4 status bits, bits 5-7 CRC
756
  //12, 13 14 - CRC
757
  // 15 bits 0-4 CRC
758
  // Starting from bit1 of ReadDataBuffer[11] we should have our 4-bit status
759
  status = (ReadDataBuffer[11] >> 1) & 0xf;
760
 
761
  //reset bit should be in ReadDataBuffer[4] as bit 5
762
  *reset = (ReadDataBuffer[4] >> 5) & 1;
763
  //stalled bit should be in ReadDataBuffer[4] as bit 6
764
  *stall = (ReadDataBuffer[4] >> 6) & 1;
765
  // Now extract the received CRC
766
  crc_read = 0;
767
  //first 3 bits (0-2) of CRC are in bits 5-7 of ReadDataBuffer[11]
768
  crc_read |= (ReadDataBuffer[11] >> 5) & 0x7;
769
  // middle 3 bytes (bits 3 to 26)
770
  for (i=0;i<3;i++)
771
    crc_read |= ((ReadDataBuffer[12+i]&0xff) << ((i*8)+3));
772
  // last 5 bits from ReadDataBuffer[15]
773
  crc_read |= (ReadDataBuffer[15]&0x1f)<<27;
774
 
775
  if (DEBUG_CMDS) printf("reset bit %x stalled bit %x:\n",
776
         ((ReadDataBuffer[4] >> 5) & 1), ((ReadDataBuffer[4] >> 6) & 1));
777
 
778
  // Now calculate CRC on status and crc_read
779
  crc_r = 0xffffffff;
780
 
781
  crc_r = crc_calc(crc_r, ((ReadDataBuffer[4] >> 5) & 1));
782
  crc_r = crc_calc(crc_r, ((ReadDataBuffer[4] >> 6) & 1));
783
  for(i=0;i<50;i++)
784
    crc_r = crc_calc(crc_r,0);
785
  for(i=0;i<DC_STATUS_SIZE;i++)
786
    crc_r = crc_calc(crc_r, (status>>i)&1);
787
 
788
  crc_generated = crc_r;
789
  // Now bit reverse status and crc_read as we unpacked them
790
  // with the MSb going to the LSb
791
  status = bit_reverse_data(status, DC_STATUS_SIZE);
792
  crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
793
 
794
  /* CRCs must match, otherwise retry */
795
  //printf("%x %x %x\n", status, crc_generated, crc_read);
796
  if (crc_read != crc_generated) {
797
    if (retry_do()) goto try_again;
798
    else return DBG_ERR_CRC;
799
  }
800
  /* we should read expected status value, otherwise retry */
801
  if (status != 0) {
802
     if (retry_do()) goto try_again;
803
    else return status;
804
  }
805
  /* reset retry counter */
806
  retry_ok();
807
  return DBG_ERR_OK;
808
}
809
 
810
 
811
/* issues a burst read/write */
812
int usb_dbg_go(unsigned char *data, uint16_t len, uint32_t read) {
813
        uint32_t status, crc_generated, crc_read;
814
        int i,j;
815
        uint8_t data_byte;
816
 
817
        // JTAG driver things
818
        FTC_STATUS Status = FTC_SUCCESS;
819
        WriteDataByteBuffer WriteDataBuffer;
820
        ReadDataByteBuffer ReadDataBuffer;
821
 
822
 try_again:
823
        usb_dbg_set_chain(dbg_chain);
824
        if (DEBUG_CMDS) printf("\n");
825
        if (DEBUG_CMDS) printf("go len is %d, read is %d\n", len, read);
826
 
827
        crc_w = 0xffffffff;
828
        // Try packing everyhing we want to send into one write buffer
829
        //Calculate CRCs first
830
        for (i=0;i<DBGCHAIN_SIZE+1;i++)
831
          crc_w = crc_calc(crc_w, (DI_GO_5BITREVERSED>>i)&1);
832
          //crc_w = crc_calc(crc_w, 
833
          //       ((bit_reverse_data((DI_GO & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
834
        // Set first 5 bits of WriteDataBuffer up to 
835
        // be the GO command and 0 first bit
836
        WriteDataBuffer[0]=DI_GO_5BITREVERSED;
837
 
838
        if (read)
839
          {
840
            // Do GO command for a read
841
            // 0 then 4-bit go command, then 32-bit CRC for the last 5 bits
842
            // Then read (len+1)*8 + 4-bit status + 32-bit CRC
843
 
844
 
845
            // Reverse crc_w
846
            crc_w = bit_reverse_data(crc_w,DBG_CRC_SIZE);
847
 
848
            // Now pack in the 32-bit CRC as we're not 
849
            // writing anything else after it
850
             //First 3 bits of each byte go in last 3 bits of buffer[i], 
851
            // next 5 go in buffer[i+1]
852
            for(i=0;i<4;i++){
853
              WriteDataBuffer[i] |= ((crc_w>>(i*8))&0x07)<<5;
854
              WriteDataBuffer[i+1] = ((crc_w>>(i*8))>>3)&0x1f;
855
            }
856
 
857
            // Should have data up to WriteDataBuffer[4] bit 4
858
            // Read data should start at ReadDataBuffer[4] bit 5
859
 
860
            //Clear the rest of the write buffer
861
            for(i=5;i<(10+len);i++)
862
              WriteDataBuffer[i]=0;
863
          }
864
        if (!read){
865
          // If we're writing we put in the 5 command bits, (len+1)*8 data bits,
866
          // and then the 32-bit CRC do first 3 bits, then next 5 bits in 
867
          // each of the for loops iterations
868
          for(i=0;i<len;i++){
869
            // Todo - load this buffer more efficiently!
870
            data_byte = bit_reverse_data(data[i], 8);
871
            WriteDataBuffer[i] |= ((data_byte&0x07)<<5);
872
            WriteDataBuffer[i+1] = ((data_byte>>3)&0x1f);
873
          }
874
 
875
          // Now update the CRC for the data we just wrote
876
          for (i=0;i<len;i++){
877
            for(j=0;j<8;j++)
878
              crc_w = crc_calc(crc_w, ((bit_reverse_data(data[i],8)>>j)&1));
879
          }
880
 
881
          // Reverse crc_w
882
          crc_w = bit_reverse_data(crc_w,DBG_CRC_SIZE);
883
 
884
          // If we have len=4 for example, we will write to 
885
          // WriteDataBuffer[4]'s first 5 bits
886
 
887
          // So now load in the 32-bit CRC from there
888
          for(i=0;i<4;i++){
889
            WriteDataBuffer[len+i] |= ((crc_w>>(i*8))&0x07)<<5;
890
            WriteDataBuffer[len+i+1] = ((crc_w>>(i*8))>>3)&0x1f;
891
          }
892
          // Should have data up to WriteDataBuffer[4+len] bit 4
893
          // Read data should start at ReadDataBuffer[4+len] bit 5
894
 
895
        }
896
 
897
        //Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,(5+(len*8)+32+36), &WriteDataBuffer, (6+len+4), &ReadDataBuffer,&dwNumBytesReturned,RUN_TEST_IDLE_STATE);
898
        Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,(5+(len*8)+32+36), &WriteDataBuffer, (6+len+4), &ReadDataBuffer,&dwNumBytesReturned,RUN_TEST_IDLE_STATE);
899
 
900
        if (Status != FTC_SUCCESS)
901
          printf("USB write fail - code %ld\n",Status);
902
 
903
        crc_r = 0xffffffff;
904
 
905
        if (read){
906
          // Look through our data, starting from ReadDataBuffer[4] bit 5
907
          // We receive len bytes, starting at ReadDataBuffer[4] bit 5, so
908
          // unpack like so
909
          if (DEBUG_USB_DRVR_FUNCS) printf("USB read data buffer: ");
910
          for(i=0;i<len;i++){
911
            // get first 3 bits
912
            data[i] = (ReadDataBuffer[4+i]>>5)&0x07;
913
            // then next 5 from next ReadDataBuffer byte
914
            data[i] |= (ReadDataBuffer[4+i+1]&0x1f)<<3;
915
 
916
            // Now calculate the CRC for this byte
917
            for(j=0;j<8;j++)
918
              crc_r = crc_calc(crc_r, (data[i]>>j)&1);
919
 
920
            // Now bit reverse the byte as it was read in MSb first but 
921
            // written to LSb first
922
            data[i] = bit_reverse_data(data[i],8);
923
 
924
            if (DEBUG_USB_DRVR_FUNCS) printf("%2x",data[i]);
925
          }
926
          if (DEBUG_USB_DRVR_FUNCS) printf("\n");
927
 
928
          // Should be up to ReadDataBuffer[4+len] bit 5 for data
929
          status = (ReadDataBuffer[4+len]>>5)&0x07;
930
          status |= (ReadDataBuffer[4+len+1]&1)<<3;
931
          // Now get out crc_read
932
          crc_read = 0;
933
          for(i=0;i<4;i++){
934
            crc_read |= ((ReadDataBuffer[4+len+1+i]>>1)&0x7f)<<(i*8);
935
            crc_read |= ((ReadDataBuffer[4+len+1+i+1]&0x1)<<7)<<(i*8);
936
          }
937
 
938
          for(i=0;i<4;i++)
939
            crc_r = crc_calc(crc_r, (status>>i)&1);
940
        }
941
        if (!read){
942
          // Just extract our 4-bits of status and CRC
943
          status = (ReadDataBuffer[4+len]>>5)&0x07;
944
          status |= (ReadDataBuffer[4+len+1]&1)<<3;
945
 
946
          // extract crc_read from the ReadDataBuffer
947
          crc_read = 0;
948
          for(i=0;i<4;i++){
949
            crc_read |= ((ReadDataBuffer[4+len+1+i]>>1)&0x7f)<<(i*8);
950
            crc_read |= ((ReadDataBuffer[4+len+1+i+1]&1)<<7)<<(i*8);
951
          }
952
          // Calculate our own CRC from the status value
953
          for(i=0;i<4;i++)
954
            crc_r = crc_calc(crc_r, (status>>i)&1);
955
 
956
        }
957
 
958
        crc_generated = crc_r;
959
        // Now bit reverse status and crc_read as we unpacked them
960
        // with the MSb going to the LSb
961
        status = bit_reverse_data(status, DC_STATUS_SIZE);
962
        crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
963
 
964
 
965
        /* CRCs must match, otherwise retry */
966
        if (crc_read != crc_generated) {
967
          if (DEBUG_CMDS || DEBUG_USB_DRVR_FUNCS)printf("CRC mismatch: %x %x %x\n", status, crc_read, crc_generated);
968
          if (retry_do()) goto try_again;
969
          else return DBG_ERR_CRC;
970
        }
971
        /* we should read expected status value, otherwise retry */
972
        if (status != 0) {
973
          if (retry_do()) goto try_again;
974
          else return status;
975
        }
976
 
977
        retry_ok();
978
        return DBG_ERR_OK;
979
}
980
 
981
/* read a word from wishbone */
982
int usb_dbg_wb_read32(uint32_t adr, uint32_t *data) {
983
  // uint32_t err;
984
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
985
  if ((err = usb_dbg_command(0x6, adr, 4))) return err;
986
  if ((err = usb_dbg_go((unsigned char*)data, 4, 1))) return err;
987
  *data = ntohl(*data);
988
  return err;
989
}
990
 
991
/* write a word to wishbone */
992
int usb_dbg_wb_write32(uint32_t adr, uint32_t data) {
993
  // uint32_t err;
994
  data = ntohl(data);
995
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
996
  if ((err = usb_dbg_command(0x2, adr, 4))) return err;
997
  if ((err = usb_dbg_go((unsigned char*)&data, 4, 0))) return err;
998
  return DBG_ERR_OK;
999
}
1000
 
1001
/* read a block from wishbone */
1002
int usb_dbg_wb_read_block32(uint32_t adr, uint32_t *data, uint32_t len) {
1003
  uint32_t i; // err;
1004
  //printf("%08x %08x\n", adr, len);
1005
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1006
  if ((err = usb_dbg_command(0x6, adr, len))) return err;
1007
  if ((err = usb_dbg_go((unsigned char*)data, len, 1))) return err;
1008
  uint32_t * data_uint32 = (uint32_t *) data; // data[i] gives us a proper 64-bit wide word on a 64-bit arch, so cast back to network sized data when ntohl'ing
1009
  for (i = 0; i < len / 4; i ++) data_uint32[i] = ntohl(data_uint32[i]);
1010
  //printf("%08x\n", err);
1011
  return DBG_ERR_OK;
1012
}
1013
 
1014
 
1015
/* write a block to wishbone */
1016
int usb_dbg_wb_write_block32(uint32_t adr, uint32_t *data, uint32_t len) {
1017
  uint32_t i; //, err;
1018
  uint32_t * data_uint32 = (uint32_t *) data; // data[i] gives us a proper 64-bit wide word on a 64-bit arch, so cast back to network sized data when ntohl'ing
1019
  for (i = 0; i < len / 4; i ++) data_uint32[i] = ntohl(data_uint32[i]);
1020
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1021
  if ((err = usb_dbg_command(0x2, adr, len))) return err;
1022
  if ((err = usb_dbg_go((unsigned char*)data, len, 0))) return err;
1023
  return DBG_ERR_OK;
1024
}
1025
 
1026
 
1027
/* read a register from cpu */
1028
int usb_dbg_cpu0_read(uint32_t adr, uint32_t *data) {
1029
  // uint32_t err;
1030
  if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
1031
  if ((err = usb_dbg_command(0x6, adr, 4))) return err;
1032
  if ((err = usb_dbg_go((unsigned char*)data, 4, 1))) return err;
1033
  *data = ntohl(*data);
1034
  return DBG_ERR_OK;
1035
}
1036
 
1037
/* write a cpu register */
1038
int usb_dbg_cpu0_write(uint32_t adr, uint32_t data) {
1039
  // uint32_t err;
1040
  data = ntohl(data);
1041
  if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
1042
  if ((err = usb_dbg_command(0x2, adr, 4))) return err;
1043
  if ((err = usb_dbg_go((unsigned char*)&data, 4, 0))) return err;
1044
  return DBG_ERR_OK;
1045
}
1046
 
1047
/* write a cpu module register */
1048
int usb_dbg_cpu0_write_ctrl(uint32_t adr, unsigned char data) {
1049
  // no ensuring that or1k is stalled here, becuase we're call this from that function
1050
  if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
1051
  if ((err = usb_dbg_ctrl(data & 2, data &1))) return err;
1052
  return DBG_ERR_OK;
1053
}
1054
 
1055
/* read a register from cpu module */
1056
int usb_dbg_cpu0_read_ctrl(uint32_t adr, unsigned char *data) {
1057
  // no ensuring that or1k is stalled here, becuase we're call this from that function
1058
  uint32_t r, s;
1059
  if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
1060
  if ((err = usb_dbg_ctrl_read(&r, &s))) return err;
1061
  *data = (r << 1) | s;
1062
  return DBG_ERR_OK;
1063
}
1064
 
1065
/* Function to close the device handle. Is called when closing the app */
1066
void usb_close_device_handle()
1067
{
1068
  FT2232_USB_JTAG_CloseDevice();
1069
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.