Teensy 4.1 core updated for C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

283 líneas
11KB

  1. #include "core_pins.h"
  2. /*
  3. struct digital_pin_bitband_and_config_table_struct {
  4. volatile uint32_t *reg;
  5. volatile uint32_t *mux;
  6. volatile uint32_t *pad;
  7. uint32_t mask;
  8. };
  9. extern const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[];
  10. #define digitalPinToPort(pin) (pin)
  11. #define digitalPinToBitMask(pin) (digital_pin_to_info_PGM[(pin)].mask)
  12. #define portOutputRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg))
  13. #define portSetRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 0x21))
  14. #define portClearRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 0x22))
  15. #define portToggleRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 0x23))
  16. #define portInputRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 2))
  17. #define portModeRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 1))
  18. #define portConfigRegister(pin) ((digital_pin_to_info_PGM[(pin)].max))
  19. #define digitalPinToPortReg(pin) (portOutputRegister(pin))
  20. */
  21. const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[] = {
  22. {&CORE_PIN0_PORTREG, &CORE_PIN0_CONFIG, &CORE_PIN0_PADCONFIG, CORE_PIN0_BITMASK},
  23. {&CORE_PIN1_PORTREG, &CORE_PIN1_CONFIG, &CORE_PIN1_PADCONFIG, CORE_PIN1_BITMASK},
  24. {&CORE_PIN2_PORTREG, &CORE_PIN2_CONFIG, &CORE_PIN2_PADCONFIG, CORE_PIN2_BITMASK},
  25. {&CORE_PIN3_PORTREG, &CORE_PIN3_CONFIG, &CORE_PIN3_PADCONFIG, CORE_PIN3_BITMASK},
  26. {&CORE_PIN4_PORTREG, &CORE_PIN4_CONFIG, &CORE_PIN4_PADCONFIG, CORE_PIN4_BITMASK},
  27. {&CORE_PIN5_PORTREG, &CORE_PIN5_CONFIG, &CORE_PIN5_PADCONFIG, CORE_PIN5_BITMASK},
  28. {&CORE_PIN6_PORTREG, &CORE_PIN6_CONFIG, &CORE_PIN6_PADCONFIG, CORE_PIN6_BITMASK},
  29. {&CORE_PIN7_PORTREG, &CORE_PIN7_CONFIG, &CORE_PIN7_PADCONFIG, CORE_PIN7_BITMASK},
  30. {&CORE_PIN8_PORTREG, &CORE_PIN8_CONFIG, &CORE_PIN8_PADCONFIG, CORE_PIN8_BITMASK},
  31. {&CORE_PIN9_PORTREG, &CORE_PIN9_CONFIG, &CORE_PIN9_PADCONFIG, CORE_PIN9_BITMASK},
  32. {&CORE_PIN10_PORTREG, &CORE_PIN10_CONFIG, &CORE_PIN10_PADCONFIG, CORE_PIN10_BITMASK},
  33. {&CORE_PIN11_PORTREG, &CORE_PIN11_CONFIG, &CORE_PIN11_PADCONFIG, CORE_PIN11_BITMASK},
  34. {&CORE_PIN12_PORTREG, &CORE_PIN12_CONFIG, &CORE_PIN12_PADCONFIG, CORE_PIN12_BITMASK},
  35. {&CORE_PIN13_PORTREG, &CORE_PIN13_CONFIG, &CORE_PIN13_PADCONFIG, CORE_PIN13_BITMASK},
  36. {&CORE_PIN14_PORTREG, &CORE_PIN14_CONFIG, &CORE_PIN14_PADCONFIG, CORE_PIN14_BITMASK},
  37. {&CORE_PIN15_PORTREG, &CORE_PIN15_CONFIG, &CORE_PIN15_PADCONFIG, CORE_PIN15_BITMASK},
  38. {&CORE_PIN16_PORTREG, &CORE_PIN16_CONFIG, &CORE_PIN16_PADCONFIG, CORE_PIN16_BITMASK},
  39. {&CORE_PIN17_PORTREG, &CORE_PIN17_CONFIG, &CORE_PIN17_PADCONFIG, CORE_PIN17_BITMASK},
  40. {&CORE_PIN18_PORTREG, &CORE_PIN18_CONFIG, &CORE_PIN18_PADCONFIG, CORE_PIN18_BITMASK},
  41. {&CORE_PIN19_PORTREG, &CORE_PIN19_CONFIG, &CORE_PIN19_PADCONFIG, CORE_PIN19_BITMASK},
  42. {&CORE_PIN20_PORTREG, &CORE_PIN20_CONFIG, &CORE_PIN20_PADCONFIG, CORE_PIN20_BITMASK},
  43. {&CORE_PIN21_PORTREG, &CORE_PIN21_CONFIG, &CORE_PIN21_PADCONFIG, CORE_PIN21_BITMASK},
  44. {&CORE_PIN22_PORTREG, &CORE_PIN22_CONFIG, &CORE_PIN22_PADCONFIG, CORE_PIN22_BITMASK},
  45. {&CORE_PIN23_PORTREG, &CORE_PIN23_CONFIG, &CORE_PIN23_PADCONFIG, CORE_PIN23_BITMASK},
  46. {&CORE_PIN24_PORTREG, &CORE_PIN24_CONFIG, &CORE_PIN24_PADCONFIG, CORE_PIN24_BITMASK},
  47. {&CORE_PIN25_PORTREG, &CORE_PIN25_CONFIG, &CORE_PIN25_PADCONFIG, CORE_PIN25_BITMASK},
  48. {&CORE_PIN26_PORTREG, &CORE_PIN26_CONFIG, &CORE_PIN26_PADCONFIG, CORE_PIN26_BITMASK},
  49. {&CORE_PIN27_PORTREG, &CORE_PIN27_CONFIG, &CORE_PIN27_PADCONFIG, CORE_PIN27_BITMASK},
  50. {&CORE_PIN28_PORTREG, &CORE_PIN28_CONFIG, &CORE_PIN28_PADCONFIG, CORE_PIN28_BITMASK},
  51. {&CORE_PIN29_PORTREG, &CORE_PIN29_CONFIG, &CORE_PIN29_PADCONFIG, CORE_PIN29_BITMASK},
  52. {&CORE_PIN30_PORTREG, &CORE_PIN30_CONFIG, &CORE_PIN30_PADCONFIG, CORE_PIN30_BITMASK},
  53. {&CORE_PIN31_PORTREG, &CORE_PIN31_CONFIG, &CORE_PIN31_PADCONFIG, CORE_PIN31_BITMASK},
  54. {&CORE_PIN32_PORTREG, &CORE_PIN32_CONFIG, &CORE_PIN32_PADCONFIG, CORE_PIN32_BITMASK},
  55. {&CORE_PIN33_PORTREG, &CORE_PIN33_CONFIG, &CORE_PIN33_PADCONFIG, CORE_PIN33_BITMASK},
  56. {&CORE_PIN34_PORTREG, &CORE_PIN34_CONFIG, &CORE_PIN34_PADCONFIG, CORE_PIN34_BITMASK},
  57. {&CORE_PIN35_PORTREG, &CORE_PIN35_CONFIG, &CORE_PIN35_PADCONFIG, CORE_PIN35_BITMASK},
  58. {&CORE_PIN36_PORTREG, &CORE_PIN36_CONFIG, &CORE_PIN36_PADCONFIG, CORE_PIN36_BITMASK},
  59. {&CORE_PIN37_PORTREG, &CORE_PIN37_CONFIG, &CORE_PIN37_PADCONFIG, CORE_PIN37_BITMASK},
  60. {&CORE_PIN38_PORTREG, &CORE_PIN38_CONFIG, &CORE_PIN38_PADCONFIG, CORE_PIN38_BITMASK},
  61. {&CORE_PIN39_PORTREG, &CORE_PIN39_CONFIG, &CORE_PIN39_PADCONFIG, CORE_PIN39_BITMASK},
  62. #if defined(ARDUINO_TEENSY41)
  63. {&CORE_PIN40_PORTREG, &CORE_PIN40_CONFIG, &CORE_PIN40_PADCONFIG, CORE_PIN40_BITMASK},
  64. {&CORE_PIN41_PORTREG, &CORE_PIN41_CONFIG, &CORE_PIN41_PADCONFIG, CORE_PIN41_BITMASK},
  65. {&CORE_PIN42_PORTREG, &CORE_PIN42_CONFIG, &CORE_PIN42_PADCONFIG, CORE_PIN42_BITMASK},
  66. {&CORE_PIN43_PORTREG, &CORE_PIN43_CONFIG, &CORE_PIN43_PADCONFIG, CORE_PIN43_BITMASK},
  67. {&CORE_PIN44_PORTREG, &CORE_PIN44_CONFIG, &CORE_PIN44_PADCONFIG, CORE_PIN44_BITMASK},
  68. {&CORE_PIN45_PORTREG, &CORE_PIN45_CONFIG, &CORE_PIN45_PADCONFIG, CORE_PIN45_BITMASK},
  69. {&CORE_PIN46_PORTREG, &CORE_PIN46_CONFIG, &CORE_PIN46_PADCONFIG, CORE_PIN46_BITMASK},
  70. {&CORE_PIN47_PORTREG, &CORE_PIN47_CONFIG, &CORE_PIN47_PADCONFIG, CORE_PIN47_BITMASK},
  71. {&CORE_PIN48_PORTREG, &CORE_PIN48_CONFIG, &CORE_PIN48_PADCONFIG, CORE_PIN48_BITMASK},
  72. {&CORE_PIN49_PORTREG, &CORE_PIN49_CONFIG, &CORE_PIN49_PADCONFIG, CORE_PIN49_BITMASK},
  73. {&CORE_PIN50_PORTREG, &CORE_PIN50_CONFIG, &CORE_PIN50_PADCONFIG, CORE_PIN50_BITMASK},
  74. {&CORE_PIN51_PORTREG, &CORE_PIN51_CONFIG, &CORE_PIN51_PADCONFIG, CORE_PIN51_BITMASK},
  75. {&CORE_PIN52_PORTREG, &CORE_PIN52_CONFIG, &CORE_PIN52_PADCONFIG, CORE_PIN52_BITMASK},
  76. {&CORE_PIN53_PORTREG, &CORE_PIN53_CONFIG, &CORE_PIN53_PADCONFIG, CORE_PIN53_BITMASK},
  77. {&CORE_PIN54_PORTREG, &CORE_PIN54_CONFIG, &CORE_PIN54_PADCONFIG, CORE_PIN54_BITMASK},
  78. #endif
  79. };
  80. void digitalWrite(uint8_t pin, uint8_t val)
  81. {
  82. const struct digital_pin_bitband_and_config_table_struct *p;
  83. uint32_t pinmode, mask;
  84. if (pin >= CORE_NUM_DIGITAL) return;
  85. p = digital_pin_to_info_PGM + pin;
  86. pinmode = *(p->reg + 1);
  87. mask = p->mask;
  88. if (pinmode & mask) {
  89. // pin is configured for output mode
  90. if (val) {
  91. *(p->reg + 0x21) = mask; // set register
  92. } else {
  93. *(p->reg + 0x22) = mask; // clear register
  94. }
  95. } else {
  96. // pin is configured for input mode
  97. // writing controls pullup resistor
  98. // TODO....
  99. }
  100. }
  101. uint8_t digitalRead(uint8_t pin)
  102. {
  103. const struct digital_pin_bitband_and_config_table_struct *p;
  104. if (pin >= CORE_NUM_DIGITAL) return 0;
  105. p = digital_pin_to_info_PGM + pin;
  106. return (*(p->reg + 2) & p->mask) ? 1 : 0;
  107. }
  108. void pinMode(uint8_t pin, uint8_t mode)
  109. {
  110. const struct digital_pin_bitband_and_config_table_struct *p;
  111. if (pin >= CORE_NUM_DIGITAL) return;
  112. p = digital_pin_to_info_PGM + pin;
  113. if (mode == OUTPUT || mode == OUTPUT_OPENDRAIN) {
  114. *(p->reg + 1) |= p->mask; // TODO: atomic
  115. if (mode == OUTPUT) {
  116. *(p->pad) = IOMUXC_PAD_DSE(7);
  117. } else { // OUTPUT_OPENDRAIN
  118. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_ODE;
  119. }
  120. } else {
  121. *(p->reg + 1) &= ~(p->mask); // TODO: atomic
  122. if (mode == INPUT) {
  123. *(p->pad) = IOMUXC_PAD_DSE(7);
  124. } else if (mode == INPUT_PULLUP) {
  125. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(3) | IOMUXC_PAD_HYS;
  126. } else if (mode == INPUT_PULLDOWN) {
  127. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(0) | IOMUXC_PAD_HYS;
  128. } else { // INPUT_DISABLE
  129. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_HYS;
  130. }
  131. }
  132. *(p->mux) = 5 | 0x10;
  133. }
  134. void _shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
  135. {
  136. if (bitOrder == LSBFIRST) {
  137. shiftOut_lsbFirst(dataPin, clockPin, value);
  138. } else {
  139. shiftOut_msbFirst(dataPin, clockPin, value);
  140. }
  141. }
  142. static const unsigned maxSpeed = 10000000ULL; //10 MHz
  143. static const unsigned maxSpeedBeforeDelay = 392000000ULL; //max F_CPU_ACTUAL before doing delays (measured for 10MHz, -O2)
  144. void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  145. {
  146. uint8_t mask;
  147. if (F_CPU_ACTUAL > maxSpeedBeforeDelay) {
  148. uint32_t cycles = (F_CPU_ACTUAL / 2 / maxSpeed);
  149. uint32_t t = ARM_DWT_CYCCNT;
  150. for (mask = 0x01; mask; mask <<= 1) {
  151. digitalWrite(dataPin, value & mask);
  152. do {;} while(ARM_DWT_CYCCNT - t < cycles);
  153. t += cycles / 2;
  154. digitalWrite(clockPin, HIGH);
  155. do {;} while(ARM_DWT_CYCCNT - t < cycles);
  156. t += cycles;
  157. digitalWrite(clockPin, LOW);
  158. do {;} while(ARM_DWT_CYCCNT - t < cycles);
  159. t += cycles / 2;
  160. }
  161. }
  162. else
  163. for (mask=0x01; mask; mask <<= 1) {
  164. digitalWrite(dataPin, value & mask);
  165. digitalWrite(clockPin, HIGH);
  166. digitalWrite(clockPin, LOW);
  167. }
  168. }
  169. void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  170. {
  171. uint32_t v;
  172. asm volatile ("rbit %0, %1" : "=r" (v) : "r" (value) );
  173. shiftOut_lsbFirst(dataPin, clockPin, v >> 24);
  174. }
  175. uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
  176. {
  177. if (bitOrder == LSBFIRST) {
  178. return shiftIn_lsbFirst(dataPin, clockPin);
  179. } else {
  180. return shiftIn_msbFirst(dataPin, clockPin);
  181. }
  182. }
  183. uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin)
  184. {
  185. uint8_t mask, value=0;
  186. for (mask=0x01; mask; mask <<= 1) {
  187. digitalWrite(clockPin, HIGH);
  188. if (digitalRead(dataPin)) value |= mask;
  189. digitalWrite(clockPin, LOW);
  190. }
  191. return value;
  192. }
  193. uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
  194. {
  195. uint8_t mask, value=0;
  196. for (mask=0x80; mask; mask >>= 1) {
  197. digitalWrite(clockPin, HIGH);
  198. if (digitalRead(dataPin)) value |= mask;
  199. digitalWrite(clockPin, LOW);
  200. }
  201. return value;
  202. }
  203. //(*portInputRegister(pin) & digitalPinToBitMask(pin))
  204. uint32_t pulseIn_high(uint8_t pin, uint32_t timeout)
  205. {
  206. const struct digital_pin_bitband_and_config_table_struct *p;
  207. p = digital_pin_to_info_PGM + pin;
  208. uint32_t usec_start, usec_stop;
  209. // wait for any previous pulse to end
  210. usec_start = micros();
  211. while ((*(p->reg + 2) & p->mask)) {
  212. if (micros()-usec_start > timeout) return 0;
  213. }
  214. // wait for the pulse to start
  215. usec_start = micros();
  216. while (!(*(p->reg + 2) & p->mask)) {
  217. if (micros()-usec_start > timeout) return 0;
  218. }
  219. usec_start = micros();
  220. // wait for the pulse to stop
  221. while ((*(p->reg + 2) & p->mask)) {
  222. if (micros()-usec_start > timeout) return 0;
  223. }
  224. usec_stop = micros();
  225. return usec_stop - usec_start;
  226. }
  227. uint32_t pulseIn_low(uint8_t pin, uint32_t timeout)
  228. {
  229. const struct digital_pin_bitband_and_config_table_struct *p;
  230. p = digital_pin_to_info_PGM + pin;
  231. uint32_t usec_start, usec_stop;
  232. // wait for any previous pulse to end
  233. usec_start = micros();
  234. while (!(*(p->reg + 2) & p->mask)) {
  235. if (micros() - usec_start > timeout) return 0;
  236. }
  237. // wait for the pulse to start
  238. usec_start = micros();
  239. while ((*(p->reg + 2) & p->mask)) {
  240. if (micros() - usec_start > timeout) return 0;
  241. }
  242. usec_start = micros();
  243. // wait for the pulse to stop
  244. while (!(*(p->reg + 2) & p->mask)) {
  245. if (micros() - usec_start > timeout) return 0;
  246. }
  247. usec_stop = micros();
  248. return usec_stop - usec_start;
  249. }
  250. // TODO: an inline version should handle the common case where state is const
  251. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  252. {
  253. if (pin >= CORE_NUM_DIGITAL) return 0;
  254. if (state) return pulseIn_high(pin, timeout);
  255. return pulseIn_low(pin, timeout);
  256. }