stm32f407-openocd/Core/Src/debug.c

171 lines
3.4 KiB
C
Raw Normal View History

2024-06-12 08:32:58 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "debug.h"
/***
* <EFBFBD>Լ<EFBFBD>д<EFBFBD><EFBFBD>printf<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>void Printf(const char *format, ...)
* Ŀǰ֧<EFBFBD><EFBFBD>%d,%c,%s,%f
* */
/**
* @brief uart<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>-interface
*
* @param c <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
*/
void PutChar(char c)
{
extern void putChar(char c);
putChar(c);
}
void printch(const char ch) // <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
{
PutChar(ch);
}
void printint(const int dec) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
// if (dec == 0)
// {
// PutChar('0');
// return;
// }
if (dec == 0)
{
return;
}
printint(dec / 10);
PutChar((char)(dec % 10 + '0'));
}
void printstr(const char *ptr) // <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
{
while (*ptr)
{
PutChar(*ptr);
ptr++;
}
}
static void printBit(const unsigned int dec)
{
if (dec == 0)
{
return;
}
printBit(dec / 2);
PutChar((char)(dec % 2 + '0'));
}
void printfloat(const float flt) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><35><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
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 printX(const unsigned int uint)
{
PutChar('0');
PutChar('x');
for (int i = 7; i >= 0; i--)
{
printint(uint >> (4 * i) & 0xf);
}
}
void Printf(const char *format, ...)
{
va_list ap;
va_start(ap, format); // <20><>apָ<70><D6B8><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ʵ<EFBFBD>ʲ<EFBFBD><CAB2><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
while (*format != '\0')
{
if (*format != '%')
{
PutChar(*format);
format++;
}
else
{
format++;
switch (*format)
{
case 'c':
{
char valch = va_arg(ap, int); // <20><>¼<EFBFBD><C2BC>ǰʵ<C7B0><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ַ
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);
2024-06-13 08:01:47 +00:00
float dot = valflt - (int)valflt;
if(dot == 0){
PutChar('0');
}
2024-06-12 08:32:58 +00:00
format++;
break;
}
case 'b':
{
unsigned int valint = va_arg(ap, unsigned int);
if (valint)
printBit(valint);
else
{
PutChar('0');
}
format++;
break;
}
case 'x':
{
unsigned int valint = va_arg(ap, unsigned int);
printX(valint);
format++;
break;
}
default:
{
printch(*format);
format++;
}
}
}
}
va_end(ap);
}