PD Buddy Sink Firmware
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

int_n.c 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "int_n.h"
  19. #include <ch.h>
  20. #include <hal.h>
  21. #include "priorities.h"
  22. #include "fusb302b.h"
  23. #include "protocol_rx.h"
  24. #include "protocol_tx.h"
  25. #include "hard_reset.h"
  26. #include "policy_engine.h"
  27. /*
  28. * INT_N polling thread
  29. */
  30. static THD_WORKING_AREA(waIntNPoll, 128);
  31. static THD_FUNCTION(IntNPoll, arg) {
  32. (void) arg;
  33. union fusb_status status;
  34. eventmask_t events;
  35. while (true) {
  36. /* If the INT_N line is low */
  37. if (palReadLine(LINE_INT_N) == PAL_LOW) {
  38. /* Read the FUSB302B status and interrupt registers */
  39. fusb_get_status(&status);
  40. /* If the I_GCRCSENT flag is set, tell the Protocol RX thread */
  41. if (status.interruptb & FUSB_INTERRUPTB_I_GCRCSENT) {
  42. chEvtSignal(pdb_prlrx_thread, PDB_EVT_PRLRX_I_GCRCSENT);
  43. }
  44. /* If the I_TXSENT or I_RETRYFAIL flag is set, tell the Protocol TX
  45. * thread */
  46. events = 0;
  47. if (status.interrupta & FUSB_INTERRUPTA_I_RETRYFAIL) {
  48. events |= PDB_EVT_PRLTX_I_RETRYFAIL;
  49. }
  50. if (status.interrupta & FUSB_INTERRUPTA_I_TXSENT) {
  51. events |= PDB_EVT_PRLTX_I_TXSENT;
  52. }
  53. chEvtSignal(pdb_prltx_thread, events);
  54. /* If the I_HARDRST or I_HARDSENT flag is set, tell the Hard Reset
  55. * thread */
  56. events = 0;
  57. if (status.interrupta & FUSB_INTERRUPTA_I_HARDRST) {
  58. events |= PDB_EVT_HARDRST_I_HARDRST;
  59. }
  60. if (status.interrupta & FUSB_INTERRUPTA_I_HARDSENT) {
  61. events |= PDB_EVT_HARDRST_I_HARDSENT;
  62. }
  63. chEvtSignal(pdb_hardrst_thread, events);
  64. /* If the I_OCP_TEMP and OVRTEMP flags are set, tell the Policy
  65. * Engine thread */
  66. if (status.interrupta & FUSB_INTERRUPTA_I_OCP_TEMP
  67. && status.status1 & FUSB_STATUS1_OVRTEMP) {
  68. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_I_OVRTEMP);
  69. }
  70. }
  71. chThdSleepMilliseconds(1);
  72. }
  73. }
  74. void pdb_int_n_run(void)
  75. {
  76. chThdCreateStatic(waIntNPoll, sizeof(waIntNPoll), PDB_PRIO_PRL_INT_N,
  77. IntNPoll, NULL);
  78. }