Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
TivaGPIOGeneric.cxx
Go to the documentation of this file.
1
36//#include <stdlib.h>
37
38#include "driverlib/rom_map.h"
39#include "driverlib/rom.h"
40
46TivaGpio::TivaGpio(unsigned number, Mode mode, Value safe)
47 : Gpio(number, mode, safe)
48 , base(0)
49{
50 /* test for plausibly valid GPIO number */
51 HASSERT(number < PIN_COUNT);
52 /* pull up and pull down should be mutually exclusive */
53 HASSERT((mode & (PULL_UP | PULL_DOWN)) == (PULL_UP | PULL_DOWN));
54
55 /* we override the base class default of 0 */
56 bit = 0x01 << (number % 8);
57
58 /* setup port specific attributes */
59 if (number <= PORTA_7)
60 {
61 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
62 base = GPIO_PORTA_BASE;
63 }
64 else if (number <= PORTB_7)
65 {
66 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
67 base = GPIO_PORTB_BASE;
68 }
69 else if (number <= PORTC_7)
70 {
71 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
72 base = GPIO_PORTC_BASE;
73 }
74 else if (number <= PORTD_7)
75 {
76 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
77 base = GPIO_PORTD_BASE;
78 }
79 else if (number <= PORTE_7)
80 {
81 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
82 base = GPIO_PORTE_BASE;
83 }
84 else if (number <= PORTF_7)
85 {
86 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
87 base = GPIO_PORTF_BASE;
88 }
89 else if (number <= PORTG_7)
90 {
91 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
92 base = GPIO_PORTG_BASE;
93 }
94 else if (number <= PORTH_7)
95 {
96 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH);
97 base = GPIO_PORTH_BASE;
98 }
99 else if (number <= PORTJ_7)
100 {
101 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
102 base = GPIO_PORTJ_BASE;
103 }
104 else if (number <= PORTK_7)
105 {
106 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOK);
107 base = GPIO_PORTK_BASE;
108 }
109 else if (number <= PORTL_7)
110 {
111 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOL);
112 base = GPIO_PORTL_BASE;
113 }
114 else if (number <= PORTM_7)
115 {
116 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOM);
117 base = GPIO_PORTM_BASE;
118 }
119 else if (number <= PORTN_7)
120 {
121 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
122 base = GPIO_PORTN_BASE;
123 }
124 else if (number <= PORTP_7)
125 {
126 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOP);
127 base = GPIO_PORTP_BASE;
128 }
129 else if (number <= PORTQ_7)
130 {
131 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOQ);
132 base = GPIO_PORTQ_BASE;
133 }
134 else if (number <= PORTR_7)
135 {
136 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOR);
137 base = GPIO_PORTR_BASE;
138 }
139 else if (number <= PORTS_7)
140 {
141 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOS);
142 base = GPIO_PORTS_BASE;
143 }
144 else if (number <= PORTT_7)
145 {
146 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOT);
147 base = GPIO_PORTT_BASE;
148 }
149
150 /* configure for input/output */
151 if (mode & OUTPUT)
152 {
153 MAP_GPIOPinTypeGPIOOutput(base, bit);
154 MAP_GPIOPinWrite(base, bit, (safeValue ^ invert) ? bit : 0);
155 }
156 else
157 {
158 MAP_GPIOPinTypeGPIOInput(base, bit);
159 }
160
161 /* configure drive stregnth and type */
162 uint32_t strength = mode & LED ? GPIO_STRENGTH_8MA : GPIO_STRENGTH_2MA;
163 uint32_t type;
164 if (mode & PULL_UP)
165 {
166 type = GPIO_PIN_TYPE_STD_WPU;
167 }
168 else if (mode & PULL_DOWN)
169 {
170 type = GPIO_PIN_TYPE_STD_WPD;
171 }
172 else
173 {
174 type = GPIO_PIN_TYPE_STD;
175 }
176 MAP_GPIOPadConfigSet(base, bit, strength, type);
177
178 /* regiseter the GPIO pin number with our tracking mechanism */
179 track();
180}
181
185void TivaGpio::direction(Mode mode)
186{
187 HASSERT(mode == INPUT || mode == OUTPUT);
188 MAP_GPIODirModeSet(base, bit, mode == INPUT ? GPIO_DIR_MODE_IN :
189 GPIO_DIR_MODE_OUT);
190}
191
196{
197 uint32_t mode = MAP_GPIODirModeGet(base, bit);
198 switch (mode)
199 {
200 default:
201 HASSERT(0);
202 case GPIO_DIR_MODE_IN:
203 return INPUT;
204 case GPIO_DIR_MODE_OUT:
205 return OUTPUT;
206 }
207}
@ PORTP_7
Port P bit 7.
@ PORTQ_7
Port Q bit 7.
@ PORTF_7
Port F bit 7.
@ PORTB_7
Port B bit 7.
@ PORTH_7
Port H bit 7.
@ PORTN_7
Port N bit 7.
@ PORTL_7
Port L bit 7.
@ PORTE_7
Port E bit 7.
@ PORTR_7
Port R bit 7.
@ PORTJ_7
Port J bit 7.
@ PORTA_7
Port A bit 7.
@ PORTG_7
Port G bit 7.
@ PORTT_7
Port T bit 7.
@ PORTK_7
Port K bit 7.
@ PORTD_7
Port D bit 7.
@ PORTS_7
Port S bit 7.
@ PORTM_7
Port M bit 7.
@ PORTC_7
Port C bit 7.
@ PIN_COUNT
Total number of GPIO pins.
OS-independent abstraction for GPIO.
Definition Gpio.hxx:43
Value
Defines the options for GPIO level.
Definition Gpio.hxx:62
constexpr TivaGpio()
This constructor is constexpr which ensures that the object can be initialized in the data section.
Definition TivaGPIO.hxx:77
uint32_t base
BASE address of the GPIO.
Direction direction() const OVERRIDE
Gets the GPIO direction.
Definition TivaGPIO.hxx:113
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138