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_SelectEndpoint(AVRISP_DATA_IN_EPNUM
); 
  61         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
  63         uint8_t ResponseStatus 
= STATUS_CMD_FAILED
; 
  67         /* Set up the synchronous USART to generate the .5MHz recovery clock on XCK pin */ 
  68         UBRR1  
= (F_CPU 
/ 500000UL); 
  69         UCSR1B 
= (1 << TXEN1
); 
  70         UCSR1C 
= (1 << UMSEL10
) | (1 << UPM11
) | (1 << USBS1
) | (1 << UCSZ11
) | (1 << UCSZ10
) | (1 << UCPOL1
); 
  73         /* Perform execution delay, initialize SPI bus */ 
  74         ISPProtocol_DelayMS(Enter_ISP_Params
.ExecutionDelayMS
); 
  77         /* Continuously attempt to synchronize with the target until either the number of attempts specified 
  78          * by the host has exceeded, or the the device sends back the expected response values */ 
  79         while (Enter_ISP_Params
.SynchLoops
-- && (ResponseStatus 
== STATUS_CMD_FAILED
) && TimeoutTicksRemaining
) 
  81                 uint8_t ResponseBytes
[4]; 
  83                 ISPTarget_ChangeTargetResetLine(true); 
  84                 ISPProtocol_DelayMS(Enter_ISP_Params
.PinStabDelayMS
); 
  86                 for (uint8_t RByte 
= 0; RByte 
< sizeof(ResponseBytes
); RByte
++) 
  88                         ISPProtocol_DelayMS(Enter_ISP_Params
.ByteDelay
); 
  89                         ResponseBytes
[RByte
] = ISPTarget_TransferByte(Enter_ISP_Params
.EnterProgBytes
[RByte
]); 
  92                 /* Check if polling disabled, or if the polled value matches the expected value */ 
  93                 if (!(Enter_ISP_Params
.PollIndex
) || (ResponseBytes
[Enter_ISP_Params
.PollIndex 
- 1] == Enter_ISP_Params
.PollValue
)) 
  95                         ResponseStatus 
= STATUS_CMD_OK
; 
  99                         ISPTarget_ChangeTargetResetLine(false); 
 100                         ISPProtocol_DelayMS(Enter_ISP_Params
.PinStabDelayMS
); 
 104         Endpoint_Write_Byte(CMD_ENTER_PROGMODE_ISP
); 
 105         Endpoint_Write_Byte(ResponseStatus
); 
 109 /** Handler for the CMD_LEAVE_ISP command, which releases the target from programming mode. */ 
 110 void ISPProtocol_LeaveISPMode(void) 
 118         Endpoint_Read_Stream_LE(&Leave_ISP_Params
, sizeof(Leave_ISP_Params
), NO_STREAM_CALLBACK
); 
 121         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
); 
 122         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 124         /* Perform pre-exit delay, release the target /RESET, disable the SPI bus and perform the post-exit delay */ 
 125         ISPProtocol_DelayMS(Leave_ISP_Params
.PreDelayMS
); 
 126         ISPTarget_ChangeTargetResetLine(false); 
 127         ISPTarget_ShutDown(); 
 128         ISPProtocol_DelayMS(Leave_ISP_Params
.PostDelayMS
); 
 130         /* Turn off the synchronous USART to terminate the recovery clock on XCK pin */ 
 131         UBRR1  
= (F_CPU 
/ 500000UL); 
 132         UCSR1B 
= (1 << TXEN1
); 
 133         UCSR1C 
