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

📄 debug.c

📁 16550 uart debug interface.
💻 C
字号:
// **************************************************************************
// *  Copyright (C) L.K, Arista Taiwan                                      *
// *  File Name : debug.c                                                   *
// *  Desc      : modul program                                             *
// *  Designed  : Louis K.                                Date:07-22-2004   *
// **************************************************************************
#include  "ExVar.h"
#include "debug.h"
#include <stdarg.h>
#include <stdio.h>
//***************************************************************************
int Help(void);
void DisableDebug(void);
void Put16550(uchar);

uchar TestBit(uchar,uchar);
//***************************************************************************
#define  MaxRecSize 100
#define  RBR  0x00
#define  THR  0x00
#define  DLL  0x00
#define  DLM  0x01
#define  IER  0x01
#define  FCR  0x02    
#define  ISR  0x02
#define  LCR  0x03
#define  MCR  0x04
#define  LSR  0x05

#define  DebugCard           P34

#define  ClearWatchDog       _WatchDog = 0
//............................................................
uchar xdata _Addr16550[8] _at_  0x8090;
//............................................................
uint     bHide=1;
static   uchar          _scom[90];
static   uint           _debugFuncNum;
static   uint           _pos;
static   uint           _recSize;
static   sDebugRecord   _rec[MaxRecSize];
static   uchar          DebugDisable=0;
//............................................................
extern  int      strcmp(const char *__s1, const char *__s2);
extern  unsigned long    strtoul(const char *__nptr, char **__endptr, int __base);
//---------------------------------------------------------------------------
//Init16550
//---------------------------------------------------------------------------
void Init16550(void)
{ 
   if(DebugCard || DebugDisable) return;
   
  _Addr16550[LCR]=0x80;             //Dlab=1
  _Addr16550[DLM]=0;                //DLM=0
  _Addr16550[DLL]=3;                //DLS=3 => baudrate=38400
  _Addr16550[LCR]=0x03;             //8-bit,none stop-bit
  _Addr16550[IER]=0x03;             //enable rx/tx

}//Init16550

//---------------------------------------------------------------------------
// HeadInfo
//---------------------------------------------------------------------------
int    HeadInfo(void)
{  uchar szDate[]=__DATE__;
   uchar szTime[]=__TIME__;
   if(DebugCard || DebugDisable) return 0;
   
   printf("\n\nCopyRight(c) Aristel Corp, All Right Reserved.");
   printf("\nIBS800 TKU Ver 0.01");
   printf(", Lastest Change at %s, %s\n", szDate, szTime);

   return 0;
}

uchar TestBit(uchar tmp1,uchar tmp2)
{
 tmp1 =tmp1>>tmp2;
 tmp1 &= 0x01;
 return tmp1;
}

