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

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