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 if (Write_Memory_Params
.ProgrammingMode
& PROG_MODE_PAGED_WRITES_MASK
)
269 for (uint16_t CurrentByte
= 0; CurrentByte
< Write_Memory_Params
.BytesToWrite
; CurrentByte
++)
271 if ((V2Command
== CMD_READ_FLASH_ISP
) && (CurrentByte
& 0x01))
272 Write_Memory_Params
.ProgrammingCommands
[0] ^= READ_WRITE_ODD_BYTE_MASK
;
274 SPI_SendByte(Write_Memory_Params
.ProgrammingCommands
[0]);
275 SPI_SendByte(CurrentAddress
>> 8);
276 SPI_SendByte(CurrentAddress
& 0xFF);
277 SPI_SendByte(Endpoint_Read_Byte());
279 // TODO - Correct Polling
281 if (((V2Command
== CMD_PROGRAM_FLASH_ISP
) && (CurrentByte
& 0x01)) || (V2Command
== CMD_PROGRAM_EEPROM_ISP
))
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
)
288 SPI_SendByte(Write_Memory_Params
.ProgrammingCommands
[1]);
289 SPI_SendByte(CurrentAddress
>> 8);
290 SPI_SendByte(CurrentAddress
& 0xFF);
296 // TODO - Read in programming data, write to device
300 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
302 Endpoint_Write_Byte(V2Command
);
303 Endpoint_Write_Byte(ProgrammingStatus
);
308 static void V2Protocol_Command_ReadMemory(uint8_t V2Command
)
312 uint16_t BytesToRead
;
313 uint8_t ReadMemoryCommand
;
314 } Read_Memory_Params
;
316 Endpoint_Read_Stream_LE(&Read_Memory_Params
, sizeof(Read_Memory_Params
));
317 Read_Memory_Params
.BytesToRead
= SwapEndian_16(Read_Memory_Params
.BytesToRead
);
320 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
322 Endpoint_Write_Byte(V2Command
);
323 Endpoint_Write_Byte(STATUS_CMD_OK
);
325 for (uint16_t CurrentByte
= 0; CurrentByte
< Read_Memory_Params
.BytesToRead
; CurrentByte
++)
327 if ((V2Command
== CMD_READ_FLASH_ISP
) && (CurrentByte
& 0x01))
328 Read_Memory_Params
.ReadMemoryCommand
^= READ_WRITE_ODD_BYTE_MASK
;
330 SPI_SendByte(Read_Memory_Params
.ReadMemoryCommand
);
331 SPI_SendByte(CurrentAddress
>> 8);
332 SPI_SendByte(CurrentAddress
& 0xFF);
333 Endpoint_Write_Byte(SPI_ReceiveByte());
335 /* Check if the endpoint bank is currently full */
336 if (!(Endpoint_IsReadWriteAllowed()))
339 Endpoint_WaitUntilReady();
342 if (((V2Command
== CMD_READ_FLASH_ISP
) && (CurrentByte
& 0x01)) || (V2Command
== CMD_READ_EEPROM_ISP
))
346 Endpoint_Write_Byte(STATUS_CMD_OK
);
348 bool EndpointBankFull
= Endpoint_IsReadWriteAllowed();
351 /* Ensure data transfer is terminated by a short packet if the last sent bank was completely full */
352 if (EndpointBankFull
)
354 Endpoint_WaitUntilReady();
359 static void V2Protocol_Command_ChipErase(void)
363 uint8_t EraseDelayMS
;
365 uint8_t EraseCommandBytes
[4];
368 Endpoint_Read_Stream_LE(&Erase_Chip_Params
, sizeof(Erase_Chip_Params
));
371 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
373 uint8_t ResponseStatus
= STATUS_CMD_OK
;
375 for (uint8_t SByte
= 0; SByte
< sizeof(Erase_Chip_Params
.EraseCommandBytes
); SByte
++)
376 SPI_SendByte(Erase_Chip_Params
.EraseCommandBytes
[SByte
]);
378 if (Erase_Chip_Params
.PollMethod
== 0)
379 V2Protocol_DelayMS(Erase_Chip_Params
.EraseDelayMS
);
381 ResponseStatus
= V2Protocol_WaitWhileTargetBusy();
383 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP
);
384 Endpoint_Write_Byte(ResponseStatus
);
388 static void V2Protocol_Command_ReadFuseLockSigOSCCAL(uint8_t V2Command
)
393 uint8_t ReadCommandBytes
[4];
394 } Read_FuseLockSigOSCCAL_Params
;
396 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params
, sizeof(Read_FuseLockSigOSCCAL_Params
));
399 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
401 uint8_t ResponseBytes
[4];
403 for (uint8_t RByte
= 0; RByte
< sizeof(ResponseBytes
); RByte
++)
404 ResponseBytes
[RByte
] = SPI_TransferByte(Read_FuseLockSigOSCCAL_Params
.ReadCommandBytes
[RByte
]);
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
);
413 static void V2Protocol_Command_WriteFuseLock(uint8_t V2Command
)
417 uint8_t WriteCommandBytes
[4];
418 } Write_FuseLockSig_Params
;
420 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params
, sizeof(Write_FuseLockSig_Params
));
423 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
425 for (uint8_t SByte
= 0; SByte
< sizeof(Write_FuseLockSig_Params
.WriteCommandBytes
); SByte
++)
426 SPI_SendByte(Write_FuseLockSig_Params
.WriteCommandBytes
[SByte
]);
428 Endpoint_Write_Byte(V2Command
);
429 Endpoint_Write_Byte(STATUS_CMD_OK
);
430 Endpoint_Write_Byte(STATUS_CMD_OK
);
434 static void V2Protocol_Command_SPIMulti(void)
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
);
449 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
451 Endpoint_Write_Byte(CMD_SPI_MULTI
);
452 Endpoint_Write_Byte(STATUS_CMD_OK
);
454 uint8_t CurrTxPos
= 0;
455 uint8_t CurrRxPos
= 0;
457 /* Write out bytes to transmit until the start of the bytes to receive is met */
458 while (CurrTxPos
< SPI_Multi_Params
.RxStartAddr
)
460 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
461 SPI_SendByte(SPI_Multi_Params
.TxData
[CurrTxPos
]);
468 /* Transmit remaining bytes with padding as needed, read in response bytes */
469 while (CurrRxPos
< SPI_Multi_Params
.RxBytes
)
471 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
472 Endpoint_Write_Byte(SPI_TransferByte(SPI_Multi_Params
.TxData
[CurrTxPos
++]));
474 Endpoint_Write_Byte(SPI_ReceiveByte());
479 Endpoint_Write_Byte(STATUS_CMD_OK
);