1 #include "jtag.h"
2 #include "iobase.h"
3 #include "javr.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #include "debug.h"
8 
9 static Jtag *avr_j;
10 static IOBase *avr_io;
11 
BitArraytoByteArray(const char * Data,unsigned char * bData,int Size)12 static void   BitArraytoByteArray(const char * Data, unsigned char *bData, int Size)
13 {
14   int i,j;
15   unsigned char bvalue;
16 
17   if (debug& UL_FUNCTIONS)
18     fprintf(stderr, "BitArraytoByteArray Size %2d  ", Size);
19   if (debug& UL_DETAILS) {
20 
21     if (Size < 17) {
22       for (i=Size-1; i>=0; i--)
23 	fprintf(stderr, "%c",(Data[i]=='1')?'1':'0');
24     }
25   }
26 
27   bvalue=0;
28   j = 0;
29   for (i=0; i<Size; i++) {
30     bvalue += (Data[i]=='1')? (1<<j):0;
31     bData[i/8]=bvalue;
32     if (j >= 7) {
33       j=0;
34       bvalue = 0;
35     }
36     else
37       j++;
38   }
39   if (debug& UL_FUNCTIONS) {
40     fprintf(stderr, " Result 0x");
41     if (Size%8)
42       fprintf(stderr, "%02x", bData[Size/8]);
43     for(i=Size/8; i >0; i--)
44       fprintf(stderr, "%02x", bData[i-1]);
45     fprintf(stderr, "\n");
46   }
47 }
48 
ByteArraytoBitArray(char * Output,unsigned char * bOutput,int Size)49 static void   ByteArraytoBitArray(char * Output, unsigned char *bOutput, int Size)
50 {
51   int i,j;
52   unsigned char bvalue;
53 
54   bvalue=0;
55   j = 0;
56   for (i=0; i<Size; i++) {
57     if ((i%8)== 0) {
58       bvalue = bOutput[i/8];
59     }
60     Output[i]=(bvalue & 1<<j)? '1': '0';
61     if (j >= 7) {
62       j=0;
63     }
64     else
65       j++;
66   }
67   if (debug& UL_FUNCTIONS)
68     fprintf(stderr, "ByteArraytoBitArray size %d ", Size);
69   if (debug& UL_DETAILS) {
70     fprintf(stderr, "0x");
71     for (i=(Size>64)?63:Size-1;i>=0; i-=8)
72       fprintf(stderr, "%02x",bOutput[i/8]);
73     fprintf(stderr, " ");
74     for (i=(Size>64)?63:Size-1;i>=0; i--)
75       fprintf(stderr, "%c",Output[i]);
76   }
77   if (debug& UL_FUNCTIONS)
78     fprintf(stderr, "\n");
79 }
80 
JTAG_Init(Jtag * j,IOBase * io)81 void JTAG_Init(Jtag *j, IOBase *io)
82 {
83   avr_j = j;
84   avr_io = io;
85 }
86 
Send_Instruction(int Size,const char * Data)87 void Send_Instruction(int Size, const char *Data){
88   unsigned char inst[1];
89   if (Size !=4){
90     fprintf(stderr," Unexpected size %d\n",Size);
91     return;
92   }
93   else {
94     BitArraytoByteArray(Data, inst, 4);
95     if (debug& UL_FUNCTIONS)
96       fprintf(stderr,"Send_Instruction 0x%02x\n",inst[0]);
97   }
98   avr_j->shiftIR(inst, 0);
99 }
100 
Send_Data(int Size,const char * Data)101 void Send_Data(int Size, const char *Data)
102 {
103   int bSize= Size/8;
104   unsigned char * bData;
105 
106   bSize+=(Size%8)?1:0;
107   bData=(byte*) malloc(bSize*sizeof(unsigned char));
108 
109   if (debug& UL_FUNCTIONS)
110     fprintf(stderr,"Send_Data Size %d\n",Size);
111   if (!bData ) {
112     fprintf(stderr,"Send_Data: malloc failed\n");
113     return;
114   }
115   BitArraytoByteArray(Data, bData, Size);
116   avr_j->shiftDR(bData,0, Size, 0, true);
117   free(bData);
118 }
119 
Send_bData_Output(int Size,unsigned char * Data,unsigned char * Output)120 void Send_bData_Output(int Size, unsigned char *Data, unsigned char *Output)
121 {
122   if (debug& UL_FUNCTIONS)
123     fprintf(stderr,"Send_bData_Output\n");
124   avr_j->shiftDR(Data,Output,Size, 0, 1);
125 }
126 
Send_Data_Output(int Size,char * Data,char * Output)127 void Send_Data_Output(int Size, char *Data, char *Output)
128 {
129   int bSize= Size/8 + ((Size%8)?1:0);
130   unsigned char * bData, * bOutput;
131 
132   bData   = (unsigned char*) malloc(bSize*sizeof(unsigned char));
133   bOutput = (unsigned char*) malloc(bSize*sizeof(unsigned char));
134 
135   if (!bData || !bOutput) {
136     fprintf(stderr,"Send_Data_Output: malloc failed\n");
137     if(bData) free(bData);
138     if(bOutput) free(bOutput);
139     return;
140   }
141   if (debug& UL_FUNCTIONS)
142     fprintf(stderr,"Send_Data_Output Size %d bSize %d\n", Size, bSize);
143   BitArraytoByteArray(Data, bData, Size);
144   avr_j->shiftDR(bData,bOutput,Size, 0, 1);
145   ByteArraytoBitArray(Output,bOutput,Size);
146   free(bData);
147   free(bOutput);
148 }
149 
150 #ifdef NEWFUNCTIONS
Send_AVR_Prog_Command(unsigned short command)151 unsigned short Send_AVR_Prog_Command(unsigned short command)
152 {
153    unsigned char array[2],output[2];
154    unsigned short mask;
155 
156    ShortToByteArray(command,&array[0]);
157    avr_j->shiftDR(array,output,15,0,1);
158    mask=ByteArrayToShort(output);
159   if (debug& UL_FUNCTIONS)
160    fprintf(stderr,"Send_AVR_Prog_Command d send 0x%04x rec 0x%04x\n",
161 	   command, mask);
162    return(mask);
163 }
164 /********************************************************************\
165 *                                                                    *
166 * Use JTAG Reset Register to put AVR in Reset                        *
167 *                                                                    *
168 \********************************************************************/
ResetAVR(void)169 void ResetAVR(void)
170 {
171   unsigned char x[1]={0xff};   /* High corresponds to external reset low */
172 
173   fprintf(stderr,"ResetAVR(new)\n");
174   Send_Instruction(4,AVR_RESET);
175   avr_j->shiftDR(x,0,1,0,1);
176 }
177 
178 /********************************************************************\
179 *                                                                    *
180 * Use JTAG Reset Register to take AVR out of Reset                   *
181 *                                                                    *
182 \********************************************************************/
ResetReleaseAVR(void)183 void ResetReleaseAVR(void)
184 {
185   unsigned char x[1]={0};   /* High corresponds to external reset low */
186 
187   fprintf(stderr,"ResetReleaseAVR(new)\n");
188   Send_Instruction(4,AVR_RESET);
189   avr_j->shiftDR(x,0,1,0,1);
190 }
191 
192 
AVR_Prog_Enable(void)193 void AVR_Prog_Enable(void)
194 {
195 
196   unsigned char inst[2];
197   fprintf(stderr,"AVR_Prog_Enable(New)\n");
198   ShortToByteArray(0xA370,&inst[0]);
199   Send_Instruction(4,PROG_ENABLE);
200   avr_j->shiftDR(inst,0,16,0,1);
201 }
202 
AVR_Prog_Disable(void)203 void AVR_Prog_Disable(void)
204 {
205 
206   unsigned char inst[2];
207   fprintf(stderr,"AVR_Prog_Disable(new)\n");
208   ShortToByteArray(0,&inst[0]);
209   Send_Instruction(4,PROG_ENABLE);
210   avr_j->shiftDR(inst,0,16,0,1);
211 }
212 #endif
213 
214