Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

105 lines
3.3KB

  1. /* Copyright (c) 2013 Daniel Gilbert, loglow@gmail.com
  2. Permission is hereby granted, free of charge, to any person obtaining a copy of
  3. this software and associated documentation files (the "Software"), to deal in the
  4. Software without restriction, including without limitation the rights to use, copy,
  5. modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  6. and to permit persons to whom the Software is furnished to do so, subject to the
  7. following conditions:
  8. The above copyright notice and this permission notice shall be included in all
  9. copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  11. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  12. PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  13. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  14. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  15. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  16. #ifndef __INTERVALTIMER_H__
  17. #define __INTERVALTIMER_H__
  18. #include "kinetis.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. class IntervalTimer {
  23. private:
  24. typedef void (*ISR)();
  25. typedef volatile uint32_t* reg;
  26. enum {TIMER_OFF, TIMER_PIT};
  27. #if defined(KINETISK)
  28. static const uint8_t NUM_PIT = 4;
  29. #elif defined(KINETISL)
  30. static const uint8_t NUM_PIT = 2;
  31. #endif
  32. static const uint32_t MAX_PERIOD = UINT32_MAX / (F_BUS / 1000000.0);
  33. static void enable_PIT();
  34. static void disable_PIT();
  35. static bool PIT_enabled;
  36. static bool PIT_used[NUM_PIT];
  37. static ISR PIT_ISR[NUM_PIT];
  38. bool allocate_PIT(uint32_t newValue);
  39. void start_PIT(uint32_t newValue);
  40. void stop_PIT();
  41. bool status;
  42. uint8_t PIT_id;
  43. reg PIT_LDVAL;
  44. reg PIT_TCTRL;
  45. uint8_t IRQ_PIT_CH;
  46. uint8_t nvic_priority;
  47. ISR myISR;
  48. bool beginCycles(ISR newISR, uint32_t cycles);
  49. public:
  50. IntervalTimer() { status = TIMER_OFF; nvic_priority = 128; }
  51. ~IntervalTimer() { end(); }
  52. bool begin(ISR newISR, unsigned int newPeriod) {
  53. if (newPeriod == 0 || newPeriod > MAX_PERIOD) return false;
  54. uint32_t newValue = (F_BUS / 1000000) * newPeriod - 1;
  55. return beginCycles(newISR, newValue);
  56. }
  57. bool begin(ISR newISR, int newPeriod) {
  58. if (newPeriod < 0) return false;
  59. return begin(newISR, (unsigned int)newPeriod);
  60. }
  61. bool begin(ISR newISR, unsigned long newPeriod) {
  62. return begin(newISR, (unsigned int)newPeriod);
  63. }
  64. bool begin(ISR newISR, long newPeriod) {
  65. return begin(newISR, (int)newPeriod);
  66. }
  67. bool begin(ISR newISR, float newPeriod) {
  68. if (newPeriod <= 0 || newPeriod > MAX_PERIOD) return false;
  69. uint32_t newValue = (float)(F_BUS / 1000000) * newPeriod + 0.5;
  70. if (newValue < 40) return false;
  71. return beginCycles(newISR, newValue);
  72. }
  73. bool begin(ISR newISR, double newPeriod) {
  74. return begin(newISR, (float)newPeriod);
  75. }
  76. void end();
  77. void priority(uint8_t n) {
  78. nvic_priority = n;
  79. if (PIT_enabled) NVIC_SET_PRIORITY(IRQ_PIT_CH, n);
  80. }
  81. #if defined(KINETISK)
  82. friend void pit0_isr();
  83. friend void pit1_isr();
  84. friend void pit2_isr();
  85. friend void pit3_isr();
  86. #elif defined(KINETISL)
  87. friend void pit_isr();
  88. #endif
  89. };
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #endif