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

📄 shell.c.g

📁 思创S3C44B0黄金开发板一个简化版本的BOOTLOADER
💻 G
字号:
#include <string.h>
#include "common.h"

#define Sequential_Data_Input_cmd  0x80
#define Read1_cmd    0x00
#define Read2_cmd    0x50
#define ReadID_cmd   0x90
#define Reset_cmd    0xff
#define Page_Program_cmd  0x10
#define Block_Erase_cmd   0x60
#define Block_Erase_Confirm_cmd  0xd0
#define Read_Status_cmd   0x70

typedef unsigned char uint8;
typedef unsigned char uchar;
//typedef unsigned int  uint;
typedef unsigned long ulong;

#define TRUE   1
#define FALSE  0

#define NAND_BUSY    (*((volatile unsigned short*)0x1d20014)&0x0100?1:0)
#define NAND_CE(x)   (x?(*((volatile unsigned short*)0x1d2000c)|=0x0200):(*((volatile unsigned short*)0x1d2000c)&=0xFD00))
#define NAND_SDATA   (*(volatile unsigned char*)0x1d20006=0x00)
#define NAND_SADDR   (*(volatile unsigned char*)0x1d20006=0x01)
#define NAND_SCMD    (*(volatile unsigned char*)0x1d20006=0x02)
#define NAND_ADDR(x) (*(volatile unsigned char*)0xa000008=(unsigned char)x)
#define NAND_CMD(x)  (*(volatile unsigned char*)0xa000004=(unsigned char)x)
#define NAND_DATA(x) (*(volatile unsigned char*)0xa000000=(unsigned char)x)
#define NAND_RDATA   (*(volatile unsigned char*)0xa000000)


uint xtoi (char *str,int *n)
{
    uint        u;
    char        c;
    char        *old = str;

    while (*str && *str == ' ') {
        str += 1;
    }

    // skip preceeding zeros 
    while (*str && *str == '0') {
        str += 1;
    }

    // skip preceeding white space 
    if (*str && (*str == 'x' || *str == 'X')) {
        str += 1;
    }

    // convert hex digits
    u = 0;
    c = *(str++);
    while (c) {
        if (c >= 'a'  &&  c <= 'f') {
            c -= 'a' - 'A';
        }

        if ((c >= '0'  &&  c <= '9')  ||  (c >= 'A'  &&  c <= 'F')) {
            u = u << 4  |  c - (c >= 'A' ? 'A'-10 : '0');
        } else {
            break;
        }
        c = *(str++);
    }

    *n = str - old;
    return u;
}


int GetLine(char *Str)
{
  char c;
  int  i;
  
  i = 0;
  Output(0,">");
  while (1) {
    do {
      c = Uart_GetKey(0);
    } while (!c) ;
    
    if (c==0x0a || c==0x0d) {
      *(Str+i)=0;
      Output(0,"\n");
      return i;
    }

    if (c==0x7f) {
      if (i>0) {
        Output(0,"\b \b");
        i--;
      }
      continue;
    }    
    Output(0,"%c",c);
    
    *(Str+i) = c; 
    i++;
    if (i>32) {
      Output(0,"\nCommand line is too long!\n");
      return 0;
    }
  }
  return 0;
}

void strlow(char *strin)
{
  char *str = strin;
  while (*str) {
    if (*str<='Z' && *str>='A') {
      *str = *str - 'A' + 'a';
    }
    str++;
  }
}

void WriteMem(uint cmd, uint addr,uint value,int arg)
{
  volatile unsigned char *pb;
  volatile unsigned short *pw;
  volatile unsigned int *pd;
  
  if (cmd==0) {pb = (volatile unsigned char *)addr; *pb =(unsigned char)value;}
  else if (cmd==1) {pw = (volatile unsigned short *)(addr&0xFFFFFFFE); *pw =(unsigned short)value;}
  else if (cmd==2) {pd = (volatile unsigned int *)(addr&0xFFFFFFFC);   *pd =(unsigned int )value;}
  else return;
   
}

