summaryrefslogtreecommitdiff
path: root/include/arch/msp430fr5994lp/driver/gpio.h
blob: c3f1abf03e61fbd1c04213ebf87858ff15f65aa7 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright 2020 Daniel Friesel
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */
#ifndef GPIO_H
#define GPIO_H

#include <msp430.h>

class GPIO {
	private:
		GPIO(const GPIO &copy);

	public:
		GPIO () {}

		enum Pin : unsigned char {
			p1_0 = 0, p1_1, p1_2, p1_3, p1_4, p1_5, p1_6, p1_7,
			p2_0, p2_1, p2_2, p2_3, p2_4, p2_5, p2_6, p2_7,
			p3_0, p3_1, p3_2, p3_3, p3_4, p3_5, p3_6, p3_7,
			p4_0, p4_1, p4_2, p4_3, p4_4, p4_5, p4_6, p4_7,
			p5_0, p5_1, p5_2, p5_3, p5_4, p5_5, p5_6, p5_7,
			p6_0, p6_1, p6_2, p6_3, p6_4, p6_5, p6_6, p6_7,
			p7_0, p7_1, p7_2, p7_3, p7_4, p7_5, p7_6, p7_7,
			p8_0, p8_1, p8_2, p8_3, p8_4, p8_5, p8_6, p8_7,
			pj_0, pj_1, pj_2, pj_3, pj_4, pj_5, pj_6, pj_7,
			PIN_INVALID
		};

