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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /*
  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 "chprintf.h"
  33. #include "shell.h"
  34. #include "usbcfg.h"
  35. #include "priorities.h"
  36. #include "led.h"
  37. #include "device_policy_manager.h"
  38. #include "policy_engine.h"
  39. #include "protocol_tx.h"
  40. #include "protocol_rx.h"
  41. #include "hard_reset.h"
  42. #include "int_n.h"
  43. #include "fusb302b.h"
  44. #include "messages.h"
  45. /*
  46. * I2C configuration object.
  47. * I2C2_TIMINGR: 1000 kHz with I2CCLK = 48 MHz, rise time = 100 ns,
  48. * fall time = 10 ns (0x00700818)
  49. */
  50. static const I2CConfig i2c2config = {
  51. 0x00700818,
  52. 0,
  53. 0
  54. };
  55. /*
  56. * Start the USB Power Delivery threads
  57. */
  58. static void start_pd(void)
  59. {
  60. /* Start I2C2 to make communication with the PHY possible */
  61. i2cStart(&I2CD2, &i2c2config);
  62. /* Initialize the FUSB302B */
  63. fusb_setup();
  64. /* Create the policy engine thread. */
  65. pdb_pe_run();
  66. /* Create the protocol layer threads. */
  67. pdb_prlrx_run();
  68. pdb_prltx_run();
  69. pdb_hardrst_run();
  70. /* Create the INT_N thread. */
  71. pdb_int_n_run();
  72. }
  73. /*
  74. * Enter setup mode
  75. */
  76. static void setup(void)
  77. {
  78. /* Configure the DPM to play nice with the shell */
  79. pdb_dpm_output_enabled = false;
  80. pdb_dpm_led_pd_status = false;
  81. pdb_dpm_usb_comms = true;
  82. /* Start the USB Power Delivery threads */
  83. start_pd();
  84. /* Indicate that we're in setup mode */
  85. chEvtSignal(pdb_led_thread, PDB_EVT_LED_CONFIG);
  86. /* Disconnect from USB */
  87. usbDisconnectBus(serusbcfg.usbp);
  88. /* Start the USB serial interface */
  89. sduObjectInit(&SDU1);
  90. sduStart(&SDU1, &serusbcfg);
  91. /* Start USB */
  92. chThdSleepMilliseconds(100);
  93. usbStart(serusbcfg.usbp, &usbcfg);
  94. usbConnectBus(serusbcfg.usbp);
  95. /* Start the shell */
  96. pdb_shell();
  97. }
  98. /*
  99. * Negotiate with the power supply for the configured power
  100. */
  101. static void sink(void)
  102. {
  103. /* Start the USB Power Delivery threads */
  104. start_pd();
  105. /* Wait, letting all the other threads do their work. */
  106. while (true) {
  107. chThdSleepMilliseconds(1000);
  108. }
  109. }
  110. /*
  111. * Application entry point.
  112. */
  113. int main(void) {
  114. /*
  115. * System initializations.
  116. * - HAL initialization, this also initializes the configured device drivers
  117. * and performs the board-specific initializations.
  118. * - Kernel initialization, the main() function becomes a thread and the
  119. * RTOS is active.
  120. */
  121. halInit();
  122. chSysInit();
  123. /* Set up the free messages mailbox */
  124. pdb_msg_pool_init();
  125. /* Create the LED thread. */
  126. pdb_led_run();
  127. /* Decide what mode to enter by the state of the button */
  128. if (palReadLine(LINE_BUTTON) == PAL_HIGH) {
  129. /* Button pressed -> setup mode */
  130. setup();
  131. } else {
  132. /* Button unpressed -> deliver power, buddy! */
  133. sink();
  134. }
  135. }