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.

int_n.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_FUNCTION(IntNPoll, cfg) {
  31. (void) cfg;
  32. union fusb_status status;
  33. eventmask_t events;
  34. while (true) {
  35. /* If the INT_N line is low */
  36. if (palReadLine(LINE_INT_N) == PAL_LOW) {
  37. /* Read the FUSB302B status and interrupt registers */
  38. fusb_get_status(&status);
  39. /* If the I_GCRCSENT flag is set, tell the Protocol RX thread */
  40. if (status.interruptb & FUSB_INTERRUPTB_I_GCRCSENT) {
  41. chEvtSignal(pdb_prlrx_thread, PDB_EVT_PRLRX_I_GCRCSENT);
  42. }
  43. /* If the I_TXSENT or I_RETRYFAIL flag is set, tell the Protocol TX
  44. * thread */
  45. events = 0;
  46. if (status.interrupta & FUSB_INTERRUPTA_I_RETRYFAIL) {
  47. events |= PDB_EVT_PRLTX_I_RETRYFAIL;
  48. }
  49. if (status.interrupta & FUSB_INTERRUPTA_I_TXSENT) {
  50. events |= PDB_EVT_PRLTX_I_TXSENT;
  51. }
  52. chEvtSignal(pdb_prltx_thread, events);
  53. /* If the I_HARDRST or I_HARDSENT flag is set, tell the Hard Reset
  54. * thread */
  55. events = 0;
  56. if (status.interrupta & FUSB_INTERRUPTA_I_HARDRST) {
  57. events |= PDB_EVT_HARDRST_I_HARDRST;
  58. }
  59. if (status.interrupta & FUSB_INTERRUPTA_I_HARDSENT) {
  60. events |= PDB_EVT_HARDRST_I_HARDSENT;
  61. }
  62. chEvtSignal(pdb_hardrst_thread, events);
  63. /* If the I_OCP_TEMP and OVRTEMP flags are set, tell the Policy
  64. * Engine thread */
  65. if (status.interrupta & FUSB_INTERRUPTA_I_OCP_TEMP
  66. && status.status1 & FUSB_STATUS1_OVRTEMP) {
  67. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_I_OVRTEMP);
  68. }
  69. }
  70. chThdSleepMilliseconds(1);
  71. }
  72. }
  73. void pdb_int_n_run(struct pdb_config *cfg)
  74. {
  75. cfg->int_n.thread = chThdCreateStatic(cfg->int_n._wa,
  76. sizeof(cfg->int_n._wa), PDB_PRIO_PRL_INT_N, IntNPoll, cfg);
  77. }