Teensy 4.1 core updated for 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.

258 lines
10KB

  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. #endif
  72. };
  73. void digitalWrite(uint8_t pin, uint8_t val)
  74. {
  75. const struct digital_pin_bitband_and_config_table_struct *p;
  76. uint32_t pinmode, mask;
  77. if (pin >= CORE_NUM_DIGITAL) return;
  78. p = digital_pin_to_info_PGM + pin;
  79. pinmode = *(p->reg + 1);
  80. mask = p->mask;
  81. if (pinmode & mask) {
  82. // pin is configured for output mode
  83. if (val) {
  84. *(p->reg + 0x21) = mask; // set register
  85. } else {
  86. *(p->reg + 0x22) = mask; // clear register
  87. }
  88. } else {
  89. // pin is configured for input mode
  90. // writing controls pullup resistor
  91. // TODO....
  92. }
  93. }
  94. uint8_t digitalRead(uint8_t pin)
  95. {
  96. const struct digital_pin_bitband_and_config_table_struct *p;
  97. if (pin >= CORE_NUM_DIGITAL) return 0;
  98. p = digital_pin_to_info_PGM + pin;
  99. return (*(p->reg + 2) & p->mask) ? 1 : 0;
  100. }
  101. void pinMode(uint8_t pin, uint8_t mode)
  102. {
  103. const struct digital_pin_bitband_and_config_table_struct *p;
  104. if (pin >= CORE_NUM_DIGITAL) return;
  105. p = digital_pin_to_info_PGM + pin;
  106. if (mode == OUTPUT || mode == OUTPUT_OPENDRAIN) {
  107. *(p->reg + 1) |= p->mask; // TODO: atomic
  108. if (mode == OUTPUT) {
  109. *(p->pad) = IOMUXC_PAD_DSE(7);
  110. } else { // OUTPUT_OPENDRAIN
  111. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_ODE;
  112. }
  113. } else {
  114. *(p->reg + 1) &= ~(p->mask); // TODO: atomic
  115. if (mode == INPUT) {
  116. *(p->pad) = IOMUXC_PAD_DSE(7);
  117. } else if (mode == INPUT_PULLUP) {
  118. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(3) | IOMUXC_PAD_HYS;
  119. } else if (mode == INPUT_PULLDOWN) {
  120. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(0) | IOMUXC_PAD_HYS;
  121. } else { // INPUT_DISABLE
  122. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_HYS;
  123. }
  124. }
  125. *(p->mux) = 5 | 0x10;
  126. }
  127. void _shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
  128. {
  129. if (bitOrder == LSBFIRST) {
  130. shiftOut_lsbFirst(dataPin, clockPin, value);
  131. } else {
  132. shiftOut_msbFirst(dataPin, clockPin, value);
  133. }
  134. }
  135. void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  136. {
  137. uint8_t mask;
  138. for (mask=0x01; mask; mask <<= 1) {
  139. digitalWrite(dataPin, value & mask);
  140. digitalWrite(clockPin, HIGH);
  141. digitalWrite(clockPin, LOW);
  142. }
  143. }
  144. void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  145. {
  146. uint8_t mask;
  147. for (mask=0x80; mask; mask >>= 1) {
  148. digitalWrite(dataPin, value & mask);
  149. digitalWrite(clockPin, HIGH);
  150. digitalWrite(clockPin, LOW);
  151. }
  152. }
  153. uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
  154. {
  155. if (bitOrder == LSBFIRST) {
  156. return shiftIn_lsbFirst(dataPin, clockPin);
  157. } else {
  158. return shiftIn_msbFirst(dataPin, clockPin);
  159. }
  160. }
  161. uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin)
  162. {
  163. uint8_t mask, value=0;
  164. for (mask=0x01; mask; mask <<= 1) {
  165. digitalWrite(clockPin, HIGH);
  166. if (digitalRead(dataPin)) value |= mask;
  167. digitalWrite(clockPin, LOW);
  168. }
  169. return value;
  170. }
  171. uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
  172. {
  173. uint8_t mask, value=0;
  174. for (mask=0x80; mask; mask >>= 1) {
  175. digitalWrite(clockPin, HIGH);
  176. if (digitalRead(dataPin)) value |= mask;
  177. digitalWrite(clockPin, LOW);
  178. }
  179. return value;
  180. }
  181. //(*portInputRegister(pin) & digitalPinToBitMask(pin))
  182. uint32_t pulseIn_high(uint8_t pin, uint32_t timeout)
  183. {
  184. const struct digital_pin_bitband_and_config_table_struct *p;
  185. p = digital_pin_to_info_PGM + pin;
  186. uint32_t usec_start, usec_stop;
  187. // wait for any previous pulse to end
  188. usec_start = micros();
  189. while ((*(p->reg + 2) & p->mask)) {
  190. if (micros()-usec_start > timeout) return 0;
  191. }
  192. // wait for the pulse to start
  193. usec_start = micros();
  194. while (!(*(p->reg + 2) & p->mask)) {
  195. if (micros()-usec_start > timeout) return 0;
  196. }
  197. usec_start = micros();
  198. // wait for the pulse to stop
  199. while ((*(p->reg + 2) & p->mask)) {
  200. if (micros()-usec_start > timeout) return 0;
  201. }
  202. usec_stop = micros();
  203. return usec_stop - usec_start;
  204. }
  205. uint32_t pulseIn_low(uint8_t pin, uint32_t timeout)
  206. {
  207. const struct digital_pin_bitband_and_config_table_struct *p;
  208. p = digital_pin_to_info_PGM + pin;
  209. uint32_t usec_start, usec_stop;
  210. // wait for any previous pulse to end
  211. usec_start = micros();
  212. while (!(*(p->reg + 2) & p->mask)) {
  213. if (micros() - usec_start > timeout) return 0;
  214. }
  215. // wait for the pulse to start
  216. usec_start = micros();
  217. while ((*(p->reg + 2) & p->mask)) {
  218. if (micros() - usec_start > timeout) return 0;
  219. }
  220. usec_start = micros();
  221. // wait for the pulse to stop
  222. while (!(*(p->reg + 2) & p->mask)) {
  223. if (micros() - usec_start > timeout) return 0;
  224. }
  225. usec_stop = micros();
  226. return usec_stop - usec_start;
  227. }
  228. // TODO: an inline version should handle the common case where state is const
  229. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  230. {
  231. if (pin >= CORE_NUM_DIGITAL) return 0;
  232. if (state) return pulseIn_high(pin, timeout);
  233. return pulseIn_low(pin, timeout);
  234. }