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.

storage.h 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * PD Buddy - USB Power Delivery for everyone
  3. * Copyright (C) 2017 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 PDB_STORAGE_H
  19. #define PDB_STORAGE_H
  20. #include <stdint.h>
  21. #include <ch.h>
  22. /*
  23. * PD Buddy Sink configuration structure
  24. */
  25. struct pdb_config {
  26. uint16_t status;
  27. uint16_t flags;
  28. uint16_t v;
  29. uint16_t i;
  30. uint16_t v_min;
  31. uint16_t v_max;
  32. uint16_t _reserved[2];
  33. } __attribute__((packed));
  34. /* Status for configuration structures. The empty status indicates that the
  35. * struct is ready to be written, including an update to the valid status.
  36. * Once the struct is no longer needed, the status is updated to invalid.
  37. * Erasing the flash page resets all structures to the empty status. */
  38. #define PDB_CONFIG_STATUS_INVALID 0x0000
  39. #define PDB_CONFIG_STATUS_VALID 0xBEEF
  40. #define PDB_CONFIG_STATUS_EMPTY 0xFFFF
  41. /* Flags for configuration structures. */
  42. /* GiveBack supported */
  43. #define PDB_CONFIG_FLAGS_GIVEBACK 0x0001
  44. /* Variable and battery PDOs supported (v_min and v_max valid) */
  45. #define PDB_CONFIG_FLAGS_VAR_BAT 0x0002
  46. /* Flash configuration array */
  47. extern struct pdb_config *pdb_config_array;
  48. /* The number of elements in the pdb_config_array */
  49. #define PDB_CONFIG_ARRAY_LEN 128
  50. /*
  51. * Print a struct pdb_config to the given BaseSequentialStream
  52. */
  53. void pdb_config_print(BaseSequentialStream *chp, const struct pdb_config *cfg);
  54. /*
  55. * Erase the flash page used for configuration
  56. */
  57. void pdb_config_flash_erase(void);
  58. /*
  59. * Write a configuration structure to flash, invalidating the previous
  60. * configuration. If necessary, the flash page is erased before writing the
  61. * new structure.
  62. */
  63. void pdb_config_flash_update(const struct pdb_config *cfg);
  64. /*
  65. * Get the first valid configuration strucure. If the flash page is empty,
  66. * return NULL instead.
  67. */
  68. struct pdb_config *pdb_config_flash_read(void);
  69. #endif /* PDB_STORAGE_H */