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