vainest 发表于 2018-6-14 16:11:23

STM8L051片子使用重定向printf函数时总是报错

STM8L051片子使用重定向printf函数时总是报错,没办法引用printf函数,不知道是什么原因,使用的是IAR编译器,总是报内存不足的错误,,求大神指导。。QQ1564135284


黑皮男 发表于 2018-6-14 16:27:16

rom太小了,才8K,太勉强了,还是自己简单搞个字符串输出吧

toofree 发表于 2018-6-14 16:50:47

本帖最后由 toofree 于 2018-6-14 16:51 编辑

FLash太小了,需要 0x3142字节空间,而你的片片只有0x009fff-0x008000=0x2000字节。

vainest 发表于 2018-6-14 17:21:59

toofree 发表于 2018-6-14 16:50
FLash太小了,需要 0x3142字节空间,而你的片片只有0x009fff-0x008000=0x2000字节。

是的,但是我不引用printf函数就不会报错,我的代码量并不大,只有几行,但是一调用printf就报这个错

vainest 发表于 2018-6-14 17:25:04

toofree 发表于 2018-6-14 16:50
FLash太小了,需要 0x3142字节空间,而你的片片只有0x009fff-0x008000=0x2000字节。

我觉得是引用printf函数造成的
#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar(char c)
#define GETCHAR_PROTOTYPE int getchar(void)
#elif defined (_COSMIC_)
#define PUTCHAR_PROTOTYPE char putchar(char c)
#define GETCHAR_PROTOTYPE char getchar(void)
#else /* _IAR_ */
#define PUTCHAR_PROTOTYPE int putchar(int c)
#define GETCHAR_PROTOTYPE int getchar(void)
#endif /* _RAISONANCE_ */


//重定向printf和getchar函数

PUTCHAR_PROTOTYPE
{
/* Write a character to the USART */
USART_SendData8(USART1, c);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)== RESET);
return(c);
}
GETCHAR_PROTOTYPE
{
int c = 0;
/* Loop until the Read data register flag is SET */
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE)== RESET);
c = USART_ReceiveData8(USART1);
return(c);
}
但是我重定向了啊,不知道是什么原因造成不能引用

vainest 发表于 2018-6-14 17:26:47

我觉得是引用printf函数造成的
#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar(char c)
#define GETCHAR_PROTOTYPE int getchar(void)
#elif defined (_COSMIC_)
#define PUTCHAR_PROTOTYPE char putchar(char c)
#define GETCHAR_PROTOTYPE char getchar(void)
#else /* _IAR_ */
#define PUTCHAR_PROTOTYPE int putchar(int c)
#define GETCHAR_PROTOTYPE int getchar(void)
#endif /* _RAISONANCE_ */


//重定向printf和getchar函数

PUTCHAR_PROTOTYPE
{
/* Write a character to the USART */
USART_SendData8(USART1, c);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)== RESET);
return(c);
}
GETCHAR_PROTOTYPE
{
int c = 0;
/* Loop until the Read data register flag is SET */
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE)== RESET);
c = USART_ReceiveData8(USART1);
return(c);
}
但是我重定向了啊,不知道是什么原因造成不能引用

toofree 发表于 2018-6-14 17:32:33

本帖最后由 toofree 于 2018-6-14 17:33 编辑

vainest 发表于 2018-6-14 17:25
我觉得是引用printf函数造成的
#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar(char c)

重定向,不会减小printf的开销,因为printf功能比较多,那怕你用一条printf,它的原函数也一样要全部编译进来。
要想减小空间占用,只能自己写个简单的类似printf的函数。
根本原因是MCU的程序空间太小,你换个大一点的,就能成功。

vainest 发表于 2018-6-14 17:35:13

toofree 发表于 2018-6-14 17:32
重定向,不会减小printf的开销,因为printf功能比较多,那怕你用一条printf,它的原函数也一样要全部编译 ...

http://mp.weixin.qq.com/s?__biz=MzU1MTAwNjA4OQ==&mid=2247484061&idx=1&sn=189cac7a2251de9704ade1dd9796fa6c&chksm=fb96b87bcce1316d047df2cfaa3fb1c27fe0c0279e7abc011333d5ca618a5b5e812b6611fce0&scene=21#wechat_redirect

但是我是按照这个来实现的,为什么他是可以的呢

vainest 发表于 2018-6-14 17:38:46

toofree 发表于 2018-6-14 17:32
重定向,不会减小printf的开销,因为printf功能比较多,那怕你用一条printf,它的原函数也一样要全部编译 ...

https://www.stmcu.org.cn/module/forum/thread-615996-1-1.html
但是我是参照这个例程实现的,为什么他这个可以呢

feixiang20 发表于 2018-6-15 01:02:52

看来你已经参考了【STM8L151在STVD/IAR下重定向printf函数】
还是要检查头文件
另外可参考【STM8S单片机printf函数重定向到串口】【IAR for STM8重定向printf 】
需要配置USART并使能它
页: [1] 2
查看完整版本: STM8L051片子使用重定向printf函数时总是报错