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

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