OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2933|回复: 12

usart.c文件的问题,和论坛之前的人的问题好像不太一样

[复制链接]

28

主题

95

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
228
金钱
228
注册时间
2017-8-1
在线时间
50 小时
发表于 2017-8-26 19:52:45 | 显示全部楼层 |阅读模式
1金钱
提示说在reent.h里定义过了,怎么解决?

HNK39LAR6@62})[S_5UGD}0.png
W5ZKHZN68OO%9VTZI9BJKVD.png
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

10

主题

266

帖子

0

精华

高级会员

Rank: 4

积分
693
金钱
693
注册时间
2017-7-27
在线时间
98 小时
发表于 2017-8-26 20:40:37 | 显示全部楼层
回复

使用道具 举报

28

主题

95

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
228
金钱
228
注册时间
2017-8-1
在线时间
50 小时
 楼主| 发表于 2017-8-26 21:06:29 来自手机 | 显示全部楼层
huanghan 发表于 2017-8-26 20:40
注释掉不用,

注释掉还能输出吗?好像reent.h里定义的FILE和这个不一样啊
回复

使用道具 举报

10

主题

266

帖子

0

精华

高级会员

Rank: 4

积分
693
金钱
693
注册时间
2017-7-27
在线时间
98 小时
发表于 2017-8-26 21:14:28 | 显示全部楼层

这样写是可以的
#if 1
#pragma import(__use_no_semihosting)            
//±ê×¼¿aDèòaμÄÖ§3Öoˉêy                 
//struct __FILE
//{
//        int handle;
//};