void ReadMem(uint cmd, uint addr,uint len,int arg)
{
  volatile unsigned char *pb;
  volatile unsigned short *pw;
  volatile unsigned int *pd;
  uint addrstart,oldaddr,i,j;

  
  if (cmd==0) {pb = (volatile unsigned char *)addr; oldaddr=(uint)pb;}
  else if (cmd==1) {pw = (volatile unsigned short *)(addr&0xFFFFFFFE); oldaddr=(uint)pw;}
  else if (cmd==2) {pd = (volatile unsigned int *)(addr&0xFFFFFFFC); oldaddr=(uint)pd;}
  else return;
  
  addrstart = addr & 0xFFFFFFF0;
  if (arg==1) len = 16;
  Output(0,"%08X: ",addrstart);
  for (i=0;i<len;i+=0) {
    if (cmd == 0) {
      if ((addrstart+i)>=oldaddr) Output(0,"%02X ",*pb++);
      else  {
        Output(0,"   ");
        len+=1;
      }
      j=1;
    }
    else if (cmd == 1) {
      if ((addrstart+i)>=oldaddr) Output(0,"%04X ",*pw++);
      else {
        Output(0,"     ");
        len+=2;
      }
      j=2;
    } else {
      if ((addrstart+i)>=oldaddr) Output(0,"%08X ",*pd++);
      else {
        Output(0,"         ");
        len+=4;
      }
      j=4;
    }
    i+=j;
    addr+=j;
    if (!(i&0xf) && (i<len)) Output(0,"\n%08X: ",addrstart+i); 
  }
  Output(0,"\n");
}


void ledout(uint8 ch, uint8 flag)
{
    char chmod1[]={0x48,0xeb,0x52, 0x62,0xe1,0x64,0x44,0xea,0x40,0x60,0xC0,0x45,0x5c,0x43,0x54,0xd4};
    unsigned short   value;
    volatile short *p = (volatile short *)0x6080000;

    if (flag) {
      value = chmod1[ch/16];
      value <<= 8;
      value |= chmod1[ch%16];
    
    } else {
      if (ch>99) ch = ch % 100;
      value = chmod1[ch/10];
      value <<= 8;
      value |= chmod1[ch%10];
    }

    *p = value;
}

uint    StatusCheck()                              //status check
{
	uint i;
	uchar status;

  NAND_CE(0);
  //NAND_SCMD;
	NAND_CMD(0x90);
	//NAND_SDATA;
	//NAND_SADDR;
  NAND_ADDR(0x00);
  //NAND_ADDR(0x00);
  //NAND_ADDR(0x00);
  //NAND_SDATA;
  Delay(5);
  Output(0,"%x  ",NAND_RDATA);
  Output(0,"%x\n",NAND_RDATA);
  Delay(25);
  
NAND_CE(0);          
}


int NandTest(void)
{
    NAND_CE(0);
    while(1) {
      StatusCheck();
    }
    NAND_CE(1);
  
  return 1;
}


int ParseCommand(char *Str)
{
  int  cmd,n,arg;
  uint addr,len;
  
  while (*Str==' ' || *Str=='\t')  Str++;
  strlow(Str);
  if (!strncmp(Str,"rb",2)) {Str+=2;cmd=0;}
  else if (!strncmp(Str,"rw",2)) {Str+=2;cmd=1;}
  else if (!strncmp(Str,"rd",2)) {Str+=2;cmd=2;}
  else if (!strncmp(Str,"wb",2)) {Str+=2;cmd=3;}
  else if (!strncmp(Str,"ww",2)) {Str+=2;cmd=4;}
  else if (!strncmp(Str,"wd",2)) {Str+=2;cmd=5;}
  else {
    Output(0,"Unknown command!\n");
    return 0;
  }  
  
  if (*Str==0) {
    Output(0,"Arguments are needed!\n");
    return 0;
  }

  if (*Str==' ' || *Str=='\b') {
    while (*Str==' ' || *Str=='\b') Str++;
    addr = xtoi(Str,&n);
    Str+=n;
    if (*Str==' ' || *(Str-1)==' ' ) arg=2;
    else arg=1; 
    len  = xtoi(Str,&n);
    
    switch(cmd) {
      case 0:
      case 1:
      case 2:
        ReadMem(cmd,addr,len,arg);
        break;
      case 3:
      case 4:
      case 5:
        if (arg==1) {
          Output(0,"More arguments are requried!\n");
          return 0;
        }
        WriteMem(cmd-3,addr,len,arg);
        break;
    }

    return 1;
  }
  
  Output(0,"Unknown command!\n");
  return 0;
}

void Shell()
{
  char Buf[32];
  Output(0,"\nWelcome to Shell!\n\n");

  NandTest();
  
  *(volatile unsigned short *)0x1d2000c = 0x5ff;
  while(1) {
    Delay(20);
    WriteMem(1, 0xa000004,0x9090,2);
    WriteMem(1, 0xa000008,0x0000,2);
    
  }

  while(1) {
    if (GetLine(Buf)) {
      ParseCommand(Buf);
    }
  }
}

⌨️ 快捷键说明

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