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

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