3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  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 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 
  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 uint8_t EEMEM EEPROM_Rest_Polarity 
= 0x00; 
  42 /* Volatile Parameter Values for RAM storage */ 
  43 static ParameterItem_t ParameterTable
[] =  
  45                 { .ParamID          
= PARAM_BUILD_NUMBER_LOW
, 
  46                   .ParamPrivileges  
= PARAM_PRIV_READ
, 
  47                   .ParamValue       
= (LUFA_VERSION_INTEGER 
>> 8)        }, 
  49                 { .ParamID          
= PARAM_BUILD_NUMBER_HIGH
, 
  50                   .ParamPrivileges  
= PARAM_PRIV_READ
, 
  51                   .ParamValue       
= (LUFA_VERSION_INTEGER 
& 0xFF),     }, 
  53                 { .ParamID          
= PARAM_HW_VER
, 
  54                   .ParamPrivileges  
= PARAM_PRIV_READ
, 
  57                 { .ParamID          
= PARAM_SW_MAJOR
, 
  58                   .ParamPrivileges  
= PARAM_PRIV_READ
, 
  61                 { .ParamID          
= PARAM_SW_MINOR
, 
  62                   .ParamPrivileges  
= PARAM_PRIV_READ
, 
  65                 { .ParamID          
= PARAM_VTARGET
, 
  66                   .ParamPrivileges  
= PARAM_PRIV_READ
, 
  69                 { .ParamID          
= PARAM_SCK_DURATION
, 
  70                   .ParamPrivileges  
= PARAM_PRIV_READ 
| PARAM_PRIV_WRITE
, 
  71                   .ParamValue       
= (TOTAL_ISP_PROGRAMMING_SPEEDS 
- 1) }, 
  73                 { .ParamID          
= PARAM_RESET_POLARITY
, 
  74                   .ParamPrivileges  
= PARAM_PRIV_WRITE
, 
  77                 { .ParamID          
= PARAM_STATUS_TGT_CONN
, 
  78                   .ParamPrivileges  
= PARAM_PRIV_READ
, 
  81                 { .ParamID          
= PARAM_DISCHARGEDELAY
, 
  82                   .ParamPrivileges  
= PARAM_PRIV_WRITE
, 
  87 /** Loads saved non-volatile parameter values from the EEPROM into the parameter table, as needed. */ 
  88 void V2Params_LoadNonVolatileParamValues(void) 
  90         /* Target RESET line polarity is a non-volatile value, retrieve current parameter value from EEPROM - 
  91          *   NB: Cannot call V2Protocol_SetParameterValue() here, as that will cause another EEPROM write! */ 
  92         V2Params_GetParamFromTable(PARAM_RESET_POLARITY
)->ParamValue 
= eeprom_read_byte(&EEPROM_Rest_Polarity
); 
  95 /** Updates any parameter values that are sourced from hardware rather than explicitly set by the host, such as 
  96  *  VTARGET levels from the ADC on supported AVR models. 
  98 void V2Params_UpdateParamValues(void) 
 101         /* Update VTARGET parameter with the latest ADC conversion of VTARGET on supported AVR models */ 
 102         V2Params_GetParamFromTable(PARAM_VTARGET
)->ParamValue 
= ((5 * 10 * ADC_GetResult()) / 1024); 
 106 /** Retrieves the host PC read/write privileges for a given parameter in the parameter table. This should 
 107  *  be called before calls to \ref V2Params_GetParameterValue() or \ref V2Params_SetParameterValue() when 
 108  *  getting or setting parameter values in response to requests from the host. 
 110  *  \param[in] ParamID  Parameter ID whose privileges are to be retrieved from the table 
 112  *  \return Privileges for the requested parameter, as a mask of PARAM_PRIV_* masks 
 114 uint8_t V2Params_GetParameterPrivileges(const uint8_t ParamID
) 
 116         ParameterItem_t
* ParamInfo 
= V2Params_GetParamFromTable(ParamID
); 
 118         if (ParamInfo 
== NULL
) 
 121         return ParamInfo
->ParamPrivileges
; 
 124 /** Retrieves the current value for a given parameter in the parameter table. 
 126  *  \param[in] ParamID  Parameter ID whose value is to be retrieved from the table 
 128  *  \return Current value of the parameter in the table, or 0 if not found 
 130 uint8_t V2Params_GetParameterValue(const uint8_t ParamID
) 
 132         ParameterItem_t
* ParamInfo 
= V2Params_GetParamFromTable(ParamID
); 
 134         if (ParamInfo 
== NULL
) 
 137         return ParamInfo
->ParamValue
; 
 140 /** Sets the value for a given parameter in the parameter table. 
 142  *  \param[in] ParamID  Parameter ID whose value is to be set in the table 
 143  *  \param[in] Value    New value to set the parameter to 
 145  *  \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise 
 147 void V2Params_SetParameterValue(const uint8_t ParamID
, const uint8_t Value
) 
 149         ParameterItem_t
* ParamInfo 
= V2Params_GetParamFromTable(ParamID
); 
 151         if (ParamInfo 
== NULL
) 
 154         ParamInfo
->ParamValue 
= Value
; 
 156         /* The target RESET line polarity is a non-volatile parameter, save to EEPROM when changed */ 
 157         if (ParamID 
== PARAM_RESET_POLARITY
) 
 158           eeprom_write_byte(&EEPROM_Rest_Polarity
, Value
);   
 161 /** Retrieves a parameter entry (including ID, value and privileges) from the parameter table that matches the given 
 164  *  \param[in] ParamID  Parameter ID to find in the table 
 166  *  \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise 
 168 static ParameterItem_t
* V2Params_GetParamFromTable(const uint8_t ParamID
) 
 170         ParameterItem_t
* CurrTableItem 
= ParameterTable
; 
 172         /* Find the parameter in the parameter table if present */ 
 173         for (uint8_t TableIndex 
= 0; TableIndex 
< TABLE_PARAM_COUNT
; TableIndex
++) 
 175                 if (ParamID 
== CurrTableItem
->ParamID
) 
 176                   return CurrTableItem
;