]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/Projects/AVRISP-MKII/Lib/V2ProtocolParams.c
Merge pull request #1471 from qmk/lufa_lib
[qmk_firmware.git] / lib / lufa / Projects / AVRISP-MKII / Lib / V2ProtocolParams.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  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 /* Volatile Parameter Values for RAM storage */
46 static ParameterItem_t ParameterTable[] =
47         {
48                 { .ParamID          = PARAM_BUILD_NUMBER_LOW,
49                   .ParamPrivileges  = PARAM_PRIV_READ,
50                   .ParamValue       = 0                                  },
51
52                 { .ParamID          = PARAM_BUILD_NUMBER_HIGH,
53                   .ParamPrivileges  = PARAM_PRIV_READ,
54                   .ParamValue       = 0                                  },
55
56                 { .ParamID          = PARAM_HW_VER,
57                   .ParamPrivileges  = PARAM_PRIV_READ,
58                   .ParamValue       = 0x00                               },
59
60                 { .ParamID          = PARAM_SW_MAJOR,
61                   .ParamPrivileges  = PARAM_PRIV_READ,
62                   .ParamValue       = 0x01                               },
63
64                 { .ParamID          = PARAM_SW_MINOR,
65                   .ParamPrivileges  = PARAM_PRIV_READ,
66                   .ParamValue       = FIRMWARE_VERSION_MINOR             },
67
68                 { .ParamID          = PARAM_VTARGET,
69                   .ParamPrivileges  = PARAM_PRIV_READ,
70                   .ParamValue       = (uint8_t)(3.3 * 10)                },
71
72                 { .ParamID          = PARAM_SCK_DURATION,
73                   .ParamPrivileges  = PARAM_PRIV_READ | PARAM_PRIV_WRITE,
74                   .ParamValue       = 6                                  },
75
76                 { .ParamID          = PARAM_RESET_POLARITY,
77                   .ParamPrivileges  = PARAM_PRIV_READ | PARAM_PRIV_WRITE,
78                   .ParamValue       = 0x01                               },
79
80                 { .ParamID          = PARAM_STATUS_TGT_CONN,
81                   .ParamPrivileges  = PARAM_PRIV_READ,
82                   .ParamValue       = STATUS_ISP_READY                   },
83
84                 { .ParamID          = PARAM_DISCHARGEDELAY,
85                   .ParamPrivileges  = PARAM_PRIV_READ | PARAM_PRIV_WRITE,
86                   .ParamValue       = 0x00                               },
87         };
88
89
90 /** Loads saved non-volatile parameter values from the EEPROM into the parameter table, as needed. */
91 void V2Params_LoadNonVolatileParamValues(void)
92 {
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);
96
97         /* Update current parameter table if the EEPROM contents was not blank */
98         if (ResetPolarity != 0xFF)
99           V2Params_GetParamFromTable(PARAM_RESET_POLARITY)->ParamValue = ResetPolarity;
100
101         /* Update current parameter table if the EEPROM contents was not blank */
102         if (SCKDuration != 0xFF)
103           V2Params_GetParamFromTable(PARAM_SCK_DURATION)->ParamValue   = SCKDuration;
104 }
105
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.
108  */
109 void V2Params_UpdateParamValues(void)
110 {
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);
114         #endif
115 }
116
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.
120  *
121  *  \param[in] ParamID  Parameter ID whose privileges are to be retrieved from the table
122  *
123  *  \return Privileges for the requested parameter, as a mask of \c PARAM_PRIV_* masks
124  */
125 uint8_t V2Params_GetParameterPrivileges(const uint8_t ParamID)
126 {
127         ParameterItem_t* const ParamInfo = V2Params_GetParamFromTable(ParamID);
128
129         if (ParamInfo == NULL)
130           return 0;
131
132         return ParamInfo->ParamPrivileges;
133 }
134
135 /** Retrieves the current value for a given parameter in the parameter table.
136  *
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.
140  *
141  *  \param[in] ParamID  Parameter ID whose value is to be retrieved from the table
142  *
143  *  \return Current value of the parameter in the table, or 0 if not found
144  */
145 uint8_t V2Params_GetParameterValue(const uint8_t ParamID)
146 {
147         ParameterItem_t* const ParamInfo = V2Params_GetParamFromTable(ParamID);
148
149         if (ParamInfo == NULL)
150           return 0;
151
152         return ParamInfo->ParamValue;
153 }
154
155 /** Sets the value for a given parameter in the parameter table.
156  *
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.
160  *
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
163  *
164  *  \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
165  */
166 void V2Params_SetParameterValue(const uint8_t ParamID,
167                                 const uint8_t Value)
168 {
169         ParameterItem_t* const ParamInfo = V2Params_GetParamFromTable(ParamID);
170
171         if (ParamInfo == NULL)
172           return;
173
174         ParamInfo->ParamValue = Value;
175
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);
179
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);
183 }
184
185 /** Retrieves a parameter entry (including ID, value and privileges) from the parameter table that matches the given
186  *  parameter ID.
187  *
188  *  \param[in] ParamID  Parameter ID to find in the table
189  *
190  *  \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
191  */
192 static ParameterItem_t* const V2Params_GetParamFromTable(const uint8_t ParamID)
193 {
194         ParameterItem_t* CurrTableItem = ParameterTable;
195
196         /* Find the parameter in the parameter table if present */
197         for (uint8_t TableIndex = 0; TableIndex < TABLE_PARAM_COUNT; TableIndex++)
198         {
199                 if (ParamID == CurrTableItem->ParamID)
200                   return CurrTableItem;
201
202                 CurrTableItem++;
203         }
204
205         return NULL;
206 }
207