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.

265 lines
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. void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  143. {
  144. uint8_t mask;
  145. for (mask=0x01; mask; mask <<= 1) {
  146. digitalWrite(dataPin, value & mask);
  147. digitalWrite(clockPin, HIGH);
  148. digitalWrite(clockPin, LOW);
  149. }
  150. }
  151. void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  152. {
  153. uint8_t mask;
  154. for (mask=0x80; mask; mask >>= 1) {
  155. digitalWrite(dataPin, value & mask);
  156. digitalWrite(clockPin, HIGH);
  157. digitalWrite(clockPin, LOW);
  158. }
  159. }
  160. uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
  161. {
  162. if (bitOrder == LSBFIRST) {
  163. return shiftIn_lsbFirst(dataPin, clockPin);
  164. } else {
  165. return shiftIn_msbFirst(dataPin, clockPin);
  166. }
  167. }
  168. uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin)
  169. {
  170. uint8_t mask, value=0;
  171. for (mask=0x01; mask; mask <<= 1) {
  172. digitalWrite(clockPin, HIGH);
  173. if (digitalRead(dataPin)) value |= mask;
  174. digitalWrite(clockPin, LOW);
  175. }
  176. return value;
  177. }
  178. uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
  179. {
  180. uint8_t mask, value=0;
  181. for (mask=0x80; mask; mask >>= 1) {
  182. digitalWrite(clockPin, HIGH);
  183. if (digitalRead(dataPin)) value |= mask;
  184. digitalWrite(clockPin, LOW);
  185. }
  186. return value;
  187. }
  188. //(*portInputRegister(pin) & digitalPinToBitMask(pin))
  189. uint32_t pulseIn_high(uint8_t pin, uint32_t timeout)
  190. {
  191. const struct digital_pin_bitband_and_config_table_struct *p;
  192. p = digital_pin_to_info_PGM + pin;
  193. uint32_t usec_start, usec_stop;
  194. // wait for any previous pulse to end
  195. usec_start = micros();
  196. while ((*(p->reg + 2) & p->mask)) {
  197. if (micros()-usec_start > timeout) return 0;
  198. }
  199. // wait for the pulse to start
  200. usec_start = micros();
  201. while (!(*(p->reg + 2) & p->mask)) {
  202. if (micros()-usec_start > timeout) return 0;
  203. }
  204. usec_start = micros();
  205. // wait for the pulse to stop
  206. while ((*(p->reg + 2) & p->mask)) {
  207. if (micros()-usec_start > timeout) return 0;
  208. }
  209. usec_stop = micros();
  210. return usec_stop - usec_start;
  211. }
  212. uint32_t pulseIn_low(uint8_t pin, uint32_t timeout)
  213. {
  214. const struct digital_pin_bitband_and_config_table_struct *p;
  215. p = digital_pin_to_info_PGM + pin;
  216. uint32_t usec_start, usec_stop;
  217. // wait for any previous pulse to end
  218. usec_start = micros();
  219. while (!(*(p->reg + 2) & p->mask)) {
  220. if (micros() - usec_start > timeout) return 0;
  221. }
  222. // wait for the pulse to start
  223. usec_start = micros();
  224. while ((*(p->reg + 2) & p->mask)) {
  225. if (micros() - usec_start > timeout) return 0;
  226. }
  227. usec_start = micros();
  228. // wait for the pulse to stop
  229. while (!(*(p->reg + 2) & p->mask)) {
  230. if (micros() - usec_start > timeout) return 0;
  231. }
  232. usec_stop = micros();
  233. return usec_stop - usec_start;
  234. }
  235. // TODO: an inline version should handle the common case where state is const
  236. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  237. {
  238. if (pin >= CORE_NUM_DIGITAL) return 0;
  239. if (state) return pulseIn_high(pin, timeout);
  240. return pulseIn_low(pin, timeout);
  241. }