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

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. 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(void)
  55. {
  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(void)
  73. {
  74. /* Wait for an event */
  75. eventmask_t evt = chEvtWaitAny(PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD
  76. | PDB_EVT_PRLTX_MSG_TX);
  77. if (evt & PDB_EVT_PRLTX_RESET) {
  78. return PRLTxPHYReset;
  79. }
  80. if (evt & PDB_EVT_PRLTX_DISCARD) {
  81. return PRLTxDiscardMessage;
  82. }
  83. /* If the policy engine is trying to send a message */
  84. if (evt & PDB_EVT_PRLTX_MSG_TX) {
  85. /* Get the message */
  86. chMBFetch(&pdb_prltx_mailbox, (msg_t *) &protocol_tx_message, TIME_IMMEDIATE);
  87. /* If it's a Soft_Reset, reset the TX layer first */
  88. if (PD_MSGTYPE_GET(protocol_tx_message) == PD_MSGTYPE_SOFT_RESET
  89. && PD_NUMOBJ_GET(protocol_tx_message) == 0) {
  90. return PRLTxReset;
  91. /* Otherwise, just send the message */
  92. } else {
  93. return PRLTxConstructMessage;
  94. }
  95. }
  96. /* Silence the compiler warning */
  97. return PRLTxDiscardMessage;
  98. }
  99. static enum protocol_tx_state protocol_tx_reset(void)
  100. {
  101. /* Clear MessageIDCounter */
  102. pdb_prltx_messageidcounter = 0;
  103. /* Tell the Protocol RX thread to reset */
  104. chEvtSignal(pdb_prlrx_thread, PDB_EVT_PRLRX_RESET);
  105. chThdYield();
  106. return PRLTxConstructMessage;
  107. }
  108. /*
  109. * PRL_Tx_Construct_Message state
  110. */
  111. static enum protocol_tx_state protocol_tx_construct_message(void)
  112. {
  113. /* Make sure nobody wants us to reset */
  114. eventmask_t evt = chEvtGetAndClearEvents(PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD);
  115. if (evt & PDB_EVT_PRLTX_RESET) {
  116. return PRLTxPHYReset;
  117. }
  118. if (evt & PDB_EVT_PRLTX_DISCARD) {
  119. return PRLTxDiscardMessage;
  120. }
  121. /* Set the correct MessageID in the message */
  122. protocol_tx_message->hdr &= ~PD_HDR_MESSAGEID;
  123. protocol_tx_message->hdr |= pdb_prltx_messageidcounter << PD_HDR_MESSAGEID_SHIFT;
  124. /* Send the message to the PHY */
  125. fusb_send_message(protocol_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(void)
  132. {
  133. /* Wait for an event. There is no need to run CRCReceiveTimer, since the
  134. * FUSB302B handles that as part of its retry mechanism. */
  135. eventmask_t evt = chEvtWaitAny(PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD
  136. | PDB_EVT_PRLTX_I_TXSENT | PDB_EVT_PRLTX_I_RETRYFAIL);
  137. if (evt & PDB_EVT_PRLTX_RESET) {
  138. return PRLTxPHYReset;
  139. }
  140. if (evt & PDB_EVT_PRLTX_DISCARD) {
  141. return PRLTxDiscardMessage;
  142. }
  143. /* If the message was sent successfully */
  144. if (evt & PDB_EVT_PRLTX_I_TXSENT) {
  145. return PRLTxMatchMessageID;
  146. }
  147. /* If the message failed to be sent */
  148. if (evt & PDB_EVT_PRLTX_I_RETRYFAIL) {
  149. return PRLTxTransmissionError;
  150. }
  151. /* Silence the compiler warning */
  152. return PRLTxDiscardMessage;
  153. }
  154. /*
  155. * PRL_Tx_Match_MessageID state
  156. */
  157. static enum protocol_tx_state protocol_tx_match_messageid(void)
  158. {
  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(void)
  172. {
  173. /* Increment MessageIDCounter */
  174. pdb_prltx_messageidcounter = (pdb_prltx_messageidcounter + 1) % 8;
  175. /* Tell the policy engine that we failed */
  176. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_TX_ERR);
  177. protocol_tx_message = NULL;
  178. return PRLTxWaitMessage;
  179. }
  180. static enum protocol_tx_state protocol_tx_message_sent(void)
  181. {
  182. /* Increment MessageIDCounter */
  183. pdb_prltx_messageidcounter = (pdb_prltx_messageidcounter + 1) % 8;
  184. /* Tell the policy engine that we succeeded */
  185. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_TX_DONE);
  186. protocol_tx_message = NULL;
  187. return PRLTxWaitMessage;
  188. }
  189. static enum protocol_tx_state protocol_tx_discard_message(void)
  190. {
  191. /* If we were working on sending a message, increment MessageIDCounter */
  192. if (protocol_tx_message != NULL) {
  193. pdb_prltx_messageidcounter = (pdb_prltx_messageidcounter + 1) % 8;
  194. }
  195. return PRLTxPHYReset;
  196. }
  197. /*
  198. * Protocol layer TX state machine thread
  199. */
  200. static THD_WORKING_AREA(waProtocolTX, 128);
  201. static THD_FUNCTION(ProtocolTX, arg) {
  202. (void) arg;
  203. enum protocol_tx_state state = PRLTxPHYReset;
  204. /* Initialize the mailbox */
  205. chMBObjectInit(&pdb_prltx_mailbox, pdb_prltx_mailbox_queue, PDB_MSG_POOL_SIZE);
  206. while (true) {
  207. switch (state) {
  208. case PRLTxPHYReset:
  209. state = protocol_tx_phy_reset();
  210. break;
  211. case PRLTxWaitMessage:
  212. state = protocol_tx_wait_message();
  213. break;
  214. case PRLTxReset:
  215. state = protocol_tx_reset();
  216. break;
  217. case PRLTxConstructMessage:
  218. state = protocol_tx_construct_message();
  219. break;
  220. case PRLTxWaitResponse:
  221. state = protocol_tx_wait_response();
  222. break;
  223. case PRLTxMatchMessageID:
  224. state = protocol_tx_match_messageid();
  225. break;
  226. case PRLTxTransmissionError:
  227. state = protocol_tx_transmission_error();
  228. break;
  229. case PRLTxMessageSent:
  230. state = protocol_tx_message_sent();
  231. break;
  232. case PRLTxDiscardMessage:
  233. state = protocol_tx_discard_message();
  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(void)
  243. {
  244. pdb_prltx_thread = chThdCreateStatic(waProtocolTX, sizeof(waProtocolTX),
  245. PDB_PRIO_PRL, ProtocolTX, NULL);
  246. }