Contiki 3.x
gpio.h
1 /*
2  * Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
3  * to the MC1322x project (http://mc1322x.devl.org)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the Institute nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * This file is part of libmc1322x: see http://mc1322x.devl.org
31  * for details.
32  *
33  *
34  */
35 
36 #ifndef GPIO_H
37 #define GPIO_H
38 
39 /* Structure-based GPIO access
40  Example usage:
41 
42  GPIO->FUNC_SEL0 |= 0x00008000; // set a whole register
43 
44  GPIO->FUNC_SEL_08 = 2; // set just one pin
45 
46  #define MY_PIN GPIO_08
47  GPIO->FUNC_SEL.MY_PIN = 2; // same, to allow #define for pin names
48  GPIO->DATA.MY_PIN = 1;
49 
50  gpio_set(GPIO_08); // efficiently set or clear a single output bit
51  gpio_reset(GPIO_08);
52 */
53 
54 // GPIO to Function Alias macros:
55 
56 #define ADC0 GPIO_30
57 #define ADC1 GPIO_31
58 #define ADC2 GPIO_32
59 #define ADC3 GPIO_33
60 #define ADC4 GPIO_34
61 #define ADC5 GPIO_35
62 #define ADC6 GPIO_36
63 #define ADC7 GPIO_37
64 #define TDO GPIO_49
65 #define TDI GPIO_48
66 #define TCK GPIO_47
67 #define TMS GPIO_46
68 #define U2RTS GPIO_21
69 #define U2CTS GPIO_20
70 #define U2RX GPIO_19
71 #define U2TX GPIO_18
72 #define U1RTS GPIO_17
73 #define U1CTS GPIO_16
74 #define U1RX GPIO_15
75 #define U1TX GPIO_14
76 #define SDA GPIO_13
77 #define SCL GPIO_12
78 #define TMR3 GPIO_11
79 #define TMR2 GPIO_10
80 #define TMR1 GPIO_09
81 #define TMR0 GPIO_08
82 #define SCK GPIO_07
83 #define MOSI GPIO_06
84 #define MISO GPIO_05
85 #define SS GPIO_04
86 #define BTCK GPIO_03
87 #define FSYN GPIO_02
88 #define SSIRX GPIO_01
89 #define SSITX GPIO_00
90 #define KBI7 GPIO_29
91 #define KBI6 GPIO_28
92 #define KBI5 GPIO_27
93 #define KBI4 GPIO_26
94 #define KBI3 GPIO_25
95 #define KBI2 GPIO_24
96 #define KBI1 GPIO_23
97 #define KBI0 GPIO_22
98 #define TXON GPIO_44
99 #define RXON GPIO_45
100 #define ANT1 GPIO_42
101 #define ANT2 GPIO_43
102 #define VREF2H GPIO_38
103 #define VREF2L GPIO_39
104 #define VREF1H GPIO_40
105 #define VREF1L GPIO_41
106 #define MDO0 GPIO_51
107 #define MDO1 GPIO_52
108 #define MDO2 GPIO_53
109 #define MDO3 GPIO_54
110 #define MDO4 GPIO_55
111 #define MDO5 GPIO_56
112 #define MDO6 GPIO_57
113 #define MDO7 GPIO_58
114 #define MSEO0 GPIO_59
115 #define MSEO1 GPIO_60
116 #define RDY GPIO_61
117 #define EVTO GPIO_62
118 #define MCKO GPIO_50
119 #define EVTI GPIO_63
120 
121 
122 
123 #define _V(x,n,i) uint32_t x##_##i : n;
124 #define _REP(x,n) \
125  _V(x,n,00) _V(x,n,01) _V(x,n,02) _V(x,n,03) _V(x,n,04) _V(x,n,05) _V(x,n,06) _V(x,n,07) \
126  _V(x,n,08) _V(x,n,09) _V(x,n,10) _V(x,n,11) _V(x,n,12) _V(x,n,13) _V(x,n,14) _V(x,n,15) \
127  _V(x,n,16) _V(x,n,17) _V(x,n,18) _V(x,n,19) _V(x,n,20) _V(x,n,21) _V(x,n,22) _V(x,n,23) \
128  _V(x,n,24) _V(x,n,25) _V(x,n,26) _V(x,n,27) _V(x,n,28) _V(x,n,29) _V(x,n,30) _V(x,n,31) \
129  _V(x,n,32) _V(x,n,33) _V(x,n,34) _V(x,n,35) _V(x,n,36) _V(x,n,37) _V(x,n,38) _V(x,n,39) \
130  _V(x,n,40) _V(x,n,41) _V(x,n,42) _V(x,n,43) _V(x,n,44) _V(x,n,45) _V(x,n,46) _V(x,n,47) \
131  _V(x,n,48) _V(x,n,49) _V(x,n,50) _V(x,n,51) _V(x,n,52) _V(x,n,53) _V(x,n,54) _V(x,n,55) \
132  _V(x,n,56) _V(x,n,57) _V(x,n,58) _V(x,n,59) _V(x,n,60) _V(x,n,61) _V(x,n,62) _V(x,n,63)
133 
134 struct GPIO_struct {
135 #define _IO(x) \
136  union { struct { uint32_t x##0; uint32_t x##1; }; \
137  struct { _REP(x, 1) }; \
138  struct GPIO_##x { _REP(GPIO, 1) } x; };
139 #define _IO_2bit(x) \
140  union { struct { uint32_t x##0; uint32_t x##1; uint32_t x##2; uint32_t x##3; }; \
141  struct { _REP(x, 2) }; \
142  struct GPIO_##x { _REP(GPIO, 2) } x; };
143 
144  _IO(PAD_DIR);
145  _IO(DATA);
146  _IO(PAD_PU_EN);
147  _IO_2bit(FUNC_SEL);
148  _IO(DATA_SEL);
149  _IO(PAD_PU_SEL);
150  _IO(PAD_HYST_EN);
151  _IO(PAD_KEEP);
152  _IO(DATA_SET);
153  _IO(DATA_RESET);
154  _IO(PAD_DIR_SET);
155  _IO(PAD_DIR_RESET);
156 };
157 #undef _IO
158 #undef _IO_2bit
159 
160 /* Build an enum lookup to map GPIO_08 -> 8 */
161 #undef _V
162 #define _V(x,n,i) __NUM_GPIO_GPIO_##i,
163 enum { _REP(0,0) };
164 
165 /* Macros to set or reset a data pin in the fastest possible way */
166 #define gpio_set(gpio_xx) __gpio_set(gpio_xx)
167 #define __gpio_set(gpio_xx) \
168  ((__NUM_GPIO_##gpio_xx < 32) \
169  ? (GPIO->DATA_SET0 = (1 << (__NUM_GPIO_##gpio_xx - 0))) \
170  : (GPIO->DATA_SET1 = (1 << (__NUM_GPIO_##gpio_xx - 32))))
171 #define gpio_reset(gpio_xx) __gpio_reset(gpio_xx)
172 #define __gpio_reset(gpio_xx) \
173  ((__NUM_GPIO_##gpio_xx < 32) \
174  ? (GPIO->DATA_RESET0 = (1 << (__NUM_GPIO_##gpio_xx - 0))) \
175  : (GPIO->DATA_RESET1 = (1 << (__NUM_GPIO_##gpio_xx - 32))))
176 
177 #undef _REP
178 #undef _V
179 
180 static volatile struct GPIO_struct * const GPIO = (void *) (0x80000000);
181 
182 
183 /* Old register definitions, for compatibility */
184 #ifndef REG_NO_COMPAT
185 
186 #define GPIO_PAD_DIR0 ((volatile uint32_t *) 0x80000000)
187 #define GPIO_PAD_DIR1 ((volatile uint32_t *) 0x80000004)
188 #define GPIO_DATA0 ((volatile uint32_t *) 0x80000008)
189 #define GPIO_DATA1 ((volatile uint32_t *) 0x8000000c)
190 #define GPIO_PAD_PU_EN0 ((volatile uint32_t *) 0x80000010)
191 #define GPIO_PAD_PU_EN1 ((volatile uint32_t *) 0x80000014)
192 #define GPIO_FUNC_SEL0 ((volatile uint32_t *) 0x80000018) /* GPIO 15 - 0; 2 bit blocks */
193 #define GPIO_FUNC_SEL1 ((volatile uint32_t *) 0x8000001c) /* GPIO 16 - 31; 2 bit blocks */
194 #define GPIO_FUNC_SEL2 ((volatile uint32_t *) 0x80000020) /* GPIO 32 - 47; 2 bit blocks */
195 #define GPIO_FUNC_SEL3 ((volatile uint32_t *) 0x80000024) /* GPIO 48 - 63; 2 bit blocks */
196 #define GPIO_DATA_SEL0 ((volatile uint32_t *) 0x80000028)
197 #define GPIO_DATA_SEL1 ((volatile uint32_t *) 0x8000002c)
198 #define GPIO_PAD_PU_SEL0 ((volatile uint32_t *) 0x80000030)
199 #define GPIO_PAD_PU_SEL1 ((volatile uint32_t *) 0x80000034)
200 
201 #define GPIO_DATA_SET0 ((volatile uint32_t *) 0x80000048)
202 #define GPIO_DATA_SET1 ((volatile uint32_t *) 0x8000004c)
203 #define GPIO_DATA_RESET0 ((volatile uint32_t *) 0x80000050)
204 #define GPIO_DATA_RESET1 ((volatile uint32_t *) 0x80000054)
205 #define GPIO_PAD_DIR_SET0 ((volatile uint32_t *) 0x80000058)
206 #define GPIO_PAD_DIR_SET1 ((volatile uint32_t *) 0x8000005c)
207 #define GPIO_PAD_DIR_RESET0 ((volatile uint32_t *) 0x80000060)
208 #define GPIO_PAD_DIR_RESET1 ((volatile uint32_t *) 0x80000064)
209 
210 inline void gpio_pad_dir(volatile uint64_t data);
211 inline void gpio_data(volatile uint64_t data);
212 inline uint64_t gpio_data_get(volatile uint64_t bits);
213 inline void gpio_pad_pu_en(volatile uint64_t data);
214 inline void gpio_data_sel(volatile uint64_t data);
215 inline void gpio_data_pu_sel(volatile uint64_t data);
216 inline void gpio_data_set(volatile uint64_t data);
217 inline void gpio_data_reset(volatile uint64_t data);
218 inline void gpio_pad_dir_set(volatile uint64_t data);
219 inline void gpio_pad_dir_reset(volatile uint64_t data);
220 
221 /* select pullup or pulldown for GPIO 0-31 (b=0-31) */
222 #define gpio_sel0_pullup(b) (set_bit(*GPIO_PAD_PU_SEL0,b))
223 #define gpio_sel0_pulldown(b) (clear_bit(*GPIO_PAD_PU_SEL0,b))
224 
225 /* select pullup or pulldown for GPIO 32-63 (b=32-63) */
226 #define gpio_sel1_pullup(b) (set_bit(*GPIO_PAD_PU_SEL1,b-32))
227 #define gpio_sel1_pulldown(b) (clear_bit(*GPIO_PAD_PU_SEL1,b-32))
228 
229 /* enable/disable pullup for GPIO 0-31 (b=0-31) */
230 #define gpio_pu0_enable(b) (set_bit(*GPIO_PAD_PU_EN0,b))
231 #define gpio_pu0_disable(b) (clear_bit(*GPIO_PAD_PU_EN0,b))
232 
233 /* enable/disable pullup for GPIO 32-63 (b=32-63) */
234 #define gpio_pu1_enable(b) (set_bit(*GPIO_PAD_PU_EN1,b-32))
235 #define gpio_pu1_disable(b) (clear_bit(*GPIO_PAD_PU_EN1,b-32))
236 
237 #endif /* REG_NO_COMPAT */
238 
239 #endif