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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #include <hal.h>
  23. /*
  24. * PD Buddy Sink configuration structure
  25. */
  26. struct pdbs_config {
  27. /* Status halfword, used to indicate which config objects can be written to
  28. * and which one is valid. */
  29. uint16_t status;
  30. /* Flags halfword for miscellaneous small fields. */
  31. uint16_t flags;
  32. /* Preferred voltage, in millivolts. */
  33. uint16_t v;
  34. /* Union for specifying how much current to request. */
  35. union {
  36. /* Required current, in centiamperes. */
  37. uint16_t i;
  38. /* Required power, in centiwatts. */
  39. uint16_t p;
  40. /* Value of resistive load, in centiohms. */
  41. uint16_t r;
  42. };
  43. /* Lower end of voltage range, in millivolts. */
  44. uint16_t vmin;
  45. /* Upper end of voltage range, in millivolts. */
  46. uint16_t vmax;
  47. /* Extra bytes reserved for future use. */
  48. uint16_t _reserved[2];
  49. } __attribute__((packed));
  50. /* Status for configuration structures. EMPTY indicates that the struct is
  51. * ready to be written, including a status update to VALID. Once the struct is
  52. * no longer needed, the status is updated to INVALID. Erasing the flash page
  53. * resets all structures to EMPTY. */
  54. #define PDBS_CONFIG_STATUS_INVALID 0x0000
  55. #define PDBS_CONFIG_STATUS_VALID 0xBEEF
  56. #define PDBS_CONFIG_STATUS_EMPTY 0xFFFF
  57. /* Flags for configuration structures. */
  58. /* GiveBack supported */
  59. #define PDBS_CONFIG_FLAGS_GIVEBACK (1 << 0)
  60. /* Variable and battery PDOs preferred (FIXME: not implemented) */
  61. #define PDBS_CONFIG_FLAGS_VAR_BAT (1 << 1)
  62. /* High voltages preferred */
  63. #define PDBS_CONFIG_FLAGS_HV_PREFERRED (1 << 2)
  64. /* Current definition type */
  65. #define PDBS_CONFIG_FLAGS_CURRENT_DEFN_SHIFT 3
  66. #define PDBS_CONFIG_FLAGS_CURRENT_DEFN (0x3 << PDBS_CONFIG_FLAGS_CURRENT_DEFN_SHIFT)
  67. #define PDBS_CONFIG_FLAGS_CURRENT_DEFN_I (0 << PDBS_CONFIG_FLAGS_CURRENT_DEFN_SHIFT)
  68. #define PDBS_CONFIG_FLAGS_CURRENT_DEFN_P (1 << PDBS_CONFIG_FLAGS_CURRENT_DEFN_SHIFT)
  69. #define PDBS_CONFIG_FLAGS_CURRENT_DEFN_R (2 << PDBS_CONFIG_FLAGS_CURRENT_DEFN_SHIFT)
  70. /* Flash configuration array */
  71. extern struct pdbs_config *pdbs_config_array;
  72. /* The number of elements in the pdbs_config_array */
  73. #define PDBS_CONFIG_ARRAY_LEN 128
  74. /*
  75. * Print a struct pdbs_config to the given BaseSequentialStream
  76. */
  77. void pdbs_config_print(BaseSequentialStream *chp, const struct pdbs_config *cfg);
  78. /*
  79. * Erase the flash page used for configuration
  80. */
  81. void pdbs_config_flash_erase(void);
  82. /*
  83. * Write a configuration structure to flash, invalidating the previous
  84. * configuration. If necessary, the flash page is erased before writing the
  85. * new structure.
  86. */
  87. void pdbs_config_flash_update(const struct pdbs_config *cfg);
  88. /*
  89. * Get the first valid configuration strucure. If the flash page is empty,
  90. * return NULL instead.
  91. *
  92. * The location of the configuration is cached, and the cache is updated when
  93. * pdbs_config_flash_erase and pdbs_config_flash_update are called. The full
  94. * lookup is only performed the first time this function is called, so there's
  95. * very little penalty to calling it repeatedly.
  96. */
  97. struct pdbs_config *pdbs_config_flash_read(void);
  98. #endif /* PDBS_CONFIG_H */