当前位置: 首页 > article >正文

Renesas R7FA8D1BH (Cortex®-M85)的UART使用介绍

目录

概述

1 软硬件

1.1 软硬件环境信息

1.2 开发板信息

1.3 调试器信息

2 FSP配置UART

2.1 配置参数

2.2 UART模块介绍

3 接口函数介绍

3.1 R_SCI_B_UART_Open()

 3.2 R_SCI_B_UART_Close()

3.3  R_SCI_B_UART_Read()

3.4  R_SCI_B_UART_Write()

3.5  R_SCI_B_UART_BaudSet()

3.6  R_SCI_B_UART_CallbackSet() 

4 应用范例

4.1 编写应用代码

​4.2 源代码文件

5 测试

5.1 测试代码

5.2 运行


概述

本文主要介绍Renesas R7FA8D1BH (Cortex®-M85)内部UART的应用的相关内容,包括使用FSP配置UART的参数,FSP提供的UART相关的应用函数接口,还编写一个Demo,调用UART的函数接口,实现数据的接收和发送功能。

1 软硬件

1.1 软硬件环境信息

软硬件信息版本信息
Renesas MCUR7FA8D1BH
KeilMDK ARM 5.38
FSP 版本5.3.0
调试工具:N32G45XVL-STBDAP-LINK

1.2 开发板信息

笔者选择使用野火耀阳开发板_瑞萨RA8,该板块的主控MCU为R7FA8D1BHECBD,7FA8D1BHECBD的内核为ARM Contex-M85。

1.3 调试器信息

对于R7FA8D1BHECBD芯片,其使用的内核为Cortex®-M85 Core, ST-LINK-V2或者J-LINK-V9不支持下载和调试功能。笔者经过多次尝试,发现N32G45XVL-STB板卡上自带的DAP-LINK可以下载和调试R7FA8D1BHECBD。

下图为N32G45XVL-STB开发板实物图:

2 FSP配置UART

2.1 配置参数

硬件连接框图

 1)在Pin面板配置IO参数

2)在Stacks面板创建UART模块

 3)配置UART的相关参数

2.2 UART模块介绍

1)功能介绍

SCI UART模块支持以下功能:

1)全双工UART通信
2)中断驱动的数据传输和接收
3)用事件代码调用用户回调函数(RX/TX完成,TX数据空,RX字符,错误等)
4)波特率在运行时改变
5)比特率调制和噪声消除
6)CTS/RTS硬件流控制(带关联引脚)
7)RS-485半双工驱动支持外部RS-485收发器
8)与DTC传输模块集成
9)中止正在进行的读/写操作
10)在支持的通道上支持FIO

2)硬件流配置 

当配置硬件CTS和软件RTS时,配置的流量控制引脚将用于RTS。当读取数据时,引脚将在接收ISR内部设置高电平。当读取所有数据时,它将被设置为低。当配置硬件CTS和硬件RTS时,CSTn_RTSn引脚将用于RTS功能,CTSn引脚将用于通道n上的CTS功能。


当选择“硬件CTS和硬件RTS”流控制时,CTSn_RTSn引脚连接到CTSn引脚时,驱动程序将遵循正确的硬件流控制功能。当CTSn_RTSn和CTSn断开时,数据仍然会传输,因为CTSn引脚在硬件上被内部拉低,而CTSn引脚被配置为SCI模块的外围引脚。如果不需要硬件流控制,请不要配置CTSn引脚。

 3)参数介绍

ConfigurationOptionsDefaultDescription
 General
NameName must be a valid C symbolg_uart0Module name.
ChannelValue must be a non-negative integer0Select the SCI channel.
Data Bits
  • 8bits
  • 7bits
  • 9bits
8bitsSelect the number of bits per word.
Parity
  • None
  • Odd
  • Even
NoneSelect the parity mode.
Stop Bits
  • 1bit
  • 2bits
1bitSelect the number of stop bits.
 Baud
 Flow Control
 Extra
 Interrupts
CallbackName must be a valid C symbolNULLA user callback function can be provided. If this callback function is provided, it will be called from the interrupt service routine (ISR).
Receive Interrupt PriorityMCU Specific OptionsSelect the receive interrupt priority.
Transmit Data Empty Interrupt PriorityMCU Specific OptionsSelect the transmit interrupt priority.
Transmit End Interrupt PriorityMCU Specific OptionsSelect the transmit end interrupt priority.
Error Interrupt PriorityMCU Specific OptionsSelect the error interrupt priority.

3 接口函数介绍

3.1 R_SCI_B_UART_Open()

函数原型


fsp_err_t R_SCI_B_UART_Open	(	uart_ctrl_t *const 	p_api_ctrl,
                                uart_cfg_t const *const 	p_cfg )	

返回值:

Return values

