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_PDIPROTOCOL_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_EnterXPROGMode(void)
96 uint8_t ReturnStatus
= XPRG_ERR_OK
;
99 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
101 PDIDATA_LINE_DDR
|= PDIDATA_LINE_MASK
;
102 PDICLOCK_LINE_DDR
|= PDICLOCK_LINE_MASK
;
104 PDIDATA_LINE_PORT
|= PDIDATA_LINE_MASK
;
108 for (uint8_t i
= 0; i
< 16; i
++)
110 PDICLOCK_LINE_PORT
^= PDICLOCK_LINE_MASK
;
111 PDICLOCK_LINE_PORT
^= PDICLOCK_LINE_MASK
;
114 static const uint8_t NVMKey
[8] = {0x12, 0x89, 0xAB, 0x45, 0xCD, 0xD8, 0x88, 0xFF};
116 PDITarget_SendByte(PDI_CMD_KEY
);
117 for (uint8_t i
= 0; i
< 8; i
++)
118 PDITarget_SendByte(NVMKey
[i
]);
120 PDITarget_SendByte(PDI_CMD_LDCS
| PD_STATUS_REG
);
121 if (!(PDITarget_ReceiveByte() & PDI_STATUS_NVM
))
122 ReturnStatus
= XPRG_ERR_FAILED
;
124 Endpoint_Write_Byte(CMD_XPROG
);
125 Endpoint_Write_Byte(XPRG_CMD_ENTER_PROGMODE
);
126 Endpoint_Write_Byte(ReturnStatus
);
130 static void PDIProtocol_LeaveXPROGMode(void)
133 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
135 PDIDATA_LINE_DDR
&= ~PDIDATA_LINE_MASK
;
136 PDICLOCK_LINE_DDR
&= ~PDICLOCK_LINE_MASK
;
138 PDIDATA_LINE_PORT
&= ~PDIDATA_LINE_MASK
;
139 PDICLOCK_LINE_PORT
&= ~PDICLOCK_LINE_MASK
;
141 Endpoint_Write_Byte(CMD_XPROG
);
142 Endpoint_Write_Byte(XPRG_CMD_LEAVE_PROGMODE
);
143 Endpoint_Write_Byte(XPRG_ERR_OK
);
147 static void PDIProtocol_EraseChip(void)
149 uint8_t ReturnStatus
= XPRG_ERR_OK
;
155 } Erase_XPROG_Params
;
157 Endpoint_Read_Stream_LE(&Erase_XPROG_Params
, sizeof(Erase_XPROG_Params
));
160 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
162 // TODO: Send erase command here via PDI protocol
164 Endpoint_Write_Byte(CMD_XPROG
);
165 Endpoint_Write_Byte(XPRG_CMD_ERASE
);
166 Endpoint_Write_Byte(ReturnStatus
);
170 static void PDIProtocol_WriteMemory(void)
172 uint8_t ReturnStatus
= XPRG_ERR_OK
;
179 uint8_t ProgData
[256];
180 } WriteMemory_XPROG_Params
;
182 Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params
, (sizeof(WriteMemory_XPROG_Params
) -
183 sizeof(WriteMemory_XPROG_Params
).ProgData
));
184 WriteMemory_XPROG_Params
.Address
= SwapEndian_32(WriteMemory_XPROG_Params
.Address
);
185 WriteMemory_XPROG_Params
.Length
= SwapEndian_16(WriteMemory_XPROG_Params
.Length
);
186 Endpoint_Read_Stream_LE(&WriteMemory_XPROG_Params
.ProgData
, WriteMemory_XPROG_Params
.Length
);
189 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
191 // TODO: Send program command here via PDI protocol
193 Endpoint_Write_Byte(CMD_XPROG
);
194 Endpoint_Write_Byte(XPRG_CMD_READ_MEM
);
195 Endpoint_Write_Byte(ReturnStatus
);
199 static void PDIProtocol_ReadMemory(void)
201 uint8_t ReturnStatus
= XPRG_ERR_OK
;
208 } ReadMemory_XPROG_Params
;
210 Endpoint_Read_Stream_LE(&ReadMemory_XPROG_Params
, sizeof(ReadMemory_XPROG_Params
));
211 ReadMemory_XPROG_Params
.Address
= SwapEndian_32(ReadMemory_XPROG_Params
.Address
);
212 ReadMemory_XPROG_Params
.Length
= SwapEndian_16(ReadMemory_XPROG_Params
.Length
);
215 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
217 // TODO: Send read command here via PDI protocol
219 Endpoint_Write_Byte(CMD_XPROG
);
220 Endpoint_Write_Byte(XPRG_CMD_READ_MEM
);
221 Endpoint_Write_Byte(ReturnStatus
);
224 uint8_t ProgData
[256];
225 for (uint16_t i
= 0; i
< ReadMemory_XPROG_Params
.Length
; i
++)
227 Endpoint_Write_Stream_LE(ProgData
, ReadMemory_XPROG_Params
.Length
);
229 if (!Endpoint_IsReadWriteAllowed())
232 while(!(Endpoint_IsReadWriteAllowed()));
239 static void PDIProtocol_ReadCRC(void)
241 uint8_t ReturnStatus
= XPRG_ERR_OK
;
243 uint8_t CRCType
= Endpoint_Read_Byte();
246 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
248 uint32_t MemoryCRC
= 0;
250 // TODO: Read device CRC for desired memory via PDI protocol
252 Endpoint_Write_Byte(CMD_XPROG
);
253 Endpoint_Write_Byte(XPRG_CMD_CRC
);
254 Endpoint_Write_Byte(ReturnStatus
);
256 if (ReturnStatus
== XPRG_ERR_OK
)
258 Endpoint_Write_Byte(MemoryCRC
>> 16);
259 Endpoint_Write_Word_LE(MemoryCRC
& 0xFFFF);
265 static void PDIProtocol_SetParam(void)
267 uint8_t ReturnStatus
= XPRG_ERR_OK
;
269 uint8_t XPROGParam
= Endpoint_Read_Byte();
271 if (XPROGParam
== XPRG_PARAM_NVMBASE
)
272 XPROG_Param_NVMBase
= Endpoint_Read_DWord_LE();
273 else if (XPROGParam
== XPRG_PARAM_EEPPAGESIZE
)
274 XPROG_Param_EEPageSize
= Endpoint_Read_Word_LE();
276 ReturnStatus
= XPRG_ERR_FAILED
;
279 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN
);
281 Endpoint_Write_Byte(CMD_XPROG
);
282 Endpoint_Write_Byte(XPRG_CMD_SET_PARAM
);
283 Endpoint_Write_Byte(ReturnStatus
);