PD Buddy Sink Firmware
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

fusb302b.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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] = {
  63. FUSB_FIFO_TX_SOP1,
  64. FUSB_FIFO_TX_SOP1,
  65. FUSB_FIFO_TX_SOP1,
  66. FUSB_FIFO_TX_SOP2,
  67. FUSB_FIFO_TX_PACKSYM
  68. };
  69. static uint8_t eop_seq[4] = {
  70. FUSB_FIFO_TX_JAM_CRC,
  71. FUSB_FIFO_TX_EOP,
  72. FUSB_FIFO_TX_TXOFF,
  73. FUSB_FIFO_TX_TXON
  74. };
  75. /* Take the I2C2 mutex now so there can't be a race condition on sop_seq */
  76. i2cAcquireBus(&I2CD2);
  77. /* Get the length of the message: a two-octet header plus NUMOBJ four-octet
  78. * data objects */
  79. uint8_t msg_len = 2 + 4 * PD_NUMOBJ_GET(msg);
  80. /* Set the number of bytes to be transmitted in the packet */
  81. sop_seq[4] = FUSB_FIFO_TX_PACKSYM | msg_len;
  82. /* Write all three parts of the message to the TX FIFO */
  83. fusb_write_buf(FUSB_FIFOS, 5, sop_seq);
  84. fusb_write_buf(FUSB_FIFOS, msg_len, msg->bytes);
  85. fusb_write_buf(FUSB_FIFOS, 4, eop_seq);
  86. i2cReleaseBus(&I2CD2);
  87. }
  88. uint8_t fusb_read_message(union pd_msg *msg)
  89. {
  90. uint8_t garbage[4];
  91. uint8_t numobj;
  92. i2cAcquireBus(&I2CD2);
  93. /* If this isn't an SOP message, return error.
  94. * Because of our configuration, we should be able to assume this means the
  95. * buffer is empty, and not try to read past a non-SOP message. */
  96. if ((fusb_read_byte(FUSB_FIFOS) & FUSB_FIFO_RX_TOKEN_BITS)
  97. != FUSB_FIFO_RX_SOP) {
  98. i2cReleaseBus(&I2CD2);
  99. return 1;
  100. }
  101. /* Read the message header into msg */
  102. fusb_read_buf(FUSB_FIFOS, 2, msg->bytes);
  103. /* Get the number of data objects */
  104. numobj = PD_NUMOBJ_GET(msg);
  105. /* If there is at least one data object, read the data objects */
  106. if (numobj > 0) {
  107. fusb_read_buf(FUSB_FIFOS, numobj * 4, msg->bytes + 2);
  108. }
  109. /* Throw the CRC32 in the garbage, since the PHY already checked it. */
  110. fusb_read_buf(FUSB_FIFOS, 4, garbage);
  111. i2cReleaseBus(&I2CD2);
  112. return 0;
  113. }
  114. void fusb_send_hardrst(void)
  115. {
  116. i2cAcquireBus(&I2CD2);
  117. /* Send a hard reset */
  118. fusb_write_byte(FUSB_CONTROL3, 0x07 | FUSB_CONTROL3_SEND_HARD_RESET);
  119. i2cReleaseBus(&I2CD2);
  120. }
  121. void fusb_setup(void)
  122. {
  123. i2cAcquireBus(&I2CD2);
  124. /* Fully reset the FUSB302B */
  125. fusb_write_byte(FUSB_RESET, FUSB_RESET_SW_RES);
  126. /* Turn on all power */
  127. fusb_write_byte(FUSB_POWER, 0x0F);
  128. /* Set interrupt masks */
  129. fusb_write_byte(FUSB_MASK1, 0x00);
  130. fusb_write_byte(FUSB_MASKA, 0x00);
  131. fusb_write_byte(FUSB_MASKB, 0x00);
  132. fusb_write_byte(FUSB_CONTROL0, 0x04);
  133. /* Enable automatic retransmission */
  134. fusb_write_byte(FUSB_CONTROL3, 0x07);
  135. /* Flush the RX buffer */
  136. fusb_write_byte(FUSB_CONTROL1, FUSB_CONTROL1_RX_FLUSH);
  137. /* Measure CC1 */
  138. fusb_write_byte(FUSB_SWITCHES0, 0x07);
  139. chThdSleepMicroseconds(250);
  140. uint8_t cc1 = fusb_read_byte(FUSB_STATUS0) & FUSB_STATUS0_BC_LVL;
  141. /* Measure CC2 */
  142. fusb_write_byte(FUSB_SWITCHES0, 0x0B);
  143. chThdSleepMicroseconds(250);
  144. uint8_t cc2 = fusb_read_byte(FUSB_STATUS0) & FUSB_STATUS0_BC_LVL;
  145. /* Select the correct CC line for BMC signaling; also enable AUTO_CRC */
  146. if (cc1 > cc2) {
  147. fusb_write_byte(FUSB_SWITCHES1, 0x25);
  148. fusb_write_byte(FUSB_SWITCHES0, 0x07);
  149. } else {
  150. fusb_write_byte(FUSB_SWITCHES1, 0x26);
  151. fusb_write_byte(FUSB_SWITCHES0, 0x0B);
  152. }
  153. /* Reset the PD logic */
  154. fusb_write_byte(FUSB_RESET, FUSB_RESET_PD_RESET);
  155. i2cReleaseBus(&I2CD2);
  156. }
  157. void fusb_get_status(union fusb_status *status)
  158. {
  159. i2cAcquireBus(&I2CD2);
  160. /* Read the interrupt and status flags into status */
  161. fusb_read_buf(FUSB_STATUS0A, 7, status->bytes);
  162. i2cReleaseBus(&I2CD2);
  163. }
  164. enum fusb_typec_current fusb_get_typec_current(void)
  165. {
  166. i2cAcquireBus(&I2CD2);
  167. /* Read the BC_LVL into a variable */
  168. enum fusb_typec_current bc_lvl = fusb_read_byte(FUSB_STATUS0)
  169. & FUSB_STATUS0_BC_LVL;
  170. i2cReleaseBus(&I2CD2);
  171. return bc_lvl;
  172. }
  173. void fusb_reset(void)
  174. {
  175. i2cAcquireBus(&I2CD2);
  176. /* Flush the TX buffer */
  177. fusb_write_byte(FUSB_CONTROL0, 0x44);
  178. /* Flush the RX buffer */
  179. fusb_write_byte(FUSB_CONTROL1, FUSB_CONTROL1_RX_FLUSH);
  180. /* Reset the PD logic */
  181. fusb_write_byte(FUSB_RESET, FUSB_RESET_PD_RESET);
  182. i2cReleaseBus(&I2CD2);
  183. }