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