Added PROGRAM_FLASH_ISP and PROGRAM_EEPROM_ISP handler code to the V2 Protocol handle...
[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 Endpoint_Read_Stream_LE(&Write_Memory_Params, sizeof(Write_Memory_Params));
263 Write_Memory_Params.BytesToWrite = SwapEndian_16(Write_Memory_Params.BytesToWrite);
264
265 uint8_t ProgrammingStatus = STATUS_CMD_OK;
266 uint16_t PollAddress = 0;
267
268 if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK)
269 {
270 /* Paged mode memory programming */
271 for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)
272 {
273 bool IsOddByte = (CurrentByte & 0x01);
274 uint8_t ByteToWrite = Endpoint_Read_Byte();
275
276 if ((V2Command == CMD_READ_FLASH_ISP) && IsOddByte)
277 Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_ODD_BYTE_MASK;
278
279 SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]);
280 SPI_SendByte(CurrentAddress >> 8);
281 SPI_SendByte(CurrentAddress & 0xFF);
282 SPI_SendByte(ByteToWrite);
283
284 if (!(PollAddress))
285 {
286 if ((ByteToWrite != Write_Memory_Params.PollValue1) && (V2Command == CMD_PROGRAM_FLASH_ISP))
287 PollAddress = (((CurrentAddress & 0xFFFF) << 1) | IsOddByte);
288 else if ((ByteToWrite != Write_Memory_Params.PollValue2) && (V2Command == CMD_PROGRAM_EEPROM_ISP))
289 PollAddress = (CurrentAddress & 0xFFFF);
290 }
291
292 if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))
293 CurrentAddress++;
294 }
295
296 /* If the current page must be committed, send the PROGRAM PAGE command to the target */
297 if (Write_Memory_Params.ProgrammingMode & PROG_MODE_COMMIT_PAGE_MASK)
298 {
299 SPI_SendByte(Write_Memory_Params.ProgrammingCommands[1]);
300 SPI_SendByte(CurrentAddress >> 8);
301 SPI_SendByte(CurrentAddress & 0xFF);
302 SPI_SendByte(0x00);
303
304 /* Check if polling is possible, if not switch to timed delay mode */
305 if (!(PollAddress))
306 {
307 Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_PAGED_VALUE_MASK;
308 Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_PAGED_TIMEDELAY_MASK;
309 }
310 }
311
312 ProgrammingStatus = V2Protocol_WaitForProgrammingComplete(PollAddress, Write_Memory_Params.ProgrammingMode);
313 }
314 else
315 {
316 /* Word/byte mode memory programming */
317 for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)
318 {
319 bool IsOddByte = (CurrentByte & 0x01);
320 uint8_t ByteToWrite = Endpoint_Read_Byte();
321
322 if ((V2Command == CMD_READ_FLASH_ISP) && IsOddByte)
323 Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_ODD_BYTE_MASK;
324
325 SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]);
326 SPI_SendByte(CurrentAddress >> 8);
327 SPI_SendByte(CurrentAddress & 0xFF);
328 SPI_SendByte(ByteToWrite);
329
330 if ((ByteToWrite != Write_Memory_Params.PollValue1) && (V2Command == CMD_PROGRAM_FLASH_ISP))
331 PollAddress = (((CurrentAddress & 0xFFFF) << 1) | IsOddByte);
332 else if ((ByteToWrite != Write_Memory_Params.PollValue2) && (V2Command == CMD_PROGRAM_EEPROM_ISP))
333 PollAddress = (CurrentAddress & 0xFFFF);
334
335 ProgrammingStatus = V2Protocol_WaitForProgrammingComplete(PollAddress, Write_Memory_Params.ProgrammingMode);
336
337 if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))
338 CurrentAddress++;
339
340 if (ProgrammingStatus != STATUS_CMD_OK)
341 break;
342 }
343 }
344
345 Endpoint_ClearOUT();
346 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
347
348 Endpoint_Write_Byte(V2Command);
349 Endpoint_Write_Byte(ProgrammingStatus);
350
351 Endpoint_ClearIN();
352 }
353
354 static void V2Protocol_Command_ReadMemory(uint8_t V2Command)
355 {
356 struct
357 {
358 uint16_t BytesToRead;
359 uint8_t ReadMemoryCommand;
360 } Read_Memory_Params;
361
362 Endpoint_Read_Stream_LE(&Read_Memory_Params, sizeof(Read_Memory_Params));
363 Read_Memory_Params.BytesToRead = SwapEndian_16(Read_Memory_Params.BytesToRead);
364
365 Endpoint_ClearOUT();
366 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
367
368 Endpoint_Write_Byte(V2Command);
369 Endpoint_Write_Byte(STATUS_CMD_OK);
370
371 for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++)
372 {
373 if ((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01))
374 Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_ODD_BYTE_MASK;
375
376 SPI_SendByte(Read_Memory_Params.ReadMemoryCommand);
377 SPI_SendByte(CurrentAddress >> 8);
378 SPI_SendByte(CurrentAddress & 0xFF);
379 Endpoint_Write_Byte(SPI_ReceiveByte());
380
381 /* Check if the endpoint bank is currently full */
382 if (!(Endpoint_IsReadWriteAllowed()))
383 {
384 Endpoint_ClearIN();
385 Endpoint_WaitUntilReady();
386 }
387
388 if (((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01)) || (V2Command == CMD_READ_EEPROM_ISP))
389 CurrentAddress++;
390 }
391
392 Endpoint_Write_Byte(STATUS_CMD_OK);
393 Endpoint_ClearIN();
394 }
395
396 static void V2Protocol_Command_ChipErase(void)
397 {
398 struct
399 {
400 uint8_t EraseDelayMS;
401 uint8_t PollMethod;
402 uint8_t EraseCommandBytes[4];
403 } Erase_Chip_Params;
404
405 Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params));
406
407 Endpoint_ClearOUT();
408 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
409
410 uint8_t ResponseStatus = STATUS_CMD_OK;
411
412 for (uint8_t SByte = 0; SByte < sizeof(Erase_Chip_Params.EraseCommandBytes); SByte++)
413 SPI_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]);
414
415 if (Erase_Chip_Params.PollMethod == 0)
416 V2Protocol_DelayMS(Erase_Chip_Params.EraseDelayMS);
417 else
418 ResponseStatus = V2Protocol_WaitWhileTargetBusy();
419
420 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP);
421 Endpoint_Write_Byte(ResponseStatus);
422 Endpoint_ClearIN();
423 }
424
425 static void V2Protocol_Command_ReadFuseLockSigOSCCAL(uint8_t V2Command)
426 {
427 struct
428 {
429 uint8_t RetByte;
430 uint8_t ReadCommandBytes[4];
431 } Read_FuseLockSigOSCCAL_Params;
432
433 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params));
434
435 Endpoint_ClearOUT();
436 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
437
438 uint8_t ResponseBytes[4];
439
440 for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)
441 ResponseBytes[RByte] = SPI_TransferByte(Read_FuseLockSigOSCCAL_Params.ReadCommandBytes[RByte]);
442
443 Endpoint_Write_Byte(V2Command);
444 Endpoint_Write_Byte(STATUS_CMD_OK);
445 Endpoint_Write_Byte(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte - 1]);
446 Endpoint_Write_Byte(STATUS_CMD_OK);
447 Endpoint_ClearIN();
448 }
449
450 static void V2Protocol_Command_WriteFuseLock(uint8_t V2Command)
451 {
452 struct
453 {
454 uint8_t WriteCommandBytes[4];
455 } Write_FuseLockSig_Params;
456
457 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params));
458
459 Endpoint_ClearOUT();
460 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
461
462 for (uint8_t SByte = 0; SByte < sizeof(Write_FuseLockSig_Params.WriteCommandBytes); SByte++)
463 SPI_SendByte(Write_FuseLockSig_Params.WriteCommandBytes[SByte]);
464
465 Endpoint_Write_Byte(V2Command);
466 Endpoint_Write_Byte(STATUS_CMD_OK);
467 Endpoint_Write_Byte(STATUS_CMD_OK);
468 Endpoint_ClearIN();
469 }
470
471 static void V2Protocol_Command_SPIMulti(void)
472 {
473 struct
474 {
475 uint8_t TxBytes;
476 uint8_t RxBytes;
477 uint8_t RxStartAddr;
478 uint8_t TxData[255];
479 } SPI_Multi_Params;
480
481 Endpoint_Read_Stream_LE(&SPI_Multi_Params, sizeof(SPI_Multi_Params) -
482 sizeof(SPI_Multi_Params.TxData));
483 Endpoint_Read_Stream_LE(&SPI_Multi_Params.TxData, SPI_Multi_Params.TxBytes);
484
485 Endpoint_ClearOUT();
486 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
487
488 Endpoint_Write_Byte(CMD_SPI_MULTI);
489 Endpoint_Write_Byte(STATUS_CMD_OK);
490
491 uint8_t CurrTxPos = 0;
492 uint8_t CurrRxPos = 0;
493
494 /* Write out bytes to transmit until the start of the bytes to receive is met */
495 while (CurrTxPos < SPI_Multi_Params.RxStartAddr)
496 {
497 if (CurrTxPos < SPI_Multi_Params.TxBytes)
498 SPI_SendByte(SPI_Multi_Params.TxData[CurrTxPos]);
499 else
500 SPI_SendByte(0);
501
502 CurrTxPos++;
503 }
504
505 /* Transmit remaining bytes with padding as needed, read in response bytes */
506 while (CurrRxPos < SPI_Multi_Params.RxBytes)
507 {
508 if (CurrTxPos < SPI_Multi_Params.TxBytes)
509 Endpoint_Write_Byte(SPI_TransferByte(SPI_Multi_Params.TxData[CurrTxPos++]));
510 else
511 Endpoint_Write_Byte(SPI_ReceiveByte());
512
513 CurrRxPos++;
514 }
515
516 Endpoint_Write_Byte(STATUS_CMD_OK);
517 Endpoint_ClearIN();
518 }