目录
概述
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 MCU | R7FA8D1BH |
Keil | MDK ARM 5.38 |
FSP 版本 | 5.3.0 |
调试工具:N32G45XVL-STB | DAP-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)参数介绍
Configuration | Options | Default | Description |
---|---|---|---|
General | |||
Name | Name must be a valid C symbol | g_uart0 | Module name. |
Channel | Value must be a non-negative integer | 0 | Select the SCI channel. |
Data Bits |
| 8bits | Select the number of bits per word. |
Parity |
| None | Select the parity mode. |
Stop Bits |
| 1bit | Select the number of stop bits. |
Baud | |||
Flow Control | |||
Extra | |||
Interrupts | |||
Callback | Name must be a valid C symbol | NULL | A user callback function can be provided. If this callback function is provided, it will be called from the interrupt service routine (ISR). |
Receive Interrupt Priority | MCU Specific Options | Select the receive interrupt priority. | |
Transmit Data Empty Interrupt Priority | MCU Specific Options | Select the transmit interrupt priority. | |
Transmit End Interrupt Priority | MCU Specific Options | Select the transmit end interrupt priority. | |
Error Interrupt Priority | MCU Specific Options | Select 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_SUCCESS Channel opened successfully. FSP_ERR_ASSERTION Pointer to UART control block or configuration structure is NULL. FSP_ERR_IP_CHANNEL_NOT_PRESENT The requested channel does not exist on this MCU. FSP_ERR_INVALID_ARGUMENT Flow control is enabled but flow control pin is not defined. FSP_ERR_ALREADY_OPEN Control 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_SUCCESS Channel successfully closed. FSP_ERR_ASSERTION Pointer to UART control block is NULL. FSP_ERR_NOT_OPEN The 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_SUCCESS Data reception successfully ends. FSP_ERR_ASSERTION Pointer to UART control block is NULL. Number of transfers outside the max or min boundary when transfer instance used FSP_ERR_INVALID_ARGUMENT Destination address or data size is not valid for 9-bit mode. FSP_ERR_NOT_OPEN The control block has not been opened FSP_ERR_IN_USE A previous read operation is still in progress. FSP_ERR_UNSUPPORTED SCI_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_SUCCESS Data transmission finished successfully. FSP_ERR_ASSERTION Pointer to UART control block is NULL. Number of transfers outside the max or min boundary when transfer instance used FSP_ERR_INVALID_ARGUMENT Source address or data size is not valid for 9-bit mode. FSP_ERR_NOT_OPEN The control block has not been opened FSP_ERR_IN_USE A UART transmission is in progress FSP_ERR_UNSUPPORTED SCI_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_SUCCESS Baud rate was successfully changed. FSP_ERR_ASSERTION Pointer to UART control block is NULL or the UART is not configured to use the internal clock. FSP_ERR_NOT_OPEN The 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_SUCCESS Callback updated successfully. FSP_ERR_ASSERTION A required pointer is NULL. FSP_ERR_NOT_OPEN The control block has not been opened. FSP_ERR_NO_CALLBACK_MEMORY p_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.cDescription: user bluetooth interface Author : tangmingfei2013@126.comDate : 2024/09/15*/
#include "bsp_uart.h"
#include "bluetooth.h"
#include "hal_data.h"#define TRANSFER_LENGTH 128static 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 inforerr = 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: