Upgrade doxygen configuration files to the latest version.
[pub/lufa.git] / Projects / AVRISP-MKII / Lib / V2ProtocolParams.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2021.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2021 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 disclaims 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 static uint8_t EEMEM EEPROM_Reset_Polarity = 0x01;
41
42 /* Non-Volatile Parameter Values for EEPROM storage */
43 static uint8_t EEMEM EEPROM_SCK_Duration = 0x06;
44
45 /* Randomly generated magic constant for EEPROM data validation */
46 #define EEPROM_MAGIC 0xEB401412UL
47
48 /* EEPROM location which should contain EEPROM_MAGIC. If this value is not correct, all other EEPROM locations are
49 * assumed to contain invalid data, therefore all parameters will be reset to their default values.
50 */
51 static uint32_t EEMEM EEPROM_Magic_Value = EEPROM_MAGIC;
52
53 /* Volatile Parameter Values for RAM storage */
54 static ParameterItem_t ParameterTable[] =
55 {
56 { .ParamID = PARAM_BUILD_NUMBER_LOW,
57 .ParamPrivileges = PARAM_PRIV_READ,
58 .ParamValue = 0 },
59
60 { .ParamID = PARAM_BUILD_NUMBER_HIGH,
61 .ParamPrivileges = PARAM_PRIV_READ,
62 .ParamValue = 0 },
63
64 { .ParamID = PARAM_HW_VER,
65 .ParamPrivileges = PARAM_PRIV_READ,
66 .ParamValue = 0x00 },
67
68 { .ParamID = PARAM_SW_MAJOR,
69 .ParamPrivileges = PARAM_PRIV_READ,
70 .ParamValue = 0x01 },
71
72 { .ParamID = PARAM_SW_MINOR,
73 .ParamPrivileges = PARAM_PRIV_READ,
74 .ParamValue = FIRMWARE_VERSION_MINOR },
75
76 { .ParamID = PARAM_VTARGET,
77 .ParamPrivileges = PARAM_PRIV_READ,
78 .ParamValue = (uint8_t)(3.3 * 10) },
79
80 { .ParamID = PARAM_SCK_DURATION,
81 .ParamPrivileges = PARAM_PRIV_READ | PARAM_PRIV_WRITE,
82 .ParamValue = 6 },
83
84 { .ParamID = PARAM_RESET_POLARITY,
85 .ParamPrivileges = PARAM_PRIV_READ | PARAM_PRIV_WRITE,
86 .ParamValue = 0x01 },
87
88 { .ParamID = PARAM_STATUS_TGT_CONN,
89 .ParamPrivileges = PARAM_PRIV_READ,
90 .ParamValue = STATUS_ISP_READY },
91
92 { .ParamID = PARAM_DISCHARGEDELAY,
93 .ParamPrivileges = PARAM_PRIV_READ | PARAM_PRIV_WRITE,
94 .ParamValue = 0x00 },
95 };
96
97
98 /** Check the non-volatile storage in the EEPROM for the proper magic value, and erase the parameter area if the magic
99 * value was not correct.
100 */
101 static void V2Params_CheckNonVolatileStorage(void)
102 {
103 /* Read the magic value from the EEPROM */
104 uint32_t MagicValue = eeprom_read_dword(&EEPROM_Magic_Value);
105 if (MagicValue != EEPROM_MAGIC) {
106 /* If the magic value was not correct, erase the parameter values in the EEPROM */
107 eeprom_update_byte(&EEPROM_Reset_Polarity, 0xFF);
108 eeprom_update_byte(&EEPROM_SCK_Duration, 0xFF);
109
110 /* Write the correct magic value to confirm that the data stored in the EEPROM is now valid */
111 eeprom_update_dword(&EEPROM_Magic_Value, EEPROM_MAGIC);
112 }
113 }
114
115 /** Loads saved non-volatile parameter values from the EEPROM into the parameter table, as needed. */
116 void V2Params_LoadNonVolatileParamValues(void)
117 {
118 /* Check that the EEPROM contains valid data */
119 V2Params_CheckNonVolatileStorage();
120
121 /* Read parameter values that are stored in non-volatile EEPROM */
122 uint8_t ResetPolarity = eeprom_read_byte(&EEPROM_Reset_Polarity);
123 uint8_t SCKDuration = eeprom_read_byte(&EEPROM_SCK_Duration);
124
125 /* Update current parameter table if the EEPROM contents was not blank */
126 if (ResetPolarity != 0xFF)
127 V2Params_GetParamFromTable(PARAM_RESET_POLARITY)->ParamValue = ResetPolarity;
128
129 /* Update current parameter table if the EEPROM contents was not blank */
130 if (SCKDuration != 0xFF)
131 V2Params_GetParamFromTable(PARAM_SCK_DURATION)->ParamValue = SCKDuration;
132 }
133
134 /** Updates any parameter values that are sourced from hardware rather than explicitly set by the host, such as
135 * VTARGET levels from the ADC on supported AVR models.
136 */
137 void V2Params_UpdateParamValues(void)
138 {
139 #if (defined(ADC) && !defined(NO_VTARGET_DETECT))
140 /* Update VTARGET parameter with the latest ADC conversion of VTARGET on supported AVR models */
141 V2Params_GetParamFromTable(PARAM_VTARGET)->ParamValue = (((uint16_t)(VTARGET_REF_VOLTS * 10 * VTARGET_SCALE_FACTOR) * ADC_GetResult()) / 1024);
142 #endif
143 }
144
145 /** Retrieves the host PC read/write privileges for a given parameter in the parameter table. This should
146 * be called before calls to \ref V2Params_GetParameterValue() or \ref V2Params_SetParameterValue() when
147 * getting or setting parameter values in response to requests from the host.
148 *
149 * \param[in] ParamID Parameter ID whose privileges are to be retrieved from the table
150 *
151 * \return Privileges for the requested parameter, as a mask of \c PARAM_PRIV_* masks
152 */
153 uint8_t V2Params_GetParameterPrivileges(const uint8_t ParamID)
154 {
155 ParameterItem_t* const ParamInfo = V2Params_GetParamFromTable(ParamID);
156
157 if (ParamInfo == NULL)
158 return 0;
159
160 return ParamInfo->ParamPrivileges;
161 }
162
163 /** Retrieves the current value for a given parameter in the parameter table.
164 *
165 * \note This function does not first check for read privileges - if the value is being sent to the host via a
166 * GET PARAM command, \ref V2Params_GetParameterPrivileges() should be called first to ensure that the
167 * parameter is host-readable.
168 *
169 * \param[in] ParamID Parameter ID whose value is to be retrieved from the table
170 *
171 * \return Current value of the parameter in the table, or 0 if not found
172 */
173 uint8_t V2Params_GetParameterValue(const uint8_t ParamID)
174 {
175 ParameterItem_t* const ParamInfo = V2Params_GetParamFromTable(ParamID);
176
177 if (ParamInfo == NULL)
178 return 0;
179
180 return ParamInfo->ParamValue;
181 }
182
183 /** Sets the value for a given parameter in the parameter table.
184 *
185 * \note This function does not first check for write privileges - if the value is being sourced from the host
186 * via a SET PARAM command, \ref V2Params_GetParameterPrivileges() should be called first to ensure that the
187 * parameter is host-writable.
188 *
189 * \param[in] ParamID Parameter ID whose value is to be set in the table
190 * \param[in] Value New value to set the parameter to
191 *
192 * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
193 */
194 void V2Params_SetParameterValue(const uint8_t ParamID,
195 const uint8_t Value)
196 {
197 ParameterItem_t* const ParamInfo = V2Params_GetParamFromTable(ParamID);
198
199 if (ParamInfo == NULL)
200 return;
201
202 ParamInfo->ParamValue = Value;
203
204 /* The target RESET line polarity is a non-volatile parameter, save to EEPROM when changed */
205 if (ParamID == PARAM_RESET_POLARITY)
206 eeprom_update_byte(&EEPROM_Reset_Polarity, Value);
207
208 /* The target SCK line period is a non-volatile parameter, save to EEPROM when changed */
209 if (ParamID == PARAM_SCK_DURATION)
210 eeprom_update_byte(&EEPROM_SCK_Duration, Value);
211 }
212
213 /** Retrieves a parameter entry (including ID, value and privileges) from the parameter table that matches the given
214 * parameter ID.
215 *
216 * \param[in] ParamID Parameter ID to find in the table
217 *
218 * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
219 */
220 static ParameterItem_t* const V2Params_GetParamFromTable(const uint8_t ParamID)
221 {
222 ParameterItem_t* CurrTableItem = ParameterTable;
223
224 /* Find the parameter in the parameter table if present */
225 for (uint8_t TableIndex = 0; TableIndex < TABLE_PARAM_COUNT; TableIndex++)
226 {
227 if (ParamID == CurrTableItem->ParamID)
228 return CurrTableItem;
229
230 CurrTableItem++;
231 }
232
233 return NULL;
234 }
235