+/EE445L

embedded systems

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

jspspikeAdded lab4 files99480b6

master
6.6 KiB206 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 <stdint.h>
25#include "PLL.h"
26#include "../inc/tm4c123gh6pm.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      0x40000000  // Divide PLL as 400 MHz vs. 200
41                                            // MHz
42#define SYSCTL_RCC2_SYSDIV2_M   0x1F800000  // System Clock Divisor 2
43#define SYSCTL_RCC2_SYSDIV2LSB  0x00400000  // Additional LSB for SYSDIV2
44#define SYSCTL_RCC2_PWRDN2      0x00002000  // Power-Down PLL 2
45#define SYSCTL_RCC2_BYPASS2     0x00000800  // PLL Bypass 2
46#define SYSCTL_RCC2_OSCSRC2_M   0x00000070  // Oscillator Source 2
47#define SYSCTL_RCC2_OSCSRC2_MO  0x00000000  // MOSC
48
49// configure the system to get its clock from the PLL
50// SYSDIV = 400/freq -1
51// bus frequency is 400MHz/(SYSDIV+1)
52void PLL_Init(uint32_t freq){
53  // 0) configure the system to use RCC2 for advanced features
54  //    such as 400 MHz PLL and non-integer System Clock Divisor
55  SYSCTL_RCC2_R |= SYSCTL_RCC2_USERCC2;
56  // 1) bypass PLL while initializing
57  SYSCTL_RCC2_R |= SYSCTL_RCC2_BYPASS2;
58  // 2) select the crystal value and oscillator source
59  SYSCTL_RCC_R &= ~SYSCTL_RCC_XTAL_M;   // clear XTAL field
60  SYSCTL_RCC_R += SYSCTL_RCC_XTAL_16MHZ;// configure for 16 MHz crystal
61  SYSCTL_RCC2_R &= ~SYSCTL_RCC2_OSCSRC2_M;// clear oscillator source field
62  SYSCTL_RCC2_R += SYSCTL_RCC2_OSCSRC2_MO;// configure for main oscillator source
63  // 3) activate PLL by clearing PWRDN
64  SYSCTL_RCC2_R &= ~SYSCTL_RCC2_PWRDN2;
65  // 4) set the desired system divider and the system divider least significant bit
66  SYSCTL_RCC2_R |= SYSCTL_RCC2_DIV400;  // use 400 MHz PLL
67  SYSCTL_RCC2_R = (SYSCTL_RCC2_R&~0x1FC00000) // clear system clock divider field
68                  + (freq<<22);      // configure for 80 MHz clock
69  // 5) wait for the PLL to lock by polling PLLLRIS
70  while((SYSCTL_RIS_R&SYSCTL_RIS_PLLLRIS)==0){};
71  // 6) enable use of PLL by clearing BYPASS
72  SYSCTL_RCC2_R &= ~SYSCTL_RCC2_BYPASS2;
73}
74
75
76/*
77SYSDIV2  Divisor  Clock (MHz)
78 0        1       reserved
79 1        2       reserved
80 2        3       reserved
81 3        4       reserved
82 4        5       80.000
83 5        6       66.667
84 6        7       reserved
85 7        8       50.000
86 8        9       44.444
87 9        10      40.000
88 10       11      36.364
89 11       12      33.333
90 12       13      30.769
91 13       14      28.571
92 14       15      26.667
93 15       16      25.000
94 16       17      23.529
95 17       18      22.222
96 18       19      21.053
97 19       20      20.000
98 20       21      19.048
99 21       22      18.182
100 22       23      17.391
101 23       24      16.667
102 24       25      16.000
103 25       26      15.385
104 26       27      14.815
105 27       28      14.286
106 28       29      13.793
107 29       30      13.333
108 30       31      12.903
109 31       32      12.500
110 32       33      12.121
111 33       34      11.765
112 34       35      11.429
113 35       36      11.111
114 36       37      10.811
115 37       38      10.526
116 38       39      10.256
117 39       40      10.000
118 40       41      9.756
119 41       42      9.524
120 42       43      9.302
121 43       44      9.091
122 44       45      8.889
123 45       46      8.696
124 46       47      8.511
125 47       48      8.333
126 48       49      8.163
127 49       50      8.000
128 50       51      7.843
129 51       52      7.692
130 52       53      7.547
131 53       54      7.407
132 54       55      7.273
133 55       56      7.143
134 56       57      7.018
135 57       58      6.897
136 58       59      6.780
137 59       60      6.667
138 60       61      6.557
139 61       62      6.452
140 62       63      6.349
141 63       64      6.250
142 64       65      6.154
143 65       66      6.061
144 66       67      5.970
145 67       68      5.882
146 68       69      5.797
147 69       70      5.714
148 70       71      5.634
149 71       72      5.556
150 72       73      5.479
151 73       74      5.405
152 74       75      5.333
153 75       76      5.263
154 76       77      5.195
155 77       78      5.128
156 78       79      5.063
157 79       80      5.000
158 80       81      4.938
159 81       82      4.878
160 82       83      4.819
161 83       84      4.762
162 84       85      4.706
163 85       86      4.651
164 86       87      4.598
165 87       88      4.545
166 88       89      4.494
167 89       90      4.444
168 90       91      4.396
169 91       92      4.348
170 92       93      4.301
171 93       94      4.255
172 94       95      4.211
173 95       96      4.167
174 96       97      4.124
175 97       98      4.082
176 98       99      4.040
177 99       100     4.000
178 100      101     3.960
179 101      102     3.922
180 102      103     3.883
181 103      104     3.846
182 104      105     3.810
183 105      106     3.774
184 106      107     3.738
185 107      108     3.704
186 108      109     3.670
187 109      110     3.636
188 110      111     3.604
189 111      112     3.571
190 112      113     3.540
191 113      114     3.509
192 114      115     3.478
193 115      116     3.448
194 116      117     3.419
195 117      118     3.390
196 118      119     3.361
197 119      120     3.333
198 120      121     3.306
199 121      122     3.279
200 122      123     3.252
201 123      124     3.226
202 124      125     3.200
203 125      126     3.175
204 126      127     3.150
205 127      128     3.125
206*/