-
Notifications
You must be signed in to change notification settings - Fork 48
/
FlashCommand.c
executable file
·234 lines (208 loc) · 8.03 KB
/
FlashCommand.c
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/// CFlashCommand Implementations
#include "FlashCommand.h"
#include "Macro.h"
#include "project.h"
#include "usbdriver.h"
#define SerialFlash_FALSE -1
#define SerialFlash_TRUE 1
extern bool Is_NewUSBCommand(int Index);
int FlashCommand_TransceiveOut(unsigned char* v, int len, int has_result_in, int Index)
{
CNTRPIPE_RQ rq;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_OUT;
rq.Request = TRANSCEIVE;
if (Is_NewUSBCommand(Index)) {
rq.Value = ((has_result_in == 1) ? RESULT_IN : NO_RESULT_IN);
rq.Index = RFU;
} else {
rq.Value = RFU;
rq.Index = ((has_result_in == 1) ? RESULT_IN : NO_RESULT_IN);
}
rq.Length = len;
#if 0
printf("len = %d",len);
for (i=0 ; i<len; i++) {
printf("\nv[%d] = 0x%x\n", i, v[i]);
}
#endif
return OutCtrlRequest(&rq, v, len, Index);
}
int FlashCommand_TransceiveIn(unsigned char* v, int len, int Index)
{
CNTRPIPE_RQ rq;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_IN;
rq.Request = TRANSCEIVE;
if (Is_NewUSBCommand(Index)) {
rq.Value = 0x01;
rq.Index = NO_REGISTER;
} else {
rq.Value = CTRL_TIMEOUT;
rq.Index = NO_REGISTER;
}
rq.Length = len;
if (InCtrlRequest(&rq, v, (unsigned long)len, Index) == SerialFlash_FALSE)
return SerialFlash_FALSE;
return FlashCommand_TRUE;
}
int FlashCommand_SendCommand_OutOnlyInstruction(unsigned char* v, int len, int Index)
{
return FlashCommand_TransceiveOut(v, len, NO_RESULT_IN, Index);
}
int FlashCommand_SendCommand_OutInstructionWithCS(unsigned char* v, int len, int Index)
{
CNTRPIPE_RQ rq;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_OUT;
rq.Request = TRANSCEIVE;
if (Is_NewUSBCommand(Index)) {
rq.Value = 0x02;
rq.Index = 0;
} else {
rq.Value = RFU;
rq.Index = 0x02;
}
rq.Length = len;
return OutCtrlRequest(&rq, v, len, Index);
}
int FlashCommand_SendCommand_OneOutOneIn(unsigned char* vOut, int out_len, unsigned char* vIn, int in_len, int Index)
{
if (FlashCommand_TransceiveOut(vOut, out_len, RESULT_IN, Index) == SerialFlash_FALSE) {
return SerialFlash_FALSE;
}
if (FlashCommand_TransceiveIn(vIn, in_len, Index) == SerialFlash_FALSE) {
return SerialFlash_FALSE;
}
return FlashCommand_TRUE;
}
int FlashCommand_SendCommand_SetupPacketForBulkWrite(struct CAddressRange* AddrRange, unsigned char modeWrite, unsigned char WriteCom, unsigned int PageSize, unsigned int AddressMode, int Index)
{
unsigned char vInstruction[15];
CNTRPIPE_RQ rq;
// length in terms of 256/128 bytes
size_t divider;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_OUT;
rq.Request = WRITE;
switch (modeWrite) {
case MODE_NUMONYX_PCM: // 512 bytes
case PP_32BYTE:
divider = 9;
break;
case PP_128BYTE: // 128 bytes
divider = 7;
break;
case PP_PROGRAM_ANYSIZE_PAGESIZE:
if(PageSize == 0x200)
divider = 9;
else if(PageSize == 0x400)
divider = 10;
else
divider = 8;
default: // 256 bytes
divider = 8;
break;
}
size_t pageNum = (AddrRange->end - AddrRange->start) >> divider;
vInstruction[0] = (unsigned char)(pageNum & 0xff); // lowest byte of length : page number
vInstruction[1] = (unsigned char)((pageNum >> 8) & 0xff); // highest byte of length: page number
vInstruction[2] = (unsigned char)((pageNum >> 16) & 0xff); // reserved
vInstruction[3] = modeWrite; // PAGE_PROGRAM, PAGE_WRITE, AAI_1_BYTE, AAI_2_BYTE, PP_128BYTE, PP_AT26DF041
vInstruction[4] = WriteCom;
if (Is_NewUSBCommand(Index)) {
vInstruction[5] = 0;
vInstruction[6] = (AddrRange->start & 0xff);
vInstruction[7] = ((AddrRange->start >> 8) & 0xff);
vInstruction[8] = ((AddrRange->start >> 16) & 0xff);
vInstruction[9] = ((AddrRange->start >> 24) & 0xff);
vInstruction[10] = (PageSize & 0xff);
vInstruction[11] = ((PageSize >> 8) & 0xff);
vInstruction[12] = ((PageSize >> 16) & 0xff);
vInstruction[13] = ((PageSize >> 24) & 0xff);
vInstruction[14] = (AddressMode & 0xff);
rq.Value = 0;
rq.Index = 0;
rq.Length = (unsigned long)(15);
} else {
rq.Value = (unsigned short)(AddrRange->start & 0xffff); // 16 bits LSB
rq.Index = (unsigned short)((AddrRange->start >> 16) & 0xffff); // 16 bits MSB
rq.Length = (unsigned long)(5);
}
// send rq via control pipe
return OutCtrlRequest(&rq, vInstruction, rq.Length, Index);
}
int FlashCommand_SendCommand_SetupPacketForAT45DBBulkWrite(struct CAddressRange* AddrRange, unsigned char modeWrite, unsigned char WriteCom, int Index)
{
/* modeWrite:
1: page-size = 256
2: page-size = 264
3: page-size = 512
4: page-size = 528
5: page-size = 1024
6: page-size = 1056
*/
size_t pageSize[7] = { 0, 256, 264, 512, 528, 1024, 1056 };
size_t pageNum = ((AddrRange->end - AddrRange->start) + pageSize[modeWrite] - 1) / pageSize[modeWrite];
// printf("modeWrite = %hhu\n",modeWrite);
// printf("pageNum = %lu\n",pageNum);
unsigned char vInstruction[10];
vInstruction[0] = (unsigned char)(pageNum & 0xff); // lowest byte of length : page number
vInstruction[1] = (unsigned char)((pageNum >> 8) & 0xff); // highest byte of length: page number
vInstruction[2] = (unsigned char)((pageNum >> 16) & 0xff); // reserved
vInstruction[3] = modeWrite;
vInstruction[4] = WriteCom;
CNTRPIPE_RQ rq;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_OUT;
rq.Request = ATMEL45_WRITE;
if (Is_NewUSBCommand(Index)) {
vInstruction[5] = 0;
vInstruction[6] = (AddrRange->start & 0xff);
vInstruction[7] = ((AddrRange->start >> 8) & 0xff);
vInstruction[8] = ((AddrRange->start >> 16) & 0xff);
vInstruction[9] = ((AddrRange->start >> 24) & 0xff);
rq.Value = 0;
rq.Index = 0;
rq.Length = (unsigned long)(10);
} else {
rq.Value = (unsigned short)(AddrRange->start & 0xffff); // 16 bits LSB
rq.Index = (unsigned short)((AddrRange->start >> 16) & 0xffff); // 16 bits MSB
rq.Length = (unsigned long)(5);
}
// send rq via control pipe
return OutCtrlRequest(&rq, vInstruction, rq.Length, Index);
}
int FlashCommand_SendCommand_SetupPacketForBulkRead(struct CAddressRange* AddrRange, unsigned char modeRead, unsigned char ReadCom, unsigned int AddrLen, unsigned int DummyLen, int Index)
{
unsigned char vInstruction[12];
CNTRPIPE_RQ rq;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_OUT;
rq.Request = DTC_READ;
// length in terms of 256 bytes
size_t pageNum = AddrRange->length >> 9;
vInstruction[0] = (unsigned char)(pageNum & 0xff); // lowest byte of length : page number
vInstruction[1] = (unsigned char)((pageNum >> 8) & 0xff); // highest byte of length: page number
vInstruction[2] = (unsigned char)((pageNum >> 16) & 0xff); // reserved
vInstruction[3] = modeRead; // BULK_NORM_READ, BULK_FAST_READ
vInstruction[4] = ReadCom;
if (Is_NewUSBCommand(Index)) {
vInstruction[5] = 0xFF;
vInstruction[6] = (AddrRange->start & 0xff);
vInstruction[7] = ((AddrRange->start >> 8) & 0xff);
vInstruction[8] = ((AddrRange->start >> 16) & 0xff);
vInstruction[9] = ((AddrRange->start >> 24) & 0xff);
vInstruction[10] = (AddrLen & 0xff);
vInstruction[11] = (DummyLen & 0xff);
rq.Value = 0;
rq.Index = 0;
rq.Length = (unsigned long)(12);
} else {
rq.Value = (unsigned short)(AddrRange->start & 0xffff); // 16 bits LSB
rq.Index = (unsigned short)((AddrRange->start >> 16) & 0xffff); // 16 bits MSB
rq.Length = (unsigned long)(5);
}
// send rq via control pipe
return OutCtrlRequest(&rq, vInstruction, rq.Length, Index);
}