My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

wiring_digital.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. wiring_digital.c - digital input and output functions
  3. Part of Arduino - http://www.arduino.cc/
  4. Copyright (c) 2005-2006 David A. Mellis
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General
  14. Public License along with this library; if not, write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. Modified 28 September 2010 by Mark Sproul
  18. $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
  19. */
  20. #include "wiring_private.h"
  21. #include "pins_arduino.h"
  22. void pinMode(uint8_t pin, uint8_t mode)
  23. {
  24. uint8_t bit = digitalPinToBitMask(pin);
  25. uint8_t port = digitalPinToPort(pin);
  26. volatile uint8_t *reg;
  27. if (port == NOT_A_PIN) return;
  28. // JWS: can I let the optimizer do this?
  29. reg = portModeRegister(port);
  30. if (mode == INPUT) {
  31. uint8_t oldSREG = SREG;
  32. cli();
  33. *reg &= ~bit;
  34. SREG = oldSREG;
  35. } else {
  36. uint8_t oldSREG = SREG;
  37. cli();
  38. *reg |= bit;
  39. SREG = oldSREG;
  40. }
  41. }
  42. // Forcing this inline keeps the callers from having to push their own stuff
  43. // on the stack. It is a good performance win and only takes 1 more byte per
  44. // user than calling. (It will take more bytes on the 168.)
  45. //
  46. // But shouldn't this be moved into pinMode? Seems silly to check and do on
  47. // each digitalread or write.
  48. //
  49. // Mark Sproul:
  50. // - Removed inline. Save 170 bytes on atmega1280
  51. // - changed to a switch statment; added 32 bytes but much easier to read and maintain.
  52. // - Added more #ifdefs, now compiles for atmega645
  53. //
  54. //static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));
  55. //static inline void turnOffPWM(uint8_t timer)
  56. static void turnOffPWM(uint8_t timer)
  57. {
  58. switch (timer)
  59. {
  60. #if defined(TCCR1A) && defined(COM1A1)
  61. case TIMER1A: cbi(TCCR1A, COM1A1); break;
  62. #endif
  63. #if defined(TCCR1A) && defined(COM1B1)
  64. case TIMER1B: cbi(TCCR1A, COM1B1); break;
  65. #endif
  66. #if defined(TCCR2) && defined(COM21)
  67. case TIMER2: cbi(TCCR2, COM21); break;
  68. #endif
  69. #if defined(TCCR0A) && defined(COM0A1)
  70. case TIMER0A: cbi(TCCR0A, COM0A1); break;
  71. #endif
  72. #if defined(TIMER0B) && defined(COM0B1)
  73. case TIMER0B: cbi(TCCR0A, COM0B1); break;
  74. #endif
  75. #if defined(TCCR2A) && defined(COM2A1)
  76. case TIMER2A: cbi(TCCR2A, COM2A1); break;
  77. #endif
  78. #if defined(TCCR2A) && defined(COM2B1)
  79. case TIMER2B: cbi(TCCR2A, COM2B1); break;
  80. #endif
  81. #if defined(TCCR3A) && defined(COM3A1)
  82. case TIMER3A: cbi(TCCR3A, COM3A1); break;
  83. #endif
  84. #if defined(TCCR3A) && defined(COM3B1)
  85. case TIMER3B: cbi(TCCR3A, COM3B1); break;
  86. #endif
  87. #if defined(TCCR3A) && defined(COM3C1)
  88. case TIMER3C: cbi(TCCR3A, COM3C1); break;
  89. #endif
  90. #if defined(TCCR4A) && defined(COM4A1)
  91. case TIMER4A: cbi(TCCR4A, COM4A1); break;
  92. #endif
  93. #if defined(TCCR4A) && defined(COM4B1)
  94. case TIMER4B: cbi(TCCR4A, COM4B1); break;
  95. #endif
  96. #if defined(TCCR4A) && defined(COM4C1)
  97. case TIMER4C: cbi(TCCR4A, COM4C1); break;
  98. #endif
  99. #if defined(TCCR5A)
  100. case TIMER5A: cbi(TCCR5A, COM5A1); break;
  101. case TIMER5B: cbi(TCCR5A, COM5B1); break;
  102. case TIMER5C: cbi(TCCR5A, COM5C1); break;
  103. #endif
  104. }
  105. }
  106. void digitalWrite(uint8_t pin, uint8_t val)
  107. {
  108. uint8_t timer = digitalPinToTimer(pin);
  109. uint8_t bit = digitalPinToBitMask(pin);
  110. uint8_t port = digitalPinToPort(pin);
  111. volatile uint8_t *out;
  112. if (port == NOT_A_PIN) return;
  113. // If the pin that support PWM output, we need to turn it off
  114. // before doing a digital write.
  115. if (timer != NOT_ON_TIMER) turnOffPWM(timer);
  116. out = portOutputRegister(port);
  117. if (val == LOW) {
  118. uint8_t oldSREG = SREG;
  119. cli();
  120. *out &= ~bit;
  121. SREG = oldSREG;
  122. } else {
  123. uint8_t oldSREG = SREG;
  124. cli();
  125. *out |= bit;
  126. SREG = oldSREG;
  127. }
  128. }
  129. int digitalRead(uint8_t pin)
  130. {
  131. uint8_t timer = digitalPinToTimer(pin);
  132. uint8_t bit = digitalPinToBitMask(pin);
  133. uint8_t port = digitalPinToPort(pin);
  134. if (port == NOT_A_PIN) return LOW;
  135. // If the pin that support PWM output, we need to turn it off
  136. // before getting a digital reading.
  137. if (timer != NOT_ON_TIMER) turnOffPWM(timer);
  138. if (*portInputRegister(port) & bit) return HIGH;
  139. return LOW;
  140. }