Added board hardware driver support for the Microsin AVR-USB162 development board.
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / XPROGProtocol.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 * XPROG Protocol handler, to process V2 Protocol wrapped XPROG commands used in Atmel programmer devices.
34 */
35
36 #define INCLUDE_FROM_XPROGPROTOCOL_C
37 #include "XPROGProtocol.h"
38
39 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)
40 /** Base absolute address for the target's NVM controller for PDI programming */
41 uint32_t XPROG_Param_NVMBase = 0x010001C0;
42
43 /** Size in bytes of the target's EEPROM page */
44 uint16_t XPROG_Param_EEPageSize = 32;
45
46 /** Address of the TPI device's NVMCMD register for TPI programming */
47 uint8_t XPROG_Param_NVMCMDRegAddr = 0x33;
48
49 /** Address of the TPI device's NVMCSR register for TPI programming */
50 uint8_t XPROG_Param_NVMCSRRegAddr = 0x32;
51
52 /** Currently selected XPROG programming protocol */
53 uint8_t XPROG_SelectedProtocol = XPRG_PROTOCOL_PDI;
54
55 /** Handler for the CMD_XPROG_SETMODE command, which sets the programmer-to-target protocol used for PDI/TPI
56 * programming.
57 */
58 void XPROGProtocol_SetMode(void)
59 {
60 struct
61 {
62 uint8_t Protocol;
63 } SetMode_XPROG_Params;
64
65 Endpoint_Read_Stream_LE(&SetMode_XPROG_Params, sizeof(SetMode_XPROG_Params), NO_STREAM_CALLBACK);
66
67 Endpoint_ClearOUT();
68 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
69 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
70
71 XPROG_SelectedProtocol = SetMode_XPROG_Params.Protocol;
72
73 Endpoint_Write_Byte(CMD_XPROG_SETMODE);
74 Endpoint_Write_Byte((SetMode_XPROG_Params.Protocol != XPRG_PROTOCOL_JTAG) ? STATUS_CMD_OK : STATUS_CMD_FAILED);
75 Endpoint_ClearIN();
76 }
77
78 /** Handler for the CMD_XPROG command, which wraps up XPROG commands in a V2 wrapper which need to be
79 * removed and processed so that the underlying XPROG command can be handled.
80 */
81 void XPROGProtocol_Command(void)
82 {
83 uint8_t XPROGCommand = Endpoint_Read_Byte();
84
85 switch (XPROGCommand)
86 {
87 case XPRG_CMD_ENTER_PROGMODE:
88 XPROGProtocol_EnterXPROGMode();
89 break;
90 case XPRG_CMD_LEAVE_PROGMODE:
91 XPROGProtocol_LeaveXPROGMode();
92 break;
93 case XPRG_CMD_ERASE:
94 XPROGProtocol_Erase();
95 break;
96 case XPRG_CMD_WRITE_MEM:
97 XPROGProtocol_WriteMemory();
98 break;
99 case XPRG_CMD_READ_MEM:
100 XPROGProtocol_ReadMemory();
101 break;
102 case XPRG_CMD_CRC:
103 XPROGProtocol_ReadCRC();
104 break;
105 case XPRG_CMD_SET_PARAM:
106 XPROGProtocol_SetParam();
107 break;
108 }
109 }
110
111 /** Handler for the XPROG ENTER_PROGMODE command to establish a connection with the attached device. */
112 static void XPROGProtocol_EnterXPROGMode(void)
113 {
114 Endpoint_ClearOUT();
115 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
116 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
117
118 bool NVMBusEnabled = false;
119
120 if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI)
121 NVMBusEnabled = XMEGANVM_EnablePDI();
122 else if (XPROG_SelectedProtocol == XPRG_PROTOCOL_TPI)
123 NVMBusEnabled = TINYNVM_EnableTPI();
124
125 Endpoint_Write_Byte(CMD_XPROG);
126 Endpoint_Write_Byte(XPRG_CMD_ENTER_PROGMODE);
127 Endpoint_Write_Byte(NVMBusEnabled ? XPRG_ERR_OK : XPRG_ERR_FAILED);
128 Endpoint_ClearIN();
129 }
130
131 /** Handler for the XPROG LEAVE_PROGMODE command to terminate the PDI programming connection with
132 * the attached device.
133 */
134 static void XPROGProtocol_LeaveXPROGMode(void)
135 {
136 Endpoint_ClearOUT();
137 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
138 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
139
140 if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI)
141 XMEGANVM_DisablePDI();
142 else
143 TINYNVM_DisableTPI();
144
145 #if defined(XCK_RESCUE_CLOCK_ENABLE) && defined(ENABLE_ISP_PROTOCOL)
146 ISPTarget_ConfigureRescueClock();
147 #endif
148
149 Endpoint_Write_Byte(CMD_XPROG);
150 Endpoint_Write_Byte(XPRG_CMD_LEAVE_PROGMODE);
151 Endpoint_Write_Byte(XPRG_ERR_OK);
152 Endpoint_ClearIN();
153 }
154
155 /** Handler for the XPRG ERASE command to erase a specific memory address space in the attached device. */
156 static void XPROGProtocol_Erase(void)
157 {
158 uint8_t ReturnStatus = XPRG_ERR_OK;
159
160 struct
161 {
162 uint8_t MemoryType;
163 uint32_t Address;
164 } Erase_XPROG_Params;
165
166 Endpoint_Read_Stream_LE(&Erase_XPROG_Params, sizeof(Erase_XPROG_Params), NO_STREAM_CALLBACK);
167 Erase_XPROG_Params.Address = SwapEndian_32(Erase_XPROG_Params.Address);
168
169 Endpoint_ClearOUT();
170 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
171 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
172
173 uint8_t EraseCommand;
174
175 if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI)
176 {
177 /* Determine which NVM command to send to the device depending on the memory to erase */
178 switch (Erase_XPROG_Params.MemoryType)
179 {
180 case XPRG_ERASE_CHIP:
181 EraseCommand = XMEGA_NVM_CMD_CHIPERASE;
182 break;
183 case XPRG_ERASE_APP:
184 EraseCommand = XMEGA_NVM_CMD_ERASEAPPSEC;
185 break;
186 case XPRG_ERASE_BOOT:
187 EraseCommand = XMEGA_NVM_CMD_ERASEBOOTSEC;
188 break;
189 case XPRG_ERASE_EEPROM:
190 EraseCommand = XMEGA_NVM_CMD_ERASEEEPROM;
191 break;
192 case XPRG_ERASE_APP_PAGE:
193 EraseCommand = XMEGA_NVM_CMD_ERASEAPPSECPAGE;
194 break;
195 case XPRG_ERASE_BOOT_PAGE:
196 EraseCommand = XMEGA_NVM_CMD_ERASEBOOTSECPAGE;
197 break;
198 case XPRG_ERASE_EEPROM_PAGE:
199 EraseCommand = XMEGA_NVM_CMD_ERASEEEPROMPAGE;
200 break;
201 case XPRG_ERASE_USERSIG:
202 EraseCommand = XMEGA_NVM_CMD_ERASEUSERSIG;
203 break;
204 default:
205 EraseCommand = XMEGA_NVM_CMD_NOOP;
206 break;
207 }
208
209 /* Erase the target memory, indicate timeout if occurred */
210 if (!(XMEGANVM_EraseMemory(EraseCommand, Erase_XPROG_Params.Address)))
211 ReturnStatus = XPRG_ERR_TIMEOUT;
212 }
213 else
214 {
215 if (Erase_XPROG_Params.MemoryType == XPRG_ERASE_CHIP)
216 EraseCommand = TINY_NVM_CMD_CHIPERASE;
217 else
218 EraseCommand = TINY_NVM_CMD_SECTIONERASE;
219
220 /* Erase the target memory, indicate timeout if occurred */
221 if (!(TINYNVM_EraseMemory(EraseCommand, Erase_XPROG_Params.Address)))
222 ReturnStatus = XPRG_ERR_TIMEOUT;
223 }
224
225 Endpoint_Write_Byte(CMD_XPROG);
226 Endpoint_Write_Byte(XPRG_CMD_ERASE);
227 Endpoint_Write_Byte(ReturnStatus);
228 Endpoint_ClearIN();
229 }
230
231 /** Handler for the XPROG WRITE_MEMORY command to write to a specific memory space within the attached device. */
232 static void XPROGProtocol_WriteMemory(void)
233 {
234 uint8_t ReturnStatus = XPRG_ERR_OK;
235
236 struct
237 {
238 uint8_t MemoryType;
239 uint8_t PageMode;
240 uint32_t Address;
241 uint16_t Length;
242 uint8_t ProgData[256];
243 } WriteMemory_XPROG_Params;
244
245 Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params, (sizeof(WriteMemory_XPROG_Params) -
246 sizeof(WriteMemory_XPROG_Params).ProgData), NO_STREAM_CALLBACK);
247 WriteMemory_XPROG_Params.Address = SwapEndian_32(WriteMemory_XPROG_Params.Address);
248 WriteMemory_XPROG_Params.Length = SwapEndian_16(WriteMemory_XPROG_Params.Length);
249 Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params.ProgData, WriteMemory_XPROG_Params.Length, NO_STREAM_CALLBACK);
250
251 Endpoint_ClearOUT();
252 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
253 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
254
255 if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI)
256 {
257 /* Assume FLASH page programming by default, as it is the common case */
258 uint8_t WriteCommand = XMEGA_NVM_CMD_WRITEFLASHPAGE;
259 uint8_t WriteBuffCommand = XMEGA_NVM_CMD_LOADFLASHPAGEBUFF;
260 uint8_t EraseBuffCommand = XMEGA_NVM_CMD_ERASEFLASHPAGEBUFF;
261 bool PagedMemory = true;
262
263 switch (WriteMemory_XPROG_Params.MemoryType)
264 {
265 case XPRG_MEM_TYPE_APPL:
266 WriteCommand = XMEGA_NVM_CMD_WRITEAPPSECPAGE;
267 break;
268 case XPRG_MEM_TYPE_BOOT:
269 WriteCommand = XMEGA_NVM_CMD_WRITEBOOTSECPAGE;
270 break;
271 case XPRG_MEM_TYPE_EEPROM:
272 WriteCommand = XMEGA_NVM_CMD_ERASEWRITEEEPROMPAGE;
273 WriteBuffCommand = XMEGA_NVM_CMD_LOADEEPROMPAGEBUFF;
274 EraseBuffCommand = XMEGA_NVM_CMD_ERASEEEPROMPAGEBUFF;
275 break;
276 case XPRG_MEM_TYPE_USERSIG:
277 WriteCommand = XMEGA_NVM_CMD_WRITEUSERSIG;
278 break;
279 case XPRG_MEM_TYPE_FUSE:
280 WriteCommand = XMEGA_NVM_CMD_WRITEFUSE;
281 PagedMemory = false;
282 break;
283 case XPRG_MEM_TYPE_LOCKBITS:
284 WriteCommand = XMEGA_NVM_CMD_WRITELOCK;
285 PagedMemory = false;
286 break;
287 }
288
289 /* Send the appropriate memory write commands to the device, indicate timeout if occurred */
290 if ((PagedMemory && !(XMEGANVM_WritePageMemory(WriteBuffCommand, EraseBuffCommand, WriteCommand,
291 WriteMemory_XPROG_Params.PageMode, WriteMemory_XPROG_Params.Address,
292 WriteMemory_XPROG_Params.ProgData, WriteMemory_XPROG_Params.Length))) ||
293 (!PagedMemory && !(XMEGANVM_WriteByteMemory(WriteCommand, WriteMemory_XPROG_Params.Address,
294 WriteMemory_XPROG_Params.ProgData[0]))))
295 {
296 ReturnStatus = XPRG_ERR_TIMEOUT;
297 }
298 }
299 else
300 {
301 /* Send write command to the TPI device, indicate timeout if occurred */
302 if (!(TINYNVM_WriteMemory(WriteMemory_XPROG_Params.Address, WriteMemory_XPROG_Params.ProgData,
303 WriteMemory_XPROG_Params.Length)))
304 {
305 ReturnStatus = XPRG_ERR_TIMEOUT;
306 }
307 }
308
309 Endpoint_Write_Byte(CMD_XPROG);
310 Endpoint_Write_Byte(XPRG_CMD_WRITE_MEM);
311 Endpoint_Write_Byte(ReturnStatus);
312 Endpoint_ClearIN();
313 }
314
315 /** Handler for the XPROG READ_MEMORY command to read data from a specific address space within the
316 * attached device.
317 */
318 static void XPROGProtocol_ReadMemory(void)
319 {
320 uint8_t ReturnStatus = XPRG_ERR_OK;
321
322 struct
323 {
324 uint8_t MemoryType;
325 uint32_t Address;
326 uint16_t Length;
327 } ReadMemory_XPROG_Params;
328
329 Endpoint_Read_Stream_LE(&ReadMemory_XPROG_Params, sizeof(ReadMemory_XPROG_Params), NO_STREAM_CALLBACK);
330 ReadMemory_XPROG_Params.Address = SwapEndian_32(ReadMemory_XPROG_Params.Address);
331 ReadMemory_XPROG_Params.Length = SwapEndian_16(ReadMemory_XPROG_Params.Length);
332
333 Endpoint_ClearOUT();
334 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
335 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
336
337 uint8_t ReadBuffer[256];
338
339 if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI)
340 {
341 /* Read the PDI target's memory, indicate timeout if occurred */
342 if (!(XMEGANVM_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length)))
343 ReturnStatus = XPRG_ERR_TIMEOUT;
344 }
345 else
346 {
347 /* Read the TPI target's memory, indicate timeout if occurred */
348 if (!(TINYNVM_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length)))
349 ReturnStatus = XPRG_ERR_TIMEOUT;
350 }
351
352 Endpoint_Write_Byte(CMD_XPROG);
353 Endpoint_Write_Byte(XPRG_CMD_READ_MEM);
354 Endpoint_Write_Byte(ReturnStatus);
355
356 if (ReturnStatus == XPRG_ERR_OK)
357 Endpoint_Write_Stream_LE(ReadBuffer, ReadMemory_XPROG_Params.Length, NO_STREAM_CALLBACK);
358
359 Endpoint_ClearIN();
360 }
361
362 /** Handler for the XPROG CRC command to read a specific memory space's CRC value for comparison between the
363 * attached device's memory and a data set on the host.
364 */
365 static void XPROGProtocol_ReadCRC(void)
366 {
367 uint8_t ReturnStatus = XPRG_ERR_OK;
368
369 struct
370 {
371 uint8_t CRCType;
372 } ReadCRC_XPROG_Params;
373
374 Endpoint_Read_Stream_LE(&ReadCRC_XPROG_Params, sizeof(ReadCRC_XPROG_Params), NO_STREAM_CALLBACK);
375
376 Endpoint_ClearOUT();
377 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
378 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
379
380 uint32_t MemoryCRC;
381
382 if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI)
383 {
384 uint8_t CRCCommand;
385
386 /* Determine which NVM command to send to the device depending on the memory to CRC */
387 switch (ReadCRC_XPROG_Params.CRCType)
388 {
389 case XPRG_CRC_APP:
390 CRCCommand = XMEGA_NVM_CMD_APPCRC;
391 break;
392 case XPRG_CRC_BOOT:
393 CRCCommand = XMEGA_NVM_CMD_BOOTCRC;
394 break;
395 default:
396 CRCCommand = XMEGA_NVM_CMD_FLASHCRC;
397 break;
398 }
399
400 /* Perform and retrieve the memory CRC, indicate timeout if occurred */
401 if (!(XMEGANVM_GetMemoryCRC(CRCCommand, &MemoryCRC)))
402 ReturnStatus = XPRG_ERR_TIMEOUT;
403 }
404 else
405 {
406 /* TPI does not support memory CRC */
407 ReturnStatus = XPRG_ERR_FAILED;
408 }
409
410 Endpoint_Write_Byte(CMD_XPROG);
411 Endpoint_Write_Byte(XPRG_CMD_CRC);
412 Endpoint_Write_Byte(ReturnStatus);
413
414 if (ReturnStatus == XPRG_ERR_OK)
415 {
416 Endpoint_Write_Byte(MemoryCRC >> 16);
417 Endpoint_Write_Word_LE(MemoryCRC & 0xFFFF);
418 }
419
420 Endpoint_ClearIN();
421 }
422
423 /** Handler for the XPROG SET_PARAM command to set a XPROG parameter for use when communicating with the
424 * attached device.
425 */
426 static void XPROGProtocol_SetParam(void)
427 {
428 uint8_t ReturnStatus = XPRG_ERR_OK;
429
430 uint8_t XPROGParam = Endpoint_Read_Byte();
431
432 /* Determine which parameter is being set, store the new parameter value */
433 switch (XPROGParam)
434 {
435 case XPRG_PARAM_NVMBASE:
436 XPROG_Param_NVMBase = Endpoint_Read_DWord_BE();
437 break;
438 case XPRG_PARAM_EEPPAGESIZE:
439 XPROG_Param_EEPageSize = Endpoint_Read_Word_BE();
440 break;
441 case XPRG_PARAM_NVMCMD_REG:
442 XPROG_Param_NVMCMDRegAddr = Endpoint_Read_Byte();
443 break;
444 case XPRG_PARAM_NVMCSR_REG:
445 XPROG_Param_NVMCSRRegAddr = Endpoint_Read_Byte();
446 break;
447 default:
448 ReturnStatus = XPRG_ERR_FAILED;
449 break;
450 }
451
452 Endpoint_ClearOUT();
453 Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
454 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
455
456 Endpoint_Write_Byte(CMD_XPROG);
457 Endpoint_Write_Byte(XPRG_CMD_SET_PARAM);
458 Endpoint_Write_Byte(ReturnStatus);
459 Endpoint_ClearIN();
460 }
461
462 #endif
463