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_rx.c 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_rx.h"
  19. #include <stdlib.h>
  20. #include "priorities.h"
  21. #include "policy_engine.h"
  22. #include "protocol_tx.h"
  23. #include "fusb302b.h"
  24. #include "messages.h"
  25. #include "pd.h"
  26. /*
  27. * Protocol RX machine states
  28. *
  29. * There is no Send_GoodCRC state because the PHY sends the GoodCRC for us.
  30. * All transitions that would go to that state instead go to Check_MessageID.
  31. */
  32. enum protocol_rx_state {
  33. PRLRxWaitPHY,
  34. PRLRxReset,
  35. PRLRxCheckMessageID,
  36. PRLRxStoreMessageID
  37. };
  38. /* The received message we're currently working with */
  39. static union pd_msg *protocol_rx_message = NULL;
  40. /* The ID of the last received message */
  41. int8_t pdb_prlrx_messageid = -1;
  42. /*
  43. * PRL_Rx_Wait_for_PHY_Message state
  44. */
  45. static enum protocol_rx_state protocol_rx_wait_phy(struct pdb_config *cfg)
  46. {
  47. (void) cfg;
  48. /* Wait for an event */
  49. eventmask_t evt = chEvtWaitAny(ALL_EVENTS);
  50. /* If we got a reset event, reset */
  51. if (evt & PDB_EVT_PRLRX_RESET) {
  52. return PRLRxWaitPHY;
  53. }
  54. /* If we got an I_GCRCSENT event, read the message and decide what to do */
  55. if (evt & PDB_EVT_PRLRX_I_GCRCSENT) {
  56. /* Get a buffer to read the message into. Guaranteed to not fail
  57. * because we have a big enough pool and are careful. */
  58. protocol_rx_message = chPoolAlloc(&pdb_msg_pool);
  59. /* Read the message */
  60. fusb_read_message(protocol_rx_message);
  61. /* If it's a Soft_Reset, go to the soft reset state */
  62. if (PD_MSGTYPE_GET(protocol_rx_message) == PD_MSGTYPE_SOFT_RESET
  63. && PD_NUMOBJ_GET(protocol_rx_message) == 0) {
  64. return PRLRxReset;
  65. /* Otherwise, check the message ID */
  66. } else {
  67. return PRLRxCheckMessageID;
  68. }
  69. }
  70. /* We shouldn't ever get here. This just silence the compiler warning. */
  71. return PRLRxWaitPHY;
  72. }
  73. /*
  74. * PRL_Rx_Layer_Reset_for_Receive state
  75. */
  76. static enum protocol_rx_state protocol_rx_reset(struct pdb_config *cfg)
  77. {
  78. /* Reset MessageIDCounter */
  79. pdb_prltx_messageidcounter = 0;
  80. /* Clear stored MessageID */
  81. pdb_prlrx_messageid = -1;
  82. /* TX transitions to its reset state */
  83. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_RESET);
  84. chThdYield();
  85. /* If we got a RESET signal, reset the machine */
  86. if (chEvtGetAndClearEvents(PDB_EVT_PRLRX_RESET) != 0) {
  87. chPoolFree(&pdb_msg_pool, protocol_rx_message);
  88. protocol_rx_message = NULL;
  89. return PRLRxWaitPHY;
  90. }
  91. /* Go to the Check_MessageID state */
  92. return PRLRxCheckMessageID;
  93. }
  94. /*
  95. * PRL_Rx_Check_MessageID state
  96. */
  97. static enum protocol_rx_state protocol_rx_check_messageid(struct pdb_config *cfg)
  98. {
  99. (void) cfg;
  100. /* If we got a RESET signal, reset the machine */
  101. if (chEvtGetAndClearEvents(PDB_EVT_PRLRX_RESET) != 0) {
  102. chPoolFree(&pdb_msg_pool, protocol_rx_message);
  103. protocol_rx_message = NULL;
  104. return PRLRxWaitPHY;
  105. }
  106. /* If the message has the stored ID, we've seen this message before. Free
  107. * it and don't pass it to the policy engine. */
  108. if (PD_MESSAGEID_GET(protocol_rx_message) == pdb_prlrx_messageid) {
  109. chPoolFree(&pdb_msg_pool, protocol_rx_message);
  110. protocol_rx_message = NULL;
  111. return PRLRxWaitPHY;
  112. /* Otherwise, there's either no stored ID or this message has an ID we
  113. * haven't just seen. Transition to the Store_MessageID state. */
  114. } else {
  115. return PRLRxStoreMessageID;
  116. }
  117. }
  118. /*
  119. * PRL_Rx_Store_MessageID state
  120. */
  121. static enum protocol_rx_state protocol_rx_store_messageid(struct pdb_config *cfg)
  122. {
  123. /* Tell ProtocolTX to discard the message being transmitted */
  124. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_DISCARD);
  125. chThdYield();
  126. /* Update the stored MessageID */
  127. pdb_prlrx_messageid = PD_MESSAGEID_GET(protocol_rx_message);
  128. /* Pass the message to the policy engine. */
  129. chMBPost(&pdb_pe_mailbox, (msg_t) protocol_rx_message, TIME_IMMEDIATE);
  130. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_MSG_RX);
  131. /* Don't check if we got a RESET because we'd do nothing different. */
  132. return PRLRxWaitPHY;
  133. }
  134. /*
  135. * Protocol layer RX state machine thread
  136. */
  137. static THD_FUNCTION(ProtocolRX, cfg) {
  138. enum protocol_rx_state state = PRLRxWaitPHY;
  139. while (true) {
  140. switch (state) {
  141. case PRLRxWaitPHY:
  142. state = protocol_rx_wait_phy(cfg);
  143. break;
  144. case PRLRxReset:
  145. state = protocol_rx_reset(cfg);
  146. break;
  147. case PRLRxCheckMessageID:
  148. state = protocol_rx_check_messageid(cfg);
  149. break;
  150. case PRLRxStoreMessageID:
  151. state = protocol_rx_store_messageid(cfg);
  152. break;
  153. default:
  154. /* This is an error. It really shouldn't happen. We might
  155. * want to handle it anyway, though. */
  156. break;
  157. }
  158. }
  159. }
  160. void pdb_prlrx_run(struct pdb_config *cfg)
  161. {
  162. cfg->prl.rx_thread = chThdCreateStatic(cfg->prl._rx_wa,
  163. sizeof(cfg->prl._rx_wa), PDB_PRIO_PRL, ProtocolRX, cfg);
  164. }