1 /******************************************************************************
2 * Copyright (c) 2010 Texas Instruments Incorporated - http://www.ti.com
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 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
14 * distribution.
15 *
16 * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 *****************************************************************************/
34 /******************************************************************************
35 *
36 * File Name: gpio.c
37 *
38 * Description: This file contains the lower level function to access GPIO
39 *
40 * History:
41 * JUL/22/2009, Amit Solanki, Created the file
42 * SEP/01/2009, Amit Solanki, Updated the documentation
43 *
44 *****************************************************************************/
45 /****************
46 * Include Files
47 ****************/
48 #include "types.h"
49 #include "gpio.h"
50 #include "target.h"
52 /******************************************************************************
53 *
54 * Function: hwGpioSetDirection
55 *
56 * Description: This function configures the specified GPIO's direction
57 *
58 * Parameters: uiNumber - GPIO number to configure
59 * direction - GPIO_OUT or GPIO_IN
60 *
61 * Return Value: void
62 *
63 *****************************************************************************/
64 void hwGpioSetDirection( Uint32 uiNumber, GpioDirection direction )
65 {
66 Uint32* puiGpioDir = ( Uint32*)GPIO_DIR_REG;
68 if ( direction == GPIO_OUT )
69 *puiGpioDir = *puiGpioDir & ~(1 << uiNumber); // Set to OUTPUT
70 else
71 *puiGpioDir = *puiGpioDir | (1 << uiNumber); // Set to INTPUT
72 }
74 /******************************************************************************
75 *
76 * Function: hwGpioSetDataBusDirection
77 *
78 * Description: This function configures the GPIO[7:0]'s direction
79 *
80 * Parameters: direction - GPIO_OUT or GPIO_IN
81 *
82 * Return Value: void
83 *
84 *****************************************************************************/
85 void hwGpioSetDataBusDirection( GpioDirection direction )
86 {
87 Uint32* puiGpioDir = ( Uint32*)GPIO_DIR_REG;
89 if ( direction == GPIO_OUT )
90 *puiGpioDir = *puiGpioDir & 0xffffff00; // Set to OUTPUT
91 else
92 *puiGpioDir = *puiGpioDir | 0x000000ff; // Set to INTPUT
93 }
95 /******************************************************************************
96 *
97 * Function: hwGpioSetOutput
98 *
99 * Description: This function sets the specified GPIO's pin state to 1
100 *
101 * Parameters: uiNumber - GPIO number to configure
102 *
103 * Return Value: void
104 *
105 * Pre-Condition: The specified GPIO should be configured as output
106 *
107 *****************************************************************************/
108 void hwGpioSetOutput( Uint32 uiNumber)
109 {
110 Uint32* puiGpioSet = (Uint32*) GPIO_SET_DATA_REG;
111 *puiGpioSet = ( 1 << (uiNumber % 32) ); // Set to 1
112 }
114 /******************************************************************************
115 *
116 * Function: hwGpioClearOutput
117 *
118 * Description: This function Clears the specified GPIO's pin state to 0
119 *
120 * Parameters: uiNumber - GPIO number to configure
121 *
122 * Return Value: void
123 *
124 * Pre-Condition: The specified GPIO should be configured as output
125 *
126 *****************************************************************************/
127 void hwGpioClearOutput( Uint32 uiNumber)
128 {
129 Uint32* puiGpioClear = (Uint32*) GPIO_CLEAR_DATA_REG;
130 *puiGpioClear = ( 1 << (uiNumber % 32) ); // Clear to 0
131 }
133 /******************************************************************************
134 *
135 * Function: hwGpioReadInput
136 *
137 * Description: This function gets the specified GPIO's pin state
138 *
139 * Parameters: uiNumber - GPIO number to configure
140 *
141 * Return Value: Uint32 - Input state of GPIO if success
142 * - else GPIO status
143 *
144 * Pre-Condition: The specified GPIO should be configured as input
145 *
146 *****************************************************************************/
147 Uint32 hwGpioReadInput( Uint32 uiNumber )
148 {
149 Uint32* puiGpioInput = (Uint32*)GPIO_IN_DATA_REG;
150 if(uiNumber > GPIO_MAX_NUMBER)
151 return INVALID_GPIO_NUMBER;
153 if( ( (*puiGpioInput >> uiNumber) & GPIO_HIGH) == GPIO_HIGH)
154 return GPIO_HIGH;
155 else
156 return GPIO_LOW;
157 }
159 /******************************************************************************
160 *
161 * Function: hwGpioWriteDataBus
162 *
163 * Description: This function sets the GPIO[7:0] pins state
164 *
165 * Parameters: uchValue - Value to set as output
166 *
167 * Return Value: void
168 *
169 * Pre-Condition: The GPIO[7:0] should be configured as output
170 *
171 *****************************************************************************/
172 void hwGpioWriteDataBus ( Uint8 uchValue )
173 {
174 Uint32* puiGpioOutput = (Uint32*) GPIO_OUT_DATA_REG;
175 *puiGpioOutput = ( (*puiGpioOutput & 0xffffff00) | uchValue );
176 }
178 /******************************************************************************
179 *
180 * Function: hwGpioClearDataBus
181 *
182 * Description: This function sets the value to the Clear data register
183 *
184 * Parameters: uchValue - Value to set as output
185 *
186 * Return Value: void
187 *
188 * Pre-Condition: None
189 *
190 *****************************************************************************/
191 void hwGpioClearDataBus(Uint8 uchValue)
192 {
193 Uint32* puiGpioOutput = (Uint32*) GPIO_CLEAR_DATA_REG;
194 *puiGpioOutput = uchValue;
195 }
197 /******************************************************************************
198 *
199 * Function: hwGpioSetDataBus
200 *
201 * Description: This function sets the value to the Set data register
202 *
203 * Parameters: uchValue - Value to set as output
204 *
205 * Return Value: void
206 *
207 * Pre-Condition: None
208 *
209 *****************************************************************************/
210 void hwGpioSetDataBus(Uint8 uchValue)
211 {
212 Uint32* puiGpioOutput = (Uint32*) GPIO_SET_DATA_REG;
213 *puiGpioOutput = uchValue;
214 }
216 /******************************************************************************
217 *
218 * Function: hwGpioReadDataBus
219 *
220 * Description: This function gets the GPIO[7:0] pins state
221 *
222 * Parameters: void
223 *
224 * Return Value: Uint8 - Input state of GPIO[7:0]
225 *
226 * Pre-Condition: The GPIO[7:0] should be configured as input
227 *
228 *****************************************************************************/
229 Uint8 hwGpioReadDataBus( void )
230 {
231 Uint8* puchGpioInput = (Uint8*)GPIO_IN_DATA_REG;
232 return *puchGpioInput;
233 }
235 /******************************************************************************
236 *
237 * Function: hwGpioEnableGlobalInterrupt
238 *
239 * Description: This function Enables GPIO interrupts to CPU
240 *
241 * Parameters: void
242 *
243 * Return Value: void
244 *
245 *****************************************************************************/
246 void hwGpioEnableGlobalInterrupt( void )
247 {
248 Uint32* puiGpioInterrupt = (Uint32*)GPIO_BINTEN_REG;
249 *puiGpioInterrupt = 0x01;
250 }
252 /******************************************************************************
253 *
254 * Function: hwGpioDisableGlobalInterrupt
255 *
256 * Description: This function Disables GPIO interrupts to CPU
257 *
258 * Parameters: void
259 *
260 * Return Value: void
261 *
262 *****************************************************************************/
263 void hwGpioDisableGlobalInterrupt( void )
264 {
265 Uint32* puiGpioInterrupt = (Uint32*)GPIO_BINTEN_REG;
266 *puiGpioInterrupt = 0x00;
267 }
269 /******************************************************************************
270 *
271 * Function: hwGpioSetRisingEdgeInterrupt
272 *
273 * Description: This function sets specified GPIO's rising edge interrupt
274 *
275 * Parameters: uiNumber - GPIO number to configure
276 *
277 * Return Value: void
278 *
279 *****************************************************************************/
280 void hwGpioSetRisingEdgeInterrupt( Uint32 uiNumber )
281 {
282 Uint32* puiGpioSetRisingEdge = (Uint32*)GPIO_SET_RIS_TRIG_REG;
283 *puiGpioSetRisingEdge = (1 << uiNumber);
284 }
286 /******************************************************************************
287 *
288 * Function: hwGpioClearRisingEdgeInterrupt
289 *
290 * Description: This function clears specified GPIO's rising edge interrupt
291 *
292 * Parameters: uiNumber - GPIO number to configure
293 *
294 * Return Value: void
295 *
296 *****************************************************************************/
297 void hwGpioClearRisingEdgeInterrupt( Uint32 uiNumber )
298 {
299 Uint32* puiGpioClearRisingEdge = (Uint32*)GPIO_CLR_RIS_TRIG_REG;
300 *puiGpioClearRisingEdge = (1 << uiNumber);
301 }
303 /******************************************************************************
304 *
305 * Function: hwGpioSetFallingEdgeInterrupt
306 *
307 * Description: This function sets specified GPIO's falling edge interrupt
308 *
309 * Parameters: uiNumber - GPIO number to configure
310 *
311 * Return Value: void
312 *
313 *****************************************************************************/
314 void hwGpioSetFallingEdgeInterrupt( Uint32 uiNumber )
315 {
316 Uint32* puiGpioSetFallingEdge = (Uint32*)GPIO_SET_FAL_TRIG_REG;
317 *puiGpioSetFallingEdge = (1 << uiNumber);
318 }
320 /******************************************************************************
321 *
322 * Function: hwGpioClearFallingEdgeInterrupt
323 *
324 * Description: This function clears specified GPIO's falling edge interrupt
325 *
326 * Parameters: uiNumber - GPIO number to configure
327 *
328 * Return Value: void
329 *
330 *****************************************************************************/
331 void hwGpioClearFallingEdgeInterrupt( Uint32 uiNumber )
332 {
333 Uint32* puiGpioClearFallingEdge = (Uint32*)GPIO_CLR_FAL_TRIG_REG;
334 *puiGpioClearFallingEdge = (1 << uiNumber);
335 }