📄 k401lib.c
字号:
/***********************************************
* NAME : K401LIB.C *
* Version : 04/AUG/2000 *
***********************************************/
//SMDK 40100 B/D Status
//LED D5 D6 D7 D8
// P2.1 P2.2 P2.3 P2.4
//S/W S3 S6 P0.7 | P2.7 | P3.7 | P6.7
// S4 S7 P1.4 | P1.5 | P1.6 | P1.7
// S5 S8 EINT8 | EINT9 | EINT10 | EINT11
//Memory Configuration
//nCS nCS0 x16 EEPROM or x16 Flash ROM
// nCS1 x16 SRAM
// nCS2 P2.1
// nCS3 P2.2
// nCS4 P2.3
// nCS5 P2.4
// nCS6 nRAS or nSCS
// nCS7 EINT1
// Revision History
// Delay()
// Repaired Port_Init() : 99.11.19
#include "..\include\k401.h"
#include "..\include\option.h"
#include "..\include\k401lib.h"
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define STACKSIZE 0xa00
#define HEAPEND (_ISR_STARTADDRESS-STACKSIZE-0x500)
extern char Image$$RW$$Limit[];
void *mallocPt=Image$$RW$$Limit;
/******************** SYSTEM CONTROL ************************/
/* If time is "0", adjust the Delay function by Timer 0 */
/* 100us resolution. */
/************************************************************/
static int delayLoopCount;
void Delay(int time)
{
int i,adjust=0;
if(time==0)
{
time=100;adjust=1;
delayLoopCount=400;
rTPRE0=(char)(MCLK/1000000-1);
rTDAT0=0xffff;
rTCON0=(1<<7);
}
for(;time>0;time--)
for(i=0;i<delayLoopCount;i++);
if(adjust==1)
{
rTCON0=0x0;
i=rTCNT0; // 1 clock -> 1us
delayLoopCount=4000000/i; // 400*200*100/i
}
}
/******************************** PORTS *********************************/
/* Function : Port Initialization */
/* Description : Set-up port configuration for demo board */
/* CAUTION:Follow the configuration order for setting the ports. */
/* 1) setting value */
/* 2) setting control register */
/* 3) configure pull-up/down resistor. */
/************************************************************************/
/********************************* UART *********************************/
/************************************************************************/
/* Function : Uart_Init(int mclk,int baud) */
/* Description : This function initializes the UART. */
/* Parameter : mclk = System Clock (50MHz) */
/* Baud = Baud rate must be one of followings */
/* 9600, 19200, 38400, 57600, (115200) */
/* (ini)(MCLK/16./baud + 0.5)-1 = (50MHz/16./115200 + 0.5)-1= 26D =1ah */
/************************************************************************/
void Uart_Init(int baud)
{
rUFCON=0x0; /* FIFO disable */
rULCON=UART_MODE_DEFAULT; /*Normal,No parity,1 stop,8 bit*/
rUCON=UART_CTRL_DEFAULT; /*Enable rx/tx/status int.*/
// rUBRDIV=0x1a; /*50MHz , 115200bps*/
rUBRDIV=0x15; /*40MHz , 115200bps*/
}
/************************************************************************/
/* Function : Uart_TxEmpty(void) */
/* Description : This function wait until Tx shifter is empty */
/************************************************************************/
void Uart_TxEmpty(void)
{
while(!(rUSTAT & UART_STAT_XMT_SHIFT_EMPTY)); //wait until tx shifter is empty.
}
/************************************************************************/
/* Function : Uart_Getch(void) */
/* Description : This function gets a character from the UART port */
/************************************************************************/
char Uart_Getch(void)
{
while(!(rUSTAT & UART_STAT_RCV_DATA)); //Receive data ready
return rURXH;
}
/************************************************************************/
/* Function : Uart_GetKey(void) */
/* Description : This function report whether the UART port receives a */
/* character or not. If it receives a character from the */
/* UART port, it return the character. Unless it receives */
/* any, it returns 0, */
/************************************************************************/
char Uart_GetKey(void)
{
if(rUSTAT & UART_STAT_RCV_DATA) // Receive data ready
return rURXH; // If the UART port receives any one character, return it
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(!(rUSTAT & UART_STAT_XMT_HOLDING_EMPTY));
Delay(4); //because the slow response of hyper_terminal
rUTXH='\r';
}
while(!(rUSTAT & UART_STAT_XMT_HOLDING_EMPTY)); //Wait until THR is empty.
Delay(4);
rUTXH=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);
}
/******************** KS17C40100 BOARD LED **********************/
void Led_Display(int data)
{
rPDAT2=(rPDAT2 & 0xe1)|(data<<1);
}
/************************* Timer ********************************/
void Timer_Start(void)
{
rTPRE0=0xff; // Timer0 Prescaler value is 0xff
rTDAT0=0xffff;
rTCON0=0xc0; // TCLK=MCLK, OTM=Match&Overflow mode, CL=No effect, Timer enable
}
int Timer_Stop(void)
{
rTCON0=0x0;
return rTCNT0;
}
/************************* General Library **********************/
void Flush_Cache(void)
{
int i;
for(i=0x11000000;i<0x11000200;i+=16) // TagRAM Address 0x11000000 ~ 0x110001ff
{
*((int *)i)=0x0;
}
}
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 + -