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 Uint32 temp;
232 Uint32* puchGpioInput = (Uint32*)GPIO_IN_DATA_REG;
233 temp = *puchGpioInput;
234 temp = temp & 0xff;
235 return ((Uint8)temp);
236 }
238 /******************************************************************************
239 *
240 * Function: hwGpioEnableGlobalInterrupt
241 *
242 * Description: This function Enables GPIO interrupts to CPU
243 *
244 * Parameters: void
245 *
246 * Return Value: void
247 *
248 *****************************************************************************/
249 void hwGpioEnableGlobalInterrupt( void )
250 {
251 Uint32* puiGpioInterrupt = (Uint32*)GPIO_BINTEN_REG;
252 *puiGpioInterrupt = 0x01;
253 }
255 /******************************************************************************
256 *
257 * Function: hwGpioDisableGlobalInterrupt
258 *
259 * Description: This function Disables GPIO interrupts to CPU
260 *
261 * Parameters: void
262 *
263 * Return Value: void
264 *
265 *****************************************************************************/
266 void hwGpioDisableGlobalInterrupt( void )
267 {
268 Uint32* puiGpioInterrupt = (Uint32*)GPIO_BINTEN_REG;
269 *puiGpioInterrupt = 0x00;
270 }
272 /******************************************************************************
273 *
274 * Function: hwGpioSetRisingEdgeInterrupt
275 *
276 * Description: This function sets specified GPIO's rising edge interrupt
277 *
278 * Parameters: uiNumber - GPIO number to configure
279 *
280 * Return Value: void
281 *
282 *****************************************************************************/
283 void hwGpioSetRisingEdgeInterrupt( Uint32 uiNumber )
284 {
285 Uint32* puiGpioSetRisingEdge = (Uint32*)GPIO_SET_RIS_TRIG_REG;
286 *puiGpioSetRisingEdge = (1 << uiNumber);
287 }
289 /******************************************************************************
290 *
291 * Function: hwGpioClearRisingEdgeInterrupt
292 *
293 * Description: This function clears specified GPIO's rising edge interrupt
294 *
295 * Parameters: uiNumber - GPIO number to configure
296 *
297 * Return Value: void
298 *
299 *****************************************************************************/
300 void hwGpioClearRisingEdgeInterrupt( Uint32 uiNumber )
301 {
302 Uint32* puiGpioClearRisingEdge = (Uint32*)GPIO_CLR_RIS_TRIG_REG;
303 *puiGpioClearRisingEdge = (1 << uiNumber);
304 }
306 /******************************************************************************
307 *
308 * Function: hwGpioSetFallingEdgeInterrupt
309 *
310 * Description: This function sets specified GPIO's falling edge interrupt
311 *
312 * Parameters: uiNumber - GPIO number to configure
313 *
314 * Return Value: void
315 *
316 *****************************************************************************/
317 void hwGpioSetFallingEdgeInterrupt( Uint32 uiNumber )
318 {
319 Uint32* puiGpioSetFallingEdge = (Uint32*)GPIO_SET_FAL_TRIG_REG;
320 *puiGpioSetFallingEdge = (1 << uiNumber);
321 }
323 /******************************************************************************
324 *
325 * Function: hwGpioClearFallingEdgeInterrupt
326 *
327 * Description: This function clears specified GPIO's falling edge interrupt
328 *
329 * Parameters: uiNumber - GPIO number to configure
330 *
331 * Return Value: void
332 *
333 *****************************************************************************/
334 void hwGpioClearFallingEdgeInterrupt( Uint32 uiNumber )
335 {
336 Uint32* puiGpioClearFallingEdge = (Uint32*)GPIO_CLR_FAL_TRIG_REG;
337 *puiGpioClearFallingEdge = (1 << uiNumber);
338 }