查看: 6551|回复: 13

[求助] LPC54628J512怎样实现printf指定串口输出?

[复制链接]
  • TA的每日心情
    奋斗
    2021-2-4 16:14
  • 签到天数: 139 天

    连续签到: 1 天

    [LV.7]常住居民III

    14

    主题

    212

    帖子

    0

    高级会员

    Rank: 4

    积分
    881
    最后登录
    2021-2-4
    发表于 2019-6-3 18:33:26 | 显示全部楼层 |阅读模式
    使用printf函数时,希望从指定串口中输出打印信息,在LPC54628J512中怎样实现(使用官方的LPCXpresso546xx/540xx开发板)?根据以前的经验,需要重写#include <stdio.h>库中的某个函数,可是要怎样实现呢?求告知!万分感谢!

    最佳答案

    l546863256 发表于 2019-6-4 15:50 我试过了,还是不行。printf调到定义处,发现它在stdio.h的头文件中,感觉和fsl_debug_console.c没关系 ... printf是格式化输出函数,的确位于标准库里;但最终会调用putchar函数 ...
    今天天气不错!签到!
    回复

    使用道具 举报

  • TA的每日心情
    郁闷
    2021-8-25 16:50
  • 签到天数: 57 天

    连续签到: 1 天

    [LV.5]常住居民I

    20

    主题

    185

    帖子

    12

    金牌会员

    Rank: 6Rank: 6

    积分
    1288
    最后登录
    2025-5-19
    发表于 2019-6-4 08:39:09 | 显示全部楼层
    1. 在BOARD_InitPins()函数里面改引脚的配置
    2. 在BOARD_InitDebugConsole();里面改你要的 UART base
    3. 调用PRINTF();
    签到
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2021-4-15 09:26
  • 签到天数: 98 天

    连续签到: 1 天

    [LV.6]常住居民II

    14

    主题

    187

    帖子

    2

    高级会员

    Rank: 4

    积分
    623
    最后登录
    2021-6-15
    发表于 2019-6-4 08:51:19 | 显示全部楼层
    在fsl_debug_console.c文件里,修改下面这个函数,在“send data”处,添加你自己的串口输出函数,指定输出到某个串口,ps:相应的串口初始化已经完成
    int __attribute__((weak)) _write(int handle, char *buffer, int size)
    {
        if (buffer == 0)
        {
            /* return -1 if error. */
            return -1;
        }

        /* This function only writes to "standard out" and "standard err" for all other file handles it returns failure. */
        if ((handle != 1) && (handle != 2))
        {
            return -1;
        }

    //    /* Do nothing if the debug UART is not initialized. */
    //    if (s_debugConsole.type == DEBUG_CONSOLE_DEVICE_TYPE_NONE)
    //    {
    //        return -1;
    //    }

        /* Send data. */
        //s_debugConsole.ops.tx_union.PutChar(s_debugConsole.base, (uint8_t *)buffer, size);
    此处添加串口输出函数
        return size;
    }
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2021-2-4 16:14
  • 签到天数: 139 天

    连续签到: 1 天

    [LV.7]常住居民III

    14

    主题

    212

    帖子

    0

    高级会员

    Rank: 4

    积分
    881
    最后登录
    2021-2-4
     楼主| 发表于 2019-6-4 10:09:42 | 显示全部楼层
    不言而喻hgj 发表于 2019-6-4 08:39
    1. 在BOARD_InitPins()函数里面改引脚的配置
    2. 在BOARD_InitDebugConsole();里面改你要的 UART base
    3. 调 ...

    你好,我使用官方例程lpcxpresso54628_usart_polling,环境:LPCXpresso546xx/540xx开发板,默认配置就是USART0,直接调用PRINTF函数,在串口0上没有PRINTF数据打出来(使用USB转串口接到电脑上查看),可以看到USART_WriteBlocking函数打出来的数据。请问哪里有问题?下面是代码:

    #include "board.h"
    #include "fsl_usart.h"
    #include "pin_mux.h"
    #include <stdbool.h>

    #include "fsl_debug_console.h"
    uint8_t txbuff[] = "Usart polling example\r\nBoard will send back received characters\r\n";
    void delay(void)
    {
        volatile uint32_t i = 0;
        for (i = 0; i < 5000000; ++i)
        {
            __asm("NOP"); /* delay */
        }
    }
    int main(void)
    {
    //   uint8_t ch;
        usart_config_t config;

        /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
        CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);

        BOARD_InitPins();
        BOARD_BootClockPLL220M();
        BOARD_InitDebugConsole();
        /*
         * config.baudRate_Bps = 115200U;
         * config.parityMode = kUSART_ParityDisabled;
         * config.stopBitCount = kUSART_OneStopBit;
         * config.loopback = false;
         * config.enableTx = false;
         * config.enableRx = false;
         */
        USART_GetDefaultConfig(&config);
        config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
        config.enableTx = true;
        config.enableRx = true;

        USART_Init(DEMO_USART, &config, DEMO_USART_CLK_FREQ);

        USART_WriteBlocking(DEMO_USART, txbuff, sizeof(txbuff) - 1);
        PRINTF("Hello UART0 1111111\r\n");
        while (1)
        {
            delay();
            PRINTF("Hello UART0\r\n");
        }
    }

    void BOARD_InitPins(void)
    {
        /* Enables the clock for the IOCON block. 0 = Disable; 1 = Enable.: 0x01u */
        CLOCK_EnableClock(kCLOCK_Iocon);

        const uint32_t port0_pin29_config = (/* Pin is configured as FC0_RXD_SDA_MOSI */
                                             IOCON_PIO_FUNC1 |
                                             /* No addition pin function */
                                             IOCON_PIO_MODE_INACT |
                                             /* Input function is not inverted */
                                             IOCON_PIO_INV_DI |
                                             /* Enables digital function */
                                             IOCON_PIO_DIGITAL_EN |
                                             /* Input filter disabled */
                                             IOCON_PIO_INPFILT_OFF |
                                             /* Standard mode, output slew rate control is enabled */
                                             IOCON_PIO_SLEW_STANDARD |
                                             /* Open drain is disabled */
                                             IOCON_PIO_OPENDRAIN_DI);
        /* PORT0 PIN29 (coords: B13) is configured as FC0_RXD_SDA_MOSI */
        IOCON_PinMuxSet(IOCON, 0U, 29U, port0_pin29_config);

        const uint32_t port0_pin30_config = (/* Pin is configured as FC0_TXD_SCL_MISO */
                                             IOCON_PIO_FUNC1 |
                                             /* No addition pin function */
                                             IOCON_PIO_MODE_INACT |
                                             /* Input function is not inverted */
                                             IOCON_PIO_INV_DI |
                                             /* Enables digital function */
                                             IOCON_PIO_DIGITAL_EN |
                                             /* Input filter disabled */
                                             IOCON_PIO_INPFILT_OFF |
                                             /* Standard mode, output slew rate control is enabled */
                                             IOCON_PIO_SLEW_STANDARD |
                                             /* Open drain is disabled */
                                             IOCON_PIO_OPENDRAIN_DI);
        /* PORT0 PIN30 (coords: A2) is configured as FC0_TXD_SCL_MISO */
        IOCON_PinMuxSet(IOCON, 0U, 30U, port0_pin30_config);
    }

    status_t BOARD_InitDebugConsole(void)
    {
        status_t result;
        /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
        CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
        RESET_PeripheralReset(BOARD_DEBUG_UART_RST);
    /*
    #define BOARD_DEBUG_UART_INSTANCE 0U
    #define BOARD_DEBUG_UART_BAUDRATE 115200
    #define BOARD_DEBUG_UART_TYPE kSerialPort_Uart

    */

        result = DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE,
                                 BOARD_DEBUG_UART_CLK_FREQ);

        assert(kStatus_Success == result);
        return result;
    }

    今天天气不错!签到!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2021-2-4 16:14
  • 签到天数: 139 天

    连续签到: 1 天

    [LV.7]常住居民III

    14

    主题

    212

    帖子

    0

    高级会员

    Rank: 4

    积分
    881
    最后登录
    2021-2-4
     楼主| 发表于 2019-6-4 10:50:40 | 显示全部楼层
    缘来有你 发表于 2019-6-4 08:51
    在fsl_debug_console.c文件里,修改下面这个函数,在“send data”处,添加你自己的串口输出函数,指定输出 ...

    你好,我按照你说的方法试了一下,调用PRINTF函数还是无数据从我指定的串口0打印出来,直接使用库函数USART_WriteBlocking有数据从串口0打印出来。我按照你说的方法,修改如下:

    int __attribute__((weak)) _write(int handle, char *buffer, int size)
    {
        if (buffer == 0)
        {
            /* return -1 if error. */
            return -1;
        }

        /* This function only writes to "standard out" and "standard err" for all other file handles it returns failure. */
        if ((handle != 1) && (handle != 2))
        {
            return -1;
        }

        /* Send data. */
        //DbgConsole_SendData((uint8_t *)buffer, size);
    //调用fsl_usart.c里面提供的函数,指定串口0
        USART_WriteBlocking(USART0, (uint8_t *)buffer, size);
        return size;
    }
    使用的官方例程lpcxpresso54628_usart_polling,环境:LPCxpresso546xx/540xx开发板,MCUXpresso IDE,
    主函数:

    #define DEMO_USART USART0
    uint8_t txbuff[] = "Usart polling example\r\nBoard will send back received characters\r\n";

    void delay(void)
    {
        volatile uint32_t i = 0;
        for (i = 0; i < 5000000; ++i)
        {
            __asm("NOP"); /* delay */
        }
    }
    int main(void)
    {
    //   uint8_t ch;
        usart_config_t config;

        /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
        CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);

        BOARD_InitPins();
        BOARD_BootClockPLL220M();
        BOARD_InitDebugConsole();
        /*
         * config.baudRate_Bps = 115200U;
         * config.parityMode = kUSART_ParityDisabled;
         * config.stopBitCount = kUSART_OneStopBit;
         * config.loopback = false;
         * config.enableTx = false;
         * config.enableRx = false;
         */
        USART_GetDefaultConfig(&config);
        config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
        config.enableTx = true;
        config.enableRx = true;

        USART_Init(DEMO_USART, &config, DEMO_USART_CLK_FREQ);

        USART_WriteBlocking(DEMO_USART, txbuff, sizeof(txbuff) - 1);
        PRINTF("Hello UART0 1111111\r\n");
        while (1)
        {
    //        USART_ReadBlocking(DEMO_USART, &ch, 1);
    //        USART_WriteBlocking(DEMO_USART, &ch, 1);
    //            USART_WriteBlocking(DEMO_USART, txbuff, sizeof(txbuff) - 1);
            delay();
            PRINTF("Hello UART0\r\n");
        }
    }
    今天天气不错!签到!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2021-4-15 09:26
  • 签到天数: 98 天

    连续签到: 1 天

    [LV.6]常住居民II

    14

    主题

    187

    帖子

    2

    高级会员

    Rank: 4

    积分
    623
    最后登录
    2021-6-15
    发表于 2019-6-4 12:45:03 | 显示全部楼层
    l546863256 发表于 2019-6-4 10:50
    你好,我按照你说的方法试了一下,调用PRINTF函数还是无数据从我指定的串口0打印出来,直接使用库函数USA ...

    把PRINTF改成printf,或者在文件fsl_debug_console.h里,修正PRINTF的宏定义
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2021-2-4 16:14
  • 签到天数: 139 天

    连续签到: 1 天

    [LV.7]常住居民III

    14

    主题

    212

    帖子

    0

    高级会员

    Rank: 4

    积分
    881
    最后登录
    2021-2-4
     楼主| 发表于 2019-6-4 14:13:47 | 显示全部楼层
    缘来有你 发表于 2019-6-4 12:45
    把PRINTF改成printf,或者在文件fsl_debug_console.h里,修正PRINTF的宏定义

    在fsl_debug_console.h头文件中,发现这段注释:
    /*! @brief Definition to select redirect toolchain printf, scanf to uart or not.
    *
    *  if SDK_DEBUGCONSOLE defined to 0,it represents select toolchain printf, scanf.
    *  if SDK_DEBUGCONSOLE defined to 1,it represents select SDK version printf, scanf.
    *  if SDK_DEBUGCONSOLE defined to 2,it represents disable debugconsole function.
    */
    所以我试着定义一个SDK_DEBUGCONSOLE,当我定义为1时,不用修改任何函数,调用大写的PRINTF函数,串口有数据,MCUXpresso IDE显示的是小写的printf函数的内容;定义为2时,关闭所以打印语句;定义为0时,MCUXpresso IDE可以显示两个printf或者PRINTF函数的打印,但是串口无数据。跳到两个函数定义处,发现PRINTF会跳到fsl_debug_console.h里面,printf函数会跳到stdio.h里面。
    目前调试可以在fsl_debug_console.h头文件定义#define SDK_DEBUGCONSOLE 1U,并调用大写PRINTF函数。小写的printf函数实现求大神!
    今天天气不错!签到!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2021-4-15 09:26
  • 签到天数: 98 天

    连续签到: 1 天

    [LV.6]常住居民II

    14

    主题

    187

    帖子

    2

    高级会员

    Rank: 4

    积分
    623
    最后登录
    2021-6-15
    发表于 2019-6-4 14:28:55 | 显示全部楼层
    l546863256 发表于 2019-6-4 14:13
    在fsl_debug_console.h头文件中,发现这段注释:
    /*! @brief Definition to select redirect toolchain p ...

    不是很理解你说的“小写的printf函数实现”是什么意思?
    #if SDK_DEBUGCONSOLE /* Select printf, scanf, putchar, getchar of SDK version. */
    //#define PRINTF DbgConsole_Printf
    #define PRINTF printf
    #define SCANF DbgConsole_Scanf
    #define PUTCHAR DbgConsole_Putchar
    #define GETCHAR DbgConsole_Getchar
    #else /* Select printf, scanf, putchar, getchar of toolchain. */
    #define PRINTF printf
    #define SCANF scanf
    #define PUTCHAR putchar
    #define GETCHAR getchar
    #endif /* SDK_DEBUGCONSOLE */

    大写的PRINTF只是被宏定义替换成了“DbgConsole_Printf”或者“printf”,参照上面的宏定义修正,就能保证大写的PRINTF被替换成小写的printf
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2021-2-4 16:14
  • 签到天数: 139 天

    连续签到: 1 天

    [LV.7]常住居民III

    14

    主题

    212

    帖子

    0

    高级会员

    Rank: 4

    积分
    881
    最后登录
    2021-2-4
     楼主| 发表于 2019-6-4 14:46:15 | 显示全部楼层
    缘来有你 发表于 2019-6-4 14:28
    不是很理解你说的“小写的printf函数实现”是什么意思?
    #if SDK_DEBUGCONSOLE /* Select printf, scanf, ...

    我这样试过,发现用大写PRINTF替换成小写printf之后两个都没打印信息,在fsl_debug_console.h的头文件中,
    我定义#define SDK_DEBUGCONSOLE 1U

    /*! @brief Definition select redirect toolchain printf, scanf to uart or not. */
    #define DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN 0U /*!< Select toolchain printf and scanf. */
    #define DEBUGCONSOLE_REDIRECT_TO_SDK 1U       /*!< Select SDK version printf, scanf. */
    #define DEBUGCONSOLE_DISABLE 2U               /*!< Disable debugconsole function. */


    /*! @brief Definition to select redirect toolchain printf, scanf to uart or not.
    *
    *  if SDK_DEBUGCONSOLE defined to 0,it represents select toolchain printf, scanf.
    *  if SDK_DEBUGCONSOLE defined to 1,it represents select SDK version printf, scanf.
    *  if SDK_DEBUGCONSOLE defined to 2,it represents disable debugconsole function.
    */
    #if SDK_DEBUGCONSOLE == DEBUGCONSOLE_DISABLE /* Disable debug console */
    #define PRINTF
    #define SCANF
    #define PUTCHAR
    #define GETCHAR
    #elif SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK /* Select printf, scanf, putchar, getchar of SDK version. */
    #define PRINTF DbgConsole_Printf
    //#define PRINTF printf   /*添加之后两个都没打印信息*/
    #define SCANF DbgConsole_Scanf
    #define PUTCHAR DbgConsole_Putchar
    #define GETCHAR DbgConsole_Getchar
    #elif SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN /* Select printf, scanf, putchar, getchar of toolchain. \ \
                                                                    */
    #define PRINTF printf
    #define SCANF scanf
    #define PUTCHAR putchar
    #define GETCHAR getchar
    #endif /* SDK_DEBUGCONSOLE */

    有点奇怪的是,我定义#define SDK_DEBUGCONSOLE 1U或者添加#define PRINTF printf编译会报警告,提示重定义了,全局搜索也没发现其它位置有定义SDK_DEBUGCONSOLE,原来有个定义:
    /*! @brief Definition to select sdk or toolchain printf, scanf. */
    #ifndef SDK_DEBUGCONSOLE
    #define SDK_DEBUGCONSOLE 1U
    #endif
    我注释掉之后仍然会有警告。
    今天天气不错!签到!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2021-4-15 09:26
  • 签到天数: 98 天

    连续签到: 1 天

    [LV.6]常住居民II

    14

    主题

    187

    帖子

    2

    高级会员

    Rank: 4

    积分
    623
    最后登录
    2021-6-15
    发表于 2019-6-4 15:35:04 | 显示全部楼层
    l546863256 发表于 2019-6-4 14:46
    我这样试过,发现用大写PRINTF替换成小写printf之后两个都没打印信息,在fsl_debug_console.h的头文件中 ...

    将fsl_debug_console.c文件里的另一个__write函数删除
    /*************Code to support toolchain's printf, scanf *******************************/
    /* These function __write and __read is used to support IAR toolchain to printf and scanf*/
    #if (defined(__ICCARM__))
    #pragma weak __write
    //size_t __write(int handle, const unsigned char *buffer, size_t size)
    //{
    //    if (buffer == 0)
    //    {
    //        /*
    //         * This means that we should flush internal buffers.  Since we don't we just return.
    //         * (Remember, "handle" == -1 means that all handles should be flushed.)
    //         */
    //        return 0;
    //    }
    //
    //    /* This function only writes to "standard out" and "standard err" for all other file handles it returns failure. */
    //    if ((handle != 1) && (handle != 2))
    //    {
    //        return ((size_t)-1);
    //    }
    //
    //    /* Do nothing if the debug UART is not initialized. */
    //    if (s_debugConsole.type == DEBUG_CONSOLE_DEVICE_TYPE_NONE)
    //    {
    //        return ((size_t)-1);
    //    }
    //
    //    /* Send data. */
    //    s_debugConsole.ops.tx_union.PutChar(s_debugConsole.base, buffer, 1);
    //    return size;
    //}

    然后再使用printf试试
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-7-20 20:37 , Processed in 0.111371 second(s), 31 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

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