1. 因为我的工程用AC6是因为要跑自己的C 和 TensorFlow lite micro. 所以是C,C++混合的工程,但是一直没法打印,所以写一个总结。
基本说明: micro_lib这种情况不要选,因为存在C++文件
第一个坑:
第二个坑:网上大部分教程的重定向printf代码是
__ASM(".global __use_no_semihosting");
//??HAL????,?????????bug
//??????????,???????????????,??
int _ttywrch(int ch)
{
ch=ch;
return ch;
}
//??????????
//struct __FILE
//{
// int handle;
// /* Whatever you require here. If the only file you are using is */
// /* standard output using printf() for debugging, no file handling */
// /* is required. */
//};
FILE __stdout;
//??_sys_exit()??????????
void _sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f){
// usart_data_transmit(USART0, (uint8_t)ch); //????????
// while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));//??????
app_uart_put(ch);
return ch;
}
某些情况下是可以的(我在52832官方demo这么写是可以的,但是我加了tensorflow lite mirco以后,打印不正常)
换一下代码后正常(串口发送哪个,要换成自己的):
__ASM(".global __use_no_semihosting");//_swi
//__ASM(".global _main_redirection");
const char __stdin_name[150];
const char __stdout_name[150];
const char __stderr_name[150];
typedef int FILEHANDLE;
typedef unsigned int clock_t; /* cpu time type */
typedef unsigned int time_t; /* date/time in unix secs past 1-Jan-70 */
extern FILE __stdout;
//???????,??printf?fopen?fclose????????????????????,???????????????,??????????????
void _sys_exit(int status)
{
while(1);
}
FILEHANDLE _sys_open(const char *name, int openmode)
{
return 0;
}
int _sys_close(FILEHANDLE fh)
{
return 0;
}
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
{
return 0;
}
int _sys_read(FILEHANDLE fh, unsigned char*buf, unsigned len, int mode)
{
return 0;
}
int _sys_istty(FILEHANDLE fh)
{
return 0;
}
int _sys_seek(FILEHANDLE fh, long pos)
{
return 0;
}
int _sys_ensure(FILEHANDLE fh)
{
return 0;
}
long _sys_flen(FILEHANDLE fh)
{
return 0;
}
int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
{
return 0;
}
void _ttywrch(int ch)
{
}
time_t time(time_t *t)
{
return 0;
}
int remove(const char *filename)
{
return 0;
}
int rename(const char *oldname, const char *newname)
{
return 0;
}
int system(const char *command)
{
return 0;
}
char *_sys_command_string(char *cmd, int len)
{
return 0;
}
clock_t clock(void)
{
return 0;
}
//???fputc??
int fputc(int ch, FILE *f)
{
app_uart_put((unsigned char)ch);
return ch;
}
第三个,写自己的打印函数;
#include <stdarg.h>
#include <string.h>
#define TX_BUF_LEN 256 /* ???????,???????? */
uint8_t TxBuf[TX_BUF_LEN]; /* ????? */
void MyPrintf(const char *__format, ...)
{
va_list ap;
va_start(ap, __format);
/* ??????? */
memset(TxBuf, 0x0, TX_BUF_LEN);
/* ??????? */
vsnprintf((char*)TxBuf, TX_BUF_LEN, (const char *)__format, ap);
va_end(ap);
int len = strlen((const char*)TxBuf);
/* ??????? */
for (int i = 0; i < len; i++)
{
app_uart_put( TxBuf[i]);
}
}