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

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