PlatformIO package of the Teensy core framework compatible with GCC 10 & 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.

71 lines
2.0KB

  1. /*
  2. || @author Paul Stoffregen (paul at pjrc dot com)
  3. || @contribution Brett Hagman <bhagman@wiring.org.co>
  4. || @url http://wiring.org.co/
  5. ||
  6. || @description
  7. || | A Software PWM Library
  8. || |
  9. || | Simple timer abstractions.
  10. || #
  11. ||
  12. || @license Please see the accompanying LICENSE.txt file for this project.
  13. ||
  14. || @name Software PWM Library support
  15. || @type Library support
  16. || @target Atmel AVR 8 Bit
  17. ||
  18. || @version 1.0.0
  19. ||
  20. */
  21. #include <avr/io.h>
  22. #include <avr/interrupt.h>
  23. // allow certain chips to use different timers
  24. #if defined(__AVR_ATmega32U4__)
  25. #define USE_TIMER4_HS // Teensy 2.0 lacks timer2, but has high speed timer4 :-)
  26. #elif defined(__arm__) && defined(TEENSYDUINO)
  27. #define USE_INTERVALTIMER // Teensy 3.x has special interval timers :-)
  28. #else
  29. #define USE_TIMER2
  30. #endif
  31. // for each timer, these macros define how to actually use it
  32. #if defined(USE_TIMER2)
  33. #define SOFTPWM_TIMER_INTERRUPT TIMER2_COMPA_vect
  34. #define SOFTPWM_TIMER_SET(val) (TCNT2 = (val))
  35. #define SOFTPWM_TIMER_INIT(ocr) ({\
  36. TIFR2 = (1 << TOV2); /* clear interrupt flag */ \
  37. TCCR2B = (1 << CS21); /* start timer (ck/8 prescalar) */ \
  38. TCCR2A = (1 << WGM21); /* CTC mode */ \
  39. OCR2A = (ocr); /* We want to have at least 30Hz or else it gets choppy */ \
  40. TIMSK2 = (1 << OCIE2A); /* enable timer2 output compare match interrupt */ \
  41. })
  42. #elif defined(USE_TIMER4_HS)
  43. #define SOFTPWM_TIMER_INTERRUPT TIMER4_COMPA_vect
  44. #define SOFTPWM_TIMER_SET(val) (TCNT4 = (val))
  45. #define SOFTPWM_TIMER_INIT(ocr) ({\
  46. TCCR4A = 0; \
  47. TCCR4B = 0x04; /* CTC Mode */\
  48. TCCR4C = 0; \
  49. TCCR4D = 0; \
  50. TCCR4E = 0; \
  51. OCR4C = 0; \
  52. OCR4C = (ocr); \
  53. TIMSK4 = (1 << OCIE4A); \
  54. })
  55. #elif defined(USE_INTERVALTIMER)
  56. #define SOFTPWM_TIMER_INTERRUPT softpwm_interval_timer
  57. #ifdef ISR
  58. #undef ISR
  59. #endif
  60. #define ISR(f) void f(void)
  61. #define SOFTPWM_TIMER_SET(val)
  62. #define SOFTPWM_TIMER_INIT(ocr) ({\
  63. IntervalTimer *t = new IntervalTimer(); \
  64. t->begin(softpwm_interval_timer, 1000000.0 / (float)(SOFTPWM_FREQ * 256)); \
  65. })
  66. #endif