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

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