1 | //========================================================================== |
---|
2 | // |
---|
3 | // mx31_gpio.c |
---|
4 | // |
---|
5 | // |
---|
6 | // Author(s): Luis Torrico, Cogent Computer Systems, Inc. |
---|
7 | // Contributors: |
---|
8 | // Date: 05/09/2008 |
---|
9 | // Description: This file contains code to intialize the MCIMX31 GPIO |
---|
10 | // section as well as functions for manipulating the GPIO |
---|
11 | // bits |
---|
12 | // |
---|
13 | //-------------------------------------------------------------------------- |
---|
14 | |
---|
15 | #include "config.h" |
---|
16 | #include "cpuio.h" |
---|
17 | #include "stddefs.h" |
---|
18 | #include "genlib.h" |
---|
19 | #include "omap3530.h" |
---|
20 | #include "cpu_gpio.h" // pull in target board specific header |
---|
21 | |
---|
22 | //#define GPIO_DBG |
---|
23 | |
---|
24 | //-------------------------------------------------------- |
---|
25 | // GPIO_init() |
---|
26 | // |
---|
27 | // This function sets the startup state for the MCIMX31 GPIO |
---|
28 | // registers as used on the target CPU. Refer to cpu_gpio.h |
---|
29 | // for a description of the default values. Here we just put |
---|
30 | // them into the chip in the following order: |
---|
31 | // 1. Port x DR - Data Register |
---|
32 | // 2. Port x DIR - Direction Register |
---|
33 | // 3. Port x PSTAT - Pad Status Register |
---|
34 | // 4. Port x ICR1 - GPIO Interrupt Configuration Register 1 |
---|
35 | // 5. Port x ICR2 - GPIO Interrupt Configuration Register 2 |
---|
36 | // 6. Port x IMASK - GPIO Interrupt Mask Register |
---|
37 | // 7. Port x ISTAT - GPIO Interrupt Status Register |
---|
38 | |
---|
39 | void GPIO_init() |
---|
40 | { |
---|
41 | // Port 1 |
---|
42 | GPIO1_REG(GPIO_OE) = PORT1_OE; |
---|
43 | GPIO1_REG(GPIO_DATAOUT) = PORT1_DR; |
---|
44 | //GPIO1_REG(GPIO_CLEARDATAOUT) = 0; |
---|
45 | //GPIO1_REG(GPIO_SETDATAOUT) = 0; |
---|
46 | |
---|
47 | // Port 2 |
---|
48 | //GPIO2_REG(GPIO_OE) = 0xFEFFFFFF; |
---|
49 | GPIO2_REG(GPIO_OE) = PORT2_OE; |
---|
50 | GPIO2_REG(GPIO_DATAOUT) = PORT2_DR; |
---|
51 | //GPIO2_REG(GPIO_CLEARDATAOUT) = 0; |
---|
52 | //GPIO2_REG(GPIO_SETDATAOUT) = 0; |
---|
53 | |
---|
54 | // Port 3 |
---|
55 | GPIO3_REG(GPIO_OE) = PORT3_OE; |
---|
56 | GPIO3_REG(GPIO_DATAOUT) = PORT3_DR; |
---|
57 | //GPIO3_REG(GPIO_CLEARDATAOUT) = 0; |
---|
58 | //GPIO3_REG(GPIO_SETDATAOUT) = 0; |
---|
59 | |
---|
60 | // Port 4 |
---|
61 | GPIO4_REG(GPIO_OE) = PORT4_OE; |
---|
62 | GPIO4_REG(GPIO_DATAOUT) = PORT4_DR; |
---|
63 | //GPIO4_REG(GPIO_CLEARDATAOUT) = 0; |
---|
64 | //GPIO4_REG(GPIO_SETDATAOUT) = 0; |
---|
65 | |
---|
66 | // Port 5 |
---|
67 | GPIO5_REG(GPIO_OE) = PORT5_OE; |
---|
68 | GPIO5_REG(GPIO_DATAOUT) = PORT5_DR; |
---|
69 | //GPIO5_REG(GPIO_CLEARDATAOUT) = 0; |
---|
70 | //GPIO5_REG(GPIO_SETDATAOUT) = 0; |
---|
71 | |
---|
72 | // Port 6 |
---|
73 | GPIO6_REG(GPIO_OE) = PORT6_OE; |
---|
74 | GPIO6_REG(GPIO_DATAOUT) = PORT6_DR; |
---|
75 | //GPIO6_REG(GPIO_CLEARDATAOUT) = 0; |
---|
76 | //GPIO6_REG(GPIO_SETDATAOUT) = 0; |
---|
77 | } |
---|
78 | |
---|
79 | //-------------------------------------------------------- |
---|
80 | // GPIO_set() |
---|
81 | // |
---|
82 | // This function sets the desired bit passed in. |
---|
83 | // NOTE: We do not test to see if setting the bit |
---|
84 | // would screw up any alternate functions. Use |
---|
85 | // this function with caution! |
---|
86 | // |
---|
87 | |
---|
88 | int GPIO_set(int gpio_bit) |
---|
89 | { |
---|
90 | // quick sanity test |
---|
91 | #ifdef GPIO_DBG |
---|
92 | printf("GPIO_set %d.\n", gpio_bit); |
---|
93 | #endif |
---|
94 | if (gpio_bit > 191) return -1; |
---|
95 | |
---|
96 | if (gpio_bit < 32) |
---|
97 | { |
---|
98 | // Port 1 |
---|
99 | GPIO1_REG(GPIO_DATAOUT) |= (1 << (gpio_bit - 0)); |
---|
100 | } |
---|
101 | else if (gpio_bit < 64) |
---|
102 | { |
---|
103 | // Port 2 |
---|
104 | GPIO2_REG(GPIO_DATAOUT) |= (1 << (gpio_bit - 32)); |
---|
105 | } |
---|
106 | else if (gpio_bit < 96) |
---|
107 | { |
---|
108 | // Port 3 |
---|
109 | GPIO3_REG(GPIO_DATAOUT) |= (1 << (gpio_bit - 64)); |
---|
110 | } |
---|
111 | else if (gpio_bit < 128) |
---|
112 | { |
---|
113 | // Port 4 |
---|
114 | GPIO4_REG(GPIO_DATAOUT) |= (1 << (gpio_bit - 96)); |
---|
115 | } |
---|
116 | else if (gpio_bit < 160) |
---|
117 | { |
---|
118 | // Port 5 |
---|
119 | GPIO5_REG(GPIO_DATAOUT) |= (1 << (gpio_bit - 128)); |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | // Port 6 |
---|
124 | GPIO6_REG(GPIO_DATAOUT) |= (1 << (gpio_bit - 160)); |
---|
125 | } |
---|
126 | return 0; |
---|
127 | } |
---|
128 | |
---|
129 | //-------------------------------------------------------- |
---|
130 | // GPIO_clr() |
---|
131 | // |
---|
132 | // This function clears the desired bit passed in. |
---|
133 | // |
---|
134 | |
---|
135 | int GPIO_clr(int gpio_bit) |
---|
136 | { |
---|
137 | #ifdef GPIO_DBG |
---|
138 | printf("GPIO_clr %d.\n", gpio_bit); |
---|
139 | #endif |
---|
140 | // quick sanity test |
---|
141 | if (gpio_bit > 191) return -1; |
---|
142 | |
---|
143 | if (gpio_bit < 32) |
---|
144 | { |
---|
145 | // Port 1 |
---|
146 | GPIO1_REG(GPIO_DATAOUT) &= ~(1 << (gpio_bit - 0)); |
---|
147 | } |
---|
148 | else if (gpio_bit < 64) |
---|
149 | { |
---|
150 | // Port 2 |
---|
151 | GPIO2_REG(GPIO_DATAOUT) &= ~(1 << (gpio_bit - 32)); |
---|
152 | } |
---|
153 | else if (gpio_bit < 96) |
---|
154 | { |
---|
155 | // Port 3 |
---|
156 | GPIO3_REG(GPIO_DATAOUT) &= ~(1 << (gpio_bit - 64)); |
---|
157 | } |
---|
158 | else if (gpio_bit < 128) |
---|
159 | { |
---|
160 | // Port 4 |
---|
161 | GPIO4_REG(GPIO_DATAOUT) &= ~(1 << (gpio_bit - 96)); |
---|
162 | } |
---|
163 | else if (gpio_bit < 160) |
---|
164 | { |
---|
165 | // Port 5 |
---|
166 | GPIO5_REG(GPIO_DATAOUT) &= ~(1 << (gpio_bit - 128)); |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | // Port 6 |
---|
171 | GPIO6_REG(GPIO_DATAOUT) &= ~(1 << (gpio_bit - 160)); |
---|
172 | } |
---|
173 | return 0; |
---|
174 | } |
---|
175 | //-------------------------------------------------------- |
---|
176 | // GPIO_tst() |
---|
177 | // |
---|
178 | // This function returns the state of desired bit passed in. |
---|
179 | // It does not test to see if it's an input or output and thus |
---|
180 | // can be used to verify if an output set/clr has taken place |
---|
181 | // as well as for testing an input state. |
---|
182 | // |
---|
183 | |
---|
184 | int GPIO_tst(int gpio_bit) |
---|
185 | { |
---|
186 | #ifdef GPIO_DBG |
---|
187 | printf("GPIO_tst %d.\n", gpio_bit); |
---|
188 | #endif |
---|
189 | // quick sanity test |
---|
190 | if (gpio_bit > 191) return -1; |
---|
191 | |
---|
192 | if (gpio_bit < 32) |
---|
193 | { |
---|
194 | // Port 1 |
---|
195 | if (GPIO1_REG(GPIO_DATAIN) & (1 << (gpio_bit - 0))) return 1; |
---|
196 | } |
---|
197 | else if (gpio_bit < 64) |
---|
198 | { |
---|
199 | // Port 2 |
---|
200 | if (GPIO2_REG(GPIO_DATAIN) & (1 << (gpio_bit - 32))) return 1; |
---|
201 | } |
---|
202 | else if (gpio_bit < 96) |
---|
203 | { |
---|
204 | // Port 3 |
---|
205 | if (GPIO3_REG(GPIO_DATAIN) & (1 << (gpio_bit - 64))) return 1; |
---|
206 | } |
---|
207 | else if (gpio_bit < 128) |
---|
208 | { |
---|
209 | // Port 4 |
---|
210 | if (GPIO4_REG(GPIO_DATAIN) & (1 << (gpio_bit - 96))) return 1; |
---|
211 | } |
---|
212 | else if (gpio_bit < 160) |
---|
213 | { |
---|
214 | // Port 5 |
---|
215 | if (GPIO5_REG(GPIO_DATAIN) & (1 << (gpio_bit - 128))) return 1; |
---|
216 | } |
---|
217 | else |
---|
218 | { |
---|
219 | // Port 6 |
---|
220 | if (GPIO6_REG(GPIO_DATAIN) & (1 << (gpio_bit - 160))) return 1; |
---|
221 | } |
---|
222 | return 0; // bit was not set |
---|
223 | } |
---|
224 | |
---|
225 | //-------------------------------------------------------- |
---|
226 | // GPIO_out() |
---|
227 | // |
---|
228 | // This function changes the direction of the desired bit |
---|
229 | // to output. NOTE: We do not test to see if changing the |
---|
230 | // direction of the bit would screw up anything. Use this |
---|
231 | // function with caution! |
---|
232 | // |
---|
233 | // This only worlks if the GPIO has been defined as a GPIO |
---|
234 | // during init. It will not override the init setting, only |
---|
235 | // change the direction bit |
---|
236 | |
---|
237 | int GPIO_out(int gpio_bit) |
---|
238 | { |
---|
239 | #ifdef GPIO_DBG |
---|
240 | printf("GPIO_out %d.\n", gpio_bit); |
---|
241 | #endif |
---|
242 | // quick sanity test |
---|
243 | if (gpio_bit > 191) return -1; |
---|
244 | |
---|
245 | if (gpio_bit < 32) |
---|
246 | { |
---|
247 | // Port 1 |
---|
248 | GPIO1_REG(GPIO_OE) &= ~(1 << (gpio_bit - 0)); |
---|
249 | } |
---|
250 | else if (gpio_bit < 64) |
---|
251 | { |
---|
252 | // Port 2 |
---|
253 | GPIO2_REG(GPIO_OE) &= ~(1 << (gpio_bit - 32)); |
---|
254 | } |
---|
255 | else if (gpio_bit < 96) |
---|
256 | { |
---|
257 | // Port 3 |
---|
258 | GPIO3_REG(GPIO_OE) &= ~(1 << (gpio_bit - 64)); |
---|
259 | } |
---|
260 | else if (gpio_bit < 128) |
---|
261 | { |
---|
262 | // Port 4 |
---|
263 | GPIO4_REG(GPIO_OE) &= ~(1 << (gpio_bit - 96)); |
---|
264 | } |
---|
265 | else if (gpio_bit < 160) |
---|
266 | { |
---|
267 | // Port 5 |
---|
268 | GPIO5_REG(GPIO_OE) &= ~(1 << (gpio_bit - 128)); |
---|
269 | } |
---|
270 | else |
---|
271 | { |
---|
272 | // Port 6 |
---|
273 | GPIO6_REG(GPIO_OE) &= ~(1 << (gpio_bit - 160)); |
---|
274 | } |
---|
275 | return 0; |
---|
276 | } |
---|
277 | |
---|
278 | //-------------------------------------------------------- |
---|
279 | // GPIO_in() |
---|
280 | // |
---|
281 | // This function changes the direction of the desired bit |
---|
282 | // to input. NOTE: We do not test to see if changing the |
---|
283 | // direction of the bit would screw up anything. Use this |
---|
284 | // function with caution! |
---|
285 | // |
---|
286 | // This only worlks if the GPIO has been defined as a GPIO |
---|
287 | // during init. It will not override the init setting, only |
---|
288 | // change the direction bit |
---|
289 | int GPIO_in(int gpio_bit) |
---|
290 | { |
---|
291 | #ifdef GPIO_DBG |
---|
292 | printf("GPIO_in %d.\n", gpio_bit); |
---|
293 | #endif |
---|
294 | // quick sanity test |
---|
295 | if (gpio_bit > 191) return -1; |
---|
296 | |
---|
297 | if (gpio_bit < 32) |
---|
298 | { |
---|
299 | // Port 1 |
---|
300 | GPIO1_REG(GPIO_OE) |= (1 << (gpio_bit - 0)); |
---|
301 | } |
---|
302 | else if (gpio_bit < 64) |
---|
303 | { |
---|
304 | // Port 2 |
---|
305 | GPIO2_REG(GPIO_OE) |= (1 << (gpio_bit - 32)); |
---|
306 | } |
---|
307 | else if (gpio_bit < 96) |
---|
308 | { |
---|
309 | // Port 3 |
---|
310 | GPIO3_REG(GPIO_OE) |= (1 << (gpio_bit - 64)); |
---|
311 | } |
---|
312 | else if (gpio_bit < 128) |
---|
313 | { |
---|
314 | // Port 4 |
---|
315 | GPIO4_REG(GPIO_OE) |= (1 << (gpio_bit - 96)); |
---|
316 | } |
---|
317 | else if (gpio_bit < 160) |
---|
318 | { |
---|
319 | // Port 5 |
---|
320 | GPIO5_REG(GPIO_OE) |= (1 << (gpio_bit - 128)); |
---|
321 | } |
---|
322 | else |
---|
323 | { |
---|
324 | // Port 6 |
---|
325 | GPIO6_REG(GPIO_OE) |= (1 << (gpio_bit - 160)); |
---|
326 | } |
---|
327 | return 0; |
---|
328 | } |
---|
329 | |
---|