PIT.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "PIT.h"
  2. #include "../TF_Handler.h"
  3. namespace
  4. {
  5. TF_Handler* Handler[4];
  6. template<int n>
  7. void dispatchFunc()
  8. {
  9. Handler[n]->stepTimerISR();
  10. }
  11. constexpr void(*dispatcher[])(void) =
  12. {
  13. dispatchFunc<0>,
  14. dispatchFunc<1>,
  15. dispatchFunc<2>,
  16. dispatchFunc<3>
  17. };
  18. void dummyISR(void) {}
  19. }
  20. bool PIT::begin(TF_Handler* handler)
  21. {
  22. if (!timer.begin(dummyISR, 1E6)) return false; // try to reserve a timer
  23. setupChannel(); // find pit channel of reserved timer
  24. const int channelNr = channel - KINETISK_PIT_CHANNELS;
  25. Handler[channelNr] = handler; // store handler
  26. timer.priority(32);
  27. timer.begin(dispatcher[channelNr], 1E6); // attach an ISR which will call the stored handler
  28. stop(); // stop doesn't clear TEN, we want to keep the IntervalTimer reserved
  29. return true;
  30. }
  31. void PIT::setupChannel()
  32. {
  33. IRQ_NUMBER_t number = (IRQ_NUMBER_t)timer;
  34. switch (number)
  35. {
  36. case IRQ_PIT_CH0:
  37. channel = KINETISK_PIT_CHANNELS + 0;
  38. break;
  39. case IRQ_PIT_CH1:
  40. channel = KINETISK_PIT_CHANNELS + 1;
  41. break;
  42. case IRQ_PIT_CH2:
  43. channel = KINETISK_PIT_CHANNELS + 2;
  44. break;
  45. case IRQ_PIT_CH3:
  46. channel = KINETISK_PIT_CHANNELS + 3;
  47. break;
  48. default:
  49. channel = nullptr;
  50. break;
  51. }
  52. }