FSP_SUCCESSChannel opened successfully.
FSP_ERR_ASSERTIONPointer to UART control block or configuration structure is NULL.
FSP_ERR_IP_CHANNEL_NOT_PRESENTThe requested channel does not exist on this MCU.
FSP_ERR_INVALID_ARGUMENTFlow control is enabled but flow control pin is not defined.
FSP_ERR_ALREADY_OPENControl block has already been opened or channel is being used by another instance. Call close() then open() to reconfigure.

 3.2 R_SCI_B_UART_Close()

函数原型: 中止任何正在进行的传输。禁用中断、接收器和发送器。如果使用,关闭较低级别传输驱动程序。删除的权力。

fsp_err_t R_SCI_B_UART_Close	(	uart_ctrl_t *const 	p_api_ctrl	)	

返回值:

Return values

FSP_SUCCESSChannel successfully closed.
FSP_ERR_ASSERTIONPointer to UART control block is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened

3.3  R_SCI_B_UART_Read()

函数原型:


fsp_err_t R_SCI_B_UART_Read	(	 uart_ctrl_t *const 	p_api_ctrl,
                                 uint8_t *const 	p_dest,
                                 uint32_t const 	bytes )	

返回值:

Return values

FSP_SUCCESSData reception successfully ends.
FSP_ERR_ASSERTIONPointer to UART control block is NULL. Number of transfers outside the max or min boundary when transfer instance used
FSP_ERR_INVALID_ARGUMENTDestination address or data size is not valid for 9-bit mode.
FSP_ERR_NOT_OPENThe control block has not been opened
FSP_ERR_IN_USEA previous read operation is still in progress.
FSP_ERR_UNSUPPORTEDSCI_B_UART_CFG_RX_ENABLE is set to 0

3.4  R_SCI_B_UART_Write()

函数原型:

fsp_err_t R_SCI_B_UART_Write	(	uart_ctrl_t *const 	p_api_ctrl,
                                    uint8_t const *const 	p_src,
                                    uint32_t const 	bytes )	

返回值:

Return values

FSP_SUCCESSData transmission finished successfully.
FSP_ERR_ASSERTIONPointer to UART control block is NULL. Number of transfers outside the max or min boundary when transfer instance used
FSP_ERR_INVALID_ARGUMENTSource address or data size is not valid for 9-bit mode.
FSP_ERR_NOT_OPENThe control block has not been opened
FSP_ERR_IN_USEA UART transmission is in progress
FSP_ERR_UNSUPPORTEDSCI_B_UART_CFG_TX_ENABLE is set to 0

3.5  R_SCI_B_UART_BaudSet()

函数原型:


fsp_err_t R_SCI_B_UART_BaudSet	(	uart_ctrl_t *const 	p_api_ctrl,
void const *const 	p_baud_setting 
)	

返回值:

Return values

FSP_SUCCESSBaud rate was successfully changed.
FSP_ERR_ASSERTIONPointer to UART control block is NULL or the UART is not configured to use the internal clock.
FSP_ERR_NOT_OPENThe control block has not been opened

3.6  R_SCI_B_UART_CallbackSet() 

函数原型:


fsp_err_t R_SCI_B_UART_CallbackSet	(  uart_ctrl_t *const 	p_api_ctrl,
                                       void(*)(uart_callback_args_t *) 	p_callback,
                                       void const *const 	p_context,
                                       uart_callback_args_t *const 	p_callback_memory 
)	

返回值:

Return values

FSP_SUCCESSCallback updated successfully.
FSP_ERR_ASSERTIONA required pointer is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.
FSP_ERR_NO_CALLBACK_MEMORYp_callback is non-secure and p_callback_memory is either secure or NULL.

4 应用范例

4.1 编写应用代码

1)初始化串口函数

代码60行: 计算baud

代码63行:设置baud

代码66行:打开串口

2)发送数据函数

3)接收数据中断回调函数

 4.2 源代码文件

创建.c文件,编写如下代码:

 /*
 FILE NAME  :  bluetooth.c
 Description:  user bluetooth interface 
 Author     :  tangmingfei2013@126.com
 Date       :  2024/09/15
 */
#include "bsp_uart.h"
#include "bluetooth.h" 
#include "hal_data.h"

#define TRANSFER_LENGTH       128

static uint8_t  g_out_of_band_received[TRANSFER_LENGTH];
static uint32_t g_transfer_complete = 0;
static uint32_t g_receive_complete  = 0;
static uint32_t g_out_of_band_index = 0;

static void r_sci_b_uart1_set_baud (uint32_t baud_rate);
void r_sci_b_uart1_sendArry ( uint8_t *str, uint32_t len);

void blueTooth_Init( void )
{
    r_sci_b_uart1_set_baud( SCI_B_UART_BAUDRATE_9600 );
}

void blueTooth_send( void )
{
    static int sec;
    rtc_time_t get_time;
    
    user_get_currentRtc(&get_time);
    if( get_time.tm_sec != sec  )
    {
        sec = get_time.tm_sec;
        r_sci_b_uart1_sendArry(g_out_of_band_received,TRANSFER_LENGTH );
    }
}

