284 lines
7.3 KiB
C
284 lines
7.3 KiB
C
|
/***************************************************************
|
|||
|
*Copyright (C), 2017, Shanghai Eastsoft Microelectronics Co., Ltd
|
|||
|
*<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> lib_printf.c
|
|||
|
*<EFBFBD><EFBFBD> <EFBFBD>ߣ<EFBFBD> Liut
|
|||
|
*<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD> V1.00
|
|||
|
*<EFBFBD><EFBFBD> <EFBFBD>ڣ<EFBFBD> 2017/07/14
|
|||
|
*<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>ӡ<EFBFBD>⺯<EFBFBD><EFBFBD>
|
|||
|
*<EFBFBD><EFBFBD> ע<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ES8P508xоƬ
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧϰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾʹ<EFBFBD>ã<EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD>κη<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ρ<EFBFBD>
|
|||
|
***************************************************************/
|
|||
|
#include "lib_printf.h"
|
|||
|
#include <stdarg.h>
|
|||
|
|
|||
|
/***************************************************************
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>fputc
|
|||
|
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD>c<EFBFBD>⺯<EFBFBD><EFBFBD>printf<EFBFBD><EFBFBD>UART
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
***************************************************************/
|
|||
|
/*ʹ<><CAB9>printf()<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>⣺Use MicroLIB*/
|
|||
|
int fputc(int ch, FILE *f)
|
|||
|
{
|
|||
|
FlagStatus Status = RESET;
|
|||
|
uint32_t Count=0;
|
|||
|
|
|||
|
#if defined __PRINTF_USE_UART3__
|
|||
|
UART_SendByte(UART3, (unsigned char) ch);
|
|||
|
do
|
|||
|
{
|
|||
|
Status = UART_GetFlagStatus(UART3,UART_FLAG_TXIDLE);
|
|||
|
Count++;
|
|||
|
}while((Status == RESET)&&(Count != 0x1CE2));
|
|||
|
|
|||
|
if(Count == 0x1CE2)
|
|||
|
return (EOF);
|
|||
|
|
|||
|
#elif defined __PRINTF_USE_UART2__
|
|||
|
UART_SendByte(UART2, (unsigned char) ch);
|
|||
|
do
|
|||
|
{
|
|||
|
Status = UART_GetFlagStatus(UART2,UART_FLAG_TXIDLE);
|
|||
|
Count++;
|
|||
|
}while((Status == RESET)&&(Count != 0x1CE2));
|
|||
|
|
|||
|
if(Count == 0x1CE2)
|
|||
|
return (EOF);
|
|||
|
|
|||
|
#elif defined __PRINTF_USE_UART1__
|
|||
|
UART_SendByte(UART1, (unsigned char) ch);
|
|||
|
do
|
|||
|
{
|
|||
|
Status = UART_GetFlagStatus(UART1,UART_FLAG_TXIDLE);
|
|||
|
Count++;
|
|||
|
}while((Status == RESET)&&(Count != 0x1CE2));
|
|||
|
|
|||
|
if(Count == 0x1CE2)
|
|||
|
return (EOF);
|
|||
|
|
|||
|
#else
|
|||
|
UART_SendByte(UART0, (unsigned char) ch);
|
|||
|
do
|
|||
|
{
|
|||
|
Status = UART_GetFlagStatus(UART0,UART_FLAG_TXIDLE);
|
|||
|
Count++;
|
|||
|
}while((Status == RESET)&&(Count != 0x1CE2));
|
|||
|
|
|||
|
if(Count == 0x1CE2)
|
|||
|
return (EOF);
|
|||
|
|
|||
|
#endif
|
|||
|
return (ch);
|
|||
|
}
|
|||
|
|
|||
|
#ifdef __clang__ //<2F><>ʹ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD>idesigner<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><F2B2BBB5><EFBFBD><EFBFBD><CEA2>
|
|||
|
/***************************************************************
|
|||
|
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD> -radix =10 <EFBFBD><EFBFBD>ʾ10<EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0
|
|||
|
* -value Ҫת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* -buf ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* -radix = 10
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
***************************************************************/
|
|||
|
static char *itoa(int value, char *string, int radix)
|
|||
|
{
|
|||
|
int i, d;
|
|||
|
int flag = 0;
|
|||
|
char *ptr = string;
|
|||
|
|
|||
|
/* This implementation only works for decimal numbers. */
|
|||
|
if (radix != 10)
|
|||
|
{
|
|||
|
*ptr = 0;
|
|||
|
return string;
|
|||
|
}
|
|||
|
|
|||
|
if (!value)
|
|||
|
{
|
|||
|
*ptr++ = 0x30;
|
|||
|
*ptr = 0;
|
|||
|
return string;
|
|||
|
}
|
|||
|
|
|||
|
/* if this is a negative value insert the minus sign. */
|
|||
|
if (value < 0)
|
|||
|
{
|
|||
|
*ptr++ = '-';
|
|||
|
|
|||
|
/* Make the value positive. */
|
|||
|
value *= -1;
|
|||
|
}
|
|||
|
|
|||
|
for (i = 10000; i > 0; i /= 10)
|
|||
|
{
|
|||
|
d = value / i;
|
|||
|
|
|||
|
if (d || flag)
|
|||
|
{
|
|||
|
*ptr++ = (char)(d + 0x30);
|
|||
|
value -= (d * i);
|
|||
|
flag = 1;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* Null terminate the string. */
|
|||
|
*ptr = 0;
|
|||
|
|
|||
|
return string;
|
|||
|
}
|
|||
|
/***************************************************************
|
|||
|
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>printf<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>C<EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD> -UARTx <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* -Data Ҫ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
* -... <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ã<EFBFBD> UART_printf("\r\n this is a demo \r\n" );
|
|||
|
* UART1_printf("\r\n %d \r\n", i );
|
|||
|
* UART1_printf("\r\n %s \r\n", j );
|
|||
|
***************************************************************/
|
|||
|
/* δ<><CEB4><EFBFBD><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ô˺<C3B4><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><43><EFBFBD>е<EFBFBD>printf<74><66><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>printfȫ<66><C8AB>
|
|||
|
ֻ֧<EFBFBD><EFBFBD>\r \n %d %s */
|
|||
|
ErrorStatus UART_printf(uint8_t *Data,...)
|
|||
|
{
|
|||
|
UART_TypeDef *UARTx;
|
|||
|
const char *s;
|
|||
|
int d;
|
|||
|
char buf[16];
|
|||
|
ErrorStatus RET = SUCCESS;
|
|||
|
FlagStatus Status = RESET;
|
|||
|
uint32_t Count=0;
|
|||
|
va_list ap;
|
|||
|
|
|||
|
/**
|
|||
|
**
|
|||
|
**ʹ<EFBFBD>ú궨<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
**
|
|||
|
***/
|
|||
|
#if defined __PRINTF_USE_UART3__
|
|||
|
|
|||
|
UARTx = UART3;
|
|||
|
|
|||
|
#elif defined __PRINTF_USE_UART2__
|
|||
|
|
|||
|
UARTx = UART2;
|
|||
|
|
|||
|
#elif defined __PRINTF_USE_UART1__
|
|||
|
|
|||
|
UARTx = UART1;
|
|||
|
|
|||
|
#else
|
|||
|
|
|||
|
UARTx = UART0;
|
|||
|
|
|||
|
#endif
|
|||
|
|
|||
|
va_start(ap, Data);
|
|||
|
|
|||
|
while ( *Data != 0) // <20>ж<EFBFBD><D0B6>Ƿ<C7B7><F1B5BDB4>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
if ( *Data == 0x5c ) //'\'
|
|||
|
{
|
|||
|
switch ( *++Data )
|
|||
|
{
|
|||
|
case 'r': //<2F>س<EFBFBD><D8B3><EFBFBD>
|
|||
|
Count=0;
|
|||
|
UART_SendByte(UARTx, 0x0d);
|
|||
|
do
|
|||
|
{
|
|||
|
Status = UART_GetFlagStatus(UARTx,UART_FLAG_TXIDLE);
|
|||
|
Count++;
|
|||
|
}while((Status == RESET)&&(Count != 0x1CE2));
|
|||
|
|
|||
|
if(Count == 0x1CE2)
|
|||
|
RET = ERROR;
|
|||
|
|
|||
|
Data ++;
|
|||
|
break;
|
|||
|
|
|||
|
case 'n': //<2F><><EFBFBD>з<EFBFBD>
|
|||
|
Count=0;
|
|||
|
UART_SendByte(UARTx, 0x0a);
|
|||
|
do
|
|||
|
{
|
|||
|
Status = UART_GetFlagStatus(UARTx,UART_FLAG_TXIDLE);
|
|||
|
Count++;
|
|||
|
}while((Status == RESET)&&(Count != 0x1CE2));
|
|||
|
|
|||
|
if(Count == 0x1CE2)
|
|||
|
RET = ERROR;
|
|||
|
|
|||
|
Data ++;
|
|||
|
break;
|
|||
|
|
|||
|
default:
|
|||
|
Data ++;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
else if ( *Data == '%')
|
|||
|
{
|
|||
|
switch ( *++Data )
|
|||
|
{
|
|||
|
case 's': //<2F>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
s = va_arg(ap, const char *);
|
|||
|
for ( ; *s; s++)
|
|||
|
{
|
|||
|
Count=0;
|
|||
|
UART_SendByte(UARTx,*s);
|
|||
|
do
|
|||
|
{
|
|||
|
Status = UART_GetFlagStatus(UARTx,UART_FLAG_TXIDLE);
|
|||
|
Count++;
|
|||
|
}while((Status == RESET)&&(Count != 0x1CE2));
|
|||
|
|
|||
|
if(Count == 0x1CE2)
|
|||
|
RET = ERROR;
|
|||
|
}
|
|||
|
Data++;
|
|||
|
break;
|
|||
|
|
|||
|
case 'd': //ʮ<><CAAE><EFBFBD><EFBFBD>
|
|||
|
d = va_arg(ap, int);
|
|||
|
itoa(d, buf, 10);
|
|||
|
for (s = buf; *s; s++)
|
|||
|
{
|
|||
|
Count=0;
|
|||
|
UART_SendByte(UARTx,*s);
|
|||
|
do
|
|||
|
{
|
|||
|
Status = UART_GetFlagStatus(UARTx,UART_FLAG_TXIDLE);
|
|||
|
Count++;
|
|||
|
}while((Status == RESET)&&(Count != 0x1CE2));
|
|||
|
|
|||
|
if(Count == 0x1CE2)
|
|||
|
RET = ERROR;
|
|||
|
}
|
|||
|
Data++;
|
|||
|
break;
|
|||
|
default:
|
|||
|
Data++;
|
|||
|
break;
|
|||
|
}
|
|||
|
} /* end of else if */
|
|||
|
else
|
|||
|
{
|
|||
|
Count=0;
|
|||
|
UART_SendByte(UARTx, *Data++);
|
|||
|
do
|
|||
|
{
|
|||
|
Status = UART_GetFlagStatus(UARTx,UART_FLAG_TXIDLE);
|
|||
|
Count++;
|
|||
|
}while((Status == RESET)&&(Count != 0x1CE2));
|
|||
|
|
|||
|
if(Count == 0x1CE2)
|
|||
|
RET = ERROR;
|
|||
|
}
|
|||
|
}
|
|||
|
return RET;
|
|||
|
}
|
|||
|
|
|||
|
#endif
|
|||
|
/*************************END OF FILE**********************/
|
|||
|
|