Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

V2ProtocolParams.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * V2Protocol parameter handler, to process V2 Protocol device parameters.
  29. */
  30. #define INCLUDE_FROM_V2PROTOCOL_PARAMS_C
  31. #include "V2ProtocolParams.h"
  32. /* Non-Volatile Parameter Values for EEPROM storage */
  33. static uint8_t EEMEM EEPROM_Reset_Polarity = 0x01;
  34. /* Non-Volatile Parameter Values for EEPROM storage */
  35. static uint8_t EEMEM EEPROM_SCK_Duration = 0x06;
  36. /* Volatile Parameter Values for RAM storage */
  37. static ParameterItem_t ParameterTable[] =
  38. {
  39. { .ParamID = PARAM_BUILD_NUMBER_LOW,
  40. .ParamPrivileges = PARAM_PRIV_READ,
  41. .ParamValue = 0 },
  42. { .ParamID = PARAM_BUILD_NUMBER_HIGH,
  43. .ParamPrivileges = PARAM_PRIV_READ,
  44. .ParamValue = 0 },
  45. { .ParamID = PARAM_HW_VER,
  46. .ParamPrivileges = PARAM_PRIV_READ,
  47. .ParamValue = 0x00 },
  48. { .ParamID = PARAM_SW_MAJOR,
  49. .ParamPrivileges = PARAM_PRIV_READ,
  50. .ParamValue = 0x01 },
  51. { .ParamID = PARAM_SW_MINOR,
  52. .ParamPrivileges = PARAM_PRIV_READ,
  53. .ParamValue = FIRMWARE_VERSION_MINOR },
  54. { .ParamID = PARAM_VTARGET,
  55. .ParamPrivileges = PARAM_PRIV_READ,
  56. .ParamValue = (uint8_t)(3.3 * 10) },
  57. { .ParamID = PARAM_SCK_DURATION,
  58. .ParamPrivileges = PARAM_PRIV_READ | PARAM_PRIV_WRITE,
  59. .ParamValue = 6 },
  60. { .ParamID = PARAM_RESET_POLARITY,
  61. .ParamPrivileges = PARAM_PRIV_READ | PARAM_PRIV_WRITE,
  62. .ParamValue = 0x01 },
  63. { .ParamID = PARAM_STATUS_TGT_CONN,
  64. .ParamPrivileges = PARAM_PRIV_READ,
  65. .ParamValue = STATUS_ISP_READY },
  66. { .ParamID = PARAM_DISCHARGEDELAY,
  67. .ParamPrivileges = PARAM_PRIV_READ | PARAM_PRIV_WRITE,
  68. .ParamValue = 0x00 },
  69. };
  70. /** Loads saved non-volatile parameter values from the EEPROM into the parameter table, as needed. */
  71. void V2Params_LoadNonVolatileParamValues(void)
  72. {
  73. /* Read parameter values that are stored in non-volatile EEPROM */
  74. uint8_t ResetPolarity = eeprom_read_byte(&EEPROM_Reset_Polarity);
  75. uint8_t SCKDuration = eeprom_read_byte(&EEPROM_SCK_Duration);
  76. /* Update current parameter table if the EEPROM contents was not blank */
  77. if (ResetPolarity != 0xFF)
  78. V2Params_GetParamFromTable(PARAM_RESET_POLARITY)->ParamValue = ResetPolarity;
  79. /* Update current parameter table if the EEPROM contents was not blank */
  80. if (SCKDuration != 0xFF)
  81. V2Params_GetParamFromTable(PARAM_SCK_DURATION)->ParamValue = SCKDuration;
  82. }
  83. /** Updates any parameter values that are sourced from hardware rather than explicitly set by the host, such as
  84. * VTARGET levels from the ADC on supported AVR models.
  85. */
  86. void V2Params_UpdateParamValues(void)
  87. {
  88. #if (defined(ADC) && !defined(NO_VTARGET_DETECT))
  89. /* Update VTARGET parameter with the latest ADC conversion of VTARGET on supported AVR models */
  90. V2Params_GetParamFromTable(PARAM_VTARGET)->ParamValue = (((uint16_t)(VTARGET_REF_VOLTS * 10 * VTARGET_SCALE_FACTOR) * ADC_GetResult()) / 1024);
  91. #endif
  92. }
  93. /** Retrieves the host PC read/write privileges for a given parameter in the parameter table. This should
  94. * be called before calls to \ref V2Params_GetParameterValue() or \ref V2Params_SetParameterValue() when
  95. * getting or setting parameter values in response to requests from the host.
  96. *
  97. * \param[in] ParamID Parameter ID whose privileges are to be retrieved from the table
  98. *
  99. * \return Privileges for the requested parameter, as a mask of \c PARAM_PRIV_* masks
  100. */
  101. uint8_t V2Params_GetParameterPrivileges(const uint8_t ParamID)
  102. {
  103. ParameterItem_t* const ParamInfo = V2Params_GetParamFromTable(ParamID);
  104. if (ParamInfo == NULL)
  105. return 0;
  106. return ParamInfo->ParamPrivileges;
  107. }
  108. /** Retrieves the current value for a given parameter in the parameter table.
  109. *
  110. * \note This function does not first check for read privileges - if the value is being sent to the host via a
  111. * GET PARAM command, \ref V2Params_GetParameterPrivileges() should be called first to ensure that the
  112. * parameter is host-readable.
  113. *
  114. * \param[in] ParamID Parameter ID whose value is to be retrieved from the table
  115. *
  116. * \return Current value of the parameter in the table, or 0 if not found
  117. */
  118. uint8_t V2Params_GetParameterValue(const uint8_t ParamID)
  119. {
  120. ParameterItem_t* const ParamInfo = V2Params_GetParamFromTable(ParamID);
  121. if (ParamInfo == NULL)
  122. return 0;
  123. return ParamInfo->ParamValue;
  124. }
  125. /** Sets the value for a given parameter in the parameter table.
  126. *
  127. * \note This function does not first check for write privileges - if the value is being sourced from the host
  128. * via a SET PARAM command, \ref V2Params_GetParameterPrivileges() should be called first to ensure that the
  129. * parameter is host-writable.
  130. *
  131. * \param[in] ParamID Parameter ID whose value is to be set in the table
  132. * \param[in] Value New value to set the parameter to
  133. *
  134. * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
  135. */
  136. void V2Params_SetParameterValue(const uint8_t ParamID,
  137. const uint8_t Value)
  138. {
  139. ParameterItem_t* const ParamInfo = V2Params_GetParamFromTable(ParamID);
  140. if (ParamInfo == NULL)
  141. return;
  142. ParamInfo->ParamValue = Value;
  143. /* The target RESET line polarity is a non-volatile parameter, save to EEPROM when changed */
  144. if (ParamID == PARAM_RESET_POLARITY)
  145. eeprom_update_byte(&EEPROM_Reset_Polarity, Value);
  146. /* The target SCK line period is a non-volatile parameter, save to EEPROM when changed */
  147. if (ParamID == PARAM_SCK_DURATION)
  148. eeprom_update_byte(&EEPROM_SCK_Duration, Value);
  149. }
  150. /** Retrieves a parameter entry (including ID, value and privileges) from the parameter table that matches the given
  151. * parameter ID.
  152. *
  153. * \param[in] ParamID Parameter ID to find in the table
  154. *
  155. * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise
  156. */
  157. static ParameterItem_t* const V2Params_GetParamFromTable(const uint8_t ParamID)
  158. {
  159. ParameterItem_t* CurrTableItem = ParameterTable;
  160. /* Find the parameter in the parameter table if present */
  161. for (uint8_t TableIndex = 0; TableIndex < TABLE_PARAM_COUNT; TableIndex++)
  162. {
  163. if (ParamID == CurrTableItem->ParamID)
  164. return CurrTableItem;
  165. CurrTableItem++;
  166. }
  167. return NULL;
  168. }