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.
38 #include <interrupt.h>
41 static uint16_t __getRegisterSettingForInput(uint32_t input)
86 bool COMP_E_initModule(uint32_t comparator, const COMP_E_Config *config)
88 uint_fast8_t positiveTerminalInput = __getRegisterSettingForInput(
89 config->positiveTerminalInput);
90 uint_fast8_t negativeTerminalInput = __getRegisterSettingForInput(
91 config->negativeTerminalInput);
94 ASSERT(positiveTerminalInput < 0x10); ASSERT(negativeTerminalInput < 0x10);
95 ASSERT(positiveTerminalInput != negativeTerminalInput);
97 config->outputFilterEnableAndDelayLevel
98 <= COMP_E_FILTEROUTPUT_DLYLVL4);
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;
104 // Set the Positive Terminal
105 if (COMP_E_VREF != positiveTerminalInput)
107 // Enable Positive Terminal Input Mux and Set to the appropriate input
108 COMP_E_CMSIS(comparator)->rCTL0.r |= CEIPEN + positiveTerminalInput;
110 // Disable the input buffer
111 COMP_E_CMSIS(comparator)->rCTL3.r |= (1 << positiveTerminalInput);
114 // Reset and Set COMPE Control 2 Register
115 BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL2.r,CERSEL_OFS) = 0;
118 // Set the Negative Terminal
119 if (COMP_E_VREF != negativeTerminalInput)
121 // Enable Negative Terminal Input Mux and Set to the appropriate input
122 COMP_E_CMSIS(comparator)->rCTL0.r |= CEIMEN
123 + (negativeTerminalInput << 8);
125 // Disable the input buffer
126 COMP_E_CMSIS(comparator)->rCTL3.r |= (1 << negativeTerminalInput);
129 // Reset and Set COMPE Control 2 Register
130 BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL2.r, CERSEL_OFS) = 1;
133 // Reset and Set COMPE Control 1 Register
134 COMP_E_CMSIS(comparator)->rCTL1.r = config->powerMode
135 + config->outputFilterEnableAndDelayLevel
136 + config->invertedOutputPolarity;
141 void COMP_E_setReferenceVoltage(uint32_t comparator,
142 uint_fast16_t supplyVoltageReferenceBase,
143 uint_fast16_t lowerLimitSupplyVoltageFractionOf32,
144 uint_fast16_t upperLimitSupplyVoltageFractionOf32)
146 ASSERT(supplyVoltageReferenceBase <= COMP_E_VREFBASE2_5V);
147 ASSERT(upperLimitSupplyVoltageFractionOf32 <= 32);
148 ASSERT(lowerLimitSupplyVoltageFractionOf32 <= 32); ASSERT(
149 upperLimitSupplyVoltageFractionOf32
150 >= lowerLimitSupplyVoltageFractionOf32);
152 BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL1.r, CEMRVS_OFS) = 0;
153 COMP_E_CMSIS(comparator)->rCTL2.r &= CERSEL;
155 // Set Voltage Source(Vcc | Vref, resistor ladder or not)
156 if (COMP_E_REFERENCE_AMPLIFIER_DISABLED == supplyVoltageReferenceBase)
158 COMP_E_CMSIS(comparator)->rCTL2.r |= CERS_1;
159 } else if (lowerLimitSupplyVoltageFractionOf32 == 32)
161 COMP_E_CMSIS(comparator)->rCTL2.r |= CERS_3;
164 COMP_E_CMSIS(comparator)->rCTL2.r |= CERS_2;
167 // Set COMPE Control 2 Register
168 COMP_E_CMSIS(comparator)->rCTL2.r |= supplyVoltageReferenceBase
169 + ((upperLimitSupplyVoltageFractionOf32 - 1) << 8)
170 + (lowerLimitSupplyVoltageFractionOf32 - 1);
173 void COMP_E_setReferenceAccuracy(uint32_t comparator,
174 uint_fast16_t referenceAccuracy)
177 (referenceAccuracy == COMP_E_ACCURACY_STATIC)
178 || (referenceAccuracy == COMP_E_ACCURACY_CLOCKED));
180 if (referenceAccuracy)
181 BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL2.r, CEREFACC_OFS) = 1;
183 BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL2.r, CEREFACC_OFS) = 0;
187 void COMP_E_setPowerMode(uint32_t comparator, uint_fast16_t powerMode)
189 COMP_E_CMSIS(comparator)->rCTL1.r = (COMP_E_CMSIS(comparator)->rCTL1.r
190 & ~(CEPWRMD_M)) | powerMode;
193 void COMP_E_enableModule(uint32_t comparator)
195 BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL1.r, CEON_OFS) = 1;
198 void COMP_E_disableModule(uint32_t comparator)
200 BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL1.r, CEON_OFS) = 0;
203 void COMP_E_shortInputs(uint32_t comparator)
205 BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL1.r, CESHORT_OFS) = 1;
208 void COMP_E_unshortInputs(uint32_t comparator)
210 BITBAND_PERI(COMP_E_CMSIS(comparator)->rCTL1.r, CESHORT_OFS) = 0;
213 void COMP_E_disableInputBuffer(uint32_t comparator, uint_fast16_t inputPort)
215 ASSERT(inputPort <= COMP_E_INPUT15);
217 COMP_E_CMSIS(comparator)->rCTL3.r |= (inputPort);
220 void COMP_E_enableInputBuffer(uint32_t comparator, uint_fast16_t inputPort)
222 ASSERT(inputPort <= COMP_E_INPUT15);
224 COMP_E_CMSIS(comparator)->rCTL3.r &= ~(inputPort);
227 void COMP_E_swapIO(uint32_t comparator)
229 COMP_E_CMSIS(comparator)->rCTL1.r ^= CEEX; // Toggle CEEX bit
232 uint8_t COMP_E_outputValue(uint32_t comparator)
234 return COMP_E_CMSIS(comparator)->rCTL1.r & CEOUT;
237 void COMP_E_enableInterrupt(uint32_t comparator, uint_fast16_t mask)
239 // Set the Interrupt enable bit
240 COMP_E_CMSIS(comparator)->rINT.r |= mask;
243 uint_fast16_t COMP_E_getEnabledInterruptStatus(uint32_t comparator)
245 return COMP_E_getInterruptStatus(comparator) &
246 COMP_E_CMSIS(comparator)->rINT.r;
249 void COMP_E_disableInterrupt(uint32_t comparator, uint_fast16_t mask)
251 COMP_E_CMSIS(comparator)->rINT.r &= ~(mask);
254 void COMP_E_clearInterruptFlag(uint32_t comparator, uint_fast16_t mask)
256 COMP_E_CMSIS(comparator)->rINT.r &= ~(mask);
259 uint_fast16_t COMP_E_getInterruptStatus(uint32_t comparator)
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));
266 void COMP_E_setInterruptEdgeDirection(uint32_t comparator,
267 uint_fast8_t edgeDirection)
269 ASSERT(edgeDirection <= COMP_E_RISINGEDGE);
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;
278 void COMP_E_toggleInterruptEdgeDirection(uint32_t comparator)
280 COMP_E_CMSIS(comparator)->rCTL1.r ^= CEIES;
283 void COMP_E_registerInterrupt(uint32_t comparator, void (*intHandler)(void))
288 Interrupt_registerInterrupt(INT_COMP_E0, intHandler);
289 Interrupt_enableInterrupt(INT_COMP_E0);
292 Interrupt_registerInterrupt(INT_COMP_E1, intHandler);
293 Interrupt_enableInterrupt(INT_COMP_E1);
300 void COMP_E_unregisterInterrupt(uint32_t comparator)
305 Interrupt_disableInterrupt(INT_COMP_E0);
306 Interrupt_unregisterInterrupt(INT_COMP_E0);
309 Interrupt_disableInterrupt(INT_COMP_E1);
310 Interrupt_unregisterInterrupt(INT_COMP_E1);