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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. thread_t *pdb_prlrx_thread;
  27. /*
  28. * Protocol RX machine states
  29. *
  30. * There is no Send_GoodCRC state because the PHY sends the GoodCRC for us.
  31. * All transitions that would go to that state instead go to Check_MessageID.
  32. */
  33. enum protocol_rx_state {
  34. PRLRxWaitPHY,
  35. PRLRxReset,
  36. PRLRxCheckMessageID,
  37. PRLRxStoreMessageID
  38. };
  39. /* The received message we're currently working with */
  40. static union pd_msg *protocol_rx_message = NULL;
  41. /* The ID of the last received message */
  42. int8_t pdb_prlrx_messageid = -1;
  43. /*
  44. * PRL_Rx_Wait_for_PHY_Message state
  45. */
  46. static enum protocol_rx_state protocol_rx_wait_phy(void)
  47. {
  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(void)
  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(pdb_prltx_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(void)
  98. {
  99. /* If we got a RESET signal, reset the machine */
  100. if (chEvtGetAndClearEvents(PDB_EVT_PRLRX_RESET) != 0) {
  101. chPoolFree(&pdb_msg_pool, protocol_rx_message);
  102. protocol_rx_message = NULL;
  103. return PRLRxWaitPHY;
  104. }
  105. /* If the message has the stored ID, we've seen this message before. Free
  106. * it and don't pass it to the policy engine. */
  107. if (PD_MESSAGEID_GET(protocol_rx_message) == pdb_prlrx_messageid) {
  108. chPoolFree(&pdb_msg_pool, protocol_rx_message);
  109. protocol_rx_message = NULL;
  110. return PRLRxWaitPHY;
  111. /* Otherwise, there's either no stored ID or this message has an ID we
  112. * haven't just seen. Transition to the Store_MessageID state. */
  113. } else {
  114. return PRLRxStoreMessageID;
  115. }
  116. }
  117. /*
  118. * PRL_Rx_Store_MessageID state
  119. */
  120. static enum protocol_rx_state protocol_rx_store_messageid(void)
  121. {
  122. /* Tell ProtocolTX to discard the message being transmitted */
  123. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_DISCARD);
  124. chThdYield();
  125. /* Update the stored MessageID */
  126. pdb_prlrx_messageid = PD_MESSAGEID_GET(protocol_rx_message);
  127. /* Pass the message to the policy engine. */
  128. chMBPost(&pdb_pe_mailbox, (msg_t) protocol_rx_message, TIME_IMMEDIATE);
  129. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_MSG_RX);
  130. /* Don't check if we got a RESET because we'd do nothing different. */
  131. return PRLRxWaitPHY;
  132. }
  133. /*
  134. * Protocol layer RX state machine thread
  135. */
  136. static THD_WORKING_AREA(waProtocolRX, 128);
  137. static THD_FUNCTION(ProtocolRX, arg) {
  138. (void) arg;
  139. enum protocol_rx_state state = PRLRxWaitPHY;
  140. while (true) {
  141. switch (state) {
  142. case PRLRxWaitPHY:
  143. state = protocol_rx_wait_phy();
  144. break;
  145. case PRLRxReset:
  146. state = protocol_rx_reset();
  147. break;
  148. case PRLRxCheckMessageID:
  149. state = protocol_rx_check_messageid();
  150. break;
  151. case PRLRxStoreMessageID:
  152. state = protocol_rx_store_messageid();
  153. break;
  154. default:
  155. /* This is an error. It really shouldn't happen. We might
  156. * want to handle it anyway, though. */
  157. break;
  158. }
  159. }
  160. }
  161. void pdb_prlrx_run(void)
  162. {
  163. pdb_prlrx_thread = chThdCreateStatic(waProtocolRX, sizeof(waProtocolRX),
  164. PDB_PRIO_PRL, ProtocolRX, NULL);
  165. }