CC26xx Driver Library
gpio.h
Go to the documentation of this file.
1 /******************************************************************************
2 * Filename: gpio.h
3 * Revised: 2015-07-16 12:12:04 +0200 (Thu, 16 Jul 2015)
4 * Revision: 44151
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 //
266 //
267 //****************************************************************************
268 __STATIC_INLINE uint32_t
269 GPIOPinRead(uint32_t ui32Pins)
270 {
271  //
272  // Check the arguments.
273  //
274  ASSERT((ui32Pins >= GPIO_PIN_0) && (ui32Pins <= GPIO_PIN_31));
275 
276  //
277  // Return the value of the pin(s).
278  //
279  return (HWREG(GPIO_BASE + GPIO_O_DIN31_0) & ui32Pins);
280 }
281 
282 //****************************************************************************
283 //
293 //
294 //****************************************************************************
295 __STATIC_INLINE void
296 GPIOPinClear(uint32_t ui32Pins)
297 {
298  //
299  // Check the arguments.
300  //
301  ASSERT(ui32Pins & GPIO_PIN_MASK);
302 
303  //
304  // Clear the pin(s).
305  //
306  HWREG(GPIO_BASE + GPIO_O_DOUTCLR31_0) = ui32Pins;
307 }
308 
309 //****************************************************************************
310 //
320 //
321 //****************************************************************************
322 __STATIC_INLINE void
323 GPIOPinToggle(uint32_t ui32Pins)
324 {
325  //
326  // Check the arguments.
327  //
328  ASSERT(ui32Pins & GPIO_PIN_MASK);
329 
330  //
331  // Toggle the pin.
332  //
333  HWREG(GPIO_BASE + GPIO_O_DOUTTGL31_0) = ui32Pins;
334 }
335 
336 //****************************************************************************
337 //
347 //
348 //****************************************************************************
349 __STATIC_INLINE uint32_t
350 GPIOEventGet(uint32_t ui32Pin)
351 {
352  //
353  // Check the arguments.
354  //
355  ASSERT(ui32Pin & GPIO_PIN_MASK);
356 
357  //
358  // Return the event status.
359  //
360  return ((HWREG(GPIO_BASE + GPIO_O_EVFLAGS31_0) & ui32Pin) ? 1 : 0);
361 }
362 
363 //****************************************************************************
364 //
374 //
375 //****************************************************************************
376 __STATIC_INLINE void
377 GPIOEventClear(uint32_t ui32Pins)
378 {
379  //
380  // Check the arguments.
381  //
382  ASSERT(ui32Pins & GPIO_PIN_MASK);
383 
384  //
385  // Toggle the pin.
386  //
387  HWREG(GPIO_BASE + GPIO_O_EVFLAGS31_0) = ui32Pins;
388 }
389 
390 //*****************************************************************************
391 //
392 // Mark the end of the C bindings section for C++ compilers.
393 //
394 //*****************************************************************************
395 #ifdef __cplusplus
396 }
397 #endif
398 
399 #endif // __GPIO_H__
400 
401 //****************************************************************************
402 //
406 //
407 //****************************************************************************
static void GPIOPinToggle(uint32_t ui32Pins)
Toggle specific pin(s).
Definition: gpio.h:323
#define GPIO_DIR_MODE_OUT
Definition: gpio.h:123
static void GPIOPinClear(uint32_t ui32Pins)
Clear specific pin(s).
Definition: gpio.h:296
#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:350
static void GPIOEventClear(uint32_t ui32Pins)
Clear an IO event on a pin.
Definition: gpio.h:377
#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:269
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