e7d3e675c149ca9094a8059050fbf4a2a93d170c
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / ISP / ISPProtocol.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 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 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
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), NO_STREAM_CALLBACK);
58
59 Endpoint_ClearOUT();
60 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
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 /* 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)
74 {
75 uint8_t ResponseBytes[4];
76
77 ISPTarget_ChangeTargetResetLine(true);
78 ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
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 }
91 else
92 {
93 ISPTarget_ChangeTargetResetLine(false);
94 ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
95 }
96 }
97
98 Endpoint_Write_Byte(CMD_ENTER_PROGMODE_ISP);
99 Endpoint_Write_Byte(ResponseStatus);
100 Endpoint_ClearIN();
101 }
102
103 /** Handler for the CMD_LEAVE_ISP command, which releases the target from programming mode. */
104 void ISPProtocol_LeaveISPMode(void)
105 {
106 struct
107 {
108 uint8_t PreDelayMS;
109 uint8_t PostDelayMS;
110 } Leave_ISP_Params;
111
112 Endpoint_Read_Stream_LE(&Leave_ISP_Params, sizeof(Leave_ISP_Params), NO_STREAM_CALLBACK);
113
114 Endpoint_ClearOUT();
115 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
116 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
117
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);
123
124 Endpoint_Write_Byte(CMD_LEAVE_PROGMODE_ISP);
125 Endpoint_Write_Byte(STATUS_CMD_OK);
126 Endpoint_ClearIN();
127 }
128
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.
131 *
132 * \param[in] V2Command Issued V2 Protocol command byte from the host
133 */
134 void ISPProtocol_ProgramMemory(uint8_t V2Command)
135 {
136 struct
137 {
138 uint16_t BytesToWrite;
139 uint8_t ProgrammingMode;
140 uint8_t DelayMS;
141 uint8_t ProgrammingCommands[3];
142 uint8_t PollValue1;
143 uint8_t PollValue2;
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
146
147 Endpoint_Read_Stream_LE(&Write_Memory_Params, (sizeof(Write_Memory_Params) -
148 sizeof(Write_Memory_Params.ProgData)), NO_STREAM_CALLBACK);
149 Write_Memory_Params.BytesToWrite = SwapEndian_16(Write_Memory_Params.BytesToWrite);
150
151 if (Write_Memory_Params.BytesToWrite > sizeof(Write_Memory_Params.ProgData))
152 {
153 Endpoint_ClearOUT();
154 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
155 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
156
157 Endpoint_Write_Byte(V2Command);
158 Endpoint_Write_Byte(STATUS_CMD_FAILED);
159 Endpoint_ClearIN();
160 return;
161 }
162
163 Endpoint_Read_Stream_LE(&Write_Memory_Params.ProgData, Write_Memory_Params.BytesToWrite, NO_STREAM_CALLBACK);
164
165 Endpoint_ClearOUT();
166 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
167 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
168
169 uint8_t ProgrammingStatus = STATUS_CMD_OK;
170 uint16_t PollAddress = 0;
171 uint8_t PollValue = (V2Command == CMD_PROGRAM_FLASH_ISP) ? Write_Memory_Params.PollValue1 :
172 Write_Memory_Params.PollValue2;
173 uint8_t* NextWriteByte = Write_Memory_Params.ProgData;
174
175 /* Check the programming mode desired by the host, either Paged or Word memory writes */
176 if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK)
177 {
178 uint16_t StartAddress = (CurrentAddress & 0xFFFF);
179
180 /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
181 if (MustLoadExtendedAddress)
182 {
183 ISPTarget_LoadExtendedAddress();
184 MustLoadExtendedAddress = false;
185 }
186
187 /* Paged mode memory programming */
188 for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)
189 {
190 bool IsOddByte = (CurrentByte & 0x01);
191 uint8_t ByteToWrite = *(NextWriteByte++);
192
193 ISPTarget_SendByte(Write_Memory_Params.ProgrammingCommands[0]);
194 ISPTarget_SendByte(CurrentAddress >> 8);
195 ISPTarget_SendByte(CurrentAddress & 0xFF);
196 ISPTarget_SendByte(ByteToWrite);
197
198 /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high
199 * or low byte at the current word address */
200 if (V2Command == CMD_PROGRAM_FLASH_ISP)
201 Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_HIGH_BYTE_MASK;
202
203 /* Check to see the write completion method, to see if we have a valid polling address */
204 if (!(PollAddress) && (ByteToWrite != PollValue))
205 {
206 if (IsOddByte && (V2Command == CMD_PROGRAM_FLASH_ISP))
207 Write_Memory_Params.ProgrammingCommands[2] |= READ_WRITE_HIGH_BYTE_MASK;
208
209 PollAddress = (CurrentAddress & 0xFFFF);
210 }
211
212 /* EEPROM increments the address on each byte, flash needs to increment on each word */
213 if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))
214 CurrentAddress++;
215 }
216
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)
219 {
220 ISPTarget_SendByte(Write_Memory_Params.ProgrammingCommands[1]);
221 ISPTarget_SendByte(StartAddress >> 8);
222 ISPTarget_SendByte(StartAddress & 0xFF);
223 ISPTarget_SendByte(0x00);
224
225 /* Check if polling is enabled and possible, if not switch to timed delay mode */
226 if ((Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_VALUE_MASK) && !(PollAddress))
227 {
228 Write_Memory_Params.ProgrammingMode = (Write_Memory_Params.ProgrammingMode & ~PROG_MODE_PAGED_VALUE_MASK) |
229 PROG_MODE_PAGED_TIMEDELAY_MASK;
230 }
231
232 ProgrammingStatus = ISPTarget_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,
233 Write_Memory_Params.DelayMS,
234 Write_Memory_Params.ProgrammingCommands[2]);
235
236 /* Check to see if the FLASH address has crossed the extended address boundary */
237 if ((V2Command == CMD_PROGRAM_FLASH_ISP) && !(CurrentAddress & 0xFFFF))
238 MustLoadExtendedAddress = true;
239 }
240 }
241 else
242 {
243 /* Word/byte mode memory programming */
244 for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)
245 {
246 bool IsOddByte = (CurrentByte & 0x01);
247 uint8_t ByteToWrite = *(NextWriteByte++);
248
249 /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
250 if (MustLoadExtendedAddress)
251 {
252 ISPTarget_LoadExtendedAddress();
253 MustLoadExtendedAddress = false;
254 }
255
256 ISPTarget_SendByte(Write_Memory_Params.ProgrammingCommands[0]);
257 ISPTarget_SendByte(CurrentAddress >> 8);
258 ISPTarget_SendByte(CurrentAddress & 0xFF);
259 ISPTarget_SendByte(ByteToWrite);
260
261 /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high
262 * or low byte at the current word address */
263 if (V2Command == CMD_PROGRAM_FLASH_ISP)
264 Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_HIGH_BYTE_MASK;
265
266 /* Save previous programming mode in case we modify it for the current word */
267 uint8_t PreviousProgrammingMode = Write_Memory_Params.ProgrammingMode;
268
269 if (ByteToWrite != PollValue)
270 {
271 if (IsOddByte && (V2Command == CMD_PROGRAM_FLASH_ISP))
272 Write_Memory_Params.ProgrammingCommands[2] |= READ_WRITE_HIGH_BYTE_MASK;
273 else
274 Write_Memory_Params.ProgrammingCommands[2] &= ~READ_WRITE_HIGH_BYTE_MASK;
275
276 PollAddress = (CurrentAddress & 0xFFFF);
277 }
278 else if (!(Write_Memory_Params.ProgrammingMode & PROG_MODE_WORD_READYBUSY_MASK))
279 {
280 Write_Memory_Params.ProgrammingMode = (Write_Memory_Params.ProgrammingMode & ~PROG_MODE_WORD_VALUE_MASK) |
281 PROG_MODE_WORD_TIMEDELAY_MASK;
282 }
283
284 ProgrammingStatus = ISPTarget_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,
285 Write_Memory_Params.DelayMS,
286 Write_Memory_Params.ProgrammingCommands[2]);
287
288 /* Restore previous programming mode mask in case the current word needed to change it */
289 Write_Memory_Params.ProgrammingMode = PreviousProgrammingMode;
290
291 /* Abort the programming loop early if the byte/word programming failed */
292 if (ProgrammingStatus != STATUS_CMD_OK)
293 break;
294
295 /* EEPROM just increments the address each byte, flash needs to increment on each word and
296 * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended
297 * address boundary has been crossed */
298 if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))
299 {
300 CurrentAddress++;
301
302 if ((V2Command != CMD_PROGRAM_EEPROM_ISP) && !(CurrentAddress & 0xFFFF))
303 MustLoadExtendedAddress = true;
304 }
305 }
306 }
307
308 Endpoint_Write_Byte(V2Command);
309 Endpoint_Write_Byte(ProgrammingStatus);
310 Endpoint_ClearIN();
311 }
312
313 /** Handler for the CMD_READ_FLASH_ISP and CMD_READ_EEPROM_ISP commands, reading in bytes,
314 * words or pages of data from the attached device.
315 *
316 * \param[in] V2Command Issued V2 Protocol command byte from the host
317 */
318 void ISPProtocol_ReadMemory(uint8_t V2Command)
319 {
320 struct
321 {
322 uint16_t BytesToRead;
323 uint8_t ReadMemoryCommand;
324 } Read_Memory_Params;
325
326 Endpoint_Read_Stream_LE(&Read_Memory_Params, sizeof(Read_Memory_Params), NO_STREAM_CALLBACK);
327 Read_Memory_Params.BytesToRead = SwapEndian_16(Read_Memory_Params.BytesToRead);
328
329 Endpoint_ClearOUT();
330 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
331 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
332
333 Endpoint_Write_Byte(V2Command);
334 Endpoint_Write_Byte(STATUS_CMD_OK);
335
336 /* Read each byte from the device and write them to the packet for the host */
337 for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++)
338 {
339 /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
340 if (MustLoadExtendedAddress)
341 {
342 ISPTarget_LoadExtendedAddress();
343 MustLoadExtendedAddress = false;
344 }
345
346 /* Read the next byte from the desired memory space in the device */
347 ISPTarget_SendByte(Read_Memory_Params.ReadMemoryCommand);
348 ISPTarget_SendByte(CurrentAddress >> 8);
349 ISPTarget_SendByte(CurrentAddress & 0xFF);
350 Endpoint_Write_Byte(ISPTarget_ReceiveByte());
351
352 /* Check if the endpoint bank is currently full, if so send the packet */
353 if (!(Endpoint_IsReadWriteAllowed()))
354 {
355 Endpoint_ClearIN();
356 Endpoint_WaitUntilReady();
357 }
358
359 /* AVR FLASH addressing requires us to modify the read command based on if we are reading a high
360 * or low byte at the current word address */
361 if (V2Command == CMD_READ_FLASH_ISP)
362 Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_HIGH_BYTE_MASK;
363
364 /* EEPROM just increments the address each byte, flash needs to increment on each word and
365 * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended
366 * address boundary has been crossed */
367 if ((CurrentByte & 0x01) || (V2Command == CMD_READ_EEPROM_ISP))
368 {
369 CurrentAddress++;
370
371 if ((V2Command != CMD_READ_EEPROM_ISP) && !(CurrentAddress & 0xFFFF))
372 MustLoadExtendedAddress = true;
373 }
374 }
375
376 Endpoint_Write_Byte(STATUS_CMD_OK);
377
378 bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed());
379 Endpoint_ClearIN();
380
381 /* Ensure last packet is a short packet to terminate the transfer */
382 if (IsEndpointFull)
383 {
384 Endpoint_WaitUntilReady();
385 Endpoint_ClearIN();
386 Endpoint_WaitUntilReady();
387 }
388 }
389
390 /** Handler for the CMD_CHI_ERASE_ISP command, clearing the target's FLASH memory. */
391 void ISPProtocol_ChipErase(void)
392 {
393 struct
394 {
395 uint8_t EraseDelayMS;
396 uint8_t PollMethod;
397 uint8_t EraseCommandBytes[4];
398 } Erase_Chip_Params;
399
400 Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params), NO_STREAM_CALLBACK);
401
402 Endpoint_ClearOUT();
403 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
404 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
405
406 uint8_t ResponseStatus = STATUS_CMD_OK;
407
408 /* Send the chip erase commands as given by the host to the device */
409 for (uint8_t SByte = 0; SByte < sizeof(Erase_Chip_Params.EraseCommandBytes); SByte++)
410 ISPTarget_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]);
411
412 /* Use appropriate command completion check as given by the host (delay or busy polling) */
413 if (!(Erase_Chip_Params.PollMethod))
414 ISPProtocol_DelayMS(Erase_Chip_Params.EraseDelayMS);
415 else
416 ResponseStatus = ISPTarget_WaitWhileTargetBusy();
417
418 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP);
419 Endpoint_Write_Byte(ResponseStatus);
420 Endpoint_ClearIN();
421 }
422
423 /** Handler for the CMD_READ_FUSE_ISP, CMD_READ_LOCK_ISP, CMD_READ_SIGNATURE_ISP and CMD_READ_OSCCAL commands,
424 * reading the requested configuration byte from the device.
425 *
426 * \param[in] V2Command Issued V2 Protocol command byte from the host
427 */
428 void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command)
429 {
430 struct
431 {
432 uint8_t RetByte;
433 uint8_t ReadCommandBytes[4];
434 } Read_FuseLockSigOSCCAL_Params;
435
436 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params), NO_STREAM_CALLBACK);
437
438 Endpoint_ClearOUT();
439 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
440 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
441
442 uint8_t ResponseBytes[4];
443
444 /* Send the Fuse or Lock byte read commands as given by the host to the device, store response */
445 for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)
446 ResponseBytes[RByte] = ISPTarget_TransferByte(Read_FuseLockSigOSCCAL_Params.ReadCommandBytes[RByte]);
447
448 Endpoint_Write_Byte(V2Command);
449 Endpoint_Write_Byte(STATUS_CMD_OK);
450 Endpoint_Write_Byte(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte - 1]);
451 Endpoint_Write_Byte(STATUS_CMD_OK);
452 Endpoint_ClearIN();
453 }
454
455 /** Handler for the CMD_WRITE_FUSE_ISP and CMD_WRITE_LOCK_ISP commands, writing the requested configuration
456 * byte to the device.
457 *
458 * \param[in] V2Command Issued V2 Protocol command byte from the host
459 */
460 void ISPProtocol_WriteFuseLock(uint8_t V2Command)
461 {
462 struct
463 {
464 uint8_t WriteCommandBytes[4];
465 } Write_FuseLockSig_Params;
466
467 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params), NO_STREAM_CALLBACK);
468
469 Endpoint_ClearOUT();
470 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
471 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
472
473 /* Send the Fuse or Lock byte program commands as given by the host to the device */
474 for (uint8_t SByte = 0; SByte < sizeof(Write_FuseLockSig_Params.WriteCommandBytes); SByte++)
475 ISPTarget_SendByte(Write_FuseLockSig_Params.WriteCommandBytes[SByte]);
476
477 Endpoint_Write_Byte(V2Command);
478 Endpoint_Write_Byte(STATUS_CMD_OK);
479 Endpoint_Write_Byte(STATUS_CMD_OK);
480 Endpoint_ClearIN();
481 }
482
483 /** Handler for the CMD_SPI_MULTI command, writing and reading arbitrary SPI data to and from the attached device. */
484 void ISPProtocol_SPIMulti(void)
485 {
486 struct
487 {
488 uint8_t TxBytes;
489 uint8_t RxBytes;
490 uint8_t RxStartAddr;
491 uint8_t TxData[255];
492 } SPI_Multi_Params;
493
494 Endpoint_Read_Stream_LE(&SPI_Multi_Params, (sizeof(SPI_Multi_Params) - sizeof(SPI_Multi_Params.TxData)), NO_STREAM_CALLBACK);
495 Endpoint_Read_Stream_LE(&SPI_Multi_Params.TxData, SPI_Multi_Params.TxBytes, NO_STREAM_CALLBACK);
496
497 Endpoint_ClearOUT();
498 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
499 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
500
501 Endpoint_Write_Byte(CMD_SPI_MULTI);
502 Endpoint_Write_Byte(STATUS_CMD_OK);
503
504 uint8_t CurrTxPos = 0;
505 uint8_t CurrRxPos = 0;
506
507 /* Write out bytes to transmit until the start of the bytes to receive is met */
508 while (CurrTxPos < SPI_Multi_Params.RxStartAddr)
509 {
510 if (CurrTxPos < SPI_Multi_Params.TxBytes)
511 ISPTarget_SendByte(SPI_Multi_Params.TxData[CurrTxPos]);
512 else
513 ISPTarget_SendByte(0);
514
515 CurrTxPos++;
516 }
517
518 /* Transmit remaining bytes with padding as needed, read in response bytes */
519 while (CurrRxPos < SPI_Multi_Params.RxBytes)
520 {
521 if (CurrTxPos < SPI_Multi_Params.TxBytes)
522 Endpoint_Write_Byte(ISPTarget_TransferByte(SPI_Multi_Params.TxData[CurrTxPos++]));
523 else
524 Endpoint_Write_Byte(ISPTarget_ReceiveByte());
525
526 /* Check to see if we have filled the endpoint bank and need to send the packet */
527 if (!(Endpoint_IsReadWriteAllowed()))
528 {
529 Endpoint_ClearIN();
530 Endpoint_WaitUntilReady();
531 }
532
533 CurrRxPos++;
534 }
535
536 Endpoint_Write_Byte(STATUS_CMD_OK);
537
538 bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed());
539 Endpoint_ClearIN();
540
541 /* Ensure last packet is a short packet to terminate the transfer */
542 if (IsEndpointFull)
543 {
544 Endpoint_WaitUntilReady();
545 Endpoint_ClearIN();
546 Endpoint_WaitUntilReady();
547 }
548 }
549
550 /** Blocking delay for a given number of milliseconds.
551 *
552 * \param[in] DelayMS Number of milliseconds to delay for
553 */
554 void ISPProtocol_DelayMS(uint8_t DelayMS)
555 {
556 while (DelayMS-- && TimeoutTicksRemaining)
557 _delay_ms(1);
558 }
559
560 #endif