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.

protocol_tx.c 8.1KB

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