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 /* Set up the synchronous USART to generate the recovery clock on XCK pin */
67 UBRR1
= (F_CPU
/ 500000UL);
68 UCSR1B
= (1 << TXEN1
);
69 UCSR1C
= (1 << UMSEL10
) | (1 << UPM11
) | (1 << USBS1
) | (1 << UCSZ11
) | (1 << UCSZ10
) | (1 << UCPOL1
);
72 /* Perform execution delay, initialize SPI bus */
73 ISPProtocol_DelayMS(Enter_ISP_Params
.ExecutionDelayMS
);
74 SPI_Init(ISPTarget_GetSPIPrescalerMask() | SPI_SCK_LEAD_RISING
| SPI_SAMPLE_LEADING
| SPI_MODE_MASTER
);
76 /* Continuously attempt to synchronize with the target until either the number of attempts specified
77 * by the host has exceeded, or the the device sends back the expected response values */
78 while (Enter_ISP_Params
.SynchLoops
-- && (ResponseStatus
== STATUS_CMD_FAILED
) && TimeoutMSRemaining
)
80 uint8_t ResponseBytes
[4];
82 ISPTarget_ChangeTargetResetLine(true);
83 ISPProtocol_DelayMS(Enter_ISP_Params
.PinStabDelayMS
);
85 for (uint8_t RByte
= 0; RByte
< sizeof(ResponseBytes
); RByte
++)
87 ISPProtocol_DelayMS(Enter_ISP_Params
.ByteDelay
);
88 ResponseBytes
[RByte
] = SPI_TransferByte(Enter_ISP_Params
.EnterProgBytes
[RByte
]);
91 /* Check if polling disabled, or if the polled value matches the expected value */
92 if (!(Enter_ISP_Params
.PollIndex
) || (ResponseBytes
[Enter_ISP_Params
.PollIndex
- 1] == Enter_ISP_Params
.PollValue
))
94 ResponseStatus
= STATUS_CMD_OK
;
98 ISPTarget_ChangeTargetResetLine(false);
99 ISPProtocol_DelayMS(Enter_ISP_Params
.PinStabDelayMS
);
103 Endpoint_Write_Byte(CMD_ENTER_PROGMODE_ISP
);
104 Endpoint_Write_Byte(ResponseStatus
);
108 /** Handler for the CMD_LEAVE_ISP command, which releases the target from programming mode. */
109 void ISPProtocol_LeaveISPMode(void)
117 Endpoint_Read_Stream_LE(&Leave_ISP_Params
, sizeof(Leave_ISP_Params
), NO_STREAM_CALLBACK
);
120 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
122 /* Perform pre-exit delay, release the target /RESET, disable the SPI bus and perform the post-exit delay */
123 ISPProtocol_DelayMS(Leave_ISP_Params
.PreDelayMS
);
124 ISPTarget_ChangeTargetResetLine(false);
126 ISPProtocol_DelayMS(Leave_ISP_Params
.PostDelayMS
);
128 /* Turn off the synchronous USART to terminate the recovery clock on XCK pin */
129 UBRR1
= (F_CPU
/ 500000UL);
130 UCSR1B
= (1 << TXEN1
);
131 UCSR1C
= (1 << UMSEL10
) | (1 << UPM11
) | (1 << USBS1
) | (1 << UCSZ11
) | (1 << UCSZ10
) | (1 << UCPOL1
);
134 Endpoint_Write_Byte(CMD_LEAVE_PROGMODE_ISP
);
135 Endpoint_Write_Byte(STATUS_CMD_OK
);
139 /** Handler for the CMD_PROGRAM_FLASH_ISP and CMD_PROGRAM_EEPROM_ISP commands, writing out bytes,
140 * words or pages of data to the attached device.
142 * \param[in] V2Command Issued V2 Protocol command byte from the host
144 void ISPProtocol_ProgramMemory(uint8_t V2Command
)
148 uint16_t BytesToWrite
;
149 uint8_t ProgrammingMode
;
151 uint8_t ProgrammingCommands
[3];
154 uint8_t ProgData
[256]; // Note, the Jungo driver has a very short ACK timeout period, need to buffer the
155 } Write_Memory_Params
; // whole page and ACK the packet as fast as possible to prevent it from aborting
157 Endpoint_Read_Stream_LE(&Write_Memory_Params
, (sizeof(Write_Memory_Params
) -
158 sizeof(Write_Memory_Params
.ProgData
)), NO_STREAM_CALLBACK
);
161 Write_Memory_Params
.BytesToWrite
= SwapEndian_16(Write_Memory_Params
.BytesToWrite
);
163 if (Write_Memory_Params
.BytesToWrite
> sizeof(Write_Memory_Params
.ProgData
))
166 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
168 Endpoint_Write_Byte(V2Command
);
169 Endpoint_Write_Byte(STATUS_CMD_FAILED
);
174 Endpoint_Read_Stream_LE(&Write_Memory_Params
.ProgData
, Write_Memory_Params
.BytesToWrite
, NO_STREAM_CALLBACK
);
177 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
179 uint8_t ProgrammingStatus
= STATUS_CMD_OK
;
180 uint16_t PollAddress
= 0;
181 uint8_t PollValue
= (V2Command
== CMD_PROGRAM_FLASH_ISP
) ? Write_Memory_Params
.PollValue1
:
182 Write_Memory_Params
.PollValue2
;
183 uint8_t* NextWriteByte
= Write_Memory_Params
.ProgData
;
185 /* Check to see if the host has issued a SET ADDRESS command and we haven't sent a
186 * LOAD EXTENDED ADDRESS command (if needed, used when operating beyond the 128KB
190 if (CurrentAddress
& (1UL << 31))
191 ISPTarget_LoadExtendedAddress();
193 MustSetAddress
= false;
196 /* Check the programming mode desired by the host, either Paged or Word memory writes */
197 if (Write_Memory_Params
.ProgrammingMode
& PROG_MODE_PAGED_WRITES_MASK
)
199 uint16_t StartAddress
= (CurrentAddress
& 0xFFFF);
201 /* Paged mode memory programming */
202 for (uint16_t CurrentByte
= 0; CurrentByte
< Write_Memory_Params
.BytesToWrite
; CurrentByte
++)
204 bool IsOddByte
= (CurrentByte
& 0x01);
205 uint8_t ByteToWrite
= *(NextWriteByte
++);
207 SPI_SendByte(Write_Memory_Params
.ProgrammingCommands
[0]);
208 SPI_SendByte(CurrentAddress
>> 8);
209 SPI_SendByte(CurrentAddress
& 0xFF);
210 SPI_SendByte(ByteToWrite
);
212 /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high
213 * or low byte at the current word address */
214 if (V2Command
== CMD_PROGRAM_FLASH_ISP
)
215 Write_Memory_Params
.ProgrammingCommands
[0] ^= READ_WRITE_HIGH_BYTE_MASK
;
217 /* Check to see the write completion method, to see if we have a valid polling address */
218 if (!(PollAddress
) && (ByteToWrite
!= PollValue
))
220 if (IsOddByte
&& (V2Command
== CMD_PROGRAM_FLASH_ISP
))
221 Write_Memory_Params
.ProgrammingCommands
[2] |= READ_WRITE_HIGH_BYTE_MASK
;
223 PollAddress
= (CurrentAddress
& 0xFFFF);
226 if (IsOddByte
|| (V2Command
== CMD_PROGRAM_EEPROM_ISP
))
230 /* If the current page must be committed, send the PROGRAM PAGE command to the target */
231 if (Write_Memory_Params
.ProgrammingMode
& PROG_MODE_COMMIT_PAGE_MASK
)
233 SPI_SendByte(Write_Memory_Params
.ProgrammingCommands
[1]);
234 SPI_SendByte(StartAddress
>> 8);
235 SPI_SendByte(StartAddress
& 0xFF);
238 /* Check if polling is possible, if not switch to timed delay mode */
241 Write_Memory_Params
.ProgrammingMode
&= ~PROG_MODE_PAGED_VALUE_MASK
;
242 Write_Memory_Params
.ProgrammingMode
|= PROG_MODE_PAGED_TIMEDELAY_MASK
;
245 ProgrammingStatus
= ISPTarget_WaitForProgComplete(Write_Memory_Params
.ProgrammingMode
, PollAddress
, PollValue
,
246 Write_Memory_Params
.DelayMS
, Write_Memory_Params
.ProgrammingCommands
[2]);
251 /* Word/byte mode memory programming */
252 for (uint16_t CurrentByte
= 0; CurrentByte
< Write_Memory_Params
.BytesToWrite
; CurrentByte
++)
254 bool IsOddByte
= (CurrentByte
& 0x01);
255 uint8_t ByteToWrite
= *(NextWriteByte
++);
257 SPI_SendByte(Write_Memory_Params
.ProgrammingCommands
[0]);
258 SPI_SendByte(CurrentAddress
>> 8);
259 SPI_SendByte(CurrentAddress
& 0xFF);
260 SPI_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 if (ByteToWrite
!= PollValue
)
269 if (IsOddByte
&& (V2Command
== CMD_PROGRAM_FLASH_ISP
))
270 Write_Memory_Params
.ProgrammingCommands
[2] |= READ_WRITE_HIGH_BYTE_MASK
;
272 PollAddress
= (CurrentAddress
& 0xFFFF);
275 if (IsOddByte
|| (V2Command
== CMD_PROGRAM_EEPROM_ISP
))
278 ProgrammingStatus
= ISPTarget_WaitForProgComplete(Write_Memory_Params
.ProgrammingMode
, PollAddress
, PollValue
,
279 Write_Memory_Params
.DelayMS
, Write_Memory_Params
.ProgrammingCommands
[2]);
281 if (ProgrammingStatus
!= STATUS_CMD_OK
)
286 Endpoint_Write_Byte(V2Command
);
287 Endpoint_Write_Byte(ProgrammingStatus
);
291 /** Handler for the CMD_READ_FLASH_ISP and CMD_READ_EEPROM_ISP commands, reading in bytes,
292 * words or pages of data from the attached device.
294 * \param[in] V2Command Issued V2 Protocol command byte from the host
296 void ISPProtocol_ReadMemory(uint8_t V2Command
)
300 uint16_t BytesToRead
;
301 uint8_t ReadMemoryCommand
;
302 } Read_Memory_Params
;
304 Endpoint_Read_Stream_LE(&Read_Memory_Params
, sizeof(Read_Memory_Params
), NO_STREAM_CALLBACK
);
305 Read_Memory_Params
.BytesToRead
= SwapEndian_16(Read_Memory_Params
.BytesToRead
);
308 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
310 Endpoint_Write_Byte(V2Command
);
311 Endpoint_Write_Byte(STATUS_CMD_OK
);
313 /* Check to see if the host has issued a SET ADDRESS command and we haven't sent a
314 * LOAD EXTENDED ADDRESS command (if needed, used when operating beyond the 128KB
318 if (CurrentAddress
& (1UL << 31))
319 ISPTarget_LoadExtendedAddress();
321 MustSetAddress
= false;
324 /* Read each byte from the device and write them to the packet for the host */
325 for (uint16_t CurrentByte
= 0; CurrentByte
< Read_Memory_Params
.BytesToRead
; CurrentByte
++)
327 /* Read the next byte from the desired memory space in the device */
328 SPI_SendByte(Read_Memory_Params
.ReadMemoryCommand
);
329 SPI_SendByte(CurrentAddress
>> 8);
330 SPI_SendByte(CurrentAddress
& 0xFF);
331 Endpoint_Write_Byte(SPI_ReceiveByte());
333 /* Check if the endpoint bank is currently full, if so send the packet */
334 if (!(Endpoint_IsReadWriteAllowed()))
337 Endpoint_WaitUntilReady();
340 /* AVR FLASH addressing requires us to modify the read command based on if we are reading a high
341 * or low byte at the current word address */
342 if (V2Command
== CMD_READ_FLASH_ISP
)
343 Read_Memory_Params
.ReadMemoryCommand
^= READ_WRITE_HIGH_BYTE_MASK
;
345 /* Only increment the current address if we have read both bytes in the current word when in FLASH
346 * read mode, or for each byte when in EEPROM read mode */
347 if (((CurrentByte
& 0x01) && (V2Command
== CMD_READ_FLASH_ISP
)) || (V2Command
== CMD_READ_EEPROM_ISP
))
351 Endpoint_Write_Byte(STATUS_CMD_OK
);
353 bool IsEndpointFull
= !(Endpoint_IsReadWriteAllowed());
356 /* Ensure last packet is a short packet to terminate the transfer */
359 Endpoint_WaitUntilReady();
361 Endpoint_WaitUntilReady();
365 /** Handler for the CMD_CHI_ERASE_ISP command, clearing the target's FLASH memory. */
366 void ISPProtocol_ChipErase(void)
370 uint8_t EraseDelayMS
;
372 uint8_t EraseCommandBytes
[4];
375 Endpoint_Read_Stream_LE(&Erase_Chip_Params
, sizeof(Erase_Chip_Params
), NO_STREAM_CALLBACK
);
378 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
380 uint8_t ResponseStatus
= STATUS_CMD_OK
;
382 /* Send the chip erase commands as given by the host to the device */
383 for (uint8_t SByte
= 0; SByte
< sizeof(Erase_Chip_Params
.EraseCommandBytes
); SByte
++)
384 SPI_SendByte(Erase_Chip_Params
.EraseCommandBytes
[SByte
]);
386 /* Use appropriate command completion check as given by the host (delay or busy polling) */
387 if (!(Erase_Chip_Params
.PollMethod
))
388 ISPProtocol_DelayMS(Erase_Chip_Params
.EraseDelayMS
);
390 ResponseStatus
= ISPTarget_WaitWhileTargetBusy();
392 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP
);
393 Endpoint_Write_Byte(ResponseStatus
);
397 /** Handler for the CMD_READ_FUSE_ISP, CMD_READ_LOCK_ISP, CMD_READ_SIGNATURE_ISP and CMD_READ_OSCCAL commands,
398 * reading the requested configuration byte from the device.
400 * \param[in] V2Command Issued V2 Protocol command byte from the host
402 void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command
)
407 uint8_t ReadCommandBytes
[4];
408 } Read_FuseLockSigOSCCAL_Params
;
410 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params
, sizeof(Read_FuseLockSigOSCCAL_Params
), NO_STREAM_CALLBACK
);
413 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
415 uint8_t ResponseBytes
[4];
417 /* Send the Fuse or Lock byte read commands as given by the host to the device, store response */
418 for (uint8_t RByte
= 0; RByte
< sizeof(ResponseBytes
); RByte
++)
419 ResponseBytes
[RByte
] = SPI_TransferByte(Read_FuseLockSigOSCCAL_Params
.ReadCommandBytes
[RByte
]);
421 Endpoint_Write_Byte(V2Command
);
422 Endpoint_Write_Byte(STATUS_CMD_OK
);
423 Endpoint_Write_Byte(ResponseBytes
[Read_FuseLockSigOSCCAL_Params
.RetByte
- 1]);
424 Endpoint_Write_Byte(STATUS_CMD_OK
);
428 /** Handler for the CMD_WRITE_FUSE_ISP and CMD_WRITE_LOCK_ISP commands, writing the requested configuration
429 * byte to the device.
431 * \param[in] V2Command Issued V2 Protocol command byte from the host
433 void ISPProtocol_WriteFuseLock(uint8_t V2Command
)
437 uint8_t WriteCommandBytes
[4];
438 } Write_FuseLockSig_Params
;
440 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params
, sizeof(Write_FuseLockSig_Params
), NO_STREAM_CALLBACK
);
443 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
445 /* Send the Fuse or Lock byte program commands as given by the host to the device */
446 for (uint8_t SByte
= 0; SByte
< sizeof(Write_FuseLockSig_Params
.WriteCommandBytes
); SByte
++)
447 SPI_SendByte(Write_FuseLockSig_Params
.WriteCommandBytes
[SByte
]);
449 Endpoint_Write_Byte(V2Command
);
450 Endpoint_Write_Byte(STATUS_CMD_OK
);
451 Endpoint_Write_Byte(STATUS_CMD_OK
);
455 /** Handler for the CMD_SPI_MULTI command, writing and reading arbitrary SPI data to and from the attached device. */
456 void ISPProtocol_SPIMulti(void)
466 Endpoint_Read_Stream_LE(&SPI_Multi_Params
, (sizeof(SPI_Multi_Params
) - sizeof(SPI_Multi_Params
.TxData
)), NO_STREAM_CALLBACK
);
467 Endpoint_Read_Stream_LE(&SPI_Multi_Params
.TxData
, SPI_Multi_Params
.TxBytes
, NO_STREAM_CALLBACK
);
470 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
472 Endpoint_Write_Byte(CMD_SPI_MULTI
);
473 Endpoint_Write_Byte(STATUS_CMD_OK
);
475 uint8_t CurrTxPos
= 0;
476 uint8_t CurrRxPos
= 0;
478 /* Write out bytes to transmit until the start of the bytes to receive is met */
479 while (CurrTxPos
< SPI_Multi_Params
.RxStartAddr
)
481 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
482 SPI_SendByte(SPI_Multi_Params
.TxData
[CurrTxPos
]);
489 /* Transmit remaining bytes with padding as needed, read in response bytes */
490 while (CurrRxPos
< SPI_Multi_Params
.RxBytes
)
492 if (CurrTxPos
< SPI_Multi_Params
.TxBytes
)
493 Endpoint_Write_Byte(SPI_TransferByte(SPI_Multi_Params
.TxData
[CurrTxPos
++]));
495 Endpoint_Write_Byte(SPI_ReceiveByte());
497 /* Check to see if we have filled the endpoint bank and need to send the packet */
498 if (!(Endpoint_IsReadWriteAllowed()))
501 Endpoint_WaitUntilReady();
507 Endpoint_Write_Byte(STATUS_CMD_OK
);
509 bool IsEndpointFull
= !(Endpoint_IsReadWriteAllowed());
512 /* Ensure last packet is a short packet to terminate the transfer */
515 Endpoint_WaitUntilReady();
517 Endpoint_WaitUntilReady();
521 /** Blocking delay for a given number of milliseconds.
523 * \param[in] DelayMS Number of milliseconds to delay for
525 void ISPProtocol_DelayMS(uint8_t DelayMS
)
527 while (DelayMS
-- && TimeoutMSRemaining
)
529 if (TimeoutMSRemaining
)
530 TimeoutMSRemaining
--;