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 uint8_t ProgrammingStatus
= STATUS_CMD_OK
;
264 Endpoint_Read_Stream_LE(&Write_Memory_Params
, sizeof(Write_Memory_Params
));
265 Write_Memory_Params
.BytesToWrite
= SwapEndian_16(Write_Memory_Params
.BytesToWrite
);
267 for (uint16_t CurrentByte
= 0; CurrentByte
< Write_Memory_Params
.BytesToWrite
; CurrentByte
++)
269 // TODO - Read in programming data, write to device
273 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
275 Endpoint_Write_Byte(V2Command
);
276 Endpoint_Write_Byte(ProgrammingStatus
);
281 static void V2Protocol_Command_ReadMemory(uint8_t V2Command
)
285 uint16_t BytesToRead
;
286 uint8_t ReadMemoryCommand
;
287 } Read_Memory_Params
;
289 Endpoint_Read_Stream_LE(&Read_Memory_Params
, sizeof(Read_Memory_Params
));
290 Read_Memory_Params
.BytesToRead
= SwapEndian_16(Read_Memory_Params
.BytesToRead
);
293 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
295 Endpoint_Write_Byte(V2Command
);
296 Endpoint_Write_Byte(STATUS_CMD_OK
);
298 for (uint16_t CurrentByte
= 0; CurrentByte
< Read_Memory_Params
.BytesToRead
; CurrentByte
++)
300 if ((V2Command
== CMD_READ_FLASH_ISP
) && (CurrentByte
& 0x01))
301 Read_Memory_Params
.ReadMemoryCommand
^= READ_WRITE_ODD_BYTE_MASK
;
303 SPI_SendByte(Read_Memory_Params
.ReadMemoryCommand
);
304 SPI_SendByte(CurrentAddress
>> 8);
305 SPI_SendByte(CurrentAddress
& 0xFF);
306 Endpoint_Write_Byte(SPI_ReceiveByte());
308 /* Check if the endpoint bank is currently full */
309 if (!(Endpoint_IsReadWriteAllowed()))
312 Endpoint_WaitUntilReady();
318 Endpoint_Write_Byte(STATUS_CMD_OK
);
320 bool EndpointBankFull
= Endpoint_IsReadWriteAllowed();
323 /* Ensure data transfer is terminated by a short packet if the last sent bank was completely full */
324 if (EndpointBankFull
)
326 Endpoint_WaitUntilReady();
331 static void V2Protocol_Command_ChipErase(void)
335 uint8_t EraseDelayMS
;
337 uint8_t EraseCommandBytes
[4];
340 Endpoint_Read_Stream_LE(&Erase_Chip_Params
, sizeof(Erase_Chip_Params
));
343 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
345 uint8_t ResponseStatus
= STATUS_CMD_OK
;
347 for (uint8_t SByte
= 0; SByte
< sizeof(Erase_Chip_Params
.EraseCommandBytes
); SByte
++)
348 SPI_SendByte(Erase_Chip_Params
.EraseCommandBytes
[SByte
]);
350 if (Erase_Chip_Params
.PollMethod
== 0)
351 V2Protocol_DelayMS(Erase_Chip_Params
.EraseDelayMS
);
353 ResponseStatus
= V2Protocol_WaitWhileTargetBusy();
355 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP
);
356 Endpoint_Write_Byte(ResponseStatus
);
360 static void V2Protocol_Command_ReadFuseLockSigOSCCAL(uint8_t V2Command
)
365 uint8_t ReadCommandBytes
[4];
366 } Read_FuseLockSigOSCCAL_Params
;
368 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params
, sizeof(Read_FuseLockSigOSCCAL_Params
));
371 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
373 uint8_t ResponseBytes
[4];
375 for (uint8_t RByte
= 0; RByte
< sizeof(ResponseBytes
); RByte
++)
376 ResponseBytes
[RByte
] = SPI_TransferByte(Read_FuseLockSigOSCCAL_Params
.ReadCommandBytes
[RByte
]);
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
);
385 static void V2Protocol_Command_WriteFuseLock(uint8_t V2Command
)
389 uint8_t WriteCommandBytes
[4];
390 } Write_FuseLockSig_Params
;
392 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params
, sizeof(Write_FuseLockSig_Params
));
395 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
397 for (uint8_t SByte
= 0; SByte
< sizeof(Write_FuseLockSig_Params
.WriteCommandBytes
); SByte
++)
398 SPI_SendByte(Write_FuseLockSig_Params
.WriteCommandBytes
[SByte
]);
400 Endpoint_Write_Byte(V2Command
);
401 Endpoint_Write_Byte(STATUS_CMD_OK
);
402 Endpoint_Write_Byte(STATUS_CMD_OK
);
406 static void V2Protocol_Command_SPIMulti(void)
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
);
421 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
423 Endpoint_Write_Byte(CMD_SPI_MULTI
);
424 Endpoint_Write_Byte(STATUS_CMD_OK
);
426 uint8_t CurrTxPos
= 0;
427 uint8_t CurrRxPos
= 0;
429 /* Write out bytes to transmit until the start of the bytes to receive is met */
430 while (CurrTxPos
< SPI_Multi_Params
.RxStartAddr
)
432 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
433 SPI_SendByte(SPI_Multi_Params
.TxData
[CurrTxPos
]);
440 /* Transmit remaining bytes with padding as needed, read in response bytes */
441 while (CurrRxPos
< SPI_Multi_Params
.RxBytes
)
443 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
444 Endpoint_Write_Byte(SPI_TransferByte(SPI_Multi_Params
.TxData
[CurrTxPos
++]));
446 Endpoint_Write_Byte(SPI_ReceiveByte());
451 Endpoint_Write_Byte(STATUS_CMD_OK
);