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.2KB

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