cffb91e025eb690f68da23f9c572282ceb871ddd
[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
348 bool EndpointBankFull = Endpoint_IsReadWriteAllowed();
349 Endpoint_ClearIN();
350
351 /* Ensure data transfer is terminated by a short packet if the last sent bank was completely full */
352 if (EndpointBankFull)
353 {
354 Endpoint_WaitUntilReady();
355 Endpoint_ClearIN();
356 }
357 }
358
359 static void V2Protocol_Command_ChipErase(void)
360 {
361 struct
362 {
363 uint8_t EraseDelayMS;
364 uint8_t PollMethod;
365 uint8_t EraseCommandBytes[4];
366 } Erase_Chip_Params;
367
368 Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params));
369
370 Endpoint_ClearOUT();
371 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
372
373 uint8_t ResponseStatus = STATUS_CMD_OK;
374
375 for (uint8_t SByte = 0; SByte < sizeof(Erase_Chip_Params.EraseCommandBytes); SByte++)
376 SPI_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]);
377
378 if (Erase_Chip_Params.PollMethod == 0)
379 V2Protocol_DelayMS(Erase_Chip_Params.EraseDelayMS);
380 else
381 ResponseStatus = V2Protocol_WaitWhileTargetBusy();
382
383 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP);
384 Endpoint_Write_Byte(ResponseStatus);
385 Endpoint_ClearIN();
386 }
387
388 static void V2Protocol_Command_ReadFuseLockSigOSCCAL(uint8_t V2Command)
389 {
390 struct
391 {
392 uint8_t RetByte;
393 uint8_t ReadCommandBytes[4];
394 } Read_FuseLockSigOSCCAL_Params;
395
396 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params));
397
398 Endpoint_ClearOUT();
399 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
400
401 uint8_t ResponseBytes[4];
402
403 for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)
404 ResponseBytes[RByte] = SPI_TransferByte(Read_FuseLockSigOSCCAL_Params.ReadCommandBytes[RByte]);
405
406 Endpoint_Write_Byte(V2Command);
407 Endpoint_Write_Byte(STATUS_CMD_OK);
408 Endpoint_Write_Byte(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte - 1]);
409 Endpoint_Write_Byte(STATUS_CMD_OK);
410 Endpoint_ClearIN();
411 }
412
413 static void V2Protocol_Command_WriteFuseLock(uint8_t V2Command)
414 {
415 struct
416 {
417 uint8_t WriteCommandBytes[4];
418 } Write_FuseLockSig_Params;
419
420 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params));
421
422 Endpoint_ClearOUT();
423 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
424
425 for (uint8_t SByte = 0; SByte < sizeof(Write_FuseLockSig_Params.WriteCommandBytes); SByte++)
426 SPI_SendByte(Write_FuseLockSig_Params.WriteCommandBytes[SByte]);
427
428 Endpoint_Write_Byte(V2Command);
429 Endpoint_Write_Byte(STATUS_CMD_OK);
430 Endpoint_Write_Byte(STATUS_CMD_OK);
431 Endpoint_ClearIN();
432 }
433
434 static void V2Protocol_Command_SPIMulti(void)
435 {
436 struct
437 {
438 uint8_t TxBytes;
439 uint8_t RxBytes;
440 uint8_t RxStartAddr;
441 uint8_t TxData[255];
442 } SPI_Multi_Params;
443
444 Endpoint_Read_Stream_LE(&SPI_Multi_Params, sizeof(SPI_Multi_Params) -
445 sizeof(SPI_Multi_Params.TxData));
446 Endpoint_Read_Stream_LE(&SPI_Multi_Params.TxData, SPI_Multi_Params.TxBytes);
447
448 Endpoint_ClearOUT();
449 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
450
451 Endpoint_Write_Byte(CMD_SPI_MULTI);
452 Endpoint_Write_Byte(STATUS_CMD_OK);
453
454 uint8_t CurrTxPos = 0;
455 uint8_t CurrRxPos = 0;
456
457 /* Write out bytes to transmit until the start of the bytes to receive is met */
458 while (CurrTxPos < SPI_Multi_Params.RxStartAddr)
459 {
460 if (CurrTxPos < SPI_Multi_Params.TxBytes)
461 SPI_SendByte(SPI_Multi_Params.TxData[CurrTxPos]);
462 else
463 SPI_SendByte(0);
464
465 CurrTxPos++;
466 }
467
468 /* Transmit remaining bytes with padding as needed, read in response bytes */
469 while (CurrRxPos < SPI_Multi_Params.RxBytes)
470 {
471 if (CurrTxPos < SPI_Multi_Params.TxBytes)
472 Endpoint_Write_Byte(SPI_TransferByte(SPI_Multi_Params.TxData[CurrTxPos++]));
473 else
474 Endpoint_Write_Byte(SPI_ReceiveByte());
475
476 CurrRxPos++;
477 }
478
479 Endpoint_Write_Byte(STATUS_CMD_OK);
480 Endpoint_ClearIN();
481 }