7bb8f71ca38f24df7f5fb844464a82a57f128cf4
[pub/USBasp.git] / Projects / Incomplete / AVRISP / Lib / V2Protocol.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * V2Protocol handler, to process V2 Protocol commands used in Atmel programmer devices.
34 */
35
36 #define INCLUDE_FROM_V2PROTOCOL_C
37 #include "V2Protocol.h"
38
39 void V2Protocol_ProcessCommand(void)
40 {
41 uint8_t V2Command = Endpoint_Read_Byte();
42
43 switch (V2Command)
44 {
45 case CMD_SIGN_ON:
46 V2Protocol_Command_SignOn();
47 break;
48 case CMD_SET_PARAMETER:
49 case CMD_GET_PARAMETER:
50 V2Protocol_Command_GetSetParam(V2Command);
51 break;
52 case CMD_LOAD_ADDRESS:
53 V2Protocol_Command_LoadAddress();
54 break;
55 case CMD_ENTER_PROGMODE_ISP:
56 V2Protocol_Command_EnterISPMode();
57 break;
58 case CMD_LEAVE_PROGMODE_ISP:
59 V2Protocol_Command_LeaveISPMode();
60 break;
61 case CMD_PROGRAM_FLASH_ISP:
62 case CMD_PROGRAM_EEPROM_ISP:
63 V2Protocol_Command_ProgramMemory(V2Command);
64 break;
65 case CMD_READ_FLASH_ISP:
66 case CMD_READ_EEPROM_ISP:
67 V2Protocol_Command_ReadMemory(V2Command);
68 break;
69 case CMD_CHIP_ERASE_ISP:
70 V2Protocol_Command_ChipErase();
71 break;
72 case CMD_READ_FUSE_ISP:
73 case CMD_READ_LOCK_ISP:
74 case CMD_READ_SIGNATURE_ISP:
75 case CMD_READ_OSCCAL_ISP:
76 V2Protocol_Command_ReadFuseLockSigOSCCAL(V2Command);
77 break;
78 case CMD_PROGRAM_FUSE_ISP:
79 case CMD_PROGRAM_LOCK_ISP:
80 V2Protocol_Command_WriteFuseLock(V2Command);
81 break;
82 case CMD_SPI_MULTI:
83 V2Protocol_Command_SPIMulti();
84 break;
85 default:
86 V2Protocol_Command_Unknown(V2Command);
87 break;
88 }
89
90 Endpoint_WaitUntilReady();
91 Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
92 }
93
94 static void V2Protocol_Command_Unknown(uint8_t V2Command)
95 {
96 /* Discard all incomming data */
97 while (Endpoint_BytesInEndpoint() == AVRISP_DATA_EPSIZE)
98 {
99 Endpoint_ClearOUT();
100 Endpoint_WaitUntilReady();
101 }
102
103 Endpoint_ClearOUT();
104 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
105
106 Endpoint_Write_Byte(V2Command);
107 Endpoint_Write_Byte(STATUS_CMD_UNKNOWN);
108 Endpoint_ClearIN();
109 }
110
111 static void V2Protocol_Command_SignOn(void)
112 {
113 Endpoint_ClearOUT();
114 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
115
116 Endpoint_Write_Byte(CMD_SIGN_ON);
117 Endpoint_Write_Byte(STATUS_CMD_OK);
118 Endpoint_Write_Byte(sizeof(PROGRAMMER_ID) - 1);
119 Endpoint_Write_Stream_LE(PROGRAMMER_ID, (sizeof(PROGRAMMER_ID) - 1));
120 Endpoint_ClearIN();
121 }
122
123 static void V2Protocol_Command_GetSetParam(uint8_t V2Command)
124 {
125 uint8_t ParamID = Endpoint_Read_Byte();
126 uint8_t ParamValue;
127
128 if (V2Command == CMD_SET_PARAMETER)
129 ParamValue = Endpoint_Read_Byte();
130
131 Endpoint_ClearOUT();
132 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
133
134 Endpoint_Write_Byte(V2Command);
135
136 uint8_t ParamPrivs = V2Params_GetParameterPrivellages(ParamID);
137
138 if ((V2Command == CMD_SET_PARAMETER) && (ParamPrivs & PARAM_PRIV_WRITE))
139 {
140 Endpoint_Write_Byte(STATUS_CMD_OK);
141 V2Params_SetParameterValue(ParamID, ParamValue);
142 }
143 else if ((V2Command == CMD_GET_PARAMETER) && (ParamPrivs & PARAM_PRIV_READ))
144 {
145 Endpoint_Write_Byte(STATUS_CMD_OK);
146 Endpoint_Write_Byte(V2Params_GetParameterValue(ParamID));
147 }
148 else
149 {
150 Endpoint_Write_Byte(STATUS_CMD_FAILED);
151 }
152
153 Endpoint_ClearIN();
154 }
155
156 static void V2Protocol_Command_LoadAddress(void)
157 {
158 Endpoint_Read_Stream_LE(&CurrentAddress, sizeof(CurrentAddress));
159
160 Endpoint_ClearOUT();
161 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
162
163 if (CurrentAddress & (1UL << 31))
164 V2Protocol_LoadExtendedAddress();
165
166 Endpoint_Write_Byte(CMD_LOAD_ADDRESS);
167 Endpoint_Write_Byte(STATUS_CMD_OK);
168 Endpoint_ClearIN();
169 }
170
171 static void V2Protocol_Command_EnterISPMode(void)
172 {
173 struct
174 {
175 uint8_t TimeoutMS;
176 uint8_t PinStabDelayMS;
177 uint8_t ExecutionDelayMS;
178 uint8_t SynchLoops;
179 uint8_t ByteDelay;
180 uint8_t PollValue;
181 uint8_t PollIndex;
182 uint8_t EnterProgBytes[4];
183 } Enter_ISP_Params;
184
185 Endpoint_Read_Stream_LE(&Enter_ISP_Params, sizeof(Enter_ISP_Params));
186
187 Endpoint_ClearOUT();
188 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
189
190 uint8_t ResponseStatus = STATUS_CMD_FAILED;
191
192 CurrentAddress = 0;
193
194 V2Protocol_DelayMS(Enter_ISP_Params.ExecutionDelayMS);
195 SPI_Init(V2Protocol_GetSPIPrescalerMask() | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING | SPI_MODE_MASTER);
196
197 while (Enter_ISP_Params.SynchLoops-- && (ResponseStatus == STATUS_CMD_FAILED))
198 {
199 uint8_t ResponseBytes[4];
200
201 V2Protocol_ChangeTargetResetLine(true);
202 V2Protocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
203
204 for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)
205 {
206 V2Protocol_DelayMS(Enter_ISP_Params.ByteDelay);
207 ResponseBytes[RByte] = SPI_TransferByte(Enter_ISP_Params.EnterProgBytes[RByte]);
208 }
209
210 /* Check if polling disabled, or if the polled value matches the expected value */
211 if (!Enter_ISP_Params.PollIndex || (ResponseBytes[Enter_ISP_Params.PollIndex - 1] == Enter_ISP_Params.PollValue))
212 {
213 ResponseStatus = STATUS_CMD_OK;
214 }
215 else
216 {
217 V2Protocol_ChangeTargetResetLine(false);
218 V2Protocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
219 }
220 }
221
222 Endpoint_Write_Byte(CMD_ENTER_PROGMODE_ISP);
223 Endpoint_Write_Byte(ResponseStatus);
224 Endpoint_ClearIN();
225 }
226
227 static void V2Protocol_Command_LeaveISPMode(void)
228 {
229 struct
230 {
231 uint8_t PreDelayMS;
232 uint8_t PostDelayMS;
233 } Leave_ISP_Params;
234
235 Endpoint_Read_Stream_LE(&Leave_ISP_Params, sizeof(Leave_ISP_Params));
236
237 Endpoint_ClearOUT();
238 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
239
240 V2Protocol_DelayMS(Leave_ISP_Params.PreDelayMS);
241 V2Protocol_ChangeTargetResetLine(false);
242 SPI_ShutDown();
243 V2Protocol_DelayMS(Leave_ISP_Params.PostDelayMS);
244
245 Endpoint_Write_Byte(CMD_LEAVE_PROGMODE_ISP);
246 Endpoint_Write_Byte(STATUS_CMD_OK);
247 Endpoint_ClearIN();
248 }
249
250 static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
251 {
252 struct
253 {
254 uint16_t BytesToWrite;
255 uint8_t ProgrammingMode;
256 uint8_t DelayMS;
257 uint8_t ProgrammingCommands[3];
258 uint8_t PollValue1;
259 uint8_t PollValue2;
260 } Write_Memory_Params;
261
262 uint8_t ProgrammingStatus = STATUS_CMD_OK;
263
264 Endpoint_Read_Stream_LE(&Write_Memory_Params, sizeof(Write_Memory_Params));
265 Write_Memory_Params.BytesToWrite = SwapEndian_16(Write_Memory_Params.BytesToWrite);
266
267 if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK)
268 {
269 for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)
270 {
271 if ((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01))
272 Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_ODD_BYTE_MASK;
273
274 SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]);
275 SPI_SendByte(CurrentAddress >> 8);
276 SPI_SendByte(CurrentAddress & 0xFF);
277 SPI_SendByte(Endpoint_Read_Byte());
278
279 // TODO - Correct Polling
280
281 if (((V2Command == CMD_PROGRAM_FLASH_ISP) && (CurrentByte & 0x01)) || (V2Command == CMD_PROGRAM_EEPROM_ISP))
282 CurrentAddress++;
283 }
284
285 /* If the current page must be committed, send the PROGRAM PAGE command to the target */
286 if (Write_Memory_Params.ProgrammingMode & PROG_MODE_COMMIT_PAGE_MASK)
287 {
288 SPI_SendByte(Write_Memory_Params.ProgrammingCommands[1]);
289 SPI_SendByte(CurrentAddress >> 8);
290 SPI_SendByte(CurrentAddress & 0xFF);
291 SPI_SendByte(0x00);
292 }
293 }
294 else
295 {
296 // TODO - Read in programming data, write to device
297 }
298
299 Endpoint_ClearOUT();
300 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
301
302 Endpoint_Write_Byte(V2Command);
303 Endpoint_Write_Byte(ProgrammingStatus);
304
305 Endpoint_ClearIN();
306 }
307
308 static void V2Protocol_Command_ReadMemory(uint8_t V2Command)
309 {
310 struct
311 {
312 uint16_t BytesToRead;
313 uint8_t ReadMemoryCommand;
314 } Read_Memory_Params;
315
316 Endpoint_Read_Stream_LE(&Read_Memory_Params, sizeof(Read_Memory_Params));
317 Read_Memory_Params.BytesToRead = SwapEndian_16(Read_Memory_Params.BytesToRead);
318
319 Endpoint_ClearOUT();
320 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
321
322 Endpoint_Write_Byte(V2Command);
323 Endpoint_Write_Byte(STATUS_CMD_OK);
324
325 for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++)
326 {
327 if ((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01))
328 Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_ODD_BYTE_MASK;
329
330 SPI_SendByte(Read_Memory_Params.ReadMemoryCommand);
331 SPI_SendByte(CurrentAddress >> 8);
332 SPI_SendByte(CurrentAddress & 0xFF);
333 Endpoint_Write_Byte(SPI_ReceiveByte());
334
335 /* Check if the endpoint bank is currently full */
336 if (!(Endpoint_IsReadWriteAllowed()))
337 {
338 Endpoint_ClearIN();
339 Endpoint_WaitUntilReady();
340 }
341
342 if (((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01)) || (V2Command == CMD_READ_EEPROM_ISP))
343 CurrentAddress++;
344 }
345
346 Endpoint_Write_Byte(STATUS_CMD_OK);
347 Endpoint_ClearIN();
348 }
349
350 static void V2Protocol_Command_ChipErase(void)
351 {
352 struct
353 {
354 uint8_t EraseDelayMS;
355 uint8_t PollMethod;
356 uint8_t EraseCommandBytes[4];
357 } Erase_Chip_Params;
358
359 Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params));
360
361 Endpoint_ClearOUT();
362 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
363
364 uint8_t ResponseStatus = STATUS_CMD_OK;
365
366 for (uint8_t SByte = 0; SByte < sizeof(Erase_Chip_Params.EraseCommandBytes); SByte++)
367 SPI_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]);
368
369 if (Erase_Chip_Params.PollMethod == 0)
370 V2Protocol_DelayMS(Erase_Chip_Params.EraseDelayMS);
371 else
372 ResponseStatus = V2Protocol_WaitWhileTargetBusy();
373
374 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP);
375 Endpoint_Write_Byte(ResponseStatus);
376 Endpoint_ClearIN();
377 }
378
379 static void V2Protocol_Command_ReadFuseLockSigOSCCAL(uint8_t V2Command)
380 {
381 struct
382 {
383 uint8_t RetByte;
384 uint8_t ReadCommandBytes[4];
385 } Read_FuseLockSigOSCCAL_Params;
386
387 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params));
388
389 Endpoint_ClearOUT();
390 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
391
392 uint8_t ResponseBytes[4];
393
394 for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)
395 ResponseBytes[RByte] = SPI_TransferByte(Read_FuseLockSigOSCCAL_Params.ReadCommandBytes[RByte]);
396
397 Endpoint_Write_Byte(V2Command);
398 Endpoint_Write_Byte(STATUS_CMD_OK);
399 Endpoint_Write_Byte(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte - 1]);
400 Endpoint_Write_Byte(STATUS_CMD_OK);
401 Endpoint_ClearIN();
402 }
403
404 static void V2Protocol_Command_WriteFuseLock(uint8_t V2Command)
405 {
406 struct
407 {
408 uint8_t WriteCommandBytes[4];
409 } Write_FuseLockSig_Params;
410
411 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params));
412
413 Endpoint_ClearOUT();
414 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
415
416 for (uint8_t SByte = 0; SByte < sizeof(Write_FuseLockSig_Params.WriteCommandBytes); SByte++)
417 SPI_SendByte(Write_FuseLockSig_Params.WriteCommandBytes[SByte]);
418
419 Endpoint_Write_Byte(V2Command);
420 Endpoint_Write_Byte(STATUS_CMD_OK);
421 Endpoint_Write_Byte(STATUS_CMD_OK);
422 Endpoint_ClearIN();
423 }
424
425 static void V2Protocol_Command_SPIMulti(void)
426 {
427 struct
428 {
429 uint8_t TxBytes;
430 uint8_t RxBytes;
431 uint8_t RxStartAddr;
432 uint8_t TxData[255];
433 } SPI_Multi_Params;
434
435 Endpoint_Read_Stream_LE(&SPI_Multi_Params, sizeof(SPI_Multi_Params) -
436 sizeof(SPI_Multi_Params.TxData));
437 Endpoint_Read_Stream_LE(&SPI_Multi_Params.TxData, SPI_Multi_Params.TxBytes);
438
439 Endpoint_ClearOUT();
440 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
441
442 Endpoint_Write_Byte(CMD_SPI_MULTI);
443 Endpoint_Write_Byte(STATUS_CMD_OK);
444
445 uint8_t CurrTxPos = 0;
446 uint8_t CurrRxPos = 0;
447
448 /* Write out bytes to transmit until the start of the bytes to receive is met */
449 while (CurrTxPos < SPI_Multi_Params.RxStartAddr)
450 {
451 if (CurrTxPos < SPI_Multi_Params.TxBytes)
452 SPI_SendByte(SPI_Multi_Params.TxData[CurrTxPos]);
453 else
454 SPI_SendByte(0);
455
456 CurrTxPos++;
457 }
458
459 /* Transmit remaining bytes with padding as needed, read in response bytes */
460 while (CurrRxPos < SPI_Multi_Params.RxBytes)
461 {
462 if (CurrTxPos < SPI_Multi_Params.TxBytes)
463 Endpoint_Write_Byte(SPI_TransferByte(SPI_Multi_Params.TxData[CurrTxPos++]));
464 else
465 Endpoint_Write_Byte(SPI_ReceiveByte());
466
467 CurrRxPos++;
468 }
469
470 Endpoint_Write_Byte(STATUS_CMD_OK);
471 Endpoint_ClearIN();
472 }