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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or_debug_proxy/] [src/] [win_usb_driver_calls.c] - Blame information for rev 211

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 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                      : win_usb_driver_calls.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
// Some generically named functions for interfacing to the JTAG driver 
44
// functions, making the code calling these platform independant. The correct 
45
// driver calling file (either Cygwin or Linux driver) is included at compile 
46
// time.
47
// Also included in this file is the USB-JTAG chip initialisation function.
48
//
49
 
50
 
51
/*$$CHANGE HISTORY*/
52
/******************************************************************************/
53
/*                                                                            */
54
/*                         C H A N G E  H I S T O R Y                         */
55
/*                                                                            */
56
/******************************************************************************/
57
 
58
// Date         Version Description
59
//------------------------------------------------------------------------
60
// 081101               First revision                                  jb
61
 
62
 
63
#include <stdio.h>
64
#include <assert.h>
65
#include <ctype.h>
66
#include <string.h>
67
#include <stdlib.h>
68
#include <stdarg.h>
69
#include <sys/stat.h>
70
#include <sys/types.h>
71
#include <windows.h>
72
 
73
#include "win_FTCJTAG.h"
74
 
75
#include "win_FTCJTAG_ptrs.h"   
76
 
77
#include "usb_driver_calls.h"
78
 
79
#include "or_debug_proxy.h"
80
 
81
 
