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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "hard_reset.h"
  19. #include <pd.h>
  20. #include "priorities.h"
  21. #include "policy_engine.h"
  22. #include "protocol_rx.h"
  23. #include "protocol_tx.h"
  24. #include "fusb302b.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. cfg->prl._rx_messageid = 0;
  47. cfg->prl._tx_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. /* Tell the PE that we're doing a hard reset */
  66. chEvtSignal(cfg->pe.thread, PDB_EVT_PE_RESET);
  67. return PRLHRWaitPE;
  68. }
  69. static enum hardrst_state hardrst_request_hard_reset(struct pdb_config *cfg)
  70. {
  71. (void) cfg;
  72. /* Tell the PHY to send a hard reset */
  73. fusb_send_hardrst(&cfg->fusb);
  74. return PRLHRWaitPHY;
  75. }
  76. static enum hardrst_state hardrst_wait_phy(struct pdb_config *cfg)
  77. {
  78. (void) cfg;
  79. /* Wait for the PHY to tell us that it's done sending the hard reset */
  80. chEvtWaitAnyTimeout(PDB_EVT_HARDRST_I_HARDSENT, PD_T_HARD_RESET_COMPLETE);
  81. /* Move on no matter what made us stop waiting. */
  82. return PRLHRHardResetRequested;
  83. }
  84. static enum hardrst_state hardrst_hard_reset_requested(struct pdb_config *cfg)
  85. {
  86. /* Tell the PE that the hard reset was sent */
  87. chEvtSignal(cfg->pe.thread, PDB_EVT_PE_HARD_SENT);
  88. return PRLHRWaitPE;
  89. }
  90. static enum hardrst_state hardrst_wait_pe(struct pdb_config *cfg)
  91. {
  92. (void) cfg;
  93. /* Wait for the PE to tell us that it's done */
  94. chEvtWaitAny(PDB_EVT_HARDRST_DONE);
  95. return PRLHRComplete;
  96. }
  97. static enum hardrst_state hardrst_complete(struct pdb_config *cfg)
  98. {
  99. (void) cfg;
  100. /* I'm not aware of anything we have to tell the FUSB302B, so just finish
  101. * the reset routine. */
  102. return PRLHRResetLayer;
  103. }
  104. /*
  105. * Hard Reset state machine thread
  106. */
  107. static THD_FUNCTION(HardReset, cfg) {
  108. enum hardrst_state state = PRLHRResetLayer;
  109. while (true) {
  110. switch (state) {
  111. case PRLHRResetLayer:
  112. state = hardrst_reset_layer(cfg);
  113. break;
  114. case PRLHRIndicateHardReset:
  115. state = hardrst_indicate_hard_reset(cfg);
  116. break;
  117. case PRLHRRequestHardReset:
  118. state = hardrst_request_hard_reset(cfg);
  119. break;
  120. case PRLHRWaitPHY:
  121. state = hardrst_wait_phy(cfg);
  122. break;
  123. case PRLHRHardResetRequested:
  124. state = hardrst_hard_reset_requested(cfg);
  125. break;
  126. case PRLHRWaitPE:
  127. state = hardrst_wait_pe(cfg);
  128. break;
  129. case PRLHRComplete:
  130. state = hardrst_complete(cfg);
  131. break;
  132. default:
  133. /* This is an error. It really shouldn't happen. We might
  134. * want to handle it anyway, though. */
  135. break;
  136. }
  137. }
  138. }
  139. void pdb_hardrst_run(struct pdb_config *cfg)
  140. {
  141. cfg->prl.hardrst_thread = chThdCreateStatic(cfg->prl._hardrst_wa,
  142. sizeof(cfg->prl._hardrst_wa), PDB_PRIO_PRL, HardReset, cfg);
  143. }