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 /* Volatile Parameter Values for RAM storage */
46 static ParameterItem_t ParameterTable
[] =
48 { .ParamID
= PARAM_BUILD_NUMBER_LOW
,
49 .ParamPrivileges
= PARAM_PRIV_READ
,
52 { .ParamID
= PARAM_BUILD_NUMBER_HIGH
,
53 .ParamPrivileges
= PARAM_PRIV_READ
,
56 { .ParamID
= PARAM_HW_VER
,
57 .ParamPrivileges
= PARAM_PRIV_READ
,
60 { .ParamID
= PARAM_SW_MAJOR
,
61 .ParamPrivileges
= PARAM_PRIV_READ
,
64 { .ParamID
= PARAM_SW_MINOR
,
65 .ParamPrivileges
= PARAM_PRIV_READ
,
66 .ParamValue
= FIRMWARE_VERSION_MINOR
},
68 { .ParamID
= PARAM_VTARGET
,
69 .ParamPrivileges
= PARAM_PRIV_READ
,
70 .ParamValue
= (uint8_t)(3.3 * 10) },
72 { .ParamID
= PARAM_SCK_DURATION
,
73 .ParamPrivileges
= PARAM_PRIV_READ
| PARAM_PRIV_WRITE
,
76 { .ParamID
= PARAM_RESET_POLARITY
,
77 .ParamPrivileges
= PARAM_PRIV_READ
| PARAM_PRIV_WRITE
,
80 { .ParamID
= PARAM_STATUS_TGT_CONN
,
81 .ParamPrivileges
= PARAM_PRIV_READ
,
82 .ParamValue
= STATUS_ISP_READY
},
84 { .ParamID
= PARAM_DISCHARGEDELAY
,
85 .ParamPrivileges
= PARAM_PRIV_READ
| PARAM_PRIV_WRITE
,
90 /** Loads saved non-volatile parameter values from the EEPROM into the parameter table, as needed. */
91 void V2Params_LoadNonVolatileParamValues(void)
93 /* Read parameter values that are stored in non-volatile EEPROM */
94 uint8_t ResetPolarity
= eeprom_read_byte(&EEPROM_Reset_Polarity
);
95 uint8_t SCKDuration
= eeprom_read_byte(&EEPROM_SCK_Duration
);
97 /* Update current parameter table if the EEPROM contents was not blank */
98 if (ResetPolarity
!= 0xFF)
99 V2Params_GetParamFromTable(PARAM_RESET_POLARITY
)->ParamValue
= ResetPolarity
;
101 /* Update current parameter table if the EEPROM contents was not blank */
102 if (SCKDuration
!= 0xFF)
103 V2Params_GetParamFromTable(PARAM_SCK_DURATION
)->ParamValue
= SCKDuration
;
106 /** Updates any parameter values that are sourced from hardware rather than explicitly set by the host, such as
107 * VTARGET levels from the ADC on supported AVR models.
109 void V2Params_UpdateParamValues(void)
111 #if (defined(ADC) && !defined(NO_VTARGET_DETECT))
112 /* Update VTARGET parameter with the latest ADC conversion of VTARGET on supported AVR models */
113 V2Params_GetParamFromTable(PARAM_VTARGET
)->ParamValue
= (((uint16_t)(VTARGET_REF_VOLTS
* 10 * VTARGET_SCALE_FACTOR
) * ADC_GetResult()) / 1024);
117 /** Retrieves the host PC read/write privileges for a given parameter in the parameter table. This should
118 * be called before calls to \ref V2Params_GetParameterValue() or \ref V2Params_SetParameterValue() when
119 * getting or setting parameter values in response to requests from the host.
121 * \param[in] ParamID Parameter ID whose privileges are to be retrieved from the table
123 * \return Privileges for the requested parameter, as a mask of \c PARAM_PRIV_* masks
125 uint8_t V2Params_GetParameterPrivileges(const uint8_t ParamID
)
127 ParameterItem_t
* const ParamInfo
= V2Params_GetParamFromTable(ParamID
);
129 if (ParamInfo
== NULL
)
132 return ParamInfo
->ParamPrivileges
;
135 /** Retrieves the current value for a given parameter in the parameter table.
137 * \note This function does not first check for read privileges - if the value is being sent to the host via a
138 * GET PARAM command, \ref V2Params_GetParameterPrivileges() should be called first to ensure that the
139 * parameter is host-readable.
141 * \param[in] ParamID Parameter ID whose value is to be retrieved from the table
143 * \return Current value of the parameter in the table, or 0 if not found
145 uint8_t V2Params_GetParameterValue(const uint8_t ParamID
)
147 ParameterItem_t
* const ParamInfo
= V2Params_GetParamFromTable(ParamID
);
149 if (ParamInfo
== NULL
)
152 return ParamInfo
->ParamValue
;
155 /** Sets the value for a given parameter in the parameter table.
157 * \note This function does not first check for write privileges - if the value is being sourced from the host
158 * via a SET PARAM command, \ref V2Params_GetParameterPrivileges() should be called first to ensure that the
159 * parameter is host-writable.
161 * \param[in] ParamID Parameter ID whose value is to be set in the table
162 * \param[in] Value New value to set the parameter to
164 * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
166 void V2Params_SetParameterValue(const uint8_t ParamID
,
169 ParameterItem_t
* const ParamInfo
= V2Params_GetParamFromTable(ParamID
);
171 if (ParamInfo
== NULL
)
174 ParamInfo
->ParamValue
= Value
;
176 /* The target RESET line polarity is a non-volatile parameter, save to EEPROM when changed */
177 if (ParamID
== PARAM_RESET_POLARITY
)
178 eeprom_update_byte(&EEPROM_Reset_Polarity
, Value
);
180 /* The target SCK line period is a non-volatile parameter, save to EEPROM when changed */
181 if (ParamID
== PARAM_SCK_DURATION
)
182 eeprom_update_byte(&EEPROM_SCK_Duration
, Value
);
185 /** Retrieves a parameter entry (including ID, value and privileges) from the parameter table that matches the given
188 * \param[in] ParamID Parameter ID to find in the table
190 * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
192 static ParameterItem_t
* const V2Params_GetParamFromTable(const uint8_t ParamID
)
194 ParameterItem_t
* CurrTableItem
= ParameterTable
;
196 /* Find the parameter in the parameter table if present */
197 for (uint8_t TableIndex
= 0; TableIndex
< TABLE_PARAM_COUNT
; TableIndex
++)
199 if (ParamID
== CurrTableItem
->ParamID
)
200 return CurrTableItem
;