+/EE445L
embedded systems
git clone https://git.julia.blue/+/EE445L
99480b6
master
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", 9ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2015 10Program 2.10, Figure 2.37 11 12Copyright 2015 by Jonathan W. Valvano, valvano@mail.utexas.edu 13You may use, edit, run or distribute this file 14as long as the above copyright notice remains 15THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED 16OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF 17MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. 18VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, 19OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 20For more information about my classes, my research, and my books, see 21http://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 55SYSCTL_RCC2_R |=SYSCTL_RCC2_USERCC2 ; 56// 1) bypass PLL while initializing 57SYSCTL_RCC2_R |=SYSCTL_RCC2_BYPASS2 ; 58// 2) select the crystal value and oscillator source 59SYSCTL_RCC_R &= ~SYSCTL_RCC_XTAL_M ;// clear XTAL field 60SYSCTL_RCC_R += SYSCTL_RCC_XTAL_16MHZ ;// configure for 16 MHz crystal 61SYSCTL_RCC2_R &= ~SYSCTL_RCC2_OSCSRC2_M ;// clear oscillator source field 62SYSCTL_RCC2_R += SYSCTL_RCC2_OSCSRC2_MO ;// configure for main oscillator source 63// 3) activate PLL by clearing PWRDN 64SYSCTL_RCC2_R &= ~SYSCTL_RCC2_PWRDN2 ; 65// 4) set the desired system divider and the system divider least significant bit 66SYSCTL_RCC2_R |=SYSCTL_RCC2_DIV400 ;// use 400 MHz PLL 67SYSCTL_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 70while ((SYSCTL_RIS_R & SYSCTL_RIS_PLLLRIS )== 0 ){}; 71// 6) enable use of PLL by clearing BYPASS 72SYSCTL_RCC2_R &= ~SYSCTL_RCC2_BYPASS2 ; 73} 74 75 76/* 77SYSDIV2 Divisor Clock (MHz) 780 1 reserved 791 2 reserved 802 3 reserved 813 4 reserved 824 5 80.000 835 6 66.667 846 7 reserved 857 8 50.000 868 9 44.444 879 10 40.000 8810 11 36.364 8911 12 33.333 9012 13 30.769 9113 14 28.571 9214 15 26.667 9315 16 25.000 9416 17 23.529 9517 18 22.222 9618 19 21.053 9719 20 20.000 9820 21 19.048 9921 22 18.182 10022 23 17.391 10123 24 16.667 10224 25 16.000 10325 26 15.385 10426 27 14.815 10527 28 14.286 10628 29 13.793 10729 30 13.333 10830 31 12.903 10931 32 12.500 11032 33 12.121 11133 34 11.765 11234 35 11.429 11335 36 11.111 11436 37 10.811 11537 38 10.526 11638 39 10.256 11739 40 10.000 11840 41 9.756 11941 42 9.524 12042 43 9.302 12143 44 9.091 12244 45 8.889 12345 46 8.696 12446 47 8.511 12547 48 8.333 12648 49 8.163 12749 50 8.000 12850 51 7.843 12951 52 7.692 13052 53 7.547 13153 54 7.407 13254 55 7.273 13355 56 7.143 13456 57 7.018 13557 58 6.897 13658 59 6.780 13759 60 6.667 13860 61 6.557 13961 62 6.452 14062 63 6.349 14163 64 6.250 14264 65 6.154 14365 66 6.061 14466 67 5.970 14567 68 5.882 14668 69 5.797 14769 70 5.714 14870 71 5.634 14971 72 5.556 15072 73 5.479 15173 74 5.405 15274 75 5.333 15375 76 5.263 15476 77 5.195 15577 78 5.128 15678 79 5.063 15779 80 5.000 15880 81 4.938 15981 82 4.878 16082 83 4.819 16183 84 4.762 16284 85 4.706 16385 86 4.651 16486 87 4.598 16587 88 4.545 16688 89 4.494 16789 90 4.444 16890 91 4.396 16991 92 4.348 17092 93 4.301 17193 94 4.255 17294 95 4.211 17395 96 4.167 17496 97 4.124 17597 98 4.082 17698 99 4.040 17799 100 4.000 178100 101 3.960 179101 102 3.922 180102 103 3.883 181103 104 3.846 182104 105 3.810 183105 106 3.774 184106 107 3.738 185107 108 3.704 186108 109 3.670 187109 110 3.636 188110 111 3.604 189111 112 3.571 190112 113 3.540 191113 114 3.509 192114 115 3.478 193115 116 3.448 194116 117 3.419 195117 118 3.390 196118 119 3.361 197119 120 3.333 198120 121 3.306 199121 122 3.279 200122 123 3.252 201123 124 3.226 202124 125 3.200 203125 126 3.175 204126 127 3.150 205127 128 3.125 206*/