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.

main.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * PD Buddy - USB Power Delivery for everyone
  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. /*
  19. ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
  20. Licensed under the Apache License, Version 2.0 (the "License");
  21. you may not use this file except in compliance with the License.
  22. You may obtain a copy of the License at
  23. http://www.apache.org/licenses/LICENSE-2.0
  24. Unless required by applicable law or agreed to in writing, software
  25. distributed under the License is distributed on an "AS IS" BASIS,
  26. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. See the License for the specific language governing permissions and
  28. limitations under the License.
  29. */
  30. #include <ch.h>
  31. #include <hal.h>
  32. #include "shell.h"
  33. #include "usbcfg.h"
  34. #include <pdb.h>
  35. #include <pd.h>
  36. #include "led.h"
  37. #include "device_policy_manager.h"
  38. /*
  39. * I2C configuration object.
  40. * I2C2_TIMINGR: 1000 kHz with I2CCLK = 48 MHz, rise time = 100 ns,
  41. * fall time = 10 ns (0x00700818)
  42. */
  43. static const I2CConfig i2c2config = {
  44. 0x00700818,
  45. 0,
  46. 0
  47. };
  48. /*
  49. * PD Buddy Sink DPM data
  50. */
  51. static struct pdbs_dpm_data dpm_data = {
  52. NULL,
  53. fusb_tcc_none,
  54. true,
  55. true,
  56. false,
  57. ._present_voltage = 5000
  58. };
  59. /*
  60. * PD Buddy firmware library configuration object
  61. */
  62. static struct pdb_config pdb_config = {
  63. .fusb = {
  64. &I2CD2,
  65. FUSB302B_ADDR
  66. },
  67. .dpm = {
  68. pdbs_dpm_evaluate_capability,
  69. pdbs_dpm_get_sink_capability,
  70. pdbs_dpm_giveback_enabled,
  71. pdbs_dpm_evaluate_typec_current,
  72. pdbs_dpm_pd_start,
  73. pdbs_dpm_transition_default,
  74. pdbs_dpm_transition_min,
  75. pdbs_dpm_transition_standby,
  76. pdbs_dpm_transition_requested,
  77. pdbs_dpm_transition_requested, /* XXX type-c current */
  78. NULL
  79. },
  80. .dpm_data = &dpm_data
  81. };
  82. /*
  83. * Enter setup mode
  84. */
  85. static void setup(void)
  86. {
  87. /* Configure the DPM to play nice with the shell */
  88. dpm_data.output_enabled = false;
  89. dpm_data.led_pd_status = false;
  90. dpm_data.usb_comms = true;
  91. /* Start the USB Power Delivery threads */
  92. pdb_init(&pdb_config);
  93. /* Indicate that we're in setup mode */
  94. chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_CONFIG);
  95. /* Disconnect from USB */
  96. usbDisconnectBus(serusbcfg.usbp);
  97. /* Start the USB serial interface */
  98. sduObjectInit(&SDU1);
  99. sduStart(&SDU1, &serusbcfg);
  100. /* Start USB */
  101. chThdSleepMilliseconds(100);
  102. usbStart(serusbcfg.usbp, &usbcfg);
  103. usbConnectBus(serusbcfg.usbp);
  104. /* Start the shell */
  105. pdbs_shell(&pdb_config);
  106. }
  107. /*
  108. * Negotiate with the power supply for the configured power
  109. */
  110. static void sink(void)
  111. {
  112. /* Start the USB Power Delivery threads */
  113. pdb_init(&pdb_config);
  114. /* Wait, letting all the other threads do their work. */
  115. while (true) {
  116. chThdSleepMilliseconds(1000);
  117. }
  118. }
  119. /*
  120. * Application entry point.
  121. */
  122. int main(void) {
  123. /*
  124. * System initializations.
  125. * - HAL initialization, this also initializes the configured device drivers
  126. * and performs the board-specific initializations.
  127. * - Kernel initialization, the main() function becomes a thread and the
  128. * RTOS is active.
  129. */
  130. halInit();
  131. chSysInit();
  132. /* Create the LED thread. */
  133. pdbs_led_run();
  134. /* Start I2C2 to make communication with the PHY possible */
  135. i2cStart(pdb_config.fusb.i2cp, &i2c2config);
  136. /* Decide what mode to enter by the state of the button */
  137. if (palReadLine(LINE_BUTTON) == PAL_HIGH) {
  138. /* Button pressed -> setup mode */
  139. setup();
  140. } else {
  141. /* Button unpressed -> deliver power, buddy! */
  142. sink();
  143. }
  144. }