Added V2 protocol handlers for the READ and WRITE memory commands in the AVRISP proje...
[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 for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)
268 {
269 // TODO - Read in programming data, write to device
270 }
271
272 Endpoint_ClearOUT();
273 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
274
275 Endpoint_Write_Byte(V2Command);
276 Endpoint_Write_Byte(ProgrammingStatus);
277
278 Endpoint_ClearIN();
279 }
280
281 static void V2Protocol_Command_ReadMemory(uint8_t V2Command)
282 {
283 struct
284 {
285 uint16_t BytesToRead;
286 uint8_t ReadMemoryCommand;
287 } Read_Memory_Params;
288
289 Endpoint_Read_Stream_LE(&Read_Memory_Params, sizeof(Read_Memory_Params));
290 Read_Memory_Params.BytesToRead = SwapEndian_16(Read_Memory_Params.BytesToRead);
291
292 Endpoint_ClearOUT();
293 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
294
295 Endpoint_Write_Byte(V2Command);
296 Endpoint_Write_Byte(STATUS_CMD_OK);
297
298 for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++)
299 {
300 if ((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01))
301 Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_ODD_BYTE_MASK;
302
303 SPI_SendByte(Read_Memory_Params.ReadMemoryCommand);
304 SPI_SendByte(CurrentAddress >> 8);
305 SPI_SendByte(CurrentAddress & 0xFF);
306 Endpoint_Write_Byte(SPI_ReceiveByte());
307
308 /* Check if the endpoint bank is currently full */
309 if (!(Endpoint_IsReadWriteAllowed()))
310 {
311 Endpoint_ClearIN();
312 Endpoint_WaitUntilReady();
313 }
314
315 CurrentAddress++;
316 }
317
318 Endpoint_Write_Byte(STATUS_CMD_OK);
319
320 bool EndpointBankFull = Endpoint_IsReadWriteAllowed();
321 Endpoint_ClearIN();
322
323 /* Ensure data transfer is terminated by a short packet if the last sent bank was completely full */
324 if (EndpointBankFull)
325 {
326 Endpoint_WaitUntilReady();
327 Endpoint_ClearIN();
328 }
329 }
330
331 static void V2Protocol_Command_ChipErase(void)
332 {
333 struct
334 {
335 uint8_t EraseDelayMS;
336 uint8_t PollMethod;
337 uint8_t EraseCommandBytes[4];
338 } Erase_Chip_Params;
339
340 Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params));
341
342 Endpoint_ClearOUT();
343 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
344
345 uint8_t ResponseStatus = STATUS_CMD_OK;
346
347 for (uint8_t SByte = 0; SByte < sizeof(Erase_Chip_Params.EraseCommandBytes); SByte++)
348 SPI_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]);
349
350 if (Erase_Chip_Params.PollMethod == 0)
351 V2Protocol_DelayMS(Erase_Chip_Params.EraseDelayMS);
352 else
353 ResponseStatus = V2Protocol_WaitWhileTargetBusy();
354
355 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP);
356 Endpoint_Write_Byte(ResponseStatus);
357 Endpoint_ClearIN();
358 }
359
360 static void V2Protocol_Command_ReadFuseLockSigOSCCAL(uint8_t V2Command)
361 {
362 struct
363 {
364 uint8_t RetByte;
365 uint8_t ReadCommandBytes[4];
366 } Read_FuseLockSigOSCCAL_Params;
367
368 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params));
369
370 Endpoint_ClearOUT();
371 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
372
373 uint8_t ResponseBytes[4];
374
375 for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)
376 ResponseBytes[RByte] = SPI_TransferByte(Read_FuseLockSigOSCCAL_Params.ReadCommandBytes[RByte]);
377
378 Endpoint_Write_Byte(V2Command);
379 Endpoint_Write_Byte(STATUS_CMD_OK);
380 Endpoint_Write_Byte(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte]);
381 Endpoint_Write_Byte(STATUS_CMD_OK);
382 Endpoint_ClearIN();
383 }
384
385 static void V2Protocol_Command_WriteFuseLock(uint8_t V2Command)
386 {
387 struct
388 {
389 uint8_t WriteCommandBytes[4];
390 } Write_FuseLockSig_Params;
391
392 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params));
393
394 Endpoint_ClearOUT();
395 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
396
397 for (uint8_t SByte = 0; SByte < sizeof(Write_FuseLockSig_Params.WriteCommandBytes); SByte++)
398 SPI_SendByte(Write_FuseLockSig_Params.WriteCommandBytes[SByte]);
399
400 Endpoint_Write_Byte(V2Command);
401 Endpoint_Write_Byte(STATUS_CMD_OK);
402 Endpoint_Write_Byte(STATUS_CMD_OK);
403 Endpoint_ClearIN();
404 }
405
406 static void V2Protocol_Command_SPIMulti(void)
407 {
408 struct
409 {
410 uint8_t TxBytes;
411 uint8_t RxBytes;
412 uint8_t RxStartAddr;
413 uint8_t TxData[255];
414 } SPI_Multi_Params;
415
416 Endpoint_Read_Stream_LE(&SPI_Multi_Params, sizeof(SPI_Multi_Params) -
417 sizeof(SPI_Multi_Params.TxData));
418 Endpoint_Read_Stream_LE(&SPI_Multi_Params.TxData, SPI_Multi_Params.TxBytes);
419
420 Endpoint_ClearOUT();
421 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
422
423 Endpoint_Write_Byte(CMD_SPI_MULTI);
424 Endpoint_Write_Byte(STATUS_CMD_OK);
425
426 uint8_t CurrTxPos = 0;
427 uint8_t CurrRxPos = 0;
428
429 /* Write out bytes to transmit until the start of the bytes to receive is met */
430 while (CurrTxPos < SPI_Multi_Params.RxStartAddr)
431 {
432 if (CurrTxPos < SPI_Multi_Params.TxBytes)
433 SPI_SendByte(SPI_Multi_Params.TxData[CurrTxPos]);
434 else
435 SPI_SendByte(0);
436
437 CurrTxPos++;
438 }
439
440 /* Transmit remaining bytes with padding as needed, read in response bytes */
441 while (CurrRxPos < SPI_Multi_Params.RxBytes)
442 {
443 if (CurrTxPos < SPI_Multi_Params.TxBytes)
444 Endpoint_Write_Byte(SPI_TransferByte(SPI_Multi_Params.TxData[CurrTxPos++]));
445 else
446 Endpoint_Write_Byte(SPI_ReceiveByte());
447
448 CurrRxPos++;
449 }
450
451 Endpoint_Write_Byte(STATUS_CMD_OK);
452 Endpoint_ClearIN();
453 }