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.

297 lines
12KB

  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. // value controls PULLUP/PULLDOWN resistors
  98. if (val) {
  99. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(3) | IOMUXC_PAD_HYS;
  100. } else {
  101. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(0) | IOMUXC_PAD_HYS;
  102. }
  103. }
  104. }
  105. void digitalToggle(uint8_t pin)
  106. {
  107. const struct digital_pin_bitband_and_config_table_struct *p;
  108. uint32_t pinmode, mask;
  109. if (pin >= CORE_NUM_DIGITAL) return;
  110. p = digital_pin_to_info_PGM + pin;
  111. mask = p->mask;
  112. *(p->reg + 0x23) = mask; // toggle register
  113. }
  114. uint8_t digitalRead(uint8_t pin)
  115. {
  116. const struct digital_pin_bitband_and_config_table_struct *p;
  117. if (pin >= CORE_NUM_DIGITAL) return 0;
  118. p = digital_pin_to_info_PGM + pin;
  119. return (*(p->reg + 2) & p->mask) ? 1 : 0;
  120. }
  121. void pinMode(uint8_t pin, uint8_t mode)
  122. {
  123. const struct digital_pin_bitband_and_config_table_struct *p;
  124. if (pin >= CORE_NUM_DIGITAL) return;
  125. p = digital_pin_to_info_PGM + pin;
  126. if (mode == OUTPUT || mode == OUTPUT_OPENDRAIN) {
  127. *(p->reg + 1) |= p->mask; // TODO: atomic
  128. if (mode == OUTPUT) {
  129. *(p->pad) = IOMUXC_PAD_DSE(7);
  130. } else { // OUTPUT_OPENDRAIN
  131. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_ODE;
  132. }
  133. } else {
  134. *(p->reg + 1) &= ~(p->mask); // TODO: atomic
  135. if (mode == INPUT) {
  136. *(p->pad) = IOMUXC_PAD_DSE(7);
  137. } else if (mode == INPUT_PULLUP) {
  138. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(3) | IOMUXC_PAD_HYS;
  139. } else if (mode == INPUT_PULLDOWN) {
  140. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(0) | IOMUXC_PAD_HYS;
  141. } else { // INPUT_DISABLE
  142. *(p->pad) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_HYS;
  143. }
  144. }
  145. *(p->mux) = 5 | 0x10;
  146. }
  147. void _shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
  148. {
  149. if (bitOrder == LSBFIRST) {
  150. shiftOut_lsbFirst(dataPin, clockPin, value);
  151. } else {
  152. shiftOut_msbFirst(dataPin, clockPin, value);
  153. }
  154. }
  155. static const unsigned maxSpeed = 10000000ULL; //10 MHz
  156. static const unsigned maxSpeedBeforeDelay = 392000000ULL; //max F_CPU_ACTUAL before doing delays (measured for 10MHz, -O2)
  157. void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  158. {
  159. uint8_t mask;
  160. if (F_CPU_ACTUAL > maxSpeedBeforeDelay) {
  161. uint32_t cycles = (F_CPU_ACTUAL / 2 / maxSpeed);
  162. uint32_t t = ARM_DWT_CYCCNT;
  163. for (mask = 0x01; mask; mask <<= 1) {
  164. digitalWrite(dataPin, value & mask);
  165. do {;} while(ARM_DWT_CYCCNT - t < cycles);
  166. t += cycles / 2;
  167. digitalWrite(clockPin, HIGH);
  168. do {;} while(ARM_DWT_CYCCNT - t < cycles);
  169. t += cycles;
  170. digitalWrite(clockPin, LOW);
  171. do {;} while(ARM_DWT_CYCCNT - t < cycles);
  172. t += cycles / 2;
  173. }
  174. }
  175. else
  176. for (mask=0x01; mask; mask <<= 1) {
  177. digitalWrite(dataPin, value & mask);
  178. digitalWrite(clockPin, HIGH);
  179. digitalWrite(clockPin, LOW);
  180. }
  181. }
  182. void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  183. {
  184. uint32_t v;
  185. asm volatile ("rbit %0, %1" : "=r" (v) : "r" (value) );
  186. shiftOut_lsbFirst(dataPin, clockPin, v >> 24);
  187. }
  188. uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
  189. {
  190. if (bitOrder == LSBFIRST) {
  191. return shiftIn_lsbFirst(dataPin, clockPin);
  192. } else {
  193. return shiftIn_msbFirst(dataPin, clockPin);
  194. }
  195. }
  196. uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin)
  197. {
  198. uint8_t mask, value=0;
  199. for (mask=0x01; mask; mask <<= 1) {
  200. digitalWrite(clockPin, HIGH);
  201. if (digitalRead(dataPin)) value |= mask;
  202. digitalWrite(clockPin, LOW);
  203. }
  204. return value;
  205. }
  206. uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
  207. {
  208. uint8_t mask, value=0;
  209. for (mask=0x80; mask; mask >>= 1) {
  210. digitalWrite(clockPin, HIGH);
  211. if (digitalRead(dataPin)) value |= mask;
  212. digitalWrite(clockPin, LOW);
  213. }
  214. return value;
  215. }
  216. //(*portInputRegister(pin) & digitalPinToBitMask(pin))
  217. uint32_t pulseIn_high(uint8_t pin, uint32_t timeout)
  218. {
  219. const struct digital_pin_bitband_and_config_table_struct *p;
  220. p = digital_pin_to_info_PGM + pin;
  221. uint32_t usec_start, usec_stop;
  222. // wait for any previous pulse to end
  223. usec_start = micros();
  224. while ((*(p->reg + 2) & p->mask)) {
  225. if (micros()-usec_start > timeout) return 0;
  226. }
  227. // wait for the pulse to start
  228. usec_start = micros();
  229. while (!(*(p->reg + 2) & p->mask)) {
  230. if (micros()-usec_start > timeout) return 0;
  231. }
  232. usec_start = micros();
  233. // wait for the pulse to stop
  234. while ((*(p->reg + 2) & p->mask)) {
  235. if (micros()-usec_start > timeout) return 0;
  236. }
  237. usec_stop = micros();
  238. return usec_stop - usec_start;
  239. }
  240. uint32_t pulseIn_low(uint8_t pin, uint32_t timeout)
  241. {
  242. const struct digital_pin_bitband_and_config_table_struct *p;
  243. p = digital_pin_to_info_PGM + pin;
  244. uint32_t usec_start, usec_stop;
  245. // wait for any previous pulse to end
  246. usec_start = micros();
  247. while (!(*(p->reg + 2) & p->mask)) {
  248. if (micros() - usec_start > timeout) return 0;
  249. }
  250. // wait for the pulse to start
  251. usec_start = micros();
  252. while ((*(p->reg + 2) & p->mask)) {
  253. if (micros() - usec_start > timeout) return 0;
  254. }
  255. usec_start = micros();
  256. // wait for the pulse to stop
  257. while (!(*(p->reg + 2) & p->mask)) {
  258. if (micros() - usec_start > timeout) return 0;
  259. }
  260. usec_stop = micros();
  261. return usec_stop - usec_start;
  262. }
  263. // TODO: an inline version should handle the common case where state is const
  264. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  265. {
  266. if (pin >= CORE_NUM_DIGITAL) return 0;
  267. if (state) return pulseIn_high(pin, timeout);
  268. return pulseIn_low(pin, timeout);
  269. }