Teensy 4.1 core updated for C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

93 lines
3.0KB

  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. uint8_t nvic_priority;
  42. ISR myISR;
  43. bool beginCycles(ISR newISR, uint32_t cycles);
  44. public:
  45. IntervalTimer() { status = TIMER_OFF; nvic_priority = 128; }
  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. void priority(uint8_t n) {
  73. nvic_priority = n;
  74. if (PIT_enabled) NVIC_SET_PRIORITY(IRQ_PIT_CH, n);
  75. }
  76. static ISR PIT_ISR[NUM_PIT];
  77. };
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. #endif