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

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. static void setup(void)
  55. {
  56. chEvtSignal(pdb_led_thread, PDB_EVT_LED_SLOW_BLINK);
  57. /* TODO: implement the configuration mode */
  58. usbDisconnectBus(serusbcfg.usbp);
  59. sduObjectInit(&SDU1);
  60. sduStart(&SDU1, &serusbcfg);
  61. chThdSleepMilliseconds(100);
  62. usbStart(serusbcfg.usbp, &usbcfg);
  63. usbConnectBus(serusbcfg.usbp);
  64. //char text[] = "Hello, world!\r\n";
  65. while (true) {
  66. //sdWrite(&SDU1, (uint8_t *) text, 15);
  67. //obqWriteTimeout(&SDU1.obqueue, (uint8_t *) text, 15, TIME_INFINITE);
  68. //chprintf(&SDU1, "Hello, world! %d\r\n", ST2S(chVTGetSystemTime()));
  69. pdb_shell();
  70. chThdSleepMilliseconds(100);
  71. }
  72. }
  73. static void pd_buddy(void)
  74. {
  75. chEvtSignal(pdb_led_thread, PDB_EVT_LED_FAST_BLINK);
  76. /* Start I2C2 to make communication with the PHY possible */
  77. i2cStart(&I2CD2, &i2c2config);
  78. /* Initialize the FUSB302B */
  79. fusb_setup();
  80. /* Create the policy engine thread. */
  81. pdb_pe_run();
  82. /* Create the protocol layer threads. */
  83. pdb_prlrx_run();
  84. pdb_prltx_run();
  85. pdb_hardrst_run();
  86. /* Create the INT_N thread. */
  87. pdb_int_n_run();
  88. /* Wait, letting all the other threads do their work. */
  89. while (true) {
  90. chThdSleepMilliseconds(1000);
  91. }
  92. }
  93. /*
  94. * Application entry point.
  95. */
  96. int main(void) {
  97. /*
  98. * System initializations.
  99. * - HAL initialization, this also initializes the configured device drivers
  100. * and performs the board-specific initializations.
  101. * - Kernel initialization, the main() function becomes a thread and the
  102. * RTOS is active.
  103. */
  104. halInit();
  105. chSysInit();
  106. /* Set up the free messages mailbox */
  107. pdb_msg_pool_init();
  108. /* Create the LED thread. */
  109. pdb_led_run();
  110. /* Decide what mode to enter by the state of the button */
  111. if (palReadLine(LINE_BUTTON) == PAL_HIGH) {
  112. /* Button pressed -> setup mode */
  113. setup();
  114. } else {
  115. /* Button unpressed -> deliver power, buddy! */
  116. pd_buddy();
  117. }
  118. }