sanguino/cores/arduino/wiring_digital.c

Fri, 17 Nov 2017 10:13:31 +0100

author
mdd
date
Fri, 17 Nov 2017 10:13:31 +0100
changeset 3
770b218a4931
parent 2
b373b0288715
permissions
-rw-r--r--

proper configuration, homing and planner optimization

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

mercurial