summaryrefslogtreecommitdiff
path: root/hardware.h
blob: ce1d8287370428c1ca61128a0a7148a5605b6f74 (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
/*
 *  XIOH power sequence
 *  Copyright (C) 2013  Avencall
 *
 *  hardware.h - platform definitions
 *  Author:
 *    Guillaume Knispel
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef HARDWARE_H
#define HARDWARE_H

enum port_gpio {
	P1, P2, P3, P4,	// not really used; might detect spurious defines
};

enum gpio_dir {		// fixed values
	DIR_IN = 0,
	DIR_OUT = 1,
};

enum gpio_ren {		// fixed values
	REN_NO = 0,
	REN_PULL = 1,
};

enum gpio_sel {		// fixed values
	SEL_GPIO = 0,
	SEL_PERIPH = 1,
};


/* macros for dir_ren_sel */
#define GPIO_IN_FLOAT	DIR_IN,  REN_NO,   SEL_GPIO
#define GPIO_IN_PULL	DIR_IN,  REN_PULL, SEL_GPIO
#define GPIO_OUT	DIR_OUT, REN_NO,   SEL_GPIO
#define PERIPH		DIR_IN,  REN_NO,   SEL_PERIPH

/*
for i in xrange(1, 5):
    for b in xrange(0, 8):
        print "#define P" + str(i) + "_" + str(b) + \
              "\t\tP" + str(i) + ", BIT" + str(b)
    print
*/

#define P1_0		P1, BIT0
#define P1_1		P1, BIT1
#define P1_2		P1, BIT2
#define P1_3		P1, BIT3
#define P1_4		P1, BIT4
#define P1_5		P1, BIT5
#define P1_6		P1, BIT6
#define P1_7		P1, BIT7

#define P2_0		P2, BIT0
#define P2_1		P2, BIT1
#define P2_2		P2, BIT2
#define P2_3		P2, BIT3
#define P2_4		P2, BIT4
#define P2_5		P2, BIT5
#define P2_6		P2, BIT6
#define P2_7		P2, BIT7

#define P3_0		P3, BIT0
#define P3_1		P3, BIT1
#define P3_2		P3, BIT2
#define P3_3		P3, BIT3
#define P3_4		P3, BIT4
#define P3_5		P3, BIT5
#define P3_6		P3, BIT6
#define P3_7		P3, BIT7

#define P4_0		P4, BIT0
#define P4_1		P4, BIT1
#define P4_2		P4, BIT2
#define P4_3		P4, BIT3
#define P4_4		P4, BIT4
#define P4_5		P4, BIT5
#define P4_6		P4, BIT6
#define P4_7		P4, BIT7

/**
 * GD(w, port, bit, dir, ren, sel, out) - Gpio Descriptor
 *
 * GD describes both on which pin the signal is and the initial
 * configuration to be given to it.
 *
 * w: Propagated from the parameter of the signal symbol
 *    At use time, this selects in which way the macro expands
 *
 * port: one of P1 to P4 - port in which the pin is
 * bit: one of BIT0 to BIT7 - pin in the port
 *
 * =WARNING= see MSP430 datasheets and errata for restrictions
 * on the following parameters
 *
 * dir: DIR_IN or DIR_OUT
 *      Initial value in register PxDIR
 *      Drive the output if DIR_OUT (only if ren=REN_NO)
 *
 * ren: REN_NO or REN_PULL
 *      Initial value in register PxREN
 *      Activate pull down / up if REN_PULL
 *
 * sel: SEL_GPIO or SEL_PERIPH
 *      Initial value in register PxSEL
 *
 * out: 0 or 1
 *      Initial value in register PxOUT
 *      If sel=SEL_GPIO:
 *          If ren=REN_PULL: Pull down or up
 *          Else If dir=DIR_OUT: Drive low or high
 */
#define GD(w, port, bit, dir, ren, sel, out) \
	GD__##w(port, bit, dir, ren, sel, out)

/**
 * GD_(w, port_bit, dir_ren_sel, out) - Like GD(), except shorter
 *
 * GD_ lets you use shortcuts to describe signals and pins more concisely.
 *
 * w: Propagated from the parameter of the signal symbol
 *    At use time, this selects in which way the macro expands
 *
 * port_bit: for ex. P1_5 means bit 5 on port 1
 *
 * dir_ren_sel: for ex. GPIO_IN_FLOAT or PERIPH
 *
 * out: 0 or 1, see description in GD()
 */
#define GD_(w, port_bit, dir_ren_sel, out) \
	GD(w, port_bit, dir_ren_sel, out)

#define GD__out(port, bit, dir, ren, sel, out)	(port##OUT), (bit)
#define GD__ren(port, bit, dir, ren, sel, out)	(port##REN), (bit)
#define GD__sel(port, bit, dir, ren, sel, out)	(port##SEL), (bit)
#define GD__dir(port, bit, dir, ren, sel, out)	(port##DIR), (bit)

#define GD__in(port, bit, dir, ren, sel, out)	(!!((port##IN) & (bit)))

#define GD__inreg(port, bit, dir, ren, sel, out) (port##IN)
#define GD__bit(port, bit, dir, ren, sel, out)	(bit)

#define SetBit(reg, bit)	((reg) |= (bit))
#define ClrBit(reg, bit)	((reg) &= (~(bit)))

#define Set(reg_bit)		SetBit(reg_bit)
#define Clr(reg_bit)		ClrBit(reg_bit)

#if defined(XIOH_V5)
# include "xioh_v5.h"
#elif defined(XIOH_V6)
# include "xioh_v6.h"
#endif

#endif //HARDWARE_H