82
// Load the pointers and loading functions of the FTCJTAG driver
83
int getFTDIJTAGFunctions (){
84
 
85
  HINSTANCE hLib;
86
 
87
  /* Load the library */
88
  hLib = LoadLibrary("lib/FTCJTAG.dll");
89
 
90
  if (hLib == NULL) {
91
    printf("Loading of library file \"FTCJTAG.dll\" failed!\n");
92
    return -1;
93
  }
94
 
95
 
96
  /* get the address of the functions */
97
  jtagGetNumDevices = (jtagGetNumDevicesPtr) GetProcAddress(hLib, "JTAG_GetNumDevices");
98
 
99
  if (jtagGetNumDevices == NULL) {
100
    printf("GetProcAddress for JTAG_GetNumDevices Failed.\n");
101
    return -1;
102
  }
103
 
104
  jtagInitDevice = (jtagInitDevicePtr) GetProcAddress(hLib, "JTAG_InitDevice");
105
  if (jtagInitDevice == NULL) {
106
    printf("GetProcAddress for JTAG_InitDevice  Failed.\n");
107
    return -1;
108
  }
109
 
110
  jtagOpen = (jtagOpenPtr) GetProcAddress(hLib, "JTAG_Open");
111
  if (jtagOpen == NULL) {
112
    printf("GetProcAddress for JTAG_Open  Failed.\n");
113
    return -1;
114
  }
115
 
116
  jtagOpenEx = (jtagOpenExPtr) GetProcAddress(hLib, "JTAG_OpenEx");
117
  if (jtagOpenEx == NULL) {
118
    printf("GetProcAddress for JTAG_OpenEx  Failed.\n");
119
    return -1;
120
  }
121
 
122
  jtagGetDeviceNameLocID = (jtagGetDeviceNameLocIDPtr) GetProcAddress(hLib, "JTAG_GetDeviceNameLocID");
123
  if (jtagGetDeviceNameLocID == NULL) {
124
    printf("GetProcAddress for  Failed JTAG_GetDeviceNameLocID.\n");
125
    return -1;
126
  }
127
 
128
  jtagClose = (jtagClosePtr) GetProcAddress(hLib, "JTAG_Close");
129
  if (jtagClose == NULL) {
130
    printf("GetProcAddress for JTAG_Close  Failed.\n");
131
    return -1;
132
  }
133
 
134
  jtagGetClock = (jtagGetClockPtr) GetProcAddress(hLib, "JTAG_GetClock");
135
  if (jtagGetClock == NULL) {
136
    printf("GetProcAddress for JTAG_GetClock  Failed.\n");
137
    return -1;
138
  }
139
 
140
  jtagSetClock = (jtagSetClockPtr) GetProcAddress(hLib, "JTAG_SetClock");
141
  if (jtagSetClock == NULL) {
142
    printf("GetProcAddress for JTAG_SetClock  Failed.\n");
143
    return -1;
144
  }
145
 
146
  jtagSetLoopback = (jtagSetLoopbackPtr) GetProcAddress(hLib, "JTAG_SetLoopback");
147
  if (jtagSetLoopback == NULL) {
148
    printf("GetProcAddress for JTAG_SetLoopback  Failed.\n");
149
    return -1;
150
  }
151
 
152
  jtagSetGPIOs = (jtagSetGPIOsPtr) GetProcAddress(hLib, "JTAG_SetGPIOs");
153
  if (jtagSetGPIOs == NULL) {
154
    printf("GetProcAddress for JTAG_SetGPIOs  Failed.\n");
155
    return -1;
156
  }
157
 
158
  jtagGetGPIOs = (jtagGetGPIOsPtr) GetProcAddress(hLib, "JTAG_GetGPIOs");
159
  if (jtagGetGPIOs == NULL) {
160
    printf("GetProcAddress for JTAG_GetGPIOs  Failed.\n");
161
    return -1;
162
  }
163
 
164
  jtagWrite = (jtagWritePtr) GetProcAddress(hLib, "JTAG_Write");
165
  if (jtagWrite == NULL) {
166
    printf("GetProcAddress for JTAG_Write  Failed.\n");
167
    return -1;
168
  }
169
 
170
  jtagRead = (jtagReadPtr) GetProcAddress(hLib, "JTAG_Read");
171
  if (jtagRead == NULL) {
172
    printf("GetProcAddress for JTAG_Read  Failed.\n");
173
    return -1;
174
  }
175
 
176
  jtagWriteRead = (jtagWriteReadPtr) GetProcAddress(hLib, "JTAG_WriteRead");
177
  if (jtagWriteRead == NULL) {
178
    printf("GetProcAddress for JTAG_WriteRead  Failed.\n");
179
    return -1;
180
  }
181
 
182
  jtagAddWriteCmd = (jtagAddWriteCmdPtr) GetProcAddress(hLib, "JTAG_AddWriteCmd");
183
  if (jtagAddWriteCmd == NULL) {
184
    printf("GetProcAddress for JTAG_AddWriteCmd  Failed.\n");
185
    return -1;
186
  }
187
 
188
  jtagAddReadCmd = (jtagAddReadCmdPtr) GetProcAddress(hLib, "JTAG_AddReadCmd");
189
  if (jtagAddReadCmd == NULL) {
190
    printf("GetProcAddress for JTAG_AddReadCmd  Failed.\n");
191
    return -1;
192
  }
193
 
194
  jtagAddWriteReadCmd = (jtagAddWriteReadCmdPtr) GetProcAddress(hLib, "JTAG_AddWriteReadCmd");
195
  if (jtagAddWriteReadCmd == NULL) {
196
    printf("GetProcAddress for JTAG_AddWriteReadCmd  Failed.\n");
197
    return -1;
198
  }
199
 
200
  jtagClearDeviceCmdSequence = (jtagClearDeviceCmdSequencePtr) GetProcAddress(hLib, "JTAG_ClearDeviceCmdSequence");
201
  if (jtagClearDeviceCmdSequence == NULL) {
202
    printf("GetProcAddress for  Failed.\n JTAG_ClearDeviceCmdSequence");
203
    return -1;
204
  }
205
 
206
  jtagAddDeviceReadCmd = (jtagAddDeviceReadCmdPtr) GetProcAddress(hLib, "JTAG_AddDeviceReadCmd");
207
  if (jtagAddDeviceReadCmd == NULL) {
208
    printf("GetProcAddress for  Failed JTAG_AddDeviceReadCmd.\n");
209
    return -1;
210
  }
211
 
212
  jtagAddDeviceWriteReadCmd = (jtagAddDeviceWriteReadCmdPtr) GetProcAddress(hLib, "JTAG_AddDeviceWriteReadCmd");
213
  if (jtagAddDeviceWriteReadCmd == NULL) {
214
    printf("GetProcAddress for  Failed.\n JTAG_AddDeviceWriteReadCmd");
215
    return -1;
216
  }
217
 
218
  jtagExecuteCmdSequence = (jtagExecuteCmdSequencePtr) GetProcAddress(hLib, "JTAG_ExecuteCmdSequence");
219
  if (jtagExecuteCmdSequence == NULL) {
220
    printf("GetProcAddress for  Failed JTAG_ExecuteCmdSequence.\n");
221
    return -1;
222
  }
223
 
224
  jtagGetDllVersion = (jtagGetDllVersionPtr) GetProcAddress(hLib, "JTAG_GetDllVersion");
225
  if (jtagGetDllVersion == NULL) {
226
    printf("GetProcAddress for JTAG_GetDllVersion  Failed.\n");
227
    return -1;
228
  }
229
 
230
  jtagGetErrorCodeString = (jtagGetErrorCodeStringPtr) GetProcAddress(hLib, "JTAG_GetErrorCodeString");
231
  if (jtagGetErrorCodeString == NULL) {
232
    printf("GetProcAddress for  Failed JTAG_GetErrorCodeString.\n");
233
    return -1;
234
  }
235
 
236
  //   = (Ptr) GetProcAddress(hLib, "");  
237
  //if ( == NULL) {
238
  //  printf("GetProcAddress for  Failed.\n");
239
  //  return -1;
240
  // }
241
 
242
  return 0;
243
 
244
}
245
 
