PD Buddy Sink Firmware
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

protocol_tx.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "protocol_tx.h"
  19. #include <pd.h>
  20. #include "priorities.h"
  21. #include "policy_engine.h"
  22. #include "protocol_rx.h"
  23. #include "fusb302b.h"
  24. /*
  25. * Protocol TX machine states
  26. *
  27. * Because the PHY can automatically send retries, the Check_RetryCounter state
  28. * has been removed, transitions relating to it are modified appropriately, and
  29. * we don't even keep a RetryCounter.
  30. */
  31. enum protocol_tx_state {
  32. PRLTxPHYReset,
  33. PRLTxWaitMessage,
  34. PRLTxReset,
  35. PRLTxConstructMessage,
  36. PRLTxWaitResponse,
  37. PRLTxMatchMessageID,
  38. PRLTxTransmissionError,
  39. PRLTxMessageSent,
  40. PRLTxDiscardMessage
  41. };
  42. /*
  43. * PRL_Tx_PHY_Layer_Reset state
  44. */
  45. static enum protocol_tx_state protocol_tx_phy_reset(struct pdb_config *cfg)
  46. {
  47. /* Reset the PHY */
  48. fusb_reset(&cfg->fusb);
  49. /* If a message was pending when we got here, tell the policy engine that
  50. * we failed to send it */
  51. if (cfg->prl._tx_message != NULL) {
  52. /* Tell the policy engine that we failed */
  53. chEvtSignal(cfg->pe.thread, PDB_EVT_PE_TX_ERR);
  54. /* Finish failing to send the message */
  55. cfg->prl._tx_message = NULL;
  56. }
  57. /* Wait for a message request */
  58. return PRLTxWaitMessage;
  59. }
  60. /*
  61. * PRL_Tx_Wait_for_Message_Request state
  62. */
  63. static enum protocol_tx_state protocol_tx_wait_message(struct pdb_config *cfg)
  64. {
  65. /* Wait for an event */
  66. eventmask_t evt = chEvtWaitAny(PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD
  67. | PDB_EVT_PRLTX_MSG_TX);
  68. if (evt & PDB_EVT_PRLTX_RESET) {
  69. return PRLTxPHYReset;
  70. }
  71. if (evt & PDB_EVT_PRLTX_DISCARD) {
  72. return PRLTxDiscardMessage;
  73. }
  74. /* If the policy engine is trying to send a message */
  75. if (evt & PDB_EVT_PRLTX_MSG_TX) {
  76. /* Get the message */
  77. chMBFetch(&cfg->prl.tx_mailbox, (msg_t *) &cfg->prl._tx_message, TIME_IMMEDIATE);
  78. /* If it's a Soft_Reset, reset the TX layer first */
  79. if (PD_MSGTYPE_GET(cfg->prl._tx_message) == PD_MSGTYPE_SOFT_RESET
  80. && PD_NUMOBJ_GET(cfg->prl._tx_message) == 0) {
  81. return PRLTxReset;
  82. /* Otherwise, just send the message */
  83. } else {
  84. return PRLTxConstructMessage;
  85. }
  86. }
  87. /* Silence the compiler warning */
  88. return PRLTxDiscardMessage;
  89. }
  90. static enum protocol_tx_state protocol_tx_reset(struct pdb_config *cfg)
  91. {
  92. /* Clear MessageIDCounter */
  93. cfg->prl._tx_messageidcounter = 0;
  94. /* Tell the Protocol RX thread to reset */
  95. chEvtSignal(cfg->prl.rx_thread, PDB_EVT_PRLRX_RESET);
  96. chThdYield();
  97. return PRLTxConstructMessage;
  98. }
  99. /*
  100. * PRL_Tx_Construct_Message state
  101. */
  102. static enum protocol_tx_state protocol_tx_construct_message(struct pdb_config *cfg)
  103. {
  104. /* Make sure nobody wants us to reset */
  105. eventmask_t evt = chEvtGetAndClearEvents(PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD);
  106. if (evt & PDB_EVT_PRLTX_RESET) {
  107. return PRLTxPHYReset;
  108. }
  109. if (evt & PDB_EVT_PRLTX_DISCARD) {
  110. return PRLTxDiscardMessage;
  111. }
  112. /* Set the correct MessageID in the message */
  113. cfg->prl._tx_message->hdr &= ~PD_HDR_MESSAGEID;
  114. cfg->prl._tx_message->hdr |= (cfg->prl._tx_messageidcounter % 8) << PD_HDR_MESSAGEID_SHIFT;
  115. /* PD 3.0 collision avoidance */
  116. if ((cfg->pe.hdr_template & PD_HDR_SPECREV) == PD_SPECREV_3_0) {
  117. /* If we're starting an AMS, wait for permission to transmit */
  118. evt = chEvtGetAndClearEvents(PDB_EVT_PRLTX_START_AMS);
  119. if (evt & PDB_EVT_PRLTX_START_AMS) {
  120. while (fusb_get_typec_current(&cfg->fusb) != fusb_sink_tx_ok) {
  121. chThdSleepMilliseconds(1);
  122. }
  123. }
  124. }
  125. /* Send the message to the PHY */
  126. fusb_send_message(&cfg->fusb, cfg->prl._tx_message);
  127. return PRLTxWaitResponse;
  128. }
  129. /*
  130. * PRL_Tx_Wait_for_PHY_Response state
  131. */
  132. static enum protocol_tx_state protocol_tx_wait_response(struct pdb_config *cfg)
  133. {
  134. (void) cfg;
  135. /* Wait for an event. There is no need to run CRCReceiveTimer, since the
  136. * FUSB302B handles that as part of its retry mechanism. */
  137. eventmask_t evt = chEvtWaitAny(PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD
  138. | PDB_EVT_PRLTX_I_TXSENT | PDB_EVT_PRLTX_I_RETRYFAIL);
  139. if (evt & PDB_EVT_PRLTX_RESET) {
  140. return PRLTxPHYReset;
  141. }
  142. if (evt & PDB_EVT_PRLTX_DISCARD) {
  143. return PRLTxDiscardMessage;
  144. }
  145. /* If the message was sent successfully */
  146. if (evt & PDB_EVT_PRLTX_I_TXSENT) {
  147. return PRLTxMatchMessageID;
  148. }
  149. /* If the message failed to be sent */
  150. if (evt & PDB_EVT_PRLTX_I_RETRYFAIL) {
  151. return PRLTxTransmissionError;
  152. }
  153. /* Silence the compiler warning */
  154. return PRLTxDiscardMessage;
  155. }
  156. /*
  157. * PRL_Tx_Match_MessageID state
  158. */
  159. static enum protocol_tx_state protocol_tx_match_messageid(struct pdb_config *cfg)
  160. {
  161. union pd_msg goodcrc;
  162. /* Read the GoodCRC */
  163. fusb_read_message(&cfg->fusb, &goodcrc);
  164. /* Check that the message is correct */
  165. if (PD_MSGTYPE_GET(&goodcrc) == PD_MSGTYPE_GOODCRC
  166. && PD_NUMOBJ_GET(&goodcrc) == 0
  167. && PD_MESSAGEID_GET(&goodcrc) == cfg->prl._tx_messageidcounter) {
  168. return PRLTxMessageSent;
  169. } else {
  170. return PRLTxTransmissionError;
  171. }
  172. }
  173. static enum protocol_tx_state protocol_tx_transmission_error(struct pdb_config *cfg)
  174. {
  175. /* Increment MessageIDCounter */
  176. cfg->prl._tx_messageidcounter = (cfg->prl._tx_messageidcounter + 1) % 8;
  177. /* Tell the policy engine that we failed */
  178. chEvtSignal(cfg->pe.thread, PDB_EVT_PE_TX_ERR);
  179. cfg->prl._tx_message = NULL;
  180. return PRLTxWaitMessage;
  181. }
  182. static enum protocol_tx_state protocol_tx_message_sent(struct pdb_config *cfg)
  183. {
  184. /* Increment MessageIDCounter */
  185. cfg->prl._tx_messageidcounter = (cfg->prl._tx_messageidcounter + 1) % 8;
  186. /* Tell the policy engine that we succeeded */
  187. chEvtSignal(cfg->pe.thread, PDB_EVT_PE_TX_DONE);
  188. cfg->prl._tx_message = NULL;
  189. return PRLTxWaitMessage;
  190. }
  191. static enum protocol_tx_state protocol_tx_discard_message(struct pdb_config *cfg)
  192. {
  193. /* If we were working on sending a message, increment MessageIDCounter */
  194. if (cfg->prl._tx_message != NULL) {
  195. cfg->prl._tx_messageidcounter = (cfg->prl._tx_messageidcounter + 1) % 8;
  196. }
  197. return PRLTxPHYReset;
  198. }
  199. /*
  200. * Protocol layer TX state machine thread
  201. */
  202. static THD_FUNCTION(ProtocolTX, vcfg) {
  203. struct pdb_config *cfg = vcfg;
  204. enum protocol_tx_state state = PRLTxPHYReset;
  205. /* Initialize the mailbox */
  206. chMBObjectInit(&cfg->prl.tx_mailbox, cfg->prl._tx_mailbox_queue, PDB_MSG_POOL_SIZE);
  207. while (true) {
  208. switch (state) {
  209. case PRLTxPHYReset:
  210. state = protocol_tx_phy_reset(cfg);
  211. break;
  212. case PRLTxWaitMessage:
  213. state = protocol_tx_wait_message(cfg);
  214. break;
  215. case PRLTxReset:
  216. state = protocol_tx_reset(cfg);
  217. break;
  218. case PRLTxConstructMessage:
  219. state = protocol_tx_construct_message(cfg);
  220. break;
  221. case PRLTxWaitResponse:
  222. state = protocol_tx_wait_response(cfg);
  223. break;
  224. case PRLTxMatchMessageID:
  225. state = protocol_tx_match_messageid(cfg);
  226. break;
  227. case PRLTxTransmissionError:
  228. state = protocol_tx_transmission_error(cfg);
  229. break;
  230. case PRLTxMessageSent:
  231. state = protocol_tx_message_sent(cfg);
  232. break;
  233. case PRLTxDiscardMessage:
  234. state = protocol_tx_discard_message(cfg);
  235. break;
  236. default:
  237. /* This is an error. It really shouldn't happen. We might
  238. * want to handle it anyway, though. */
  239. break;
  240. }
  241. }
  242. }
  243. void pdb_prltx_run(struct pdb_config *cfg)
  244. {
  245. cfg->prl.tx_thread = chThdCreateStatic(cfg->prl._tx_wa,
  246. sizeof(cfg->prl._tx_wa), PDB_PRIO_PRL, ProtocolTX, cfg);
  247. }