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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [or_debug_proxy/] [src/] [win_usb_driver_calls.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                      : 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
 
332
 
333
 
334
// Set clock frequency
335
// Frequency = 12Mhz/((1+divisor)*2), 
336
// divisor=1000 => Freq=5995Hz~=6kHz, divisor=500=>Freq=12kHz
337
#define USB_JTAG_CLK_DIVIDER 0
338
 
339
int init_usb_jtag()
340
{
341
  FTC_STATUS Status = FTC_SUCCESS;
342
  DWORD dwNumDevices = 0;
343
  char szDeviceName[100];
344
  DWORD dwLocationID = 0;
345
  // Made global: FTC_HANDLE ftHandle;
346
  DWORD dwClockFrequencyHz = 0;
347
  FTC_INPUT_OUTPUT_PINS LowInputOutputPinsData;
348
  FTC_INPUT_OUTPUT_PINS HighInputOutputPinsData;
349
  FTC_LOW_HIGH_PINS LowPinsInputData;
350
  FTC_LOW_HIGH_PINS HighPinsInputData;
351
  BOOL bPerformCommandSequence = false;
352
 
353
  DWORD dwLoopCntr = 0;
354
  char szDllVersion[10];
355
 
356
  Status = (jtagGetNumDevices)(&dwNumDevices);
357
  if (DEBUG_USB_DRVR_FUNCS)
358
    printf("JTAG_GetNumDevices returned Status: 0x%x and %d devices\n",
359
                    Status, dwNumDevices);
360
 
361
 
362
 
363
  if (dwNumDevices == 0)
364
    {
365
      printf("Error: USB debug cable not detected\nPlease ensure the device is attached and correctly installed\n\n");
366
      exit(-1);
367
    }
368
 
369
 
370
  Status = (jtagGetDllVersion)(szDllVersion, 10);
371
 
372
  if (DEBUG_USB_DRVR_FUNCS)
373
    printf("JTAG_GetDLLVersion returned Status: 0x%x and version %s\n",
374
                     Status, szDllVersion);
375
 
376
 
377
  // To do: Iterate through dwNumDevices by index number ,looking for the ORSoC Device, "ORSoC OpenRISC debug cable A\0",
378
 
379
  if ((Status == FTC_SUCCESS) && (dwNumDevices > 0))
380
    {
381
      if (dwNumDevices == 1)
382
        {
383
          Status = (jtagGetDeviceNameLocID)(0,szDeviceName,50, &dwLocationID);
384
 
385
          if (DEBUG_USB_DRVR_FUNCS)
386
            printf("JTAG_GetDeviceNameLocID: %s at: 0x%x\n",
387
                   szDeviceName, dwLocationID);
388
 
389
          if (Status == FTC_SUCCESS)
390
            {
391
              Status = (jtagOpenEx)(szDeviceName, dwLocationID, &ftHandle);
392
 
393
            }
394
        }
395
      else
396
        {
397
          if (dwNumDevices == 2)
398
            {
399
              Status=(jtagGetDeviceNameLocID)(1,szDeviceName,50,&dwLocationID);
400
 
401
              if (Status == FTC_SUCCESS)
402
                {
403
                  Status = (jtagOpenEx)(szDeviceName, dwLocationID, &ftHandle);
404
                }
405
            }
406
        }
407
    }
408
 
409
 
410
  if ((Status == FTC_SUCCESS) && (dwNumDevices > 0))
411
    {
412
      Status = (jtagInitDevice)(ftHandle, 0);
413
 
414
      // Set clock frequency
415
      // Frequency = 12Mhz/((1+divisor)*2), 
416
      // divisor=1000 => Freq=5995Hz~=6kHz, divisor=500=>Freq=12kHz
417
      if (Status == FTC_SUCCESS)
418
        Status = (jtagSetClock)(ftHandle, USB_JTAG_CLK_DIVIDER,
419
                                &dwClockFrequencyHz);
420
 
421
 
422
      if (Status == FTC_SUCCESS){
423
        printf("USB JTAG interface initialised\n");
424
        Status = (jtagGetClock)(5, &dwClockFrequencyHz);
425
      }
426
 
427
      if (Status == FTC_SUCCESS)
428
        { // true=output, false=input
429
          LowInputOutputPinsData.bPin1InputOutputState = true; // ADBUS4 - 
430
          // set ADBUS4 to output 0 to enable JTAG buffers on Olimex devices
431
          LowInputOutputPinsData.bPin2InputOutputState = true; //ADBUS5
432
          LowInputOutputPinsData.bPin3InputOutputState = true;
433
          LowInputOutputPinsData.bPin4InputOutputState = true;
434
          //ADBUS4 = 0 (enable JTAG buffers on Olimex devices)
435
          LowInputOutputPinsData.bPin1LowHighState = false; // ADBUS4
436
          LowInputOutputPinsData.bPin2LowHighState = false; // ADBUS5
437
          LowInputOutputPinsData.bPin3LowHighState = true; // ADBUS6
438
          LowInputOutputPinsData.bPin4LowHighState = true; // ADBUS7
439
 
440
          HighInputOutputPinsData.bPin1InputOutputState = true; // ACBUS0
441
          HighInputOutputPinsData.bPin2InputOutputState = true; // ACBUS1
442
          HighInputOutputPinsData.bPin3InputOutputState = true; // ACBUS2
443
          HighInputOutputPinsData.bPin4InputOutputState = true; // ACBUS3
444
          HighInputOutputPinsData.bPin1LowHighState = true; // ACBUS0
445
          HighInputOutputPinsData.bPin2LowHighState = true; // ACBUS1
446
          HighInputOutputPinsData.bPin3LowHighState = true; // ACBUS2
447
            HighInputOutputPinsData.bPin4LowHighState =true; // ACBUS3
448
 
449
          Status = (jtagSetGPIOs)(ftHandle, true, &LowInputOutputPinsData,
450
                                        true, &HighInputOutputPinsData);
451
 
452
          if (Status == FTC_SUCCESS)
453
            Status = (jtagGetGPIOs)(ftHandle, true, &LowPinsInputData,
454
                                    true, &HighPinsInputData);
455
 
456
          if (DEBUG_USB_DRVR_FUNCS)
457
            printf("JTAG low gpio pins values: (ADBUS7-ADBUS4):%x%x%x%x\n",
458
                   (LowPinsInputData.bPin4LowHighState==true),
459
                   (LowPinsInputData.bPin3LowHighState==true),
460
                   (LowPinsInputData.bPin2LowHighState==true),
461
                   (LowPinsInputData.bPin1LowHighState==true));
462
 
463
          if (DEBUG_USB_DRVR_FUNCS)
464
            printf("JTAG high gpio pins values: (ACBUS3-ACBUS0): %x%x%x%x\n",
465
                   (HighPinsInputData.bPin4LowHighState==true),
466
                   (HighPinsInputData.bPin3LowHighState==true),
467
                   (HighPinsInputData.bPin2LowHighState==true),
468
                   (HighPinsInputData.bPin1LowHighState==true));
469
        }
470
    }
471
 
472
  if (Status == FTC_SUCCESS)
473
    return 0;
474
  else
475
    return 1;
476
 
477
}

powered by: WebSVN 2.1.0

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