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

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