3 Copyright (C) Dean Camera, 2021.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2021 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
33 * V2Protocol parameter handler, to process V2 Protocol device parameters.
36 #define INCLUDE_FROM_V2PROTOCOL_PARAMS_C
37 #include "V2ProtocolParams.h"
39 /* Non-Volatile Parameter Values for EEPROM storage */
40 static uint8_t EEMEM EEPROM_Reset_Polarity
= 0x01;
42 /* Non-Volatile Parameter Values for EEPROM storage */
43 static uint8_t EEMEM EEPROM_SCK_Duration
= 0x06;
45 /* Randomly generated magic constant for EEPROM data validation */
46 #define EEPROM_MAGIC 0xEB401412UL
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.
51 static uint32_t EEMEM EEPROM_Magic_Value
= EEPROM_MAGIC
;
53 /* Volatile Parameter Values for RAM storage */
54 static ParameterItem_t ParameterTable
[] =
56 { .ParamID
= PARAM_BUILD_NUMBER_LOW
,
57 .ParamPrivileges
= PARAM_PRIV_READ
,
60 { .ParamID
= PARAM_BUILD_NUMBER_HIGH
,
61 .ParamPrivileges
= PARAM_PRIV_READ
,
64 { .ParamID
= PARAM_HW_VER
,
65 .ParamPrivileges
= PARAM_PRIV_READ
,
68 { .ParamID
= PARAM_SW_MAJOR
,
69 .ParamPrivileges
= PARAM_PRIV_READ
,
72 { .ParamID
= PARAM_SW_MINOR
,
73 .ParamPrivileges
= PARAM_PRIV_READ
,
74 .ParamValue
= FIRMWARE_VERSION_MINOR
},
76 { .ParamID
= PARAM_VTARGET
,
77 .ParamPrivileges
= PARAM_PRIV_READ
,
78 .ParamValue
= (uint8_t)(3.3 * 10) },
80 { .ParamID
= PARAM_SCK_DURATION
,
81 .ParamPrivileges
= PARAM_PRIV_READ
| PARAM_PRIV_WRITE
,
84 { .ParamID
= PARAM_RESET_POLARITY
,
85 .ParamPrivileges
= PARAM_PRIV_READ
| PARAM_PRIV_WRITE
,
88 { .ParamID
= PARAM_STATUS_TGT_CONN
,
89 .ParamPrivileges
= PARAM_PRIV_READ
,
90 .ParamValue
= STATUS_ISP_READY
},
92 { .ParamID
= PARAM_DISCHARGEDELAY
,
93 .ParamPrivileges
= PARAM_PRIV_READ
| PARAM_PRIV_WRITE
,
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.
101 static void V2Params_CheckNonVolatileStorage(void)
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);
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
);
115 /** Loads saved non-volatile parameter values from the EEPROM into the parameter table, as needed. */
116 void V2Params_LoadNonVolatileParamValues(void)
118 /* Check that the EEPROM contains valid data */
119 V2Params_CheckNonVolatileStorage();
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
);
125 /* Update current parameter table if the EEPROM contents was not blank */
126 if (ResetPolarity
!= 0xFF)
127 V2Params_GetParamFromTable(PARAM_RESET_POLARITY
)->ParamValue
= ResetPolarity
;
129 /* Update current parameter table if the EEPROM contents was not blank */
130 if (SCKDuration
!= 0xFF)
131 V2Params_GetParamFromTable(PARAM_SCK_DURATION
)->ParamValue
= SCKDuration
;
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.
137 void V2Params_UpdateParamValues(void)
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);
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.
149 * \param[in] ParamID Parameter ID whose privileges are to be retrieved from the table
151 * \return Privileges for the requested parameter, as a mask of \c PARAM_PRIV_* masks
153 uint8_t V2Params_GetParameterPrivileges(const uint8_t ParamID
)
155 ParameterItem_t
* const ParamInfo
= V2Params_GetParamFromTable(ParamID
);
157 if (ParamInfo
== NULL
)
160 return ParamInfo
->ParamPrivileges
;
163 /** Retrieves the current value for a given parameter in the parameter table.
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.
169 * \param[in] ParamID Parameter ID whose value is to be retrieved from the table
171 * \return Current value of the parameter in the table, or 0 if not found
173 uint8_t V2Params_GetParameterValue(const uint8_t ParamID
)
175 ParameterItem_t
* const ParamInfo
= V2Params_GetParamFromTable(ParamID
);
177 if (ParamInfo
== NULL
)
180 return ParamInfo
->ParamValue
;
183 /** Sets the value for a given parameter in the parameter table.
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.
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
192 * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
194 void V2Params_SetParameterValue(const uint8_t ParamID
,
197 ParameterItem_t
* const ParamInfo
= V2Params_GetParamFromTable(ParamID
);
199 if (ParamInfo
== NULL
)
202 ParamInfo
->ParamValue
= Value
;
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
);
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
);
213 /** Retrieves a parameter entry (including ID, value and privileges) from the parameter table that matches the given
216 * \param[in] ParamID Parameter ID to find in the table
218 * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
220 static ParameterItem_t
* const V2Params_GetParamFromTable(const uint8_t ParamID
)
222 ParameterItem_t
* CurrTableItem
= ParameterTable
;
224 /* Find the parameter in the parameter table if present */
225 for (uint8_t TableIndex
= 0; TableIndex
< TABLE_PARAM_COUNT
; TableIndex
++)
227 if (ParamID
== CurrTableItem
->ParamID
)
228 return CurrTableItem
;