nRF52832-Bluefruit52学习之Arduino开发(4)-- 蓝牙组网一拖8主从机模式(dual_roles_bleuart)

       nRF52832技术交流群:680723714

       nRF52832-Bluefruit52核心板详细介绍:

https://blog.csdn.net/solar_Lan/article/details/88688451

       github仓库地址:https://github.com/Afantor/Bluefruit52_Arduino.git

       

       Arduino例程目前分为6大部分:Central、DualRoles、Display、Hardware、Peripheral、Project。

一、Central:主设备与其他设备通信例程

二、DualRoles:从设备与主设备通信例程

本篇讲解蓝牙组网一拖8实验,本实验展示了以一个nRF52382为中心,其他nRF52832模块为外围设备,进行级联通信控制的过程。示例中演示串口打印连接状态和接收到数据,通过数据收发控制板载LED灯。主机中心为一个主模式设备,检测按键发送控制LED的字符。从设备检测接收到的数据,控制LED灯的状态。

下面直接讲解代码:

Dual Roles BLEUART双重角色BLEUART

如果不熟悉中心角色,建议先查看“Central BLEUART”示例,和上一篇博文然后再继续。
此示例演示了如何使用nRF52832同时使用bleuart(AKA'NUS')服务连接其他7个Bluefruit或BLE设备,同时设备在外围设备和中心设备上运行。

此双重角色示例充当BLE中继站,它位于中央和外围转发前端消息之间来回,如下图所示:

microcontrollers_dual_roles.jpg

Server & Client Service Setup服务端和客户端设置

由于Bluefruit设备将充当中心和外围设备,因此我们需要声明bleuart帮助程序类的服务器和客户端实例:

// Peripheral uart service
BLEUart bleuart;

// Central uart client
BLEClientUart clientUart;

在我们配置客户端服务之前,必须至少调用Bluefruit.begin(),以获得外围和中央模式的并发连接数:

// Initialize Bluefruit with max concurrent connections as Peripheral = 1, Central = 1
Bluefruit.begin(1, 1);

在此之后,必须通过调用其begin()函数初始化客户端服务,然后调用您希望连接的任何回调:

// Configure and Start BLE Uart Service
bleuart.begin();
bleuart.setRxCallback(prph_bleuart_rx_callback);

// Init BLE Central Uart Serivce
clientUart.begin();
clientUart.setRxCallback(cent_bleuart_rx_callback);

然后我们准备使用回调将数据从中央转发到外围,反之亦然:

void cent_bleuart_rx_callback(BLEClientUart& cent_uart)
{
  char str[20+1] = { 0 };
  cent_uart.read(str, 20);
      
  Serial.print("[Cent] RX: ");
  Serial.println(str);

  if ( bleuart.notifyEnabled() )
  {
    // Forward data from our peripheral to Mobile
    bleuart.print( str );
  }else
  {
    // response with no prph message
    clientUart.println("[Cent] Peripheral role not connected");
  }  
}

void prph_bleuart_rx_callback(void)
{
  // Forward data from Mobile to our peripheral
  char str[20+1] = { 0 };
  bleuart.read(str, 20);

  Serial.print("[Prph] RX: ");
  Serial.println(str);  

  if ( clientUart.discovered() )
  {
    clientUart.print(str);
  }else
  {
    bleuart.println("[Prph] Central role not connected");
  }
}

Peripheral Role外设角色

我们代码的外围部分要做的第一件事就是设置连接回调,当与中心建立/断开连接时,它会触发。 或者,您可以使用connected()轮询连接状态,但回调有助于显着简化代码:

// Callbacks for Peripheral
Bluefruit.setConnectCallback(prph_connect_callback);
Bluefruit.setDisconnectCallback(prph_disconnect_callback);

Central Role中心角色

接下来,我们设置中央模式连接回调,当与外围设备建立/断开连接时将触发:

// Callbacks for Central
Bluefruit.Central.setConnectCallback(cent_connect_callback);
Bluefruit.Central.setDisconnectCallback(cent_disconnect_callback);

Advertising and Scanner广播和扫描

