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

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