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 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "int_n.h"
  19. #include <ch.h>
  20. #include <hal.h>
  21. #include <pdb.h>
  22. #include "priorities.h"
  23. #include "fusb302b.h"
  24. #include "protocol_rx.h"
  25. #include "protocol_tx.h"
  26. #include "hard_reset.h"
  27. #include "policy_engine.h"
  28. /*
  29. * INT_N polling thread
  30. */
  31. static THD_FUNCTION(IntNPoll, vcfg) {
  32. struct pdb_config *cfg = vcfg;
  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(&cfg->fusb, &status);
  40. /* If the I_GCRCSENT flag is set, tell the Protocol RX thread */
  41. if (status.interruptb & FUSB_INTERRUPTB_I_GCRCSENT) {
  42. chEvtSignal(cfg->prl.rx_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(cfg->prl.tx_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(cfg->prl.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(cfg->pe.thread, PDB_EVT_PE_I_OVRTEMP);
  69. }
  70. }
  71. chThdSleepMilliseconds(1);
  72. }
  73. }
  74. void pdb_int_n_run(struct pdb_config *cfg)
  75. {
  76. cfg->int_n.thread = chThdCreateStatic(cfg->int_n._wa,
  77. sizeof(cfg->int_n._wa), PDB_PRIO_PRL_INT_N, IntNPoll, cfg);
  78. }