可以同时启动扫描仪和广告,以便我们可以发现并被其他BLE设备发现。 对于扫描程序,如果在对等设备的广告数据中找到特定的UUID,我们使用仅触发回调的过滤器:

/* Start Central Scanning
 * - Enable auto scan if disconnected
 * - Interval = 100 ms, window = 80 ms
 * - Filter only accept bleuart service
 * - Don't use active scan
 * - Start(timeout) with timeout = 0 will scan forever (until connected)
 */
Bluefruit.Scanner.setRxCallback(scan_callback);
Bluefruit.Scanner.restartOnDisconnect(true);
Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms
Bluefruit.Scanner.filterUuid(bleuart.uuid);
Bluefruit.Scanner.useActiveScan(false);
Bluefruit.Scanner.start(0);                   // 0 = Don't stop scanning after n seconds

// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();

// Include bleuart 128-bit uuid
Bluefruit.Advertising.addService(bleuart);

// Secondary Scan Response packet (optional)
// Since there is no room for 'Name' in Advertising packet
Bluefruit.ScanResponse.addName();

/* Start Advertising
 * - Enable auto advertising if disconnected
 * - Interval:  fast mode = 20 ms, slow mode = 152.5 ms
 * - Timeout for fast mode is 30 seconds
 * - Start(timeout) with timeout = 0 will advertise forever (until connected)
 *
 * For recommended advertising interval
 * https://developer.apple.com/library/content/qa/qa1931/_index.html
 */
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244);    // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30);      // number of seconds in fast mode
Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds

例程的完整代码:

/*********************************************************************
 This is an example for our nRF52 based Bluefruit LE modules

 Pick one up today in the adafruit shop!

 Adafruit invests time and resources providing this open source code,
 please support Adafruit and open-source hardware by purchasing
 products from Adafruit!

 MIT license, check LICENSE for more information
 All text above, and the splash screen below must be included in
 any redistribution
*********************************************************************/

/*
 * This sketch demonstrate how to run both Central and Peripheral roles
 * at the same time. It will act as a relay between an central (mobile)
 * to another peripheral using bleuart service.
 * 
 * Mobile <--> DualRole <--> peripheral Ble Uart
 */
#include <bluefruit.h>

// OTA DFU service
BLEDfu bledfu;

// Peripheral uart service
BLEUart bleuart;

// Central uart client
BLEClientUart clientUart;

void setup()
{
  Serial.begin(115200);
  while ( !Serial ) delay(10);   // for nrf52840 with native usb

  Serial.println("Bluefruit52 Dual Role BLEUART Example");
  Serial.println("-------------------------------------\n");
  
  // Initialize Bluefruit with max concurrent connections as Peripheral = 1, Central = 1
  // SRAM usage required by SoftDevice will increase with number of connections
  Bluefruit.begin(1, 1);
  Bluefruit.setTxPower(4);    // Check bluefruit.h for supported values
  Bluefruit.setName("Bluefruit52 duo");

  // Callbacks for Peripheral
  Bluefruit.Periph.setConnectCallback(prph_connect_callback);
  Bluefruit.Periph.setDisconnectCallback(prph_disconnect_callback);

  // Callbacks for Central
  Bluefruit.Central.setConnectCallback(cent_connect_callback);
  Bluefruit.Central.setDisconnectCallback(cent_disconnect_callback);

  // To be consistent OTA DFU should be added first if it exists
  bledfu.begin();

  // Configure and Start BLE Uart Service
  bleuart.begin();
  bleuart.setRxCallback(prph_bleuart_rx_callback);

  // Init BLE Central Uart Serivce
  clientUart.begin();
  clientUart.setRxCallback(cent_bleuart_rx_callback);


  /* Start Central Scanning
   * - Enable auto scan if disconnected
   * - Interval = 100 ms, window = 80 ms
   * - Filter only accept bleuart service
   * - Don't use active scan
   * - Start(timeout) with timeout = 0 will scan forever (until connected)
   */
  Bluefruit.Scanner.setRxCallback(scan_callback);
  Bluefruit.Scanner.restartOnDisconnect(true);
  Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms
  Bluefruit.Scanner.filterUuid(bleuart.uuid);
  Bluefruit.Scanner.useActiveScan(false);
  Bluefruit.Scanner.start(0);                   // 0 = Don't stop scanning after n seconds

  // Set up and start advertising
  startAdv();
}

