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.

FreqMeasureMultiIMXRT.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* FreqMeasure Library, for measuring relatively low frequencies
  2. * http://www.pjrc.com/teensy/td_libs_FreqMeasure.html
  3. * Copyright (c) 2015 PJRC.COM, LLC - Paul Stoffregen <paul@pjrc.com>
  4. * Extended 2016 by Thierry Frenkel <theremingenieur@gmail.com>
  5. *
  6. * Version 0.22
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "FreqMeasureMulti.h"
  27. #if defined(__IMXRT1062__)
  28. // This file is to handle the Teensy 4.x boards.
  29. #define M(a, b) ((((a) - 1) << 2) | (b)) // should translate from 0-15
  30. FreqMeasureMulti * FreqMeasureMulti::list[16] = {nullptr, nullptr, nullptr, nullptr};
  31. void FreqMeasureMulti::flexpwm1_0_isr() {list[M(1,0)]->isr();}
  32. void FreqMeasureMulti::flexpwm1_1_isr() {list[M(1,1)]->isr();}
  33. void FreqMeasureMulti::flexpwm1_2_isr() {list[M(1,2)]->isr();}
  34. void FreqMeasureMulti::flexpwm1_3_isr() {list[M(1,3)]->isr();}
  35. void FreqMeasureMulti::flexpwm2_0_isr() {list[M(2,0)]->isr();}
  36. void FreqMeasureMulti::flexpwm2_1_isr() {list[M(2,1)]->isr();}
  37. void FreqMeasureMulti::flexpwm2_2_isr() {list[M(2,2)]->isr();}
  38. void FreqMeasureMulti::flexpwm2_3_isr() {list[M(2,3)]->isr();}
  39. void FreqMeasureMulti::flexpwm3_0_isr() {list[M(3,0)]->isr();}
  40. void FreqMeasureMulti::flexpwm3_1_isr() {list[M(3,1)]->isr();}
  41. void FreqMeasureMulti::flexpwm3_3_isr() {list[M(3,3)]->isr();}
  42. void FreqMeasureMulti::flexpwm4_0_isr() {list[M(4,0)]->isr();}
  43. void FreqMeasureMulti::flexpwm4_1_isr() {list[M(4,1)]->isr();}
  44. void FreqMeasureMulti::flexpwm4_2_isr() {list[M(4,2)]->isr();}
  45. const struct freq_pwm_pin_info_struct FreqMeasureMulti::freq_pwm_pin_info[] = {
  46. {&IMXRT_FLEXPWM1, M(1,1), 0, 4 | 0x10, IRQ_FLEXPWM1_1, &FreqMeasureMulti::flexpwm1_1_isr, nullptr, 0}, // FlexPWM1_1_X 0 // AD_B0_03
  47. {&IMXRT_FLEXPWM1, M(1,0), 0, 4 | 0x10, IRQ_FLEXPWM1_0, &FreqMeasureMulti::flexpwm1_0_isr, nullptr, 0}, // FlexPWM1_0_X 1 // AD_B0_02
  48. {&IMXRT_FLEXPWM4, M(4,2), 1, 1 | 0x10, IRQ_FLEXPWM4_2, &FreqMeasureMulti::flexpwm4_2_isr, &IOMUXC_FLEXPWM4_PWMA2_SELECT_INPUT, 0}, // FlexPWM4_2_A 2 // EMC_04
  49. {&IMXRT_FLEXPWM4, M(4,2), 2, 1 | 0x10, IRQ_FLEXPWM4_2, &FreqMeasureMulti::flexpwm4_2_isr, nullptr, 0}, // FlexPWM4_2_B 3 // EMC_05
  50. {&IMXRT_FLEXPWM2, M(2,0), 1, 1 | 0x10, IRQ_FLEXPWM2_0, &FreqMeasureMulti::flexpwm2_0_isr, &IOMUXC_FLEXPWM2_PWMA0_SELECT_INPUT, 0}, // FlexPWM2_0_A 4 // EMC_06
  51. {&IMXRT_FLEXPWM2, M(2,1), 1, 1 | 0x10, IRQ_FLEXPWM2_1, &FreqMeasureMulti::flexpwm2_1_isr, &IOMUXC_FLEXPWM2_PWMA1_SELECT_INPUT, 0}, // FlexPWM2_1_A 5 // EMC_08
  52. {&IMXRT_FLEXPWM2, M(2,2), 1, 2 | 0x10, IRQ_FLEXPWM2_2, &FreqMeasureMulti::flexpwm2_2_isr, &IOMUXC_FLEXPWM2_PWMA2_SELECT_INPUT, 1}, // FlexPWM2_2_A 6 // B0_10
  53. {&IMXRT_FLEXPWM1, M(1,3), 2, 6 | 0x10, IRQ_FLEXPWM1_3, &FreqMeasureMulti::flexpwm1_3_isr, &IOMUXC_FLEXPWM1_PWMB3_SELECT_INPUT, 4}, // FlexPWM1_3_B 7 // B1_01
  54. {&IMXRT_FLEXPWM1, M(1,3), 1, 6 | 0x10, IRQ_FLEXPWM1_3, &FreqMeasureMulti::flexpwm1_3_isr, &IOMUXC_FLEXPWM1_PWMA3_SELECT_INPUT, 4}, // FlexPWM1_3_A 8 // B1_00
  55. {&IMXRT_FLEXPWM2, M(2,2), 2, 2 | 0x10, IRQ_FLEXPWM2_2, &FreqMeasureMulti::flexpwm2_2_isr, &IOMUXC_FLEXPWM2_PWMB2_SELECT_INPUT, 1}, // FlexPWM2_2_B 9 // B0_11
  56. {nullptr, 0, 0, 1 | 0x10, 0, nullptr, nullptr, 0}, // QuadTimer1_0 10 // B0_00
  57. {nullptr, 0, 0, 1 | 0x10, 0, nullptr, nullptr, 0}, // QuadTimer1_2 11 // B0_02
  58. {nullptr, 0, 0, 1 | 0x10, 0, nullptr, nullptr, 0}, // QuadTimer1_1 12 // B0_01
  59. {nullptr, 0, 0, 1 | 0x10, 0, nullptr, nullptr, 0}, // QuadTimer2_0 13 // B0_03
  60. {nullptr, 0, 0, 1 | 0x10, 0, nullptr, nullptr, 0}, // QuadTimer3_2 14 // AD_B1_02
  61. {nullptr, 0, 0, 1 | 0x10, 0, nullptr, nullptr, 0}, // QuadTimer3_3 15 // AD_B1_03
  62. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  63. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  64. {nullptr, 0, 0, 1 | 0x10, 0, nullptr, nullptr, 0}, // QuadTimer3_1 18 // AD_B1_01
  65. {nullptr, 0, 0, 1 | 0x10, 0, nullptr, nullptr, 0}, // QuadTimer3_0 19 // AD_B1_00
  66. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  67. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  68. {&IMXRT_FLEXPWM4, M(4,0), 1, 1 | 0x10, IRQ_FLEXPWM4_0, &FreqMeasureMulti::flexpwm4_0_isr, &IOMUXC_FLEXPWM4_PWMA0_SELECT_INPUT, 1}, // FlexPWM4_0_A 22 // AD_B1_08
  69. {&IMXRT_FLEXPWM4, M(4,1), 1, 1 | 0x10, IRQ_FLEXPWM4_1, &FreqMeasureMulti::flexpwm4_1_isr, &IOMUXC_FLEXPWM4_PWMA1_SELECT_INPUT, 1}, // FlexPWM4_1_A 23 // AD_B1_09
  70. {&IMXRT_FLEXPWM1, M(1,2), 0, 4 | 0x10, IRQ_FLEXPWM1_2, &FreqMeasureMulti::flexpwm1_2_isr, nullptr, 0}, // FlexPWM1_2_X 24 // AD_B0_12
  71. {&IMXRT_FLEXPWM1, M(1,3), 0, 4 | 0x10, IRQ_FLEXPWM1_3, &FreqMeasureMulti::flexpwm1_3_isr, nullptr, 0}, // FlexPWM1_3_X 25 // AD_B0_13
  72. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  73. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  74. {&IMXRT_FLEXPWM3, M(3,1), 2, 1 | 0x10, IRQ_FLEXPWM3_1, &FreqMeasureMulti::flexpwm3_1_isr, nullptr, 0}, // FlexPWM3_1_B 28 // EMC_32
  75. {&IMXRT_FLEXPWM3, M(3,1), 1, 1 | 0x10, IRQ_FLEXPWM3_1, &FreqMeasureMulti::flexpwm3_1_isr, nullptr, 0}, // FlexPWM3_1_A 29 // EMC_31
  76. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  77. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  78. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  79. {&IMXRT_FLEXPWM2, M(2,0), 2, 1 | 0x10, IRQ_FLEXPWM2_0, &FreqMeasureMulti::flexpwm2_0_isr, &IOMUXC_FLEXPWM2_PWMB0_SELECT_INPUT, 0}, // FlexPWM2_0_B 33 // EMC_07
  80. #ifdef ARDUINO_TEENSY40
  81. {&IMXRT_FLEXPWM1, M(1,1), 2, 1 | 0x10, IRQ_FLEXPWM1_1, &FreqMeasureMulti::flexpwm1_1_isr, &IOMUXC_FLEXPWM1_PWMB1_SELECT_INPUT, 1}, // FlexPWM1_1_B 34 // SD_B0_03
  82. {&IMXRT_FLEXPWM1, M(1,1), 1, 1 | 0x10, IRQ_FLEXPWM1_1, &FreqMeasureMulti::flexpwm1_1_isr, &IOMUXC_FLEXPWM1_PWMA1_SELECT_INPUT, 1}, // FlexPWM1_1_A 35 // SD_B0_02
  83. {&IMXRT_FLEXPWM1, M(1,0), 2, 1 | 0x10, IRQ_FLEXPWM1_0, &FreqMeasureMulti::flexpwm1_0_isr, &IOMUXC_FLEXPWM1_PWMB0_SELECT_INPUT, 1}, // FlexPWM1_0_B 36 // SD_B0_01
  84. {&IMXRT_FLEXPWM1, M(1,0), 1, 1 | 0x10, IRQ_FLEXPWM1_0, &FreqMeasureMulti::flexpwm1_0_isr, &IOMUXC_FLEXPWM1_PWMA0_SELECT_INPUT, 1}, // FlexPWM1_0_A 37 // SD_B0_00
  85. {&IMXRT_FLEXPWM1, M(1,2), 2, 1 | 0x10, IRQ_FLEXPWM1_2, &FreqMeasureMulti::flexpwm1_2_isr, &IOMUXC_FLEXPWM1_PWMB2_SELECT_INPUT, 1}, // FlexPWM1_2_B 38 // SD_B0_05
  86. {&IMXRT_FLEXPWM1, M(1,2), 1, 1 | 0x10, IRQ_FLEXPWM1_2, &FreqMeasureMulti::flexpwm1_2_isr, &IOMUXC_FLEXPWM1_PWMA2_SELECT_INPUT, 1}, // FlexPWM1_2_A 39 // SD_B0_04
  87. #endif
  88. #ifdef ARDUINO_TEENSY41
  89. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  90. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  91. {&IMXRT_FLEXPWM2, M(2,3), 1, 6 | 0x10, IRQ_FLEXPWM2_3, &FreqMeasureMulti::flexpwm2_3_isr, &IOMUXC_FLEXPWM2_PWMA3_SELECT_INPUT, 4}, // FlexPWM2_3_A 36 // B1_02
  92. {&IMXRT_FLEXPWM2, M(2,3), 2, 6 | 0x10, IRQ_FLEXPWM2_3, &FreqMeasureMulti::flexpwm2_3_isr, &IOMUXC_FLEXPWM2_PWMB3_SELECT_INPUT, 3}, // FlexPWM2_3_B 37 // B1_03
  93. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  94. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  95. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  96. {nullptr, 0, 0, 0 | 0x10, 0, nullptr, nullptr, 0},
  97. {&IMXRT_FLEXPWM1, M(1,1), 2, 1 | 0x10, IRQ_FLEXPWM1_1, &FreqMeasureMulti::flexpwm1_1_isr, &IOMUXC_FLEXPWM1_PWMB1_SELECT_INPUT, 1}, // FlexPWM1_1_B 42 // SD_B0_03
  98. {&IMXRT_FLEXPWM1, M(1,1), 1, 1 | 0x10, IRQ_FLEXPWM1_1, &FreqMeasureMulti::flexpwm1_1_isr, &IOMUXC_FLEXPWM1_PWMA1_SELECT_INPUT, 1}, // FlexPWM1_1_A 43 // SD_B0_02
  99. {&IMXRT_FLEXPWM1, M(1,0), 2, 1 | 0x10, IRQ_FLEXPWM1_0, &FreqMeasureMulti::flexpwm1_0_isr, &IOMUXC_FLEXPWM1_PWMB0_SELECT_INPUT, 1}, // FlexPWM1_0_B 44 // SD_B0_01
  100. {&IMXRT_FLEXPWM1, M(1,0), 1, 1 | 0x10, IRQ_FLEXPWM1_0, &FreqMeasureMulti::flexpwm1_0_isr, &IOMUXC_FLEXPWM1_PWMA0_SELECT_INPUT, 1}, // FlexPWM1_0_A 45 // SD_B0_00
  101. {&IMXRT_FLEXPWM1, M(1,2), 2, 1 | 0x10, IRQ_FLEXPWM1_2, &FreqMeasureMulti::flexpwm1_2_isr, &IOMUXC_FLEXPWM1_PWMB2_SELECT_INPUT, 1}, // FlexPWM1_2_B 46 // SD_B0_05
  102. {&IMXRT_FLEXPWM1, M(1,2), 1, 1 | 0x10, IRQ_FLEXPWM1_2, &FreqMeasureMulti::flexpwm1_2_isr, &IOMUXC_FLEXPWM1_PWMA2_SELECT_INPUT, 1}, // FlexPWM1_2_A 47 // SD_B0_04
  103. {&IMXRT_FLEXPWM1, M(1,0), 0, 0 | 0x10, IRQ_FLEXPWM1_0, &FreqMeasureMulti::flexpwm1_0_isr, &IOMUXC_FLEXPWM1_PWMB0_SELECT_INPUT, 0}, // duplicate FlexPWM1_0_B
  104. {&IMXRT_FLEXPWM1, M(1,0), 0, 0 | 0x10, IRQ_FLEXPWM1_2, &FreqMeasureMulti::flexpwm1_2_isr, &IOMUXC_FLEXPWM1_PWMA2_SELECT_INPUT, 0}, // duplicate FlexPWM1_2_A
  105. {&IMXRT_FLEXPWM1, M(1,0), 0, 0 | 0x10, IRQ_FLEXPWM1_2, &FreqMeasureMulti::flexpwm1_2_isr, &IOMUXC_FLEXPWM1_PWMB2_SELECT_INPUT, 0}, // duplicate FlexPWM1_2_B
  106. {&IMXRT_FLEXPWM3, M(3,3), 2, 1 | 0x10, IRQ_FLEXPWM3_3, &FreqMeasureMulti::flexpwm3_3_isr, nullptr, 0}, // FlexPWM3_3_B 51 // EMC_22
  107. {&IMXRT_FLEXPWM1, M(1,0), 0, 0 | 0x10, IRQ_FLEXPWM1_1, &FreqMeasureMulti::flexpwm1_1_isr, &IOMUXC_FLEXPWM1_PWMB1_SELECT_INPUT, 0}, // duplicate FlexPWM1_1_B
  108. {&IMXRT_FLEXPWM1, M(1,0), 0, 0 | 0x10, IRQ_FLEXPWM1_1, &FreqMeasureMulti::flexpwm1_1_isr, &IOMUXC_FLEXPWM1_PWMA1_SELECT_INPUT, 0}, // duplicate FlexPWM1_1_A
  109. {&IMXRT_FLEXPWM3, M(3,0), 1, 1 | 0x10, IRQ_FLEXPWM3_0, &FreqMeasureMulti::flexpwm3_0_isr, nullptr, 0}, // FlexPWM3_0_A 54 // EMC_29
  110. #endif
  111. };
  112. #if 0
  113. #define FTM_SC_VALUE (FTM_SC_TOIE | FTM_SC_CLKS(1) | FTM_SC_PS(0))
  114. #define FTM_CSC_RAISING (FTM_CSC_CHIE | FTM_CSC_ELSA)
  115. #define FTM_CSC_FALLING (FTM_CSC_CHIE | FTM_CSC_ELSB)
  116. #define UPDATE_ON_RAISING 1
  117. #define UPDATE_ON_FALLING 2
  118. #define UPDATE_DIFFERENCE 4
  119. #endif
  120. //static uint8_t channelmask = 0;
  121. bool FreqMeasureMulti::begin(uint32_t pin) {
  122. return begin(pin, FREQMEASUREMULTI_RAISING);
  123. }
  124. bool FreqMeasureMulti::begin(uint32_t pin, uint8_t mode)
  125. {
  126. uint16_t capture_mode;
  127. if (pin > (sizeof(freq_pwm_pin_info)/sizeof(freq_pwm_pin_info[0]))) return false;
  128. if (freq_pwm_pin_info[pin].pflexpwm == nullptr) return false;
  129. _pin = pin; // remember the pin
  130. _mode = mode;
  131. _channel = freq_pwm_pin_info[pin].channel; // remember the channel
  132. // So we may now have a valid pin, lets configure it.
  133. *(portConfigRegister(pin)) = freq_pwm_pin_info[pin].muxval;
  134. if (freq_pwm_pin_info[pin].select_input_register)
  135. *freq_pwm_pin_info[pin].select_input_register = freq_pwm_pin_info[pin].select_val;
  136. IMXRT_FLEXPWM_t *pflexpwm = freq_pwm_pin_info[pin].pflexpwm;
  137. uint8_t sub_module = freq_pwm_pin_info[pin].module & 3;
  138. uint8_t sub_module_bit = 1 << sub_module;
  139. pflexpwm->FCTRL0 |= FLEXPWM_FCTRL0_FLVL(sub_module_bit);
  140. pflexpwm->FSTS0 = sub_module_bit;
  141. pflexpwm->MCTRL |= FLEXPWM_MCTRL_CLDOK(sub_module_bit);
  142. pflexpwm->SM[sub_module].CTRL2 = FLEXPWM_SMCTRL2_INDEP;
  143. pflexpwm->SM[sub_module].CTRL = FLEXPWM_SMCTRL_HALF;
  144. pflexpwm->SM[sub_module].INIT = 0;
  145. pflexpwm->SM[sub_module].VAL0 = 0;
  146. pflexpwm->SM[sub_module].VAL1 = 65535;
  147. pflexpwm->SM[sub_module].VAL2 = 0;
  148. pflexpwm->SM[sub_module].VAL3 = 0;
  149. pflexpwm->SM[sub_module].VAL4 = 0;
  150. pflexpwm->SM[sub_module].VAL5 = 0;
  151. pflexpwm->MCTRL |= FLEXPWM_MCTRL_LDOK(sub_module_bit) | FLEXPWM_MCTRL_RUN(sub_module_bit);
  152. // How do we handle the three different channels?
  153. // Can maybe capture two different values.
  154. //00b - Disabled
  155. //01b - Capture falling edges
  156. //10b - Capture rising edges
  157. //11b - Capture any edge
  158. // BUGBUG: FLEXPWM_SMCAPTCTRLA_EDGA0(1) is used in cases wehre it is also _EDGB0 or _EDGX0, but
  159. // macros are the same
  160. // Quick and dirty way to set which interrupt bits we want...
  161. uint16_t inten = FLEXPWM_SMINTEN_CA0IE | FLEXPWM_SMINTEN_CB0IE | FLEXPWM_SMINTEN_CX0IE; // one of the 0 edge bits.
  162. switch (mode) {
  163. case FREQMEASUREMULTI_FALLING:
  164. capture_mode = FLEXPWM_SMCAPTCTRLA_EDGA0(1);
  165. break;
  166. case FREQMEASUREMULTI_INTERLEAVE:
  167. capture_mode = FLEXPWM_SMCAPTCTRLA_EDGA0(2) | FLEXPWM_SMCAPTCTRLA_EDGA1(1);
  168. inten = FLEXPWM_SMINTEN_CA0IE | FLEXPWM_SMINTEN_CB0IE | FLEXPWM_SMINTEN_CX0IE
  169. | FLEXPWM_SMINTEN_CA1IE | FLEXPWM_SMINTEN_CB1IE | FLEXPWM_SMINTEN_CX1IE;
  170. break;
  171. case FREQMEASUREMULTI_MARK_ONLY:
  172. capture_mode = FLEXPWM_SMCAPTCTRLA_EDGA0(1) | FLEXPWM_SMCAPTCTRLA_EDGA1(2);
  173. inten = FLEXPWM_SMINTEN_CA0IE | FLEXPWM_SMINTEN_CB0IE | FLEXPWM_SMINTEN_CX0IE
  174. | FLEXPWM_SMINTEN_CA1IE | FLEXPWM_SMINTEN_CB1IE | FLEXPWM_SMINTEN_CX1IE;
  175. break;
  176. case FREQMEASUREMULTI_SPACE_ONLY:
  177. capture_mode = FLEXPWM_SMCAPTCTRLA_EDGA0(2) | FLEXPWM_SMCAPTCTRLA_EDGA1(1);
  178. inten = FLEXPWM_SMINTEN_CA0IE | FLEXPWM_SMINTEN_CB0IE | FLEXPWM_SMINTEN_CX0IE
  179. | FLEXPWM_SMINTEN_CA1IE | FLEXPWM_SMINTEN_CB1IE | FLEXPWM_SMINTEN_CX1IE;
  180. break;
  181. // ? Both edges?
  182. case FREQMEASUREMULTI_ALTERNATE:
  183. capture_mode = FLEXPWM_SMCAPTCTRLA_EDGA0(2) | FLEXPWM_SMCAPTCTRLA_EDGA1(1);
  184. inten = FLEXPWM_SMINTEN_CA0IE | FLEXPWM_SMINTEN_CB0IE | FLEXPWM_SMINTEN_CX0IE
  185. | FLEXPWM_SMINTEN_CA1IE | FLEXPWM_SMINTEN_CB1IE | FLEXPWM_SMINTEN_CX1IE;
  186. break;
  187. default:
  188. capture_mode = FLEXPWM_SMCAPTCTRLA_EDGA0(2);
  189. }
  190. switch (_channel) {
  191. case 0: // X channel
  192. pflexpwm->SM[sub_module].CAPTCTRLX = capture_mode | FLEXPWM_SMCAPTCTRLA_ARMA;
  193. pflexpwm->SM[sub_module].INTEN |= (inten & (FLEXPWM_SMINTEN_CX0IE | FLEXPWM_SMINTEN_CX1IE)) | FLEXPWM_SMINTEN_RIE;
  194. break;
  195. case 1: // A Channel
  196. pflexpwm->SM[sub_module].CAPTCTRLA = capture_mode | FLEXPWM_SMCAPTCTRLA_ARMA;
  197. pflexpwm->SM[sub_module].INTEN |= (inten & (FLEXPWM_SMINTEN_CA0IE | FLEXPWM_SMINTEN_CA1IE)) | FLEXPWM_SMINTEN_RIE;
  198. break;
  199. case 2: // B Channel;
  200. pflexpwm->SM[sub_module].CAPTCTRLB = capture_mode | FLEXPWM_SMCAPTCTRLA_ARMA;
  201. pflexpwm->SM[sub_module].INTEN |= (inten & (FLEXPWM_SMINTEN_CB0IE | FLEXPWM_SMINTEN_CB1IE)) | FLEXPWM_SMINTEN_RIE;
  202. break;
  203. }
  204. __disable_irq();
  205. if (list[freq_pwm_pin_info[pin].module] == nullptr) {
  206. list[freq_pwm_pin_info[pin].module] = this; // remember this object
  207. _next = nullptr;
  208. attachInterruptVector((IRQ_NUMBER_t)freq_pwm_pin_info[pin].irq, freq_pwm_pin_info[pin].isr);
  209. NVIC_SET_PRIORITY(freq_pwm_pin_info[pin].irq, 48);
  210. NVIC_ENABLE_IRQ(freq_pwm_pin_info[pin].irq);
  211. } else {
  212. // BUGBUG:: Maybe should check for duplicate signals, but...
  213. _next = list[freq_pwm_pin_info[pin].module];
  214. list[freq_pwm_pin_info[pin].module] = this;
  215. }
  216. __enable_irq();
  217. return true;
  218. }
  219. uint32_t FreqMeasureMulti::available(void)
  220. {
  221. uint32_t head = buffer_head;
  222. uint32_t tail = buffer_tail;
  223. if (head >= tail) return head - tail;
  224. return FREQMEASUREMULTI_BUFFER_LEN + head - tail;
  225. }
  226. uint32_t FreqMeasureMulti::read(void)
  227. {
  228. uint32_t head = buffer_head;
  229. uint32_t tail = buffer_tail;
  230. if (head == tail) return 0xFFFFFFFF;
  231. tail = tail + 1;
  232. if (tail >= FREQMEASUREMULTI_BUFFER_LEN) tail = 0;
  233. uint32_t value = buffer_value[tail].count;
  234. last_read_level = buffer_value[tail].level;
  235. buffer_tail = tail;
  236. return value;
  237. }
  238. uint8_t FreqMeasureMulti::readLevel(void)
  239. {
  240. return last_read_level;
  241. }
  242. float FreqMeasureMulti::countToFrequency(uint32_t count)
  243. {
  244. return (float)F_BUS_ACTUAL / (float)count;
  245. }
  246. float FreqMeasureMulti::countToNanoseconds(uint32_t count)
  247. {
  248. return (float)count * 1000000000.0f / (float)F_BUS_ACTUAL;
  249. }
  250. void FreqMeasureMulti::end(void)
  251. {
  252. if (list[freq_pwm_pin_info[_pin].module] == this) {
  253. list[freq_pwm_pin_info[_pin].module] = _next; // removed us from head of list
  254. if (list[freq_pwm_pin_info[_pin].module] == nullptr) {
  255. // no one left active on this one.
  256. IMXRT_FLEXPWM_t *pflexpwm = freq_pwm_pin_info[_pin].pflexpwm;
  257. uint8_t sub_module = freq_pwm_pin_info[_pin].module & 3;
  258. pflexpwm->SM[sub_module].INTEN = 0;
  259. pflexpwm->SM[sub_module].CAPTCTRLA = 0;
  260. NVIC_DISABLE_IRQ(freq_pwm_pin_info[_pin].irq);
  261. }
  262. } else {
  263. if (list[freq_pwm_pin_info[_pin].module]->_next == this) list[freq_pwm_pin_info[_pin].module]->_next = _next;
  264. if (list[freq_pwm_pin_info[_pin].module]->_next->_next == this) list[freq_pwm_pin_info[_pin].module]->_next->_next = _next;
  265. }
  266. }
  267. void FreqMeasureMulti::processChannelISR(uint8_t channel, uint32_t capture, uint8_t edge)
  268. {
  269. uint8_t level = LEVEL_UNDEFINED;
  270. uint32_t i;
  271. bool add_item_to_buffer = true;
  272. uint32_t period = capture - _capture_previous[edge];
  273. if (channel == _channel) {
  274. // compute the waveform period
  275. switch (_mode) {
  276. case FREQMEASUREMULTI_RAISING:
  277. level = LEVEL_MARK_SPACE;
  278. break;
  279. case FREQMEASUREMULTI_FALLING:
  280. level = LEVEL_SPACE_MARK;
  281. break;
  282. case FREQMEASUREMULTI_INTERLEAVE:
  283. level = edge? LEVEL_SPACE_MARK : LEVEL_MARK_SPACE;
  284. break;
  285. case FREQMEASUREMULTI_SPACE_ONLY:
  286. level = LEVEL_SPACE_ONLY;
  287. if (edge) add_item_to_buffer = false;
  288. else period = capture - _capture_previous[1];
  289. break;
  290. case FREQMEASUREMULTI_MARK_ONLY:
  291. level = LEVEL_MARK_ONLY;
  292. if (edge) add_item_to_buffer = false;
  293. else period = capture - _capture_previous[1];
  294. break;
  295. case FREQMEASUREMULTI_ALTERNATE:
  296. if (edge) {
  297. period = capture - _capture_previous[0];
  298. level = LEVEL_MARK_ONLY;
  299. } else {
  300. period = capture - _capture_previous[1];
  301. level = LEVEL_SPACE_ONLY;
  302. }
  303. break;
  304. }
  305. _capture_previous[edge] = capture;
  306. // store it into the buffer
  307. if (add_item_to_buffer) {
  308. i = buffer_head + 1;
  309. if (i >= FREQMEASUREMULTI_BUFFER_LEN) i = 0;
  310. if (i != buffer_tail) {
  311. buffer_value[i].level = level;
  312. buffer_value[i].count = period;
  313. //buffer_value[i] = period;
  314. buffer_head = i;
  315. }
  316. }
  317. } else if (_next) {
  318. _next->processChannelISR(channel, capture, edge);
  319. }
  320. }
  321. void FreqMeasureMulti::isr()
  322. {
  323. IMXRT_FLEXPWM_t *pflexpwm = freq_pwm_pin_info[_pin].pflexpwm;
  324. uint8_t sub_module = freq_pwm_pin_info[_pin].module & 3;
  325. uint32_t capture;
  326. bool inc = false;
  327. // See if we have an overflow
  328. if (pflexpwm->SM[sub_module].STS & FLEXPWM_SMSTS_RF) {
  329. pflexpwm->SM[sub_module].STS = FLEXPWM_SMSTS_RF; // clear the status
  330. capture_msw++;
  331. inc = true;
  332. }
  333. // This section handels inputs on Pin A
  334. // We can probably combine and need to handle multiple pins on same one...
  335. if (pflexpwm->SM[sub_module].STS & FLEXPWM_SMSTS_CFA0) { // CAPTURE FLAG A0 (HINT)
  336. capture = pflexpwm->SM[sub_module].VAL2;
  337. pflexpwm->SM[sub_module].STS = FLEXPWM_SMSTS_CFA0; // clear status
  338. if (capture <= 0xE000 || !inc) {
  339. capture |= (capture_msw << 16);
  340. } else {
  341. capture |= ((capture_msw - 1) << 16);
  342. }
  343. processChannelISR(1, capture, 0);
  344. }
  345. if (pflexpwm->SM[sub_module].STS & FLEXPWM_SMSTS_CFA1) { // CAPTURE FLAG A0 (HINT)
  346. capture = pflexpwm->SM[sub_module].VAL3;
  347. pflexpwm->SM[sub_module].STS = FLEXPWM_SMSTS_CFA1; // clear status
  348. if (capture <= 0xE000 || !inc) {
  349. capture |= (capture_msw << 16);
  350. } else {
  351. capture |= ((capture_msw - 1) << 16);
  352. }
  353. processChannelISR(1, capture, 1);
  354. }
  355. // This section handels inputs on Pin B
  356. if (pflexpwm->SM[sub_module].STS & FLEXPWM_SMSTS_CFB0) { // CAPTURE FLAG B0 (HINT)
  357. capture = pflexpwm->SM[sub_module].VAL4; // Guessing ?
  358. pflexpwm->SM[sub_module].STS = FLEXPWM_SMSTS_CFB0; // clear status
  359. if (capture <= 0xE000 || !inc) {
  360. capture |= (capture_msw << 16);
  361. } else {
  362. capture |= ((capture_msw - 1) << 16);
  363. }
  364. processChannelISR(2, capture, 0);
  365. }
  366. if (pflexpwm->SM[sub_module].STS & FLEXPWM_SMSTS_CFB1) { // CAPTURE FLAG B0 (HINT)
  367. capture = pflexpwm->SM[sub_module].VAL5; // Guessing ?
  368. pflexpwm->SM[sub_module].STS = FLEXPWM_SMSTS_CFB1; // clear status
  369. if (capture <= 0xE000 || !inc) {
  370. capture |= (capture_msw << 16);
  371. } else {
  372. capture |= ((capture_msw - 1) << 16);
  373. }
  374. processChannelISR(2, capture, 1);
  375. }
  376. // This section handels inputs on Pin X
  377. if (pflexpwm->SM[sub_module].STS & FLEXPWM_SMSTS_CFX0) { // CAPTURE FLAG X0 (HINT)
  378. capture = pflexpwm->SM[sub_module].VAL0; //
  379. pflexpwm->SM[sub_module].STS = FLEXPWM_SMSTS_CFX0; // clear status
  380. if (capture <= 0xE000 || !inc) {
  381. capture |= (capture_msw << 16);
  382. } else {
  383. capture |= ((capture_msw - 1) << 16);
  384. }
  385. processChannelISR(0, capture, 0);
  386. }
  387. if (pflexpwm->SM[sub_module].STS & FLEXPWM_SMSTS_CFX1) { // CAPTURE FLAG X0 (HINT)
  388. capture = pflexpwm->SM[sub_module].VAL1; // Guessing?
  389. pflexpwm->SM[sub_module].STS = FLEXPWM_SMSTS_CFX1; // clear status
  390. if (capture <= 0xE000 || !inc) {
  391. capture |= (capture_msw << 16);
  392. } else {
  393. capture |= ((capture_msw - 1) << 16);
  394. }
  395. processChannelISR(0, capture, 1);
  396. }
  397. }
  398. #endif