📄 tm_clk.c
字号:
#include "../inc/def.h"
#include "../inc/config.h"
#include "../inc/board.h"
#include "../inc/utils.h"
#include "44b.h"
#include "option.h"
static struct{
U32 mclk;
U32 pclk;
U32 sclk;
U32 freq;
} ClkPara;
static void set_pll(void)
{
U32 i, j;
if(ClkPara.mclk>255)
ClkPara.mclk = 255;
if(ClkPara.pclk>63)
ClkPara.pclk = 63;
if(ClkPara.sclk>3)
ClkPara.sclk = 3;
j = ClkPara.sclk;
i = 1;
while(j--)
i *= 2;
ClkPara.freq = (EXT_OSC_CLK*(ClkPara.mclk+8))/((ClkPara.pclk+2)*i);
if((ClkPara.freq>=90000000)||(ClkPara.freq<EXT_OSC_CLK)) {
ClkPara.mclk = 53;
ClkPara.pclk = 3;
ClkPara.pclk = 1;
ClkPara.freq = EXT_OSC_CLK*61/10;
}
rPLLCON = (ClkPara.mclk << 12) | (ClkPara.pclk << 4) | ClkPara.sclk;
}
void SystemClockInit(void)
{
set_pll();
}
U32 GetCpuClock(void)
{
return ClkPara.freq;
}
U32 GetMasterClock(void)
{
return GetCpuClock();
}
int SetSysClock(void)
{
int mdiv, pdiv, sdiv;
char tmp[4];
printf("Please enter the PLL parameter to use, mdiv[0-255], pdiv[0-63], sdiv[0-3]\n");
printf("mdiv: ");
if((mdiv=strtodec(tmp, GetParameter(tmp, 3)))<0) {
printf("\nget mdiv Error!\n");
return -1;
}
printf("\npdiv: ");
if((pdiv=strtodec(tmp, GetParameter(tmp, 2)))<0) {
printf("\nget pdiv Error!\n");
return -1;
}
printf("\nsdiv: ");
if((sdiv=strtodec(tmp, GetParameter(tmp, 1)))<0) {
printf("\nget sdiv Error!\n");
return -1;
}
printf("\nYou set System clock mdiv = %d, pdiv = %d, sdiv = %d\n", mdiv, pdiv, sdiv);
Delay(10);
ClkPara.mclk = mdiv;
ClkPara.pclk = pdiv;
ClkPara.sclk = sdiv;
set_pll();
// Delay(0);
return 0;
}
U32 GetSysClock(SysClock pSysClock[])
{
pSysClock[0].name = "CPU";
pSysClock[0].freq = GetCpuClock();
return 1;
}
void SetSysClockPara(void *addr)
{
U32 *pData = addr;
ClkPara.mclk = pData[0];
ClkPara.pclk = pData[1];
ClkPara.sclk = pData[2];
set_pll();
}
void GetSysClockPara(void *addr)
{
U32 *pData = addr;
pData[0] = ClkPara.mclk;
pData[1] = ClkPara.pclk;
pData[2] = ClkPara.sclk;
pData[3] = ClkPara.freq;
}
void TimerInit(U32 HZ)
{
rTCON &= ~(0xf<<24); //clear manual update bit, stop Timer5
rTCFG0 &= ~(0xff<<16); //set Timer 4&5 prescaler 1/2
rTCFG0 |= 1<<16;
rTCFG1 &= ~(0xf<<20); //set Timer 5 MUX 1/16
rTCFG1 |= 3<<20;
rTCNTB5 = GetCpuClock()/(HZ*2*16)-1;
rTCON |= 2<<24; //manual update
rTCON &= ~(2<<24); //clear manal update bit
rTCON |= (5<<24); //auto reload, inverter on, start Timer 5
}
/*void ResetTimer(void)
{
rTCON &= ~(1<<24);
rTCON |= (1<<24);
}*/
void Delay(U32 ms)
{
U16 i;
i = rTCNTB5>>1; //1000us/2
rTCON &= ~(1<<24);
rTCON |= (1<<24); //停止再启动,重装初值,减计数
while(ms--) {
while(rTCNTO5>=i);
while(rTCNTO5<i);
}
}
int WaitEventWithTimeout(int (*event)(void), int cond, U32 ms)
{
int result;
U16 half, ms_h;
rTCON &= ~(1<<24);
rTCON |= (1<<24); //停止再启动,重装初值,减计数
half = rTCNTB5>>1; //1000us/2
ms_h = 0;
while(ms) {
result = (*event)();
if(cond) {
if(result)
return 0;
} else {
if(!result)
return 0;
}
if(!ms_h) {
if(rTCNTO5<=half)
ms_h = 1;
} else {
if(rTCNTO5>half) {
ms_h = 0;
ms--;
}
}
}
return -1;
}
/*
static int delayLoopCount=400;
void Delay(U32 time)
{
int i, adjust = 0;
U32 mclk = GetCpuClock();
if(time==0)
{
time=200;
adjust=1;
delayLoopCount=400;
rWTCON=((mclk/1000000-1)<<8)|(2<<3); //MCLK/1M,Watch-dog disable,1/64,interrupt disable,reset disable
rWTDAT=0xffff;//for first update
rWTCNT=0xffff;//resolution=64us @any MCLK
rWTCON=((mclk/1000000-1)<<8)|(2<<3)|(1<<5); //Watch-dog timer start
}
for(;time>0;time--)
for(i=0;i<delayLoopCount;i++);
if(adjust==1)
{
rWTCON=((mclk/1000000-1)<<8)|(2<<3);//Watch-dog timer stop
i=0xffff-rWTCNT; //1count->64us, 200*400 cycle runtime = 64*i us
delayLoopCount=8000000/(i*64); //200*400:64*i=1*x:100 -> x=80000*100/(64*i)
}
}*/
/*************************************************************/
#define RTC_RW_EN() (rRTCCON = 1) //|= 1
#define RTC_RW_DS() (rRTCCON &= ~1)
void RtcSetDay(TIME_STRUC *time)
{
RTC_RW_EN(); //RTC读写使能,选择BCD时钟、计数器,无复位,1/32768
rBCDYEAR = time->year&0xff; //年
rBCDMON = time->month; //月
rBCDDAY = time->day; //日
RTC_RW_DS();
}
void RtcSetWeek(TIME_STRUC *time)
{
RTC_RW_EN();
rBCDDATE = time->weekday;
RTC_RW_DS();
}
void RtcSetTime(TIME_STRUC *time)
{
RTC_RW_EN(); //RTC读写使能,选择BCD时钟、计数器,无复位,1/32768
rBCDHOUR = time->hour; //小时
rBCDMIN = time->min; //分
rBCDSEC = time->sec; //秒
RTC_RW_DS();
}
void RtcGetTime(TIME_STRUC *time)
{
unsigned char m;
RTC_RW_EN(); //RTC读写使能
time->year = 0x2000 + rBCDYEAR;
time->weekday = rBCDDATE;
m = rBCDMON;
time->month = m;//(m>>4)*10 + (m&0xf); //BCD码转十进制
m = rBCDDAY;
time->day = m;//(m>>4)*10 + (m&0xf); //BCD码转十进制
m = rBCDHOUR;
time->hour = m;//(m>>4)*10 + (m&0xf); //BCD码转十进制
m = rBCDMIN;
time->min = m;//(m>>4)*10 + (m&0xf); //BCD码转十进制
m = rBCDSEC;
time->sec = m;//(m>>4)*10 + (m&0xf); //BCD码转十进制
RTC_RW_DS(); //RTC读写禁止(降低功率消耗),选择BCD时钟、计数器,无复位,1/32768
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -