⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pll.c

📁 linux flash 操作控制程序源码! l inux flash 操作控制程序源码!
💻 C
字号:
#define __PLL

#include <stdio.h>
#include <math.h>
#include "Common.h"
#include "mx21.h"
#include "pll.h"



/*

void SetCLKO_SEL (void)
{
	char *clko[6] = {"PERCLK", "HCLK", "CLK48M", "CLK16M", "PREMCLK", "FCLK"};
	U32 value;
	U8 i;

	printf ("\n Select the CLKO_SEL : \n\n");
	
	for (i=0; i<6; i++)
		printf (" %d : %s\n", i, clko[i]);
	
	printf ("\n Enter your selection : ");
	value=(U8)GetDecNum(0,5);
	
	printf ("\n\n");
	SetCLKO(value);

	printf(" The clock source of CLKO is %s.\n\n", clko[value]);
}

void ChangeBCLK_DIV (void)
{
	U32 value;
	U8 i;

	printf ("\n Input the value of BCLK divider (1-16) : \n\n");
	
	value=(U8)GetDecNum(1, 16);
	
	printf ("\n\n");
	SetBCLK_DIV (value-1);

	printf(" BCLK_DIV = %d, or the system pll freq. is divided by %d.\n\n", value-1, value);
}




void SetCLKO (U8 clko_value)
{
	*(volatile P_U32)CRM_CSCR &= ~(bit31|bit30|bit29);
	*(volatile P_U32)CRM_CSCR |= ((clko_value)<<29);
}	


void SetBCLK_DIV (U8 bclk_div_value)
{

	*(volatile P_U32)CRM_CSCR &= ~(bit13|bit12|bit11|bit10);
	*(volatile P_U32)CRM_CSCR |= (bclk_div_value<<10);	
}			

void SetSysPll (U16 sys_freq)
{
//	SetCLKO (CLKO_HCLK);
	SetPll (sys_freq, CRM_SPCTL0);

	*(volatile P_U32)CRM_CSCR |= bit22;		// restart the PLL
}			

*/

void SetMcuPll (U16 mcu_freq)
{
	SetPll (mcu_freq, CRM_MPCTL0);

	*(volatile P_U32)CRM_CSCR |= bit21;		// restart the PLL
	
	while ((*(volatile U32 *)CRM_MPCTL1 & bit15)==0);
}			



void SetPll (U16 freq, U32 pll_reg)
{
	double fi;
	U32 mfd,mfn,mfi,pd;

	mfd = 999;

	pd = freq<160 ? 1 : 0;
	mfn = (modf (freq/32.768*(pd+1), &fi) + 0.0005)* 1000;
	mfi = fi;

// PD, MFI = 0..15
// MFD, MFN = 0..1023
//                          Set PD,      MFD,      MFI,      MFN
	*(volatile P_U32)pll_reg = (pd<<26)|(mfd<<16)|(mfi<<10)|(mfn<<0);
/*	
	printf ("PD = %d\n",pd);
	printf ("FN = %d\n",mfn);
	printf ("FI = %d\n",mfi);
*/
}			



void ShowMPLL (void)
{
	printf ("MPLL = %fMHz\n", GetPllFreq(CRM_MPCTL0));
}

void ShowSPLL (void)
{
	printf ("SPLL = %fMHz\n", GetPllFreq(CRM_SPCTL0));
}

void ShowFCLK (void)
{
	printf ("FCLK = %fMHz\n", GetPllFreq(CRM_MPCTL0) / GetPRESC());
}

void ShowHCLK (void)
{
	printf ("HCLK = %fMHz\n", GetPllFreq(CRM_MPCTL0) / GetPRESC() / GetBCLKDIV() );
}


double RtnHCLK (void)
{
	return ( GetPllFreq(CRM_MPCTL0) / GetPRESC() / GetBCLKDIV() );
}

void SetPRESC (U32 value)
{
	U32 tmp;
	tmp = (*(volatile P_U32)CRM_CSCR) & (~bit15_14);
	tmp |= ((value-1)<<14);
	(*(volatile P_U32)CRM_CSCR) = tmp;
}

void SetBCLKDIV (U32 value)
{
	U32 tmp;
	tmp = (*(volatile P_U32)CRM_CSCR) & (~0x00003c00);
	tmp |= ((value-1)<<10);
	(*(volatile P_U32)CRM_CSCR) = tmp;
}

void SetPERDIV1 (U32 value)
{
	U32 tmp;
	tmp = (*(volatile P_U32)CRM_PCDR) & (~(0xf<<8));
	tmp |= ((value-1)<<8);
	(*(volatile P_U32)CRM_PCDR) = tmp;
}

U32 GetPRESC (void)
{
	return ((((*(volatile P_U32)CRM_CSCR & bit15_14)>>14) & 0x3)+1);
}

U32 GetIPDIV (void)
{
	return ((((*(volatile P_U32)CRM_CSCR & bit9)>>9) & 0x1)+1);
}

U32 GetBCLKDIV (void)
{
	return ((((*(volatile P_U32)CRM_CSCR & 0x00003c00)>>10) & 0xf)+1);
}


double GetPllFreq (U32 pll_reg)
{
	double pd, mfd, mfi, mfn, fclk;
	
	pd  = ((*(volatile P_U32)pll_reg)>>26) & 0xf;
	mfi = ((*(volatile P_U32)pll_reg)>>10) & 0xf;
	mfd = ((*(volatile P_U32)pll_reg)>>16) & 0x3ff;
	mfn = ((*(volatile P_U32)pll_reg)>>00) & 0x3ff;

	return (2*512*CRYSTAL_FREQ*(mfi+mfn/(mfd+1))/(pd+1));
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -