前言
有童鞋說(shuō)在gcc下,vsnprintf來(lái)替代rt_vsnprintf來(lái)打印浮點(diǎn)會(huì)引發(fā)死機(jī)
經(jīng)過(guò)實(shí)際驗(yàn)證,沒(méi)有發(fā)現(xiàn)死機(jī)
但并不建議直接用vsnprintf來(lái)替代rt_vsnprintf,打印浮點(diǎn)多半用于調(diào)試,可以獨(dú)立成一個(gè)專門(mén)的打印函數(shù)
vsnprintf只是用于把浮點(diǎn)格式化到字符串,只要字符串長(zhǎng)度不溢出,理論上不會(huì)造成死機(jī)
如果發(fā)現(xiàn)死機(jī),需要確認(rèn)打印的環(huán)境,如中斷中,尤其在串口輸出的情況下
測(cè)試?yán)?/p>
RT-Thread Studio arm-none-eabi-gcc 環(huán)境:未發(fā)現(xiàn)死機(jī)現(xiàn)象
Keil MDK5 環(huán)境:未發(fā)現(xiàn)死機(jī)現(xiàn)象
cygwin gcc 環(huán)境:未發(fā)現(xiàn)死機(jī)現(xiàn)象
可以正常的用于浮點(diǎn)數(shù)的調(diào)試打印
/*
Copyright (c) 2006-2018, RT-Thread Development Team
SPDX-License-Identifier: Apache-2.0
Change Logs:
Date Author Notes
2018-11-06 SummerGift change to new framework
/
#include
#include
#include
#include "drv_gpio.h"
/ defined the LED0 pin: PA5 /
#define LED0_PIN GET_PIN(A, 5)
#include
#define DBG_BUFF_MAX_LEN 256
/ debug print : support float double */
int dbg_printf(const char fmt, ...)
{
va_list args;
static char rt_log_buf[DBG_BUFF_MAX_LEN] = { 0 };
va_start(args, fmt);
int length = vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
rt_kputs(rt_log_buf);
return length;
}
int main(void)
{
double a = 334455.00123345;
double b = 667788.14234234;
dbg_printf("double a = %lfrn", a);
int count = 1;
/ set LED0 pin mode to output */
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
while (count++)
{
rt_pin_write(LED0_PIN, PIN_HIGH);
dbg_printf("double a + b = %lfrn", a+b+count);
rt_thread_mdelay(1000);
rt_pin_write(LED0_PIN, PIN_LOW);
rt_thread_mdelay(2000);
}
return RT_EOK;
}
cygwin的測(cè)試
/ vsnprintf example /
include
include
define DBG_BUFF_MAX_LEN 256
/ debug print : support float double /
int dbg_printf(const char *fmt, …)
{
va_list args;
static char rt_log_buf[DBG_BUFF_MAX_LEN] = { 0 };
va_start(args, fmt);
int length = vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
printf("%srn", rt_log_buf);
return length;
}
int main ()
{
double a = 123453.0001444;
dbg_printf(“%lf”, a);
return 0;
}
## 輸出效果
```c
msh >
| /
- RT - Thread Operating System
/ | 4.1.0 build Feb 3 2022 21:11:28
2006 - 2022 Copyright by RT-Thread team
double a = 334455.001233
double a + b = 1002245.143576
msh >double a + b = 1002246.143576
double a + b = 1002247.143576
double a + b = 1002248.143576
double a + b = 1002249.143576
double a + b = 1002250.143576
double a + b = 1002251.143576
double a + b = 1002252.143576
double a + b = 1002253.143576
double a + b = 1002254.143576
double a + b = 1002255.143576
double a + b = 1002256.143576
double a + b = 1002257.143576
double a + b = 1002258.143576
double a + b = 1002259.143576
double a + b = 1002260.143576
double a + b = 1002261.143576
cygwin的打?。?$ ./a.exe
123453.000144
小結(jié)
rt_kprintf,采用不依賴標(biāo)準(zhǔn)C庫(kù)的方式,主要目的是為了減少ROM(Flash)的占用
如果實(shí)際中需要浮點(diǎn)打印(如打印到串口、打印到文件),可以基于vsnprintf編寫(xiě)一個(gè)簡(jiǎn)單的全功能格式打印函數(shù)
RT-Thread ulog可以開(kāi)啟浮點(diǎn)支持,開(kāi)啟后使用:vsnprintf
-
ROM
+關(guān)注
關(guān)注
4文章
572瀏覽量
85772 -
串口輸出
+關(guān)注
關(guān)注
0文章
16瀏覽量
7490 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1289瀏覽量
40134 -
Flash單片機(jī)
+關(guān)注
關(guān)注
0文章
111瀏覽量
9402 -
gcc編譯器
+關(guān)注
關(guān)注
0文章
78瀏覽量
3385
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論