246
 
247
// Global USB JTAG device handle
248
FTC_HANDLE ftHandle;
249
 
250
FTC_STATUS
251
FT2232_USB_JTAG_WriteDataToExternalDevice(
252
                                          //FTC_HANDLE ftHandle, 
253
                                          BOOL bInstructionTestData,
254
                                          DWORD dwNumBitsToWrite,
255
                                          PWriteDataByteBuffer pWriteDataBuffer,
256
                                          DWORD dwNumBytesToWrite,
257
                                          DWORD dwTapControllerState)
258
{
259
  // Return the appropritae call to the Windows driver
260
  return (jtagWrite)(
261
                     ftHandle,
262
                     bInstructionTestData,
263
                     dwNumBitsToWrite,
264
                     pWriteDataBuffer,
265
                     dwNumBytesToWrite,
266
                     dwTapControllerState);
267
 
268
}
269
 
270
 
271
// Read data from external device
272
FTC_STATUS
273
FT2232_USB_JTAG_ReadDataFromExternalDevice(
274
                                           //FTC_HANDLE ftHandle, 
275
                                            BOOL bInstructionTestData,
276
                                            DWORD dwNumBitsToRead,
277
                                            PReadDataByteBuffer pReadDataBuffer,
278
                                            LPDWORD lpdwNumBytesReturned,
279
                                            DWORD dwTapControllerState)
280
 
281
{
282
  return (jtagRead)(
283
                    ftHandle,
284
                    bInstructionTestData,
285
                    dwNumBitsToRead,
286
                    pReadDataBuffer,
287
                    lpdwNumBytesReturned,
288
                    dwTapControllerState);
289
 
290
}
291
 
292
 
293
 
294
 
295
 
296
 
297
// Write Read
298
FTC_STATUS
299
FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(
300
                                                  //FTC_HANDLE ftHandle, 
301
                                                  BOOL bInstructionTestData,
302
                                                  DWORD dwNumBitsToWriteRead,
303
                                                  PWriteDataByteBuffer pWriteDataBuffer,
304
                                                  DWORD dwNumBytesToWrite,
305
                                                  PReadDataByteBuffer pReadDataBuffer,
306
                                                  LPDWORD lpdwNumBytesReturned,
307
                                                  DWORD dwTapControllerState)
308
{
309
 
310
 return (jtagWriteRead)(
311
                        ftHandle,
312
                        bInstructionTestData,
313
                        dwNumBitsToWriteRead,
314
                        pWriteDataBuffer,
315
                        dwNumBytesToWrite,
316
                        pReadDataBuffer,
317
                        lpdwNumBytesReturned,
318
                        dwTapControllerState);
319
 
320
}
321
 