= (1 << UMSEL10
) | (1 << UPM11
) | (1 << USBS1
) | (1 << UCSZ11
) | (1 << UCSZ10
) | (1 << UCPOL1
); 
 136         Endpoint_Write_Byte(CMD_LEAVE_PROGMODE_ISP
); 
 137         Endpoint_Write_Byte(STATUS_CMD_OK
); 
 141 /** Handler for the CMD_PROGRAM_FLASH_ISP and CMD_PROGRAM_EEPROM_ISP commands, writing out bytes, 
 142  *  words or pages of data to the attached device. 
 144  *  \param[in] V2Command  Issued V2 Protocol command byte from the host 
 146 void ISPProtocol_ProgramMemory(uint8_t V2Command
) 
 150                 uint16_t BytesToWrite
; 
 151                 uint8_t  ProgrammingMode
; 
 153                 uint8_t  ProgrammingCommands
[3]; 
 156                 uint8_t  ProgData
[256]; // Note, the Jungo driver has a very short ACK timeout period, need to buffer the 
 157         } Write_Memory_Params
;      // whole page and ACK the packet as fast as possible to prevent it from aborting 
 159         Endpoint_Read_Stream_LE(&Write_Memory_Params
, (sizeof(Write_Memory_Params
) - 
 160                                                        sizeof(Write_Memory_Params
.ProgData
)), NO_STREAM_CALLBACK
); 
 163         Write_Memory_Params
.BytesToWrite 
= SwapEndian_16(Write_Memory_Params
.BytesToWrite
); 
 165         if (Write_Memory_Params
.BytesToWrite 
> sizeof(Write_Memory_Params
.ProgData
)) 
 168                 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
); 
 169                 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 171                 Endpoint_Write_Byte(V2Command
); 
 172                 Endpoint_Write_Byte(STATUS_CMD_FAILED
); 
 177         Endpoint_Read_Stream_LE(&Write_Memory_Params
.ProgData
, Write_Memory_Params
.BytesToWrite
, NO_STREAM_CALLBACK
); 
 180         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
); 
 181         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 183         uint8_t  ProgrammingStatus 
= STATUS_CMD_OK
; 
 184         uint16_t PollAddress       
= 0; 
 185         uint8_t  PollValue         
= (V2Command 
== CMD_PROGRAM_FLASH_ISP
) ? Write_Memory_Params
.PollValue1 
: 
 186                                                                             Write_Memory_Params
.PollValue2
; 
 187         uint8_t* NextWriteByte     
= Write_Memory_Params
.ProgData
; 
 189         /* Check the programming mode desired by the host, either Paged or Word memory writes */ 
 190         if (Write_Memory_Params
.ProgrammingMode 
& PROG_MODE_PAGED_WRITES_MASK
) 
 192                 uint16_t StartAddress 
= (CurrentAddress 
& 0xFFFF); 
 194                 /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */ 
 195                 if (MustLoadExtendedAddress
) 
 197                         ISPTarget_LoadExtendedAddress(); 
 198                         MustLoadExtendedAddress 
= false; 
 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                         ISPTarget_SendByte(Write_Memory_Params
.ProgrammingCommands
[0]); 
 208                         ISPTarget_SendByte(CurrentAddress 
>> 8); 
 209                         ISPTarget_SendByte(CurrentAddress 
& 0xFF); 
 210                         ISPTarget_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                         /* EEPROM increments the address on each byte, flash needs to increment on each word */ 
 227                         if (IsOddByte 
|| (V2Command 
== CMD_PROGRAM_EEPROM_ISP
)) 
 231                 /* If the current page must be committed, send the PROGRAM PAGE command to the target */ 
 232                 if (Write_Memory_Params
.ProgrammingMode 
& PROG_MODE_COMMIT_PAGE_MASK
) 
 234                         ISPTarget_SendByte(Write_Memory_Params
.ProgrammingCommands
[1]); 
 235                         ISPTarget_SendByte(StartAddress 
>> 8); 
 236                         ISPTarget_SendByte(StartAddress 
& 0xFF); 
 237                         ISPTarget_SendByte(0x00); 
 239                         /* Check if polling is possible and enabled, if not switch to timed delay mode */ 
 240                         if (!(PollAddress
) && (Write_Memory_Params
.ProgrammingMode 
& PROG_MODE_PAGED_VALUE_MASK
)) 
 242                                 Write_Memory_Params
.ProgrammingMode 
&= ~PROG_MODE_PAGED_VALUE_MASK
; 
 243                                 Write_Memory_Params
.ProgrammingMode 
|=  PROG_MODE_PAGED_TIMEDELAY_MASK
; 
 246                         ProgrammingStatus 
= ISPTarget_WaitForProgComplete(Write_Memory_Params
.ProgrammingMode
, PollAddress
, PollValue
, 
 247                                                                           Write_Memory_Params
.DelayMS
, Write_Memory_Params
.ProgrammingCommands
[2]); 
 249                         /* Check to see if the FLASH address has crossed the extended address boundary */ 
 250                         if ((V2Command 
== CMD_PROGRAM_FLASH_ISP
) && !(CurrentAddress 
& 0xFFFF)) 
 251                           MustLoadExtendedAddress 
= true; 
 256                 /* Word/byte mode memory programming */ 
 257                 for (uint16_t CurrentByte 
= 0; CurrentByte 
< Write_Memory_Params
.BytesToWrite
; CurrentByte
++) 
 259                         bool    IsOddByte   
