完整工程代码见附件:
替换\STM32Cube_FW_H5_V1.2.0\Projects\NUCLEO-H533RE\Applications\ThreadX\Tx_Thread_Creation
一.前言
前面我们实现了好用的串口读写接口,现在在此基础上我们来移植一个轻量级的标准输入输出函数库,详见微信公众号”嵌入式Lee”的文章:https://mp.weixin.qq.com/s/y4MHV3cd4T0b51L5M4J5Xg,极海半导体APM32F107VC开发板-移植超轻量级标准输入输出函数库xprintf
二.准备
按照公众号文章说明下载xprintf.c/h添加到工程中。
将xprintf.h中所有宏配置为1
三.接口配置
Uart_task.h
中
#include "xprintf.h"
#include "xprintf.h"
static void xprintf_out(int ch)
{
uint8_t tmp = ch;
uart_write(&tmp,1);
}
static int xprintf_in(void)
{
uint8_t tmp;
uint32_t len;
do
{
len = uart_read(&tmp,1);
if(len == 0)
{
tx_thread_sleep(1); /* avoid block low pr thread*/
}
}while(len == 0);
return tmp;
}
static void uart_task_entry(ULONG thread_input)
{
uart_init();
xdev_out(xprintf_out);
xdev_in(xprintf_in);
xprintf("start uart task\r\n");
while(1)
{
uint32_t len;
/* tx */
do
{
__disable_interrupts();
len = fifo_out(&tx_fifo_dev,tx_fifo_buffer,sizeof(tx_fifo_buffer));
__enable_interrupts();
if(len > 0)
{
for(uint32_t i=0 ;i<len; i++)
{
LL_USART_TransmitData8(USART2,tx_fifo_buffer[i]);
while(LL_USART_IsActiveFlag_TC(USART2) == 0);
}
}
}while(len >0 );
tx_thread_sleep(1);
}
}
四.测试
Main_task.c中
#include "xprintf.h"
static void main_task_entry(ULONG thread_input)
{
tx_thread_sleep(100); /* wait uart and xprintf init done */
xprintf("start main task\r\n");
while(1)
{
#if 0
uint32_t len = uart_read(rx_buffer,sizeof(rx_buffer));
if(len > 0)
{
uart_write(rx_buffer,len);
}
//uart_write(tx_buffer,sizeof(tx_buffer));
tx_thread_sleep(1);
#else
xprintf("%d\n", 1234); /* "1234" */
xprintf("%6d,%3d%%\n", -200, 5); /* " -200, 5%" */
xprintf("%-6u\n", 100); /* "100 " */
xprintf("%ld\n", 12345678); /* "12345678" */
xprintf("%llu\n", 0x100000000); /* "4294967296" <XF_USE_LLI> */
xprintf("%lld\n", -1LL); /* "-1" <XF_USE_LLI> */
xprintf("%04x\n", 0xA3); /* "00a3" */
xprintf("%08lX\n", 0x123ABC); /* "00123ABC" */
xprintf("%016b\n", 0x550F); /* "0101010100001111" */
xprintf("%*d\n", 6, 100); /* " 100" */
xprintf("%s\n", "abcdefg"); /* "abcdefg" */
xprintf("%5s\n", "abc"); /* " abc" */
xprintf("%-5s\n", "abc"); /* "abc " */
xprintf("%.5s\n", "abcdefg"); /* "abcde" */
xprintf("%-5.2s\n", "abcdefg"); /* "ab " */
xprintf("%c\n", 'a'); /* "a" */
xprintf("%12f\n", 10.0); /* " 10.000000" <XF_USE_FP> */
xprintf("%.4E\n", 123.45678); /* "1.2346E+02" <XF_USE_FP> */
while(1)
{
char input_buffer[32];
char* str = input_buffer;
xprintf("please input two int\r\n");
xgets(input_buffer, sizeof(input_buffer));
long a;
xatoi(&str,&a);
long b;
xatoi(&str,&b);
xprintf("%d+%d=%d\r\n",a,b,a+b);
}
#endif
}
}
测试如下,输入输出OK
五.总结
以上实现了标准输入输出交互后面就方便进行开发调试了。