PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

224 Zeilen
7.8KB

  1. #ifndef __QUADENCODER_H__
  2. #define __QUADENCODER_H__
  3. #ifndef __IMXRT1062__
  4. #error "Sorry, Quad Encoder only works on Teensy 4.x boards"
  5. #endif
  6. #include <Arduino.h>
  7. #define ENC_CTRL_HIRQ_MASK (0x8000U)
  8. #define ENC_CTRL_XIRQ_MASK (0x100U)
  9. #define ENC_CTRL_DIRQ_MASK (0x10U)
  10. #define ENC_CTRL_CMPIRQ_MASK (0x2U)
  11. #define ENC_CTRL_HIP_MASK (0x2000U)
  12. #define ENC_CTRL_HNE_MASK (0x1000U)
  13. #define ENC_CTRL_REV_MASK (0x400U)
  14. #define ENC_CTRL_PH1_MASK (0x200U)
  15. #define ENC_CTRL_XIP_MASK (0x40U)
  16. #define ENC_CTRL_XNE_MASK (0x20U)
  17. #define ENC_CTRL_WDE_MASK (0x4U)
  18. #define ENC_CTRL_WDE_MASK (0x4U)
  19. #define ENC_CTRL_SWIP_MASK (0x800U)
  20. //interrupts
  21. #define ENC_CTRL_HIE_MASK (0x4000U)
  22. #define ENC_CTRL_XIE_MASK (0x80U)
  23. #define ENC_CTRL_CMPIE_MASK (0x1U)
  24. #define ENC_CTRL2_SABIRQ_MASK (0x800U)
  25. #define ENC_CTRL2_ROIRQ_MASK (0x80U)
  26. #define ENC_CTRL2_RUIRQ_MASK (0x20U)
  27. #define ENC_CTRL2_OUTCTL_MASK (0x200U)
  28. #define ENC_CTRL2_REVMOD_MASK (0x100U)
  29. #define ENC_CTRL2_MOD_MASK (0x4U)
  30. #define ENC_CTRL2_UPDHLD_MASK (0x1U)
  31. #define ENC_CTRL2_UPDPOS_MASK (0x2U)
  32. //interrupts
  33. //#define ENC_CTRL2_ROIE_MASK (0x80U)
  34. //#define ENC_CTRL2_RUIE_MASK (0x20U)
  35. #define ENC_CTRL_W1C_FLAGS (ENC_CTRL_HIRQ_MASK | ENC_CTRL_XIRQ_MASK | ENC_CTRL_DIRQ_MASK | ENC_CTRL_CMPIRQ_MASK)
  36. #define ENC_CTRL2_W1C_FLAGS (ENC_CTRL2_SABIRQ_MASK | ENC_CTRL2_ROIRQ_MASK | ENC_CTRL2_RUIRQ_MASK)
  37. #define ENC_FILT_FILT_PER(x) (((uint16_t)(((uint16_t)(x)) << 0U)) & 0xFFU)
  38. #define ENC_FILT_FILT_CNT(x) (((uint16_t)(((uint16_t)(x)) << 8U)) & 0x700U)
  39. #define DISABLE 0
  40. #define ENABLE 1
  41. #define FALLING_EDGE 2
  42. #define RISING_EDGE 1
  43. #define PHASEA 1
  44. #define PHASEB 2
  45. #define INDEX 3
  46. #define HOME 4
  47. #define TRIGGER 5
  48. #define PULLUPS 0 //set to 1 if pullups are needed
  49. #define CORE_XIO_PIN0 IOMUXC_XBAR1_IN17_SELECT_INPUT //ALT1
  50. #define CORE_XIO_PIN1 IOMUXC_XBAR1_IN16_SELECT_INPUT //ALT1
  51. #define CORE_XIO_PIN2 IOMUXC_XBAR1_IN06_SELECT_INPUT //ALT3
  52. #define CORE_XIO_PIN3 IOMUXC_XBAR1_IN07_SELECT_INPUT //ALT3
  53. #define CORE_XIO_PIN4 IOMUXC_XBAR1_IN08_SELECT_INPUT //ALT3
  54. #define CORE_XIO_PIN5 IOMUXC_XBAR1_IN17_SELECT_INPUT //ALT3
  55. #define CORE_XIO_PIN7 IOMUXC_XBAR1_IN15_SELECT_INPUT //ALT1
  56. #define CORE_XIO_PIN8 IOMUXC_XBAR1_IN14_SELECT_INPUT //ALT1
  57. #define CORE_XIO_PIN30 IOMUXC_XBAR1_IN23_SELECT_INPUT //ALT1,0
  58. #define CORE_XIO_PIN31 IOMUXC_XBAR1_IN22_SELECT_INPUT //ALT1,0
  59. #define CORE_XIO_PIN33 IOMUXC_XBAR1_IN09_SELECT_INPUT //ALT3,0
  60. #ifdef ARDUINO_TEENSY41
  61. #define CORE_XIO_PIN36 IOMUXC_XBAR1_IN16_SELECT_INPUT
  62. #define CORE_XIO_PIN37 IOMUXC_XBAR1_IN17_SELECT_INPUT
  63. #endif
  64. enum _flags
  65. {
  66. _HOMETransitionFlag = (1 << 0),
  67. _INDEXPulseFlag = (1 << 1),
  68. _positionCompareFlag = (1 << 3),
  69. _positionRollOverFlag = (1 << 5),
  70. _positionRollUnderFlag = (1 << 6),
  71. _lastDirectionFlag = (1 << 7),
  72. };
  73. enum _interrupts
  74. {
  75. _HOMETransitionEnable = (1 << 0),
  76. _INDEXPulseEnable = (1 << 1),
  77. _positionCompareEnable = (1 << 3),
  78. _positionROEnable = (1 << 5),
  79. _positionRUEnable = (1 << 6),
  80. };
  81. class QuadEncoder
  82. {
  83. public:
  84. void isr(uint8_t index);
  85. typedef struct
  86. {
  87. /* Basic counter. */
  88. bool enableReverseDirection;
  89. bool decoderWorkMode; // 0 = Normal mode, 1 = PHASEA input generates a count signal while PHASEB input control the direction.
  90. /* Signal detection. */
  91. uint8_t HOMETriggerMode; //0 - disable, 1 - rising, 2 - falling
  92. uint8_t INDEXTriggerMode; //0 - disabled, 1 - Use positive going edge-to-trigger initialization of position counters!, 2 - use falling
  93. bool clearCounter;
  94. bool clearHoldCounter;
  95. /* Filter for PHASEA, PHASEB, INDEX and HOME. */
  96. /* Input Filter Sample Count. This value should be chosen to reduce the probability of noisy samples causing an incorrect transition to be recognized. The value represent the number of consecutive samples that must agree prior to the input filter accepting an input transition. A value of 0x0 represents 3 samples. A value of 0x7 represents 10 samples. The Available range is 0 - 7. */
  97. uint16_t filterCount;
  98. /* Input Filter Sample Period. This value should be set such that the sampling period is larger than the period of the expected noise. This value represents the sampling period (in IPBus clock cycles) of the decoder input signals. The available range is 0 - 255. */
  99. uint16_t filterSamplePeriod;
  100. /* Position compare. */
  101. /* 0 - POSMATCH pulses when a match occurs between the position counters (POS) and the compare value (COMP). 1 - POSMATCH pulses when any position counter register is read. */
  102. bool positionMatchMode;
  103. /*!< Position compare value. The available value is a 32-bit number.*/
  104. uint32_t positionCompareValue;
  105. /* Modulus counting. */
  106. /*0 - Use INDEX pulse to increment/decrement revolution counter. 1 - Use modulus counting roll-over/under to increment/decrement revolution counter. */
  107. bool revolutionCountCondition;
  108. bool enableModuloCountMode; //Enable Modulo Counting. */
  109. /*Position modulus value. This value would be available only when "enableModuloCountMode" = true. The available value is a 32-bit number. */
  110. uint32_t positionModulusValue;
  111. //Position initial value. The available value is a 32-bit number. */
  112. uint32_t positionInitialValue;
  113. //Position Roll Over or Roll Under Interrupt Enable
  114. uint8_t positionROIE;
  115. uint8_t positionRUIE;
  116. bool IndexTrigger;
  117. bool HomeTrigger;
  118. } enc_config_t;
  119. //encoder
  120. typedef struct {
  121. uint8_t enc_ch;
  122. volatile IMXRT_ENC_t* ENC;
  123. IRQ_NUMBER_t interrupt;
  124. void (*isr)();
  125. uint16_t phaseA;
  126. uint16_t phaseB;
  127. uint16_t index;
  128. uint16_t home;
  129. uint16_t trigger;
  130. volatile uint32_t *clock_gate_register;
  131. uint32_t clock_gate_mask;
  132. } ENC_Channel_t;
  133. //Encoder channel structure
  134. typedef struct {
  135. uint8_t idx;
  136. uint8_t pin;
  137. volatile uint32_t *reg; // Which register controls the selection
  138. const uint32_t select_val; // Value for that selection
  139. uint16_t xbarIO;
  140. uint8_t xbarMUX;
  141. } ENC_Hardware_t;
  142. enc_config_t EncConfig;
  143. static const ENC_Channel_t channel[];
  144. static const ENC_Hardware_t hardware[];
  145. static const uint8_t _channel_count;
  146. static const uint8_t _hardware_count;
  147. public:
  148. QuadEncoder(uint8_t encoder_ch = 255, uint8_t PhaseA_pin = 255, uint8_t PhaseB_pin = 255, uint8_t pin_pus = 0, uint8_t index_pin = 255, uint8_t home_pin = 255, uint8_t trigger_pin = 255);
  149. void getConfig1(enc_config_t *config);
  150. void setConfigInitialPosition();
  151. void setInitConfig();
  152. void init();
  153. void Init(const enc_config_t *config);
  154. int32_t read();
  155. void write(uint32_t value);
  156. uint32_t getHoldPosition();
  157. uint16_t getPositionDifference();
  158. uint16_t getHoldDifference();
  159. uint16_t getRevolution();
  160. uint16_t getHoldRevolution();
  161. void printConfig(enc_config_t *config);
  162. //xbara1 configuration
  163. void enc_xbara_mapping(uint8_t pin, uint8_t PHASE, uint8_t PUS);
  164. void xbar_connect(unsigned int input, unsigned int output);
  165. //interrupts
  166. void enableInterrupts(const enc_config_t *config);
  167. void disableInterrupts(uint32_t flag);
  168. void clearStatusFlags(uint32_t flag, uint8_t index);
  169. // static class functions
  170. static void isrEnc1();
  171. static void isrEnc2();
  172. static void isrEnc3();
  173. static void isrEnc4();
  174. static inline void checkAndProcessInterrupt(uint8_t index);
  175. //counters
  176. static uint32_t homeCounter;
  177. static uint32_t indexCounter;
  178. static uint8_t compareValueFlag;
  179. private:
  180. static QuadEncoder *list[5];
  181. volatile uint16_t _encoder_ch, _pin_idx;
  182. };
  183. #endif