CC13xx Driver Library
gpio.h
Go to the documentation of this file.
1 /******************************************************************************
2 * Filename: gpio.h
3 * Revised: 2015-08-27 14:42:01 +0200 (Thu, 27 Aug 2015)
4 * Revision: 44480
5 *
6 * Description: Defines and prototypes for the GPIO.
7 *
8 * Copyright (c) 2015, Texas Instruments Incorporated
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * 1) Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 *
17 * 2) Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 *
21 * 3) Neither the name of the ORGANIZATION nor the names of its contributors may
22 * be used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 ******************************************************************************/
38 
39 //****************************************************************************
40 //
45 //
46 //****************************************************************************
47 
48 #ifndef __GPIO_H__
49 #define __GPIO_H__
50 
51 //*****************************************************************************
52 //
53 // If building with a C++ compiler, make all of the definitions in this header
54 // have a C binding.
55 //
56 //*****************************************************************************
57 #ifdef __cplusplus
58 extern "C"
59 {
60 #endif
61 
62 #include <stdbool.h>
63 #include <stdint.h>
64 #include <inc/hw_types.h>
65 #include <inc/hw_memmap.h>
66 #include <inc/hw_gpio.h>
67 #include <driverlib/debug.h>
68 
69 //*****************************************************************************
70 //
71 // Number of GPIO pins
72 //
73 //*****************************************************************************
74 #define NUM_GPIO_PINS 32
75 
76 //*****************************************************************************
77 //
78 // The following values define the bit field for the GPIO Pins in a port.
79 //
80 //*****************************************************************************
81 #define GPIO_PIN_0 0x00000001 // GPIO pin 0
82 #define GPIO_PIN_1 0x00000002 // GPIO pin 1
83 #define GPIO_PIN_2 0x00000004 // GPIO pin 2
84 #define GPIO_PIN_3 0x00000008 // GPIO pin 3
85 #define GPIO_PIN_4 0x00000010 // GPIO pin 4
86 #define GPIO_PIN_5 0x00000020 // GPIO pin 5
87 #define GPIO_PIN_6 0x00000040 // GPIO pin 6
88 #define GPIO_PIN_7 0x00000080 // GPIO pin 7
89 #define GPIO_PIN_8 0x00000100 // GPIO pin 8
90 #define GPIO_PIN_9 0x00000200 // GPIO pin 9
91 #define GPIO_PIN_10 0x00000400 // GPIO pin 10
92 #define GPIO_PIN_11 0x00000800 // GPIO pin 11
93 #define GPIO_PIN_12 0x00001000 // GPIO pin 12
94 #define GPIO_PIN_13 0x00002000 // GPIO pin 13
95 #define GPIO_PIN_14 0x00004000 // GPIO pin 14
96 #define GPIO_PIN_15 0x00008000 // GPIO pin 15
97 #define GPIO_PIN_16 0x00010000 // GPIO pin 16
98 #define GPIO_PIN_17 0x00020000 // GPIO pin 17
99 #define GPIO_PIN_18 0x00040000 // GPIO pin 18
100 #define GPIO_PIN_19 0x00080000 // GPIO pin 19
101 #define GPIO_PIN_20 0x00100000 // GPIO pin 20
102 #define GPIO_PIN_21 0x00200000 // GPIO pin 21
103 #define GPIO_PIN_22 0x00400000 // GPIO pin 22
104 #define GPIO_PIN_23 0x00800000 // GPIO pin 23
105 #define GPIO_PIN_24 0x01000000 // GPIO pin 24
106 #define GPIO_PIN_25 0x02000000 // GPIO pin 25
107 #define GPIO_PIN_26 0x04000000 // GPIO pin 26
108 #define GPIO_PIN_27 0x08000000 // GPIO pin 27
109 #define GPIO_PIN_28 0x10000000 // GPIO pin 28
110 #define GPIO_PIN_29 0x20000000 // GPIO pin 29
111 #define GPIO_PIN_30 0x40000000 // GPIO pin 30
112 #define GPIO_PIN_31 0x80000000 // GPIO pin 31
113 #define GPIO_PIN_UNUSED 0x00000000 // GPIO pin unused
114 #define GPIO_PIN_MASK 0xFFFFFFFF // GPIO pin mask
115 
116 //*****************************************************************************
117 //
118 // Values that can be passed to GPIODirModeSet as the ui32PinIO parameter, and
119 // returned from GPIODirModeGet.
120 //
121 //*****************************************************************************
122 #define GPIO_DIR_MODE_IN 0x00000000 // Pin is a GPIO input
123 #define GPIO_DIR_MODE_OUT 0x00000001 // Pin is a GPIO output
124 
125 //*****************************************************************************
126 //
127 // API Functions and prototypes
128 //
129 //*****************************************************************************
130 
131 //****************************************************************************
132 //
148 //
149 //****************************************************************************
150 __STATIC_INLINE void
151 GPIODirModeSet(uint32_t ui32Pins, uint32_t ui32Dir)
152 {
153  uint32_t ui32Reg;
154 
155  //
156  // Check the arguments.
157  //
158  ASSERT(ui32Pins & GPIO_PIN_MASK);
159  ASSERT((ui32Dir == GPIO_DIR_MODE_IN) ||
160  (ui32Dir == GPIO_DIR_MODE_OUT));
161 
162  //
163  // Update the output pin enable bit.
164  //
165  ui32Reg = HWREG(GPIO_BASE + GPIO_O_DOE31_0);
166  if(ui32Dir == GPIO_DIR_MODE_IN)
167  {
168  ui32Reg &= ~ui32Pins;
169  }
170  else
171  {
172  ui32Reg |= ui32Pins;
173  }
174  HWREG(GPIO_BASE + GPIO_O_DOE31_0) = ui32Reg;
175 }
176 
177 //*****************************************************************************
178 //
193 //
194 //*****************************************************************************
195 __STATIC_INLINE uint32_t
196 GPIODirModeGet(uint32_t ui32Pin)
197 {
198  uint32_t ui32Reg;
199 
200  //
201  // Check the arguments.
202  //
203  ASSERT((ui32Pin >= GPIO_PIN_0) && (ui32Pin <= GPIO_PIN_31));
204 
205  //
206  // Return the pin direction.
207  //
208  ui32Reg = HWREG(GPIO_BASE + GPIO_O_DOE31_0);
209  return((ui32Reg & ui32Pin) ? GPIO_DIR_MODE_OUT : GPIO_DIR_MODE_IN);
210 }
211 
212 //****************************************************************************
213 //
229 //
230 //****************************************************************************
231 __STATIC_INLINE void
232 GPIOPinWrite(uint32_t ui32Pins, uint32_t ui32Val)
233 {
234  uint32_t ui32Addr;
235 
236  //
237  // Check the arguments.
238  //
239  ASSERT(ui32Pins & GPIO_PIN_MASK);
240 
241  //
242  // Write to the port.
243  //
244  if(ui32Val)
245  {
246  ui32Addr = GPIO_BASE + GPIO_O_DOUTSET31_0;
247  }
248  else
249  {
250  ui32Addr = GPIO_BASE + GPIO_O_DOUTCLR31_0;
251  }
252  HWREG(ui32Addr) = ui32Pins;
253 }
254 
255 //****************************************************************************
256 //
269 //
270 //****************************************************************************
271 __STATIC_INLINE uint32_t
272 GPIOPinRead(uint32_t ui32Pins)
273 {
274  //
275  // Check the arguments.
276  //
277  ASSERT((ui32Pins >= GPIO_PIN_0) && (ui32Pins <= GPIO_PIN_31));
278 
279  //
280  // Return the value of the pin(s).
281  //
282  return (HWREG(GPIO_BASE + GPIO_O_DIN31_0) & ui32Pins);
283 }
284 
285 //****************************************************************************
286 //
296 //
297 //****************************************************************************
298 __STATIC_INLINE void
299 GPIOPinClear(uint32_t ui32Pins)
300 {
301  //
302  // Check the arguments.
303  //
304  ASSERT(ui32Pins & GPIO_PIN_MASK);
305 
306  //
307  // Clear the pin(s).
308  //
309  HWREG(GPIO_BASE + GPIO_O_DOUTCLR31_0) = ui32Pins;
310 }
311 
312 //****************************************************************************
313 //
323 //
324 //****************************************************************************
325 __STATIC_INLINE void
326 GPIOPinToggle(uint32_t ui32Pins)
327 {
328  //
329  // Check the arguments.
330  //
331  ASSERT(ui32Pins & GPIO_PIN_MASK);
332 
333  //
334  // Toggle the pin.
335  //
336  HWREG(GPIO_BASE + GPIO_O_DOUTTGL31_0) = ui32Pins;
337 }
338 
339 //****************************************************************************
340 //
350 //
351 //****************************************************************************
352 __STATIC_INLINE uint32_t
353 GPIOEventGet(uint32_t ui32Pin)
354 {
355  //
356  // Check the arguments.
357  //
358  ASSERT(ui32Pin & GPIO_PIN_MASK);
359 
360  //
361  // Return the event status.
362  //
363  return ((HWREG(GPIO_BASE + GPIO_O_EVFLAGS31_0) & ui32Pin) ? 1 : 0);
364 }
365 
366 //****************************************************************************
367 //
377 //
378 //****************************************************************************
379 __STATIC_INLINE void
380 GPIOEventClear(uint32_t ui32Pins)
381 {
382  //
383  // Check the arguments.
384  //
385  ASSERT(ui32Pins & GPIO_PIN_MASK);
386 
387  //
388  // Toggle the pin.
389  //
390  HWREG(GPIO_BASE + GPIO_O_EVFLAGS31_0) = ui32Pins;
391 }
392 
393 //*****************************************************************************
394 //
395 // Mark the end of the C bindings section for C++ compilers.
396 //
397 //*****************************************************************************
398 #ifdef __cplusplus
399 }
400 #endif
401 
402 #endif // __GPIO_H__
403 
404 //****************************************************************************
405 //
409 //
410 //****************************************************************************
static void GPIOPinToggle(uint32_t ui32Pins)
Toggle specific pin(s).
Definition: gpio.h:326
#define GPIO_DIR_MODE_OUT
Definition: gpio.h:123
static void GPIOPinClear(uint32_t ui32Pins)
Clear specific pin(s).
Definition: gpio.h:299
#define GPIO_PIN_31
Definition: gpio.h:112
static uint32_t GPIOEventGet(uint32_t ui32Pin)
Get the event status of a specific pin.
Definition: gpio.h:353
static void GPIOEventClear(uint32_t ui32Pins)
Clear an IO event on a pin.
Definition: gpio.h:380
#define GPIO_PIN_MASK
Definition: gpio.h:114
#define ASSERT(expr)
Definition: debug.h:74
static void GPIOPinWrite(uint32_t ui32Pins, uint32_t ui32Val)
Write to pin(s).
Definition: gpio.h:232
#define GPIO_DIR_MODE_IN
Definition: gpio.h:122
static uint32_t GPIODirModeGet(uint32_t ui32Pin)
Gets the direction of a pin.
Definition: gpio.h:196
static uint32_t GPIOPinRead(uint32_t ui32Pins)
Read the value of specific pin(s).
Definition: gpio.h:272
static void GPIODirModeSet(uint32_t ui32Pins, uint32_t ui32Dir)
Sets the direction of the specified pin(s).
Definition: gpio.h:151
#define GPIO_PIN_0
Definition: gpio.h:81