= (CurrentByte 
& 0x01); 
 260                         uint8_t ByteToWrite 
= *(NextWriteByte
++); 
 262                         /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */ 
 263                         if (MustLoadExtendedAddress
) 
 265                                 ISPTarget_LoadExtendedAddress(); 
 266                                 MustLoadExtendedAddress 
= false; 
 269                         ISPTarget_SendByte(Write_Memory_Params
.ProgrammingCommands
[0]); 
 270                         ISPTarget_SendByte(CurrentAddress 
>> 8); 
 271                         ISPTarget_SendByte(CurrentAddress 
& 0xFF); 
 272                         ISPTarget_SendByte(ByteToWrite
); 
 274                         /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high 
 275                          * or low byte at the current word address */ 
 276                         if (V2Command 
== CMD_PROGRAM_FLASH_ISP
) 
 277                           Write_Memory_Params
.ProgrammingCommands
[0] ^= READ_WRITE_HIGH_BYTE_MASK
; 
 279                         /* Save previous programming mode in case we modify it for the current word */ 
 280                         uint8_t PreviousProgrammingMode 
= Write_Memory_Params
.ProgrammingMode
; 
 282                         if (ByteToWrite 
!= PollValue
) 
 284                                 if (IsOddByte 
&& (V2Command 
== CMD_PROGRAM_FLASH_ISP
)) 
 285                                   Write_Memory_Params
.ProgrammingCommands
[2] |= READ_WRITE_HIGH_BYTE_MASK
; 
 287                                 PollAddress 
= (CurrentAddress 
& 0xFFFF); 
 289                         else if (!(Write_Memory_Params
.ProgrammingMode 
& PROG_MODE_WORD_READYBUSY_MASK
)) 
 291                                 Write_Memory_Params
.ProgrammingMode 
&= ~PROG_MODE_WORD_VALUE_MASK
; 
 292                                 Write_Memory_Params
.ProgrammingMode 
|=  PROG_MODE_WORD_TIMEDELAY_MASK
; 
 295                         ProgrammingStatus 
= ISPTarget_WaitForProgComplete(Write_Memory_Params
.ProgrammingMode
, PollAddress
, PollValue
, 
 296                                                                           Write_Memory_Params
.DelayMS
, Write_Memory_Params
.ProgrammingCommands
[2]); 
 298                         /* Restore previous programming mode mask in case the current word needed to change it */ 
 299                         Write_Memory_Params
.ProgrammingMode 
= PreviousProgrammingMode
; 
 301                         /* Abort the programming loop early if the byte/word programming failed */ 
 302                         if (ProgrammingStatus 
!= STATUS_CMD_OK
) 
 305                         /* EEPROM just increments the address each byte, flash needs to increment on each word and 
 306                          * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended 
 307                          * address boundary has been crossed */ 
 308                         if ((CurrentByte 
& 0x01) || (V2Command 
== CMD_PROGRAM_EEPROM_ISP
)) 
 312                                 if ((V2Command 
!= CMD_PROGRAM_EEPROM_ISP
) && !(CurrentAddress 
& 0xFFFF)) 
 313                                   MustLoadExtendedAddress 
= true; 
 318         Endpoint_Write_Byte(V2Command
); 
 319         Endpoint_Write_Byte(ProgrammingStatus
); 
 323 /** Handler for the CMD_READ_FLASH_ISP and CMD_READ_EEPROM_ISP commands, reading in bytes, 
 324  *  words or pages of data from the attached device. 
 326  *  \param[in] V2Command  Issued V2 Protocol command byte from the host 
 328 void ISPProtocol_ReadMemory(uint8_t V2Command
) 
 332                 uint16_t BytesToRead
; 
 333                 uint8_t  ReadMemoryCommand
; 
 334         } Read_Memory_Params
; 
 336         Endpoint_Read_Stream_LE(&Read_Memory_Params
, sizeof(Read_Memory_Params
), NO_STREAM_CALLBACK
); 
 337         Read_Memory_Params
.BytesToRead 
= SwapEndian_16(Read_Memory_Params
.BytesToRead
); 
 340         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
); 
 341         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 343         Endpoint_Write_Byte(V2Command
); 
 344         Endpoint_Write_Byte(STATUS_CMD_OK
); 
 346         /* Read each byte from the device and write them to the packet for the host */ 
 347         for (uint16_t CurrentByte 
= 0; CurrentByte 
< Read_Memory_Params
.BytesToRead
; CurrentByte
++) 
 349                 /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */ 
 350                 if (MustLoadExtendedAddress
) 
 352                         ISPTarget_LoadExtendedAddress(); 
 353                         MustLoadExtendedAddress 
