Contiki 3.x
system_ADuCRF101.c
Go to the documentation of this file.
1 /**
2  * Copyright (c) 2014, Analog Devices, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted (subject to the limitations in the
6  * disclaimer below) provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * - Neither the name of Analog Devices, Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
21  * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /**
35 @file system_ADuCRF101.c
36 @brief CMSIS Cortex-M3 Device Peripheral Access Layer Implementation File
37  for the ADuCRF101
38 @version v1.0
39 @author PAD CSE group, Analog Devices Inc
40 @date January 14th 2013
41 **/
42 
43 #include <stdint.h>
44 #include "ADuCRF101.h"
45 
46 
47 /*----------------------------------------------------------------------------
48  DEFINES
49  *---------------------------------------------------------------------------*/
50 
51 /* Extract the Clock Divider */
52 #define __CCLK_DIV (1 << (pADI_CLKCTL->CLKCON & CLKCON_CD_MSK) )
53 
54 /* define the clock multiplexer input frequencies */
55 #define __HFOSC 16000000
56 #define __LFXTAL 32768
57 #define __LFOSC 32768
58 
59 /*----------------------------------------------------------------------------
60  Internal Clock Variables
61  *---------------------------------------------------------------------------*/
62 static uint32_t uClk = 0; /* Undivided System Clock Frequency (UCLK) */
63 static uint32_t uClkDiv = 0; /* Divided System Clock Frequency (UCLK_DIV) */
64 
65 /* Frequency of the external clock source connected to P0.5 */
66 static uint32_t SystemExtClock = 0;
67 
68 /*----------------------------------------------------------------------------
69  Clock functions
70  *---------------------------------------------------------------------------*/
71 void SystemCoreClockUpdate (void) /* Get Core Clock Frequency */
72 {
73 /* pre-processor verification that clock mux mask and allowed values agree */
74 #if ((CLKCON_CLKMUX_HFOSC \
75  | CLKCON_CLKMUX_LFXTAL \
76  | CLKCON_CLKMUX_LFOSC \
77  | CLKCON_CLKMUX_EXTP05) \
78  == CLKCON_CLKMUX_MSK)
79 
80  /* update the system core clock according the the current clock mux setting */
81  switch (pADI_CLKCTL->CLKCON & CLKCON_CLKMUX_MSK ) {
82 
83  case CLKCON_CLKMUX_HFOSC:
84  uClk = __HFOSC;
85  break;
86  case CLKCON_CLKMUX_LFXTAL:
87  uClk = __LFXTAL;
88  break;
89  case CLKCON_CLKMUX_LFOSC:
90  uClk = __LFOSC;
91  break;
92  case CLKCON_CLKMUX_ECLKIN:
93  uClk = SystemExtClock;
94  break;
95  /* no need to catch default case due to pre-processor test */
96  }
97 
98  /* update the divided system clock */
99  uClkDiv = uClk / __CCLK_DIV;
100 
101 #else
102 #error "Clock mux mask and allowed value mismatch!"
103 #endif
104 }
105 
106 /**
107  * Initialize the system
108  *
109  * @return none
110  *
111  * @brief Setup the microcontroller system.
112  * Initialize the System and update the SystemFrequency variable.
113  */
114 void SystemInit (void)
115 {
116  /* reset CLKCON register */
117  pADI_CLKCTL->CLKCON = CLKCON_RVAL;
118 
119  /* reset XOSCCON register */
120  pADI_CLKCTL->XOSCCON = XOSCCON_RVAL;
121 
122  /* compute internal clocks */
124 }
125 
126 /**
127  * @brief Sets the system external clock frequency
128  *
129  * @param ExtClkFreq External clock frequency in Hz
130  * @return none
131  *
132  * Sets the clock frequency of the source connected to P0.5 clock input source
133  */
134 void SetSystemExtClkFreq (uint32_t ExtClkFreq)
135 {
136  SystemExtClock = ExtClkFreq;
137 }
138 
139 /**
140  * @brief Gets the system external clock frequency
141  *
142  * @return External Clock frequency
143  *
144  * Gets the clock frequency of the source connected to P0.5 clock input source
145  */
146 uint32_t GetSystemExtClkFreq (void)
147 {
148  return SystemExtClock;
149 }
150 
151 
152 /* set the system clock dividers */
153 void SystemSetClockDivider(uint16_t div)
154 {
155  /* critical region */
156  __disable_irq();
157 
158  /* read-modify-write without any interrupts */
159 
160  pADI_CLKCTL->CLKCON &= ~(CLKCON_CD_MSK); /* keep everything else */
161  pADI_CLKCTL->CLKCON |= div; /* set new value */
162 
163  /* end critical region */
164  __enable_irq();
165 
166  /* refresh internal clock variables */
168 }
169 
170 
171 uint32_t SystemGetClockFrequency(void)
172 {
173  return uClkDiv;
174 }
175 
176 
177 
178 
void SystemCoreClockUpdate(void)
Update internal SystemCoreClock variable.
uint32_t GetSystemExtClkFreq(void)
Gets the system external clock frequency.
void SetSystemExtClkFreq(uint32_t ExtClkFreq)
Sets the system external clock frequency.
Copyright (c) 2014, Analog Devices, Inc.
void SystemInit(void)
Initialize the system.