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