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.

fusb302b.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "fusb302b.h"
  19. #include <ch.h>
  20. #include <hal.h>
  21. #include "pd.h"
  22. /*
  23. * Read a single byte from the FUSB302B
  24. */
  25. static uint8_t fusb_read_byte(uint8_t addr)
  26. {
  27. uint8_t buf;
  28. i2cMasterTransmit(&I2CD2, FUSB302B_ADDR, &addr, 1, &buf, 1);
  29. return buf;
  30. }
  31. /*
  32. * Read multiple bytes from the FUSB302B
  33. */
  34. static void fusb_read_buf(uint8_t addr, uint8_t size, uint8_t *buf)
  35. {
  36. i2cMasterTransmit(&I2CD2, FUSB302B_ADDR, &addr, 1, buf, size);
  37. }
  38. /*
  39. * Write a single byte to the FUSB302B
  40. */
  41. static void fusb_write_byte(uint8_t addr, uint8_t byte)
  42. {
  43. uint8_t buf[2] = {addr, byte};
  44. i2cMasterTransmit(&I2CD2, FUSB302B_ADDR, buf, 2, NULL, 0);
  45. }
  46. /*
  47. * Write multiple bytes to the FUSB302B
  48. */
  49. static void fusb_write_buf(uint8_t addr, uint8_t size, const uint8_t *buf)
  50. {
  51. uint8_t txbuf[size + 1];
  52. /* Prepare the transmit buffer */
  53. txbuf[0] = addr;
  54. for (int i = 0; i < size; i++) {
  55. txbuf[i + 1] = buf[i];
  56. }
  57. i2cMasterTransmit(&I2CD2, FUSB302B_ADDR, txbuf, size + 1, NULL, 0);
  58. }
  59. void fusb_send_message(const union pd_msg *msg)
  60. {
  61. /* Token sequences for the FUSB302B */
  62. static uint8_t sop_seq[5] = {0x12, 0x12, 0x12, 0x13, 0x80};
  63. static uint8_t eop_seq[4] = {0xFF, 0x14, 0xFE, 0xA1};
  64. /* Take the I2C2 mutex now so there can't be a race condition on sop_seq */
  65. i2cAcquireBus(&I2CD2);
  66. /* Get the length of the message: a two-octet header plus NUMOBJ four-octet
  67. * data objects */
  68. uint8_t msg_len = 2 + 4 * PD_NUMOBJ_GET(msg);
  69. /* Set the number of bytes to be transmitted in the packet */
  70. sop_seq[4] = 0x80 | msg_len;
  71. /* Write all three parts of the message to the TX FIFO */
  72. fusb_write_buf(FUSB_FIFOS, 5, sop_seq);
  73. fusb_write_buf(FUSB_FIFOS, msg_len, msg->bytes);
  74. fusb_write_buf(FUSB_FIFOS, 4, eop_seq);
  75. i2cReleaseBus(&I2CD2);
  76. }
  77. uint8_t fusb_read_message(union pd_msg *msg)
  78. {
  79. uint8_t garbage[4];
  80. uint8_t numobj;
  81. i2cAcquireBus(&I2CD2);
  82. /* If this isn't an SOP message, return error.
  83. * Because of our configuration, we should be able to assume this means the
  84. * buffer is empty, and not try to read past a non-SOP message. */
  85. if ((fusb_read_byte(FUSB_FIFOS) & FUSB_FIFO_RX_TOKEN_BITS)
  86. != FUSB_FIFO_RX_SOP) {
  87. i2cReleaseBus(&I2CD2);
  88. return 1;
  89. }
  90. /* Read the message header into msg */
  91. fusb_read_buf(FUSB_FIFOS, 2, msg->bytes);
  92. /* Get the number of data objects */
  93. numobj = PD_NUMOBJ_GET(msg);
  94. /* If there is at least one data object, read the data objects */
  95. if (numobj > 0) {
  96. fusb_read_buf(FUSB_FIFOS, numobj * 4, msg->bytes + 2);
  97. }
  98. /* Throw the CRC32 in the garbage, since the PHY already checked it. */
  99. fusb_read_buf(FUSB_FIFOS, 4, garbage);
  100. i2cReleaseBus(&I2CD2);
  101. return 0;
  102. }
  103. void fusb_send_hardrst(void)
  104. {
  105. i2cAcquireBus(&I2CD2);
  106. /* Send a hard reset */
  107. fusb_write_byte(FUSB_CONTROL3, 0x07 | FUSB_CONTROL3_SEND_HARD_RESET);
  108. i2cReleaseBus(&I2CD2);
  109. }
  110. void fusb_setup(void)
  111. {
  112. i2cAcquireBus(&I2CD2);
  113. /* Fully reset the FUSB302B */
  114. fusb_write_byte(FUSB_RESET, FUSB_RESET_SW_RES);
  115. /* Turn on all power */
  116. fusb_write_byte(FUSB_POWER, 0x0F);
  117. /* Set interrupt masks */
  118. fusb_write_byte(FUSB_MASK1, 0x00);
  119. fusb_write_byte(FUSB_MASKA, 0x00);
  120. fusb_write_byte(FUSB_MASKB, 0x00);
  121. fusb_write_byte(FUSB_CONTROL0, 0x04);
  122. /* Enable automatic retransmission */
  123. fusb_write_byte(FUSB_CONTROL3, 0x07);
  124. /* Flush the RX buffer */
  125. fusb_write_byte(FUSB_CONTROL1, FUSB_CONTROL1_RX_FLUSH);
  126. /* Measure CC1 */
  127. fusb_write_byte(FUSB_SWITCHES0, 0x07);
  128. chThdSleepMicroseconds(250);
  129. uint8_t cc1;
  130. cc1 = fusb_read_byte(FUSB_STATUS0) & FUSB_STATUS0_BC_LVL;
  131. /* Measure CC2 */
  132. fusb_write_byte(FUSB_SWITCHES0, 0x0B);
  133. chThdSleepMicroseconds(250);
  134. uint8_t cc2;
  135. cc2 = fusb_read_byte(FUSB_STATUS0) & FUSB_STATUS0_BC_LVL;
  136. /* Select the correct CC line for BMC signaling; also enable AUTO_CRC */
  137. if (cc1 > cc2) {
  138. fusb_write_byte(FUSB_SWITCHES1, 0x25);
  139. fusb_write_byte(FUSB_SWITCHES0, 0x07);
  140. } else {
  141. fusb_write_byte(FUSB_SWITCHES1, 0x26);
  142. fusb_write_byte(FUSB_SWITCHES0, 0x0B);
  143. }
  144. /* Reset the PD logic */
  145. fusb_write_byte(FUSB_RESET, FUSB_RESET_PD_RESET);
  146. i2cReleaseBus(&I2CD2);
  147. }
  148. void fusb_get_status(union fusb_status *status)
  149. {
  150. i2cAcquireBus(&I2CD2);
  151. /* Read the interrupt and status flags into status */
  152. fusb_read_buf(FUSB_STATUS0A, 7, status->bytes);
  153. i2cReleaseBus(&I2CD2);
  154. }
  155. void fusb_reset(void)
  156. {
  157. i2cAcquireBus(&I2CD2);
  158. /* Flush the TX buffer */
  159. fusb_write_byte(FUSB_CONTROL0, 0x44);
  160. /* Flush the RX buffer */
  161. fusb_write_byte(FUSB_CONTROL1, FUSB_CONTROL1_RX_FLUSH);
  162. /* Reset the PD logic */
  163. fusb_write_byte(FUSB_RESET, FUSB_RESET_PD_RESET);
  164. i2cReleaseBus(&I2CD2);
  165. }