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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /* Protocol layer TX thread mailbox */
  26. mailbox_t pdb_prltx_mailbox;
  27. /*
  28. * Protocol TX machine states
  29. *
  30. * Because the PHY can automatically send retries, the Check_RetryCounter state
  31. * has been removed, transitions relating to it are modified appropriately, and
  32. * we don't even keep a RetryCounter.
  33. */
  34. enum protocol_tx_state {
  35. PRLTxPHYReset,
  36. PRLTxWaitMessage,
  37. PRLTxReset,
  38. PRLTxConstructMessage,
  39. PRLTxWaitResponse,
  40. PRLTxMatchMessageID,
  41. PRLTxTransmissionError,
  42. PRLTxMessageSent,
  43. PRLTxDiscardMessage
  44. };
  45. /* The message we're currently working on transmitting */
  46. static union pd_msg *protocol_tx_message = NULL;
  47. /* The ID to be used in transmission */
  48. int8_t pdb_prltx_messageidcounter = 0;
  49. /*
  50. * PRL_Tx_PHY_Layer_Reset state
  51. */
  52. static enum protocol_tx_state protocol_tx_phy_reset(struct pdb_config *cfg)
  53. {
  54. (void) cfg;
  55. /* Reset the PHY */
  56. fusb_reset();
  57. /* If a message was pending when we got here, tell the policy engine that
  58. * we failed to send it */
  59. if (protocol_tx_message != NULL) {
  60. /* Tell the policy engine that we failed */
  61. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_TX_ERR);
  62. /* Finish failing to send the message */
  63. protocol_tx_message = NULL;
  64. }
  65. /* Wait for a message request */
  66. return PRLTxWaitMessage;
  67. }
  68. /*
  69. * PRL_Tx_Wait_for_Message_Request state
  70. */
  71. static enum protocol_tx_state protocol_tx_wait_message(struct pdb_config *cfg)
  72. {
  73. (void) cfg;
  74. /* Wait for an event */
  75. eventmask_t evt = chEvtWaitAny(PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD
  76. | PDB_EVT_PRLTX_MSG_TX);
  77. if (evt & PDB_EVT_PRLTX_RESET) {
  78. return PRLTxPHYReset;
  79. }
  80. if (evt & PDB_EVT_PRLTX_DISCARD) {
  81. return PRLTxDiscardMessage;
  82. }
  83. /* If the policy engine is trying to send a message */
  84. if (evt & PDB_EVT_PRLTX_MSG_TX) {
  85. /* Get the message */
  86. chMBFetch(&pdb_prltx_mailbox, (msg_t *) &protocol_tx_message, TIME_IMMEDIATE);
  87. /* If it's a Soft_Reset, reset the TX layer first */
  88. if (PD_MSGTYPE_GET(protocol_tx_message) == PD_MSGTYPE_SOFT_RESET
  89. && PD_NUMOBJ_GET(protocol_tx_message) == 0) {
  90. return PRLTxReset;
  91. /* Otherwise, just send the message */
  92. } else {
  93. return PRLTxConstructMessage;
  94. }
  95. }
  96. /* Silence the compiler warning */
  97. return PRLTxDiscardMessage;
  98. }
  99. static enum protocol_tx_state protocol_tx_reset(struct pdb_config *cfg)
  100. {
  101. /* Clear MessageIDCounter */
  102. pdb_prltx_messageidcounter = 0;
  103. /* Tell the Protocol RX thread to reset */
  104. chEvtSignal(cfg->prl.rx_thread, PDB_EVT_PRLRX_RESET);
  105. chThdYield();
  106. return PRLTxConstructMessage;
  107. }
  108. /*
  109. * PRL_Tx_Construct_Message state
  110. */
  111. static enum protocol_tx_state protocol_tx_construct_message(struct pdb_config *cfg)
  112. {
  113. (void) cfg;
  114. /* Make sure nobody wants us to reset */
  115. eventmask_t evt = chEvtGetAndClearEvents(PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD);
  116. if (evt & PDB_EVT_PRLTX_RESET) {
  117. return PRLTxPHYReset;
  118. }
  119. if (evt & PDB_EVT_PRLTX_DISCARD) {
  120. return PRLTxDiscardMessage;
  121. }
  122. /* Set the correct MessageID in the message */
  123. protocol_tx_message->hdr &= ~PD_HDR_MESSAGEID;
  124. protocol_tx_message->hdr |= pdb_prltx_messageidcounter << PD_HDR_MESSAGEID_SHIFT;
  125. /* Send the message to the PHY */
  126. fusb_send_message(protocol_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. (void) cfg;
  162. union pd_msg goodcrc;
  163. /* Read the GoodCRC */
  164. fusb_read_message(&goodcrc);
  165. /* Check that the message is correct */
  166. if (PD_MSGTYPE_GET(&goodcrc) == PD_MSGTYPE_GOODCRC
  167. && PD_NUMOBJ_GET(&goodcrc) == 0
  168. && PD_MESSAGEID_GET(&goodcrc) == pdb_prltx_messageidcounter) {
  169. return PRLTxMessageSent;
  170. } else {
  171. return PRLTxTransmissionError;
  172. }
  173. }
  174. static enum protocol_tx_state protocol_tx_transmission_error(struct pdb_config *cfg)
  175. {
  176. (void) cfg;
  177. /* Increment MessageIDCounter */
  178. pdb_prltx_messageidcounter = (pdb_prltx_messageidcounter + 1) % 8;
  179. /* Tell the policy engine that we failed */
  180. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_TX_ERR);
  181. protocol_tx_message = NULL;
  182. return PRLTxWaitMessage;
  183. }
  184. static enum protocol_tx_state protocol_tx_message_sent(struct pdb_config *cfg)
  185. {
  186. (void) cfg;
  187. /* Increment MessageIDCounter */
  188. pdb_prltx_messageidcounter = (pdb_prltx_messageidcounter + 1) % 8;
  189. /* Tell the policy engine that we succeeded */
  190. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_TX_DONE);
  191. protocol_tx_message = NULL;
  192. return PRLTxWaitMessage;
  193. }
  194. static enum protocol_tx_state protocol_tx_discard_message(struct pdb_config *cfg)
  195. {
  196. (void) cfg;
  197. /* If we were working on sending a message, increment MessageIDCounter */
  198. if (protocol_tx_message != NULL) {
  199. pdb_prltx_messageidcounter = (pdb_prltx_messageidcounter + 1) % 8;
  200. }
  201. return PRLTxPHYReset;
  202. }
  203. /*
  204. * Protocol layer TX state machine thread
  205. */
  206. static THD_FUNCTION(ProtocolTX, vcfg) {
  207. struct pdb_config *cfg = vcfg;
  208. enum protocol_tx_state state = PRLTxPHYReset;
  209. /* Initialize the mailbox */
  210. chMBObjectInit(&pdb_prltx_mailbox, cfg->prl._tx_mailbox_queue, PDB_MSG_POOL_SIZE);
  211. while (true) {
  212. switch (state) {
  213. case PRLTxPHYReset:
  214. state = protocol_tx_phy_reset(cfg);
  215. break;
  216. case PRLTxWaitMessage:
  217. state = protocol_tx_wait_message(cfg);
  218. break;
  219. case PRLTxReset:
  220. state = protocol_tx_reset(cfg);
  221. break;
  222. case PRLTxConstructMessage:
  223. state = protocol_tx_construct_message(cfg);
  224. break;
  225. case PRLTxWaitResponse:
  226. state = protocol_tx_wait_response(cfg);
  227. break;
  228. case PRLTxMatchMessageID:
  229. state = protocol_tx_match_messageid(cfg);
  230. break;
  231. case PRLTxTransmissionError:
  232. state = protocol_tx_transmission_error(cfg);
  233. break;
  234. case PRLTxMessageSent:
  235. state = protocol_tx_message_sent(cfg);
  236. break;
  237. case PRLTxDiscardMessage:
  238. state = protocol_tx_discard_message(cfg);
  239. break;
  240. default:
  241. /* This is an error. It really shouldn't happen. We might
  242. * want to handle it anyway, though. */
  243. break;
  244. }
  245. }
  246. }
  247. void pdb_prltx_run(struct pdb_config *cfg)
  248. {
  249. cfg->prl.tx_thread = chThdCreateStatic(cfg->prl._tx_wa,
  250. sizeof(cfg->prl._tx_wa), PDB_PRIO_PRL, ProtocolTX, cfg);
  251. }