2 * -------------------------------------------
3 * MSP432 DriverLib - v01_04_00_18
4 * -------------------------------------------
6 * --COPYRIGHT--,BSD,BSD
7 * Copyright (c) 2015, Texas Instruments Incorporated
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * * Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * * Neither the name of Texas Instruments Incorporated nor the names of
22 * its contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
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,
27 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 /* Standard Includes */
40 /* DriverLib Includes */
43 #include <interrupt.h>
45 static const uint32_t GPIO_PORT_TO_INT[] =
54 static const uint32_t GPIO_PORT_TO_BASE[] =
69 void GPIO_setAsOutputPin(uint_fast8_t selectedPort, uint_fast16_t selectedPins)
71 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
73 HWREG16(baseAddress + OFS_PASEL0) &= ~selectedPins;
74 HWREG16(baseAddress + OFS_PASEL1) &= ~selectedPins;
75 HWREG16(baseAddress + OFS_PADIR) |= selectedPins;
80 void GPIO_setAsInputPin(uint_fast8_t selectedPort, uint_fast16_t selectedPins)
83 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
85 HWREG16(baseAddress + OFS_PASEL0) &= ~selectedPins;
86 HWREG16(baseAddress + OFS_PASEL1) &= ~selectedPins;
87 HWREG16(baseAddress + OFS_PADIR) &= ~selectedPins;
88 HWREG16(baseAddress + OFS_PAREN) &= ~selectedPins;
92 void GPIO_setAsPeripheralModuleFunctionOutputPin(uint_fast8_t selectedPort,
93 uint_fast16_t selectedPins, uint_fast8_t mode)
96 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
98 HWREG16(baseAddress + OFS_PADIR) |= selectedPins;
101 case GPIO_PRIMARY_MODULE_FUNCTION:
102 HWREG16(baseAddress + OFS_PASEL0) |= selectedPins;
103 HWREG16(baseAddress + OFS_PASEL1) &= ~selectedPins;
105 case GPIO_SECONDARY_MODULE_FUNCTION:
106 HWREG16(baseAddress + OFS_PASEL0) &= ~selectedPins;
107 HWREG16(baseAddress + OFS_PASEL1) |= selectedPins;
109 case GPIO_TERTIARY_MODULE_FUNCTION:
110 HWREG16(baseAddress + OFS_PASEL0) |= selectedPins;
111 HWREG16(baseAddress + OFS_PASEL1) |= selectedPins;
117 void GPIO_setAsPeripheralModuleFunctionInputPin(uint_fast8_t selectedPort,
118 uint_fast16_t selectedPins, uint_fast8_t mode)
120 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
122 HWREG16(baseAddress + OFS_PADIR) &= ~selectedPins;
125 case GPIO_PRIMARY_MODULE_FUNCTION:
126 HWREG16(baseAddress + OFS_PASEL0) |= selectedPins;
127 HWREG16(baseAddress + OFS_PASEL1) &= ~selectedPins;
129 case GPIO_SECONDARY_MODULE_FUNCTION:
130 HWREG16(baseAddress + OFS_PASEL0) &= ~selectedPins;
131 HWREG16(baseAddress + OFS_PASEL1) |= selectedPins;
133 case GPIO_TERTIARY_MODULE_FUNCTION:
134 HWREG16(baseAddress + OFS_PASEL0) |= selectedPins;
135 HWREG16(baseAddress + OFS_PASEL1) |= selectedPins;
141 void GPIO_setOutputHighOnPin(uint_fast8_t selectedPort,
142 uint_fast16_t selectedPins)
145 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
147 HWREG16(baseAddress + OFS_PAOUT) |= selectedPins;
151 void GPIO_setOutputLowOnPin(uint_fast8_t selectedPort,
152 uint_fast16_t selectedPins)
155 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
157 HWREG16(baseAddress + OFS_PAOUT) &= ~selectedPins;
161 void GPIO_toggleOutputOnPin(uint_fast8_t selectedPort,
162 uint_fast16_t selectedPins)
165 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
167 HWREG16(baseAddress + OFS_PAOUT) ^= selectedPins;
171 void GPIO_setAsInputPinWithPullDownResistor(uint_fast8_t selectedPort,
172 uint_fast16_t selectedPins)
175 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
177 HWREG16(baseAddress + OFS_PASEL0) &= ~selectedPins;
178 HWREG16(baseAddress + OFS_PASEL1) &= ~selectedPins;
180 HWREG16(baseAddress + OFS_PADIR) &= ~selectedPins;
181 HWREG16(baseAddress + OFS_PAREN) |= selectedPins;
182 HWREG16(baseAddress + OFS_PAOUT) &= ~selectedPins;
186 void GPIO_setAsInputPinWithPullUpResistor(uint_fast8_t selectedPort,
187 uint_fast16_t selectedPins)
190 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
192 HWREG16(baseAddress + OFS_PASEL0) &= ~selectedPins;
193 HWREG16(baseAddress + OFS_PASEL1) &= ~selectedPins;
194 HWREG16(baseAddress + OFS_PADIR) &= ~selectedPins;
195 HWREG16(baseAddress + OFS_PAREN) |= selectedPins;
196 HWREG16(baseAddress + OFS_PAOUT) |= selectedPins;
200 uint8_t GPIO_getInputPinValue(uint_fast8_t selectedPort,
201 uint_fast16_t selectedPins)
203 uint16_t inputPinValue;
204 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
206 inputPinValue = HWREG16(baseAddress + OFS_PAIN) & (selectedPins);
208 if (inputPinValue > 0)
209 return GPIO_INPUT_PIN_HIGH;
210 return GPIO_INPUT_PIN_LOW;
214 void GPIO_enableInterrupt(uint_fast8_t selectedPort, uint_fast16_t selectedPins)
217 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
219 HWREG16(baseAddress + OFS_PAIE) |= selectedPins;
223 void GPIO_disableInterrupt(uint_fast8_t selectedPort,
224 uint_fast16_t selectedPins)
227 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
229 HWREG16(baseAddress + OFS_PAIE) &= ~selectedPins;
233 uint_fast16_t GPIO_getInterruptStatus(uint_fast8_t selectedPort,
234 uint_fast16_t selectedPins)
237 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
239 return HWREG16(baseAddress + OFS_PAIFG) & selectedPins;
243 void GPIO_clearInterruptFlag(uint_fast8_t selectedPort,
244 uint_fast16_t selectedPins)
247 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
250 HWREG16(baseAddress + OFS_PAIFG) &= ~selectedPins;
254 void GPIO_interruptEdgeSelect(uint_fast8_t selectedPort,
255 uint_fast16_t selectedPins, uint_fast8_t edgeSelect)
258 uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
261 if (GPIO_LOW_TO_HIGH_TRANSITION == edgeSelect)
262 HWREG16(baseAddress + OFS_PAIES) &= ~selectedPins;
264 HWREG16(baseAddress + OFS_PAIES) |= selectedPins;
267 uint_fast16_t GPIO_getEnabledInterruptStatus(uint_fast8_t selectedPort)
269 uint_fast16_t pendingInts;
272 pendingInts = GPIO_getInterruptStatus(selectedPort, 0xFFFF);
273 baseAddr = GPIO_PORT_TO_BASE[selectedPort];
275 ASSERT(baseAddr != 0xFFFF);
277 switch (selectedPort)
284 return (HWREG8(baseAddr + OFS_P1IE) & pendingInts);
290 return (HWREG8(baseAddr + OFS_P2IE) & pendingInts);
292 return (HWREG16(baseAddr + OFS_PAIE) & pendingInts);
299 void GPIO_setDriveStrengthHigh(uint_fast8_t selectedPort,
300 uint_fast8_t selectedPins)
304 baseAddr = GPIO_PORT_TO_BASE[selectedPort];
306 HWREG8(baseAddr + OFS_PADS) |= selectedPins;
310 void GPIO_setDriveStrengthLow(uint_fast8_t selectedPort,
311 uint_fast8_t selectedPins)
315 baseAddr = GPIO_PORT_TO_BASE[selectedPort];
317 HWREG8(baseAddr + OFS_PADS) &= ~selectedPins;
321 void GPIO_registerInterrupt(uint_fast8_t selectedPort, void (*intHandler)(void))
325 wPortInt = GPIO_PORT_TO_INT[selectedPort];
328 // Register the interrupt handler, returning an error if an error occurs.
330 Interrupt_registerInterrupt(wPortInt, intHandler);
333 // Enable the system control interrupt.
335 Interrupt_enableInterrupt(wPortInt);
339 void GPIO_unregisterInterrupt(uint_fast8_t selectedPort)
343 wPortInt = GPIO_PORT_TO_INT[selectedPort];
346 // Disable the interrupt.
348 Interrupt_disableInterrupt(wPortInt);
351 // Unregister the interrupt handler.
353 Interrupt_unregisterInterrupt(wPortInt);