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 Firmware Library - USB Power Delivery for everyone
  3. * Copyright 2017-2018 Clayton G. Hobbs
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "int_n.h"
  18. #include <ch.h>
  19. #include <hal.h>
  20. #include <pdb.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, vcfg) {
  31. struct pdb_config *cfg = vcfg;
  32. union fusb_status status;
  33. eventmask_t events;
  34. while (true) {
  35. /* If the INT_N line is low */
  36. if (palReadLine(cfg->fusb.int_n) == PAL_LOW) {
  37. /* Read the FUSB302B status and interrupt registers */
  38. fusb_get_status(&cfg->fusb, &status);
  39. /* If the I_GCRCSENT flag is set, tell the Protocol RX thread */
  40. if (status.interruptb & FUSB_INTERRUPTB_I_GCRCSENT) {
  41. chEvtSignal(cfg->prl.rx_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(cfg->prl.tx_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(cfg->prl.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(cfg->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. }