void startAdv(void)
{
  // Advertising packet
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();

  // Include bleuart 128-bit uuid
  Bluefruit.Advertising.addService(bleuart);

  // Secondary Scan Response packet (optional)
  // Since there is no room for 'Name' in Advertising packet
  Bluefruit.ScanResponse.addName();

  /* Start Advertising
   * - Enable auto advertising if disconnected
   * - Interval:  fast mode = 20 ms, slow mode = 152.5 ms
   * - Timeout for fast mode is 30 seconds
   * - Start(timeout) with timeout = 0 will advertise forever (until connected)
   *
   * For recommended advertising interval
   * https://developer.apple.com/library/content/qa/qa1931/_index.html
   */
  Bluefruit.Advertising.restartOnDisconnect(true);
  Bluefruit.Advertising.setInterval(32, 244);    // in unit of 0.625 ms
  Bluefruit.Advertising.setFastTimeout(30);      // number of seconds in fast mode
  Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds
}

void loop()
{
  // do nothing, all the work is done in callback
}

/*------------------------------------------------------------------*/
/* Peripheral
 *------------------------------------------------------------------*/
void prph_connect_callback(uint16_t conn_handle)
{
  // Get the reference to current connection
  BLEConnection* connection = Bluefruit.Connection(conn_handle);

  char peer_name[32] = { 0 };
  connection->getPeerName(peer_name, sizeof(peer_name));

  Serial.print("[Prph] Connected to ");
  Serial.println(peer_name);
}

void prph_disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
  (void) conn_handle;
  (void) reason;

  Serial.println();
  Serial.println("[Prph] Disconnected");
}

void prph_bleuart_rx_callback(uint16_t conn_handle)
{
  (void) conn_handle;
  
  // Forward data from Mobile to our peripheral
  char str[20+1] = { 0 };
  bleuart.read(str, 20);

  Serial.print("[Prph] RX: ");
  Serial.println(str);  

  if ( clientUart.discovered() )
  {
    clientUart.print(str);
  }else
  {
    bleuart.println("[Prph] Central role not connected");
  }
}

/*------------------------------------------------------------------*/
/* Central
 *------------------------------------------------------------------*/
void scan_callback(ble_gap_evt_adv_report_t* report)
{
  // Since we configure the scanner with filterUuid()
  // Scan callback only invoked for device with bleuart service advertised  
  // Connect to the device with bleuart service in advertising packet  
  Bluefruit.Central.connect(report);
}

void cent_connect_callback(uint16_t conn_handle)
{
  // Get the reference to current connection
  BLEConnection* connection = Bluefruit.Connection(conn_handle);

  char peer_name[32] = { 0 };
  connection->getPeerName(peer_name, sizeof(peer_name));

  Serial.print("[Cent] Connected to ");
  Serial.println(peer_name);;

  if ( clientUart.discover(conn_handle) )
  {
    // Enable TXD's notify
    clientUart.enableTXD();
  }else
  {
    // disconnect since we couldn't find bleuart service
    Bluefruit.disconnect(conn_handle);
  }  
}

void cent_disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
  (void) conn_handle;
  (void) reason;
  
  Serial.println("[Cent] Disconnected");
}

/**
 * Callback invoked when uart received data
 * @param cent_uart Reference object to the service where the data 
 * arrived. In this example it is clientUart
 */
