RA生态之串口与printf()函数重定向

Posted 衾许°

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RA生态之串口与printf()函数重定向相关的知识,希望对你有一定的参考价值。

UART 配置:点击 Stacks->New Stack -> Connectivity -> UART Driver on r_sci_uart

改的地方就上面一处,其他地方默认即可,默认串口配置:115200波特率,无校验一停止通道0

R_SCI_UART_Open()函数:开启和初始化 UART

/* Open the transfer instance with initial configuration. */ fsp_err_t err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg); assert(FSP_SUCCESS == err);

回调函数 user_uart_callback ():(函数名自定义):相当于串口中断,在这里的案例是接收中断

volatile bool uart_send_complete_flag = false ; void user_uart_callback (uart_callback_args_t * p_args)         if (p_args-> event == UART_EVENT_TX_COMPLETE)                          uart_send_complete_flag = true ;         

 R_SCI_UART_Write()函数:进行串口数据输出

unsigned char buff[]= "RA E2STUDIO" ; uint8_t buff_len = strlen (buff); err = R_SCI_UART_Write(&g_uart0_ctrl, buff, buff_len); if (FSP_SUCCESS != err) __BKPT(); while (uart_send_complete_flag == false ) uart_send_complete_flag = false ;

 sprintf()函数:这个应该很多人用过了吧

直接给功能示例了:把格式化的数据写入某个字符串缓冲区

sprintf (send_buff, "\\nHello World!.\\n" ); uint8_t len = strlen (send_buff); err = R_SCI_UART_Write(&g_uart0_ctrl, send_buff, len); if (FSP_SUCCESS != err) __BKPT(); while (uart_send_complete_flag == false ) uart_send_complete_flag = false ; memset (send_buff, '\\0' , sizeof ( 100 ));

 printf()函数:

式样化输出函数, 一般用于向准则输出设备按规定式样输出消息。 正在编写步骤时经常会用到此函数

 

 设置 E2STUDIO 堆栈:BSP处设置

( RA-Eco-RA2L1-48PIN-V1.0 设置 heap size 0x2000 ) ( RA-Eco-RA2E1-48PIN-V1.0 设置 heap size 0x1000 )

 

 printf 输出重定向到串口:将 printf 的输出重定向到串口, 然后通过串口将数据发送出去

#ifdef __GNUC__                 //串口重定向 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif PUTCHAR_PROTOTYPE         err = R_SCI_UART_Write(&g_uart0_ctrl, ( uint8_t *)&ch, 1 );         if (FSP_SUCCESS != err) __BKPT();         while (uart_send_complete_flag == false )         uart_send_complete_flag = false ;         return ch; int _write( int fd, char *pBuffer, int size)         for ( int i= 0 ;i<size;i++)                  __io_putchar(*pBuffer++);          return size;

 printf 输出

int int_i= 55 ; float float_i= 66.20f ; char char_i[]= "hello e2studio" ; while ( 1 )         printf ( "int_i=%d\\n" ,int_i);         printf ( "float_i=%.2f\\n" ,float_i);         printf ( "char_i='%s'\\n" ,char_i);         R_BSP_SoftwareDelay( 1000 , BSP_DELAY_UNITS_MILLISECONDS); // NOLINT100->160

 完整例程:

#include "hal_data.h" #include <stdio.h> FSP_CPP_HEADER void R_BSP_WarmStart ( bsp_warm_start_event_t event); FSP_CPP_FOOTER fsp_err_t err = FSP_SUCCESS ; unsigned char send_buff[100]; volatile bool uart_send_complete_flag = false; void user_uart_callback ( uart_callback_args_t * p_args)         if (p_args->event == UART_EVENT_TX_COMPLETE )                          uart_send_complete_flag = true;          #ifdef __GNUC__                 // 串口重定向 #define PUTCHAR_PROTOTYPE int __io_putchar( int ch) #else #define PUTCHAR_PROTOTYPE int fputc( int ch, FILE *f) #endif PUTCHAR_PROTOTYPE         err = R_SCI_UART_Write(&g_uart0_ctrl, ( uint8_t *)&ch, 1);         if ( FSP_SUCCESS != err) __BKPT();         while (uart_send_complete_flag == false)         uart_send_complete_flag = false;         return ch; int _write ( int fd, char *pBuffer, int size)         for ( int i=0;i<size;i++)                          __io_putchar(*pBuffer++);                  return size; void hal_entry ( void )         /* TODO : add your own code here */         /* Open the transfer instance with initial configuration. */         err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);         assert( FSP_SUCCESS == err);         unsigned char buff[]= "RA E2STUDIO" ;         uint8_t buff_len = strlen (buff);         err = R_SCI_UART_Write(&g_uart0_ctrl, buff, buff_len);         if ( FSP_SUCCESS != err) __BKPT();         while (uart_send_complete_flag == false)         uart_send_complete_flag = false;         sprintf (send_buff, "\\nHello World!.\\n" );         uint8_t len = strlen (send_buff);         err = R_SCI_UART_Write(&g_uart0_ctrl, send_buff, len); if ( FSP_SUCCESS != err)         __BKPT();         while (uart_send_complete_flag == false)         uart_send_complete_flag = false;         memset (send_buff, '\\0' , sizeof (100));         int int_i=55;         float float_i=66.20f;         char char_i[]= "hello e2studio" ;         while (1)                          printf ( "int_i=%d\\n" ,int_i);                 printf ( "float_i=%.2f\\n" ,float_i);                 printf ( "char_i='%s'\\n" ,char_i);                 R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS );                 // NOLINT100->160                  #if BSP_TZ_SECURE_BUILD         /* Enter non-secure code */         R_BSP_NonSecureEnter();         #endif

效果演示:

 

以上是关于RA生态之串口与printf()函数重定向的主要内容,如果未能解决你的问题,请参考以下文章

RA生态之串口与printf()函数重定向

STM32串口的介绍与使用(原理结构体发送字符串printf函数重定向)

STM32为啥要用重定向printf来打印串口数据?直接用串口发送函数不行吗

STM32串口printf()重定向问题

Keil C51重定向printf到串口

RA生态之USART通过定时器中断接收不定长数据