Enhancements to the AVRISP Programmer project to attempt to get AVRStudio to communic...
[pub/USBasp.git] / Projects / 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_ProcessCommand(void)
64 {
65 uint8_t V2Command = Endpoint_Read_Byte();
66
67 printf("COMMAND %d\r\n", V2Command);
68
69 switch (V2Command)
70 {
71 case CMD_SIGN_ON:
72 V2Protocol_ProcessCmdSignOn();
73 break;
74 case CMD_SET_PARAMETER:
75 V2Protocol_ProcessCmdSetParam();
76 break;
77 case CMD_GET_PARAMETER:
78 V2Protocol_ProcessCmdGetParam();
79 break;
80 default:
81 Endpoint_ClearOUT();
82 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
83 Endpoint_Write_Byte(STATUS_CMD_UNKNOWN);
84 Endpoint_ClearIN();
85 break;
86 }
87
88 /* Reset Endpoint direction to OUT ready for next command */
89 Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
90 }
91
92 static ParameterItem_t* V2Protocol_GetParameterItem(uint8_t ParamID)
93 {
94 for (uint8_t TableIndex = 0; TableIndex < (sizeof(ParameterTable) / sizeof(ParameterTable[0])); TableIndex++)
95 {
96 if (ParamID == eeprom_read_byte(&ParameterTable[TableIndex].ParameterID))
97 return &ParameterTable[TableIndex];
98 }
99
100 return NULL;
101 }
102
103 static void V2Protocol_ProcessCmdSignOn(void)
104 {
105 Endpoint_ClearOUT();
106 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
107
108 Endpoint_Write_Byte(CMD_SIGN_ON);
109 Endpoint_Write_Byte(STATUS_CMD_OK);
110 Endpoint_Write_Byte(PROGRAMMER_ID_LEN);
111 Endpoint_Write_Stream_LE(PROGRAMMER_ID, PROGRAMMER_ID_LEN);
112 Endpoint_ClearIN();
113 }
114
115 static void V2Protocol_ProcessCmdSetParam(void)
116 {
117 uint8_t ParamID = Endpoint_Read_Byte();
118 uint8_t ParamValue = Endpoint_Read_Byte();
119
120 ParameterItem_t* ParameterItem = V2Protocol_GetParameterItem(ParamID);
121
122 Endpoint_ClearOUT();
123 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
124
125 if (ParameterItem != NULL)
126 {
127 eeprom_write_byte(&ParameterItem->ParameterValue, ParamValue);
128
129 Endpoint_Write_Byte(CMD_SET_PARAMETER);
130 Endpoint_Write_Byte(STATUS_CMD_OK);
131 }
132 else
133 {
134 Endpoint_Write_Byte(STATUS_CMD_FAILED);
135 }
136
137 Endpoint_ClearIN();
138 }
139
140 static void V2Protocol_ProcessCmdGetParam(void)
141 {
142 uint8_t ParamID = Endpoint_Read_Byte();
143
144 ParameterItem_t* ParameterItem = V2Protocol_GetParameterItem(ParamID);
145
146 Endpoint_ClearOUT();
147 Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
148
149 if (ParameterItem != NULL)
150 {
151 Endpoint_Write_Byte(CMD_GET_PARAMETER);
152 Endpoint_Write_Byte(STATUS_CMD_OK);
153 Endpoint_Write_Byte(eeprom_read_byte(&ParameterItem->ParameterValue));
154 }
155 else
156 {
157 Endpoint_Write_Byte(STATUS_CMD_FAILED);
158 }
159
160 Endpoint_ClearIN();
161 }