F29H85x-SDK  26.00.00
 
gpio.h
Go to the documentation of this file.
1//###########################################################################
2//
3// FILE: gpio.h
4//
5// TITLE: C29x GPIO driver.
6//
7//###########################################################################
8// //
9// Copyright: Copyright (C) Texas Instruments Incorporated
10// All rights reserved not granted herein.
11//
12// Redistribution and use in source and binary forms, with or without
13// modification, are permitted provided that the following conditions
14// are met:
15//
16// Redistributions of source code must retain the above copyright
17// notice, this list of conditions and the following disclaimer.
18//
19// Redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the
22// distribution.
23//
24// Neither the name of Texas Instruments Incorporated nor the names of
25// its contributors may be used to endorse or promote products derived
26// from this software without specific prior written permission.
27//
28// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39//
40
41//###########################################################################
42
43#ifndef GPIO_H
44#define GPIO_H
45
46//*****************************************************************************
47//
48// If building with a C++ compiler, make all of the definitions in this header
49// have a C binding.
50//
51//*****************************************************************************
52#ifdef __cplusplus
53extern "C"
54{
55#endif
56
57//*****************************************************************************
58//
61//
62//*****************************************************************************
63
64#include <stdbool.h>
65#include <stdint.h>
66#include "inc/hw_gpio.h"
67#include "inc/hw_memmap.h"
68#include "inc/hw_types.h"
69#include "inc/hw_xint.h"
70#include "inc/hw_asysctl.h"
71#include "cpu.h"
72#include "xbar.h"
73#include "debug.h"
74
75//*****************************************************************************
76//
77// Useful defines used within the driver functions to access gpio registers.
78// Not intended for use by application code.
79//
80//*****************************************************************************
81#define GPIO_CTRL_REGS_STEP ((GPIO_O_GPBCTRL - GPIO_O_GPACTRL) / 4U)
82#define GPIO_DATA_REGS_STEP ((GPIO_O_GPBDAT - GPIO_O_GPADAT) / 4U)
83#define GPIO_DATA_READ_REGS_STEP ((GPIO_O_GPBDAT_R - GPIO_O_GPADAT_R) / 4U)
84
85#define GPIO_GPxCTRL_INDEX (0U)
86#define GPIO_GPxQSEL_INDEX (1U)
87#define GPIO_GPxMUX_INDEX (3U)
88#define GPIO_GPxPUD_INDEX (6U)
89#define GPIO_GPxINV_INDEX (8U)
90#define GPIO_GPxODR_INDEX (9U)
91#define GPIO_GPxAMSEL_INDEX (10U)
92#define GPIO_GPxGMUX_INDEX (16U)
93#define GPIO_GPxCSEL_INDEX (20U)
94#define GPIO_GPxLOCK_INDEX (30U)
95#define GPIO_GPxCR_INDEX (31U)
96
97#define GPIO_GPxDAT_INDEX (0U)
98#define GPIO_GPxSET_INDEX (1U)
99#define GPIO_GPxCLEAR_INDEX (2U)
100#define GPIO_GPxTOGGLE_INDEX (3U)
101#define GPIO_GPxDIR_INDEX (4U)
102
103#define GPIO_GPxDAT_R_INDEX (0U)
104
105#define GPIO_MUX_TO_GMUX (GPIO_O_GPAGMUX1 - GPIO_O_GPAMUX1)
106
107
108//*****************************************************************************
109//
110// Values that can be passed to GPIO_setPadConfig() as the pinType parameter
111// and returned by GPIO_getPadConfig().
112//
113//*****************************************************************************
114#define GPIO_PIN_TYPE_STD 0x0000U
115#define GPIO_PIN_TYPE_PULLUP 0x0001U
116#define GPIO_PIN_TYPE_INVERT 0x0002U
117#define GPIO_PIN_TYPE_OD 0x0004U
118
119//*****************************************************************************
120//
123//
124//*****************************************************************************
130
131//*****************************************************************************
132//
135//
136//*****************************************************************************
143
144//*****************************************************************************
145//
148//
149//*****************************************************************************
157
158//*****************************************************************************
159//
161//
162//*****************************************************************************
168
169//*****************************************************************************
170//
173//
174//*****************************************************************************
181
182//*****************************************************************************
183//
186//
187//*****************************************************************************
199
200//*****************************************************************************
201//
205//
206//*****************************************************************************
215
216//*****************************************************************************
217//
218// Prototypes for the APIs.
219//
220//*****************************************************************************
221//*****************************************************************************
222//
231//
232//*****************************************************************************
233#ifdef DEBUG
234static inline bool
235GPIO_isPinValid(uint32_t pin)
236{
237 return(
238 (pin <= 105U) || (pin == 127U) || // GPIO
239 ((pin >= 219U) && (pin <= 223U)) || // GPIO
240 ((pin >= 160U) && (pin <= 213U)) || // AIO
241 ((pin >= 224U) && (pin <= 249U)) // AGPIO
242 );
243}
244#endif
245
246//*****************************************************************************
247//
274//
275//*****************************************************************************
276static inline void
278{
279 //
280 // Write the selected polarity to the appropriate register.
281 //
282 HWREGH(XINT_BASE + (uint16_t)extIntNum) =
283 (uint16_t)((HWREGH(XINT_BASE + (uint16_t)extIntNum) & ~XINT_1CR_POLARITY_M) |
284 (uint16_t)intType);
285}
286
287//*****************************************************************************
288//
307//
308//*****************************************************************************
309static inline GPIO_IntType
311{
312 //
313 // Read the selected polarity from the appropriate register.
314 //
315 return((GPIO_IntType)(HWREGH(XINT_BASE + (uint16_t)extIntNum) &
316 XINT_1CR_POLARITY_M));
317}
318
319//*****************************************************************************
320//
339//
340//*****************************************************************************
341static inline void
343{
344 //
345 // Set the enable bit for the specified interrupt.
346 //
347 HWREGH(XINT_BASE + (uint16_t)extIntNum) |= XINT_1CR_ENABLE;
348}
349
350//*****************************************************************************
351//
370//
371//*****************************************************************************
372static inline void
374{
375 //
376 // Clear the enable bit for the specified interrupt
377 //
378 HWREGH(XINT_BASE + (uint16_t)extIntNum) &= (uint16_t)~XINT_1CR_ENABLE;
379}
380
381//*****************************************************************************
382//
397//
398//*****************************************************************************
399static inline uint16_t
401{
402 ASSERT(extIntNum <= GPIO_INT_XINT3);
403
404 //
405 // Read the counter value from the appropriate register.
406 //
407 return((HWREGH(XINT_BASE + XINT_O_1CTR + (uint16_t)extIntNum)));
408}
409
410//*****************************************************************************
411//
423//
424//*****************************************************************************
425static inline uint32_t
426GPIO_readPin(uint32_t pin)
427{
428 volatile uint32_t *gpioDataReg;
429
430 //
431 // Check the arguments.
432 //
433 ASSERT(GPIO_isPinValid(pin));
434
435 gpioDataReg = (uint32_t *)((uintptr_t)GPIODATA_BASE) +
436 ((pin / 32U) * GPIO_DATA_REGS_STEP);
437
438 return((gpioDataReg[GPIO_GPxDAT_INDEX] >> (pin % 32U)) & (uint32_t)0x1U);
439}
440
441//*****************************************************************************
442//
456//
457//*****************************************************************************
458static inline uint32_t
460{
461 volatile uint32_t *gpioDataReg;
462
463 //
464 // Check the arguments.
465 //
466 ASSERT(GPIO_isPinValid(pin));
467
468 gpioDataReg = (uint32_t *)((uintptr_t)GPIODATAREAD_BASE) +
469 ((pin / 32U) * GPIO_DATA_READ_REGS_STEP);
470
471 return((gpioDataReg[GPIO_GPxDAT_R_INDEX] >> (pin % 32U)) & (uint32_t)0x1U);
472}
473
474//*****************************************************************************
475//
488//
489//*****************************************************************************
490static inline void
491GPIO_writePin(uint32_t pin, uint32_t outVal)
492{
493 volatile uint32_t *gpioDataReg;
494 uint32_t pinMask;
495
496 //
497 // Check the arguments.
498 //
499 ASSERT(GPIO_isPinValid(pin));
500
501 gpioDataReg = (uint32_t *)((uintptr_t)GPIODATA_BASE) +
502 ((pin / 32U) * GPIO_DATA_REGS_STEP);
503
504 pinMask = (uint32_t)1U << (pin % 32U);
505
506 if(outVal == 0U)
507 {
508 gpioDataReg[GPIO_GPxCLEAR_INDEX] = pinMask;
509 }
510 else
511 {
512 gpioDataReg[GPIO_GPxSET_INDEX] = pinMask;
513 }
514}
515
516//*****************************************************************************
517//
529//
530//*****************************************************************************
531static inline void
532GPIO_togglePin(uint32_t pin)
533{
534 volatile uint32_t *gpioDataReg;
535
536 //
537 // Check the arguments.
538 //
539 ASSERT(GPIO_isPinValid(pin));
540
541 gpioDataReg = (uint32_t *)((uintptr_t)GPIODATA_BASE) +
542 ((pin / 32U) * GPIO_DATA_REGS_STEP);
543
544 gpioDataReg[GPIO_GPxTOGGLE_INDEX] = (uint32_t)1U << (pin % 32U);
545}
546
547//*****************************************************************************
548//
557//
558//*****************************************************************************
559static inline uint32_t
561{
562 volatile uint32_t *gpioDataReg;
563
564 //
565 // Get the starting address of the port's registers and return DATA.
566 //
567 gpioDataReg = (uint32_t *)((uintptr_t)GPIODATA_BASE) +
568 ((uint32_t)port * GPIO_DATA_REGS_STEP);
569
570 return(gpioDataReg[GPIO_GPxDAT_INDEX]);
571}
572
573//*****************************************************************************
574//
590//
591//*****************************************************************************
592static inline uint32_t
594{
595 volatile uint32_t *gpioDataReg;
596
597 //
598 // Get the starting address of the port's registers and return DATA.
599 //
600 gpioDataReg = (uint32_t *)((uintptr_t)GPIODATAREAD_BASE) +
601 ((uint32_t)port * GPIO_DATA_READ_REGS_STEP);
602
603 return(gpioDataReg[GPIO_GPxDAT_R_INDEX]);
604}
605
606//*****************************************************************************
607//
623//
624//*****************************************************************************
625static inline void
626GPIO_writePortData(GPIO_Port port, uint32_t outVal)
627{
628 volatile uint32_t *gpioDataReg;
629
630 //
631 // Get the starting address of the port's registers and write to DATA.
632 //
633 gpioDataReg = (uint32_t *)((uintptr_t)GPIODATA_BASE) +
634 ((uint32_t)port * GPIO_DATA_REGS_STEP);
635
636 gpioDataReg[GPIO_GPxDAT_INDEX] = outVal;
637}
638
639//*****************************************************************************
640//
656//
657//*****************************************************************************
658static inline void
659GPIO_setPortPins(GPIO_Port port, uint32_t pinMask)
660{
661 volatile uint32_t *gpioDataReg;
662
663 //
664 // Get the starting address of the port's registers and write to SET.
665 //
666 gpioDataReg = (uint32_t *)((uintptr_t)GPIODATA_BASE) +
667 ((uint32_t)port * GPIO_DATA_REGS_STEP);
668
669 gpioDataReg[GPIO_GPxSET_INDEX] = pinMask;
670}
671
672//*****************************************************************************
673//
689//
690//*****************************************************************************
691static inline void
692GPIO_clearPortPins(GPIO_Port port, uint32_t pinMask)
693{
694 volatile uint32_t *gpioDataReg;
695
696 //
697 // Get the starting address of the port's registers and write to CLEAR.
698 //
699 gpioDataReg = (uint32_t *)((uintptr_t)GPIODATA_BASE) +
700 ((uint32_t)port * GPIO_DATA_REGS_STEP);
701
702 gpioDataReg[GPIO_GPxCLEAR_INDEX] = pinMask;
703}
704
705//*****************************************************************************
706//
722//
723//*****************************************************************************
724static inline void
725GPIO_togglePortPins(GPIO_Port port, uint32_t pinMask)
726{
727 volatile uint32_t *gpioDataReg;
728
729 //
730 // Get the starting address of the port's registers and write to TOGGLE.
731 //
732 gpioDataReg = (uint32_t *)((uintptr_t)GPIODATA_BASE) +
733 ((uint32_t)port * GPIO_DATA_REGS_STEP);
734
735 gpioDataReg[GPIO_GPxTOGGLE_INDEX] = pinMask;
736}
737
738//*****************************************************************************
739//
759//
760//*****************************************************************************
761__attribute__((section(".text.link2.GPIO_lockPortConfig")))
762static inline void
763GPIO_lockPortConfig(GPIO_Port port, uint32_t pinMask)
764{
765 volatile uint32_t *gpioDataReg;
766
767 //
768 // Get the starting address of the port's registers and write to the lock.
769 //
770 gpioDataReg = (uint32_t *)((uintptr_t)GPIOCTRL_BASE) +
771 ((uint32_t)port * GPIO_CTRL_REGS_STEP);
772
773 gpioDataReg[GPIO_GPxLOCK_INDEX] |= pinMask;
774}
775
776//*****************************************************************************
777//
793//
794//*****************************************************************************
795__attribute__((section(".text.link2.GPIO_unlockPortConfig")))
796static inline void
797GPIO_unlockPortConfig(GPIO_Port port, uint32_t pinMask)
798{
799 volatile uint32_t *gpioDataReg;
800
801 //
802 // Get the starting address of the port's registers and write to the lock.
803 //
804 gpioDataReg = (uint32_t *)((uintptr_t)GPIOCTRL_BASE) +
805 ((uint32_t)port * GPIO_CTRL_REGS_STEP);
806
807 gpioDataReg[GPIO_GPxLOCK_INDEX] &= ~pinMask;
808}
809
810//*****************************************************************************
811//
831//
832//*****************************************************************************
833__attribute__((section(".text.link2.GPIO_commitPortConfig")))
834static inline void
835GPIO_commitPortConfig(GPIO_Port port, uint32_t pinMask)
836{
837 volatile uint32_t *gpioDataReg;
838
839 //
840 // Get the starting address of the port's registers and write to the lock.
841 //
842 gpioDataReg = (uint32_t *)((uintptr_t)GPIOCTRL_BASE) +
843 ((uint32_t)port * GPIO_CTRL_REGS_STEP);
844
845 gpioDataReg[GPIO_GPxCR_INDEX] |= pinMask;
846}
847
848//*****************************************************************************
849//
871//
872//*****************************************************************************
873extern void
875
876//*****************************************************************************
877//
888//
889//*****************************************************************************
890extern GPIO_Direction
892
893//*****************************************************************************
894//
917//
918//*****************************************************************************
919__attribute__((section(".text.link2.GPIO_setInterruptPin")))
920extern void
922
923//*****************************************************************************
924//
945//
946//*****************************************************************************
947__attribute__((section(".text.link2.GPIO_setPadConfig")))
948extern void
949GPIO_setPadConfig(uint32_t pin, uint32_t pinType);
950
951//*****************************************************************************
952//
962//
963//*****************************************************************************
964extern uint32_t
965GPIO_getPadConfig(uint32_t pin);
966
967//*****************************************************************************
968//
985//
986//*****************************************************************************
987__attribute__((section(".text.link2.GPIO_setQualificationMode")))
988extern void
990
991//*****************************************************************************
992//
1000//
1001//*****************************************************************************
1004
1005//*****************************************************************************
1006//
1025//
1026//*****************************************************************************
1027__attribute__((section(".text.link2.GPIO_setQualificationPeriod")))
1028extern void
1029GPIO_setQualificationPeriod(uint32_t pin, uint32_t divider);
1030
1031//*****************************************************************************
1032//
1047//
1048//*****************************************************************************
1049__attribute__((section(".text.link2.GPIO_setControllerCore")))
1050extern void
1052
1053//*****************************************************************************
1054//
1075//
1076//*****************************************************************************
1077__attribute__((section(".text.link2.GPIO_setAnalogMode")))
1078extern void
1080
1081//*****************************************************************************
1082//
1097//
1098//*****************************************************************************
1099__attribute__((section(".text.link2.GPIO_setPinConfig")))
1100extern void
1101GPIO_setPinConfig(uint32_t pinConfig);
1102
1103//*****************************************************************************
1104//
1105// Close the Doxygen group.
1107//
1108//*****************************************************************************
1109
1110//*****************************************************************************
1111//
1112// Mark the end of the C bindings section for C++ compilers.
1113//
1114//*****************************************************************************
1115#ifdef __cplusplus
1116}
1117#endif
1118
1119#endif // GPIO_H
static void GPIO_commitPortConfig(GPIO_Port port, uint32_t pinMask)
Definition gpio.h:835
GPIO_Direction
Definition gpio.h:126
void GPIO_setQualificationPeriod(uint32_t pin, uint32_t divider)
void GPIO_setQualificationMode(uint32_t pin, GPIO_QualificationMode qualification)
static void GPIO_disableInterrupt(GPIO_ExternalIntNum extIntNum)
Definition gpio.h:373
#define GPIO_DATA_READ_REGS_STEP
Definition gpio.h:83
static uint32_t GPIO_readPortData(GPIO_Port port)
Definition gpio.h:560
#define GPIO_GPxCR_INDEX
Definition gpio.h:95
static uint32_t GPIO_readPin(uint32_t pin)
Definition gpio.h:426
void GPIO_setAnalogMode(uint32_t pin, GPIO_AnalogMode mode)
GPIO_Direction GPIO_getDirectionMode(uint32_t pin)
static uint32_t GPIO_readPortDataRegister(GPIO_Port port)
Definition gpio.h:593
#define GPIO_CTRL_REGS_STEP
Definition gpio.h:81
static void GPIO_setInterruptType(GPIO_ExternalIntNum extIntNum, GPIO_IntType intType)
Definition gpio.h:277
GPIO_AnalogMode
Values that can be passed to GPIO_setAnalogMode() as the mode parameter.
Definition gpio.h:164
GPIO_CoreSelect
Definition gpio.h:176
void GPIO_setPinConfig(uint32_t pinConfig)
#define GPIO_GPxLOCK_INDEX
Definition gpio.h:94
void GPIO_setInterruptPin(uint32_t pin, GPIO_ExternalIntNum extIntNum)
#define GPIO_GPxCLEAR_INDEX
Definition gpio.h:99
GPIO_QualificationMode GPIO_getQualificationMode(uint32_t pin)
static GPIO_IntType GPIO_getInterruptType(GPIO_ExternalIntNum extIntNum)
Definition gpio.h:310
static bool GPIO_isPinValid(uint32_t pin)
Definition gpio.h:235
static void GPIO_togglePortPins(GPIO_Port port, uint32_t pinMask)
Definition gpio.h:725
#define GPIO_GPxTOGGLE_INDEX
Definition gpio.h:100
uint32_t GPIO_getPadConfig(uint32_t pin)
static void GPIO_writePortData(GPIO_Port port, uint32_t outVal)
Definition gpio.h:626
static void GPIO_setPortPins(GPIO_Port port, uint32_t pinMask)
Definition gpio.h:659
static uint32_t GPIO_readPinDataRegister(uint32_t pin)
Definition gpio.h:459
static void GPIO_writePin(uint32_t pin, uint32_t outVal)
Definition gpio.h:491
void GPIO_setDirectionMode(uint32_t pin, GPIO_Direction pinIO)
GPIO_IntType
Definition gpio.h:138
GPIO_QualificationMode
Definition gpio.h:151
#define GPIO_GPxDAT_R_INDEX
Definition gpio.h:103
#define GPIO_GPxDAT_INDEX
Definition gpio.h:97
#define GPIO_GPxSET_INDEX
Definition gpio.h:98
void GPIO_setPadConfig(uint32_t pin, uint32_t pinType)
void GPIO_setControllerCore(uint32_t pin, GPIO_CoreSelect core)
GPIO_ExternalIntNum
Definition gpio.h:208
#define GPIO_DATA_REGS_STEP
Definition gpio.h:82
static void GPIO_togglePin(uint32_t pin)
Definition gpio.h:532
static void GPIO_unlockPortConfig(GPIO_Port port, uint32_t pinMask)
Definition gpio.h:797
static uint16_t GPIO_getInterruptCounter(GPIO_ExternalIntNum extIntNum)
Definition gpio.h:400
static void GPIO_lockPortConfig(GPIO_Port port, uint32_t pinMask)
Definition gpio.h:763
GPIO_Port
Definition gpio.h:189
static void GPIO_clearPortPins(GPIO_Port port, uint32_t pinMask)
Definition gpio.h:692
static void GPIO_enableInterrupt(GPIO_ExternalIntNum extIntNum)
Definition gpio.h:342
@ GPIO_DIR_MODE_OUT
Pin is a GPIO output.
Definition gpio.h:128
@ GPIO_DIR_MODE_IN
Pin is a GPIO input.
Definition gpio.h:127
@ GPIO_ANALOG_ENABLED
Pin is in analog mode.
Definition gpio.h:166
@ GPIO_ANALOG_DISABLED
Pin is in digital mode.
Definition gpio.h:165
@ GPIO_CORE_CPU2
CPU2 selected as controller core.
Definition gpio.h:178
@ GPIO_CORE_CPU1
CPU1 selected as controller core.
Definition gpio.h:177
@ GPIO_CORE_CPU3
CPU3 selected as controller core.
Definition gpio.h:179
@ GPIO_INT_TYPE_RISING_EDGE
Interrupt on rising edge.
Definition gpio.h:140
@ GPIO_INT_TYPE_FALLING_EDGE
Interrupt on falling edge.
Definition gpio.h:139
@ GPIO_INT_TYPE_BOTH_EDGES
Interrupt on both edges.
Definition gpio.h:141
@ GPIO_QUAL_3SAMPLE
Qualified with 3 samples.
Definition gpio.h:153
@ GPIO_QUAL_6SAMPLE
Qualified with 6 samples.
Definition gpio.h:154
@ GPIO_QUAL_SYNC
Synchronization to SYSCLK.
Definition gpio.h:152
@ GPIO_QUAL_ASYNC
No synchronization.
Definition gpio.h:155
@ GPIO_INT_XINT1
External Interrupt 1.
Definition gpio.h:209
@ GPIO_INT_XINT4
External Interrupt 4.
Definition gpio.h:212
@ GPIO_INT_XINT2
External Interrupt 2.
Definition gpio.h:210
@ GPIO_INT_XINT5
External Interrupt 5.
Definition gpio.h:213
@ GPIO_INT_XINT3
External Interrupt 3.
Definition gpio.h:211
@ GPIO_PORT_H
GPIO port H.
Definition gpio.h:197
@ GPIO_PORT_D
GPIO port D.
Definition gpio.h:193
@ GPIO_PORT_F
GPIO port F.
Definition gpio.h:195
@ GPIO_PORT_A
GPIO port A.
Definition gpio.h:190
@ GPIO_PORT_G
GPIO port G.
Definition gpio.h:196
@ GPIO_PORT_E
GPIO port E.
Definition gpio.h:194
@ GPIO_PORT_C
GPIO port C.
Definition gpio.h:192
@ GPIO_PORT_B
GPIO port B.
Definition gpio.h:191