Clean up Webserver project - add more Doxygen documentation for the new DHCP client...
[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.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_SetEndpointDirection(ENDPOINT_DIR_IN);
61
62 uint8_t ResponseStatus = STATUS_CMD_FAILED;
63
64 CurrentAddress = 0;
65
66 ISPProtocol_DelayMS(Enter_ISP_Params.ExecutionDelayMS);
67 SPI_Init(ISPTarget_GetSPIPrescalerMask() | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING | SPI_MODE_MASTER);
68
69 /* Continuously attempt to synchronize with the target until either the number of attempts specified
70 * by the host has exceeded, or the the device sends back the expected response values */
71 while (Enter_ISP_Params.SynchLoops-- && (ResponseStatus == STATUS_CMD_FAILED))
72 {
73 uint8_t ResponseBytes[4];
74
75 ISPTarget_ChangeTargetResetLine(true);
76 ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
77
78 for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)
79 {
80 ISPProtocol_DelayMS(Enter_ISP_Params.ByteDelay);
81 ResponseBytes[RByte] = SPI_TransferByte(Enter_ISP_Params.EnterProgBytes[RByte]);
82 }
83
84 /* Check if polling disabled, or if the polled value matches the expected value */
85 if (!(Enter_ISP_Params.PollIndex) || (ResponseBytes[Enter_ISP_Params.PollIndex - 1] == Enter_ISP_Params.PollValue))
86 {
87 ResponseStatus = STATUS_CMD_OK;
88 }
89 else
90 {
91 ISPTarget_ChangeTargetResetLine(false);
92 ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
93 }
94 }
95
96 Endpoint_Write_Byte(CMD_ENTER_PROGMODE_ISP);
97 Endpoint_Write_Byte(ResponseStatus);
98 Endpoint_ClearIN();
99 }
100
101 /** Handler for the CMD_LEAVE_ISP command, which releases the target from programming mode. */
102 void ISPProtocol_LeaveISPMode(void)
103 {
104 struct
105 {
106 uint8_t PreDelayMS;
107 uint8_t PostDelayMS;
108 } Leave_ISP_Params;
109
110 Endpoint_Read_Stream_LE(&Leave_ISP_Params, sizeof(Leave_ISP_Params), NO_STREAM_CALLBACK);
111
112 Endpoint_ClearOUT();
113 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
114
115 /* Perform pre-exit delay, release the target /RESET, disable the SPI bus and perform the post-exit delay */
116 ISPProtocol_DelayMS(Leave_ISP_Params.PreDelayMS);
117 ISPTarget_ChangeTargetResetLine(false);
118 SPI_ShutDown();
119 ISPProtocol_DelayMS(Leave_ISP_Params.PostDelayMS);
120
121 Endpoint_Write_Byte(CMD_LEAVE_PROGMODE_ISP);
122 Endpoint_Write_Byte(STATUS_CMD_OK);
123 Endpoint_ClearIN();
124 }
125
126 /** Handler for the CMD_PROGRAM_FLASH_ISP and CMD_PROGRAM_EEPROM_ISP commands, writing out bytes,
127 * words or pages of data to the attached device.
128 *
129 * \param[in] V2Command Issued V2 Protocol command byte from the host
130 */
131 void ISPProtocol_ProgramMemory(uint8_t V2Command)
132 {
133 struct
134 {
135 uint16_t BytesToWrite;
136 uint8_t ProgrammingMode;
137 uint8_t DelayMS;
138 uint8_t ProgrammingCommands[3];
139 uint8_t PollValue1;
140 uint8_t PollValue2;
141 uint8_t ProgData[256]; // Note, the Jungo driver has a very short ACK timeout period, need to buffer the
142 } Write_Memory_Params; // whole page and ACK the packet as fast as possible to prevent it from aborting
143
144 Endpoint_Read_Stream_LE(&Write_Memory_Params, (sizeof(Write_Memory_Params) -
145 sizeof(Write_Memory_Params.ProgData)), NO_STREAM_CALLBACK);
146
147
148 Write_Memory_Params.BytesToWrite = SwapEndian_16(Write_Memory_Params.BytesToWrite);
149
150 if (Write_Memory_Params.BytesToWrite > sizeof(Write_Memory_Params.ProgData))
151 {
152 Endpoint_ClearOUT();
153 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
154
155 Endpoint_Write_Byte(V2Command);
156 Endpoint_Write_Byte(STATUS_CMD_FAILED);
157 Endpoint_ClearIN();
158 return;
159 }
160
161 Endpoint_Read_Stream_LE(&Write_Memory_Params.ProgData, Write_Memory_Params.BytesToWrite, NO_STREAM_CALLBACK);
162
163 Endpoint_ClearOUT();
164 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
165
166 uint8_t ProgrammingStatus = STATUS_CMD_OK;
167 uint16_t PollAddress = 0;
168 uint8_t PollValue = (V2Command == CMD_PROGRAM_FLASH_ISP) ? Write_Memory_Params.PollValue1 :
169 Write_Memory_Params.PollValue2;
170 uint8_t* NextWriteByte = Write_Memory_Params.ProgData;
171
172 /* Check to see if the host has issued a SET ADDRESS command and we haven't sent a
173 * LOAD EXTENDED ADDRESS command (if needed, used when operating beyond the 128KB
174 * FLASH barrier) */
175 if (MustSetAddress)
176 {
177 if (CurrentAddress & (1UL << 31))
178 ISPTarget_LoadExtendedAddress();
179
180 MustSetAddress = false;
181 }
182
183 /* Check the programming mode desired by the host, either Paged or Word memory writes */
184 if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK)
185 {
186 uint16_t StartAddress = (CurrentAddress & 0xFFFF);
187
188 /* Paged mode memory programming */
189 for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)
190 {
191 bool IsOddByte = (CurrentByte & 0x01);
192 uint8_t ByteToWrite = *(NextWriteByte++);
193
194 SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]);
195 SPI_SendByte(CurrentAddress >> 8);
196 SPI_SendByte(CurrentAddress & 0xFF);
197 SPI_SendByte(ByteToWrite);
198
199 /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high
200 * or low byte at the current word address */
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 if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))
213 CurrentAddress++;
214 }
215
216 /* If the current page must be committed, send the PROGRAM PAGE command to the target */
217 if (Write_Memory_Params.ProgrammingMode & PROG_MODE_COMMIT_PAGE_MASK)
218 {
219 SPI_SendByte(Write_Memory_Params.ProgrammingCommands[1]);
220 SPI_SendByte(StartAddress >> 8);
221 SPI_SendByte(StartAddress & 0xFF);
222 SPI_SendByte(0x00);
223
224 /* Check if polling is possible, if not switch to timed delay mode */
225 if (!(PollAddress))
226 {
227 Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_PAGED_VALUE_MASK;
228 Write_Memory_Params.ProgrammingMode |= PROG_MODE_PAGED_TIMEDELAY_MASK;
229 }
230
231 ProgrammingStatus = ISPTarget_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,
232 Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]);
233 }
234 }
235 else
236 {
237 /* Word/byte mode memory programming */
238 for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)
239 {
240 bool IsOddByte = (CurrentByte & 0x01);
241 uint8_t ByteToWrite = *(NextWriteByte++);
242
243 if (IsOddByte && (V2Command == CMD_READ_FLASH_ISP))
244 Write_Memory_Params.ProgrammingCommands[0] |= READ_WRITE_HIGH_BYTE_MASK;
245 else
246 Write_Memory_Params.ProgrammingCommands[0] &= ~READ_WRITE_HIGH_BYTE_MASK;
247
248 SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]);
249 SPI_SendByte(CurrentAddress >> 8);
250 SPI_SendByte(CurrentAddress & 0xFF);
251 SPI_SendByte(ByteToWrite);
252
253 if (ByteToWrite != PollValue)
254 {
255 if (IsOddByte && (V2Command == CMD_PROGRAM_FLASH_ISP))
256 Write_Memory_Params.ProgrammingCommands[2] |= READ_WRITE_HIGH_BYTE_MASK;
257
258 PollAddress = (CurrentAddress & 0xFFFF);
259 }
260
261 if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))
262 CurrentAddress++;
263
264 ProgrammingStatus = ISPTarget_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,
265 Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]);
266
267 if (ProgrammingStatus != STATUS_CMD_OK)
268 break;
269 }
270 }
271
272 Endpoint_Write_Byte(V2Command);
273 Endpoint_Write_Byte(ProgrammingStatus);
274 Endpoint_ClearIN();
275 }
276
277 /** Handler for the CMD_READ_FLASH_ISP and CMD_READ_EEPROM_ISP commands, reading in bytes,
278 * words or pages of data from the attached device.
279 *
280 * \param[in] V2Command Issued V2 Protocol command byte from the host
281 */
282 void ISPProtocol_ReadMemory(uint8_t V2Command)
283 {
284 struct
285 {
286 uint16_t BytesToRead;
287 uint8_t ReadMemoryCommand;
288 } Read_Memory_Params;
289
290 Endpoint_Read_Stream_LE(&Read_Memory_Params, sizeof(Read_Memory_Params), NO_STREAM_CALLBACK);
291 Read_Memory_Params.BytesToRead = SwapEndian_16(Read_Memory_Params.BytesToRead);
292
293 Endpoint_ClearOUT();
294 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
295
296 Endpoint_Write_Byte(V2Command);
297 Endpoint_Write_Byte(STATUS_CMD_OK);
298
299 /* Check to see if the host has issued a SET ADDRESS command and we haven't sent a
300 * LOAD EXTENDED ADDRESS command (if needed, used when operating beyond the 128KB
301 * FLASH barrier) */
302 if (MustSetAddress)
303 {
304 if (CurrentAddress & (1UL << 31))
305 ISPTarget_LoadExtendedAddress();
306
307 MustSetAddress = false;
308 }
309
310 /* Read each byte from the device and write them to the packet for the host */
311 for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++)
312 {
313 /* Read the next byte from the desired memory space in the device */
314 SPI_SendByte(Read_Memory_Params.ReadMemoryCommand);
315 SPI_SendByte(CurrentAddress >> 8);
316 SPI_SendByte(CurrentAddress & 0xFF);
317 Endpoint_Write_Byte(SPI_ReceiveByte());
318
319 /* Check if the endpoint bank is currently full, if so send the packet */
320 if (!(Endpoint_IsReadWriteAllowed()))
321 {
322 Endpoint_ClearIN();
323 Endpoint_WaitUntilReady();
324 }
325
326 /* AVR FLASH addressing requires us to modify the read command based on if we are reading a high
327 * or low byte at the current word address */
328 if (V2Command == CMD_READ_FLASH_ISP)
329 Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_HIGH_BYTE_MASK;
330
331 /* Only increment the current address if we have read both bytes in the current word when in FLASH
332 * read mode, or for each byte when in EEPROM read mode */
333 if (((CurrentByte & 0x01) && (V2Command == CMD_READ_FLASH_ISP)) || (V2Command == CMD_READ_EEPROM_ISP))
334 CurrentAddress++;
335 }
336
337 Endpoint_Write_Byte(STATUS_CMD_OK);
338
339 bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed());
340 Endpoint_ClearIN();
341
342 /* Ensure last packet is a short packet to terminate the transfer */
343 if (IsEndpointFull)
344 {
345 Endpoint_WaitUntilReady();
346 Endpoint_ClearIN();
347 Endpoint_WaitUntilReady();
348 }
349 }
350
351 /** Handler for the CMD_CHI_ERASE_ISP command, clearing the target's FLASH memory. */
352 void ISPProtocol_ChipErase(void)
353 {
354 struct
355 {
356 uint8_t EraseDelayMS;
357 uint8_t PollMethod;
358 uint8_t EraseCommandBytes[4];
359 } Erase_Chip_Params;
360
361 Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params), NO_STREAM_CALLBACK);
362
363 Endpoint_ClearOUT();
364 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
365
366 uint8_t ResponseStatus = STATUS_CMD_OK;
367
368 /* Send the chip erase commands as given by the host to the device */
369 for (uint8_t SByte = 0; SByte < sizeof(Erase_Chip_Params.EraseCommandBytes); SByte++)
370 SPI_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]);
371
372 /* Use appropriate command completion check as given by the host (delay or busy polling) */
373 if (!(Erase_Chip_Params.PollMethod))
374 ISPProtocol_DelayMS(Erase_Chip_Params.EraseDelayMS);
375 else
376 ResponseStatus = ISPTarget_WaitWhileTargetBusy();
377
378 Endpoint_Write_Byte(CMD_CHIP_ERASE_ISP);
379 Endpoint_Write_Byte(ResponseStatus);
380 Endpoint_ClearIN();
381 }
382
383 /** Handler for the CMD_READ_FUSE_ISP, CMD_READ_LOCK_ISP, CMD_READ_SIGNATURE_ISP and CMD_READ_OSCCAL commands,
384 * reading the requested configuration byte from the device.
385 *
386 * \param[in] V2Command Issued V2 Protocol command byte from the host
387 */
388 void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command)
389 {
390 struct
391 {
392 uint8_t RetByte;
393 uint8_t ReadCommandBytes[4];
394 } Read_FuseLockSigOSCCAL_Params;
395
396 Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params), NO_STREAM_CALLBACK);
397
398 Endpoint_ClearOUT();
399 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
400
401 uint8_t ResponseBytes[4];
402
403 /* Send the Fuse or Lock byte read commands as given by the host to the device, store response */
404 for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)
405 ResponseBytes[RByte] = SPI_TransferByte(Read_FuseLockSigOSCCAL_Params.ReadCommandBytes[RByte]);
406
407 Endpoint_Write_Byte(V2Command);
408 Endpoint_Write_Byte(STATUS_CMD_OK);
409 Endpoint_Write_Byte(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte - 1]);
410 Endpoint_Write_Byte(STATUS_CMD_OK);
411 Endpoint_ClearIN();
412 }
413
414 /** Handler for the CMD_WRITE_FUSE_ISP and CMD_WRITE_LOCK_ISP commands, writing the requested configuration
415 * byte to the device.
416 *
417 * \param[in] V2Command Issued V2 Protocol command byte from the host
418 */
419 void ISPProtocol_WriteFuseLock(uint8_t V2Command)
420 {
421 struct
422 {
423 uint8_t WriteCommandBytes[4];
424 } Write_FuseLockSig_Params;
425
426 Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params), NO_STREAM_CALLBACK);
427
428 Endpoint_ClearOUT();
429 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
430
431 /* Send the Fuse or Lock byte program commands as given by the host to the device */
432 for (uint8_t SByte = 0; SByte < sizeof(Write_FuseLockSig_Params.WriteCommandBytes); SByte++)
433 SPI_SendByte(Write_FuseLockSig_Params.WriteCommandBytes[SByte]);
434
435 Endpoint_Write_Byte(V2Command);
436 Endpoint_Write_Byte(STATUS_CMD_OK);
437 Endpoint_Write_Byte(STATUS_CMD_OK);
438 Endpoint_ClearIN();
439 }
440
441 /** Handler for the CMD_SPI_MULTI command, writing and reading arbitrary SPI data to and from the attached device. */
442 void ISPProtocol_SPIMulti(void)
443 {
444 struct
445 {
446 uint8_t TxBytes;
447 uint8_t RxBytes;
448 uint8_t RxStartAddr;
449 uint8_t TxData[255];
450 } SPI_Multi_Params;
451
452 Endpoint_Read_Stream_LE(&SPI_Multi_Params, (sizeof(SPI_Multi_Params) - sizeof(SPI_Multi_Params.TxData)), NO_STREAM_CALLBACK);
453 Endpoint_Read_Stream_LE(&SPI_Multi_Params.TxData, SPI_Multi_Params.TxBytes, NO_STREAM_CALLBACK);
454
455 Endpoint_ClearOUT();
456 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
457
458 Endpoint_Write_Byte(CMD_SPI_MULTI);
459 Endpoint_Write_Byte(STATUS_CMD_OK);
460
461 uint8_t CurrTxPos = 0;
462 uint8_t CurrRxPos = 0;
463
464 /* Write out bytes to transmit until the start of the bytes to receive is met */
465 while (CurrTxPos < SPI_Multi_Params.RxStartAddr)
466 {
467 if (CurrTxPos < SPI_Multi_Params.TxBytes)
468 SPI_SendByte(SPI_Multi_Params.TxData[CurrTxPos]);
469 else
470 SPI_SendByte(0);
471
472 CurrTxPos++;
473 }
474
475 /* Transmit remaining bytes with padding as needed, read in response bytes */
476 while (CurrRxPos < SPI_Multi_Params.RxBytes)
477 {
478 if (CurrTxPos < SPI_Multi_Params.TxBytes)
479 Endpoint_Write_Byte(SPI_TransferByte(SPI_Multi_Params.TxData[CurrTxPos++]));
480 else
481 Endpoint_Write_Byte(SPI_ReceiveByte());
482
483 /* Check to see if we have filled the endpoint bank and need to send the packet */
484 if (!(Endpoint_IsReadWriteAllowed()))
485 {
486 Endpoint_ClearIN();
487 Endpoint_WaitUntilReady();
488 }
489
490 CurrRxPos++;
491 }
492
493 Endpoint_Write_Byte(STATUS_CMD_OK);
494
495 bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed());
496 Endpoint_ClearIN();
497
498 /* Ensure last packet is a short packet to terminate the transfer */
499 if (IsEndpointFull)
500 {
501 Endpoint_WaitUntilReady();
502 Endpoint_ClearIN();
503 Endpoint_WaitUntilReady();
504 }
505 }
506
507 #endif