]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/driverlib/comp_e.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / driverlib / comp_e.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 #include <comp_e.h>
38 #include <interrupt.h>
39 #include <debug.h>
40
41 static uint16_t __getRegisterSettingForInput(uint32_t input)
42 {
43     switch (input)
44     {
45     case COMP_E_INPUT0:
46         return CEIPSEL_0;
47     case COMP_E_INPUT1:
48         return CEIPSEL_1;
49     case COMP_E_INPUT2:
50         return CEIPSEL_2;
51     case COMP_E_INPUT3:
52         return CEIPSEL_3;
53     case COMP_E_INPUT4:
54         return CEIPSEL_4;
55     case COMP_E_INPUT5:
56         return CEIPSEL_5;
57     case COMP_E_INPUT6:
58         return CEIPSEL_6;
59     case COMP_E_INPUT7:
60         return CEIPSEL_7;
61     case COMP_E_INPUT8:
62         return CEIPSEL_8;
63     case COMP_E_INPUT9:
64         return CEIPSEL_9;
65     case COMP_E_INPUT10:
66         return CEIPSEL_10;
67     case COMP_E_INPUT11:
68         return CEIPSEL_11;
69     case COMP_E_INPUT12:
70         return CEIPSEL_12;
71     case COMP_E_INPUT13:
72         return CEIPSEL_13;
73     case COMP_E_INPUT14:
74         return CEIPSEL_14;
75     case COMP_E_INPUT15:
76         return CEIPSEL_15;
77     case COMP_E_VREF:
78         return COMP_E_VREF;
79     default:
80         ASSERT(false);
81         return 0x11;
82     }
83
84 }
85
86 bool COMP_E_initModule(uint32_t comparator, const COMP_E_Config *config)
87 {
88     uint_fast8_t positiveTerminalInput = __getRegisterSettingForInput(
89             config->positiveTerminalInput);
90     uint_fast8_t negativeTerminalInput = __getRegisterSettingForInput(
91             config->negativeTerminalInput);
92     bool retVal = true;
93
94     ASSERT(positiveTerminalInput < 0x10); ASSERT(negativeTerminalInput < 0x10);
95     ASSERT(positiveTerminalInput != negativeTerminalInput);
96     ASSERT(
97             config->outputFilterEnableAndDelayLevel
98             <= COMP_E_FILTEROUTPUT_DLYLVL4);
99
100     /* Reset COMPE Control 1 & Interrupt Registers for initialization */
101     COMP_E_CMSIS(comparator)->rCTL0.r = 0;
102     COMP_E_CMSIS(comparator)->rINT.r = 0;
103
104     // Set the Positive Terminal
105     if (COMP_E_VREF != positiveTerminalInput)
106     {
107         // Enable Positive Terminal Input Mux and Set to the appropriate input
108         COMP_E_CMSIS(comparator)->rCTL0.r |= CEIPEN + positiveTerminalInput;
109
110         // Disable the input buffer
111         COMP_E_CMSIS(comparator)->rCTL3.r |= (1 << positiveTerminalInput);
112     } else
113     {
114         //  Reset and Set COMPE Control 2 Register
115         BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL2.r,CERSEL_OFS) = 0;
116     }
117
118     // Set the Negative Terminal
119     if (COMP_E_VREF != negativeTerminalInput)
120     {
121         // Enable Negative Terminal Input Mux and Set  to the appropriate input
122         COMP_E_CMSIS(comparator)->rCTL0.r |= CEIMEN
123                 + (negativeTerminalInput << 8);
124
125         // Disable the input buffer
126         COMP_E_CMSIS(comparator)->rCTL3.r |= (1 << negativeTerminalInput);
127     } else
128     {
129         // Reset and Set COMPE Control 2 Register
130         BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL2.r, CERSEL_OFS) = 1;
131     }
132
133     // Reset and Set COMPE Control 1 Register
134     COMP_E_CMSIS(comparator)->rCTL1.r = config->powerMode
135             + config->outputFilterEnableAndDelayLevel
136             + config->invertedOutputPolarity;
137
138     return retVal;
139 }
140
141 void COMP_E_setReferenceVoltage(uint32_t comparator,
142         uint_fast16_t supplyVoltageReferenceBase,
143         uint_fast16_t lowerLimitSupplyVoltageFractionOf32,
144         uint_fast16_t upperLimitSupplyVoltageFractionOf32)
145 {
146     ASSERT(supplyVoltageReferenceBase <= COMP_E_VREFBASE2_5V);
147     ASSERT(upperLimitSupplyVoltageFractionOf32 <= 32);
148     ASSERT(lowerLimitSupplyVoltageFractionOf32 <= 32); ASSERT(
149             upperLimitSupplyVoltageFractionOf32
150             >= lowerLimitSupplyVoltageFractionOf32);
151
152     BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL1.r, CEMRVS_OFS) = 0;
153     COMP_E_CMSIS(comparator)->rCTL2.r &= CERSEL;
154
155     // Set Voltage Source(Vcc | Vref, resistor ladder or not)
156     if (COMP_E_REFERENCE_AMPLIFIER_DISABLED == supplyVoltageReferenceBase)
157     {
158         COMP_E_CMSIS(comparator)->rCTL2.r |= CERS_1;
159     } else if (lowerLimitSupplyVoltageFractionOf32 == 32)
160     {
161         COMP_E_CMSIS(comparator)->rCTL2.r |= CERS_3;
162     } else
163     {
164         COMP_E_CMSIS(comparator)->rCTL2.r |= CERS_2;
165     }
166
167     // Set COMPE Control 2 Register
168     COMP_E_CMSIS(comparator)->rCTL2.r |= supplyVoltageReferenceBase
169             + ((upperLimitSupplyVoltageFractionOf32 - 1) << 8)
170             + (lowerLimitSupplyVoltageFractionOf32 - 1);
171 }
172
173 void COMP_E_setReferenceAccuracy(uint32_t comparator,
174         uint_fast16_t referenceAccuracy)
175 {
176     ASSERT(
177             (referenceAccuracy == COMP_E_ACCURACY_STATIC)
178             || (referenceAccuracy == COMP_E_ACCURACY_CLOCKED));
179
180     if (referenceAccuracy)
181         BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL2.r, CEREFACC_OFS) = 1;
182     else
183         BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL2.r, CEREFACC_OFS) = 0;
184
185 }
186
187 void COMP_E_setPowerMode(uint32_t comparator, uint_fast16_t powerMode)
188 {
189     COMP_E_CMSIS(comparator)->rCTL1.r = (COMP_E_CMSIS(comparator)->rCTL1.r
190             & ~(CEPWRMD_M)) | powerMode;
191 }
192
193 void COMP_E_enableModule(uint32_t comparator)
194 {
195     BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL1.r, CEON_OFS) = 1;
196 }
197
198 void COMP_E_disableModule(uint32_t comparator)
199 {
200     BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL1.r, CEON_OFS) = 0;
201 }
202
203 void COMP_E_shortInputs(uint32_t comparator)
204 {
205     BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL1.r, CESHORT_OFS) = 1;
206 }
207
208 void COMP_E_unshortInputs(uint32_t comparator)
209 {
210     BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL1.r, CESHORT_OFS) = 0;
211 }
212
213 void COMP_E_disableInputBuffer(uint32_t comparator, uint_fast16_t inputPort)
214 {
215     ASSERT(inputPort <= COMP_E_INPUT15);
216
217     COMP_E_CMSIS(comparator)->rCTL3.r |= (inputPort);
218 }
219
220 void COMP_E_enableInputBuffer(uint32_t comparator, uint_fast16_t inputPort)
221 {
222     ASSERT(inputPort <= COMP_E_INPUT15);
223
224     COMP_E_CMSIS(comparator)->rCTL3.r &= ~(inputPort);
225 }
226
227 void COMP_E_swapIO(uint32_t comparator)
228 {
229     COMP_E_CMSIS(comparator)->rCTL1.r ^= CEEX; // Toggle CEEX bit
230 }
231
232 uint8_t COMP_E_outputValue(uint32_t comparator)
233 {
234     return COMP_E_CMSIS(comparator)->rCTL1.r & CEOUT;
235 }
236
237 void COMP_E_enableInterrupt(uint32_t comparator, uint_fast16_t mask)
238 {
239     // Set the Interrupt enable bit
240     COMP_E_CMSIS(comparator)->rINT.r |= mask;
241 }
242
243 uint_fast16_t COMP_E_getEnabledInterruptStatus(uint32_t comparator)
244 {
245     return COMP_E_getInterruptStatus(comparator) &
246     COMP_E_CMSIS(comparator)->rINT.r;
247 }
248
249 void COMP_E_disableInterrupt(uint32_t comparator, uint_fast16_t mask)
250 {
251     COMP_E_CMSIS(comparator)->rINT.r &= ~(mask);
252 }
253
254 void COMP_E_clearInterruptFlag(uint32_t comparator, uint_fast16_t mask)
255 {
256     COMP_E_CMSIS(comparator)->rINT.r &= ~(mask);
257 }
258
259 uint_fast16_t COMP_E_getInterruptStatus(uint32_t comparator)
260 {
261     return (COMP_E_CMSIS(comparator)->rINT.r & (COMP_E_OUTPUT_INTERRUPT_FLAG |
262     COMP_E_INTERRUPT_FLAG_INVERTED_POLARITY |
263     COMP_E_INTERRUPT_FLAG_READY));
264 }
265
266 void COMP_E_setInterruptEdgeDirection(uint32_t comparator,
267         uint_fast8_t edgeDirection)
268 {
269     ASSERT(edgeDirection <= COMP_E_RISINGEDGE);
270
271     // Set the edge direction that will trigger an interrupt
272     if (COMP_E_RISINGEDGE == edgeDirection)
273         BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL1.r, CEIES_OFS) = 1;
274     else if (COMP_E_FALLINGEDGE == edgeDirection)
275         BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL1.r, CEIES_OFS) = 0;
276 }
277
278 void COMP_E_toggleInterruptEdgeDirection(uint32_t comparator)
279 {
280     COMP_E_CMSIS(comparator)->rCTL1.r ^= CEIES;
281 }
282
283 void COMP_E_registerInterrupt(uint32_t comparator, void (*intHandler)(void))
284 {
285     switch (comparator)
286     {
287     case COMP_E0_MODULE:
288         Interrupt_registerInterrupt(INT_COMP_E0, intHandler);
289         Interrupt_enableInterrupt(INT_COMP_E0);
290         break;
291     case COMP_E1_MODULE:
292         Interrupt_registerInterrupt(INT_COMP_E1, intHandler);
293         Interrupt_enableInterrupt(INT_COMP_E1);
294         break;
295     default:
296         ASSERT(false);
297     }
298 }
299
300 void COMP_E_unregisterInterrupt(uint32_t comparator)
301 {
302     switch (comparator)
303     {
304     case COMP_E0_MODULE:
305         Interrupt_disableInterrupt(INT_COMP_E0);
306         Interrupt_unregisterInterrupt(INT_COMP_E0);
307         break;
308     case COMP_E1_MODULE:
309         Interrupt_disableInterrupt(INT_COMP_E1);
310         Interrupt_unregisterInterrupt(INT_COMP_E1);
311         break;
312     default:
313         ASSERT(false);
314     }
315 }
316