-
Notifications
You must be signed in to change notification settings - Fork 0
/
clocks.c
120 lines (108 loc) · 4.8 KB
/
clocks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// ------------------------------------------------------------------------------
//
// Description: This file contains the Clock Initialization
//
// Jim Carlson
// Jan 2016
// Built with IAR Embedded Workbench Version: V7.12.1
// ------------------------------------------------------------------------------
#include "functions.h"
#include "msp430.h"
#include "macros.h"
// Function Prototypes
void Init_Clocks(void);
void Software_Trim(void);
void Init_Clocks(void){
// -----------------------------------------------------------------------------
// Clock Configurtaions
// This is the clock initialization for the program.
// Initial clock configuration, runs immediately after port configuration.
// Disables 1ms watchdog timer,
// Configure MCLK for 8MHz and XT1 sourcing ACLK and FLLREF.
//
// Description: Configure ACLK = 32768Hz,
// MCLK = DCO + XT1CLK REF = 8MHz,
// SMCLK = MCLK/2 = 4MHz.
// Toggle LED to indicate that the program is running.
//
// -----------------------------------------------------------------------------
WDTCTL = WDTPW | WDTHOLD; // Disable watchdog
do{
CSCTL7 &= ~XT1OFFG; // Clear XT1 fault flag
CSCTL7 &= ~DCOFFG; // Clear DCO fault flag
SFRIFG1 &= ~OFIFG;
} while (SFRIFG1 & OFIFG); // Test oscillator fault flag
__bis_SR_register(SCG0); // disable FLL
CSCTL1 = DCOFTRIMEN_1;
CSCTL1 |= DCOFTRIM0;
CSCTL1 |= DCOFTRIM1; // DCOFTRIM=3
CSCTL1 |= DCORSEL_3; // DCO Range = 8MHz
CSCTL2 = FLLD_0 + 243; // DCODIV = 8MHz
CSCTL3 |= SELREF__XT1CLK; // Set XT1CLK as FLL reference source
__delay_cycles(DELAY_CYCLES_THREE);
__bic_SR_register(SCG0); // enable FLL
Software_Trim(); // Software Trim to get the best DCOFTRIM value
CSCTL4 = SELA__XT1CLK; // Set ACLK = XT1CLK = 32768Hz
CSCTL4 |= SELMS__DCOCLKDIV;// DCOCLK = MCLK and SMCLK source
CSCTL5 |= DIVM_0; // MCLK = DCOCLK = 8MHZ,
CSCTL5 |= DIVS_0; // SMCLK = DCOCLK = 8MHz
PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode
// to activate previously configured port settings
}
void Software_Trim(void){
// --COPYRIGHT--,BSD_EX
// Copyright (c) 2014, Texas Instruments Incorporated
// All rights reserved.
unsigned int oldDcoTap = ONES_WORD;
unsigned int newDcoTap = ONES_WORD;
unsigned int newDcoDelta = ONES_WORD;
unsigned int bestDcoDelta = ONES_WORD;
unsigned int csCtl0Copy = RESET_STATE;
unsigned int csCtl1Copy = RESET_STATE;
unsigned int csCtl0Read = RESET_STATE;
unsigned int csCtl1Read = RESET_STATE;
unsigned int dcoFreqTrim = 3;
unsigned char endLoop = RESET_STATE;
do{
CSCTL0 = 0x100; // DCO Tap = 256
do{
CSCTL7 &= ~DCOFFG; // Clear DCO fault flag
}while (CSCTL7 & DCOFFG); // Test DCO fault flag
// Wait FLL lock status (FLLUNLOCK) to be stable
// Suggest to wait 24 cycles of divided FLL reference clock
__delay_cycles((unsigned int)3000 * MCLK_FREQ_MHZ);
while((CSCTL7 & (FLLUNLOCK0 | FLLUNLOCK1)) &&
((CSCTL7 & DCOFFG) == 0));
csCtl0Read = CSCTL0; // Read CSCTL0
csCtl1Read = CSCTL1; // Read CSCTL1
oldDcoTap = newDcoTap; // Record DCOTAP value of last time
newDcoTap = csCtl0Read & 0x01ff; // Get DCOTAP value of this time
dcoFreqTrim = (csCtl1Read & 0x0070)>>4; // Get DCOFTRIM value
if(newDcoTap < 256){ // DCOTAP < 256
newDcoDelta = 256 - newDcoTap; // Delta value between DCPTAP and 256
if((oldDcoTap != ONES_WORD) &&
(oldDcoTap >= 256)){ // DCOTAP cross 256
endLoop = TRUE; // Stop while loop
}else{
dcoFreqTrim--;
CSCTL1 = (csCtl1Read & (~DCOFTRIM)) | (dcoFreqTrim<<4);
}
}else{ // DCOTAP >= 256
newDcoDelta = newDcoTap - 256; // Delta value between DCPTAP and 256
if(oldDcoTap < 256){ // DCOTAP cross 256
endLoop = TRUE; // Stop while loop
}else{
dcoFreqTrim++;
CSCTL1 = (csCtl1Read & (~DCOFTRIM)) | (dcoFreqTrim<<4);
}
}
if(newDcoDelta < bestDcoDelta){ // Record DCOTAP closest to 256
csCtl0Copy = csCtl0Read;
csCtl1Copy = csCtl1Read;
bestDcoDelta = newDcoDelta;
}
}while(endLoop == RESET_STATE); // Poll until endLoop == 1
CSCTL0 = csCtl0Copy; // Reload locked DCOTAP
CSCTL1 = csCtl1Copy; // Reload locked DCOFTRIM
while(CSCTL7 & (FLLUNLOCK0 | FLLUNLOCK1));// Poll until FLL is locked
}