// --------------------------------------------------------------------------
// Init16550
// --------------------------------------------------------------------------
void Put16550(uchar tData)
{
 while (!TestBit(_Addr16550[5],5));
 while (!TestBit(_Addr16550[5],6));
 _Addr16550[0]=tData;
}
// --------------------------------------------------------------------------
// Uartprintf()
// --------------------------------------------------------------------------
void   UartPrint(char *pS)
{   while (*pS!=0) {
       if (*pS=='\n') Put16550(0x0D);
       Put16550(*pS);
       pS++;
    }
}
// --------------------------------------------------------------------------
// myprintf()
// --------------------------------------------------------------------------
int myprintf(char *pFormat, ...)
{  static char buf[192];
   va_list     ap;
   int         len;

    if(DebugCard || DebugDisable) return;
    ClearWatchDog;

    va_start(ap, pFormat);
       len=vsprintf(buf, pFormat, ap);
    va_end(ap);

    UartPrint(buf);
    return len;
}
//---------------------------------------------------------------------------
// InstallDebugFunc
//---------------------------------------------------------------------------
void  InstallDebugFunc(char *stgCmd, char *stgNote, VDebugProc *vproc)
{   if(_recSize>=MaxRecSize || DebugCard ||DebugDisable)  return;
    
    _rec[_recSize].stgCmd =stgCmd;
    _rec[_recSize].stgNote=stgNote;
    _rec[_recSize].vproc=vproc;
    _recSize++;
}
//---------------------------------------------------------------------------
// InitDebug
//---------------------------------------------------------------------------
void   InitDebug(void)
{   _pos=0;  _recSize=0;

    if(DebugCard || DebugDisable) return;
    
    InstallDebugFunc("?",    "none",                     Help);
    InstallDebugFunc("dbug",    "none",          DisableDebug);
    _debugFuncNum=_recSize;
}
//---------------------------------------------------------------------------
// Get16550
//---------------------------------------------------------------------------
int    Get16550(uchar *pDat)
{ uchar st;
    st=_Addr16550[LSR]&0x01;
    if(st==0) return 0;

    if(pDat) *pDat=_Addr16550[RBR]; else st=_Addr16550[RBR];
    return 1;
}
//---------------------------------------------------------------------------
// GetCh
//---------------------------------------------------------------------------
char   GetCh(void)
{ uchar dat;
    if (Get16550(&dat)) return dat;
    return 0;
}
//---------------------------------------------------------------------------
// DebugEventHandler
//---------------------------------------------------------------------------
void   DebugEventHandler(int argc, char *argv[])
{  int ii, res;

    for(ii=0; ii<_recSize; ii++) {
       if (strcmp(argv[0], _rec[ii].stgCmd)==0) break;
    }

    if(ii>=_recSize) {
       printf("\n unknown command");
       return;
    }

    res=_rec[ii].vproc(argc, argv);
    if(res==-1) printf("\n parameter # error");
    if(res==-2) printf("\n unknown command");
}
//---------------------------------------------------------------------------
// DebugISR2
//---------------------------------------------------------------------------
void   DebugISR(void)
{  uchar          *argv[10];
   uint           argc;
   uchar          dat;
   uint           ii;

    if(DebugCard || DebugDisable) return;
    
    //......................................................................
    //   Modem State Parsor
    //......................................................................
    dat=(uchar)GetCh();
    if (dat==0) return;    
    //......................................................................
    //   Uart Console
    //......................................................................
    switch (dat) {
      case 0x01:           // up
      case 0x03:           // right
      case 0x04:           // down
        return;
      case 0x02:           // left
      case 0x08:           // backspace
        if (_pos>0) _pos--;
        _scom[_pos]=0x00;
        printf("%c", 0x08);
        return;
      case 0x10:
        printf("\n>");
        return;
      case 0x1b:           // Esc -- cancel command
        _pos=0; _scom[0]=0x00;
        printf("\n>");
        return;
      case 0xA1:           // F1
        return;
      case 0x0D:
        //printf("\n");
        break;
      case 0x0A:
        return;
      default:
        if( (dat>=' ')&&(dat<='~') ) {
           printf("%c", dat);
           _scom[_pos]=dat;
           if (_pos<80) _pos++;
        }
        return;
    }

    ii=0;  _scom[_pos]=0x00; argc=0;
    while(1) {
       while( (_scom[ii]==' ')&&(ii<_pos) ) ii++;
       if(ii>=_pos)  break;
       if(argc>=10)  break;

       argv[argc++]= &_scom[ii];
       while( (_scom[ii]!=' ')&&(ii<_pos) ) ii++;
       _scom[ii]=0x00; ii++;
    }

    if(argc>0) DebugEventHandler(argc, argv);
    _pos=0;  printf("\n>");
}
//***************************************************************************
// Function
//***************************************************************************
int    Help(void)
{ int ii;
    HeadInfo();
    for (ii=0; ii<_recSize; ii++) {         
       if ((bHide)&&(_rec[ii].stgCmd[0]=='@')) continue;
       printf("\n %-10s", _rec[ii].stgCmd, _rec[ii].stgNote);
       printf("%s", _rec[ii].stgNote);
    }                      
    return 0;
}
//---------------------------------------------------------------------------
// DisableDebug
//---------------------------------------------------------------------------
void DisableDebug(void)
{ printf("\n\n     Debug Func. Has Been Disable!!!\n");
  DebugDisable=0xFF;}  


⌨️ 快捷键说明

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