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

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