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.

hard_reset.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "hard_reset.h"
  19. #include "priorities.h"
  20. #include "policy_engine.h"
  21. #include "protocol_rx.h"
  22. #include "protocol_tx.h"
  23. #include "fusb302b.h"
  24. #include "pd.h"
  25. /*
  26. * Hard Reset machine states
  27. */
  28. enum hardrst_state {
  29. PRLHRResetLayer,
  30. PRLHRIndicateHardReset,
  31. PRLHRRequestHardReset,
  32. PRLHRWaitPHY,
  33. PRLHRHardResetRequested,
  34. PRLHRWaitPE,
  35. PRLHRComplete
  36. };
  37. /*
  38. * PRL_HR_Reset_Layer state
  39. */
  40. static enum hardrst_state hardrst_reset_layer(struct pdb_config *cfg)
  41. {
  42. /* First, wait for the signal to run a hard reset. */
  43. eventmask_t evt = chEvtWaitAny(PDB_EVT_HARDRST_RESET
  44. | PDB_EVT_HARDRST_I_HARDRST);
  45. /* Reset the stored message IDs */
  46. pdb_prlrx_messageid = 0;
  47. pdb_prltx_messageidcounter = 0;
  48. /* Reset the Protocol RX machine */
  49. chEvtSignal(cfg->prl.rx_thread, PDB_EVT_PRLRX_RESET);
  50. chThdYield();
  51. /* Reset the Protocol TX machine */
  52. chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_RESET);
  53. chThdYield();
  54. /* Continue the process based on what event started the reset. */
  55. if (evt & PDB_EVT_HARDRST_RESET) {
  56. /* Policy Engine started the reset. */
  57. return PRLHRRequestHardReset;
  58. } else {
  59. /* PHY started the reset */
  60. return PRLHRIndicateHardReset;
  61. }
  62. }
  63. static enum hardrst_state hardrst_indicate_hard_reset(struct pdb_config *cfg)
  64. {
  65. (void) cfg;
  66. /* Tell the PE that we're doing a hard reset */
  67. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_RESET);
  68. return PRLHRWaitPE;
  69. }
  70. static enum hardrst_state hardrst_request_hard_reset(struct pdb_config *cfg)
  71. {
  72. (void) cfg;
  73. /* Tell the PHY to send a hard reset */
  74. fusb_send_hardrst();
  75. return PRLHRWaitPHY;
  76. }
  77. static enum hardrst_state hardrst_wait_phy(struct pdb_config *cfg)
  78. {
  79. (void) cfg;
  80. /* Wait for the PHY to tell us that it's done sending the hard reset */
  81. chEvtWaitAnyTimeout(PDB_EVT_HARDRST_I_HARDSENT, PD_T_HARD_RESET_COMPLETE);
  82. /* Move on no matter what made us stop waiting. */
  83. return PRLHRHardResetRequested;
  84. }
  85. static enum hardrst_state hardrst_hard_reset_requested(struct pdb_config *cfg)
  86. {
  87. (void) cfg;
  88. /* Tell the PE that the hard reset was sent */
  89. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_HARD_SENT);
  90. return PRLHRWaitPE;
  91. }
  92. static enum hardrst_state hardrst_wait_pe(struct pdb_config *cfg)
  93. {
  94. (void) cfg;
  95. /* Wait for the PE to tell us that it's done */
  96. chEvtWaitAny(PDB_EVT_HARDRST_DONE);
  97. return PRLHRComplete;
  98. }
  99. static enum hardrst_state hardrst_complete(struct pdb_config *cfg)
  100. {
  101. (void) cfg;
  102. /* I'm not aware of anything we have to tell the FUSB302B, so just finish
  103. * the reset routine. */
  104. return PRLHRResetLayer;
  105. }
  106. /*
  107. * Hard Reset state machine thread
  108. */
  109. static THD_FUNCTION(HardReset, cfg) {
  110. enum hardrst_state state = PRLHRResetLayer;
  111. while (true) {
  112. switch (state) {
  113. case PRLHRResetLayer:
  114. state = hardrst_reset_layer(cfg);
  115. break;
  116. case PRLHRIndicateHardReset:
  117. state = hardrst_indicate_hard_reset(cfg);
  118. break;
  119. case PRLHRRequestHardReset:
  120. state = hardrst_request_hard_reset(cfg);
  121. break;
  122. case PRLHRWaitPHY:
  123. state = hardrst_wait_phy(cfg);
  124. break;
  125. case PRLHRHardResetRequested:
  126. state = hardrst_hard_reset_requested(cfg);
  127. break;
  128. case PRLHRWaitPE:
  129. state = hardrst_wait_pe(cfg);
  130. break;
  131. case PRLHRComplete:
  132. state = hardrst_complete(cfg);
  133. break;
  134. default:
  135. /* This is an error. It really shouldn't happen. We might
  136. * want to handle it anyway, though. */
  137. break;
  138. }
  139. }
  140. }
  141. void pdb_hardrst_run(struct pdb_config *cfg)
  142. {
  143. cfg->prl.hardrst_thread = chThdCreateStatic(cfg->prl._hardrst_wa,
  144. sizeof(cfg->prl._hardrst_wa), PDB_PRIO_PRL, HardReset, cfg);
  145. }