void cent_bleuart_rx_callback(BLEClientUart& cent_uart)
{
  char str[20+1] = { 0 };
  cent_uart.read(str, 20);
      
  Serial.print("[Cent] RX: ");
  Serial.println(str);

  if ( bleuart.notifyEnabled() )
  {
    // Forward data from our peripheral to Mobile
    bleuart.print( str );
  }else
  {
    // response with no prph message
    clientUart.println("[Cent] Peripheral role not connected");
  }  
}

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: nrf52832-qfaa是一款由Nordic Semiconductor公司开发的低功耗蓝牙系统级芯片。原理图即为该芯片的电路连接图,它展示了芯片内部的各个功能模块之间的连接方式和电气特性。对于电路设计和电路分析人员来说,原理图是理解芯片内部结构和工作原理的重要工具。 nrf52832-qfaa原理图主要包括以下几个方面的内容: 1. 电源管理电路:负责芯片的供电和电池管理,包括电源接口、电源描述和电源过滤电路等。 2. MCU核心:这部分含有nrf52832芯片的主控制单元,包括处理器核心、存储器和系统总线等。 3. 外设接口:负责芯片与外部设备的通信,如UART、SPI、I2C和GPIO等。 4. 射频前端:负责无线通信,包括蓝牙低功耗无线模块和射频天线。 5. 时钟和定时器:包括系统时钟和RTC(实时时钟)等,用于芯片的时序控制和定时功能。 6. I/O和连接器:包括与外部电路的连接接口和引脚布局等。 7. 其他模块:如中断控制器、电源管理单元、温度传感器等。 通过阅读nrf52832-qfaa的原理图,电路设计人员可以了解芯片的各个模块的连接方式和通信协议,有助于进行电路设计和系统验证。同时,它也为故障排除和错误修复提供了重要的参考依据。原理图是一个工程师在开发和设计过程中不可或缺的工具,它大大简化了电路设计的复杂性,提高了设计的效率和可靠性。 ### 回答2: nrf52832-qfaa是一款蓝牙低功耗系统芯片,可以用于物联网设备和可穿戴设备等领域。原理图是描述该芯片内部电路连接关系的图纸。 nrf52832-qfaa原理图主要包括器件、排列方式、连接线路等信息。例如,原理图会显示芯片上的各个引脚,以及它们之间的连接方式。它会标明不同器件的型号、值以及相互之间的连接关系,以帮助设计师正确布局和设计电路。 在nrf52832-qfaa原理图中,可以看到该芯片的主要模块,包括处理器、存储器、射频收发器、时钟电路等。这些模块通过连接线路相互连接,以实现各个功能模块之间的通信和控制。 此外,原理图还能显示一些外部器件的连接方式,比如电源管理模块、传感器、接口等。这些外部器件可以通过连接线路与nrf52832-qfaa芯片相互连接,以实现更丰富的功能。 通过阅读nrf52832-qfaa原理图,设计人员可以了解芯片的内部架构和连接方式,从而有助于他们进行电路布局和调试工作。同时,对于学习者来说,也可以通过阅读原理图深入了解这款芯片的工作原理和内部结构。 ### 回答3: nrf52832-qfaa是一款由Nordic Semiconductor公司生产的芯片,主要用于物联网设备和无线通信应用。原理图是对该芯片电路连接的图示,展示各个元件之间的连接方式。 nrf52832-qfaa原理图主要包含以下几个部分: 1. 电源部分:包括电源芯片、电源电容、电源指示灯等,用于提供电源给整个芯片及其外围电路。 2. 调试部分:包括调试接口芯片和相应的连接器,用于对该芯片进行调试和软件烧录。 3. 外部接口部分:包括与其他外部元件连接的接口,如UART、SPI、I2C等,用于实现与其他设备的数据通信。 4. 时钟部分:包括晶振和时钟电路,提供准确稳定的时钟信号给芯片的各个模块。 5. 外设接口部分:包括与外部设备连接的接口,如GPIO(通用输入输出)、ADC(模数转换器)、PWM(脉宽调制)等,用于实现与外部设备的数据交换和控制。 6. 射频部分:包括射频收发器、射频天线等,用于无线通信功能。 原理图中通过不同的连线和符号表示了各个元件之间的连接关系和信号传输方式。通过分析原理图,我们可以了解到该芯片的电路结构和各个功能模块的配置情况,以及与其他外部元件的连接方式。这对于硬件工程师在进行电路设计和调试时非常有帮助,同时也为软件开发人员提供了基础的硬件信息,方便他们进行软硬件的协同开发工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Lanson

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值