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

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