summaryrefslogtreecommitdiffstats
path: root/lib/msp430/wiring_digital.c
blob: cf851b3fbe1cd6ba07c92db30668c4b09ebf7a7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
  ************************************************************************
  *	HardwareSerial.cpp
  *
  *	Arduino core files for MSP430
  *		Copyright (c) 2012 Robert Wessels. All right reserved.
  *
  *
  ***********************************************************************
  Derived from:
  wiring_digital.c - digital input and output functions
  Part of Arduino - http://www.arduino.cc/

  Copyright (c) 2005-2006 David A. Mellis

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

#define ARDUINO_MAIN
#include "wiring_private.h"
#include "pins_energia.h"

void pinMode(uint8_t pin, uint8_t mode)
{
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);

	volatile uint8_t *dir;
	volatile uint8_t *ren;
	volatile uint8_t *out;

	if (port == NOT_A_PORT) return;

	dir = portDirRegister(port);
	ren = portRenRegister(port);
	out = portOutputRegister(port);

	if (mode == INPUT) {
		*dir &= ~bit;
	} else if (mode == INPUT_PULLUP) {
		*dir &= ~bit;
                *out |= bit;
                *ren |= bit;
        } else if (mode == INPUT_PULLDOWN) {
		*dir &= ~bit;
                *out &= ~bit;
                *ren |= bit;
        } else {
		*dir |= bit;
	}
}

void pinMode_int(uint8_t pin, uint8_t mode)
{
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);

	volatile uint8_t *dir;
	volatile uint8_t *ren;
	volatile uint8_t *out;
	volatile uint8_t *sel;

	if (port == NOT_A_PORT) return;

	dir = portDirRegister(port);
	ren = portRenRegister(port);
	out = portOutputRegister(port);

	if (mode & OUTPUT) {
		*dir |= bit;
	} else {
		*dir &= ~bit;
		if (mode & INPUT_PULLUP) {
                *out |= bit;
                *ren |= bit;
        } else if (mode & INPUT_PULLDOWN) {
                *out &= ~bit;
                *ren |= bit;
        }
	}

	#if (defined(P1SEL_) || defined(P1SEL))
	sel = portSel0Register(port);	/* get the port function select register address */
	if (mode & PORT_SELECTION0) {
		*sel |= bit;
    } else {
		*sel &= ~bit;
	}
	#if (defined(P1SEL2_) || defined(P1SEL2))
	sel = portSel2Register(port);	/* get the port function select register address */
	if (mode & PORT_SELECTION1) {
		*sel |= bit;
    } else {
		*sel &= ~bit;
	}
	#endif
	#endif
	
	#if (defined(P1SEL0_) || defined(P1SEL0))
	sel = portSel0Register(port);	/* get the port function select register address */
	if (mode & PORT_SELECTION0) {
		*sel |= bit;
    } else {
		*sel &= ~bit;
	}
	#if (defined(P1SEL1_) || defined(P1SEL1))
	sel = portSel1Register(port);	/* get the port function select register address */
	if (mode & PORT_SELECTION1) {
		*sel |= bit;
    } else {
		*sel &= ~bit;
	}
	#endif
	#endif

}

int digitalRead(uint8_t pin)
{
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);

	if (port == NOT_A_PORT) return LOW;

	if (*portInputRegister(port) & bit) return HIGH;
	return LOW;
}

void digitalWrite(uint8_t pin, uint8_t val)
{
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);
	volatile uint8_t *out;
	volatile uint8_t *sel;

	if (port == NOT_A_PORT) return;

	/*
	 * Clear bit in PxSEL register to select GPIO function. Other functions like analogWrite(...) 
	 * will set this bit so need to clear it.
	 */
	#if (defined(P1SEL_) || defined(P1SEL))
	sel = portSel0Register(port);	/* get the port function select register address */
	*sel &= ~bit;			/* clear bit in pin function select register */
	#if (defined(P1SEL2_) || defined(P1SEL2))
	sel = portSel2Register(port);	/* get the port function select register address */
	*sel &= ~bit;			/* clear bit in pin function select register */
	#endif
	#endif
	
	#if (defined(P1SEL0_) || defined(P1SEL0))
	sel = portSel0Register(port);	/* get the port function select register address */
	*sel &= ~bit;			/* clear bit in pin function select register */
	#if (defined(P1SEL1_) || defined(P1SEL1))
	sel = portSel1Register(port);	/* get the port function select register address */
	*sel &= ~bit;			/* clear bit in pin function select register */
	#endif
	#endif
	
	
	out = portOutputRegister(port);

	if (val == LOW) {
		*out &= ~bit;
	} else {
		*out |= bit;
	}
}