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