]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/driverlib/gpio.c
Update MSP432 projects to use updated driver library files.
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / driverlib / gpio.c
1 /*
2  * -------------------------------------------
3  *    MSP432 DriverLib - v3_10_00_09 
4  * -------------------------------------------
5  *
6  * --COPYRIGHT--,BSD,BSD
7  * Copyright (c) 2014, Texas Instruments Incorporated
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * *  Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
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.
20  *
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.
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,
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.
36  * --/COPYRIGHT--*/
37 /* Standard Includes */
38 #include <stdint.h>
39
40 /* DriverLib Includes */
41 #include <gpio.h>
42 #include <debug.h>
43 #include <interrupt.h>
44 #include <hw_memmap.h>
45
46 /* DriverLib internal GPIO register offset for optimized performace */
47 #define OFS_LIB_PAIN  ((uint32_t)&P1->IN - (uint32_t)P1)
48 #define OFS_LIB_PAOUT   ((uint32_t)&P1->OUT - (uint32_t)P1)
49 #define OFS_LIB_PADIR   ((uint32_t)&P1->DIR - (uint32_t)P1)
50 #define OFS_LIB_PAREN   ((uint32_t)&P1->REN - (uint32_t)P1)
51 #define OFS_LIB_PADS   ((uint32_t)&P1->DS - (uint32_t)P1)
52 #define OFS_LIB_PASEL0  ((uint32_t)&P1->SEL0 - (uint32_t)P1)
53 #define OFS_LIB_PASEL1  ((uint32_t)&P1->SEL1 - (uint32_t)P1)
54 #define OFS_LIB_PAIE    ((uint32_t)&P1->IE - (uint32_t)P1)
55 #define OFS_LIB_PAIES   ((uint32_t)&P1->IES - (uint32_t)P1)
56 #define OFS_LIB_PAIFG   ((uint32_t)&P1->IFG - (uint32_t)P1)
57 #define OFS_LIB_P1IE    ((uint32_t)&P1->IE - (uint32_t)P1)
58 #define OFS_LIB_P2IE    ((uint32_t)&P2->IE - (uint32_t)P2)
59
60 static const uint32_t GPIO_PORT_TO_INT[] =
61 { 0x00,
62 INT_PORT1,
63 INT_PORT2,
64 INT_PORT3,
65 INT_PORT4,
66 INT_PORT5,
67 INT_PORT6 };
68
69 static uint32_t GPIO_PORT_TO_BASE[] =
70 {   0x00,
71         (uint32_t)P1,
72         (uint32_t)P1+1,
73         (uint32_t)P3,
74         (uint32_t)P3+1,
75         (uint32_t)P5,
76         (uint32_t)P5+1,
77         (uint32_t)P7,
78         (uint32_t)P7+1,
79         (uint32_t)P9,
80         (uint32_t)P9+1,
81         (uint32_t)PJ
82     };
83
84 void GPIO_setAsOutputPin(uint_fast8_t selectedPort, uint_fast16_t selectedPins)
85 {
86     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
87
88     HWREG16(baseAddress + OFS_LIB_PASEL0) &= ~selectedPins;
89     HWREG16(baseAddress + OFS_LIB_PASEL1) &= ~selectedPins;
90     HWREG16(baseAddress + OFS_LIB_PADIR) |= selectedPins;
91 }
92
93
94 void GPIO_setAsInputPin(uint_fast8_t selectedPort, uint_fast16_t selectedPins)
95 {
96     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
97
98     HWREG16(baseAddress + OFS_LIB_PASEL0) &= ~selectedPins;
99     HWREG16(baseAddress + OFS_LIB_PASEL1) &= ~selectedPins;
100     HWREG16(baseAddress + OFS_LIB_PADIR) &= ~selectedPins;
101     HWREG16(baseAddress + OFS_LIB_PAREN) &= ~selectedPins;
102 }
103
104
105 void GPIO_setAsPeripheralModuleFunctionOutputPin(uint_fast8_t selectedPort,
106         uint_fast16_t selectedPins, uint_fast8_t mode)
107 {
108
109     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
110
111     HWREG16(baseAddress + OFS_LIB_PADIR) |= selectedPins;
112     switch (mode)
113     {
114     case GPIO_PRIMARY_MODULE_FUNCTION:
115         HWREG16(baseAddress + OFS_LIB_PASEL0) |= selectedPins;
116         HWREG16(baseAddress + OFS_LIB_PASEL1) &= ~selectedPins;
117         break;
118     case GPIO_SECONDARY_MODULE_FUNCTION:
119         HWREG16(baseAddress + OFS_LIB_PASEL0) &= ~selectedPins;
120         HWREG16(baseAddress + OFS_LIB_PASEL1) |= selectedPins;
121         break;
122     case GPIO_TERTIARY_MODULE_FUNCTION:
123         HWREG16(baseAddress + OFS_LIB_PASEL0) |= selectedPins;
124         HWREG16(baseAddress + OFS_LIB_PASEL1) |= selectedPins;
125         break;
126     }
127 }
128
129
130 void GPIO_setAsPeripheralModuleFunctionInputPin(uint_fast8_t selectedPort,
131         uint_fast16_t selectedPins, uint_fast8_t mode)
132 {
133     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
134
135     HWREG16(baseAddress + OFS_LIB_PADIR) &= ~selectedPins;
136     switch (mode)
137     {
138     case GPIO_PRIMARY_MODULE_FUNCTION:
139         HWREG16(baseAddress + OFS_LIB_PASEL0) |= selectedPins;
140         HWREG16(baseAddress + OFS_LIB_PASEL1) &= ~selectedPins;
141         break;
142     case GPIO_SECONDARY_MODULE_FUNCTION:
143         HWREG16(baseAddress + OFS_LIB_PASEL0) &= ~selectedPins;
144         HWREG16(baseAddress + OFS_LIB_PASEL1) |= selectedPins;
145         break;
146     case GPIO_TERTIARY_MODULE_FUNCTION:
147         HWREG16(baseAddress + OFS_LIB_PASEL0) |= selectedPins;
148         HWREG16(baseAddress + OFS_LIB_PASEL1) |= selectedPins;
149         break;
150     }
151 }
152
153
154 void GPIO_setOutputHighOnPin(uint_fast8_t selectedPort,
155         uint_fast16_t selectedPins)
156 {
157
158     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
159
160     HWREG16(baseAddress + OFS_LIB_PAOUT) |= selectedPins;
161 }
162
163
164 void GPIO_setOutputLowOnPin(uint_fast8_t selectedPort,
165         uint_fast16_t selectedPins)
166 {
167
168     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
169
170     HWREG16(baseAddress + OFS_LIB_PAOUT) &= ~selectedPins;
171 }
172
173
174 void GPIO_toggleOutputOnPin(uint_fast8_t selectedPort,
175         uint_fast16_t selectedPins)
176 {
177
178     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
179
180     HWREG16(baseAddress + OFS_LIB_PAOUT) ^= selectedPins;
181 }
182
183
184 void GPIO_setAsInputPinWithPullDownResistor(uint_fast8_t selectedPort,
185         uint_fast16_t selectedPins)
186 {
187
188     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
189
190     HWREG16(baseAddress + OFS_LIB_PASEL0) &= ~selectedPins;
191     HWREG16(baseAddress + OFS_LIB_PASEL1) &= ~selectedPins;
192
193     HWREG16(baseAddress + OFS_LIB_PADIR) &= ~selectedPins;
194     HWREG16(baseAddress + OFS_LIB_PAREN) |= selectedPins;
195     HWREG16(baseAddress + OFS_LIB_PAOUT) &= ~selectedPins;
196 }
197
198
199 void GPIO_setAsInputPinWithPullUpResistor(uint_fast8_t selectedPort,
200         uint_fast16_t selectedPins)
201 {
202
203     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
204
205     HWREG16(baseAddress + OFS_LIB_PASEL0) &= ~selectedPins;
206     HWREG16(baseAddress + OFS_LIB_PASEL1) &= ~selectedPins;
207     HWREG16(baseAddress + OFS_LIB_PADIR) &= ~selectedPins;
208     HWREG16(baseAddress + OFS_LIB_PAREN) |= selectedPins;
209     HWREG16(baseAddress + OFS_LIB_PAOUT) |= selectedPins;
210 }
211
212
213 uint8_t GPIO_getInputPinValue(uint_fast8_t selectedPort,
214         uint_fast16_t selectedPins)
215 {
216     uint_fast16_t inputPinValue;
217     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
218
219     inputPinValue = HWREG16(baseAddress + OFS_LIB_PAIN) & (selectedPins);
220
221     if (inputPinValue > 0)
222         return GPIO_INPUT_PIN_HIGH;
223     return GPIO_INPUT_PIN_LOW;
224 }
225
226
227 void GPIO_enableInterrupt(uint_fast8_t selectedPort, uint_fast16_t selectedPins)
228 {
229
230     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
231
232     HWREG16(baseAddress + OFS_LIB_PAIE) |= selectedPins;
233 }
234
235
236 void GPIO_disableInterrupt(uint_fast8_t selectedPort,
237         uint_fast16_t selectedPins)
238 {
239
240     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
241
242     HWREG16(baseAddress + OFS_LIB_PAIE) &= ~selectedPins;
243 }
244
245
246 uint_fast16_t GPIO_getInterruptStatus(uint_fast8_t selectedPort,
247         uint_fast16_t selectedPins)
248 {
249
250     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
251
252     return HWREG16(baseAddress + OFS_LIB_PAIFG) & selectedPins;
253 }
254
255
256 void GPIO_clearInterruptFlag(uint_fast8_t selectedPort,
257         uint_fast16_t selectedPins)
258 {
259
260     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
261
262
263     HWREG16(baseAddress + OFS_LIB_PAIFG) &= ~selectedPins;
264 }
265
266
267 void GPIO_interruptEdgeSelect(uint_fast8_t selectedPort,
268         uint_fast16_t selectedPins, uint_fast8_t edgeSelect)
269 {
270
271     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
272
273
274     if (GPIO_LOW_TO_HIGH_TRANSITION == edgeSelect)
275         HWREG16(baseAddress + OFS_LIB_PAIES) &= ~selectedPins;
276     else
277         HWREG16(baseAddress + OFS_LIB_PAIES) |= selectedPins;
278 }
279
280 uint_fast16_t GPIO_getEnabledInterruptStatus(uint_fast8_t selectedPort)
281 {
282     uint_fast16_t pendingInts;
283     uint32_t baseAddr;
284
285     pendingInts = GPIO_getInterruptStatus(selectedPort, 0xFFFF);
286     baseAddr = GPIO_PORT_TO_BASE[selectedPort];
287
288     ASSERT(baseAddr != 0xFFFF);
289
290     switch (selectedPort)
291     {
292     case GPIO_PORT_P1:
293     case GPIO_PORT_P3:
294     case GPIO_PORT_P5:
295     case GPIO_PORT_P7:
296     case GPIO_PORT_P9:
297         return (HWREG8(baseAddr + OFS_LIB_P1IE) & pendingInts);
298     case GPIO_PORT_P2:
299     case GPIO_PORT_P4:
300     case GPIO_PORT_P6:
301     case GPIO_PORT_P8:
302     case GPIO_PORT_P10:
303         return (HWREG8(baseAddr + OFS_LIB_P2IE) & pendingInts);
304     case GPIO_PORT_PJ:
305         return (HWREG16(baseAddr + OFS_LIB_PAIE) & pendingInts);
306     default:
307         return 0;
308     }
309 }
310
311
312 void GPIO_setDriveStrengthHigh(uint_fast8_t selectedPort,
313         uint_fast8_t selectedPins)
314 {
315     uint32_t baseAddr;
316
317     baseAddr = GPIO_PORT_TO_BASE[selectedPort];
318
319     HWREG8(baseAddr + OFS_LIB_PADS) |= selectedPins;
320
321 }
322
323 void GPIO_setDriveStrengthLow(uint_fast8_t selectedPort,
324         uint_fast8_t selectedPins)
325 {
326     uint32_t baseAddr;
327
328     baseAddr = GPIO_PORT_TO_BASE[selectedPort];
329
330     HWREG8(baseAddr + OFS_LIB_PADS) &= ~selectedPins;
331
332 }
333
334 void GPIO_registerInterrupt(uint_fast8_t selectedPort, void (*intHandler)(void))
335 {
336     uint32_t wPortInt;
337
338     wPortInt = GPIO_PORT_TO_INT[selectedPort];
339
340     //
341     // Register the interrupt handler, returning an error if an error occurs.
342     //
343     Interrupt_registerInterrupt(wPortInt, intHandler);
344
345     //
346     // Enable the system control interrupt.
347     //
348     Interrupt_enableInterrupt(wPortInt);
349 }
350
351
352 void GPIO_unregisterInterrupt(uint_fast8_t selectedPort)
353 {
354     uint32_t wPortInt;
355
356     wPortInt = GPIO_PORT_TO_INT[selectedPort];
357
358     //
359     // Disable the interrupt.
360     //
361     Interrupt_disableInterrupt(wPortInt);
362
363     //
364     // Unregister the interrupt handler.
365     //
366     Interrupt_unregisterInterrupt(wPortInt);
367 }
368
369