322
 
323
 
324
// Close device
325
FTC_STATUS
326
FT2232_USB_JTAG_CloseDevice()
327
{
328
  return (jtagClose)(ftHandle);
329
}
330
 
331 47 julius
void
332
reinit_usb_jtag(void)
333
{
334
  FTC_STATUS Status = FTC_SUCCESS;
335
  if (DEBUG_USB_DRVR_FUNCS) printf("win_usb_driver_calls: reset_usb_jtag() - ");
336
  Status = (jtagInitDevice)(ftHandle, 0);
337
  if (DEBUG_USB_DRVR_FUNCS) printf("return Status: %d\n", (int) Status);
338
}
339 39 julius
 
340
// Set clock frequency
341
// Frequency = 12Mhz/((1+divisor)*2), 
342
// divisor=1000 => Freq=5995Hz~=6kHz, divisor=500=>Freq=12kHz
343
#define USB_JTAG_CLK_DIVIDER 0
344
 
345
int init_usb_jtag()
346
{
347
  FTC_STATUS Status = FTC_SUCCESS;
348
  DWORD dwNumDevices = 0;
349
  char szDeviceName[100];
350
  DWORD dwLocationID = 0;
351
  // Made global: FTC_HANDLE ftHandle;
352
  DWORD dwClockFrequencyHz = 0;
353
  FTC_INPUT_OUTPUT_PINS LowInputOutputPinsData;
354
  FTC_INPUT_OUTPUT_PINS HighInputOutputPinsData;
355
  FTC_LOW_HIGH_PINS LowPinsInputData;
356
  FTC_LOW_HIGH_PINS HighPinsInputData;
357
  BOOL bPerformCommandSequence = false;
358
 
359
  DWORD dwLoopCntr = 0;
360
  char szDllVersion[10];
361
 
362
  Status = (jtagGetNumDevices)(&dwNumDevices);
363
  if (DEBUG_USB_DRVR_FUNCS)
364
    printf("JTAG_GetNumDevices returned Status: 0x%x and %d devices\n",
365
                    Status, dwNumDevices);
366
 
367
 
368
 
369
  if (dwNumDevices == 0)
370
    {
371
      printf("Error: USB debug cable not detected\nPlease ensure the device is attached and correctly installed\n\n");
372
      exit(-1);
373
    }
374
 
375
 
376
  Status = (jtagGetDllVersion)(szDllVersion, 10);
377
 
378
  if (DEBUG_USB_DRVR_FUNCS)
379
    printf("JTAG_GetDLLVersion returned Status: 0x%x and version %s\n",
380
                     Status, szDllVersion);
381
 
382
 
383
  // To do: Iterate through dwNumDevices by index number ,looking for the ORSoC Device, "ORSoC OpenRISC debug cable A\0",
384
 
385
  if ((Status == FTC_SUCCESS) && (dwNumDevices > 0))
386
    {
387
      if (dwNumDevices == 1)
388
        {
389
          Status = (jtagGetDeviceNameLocID)(0,szDeviceName,50, &dwLocationID);
390
 
391
          if (DEBUG_USB_DRVR_FUNCS)
392
            printf("JTAG_GetDeviceNameLocID: %s at: 0x%x\n",
393
                   szDeviceName, dwLocationID);
394
 
395
          if (Status == FTC_SUCCESS)
396
            {
397
              Status = (jtagOpenEx)(szDeviceName, dwLocationID, &ftHandle);
398
 
399
            }
400
        }
401
      else
402
        {
403
          if (dwNumDevices == 2)
404
            {
405
              Status=(jtagGetDeviceNameLocID)(1,szDeviceName,50,&dwLocationID);
406
 
407
              if (Status == FTC_SUCCESS)
408
                {
409
                  Status = (jtagOpenEx)(szDeviceName, dwLocationID, &ftHandle);
410
                }
411
            }
412
        }
413
    }
414
 
415
 
416
  if ((Status == FTC_SUCCESS) && (dwNumDevices > 0))
417
    {
418
      Status = (jtagInitDevice)(ftHandle, 0);
419
 
420
      // Set clock frequency
421
      // Frequency = 12Mhz/((1+divisor)*2), 
422
      // divisor=1000 => Freq=5995Hz~=6kHz, divisor=500=>Freq=12kHz
423
      if (Status == FTC_SUCCESS)
424
        Status = (jtagSetClock)(ftHandle, USB_JTAG_CLK_DIVIDER,
425
                                &dwClockFrequencyHz);
426
 
427
 
428
      if (Status == FTC_SUCCESS){
429
        printf("USB JTAG interface initialised\n");
430
        Status = (jtagGetClock)(5, &dwClockFrequencyHz);
431
      }
432
 
433
      if (Status == FTC_SUCCESS)
434
        { // true=output, false=input
435
          LowInputOutputPinsData.bPin1InputOutputState = true; // ADBUS4 - 
436
          // set ADBUS4 to output 0 to enable JTAG buffers on Olimex devices
437
          LowInputOutputPinsData.bPin2InputOutputState = true; //ADBUS5
438
          LowInputOutputPinsData.bPin3InputOutputState = true;
439
          LowInputOutputPinsData.bPin4InputOutputState = true;
440
          //ADBUS4 = 0 (enable JTAG buffers on Olimex devices)
441
          LowInputOutputPinsData.bPin1LowHighState = false; // ADBUS4
442
          LowInputOutputPinsData.bPin2LowHighState = false; // ADBUS5
443
          LowInputOutputPinsData.bPin3LowHighState = true; // ADBUS6
444
          LowInputOutputPinsData.bPin4LowHighState = true; // ADBUS7
445
 
446
          HighInputOutputPinsData.bPin1InputOutputState = true; // ACBUS0
447
          HighInputOutputPinsData.bPin2InputOutputState = true; // ACBUS1
448
          HighInputOutputPinsData.bPin3InputOutputState = true; // ACBUS2
449
          HighInputOutputPinsData.bPin4InputOutputState = true; // ACBUS3
450
          HighInputOutputPinsData.bPin1LowHighState = true; // ACBUS0
451
          HighInputOutputPinsData.bPin2LowHighState = true; // ACBUS1
452
          HighInputOutputPinsData.bPin3LowHighState = true; // ACBUS2
453
            HighInputOutputPinsData.bPin4LowHighState =true; // ACBUS3
454
 
455
          Status = (jtagSetGPIOs)(ftHandle, true, &LowInputOutputPinsData,
456
                                        true, &HighInputOutputPinsData);
457
 
458
          if (Status == FTC_SUCCESS)
459
            Status = (jtagGetGPIOs)(ftHandle, true, &LowPinsInputData,
460
                                    true, &HighPinsInputData);
461
 
462
          if (DEBUG_USB_DRVR_FUNCS)
463
            printf("JTAG low gpio pins values: (ADBUS7-ADBUS4):%x%x%x%x\n",
464
                   (LowPinsInputData.bPin4LowHighState==true),
465
                   (LowPinsInputData.bPin3LowHighState==true),
466
                   (LowPinsInputData.bPin2LowHighState==true),
467
                   (LowPinsInputData.bPin1LowHighState==true));
468
 
469
          if (DEBUG_USB_DRVR_FUNCS)
470
            printf("JTAG high gpio pins values: (ACBUS3-ACBUS0): %x%x%x%x\n",
471
                   (HighPinsInputData.bPin4LowHighState==true),
472
                   (HighPinsInputData.bPin3LowHighState==true),
473
                   (HighPinsInputData.bPin2LowHighState==true),
474
                   (HighPinsInputData.bPin1LowHighState==true));
475
        }
476
    }
477
 
478
  if (Status == FTC_SUCCESS)
479
    return 0;
480
  else
481
    return 1;
482
 
483
}

powered by: WebSVN 2.1.0

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