]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/driverlib/gpio.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / driverlib / gpio.c
1 /*
2  * -------------------------------------------
3  *    MSP432 DriverLib - v01_04_00_18 
4  * -------------------------------------------
5  *
6  * --COPYRIGHT--,BSD,BSD
7  * Copyright (c) 2015, 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
45 static const uint32_t GPIO_PORT_TO_INT[] =
46 { 0x00,
47 INT_PORT1,
48 INT_PORT2,
49 INT_PORT3,
50 INT_PORT4,
51 INT_PORT5,
52 INT_PORT6 };
53
54 static const uint32_t GPIO_PORT_TO_BASE[] =
55 {   0x00,
56                 0x40004C00,
57                 0x40004C01,
58                 0x40004C20,
59                 0x40004C21,
60                 0x40004C40,
61                 0x40004C41,
62                 0x40004C60,
63                 0x40004C61,
64                 0x40004C80,
65                 0x40004C81,
66                 0x40004D20
67     };
68
69 void GPIO_setAsOutputPin(uint_fast8_t selectedPort, uint_fast16_t selectedPins)
70 {
71     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
72
73     HWREG16(baseAddress + OFS_PASEL0) &= ~selectedPins;
74     HWREG16(baseAddress + OFS_PASEL1) &= ~selectedPins;
75     HWREG16(baseAddress + OFS_PADIR) |= selectedPins;
76
77     return;
78 }
79
80 void GPIO_setAsInputPin(uint_fast8_t selectedPort, uint_fast16_t selectedPins)
81 {
82
83     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
84
85     HWREG16(baseAddress + OFS_PASEL0) &= ~selectedPins;
86     HWREG16(baseAddress + OFS_PASEL1) &= ~selectedPins;
87     HWREG16(baseAddress + OFS_PADIR) &= ~selectedPins;
88     HWREG16(baseAddress + OFS_PAREN) &= ~selectedPins;
89 }
90
91
92 void GPIO_setAsPeripheralModuleFunctionOutputPin(uint_fast8_t selectedPort,
93         uint_fast16_t selectedPins, uint_fast8_t mode)
94 {
95
96     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
97
98     HWREG16(baseAddress + OFS_PADIR) |= selectedPins;
99     switch (mode)
100     {
101     case GPIO_PRIMARY_MODULE_FUNCTION:
102         HWREG16(baseAddress + OFS_PASEL0) |= selectedPins;
103         HWREG16(baseAddress + OFS_PASEL1) &= ~selectedPins;
104         break;
105     case GPIO_SECONDARY_MODULE_FUNCTION:
106         HWREG16(baseAddress + OFS_PASEL0) &= ~selectedPins;
107         HWREG16(baseAddress + OFS_PASEL1) |= selectedPins;
108         break;
109     case GPIO_TERTIARY_MODULE_FUNCTION:
110         HWREG16(baseAddress + OFS_PASEL0) |= selectedPins;
111         HWREG16(baseAddress + OFS_PASEL1) |= selectedPins;
112         break;
113     }
114 }
115
116
117 void GPIO_setAsPeripheralModuleFunctionInputPin(uint_fast8_t selectedPort,
118         uint_fast16_t selectedPins, uint_fast8_t mode)
119 {
120     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
121
122     HWREG16(baseAddress + OFS_PADIR) &= ~selectedPins;
123     switch (mode)
124     {
125     case GPIO_PRIMARY_MODULE_FUNCTION:
126         HWREG16(baseAddress + OFS_PASEL0) |= selectedPins;
127         HWREG16(baseAddress + OFS_PASEL1) &= ~selectedPins;
128         break;
129     case GPIO_SECONDARY_MODULE_FUNCTION:
130         HWREG16(baseAddress + OFS_PASEL0) &= ~selectedPins;
131         HWREG16(baseAddress + OFS_PASEL1) |= selectedPins;
132         break;
133     case GPIO_TERTIARY_MODULE_FUNCTION:
134         HWREG16(baseAddress + OFS_PASEL0) |= selectedPins;
135         HWREG16(baseAddress + OFS_PASEL1) |= selectedPins;
136         break;
137     }
138 }
139
140
141 void GPIO_setOutputHighOnPin(uint_fast8_t selectedPort,
142         uint_fast16_t selectedPins)
143 {
144
145     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
146
147     HWREG16(baseAddress + OFS_PAOUT) |= selectedPins;
148 }
149
150
151 void GPIO_setOutputLowOnPin(uint_fast8_t selectedPort,
152         uint_fast16_t selectedPins)
153 {
154
155     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
156
157     HWREG16(baseAddress + OFS_PAOUT) &= ~selectedPins;
158 }
159
160
161 void GPIO_toggleOutputOnPin(uint_fast8_t selectedPort,
162         uint_fast16_t selectedPins)
163 {
164
165     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
166
167     HWREG16(baseAddress + OFS_PAOUT) ^= selectedPins;
168 }
169
170
171 void GPIO_setAsInputPinWithPullDownResistor(uint_fast8_t selectedPort,
172         uint_fast16_t selectedPins)
173 {
174
175     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
176
177     HWREG16(baseAddress + OFS_PASEL0) &= ~selectedPins;
178     HWREG16(baseAddress + OFS_PASEL1) &= ~selectedPins;
179
180     HWREG16(baseAddress + OFS_PADIR) &= ~selectedPins;
181     HWREG16(baseAddress + OFS_PAREN) |= selectedPins;
182     HWREG16(baseAddress + OFS_PAOUT) &= ~selectedPins;
183 }
184
185
186 void GPIO_setAsInputPinWithPullUpResistor(uint_fast8_t selectedPort,
187         uint_fast16_t selectedPins)
188 {
189
190     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
191
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;
197 }
198
199
200 uint8_t GPIO_getInputPinValue(uint_fast8_t selectedPort,
201         uint_fast16_t selectedPins)
202 {
203         uint16_t inputPinValue;
204     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
205
206     inputPinValue = HWREG16(baseAddress + OFS_PAIN) & (selectedPins);
207
208     if (inputPinValue > 0)
209         return GPIO_INPUT_PIN_HIGH;
210     return GPIO_INPUT_PIN_LOW;
211 }
212
213
214 void GPIO_enableInterrupt(uint_fast8_t selectedPort, uint_fast16_t selectedPins)
215 {
216
217     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
218
219     HWREG16(baseAddress + OFS_PAIE) |= selectedPins;
220 }
221
222
223 void GPIO_disableInterrupt(uint_fast8_t selectedPort,
224         uint_fast16_t selectedPins)
225 {
226
227     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
228
229     HWREG16(baseAddress + OFS_PAIE) &= ~selectedPins;
230 }
231
232
233 uint_fast16_t GPIO_getInterruptStatus(uint_fast8_t selectedPort,
234         uint_fast16_t selectedPins)
235 {
236
237     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
238
239     return HWREG16(baseAddress + OFS_PAIFG) & selectedPins;
240 }
241
242
243 void GPIO_clearInterruptFlag(uint_fast8_t selectedPort,
244         uint_fast16_t selectedPins)
245 {
246
247     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
248
249
250     HWREG16(baseAddress + OFS_PAIFG) &= ~selectedPins;
251 }
252
253
254 void GPIO_interruptEdgeSelect(uint_fast8_t selectedPort,
255         uint_fast16_t selectedPins, uint_fast8_t edgeSelect)
256 {
257
258     uint32_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
259
260
261     if (GPIO_LOW_TO_HIGH_TRANSITION == edgeSelect)
262         HWREG16(baseAddress + OFS_PAIES) &= ~selectedPins;
263     else
264         HWREG16(baseAddress + OFS_PAIES) |= selectedPins;
265 }
266
267 uint_fast16_t GPIO_getEnabledInterruptStatus(uint_fast8_t selectedPort)
268 {
269     uint_fast16_t pendingInts;
270     uint32_t baseAddr;
271
272     pendingInts = GPIO_getInterruptStatus(selectedPort, 0xFFFF);
273     baseAddr = GPIO_PORT_TO_BASE[selectedPort];
274
275     ASSERT(baseAddr != 0xFFFF);
276
277     switch (selectedPort)
278     {
279     case GPIO_PORT_P1:
280     case GPIO_PORT_P3:
281     case GPIO_PORT_P5:
282     case GPIO_PORT_P7:
283     case GPIO_PORT_P9:
284         return (HWREG8(baseAddr + OFS_P1IE) & pendingInts);
285     case GPIO_PORT_P2:
286     case GPIO_PORT_P4:
287     case GPIO_PORT_P6:
288     case GPIO_PORT_P8:
289     case GPIO_PORT_P10:
290         return (HWREG8(baseAddr + OFS_P2IE) & pendingInts);
291     case GPIO_PORT_PJ:
292         return (HWREG16(baseAddr + OFS_PAIE) & pendingInts);
293     default:
294         return 0;
295     }
296 }
297
298
299 void GPIO_setDriveStrengthHigh(uint_fast8_t selectedPort,
300         uint_fast8_t selectedPins)
301 {
302     uint32_t baseAddr;
303
304     baseAddr = GPIO_PORT_TO_BASE[selectedPort];
305
306     HWREG8(baseAddr + OFS_PADS) |= selectedPins;
307
308 }
309
310 void GPIO_setDriveStrengthLow(uint_fast8_t selectedPort,
311         uint_fast8_t selectedPins)
312 {
313     uint32_t baseAddr;
314
315     baseAddr = GPIO_PORT_TO_BASE[selectedPort];
316
317     HWREG8(baseAddr + OFS_PADS) &= ~selectedPins;
318
319 }
320
321 void GPIO_registerInterrupt(uint_fast8_t selectedPort, void (*intHandler)(void))
322 {
323     uint32_t wPortInt;
324
325     wPortInt = GPIO_PORT_TO_INT[selectedPort];
326
327     //
328     // Register the interrupt handler, returning an error if an error occurs.
329     //
330     Interrupt_registerInterrupt(wPortInt, intHandler);
331
332     //
333     // Enable the system control interrupt.
334     //
335     Interrupt_enableInterrupt(wPortInt);
336 }
337
338
339 void GPIO_unregisterInterrupt(uint_fast8_t selectedPort)
340 {
341     uint32_t wPortInt;
342
343     wPortInt = GPIO_PORT_TO_INT[selectedPort];
344
345     //
346     // Disable the interrupt.
347     //
348     Interrupt_disableInterrupt(wPortInt);
349
350     //
351     // Unregister the interrupt handler.
352     //
353     Interrupt_unregisterInterrupt(wPortInt);
354 }
355
356