//FILE __stdout;      
//¶¨òå_sys_exit()òÔ±üÃaê1óðëÖ÷»úģ꽠  
//_sys_exit(int x)
//{
//        x = x;
//}
//ÖØ¶¨òåfputcoˉêy
int fputc(int ch, FILE *f)
{        
        while((USART3->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï   
        USART3->DR = (u8) ch;      
        return ch;
}
#endif
回复

使用道具 举报

10

主题

266

帖子

0

精华

高级会员

Rank: 4

积分
693
金钱
693
注册时间
2017-7-27
在线时间
98 小时
发表于 2017-8-26 21:18:42 | 显示全部楼层
在如图的地方打钩就可以了
2017-08-26_211711.png
回复

使用道具 举报

28

主题

95

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
228
金钱
228
注册时间
2017-8-1
在线时间
50 小时
 楼主| 发表于 2017-8-26 21:56:01 来自手机 | 显示全部楼层
huanghan 发表于 2017-8-26 21:18
在如图的地方打钩就可以了

我用的不是keil……
回复

使用道具 举报

6

主题

72

帖子

0

精华

初级会员

Rank: 2

积分
198
金钱
198
注册时间
2017-7-25
在线时间
39 小时
发表于 2017-8-26 22:06:22 | 显示全部楼层
reent.h是你自己写的头文件吗,
如果是,那就自己去把那里的头文件修改一下
如果reent.h是什么软件提供的或者其他什么提供的(反正就不是你自己写的话),嘿嘿,我就不知道了
回复

使用道具 举报

28

主题

95

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
228
金钱
228
注册时间
2017-8-1
在线时间
50 小时
 楼主| 发表于 2017-8-26 23:01:07 来自手机 | 显示全部楼层
17602515236 发表于 2017-8-26 22:06
reent.h是你自己写的头文件吗,
如果是,那就自己去把那里的头文件修改一下
如果reent.h是什么软件提供的 ...

不是我自己写的,我也不知道是什么文件
回复

使用道具 举报

28

主题

95

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
228
金钱
228
注册时间
2017-8-1
在线时间
50 小时
 楼主| 发表于 2017-8-26 23:02:31 来自手机 | 显示全部楼层
17602515236 发表于 2017-8-26 22:06
reent.h是你自己写的头文件吗,
如果是,那就自己去把那里的头文件修改一下
如果reent.h是什么软件提供的 ...

好像跟stdio.h有关,因为我把reent.h中的定义FILE部分注释掉后stdio.h就一堆报错
回复

使用道具 举报

28

主题

95

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
228
金钱
228
注册时间
2017-8-1
在线时间
50 小时
 楼主| 发表于 2017-8-27 08:32:15 | 显示全部楼层
@正点原子 原子哥求助,FILE已经定义过了怎么办,我看了一下,在stdio.h里包含了reent.h
_D}2B9]~$QO7TU{D$I23OFU.png
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2017-8-28 01:25:09 | 显示全部楼层
1473421439 发表于 2017-8-27 08:32
@正点原子 原子哥求助,FILE已经定义过了怎么办,我看了一下,在stdio.h里包含了reent.h

这个你只能慢慢试了
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

28

主题

95

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
228
金钱
228
注册时间
2017-8-1
在线时间
50 小时
 楼主| 发表于 2017-8-28 10:50:09 | 显示全部楼层
解决了,只要新建一个printf.c并且添加如下代码,然后注释掉usart.c中重定向printf部分代码,就可以在newlib-nano下使用printf了,不过这样使用printf有个小问题,比如输出ok\r\n,这样串口助手接受的数据没有回车,但是变成\r\nok就可以接手回车了,我也不知道为什么
回复

使用道具 举报

28

主题

95

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
228
金钱
228
注册时间
2017-8-1
在线时间
50 小时
 楼主| 发表于 2017-8-28 10:50:29 | 显示全部楼层
/**************************************************************************//*****
* @file     printf.c
* @brief    Implementation of several stdio.h methods, such as printf(),
*           sprintf() and so on. This reduces the memory footprint of the
*           binary when using those methods, compared to the libc implementation.
********************************************************************************/
#include <stdio.h>
#include <stdarg.h>
#include "usart.h"
/** @addtogroup RetargetPrintf
  * @{
  */

/** @addtogroup printf
  * @{
  */

/**
* @brief  Transmit a char, if you want to use printf(),
*         you need implement this function
*
* @param  pStr        Storage string.
* @param  c    Character to write.
*/
__attribute__((weak)) void PrintChar(char c)
{
        USART_SendData(USART1, (unsigned char)c);
        while (!(USART1->SR&USART_FLAG_TXE))
                ;
}

/** Maximum string size allowed (in bytes). */
#define MAX_STRING_SIZE         100


/** Required for proper compilation. */
struct _reent r = { 0, (FILE *) 0, (FILE *) 1, (FILE *) 0 };
//struct _reent *_impure_ptr = &r;

/**
* @brief  Writes a character inside the given string. Returns 1.
*
* @param  pStr        Storage string.
* @param  c    Character to write.
*/
signed int PutChar(char *pStr, char c)
{
        *pStr = c;
        return 1;
}


/**
* @brief  Writes a string inside the given string.
*
* @param  pStr     Storage string.
* @param  pSource  Source string.
* @return  The size of the written
*/
signed int PutString(char *pStr, const char *pSource)
{
        signed int num = 0;

        while (*pSource != 0) {

                *pStr++ = *pSource++;
                num++;
        }

        return num;
}


/**
* @brief  Writes an unsigned int inside the given string, using the provided fill &
*         width parameters.
*
* @param  pStr  Storage string.
* @param  fill  Fill character.
* @param  width  Minimum integer width.
* @param  value  Integer value.   
*/
signed int PutUnsignedInt(
    char *pStr,
        char fill,
        signed int width,
        unsigned int value)
{
        signed int num = 0;

            /* Take current digit into account when calculating width */
        width--;

            /* Recursively write upper digits */
        if ((value / 10) > 0) {

                num = PutUnsignedInt(pStr, fill, width, value / 10);
                pStr += num;
        }

        /* Write filler characters */
        else {

                while (width > 0) {

                        PutChar(pStr, fill);
                        pStr++;
                        num++;
                        width--;
                }
        }

            /* Write lower digit */
        num += PutChar(pStr, (value % 10) + '0');

        return num;
}


/**
* @brief  Writes a signed int inside the given string, using the provided fill & width
*         parameters.
*
* @param pStr   Storage string.
* @param fill   Fill character.
* @param width  Minimum integer width.
* @param value  Signed integer value.
*/
signed int PutSignedInt(
    char *pStr,
        char fill,
        signed int width,
        signed int value)
{
        signed int num = 0;
        unsigned int absolute;

            /* Compute absolute value */
        if (value < 0) {

                absolute = -value;
        }
        else {

                absolute = value;
        }

            /* Take current digit into account when calculating width */
        width--;

            /* Recursively write upper digits */
        if ((absolute / 10) > 0) {

                if (value < 0) {

                        num = PutSignedInt(pStr, fill, width, -(absolute / 10));
                }
                else {

                        num = PutSignedInt(pStr, fill, width, absolute / 10);
                }
                pStr += num;
        }
        else {

                /* Reserve space for sign */
                if (value < 0) {

                        width--;
                }

                        /* Write filler characters */
                while (width > 0) {

                        PutChar(pStr, fill);
                        pStr++;
                        num++;
                        width--;
                }

                        /* Write sign */
                if (value < 0) {

                        num += PutChar(pStr, '-');
                        pStr++;
                }
        }

            /* Write lower digit */
        num += PutChar(pStr, (absolute % 10) + '0');

        return num;
}


/**
* @brief  Writes an hexadecimal value into a string, using the given fill, width &
*         capital parameters.
*
* @param pStr   Storage string.
* @param fill   Fill character.
* @param width  Minimum integer width.
* @param maj    Indicates if the letters must be printed in lower- or upper-case.
* @param value  Hexadecimal value.
*
* @return  The number of char written
*/
signed int PutHexa(
    char *pStr,
        char fill,
        signed int width,
        unsigned char maj,
        unsigned int value)
{
        signed int num = 0;

            /* Decrement width */
        width--;

            /* Recursively output upper digits */
        if ((value >> 4) > 0) {

                num += PutHexa(pStr, fill, width, maj, value >> 4);
                pStr += num;
        }
        /* Write filler chars */
        else {

                while (width > 0) {

                        PutChar(pStr, fill);
                        pStr++;
                        num++;
                        width--;
                }
        }

            /* Write current digit */
        if ((value & 0xF) < 10) {

                PutChar(pStr, (value & 0xF) + '0');
        }
        else if (maj) {

                PutChar(pStr, (value & 0xF) - 10 + 'A');
        }
        else {

                PutChar(pStr, (value & 0xF) - 10 + 'a');
        }
        num++;

        return num;
}



/* Global Functions ----------------------------------------------------------- */


/**
* @brief  Stores the result of a formatted string into another string. Format
*         arguments are given in a va_list instance.
*
* @param pStr    Destination string.
* @param length  Length of Destination string.
* @param pFormat Format string.
* @param ap      Argument list.
*
* @return  The number of characters written.
*/
signed int vsnprintf(char *pStr, size_t length, const char *pFormat, va_list ap)
{
        char          fill;
        unsigned char width;
        signed int    num = 0;
        signed int    size = 0;

            /* Clear the string */
        if (pStr) {

                *pStr = 0;
        }

            /* Phase string */
        while (*pFormat != 0 && size < length) {

                /* Normal character */
                if (*pFormat != '%') {

                        *pStr++ = *pFormat++;
                        size++;
                }
                /* Escaped '%' */
                else if (*(pFormat + 1) == '%') {

                        *pStr++ = '%';
                        pFormat += 2;
                        size++;
                }
                /* Token delimiter */
                else {

                        fill = ' ';
                        width = 0;
                        pFormat++;

                                    /* Parse filler */
                        if (*pFormat == '0') {

                                fill = '0';
                                pFormat++;
                        }

                                    /* Parse width */
                        while ((*pFormat >= '0') && (*pFormat <= '9')) {

                                width = (width * 10) + *pFormat - '0';
                                pFormat++;
                        }

                                    /* Check if there is enough space */
                        if (size + width > length) {

                                width = length - size;
                        }

                    /* Parse type */
                        switch (*pFormat) {
                        case 'd':
                        case 'i': num = PutSignedInt(pStr, fill, width, va_arg(ap, signed int)); break;
                        case 'u': num = PutUnsignedInt(pStr, fill, width, va_arg(ap, unsigned int)); break;
                        case 'x': num = PutHexa(pStr, fill, width, 0, va_arg(ap, unsigned int)); break;
                        case 'X': num = PutHexa(pStr, fill, width, 1, va_arg(ap, unsigned int)); break;
                        case 's': num = PutString(pStr, va_arg(ap, char *)); break;
                        case 'c': num = PutChar(pStr, va_arg(ap, unsigned int)); break;
                        default:
                                return EOF;
                        }

                        pFormat++;
                        pStr += num;
                        size += num;
                }
        }

            /* NULL-terminated (final \0 is not counted) */
        if (size < length) {

                *pStr = 0;
        }
        else {

                *(--pStr) = 0;
                size--;
        }

        return size;
}


/**
* @brief  Stores the result of a formatted string into another string. Format
*         arguments are given in a va_list instance.
*
* @param pStr    Destination string.
* @param length  Length of Destination string.
* @param pFormat Format string.
* @param ...     Other arguments
*
* @return  The number of characters written.
*/
signed int snprintf(char *pString, size_t length, const char *pFormat, ...)
{
        va_list    ap;
        signed int rc;

        va_start(ap, pFormat);
        rc = vsnprintf(pString, length, pFormat, ap);
        va_end(ap);

        return rc;
}


/**
* @brief  Stores the result of a formatted string into another string. Format
*         arguments are given in a va_list instance.
*
* @param pString  Destination string.
* @param length   Length of Destination string.
* @param pFormat  Format string.
* @param ap       Argument list.
*
* @return  The number of characters written.
*/
signed int vsprintf(char *pString, const char *pFormat, va_list ap)
{
        return vsnprintf(pString, MAX_STRING_SIZE, pFormat, ap);
}

/**
* @brief  Outputs a formatted string on the given stream. Format arguments are given
*         in a va_list instance.
*
* @param pStream  Output stream.
* @param pFormat  Format string
* @param ap       Argument list.
*/
signed int vfprintf(FILE *pStream, const char *pFormat, va_list ap)
{
        char pStr[MAX_STRING_SIZE];
        char pError[] = "stdio.c: increase MAX_STRING_SIZE\n\r";

            /* Write formatted string in buffer */
        if (vsprintf(pStr, pFormat, ap) >= MAX_STRING_SIZE) {

                fputs(pError, stderr);
                while (1)
                        ; /* Increase MAX_STRING_SIZE */
        }

            /* Display string */
        return fputs(pStr, pStream);
}


/**
* @brief  Outputs a formatted string on the DBGU stream. Format arguments are given
*         in a va_list instance.
*
* @param pFormat  Format string.
* @param ap  Argument list.
*/
signed int vprintf(const char *pFormat, va_list ap)
{
        return vfprintf(stdout, pFormat, ap);
}


/**
* @brief  Outputs a formatted string on the given stream, using a variable
*         number of arguments.
*
* @param pStream  Output stream.
* @param pFormat  Format string.
*/
signed int fprintf(FILE *pStream, const char *pFormat, ...)
{
        va_list ap;
        signed int result;

            /* Forward call to vfprintf */
        va_start(ap, pFormat);
        result = vfprintf(pStream, pFormat, ap);
        va_end(ap);

        return result;
}


/**
* @brief  Outputs a formatted string on the DBGU stream, using a variable number of
*         arguments.
*
* @param  pFormat  Format string.
*/
signed int printf(const char *pFormat, ...)
{
        va_list ap;
        signed int result;

            /* Forward call to vprintf */
        va_start(ap, pFormat);
        result = vprintf(pFormat, ap);
        va_end(ap);

        return result;
}


/**
* @brief  Writes a formatted string inside another string.
*
* @param pStr     torage string.
* @param pFormat  Format string.
*/
signed int sprintf(char *pStr, const char *pFormat, ...)
{
        va_list ap;
        signed int result;

            // Forward call to vsprintf
        va_start(ap, pFormat);
        result = vsprintf(pStr, pFormat, ap);
        va_end(ap);

        return result;
}


/**
* @brief  Outputs a string on stdout.
*
* @param pStr  String to output.
*/
signed int puts(const char *pStr)
{
        return fputs(pStr, stdout);
}


/**
* @brief  Implementation of fputc using the DBGU as the standard output. Required
*         for printf().
*
* @param c        Character to write.
* @param pStream  Output stream.
* @param The character written if successful, or -1 if the output stream is
*        not stdout or stderr.
*/
signed int fputc(signed int c, FILE *pStream)
{
        if ((pStream == stdout) || (pStream == stderr)) {

                PrintChar(c);

                return c;
        }
        else {

                return EOF;
        }
}


/**
* @brief  Implementation of fputs using the DBGU as the standard output. Required
*         for printf().
*
* @param pStr     String to write.
* @param pStream  Output stream.
*
* @return  Number of characters written if successful, or -1 if the output
*          stream is not stdout or stderr.
*/
signed int fputs(const char *pStr, FILE *pStream)
{
        signed int num = 0;

        while (*pStr != 0) {

                if (fputc(*pStr, pStream) == -1) {

                        return -1;
                }
                num++;
                pStr++;
        }

        return num;
}
/**
  * @}
  */
/**
  * @}
  */
/* --------------------------------- End Of File ------------------------------ */

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-20 07:45

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表