+/EE445L
embedded systems
git clone https://git.julia.blue/+/EE445L
a8296fb
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 "../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 56SYSCTL_RCC2_R |=SYSCTL_RCC2_USERCC2 ; 57// 1) bypass PLL while initializing 58SYSCTL_RCC2_R |=SYSCTL_RCC2_BYPASS2 ; 59// 2) select the crystal value and oscillator source 60SYSCTL_RCC_R &= ~SYSCTL_RCC_XTAL_M ;// clear XTAL field 61SYSCTL_RCC_R += SYSCTL_RCC_XTAL_16MHZ ;// configure for 16 MHz crystal 62SYSCTL_RCC2_R &= ~SYSCTL_RCC2_OSCSRC2_M ;// clear oscillator source field 63SYSCTL_RCC2_R += 64SYSCTL_RCC2_OSCSRC2_MO ;// configure for main oscillator source 65// 3) activate PLL by clearing PWRDN 66SYSCTL_RCC2_R &= ~SYSCTL_RCC2_PWRDN2 ; 67// 4) set the desired system divider and the system divider least 68// significant bit 69SYSCTL_RCC2_R |=SYSCTL_RCC2_DIV400 ;// use 400 MHz PLL 70SYSCTL_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 74while ((SYSCTL_RIS_R & SYSCTL_RIS_PLLLRIS )== 0 ) {}; 75// 6) enable use of PLL by clearing BYPASS 76SYSCTL_RCC2_R &= ~SYSCTL_RCC2_BYPASS2 ; 77} 78 79/* 80SYSDIV2 Divisor Clock (MHz) 810 1 reserved 821 2 reserved 832 3 reserved 843 4 reserved 854 5 80.000 865 6 66.667 876 7 reserved 887 8 50.000 898 9 44.444 909 10 40.000 9110 11 36.364 9211 12 33.333 9312 13 30.769 9413 14 28.571 9514 15 26.667 9615 16 25.000 9716 17 23.529 9817 18 22.222 9918 19 21.053 10019 20 20.000 10120 21 19.048 10221 22 18.182 10322 23 17.391 10423 24 16.667 10524 25 16.000 10625 26 15.385 10726 27 14.815 10827 28 14.286 10928 29 13.793 11029 30 13.333 11130 31 12.903 11231 32 12.500 11332 33 12.121 11433 34 11.765 11534 35 11.429 11635 36 11.111 11736 37 10.811 11837 38 10.526 11938 39 10.256 12039 40 10.000 12140 41 9.756 12241 42 9.524 12342 43 9.302 12443 44 9.091 12544 45 8.889 12645 46 8.696 12746 47 8.511 12847 48 8.333 12948 49 8.163 13049 50 8.000 13150 51 7.843 13251 52 7.692 13352 53 7.547 13453 54 7.407 13554 55 7.273 13655 56 7.143 13756 57 7.018 13857 58 6.897 13958 59 6.780 14059 60 6.667 14160 61 6.557 14261 62 6.452 14362 63 6.349 14463 64 6.250 14564 65 6.154 14665 66 6.061 14766 67 5.970 14867 68 5.882 14968 69 5.797 15069 70 5.714 15170 71 5.634 15271 72 5.556 15372 73 5.479 15473 74 5.405 15574 75 5.333 15675 76 5.263 15776 77 5.195 15877 78 5.128 15978 79 5.063 16079 80 5.000 16180 81 4.938 16281 82 4.878 16382 83 4.819 16483 84 4.762 16584 85 4.706 16685 86 4.651 16786 87 4.598 16887 88 4.545 16988 89 4.494 17089 90 4.444 17190 91 4.396 17291 92 4.348 17392 93 4.301 17493 94 4.255 17594 95 4.211 17695 96 4.167 17796 97 4.124 17897 98 4.082 17998 99 4.040 18099 100 4.000 181100 101 3.960 182101 102 3.922 183102 103 3.883 184103 104 3.846 185104 105 3.810 186105 106 3.774 187106 107 3.738 188107 108 3.704 189108 109 3.670 190109 110 3.636 191110 111 3.604 192111 112 3.571 193112 113 3.540 194113 114 3.509 195114 115 3.478 196115 116 3.448 197116 117 3.419 198117 118 3.390 199118 119 3.361 200119 120 3.333 201120 121 3.306 202121 122 3.279 203122 123 3.252 204123 124 3.226 205124 125 3.200 206125 126 3.175 207126 127 3.150 208127 128 3.125 209*/