瑞萨单片机论坛
直播中

hehung

8年用户 659经验值
擅长:嵌入式技术
私信 关注
[经验]

【RA4M2设计挑战赛】4. DA14531蓝牙模块使用

过往分享

下面是参加RA4M2使用活动的分享:
【瑞萨RA4系列开发板体验】1. 新建工程+按键控制LED
【瑞萨RA4系列开发板体验】2. KEIL环境搭建+STLINK调试+FreeRTOS使用
【瑞萨RA4系列开发板体验】3. KEIL下UART实现printf与scanf重定向
【瑞萨RA4系列开发板体验】4. PWM驱动LED
【瑞萨RA4系列开发板体验】5. 硬件IIC驱动OLED显示汉字
【瑞萨RA4系列开发板体验】6. ADC测量摇杆模块偏移量
【瑞萨RA4系列开发板体验】7. 用DAC输出正弦波以及余弦波
【瑞萨RA4系列开发板体验】8. 超声波测距模块在RA4M2上的应用
【瑞萨RA4系列开发板体验】9. 用两路DAC在示波器上显示一个爱心

RA4M2挑战赛分享:
【RA4M2设计挑战赛】1. RASC配置FreeRTOS
【RA4M2设计挑战赛】2. 硬件IIC读取HS3003的温湿度数据
【RA4M2设计挑战赛】3. 硬件IIC读取ISL29035采集光照强度

前言

蓝牙模块算是比较简单的模块,直接通过串口进行通信,但是其中还是有一些需要注意的点,本文将详细说明。
主要是蓝牙的LPR和RESET引脚必须连接高电平,不然模块不工作。

硬件连接

蓝牙模块可以直接连接在板载的PMOD1接口上,原理图如下:

注意:LPR必须接高电平,不然串口不能工作,RESET引脚必须接高电平,不然模块一直在复位状态,不工作

CONNECT引脚是连接状态指示引脚,连接之后为高电平,没连接蓝牙为低电平

上述要点很重要,就是这个原因导致我调试了很久这个模块都不能工作,最后还是把手册读了一边才知道的,哎。

对应RA4M2板子的PMOD1的接口如下:

即:P402和P403必须链接高电平才能工作,P008可以作为读引脚读取电平状态进而判断蓝牙有没有连接成功。

本文中P008连接时中断引脚,在中断中判断蓝牙连接状态。

配置

串口

使用了串口4,配置如下:

P008配置为中断引脚,连接蓝牙模块的CONNECT引脚,配置如下:

P402和P403配置为普通IO即可,在初始化的时候拉高

代码实现

app_buletooth.c

/*
@hehung
2023-2-8
转载请注明出处,版权由@hehung所有
email: 1398660197@qq.com
wechat: hehung95
*/

// 规定蓝牙数据发送的格式为:
// 数据长度(十六进制) + 数据

#include "hal_data.h"
#include "app_buletooth.h"
#include "app_uart.h"
#include "app_hs300x.h"
#include "app_isl29035.h"
#include "app_led.h"

#include <string.h>

#define BT_DEBUG

#ifdef BT_DEBUG
#include <stdio.h>
#define LOG(fmt, ...) 	                printf(fmt, ##__VA_ARGS__)
#else
#define LOG(fmt, ...)
#endif

#define BT_BUFFER_SIZE                  (32U)


// this structure is used to cache the received AT informations
typedef struct
{
    char bt_buf[BT_BUFFER_SIZE];
    uint16_t bt_len;
    uint16_t bt_timeout;
} s_BtCmdType;



static e_BtConnStatusType bt_conn_status = BT_DISCONNECTED;
static s_BtCmdType bt_buff;


// callback function for uart4(used for buletooth with communication)
void uart4_notification(uart_callback_args_t * p_args)
{
	if (p_args->event == UART_EVENT_TX_COMPLETE)
	{
		Bt_TxNotification();
	}
	else if (p_args->event == UART_EVENT_RX_CHAR)
	{
		Bt_RxNotification(p_args->data);
	}
}


// notification for buletooth device connect status
void ext_irq12_notification(external_irq_callback_args_t *p_args)
{
	(void)p_args;
	bsp_io_level_t pin_level;

	if (FSP_SUCCESS ==  R_IOPORT_PinRead(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_08, &pin_level))
    {
        if (BSP_IO_LEVEL_LOW == pin_level)
        {
			// buletooth is connected
			bt_conn_status = BT_DISCONNECTED;
        }
		else
		{
			// buletooth is disconnected
			bt_conn_status = BT_CONNECTED;
		}
    }
}

// initialize buletooth device pins
void Bt_Init(void)
{
	fsp_err_t err;
	bsp_io_level_t pin_level;

	// initialize the uart for communication between buletooth and MCU
	err = R_SCI_UART_Open(&g_uart4_ctrl, &g_uart4_cfg);
    assert(FSP_SUCCESS == err);

	// initialize the P008(Irq12) as interrupt pin, it connnect to the connnect pin in buletooth board
	/* Configure the external interrupt. */
    err = R_ICU_ExternalIrqOpen(&g_external_irq12_ctrl, &g_external_irq12_cfg);
    assert(FSP_SUCCESS == err);
    /* Enable the external interrupt. */
    /* Enable not required when used with ELC or DMAC. */
    err =  R_ICU_ExternalIrqEnable(&g_external_irq12_ctrl);
    assert(FSP_SUCCESS == err);

	// buletooth device Reset pin
	R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_03, BSP_IO_LEVEL_HIGH);
	// buletooth device low power pin - high level for work mode
	R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_02, BSP_IO_LEVEL_HIGH);

	// judge the buletooth connect status
	if (FSP_SUCCESS ==  R_IOPORT_PinRead(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_08, &pin_level))
    {
        if (BSP_IO_LEVEL_LOW == pin_level)
        {
			// buletooth is connected
			bt_conn_status = BT_DISCONNECTED;
        }
		else
		{
			// buletooth is disconnected
			bt_conn_status = BT_CONNECTED;
		}
    }
}

// notification for buletooth device, successfuly sent a charater
void Bt_TxNotification(void)
{

}

// notification for buletooth device, successfully received a character
void Bt_RxNotification(uint32_t rx_data)
{
	// fsp_err_t err;

	if (bt_buff.bt_len < BT_BUFFER_SIZE)
	{
		bt_buff.bt_buf[bt_buff.bt_len] = (char)rx_data;
		bt_buff.bt_len++;
	}

    if (rx_data == '0')
	{
		Led_LED_2PwmSet(0);
		err = R_SCI_UART_Write(&g_uart4_ctrl, (uint8_t*)"Closed", 6);
	}
	else if (rx_data == '1')
	{
		Led_LED_2PwmSet(1);
		err = R_SCI_UART_Write(&g_uart4_ctrl, (uint8_t*)"Opened", 6);
	}
	else
	{
		err = R_SCI_UART_Write(&g_uart4_ctrl, (uint8_t*)"Unknow", 6);
	}
}

app_buletooth.h

/*
@hehung
2023-2-8
转载请注明出处,版权由@hehung所有
email: 1398660197@qq.com
wechat: hehung95
*/

#ifndef APP_BULETOOTH_H_
#define APP_BULETOOTH_H_

#include "app_common.h"


typedef enum
{
    BT_CONNECTED = 0,
    BT_DISCONNECTED
} e_BtConnStatusType;



extern void Bt_Init(void);
extern void Bt_SendData(void);

#endif /* APP_BULETOOTH_H_ */

其他逻辑可以基于上述代码自行实现。

效果展示

更多回帖

发帖
×
20
完善资料,
赚取积分