3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
33 * V2Protocol handler, to process V2 Protocol commands used in Atmel programmer devices.
36 #define INCLUDE_FROM_V2PROTOCOL_C
37 #include "V2Protocol.h"
39 void V2Protocol_ProcessCommand(void)
41 uint8_t V2Command
= Endpoint_Read_Byte();
46 V2Protocol_Command_SignOn();
48 case CMD_SET_PARAMETER
:
49 case CMD_GET_PARAMETER
:
50 V2Protocol_Command_GetSetParam(V2Command
);
52 case CMD_LOAD_ADDRESS
:
53 V2Protocol_Command_LoadAddress();
55 case CMD_ENTER_PROGMODE_ISP
:
56 V2Protocol_Command_EnterISPMode();
58 case CMD_LEAVE_PROGMODE_ISP
:
59 V2Protocol_Command_LeaveISPMode();
61 case CMD_PROGRAM_FLASH_ISP
:
62 case CMD_PROGRAM_EEPROM_ISP
:
63 V2Protocol_Command_ProgramMemory(V2Command
);
65 case CMD_READ_FLASH_ISP
:
66 case CMD_READ_EEPROM_ISP
:
67 V2Protocol_Command_ReadMemory(V2Command
);
69 case CMD_CHIP_ERASE_ISP
:
70 V2Protocol_Command_ChipErase();
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
);
78 case CMD_PROGRAM_FUSE_ISP
:
79 case CMD_PROGRAM_LOCK_ISP
:
80 V2Protocol_Command_WriteFuseLock(V2Command
);
83 V2Protocol_Command_SPIMulti();
86 V2Protocol_Command_Unknown(V2Command
);
90 Endpoint_WaitUntilReady();
91 Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT
);
94 static void V2Protocol_Command_Unknown(uint8_t V2Command
)
96 /* Discard all incomming data */
97 while (Endpoint_BytesInEndpoint() == AVRISP_DATA_EPSIZE
)
100 Endpoint_WaitUntilReady();
104 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
106 Endpoint_Write_Byte(V2Command
);
107 Endpoint_Write_Byte(STATUS_CMD_UNKNOWN
);
111 static void V2Protocol_Command_SignOn(void)
114 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
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));
123 static void V2Protocol_Command_GetSetParam(uint8_t V2Command
)
125 uint8_t ParamID
= Endpoint_Read_Byte();
128 if (V2Command
== CMD_SET_PARAMETER
)
129 ParamValue
= Endpoint_Read_Byte();
132 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
134 Endpoint_Write_Byte(V2Command
);
136 uint8_t ParamPrivs
= V2Params_GetParameterPrivellages(ParamID
);
138 if ((V2Command
== CMD_SET_PARAMETER
) && (ParamPrivs
& PARAM_PRIV_WRITE
))
140 Endpoint_Write_Byte(STATUS_CMD_OK
);
141 V2Params_SetParameterValue(ParamID
, ParamValue
);
143 else if ((V2Command
== CMD_GET_PARAMETER
) && (ParamPrivs
& PARAM_PRIV_READ
))
145 Endpoint_Write_Byte(STATUS_CMD_OK
);
146 Endpoint_Write_Byte(V2Params_GetParameterValue(ParamID
));
150 Endpoint_Write_Byte(STATUS_CMD_FAILED
);
156 static void V2Protocol_Command_LoadAddress(void)
158 Endpoint_Read_Stream_LE(&CurrentAddress
, sizeof(CurrentAddress
));
161 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
163 if (CurrentAddress
& (1UL << 31))
164 V2Protocol_LoadExtendedAddress();
166 Endpoint_Write_Byte(CMD_LOAD_ADDRESS
);
167 Endpoint_Write_Byte(STATUS_CMD_OK
);
171 static void V2Protocol_Command_EnterISPMode(void)
176 uint8_t PinStabDelayMS
;
177 uint8_t ExecutionDelayMS
;
182 uint8_t EnterProgBytes
[4];
185 Endpoint_Read_Stream_LE(&Enter_ISP_Params
, sizeof(Enter_ISP_Params
));
188 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
190 uint8_t ResponseStatus
= STATUS_CMD_FAILED
;
194 V2Protocol_DelayMS(Enter_ISP_Params
.ExecutionDelayMS
);
195 SPI_Init(V2Protocol_GetSPIPrescalerMask() | SPI_SCK_LEAD_RISING
| SPI_SAMPLE_LEADING
| SPI_MODE_MASTER
);
197 while (Enter_ISP_Params
.SynchLoops
-- && (ResponseStatus
== STATUS_CMD_FAILED
))
199 uint8_t ResponseBytes
[4];
201 V2Protocol_ChangeTargetResetLine(true);
202 V2Protocol_DelayMS(Enter_ISP_Params
.PinStabDelayMS
);
204 for (uint8_t RByte
= 0; RByte
< sizeof(ResponseBytes
); RByte
++)
206 V2Protocol_DelayMS(Enter_ISP_Params
.ByteDelay
);
207 ResponseBytes
[RByte
] = SPI_TransferByte(Enter_ISP_Params
.EnterProgBytes
[RByte
]);
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
))
213 ResponseStatus
= STATUS_CMD_OK
;
217 V2Protocol_ChangeTargetResetLine(false);
218 V2Protocol_DelayMS(Enter_ISP_Params
.PinStabDelayMS
);
222 Endpoint_Write_Byte(CMD_ENTER_PROGMODE_ISP
);
223 Endpoint_Write_Byte(ResponseStatus
);
227 static void V2Protocol_Command_LeaveISPMode(void)
235 Endpoint_Read_Stream_LE(&Leave_ISP_Params
, sizeof(Leave_ISP_Params
));
238 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
240 V2Protocol_DelayMS(Leave_ISP_Params
.PreDelayMS
);
241 V2Protocol_ChangeTargetResetLine(false);
243 V2Protocol_DelayMS(Leave_ISP_Params
.PostDelayMS
);
245 Endpoint_Write_Byte(CMD_LEAVE_PROGMODE_ISP
);
246 Endpoint_Write_Byte(STATUS_CMD_OK
);
250 static void V2Protocol_Command_ProgramMemory(uint8_t V2Command
)
254 uint16_t BytesToWrite
;
255 uint8_t ProgrammingMode
;
257 uint8_t ProgrammingCommands
[3];
260 } Write_Memory_Params
;
262 Endpoint_Read_Stream_LE(&Write_Memory_Params
, sizeof(Write_Memory_Params
));
263 Write_Memory_Params
.BytesToWrite
= SwapEndian_16(Write_Memory_Params
.BytesToWrite
);
265 uint8_t ProgrammingStatus
= STATUS_CMD_OK
;
266 uint16_t PollAddress
= 0;
268 if (Write_Memory_Params
.ProgrammingMode
& PROG_MODE_PAGED_WRITES_MASK
)
270 /* Paged mode memory programming */
271 for (uint16_t CurrentByte
= 0; CurrentByte
< Write_Memory_Params
.BytesToWrite
; CurrentByte
++)
273 bool IsOddByte
= (CurrentByte
& 0x01);
274 uint8_t ByteToWrite
= Endpoint_Read_Byte();
276 if ((V2Command
== CMD_READ_FLASH_ISP
) && IsOddByte
)
277 Write_Memory_Params
.ProgrammingCommands
[0] ^= READ_WRITE_ODD_BYTE_MASK
;
279 SPI_SendByte(Write_Memory_Params
.ProgrammingCommands
[0]);
280 SPI_SendByte(CurrentAddress
>> 8);
281 SPI_SendByte(CurrentAddress
& 0xFF);
282 SPI_SendByte(ByteToWrite
);
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);
292 if (IsOddByte
|| (V2Command
== CMD_PROGRAM_EEPROM_ISP
))
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
)
299 SPI_SendByte(Write_Memory_Params
.ProgrammingCommands
[1]);
300 SPI_SendByte(CurrentAddress
>> 8);
301 SPI_SendByte(CurrentAddress
& 0xFF);
304 /* Check if polling is possible, if not switch to timed delay mode */
307 Write_Memory_Params
.ProgrammingMode
&= ~PROG_MODE_PAGED_VALUE_MASK
;
308 Write_Memory_Params
.ProgrammingMode
&= ~PROG_MODE_PAGED_TIMEDELAY_MASK
;
312 ProgrammingStatus
= V2Protocol_WaitForProgrammingComplete(PollAddress
, Write_Memory_Params
.ProgrammingMode
);
316 /* Word/byte mode memory programming */
317 for (uint16_t CurrentByte
= 0; CurrentByte
< Write_Memory_Params
.BytesToWrite
; CurrentByte
++)
319 bool IsOddByte
= (CurrentByte
& 0x01);
320 uint8_t ByteToWrite
= Endpoint_Read_Byte();
322 if ((V2Command
== CMD_READ_FLASH_ISP
) && IsOddByte
)
323 Write_Memory_Params
.ProgrammingCommands
[0] ^= READ_WRITE_ODD_BYTE_MASK
;
325 SPI_SendByte(Write_Memory_Params
.ProgrammingCommands
[0]);
326 SPI_SendByte(CurrentAddress
>> 8);
327 SPI_SendByte(CurrentAddress
& 0xFF);
328 SPI_SendByte(ByteToWrite
);
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);
335 ProgrammingStatus
= V2Protocol_WaitForProgrammingComplete(PollAddress
, Write_Memory_Params
.ProgrammingMode
);
337 if (IsOddByte
|| (V2Command
== CMD_PROGRAM_EEPROM_ISP
))
340 if (ProgrammingStatus
!= STATUS_CMD_OK
)
346 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
348 Endpoint_Write_Byte(V2Command
);
349 Endpoint_Write_Byte(ProgrammingStatus
);
354 static void V2Protocol_Command_ReadMemory(uint8_t V2Command
)
358 uint16_t BytesToRead
;
359 uint8_t ReadMemoryCommand
;
360 } Read_Memory_Params
;
362 Endpoint_Read_Stream_LE(&Read_Memory_Params
, sizeof(Read_Memory_Params
));
363 Read_Memory_Params
.BytesToRead
= SwapEndian_16(Read_Memory_Params
.BytesToRead
);
366 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
368 Endpoint_Write_Byte(V2Command
);
369 Endpoint_Write_Byte(STATUS_CMD_OK
);
371 for (uint16_t CurrentByte
= 0; CurrentByte
< Read_Memory_Params
.BytesToRead
; CurrentByte
++)
373 if ((V2Command
== CMD_READ_FLASH_ISP
) && (CurrentByte
& 0x01))
374 Read_Memory_Params
.ReadMemoryCommand
^= READ_WRITE_ODD_BYTE_MASK
;
376 SPI_SendByte(Read_Memory_Params
.ReadMemoryCommand
);
377 SPI_SendByte(CurrentAddress
>> 8);
378 SPI_SendByte(CurrentAddress
& 0xFF);
379 Endpoint_Write_Byte(SPI_ReceiveByte());
381 /* Check if the endpoint bank is currently full */
382 if (!(Endpoint_IsReadWriteAllowed()))
385 Endpoint_WaitUntilReady();
388 if (((V2Command
== CMD_READ_FLASH_ISP
) && (CurrentByte
& 0x01)) || (V2Command
== CMD_READ_EEPROM_ISP
))
392 Endpoint_Write_Byte(STATUS_CMD_OK
);
396 static void V2Protocol_Command_ChipErase(void)
400 uint8_t EraseDelayMS
;
402 uint8_t EraseCommandBytes
[4];
405 Endpoint_Read_Stream_LE(&Erase_Chip_Params
, sizeof(Erase_Chip_Params
));
408 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
410 uint8_t ResponseStatus
= STATUS_CMD_OK
;
412 for (uint8_t SByte
= 0; SByte
< sizeof(Erase_Chip_Params
.EraseCommandBytes
); SByte
++)
413 SPI_SendByte(Erase_Chip_Params
.EraseCommandBytes
[SByte
]);
415 if (Erase_Chip_Params
.PollMethod
== 0)
416 V2Protocol_DelayMS(Erase_Chip_Params
.EraseDelayMS
);
418 ResponseStatus
= V2Protocol_WaitWhileTargetBusy();
420 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP
);
421 Endpoint_Write_Byte(ResponseStatus
);
425 static void V2Protocol_Command_ReadFuseLockSigOSCCAL(uint8_t V2Command
)
430 uint8_t ReadCommandBytes
[4];
431 } Read_FuseLockSigOSCCAL_Params
;
433 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params
, sizeof(Read_FuseLockSigOSCCAL_Params
));
436 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
438 uint8_t ResponseBytes
[4];
440 for (uint8_t RByte
= 0; RByte
< sizeof(ResponseBytes
); RByte
++)
441 ResponseBytes
[RByte
] = SPI_TransferByte(Read_FuseLockSigOSCCAL_Params
.ReadCommandBytes
[RByte
]);
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
);
450 static void V2Protocol_Command_WriteFuseLock(uint8_t V2Command
)
454 uint8_t WriteCommandBytes
[4];
455 } Write_FuseLockSig_Params
;
457 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params
, sizeof(Write_FuseLockSig_Params
));
460 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
462 for (uint8_t SByte
= 0; SByte
< sizeof(Write_FuseLockSig_Params
.WriteCommandBytes
); SByte
++)
463 SPI_SendByte(Write_FuseLockSig_Params
.WriteCommandBytes
[SByte
]);
465 Endpoint_Write_Byte(V2Command
);
466 Endpoint_Write_Byte(STATUS_CMD_OK
);
467 Endpoint_Write_Byte(STATUS_CMD_OK
);
471 static void V2Protocol_Command_SPIMulti(void)
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
);
486 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
488 Endpoint_Write_Byte(CMD_SPI_MULTI
);
489 Endpoint_Write_Byte(STATUS_CMD_OK
);
491 uint8_t CurrTxPos
= 0;
492 uint8_t CurrRxPos
= 0;
494 /* Write out bytes to transmit until the start of the bytes to receive is met */
495 while (CurrTxPos
< SPI_Multi_Params
.RxStartAddr
)
497 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
498 SPI_SendByte(SPI_Multi_Params
.TxData
[CurrTxPos
]);
505 /* Transmit remaining bytes with padding as needed, read in response bytes */
506 while (CurrRxPos
< SPI_Multi_Params
.RxBytes
)
508 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
509 Endpoint_Write_Byte(SPI_TransferByte(SPI_Multi_Params
.TxData
[CurrTxPos
++]));
511 Endpoint_Write_Byte(SPI_ReceiveByte());
516 Endpoint_Write_Byte(STATUS_CMD_OK
);