3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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.
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
31 #if defined(ENABLE_XPROG_PROTOCOL)
35 * PDI Protocol handler, to process V2 Protocol wrapped PDI commands used in Atmel programmer devices.
38 #define INCLUDE_FROM_XPROG_C
39 #include "PDIProtocol.h"
41 uint32_t XPROG_Param_NVMBase
;
42 uint32_t XPROG_Param_EEPageSize
;
44 /** Handler for the CMD_XPROG_SETMODE command, which sets the programmer-to-target protocol used for PDI
45 * XMEGA programming (either PDI or JTAG). Only PDI programming is supported.
47 void PDIProtocol_XPROG_SetMode(void)
52 } SetMode_XPROG_Params
;
54 Endpoint_Read_Stream_LE(&SetMode_XPROG_Params
, sizeof(SetMode_XPROG_Params
));
57 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
59 Endpoint_Write_Byte(CMD_XPROG_SETMODE
);
60 Endpoint_Write_Byte(SetMode_XPROG_Params
.Protocol ? STATUS_CMD_FAILED
: STATUS_CMD_OK
);
64 void PDIProtocol_XPROG_Command(void)
66 uint8_t XPROGCommand
= Endpoint_Read_Byte();
70 case XPRG_CMD_ENTER_PROGMODE
:
71 PDIProtocol_EnterXPROGMode();
73 case XPRG_CMD_LEAVE_PROGMODE
:
74 PDIProtocol_LeaveXPROGMode();
77 PDIProtocol_EraseChip();
79 case XPRG_CMD_WRITE_MEM
:
80 PDIProtocol_WriteMemory();
82 case XPRG_CMD_READ_MEM
:
83 PDIProtocol_ReadMemory();
86 PDIProtocol_ReadCRC();
88 case XPRG_CMD_SET_PARAM
:
89 PDIProtocol_SetParam();
94 static void PDIProtocol_EraseChip(void)
96 uint8_t ReturnStatus
= XPRG_ERR_OK
;
102 } Erase_XPROG_Params
;
104 Endpoint_Read_Stream_LE(&Erase_XPROG_Params
, sizeof(Erase_XPROG_Params
));
107 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
109 // TODO: Send erase command here via PDI protocol
111 Endpoint_Write_Byte(CMD_XPROG
);
112 Endpoint_Write_Byte(XPRG_CMD_ERASE
);
113 Endpoint_Write_Byte(ReturnStatus
);
117 static void PDIProtocol_WriteMemory(void)
119 uint8_t ReturnStatus
= XPRG_ERR_OK
;
126 uint8_t ProgData
[256];
127 } WriteMemory_XPROG_Params
;
129 Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params
, (sizeof(WriteMemory_XPROG_Params
) -
130 sizeof(WriteMemory_XPROG_Params
).ProgData
));
131 WriteMemory_XPROG_Params
.Address
= SwapEndian_32(WriteMemory_XPROG_Params
.Address
);
132 WriteMemory_XPROG_Params
.Length
= SwapEndian_16(WriteMemory_XPROG_Params
.Length
);
133 Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params
.ProgData
, WriteMemory_XPROG_Params
.Length
);
136 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
138 // TODO: Send program command here via PDI protocol
140 Endpoint_Write_Byte(CMD_XPROG
);
141 Endpoint_Write_Byte(XPRG_CMD_READ_MEM
);
142 Endpoint_Write_Byte(ReturnStatus
);
146 static void PDIProtocol_ReadMemory(void)
148 uint8_t ReturnStatus
= XPRG_ERR_OK
;
155 } ReadMemory_XPROG_Params
;
157 Endpoint_Read_Stream_LE(&ReadMemory_XPROG_Params
, sizeof(ReadMemory_XPROG_Params
));
158 ReadMemory_XPROG_Params
.Address
= SwapEndian_32(ReadMemory_XPROG_Params
.Address
);
159 ReadMemory_XPROG_Params
.Length
= SwapEndian_16(ReadMemory_XPROG_Params
.Length
);
162 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
164 // TODO: Send read command here via PDI protocol
166 Endpoint_Write_Byte(CMD_XPROG
);
167 Endpoint_Write_Byte(XPRG_CMD_READ_MEM
);
168 Endpoint_Write_Byte(ReturnStatus
);
171 uint8_t ProgData
[256];
172 for (uint16_t i
= 0; i
< ReadMemory_XPROG_Params
.Length
; i
++)
174 Endpoint_Write_Stream_LE(ProgData
, ReadMemory_XPROG_Params
.Length
);
176 if (!Endpoint_IsReadWriteAllowed())
179 while(!(Endpoint_IsReadWriteAllowed()));
186 static void PDIProtocol_ReadCRC(void)
188 uint8_t ReturnStatus
= XPRG_ERR_OK
;
190 uint8_t CRCType
= Endpoint_Read_Byte();
193 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
195 uint32_t MemoryCRC
= 0;
197 // TODO: Read device CRC for desired memory via PDI protocol
199 Endpoint_Write_Byte(CMD_XPROG
);
200 Endpoint_Write_Byte(XPRG_CMD_CRC
);
201 Endpoint_Write_Byte(ReturnStatus
);
203 if (ReturnStatus
== XPRG_ERR_OK
)
205 Endpoint_Write_Byte(MemoryCRC
>> 16);
206 Endpoint_Write_Word_LE(MemoryCRC
& 0xFFFF);
212 static void PDIProtocol_EnterXPROGMode(void)
214 uint8_t ReturnStatus
= XPRG_ERR_OK
;
217 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
219 // TODO: Enter device programming mode here via PDI protocol
221 Endpoint_Write_Byte(CMD_XPROG
);
222 Endpoint_Write_Byte(XPRG_CMD_ENTER_PROGMODE
);
223 Endpoint_Write_Byte(ReturnStatus
);
227 static void PDIProtocol_LeaveXPROGMode(void)
230 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
232 // TODO: Exit device programming mode here via PDI protocol
234 Endpoint_Write_Byte(CMD_XPROG
);
235 Endpoint_Write_Byte(XPRG_CMD_LEAVE_PROGMODE
);
236 Endpoint_Write_Byte(XPRG_ERR_OK
);
240 static void PDIProtocol_SetParam(void)
242 uint8_t ReturnStatus
= XPRG_ERR_OK
;
244 uint8_t XPROGParam
= Endpoint_Read_Byte();
246 if (XPROGParam
== XPRG_PARAM_NVMBASE
)
247 XPROG_Param_NVMBase
= Endpoint_Read_DWord_LE();
248 else if (XPROGParam
== XPRG_PARAM_EEPPAGESIZE
)
249 XPROG_Param_EEPageSize
= Endpoint_Read_Word_LE();
251 ReturnStatus
= XPRG_ERR_FAILED
;
254 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
256 Endpoint_Write_Byte(CMD_XPROG
);
257 Endpoint_Write_Byte(XPRG_CMD_SET_PARAM
);
258 Endpoint_Write_Byte(ReturnStatus
);