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 3.7KB

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