		/*
		 * Set all non-standard GPIOs to input with pull-down.
		 * This avoids excessive current draw due to floating inputs.
		 * Note that we do not set GPIOs to output low, as that might
		 * short-circuit other peripherals.
		 */
		inline void setup() {
			P1DIR = BIT0 | BIT1; // red LED, green LED
			P2DIR = 0;
			P3DIR = 0;
			P4DIR = 0;
			P5DIR = 0;
			P6DIR = 0;
			P7DIR = 0;
			P8DIR = 0;
			PJDIR = 0;
			P1OUT = 0;
			P2OUT = 0;
			P3OUT = 0;
			P4OUT = 0;
			P5OUT = 0;
			P6OUT = 0;
			P7OUT = 0;
			P8OUT = 0;
			PJOUT = 0;
			P1REN = 0xff & ~(BIT0 | BIT1); // red LED, green LED
			P2REN = 0xff & ~(BIT0 | BIT1); // UART
			P3REN = 0xff;
			P4REN = 0xff;
			P5REN = 0xff;
			P6REN = 0xff;
			P7REN = 0xff;
			P8REN = 0xff;
			PJREN = BIT6 | BIT7; // HFXT (not populated)
		}
		inline void led_on(unsigned char id) {
			if (id == 0) {
				P1OUT |= BIT0;
			} else {
				P1OUT |= BIT1;
			}
		}
		inline void led_off(unsigned char id) {
			if (id == 0) {
				P1OUT &= ~BIT0;
			} else {
				P1OUT &= ~BIT1;
			}
		}
		inline void led_toggle(unsigned char id) {
			if (id == 0) {
				P1OUT ^= BIT0;
			} else {
				P1OUT ^= BIT1;
			}
		}
		inline void input(unsigned char const pin) {
			if (pin < p2_0) {
				P1DIR &= ~(1 << pin);
			} else if (pin < p3_0) {
				P2DIR &= ~(1 << (pin - p2_0));
			} else if (pin < p4_0) {
				P3DIR &= ~(1 << (pin - p3_0));
			} else if (pin < p5_0) {
				P4DIR &= ~(1 << (pin - p4_0));
			} else if (pin < p6_0) {
				P5DIR &= ~(1 << (pin - p5_0));
			} else if (pin < p7_0) {
				P6DIR &= ~(1 << (pin - p6_0));
			} else if (pin < p8_0) {
				P7DIR &= ~(1 << (pin - p7_0));
			} else if (pin < pj_0) {
				P8DIR &= ~(1 << (pin - p8_0));
			} else if (pin < PIN_INVALID) {
				PJDIR &= ~(1 << (pin - pj_0));
			}
		}
		inline void input(unsigned char const pin, unsigned char const pull) {
			if (pin < p2_0) {
				P1DIR &= ~(1 << pin);
				P1OUT = pull ? (P1OUT | (1 << pin)) : (P1OUT & ~(1 << pin));
				P1REN |= (1 << pin);
			} else if (pin < p3_0) {
				P2DIR &= ~(1 << (pin - p2_0));
				P2OUT = pull ? (P2OUT | (1 << (pin - p2_0))) : (P2OUT & ~(1 << (pin - p2_0)));
				P2REN |= (1 << (pin - p2_0));
			} else if (pin < p4_0) {
				P3DIR &= ~(1 << (pin - p3_0));
				P3OUT = pull ? (P3OUT | (1 << (pin - p3_0))) : (P3OUT & ~(1 << (pin - p3_0)));
				P3REN |= (1 << (pin - p3_0));
			} else if (pin < p5_0) {
				P4DIR &= ~(1 << (pin - p4_0));
				P4OUT = pull ? (P4OUT | (1 << (pin - p4_0))) : (P4OUT & ~(1 << (pin - p4_0)));
				P4REN |= (1 << (pin - p4_0));
			} else if (pin < p6_0) {
				P5DIR &= ~(1 << (pin - p5_0));
				P5OUT = pull ? (P5OUT | (1 << (pin - p5_0))) : (P5OUT & ~(1 << (pin - p5_0)));
				P5REN |= (1 << (pin - p5_0));
			} else if (pin < p7_0) {
				P6DIR &= ~(1 << (pin - p6_0));
				P6OUT = pull ? (P6OUT | (1 << (pin - p6_0))) : (P6OUT & ~(1 << (pin - p6_0)));
				P6REN |= (1 << (pin - p6_0));
			} else if (pin < p8_0) {
				P7DIR &= ~(1 << (pin - p7_0));
				P7OUT = pull ? (P7OUT | (1 << (pin - p7_0))) : (P7OUT & ~(1 << (pin - p7_0)));
				P7REN |= (1 << (pin - p7_0));
			} else if (pin < pj_0) {
				P8DIR &= ~(1 << (pin - p8_0));
				P8OUT = pull ? (P8OUT | (1 << (pin - p8_0))) : (P8OUT & ~(1 << (pin - p8_0)));
				P8REN |= (1 << (pin - p8_0));
			} else if (pin < PIN_INVALID) {
				PJDIR &= ~(1 << (pin - pj_0));
				PJOUT = pull ? (PJOUT | (1 << (pin - pj_0))) : (PJOUT & ~(1 << (pin - pj_0)));
				PJREN |= (1 << (pin - pj_0));
			}
		}
		inline void output(unsigned char const pin) {
			if (pin < p2_0) {
				P1DIR |= (1 << pin);
			} else if (pin < p3_0) {
				P2DIR |= (1 << (pin - p2_0));
			} else if (pin < p4_0) {
				P3DIR |= (1 << (pin - p3_0));
			} else if (pin < p5_0) {
				P4DIR |= (1 << (pin - p4_0));
			} else if (pin < p6_0) {
				P5DIR |= (1 << (pin - p5_0));
			} else if (pin < p7_0) {
				P6DIR |= (1 << (pin - p6_0));
			} else if (pin < p8_0) {
				P7DIR |= (1 << (pin - p7_0));
			} else if (pin < pj_0) {
				P8DIR |= (1 << (pin - p8_0));
			} else if (pin < PIN_INVALID) {
				PJDIR |= (1 << (pin - pj_0));
			}
		}
		inline void output(unsigned char const pin, unsigned char const value) {
			if (pin < p2_0) {
				P1OUT = value ? (P1OUT | (1 << pin)) : (P1OUT & ~(1 << pin));
				P1DIR |= (1 << pin);
			} else if (pin < p3_0) {
				P2OUT = value ? (P2OUT | (1 << (pin - p2_0))) : (P2OUT & ~(1 << (pin - p2_0)));
				P2DIR |= (1 << (pin - p2_0));
			} else if (pin < p4_0) {
				P3OUT = value ? (P3OUT | (1 << (pin - p3_0))) : (P3OUT & ~(1 << (pin - p3_0)));
				P3DIR |= (1 << (pin - p3_0));
			} else if (pin < p5_0) {
				P4OUT = value ? (P4OUT | (1 << (pin - p4_0))) : (P4OUT & ~(1 << (pin - p4_0)));
				P4DIR |= (1 << (pin - p4_0));
			} else if (pin < p6_0) {
				P5OUT = value ? (P5OUT | (1 << (pin - p5_0))) : (P5OUT & ~(1 << (pin - p5_0)));
				P5DIR |= (1 << (pin - p5_0));
			} else if (pin < p7_0) {
				P6OUT = value ? (P6OUT | (1 << (pin - p6_0))) : (P6OUT & ~(1 << (pin - p6_0)));
				P6DIR |= (1 << (pin - p6_0));
			} else if (pin < p8_0) {
				P7OUT = value ? (P7OUT | (1 << (pin - p7_0))) : (P7OUT & ~(1 << (pin - p7_0)));
				P7DIR |= (1 << (pin - p7_0));
			} else if (pin < pj_0) {
				P8OUT = value ? (P8OUT | (1 << (pin - p8_0))) : (P8OUT & ~(1 << (pin - p8_0)));
				P8DIR |= (1 << (pin - p8_0));
			} else if (pin < PIN_INVALID) {
				PJOUT = value ? (PJOUT | (1 << (pin - pj_0))) : (PJOUT & ~(1 << (pin - pj_0)));
				PJDIR |= (1 << (pin - pj_0));
			}
		}
		inline unsigned char read(unsigned char const pin) {
			if (pin < p2_0) {
				return P1IN & (1 << pin);
			} else if (pin < p3_0) {
				return P2IN & (1 << (pin - p2_0));
			} else if (pin < p4_0) {
				return P3IN & (1 << (pin - p3_0));
			} else if (pin < p5_0) {
				return P4IN & (1 << (pin - p4_0));
			} else if (pin < p6_0) {
				return P5IN & (1 << (pin - p5_0));
			} else if (pin < p7_0) {
				return P6IN & (1 << (pin - p6_0));
			} else if (pin < p8_0) {
				return P7IN & (1 << (pin - p7_0));
			} else if (pin < pj_0) {
				return P8IN & (1 << (pin - p8_0));
			} else if (pin < PIN_INVALID) {
				return PJIN & (1 << (pin - pj_0));
			}
			return 0;
		}
		inline void write(unsigned char const pin, unsigned char value) {
			if (pin < p2_0) {
				if (value) {
					P1OUT |= (1 << pin);
				} else {
					P1OUT &= ~(1 << pin);
				}
			} else if (pin < p3_0) {
				if (value) {
					P2OUT |= (1 << (pin - p2_0));
				} else {
					P2OUT &= ~(1 << (pin - p2_0));
				}
			} else if (pin < p4_0) {
				if (value) {
					P3OUT |= (1 << (pin - p3_0));
				} else {
					P3OUT &= ~(1 << (pin - p3_0));
				}
			} else if (pin < p5_0) {
				if (value) {
					P4OUT |= (1 << (pin - p4_0));
				} else {
					P4OUT &= ~(1 << (pin - p4_0));
				}
			} else if (pin < p6_0) {
				if (value) {
					P5OUT |= (1 << (pin - p5_0));
				} else {
					P5OUT &= ~(1 << (pin - p5_0));
				}
			} else if (pin < p7_0) {
				if (value) {
					P6OUT |= (1 << (pin - p6_0));
				} else {
					P6OUT &= ~(1 << (pin - p6_0));
				}
			} else if (pin < p8_0) {
				if (value) {
					P7OUT |= (1 << (pin - p7_0));
				} else {
					P7OUT &= ~(1 << (pin - p7_0));
				}
			} else if (pin < pj_0) {
				if (value) {
					P8OUT |= (1 << (pin - p8_0));
				} else {
					P8OUT &= ~(1 << (pin - p8_0));
				}
			} else if (pin < PIN_INVALID) {
				if (value) {
					PJOUT |= (1 << (pin - pj_0));
				} else {
					PJOUT &= ~(1 << (pin - pj_0));
				}
			}
		}
		inline void write_mask(unsigned char const pin_base, unsigned char set_mask, unsigned char clear_mask) {
			if (pin_base < p2_0) {
				P1OUT = (P1OUT | set_mask) & ~clear_mask;
			} else if (pin_base < p3_0) {
				P2OUT = (P2OUT | set_mask) & ~clear_mask;
			} else if (pin_base < p4_0) {
				P3OUT = (P3OUT | set_mask) & ~clear_mask;
			} else if (pin_base < p5_0) {
				P4OUT = (P4OUT | set_mask) & ~clear_mask;
			} else if (pin_base < p6_0) {
				P5OUT = (P5OUT | set_mask) & ~clear_mask;
			} else if (pin_base < p7_0) {
				P6OUT = (P6OUT | set_mask) & ~clear_mask;
			} else if (pin_base < p8_0) {
				P7OUT = (P7OUT | set_mask) & ~clear_mask;
			} else if (pin_base < pj_0) {
				P8OUT = (P8OUT | set_mask) & ~clear_mask;
			} else if (pin_base < PIN_INVALID) {
				PJOUT = (PJOUT | set_mask) & ~clear_mask;
			}
		}
};

extern GPIO gpio;

#endif