📄 k44lib.c
字号:
/***********************************************
* NAME : K44LIB.C *
* Version : 7.JUN.2000 *
***********************************************/
// Revision History
// Delay(): Delay function is fixed for the case that rBTCNT=0.
// Timer0_Start()/Timer0_Stop() is fixed.
#include "def.h"
#include "option.h"
#include "k44.h"
#include "k44lib.h"
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
//SVC stack area only if all heap area are used.:(x3ff80-0x400) ~ (0x3ff80-0x220)
#define HEAPEND (_ISR_STARTADDRESS-0x400)
extern char Image$$RW$$Limit[];
void *mallocPt=Image$$RW$$Limit;
/************************* SYSTEM *************************/
void Delay(int time)
// time=0: adjust the Delay function by WatchDog timer.
// 100us resolution.
{
static int t100u=DELAY_START_VAL;
int i,j;
if(time==0)
{
rBTCON=0xa503;
for(i=0;i<t100u;i++);
i=rBTCNT;
if(i==0)i=1;
t100u=(255*(EXTCLK_FREQ/512/10000+1))/i;
//+1 is added because of round-off.
}
else
{
for(i=0;i<time;i++)
for(j=0;j<t100u;j++);
}
}
/************************* PORTS ****************************/
void Port_Init(void)
{
rP0CON=0x0f; //1:output 0:input
rP0PUR=0xf0; //1:enable 0:disable
// G7 G6 G5 G4 G3 G2 G1 G0
//P0CON: I I I I O O O O
//P0PUR: P P P P x x x x
rP1CON=0x2aaa;
rP1PUDR=0x0; //1:enable 0:disable
// G15 G14 G13 G12 G11 G10 G09 G08
//P1CON :RXD TXD A17 A16 A15 A14 A13 A12
// 00 10, 10 10, 10 10, 10 10
//P1PUDR: x x x x x x x x
rP2CON=0x0; //1:output 0:input
rP2PUR=0x7; //1:enable 0:disable
// E2 E1 E0
//P1CON : I I I
//P1PUR : P P P
}
/*************************** UART ****************************/
void Uart_Init(int baud)
{
//EXTCLK is 20Mhz
rLCON=0x43; //NOIR EXTCLK NP 1STOP 8bit
//0 1 000 0 11
rUCON=0x9; // NO_LPBK NO_BR 0 01 NO_STINT 01
// 0 0 0 01 0 01
//rUBRDR=( (int)(EXTCLK_FREQ/16./baud + 0.5)-1 );
rUBRDR=( (int)(MCLK_FREQ/16./baud + 0.5)-1 );
}
void Uart_TxEmpty(void)
{
while(!(rUSSR & 0x80)); //wait until tx shifter is empty.
}
char Uart_Getch(void)
{
while(!(rUSSR & 0x20)); //Receive data read
return rRBR;
}
char Uart_GetKey(void)
{
if(rUSSR & 0x20) //Receive data ready
return rRBR;
else
return 0;
}
void Uart_GetString(char *string)
{
char *string2=string;
char c;
while((c=Uart_Getch())!='\r')
{
if(c=='\b')
{
if((int)string2<(int)string){Uart_Printf("\b \b");string--;}
}
else {*string++=c;Uart_SendByte(c);}
}
*string='\0';
Uart_SendByte('\n');
}
int Uart_GetIntNum(void)
{
char str[30];
char *string=str;
int base=10;
int minus=0;
int lastIndex;
int result=0;
int i;
Uart_GetString(string);
if(string[0]=='-'){minus=1;string++;}
if(string[0]=='0' && (string[1]=='x' || string[1]=='X'))
{
base=16;
string+=2;
}
lastIndex=strlen(string)-1;
if( string[lastIndex]=='h' || string[lastIndex]=='H' )
{
base=16;
string[lastIndex]=0;
lastIndex--;
}
if(base==10)
{
result=atoi(string);
result=minus ? (-1*result):result;
}
else
{
for(i=0;i<=lastIndex;i++)
{
if(isalpha(string[i]))
{
if(isupper(string[i]))
result=(result<<4)+string[i]-'A'+10;
else
result=(result<<4)+string[i]-'a'+10;
}
else
{
result=(result<<4)+string[i]-'0';
}
}
result=minus ? (-1*result):result;
}
return result;
}
void Uart_SendByte(int data)
{
if(data=='\n')
{
while(!(rUSSR & 0x40));
Delay(4); //because the slow response of hyper_terminal
rTBR='\r';
}
while(!(rUSSR & 0x40)); //Wait until THR is empty.
Delay(4); //because the slow response of hyper_terminal
rTBR=(U8)data;
}
void Uart_SendString(char *pt)
{
while(*pt)Uart_SendByte(*pt++);
}
//if you don't use vsprintf(), the code size is reduced very much.
void Uart_Printf(char *fmt,...)
{
va_list ap;
char string[256];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
Uart_SendString(string);
va_end(ap);
}
/******************** KS32C41000 BOARD LED **********************/
void Led_Display(int data)
{
rP0=(rP0 & 0xf0)|(data&0xf);
}
/************************* Timer ********************************/
static int ovfCnt;
static void __irq Isr_Timer0(void)
{
rINTPEND=~BIT_T0OVF;
ovfCnt++;
}
void Timer0_Start(int divider) //19:1us, 199:10us
{
rINTMASK=rINTMASK|BIT_T0OVF;
pISR_T0OVF=(unsigned)Isr_Timer0;
ovfCnt=0;
rT0CON=0x48; //DIS CLR MAT_OVF EXTCLK 00
//0 1 001 0 00
rT0PRE=divider; //1us resoultion
rT0CON=0x88; //EN NOCLR MAT_OVF EXTCLK 00
//1 0 001 0 00
}
int Timer0_Stop(void)
{
rT0CON=0x08; //DIS NOCLR MAT_OVF EXTCLK 00
//0 0 001 0 00
rINTMASK=rINTMASK&(~BIT_T0OVF);
return (ovfCnt*0x10000+rT0CNT);
}
/************************* PLL ********************************/
void ChangePllValue(int mdiv,int pdiv,int sdiv)
{
int i;
rSYSCON=rSYSCON&(~(1<<6))|(1<<7); //CLKSRCSEL=EXTCLK, PLL=on
rPLLCON=(mdiv<<12)|(pdiv<<4)|sdiv;
for(i=0;i<(10*150);i++);//wait 150us for PLL lock-time
rSYSCON=rSYSCON|(1<<6); //CLKSRCSEL=PLL_output
}
/************************* General Library **********************/
void *malloc(unsigned nbyte)
/*Very simple; Use malloc() & free() like Stack*/
{
void *returnPt=mallocPt;
mallocPt= (int *)mallocPt+nbyte/4+((nbyte%4)>0);
if( (int)mallocPt > HEAPEND){mallocPt=returnPt;return NULL;}
return returnPt;
}
void free(void *pt)
{
mallocPt=pt;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -