Teensy 4.1 core updated for C++20
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.

88 lines
2.8KB

  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. static const uint8_t NUM_PIT = 4;
  28. static const uint32_t MAX_PERIOD = UINT32_MAX / (F_BUS / 1000000.0);
  29. static void enable_PIT();
  30. static void disable_PIT();
  31. static bool PIT_enabled;
  32. static bool PIT_used[NUM_PIT];
  33. bool allocate_PIT(uint32_t newValue);
  34. void start_PIT(uint32_t newValue);
  35. void stop_PIT();
  36. bool status;
  37. uint8_t PIT_id;
  38. reg PIT_LDVAL;
  39. reg PIT_TCTRL;
  40. uint8_t IRQ_PIT_CH;
  41. ISR myISR;
  42. bool beginCycles(ISR newISR, uint32_t cycles);
  43. public:
  44. IntervalTimer() { status = TIMER_OFF; }
  45. ~IntervalTimer() { end(); }
  46. bool begin(ISR newISR, unsigned int newPeriod) {
  47. if (newPeriod == 0 || newPeriod > MAX_PERIOD) return false;
  48. uint32_t newValue = (F_BUS / 1000000) * newPeriod - 1;
  49. return beginCycles(newISR, newValue);
  50. }
  51. bool begin(ISR newISR, int newPeriod) {
  52. if (newPeriod < 0) return false;
  53. return begin(newISR, (unsigned int)newPeriod);
  54. }
  55. bool begin(ISR newISR, unsigned long newPeriod) {
  56. return begin(newISR, (unsigned int)newPeriod);
  57. }
  58. bool begin(ISR newISR, long newPeriod) {
  59. return begin(newISR, (int)newPeriod);
  60. }
  61. bool begin(ISR newISR, float newPeriod) {
  62. if (newPeriod <= 0 || newPeriod > MAX_PERIOD) return false;
  63. uint32_t newValue = (float)(F_BUS / 1000000) * newPeriod + 0.5;
  64. if (newValue < 40) return false;
  65. return beginCycles(newISR, newValue);
  66. }
  67. bool begin(ISR newISR, double newPeriod) {
  68. return begin(newISR, (float)newPeriod);
  69. }
  70. void end();
  71. static ISR PIT_ISR[NUM_PIT];
  72. };
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76. #endif