📄 main.c
字号:
// *************************************************
// 44B0学习板BIOS程序
//2005.06.17.....
//广州新赛格
//=======================
#include "44b.h"
#include "sysloader.h"
#include "lcd.h"
#include "io_port.h"
#include "39vf160.h"
#include "catch.h"
#include "Nand.h"
#include "ctrl_set.h"
#include "console.h"
#include "utils.h"
#include "DataType.h"
//================================
void loadma();
void (*__SYS)(void);
void Uart0_Rx();
void Com_Proc();
void TX(char *pBuf,unsigned int TL);
static int ParseCmd(char *cmdline, int cmd_len);
unsigned int OS_ENTER(void);
INT32U te1,te2,te3;
INT32U EnRx = 0;
U32 SERIAL_BAUD = 57600;
//U32 SERIAL_BAUD = 115200;
#define DFT_DOWNLOAD_ADDR 0xc000000
#define MAX_CMD_LEN 128
#define MAX_ARGS MAX_CMD_LEN/4
#define prompt() puts("\\>")
#define SECTOR_SIZE 0x1000
extern U32 MCLK;
int IP_ADDRESS = 0xc0a80164; //设置默认的IP地址
U32 download_addr;
U32 program_begin;
#define prompt() puts("\\>")
/*********************************************************/
void Main(void)
{
INT32U k,tcount=0;
int i, key;
char command[MAX_CMD_LEN];
/*初始化IO*/
IO_PortInit();
/*初始化LCD 256色640*480*/
Lcd_Init();
rPDATD = rPDATD | 0x40; //PD6 = 1,LCD(MTV-C3224) On
/*初始化串口*/
console_init(SERIAL_BAUD);
/*初始化Cache*/
Cache_Init();
rPLLCON = (52<<12)|(3<<4)|1; //修改系统主频为60MHz
if (((rPDATG>>4)&0x7)>6) //无按任何按键,
OS_ENTER(); //进入UCOS_II,或uCLinux
rINTMSK = rINTMSK | BIT_GLOBAL ; //关全局中断,
Lcd_Clr(DC,bluecolor);
/*初始化字库*/
Sysloader_LoadFont0816(); // 8*16 ASCII 字库
Sysloader_LoadFont1616(); //16*16 国标字库
VGA_X = 0;
VGA_Y = 0;
Lcd_printf(&DC,0xff,ncolor," QX44B0_Bios Version 3.01\n");
Lcd_Clrxy(CON_X0-5, CON_Y0-5,CON_X+10 , CON_Y+10,CON_color);
VGA_X = 0;
VGA_Y = 195;
Lcd_printf(&DC,0xff,ncolor," 广州市微嵌计算机有限公司\n");
Lcd_printf(&DC,0xff,ncolor," www.QXLCD.com 020-38497067\n");
printf("\n");
printf(" QX44B0_Bios Version 3.01\n");
printf("\n");
printf("Serial Baud rate is 57600 N 8 1\n");
printf("\n");
puts("\\>");
/* ====================================== */
i = 0;
while(1)
{
if(kbhit())
{
key = getkey();
if(key == BACK_KEY)
{
if(i)
{
putch(key);
putch(' ');
putch(key);
}
i -= i?1:0;
}
else
if(key == ENTER_KEY)
{
int tmp;
command[i] = 0;
printf("\n");
tmp = ParseCmd(command, i);
if(tmp<0)
puts("Bad command\n");
prompt();
i = 0;
}
else
{
if(i<MAX_CMD_LEN-1)
{
command[i++] = key;
putch(key);
}
}
}
}
}
unsigned int OS_ENTER(void)
{
unsigned short arr[2];
FlashRead(0x10000, (unsigned short *)arr, 4);
if (arr[0] == 0xffff && arr[1] == 0x0ffff)
return 0;
__SYS = (void(*)())0x0c000000;
if (arr[0] == 0x0000 || arr[1] == 0x0000)
{
/*初始化字库*/
Sysloader_LoadFont0816(); // 8*16 ASCII 字库
Sysloader_LoadFont1616(); //16*16 国标字库
VGA_X = 100;
VGA_Y = 100;
Lcd_printf(&DC,0xff,ncolor," Welcome to uClinux \n");
FlashRead(0x10000, (unsigned short *)0x0c000000, 0x0100000);
(*__SYS)(); //跳转到0C000000;
}else
{
FlashRead(0x10000, (unsigned short *)0x0c000000, 0x0A0000);
(*__SYS)(); //跳转到0C000000;
}
return 0;
}
/*********************************************************/
static void delay(U32 cnt)
{
while(cnt--);
}
static int getkey_con(void)
{
puts("Press any key to continue...\n");
return (getch()==0x1b);
}
/*********************************************************/
int pro_Flash_w(unsigned long data_begin,unsigned long size)
{
printf("program flash begin @0x%x, size = %dBytes\nAre you sure? [y/n]", download_addr, size);
while(1)
{
if(kbhit())
{
int key = getkey();
if(key=='y'||key=='Y')
{
putch(key);
next_line();
break;
}
if(key=='n'||key=='N')
{
putch(key);
next_line();
return 0;
}
}
}
if (download_addr <0x0200000 && download_addr >= 0x010000)
{
SectorProg(download_addr, (unsigned short *)data_begin, size);
}else if(download_addr <0x04000000 && download_addr >= 0x02000000)
{Nand_Program(download_addr>>14,(unsigned char *)data_begin,size);}
}
int uart_load(int argc, char *argv[])
{
unsigned long count1=0,count2=0,size,i;
unsigned long save_UBRDIV;
unsigned char *pdata;
if(argc<2)
{
download_addr = DFT_DOWNLOAD_ADDR;
printf("No begin address for download, use default address 0x%x\n", download_addr);
}
else
download_addr = strtoul(argv[1]);
if(download_addr >= 0xc000000)
{pdata = (unsigned char *)download_addr;}
else{pdata = (unsigned char *)DFT_DOWNLOAD_ADDR;}
size = strtoul(argv[2]);
for(i=0; i<1000000;i++){}
printf("ch");
for(i=0; i<1000000;i++){}
save_UBRDIV = rUBRDIV0;
rUBRDIV0 = (int)(MCLK/(16.0*115200)+0.5)-1;
for(i=0; i<1000000;i++){}
rI_ISPC = BIT_URXD0;
*pdata = rURXH0;
printf("p");
while(1)
{
if((rINTPND & BIT_URXD0) ==BIT_URXD0)
{
rI_ISPC = BIT_URXD0; //rI_ISPC 是IRQ 中断服务清除寄存器"01e00024", BIT_URXD1 是常数0x40
do
{
*pdata++ = rURXH0;
count1++;
count2++;
}while( (rUFSTAT0&0xf) >0 ); //rUFSTAT 是UART的FIFO STATUS 寄存器
}
if(count2==512)
{
count2 =0;
printf("p");
}
if(count1>=size) break;
}
printf("p");
for(i=0;i<1000000;i++){}
rUBRDIV0 = save_UBRDIV;
for(i=0;i<1000000;i++){}
printf("\nReceived %x Bytes, END...\n", count1);
if (download_addr <0x4000000 && download_addr >= 0x010000)
{
pro_Flash_w(DFT_DOWNLOAD_ADDR,size);
}
return 0;
}
/*********************************************************/
int tftp_main(unsigned long ip, unsigned long addr, int method);
int LoadFile2Mem(int argc, char *argv[])
{
unsigned long size;
if(argc<2)
{
download_addr = DFT_DOWNLOAD_ADDR;
printf("No begin address for download, use default address 0x%x\n", download_addr);
}
else
download_addr = strtoul(argv[1]);
if (download_addr <0x4000000 && download_addr >= 0x010000)
{
size = tftp_main(IP_ADDRESS, DFT_DOWNLOAD_ADDR, 0);
pro_Flash_w(DFT_DOWNLOAD_ADDR,size);
}
else if(download_addr >= 0x0c000000)
tftp_main(IP_ADDRESS, download_addr, 0);
return 0;
}
int ProgFlash(int argc, char *argv[])
{
unsigned int size = ~0;
unsigned int data_begin = ~0;
unsigned int prog_begin = ~0;
int overwrite0 = 1;
int modify_a0 = 0;
unsigned int bios_load_addr;
if(argc<4)
{
puts("Arguments error!\n");
puts("Usage: prog a1 a2 size [-no0]\na1 = program begin address\na2 = data pointer to ram\nsize = program size(Bytes)\n-no0 = don't overwrite address 0\n");
return -1;
}
download_addr = strtoul(argv[1]);
data_begin = strtoul(argv[2]);
size = strtoul(argv[3]);
if(size==0)
{
puts("Write 0 Bytes!\n");
return -1;
}
if (download_addr <0x4000000 && download_addr >= 0x010000)
{
pro_Flash_w(data_begin,size);
}
return 0;
}
int p_boot(unsigned long size)
{
int i;
unsigned int *pboot1=(unsigned int *)DFT_DOWNLOAD_ADDR;
unsigned int *pboot2=(unsigned int *)(DFT_DOWNLOAD_ADDR+SECTOR_SIZE);
if(size==60)
{
FlashRead(0, (unsigned short *)(DFT_DOWNLOAD_ADDR+SECTOR_SIZE), SECTOR_SIZE);
for(i=0 ; i<15; i++)
{
*pboot2++ = *pboot1++;
}
SectorProg(0, (unsigned short *)(DFT_DOWNLOAD_ADDR+SECTOR_SIZE), SECTOR_SIZE);
}
}
int prog_boot(int argc, char *argv[])
{
unsigned long size;
size = tftp_main(IP_ADDRESS, DFT_DOWNLOAD_ADDR, 1);
p_boot(size);
return 0;
}
int prog_uart_boot(int argc, char *argv[])
{
char ch_tmp[3][10];
char *para_ptr[4];
unsigned long count1=0,count2=0,size,i;
unsigned char *pdata;
pdata = (unsigned char *)DFT_DOWNLOAD_ADDR;
size = strtoul(argv[2]);
while(1)
{
if((rINTPND & BIT_URXD0) ==BIT_URXD0)
{
rI_ISPC = BIT_URXD0; //rI_ISPC 是IRQ 中断服务清除寄存器"01e00024", BIT_URXD1 是常数0x40
do
{
*pdata++ = rURXH0;
count1++;
}while( (rUFSTAT0&0xf) >0 ); //rUFSTAT 是UART的FIFO STATUS 寄存器
}
if(count1>=size) break;
}
printf("\nReceived %x Bytes, END...\n", count1);
p_boot(size);
return 0;
}
int RunProgram(int argc, char *argv[])
{
int en_t= 1;
void (*fp)(U32, U32) = (void (*)(U32, U32))DFT_DOWNLOAD_ADDR;
if(argc>1)
{
int p = strtoul(argv[1]);
if(p>0)
fp = (void (*)(U32, U32))p;
}
printf("\nRun program from 0x%x, \n", fp);
(*fp)(0, 178);
return 0;
}
int MoveFlashToRam(int argc, char *argv[])
{
int rd_addr, wr_addr, size;
if(argc<4)
{
puts("Please enter move a1 a2 size\na1 = flash addr, a2 = ram addr\n");
return -1;
}
rd_addr = strtoul(argv[1]);
wr_addr = strtoul(argv[2]);
size = strtoul(argv[3]);
if(rd_addr==-1||wr_addr==-1||size==-1)
{
puts("Parameter erroe\n");
return -1;
}
if (rd_addr <0x04000000 && rd_addr >= 0x02000000)
{
Nand_Read(rd_addr>>14,(U8 *)wr_addr,size);
}
else if(wr_addr <0x02000000 && wr_addr >= 0)
{FlashRead(rd_addr, (U16 *)wr_addr, size);}
return 0;
}
int CopyFlash(int argc, char *argv[])
{
unsigned int src, dst, size;
char tmp[10];
if(argc<4)
{
puts("Usage : copy a1 a2 size\n");
puts("a1 = src address, a2 = dst address, size = copy bytes (all in hex)\n");
return -1;
}
src = strtoul(argv[1]);
dst = strtoul(argv[2]);
size = strtoul(argv[3]);
if(src==-1||dst==-1||size==-1)
{
puts("give error address\n");
return -1;
}
FlashRead(src, (unsigned short *)DFT_DOWNLOAD_ADDR, size);
argv[1] = argv[2];
argv[2] = tmp;
ultostr(tmp, DFT_DOWNLOAD_ADDR);
ProgFlash(4, argv);
return 0;
}
int SetIPAddr(int argc, char *argv[])
{
int i, j, err = 0;
char *str;
int ip[4];
char ch = '.';
if(argc<2)
{
printf("IP address : %u.%u.%u.%u\n", (IP_ADDRESS>>24)&0xff, (IP_ADDRESS>>16)&0xff, (IP_ADDRESS>>8)&0xff, IP_ADDRESS&0xff);
return 0;
}
str = argv[1];
for(j=0; j<4; j++)
{
if(j==3)
ch = 0;
i = 0;
if(str[++i]!=ch)
if(str[++i]!=ch)
if(str[++i]!=ch)
err = 1;
str[i] = 0;
ip[j] = strtodec(str, i);
if(ip[j]<0||ip[j]>254||err)
{
puts("IP address error\n");
return -1;
}
str += i+1;
}
printf("Set IP address : %u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]);
IP_ADDRESS = (ip[0]<<24)|(ip[1]<<16)|(ip[2]<<8)|ip[3];
return 0;
}
int testR232(int argc, char *argv[])
{
printf("RS232 is ok\n");
return 0;
}
/*********************************************************/
typedef int (*cmdproc)(int argc, char *argv[]);
typedef struct {
char *cmd;
char *hlp;
cmdproc proc;
}CMD_STRUC;
CMD_STRUC CMD_INNER[] =
{
//{"help", "show help", Help},
{"testrs232","test R232",testR232},
{"load", "load file to memory", LoadFile2Mem},
{"uart_load", "load file to memory", uart_load},
{"boot", "load boot.bin to flash &0x000", prog_boot},
{"uart_boot", "load boot.bin to flash &0x000", prog_uart_boot},
{"prog", "program flash", ProgFlash},
{"ipcfg", "show or set IP address", SetIPAddr},
{"move", "move data from flash to ram", MoveFlashToRam},
{"run", "run from ram", RunProgram},
{NULL, NULL, NULL}
};
/*********************************************************/
static void ParseArgs(char *cmdline, int *argc, char **argv)
{
#define STATE_WHITESPACE 0
#define STATE_WORD 1
char *c;
int state = STATE_WHITESPACE;
int i;
*argc = 0;
if(strlen(cmdline) == 0)
return;
/* convert all tabs into single spaces */
c = cmdline;
while(*c != '\0')
{
if(*c == '\t')
*c = ' ';
c++;
}
c = cmdline;
i = 0;
/* now find all words on the command line */
while(*c != '\0')
{
if(state == STATE_WHITESPACE)
{
if(*c != ' ')
{
argv[i] = c; //将argv[i]指向c
i++;
state = STATE_WORD;
}
}
else
{ /* state == STATE_WORD */
if(*c == ' ')
{
*c = '\0';
state = STATE_WHITESPACE;
}
}
c++;
}
*argc = i;
#undef STATE_WHITESPACE
#undef STATE_WORD
}
static int GetCmdMatche(char *cmdline)
{
int i;
for(i=0; CMD_INNER[i].cmd!=NULL; i++)
{
if(strncmp(CMD_INNER[i].cmd, cmdline, strlen(CMD_INNER[i].cmd))==0)
return i;
}
return -1;
}
static int ParseCmd(char *cmdline, int cmd_len)
{
int argc, num_commands;
char *argv[MAX_ARGS];
ParseArgs(cmdline, &argc, argv);
/* only whitespace */
if(argc == 0)
return 0;
num_commands = GetCmdMatche(argv[0]);
if(num_commands<0)
return -1;
if(CMD_INNER[num_commands].proc!=NULL)
CMD_INNER[num_commands].proc(argc, argv);
return 0;
}
/*********************************************************/
int pIrqStart = 0;
int pIrqHandler = 0;
int pIrqFinish = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -