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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * PD Buddy - USB Power Delivery for everyone
  3. * Copyright (C) 2017-2018 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. * cfg: The FUSB302B to communicate with
  26. * addr: The memory address from which to read
  27. *
  28. * Returns the value read from addr.
  29. */
  30. static uint8_t fusb_read_byte(struct pdb_fusb_config *cfg, uint8_t addr)
  31. {
  32. uint8_t buf;
  33. i2cMasterTransmit(cfg->i2cp, cfg->addr, &addr, 1, &buf, 1);
  34. return buf;
  35. }
  36. /*
  37. * Read multiple bytes from the FUSB302B
  38. *
  39. * cfg: The FUSB302B to communicate with
  40. * addr: The memory address from which to read
  41. * size: The number of bytes to read
  42. * buf: The buffer into which data will be read
  43. */
  44. static void fusb_read_buf(struct pdb_fusb_config *cfg, uint8_t addr,
  45. uint8_t size, uint8_t *buf)
  46. {
  47. i2cMasterTransmit(cfg->i2cp, cfg->addr, &addr, 1, buf, size);
  48. }
  49. /*
  50. * Write a single byte to the FUSB302B
  51. *
  52. * cfg: The FUSB302B to communicate with
  53. * addr: The memory address to which we will write
  54. * byte: The value to write
  55. */
  56. static void fusb_write_byte(struct pdb_fusb_config *cfg, uint8_t addr,
  57. uint8_t byte)
  58. {
  59. uint8_t buf[2] = {addr, byte};
  60. i2cMasterTransmit(cfg->i2cp, cfg->addr, buf, 2, NULL, 0);
  61. }
  62. /*
  63. * Write multiple bytes to the FUSB302B
  64. *
  65. * cfg: The FUSB302B to communicate with
  66. * addr: The memory address to which we will write
  67. * size: The number of bytes to write
  68. * buf: The buffer to write
  69. */
  70. static void fusb_write_buf(struct pdb_fusb_config *cfg, uint8_t addr,
  71. uint8_t size, const uint8_t *buf)
  72. {
  73. uint8_t txbuf[size + 1];
  74. /* Prepare the transmit buffer */
  75. txbuf[0] = addr;
  76. for (int i = 0; i < size; i++) {
  77. txbuf[i + 1] = buf[i];
  78. }
  79. i2cMasterTransmit(cfg->i2cp, cfg->addr, txbuf, size + 1, NULL, 0);
  80. }
  81. void fusb_send_message(struct pdb_fusb_config *cfg, const union pd_msg *msg)
  82. {
  83. /* Token sequences for the FUSB302B */
  84. static uint8_t sop_seq[5] = {
  85. FUSB_FIFO_TX_SOP1,
  86. FUSB_FIFO_TX_SOP1,
  87. FUSB_FIFO_TX_SOP1,
  88. FUSB_FIFO_TX_SOP2,
  89. FUSB_FIFO_TX_PACKSYM
  90. };
  91. static uint8_t eop_seq[4] = {
  92. FUSB_FIFO_TX_JAM_CRC,
  93. FUSB_FIFO_TX_EOP,
  94. FUSB_FIFO_TX_TXOFF,
  95. FUSB_FIFO_TX_TXON
  96. };
  97. /* Take the I2C2 mutex now so there can't be a race condition on sop_seq */
  98. i2cAcquireBus(cfg->i2cp);
  99. /* Get the length of the message: a two-octet header plus NUMOBJ four-octet
  100. * data objects */
  101. uint8_t msg_len = 2 + 4 * PD_NUMOBJ_GET(msg);
  102. /* Set the number of bytes to be transmitted in the packet */
  103. sop_seq[4] = FUSB_FIFO_TX_PACKSYM | msg_len;
  104. /* Write all three parts of the message to the TX FIFO */
  105. fusb_write_buf(cfg, FUSB_FIFOS, 5, sop_seq);
  106. fusb_write_buf(cfg, FUSB_FIFOS, msg_len, msg->bytes);
  107. fusb_write_buf(cfg, FUSB_FIFOS, 4, eop_seq);
  108. i2cReleaseBus(cfg->i2cp);
  109. }
  110. uint8_t fusb_read_message(struct pdb_fusb_config *cfg, union pd_msg *msg)
  111. {
  112. uint8_t garbage[4];
  113. uint8_t numobj;
  114. i2cAcquireBus(cfg->i2cp);
  115. /* If this isn't an SOP message, return error.
  116. * Because of our configuration, we should be able to assume this means the
  117. * buffer is empty, and not try to read past a non-SOP message. */
  118. if ((fusb_read_byte(cfg, FUSB_FIFOS) & FUSB_FIFO_RX_TOKEN_BITS)
  119. != FUSB_FIFO_RX_SOP) {
  120. i2cReleaseBus(cfg->i2cp);
  121. return 1;
  122. }
  123. /* Read the message header into msg */
  124. fusb_read_buf(cfg, FUSB_FIFOS, 2, msg->bytes);
  125. /* Get the number of data objects */
  126. numobj = PD_NUMOBJ_GET(msg);
  127. /* If there is at least one data object, read the data objects */
  128. if (numobj > 0) {
  129. fusb_read_buf(cfg, FUSB_FIFOS, numobj * 4, msg->bytes + 2);
  130. }
  131. /* Throw the CRC32 in the garbage, since the PHY already checked it. */
  132. fusb_read_buf(cfg, FUSB_FIFOS, 4, garbage);
  133. i2cReleaseBus(cfg->i2cp);
  134. return 0;
  135. }
  136. void fusb_send_hardrst(struct pdb_fusb_config *cfg)
  137. {
  138. i2cAcquireBus(cfg->i2cp);
  139. /* Send a hard reset */
  140. fusb_write_byte(cfg, FUSB_CONTROL3, 0x07 | FUSB_CONTROL3_SEND_HARD_RESET);
  141. i2cReleaseBus(cfg->i2cp);
  142. }
  143. void fusb_setup(struct pdb_fusb_config *cfg)
  144. {
  145. i2cAcquireBus(cfg->i2cp);
  146. /* Fully reset the FUSB302B */
  147. fusb_write_byte(cfg, FUSB_RESET, FUSB_RESET_SW_RES);
  148. /* Turn on all power */
  149. fusb_write_byte(cfg, FUSB_POWER, 0x0F);
  150. /* Set interrupt masks */
  151. fusb_write_byte(cfg, FUSB_MASK1, 0x00);
  152. fusb_write_byte(cfg, FUSB_MASKA, 0x00);
  153. fusb_write_byte(cfg, FUSB_MASKB, 0x00);
  154. fusb_write_byte(cfg, FUSB_CONTROL0, 0x04);
  155. /* Enable automatic retransmission */
  156. fusb_write_byte(cfg, FUSB_CONTROL3, 0x07);
  157. /* Flush the RX buffer */
  158. fusb_write_byte(cfg, FUSB_CONTROL1, FUSB_CONTROL1_RX_FLUSH);
  159. /* Measure CC1 */
  160. fusb_write_byte(cfg, FUSB_SWITCHES0, 0x07);
  161. chThdSleepMicroseconds(250);
  162. uint8_t cc1 = fusb_read_byte(cfg, FUSB_STATUS0) & FUSB_STATUS0_BC_LVL;
  163. /* Measure CC2 */
  164. fusb_write_byte(cfg, FUSB_SWITCHES0, 0x0B);
  165. chThdSleepMicroseconds(250);
  166. uint8_t cc2 = fusb_read_byte(cfg, FUSB_STATUS0) & FUSB_STATUS0_BC_LVL;
  167. /* Select the correct CC line for BMC signaling; also enable AUTO_CRC */
  168. if (cc1 > cc2) {
  169. fusb_write_byte(cfg, FUSB_SWITCHES1, 0x25);
  170. fusb_write_byte(cfg, FUSB_SWITCHES0, 0x07);
  171. } else {
  172. fusb_write_byte(cfg, FUSB_SWITCHES1, 0x26);
  173. fusb_write_byte(cfg, FUSB_SWITCHES0, 0x0B);
  174. }
  175. /* Reset the PD logic */
  176. fusb_write_byte(cfg, FUSB_RESET, FUSB_RESET_PD_RESET);
  177. i2cReleaseBus(cfg->i2cp);
  178. }
  179. void fusb_get_status(struct pdb_fusb_config *cfg, union fusb_status *status)
  180. {
  181. i2cAcquireBus(cfg->i2cp);
  182. /* Read the interrupt and status flags into status */
  183. fusb_read_buf(cfg, FUSB_STATUS0A, 7, status->bytes);
  184. i2cReleaseBus(cfg->i2cp);
  185. }
  186. enum fusb_typec_current fusb_get_typec_current(struct pdb_fusb_config *cfg)
  187. {
  188. i2cAcquireBus(cfg->i2cp);
  189. /* Read the BC_LVL into a variable */
  190. enum fusb_typec_current bc_lvl = fusb_read_byte(cfg, FUSB_STATUS0)
  191. & FUSB_STATUS0_BC_LVL;
  192. i2cReleaseBus(cfg->i2cp);
  193. return bc_lvl;
  194. }
  195. void fusb_reset(struct pdb_fusb_config *cfg)
  196. {
  197. i2cAcquireBus(cfg->i2cp);
  198. /* Flush the TX buffer */
  199. fusb_write_byte(cfg, FUSB_CONTROL0, 0x44);
  200. /* Flush the RX buffer */
  201. fusb_write_byte(cfg, FUSB_CONTROL1, FUSB_CONTROL1_RX_FLUSH);
  202. /* Reset the PD logic */
  203. fusb_write_byte(cfg, FUSB_RESET, FUSB_RESET_PD_RESET);
  204. i2cReleaseBus(cfg->i2cp);
  205. }