1 /*******************************************************************************
2  *
3  * HAL_UCS.h
4  * Provides Functions to Initialize the UCS/FLL and clock sources
5  *
6  *
7  * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
8  *
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
20  *    distribution.
21  *
22  *    Neither the name of Texas Instruments Incorporated nor the names of
23  *    its contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  *
38  * Created: Version 1.0 11/24/2009
39  * Updated: Version 2.0 12/15/2010
40  *          Added Functions: XT2_Stop() and XT1_Stop()
41  *
42  ******************************************************************************/
43 
44 #ifndef HAL_UCS_H
45 #define HAL_UCS_H
46 
47 #include <stdint.h>
48 #include "hal_macros.h"
49 
50 /*******************************************************************************
51  * Macros
52  ******************************************************************************/
53 
54 /* Select source for FLLREF  e.g. SELECT_FLLREF(SELREF__XT1CLK) */
55 #define SELECT_FLLREF(source) st(UCSCTL3 = (UCSCTL3 & ~(SELREF_7)) | (source);)
56 /* Select source for ACLK    e.g. SELECT_ACLK(SELA__XT1CLK) */
57 #define SELECT_ACLK(source)   st(UCSCTL4 = (UCSCTL4 & ~(SELA_7))   | (source);)
58 /* Select source for MCLK    e.g. SELECT_MCLK(SELM__XT2CLK) */
59 #define SELECT_MCLK(source)   st(UCSCTL4 = (UCSCTL4 & ~(SELM_7))   | (source);)
60 /* Select source for SMCLK   e.g. SELECT_SMCLK(SELS__XT2CLK) */
61 #define SELECT_SMCLK(source)  st(UCSCTL4 = (UCSCTL4 & ~(SELS_7))   | (source);)
62 /* Select source for MCLK and SMCLK e.g. SELECT_MCLK_SMCLK(SELM__DCOCLK + SELS__DCOCLK) */
63 #define SELECT_MCLK_SMCLK(sources) st(UCSCTL4 = (UCSCTL4 & ~(SELM_7 + SELS_7)) | (sources);)
64 
65 /* set ACLK/x */
66 #define ACLK_DIV(x)         st(UCSCTL5 = (UCSCTL5 & ~(DIVA_7)) | (DIVA__##x);)
67 /* set MCLK/x */
68 #define MCLK_DIV(x)         st(UCSCTL5 = (UCSCTL5 & ~(DIVM_7)) | (DIVM__##x);)
69 /* set SMCLK/x */
70 #define SMCLK_DIV(x)        st(UCSCTL5 = (UCSCTL5 & ~(DIVS_7)) | (DIVS__##x);)
71 /* Select divider for FLLREF  e.g. SELECT_FLLREFDIV(2) */
72 #define SELECT_FLLREFDIV(x) st(UCSCTL3 = (UCSCTL3 & ~(FLLREFDIV_7))|(FLLREFDIV__##x);)
73 
74 /*******************************************************************************
75  * Defines
76  ******************************************************************************/
77 #define UCS_STATUS_OK     0
78 #define UCS_STATUS_ERROR  1
79 
80 /*******************************************************************************
81  * \brief   Startup routine for 32kHz Crystal on LFXT1
82  *
83  * \param xtdrive   Bits defining the LFXT drive mode after startup
84  ******************************************************************************/
85 extern void LFXT_Start(uint16_t xtdrive);
86 
87 /*******************************************************************************
88  * \brief   Startup routine for 32kHz Crystal on LFXT1 with timeout counter
89  *
90  * \param xtdrive   Bits defining the LFXT drive mode after startup
91  * \param timeout   Value for the timeout counter
92  ******************************************************************************/
93 extern uint16_t LFXT_Start_Timeout(uint16_t xtdrive, uint16_t timeout);
94 
95 /*******************************************************************************
96  * \brief   Startup routine for XT1
97  *
98  * \param xtdrive   Bits defining the XT drive mode
99  ******************************************************************************/
100 extern void XT1_Start(uint16_t xtdrive);
101 
102 /*******************************************************************************
103  * \brief   Startup routine for XT1 with timeout counter
104  *
105  * \param xtdrive   Bits defining the XT drive mode
106  * \param timeout   Value for the timeout counter
107  ******************************************************************************/
108 extern uint16_t XT1_Start_Timeout(uint16_t xtdrive, uint16_t timeout);
109 
110 /*******************************************************************************
111  * \brief   Use XT1 in Bypasss mode
112  ******************************************************************************/
113 extern void XT1_Bypass(void);
114 
115 /*******************************************************************************
116  * \brief   Stop XT1 oscillator
117  ******************************************************************************/
118 extern void XT1_Stop(void);
119 
120 /*******************************************************************************
121  * \brief   Startup routine for XT2
122  *
123  * \param xtdrive   Bits defining the XT drive mode
124  ******************************************************************************/
125 extern void XT2_Start(uint16_t xtdrive);
126 
127 /*******************************************************************************
128  * \brief   Startup routine for XT2 with timeout counter
129  *
130  * \param xtdrive   Bits defining the XT drive mode
131  * \param timeout   Value for the timeout counter
132  ******************************************************************************/
133 extern uint16_t XT2_Start_Timeout(uint16_t xtdrive, uint16_t timeout);
134 
135 /*******************************************************************************
136  * \brief   Use XT2 in Bypasss mode for MCLK
137  ******************************************************************************/
138 extern void XT2_Bypass(void);
139 
140 /*******************************************************************************
141  * \brief   Stop XT2 oscillator
142  ******************************************************************************/
143 extern void XT2_Stop(void);
144 
145 /*******************************************************************************
146  * \brief   Initializes FLL of the UCS and wait till settled before allowing
147  *          code execution to resume. The use of this function is preferred
148  *          over the use of Init_FLL().
149  *
150  * \param fsystem  Required system frequency (MCLK) in kHz
151  * \param ratio    Ratio between fsystem and FLLREFCLK
152  ******************************************************************************/
153 extern void Init_FLL_Settle(uint16_t fsystem, uint16_t ratio);
154 
155 /*******************************************************************************
156  * \brief   Initializes FLL of the UCS
157  *
158  * \param fsystem  Required system frequency (MCLK) in kHz
159  * \param ratio    Ratio between fsystem and FLLREFCLK
160  ******************************************************************************/
161 extern void Init_FLL(uint16_t fsystem, uint16_t ratio);
162 
163 #endif /* HAL_UCS_H */
164