Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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