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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * PD Buddy Firmware Library - USB Power Delivery for everyone
  3. * Copyright 2017-2018 Clayton G. Hobbs
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "protocol_tx.h"
  18. #include <pd.h>
  19. #include "priorities.h"
  20. #include "policy_engine.h"
  21. #include "protocol_rx.h"
  22. #include "fusb302b.h"
  23. /*
  24. * Protocol TX machine states
  25. *
  26. * Because the PHY can automatically send retries, the Check_RetryCounter state
  27. * has been removed, transitions relating to it are modified appropriately, and
  28. * we don't even keep a RetryCounter.
  29. */
  30. enum protocol_tx_state {
  31. PRLTxPHYReset,
  32. PRLTxWaitMessage,
  33. PRLTxReset,
  34. PRLTxConstructMessage,
  35. PRLTxWaitResponse,
  36. PRLTxMatchMessageID,
  37. PRLTxTransmissionError,
  38. PRLTxMessageSent,
  39. PRLTxDiscardMessage
  40. };
  41. /*
  42. * PRL_Tx_PHY_Layer_Reset state
  43. */
  44. static enum protocol_tx_state protocol_tx_phy_reset(struct pdb_config *cfg)
  45. {
  46. /* Reset the PHY */
  47. fusb_reset(&cfg->fusb);
  48. /* If a message was pending when we got here, tell the policy engine that
  49. * we failed to send it */
  50. if (cfg->prl._tx_message != NULL) {
  51. /* Tell the policy engine that we failed */
  52. chEvtSignal(cfg->pe.thread, PDB_EVT_PE_TX_ERR);
  53. /* Finish failing to send the message */
  54. cfg->prl._tx_message = NULL;
  55. }
  56. /* Wait for a message request */
  57. return PRLTxWaitMessage;
  58. }
  59. /*
  60. * PRL_Tx_Wait_for_Message_Request state
  61. */
  62. static enum protocol_tx_state protocol_tx_wait_message(struct pdb_config *cfg)
  63. {
  64. /* Wait for an event */
  65. eventmask_t evt = chEvtWaitAny(PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD
  66. | PDB_EVT_PRLTX_MSG_TX);
  67. if (evt & PDB_EVT_PRLTX_RESET) {
  68. return PRLTxPHYReset;
  69. }
  70. if (evt & PDB_EVT_PRLTX_DISCARD) {
  71. return PRLTxDiscardMessage;
  72. }
  73. /* If the policy engine is trying to send a message */
  74. if (evt & PDB_EVT_PRLTX_MSG_TX) {
  75. /* Get the message */
  76. chMBFetchTimeout(&cfg->prl.tx_mailbox, (msg_t *) &cfg->prl._tx_message, TIME_IMMEDIATE);
  77. /* If it's a Soft_Reset, reset the TX layer first */
  78. if (PD_MSGTYPE_GET(cfg->prl._tx_message) == PD_MSGTYPE_SOFT_RESET
  79. && PD_NUMOBJ_GET(cfg->prl._tx_message) == 0) {
  80. return PRLTxReset;
  81. /* Otherwise, just send the message */
  82. } else {
  83. return PRLTxConstructMessage;
  84. }
  85. }
  86. /* Silence the compiler warning */
  87. return PRLTxDiscardMessage;
  88. }
  89. static enum protocol_tx_state protocol_tx_reset(struct pdb_config *cfg)
  90. {
  91. /* Clear MessageIDCounter */
  92. cfg->prl._tx_messageidcounter = 0;
  93. /* Tell the Protocol RX thread to reset */
  94. chEvtSignal(cfg->prl.rx_thread, PDB_EVT_PRLRX_RESET);
  95. chThdYield();
  96. return PRLTxConstructMessage;
  97. }
  98. /*
  99. * PRL_Tx_Construct_Message state
  100. */
  101. static enum protocol_tx_state protocol_tx_construct_message(struct pdb_config *cfg)
  102. {
  103. /* Make sure nobody wants us to reset */
  104. eventmask_t evt = chEvtGetAndClearEvents(PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD);
  105. if (evt & PDB_EVT_PRLTX_RESET) {
  106. return PRLTxPHYReset;
  107. }
  108. if (evt & PDB_EVT_PRLTX_DISCARD) {
  109. return PRLTxDiscardMessage;
  110. }
  111. /* Set the correct MessageID in the message */
  112. cfg->prl._tx_message->hdr &= ~PD_HDR_MESSAGEID;
  113. cfg->prl._tx_message->hdr |= (cfg->prl._tx_messageidcounter % 8) << PD_HDR_MESSAGEID_SHIFT;
  114. /* PD 3.0 collision avoidance */
  115. if ((cfg->pe.hdr_template & PD_HDR_SPECREV) == PD_SPECREV_3_0) {
  116. /* If we're starting an AMS, wait for permission to transmit */
  117. evt = chEvtGetAndClearEvents(PDB_EVT_PRLTX_START_AMS);
  118. if (evt & PDB_EVT_PRLTX_START_AMS) {
  119. while (fusb_get_typec_current(&cfg->fusb) != fusb_sink_tx_ok) {
  120. chThdSleepMilliseconds(1);
  121. }
  122. }
  123. }
  124. /* Send the message to the PHY */
  125. fusb_send_message(&cfg->fusb, cfg->prl._tx_message);
  126. return PRLTxWaitResponse;
  127. }
  128. /*
  129. * PRL_Tx_Wait_for_PHY_Response state
  130. */
  131. static enum protocol_tx_state protocol_tx_wait_response(struct pdb_config *cfg)
  132. {
  133. (void) cfg;
  134. /* Wait for an event. There is no need to run CRCReceiveTimer, since the
  135. * FUSB302B handles that as part of its retry mechanism. */
  136. eventmask_t evt = chEvtWaitAny(PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD
  137. | PDB_EVT_PRLTX_I_TXSENT | PDB_EVT_PRLTX_I_RETRYFAIL);
  138. if (evt & PDB_EVT_PRLTX_RESET) {
  139. return PRLTxPHYReset;
  140. }
  141. if (evt & PDB_EVT_PRLTX_DISCARD) {
  142. return PRLTxDiscardMessage;
  143. }
  144. /* If the message was sent successfully */
  145. if (evt & PDB_EVT_PRLTX_I_TXSENT) {
  146. return PRLTxMatchMessageID;
  147. }
  148. /* If the message failed to be sent */
  149. if (evt & PDB_EVT_PRLTX_I_RETRYFAIL) {
  150. return PRLTxTransmissionError;
  151. }
  152. /* Silence the compiler warning */
  153. return PRLTxDiscardMessage;
  154. }
  155. /*
  156. * PRL_Tx_Match_MessageID state
  157. */
  158. static enum protocol_tx_state protocol_tx_match_messageid(struct pdb_config *cfg)
  159. {
  160. union pd_msg goodcrc;
  161. /* Read the GoodCRC */
  162. fusb_read_message(&cfg->fusb, &goodcrc);
  163. /* Check that the message is correct */
  164. if (PD_MSGTYPE_GET(&goodcrc) == PD_MSGTYPE_GOODCRC
  165. && PD_NUMOBJ_GET(&goodcrc) == 0
  166. && PD_MESSAGEID_GET(&goodcrc) == cfg->prl._tx_messageidcounter) {
  167. return PRLTxMessageSent;
  168. } else {
  169. return PRLTxTransmissionError;
  170. }
  171. }
  172. static enum protocol_tx_state protocol_tx_transmission_error(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 failed */
  177. chEvtSignal(cfg->pe.thread, PDB_EVT_PE_TX_ERR);
  178. cfg->prl._tx_message = NULL;
  179. return PRLTxWaitMessage;
  180. }
  181. static enum protocol_tx_state protocol_tx_message_sent(struct pdb_config *cfg)
  182. {
  183. /* Increment MessageIDCounter */
  184. cfg->prl._tx_messageidcounter = (cfg->prl._tx_messageidcounter + 1) % 8;
  185. /* Tell the policy engine that we succeeded */
  186. chEvtSignal(cfg->pe.thread, PDB_EVT_PE_TX_DONE);
  187. cfg->prl._tx_message = NULL;
  188. return PRLTxWaitMessage;
  189. }
  190. static enum protocol_tx_state protocol_tx_discard_message(struct pdb_config *cfg)
  191. {
  192. /* If we were working on sending a message, increment MessageIDCounter */
  193. if (cfg->prl._tx_message != NULL) {
  194. cfg->prl._tx_messageidcounter = (cfg->prl._tx_messageidcounter + 1) % 8;
  195. }
  196. return PRLTxPHYReset;
  197. }
  198. /*
  199. * Protocol layer TX state machine thread
  200. */
  201. static THD_FUNCTION(ProtocolTX, vcfg) {
  202. struct pdb_config *cfg = vcfg;
  203. enum protocol_tx_state state = PRLTxPHYReset;
  204. /* Initialize the mailbox */
  205. chMBObjectInit(&cfg->prl.tx_mailbox, cfg->prl._tx_mailbox_queue, PDB_MSG_POOL_SIZE);
  206. while (true) {
  207. switch (state) {
  208. case PRLTxPHYReset:
  209. state = protocol_tx_phy_reset(cfg);
  210. break;
  211. case PRLTxWaitMessage:
  212. state = protocol_tx_wait_message(cfg);
  213. break;
  214. case PRLTxReset:
  215. state = protocol_tx_reset(cfg);
  216. break;
  217. case PRLTxConstructMessage:
  218. state = protocol_tx_construct_message(cfg);
  219. break;
  220. case PRLTxWaitResponse:
  221. state = protocol_tx_wait_response(cfg);
  222. break;
  223. case PRLTxMatchMessageID:
  224. state = protocol_tx_match_messageid(cfg);
  225. break;
  226. case PRLTxTransmissionError:
  227. state = protocol_tx_transmission_error(cfg);
  228. break;
  229. case PRLTxMessageSent:
  230. state = protocol_tx_message_sent(cfg);
  231. break;
  232. case PRLTxDiscardMessage:
  233. state = protocol_tx_discard_message(cfg);
  234. break;
  235. default:
  236. /* This is an error. It really shouldn't happen. We might
  237. * want to handle it anyway, though. */
  238. break;
  239. }
  240. }
  241. }
  242. void pdb_prltx_run(struct pdb_config *cfg)
  243. {
  244. cfg->prl.tx_thread = chThdCreateStatic(cfg->prl._tx_wa,
  245. sizeof(cfg->prl._tx_wa), PDB_PRIO_PRL, ProtocolTX, cfg);
  246. }