+/EE445L

embedded systems

git clone https://git.julia.blue/+/EE445L

Joseph RyanAdd project templatea8296fb

master
6.8 KiB209 linesraw
1// PLL.c
2// Runs on LM4F120/TM4C123
3// A software function to change the bus frequency using the PLL.
4// Daniel Valvano
5// May 2, 2015
6
7/* This example accompanies the book
8   "Embedded Systems: Real Time Interfacing to Arm Cortex M Microcontrollers",
9   ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2015
10   Program 2.10, Figure 2.37
11
12 Copyright 2015 by Jonathan W. Valvano, valvano@mail.utexas.edu
13    You may use, edit, run or distribute this file
14    as long as the above copyright notice remains
15 THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
16 OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
17 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
18 VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
19 OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
20 For more information about my classes, my research, and my books, see
21 http://users.ece.utexas.edu/~valvano/
22 */
23
24#include "../inc/PLL.h"
25#include "../inc/tm4c123gh6pm.h"
26#include <stdint.h>
27
28// The #define statement SYSDIV2 in PLL.h
29// initializes the PLL to the desired frequency.
30
31// bus frequency is 400MHz/(SYSDIV2+1) = 400MHz/(7+1) = 50 MHz
32// see the table at the end of this file
33
34#define SYSCTL_RIS_PLLLRIS 0x00000040    // PLL Lock Raw Interrupt Status
35#define SYSCTL_RCC_XTAL_M 0x000007C0     // Crystal Value
36#define SYSCTL_RCC_XTAL_6MHZ 0x000002C0  // 6 MHz Crystal
37#define SYSCTL_RCC_XTAL_8MHZ 0x00000380  // 8 MHz Crystal
38#define SYSCTL_RCC_XTAL_16MHZ 0x00000540 // 16 MHz Crystal
39#define SYSCTL_RCC2_USERCC2 0x80000000   // Use RCC2
40#define SYSCTL_RCC2_DIV400                                                     \
41    0x40000000                            // Divide PLL as 400 MHz vs. 200
42                                          // MHz
43#define SYSCTL_RCC2_SYSDIV2_M 0x1F800000  // System Clock Divisor 2
44#define SYSCTL_RCC2_SYSDIV2LSB 0x00400000 // Additional LSB for SYSDIV2
45#define SYSCTL_RCC2_PWRDN2 0x00002000     // Power-Down PLL 2
46#define SYSCTL_RCC2_BYPASS2 0x00000800    // PLL Bypass 2
47#define SYSCTL_RCC2_OSCSRC2_M 0x00000070  // Oscillator Source 2
48#define SYSCTL_RCC2_OSCSRC2_MO 0x00000000 // MOSC
49
50// configure the system to get its clock from the PLL
51// SYSDIV = 400/freq -1
52// bus frequency is 400MHz/(SYSDIV+1)
53void PLL_Init(uint32_t freq) {
54    // 0) configure the system to use RCC2 for advanced features
55    //    such as 400 MHz PLL and non-integer System Clock Divisor
56    SYSCTL_RCC2_R |= SYSCTL_RCC2_USERCC2;
57    // 1) bypass PLL while initializing
58    SYSCTL_RCC2_R |= SYSCTL_RCC2_BYPASS2;
59    // 2) select the crystal value and oscillator source
60    SYSCTL_RCC_R &= ~SYSCTL_RCC_XTAL_M;      // clear XTAL field
61    SYSCTL_RCC_R += SYSCTL_RCC_XTAL_16MHZ;   // configure for 16 MHz crystal
62    SYSCTL_RCC2_R &= ~SYSCTL_RCC2_OSCSRC2_M; // clear oscillator source field
63    SYSCTL_RCC2_R +=
64        SYSCTL_RCC2_OSCSRC2_MO; // configure for main oscillator source
65    // 3) activate PLL by clearing PWRDN
66    SYSCTL_RCC2_R &= ~SYSCTL_RCC2_PWRDN2;
67    // 4) set the desired system divider and the system divider least
68    // significant bit
69    SYSCTL_RCC2_R |= SYSCTL_RCC2_DIV400; // use 400 MHz PLL
70    SYSCTL_RCC2_R =
71        (SYSCTL_RCC2_R & ~0x1FC00000) // clear system clock divider field
72        + (freq << 22);               // configure for 80 MHz clock
73    // 5) wait for the PLL to lock by polling PLLLRIS
74    while ((SYSCTL_RIS_R & SYSCTL_RIS_PLLLRIS) == 0) {};
75    // 6) enable use of PLL by clearing BYPASS
76    SYSCTL_RCC2_R &= ~SYSCTL_RCC2_BYPASS2;
77}
78
79/*
80SYSDIV2  Divisor  Clock (MHz)
81 0        1       reserved
82 1        2       reserved
83 2        3       reserved
84 3        4       reserved
85 4        5       80.000
86 5        6       66.667
87 6        7       reserved
88 7        8       50.000
89 8        9       44.444
90 9        10      40.000
91 10       11      36.364
92 11       12      33.333
93 12       13      30.769
94 13       14      28.571
95 14       15      26.667
96 15       16      25.000
97 16       17      23.529
98 17       18      22.222
99 18       19      21.053
100 19       20      20.000
101 20       21      19.048
102 21       22      18.182
103 22       23      17.391
104 23       24      16.667
105 24       25      16.000
106 25       26      15.385
107 26       27      14.815
108 27       28      14.286
109 28       29      13.793
110 29       30      13.333
111 30       31      12.903
112 31       32      12.500
113 32       33      12.121
114 33       34      11.765
115 34       35      11.429
116 35       36      11.111
117 36       37      10.811
118 37       38      10.526
119 38       39      10.256
120 39       40      10.000
121 40       41      9.756
122 41       42      9.524
123 42       43      9.302
124 43       44      9.091
125 44       45      8.889
126 45       46      8.696
127 46       47      8.511
128 47       48      8.333
129 48       49      8.163
130 49       50      8.000
131 50       51      7.843
132 51       52      7.692
133 52       53      7.547
134 53       54      7.407
135 54       55      7.273
136 55       56      7.143
137 56       57      7.018
138 57       58      6.897
139 58       59      6.780
140 59       60      6.667
141 60       61      6.557
142 61       62      6.452
143 62       63      6.349
144 63       64      6.250
145 64       65      6.154
146 65       66      6.061
147 66       67      5.970
148 67       68      5.882
149 68       69      5.797
150 69       70      5.714
151 70       71      5.634
152 71       72      5.556
153 72       73      5.479
154 73       74      5.405
155 74       75      5.333
156 75       76      5.263
157 76       77      5.195
158 77       78      5.128
159 78       79      5.063
160 79       80      5.000
161 80       81      4.938
162 81       82      4.878
163 82       83      4.819
164 83       84      4.762
165 84       85      4.706
166 85       86      4.651
167 86       87      4.598
168 87       88      4.545
169 88       89      4.494
170 89       90      4.444
171 90       91      4.396
172 91       92      4.348
173 92       93      4.301
174 93       94      4.255
175 94       95      4.211
176 95       96      4.167
177 96       97      4.124
178 97       98      4.082
179 98       99      4.040
180 99       100     4.000
181 100      101     3.960
182 101      102     3.922
183 102      103     3.883
184 103      104     3.846
185 104      105     3.810
186 105      106     3.774
187 106      107     3.738
188 107      108     3.704
189 108      109     3.670
190 109      110     3.636
191 110      111     3.604
192 111      112     3.571
193 112      113     3.540
194 113      114     3.509
195 114      115     3.478
196 115      116     3.448
197 116      117     3.419
198 117      118     3.390
199 118      119     3.361
200 119      120     3.333
201 120      121     3.306
202 121      122     3.279
203 122      123     3.252
204 123      124     3.226
205 124      125     3.200
206 125      126     3.175
207 126      127     3.150
208 127      128     3.125
209*/