= false; 
 356                 /* Read the next byte from the desired memory space in the device */ 
 357                 ISPTarget_SendByte(Read_Memory_Params
.ReadMemoryCommand
); 
 358                 ISPTarget_SendByte(CurrentAddress 
>> 8); 
 359                 ISPTarget_SendByte(CurrentAddress 
& 0xFF); 
 360                 Endpoint_Write_Byte(ISPTarget_ReceiveByte()); 
 362                 /* Check if the endpoint bank is currently full, if so send the packet */ 
 363                 if (!(Endpoint_IsReadWriteAllowed())) 
 366                         Endpoint_WaitUntilReady(); 
 369                 /* AVR FLASH addressing requires us to modify the read command based on if we are reading a high 
 370                  * or low byte at the current word address */ 
 371                 if (V2Command 
== CMD_READ_FLASH_ISP
) 
 372                   Read_Memory_Params
.ReadMemoryCommand 
^= READ_WRITE_HIGH_BYTE_MASK
; 
 374                 /* EEPROM just increments the address each byte, flash needs to increment on each word and 
 375                  * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended 
 376                  * address boundary has been crossed */ 
 377                 if ((CurrentByte 
& 0x01) || (V2Command 
== CMD_READ_EEPROM_ISP
)) 
 381                         if ((V2Command 
!= CMD_READ_EEPROM_ISP
) && !(CurrentAddress 
& 0xFFFF)) 
 382                           MustLoadExtendedAddress 
= true; 
 386         Endpoint_Write_Byte(STATUS_CMD_OK
); 
 388         bool IsEndpointFull 
= !(Endpoint_IsReadWriteAllowed()); 
 391         /* Ensure last packet is a short packet to terminate the transfer */ 
 394                 Endpoint_WaitUntilReady(); 
 396                 Endpoint_WaitUntilReady(); 
 400 /** Handler for the CMD_CHI_ERASE_ISP command, clearing the target's FLASH memory. */ 
 401 void ISPProtocol_ChipErase(void) 
 405                 uint8_t EraseDelayMS
; 
 407                 uint8_t EraseCommandBytes
[4]; 
 410         Endpoint_Read_Stream_LE(&Erase_Chip_Params
, sizeof(Erase_Chip_Params
), NO_STREAM_CALLBACK
); 
 413         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
); 
 414         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 416         uint8_t ResponseStatus 
= STATUS_CMD_OK
; 
 418         /* Send the chip erase commands as given by the host to the device */ 
 419         for (uint8_t SByte 
= 0; SByte 
< sizeof(Erase_Chip_Params
.EraseCommandBytes
); SByte
++) 
 420           ISPTarget_SendByte(Erase_Chip_Params
.EraseCommandBytes
[SByte
]); 
 422         /* Use appropriate command completion check as given by the host (delay or busy polling) */ 
 423         if (!(Erase_Chip_Params
.PollMethod
)) 
 424           ISPProtocol_DelayMS(Erase_Chip_Params
.EraseDelayMS
); 
 426           ResponseStatus 
= ISPTarget_WaitWhileTargetBusy(); 
 428         Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP
); 
 429         Endpoint_Write_Byte(ResponseStatus
); 
 433 /** Handler for the CMD_READ_FUSE_ISP, CMD_READ_LOCK_ISP, CMD_READ_SIGNATURE_ISP and CMD_READ_OSCCAL commands, 
 434  *  reading the requested configuration byte from the device. 
 436  *  \param[in] V2Command  Issued V2 Protocol command byte from the host 
 438 void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command
) 
 443                 uint8_t ReadCommandBytes
[4]; 
 444         } Read_FuseLockSigOSCCAL_Params
; 
 446         Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params
, sizeof(Read_FuseLockSigOSCCAL_Params
), NO_STREAM_CALLBACK
); 
 449         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
); 
 450         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 452         uint8_t ResponseBytes
[4]; 
 454         /* Send the Fuse or Lock byte read commands as given by the host to the device, store response */ 
 455         for (uint8_t RByte 
= 0; RByte 
< sizeof(ResponseBytes
); RByte
++) 
 456           ResponseBytes
