Altered all endpoint/pipe stream transfers so that the new BytesProcessed parameter...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / V2ProtocolParams.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 * V2Protocol parameter handler, to process V2 Protocol device parameters.
34 */
35
36 #define INCLUDE_FROM_V2PROTOCOL_PARAMS_C
37 #include "V2ProtocolParams.h"
38
39 /* Non-Volatile Parameter Values for EEPROM storage */
40 uint8_t EEMEM EEPROM_Rest_Polarity = 0x00;
41
42 /* Volatile Parameter Values for RAM storage */
43 static ParameterItem_t ParameterTable[] =
44 {
45 { .ParamID = PARAM_BUILD_NUMBER_LOW,
46 .ParamPrivileges = PARAM_PRIV_READ,
47 .ParamValue = 0 },
48
49 { .ParamID = PARAM_BUILD_NUMBER_HIGH,
50 .ParamPrivileges = PARAM_PRIV_READ,
51 .ParamValue = 0 },
52
53 { .ParamID = PARAM_HW_VER,
54 .ParamPrivileges = PARAM_PRIV_READ,
55 .ParamValue = 0x00 },
56
57 { .ParamID = PARAM_SW_MAJOR,
58 .ParamPrivileges = PARAM_PRIV_READ,
59 .ParamValue = 0x01 },
60
61 { .ParamID = PARAM_SW_MINOR,
62 .ParamPrivileges = PARAM_PRIV_READ,
63 .ParamValue = 0x0D },
64
65 { .ParamID = PARAM_VTARGET,
66 .ParamPrivileges = PARAM_PRIV_READ,
67 .ParamValue = 0x32 },
68
69 { .ParamID = PARAM_SCK_DURATION,
70 .ParamPrivileges = PARAM_PRIV_READ | PARAM_PRIV_WRITE,
71 .ParamValue = 6 },
72
73 { .ParamID = PARAM_RESET_POLARITY,
74 .ParamPrivileges = PARAM_PRIV_WRITE,
75 .ParamValue = 0x01 },
76
77 { .ParamID = PARAM_STATUS_TGT_CONN,
78 .ParamPrivileges = PARAM_PRIV_READ,
79 .ParamValue = STATUS_ISP_READY },
80
81 { .ParamID = PARAM_DISCHARGEDELAY,
82 .ParamPrivileges = PARAM_PRIV_WRITE,
83 .ParamValue = 0x00 },
84 };
85
86
87 /** Loads saved non-volatile parameter values from the EEPROM into the parameter table, as needed. */
88 void V2Params_LoadNonVolatileParamValues(void)
89 {
90 /* Target RESET line polarity is a non-volatile value, retrieve current parameter value from EEPROM */
91 V2Params_GetParamFromTable(PARAM_RESET_POLARITY)->ParamValue = eeprom_read_byte(&EEPROM_Rest_Polarity);
92 }
93
94 /** Updates any parameter values that are sourced from hardware rather than explicitly set by the host, such as
95 * VTARGET levels from the ADC on supported AVR models.
96 */
97 void V2Params_UpdateParamValues(void)
98 {
99 #if (defined(ADC) && !defined(NO_VTARGET_DETECT))
100 /* Update VTARGET parameter with the latest ADC conversion of VTARGET on supported AVR models */
101 V2Params_GetParamFromTable(PARAM_VTARGET)->ParamValue = (((uint16_t)(VTARGET_REF_VOLTS * 10 * VTARGET_SCALE_FACTOR) * ADC_GetResult()) / 1024);
102 #endif
103 }
104
105 /** Retrieves the host PC read/write privileges for a given parameter in the parameter table. This should
106 * be called before calls to \ref V2Params_GetParameterValue() or \ref V2Params_SetParameterValue() when
107 * getting or setting parameter values in response to requests from the host.
108 *
109 * \param[in] ParamID Parameter ID whose privileges are to be retrieved from the table
110 *
111 * \return Privileges for the requested parameter, as a mask of PARAM_PRIV_* masks
112 */
113 uint8_t V2Params_GetParameterPrivileges(const uint8_t ParamID)
114 {
115 ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID);
116
117 if (ParamInfo == NULL)
118 return 0;
119
120 return ParamInfo->ParamPrivileges;
121 }
122
123 /** Retrieves the current value for a given parameter in the parameter table.
124 *
125 * \note This function does not first check for read privileges - if the value is being sent to the host via a
126 * GET PARAM command, \ref V2Params_GetParameterPrivileges() should be called first to ensure that the
127 * parameter is host-readable.
128 *
129 * \param[in] ParamID Parameter ID whose value is to be retrieved from the table
130 *
131 * \return Current value of the parameter in the table, or 0 if not found
132 */
133 uint8_t V2Params_GetParameterValue(const uint8_t ParamID)
134 {
135 ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID);
136
137 if (ParamInfo == NULL)
138 return 0;
139
140 return ParamInfo->ParamValue;
141 }
142
143 /** Sets the value for a given parameter in the parameter table.
144 *
145 * \note This function does not first check for write privileges - if the value is being sourced from the host
146 * via a SET PARAM command, \ref V2Params_GetParameterPrivileges() should be called first to ensure that the
147 * parameter is host-writable.
148 *
149 * \param[in] ParamID Parameter ID whose value is to be set in the table
150 * \param[in] Value New value to set the parameter to
151 *
152 * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
153 */
154 void V2Params_SetParameterValue(const uint8_t ParamID,
155 const uint8_t Value)
156 {
157 ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID);
158
159 if (ParamInfo == NULL)
160 return;
161
162 ParamInfo->ParamValue = Value;
163
164 /* The target RESET line polarity is a non-volatile parameter, save to EEPROM when changed */
165 if (ParamID == PARAM_RESET_POLARITY)
166 eeprom_update_byte(&EEPROM_Rest_Polarity, Value);
167 }
168
169 /** Retrieves a parameter entry (including ID, value and privileges) from the parameter table that matches the given
170 * parameter ID.
171 *
172 * \param[in] ParamID Parameter ID to find in the table
173 *
174 * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
175 */
176 static ParameterItem_t* V2Params_GetParamFromTable(const uint8_t ParamID)
177 {
178 ParameterItem_t* CurrTableItem = ParameterTable;
179
180 /* Find the parameter in the parameter table if present */
181 for (uint8_t TableIndex = 0; TableIndex < TABLE_PARAM_COUNT; TableIndex++)
182 {
183 if (ParamID == CurrTableItem->ParamID)
184 return CurrTableItem;
185
186 CurrTableItem++;
187 }
188
189 return NULL;
190 }
191