PD Buddy Sink Firmware
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

device_policy_manager.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include "device_policy_manager.h"
  19. #include <stdint.h>
  20. #include <hal.h>
  21. #include "led.h"
  22. #include "storage.h"
  23. #include "pd.h"
  24. /* Whether or not the power supply is unconstrained */
  25. static uint8_t dpm_unconstrained_power;
  26. bool pdb_dpm_evaluate_capability(const union pd_msg *capabilities, union pd_msg *request)
  27. {
  28. /* Get the current configuration */
  29. struct pdb_config *cfg = pdb_config_flash_read();
  30. /* Get the number of PDOs */
  31. uint8_t numobj = PD_NUMOBJ_GET(capabilities);
  32. /* Make the LED blink to indicate ongoing power negotiations */
  33. chEvtSignal(pdb_led_thread, PDB_EVT_LED_FAST_BLINK);
  34. /* Get whether or not the power supply is constrained */
  35. if (capabilities->obj[0] & PD_PDO_SRC_FIXED_UNCONSTRAINED) {
  36. dpm_unconstrained_power = 1;
  37. } else {
  38. dpm_unconstrained_power = 0;
  39. }
  40. /* Make sure we have configuration */
  41. if (cfg != NULL) {
  42. /* Look at the PDOs to see if one matches our desires */
  43. for (uint8_t i = 0; i < numobj; i++) {
  44. /* Fixed Supply PDOs come first, so when we see a PDO that isn't a
  45. * Fixed Supply, stop reading. */
  46. if ((capabilities->obj[i] & PD_PDO_TYPE) != PD_PDO_TYPE_FIXED) {
  47. break;
  48. }
  49. /* If the V from the PDO equals our desired V and the I is at least
  50. * our desired I */
  51. if (PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) == cfg->v
  52. && PD_PDO_SRC_FIXED_CURRENT_GET(capabilities, i) >= cfg->i) {
  53. /* We got what we wanted, so build a request for that */
  54. request->hdr = PD_MSGTYPE_REQUEST | PD_DATAROLE_UFP |
  55. PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
  56. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(cfg->i)
  57. | PD_RDO_FV_CURRENT_SET(cfg->i)
  58. | PD_RDO_NO_USB_SUSPEND | PD_RDO_OBJPOS_SET(i + 1);
  59. return true;
  60. }
  61. }
  62. }
  63. /* Nothing matched, so get 5 V */
  64. request->hdr = PD_MSGTYPE_REQUEST | PD_DATAROLE_UFP |
  65. PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
  66. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(10)
  67. | PD_RDO_FV_CURRENT_SET(10)
  68. | PD_RDO_NO_USB_SUSPEND | PD_RDO_CAP_MISMATCH
  69. | PD_RDO_OBJPOS_SET(1);
  70. return false;
  71. }
  72. void pdb_dpm_get_sink_capability(union pd_msg *cap)
  73. {
  74. /* Get the current configuration */
  75. struct pdb_config *cfg = pdb_config_flash_read();
  76. /* If we have no configuration, request 0.1 A at 5 V. */
  77. if (cfg == NULL) {
  78. /* Sink_Capabilities message */
  79. cap->hdr = PD_MSGTYPE_SINK_CAPABILITIES | PD_DATAROLE_UFP
  80. | PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
  81. /* vSafe5V at the desired current. */
  82. cap->obj[0] = PD_PDO_TYPE_FIXED
  83. | PD_PDO_SNK_FIXED_VOLTAGE_SET(100)
  84. | PD_PDO_SNK_FIXED_CURRENT_SET(10);
  85. /* If we want 5 V, we need to send only one PDO */
  86. } else if (cfg->v == 100) {
  87. /* Sink_Capabilities message */
  88. cap->hdr = PD_MSGTYPE_SINK_CAPABILITIES | PD_DATAROLE_UFP
  89. | PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
  90. /* vSafe5V at the desired current. */
  91. cap->obj[0] = PD_PDO_TYPE_FIXED
  92. | PD_PDO_SNK_FIXED_VOLTAGE_SET(cfg->v)
  93. | PD_PDO_SNK_FIXED_CURRENT_SET(cfg->i);
  94. /* Otherwise, send two PDOs, one for 5 V and one for the desired power. */
  95. } else {
  96. /* Sink_Capabilities message */
  97. cap->hdr = PD_MSGTYPE_SINK_CAPABILITIES | PD_DATAROLE_UFP
  98. | PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(2);
  99. /* First, vSafe5V. 100 mA, 5 V, and higher capability. */
  100. cap->obj[0] = PD_PDO_TYPE_FIXED
  101. | PD_PDO_SNK_FIXED_VOLTAGE_SET(100)
  102. | PD_PDO_SNK_FIXED_CURRENT_SET(10);
  103. /* Next, desired_v and desired_i */
  104. cap->obj[1] = PD_PDO_TYPE_FIXED
  105. | PD_PDO_SNK_FIXED_VOLTAGE_SET(cfg->v)
  106. | PD_PDO_SNK_FIXED_CURRENT_SET(cfg->i);
  107. }
  108. /* Set the unconstrained power flag. */
  109. if (dpm_unconstrained_power) {
  110. cap->obj[0] |= PD_PDO_SNK_FIXED_UNCONSTRAINED;
  111. }
  112. }
  113. void pdb_dpm_output_on(void)
  114. {
  115. chEvtSignal(pdb_led_thread, PDB_EVT_LED_MEDIUM_BLINK_OFF);
  116. palSetLine(LINE_OUT_CTRL);
  117. }
  118. void pdb_dpm_output_off(void)
  119. {
  120. chEvtSignal(pdb_led_thread, PDB_EVT_LED_ON);
  121. palClearLine(LINE_OUT_CTRL);
  122. }