void r_sci_b_uart1_sendArry ( uint8_t *str, uint32_t len)
{
    fsp_err_t err;

    // send the messsage infor
    err = R_SCI_B_UART_Write(&g_uart1_ctrl,str, len);
    assert(FSP_SUCCESS == err);
    while (!g_transfer_complete)
    {
        R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MICROSECONDS);
    }
    g_transfer_complete = 0;
}

static void r_sci_b_uart1_set_baud (uint32_t baud_rate)
{
    fsp_err_t err ;
    sci_b_baud_setting_t baud_setting;
    bool                 enable_bitrate_modulation = false;
    uint32_t             error_rate_x_1000         = SCI_B_UART_BAUDRATE_ERROR_PERCENT_5;
    
    err = R_SCI_B_UART_BaudCalculate(baud_rate, enable_bitrate_modulation, error_rate_x_1000, &baud_setting);
    assert(FSP_SUCCESS == err);
    err = R_SCI_B_UART_BaudSet(&g_uart1_ctrl, (void *) &baud_setting);
    assert(FSP_SUCCESS == err);
    
    /* Open the transfer instance with initial configuration. */
    err = R_SCI_B_UART_Open(&g_uart1_ctrl, &g_uart1_cfg);
    assert(FSP_SUCCESS == err);

}

void g_uart1_Callback (uart_callback_args_t * p_args)
{
    /* Handle the UART event */
    switch (p_args->event)
    {
        /* Received a character */
        case UART_EVENT_RX_CHAR:
        {
            /* Only put the next character in the receive buffer if there is space for it */
            if (g_out_of_band_index < TRANSFER_LENGTH)
            {
                /* Write either the next one or two bytes depending on the receive data size */
                if (UART_DATA_BITS_8 >= g_uart1_cfg.data_bits)
                {
                    g_out_of_band_received[g_out_of_band_index] = (uint8_t) p_args->data;
                    g_out_of_band_index++;
                }
                else
                {
                    uint16_t * p_dest = (uint16_t *) &g_out_of_band_received[g_out_of_band_index];
                    *p_dest              = (uint16_t) p_args->data;
                    g_out_of_band_index += 2;
                }
            }
            else
            {
                g_out_of_band_index = 0;
            }
            break;
        }
        /* Receive complete */
        case UART_EVENT_RX_COMPLETE:
        {
            g_receive_complete = 1;
            break;
        }
        /* Transmit complete */
        case UART_EVENT_TX_COMPLETE:
        {
            g_transfer_complete = 1;
            break;
        }
        default:
        {
        }
    }
}



/* End of this file */

5 测试

使用串口调试助手与板卡连接起来,实现数据发生和接收功能。

5.1 测试代码

代码功能介绍:间隔1s发送g_out_of_band_received中的数据,

间隔时间通过MCU内部的RTC模块获取

void blueTooth_send( void )
{
    static int sec;
    rtc_time_t get_time;
    
    user_get_currentRtc(&get_time);
    if( get_time.tm_sec != sec  )
    {
        sec = get_time.tm_sec;
        r_sci_b_uart1_sendArry(g_out_of_band_received,TRANSFER_LENGTH );
    }
}

5.2 运行

系统硬件环境:

测试数据log:


http://www.kler.cn/a/320312.html

相关文章:

  • Vue语音播报功能
  • LeetCode 热题 100_从前序与中序遍历序列构造二叉树(47_105_中等_C++)(二叉树;递归)
  • 通过外部链接启动 Flutter App(详细介绍及示例)
  • Elasticsearch ES|QL 地理空间索引加入纽约犯罪地图
  • 基于当前最前沿的前端(Vue3 + Vite + Antdv)和后台(Spring boot)实现的低代码开发平台
  • ImportError: attempted relative import with no known parent package 报错的解决!
  • JavaWeb - 7 - SpringBootWeb入门
  • Oracle(136)什么是UNDO表空间?
  • 汽车总线之---- CAN FD总线
  • Greedy_approach贪心算法
  • MATLAB中多张fig图合并为一个图
  • 国产操作系统(统信UOS)网络安全等级保护基础安全加固
  • 9.25盒马鲜生一面
  • 打卡软件——人脸识别综合实现Pro
  • Remotion:使用前端技术开发视频
  • ES6的简单介绍(第三部分)
  • AR传送门+特定区域显示内容+放大镜 效果着色器使用
  • 文件上传漏洞+CTF实例
  • 时频分析法——连续小波变换(CWT)
  • ubuntu数据硬盘故障导致系统启动失败
  • 四元组问题
  • 医院伤员小程序点餐———未来之窗行业应用跨平台架构
  • C# 游戏引擎中的协程
  • Dubbo快速入门(一):分布式相关概念
  • python学习记录3
  • 专业学习|《随机过程》学习笔记(二)(定义、分类及相关过程)