PD Buddy Sink Firmware
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.

config.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * PD Buddy Sink Firmware - Smart power jack for USB Power Delivery
  3. * Copyright (C) 2017-2018 Clayton G. Hobbs <clay@lakeserv.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef PDBS_CONFIG_H
  19. #define PDBS_CONFIG_H
  20. #include <stdint.h>
  21. #include <ch.h>
  22. /*
  23. * PD Buddy Sink configuration structure
  24. */
  25. struct pdbs_config {
  26. /* Status halfword, used to indicate which config objects can be written to
  27. * and which one is valid. */
  28. uint16_t status;
  29. /* Flags halfword for miscellaneous small fields. */
  30. uint16_t flags;
  31. /* Preferred voltage, in millivolts. */
  32. uint16_t v;
  33. /* Union for specifying how much current to request. */
  34. union {
  35. /* Required current, in centiamperes. */
  36. uint16_t i;
  37. /* Required power, in centiwatts. */
  38. uint16_t p;
  39. /* Value of resistive load, in centiohms. */
  40. uint16_t r;
  41. };
  42. /* Lower end of voltage range, in millivolts. */
  43. uint16_t vmin;
  44. /* Upper end of voltage range, in millivolts. */
  45. uint16_t vmax;
  46. /* Extra bytes reserved for future use. */
  47. uint16_t _reserved[2];
  48. } __attribute__((packed));
  49. /* Status for configuration structures. EMPTY indicates that the struct is
  50. * ready to be written, including a status update to VALID. Once the struct is
  51. * no longer needed, the status is updated to INVALID. Erasing the flash page
  52. * resets all structures to EMPTY. */
  53. #define PDBS_CONFIG_STATUS_INVALID 0x0000
  54. #define PDBS_CONFIG_STATUS_VALID 0xBEEF
  55. #define PDBS_CONFIG_STATUS_EMPTY 0xFFFF
  56. /* Flags for configuration structures. */
  57. /* GiveBack supported */
  58. #define PDBS_CONFIG_FLAGS_GIVEBACK (1 << 0)
  59. /* Variable and battery PDOs preferred (FIXME: not implemented) */
  60. #define PDBS_CONFIG_FLAGS_VAR_BAT (1 << 1)
  61. /* High voltages preferred */
  62. #define PDBS_CONFIG_FLAGS_HV_PREFERRED (1 << 2)
  63. /* Current definition type */
  64. #define PDBS_CONFIG_FLAGS_CURRENT_DEFN_SHIFT 3
  65. #define PDBS_CONFIG_FLAGS_CURRENT_DEFN (0x3 << PDBS_CONFIG_FLAGS_CURRENT_DEFN_SHIFT)
  66. #define PDBS_CONFIG_FLAGS_CURRENT_DEFN_I (0 << PDBS_CONFIG_FLAGS_CURRENT_DEFN_SHIFT)
  67. #define PDBS_CONFIG_FLAGS_CURRENT_DEFN_P (1 << PDBS_CONFIG_FLAGS_CURRENT_DEFN_SHIFT)
  68. #define PDBS_CONFIG_FLAGS_CURRENT_DEFN_R (2 << PDBS_CONFIG_FLAGS_CURRENT_DEFN_SHIFT)
  69. /* Flash configuration array */
  70. extern struct pdbs_config *pdbs_config_array;
  71. /* The number of elements in the pdbs_config_array */
  72. #define PDBS_CONFIG_ARRAY_LEN 128
  73. /*
  74. * Print a struct pdbs_config to the given BaseSequentialStream
  75. */
  76. void pdbs_config_print(BaseSequentialStream *chp, const struct pdbs_config *cfg);
  77. /*
  78. * Erase the flash page used for configuration
  79. */
  80. void pdbs_config_flash_erase(void);
  81. /*
  82. * Write a configuration structure to flash, invalidating the previous
  83. * configuration. If necessary, the flash page is erased before writing the
  84. * new structure.
  85. */
  86. void pdbs_config_flash_update(const struct pdbs_config *cfg);
  87. /*
  88. * Get the first valid configuration strucure. If the flash page is empty,
  89. * return NULL instead.
  90. *
  91. * The location of the configuration is cached, and the cache is updated when
  92. * pdbs_config_flash_erase and pdbs_config_flash_update are called. The full
  93. * lookup is only performed the first time this function is called, so there's
  94. * very little penalty to calling it repeatedly.
  95. */
  96. struct pdbs_config *pdbs_config_flash_read(void);
  97. #endif /* PDBS_CONFIG_H */