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 */
41 /* DriverLib Includes */
44 #include <interrupt.h>
47 static volatile uint32_t* const _ctlRegs[32] =
48 { &ADC14->rMCTL0.r, &ADC14->rMCTL1.r, &ADC14->rMCTL2.r, &ADC14->rMCTL3.r,
49 &ADC14->rMCTL4.r, &ADC14->rMCTL5.r, &ADC14->rMCTL6.r, &ADC14->rMCTL7.r,
50 &ADC14->rMCTL8.r, &ADC14->rMCTL9.r, &ADC14->rMCTL10.r,
51 &ADC14->rMCTL11.r, &ADC14->rMCTL12.r, &ADC14->rMCTL13.r,
52 &ADC14->rMCTL14.r, &ADC14->rMCTL15.r, &ADC14->rMCTL16.r,
53 &ADC14->rMCTL17.r, &ADC14->rMCTL18.r, &ADC14->rMCTL19.r,
54 &ADC14->rMCTL20.r, &ADC14->rMCTL21.r, &ADC14->rMCTL22.r,
55 &ADC14->rMCTL23.r, &ADC14->rMCTL24.r, &ADC14->rMCTL25.r,
56 &ADC14->rMCTL26.r, &ADC14->rMCTL27.r, &ADC14->rMCTL28.r,
57 &ADC14->rMCTL29.r, &ADC14->rMCTL30.r, &ADC14->rMCTL31.r };
59 static uint_fast8_t _getIndexForMemRegister(uint32_t reg)
129 return ADC_INVALID_MEM;
134 //*****************************************************************************
137 //! Returns a boolean value that tells if conversion is active/running or is
138 //! not acMSP432 ted.
140 //! Originally a public function, but moved to static. External customers should
141 //! use the ADC14_isBusy function.
143 //! \return true if conversion is active, false otherwise
145 //*****************************************************************************
146 static bool ADCIsConversionRunning(void)
148 return BITBAND_PERI(ADC14->rCTL0.r, ADC14BUSY_OFS);
151 void ADC14_enableModule(void)
153 BITBAND_PERI(ADC14->rCTL0.r, ADC14ON_OFS) = 1;
156 bool ADC14_disableModule(void)
158 if (ADCIsConversionRunning())
161 BITBAND_PERI(ADC14->rCTL0.r, ADC14ON_OFS) = 0;
166 bool ADC14_enableSampleTimer(uint32_t multiSampleConvert)
168 if (ADCIsConversionRunning())
171 BITBAND_PERI(ADC14->rCTL0.r, ADC14SHP_OFS) = 1;
173 if (multiSampleConvert == ADC_MANUAL_ITERATION)
175 BITBAND_PERI(ADC14->rCTL0.r, ADC14MSC_OFS) = 0;
178 BITBAND_PERI(ADC14->rCTL0.r, ADC14MSC_OFS) = 1;
184 bool ADC14_disableSampleTimer(void)
186 if (ADCIsConversionRunning())
189 BITBAND_PERI(ADC14->rCTL0.r, ADC14SHP_OFS) = 0;
194 bool ADC14_initModule(uint32_t clockSource, uint32_t clockPredivider,
195 uint32_t clockDivider, uint32_t internalChannelMask)
198 clockSource == ADC_CLOCKSOURCE_ADCOSC
199 || clockSource == ADC_CLOCKSOURCE_SYSOSC
200 || clockSource == ADC_CLOCKSOURCE_ACLK
201 || clockSource == ADC_CLOCKSOURCE_MCLK
202 || clockSource == ADC_CLOCKSOURCE_SMCLK
203 || clockSource == ADC_CLOCKSOURCE_HSMCLK);
206 clockPredivider == ADC_PREDIVIDER_1
207 || clockPredivider == ADC_PREDIVIDER_4
208 || clockPredivider == ADC_PREDIVIDER_32
209 || clockPredivider == ADC_PREDIVIDER_64);
212 clockDivider == ADC_DIVIDER_1 || clockDivider == ADC_DIVIDER_2
213 || clockDivider == ADC_DIVIDER_3
214 || clockDivider == ADC_DIVIDER_4
215 || clockDivider == ADC_DIVIDER_5
216 || clockDivider == ADC_DIVIDER_6
217 || clockDivider == ADC_DIVIDER_7
218 || clockDivider == ADC_DIVIDER_8);
221 !(internalChannelMask
222 & ~(ADC_MAPINTCH3 | ADC_MAPINTCH2 | ADC_MAPINTCH1
223 | ADC_MAPINTCH0 | ADC_TEMPSENSEMAP | ADC_BATTMAP)));
225 if (ADCIsConversionRunning())
228 ADC14->rCTL0.r = (ADC14->rCTL0.r
229 & ~(ADC14PDIV_M | ADC14DIV_M | ADC14SSEL_M))
230 | clockDivider | clockPredivider | clockSource;
232 ADC14->rCTL1.r = (ADC14->rCTL1.r
233 & ~(ADC_MAPINTCH3 | ADC_MAPINTCH2 | ADC_MAPINTCH1 | ADC_MAPINTCH0
234 | ADC_TEMPSENSEMAP | ADC_BATTMAP)) | internalChannelMask;
239 void ADC14_setResolution(uint32_t resolution)
242 resolution == ADC_8BIT || resolution == ADC_10BIT
243 || resolution == ADC_12BIT || resolution == ADC_14BIT);
245 ADC14->rCTL1.r = (ADC14->rCTL1.r & ~ADC14RES_M) | resolution;
248 uint_fast32_t ADC14_getResolution(void)
250 return ADC14->rCTL1.r & ADC14RES_M;
253 bool ADC14_setSampleHoldTrigger(uint32_t source, bool invertSignal)
257 source == ADC_TRIGGER_ADCSC || source == ADC_TRIGGER_SOURCE1
258 || source == ADC_TRIGGER_SOURCE2
259 || source == ADC_TRIGGER_SOURCE3
260 || source == ADC_TRIGGER_SOURCE4
261 || source == ADC_TRIGGER_SOURCE5
262 || source == ADC_TRIGGER_SOURCE6
263 || source == ADC_TRIGGER_SOURCE7);
265 if (ADCIsConversionRunning())
270 ADC14->rCTL0.r = (ADC14->rCTL0.r
271 & ~(ADC14ISSH | ADC14SHS_M)) | source
275 ADC14->rCTL0.r = (ADC14->rCTL0.r
276 & ~(ADC14ISSH | ADC14SHS_M)) | source;
282 bool ADC14_setSampleHoldTime(uint32_t firstPulseWidth,
283 uint32_t secondPulseWidth)
286 firstPulseWidth == ADC_PULSE_WIDTH_4
287 || firstPulseWidth == ADC_PULSE_WIDTH_8
288 || firstPulseWidth == ADC_PULSE_WIDTH_16
289 || firstPulseWidth == ADC_PULSE_WIDTH_32
290 || firstPulseWidth == ADC_PULSE_WIDTH_64
291 || firstPulseWidth == ADC_PULSE_WIDTH_96
292 || firstPulseWidth == ADC_PULSE_WIDTH_128
293 || firstPulseWidth == ADC_PULSE_WIDTH_192);
296 secondPulseWidth == ADC_PULSE_WIDTH_4
297 || secondPulseWidth == ADC_PULSE_WIDTH_8
298 || secondPulseWidth == ADC_PULSE_WIDTH_16
299 || secondPulseWidth == ADC_PULSE_WIDTH_32
300 || secondPulseWidth == ADC_PULSE_WIDTH_64
301 || secondPulseWidth == ADC_PULSE_WIDTH_96
302 || secondPulseWidth == ADC_PULSE_WIDTH_128
303 || secondPulseWidth == ADC_PULSE_WIDTH_192);
305 if (ADCIsConversionRunning())
308 ADC14->rCTL0.r = (ADC14->rCTL0.r
309 & ~(ADC14SHT0_M | ADC14SHT1_M)) | secondPulseWidth
310 | (firstPulseWidth >> 4);
315 bool ADC14_configureMultiSequenceMode(uint32_t memoryStart, uint32_t memoryEnd,
321 _getIndexForMemRegister(memoryStart) != ADC_INVALID_MEM
322 && _getIndexForMemRegister(memoryEnd) != ADC_INVALID_MEM);
324 if (ADCIsConversionRunning())
327 /* Clearing out any lingering EOS */
328 for (ii = 0; ii < 32; ii++)
330 BITBAND_PERI(*(_ctlRegs[ii]), ADC14EOS_OFS) = 0;
333 /* Setting Start/Stop locations */
335 (*(_ctlRegs[_getIndexForMemRegister(memoryEnd)])),
338 ADC14->rCTL1.r = (ADC14->rCTL1.r & ~(ADC14CSTARTADD_M))
339 | (_getIndexForMemRegister(memoryStart) << 16);
341 /* Setting multiple sample mode */
344 ADC14->rCTL0.r = (ADC14->rCTL0.r & ~(ADC14CONSEQ_M))
348 ADC14->rCTL0.r = (ADC14->rCTL0.r & ~(ADC14CONSEQ_M))
355 bool ADC14_configureSingleSampleMode(uint32_t memoryDestination,
358 ASSERT(_getIndexForMemRegister(memoryDestination) != 32);
360 if (ADCIsConversionRunning())
363 /* Setting the destination register */
364 ADC14->rCTL1.r = (ADC14->rCTL1.r & ~(ADC14CSTARTADD_M))
365 | (_getIndexForMemRegister(memoryDestination) << 16);
367 /* Setting single sample mode */
370 ADC14->rCTL0.r = (ADC14->rCTL0.r & ~(ADC14CONSEQ_M))
374 ADC14->rCTL0.r = (ADC14->rCTL0.r & ~(ADC14CONSEQ_M))
381 bool ADC14_enableConversion(void)
383 if (ADCIsConversionRunning() || !BITBAND_PERI(ADC14->rCTL0.r, ADC14ON_OFS))
386 ADC14->rCTL0.r |= (ADC14ENC);
391 bool ADC14_toggleConversionTrigger(void)
393 if (!BITBAND_PERI(ADC14->rCTL0.r, ADC14ON_OFS))
396 if (BITBAND_PERI(ADC14->rCTL0.r, ADC14SC_OFS))
398 BITBAND_PERI(ADC14->rCTL0.r, ADC14SC_OFS) = 0;
401 BITBAND_PERI(ADC14->rCTL0.r, ADC14SC_OFS) = 1;
407 void ADC14_disableConversion(void)
409 ADC14->rCTL0.r &= ~(ADC14SC | ADC14ENC);
412 bool ADC14_isBusy(void)
414 return BITBAND_PERI(ADC14->rCTL0.r, ADC14BUSY_OFS);
417 bool ADC14_configureConversionMemory(uint32_t memorySelect, uint32_t refSelect,
418 uint32_t channelSelect, bool differntialMode)
420 uint32_t currentReg, ii;
427 if (ADCIsConversionRunning())
430 while (memorySelect != 0)
432 if (!(memorySelect & ii))
438 currentReg = memorySelect & ii;
442 curReg = (uint32_t*) _ctlRegs[_getIndexForMemRegister(currentReg)];
446 (*curReg) = ((*curReg)
447 & ~(ADC14VRSEL_M | ADC14INCH_M
449 | (channelSelect | refSelect | ADC14DIF);
452 (*curReg) = ((*curReg)
453 & ~(ADC14VRSEL_M | ADC14INCH_M
454 | ADC14DIF)) | (channelSelect | refSelect);
462 bool ADC14_enableComparatorWindow(uint32_t memorySelect, uint32_t windowSelect)
464 uint32_t currentReg, ii;
465 uint32_t *curRegPoint;
471 if (ADCIsConversionRunning())
474 while (memorySelect != 0)
476 if (!(memorySelect & ii))
482 currentReg = memorySelect & ii;
487 (uint32_t*) _ctlRegs[_getIndexForMemRegister(currentReg)];
489 if (windowSelect == ADC_COMP_WINDOW0)
491 (*curRegPoint) = ((*curRegPoint)
492 & ~(ADC14WINC | ADC14WINCTH))
494 } else if (windowSelect == ADC_COMP_WINDOW1)
496 (*curRegPoint) |= ADC14WINC | ADC14WINCTH;
504 bool ADC14_disableComparatorWindow(uint32_t memorySelect)
506 uint32_t currentReg, ii;
512 if (ADCIsConversionRunning())
515 while (memorySelect != 0)
517 if (!(memorySelect & ii))
523 currentReg = memorySelect & ii;
527 (*(_ctlRegs[_getIndexForMemRegister(currentReg)])) &=
535 bool ADC14_setComparatorWindowValue(uint32_t window, int16_t low, int16_t high)
537 if (ADCIsConversionRunning())
540 if (window == ADC_COMP_WINDOW0)
542 ADC14->rHI0.r = (high);
543 ADC14->rLO0.r = (low);
545 } else if (window == ADC_COMP_WINDOW1)
547 ADC14->rHI1.r = (high);
548 ADC14->rLO1.r = (low);
558 bool ADC14_setResultFormat(uint32_t resultFormat)
560 if (ADCIsConversionRunning())
563 if (resultFormat == ADC_UNSIGNED_BINARY)
565 BITBAND_PERI(ADC14->rCTL1.r, ADC14DF_OFS) = 0;
566 } else if (resultFormat == ADC_SIGNED_BINARY)
568 BITBAND_PERI(ADC14->rCTL1.r, ADC14DF_OFS) = 1;
577 uint_fast16_t ADC14_getResult(uint32_t memorySelect)
579 return *((uint16_t*) (_ctlRegs[_getIndexForMemRegister(memorySelect)]
583 void ADC14_getMultiSequenceResult(uint16_t* res)
585 uint32_t *startAddr, *curAddr;
588 startAddr = (uint32_t*) _ctlRegs[(ADC14->rCTL1.r & ADC14CSTARTADD_M)
593 for (ii = 0; ii < 32; ii++)
595 res[ii] = *(((uint16_t*) curAddr) + 0x80);
597 if (BITBAND_PERI((*curAddr), ADC14EOS_OFS))
600 if (curAddr == _ctlRegs[31])
601 curAddr = (uint32_t*) _ctlRegs[0];
608 void ADC14_getResultArray(uint32_t memoryStart, uint32_t memoryEnd,
612 uint32_t *firstPoint, *secondPoint;
614 bool foundEnd = false;
617 _getIndexForMemRegister(memoryStart) != ADC_INVALID_MEM
618 && _getIndexForMemRegister(memoryEnd) != ADC_INVALID_MEM);
620 firstPoint = (uint32_t*) _ctlRegs[_getIndexForMemRegister(memoryStart)];
621 secondPoint = (uint32_t*) _ctlRegs[_getIndexForMemRegister(memoryEnd)];
625 if (firstPoint == secondPoint)
630 res[ii] = *(((uint16_t*) firstPoint) + 0x80);
632 if (firstPoint == _ctlRegs[31])
633 firstPoint = (uint32_t*) _ctlRegs[0];
639 bool ADC14_enableReferenceBurst(void)
641 if (ADCIsConversionRunning())
644 BITBAND_PERI(ADC14->rCTL1.r, ADC14REFBURST_OFS) = 1;
649 bool ADC14_disableReferenceBurst(void)
651 if (ADCIsConversionRunning())
654 BITBAND_PERI(ADC14->rCTL1.r, ADC14REFBURST_OFS) = 0;
659 bool ADC14_setPowerMode(uint32_t adcPowerMode)
661 if (ADCIsConversionRunning())
664 switch (adcPowerMode)
666 case ADC_UNRESTRICTED_POWER_MODE:
667 ADC14->rCTL1.r = (ADC14->rCTL1.r & ~(ADC14PWRMD_M))
670 case ADC_ULTRA_LOW_POWER_MODE:
671 ADC14->rCTL1.r = (ADC14->rCTL1.r & ~(ADC14PWRMD_M))
682 void ADC14_enableInterrupt(uint_fast64_t mask)
684 uint32_t stat = mask & 0xFFFFFFFF;
686 ADC14->rIER0.r |= stat;
688 ADC14->rIER1.r |= (stat);
691 void ADC14_disableInterrupt(uint_fast64_t mask)
693 uint32_t stat = mask & 0xFFFFFFFF;
695 ADC14->rIER0.r &= ~stat;
697 ADC14->rIER1.r &= ~(stat);
700 uint_fast64_t ADC14_getInterruptStatus(void)
702 uint_fast64_t status = ADC14->rIFGR1.r;
703 return ((status << 32) | ADC14->rIFGR0.r);
706 uint_fast64_t ADC14_getEnabledInterruptStatus(void)
708 uint_fast64_t stat = ADC14->rIER1.r;
710 return ADC14_getInterruptStatus() & ((stat << 32) | ADC14->rIER0.r);
714 void ADC14_clearInterruptFlag(uint_fast64_t mask)
716 uint32_t stat = mask & 0xFFFFFFFF;
718 ADC14->rCLRIFGR0.r |= stat;
720 ADC14->rCLRIFGR1.r |= (stat);
723 void ADC14_registerInterrupt(void (*intHandler)(void))
726 // Register the interrupt handler, returning an error if an error occurs.
728 Interrupt_registerInterrupt(INT_ADC14, intHandler);
731 // Enable the ADC interrupt.
733 Interrupt_enableInterrupt(INT_ADC14);
736 void ADC14_unregisterInterrupt(void)
739 // Disable the interrupt.
741 Interrupt_disableInterrupt(INT_ADC14);
744 // Unregister the interrupt handler.
746 Interrupt_unregisterInterrupt(INT_ADC14);