Renesas R7FA8D1BH (Cortex®-M85)和蓝牙模块通信
目录
概述
1 软硬件
1.1 软硬件环境信息
1.2 开发板信息
1.3 调试器信息
2 硬件架构
2.1 系统架构
2.2 蓝牙模块介绍
3 软件实现
3.1 FSP配置参数
3.2 代码实现
3.2.1 驱动函数
3.2.2 功能函数
概述
本文主要介绍Renesas R7FA8D1BH (Cortex®-M85)和蓝牙模块通信的实现方法,具体内容包括整个系统的架构,蓝牙模块功能介绍,FSP配置参数的方法,蓝牙模块驱动代码,功能代码。
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 硬件架构
2.1 系统架构
系统功能介绍:
1)使用4路PWM控制小车的四个轮子转动,并且控制小车的运行方向
2)蓝牙模块通过UART接口和MCU通信
2.2 蓝牙模块介绍
HC-08蓝牙串口通信模块是新一代的基于Bluetooth Specification V4.0 BLE 蓝牙协议的数传模块。无线工作频段为 2.4GHz ISM,调制方式是 GFSK。模块最大发射功率为4dBm,接收灵敏度-93dBm,空旷环境下和 手机可以实现 80 米超远距离通信。其和MCU之间通过串口通信,软件设计也简单便捷,且不需要考虑蓝牙协议栈问题,非常适合做速成产品。
蓝牙模块与MCU之间连接图:
3 软件实现
3.1 FSP配置参数
1)蓝牙模块通信接口配置
配置IO参数
配置通信参数
3.2 代码实现
3.2.1 驱动函数
在3.1步骤中完成参数配置之后,就可以生成配置代码,编写功能代码。
1)初始化函数和数据发送函数
2)字符串发送函数
3)配置baud函数
4)接收中断回调函数
源代码文件:
/*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"
#include "app_bluetooth.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++;bluetoothCmd_DataRecvByte( (uint8_t) p_args->data );}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 */
3.2.2 功能函数
1) 蓝牙模块接收数据函数
2)发送数据接口
3)发送log函数
源代码文件:
/*FILE NAME : app_bluetooth.cDescription: app UIAuthor : tangmingfei2013@126.comDate : 2024/06/03*/
#include "app_bluetooth.h"
#include "app_main.h"
#include "bluetooth.h" #define PROT_FRAME_LEN 16Stru_BlueCmd stru_BlueCmd;static uint8_t recv_buf[PROT_FRAME_LEN];
static uint8_t rev_cnt = 0;void bluetoothCmd_DataRecvByte(uint8_t data )
{recv_buf[rev_cnt++] = data;if( rev_cnt >= PROT_FRAME_LEN)rev_cnt = 0;// bluetooth commandstru_BlueCmd.recStatus +=1;if( stru_BlueCmd.recStatus > PROT_FRAME_LEN )stru_BlueCmd.recStatus = 0;if( rev_cnt >= 2 ){rev_cnt = 0;stru_BlueCmd.mcmd = recv_buf[0];stru_BlueCmd.mode = recv_buf[1];}
}static void SendInterface(uint8_t* pu8_buf, uint16_t u16_len )
{r_sci_b_uart1_sendArry(pu8_buf, u16_len);
}static void bluetooth_sendString( uint8_t *ss )
{uint8_t buff[2];uint8_t index = 0;while ( ss[index]!='\0' ){buff[0]= ss[index];SendInterface(buff, 1);index++;}
}void bluetooth_sengLog( void )
{static uint8_t step = 0;Struc_SensorPack *pSensorData;uint8_t dataBuff[128];pSensorData = &stru_SensorData;switch( step ){default:case 0:memset((char*)dataBuff, '\0', sizeof(dataBuff));//sprintf((char*)dataBuff, "log:%d,%.2f,", pSensorData->luxValue,pSensorData->sr_value);// bluetooth_sendString( dataBuff );step = 1;break;case 1://memset((char*)dataBuff, '\0', sizeof(dataBuff));sprintf((char*)dataBuff, "log:%.2f,%.2f,%d,%.2f:end", pSensorData->temperature*0.01, pSensorData->humidity*0.01, pSensorData->luxValue,pSensorData->mcuTemp_value*0.01);bluetooth_sendString( dataBuff );step = 0;break;}
}/* End of this file */