2 * -------------------------------------------
3 * MSP432 DriverLib - v3_10_00_09
4 * -------------------------------------------
6 * --COPYRIGHT--,BSD,BSD
7 * Copyright (c) 2014, 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 */
40 /* DriverLib Includes */
44 #include <interrupt.h>
47 static uint32_t hfxtFreq;
48 static uint32_t lfxtFreq;
52 bool _CSIsClockDividerValid(uint8_t divider)
54 return ((divider == CS_CLOCK_DIVIDER_1) || (divider == CS_CLOCK_DIVIDER_2)
55 || (divider == CS_CLOCK_DIVIDER_4) || (divider == CS_CLOCK_DIVIDER_8)
56 || (divider == CS_CLOCK_DIVIDER_16) || (divider == CS_CLOCK_DIVIDER_32)
57 || (divider == CS_CLOCK_DIVIDER_64) || (divider == CS_CLOCK_DIVIDER_128));
62 static uint32_t _CSGetHFXTFrequency()
64 if (hfxtFreq >= CS_1MHZ && hfxtFreq <= CS_4MHZ)
65 return CS_CTL2_HFXTFREQ_0;
66 else if (hfxtFreq > CS_4MHZ && hfxtFreq <= CS_8MHZ)
67 return CS_CTL2_HFXTFREQ_1;
68 else if (hfxtFreq > CS_8MHZ && hfxtFreq <= CS_16MHZ)
69 return CS_CTL2_HFXTFREQ_2;
70 else if (hfxtFreq > CS_16MHZ && hfxtFreq <= CS_24MHZ)
71 return CS_CTL2_HFXTFREQ_3;
72 else if (hfxtFreq > CS_24MHZ && hfxtFreq <= CS_32MHZ)
73 return CS_CTL2_HFXTFREQ_4;
74 else if (hfxtFreq > CS_32MHZ && hfxtFreq <= CS_40MHZ)
75 return CS_CTL2_HFXTFREQ_5;
76 else if (hfxtFreq > CS_40MHZ && hfxtFreq <= CS_48MHZ)
77 return CS_CTL2_HFXTFREQ_5;
86 static uint32_t _CSGetDividerValue(uint32_t wDivider)
90 case CS_CLOCK_DIVIDER_1:
92 case CS_CLOCK_DIVIDER_2:
94 case CS_CLOCK_DIVIDER_4:
96 case CS_CLOCK_DIVIDER_8:
98 case CS_CLOCK_DIVIDER_16:
100 case CS_CLOCK_DIVIDER_32:
102 case CS_CLOCK_DIVIDER_64:
104 case CS_CLOCK_DIVIDER_128:
112 static uint32_t _CSComputeCLKFrequency(uint32_t wClockSource, uint32_t wDivider)
114 uint_fast8_t bDivider;
116 bDivider = _CSGetDividerValue(wDivider);
118 switch (wClockSource)
120 case CS_LFXTCLK_SELECT:
122 if (BITBAND_PERI(CS->IFG, CS_IFG_LFXTIFG_OFS))
124 CS_clearInterruptFlag(CS_LFXT_FAULT);
126 if (BITBAND_PERI(CS->IFG, CS_IFG_LFXTIFG_OFS))
128 if (BITBAND_PERI(CS->CLKEN, CS_CLKEN_REFOFSEL_OFS))
129 return (128000 / bDivider);
131 return (32768 / bDivider);
134 return lfxtFreq / bDivider;
136 case CS_HFXTCLK_SELECT:
138 if (BITBAND_PERI(CS->IFG, CS_IFG_HFXTIFG_OFS))
140 CS_clearInterruptFlag(CS_HFXT_FAULT);
142 if (BITBAND_PERI(CS->IFG, CS_IFG_HFXTIFG_OFS))
144 if (BITBAND_PERI(CS->CLKEN, CS_CLKEN_REFOFSEL_OFS))
145 return (128000 / bDivider);
147 return (32768 / bDivider);
150 return hfxtFreq / bDivider;
152 case CS_VLOCLK_SELECT:
153 return CS_VLOCLK_FREQUENCY / bDivider;
154 case CS_REFOCLK_SELECT:
156 if (BITBAND_PERI(CS->CLKEN, CS_CLKEN_REFOFSEL_OFS))
157 return (128000 / bDivider);
159 return (32768 / bDivider);
161 case CS_DCOCLK_SELECT:
162 return (CS_getDCOFrequency() / bDivider);
163 case CS_MODOSC_SELECT:
164 return CS_MODCLK_FREQUENCY / bDivider;
171 //******************************************************************************
172 // Internal function for getting DCO nominal frequency
173 //******************************************************************************
174 static uint32_t _CSGetDOCFrequency(void)
178 switch (CS->CTL0 & CS_CTL0_DCORSEL_MASK)
180 case CS_CTL0_DCORSEL_0:
183 case CS_CTL0_DCORSEL_1:
186 case CS_CTL0_DCORSEL_2:
189 case CS_CTL0_DCORSEL_3:
192 case CS_CTL0_DCORSEL_4:
195 case CS_CTL0_DCORSEL_5:
205 void CS_setExternalClockSourceFrequency(uint32_t lfxt_XT_CLK_frequency,
206 uint32_t hfxt_XT_CLK_frequency)
208 hfxtFreq = hfxt_XT_CLK_frequency;
209 lfxtFreq = lfxt_XT_CLK_frequency;
212 void CS_initClockSignal(uint32_t selectedClockSignal, uint32_t clockSource,
213 uint32_t clockSourceDivider)
215 ASSERT(_CSIsClockDividerValid(clockSourceDivider));
217 /* Unlocking the CS Module */
220 switch (selectedClockSignal)
224 /* Making sure that the clock signal for ACLK isn't set to anything
228 (selectedClockSignal != CS_DCOCLK_SELECT)
229 && (selectedClockSignal != CS_MODOSC_SELECT)
230 && (selectedClockSignal != CS_HFXTCLK_SELECT));
232 /* Waiting for the clock source ready bit to be valid before
234 while (!BITBAND_PERI(CS->STAT, CS_STAT_ACLK_READY_OFS))
237 /* Setting the divider and source */
238 CS->CTL1 = ((clockSourceDivider >> CS_ACLK_DIV_BITPOS)
239 | (clockSource << CS_ACLK_SRC_BITPOS))
240 | (CS->CTL1 & ~(CS_CTL1_SELA_MASK | CS_CTL1_DIVA_MASK));
242 /* Waiting for ACLK to be ready again */
243 while (!BITBAND_PERI(CS->STAT, CS_STAT_ACLK_READY_OFS))
251 /* Waiting for the clock source ready bit to be valid before
253 while (!BITBAND_PERI(CS->STAT, CS_STAT_MCLK_READY_OFS))
256 CS->CTL1 = ((clockSourceDivider >> CS_MCLK_DIV_BITPOS)
257 | (clockSource << CS_MCLK_SRC_BITPOS))
258 | (CS->CTL1 & ~(CS_CTL1_SELM_MASK | CS_CTL1_DIVM_MASK));
260 /* Waiting for MCLK to be ready */
261 while (!BITBAND_PERI(CS->STAT, CS_STAT_MCLK_READY_OFS))
268 /* Waiting for the clock source ready bit to be valid before
270 while (!BITBAND_PERI(CS->STAT, CS_STAT_SMCLK_READY_OFS))
273 CS->CTL1 = ((clockSourceDivider >> CS_SMCLK_DIV_BITPOS)
274 | (clockSource << CS_HSMCLK_SRC_BITPOS))
275 | (CS->CTL1 & ~(CS_CTL1_DIVS_MASK | CS_CTL1_SELS_MASK));
277 /* Waiting for SMCLK to be ready */
278 while (!BITBAND_PERI(CS->STAT, CS_STAT_SMCLK_READY_OFS))
285 /* Waiting for the clock source ready bit to be valid before
287 while (!BITBAND_PERI(CS->STAT, CS_STAT_HSMCLK_READY_OFS))
290 CS->CTL1 = ((clockSourceDivider >> CS_HSMCLK_DIV_BITPOS)
291 | (clockSource << CS_HSMCLK_SRC_BITPOS))
292 | (CS->CTL1 & ~(CS_CTL1_DIVHS_MASK | CS_CTL1_SELS_MASK));
294 /* Waiting for HSMCLK to be ready */
295 while (!BITBAND_PERI(CS->STAT, CS_STAT_HSMCLK_READY_OFS))
303 /* Waiting for the clock source ready bit to be valid before
305 while (!BITBAND_PERI(CS->STAT, CS_STAT_BCLK_READY_OFS))
308 /* Setting the clock source and then returning
309 * (cannot divide CLK)
311 if (clockSource == CS_LFXTCLK_SELECT)
312 BITBAND_PERI(CS->CTL1, CS_CTL1_SELB_OFS) = 0;
313 else if (clockSource == CS_REFOCLK_SELECT)
314 BITBAND_PERI(CS->CTL1, CS_CTL1_SELB_OFS) = 1;
318 /* Waiting for BCLK to be ready */
319 while (!BITBAND_PERI(CS->STAT, CS_STAT_BCLK_READY_OFS))
326 /* Should never get here */
331 /* Locking the module */
332 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
335 bool CS_startHFXT(bool bypassMode)
337 return CS_startHFXTWithTimeout(bypassMode, 0);
340 bool CS_startHFXTWithTimeout(bool bypassMode, uint32_t timeout)
342 uint32_t wHFFreqRange;
343 uint_fast8_t bNMIStatus;
346 /* Unlocking the CS Module */
349 /* Saving status and temporarily disabling NMIs for UCS faults */
350 bNMIStatus = SysCtl_getNMISourceStatus() & SYSCTL_CS_SRC;
351 SysCtl_disableNMISource(SYSCTL_CS_SRC);
353 /* Determining which frequency range to use */
354 wHFFreqRange = _CSGetHFXTFrequency();
355 boolTimeout = (timeout == 0) ? false : true;
357 /* Setting to maximum drive strength */
358 BITBAND_PERI(CS->CTL2, CS_CTL2_HFXTDRIVE_OFS) = 1;
359 CS->CTL2 = (CS->CTL2 & (~CS_CTL2_HFXTFREQ_MASK)) | (wHFFreqRange);
363 BITBAND_PERI(CS->CTL2, CS_CTL2_HFXTBYPASS_OFS) = 1;
366 BITBAND_PERI(CS->CTL2, CS_CTL2_HFXTBYPASS_OFS) = 0;
369 /* Starting and Waiting for frequency stabilization */
370 BITBAND_PERI(CS->CTL2, CS_CTL2_HFXT_EN_OFS) = 1;
371 while (BITBAND_PERI(CS->IFG, CS_IFG_HFXTIFG_OFS))
373 if (boolTimeout && ((--timeout) == 0))
376 BITBAND_PERI(CS->CLRIFG,CS_CLRIFG_CLR_HFXTIFG_OFS) = 1;
379 /* Setting the drive strength */
382 if (wHFFreqRange != CS_CTL2_HFXTFREQ_0)
383 BITBAND_PERI(CS->CTL2, CS_CTL2_HFXTDRIVE_OFS) = 1;
385 BITBAND_PERI(CS->CTL2, CS_CTL2_HFXTDRIVE_OFS) = 0;
388 /* Locking the module */
389 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
391 /* Enabling the NMI state */
392 SysCtl_enableNMISource(bNMIStatus);
394 if(boolTimeout && timeout == 0)
400 bool CS_startLFXT(uint32_t xtDrive)
402 return CS_startLFXTWithTimeout(xtDrive, 0);
405 bool CS_startLFXTWithTimeout(uint32_t xtDrive, uint32_t timeout)
408 bool boolBypassMode, boolTimeout;
410 ASSERT(lfxtFreq != 0)
412 (xtDrive == CS_LFXT_DRIVE0) || (xtDrive == CS_LFXT_DRIVE1)
413 || (xtDrive == CS_LFXT_DRIVE2)
414 || (xtDrive == CS_LFXT_DRIVE3)
415 || (xtDrive == CS_LFXT_BYPASS));
417 /* Unlocking the CS Module */
420 /* Saving status and temporarily disabling NMIs for UCS faults */
421 bNMIStatus = SysCtl_getNMISourceStatus() & SYSCTL_CS_SRC;
422 SysCtl_disableNMISource(SYSCTL_CS_SRC);
423 boolBypassMode = (xtDrive == CS_LFXT_BYPASS) ? true : false;
424 boolTimeout = (timeout == 0) ? false : true;
426 /* Setting to maximum drive strength */
429 BITBAND_PERI(CS->CTL2, CS_CTL2_LFXTBYPASS_OFS) = 1;
432 CS->CTL2 |= (CS_LFXT_DRIVE3);
433 BITBAND_PERI(CS->CTL2, CS_CTL2_LFXTBYPASS_OFS) = 0;
436 /* Waiting for frequency stabilization */
437 BITBAND_PERI(CS->CTL2, CS_CTL2_LFXT_EN_OFS) = 1;
439 while (BITBAND_PERI(CS->IFG, CS_IFG_LFXTIFG_OFS))
441 if (boolTimeout && ((--timeout) == 0))
444 BITBAND_PERI(CS->CLRIFG,CS_CLRIFG_CLR_LFXTIFG_OFS) = 1;
447 /* Setting the drive strength */
450 CS->CTL2 = ((CS->CTL2 & ~CS_LFXT_DRIVE3) | xtDrive);
453 /* Locking the module */
454 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
456 /* Enabling the NMI state */
457 SysCtl_enableNMISource(bNMIStatus);
459 if(boolTimeout && timeout == 0)
465 void CS_enableClockRequest(uint32_t selectClock)
468 selectClock == CS_ACLK || selectClock == CS_HSMCLK
469 || selectClock == CS_SMCLK || selectClock == CS_MCLK);
471 /* Unlocking the module */
474 CS->CLKEN |= selectClock;
476 /* Locking the module */
477 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
480 void CS_disableClockRequest(uint32_t selectClock)
483 selectClock == CS_ACLK || selectClock == CS_HSMCLK
484 || selectClock == CS_SMCLK || selectClock == CS_MCLK);
486 /* Unlocking the module */
489 CS->CLKEN &= ~selectClock;
491 /* Locking the module */
492 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
495 void CS_setReferenceOscillatorFrequency(uint8_t referenceFrequency)
498 referenceFrequency == CS_REFO_32KHZ
499 || referenceFrequency == CS_REFO_128KHZ);
501 /* Unlocking the module */
504 BITBAND_PERI(CS->CLKEN, CS_CLKEN_REFOFSEL_OFS) = referenceFrequency;
506 /* Locking the module */
507 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
510 void CS_enableDCOExternalResistor(void)
512 /* Unlocking the module */
515 BITBAND_PERI(CS->CTL0,CS_CTL0_DCORES_OFS) = 1;
517 /* Locking the module */
518 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
521 void CS_setDCOExternalResistorCalibration(uint_fast8_t calData,
522 uint_fast8_t freqRange)
524 uint_fast8_t rselVal;
526 /* Unlocking the module */
529 rselVal = (CS->CTL0 | CS_CTL0_DCORSEL_MASK)>>CS_CTL0_DCORSEL_OFS;
531 CS->CTL0 &= ~CS_CTL0_DCORSEL_MASK;
533 if( (freqRange == CS_OVER32MHZ) && ( TLV->HWREV > DEVICE_PG1_1))
535 CS->DCOERCAL1 &= ~CS_DCOERCAL1_DCO_FCAL_RSEL5_MASK;
536 CS->DCOERCAL1 |= (calData);
540 CS->DCOERCAL0 &= ~CS_DCOERCAL0_DCO_FCAL_RSEL04_MASK;
541 CS->DCOERCAL0 |= (calData)<<CS_DCOERCAL0_DCO_FCAL_RSEL04_OFS;
544 CS->CTL0 |= (rselVal)<<CS_CTL0_DCORSEL_OFS;
546 /* Locking the module */
547 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
551 void CS_disableDCOExternalResistor(void)
553 /* Unlocking the module */
556 BITBAND_PERI(CS->CTL0,CS_CTL0_DCORES_OFS) = 0;
558 /* Locking the module */
559 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
562 void CS_setDCOCenteredFrequency(uint32_t dcoFreq)
565 dcoFreq == CS_DCO_FREQUENCY_1_5 || dcoFreq == CS_DCO_FREQUENCY_3
566 || dcoFreq == CS_DCO_FREQUENCY_6
567 || dcoFreq == CS_DCO_FREQUENCY_12
568 || dcoFreq == CS_DCO_FREQUENCY_24
569 || dcoFreq == CS_DCO_FREQUENCY_48);
571 /* Unlocking the CS Module */
574 /* Resetting Tuning Parameters and Setting the frequency */
575 CS->CTL0 = ((CS->CTL0 & ~CS_CTL0_DCORSEL_MASK) | dcoFreq);
577 /* Locking the CS Module */
578 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
581 void CS_tuneDCOFrequency(int16_t tuneParameter)
585 uint16_t dcoTuneMask = 0x1FFF;
586 uint16_t dcoTuneSigned = 0x1000;
588 if (TLV->HWREV > DEVICE_PG1_1) {
590 dcoTuneSigned = 0x200;
593 if (tuneParameter < 0)
595 CS->CTL0 = ((CS->CTL0 & ~dcoTuneMask) | (tuneParameter
596 & dcoTuneMask) | dcoTuneSigned);
600 CS->CTL0 = ((CS->CTL0 & ~dcoTuneMask) | (tuneParameter
604 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
607 uint32_t CS_getDCOFrequency(void)
611 uint32_t centeredFreq;
613 uint_fast8_t tlvLength;
614 SysCtl_CSCalTLV_Info *csInfo;
617 centeredFreq = _CSGetDOCFrequency();
619 /* Parsing the TLV and getting the maximum erase pulses */
620 SysCtl_getTLVInfo(TLV_TAG_CS, 0, &tlvLength, (uint32_t**)&csInfo);
627 /* Checking to see if we need to do signed conversion */
628 if ( TLV->HWREV > DEVICE_PG1_1)
630 dcoTune = CS->CTL0 & 0x3FF;
633 dcoTune = dcoTune | 0xFE00;
638 dcoTune = CS->CTL0 & 0x1FFF;
639 if (dcoTune & 0x1000)
641 dcoTune = dcoTune | 0xF000;
646 return (uint32_t) centeredFreq;
649 if ((centeredFreq == 48000000) && ( TLV->HWREV > DEVICE_PG1_1))
651 /* External Resistor */
652 if (BITBAND_PERI(CS->CTL0, CS_CTL0_DCORES_OFS))
654 dcoConst = *((float *) &csInfo->rDCOER_CONSTK_RSEL5);
655 calVal = csInfo->rDCOER_FCAL_RSEL5;
657 /* Internal Resistor */
660 dcoConst = *((float *) &csInfo->rDCOIR_CONSTK_RSEL5);
661 calVal = csInfo->rDCOIR_FCAL_RSEL5;
667 /* External Resistor */
668 if (BITBAND_PERI(CS->CTL0, CS_CTL0_DCORES_OFS))
670 dcoConst = *((float *) &csInfo->rDCOER_CONSTK_RSEL04);
671 calVal = csInfo->rDCOER_FCAL_RSEL04;
673 /* Internal Resistor */
676 dcoConst = *((float *) &csInfo->rDCOIR_CONSTK_RSEL04);
677 calVal = csInfo->rDCOIR_FCAL_RSEL04;
681 if( TLV->HWREV > DEVICE_PG1_1 )
683 retVal = (uint32_t) (centeredFreq)
684 / (1 - ((dcoConst * dcoTune)
685 / ((1 + dcoConst * (768 - calVal)))));
689 retVal = (uint32_t) (centeredFreq)
690 / (1 - ((dcoConst * dcoTune)
691 / (8 * (1 + dcoConst * (768 - calVal)))));
696 void CS_setDCOFrequency(uint32_t dcoFrequency)
698 int32_t nomFreq, calVal, dcoSigned;
702 dcoSigned = (int32_t) dcoFrequency;
703 uint_fast8_t tlvLength;
704 SysCtl_CSCalTLV_Info *csInfo;
706 if (dcoFrequency < 2000000)
709 CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_1_5);
710 } else if (dcoFrequency < 4000000)
713 CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_3);
714 } else if (dcoFrequency < 8000000)
717 CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_6);
718 } else if (dcoFrequency < 16000000)
721 CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12);
722 } else if (dcoFrequency < 32000000)
725 CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_24);
726 } else if (dcoFrequency < 640000001)
729 CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_48);
737 /* Parsing the TLV and getting the maximum erase pulses */
738 SysCtl_getTLVInfo(TLV_TAG_CS, 0, &tlvLength, (uint32_t**)&csInfo);
740 if(dcoFrequency == nomFreq || tlvLength == 0)
742 CS_tuneDCOFrequency(0);
746 if ((rsel5) && ( TLV->HWREV > DEVICE_PG1_1))
748 /* External Resistor*/
749 if (BITBAND_PERI(CS->CTL0, CS_CTL0_DCORES_OFS))
751 dcoConst = *((float *) &csInfo->rDCOER_CONSTK_RSEL5);
752 calVal = csInfo->rDCOER_FCAL_RSEL5;
754 /* Internal Resistor */
757 dcoConst = *((float *) &csInfo->rDCOIR_CONSTK_RSEL5);
758 calVal = csInfo->rDCOIR_FCAL_RSEL5;
764 /* External Resistor */
765 if (BITBAND_PERI(CS->CTL0, CS_CTL0_DCORES_OFS))
767 dcoConst = *((float *) &csInfo->rDCOER_CONSTK_RSEL04);
768 calVal = csInfo->rDCOER_FCAL_RSEL04;
770 /* Internal Resistor */
773 dcoConst = *((float *) &csInfo->rDCOIR_CONSTK_RSEL04);
774 calVal = csInfo->rDCOIR_FCAL_RSEL04;
778 if ( TLV->HWREV > DEVICE_PG1_1)
779 dcoTune = (int16_t) (((dcoSigned - nomFreq)
780 * (1.0f + dcoConst * (768.0f - calVal)))
781 / (dcoSigned * dcoConst));
783 dcoTune = (int16_t) (((dcoSigned - nomFreq)
784 * (1.0f + dcoConst * (768.0f - calVal)) * 8.0f)
785 / (dcoSigned * dcoConst));
787 CS_tuneDCOFrequency(dcoTune);
791 uint32_t CS_getBCLK(void)
793 if (BITBAND_PERI(CS->CTL1, CS_CTL1_SELB_OFS))
794 return _CSComputeCLKFrequency(CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1);
796 return _CSComputeCLKFrequency(CS_LFXTCLK_SELECT, CS_CLOCK_DIVIDER_1);
799 uint32_t CS_getHSMCLK(void)
801 uint32_t wSource, wDivider;
803 wSource = (CS->CTL1 & CS_CTL1_SELS_MASK) >> CS_HSMCLK_SRC_BITPOS;
804 wDivider = ((CS->CTL1 & CS_CTL1_DIVHS_MASK) << CS_HSMCLK_DIV_BITPOS);
806 return _CSComputeCLKFrequency(wSource, wDivider);
809 uint32_t CS_getACLK(void)
811 uint32_t wSource, wDivider;
813 wSource = (CS->CTL1 & CS_CTL1_SELA_MASK) >> CS_ACLK_SRC_BITPOS;
814 wDivider = ((CS->CTL1 & CS_CTL1_DIVA_MASK) << CS_ACLK_DIV_BITPOS);
816 return _CSComputeCLKFrequency(wSource, wDivider);
819 uint32_t CS_getSMCLK(void)
821 uint32_t wDivider, wSource;
823 wSource = (CS->CTL1 & CS_CTL1_SELS_MASK) >> CS_HSMCLK_SRC_BITPOS;
824 wDivider = ((CS->CTL1 & CS_CTL1_DIVS_MASK));
826 return _CSComputeCLKFrequency(wSource, wDivider);
830 uint32_t CS_getMCLK(void)
832 uint32_t wSource, wDivider;
834 wSource = (CS->CTL1 & CS_CTL1_SELM_MASK) << CS_MCLK_SRC_BITPOS;
835 wDivider = ((CS->CTL1 & CS_CTL1_DIVM_MASK) << CS_MCLK_DIV_BITPOS);
837 return _CSComputeCLKFrequency(wSource, wDivider);
840 void CS_enableFaultCounter(uint_fast8_t counterSelect)
842 ASSERT(counterSelect == CS_HFXT_FAULT_COUNTER ||
843 counterSelect == CS_HFXT_FAULT_COUNTER);
845 /* Unlocking the module */
848 if (counterSelect == CS_HFXT_FAULT_COUNTER)
850 BITBAND_PERI(CS->CTL3, CS_CTL3_FCNTHF_EN_OFS) = 1;
853 BITBAND_PERI(CS->CTL3, CS_CTL3_FCNTLF_EN_OFS) = 1;
856 /* Locking the module */
857 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
860 void CS_disableFaultCounter(uint_fast8_t counterSelect)
862 ASSERT(counterSelect == CS_HFXT_FAULT_COUNTER ||
863 counterSelect == CS_HFXT_FAULT_COUNTER);
865 /* Unlocking the module */
868 if (counterSelect == CS_HFXT_FAULT_COUNTER)
870 BITBAND_PERI(CS->CTL3, CS_CTL3_FCNTHF_EN_OFS) = 0;
873 BITBAND_PERI(CS->CTL3, CS_CTL3_FCNTLF_EN_OFS) = 0;
876 /* Locking the module */
877 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
880 void CS_resetFaultCounter(uint_fast8_t counterSelect)
882 ASSERT(counterSelect == CS_HFXT_FAULT_COUNTER ||
883 counterSelect == CS_HFXT_FAULT_COUNTER);
885 /* Unlocking the module */
888 if (counterSelect == CS_HFXT_FAULT_COUNTER)
890 BITBAND_PERI(CS->CTL3, CS_CTL3_RFCNTHF_OFS) = 1;
893 BITBAND_PERI(CS->CTL3, CS_CTL3_RFCNTLF_OFS) = 1;
896 /* Locking the module */
897 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
900 void CS_startFaultCounter(uint_fast8_t counterSelect, uint_fast8_t countValue)
902 ASSERT(counterSelect == CS_HFXT_FAULT_COUNTER ||
903 counterSelect == CS_HFXT_FAULT_COUNTER);
905 ASSERT(countValue == CS_FAULT_COUNTER_4096_CYCLES ||
906 countValue == CS_FAULT_COUNTER_8192_CYCLES ||
907 countValue == CS_FAULT_COUNTER_16384_CYCLES ||
908 countValue == CS_FAULT_COUNTER_32768_CYCLES);
910 /* Unlocking the module */
913 if (counterSelect == CS_HFXT_FAULT_COUNTER)
915 CS->CTL3 = ((CS->CTL3 & ~CS_CTL3_FCNTHF_MASK) | (countValue << 4));
918 CS->CTL3 = ((CS->CTL3 & ~CS_CTL3_FCNTLF_MASK) | (countValue));
921 /* Locking the module */
922 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
925 void CS_enableInterrupt(uint32_t flags)
927 /* Unlocking the module */
932 /* Locking the module */
933 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
936 void CS_disableInterrupt(uint32_t flags)
938 /* Unlocking the module */
943 /* Locking the module */
944 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
947 uint32_t CS_getInterruptStatus(void)
952 uint32_t CS_getEnabledInterruptStatus(void)
954 return CS_getInterruptStatus() & CS->IE;
957 void CS_clearInterruptFlag(uint32_t flags)
959 /* Unlocking the module */
964 /* Locking the module */
965 BITBAND_PERI(CS->KEY, CS_KEY_KEY_OFS) = 1;
968 void CS_registerInterrupt(void (*intHandler)(void))
971 // Register the interrupt handler, returning an error if an error occurs.
973 Interrupt_registerInterrupt(INT_CS, intHandler);
976 // Enable the system control interrupt.
978 Interrupt_enableInterrupt(INT_CS);
981 void CS_unregisterInterrupt(void)
984 // Disable the interrupt.
986 Interrupt_disableInterrupt(INT_CS);
989 // Unregister the interrupt handler.
991 Interrupt_unregisterInterrupt(INT_CS);