3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] 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_SelectEndpoint(AVRISP_DATA_IN_EPNUM
);
61 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
63 uint8_t ResponseStatus
= STATUS_CMD_FAILED
;
67 /* Perform execution delay, initialize SPI bus */
68 ISPProtocol_DelayMS(Enter_ISP_Params
.ExecutionDelayMS
);
69 ISPTarget_EnableTargetISP();
71 /* Continuously attempt to synchronize with the target until either the number of attempts specified
72 * by the host has exceeded, or the the device sends back the expected response values */
73 while (Enter_ISP_Params
.SynchLoops
-- && (ResponseStatus
== STATUS_CMD_FAILED
) && TimeoutTicksRemaining
)
75 uint8_t ResponseBytes
[4];
77 ISPTarget_ChangeTargetResetLine(true);
78 ISPProtocol_DelayMS(Enter_ISP_Params
.PinStabDelayMS
);
80 for (uint8_t RByte
= 0; RByte
< sizeof(ResponseBytes
); RByte
++)
82 ISPProtocol_DelayMS(Enter_ISP_Params
.ByteDelay
);
83 ResponseBytes
[RByte
] = ISPTarget_TransferByte(Enter_ISP_Params
.EnterProgBytes
[RByte
]);
86 /* Check if polling disabled, or if the polled value matches the expected value */
87 if (!(Enter_ISP_Params
.PollIndex
) || (ResponseBytes
[Enter_ISP_Params
.PollIndex
- 1] == Enter_ISP_Params
.PollValue
))
89 ResponseStatus
= STATUS_CMD_OK
;
93 ISPTarget_ChangeTargetResetLine(false);
94 ISPProtocol_DelayMS(Enter_ISP_Params
.PinStabDelayMS
);
98 Endpoint_Write_Byte(CMD_ENTER_PROGMODE_ISP
);
99 Endpoint_Write_Byte(ResponseStatus
);
103 /** Handler for the CMD_LEAVE_ISP command, which releases the target from programming mode. */
104 void ISPProtocol_LeaveISPMode(void)
112 Endpoint_Read_Stream_LE(&Leave_ISP_Params
, sizeof(Leave_ISP_Params
), NO_STREAM_CALLBACK
);
115 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
);
116 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
118 /* Perform pre-exit delay, release the target /RESET, disable the SPI bus and perform the post-exit delay */
119 ISPProtocol_DelayMS(Leave_ISP_Params
.PreDelayMS
);
120 ISPTarget_ChangeTargetResetLine(false);
121 ISPTarget_DisableTargetISP();
122 ISPProtocol_DelayMS(Leave_ISP_Params
.PostDelayMS
);
124 Endpoint_Write_Byte(CMD_LEAVE_PROGMODE_ISP
);
125 Endpoint_Write_Byte(STATUS_CMD_OK
);
129 /** Handler for the CMD_PROGRAM_FLASH_ISP and CMD_PROGRAM_EEPROM_ISP commands, writing out bytes,
130 * words or pages of data to the attached device.
132 * \param[in] V2Command Issued V2 Protocol command byte from the host
134 void ISPProtocol_ProgramMemory(uint8_t V2Command
)
138 uint16_t BytesToWrite
;
139 uint8_t ProgrammingMode
;
141 uint8_t ProgrammingCommands
[3];
144 uint8_t ProgData
[256]; // Note, the Jungo driver has a very short ACK timeout period, need to buffer the
145 } Write_Memory_Params
; // whole page and ACK the packet as fast as possible to prevent it from aborting
147 Endpoint_Read_Stream_LE(&Write_Memory_Params
, (sizeof(Write_Memory_Params
) -
148 sizeof(Write_Memory_Params
.ProgData
)), NO_STREAM_CALLBACK
);
151 Write_Memory_Params
.BytesToWrite
= SwapEndian_16(Write_Memory_Params
.BytesToWrite
);
153 if (Write_Memory_Params
.BytesToWrite
> sizeof(Write_Memory_Params
.ProgData
))
156 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
);
157 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
159 Endpoint_Write_Byte(V2Command
);
160 Endpoint_Write_Byte(STATUS_CMD_FAILED
);
165 Endpoint_Read_Stream_LE(&Write_Memory_Params
.ProgData
, Write_Memory_Params
.BytesToWrite
, NO_STREAM_CALLBACK
);
168 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
);
169 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
171 uint8_t ProgrammingStatus
= STATUS_CMD_OK
;
172 uint16_t PollAddress
= 0;
173 uint8_t PollValue
= (V2Command
== CMD_PROGRAM_FLASH_ISP
) ? Write_Memory_Params
.PollValue1
:
174 Write_Memory_Params
.PollValue2
;
175 uint8_t* NextWriteByte
= Write_Memory_Params
.ProgData
;
177 /* Check the programming mode desired by the host, either Paged or Word memory writes */
178 if (Write_Memory_Params
.ProgrammingMode
& PROG_MODE_PAGED_WRITES_MASK
)
180 uint16_t StartAddress
= (CurrentAddress
& 0xFFFF);
182 /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
183 if (MustLoadExtendedAddress
)
185 ISPTarget_LoadExtendedAddress();
186 MustLoadExtendedAddress
= false;
189 /* Paged mode memory programming */
190 for (uint16_t CurrentByte
= 0; CurrentByte
< Write_Memory_Params
.BytesToWrite
; CurrentByte
++)
192 bool IsOddByte
= (CurrentByte
& 0x01);
193 uint8_t ByteToWrite
= *(NextWriteByte
++);
195 ISPTarget_SendByte(Write_Memory_Params
.ProgrammingCommands
[0]);
196 ISPTarget_SendByte(CurrentAddress
>> 8);
197 ISPTarget_SendByte(CurrentAddress
& 0xFF);
198 ISPTarget_SendByte(ByteToWrite
);
200 /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high
201 * or low byte at the current word address */
202 if (V2Command
== CMD_PROGRAM_FLASH_ISP
)
203 Write_Memory_Params
.ProgrammingCommands
[0] ^= READ_WRITE_HIGH_BYTE_MASK
;
205 /* Check to see the write completion method, to see if we have a valid polling address */
206 if (!(PollAddress
) && (ByteToWrite
!= PollValue
))
208 if (IsOddByte
&& (V2Command
== CMD_PROGRAM_FLASH_ISP
))
209 Write_Memory_Params
.ProgrammingCommands
[2] |= READ_WRITE_HIGH_BYTE_MASK
;
211 PollAddress
= (CurrentAddress
& 0xFFFF);
214 /* EEPROM increments the address on each byte, flash needs to increment on each word */
215 if (IsOddByte
|| (V2Command
== CMD_PROGRAM_EEPROM_ISP
))
219 /* If the current page must be committed, send the PROGRAM PAGE command to the target */
220 if (Write_Memory_Params
.ProgrammingMode
& PROG_MODE_COMMIT_PAGE_MASK
)
222 ISPTarget_SendByte(Write_Memory_Params
.ProgrammingCommands
[1]);
223 ISPTarget_SendByte(StartAddress
>> 8);
224 ISPTarget_SendByte(StartAddress
& 0xFF);
225 ISPTarget_SendByte(0x00);
227 /* Check if polling is possible and enabled, if not switch to timed delay mode */
228 if (!(PollAddress
) && (Write_Memory_Params
.ProgrammingMode
& PROG_MODE_PAGED_VALUE_MASK
))
230 Write_Memory_Params
.ProgrammingMode
&= ~PROG_MODE_PAGED_VALUE_MASK
;
231 Write_Memory_Params
.ProgrammingMode
|= PROG_MODE_PAGED_TIMEDELAY_MASK
;
234 ProgrammingStatus
= ISPTarget_WaitForProgComplete(Write_Memory_Params
.ProgrammingMode
, PollAddress
, PollValue
,
235 Write_Memory_Params
.DelayMS
, Write_Memory_Params
.ProgrammingCommands
[2]);
237 /* Check to see if the FLASH address has crossed the extended address boundary */
238 if ((V2Command
== CMD_PROGRAM_FLASH_ISP
) && !(CurrentAddress
& 0xFFFF))
239 MustLoadExtendedAddress
= true;
244 /* Word/byte mode memory programming */
245 for (uint16_t CurrentByte
= 0; CurrentByte
< Write_Memory_Params
.BytesToWrite
; CurrentByte
++)
247 bool IsOddByte
= (CurrentByte
& 0x01);
248 uint8_t ByteToWrite
= *(NextWriteByte
++);
250 /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
251 if (MustLoadExtendedAddress
)
253 ISPTarget_LoadExtendedAddress();
254 MustLoadExtendedAddress
= false;
257 ISPTarget_SendByte(Write_Memory_Params
.ProgrammingCommands
[0]);
258 ISPTarget_SendByte(CurrentAddress
>> 8);
259 ISPTarget_SendByte(CurrentAddress
& 0xFF);
260 ISPTarget_SendByte(ByteToWrite
);
262 /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high
263 * or low byte at the current word address */
264 if (V2Command
== CMD_PROGRAM_FLASH_ISP
)
265 Write_Memory_Params
.ProgrammingCommands
[0] ^= READ_WRITE_HIGH_BYTE_MASK
;
267 /* Save previous programming mode in case we modify it for the current word */
268 uint8_t PreviousProgrammingMode
= Write_Memory_Params
.ProgrammingMode
;
270 if (ByteToWrite
!= PollValue
)
272 if (IsOddByte
&& (V2Command
== CMD_PROGRAM_FLASH_ISP
))
273 Write_Memory_Params
.ProgrammingCommands
[2] |= READ_WRITE_HIGH_BYTE_MASK
;
275 PollAddress
= (CurrentAddress
& 0xFFFF);
277 else if (!(Write_Memory_Params
.ProgrammingMode
& PROG_MODE_WORD_READYBUSY_MASK
))
279 Write_Memory_Params
.ProgrammingMode
&= ~PROG_MODE_WORD_VALUE_MASK
;
280 Write_Memory_Params
.ProgrammingMode
|= PROG_MODE_WORD_TIMEDELAY_MASK
;
283 ProgrammingStatus
= ISPTarget_WaitForProgComplete(Write_Memory_Params
.ProgrammingMode
, PollAddress
, PollValue
,
284 Write_Memory_Params
.DelayMS
, Write_Memory_Params
.ProgrammingCommands
[2]);
286 /* Restore previous programming mode mask in case the current word needed to change it */
287 Write_Memory_Params
.ProgrammingMode
= PreviousProgrammingMode
;
289 /* Abort the programming loop early if the byte/word programming failed */
290 if (ProgrammingStatus
!= STATUS_CMD_OK
)
293 /* EEPROM just increments the address each byte, flash needs to increment on each word and
294 * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended
295 * address boundary has been crossed */
296 if ((CurrentByte
& 0x01) || (V2Command
== CMD_PROGRAM_EEPROM_ISP
))
300 if ((V2Command
!= CMD_PROGRAM_EEPROM_ISP
) && !(CurrentAddress
& 0xFFFF))
301 MustLoadExtendedAddress
= true;
306 Endpoint_Write_Byte(V2Command
);
307 Endpoint_Write_Byte(ProgrammingStatus
);
311 /** Handler for the CMD_READ_FLASH_ISP and CMD_READ_EEPROM_ISP commands, reading in bytes,
312 * words or pages of data from the attached device.
314 * \param[in] V2Command Issued V2 Protocol command byte from the host
316 void ISPProtocol_ReadMemory(uint8_t V2Command
)
320 uint16_t BytesToRead
;
321 uint8_t ReadMemoryCommand
;
322 } Read_Memory_Params
;
324 Endpoint_Read_Stream_LE(&Read_Memory_Params
, sizeof(Read_Memory_Params
), NO_STREAM_CALLBACK
);
325 Read_Memory_Params
.BytesToRead
= SwapEndian_16(Read_Memory_Params
.BytesToRead
);
328 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
);
329 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
331 Endpoint_Write_Byte(V2Command
);
332 Endpoint_Write_Byte(STATUS_CMD_OK
);
334 /* Read each byte from the device and write them to the packet for the host */
335 for (uint16_t CurrentByte
= 0; CurrentByte
< Read_Memory_Params
.BytesToRead
; CurrentByte
++)
337 /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
338 if (MustLoadExtendedAddress
)
340 ISPTarget_LoadExtendedAddress();
341 MustLoadExtendedAddress
= false;
344 /* Read the next byte from the desired memory space in the device */
345 ISPTarget_SendByte(Read_Memory_Params
.ReadMemoryCommand
);
346 ISPTarget_SendByte(CurrentAddress
>> 8);
347 ISPTarget_SendByte(CurrentAddress
& 0xFF);
348 Endpoint_Write_Byte(ISPTarget_ReceiveByte());
350 /* Check if the endpoint bank is currently full, if so send the packet */
351 if (!(Endpoint_IsReadWriteAllowed()))
354 Endpoint_WaitUntilReady();
357 /* AVR FLASH addressing requires us to modify the read command based on if we are reading a high
358 * or low byte at the current word address */
359 if (V2Command
== CMD_READ_FLASH_ISP
)
360 Read_Memory_Params
.ReadMemoryCommand
^= READ_WRITE_HIGH_BYTE_MASK
;
362 /* EEPROM just increments the address each byte, flash needs to increment on each word and
363 * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended
364 * address boundary has been crossed */
365 if ((CurrentByte
& 0x01) || (V2Command
== CMD_READ_EEPROM_ISP
))
369 if ((V2Command
!= CMD_READ_EEPROM_ISP
) && !(CurrentAddress
& 0xFFFF))
370 MustLoadExtendedAddress
= true;
374 Endpoint_Write_Byte(STATUS_CMD_OK
);
376 bool IsEndpointFull
= !(Endpoint_IsReadWriteAllowed());
379 /* Ensure last packet is a short packet to terminate the transfer */
382 Endpoint_WaitUntilReady();
384 Endpoint_WaitUntilReady();
388 /** Handler for the CMD_CHI_ERASE_ISP command, clearing the target's FLASH memory. */
389 void ISPProtocol_ChipErase(void)
393 uint8_t EraseDelayMS
;
395 uint8_t EraseCommandBytes
[4];
398 Endpoint_Read_Stream_LE(&Erase_Chip_Params
, sizeof(Erase_Chip_Params
), NO_STREAM_CALLBACK
);
401 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
);
402 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
404 uint8_t ResponseStatus
= STATUS_CMD_OK
;
406 /* Send the chip erase commands as given by the host to the device */
407 for (uint8_t SByte
= 0; SByte
< sizeof(Erase_Chip_Params
.EraseCommandBytes
); SByte
++)
408 ISPTarget_SendByte(Erase_Chip_Params
.EraseCommandBytes
[SByte
]);
410 /* Use appropriate command completion check as given by the host (delay or busy polling) */
411 if (!(Erase_Chip_Params
.PollMethod
))
412 ISPProtocol_DelayMS(Erase_Chip_Params
.EraseDelayMS
);
414 ResponseStatus
= ISPTarget_WaitWhileTargetBusy();
416 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP
);
417 Endpoint_Write_Byte(ResponseStatus
);
421 /** Handler for the CMD_READ_FUSE_ISP, CMD_READ_LOCK_ISP, CMD_READ_SIGNATURE_ISP and CMD_READ_OSCCAL commands,
422 * reading the requested configuration byte from the device.
424 * \param[in] V2Command Issued V2 Protocol command byte from the host
426 void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command
)
431 uint8_t ReadCommandBytes
[4];
432 } Read_FuseLockSigOSCCAL_Params
;
434 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params
, sizeof(Read_FuseLockSigOSCCAL_Params
), NO_STREAM_CALLBACK
);
437 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
);
438 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
440 uint8_t ResponseBytes
[4];
442 /* Send the Fuse or Lock byte read commands as given by the host to the device, store response */
443 for (uint8_t RByte
= 0; RByte
< sizeof(ResponseBytes
); RByte
++)
444 ResponseBytes
[RByte
] = ISPTarget_TransferByte(Read_FuseLockSigOSCCAL_Params
.ReadCommandBytes
[RByte
]);
446 Endpoint_Write_Byte(V2Command
);
447 Endpoint_Write_Byte(STATUS_CMD_OK
);
448 Endpoint_Write_Byte(ResponseBytes
[Read_FuseLockSigOSCCAL_Params
.RetByte
- 1]);
449 Endpoint_Write_Byte(STATUS_CMD_OK
);
453 /** Handler for the CMD_WRITE_FUSE_ISP and CMD_WRITE_LOCK_ISP commands, writing the requested configuration
454 * byte to the device.
456 * \param[in] V2Command Issued V2 Protocol command byte from the host
458 void ISPProtocol_WriteFuseLock(uint8_t V2Command
)
462 uint8_t WriteCommandBytes
[4];
463 } Write_FuseLockSig_Params
;
465 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params
, sizeof(Write_FuseLockSig_Params
), NO_STREAM_CALLBACK
);
468 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
);
469 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
471 /* Send the Fuse or Lock byte program commands as given by the host to the device */
472 for (uint8_t SByte
= 0; SByte
< sizeof(Write_FuseLockSig_Params
.WriteCommandBytes
); SByte
++)
473 ISPTarget_SendByte(Write_FuseLockSig_Params
.WriteCommandBytes
[SByte
]);
475 Endpoint_Write_Byte(V2Command
);
476 Endpoint_Write_Byte(STATUS_CMD_OK
);
477 Endpoint_Write_Byte(STATUS_CMD_OK
);
481 /** Handler for the CMD_SPI_MULTI command, writing and reading arbitrary SPI data to and from the attached device. */
482 void ISPProtocol_SPIMulti(void)
492 Endpoint_Read_Stream_LE(&SPI_Multi_Params
, (sizeof(SPI_Multi_Params
) - sizeof(SPI_Multi_Params
.TxData
)), NO_STREAM_CALLBACK
);
493 Endpoint_Read_Stream_LE(&SPI_Multi_Params
.TxData
, SPI_Multi_Params
.TxBytes
, NO_STREAM_CALLBACK
);
496 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
);
497 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
499 Endpoint_Write_Byte(CMD_SPI_MULTI
);
500 Endpoint_Write_Byte(STATUS_CMD_OK
);
502 uint8_t CurrTxPos
= 0;
503 uint8_t CurrRxPos
= 0;
505 /* Write out bytes to transmit until the start of the bytes to receive is met */
506 while (CurrTxPos
< SPI_Multi_Params
.RxStartAddr
)
508 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
509 ISPTarget_SendByte(SPI_Multi_Params
.TxData
[CurrTxPos
]);
511 ISPTarget_SendByte(0);
516 /* Transmit remaining bytes with padding as needed, read in response bytes */
517 while (CurrRxPos
< SPI_Multi_Params
.RxBytes
)
519 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
520 Endpoint_Write_Byte(ISPTarget_TransferByte(SPI_Multi_Params
.TxData
[CurrTxPos
++]));
522 Endpoint_Write_Byte(ISPTarget_ReceiveByte());
524 /* Check to see if we have filled the endpoint bank and need to send the packet */
525 if (!(Endpoint_IsReadWriteAllowed()))
528 Endpoint_WaitUntilReady();
534 Endpoint_Write_Byte(STATUS_CMD_OK
);
536 bool IsEndpointFull
= !(Endpoint_IsReadWriteAllowed());
539 /* Ensure last packet is a short packet to terminate the transfer */
542 Endpoint_WaitUntilReady();
544 Endpoint_WaitUntilReady();
548 /** Blocking delay for a given number of milliseconds.
550 * \param[in] DelayMS Number of milliseconds to delay for
552 void ISPProtocol_DelayMS(uint8_t DelayMS
)
554 while (DelayMS
-- && TimeoutTicksRemaining
)