[RByte
] = ISPTarget_TransferByte(Read_FuseLockSigOSCCAL_Params
.ReadCommandBytes
[RByte
]); 
 458         Endpoint_Write_Byte(V2Command
); 
 459         Endpoint_Write_Byte(STATUS_CMD_OK
); 
 460         Endpoint_Write_Byte(ResponseBytes
[Read_FuseLockSigOSCCAL_Params
.RetByte 
- 1]); 
 461         Endpoint_Write_Byte(STATUS_CMD_OK
); 
 465 /** Handler for the CMD_WRITE_FUSE_ISP and CMD_WRITE_LOCK_ISP commands, writing the requested configuration 
 466  *  byte to the device. 
 468  *  \param[in] V2Command  Issued V2 Protocol command byte from the host 
 470 void ISPProtocol_WriteFuseLock(uint8_t V2Command
) 
 474                 uint8_t WriteCommandBytes
[4]; 
 475         } Write_FuseLockSig_Params
; 
 477         Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params
, sizeof(Write_FuseLockSig_Params
), NO_STREAM_CALLBACK
); 
 480         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
); 
 481         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 483         /* Send the Fuse or Lock byte program commands as given by the host to the device */ 
 484         for (uint8_t SByte 
= 0; SByte 
< sizeof(Write_FuseLockSig_Params
.WriteCommandBytes
); SByte
++) 
 485           ISPTarget_SendByte(Write_FuseLockSig_Params
.WriteCommandBytes
[SByte
]); 
 487         Endpoint_Write_Byte(V2Command
); 
 488         Endpoint_Write_Byte(STATUS_CMD_OK
); 
 489         Endpoint_Write_Byte(STATUS_CMD_OK
); 
 493 /** Handler for the CMD_SPI_MULTI command, writing and reading arbitrary SPI data to and from the attached device. */ 
 494 void ISPProtocol_SPIMulti(void) 
 504         Endpoint_Read_Stream_LE(&SPI_Multi_Params
, (sizeof(SPI_Multi_Params
) - sizeof(SPI_Multi_Params
.TxData
)), NO_STREAM_CALLBACK
); 
 505         Endpoint_Read_Stream_LE(&SPI_Multi_Params
.TxData
, SPI_Multi_Params
.TxBytes
, NO_STREAM_CALLBACK
); 
 508         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM
); 
 509         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
); 
 511         Endpoint_Write_Byte(CMD_SPI_MULTI
); 
 512         Endpoint_Write_Byte(STATUS_CMD_OK
); 
 514         uint8_t CurrTxPos 
= 0; 
 515         uint8_t CurrRxPos 
= 0; 
 517         /* Write out bytes to transmit until the start of the bytes to receive is met */ 
 518         while (CurrTxPos 
< SPI_Multi_Params
.RxStartAddr
) 
 520                 if (CurrTxPos 
< SPI_Multi_Params
.TxBytes
) 
 521                   ISPTarget_SendByte(SPI_Multi_Params
.TxData
[CurrTxPos
]); 
 523                   ISPTarget_SendByte(0); 
 528         /* Transmit remaining bytes with padding as needed, read in response bytes */ 
 529         while (CurrRxPos 
< SPI_Multi_Params
.RxBytes
) 
 531                 if (CurrTxPos 
< SPI_Multi_Params
.TxBytes
) 
 532                   Endpoint_Write_Byte(ISPTarget_TransferByte(SPI_Multi_Params
.TxData
[CurrTxPos
++])); 
 534                   Endpoint_Write_Byte(ISPTarget_ReceiveByte()); 
 536                 /* Check to see if we have filled the endpoint bank and need to send the packet */ 
 537                 if (!(Endpoint_IsReadWriteAllowed())) 
 540                         Endpoint_WaitUntilReady(); 
 546         Endpoint_Write_Byte(STATUS_CMD_OK
); 
 548         bool IsEndpointFull 
= !(Endpoint_IsReadWriteAllowed()); 
 551         /* Ensure last packet is a short packet to terminate the transfer */ 
 554                 Endpoint_WaitUntilReady(); 
 556                 Endpoint_WaitUntilReady(); 
 560 /** Blocking delay for a given number of milliseconds. 
 562  *  \param[in] DelayMS  Number of milliseconds to delay for 
 564 void ISPProtocol_DelayMS(uint8_t DelayMS
) 
 566         while (DelayMS
-- && TimeoutTicksRemaining
)