这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 企业专区 » Renesas » 瑞萨RA6M4单片机连载之3串口printf

共1条 1/1 1 跳转至

瑞萨RA6M4单片机连载之3串口printf

高工
2025-11-11 12:47:03     打赏

【前言】

在前面一篇,我做准备移植shell时,遇到了printf不能输出,今天单独拿出一篇帖子来验证。

【开发环境】

e2studio

【新建工程】

1、打开e2studio,创建一个基于RA6M4的基础工程。

image.png

2、打开sci9,定位输出IO为P109与P110。

image.png

3、在配置界面底部点击 “Stack”,加入串口 UART 模块,配置他为uart9通道,回调函数为uart9_callback

image.png

4、添加heap为0x1000大小,注意这个数,一定要能被8整除:

image.png

【代码编写】

  然后再 src 下创建一个 uart 的文件夹,在里面创建 bsp_uart.c 与 bsp_uart.h 两个文件,创建一个 Applay 的文件夹,在里面创建 app.c 与 app.h 两个文件。

image.png

bsp_uart.h:

#ifndef __BSP_UART_H_
#define __BSP_UART_H_
#include "hal_data.h"
#include "stdio.h"

#include <unistd.h>
#include <errno.h>

#include <sys/stat.h>  // 添加这个头文件以获得struct stat定义

#include <stdint.h>


void UART9_Init(void);
void uart9_callback(uart_callback_args_t *p_args);

#endif

bsp_uart.c:

#include "bsp_uart.h"

// 发送完成标志位
volatile bool Uart9_Send_Flag = false;
// 接收完成标志位
volatile bool Uart9_Receive_Flag = false;
// 写入的字节
uint32_t  bytes = 1;

//调试串口 Uart9 初始化
void UART9_Init(void)
{
     fsp_err_t err = R_SCI_UART_Open (&g_uart9_ctrl, &g_uart9_cfg);

    if (FSP_SUCCESS != err) {
           printf("串口初始化失败! \n");
           return;
       }
}

/* 串口中断回调 */
void uart9_callback(uart_callback_args_t *p_args)
{
    switch (p_args->event)
    {
        case UART_EVENT_RX_CHAR: //收到数据
        {
            R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t*) &(p_args->data), bytes);
            break;
        }
        case UART_EVENT_RX_COMPLETE:  //接收完整的事件
        {
            Uart9_Receive_Flag = true;
            break;
        }
        case UART_EVENT_TX_COMPLETE://发送完整的事件
        {
            Uart9_Send_Flag = true;
            break;
        }
        default:
        break;
    }
}


//串口重定义
// 函数前置声明(仅需声明一次)
int __io_putchar(int ch);
int _write(int fd, char *pBuffer, int size);
int _close(int fd);
int _lseek(int fd, off_t ptr, int dir);
int _read(int fd, char *pBuffer, int size);
int _fstat(int fd, struct stat *pStat);
int _isatty(int fd);

// 弱符号实现(仅需实现一次)
__attribute__((weak)) int _close(int fd) {
    (void)fd;  // 忽略未使用参数
    return -1;
}

__attribute__((weak)) int _lseek(int fd, off_t ptr, int dir) {
    (void)fd; (void)ptr; (void)dir;
    return -1;
}

__attribute__((weak)) int _read(int fd, char *pBuffer, int size) {
    (void)fd; (void)pBuffer; (void)size;
    return 0;
}

__attribute__((weak)) int _fstat(int fd, struct stat *pStat) {
    (void)fd; (void)pStat;
    return -1;
}

__attribute__((weak)) int _isatty(int fd) {
    (void)fd;
    return 1;
}

#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

PUTCHAR_PROTOTYPE {
    fsp_err_t err;
    err = R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t *)&ch, 1);
    if (FSP_SUCCESS != err) __BKPT();
    while (Uart9_Send_Flag == false);
    Uart9_Send_Flag = false;
    return ch;
}

int _write(int fd, char *pBuffer, int size) {
    (void)fd;  // 忽略未使用参数
    R_SCI_UART_Write (&g_uart9_ctrl, (uint8_t*) pBuffer, (uint32_t) size);
    while (Uart9_Send_Flag == false);
    Uart9_Send_Flag = false;
    return size;
}

app.h:

#ifndef __APP_H
#define __APP_H

#include "hal_data.h"
#include <stdio.h>

void Run(void);

#endif

app.c

#include "app.h"
#include "uart\bsp_uart.h"
void Run(void)
{
    UART9_Init();

    printf("欢迎来到电子产品世界·RA6M4开发板\r\n");
    printf("接下来开始串口循环实验,请输入回环内容:\r\n");

    while(1);
}

在halentry.c中添加Run,然后下载到开发板.

【实验现象】

首先打印出欢迎字符,然后,我们在串口输出,即回显到串口终端中:

image.png

【总结】

这次试验是学习立创开发板的RA6E2的例程,他主要是实现了bsp_uart中的printf重定向函数。跟以往的简单的书写putc不同,添加了许多不同的函数。





关键词: RA6M4     串口     printf     瑞萨    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]