PD Buddy Sink Firmware
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

protocol_tx.c 8.2KB

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