Added handler for the V2 Protocol SPI_MULTI command to the AVRISP Programmer project.
[pub/USBasp.git] / Projects / Unfinished / AVRISP / Lib / V2Protocol.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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 * V2Protocol handler, to process V2 Protocol commands used in Atmel programmer devices.
34 */
35
36 #define INCLUDE_FROM_V2PROTOCOL_C
37 #include "V2Protocol.h"
38
39 ParameterItem_t ParameterTable[] EEMEM =
40 {
41 { .ParameterID = PARAM_BUILD_NUMBER_LOW,
42 .ParameterValue = 0x00 },
43 { .ParameterID = PARAM_BUILD_NUMBER_HIGH,
44 .ParameterValue = 0x00 },
45 { .ParameterID = PARAM_HW_VER,
46 .ParameterValue = 0x01 },
47 { .ParameterID = PARAM_SW_MAJOR,
48 .ParameterValue = 0x01 },
49 { .ParameterID = PARAM_SW_MINOR,
50 .ParameterValue = 0x00 },
51 { .ParameterID = PARAM_VTARGET,
52 .ParameterValue = 0x00 },
53 { .ParameterID = PARAM_SCK_DURATION,
54 .ParameterValue = 0x00 },
55 { .ParameterID = PARAM_RESET_POLARITY,
56 .ParameterValue = 0x00 },
57 { .ParameterID = PARAM_STATUS_TGT_CONN,
58 .ParameterValue = 0x00 },
59 { .ParameterID = PARAM_DISCHARGEDELAY,
60 .ParameterValue = 0x00 },
61 };
62
63 void V2Protocol_ConfigureHardware(void)
64 {
65 #if F_CPU == 8000000
66 uint8_t SPIMaskFromSCKDuration[] = {SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128,
67 SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128};
68 #else
69 uint8_t SPIMaskFromSCKDuration[] = {SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128,
70 SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128};
71 #endif
72
73 uint8_t SCKDuration = eeprom_read_byte(&V2Protocol_GetParameterItem(PARAM_SCK_DURATION)->ParameterValue);
74
75 if (SCKDuration > sizeof(SPIMaskFromSCKDuration))
76 SCKDuration = SPIMaskFromSCKDuration;
77
78 SPI_Init(SPIMaskFromSCKDuration[SCKDuration], true);
79 }
80
81 void V2Protocol_ProcessCommand(void)
82 {
83 uint8_t V2Command = Endpoint_Read_Byte();
84
85 switch (V2Command)
86 {
87 case CMD_SIGN_ON:
88 V2Protocol_ProcessCmdSignOn();
89 break;
90 case CMD_SET_PARAMETER:
91 case CMD_GET_PARAMETER:
92 V2Protocol_ProcessCmdGetSetParam(V2Command);
93 break;
94 case CMD_SPI_MULTI:
95 V2Protocol_ProcessCmdSPIMulti();
96 break;
97 default:
98 while (Endpoint_BytesInEndpoint() == AVRISP_DATA_EPSIZE)
99 {
100 Endpoint_ClearOUT();
101 while (!(Endpoint_IsOUTReceived()));
102 }
103
104 Endpoint_ClearOUT();
105 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
106
107 Endpoint_Write_Byte(V2Command);
108 Endpoint_Write_Byte(STATUS_CMD_UNKNOWN);
109 Endpoint_ClearIN();
110 break;
111 }
112
113 printf("COMMAND 0x%02x\r\n", V2Command);
114
115 Endpoint_WaitUntilReady();
116 Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
117 }
118
119 static ParameterItem_t* V2Protocol_GetParameterItem(uint8_t ParamID)
120 {
121 for (uint8_t TableIndex = 0; TableIndex < (sizeof(ParameterTable) / sizeof(ParameterTable[0])); TableIndex++)
122 {
123 if (ParamID == eeprom_read_byte(&ParameterTable[TableIndex].ParameterID))
124 return &ParameterTable[TableIndex];
125 }
126
127 return NULL;
128 }
129
130 static void V2Protocol_ProcessCmdSignOn(void)
131 {
132 Endpoint_ClearOUT();
133 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
134 Endpoint_WaitUntilReady();
135
136 V2Protocol_ConfigureHardware();
137
138 Endpoint_Write_Byte(CMD_SIGN_ON);
139 Endpoint_Write_Byte(STATUS_CMD_OK);
140 Endpoint_Write_Byte(PROGRAMMER_ID_LEN);
141 Endpoint_Write_Stream_LE(PROGRAMMER_ID, PROGRAMMER_ID_LEN);
142 Endpoint_ClearIN();
143 }
144
145 static void V2Protocol_ProcessCmdGetSetParam(uint8_t V2Command)
146 {
147 uint8_t ParamID = Endpoint_Read_Byte();
148 uint8_t ParamValue = Endpoint_Read_Byte();
149
150 Endpoint_ClearOUT();
151 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
152 Endpoint_WaitUntilReady();
153
154 ParameterItem_t* ParameterItem = V2Protocol_GetParameterItem(ParamID);
155
156 Endpoint_Write_Byte(V2Command);
157
158 if (ParameterItem != NULL)
159 {
160 Endpoint_Write_Byte(STATUS_CMD_OK);
161
162 if (V2Command == CMD_SET_PARAMETER)
163 eeprom_write_byte(&ParameterItem->ParameterValue, ParamValue);
164 else
165 Endpoint_Write_Byte(eeprom_read_byte(&ParameterItem->ParameterValue));
166 }
167 else
168 {
169 Endpoint_Write_Byte(STATUS_CMD_FAILED);
170 }
171
172 Endpoint_ClearIN();
173 }
174
175 static void V2Protocol_ProcessCmdSPIMulti(void)
176 {
177 uint8_t TxBytes = Endpoint_Read_Byte();
178 uint8_t RxBytes = Endpoint_Read_Byte();
179 uint8_t RxStartAddr = Endpoint_Read_Byte();
180 uint8_t TxData[255];
181
182 Endpoint_Read_Stream_LE(TxData, TxBytes);
183
184 Endpoint_ClearOUT();
185 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
186 Endpoint_WaitUntilReady();
187
188 Endpoint_Write_Byte(CMD_SPI_MULTI);
189 Endpoint_Write_Byte(STATUS_CMD_OK);
190
191 uint8_t CurrTxPos = 0;
192 uint8_t CurrRxPos = 0;
193
194 while (CurrTxPos < RxStartAddr)
195 {
196 if (CurrTxPos < TxBytes)
197 SPI_SendByte(TxData[CurrTxPos]);
198 else
199 SPI_SendByte(0);
200
201 CurrTxPos++;
202 }
203
204 while (CurrRxPos < RxBytes)
205 {
206 if (CurrTxPos < TxBytes)
207 {
208 Endpoint_Write_Byte(SPI_TransferByte(TxData[CurrTxPos]));
209 CurrTxPos++;
210 }
211 else
212 {
213 Endpoint_Write_Byte(SPI_ReceiveByte());
214 }
215
216 CurrRxPos++;
217 }
218
219 Endpoint_Write_Byte(STATUS_CMD_OK);
220 Endpoint_ClearIN();
221 }