]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / lib / lufa / Projects / AVRISP-MKII / Lib / ISP / ISPProtocol.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
21   The author disclaims 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
28   this software.
29 */
30
31 /** \file
32  *
33  *  ISP Protocol handler, to process V2 Protocol wrapped ISP commands used in Atmel programmer devices.
34  */
35
36 #include "ISPProtocol.h"
37
38 #if defined(ENABLE_ISP_PROTOCOL) || defined(__DOXYGEN__)
39
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.
42  */
43 void ISPProtocol_EnterISPMode(void)
44 {
45         struct
46         {
47                 uint8_t TimeoutMS;
48                 uint8_t PinStabDelayMS;
49                 uint8_t ExecutionDelayMS;
50                 uint8_t SynchLoops;
51                 uint8_t ByteDelay;
52                 uint8_t PollValue;
53                 uint8_t PollIndex;
54                 uint8_t EnterProgBytes[4];
55         } Enter_ISP_Params;
56
57         Endpoint_Read_Stream_LE(&Enter_ISP_Params, sizeof(Enter_ISP_Params), NULL);
58
59         Endpoint_ClearOUT();
60         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
61         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
62
63         uint8_t ResponseStatus = STATUS_CMD_FAILED;
64
65         CurrentAddress = 0;
66
67         /* Perform execution delay, initialize SPI bus */
68         ISPProtocol_DelayMS(Enter_ISP_Params.ExecutionDelayMS);
69         ISPTarget_EnableTargetISP();
70
71         ISPTarget_ChangeTargetResetLine(true);
72         ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
73
74         /* Continuously attempt to synchronize with the target until either the number of attempts specified
75          * by the host has exceeded, or the the device sends back the expected response values */
76         while (Enter_ISP_Params.SynchLoops-- && TimeoutTicksRemaining)
77         {
78                 uint8_t ResponseBytes[4];
79
80                 for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)
81                 {
82                         ISPProtocol_DelayMS(Enter_ISP_Params.ByteDelay);
83                         ResponseBytes[RByte] = ISPTarget_TransferByte(Enter_ISP_Params.EnterProgBytes[RByte]);
84                 }
85
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))
88                 {
89                         ResponseStatus = STATUS_CMD_OK;
90                         break;
91                 }
92                 else
93                 {
94                         ISPTarget_ChangeTargetResetLine(false);
95                         ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
96                         ISPTarget_ChangeTargetResetLine(true);
97                         ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
98                 }
99         }
100
101         Endpoint_Write_8(CMD_ENTER_PROGMODE_ISP);
102         Endpoint_Write_8(ResponseStatus);
103         Endpoint_ClearIN();
104 }
105
106 /** Handler for the CMD_LEAVE_ISP command, which releases the target from programming mode. */
107 void ISPProtocol_LeaveISPMode(void)
108 {
109         struct
110         {
111                 uint8_t PreDelayMS;
112                 uint8_t PostDelayMS;
113         } Leave_ISP_Params;
114
115         Endpoint_Read_Stream_LE(&Leave_ISP_Params, sizeof(Leave_ISP_Params), NULL);
116
117         Endpoint_ClearOUT();
118         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
119         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
120
121         /* Perform pre-exit delay, release the target /RESET, disable the SPI bus and perform the post-exit delay */
122         ISPProtocol_DelayMS(Leave_ISP_Params.PreDelayMS);
123         ISPTarget_ChangeTargetResetLine(false);
124         ISPTarget_DisableTargetISP();
125         ISPProtocol_DelayMS(Leave_ISP_Params.PostDelayMS);
126
127         Endpoint_Write_8(CMD_LEAVE_PROGMODE_ISP);
128         Endpoint_Write_8(STATUS_CMD_OK);
129         Endpoint_ClearIN();
130 }
131
132 /** Handler for the CMD_PROGRAM_FLASH_ISP and CMD_PROGRAM_EEPROM_ISP commands, writing out bytes,
133  *  words or pages of data to the attached device.
134  *
135  *  \param[in] V2Command  Issued V2 Protocol command byte from the host
136  */
137 void ISPProtocol_ProgramMemory(uint8_t V2Command)
138 {
139         struct
140         {
141                 uint16_t BytesToWrite;
142                 uint8_t  ProgrammingMode;
143                 uint8_t  DelayMS;
144                 uint8_t  ProgrammingCommands[3];
145                 uint8_t  PollValue1;
146                 uint8_t  PollValue2;
147                 uint8_t  ProgData[256]; // Note, the Jungo driver has a very short ACK timeout period, need to buffer the
148         } Write_Memory_Params;      // whole page and ACK the packet as fast as possible to prevent it from aborting
149
150         Endpoint_Read_Stream_LE(&Write_Memory_Params, (sizeof(Write_Memory_Params) -
151                                                        sizeof(Write_Memory_Params.ProgData)), NULL);
152         Write_Memory_Params.BytesToWrite = SwapEndian_16(Write_Memory_Params.BytesToWrite);
153
154         if (Write_Memory_Params.BytesToWrite > sizeof(Write_Memory_Params.ProgData))
155         {
156                 Endpoint_ClearOUT();
157                 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
158                 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
159
160                 Endpoint_Write_8(V2Command);
161                 Endpoint_Write_8(STATUS_CMD_FAILED);
162                 Endpoint_ClearIN();
163                 return;
164         }
165
166         Endpoint_Read_Stream_LE(&Write_Memory_Params.ProgData, Write_Memory_Params.BytesToWrite, NULL);
167
168         // The driver will terminate transfers that are a round multiple of the endpoint bank in size with a ZLP, need
169         // to catch this and discard it before continuing on with packet processing to prevent communication issues
170         if (((sizeof(uint8_t) + sizeof(Write_Memory_Params) - sizeof(Write_Memory_Params.ProgData)) +
171             Write_Memory_Params.BytesToWrite) % AVRISP_DATA_EPSIZE == 0)
172         {
173                 Endpoint_ClearOUT();
174                 Endpoint_WaitUntilReady();
175         }
176
177         Endpoint_ClearOUT();
178         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
179         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
180
181         uint8_t  ProgrammingStatus = STATUS_CMD_OK;
182         uint8_t  PollValue         = (V2Command == CMD_PROGRAM_FLASH_ISP) ? Write_Memory_Params.PollValue1 :
183                                                                             Write_Memory_Params.PollValue2;
184         uint16_t PollAddress       = 0;
185         uint8_t* NextWriteByte     = Write_Memory_Params.ProgData;
186         uint16_t PageStartAddress  = (CurrentAddress & 0xFFFF);
187
188         for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)
189         {
190                 uint8_t ByteToWrite     = *(NextWriteByte++);
191                 uint8_t ProgrammingMode = Write_Memory_Params.ProgrammingMode;
192
193                 /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
194                 if (MustLoadExtendedAddress)
195                 {
196                         ISPTarget_LoadExtendedAddress();
197                         MustLoadExtendedAddress = false;
198                 }
199
200                 ISPTarget_SendByte(Write_Memory_Params.ProgrammingCommands[0]);
201                 ISPTarget_SendByte(CurrentAddress >> 8);
202                 ISPTarget_SendByte(CurrentAddress & 0xFF);
203                 ISPTarget_SendByte(ByteToWrite);
204
205                 /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high
206                  * or low byte at the current word address */
207                 if (V2Command == CMD_PROGRAM_FLASH_ISP)
208                   Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_HIGH_BYTE_MASK;
209
210                 /* Check to see if we have a valid polling address */
211                 if (!(PollAddress) && (ByteToWrite != PollValue))
212                 {
213                         if ((CurrentByte & 0x01) && (V2Command == CMD_PROGRAM_FLASH_ISP))
214                           Write_Memory_Params.ProgrammingCommands[2] |=  READ_WRITE_HIGH_BYTE_MASK;
215                         else
216                           Write_Memory_Params.ProgrammingCommands[2] &= ~READ_WRITE_HIGH_BYTE_MASK;
217
218                         PollAddress = (CurrentAddress & 0xFFFF);
219                 }
220
221                 /* If in word programming mode, commit the byte to the target's memory */
222                 if (!(ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK))
223                 {
224                         /* If the current polling address is invalid, switch to timed delay write completion mode */
225                         if (!(PollAddress) && !(ProgrammingMode & PROG_MODE_WORD_READYBUSY_MASK))
226                           ProgrammingMode = (ProgrammingMode & ~PROG_MODE_WORD_VALUE_MASK) | PROG_MODE_WORD_TIMEDELAY_MASK;
227
228                         ProgrammingStatus = ISPTarget_WaitForProgComplete(ProgrammingMode, PollAddress, PollValue,
229                                                                           Write_Memory_Params.DelayMS,
230                                                                           Write_Memory_Params.ProgrammingCommands[2]);
231
232                         /* Abort the programming loop early if the byte/word programming failed */
233                         if (ProgrammingStatus != STATUS_CMD_OK)
234                           break;
235
236                         /* Must reset the polling address afterwards, so it is not erroneously used for the next byte */
237                         PollAddress = 0;
238                 }
239
240                 /* EEPROM just increments the address each byte, flash needs to increment on each word and
241                  * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended
242                  * address boundary has been crossed during FLASH memory programming */
243                 if ((CurrentByte & 0x01) || (V2Command == CMD_PROGRAM_EEPROM_ISP))
244                 {
245                         CurrentAddress++;
246
247                         if ((V2Command == CMD_PROGRAM_FLASH_ISP) && !(CurrentAddress & 0xFFFF))
248                           MustLoadExtendedAddress = true;
249                 }
250         }
251
252         /* If the current page must be committed, send the PROGRAM PAGE command to the target */
253         if (Write_Memory_Params.ProgrammingMode & PROG_MODE_COMMIT_PAGE_MASK)
254         {
255                 ISPTarget_SendByte(Write_Memory_Params.ProgrammingCommands[1]);
256                 ISPTarget_SendByte(PageStartAddress >> 8);
257                 ISPTarget_SendByte(PageStartAddress & 0xFF);
258                 ISPTarget_SendByte(0x00);
259
260                 /* Check if polling is enabled and possible, if not switch to timed delay mode */
261                 if ((Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_VALUE_MASK) && !(PollAddress))
262                 {
263                         Write_Memory_Params.ProgrammingMode = (Write_Memory_Params.ProgrammingMode & ~PROG_MODE_PAGED_VALUE_MASK) |
264                                                                                                    PROG_MODE_PAGED_TIMEDELAY_MASK;
265                 }
266
267                 ProgrammingStatus = ISPTarget_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,
268                                                                   Write_Memory_Params.DelayMS,
269                                                                   Write_Memory_Params.ProgrammingCommands[2]);
270
271                 /* Check to see if the FLASH address has crossed the extended address boundary */
272                 if ((V2Command == CMD_PROGRAM_FLASH_ISP) && !(CurrentAddress & 0xFFFF))
273                   MustLoadExtendedAddress = true;
274         }
275
276         Endpoint_Write_8(V2Command);
277         Endpoint_Write_8(ProgrammingStatus);
278         Endpoint_ClearIN();
279 }
280
281 /** Handler for the CMD_READ_FLASH_ISP and CMD_READ_EEPROM_ISP commands, reading in bytes,
282  *  words or pages of data from the attached device.
283  *
284  *  \param[in] V2Command  Issued V2 Protocol command byte from the host
285  */
286 void ISPProtocol_ReadMemory(uint8_t V2Command)
287 {
288         struct
289         {
290                 uint16_t BytesToRead;
291                 uint8_t  ReadMemoryCommand;
292         } Read_Memory_Params;
293
294         Endpoint_Read_Stream_LE(&Read_Memory_Params, sizeof(Read_Memory_Params), NULL);
295         Read_Memory_Params.BytesToRead = SwapEndian_16(Read_Memory_Params.BytesToRead);
296
297         Endpoint_ClearOUT();
298         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
299         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
300
301         Endpoint_Write_8(V2Command);
302         Endpoint_Write_8(STATUS_CMD_OK);
303
304         /* Read each byte from the device and write them to the packet for the host */
305         for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++)
306         {
307                 /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
308                 if (MustLoadExtendedAddress)
309                 {
310                         ISPTarget_LoadExtendedAddress();
311                         MustLoadExtendedAddress = false;
312                 }
313
314                 /* Read the next byte from the desired memory space in the device */
315                 ISPTarget_SendByte(Read_Memory_Params.ReadMemoryCommand);
316                 ISPTarget_SendByte(CurrentAddress >> 8);
317                 ISPTarget_SendByte(CurrentAddress & 0xFF);
318                 Endpoint_Write_8(ISPTarget_ReceiveByte());
319
320                 /* Check if the endpoint bank is currently full, if so send the packet */
321                 if (!(Endpoint_IsReadWriteAllowed()))
322                 {
323                         Endpoint_ClearIN();
324                         Endpoint_WaitUntilReady();
325                 }
326
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;
331
332                 /* EEPROM just increments the address each byte, flash needs to increment on each word and
333                  * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended
334                  * address boundary has been crossed */
335                 if ((CurrentByte & 0x01) || (V2Command == CMD_READ_EEPROM_ISP))
336                 {
337                         CurrentAddress++;
338
339                         if ((V2Command != CMD_READ_EEPROM_ISP) && !(CurrentAddress & 0xFFFF))
340                           MustLoadExtendedAddress = true;
341                 }
342         }
343
344         Endpoint_Write_8(STATUS_CMD_OK);
345
346         bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed());
347         Endpoint_ClearIN();
348
349         /* Ensure last packet is a short packet to terminate the transfer */
350         if (IsEndpointFull)
351         {
352                 Endpoint_WaitUntilReady();
353                 Endpoint_ClearIN();
354                 Endpoint_WaitUntilReady();
355         }
356 }
357
358 /** Handler for the CMD_CHI_ERASE_ISP command, clearing the target's FLASH memory. */
359 void ISPProtocol_ChipErase(void)
360 {
361         struct
362         {
363                 uint8_t EraseDelayMS;
364                 uint8_t PollMethod;
365                 uint8_t EraseCommandBytes[4];
366         } Erase_Chip_Params;
367
368         Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params), NULL);
369
370         Endpoint_ClearOUT();
371         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
372         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
373
374         uint8_t ResponseStatus = STATUS_CMD_OK;
375
376         /* Send the chip erase commands as given by the host to the device */
377         for (uint8_t SByte = 0; SByte < sizeof(Erase_Chip_Params.EraseCommandBytes); SByte++)
378           ISPTarget_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]);
379
380         /* Use appropriate command completion check as given by the host (delay or busy polling) */
381         if (!(Erase_Chip_Params.PollMethod))
382           ISPProtocol_DelayMS(Erase_Chip_Params.EraseDelayMS);
383         else
384           ResponseStatus = ISPTarget_WaitWhileTargetBusy();
385
386         Endpoint_Write_8(CMD_CHIP_ERASE_ISP);
387         Endpoint_Write_8(ResponseStatus);
388         Endpoint_ClearIN();
389 }
390
391 /** Handler for the CMD_READ_FUSE_ISP, CMD_READ_LOCK_ISP, CMD_READ_SIGNATURE_ISP and CMD_READ_OSCCAL commands,
392  *  reading the requested configuration byte from the device.
393  *
394  *  \param[in] V2Command  Issued V2 Protocol command byte from the host
395  */
396 void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command)
397 {
398         struct
399         {
400                 uint8_t RetByte;
401                 uint8_t ReadCommandBytes[4];
402         } Read_FuseLockSigOSCCAL_Params;
403
404         Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params), NULL);
405
406         Endpoint_ClearOUT();
407         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
408         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
409
410         uint8_t ResponseBytes[4];
411
412         /* Send the Fuse or Lock byte read commands as given by the host to the device, store response */
413         for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)
414           ResponseBytes[RByte] = ISPTarget_TransferByte(Read_FuseLockSigOSCCAL_Params.ReadCommandBytes[RByte]);
415
416         Endpoint_Write_8(V2Command);
417         Endpoint_Write_8(STATUS_CMD_OK);
418         Endpoint_Write_8(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte - 1]);
419         Endpoint_Write_8(STATUS_CMD_OK);
420         Endpoint_ClearIN();
421 }
422
423 /** Handler for the CMD_WRITE_FUSE_ISP and CMD_WRITE_LOCK_ISP commands, writing the requested configuration
424  *  byte to the device.
425  *
426  *  \param[in] V2Command  Issued V2 Protocol command byte from the host
427  */
428 void ISPProtocol_WriteFuseLock(uint8_t V2Command)
429 {
430         struct
431         {
432                 uint8_t WriteCommandBytes[4];
433         } Write_FuseLockSig_Params;
434
435         Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params), NULL);
436
437         Endpoint_ClearOUT();
438         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
439         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
440
441         /* Send the Fuse or Lock byte program commands as given by the host to the device */
442         for (uint8_t SByte = 0; SByte < sizeof(Write_FuseLockSig_Params.WriteCommandBytes); SByte++)
443           ISPTarget_SendByte(Write_FuseLockSig_Params.WriteCommandBytes[SByte]);
444
445         Endpoint_Write_8(V2Command);
446         Endpoint_Write_8(STATUS_CMD_OK);
447         Endpoint_Write_8(STATUS_CMD_OK);
448         Endpoint_ClearIN();
449 }
450
451 /** Handler for the CMD_SPI_MULTI command, writing and reading arbitrary SPI data to and from the attached device. */
452 void ISPProtocol_SPIMulti(void)
453 {
454         struct
455         {
456                 uint8_t TxBytes;
457                 uint8_t RxBytes;
458                 uint8_t RxStartAddr;
459                 uint8_t TxData[255];
460         } SPI_Multi_Params;
461
462         Endpoint_Read_Stream_LE(&SPI_Multi_Params, (sizeof(SPI_Multi_Params) - sizeof(SPI_Multi_Params.TxData)), NULL);
463         Endpoint_Read_Stream_LE(&SPI_Multi_Params.TxData, SPI_Multi_Params.TxBytes, NULL);
464
465         Endpoint_ClearOUT();
466         Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
467         Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
468
469         Endpoint_Write_8(CMD_SPI_MULTI);
470         Endpoint_Write_8(STATUS_CMD_OK);
471
472         uint8_t CurrTxPos = 0;
473         uint8_t CurrRxPos = 0;
474
475         /* Write out bytes to transmit until the start of the bytes to receive is met */
476         while (CurrTxPos < SPI_Multi_Params.RxStartAddr)
477         {
478                 if (CurrTxPos < SPI_Multi_Params.TxBytes)
479                   ISPTarget_SendByte(SPI_Multi_Params.TxData[CurrTxPos]);
480                 else
481                   ISPTarget_SendByte(0);
482
483                 CurrTxPos++;
484         }
485
486         /* Transmit remaining bytes with padding as needed, read in response bytes */
487         while (CurrRxPos < SPI_Multi_Params.RxBytes)
488         {
489                 if (CurrTxPos < SPI_Multi_Params.TxBytes)
490                   Endpoint_Write_8(ISPTarget_TransferByte(SPI_Multi_Params.TxData[CurrTxPos++]));
491                 else
492                   Endpoint_Write_8(ISPTarget_ReceiveByte());
493
494                 /* Check to see if we have filled the endpoint bank and need to send the packet */
495                 if (!(Endpoint_IsReadWriteAllowed()))
496                 {
497                         Endpoint_ClearIN();
498                         Endpoint_WaitUntilReady();
499                 }
500
501                 CurrRxPos++;
502         }
503
504         Endpoint_Write_8(STATUS_CMD_OK);
505
506         bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed());
507         Endpoint_ClearIN();
508
509         /* Ensure last packet is a short packet to terminate the transfer */
510         if (IsEndpointFull)
511         {
512                 Endpoint_WaitUntilReady();
513                 Endpoint_ClearIN();
514                 Endpoint_WaitUntilReady();
515         }
516 }
517
518 /** Blocking delay for a given number of milliseconds. This provides a simple wrapper around
519  *  the avr-libc provided delay function, so that the delay function can be called with a
520  *  constant value (to prevent run-time floating point operations being required).
521  *
522  *  \param[in] DelayMS  Number of milliseconds to delay for
523  */
524 void ISPProtocol_DelayMS(uint8_t DelayMS)
525 {
526         while (DelayMS-- && TimeoutTicksRemaining)
527           Delay_MS(1);
528 }
529
530 #endif
531