MTK 参考 printf
时间:10-02
整理:3721RD
点击:
hal\peripheral\src\dbgprint.c
void dbg_print(char *fmt,...)
{
#if (!defined(IC_MODULE_TEST))
va_list ap;
double dval;
int ival;
char *p, *sval;
char *bp, cval;
int fract;
unsigned short len;
UART_CTRL_PUT_BYTES_T BMTPutBytes;
UART_CTRL_PUT_UART_BYTE_T data1;
DCL_HANDLE handle;
bp= print_buf;
*bp= 0;
va_start (ap, fmt);
for (p= fmt; *p; p++)
{
if (*p != '%')
{
*bp++= *p;
continue;
}
switch (*++p) {
case 'd':
ival= va_arg(ap, int);
if (ival < 0){
*bp++= '-';
ival= -ival;
}
itoa (&bp, ival, 10);
break;
case 'o':
ival= va_arg(ap, int);
if (ival < 0){
*bp++= '-';
ival= -ival;
}
*bp++= '0';
itoa (&bp, ival, 8);
break;
case 'x':
ival= va_arg(ap, int);
if (ival < 0){
*bp++= '-';
ival= -ival;
}
*bp++= '0';
*bp++= 'x';
itoa (&bp, ival, 16);
break;
case 'c':
cval= va_arg(ap, int);
*bp++= cval;
break;
case 'f':
dval= va_arg(ap, double);
if (dval < 0){
*bp++= '-';
dval= -dval;
}
if (dval >= 1.0)
itoa (&bp, (int)dval, 10);
else
*bp++= '0';
*bp++= '.';
fract= (int)((dval- (double)(int)dval)*(double)(MAXFRACT));
itof(&bp, fract);
break;
case 's':
for (sval = va_arg(ap, char *) ; *sval ; sval++ )
*bp++= *sval;
break;
}
}
*bp= 0;
len = (unsigned short)(bp - print_buf);
handle = DclSerialPort_Open(DBG_PRINT_PORT,0);
if(UARTPort[DBG_PRINT_PORT].power_on == KAL_FALSE)//add liming for( phase out
UART port power on\off in EM)
UART_TurnOnPower(DBG_PRINT_PORT,KAL_TRUE);
#ifdef __DMA_UART_VIRTUAL_FIFO__
for (bp= print_buf; *bp; bp++)
{
//PutUARTByte(DBG_PRINT_PORT,*bp);
data1.uData = *bp;
DclSerialPort_Control(handle,SIO_CMD_PUT_UART_BYTE,
(DCL_CTRL_DATA_T*)&data1);
}
#else
BMTPutBytes.puBuffaddr = (kal_uint8 *)print_buf;
BMTPutBytes.u2Length = len;
DclSerialPort_Control( handle,UART_CMD_BMT_PUT_BYTES,(DCL_CTRL_DATA_T*)
&BMTPutBytes);
// BMT_PutBytes(DBG_PRINT_PORT,(kal_uint8 *)print_buf,len);
#endif
va_end (ap);
#endif
}
void dbg_print(char *fmt,...)
{
#if (!defined(IC_MODULE_TEST))
va_list ap;
double dval;
int ival;
char *p, *sval;
char *bp, cval;
int fract;
unsigned short len;
UART_CTRL_PUT_BYTES_T BMTPutBytes;
UART_CTRL_PUT_UART_BYTE_T data1;
DCL_HANDLE handle;
bp= print_buf;
*bp= 0;
va_start (ap, fmt);
for (p= fmt; *p; p++)
{
if (*p != '%')
{
*bp++= *p;
continue;
}
switch (*++p) {
case 'd':
ival= va_arg(ap, int);
if (ival < 0){
*bp++= '-';
ival= -ival;
}
itoa (&bp, ival, 10);
break;
case 'o':
ival= va_arg(ap, int);
if (ival < 0){
*bp++= '-';
ival= -ival;
}
*bp++= '0';
itoa (&bp, ival, 8);
break;
case 'x':
ival= va_arg(ap, int);
if (ival < 0){
*bp++= '-';
ival= -ival;
}
*bp++= '0';
*bp++= 'x';
itoa (&bp, ival, 16);
break;
case 'c':
cval= va_arg(ap, int);
*bp++= cval;
break;
case 'f':
dval= va_arg(ap, double);
if (dval < 0){
*bp++= '-';
dval= -dval;
}
if (dval >= 1.0)
itoa (&bp, (int)dval, 10);
else
*bp++= '0';
*bp++= '.';
fract= (int)((dval- (double)(int)dval)*(double)(MAXFRACT));
itof(&bp, fract);
break;
case 's':
for (sval = va_arg(ap, char *) ; *sval ; sval++ )
*bp++= *sval;
break;
}
}
*bp= 0;
len = (unsigned short)(bp - print_buf);
handle = DclSerialPort_Open(DBG_PRINT_PORT,0);
if(UARTPort[DBG_PRINT_PORT].power_on == KAL_FALSE)//add liming for( phase out
UART port power on\off in EM)
UART_TurnOnPower(DBG_PRINT_PORT,KAL_TRUE);
#ifdef __DMA_UART_VIRTUAL_FIFO__
for (bp= print_buf; *bp; bp++)
{
//PutUARTByte(DBG_PRINT_PORT,*bp);
data1.uData = *bp;
DclSerialPort_Control(handle,SIO_CMD_PUT_UART_BYTE,
(DCL_CTRL_DATA_T*)&data1);
}
#else
BMTPutBytes.puBuffaddr = (kal_uint8 *)print_buf;
BMTPutBytes.u2Length = len;
DclSerialPort_Control( handle,UART_CMD_BMT_PUT_BYTES,(DCL_CTRL_DATA_T*)
&BMTPutBytes);
// BMT_PutBytes(DBG_PRINT_PORT,(kal_uint8 *)print_buf,len);
#endif
va_end (ap);
#endif
}