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

преди 11 години
преди 10 години
преди 11 години
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <stdint.h>
  19. #include "mk20dx128.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. class IntervalTimer {
  24. private:
  25. typedef void (*ISR)();
  26. typedef volatile uint32_t* reg;
  27. enum {TIMER_OFF, TIMER_PIT};
  28. static const uint8_t NUM_PIT = 4;
  29. static const uint32_t MAX_PERIOD = UINT32_MAX / (F_BUS / 1000000.0);
  30. static void enable_PIT();
  31. static void disable_PIT();
  32. static bool PIT_enabled;
  33. static bool PIT_used[NUM_PIT];
  34. bool allocate_PIT(uint32_t newValue);
  35. void start_PIT(uint32_t newValue);
  36. void stop_PIT();
  37. bool status;
  38. uint8_t PIT_id;
  39. reg PIT_LDVAL;
  40. reg PIT_TCTRL;
  41. uint8_t IRQ_PIT_CH;
  42. ISR myISR;
  43. bool beginCycles(ISR newISR, uint32_t cycles);
  44. public:
  45. IntervalTimer() { status = TIMER_OFF; }
  46. ~IntervalTimer() { end(); }
  47. bool begin(ISR newISR, unsigned int newPeriod) {
  48. if (newPeriod == 0 || newPeriod > MAX_PERIOD) return false;
  49. uint32_t newValue = (F_BUS / 1000000) * newPeriod - 1;
  50. return beginCycles(newISR, newValue);
  51. }
  52. bool begin(ISR newISR, int newPeriod) {
  53. if (newPeriod < 0) return false;
  54. return begin(newISR, (unsigned int)newPeriod);
  55. }
  56. bool begin(ISR newISR, unsigned long newPeriod) {
  57. return begin(newISR, (unsigned int)newPeriod);
  58. }
  59. bool begin(ISR newISR, long newPeriod) {
  60. return begin(newISR, (int)newPeriod);
  61. }
  62. bool begin(ISR newISR, float newPeriod) {
  63. if (newPeriod <= 0 || newPeriod > MAX_PERIOD) return false;
  64. uint32_t newValue = (float)(F_BUS / 1000000) * newPeriod + 0.5;
  65. if (newValue < 40) return false;
  66. return beginCycles(newISR, newValue);
  67. }
  68. bool begin(ISR newISR, double newPeriod) {
  69. return begin(newISR, (float)newPeriod);
  70. }
  71. void end();
  72. static ISR PIT_ISR[NUM_PIT];
  73. };
  74. #ifdef __cplusplus
  75. }
  76. #endif
  77. #endif