CC27xxDriverLibrary
gpio.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Filename: gpio.h
3  *
4  * Description: Defines and prototypes for the GPIO.
5  *
6  * Copyright (c) 2022-2024 Texas Instruments Incorporated
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1) Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * 2) Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3) Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  ******************************************************************************/
35 
36 #ifndef __GPIO_H__
37 #define __GPIO_H__
38 
39 //*****************************************************************************
40 //
45 //
46 //*****************************************************************************
47 
48 //*****************************************************************************
49 //
50 // If building with a C++ compiler, make all of the definitions in this header
51 // have a C binding.
52 //
53 //*****************************************************************************
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 #include <stdint.h>
59 #include "../inc/hw_types.h"
60 #include "../inc/hw_memmap.h"
61 #include "../inc/hw_gpio.h"
62 #include "debug.h"
63 
64 //*****************************************************************************
65 //
66 // Check for legal range of variable dioNumber
67 //
68 //*****************************************************************************
69 #ifdef DRIVERLIB_DEBUG
70 static bool dioNumberLegal(uint32_t dioNumber)
71 {
72  // TODO: Implement properly
73  // Get number of GPIOs supported by platform. Number of DIOs is NUMDIO + 1.
74  // Actual number available may be less for some package types.
75  uint32_t numDio = 1 + ((HWREG(GPIO_BASE + GPIO_O_DESCEX) & GPIO_DESCEX_NUMDIO_M) >> GPIO_DESCEX_NUMDIO_S);
76  return (dioNumber < numDio);
77 }
78 #endif
79 
80 //*****************************************************************************
81 //
82 // The following values define the bit field for the GPIO DIOs.
83 //
84 //*****************************************************************************
85 #define GPIO_DIO_0_MASK 0x00000001 // GPIO DIO 0 mask
86 #define GPIO_DIO_1_MASK 0x00000002 // GPIO DIO 1 mask
87 #define GPIO_DIO_2_MASK 0x00000004 // GPIO DIO 2 mask
88 #define GPIO_DIO_3_MASK 0x00000008 // GPIO DIO 3 mask
89 #define GPIO_DIO_4_MASK 0x00000010 // GPIO DIO 4 mask
90 #define GPIO_DIO_5_MASK 0x00000020 // GPIO DIO 5 mask
91 #define GPIO_DIO_6_MASK 0x00000040 // GPIO DIO 6 mask
92 #define GPIO_DIO_7_MASK 0x00000080 // GPIO DIO 7 mask
93 #define GPIO_DIO_8_MASK 0x00000100 // GPIO DIO 8 mask
94 #define GPIO_DIO_9_MASK 0x00000200 // GPIO DIO 9 mask
95 #define GPIO_DIO_10_MASK 0x00000400 // GPIO DIO 10 mask
96 #define GPIO_DIO_11_MASK 0x00000800 // GPIO DIO 11 mask
97 #define GPIO_DIO_12_MASK 0x00001000 // GPIO DIO 12 mask
98 #define GPIO_DIO_13_MASK 0x00002000 // GPIO DIO 13 mask
99 #define GPIO_DIO_14_MASK 0x00004000 // GPIO DIO 14 mask
100 #define GPIO_DIO_15_MASK 0x00008000 // GPIO DIO 15 mask
101 #define GPIO_DIO_16_MASK 0x00010000 // GPIO DIO 16 mask
102 #define GPIO_DIO_17_MASK 0x00020000 // GPIO DIO 17 mask
103 #define GPIO_DIO_18_MASK 0x00040000 // GPIO DIO 18 mask
104 #define GPIO_DIO_19_MASK 0x00080000 // GPIO DIO 19 mask
105 #define GPIO_DIO_20_MASK 0x00100000 // GPIO DIO 20 mask
106 #define GPIO_DIO_21_MASK 0x00200000 // GPIO DIO 21 mask
107 #define GPIO_DIO_22_MASK 0x00400000 // GPIO DIO 22 mask
108 #define GPIO_DIO_23_MASK 0x00800000 // GPIO DIO 23 mask
109 #define GPIO_DIO_24_MASK 0x01000000 // GPIO DIO 24 mask
110 #define GPIO_DIO_25_MASK 0x02000000 // GPIO DIO 25 mask
111 #define GPIO_DIO_26_MASK 0x04000000 // GPIO DIO 26 mask
112 #define GPIO_DIO_27_MASK 0x08000000 // GPIO DIO 27 mask
113 #define GPIO_DIO_28_MASK 0x10000000 // GPIO DIO 28 mask
114 #define GPIO_DIO_29_MASK 0x20000000 // GPIO DIO 29 mask
115 #define GPIO_DIO_30_MASK 0x40000000 // GPIO DIO 30 mask
116 #define GPIO_DIO_ALL_MASK 0xFFFFFFFF // GPIO all DIOs mask
117 
118 //*****************************************************************************
119 //
120 // Define constants that shall be passed as the outputEnableValue parameter to
121 // GPIOSetOutputEnableDio() and will be returned from the function
122 // GPIOGetOutputEnableDio().
123 //
124 //*****************************************************************************
125 #define GPIO_OUTPUT_DISABLE 0x00000000 // DIO output is disabled
126 #define GPIO_OUTPUT_ENABLE 0x00000001 // DIO output is enabled
127 
128 //*****************************************************************************
129 //
130 // API Functions and prototypes
131 //
132 //*****************************************************************************
133 
134 //*****************************************************************************
135 //
143 //
144 //*****************************************************************************
145 __STATIC_INLINE uint32_t GPIOReadDio(uint32_t dioNumber)
146 {
147  // Check the arguments.
148  ASSERT(dioNumberLegal(dioNumber));
149 
150  // Return the input value from the specified DIO.
151  return ((HWREG( GPIO_BASE + GPIO_O_DIN31_0 ) >> dioNumber) & 1);
152 }
153 
154 //*****************************************************************************
155 //
172 //
173 //*****************************************************************************
174 __STATIC_INLINE uint32_t GPIOReadMultiDio(uint32_t dioMask)
175 {
176  // Check the arguments.
177  ASSERT(dioMask & GPIO_DIO_ALL_MASK);
178 
179  // Return the input value from the specified DIOs.
180  return (HWREG( GPIO_BASE + GPIO_O_DIN31_0 ) & dioMask);
181 }
182 
183 //*****************************************************************************
184 //
195 //
196 //*****************************************************************************
197 __STATIC_INLINE void GPIOWriteDio(uint32_t dioNumber, uint32_t value)
198 {
199  // Check the arguments.
200  ASSERT(dioNumberLegal(dioNumber));
201  ASSERT((value == 0) || (value == 1));
202 
203  // Write 0 or 1 to the byte indexed DOUT map
204  HWREGB( GPIO_BASE + GPIO_O_DOUT3_0 + dioNumber ) = value;
205 }
206 
207 //*****************************************************************************
208 //
226 //
227 //*****************************************************************************
228 __STATIC_INLINE void GPIOWriteMultiDio(uint32_t dioMask, uint32_t bitVectoredValue)
229 {
230  // Check the arguments.
231  ASSERT(dioMask & GPIO_DIO_ALL_MASK);
232 
233  HWREG( GPIO_BASE + GPIO_O_DOUT31_0 ) = (HWREG( GPIO_BASE + GPIO_O_DOUT31_0 ) & ~dioMask) |
234  (bitVectoredValue & dioMask);
235 }
236 
237 //*****************************************************************************
238 //
246 //
247 //*****************************************************************************
248 __STATIC_INLINE void GPIOSetDio(uint32_t dioNumber)
249 {
250  // Check the arguments.
251  ASSERT(dioNumberLegal(dioNumber));
252 
253  // Set the specified DIO.
254  HWREG( GPIO_BASE + GPIO_O_DOUTSET31_0 ) = (1 << dioNumber);
255 }
256 
257 //*****************************************************************************
258 //
270 //
271 //*****************************************************************************
272 __STATIC_INLINE void GPIOSetMultiDio(uint32_t dioMask)
273 {
274  // Check the arguments.
275  ASSERT(dioMask & GPIO_DIO_ALL_MASK);
276 
277  // Set the DIOs.
278  HWREG( GPIO_BASE + GPIO_O_DOUTSET31_0 ) = dioMask;
279 }
280 
281 //*****************************************************************************
282 //
290 //
291 //*****************************************************************************
292 __STATIC_INLINE void GPIOClearDio(uint32_t dioNumber)
293 {
294  // Check the arguments.
295  ASSERT(dioNumberLegal(dioNumber));
296 
297  // Clear the specified DIO.
298  HWREG( GPIO_BASE + GPIO_O_DOUTCLR31_0 ) = (1 << dioNumber);
299 }
300 
301 //*****************************************************************************
302 //
314 //
315 //*****************************************************************************
316 __STATIC_INLINE void GPIOClearMultiDio(uint32_t dioMask)
317 {
318  // Check the arguments.
319  ASSERT(dioMask & GPIO_DIO_ALL_MASK);
320 
321  // Clear the DIOs.
322  HWREG( GPIO_BASE + GPIO_O_DOUTCLR31_0 ) = dioMask;
323 }
324 
325 //*****************************************************************************
326 //
334 //
335 //*****************************************************************************
336 __STATIC_INLINE void GPIOToggleDio(uint32_t dioNumber)
337 {
338  // Check the arguments.
339  ASSERT(dioNumberLegal(dioNumber));
340 
341  // Toggle the specified DIO.
342  HWREG( GPIO_BASE + GPIO_O_DOUTTGL31_0 ) = (1 << dioNumber);
343 }
344 
345 //*****************************************************************************
346 //
358 //
359 //*****************************************************************************
360 __STATIC_INLINE void GPIOToggleMultiDio(uint32_t dioMask)
361 {
362  // Check the arguments.
363  ASSERT(dioMask & GPIO_DIO_ALL_MASK);
364 
365  // Toggle the DIOs.
366  HWREG( GPIO_BASE + GPIO_O_DOUTTGL31_0 ) = dioMask;
367 }
368 
369 //*****************************************************************************
370 //
383 //
384 //*****************************************************************************
385 __STATIC_INLINE uint32_t GPIOGetOutputEnableDio(uint32_t dioNumber)
386 {
387  // Check the arguments.
388  ASSERT(dioNumberLegal(dioNumber));
389 
390  // Return the output enable status for the specified DIO.
391  return ((HWREG( GPIO_BASE + GPIO_O_DOE31_0 ) >> dioNumber) & 1);
392 }
393 
394 //*****************************************************************************
395 //
412 //
413 //*****************************************************************************
415 {
416  // Check the arguments.
417  ASSERT(dioMask & GPIO_DIO_ALL_MASK);
418 
419  // Return the output enable value for the specified DIOs.
420  return (HWREG( GPIO_BASE + GPIO_O_DOE31_0 ) & dioMask);
421 }
422 
423 //*****************************************************************************
424 //
438 //
439 //*****************************************************************************
440 __STATIC_INLINE void GPIOSetOutputEnableDio(uint32_t dioNumber, uint32_t outputEnableValue)
441 {
442  // Check the arguments.
443  ASSERT(dioNumberLegal(dioNumber));
444  ASSERT((outputEnableValue == GPIO_OUTPUT_DISABLE) || (outputEnableValue == GPIO_OUTPUT_ENABLE));
445 
446  // Update the output enable bit for the specified DIO.
447  if (outputEnableValue == GPIO_OUTPUT_ENABLE)
448  {
449  HWREG( GPIO_BASE + GPIO_O_DOE31_0 ) |= (1 << dioNumber);
450  }
451  else
452  {
453  HWREG( GPIO_BASE + GPIO_O_DOE31_0 ) &= ~(1 << dioNumber);
454  }
455 }
456 
457 //*****************************************************************************
458 //
479 //
480 //*****************************************************************************
481 __STATIC_INLINE void GPIOSetOutputEnableMultiDio(uint32_t dioMask, uint32_t bitVectoredOutputEnable)
482 {
483  // Check the arguments.
484  ASSERT(dioMask & GPIO_DIO_ALL_MASK);
485 
486  HWREG( GPIO_BASE + GPIO_O_DOE31_0 ) = (HWREG( GPIO_BASE + GPIO_O_DOE31_0 ) & ~dioMask) |
487  (bitVectoredOutputEnable & dioMask);
488 }
489 
490 //*****************************************************************************
491 //
501 //
502 //*****************************************************************************
503 __STATIC_INLINE uint32_t GPIOGetEventDio(uint32_t dioNumber)
504 {
505  // Check the arguments.
506  ASSERT(dioNumberLegal(dioNumber));
507 
508  // Return the event status for the specified DIO.
509  return ((HWREG( GPIO_BASE + GPIO_O_RIS ) >> dioNumber) & 1);
510 }
511 
512 //*****************************************************************************
513 //
531 //
532 //*****************************************************************************
533 __STATIC_INLINE uint32_t GPIOGetEventMultiDio(uint32_t dioMask)
534 {
535  // Check the arguments.
536  ASSERT(dioMask & GPIO_DIO_ALL_MASK);
537 
538  // Return the event status for the specified DIO.
539  return (HWREG( GPIO_BASE + GPIO_O_RIS ) & dioMask);
540 }
541 
542 //*****************************************************************************
543 //
551 //
552 //*****************************************************************************
553 __STATIC_INLINE void GPIOClearEventDio(uint32_t dioNumber)
554 {
555  // Check the arguments.
556  ASSERT(dioNumberLegal(dioNumber));
557 
558  // Clear the event status for the specified DIO.
559  HWREG( GPIO_BASE + GPIO_O_ICLR ) = (1 << dioNumber);
560 }
561 
562 //*****************************************************************************
563 //
576 //
577 //*****************************************************************************
579 {
580  // Check the arguments.
581  ASSERT(dioMask & GPIO_DIO_ALL_MASK);
582 
583  // Clear the event status for the specified DIOs.
584  HWREG( GPIO_BASE + GPIO_O_ICLR ) = dioMask;
585 }
586 
587 //*****************************************************************************
588 //
589 // Mark the end of the C bindings section for C++ compilers.
590 //
591 //*****************************************************************************
592 #ifdef __cplusplus
593 }
594 #endif
595 
596 //*****************************************************************************
597 //
601 //
602 //*****************************************************************************
603 
604 #endif // __GPIO_H__
__STATIC_INLINE uint32_t GPIOGetOutputEnableMultiDio(uint32_t dioMask)
Gets the output enable setting of the specified DIOs.
Definition: gpio.h:414
#define GPIO_DIO_ALL_MASK
Definition: gpio.h:116
__STATIC_INLINE void GPIOSetMultiDio(uint32_t dioMask)
Sets the specified DIOs to 1 (high).
Definition: gpio.h:272
#define GPIO_OUTPUT_ENABLE
Definition: gpio.h:126
__STATIC_INLINE void GPIOClearDio(uint32_t dioNumber)
Clears a specific DIO to 0 (low).
Definition: gpio.h:292
__STATIC_INLINE uint32_t GPIOGetEventDio(uint32_t dioNumber)
Gets the event status of a specific DIO.
Definition: gpio.h:503
__STATIC_INLINE void GPIOToggleDio(uint32_t dioNumber)
Toggles a specific DIO.
Definition: gpio.h:336
__STATIC_INLINE void GPIOWriteMultiDio(uint32_t dioMask, uint32_t bitVectoredValue)
Writes masked data to the specified DIOs.
Definition: gpio.h:228
__STATIC_INLINE void GPIOSetOutputEnableMultiDio(uint32_t dioMask, uint32_t bitVectoredOutputEnable)
Configures the output enable setting for all specified DIOs.
Definition: gpio.h:481
__STATIC_INLINE void GPIOClearEventDio(uint32_t dioNumber)
Clears the IO event status of a specific DIO.
Definition: gpio.h:553
#define ASSERT(expr)
Definition: debug.h:71
__STATIC_INLINE void GPIOSetOutputEnableDio(uint32_t dioNumber, uint32_t outputEnableValue)
Sets output enable of a specific DIO.
Definition: gpio.h:440
__STATIC_INLINE uint32_t GPIOReadDio(uint32_t dioNumber)
Reads a specific DIO.
Definition: gpio.h:145
__STATIC_INLINE void GPIOToggleMultiDio(uint32_t dioMask)
Toggles the specified DIOs.
Definition: gpio.h:360
__STATIC_INLINE uint32_t GPIOGetOutputEnableDio(uint32_t dioNumber)
Gets the output enable status of a specific DIO.
Definition: gpio.h:385
__STATIC_INLINE void GPIOSetDio(uint32_t dioNumber)
Sets a specific DIO to 1 (high).
Definition: gpio.h:248
#define GPIO_OUTPUT_DISABLE
Definition: gpio.h:125
__STATIC_INLINE void GPIOWriteDio(uint32_t dioNumber, uint32_t value)
Writes a value to a specific DIO.
Definition: gpio.h:197
#define __STATIC_INLINE
Definition: cmsis_gcc.h:47
__STATIC_INLINE void GPIOClearEventMultiDio(uint32_t dioMask)
Clears the IO event status on the specified DIOs.
Definition: gpio.h:578
__STATIC_INLINE uint32_t GPIOGetEventMultiDio(uint32_t dioMask)
Gets the event status of the specified DIOs.
Definition: gpio.h:533
__STATIC_INLINE uint32_t GPIOReadMultiDio(uint32_t dioMask)
Reads the input value for the specified DIOs.
Definition: gpio.h:174
__STATIC_INLINE void GPIOClearMultiDio(uint32_t dioMask)
Clears the specified DIOs to 0 (low).
Definition: gpio.h:316