lvgl-v8/UART/SimplUART/APP/debug.c

176 lines
3.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "debug.h"
//
// Created by KS00596 on 2022/7/12.
//
/**********************************************************
行注释
内容HAL 接口层
**********************************************************/
static void SerialInit(void)
{
UART_InitStructure UART_initStruct;
PORT_Init(PORTC, PIN2, FUNMUX0_UART0_RXD, 1); // GPIOA.2配置为UART0输入引脚
PORT_Init(PORTC, PIN3, FUNMUX1_UART0_TXD, 0); // GPIOA.3配置为UART0输出引脚
UART_initStruct.Baudrate = 115200;
UART_initStruct.DataBits = UART_DATA_8BIT;
UART_initStruct.Parity = UART_PARITY_NONE;
UART_initStruct.StopBits = UART_STOP_1BIT;
UART_initStruct.RXThreshold = 3;
UART_initStruct.RXThresholdIEn = 1;
UART_initStruct.TXThreshold = 3;
UART_initStruct.TXThresholdIEn = 0;
UART_initStruct.TimeoutTime = 10; //10个字符时间内未接收到新的数据则触发超时中断
UART_initStruct.TimeoutIEn = 1;
UART_Init(UART0, &UART_initStruct);
UART_Open(UART0);
}
static void putChar(char ch)
{
UART_WriteByte(UART0, (u32)ch);
while (UART_IsTXBusy(UART0));
}
void DebugInit(void){
SerialInit();
}
/***
* 自己写的printf函数
* 函数定义void Printf(const char *format, ...)
* 目前支持%d,%c,%s,%f
* */
/**
* @brief uart输出单个字符-interface
*
* @param c 输出字符
*/
void PutChar(char c)
{
// extern void putChar(char c);
putChar(c);
}
void printch(const char ch) //输出字符
{
PutChar(ch);
}
void printint(const int dec) //输出整型数
{
if (dec == 0)
{
return;
}
printint(dec / 10);
PutChar((char)(dec % 10 + '0'));
}
void printstr(const char *ptr) //输出字符串
{
while (*ptr)
{
PutChar(*ptr);
ptr++;
}
}
static void printBit(const int dec)
{
if (dec == 0)
{
return;
}
printBit(dec / 2);
PutChar((char)(dec % 2 + '0'));
}
void printfloat(const float flt) //输出浮点数小数点第5位四舍五入
{
int tmpint = (int)flt;
int tmpflt = (int)(100000 * (flt - tmpint));
if (tmpflt % 10 >= 5)
{
tmpflt = tmpflt / 10 + 1;
}
else
{
tmpflt = tmpflt / 10;
}
printint(tmpint);
PutChar('.');
printint(tmpflt);
}
void Printf(const char *format, ...)
{
va_list ap;
va_start(ap, format); //将ap指向第一个实际参数的地址
while (*format != '\0')
{
if (*format != '%')
{
PutChar(*format);
format++;
}
else
{
format++;
switch (*format)
{
case 'c':
{
char valch = va_arg(ap, int); //记录当前实践参数所在地址
printch(valch);
format++;
break;
}
case 'd':
{
int valint = va_arg(ap, int);
if (valint)
printint(valint);
else
{
PutChar('0');
}
format++;
break;
}
case 's':
{
char *valstr = va_arg(ap, char *);
printstr(valstr);
format++;
break;
}
case 'f':
{
float valflt = va_arg(ap, double);
printfloat(valflt);
format++;
break;
}
case 'b':
{
int valint = va_arg(ap, int);
if (valint)
printBit(valint);
else
{
PutChar('0');
}
format++;
break;
}
default:
{
printch(*format);
format++;
}
}
}
}
va_end(ap);
}