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.

digital.c 9.5KB

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