PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

124 行
3.8KB

  1. /* PulsePosition Library for Teensy 3.1
  2. * High resolution input and output of PPM encoded signals
  3. * http://www.pjrc.com/teensy/td_libs_PulsePosition.html
  4. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  5. *
  6. * Development of this library was funded by PJRC.COM, LLC by sales of Teensy
  7. * boards. Please support PJRC's efforts to develop open source software by
  8. * purchasing Teensy or other PJRC products.
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice, development funding notice, and this permission
  18. * notice shall be included in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #ifndef __PULSE_POSITION_IMXRT_H__
  29. #define __PULSE_POSITION_IMXRT_H__
  30. #if defined(__IMXRT1062__)
  31. #include <Arduino.h>
  32. #define PULSEPOSITION_MAXCHANNELS 16
  33. class PulsePositionBase
  34. {
  35. public:
  36. protected:
  37. static PulsePositionBase *list[10];
  38. virtual void isr() = 0;
  39. typedef struct {
  40. uint8_t pin;
  41. uint8_t channel;
  42. volatile IMXRT_TMR_t* tmr;
  43. volatile uint32_t *clock_gate_register;
  44. uint32_t clock_gate_mask;
  45. IRQ_NUMBER_t interrupt;
  46. void (*isr)();
  47. volatile uint32_t *select_input_register; // Which register controls the selection
  48. const uint32_t select_val; // Value for that selection
  49. } TMR_Hardware_t;
  50. static const TMR_Hardware_t hardware[];
  51. static const uint8_t _hardware_count;
  52. // static class functions
  53. static void isrTimer1();
  54. static void isrTimer2();
  55. static void isrTimer3();
  56. static void isrTimer4();
  57. static inline void checkAndProcessTimerCHInPending(uint8_t index, volatile IMXRT_TMR_CH_t *tmr_ch);
  58. };
  59. class PulsePositionOutput : public PulsePositionBase
  60. {
  61. public:
  62. PulsePositionOutput(void);
  63. PulsePositionOutput(int polarity);
  64. bool begin(uint8_t txPin); // txPin can be 6,9,10,11,12,13,14,15,18,19
  65. bool begin(uint8_t txPin, uint32_t _framePin);
  66. bool write(uint8_t channel, float microseconds);
  67. private:
  68. uint8_t outPolarity = 1; // Polarity rising
  69. uint8_t inPolarity = 1;
  70. volatile uint8_t framePinMask;
  71. uint32_t state, total_channels, total_channels_buffer, pulse_remaining,
  72. current_channel, framePin = 255;
  73. volatile uint32_t ticks;
  74. uint32_t pulse_width[PULSEPOSITION_MAXCHANNELS + 1];
  75. uint32_t pulse_buffer[PULSEPOSITION_MAXCHANNELS + 1];
  76. // member variables...
  77. uint16_t idx_channel;
  78. virtual void isr();
  79. };
  80. class PulsePositionInput : public PulsePositionBase
  81. {
  82. public:
  83. PulsePositionInput(void);
  84. PulsePositionInput(int polarity);
  85. bool begin(uint8_t rxPin); // rxPin can be 6,9,10,11,12,13,14,15,18,19
  86. int available(void);
  87. float read(uint8_t channel);
  88. private:
  89. uint32_t pulse_width[PULSEPOSITION_MAXCHANNELS+1];
  90. uint32_t pulse_buffer[PULSEPOSITION_MAXCHANNELS+1];
  91. uint32_t prev;
  92. uint8_t write_index;
  93. uint8_t total_channels;
  94. uint8_t outPolarity = 1; // Polarity rising
  95. uint8_t inPolarity = 1;
  96. volatile uint32_t ticks, overflow_count;
  97. volatile bool overflow_inc, available_flag;
  98. static uint8_t channelmask;
  99. // member variables...
  100. uint16_t idx_channel;
  101. virtual void isr();
  102. };
  103. #endif
  104. #endif