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
);
350 static void V2Protocol_Command_ChipErase(void)
354 uint8_t EraseDelayMS
;
356 uint8_t EraseCommandBytes
[4];
359 Endpoint_Read_Stream_LE(&Erase_Chip_Params
, sizeof(Erase_Chip_Params
));
362 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
364 uint8_t ResponseStatus
= STATUS_CMD_OK
;
366 for (uint8_t SByte
= 0; SByte
< sizeof(Erase_Chip_Params
.EraseCommandBytes
); SByte
++)
367 SPI_SendByte(Erase_Chip_Params
.EraseCommandBytes
[SByte
]);
369 if (Erase_Chip_Params
.PollMethod
== 0)
370 V2Protocol_DelayMS(Erase_Chip_Params
.EraseDelayMS
);
372 ResponseStatus
= V2Protocol_WaitWhileTargetBusy();
374 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP
);
375 Endpoint_Write_Byte(ResponseStatus
);
379 static void V2Protocol_Command_ReadFuseLockSigOSCCAL(uint8_t V2Command
)
384 uint8_t ReadCommandBytes
[4];
385 } Read_FuseLockSigOSCCAL_Params
;
387 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params
, sizeof(Read_FuseLockSigOSCCAL_Params
));
390 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
392 uint8_t ResponseBytes
[4];
394 for (uint8_t RByte
= 0; RByte
< sizeof(ResponseBytes
); RByte
++)
395 ResponseBytes
[RByte
] = SPI_TransferByte(Read_FuseLockSigOSCCAL_Params
.ReadCommandBytes
[RByte
]);
397 Endpoint_Write_Byte(V2Command
);
398 Endpoint_Write_Byte(STATUS_CMD_OK
);
399 Endpoint_Write_Byte(ResponseBytes
[Read_FuseLockSigOSCCAL_Params
.RetByte
- 1]);
400 Endpoint_Write_Byte(STATUS_CMD_OK
);
404 static void V2Protocol_Command_WriteFuseLock(uint8_t V2Command
)
408 uint8_t WriteCommandBytes
[4];
409 } Write_FuseLockSig_Params
;
411 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params
, sizeof(Write_FuseLockSig_Params
));
414 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
416 for (uint8_t SByte
= 0; SByte
< sizeof(Write_FuseLockSig_Params
.WriteCommandBytes
); SByte
++)
417 SPI_SendByte(Write_FuseLockSig_Params
.WriteCommandBytes
[SByte
]);
419 Endpoint_Write_Byte(V2Command
);
420 Endpoint_Write_Byte(STATUS_CMD_OK
);
421 Endpoint_Write_Byte(STATUS_CMD_OK
);
425 static void V2Protocol_Command_SPIMulti(void)
435 Endpoint_Read_Stream_LE(&SPI_Multi_Params
, sizeof(SPI_Multi_Params
) -
436 sizeof(SPI_Multi_Params
.TxData
));
437 Endpoint_Read_Stream_LE(&SPI_Multi_Params
.TxData
, SPI_Multi_Params
.TxBytes
);
440 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
442 Endpoint_Write_Byte(CMD_SPI_MULTI
);
443 Endpoint_Write_Byte(STATUS_CMD_OK
);
445 uint8_t CurrTxPos
= 0;
446 uint8_t CurrRxPos
= 0;
448 /* Write out bytes to transmit until the start of the bytes to receive is met */
449 while (CurrTxPos
< SPI_Multi_Params
.RxStartAddr
)
451 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
452 SPI_SendByte(SPI_Multi_Params
.TxData
[CurrTxPos
]);
459 /* Transmit remaining bytes with padding as needed, read in response bytes */
460 while (CurrRxPos
< SPI_Multi_Params
.RxBytes
)
462 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
463 Endpoint_Write_Byte(SPI_TransferByte(SPI_Multi_Params
.TxData
[CurrTxPos
++]));
465 Endpoint_Write_Byte(SPI_ReceiveByte());
470 Endpoint_Write_Byte(STATUS_CMD_OK
);