9914c085e5edaab3b46687ba8256b49eaddd8ec0
[pub/USBasp.git] / Projects / AVRISP / Lib / V2ProtocolParams.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 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 .ParamValue = (LUFA_VERSION_INTEGER >> 8),
47 .ParamPrivellages = PARAM_PRIV_READ },
48
49 { .ParamID = PARAM_BUILD_NUMBER_HIGH,
50 .ParamValue = (LUFA_VERSION_INTEGER & 0xFF),
51 .ParamPrivellages = PARAM_PRIV_READ },
52
53 { .ParamID = PARAM_HW_VER,
54 .ParamValue = 0x00,
55 .ParamPrivellages = PARAM_PRIV_READ },
56
57 { .ParamID = PARAM_SW_MAJOR,
58 .ParamValue = 0x01,
59 .ParamPrivellages = PARAM_PRIV_READ },
60
61 { .ParamID = PARAM_SW_MINOR,
62 .ParamValue = 0x0C,
63 .ParamPrivellages = PARAM_PRIV_READ },
64
65 { .ParamID = PARAM_VTARGET,
66 .ParamValue = 0x32,
67 .ParamPrivellages = PARAM_PRIV_READ },
68
69 { .ParamID = PARAM_SCK_DURATION,
70 .ParamValue = (TOTAL_PROGRAMMING_SPEEDS - 1),
71 .ParamPrivellages = PARAM_PRIV_READ | PARAM_PRIV_WRITE },
72
73 { .ParamID = PARAM_RESET_POLARITY,
74 .ParamValue = 0x00,
75 .ParamPrivellages = PARAM_PRIV_WRITE },
76
77 { .ParamID = PARAM_STATUS_TGT_CONN,
78 .ParamValue = 0x00,
79 .ParamPrivellages = PARAM_PRIV_READ },
80
81 { .ParamID = PARAM_DISCHARGEDELAY,
82 .ParamValue = 0x00,
83 .ParamPrivellages = PARAM_PRIV_WRITE },
84 };
85
86
87 /** Loads saved non-volatile parameter values from the EEPROM into the parameter table, as needed. */
88 void V2Params_LoadEEPROMParamValues(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 /** Retrieves the host PC read/write privellages for a given parameter in the parameter table. This should
95 * be called before calls to \ref V2Params_GetParameterValue() or \ref V2Params_SetParameterValue() when
96 * getting or setting parameter values in response to requests from the host.
97 *
98 * \param ParamID Parameter ID whose privellages are to be retrieved from the table
99 *
100 * \return Privellages for the requested parameter, as a mask of PARAM_PRIV_* masks
101 */
102 uint8_t V2Params_GetParameterPrivellages(uint8_t ParamID)
103 {
104 ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID);
105
106 if (ParamInfo == NULL)
107 return 0;
108
109 return ParamInfo->ParamPrivellages;
110 }
111
112 /** Retrieves the current value for a given parameter in the parameter table.
113 *
114 * \param ParamID Parameter ID whose value is to be retrieved from the table
115 *
116 * \return Current value of the parameter in the table, or 0 if not found
117 */
118 uint8_t V2Params_GetParameterValue(uint8_t ParamID)
119 {
120 ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID);
121
122 if (ParamInfo == NULL)
123 return 0;
124
125 return ParamInfo->ParamValue;
126 }
127
128 /** Sets the value for a given parameter in the parameter table.
129 *
130 * \param ParamID Parameter ID whose value is to be set in the table
131 * \param Value New value to set the parameter to
132 *
133 * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
134 */
135 void V2Params_SetParameterValue(uint8_t ParamID, uint8_t Value)
136 {
137 ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID);
138
139 if (ParamInfo == NULL)
140 return;
141
142 ParamInfo->ParamValue = Value;
143
144 /* The target RESET line polarity is a non-volatile parameter, save to EEPROM when changed */
145 if (ParamID == PARAM_RESET_POLARITY)
146 eeprom_write_byte(&EEPROM_Rest_Polarity, Value);
147 }
148
149 /** Retrieves a parameter entry (including ID, value and privellages) from the parameter table that matches the given
150 * parameter ID.
151 *
152 * \param ParamID Parameter ID to find in the table
153 *
154 * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
155 */
156 static ParameterItem_t* V2Params_GetParamFromTable(uint8_t ParamID)
157 {
158 /* Find the parameter in the parameter table if present */
159 for (uint8_t TableIndex = 0; TableIndex < (sizeof(ParameterTable) / sizeof(ParameterTable[0])); TableIndex++)
160 {
161 if (ParamID == ParameterTable[TableIndex].ParamID)
162 return &ParameterTable[TableIndex];
163 }
164
165 return NULL;
166 }