File: hw_opsis.c

package info (click to toggle)
ixo-usb-jtag 0.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 328 kB
  • sloc: ansic: 2,572; makefile: 90
file content (175 lines) | stat: -rw-r--r-- 5,562 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*-----------------------------------------------------------------------------
 *
 * Hardware-dependent code for usb_jtag
 *-----------------------------------------------------------------------------
 * Copyright (C) 2007 Kolja Waschk, ixo.de
 *-----------------------------------------------------------------------------
 * This code is part of usbjtag. usbjtag is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version. usbjtag is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.  You should have received a
 * copy of the GNU General Public License along with this program in the file
 * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA  02110-1301  USA
 *-----------------------------------------------------------------------------
 */

#include "hardware.h"
#include "fx2regs.h"
#include "syncdelay.h"

//---------------------------------------------------------------------------

#define JTAG_PORT IOE

#define SetOrClear(port, mask, input) \
  ((input) ? (port|=mask) : (port&=~mask))

//#define SetTCK(x)     do{if(x) IOE|=0x08; else IOE&=~0x08; }while(0)
#define bmTCK bmBIT4 // Output
#define SetTCK(x)     SetOrClear(JTAG_PORT, bmTCK, x)

//#define SetTMS(x)     do{if(x) IOE|=0x10; else IOE&=~0x10; }while(0)
#define bmTMS bmBIT3 // Output
#define SetTMS(x)     SetOrClear(JTAG_PORT, bmTMS, x)

//#define SetTDI(x)     do{if(x) IOE|=0x40; else IOE&=~0x40; }while(0)
#define bmTDI bmBIT2 // Output - Data from FX2 into FPGA
#define SetTDI(x)     SetOrClear(JTAG_PORT, bmTDI, x)

//#define GetTDO()      ((IOE>>5)&1)
#define bmTDO bmBIT0 // Input - Data from FPGA into FX2
#define bitTDO 0

#define GetTDO()      GetTDOToBit(0)
#define GetTDOToBit(bitPos) \
 (((int)(bitTDO-bitPos) > (int)0) ? \
   ((JTAG_PORT & bmTDO)>>(bitTDO-bitPos)) : \
   ((JTAG_PORT & bmTDO)<<(bitPos-bitTDO)) ) \

/* XPCU has neither AS nor PS mode pins */

#define HAVE_OE_LED 1
#define bmLED bmBIT5 // Output
#define SetOELED(x)   SetOrClear(JTAG_PORT, bmLED, x)

#define JTAG_PORT_OE bmTCK|bmTMS|bmTDI|bmLED

//-----------------------------------------------------------------------------

void ProgIO_Poll(void)    {}
void ProgIO_Enable(void)  {}
void ProgIO_Disable(void) {}
void ProgIO_Deinit(void)  {}

void ProgIO_Init(void)
{
  /* The following code depends on your actual circuit design.
     Make required changes _before_ you try the code! */

  // set the CPU clock to 48MHz, enable clock output to FPGA
  CPUCS = bmCLKOE | bmCLKSPD1;

  // Use internal 48 MHz, enable output, use "Port" mode for all pins
  IFCONFIG = bmIFCLKSRC | bm3048MHZ | bmIFCLKOE;

  GPIFABORT = 0xFF;

  PORTACFG = 0x00; OEA = 0x00; IOA=0x00;
  PORTCCFG = 0x00; OEC = 0x00; IOC=0x00;
  PORTECFG = 0x00; OEE = JTAG_PORT_OE; IOE=0x00;
}

void ProgIO_Set_State(unsigned char d)
{
  /* Set state of output pins
   * (d is the byte from the host):
   *
   * d.0 => TCK
   * d.1 => TMS
   * d.2 => nCE (only #ifdef HAVE_AS_MODE)
   * d.3 => nCS (only #ifdef HAVE_AS_MODE)
   * d.4 => TDI
   * d.6 => LED / Output Enable
   */

  SetTCK((d & bmBIT0) ? 1 : 0);
  SetTMS((d & bmBIT1) ? 1 : 0);
  SetTDI((d & bmBIT4) ? 1 : 0);
#ifdef HAVE_OE_LED
  SetOELED((d & bmBIT5) ? 1 : 0);
#endif
}

unsigned char ProgIO_Set_Get_State(unsigned char d)
{
  /* Set state of output pins (s.a.)
   * then read state of input pins:
   *
   * TDO => d.0
   * DATAOUT => d.1 (only #ifdef HAVE_AS_MODE)
   */

  ProgIO_Set_State(d);
  return 2|GetTDO(); /* DATAOUT assumed high, no AS mode */
}

void ProgIO_ShiftOut(unsigned char c)
{
  /* Shift out byte C:
   *
   * 8x {
   *   Output least significant bit on TDI
   *   Raise TCK
   *   Shift c right
   *   Lower TCK
   * }
   */

  unsigned char lc=c;

  SetTDI(lc & bmBIT0); SetTCK(1); lc>>=1; SetTCK(0);
  SetTDI(lc & bmBIT0); SetTCK(1); lc>>=1; SetTCK(0);
  SetTDI(lc & bmBIT0); SetTCK(1); lc>>=1; SetTCK(0);
  SetTDI(lc & bmBIT0); SetTCK(1); lc>>=1; SetTCK(0);

  SetTDI(lc & bmBIT0); SetTCK(1); lc>>=1; SetTCK(0);
  SetTDI(lc & bmBIT0); SetTCK(1); lc>>=1; SetTCK(0);
  SetTDI(lc & bmBIT0); SetTCK(1); lc>>=1; SetTCK(0);
  SetTDI(lc & bmBIT0); SetTCK(1); lc>>=1; SetTCK(0);
}

unsigned char ProgIO_ShiftInOut(unsigned char c)
{
  /* Shift out byte C, shift in from TDO:
   *
   * 8x {
   *   Read carry from TDO
   *   Output least significant bit on TDI
   *   Raise TCK
   *   Shift c right, append carry (TDO) at left (into MSB)
   *   Lower TCK
   * }
   * Return c.
   */

  unsigned char carry;
  unsigned char lc=c;

  carry = GetTDOToBit(7); SetTDI(lc & bmBIT0); SetTCK(1); lc=carry|(lc>>1); SetTCK(0); 
  carry = GetTDOToBit(7); SetTDI(lc & bmBIT0); SetTCK(1); lc=carry|(lc>>1); SetTCK(0);
  carry = GetTDOToBit(7); SetTDI(lc & bmBIT0); SetTCK(1); lc=carry|(lc>>1); SetTCK(0);
  carry = GetTDOToBit(7); SetTDI(lc & bmBIT0); SetTCK(1); lc=carry|(lc>>1); SetTCK(0);

  carry = GetTDOToBit(7); SetTDI(lc & bmBIT0); SetTCK(1); lc=carry|(lc>>1); SetTCK(0);
  carry = GetTDOToBit(7); SetTDI(lc & bmBIT0); SetTCK(1); lc=carry|(lc>>1); SetTCK(0);
  carry = GetTDOToBit(7); SetTDI(lc & bmBIT0); SetTCK(1); lc=carry|(lc>>1); SetTCK(0);
  carry = GetTDOToBit(7); SetTDI(lc & bmBIT0); SetTCK(1); lc=carry|(lc>>1); SetTCK(0);

  return lc;
}