3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 * ISP Protocol handler, to process V2 Protocol wrapped ISP commands used in Atmel programmer devices.
36 #include "ISPProtocol.h"
38 #if defined(ENABLE_ISP_PROTOCOL) || defined(__DOXYGEN__)
40 /** Handler for the CMD_ENTER_PROGMODE_ISP command, which attempts to enter programming mode on
41 * the attached device, returning success or failure back to the host.
43 void ISPProtocol_EnterISPMode(void)
48 uint8_t PinStabDelayMS
;
49 uint8_t ExecutionDelayMS
;
54 uint8_t EnterProgBytes
[4];
57 Endpoint_Read_Stream_LE(&Enter_ISP_Params
, sizeof(Enter_ISP_Params
), NO_STREAM_CALLBACK
);
60 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
62 uint8_t ResponseStatus
= STATUS_CMD_FAILED
;
66 ISPProtocol_DelayMS(Enter_ISP_Params
.ExecutionDelayMS
);
67 SPI_Init(ISPTarget_GetSPIPrescalerMask() | SPI_SCK_LEAD_RISING
| SPI_SAMPLE_LEADING
| SPI_MODE_MASTER
);
69 /* Continuously attempt to synchronize with the target until either the number of attempts specified
70 * by the host has exceeded, or the the device sends back the expected response values */
71 while (Enter_ISP_Params
.SynchLoops
-- && (ResponseStatus
== STATUS_CMD_FAILED
))
73 uint8_t ResponseBytes
[4];
75 ISPTarget_ChangeTargetResetLine(true);
76 ISPProtocol_DelayMS(Enter_ISP_Params
.PinStabDelayMS
);
78 for (uint8_t RByte
= 0; RByte
< sizeof(ResponseBytes
); RByte
++)
80 ISPProtocol_DelayMS(Enter_ISP_Params
.ByteDelay
);
81 ResponseBytes
[RByte
] = SPI_TransferByte(Enter_ISP_Params
.EnterProgBytes
[RByte
]);
84 /* Check if polling disabled, or if the polled value matches the expected value */
85 if (!(Enter_ISP_Params
.PollIndex
) || (ResponseBytes
[Enter_ISP_Params
.PollIndex
- 1] == Enter_ISP_Params
.PollValue
))
87 ResponseStatus
= STATUS_CMD_OK
;
91 ISPTarget_ChangeTargetResetLine(false);
92 ISPProtocol_DelayMS(Enter_ISP_Params
.PinStabDelayMS
);
96 Endpoint_Write_Byte(CMD_ENTER_PROGMODE_ISP
);
97 Endpoint_Write_Byte(ResponseStatus
);
101 /** Handler for the CMD_LEAVE_ISP command, which releases the target from programming mode. */
102 void ISPProtocol_LeaveISPMode(void)
110 Endpoint_Read_Stream_LE(&Leave_ISP_Params
, sizeof(Leave_ISP_Params
), NO_STREAM_CALLBACK
);
113 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
115 /* Perform pre-exit delay, release the target /RESET, disable the SPI bus and perform the post-exit delay */
116 ISPProtocol_DelayMS(Leave_ISP_Params
.PreDelayMS
);
117 ISPTarget_ChangeTargetResetLine(false);
119 ISPProtocol_DelayMS(Leave_ISP_Params
.PostDelayMS
);
121 Endpoint_Write_Byte(CMD_LEAVE_PROGMODE_ISP
);
122 Endpoint_Write_Byte(STATUS_CMD_OK
);
126 /** Handler for the CMD_PROGRAM_FLASH_ISP and CMD_PROGRAM_EEPROM_ISP commands, writing out bytes,
127 * words or pages of data to the attached device.
129 * \param[in] V2Command Issued V2 Protocol command byte from the host
131 void ISPProtocol_ProgramMemory(uint8_t V2Command
)
135 uint16_t BytesToWrite
;
136 uint8_t ProgrammingMode
;
138 uint8_t ProgrammingCommands
[3];
141 uint8_t ProgData
[256]; // Note, the Jungo driver has a very short ACK timeout period, need to buffer the
142 } Write_Memory_Params
; // whole page and ACK the packet as fast as possible to prevent it from aborting
144 Endpoint_Read_Stream_LE(&Write_Memory_Params
, (sizeof(Write_Memory_Params
) -
145 sizeof(Write_Memory_Params
.ProgData
)), NO_STREAM_CALLBACK
);
148 Write_Memory_Params
.BytesToWrite
= SwapEndian_16(Write_Memory_Params
.BytesToWrite
);
150 if (Write_Memory_Params
.BytesToWrite
> sizeof(Write_Memory_Params
.ProgData
))
153 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
155 Endpoint_Write_Byte(V2Command
);
156 Endpoint_Write_Byte(STATUS_CMD_FAILED
);
161 Endpoint_Read_Stream_LE(&Write_Memory_Params
.ProgData
, Write_Memory_Params
.BytesToWrite
, NO_STREAM_CALLBACK
);
164 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
166 uint8_t ProgrammingStatus
= STATUS_CMD_OK
;
167 uint16_t PollAddress
= 0;
168 uint8_t PollValue
= (V2Command
== CMD_PROGRAM_FLASH_ISP
) ? Write_Memory_Params
.PollValue1
:
169 Write_Memory_Params
.PollValue2
;
170 uint8_t* NextWriteByte
= Write_Memory_Params
.ProgData
;
172 /* Check to see if the host has issued a SET ADDRESS command and we haven't sent a
173 * LOAD EXTENDED ADDRESS command (if needed, used when operating beyond the 128KB
177 if (CurrentAddress
& (1UL << 31))
178 ISPTarget_LoadExtendedAddress();
180 MustSetAddress
= false;
183 /* Check the programming mode desired by the host, either Paged or Word memory writes */
184 if (Write_Memory_Params
.ProgrammingMode
& PROG_MODE_PAGED_WRITES_MASK
)
186 uint16_t StartAddress
= (CurrentAddress
& 0xFFFF);
188 /* Paged mode memory programming */
189 for (uint16_t CurrentByte
= 0; CurrentByte
< Write_Memory_Params
.BytesToWrite
; CurrentByte
++)
191 bool IsOddByte
= (CurrentByte
& 0x01);
192 uint8_t ByteToWrite
= *(NextWriteByte
++);
194 SPI_SendByte(Write_Memory_Params
.ProgrammingCommands
[0]);
195 SPI_SendByte(CurrentAddress
>> 8);
196 SPI_SendByte(CurrentAddress
& 0xFF);
197 SPI_SendByte(ByteToWrite
);
199 /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high
200 * or low byte at the current word address */
201 if (V2Command
== CMD_PROGRAM_FLASH_ISP
)
202 Write_Memory_Params
.ProgrammingCommands
[0] ^= READ_WRITE_HIGH_BYTE_MASK
;
204 /* Check to see the write completion method, to see if we have a valid polling address */
205 if (!(PollAddress
) && (ByteToWrite
!= PollValue
))
207 if (IsOddByte
&& (V2Command
== CMD_PROGRAM_FLASH_ISP
))
208 Write_Memory_Params
.ProgrammingCommands
[2] |= READ_WRITE_HIGH_BYTE_MASK
;
210 PollAddress
= (CurrentAddress
& 0xFFFF);
213 if (IsOddByte
|| (V2Command
== CMD_PROGRAM_EEPROM_ISP
))
217 /* If the current page must be committed, send the PROGRAM PAGE command to the target */
218 if (Write_Memory_Params
.ProgrammingMode
& PROG_MODE_COMMIT_PAGE_MASK
)
220 SPI_SendByte(Write_Memory_Params
.ProgrammingCommands
[1]);
221 SPI_SendByte(StartAddress
>> 8);
222 SPI_SendByte(StartAddress
& 0xFF);
225 /* Check if polling is possible, if not switch to timed delay mode */
228 Write_Memory_Params
.ProgrammingMode
&= ~PROG_MODE_PAGED_VALUE_MASK
;
229 Write_Memory_Params
.ProgrammingMode
|= PROG_MODE_PAGED_TIMEDELAY_MASK
;
232 ProgrammingStatus
= ISPTarget_WaitForProgComplete(Write_Memory_Params
.ProgrammingMode
, PollAddress
, PollValue
,
233 Write_Memory_Params
.DelayMS
, Write_Memory_Params
.ProgrammingCommands
[2]);
238 /* Word/byte mode memory programming */
239 for (uint16_t CurrentByte
= 0; CurrentByte
< Write_Memory_Params
.BytesToWrite
; CurrentByte
++)
241 bool IsOddByte
= (CurrentByte
& 0x01);
242 uint8_t ByteToWrite
= *(NextWriteByte
++);
244 SPI_SendByte(Write_Memory_Params
.ProgrammingCommands
[0]);
245 SPI_SendByte(CurrentAddress
>> 8);
246 SPI_SendByte(CurrentAddress
& 0xFF);
247 SPI_SendByte(ByteToWrite
);
249 /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high
250 * or low byte at the current word address */
251 if (V2Command
== CMD_PROGRAM_FLASH_ISP
)
252 Write_Memory_Params
.ProgrammingCommands
[0] ^= READ_WRITE_HIGH_BYTE_MASK
;
254 if (ByteToWrite
!= PollValue
)
256 if (IsOddByte
&& (V2Command
== CMD_PROGRAM_FLASH_ISP
))
257 Write_Memory_Params
.ProgrammingCommands
[2] |= READ_WRITE_HIGH_BYTE_MASK
;
259 PollAddress
= (CurrentAddress
& 0xFFFF);
262 if (IsOddByte
|| (V2Command
== CMD_PROGRAM_EEPROM_ISP
))
265 ProgrammingStatus
= ISPTarget_WaitForProgComplete(Write_Memory_Params
.ProgrammingMode
, PollAddress
, PollValue
,
266 Write_Memory_Params
.DelayMS
, Write_Memory_Params
.ProgrammingCommands
[2]);
268 if (ProgrammingStatus
!= STATUS_CMD_OK
)
273 Endpoint_Write_Byte(V2Command
);
274 Endpoint_Write_Byte(ProgrammingStatus
);
278 /** Handler for the CMD_READ_FLASH_ISP and CMD_READ_EEPROM_ISP commands, reading in bytes,
279 * words or pages of data from the attached device.
281 * \param[in] V2Command Issued V2 Protocol command byte from the host
283 void ISPProtocol_ReadMemory(uint8_t V2Command
)
287 uint16_t BytesToRead
;
288 uint8_t ReadMemoryCommand
;
289 } Read_Memory_Params
;
291 Endpoint_Read_Stream_LE(&Read_Memory_Params
, sizeof(Read_Memory_Params
), NO_STREAM_CALLBACK
);
292 Read_Memory_Params
.BytesToRead
= SwapEndian_16(Read_Memory_Params
.BytesToRead
);
295 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
297 Endpoint_Write_Byte(V2Command
);
298 Endpoint_Write_Byte(STATUS_CMD_OK
);
300 /* Check to see if the host has issued a SET ADDRESS command and we haven't sent a
301 * LOAD EXTENDED ADDRESS command (if needed, used when operating beyond the 128KB
305 if (CurrentAddress
& (1UL << 31))
306 ISPTarget_LoadExtendedAddress();
308 MustSetAddress
= false;
311 /* Read each byte from the device and write them to the packet for the host */
312 for (uint16_t CurrentByte
= 0; CurrentByte
< Read_Memory_Params
.BytesToRead
; CurrentByte
++)
314 /* Read the next byte from the desired memory space in the device */
315 SPI_SendByte(Read_Memory_Params
.ReadMemoryCommand
);
316 SPI_SendByte(CurrentAddress
>> 8);
317 SPI_SendByte(CurrentAddress
& 0xFF);
318 Endpoint_Write_Byte(SPI_ReceiveByte());
320 /* Check if the endpoint bank is currently full, if so send the packet */
321 if (!(Endpoint_IsReadWriteAllowed()))
324 Endpoint_WaitUntilReady();
327 /* AVR FLASH addressing requires us to modify the read command based on if we are reading a high
328 * or low byte at the current word address */
329 if (V2Command
== CMD_READ_FLASH_ISP
)
330 Read_Memory_Params
.ReadMemoryCommand
^= READ_WRITE_HIGH_BYTE_MASK
;
332 /* Only increment the current address if we have read both bytes in the current word when in FLASH
333 * read mode, or for each byte when in EEPROM read mode */
334 if (((CurrentByte
& 0x01) && (V2Command
== CMD_READ_FLASH_ISP
)) || (V2Command
== CMD_READ_EEPROM_ISP
))
338 Endpoint_Write_Byte(STATUS_CMD_OK
);
340 bool IsEndpointFull
= !(Endpoint_IsReadWriteAllowed());
343 /* Ensure last packet is a short packet to terminate the transfer */
346 Endpoint_WaitUntilReady();
348 Endpoint_WaitUntilReady();
352 /** Handler for the CMD_CHI_ERASE_ISP command, clearing the target's FLASH memory. */
353 void ISPProtocol_ChipErase(void)
357 uint8_t EraseDelayMS
;
359 uint8_t EraseCommandBytes
[4];
362 Endpoint_Read_Stream_LE(&Erase_Chip_Params
, sizeof(Erase_Chip_Params
), NO_STREAM_CALLBACK
);
365 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
367 uint8_t ResponseStatus
= STATUS_CMD_OK
;
369 /* Send the chip erase commands as given by the host to the device */
370 for (uint8_t SByte
= 0; SByte
< sizeof(Erase_Chip_Params
.EraseCommandBytes
); SByte
++)
371 SPI_SendByte(Erase_Chip_Params
.EraseCommandBytes
[SByte
]);
373 /* Use appropriate command completion check as given by the host (delay or busy polling) */
374 if (!(Erase_Chip_Params
.PollMethod
))
375 ISPProtocol_DelayMS(Erase_Chip_Params
.EraseDelayMS
);
377 ResponseStatus
= ISPTarget_WaitWhileTargetBusy();
379 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP
);
380 Endpoint_Write_Byte(ResponseStatus
);
384 /** Handler for the CMD_READ_FUSE_ISP, CMD_READ_LOCK_ISP, CMD_READ_SIGNATURE_ISP and CMD_READ_OSCCAL commands,
385 * reading the requested configuration byte from the device.
387 * \param[in] V2Command Issued V2 Protocol command byte from the host
389 void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command
)
394 uint8_t ReadCommandBytes
[4];
395 } Read_FuseLockSigOSCCAL_Params
;
397 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params
, sizeof(Read_FuseLockSigOSCCAL_Params
), NO_STREAM_CALLBACK
);
400 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
402 uint8_t ResponseBytes
[4];
404 /* Send the Fuse or Lock byte read commands as given by the host to the device, store response */
405 for (uint8_t RByte
= 0; RByte
< sizeof(ResponseBytes
); RByte
++)
406 ResponseBytes
[RByte
] = SPI_TransferByte(Read_FuseLockSigOSCCAL_Params
.ReadCommandBytes
[RByte
]);
408 Endpoint_Write_Byte(V2Command
);
409 Endpoint_Write_Byte(STATUS_CMD_OK
);
410 Endpoint_Write_Byte(ResponseBytes
[Read_FuseLockSigOSCCAL_Params
.RetByte
- 1]);
411 Endpoint_Write_Byte(STATUS_CMD_OK
);
415 /** Handler for the CMD_WRITE_FUSE_ISP and CMD_WRITE_LOCK_ISP commands, writing the requested configuration
416 * byte to the device.
418 * \param[in] V2Command Issued V2 Protocol command byte from the host
420 void ISPProtocol_WriteFuseLock(uint8_t V2Command
)
424 uint8_t WriteCommandBytes
[4];
425 } Write_FuseLockSig_Params
;
427 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params
, sizeof(Write_FuseLockSig_Params
), NO_STREAM_CALLBACK
);
430 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
432 /* Send the Fuse or Lock byte program commands as given by the host to the device */
433 for (uint8_t SByte
= 0; SByte
< sizeof(Write_FuseLockSig_Params
.WriteCommandBytes
); SByte
++)
434 SPI_SendByte(Write_FuseLockSig_Params
.WriteCommandBytes
[SByte
]);
436 Endpoint_Write_Byte(V2Command
);
437 Endpoint_Write_Byte(STATUS_CMD_OK
);
438 Endpoint_Write_Byte(STATUS_CMD_OK
);
442 /** Handler for the CMD_SPI_MULTI command, writing and reading arbitrary SPI data to and from the attached device. */
443 void ISPProtocol_SPIMulti(void)
453 Endpoint_Read_Stream_LE(&SPI_Multi_Params
, (sizeof(SPI_Multi_Params
) - sizeof(SPI_Multi_Params
.TxData
)), NO_STREAM_CALLBACK
);
454 Endpoint_Read_Stream_LE(&SPI_Multi_Params
.TxData
, SPI_Multi_Params
.TxBytes
, NO_STREAM_CALLBACK
);
457 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
459 Endpoint_Write_Byte(CMD_SPI_MULTI
);
460 Endpoint_Write_Byte(STATUS_CMD_OK
);
462 uint8_t CurrTxPos
= 0;
463 uint8_t CurrRxPos
= 0;
465 /* Write out bytes to transmit until the start of the bytes to receive is met */
466 while (CurrTxPos
< SPI_Multi_Params
.RxStartAddr
)
468 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
469 SPI_SendByte(SPI_Multi_Params
.TxData
[CurrTxPos
]);
476 /* Transmit remaining bytes with padding as needed, read in response bytes */
477 while (CurrRxPos
< SPI_Multi_Params
.RxBytes
)
479 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
480 Endpoint_Write_Byte(SPI_TransferByte(SPI_Multi_Params
.TxData
[CurrTxPos
++]));
482 Endpoint_Write_Byte(SPI_ReceiveByte());
484 /* Check to see if we have filled the endpoint bank and need to send the packet */
485 if (!(Endpoint_IsReadWriteAllowed()))
488 Endpoint_WaitUntilReady();
494 Endpoint_Write_Byte(STATUS_CMD_OK
);
496 bool IsEndpointFull
= !(Endpoint_IsReadWriteAllowed());
499 /* Ensure last packet is a short packet to terminate the transfer */
502 Endpoint_WaitUntilReady();
504 Endpoint_WaitUntilReady();