中级
20 分钟

使用BT840和STM32F103RB控制智能家居设备和家电

基于nRF52840 SoC的高要求物联网应用的蓝牙低功耗(BLE)连接

BT840 Click with Nucleo 64 with STM32F103RB MCU

已发布 1月 20, 2025

点击板

BT840 Click

开发板

Nucleo 64 with STM32F103RB MCU

编译器

NECTO Studio

微控制器单元

STM32F103RB

为物联网设备提供安全且远距离的蓝牙低功耗 (Bluetooth LE) 连接

A

A

硬件概览

它是如何工作的?

BT840 Click 基于 Fanstel 的 BT840,这是一款超低功耗的蓝牙低能耗 (BLE) 模块,专为满足现代物联网应用的苛刻需求而设计。它集成了 Nordic Semiconductor 的 nRF52840 QIAA SoC,该 SoC 配备强大的 Cortex M4F MCU,拥有 1MB 的闪存和 256KB 的 RAM。此外,该模块还包括 ARM® TrustZone® Cryptocell-310 协处理器,确保敏感数据的强大行业级安全性。它通过标准 UART 接口与主 MCU 通信,简化了集成,便于开发高性能、低功耗的物联网系统。BT840 模块是一个完整的射频解决方案,配备嵌入式 2.4GHz 多协议收发器并支持 NFC 功能,使其适用于广泛的物联网用例。集成的 PCB 路径天线可实现可靠的通信,最大传输功率为 +4.9dBm,支持长距离性能。根据数据速率,它可以在 1Mbps 下实现高达 150 米的通信范围,在 

125kbps 下实现高达 210 米的通信范围,为各种应用提供了卓越的灵活性。BT840 Click 非常适合需要高效且安全无线通信的物联网设备,支持超低功耗、长距离连接和高吞吐量。其先进功能(包括加密加速器)确保了数据传输的安全性和可靠性,满足工业、商业和消费物联网应用的关键需求。正如前述,BT840 Click 通过 UART 接口与主 MCU 通信,使用 TX 和 RX 引脚以默认波特率 115200bps 进行数据交换。除了 UART 接口外,板上还提供专用控制引脚以增强功能:WUP 引脚通过切换逻辑状态唤醒模块,而 CMD 引脚通过设置为高电平启用命令模式。BT840 Click 还包括一个外部 NFC 天线 u.Fl 接口,支持高级近场通信 (NFC) 功能。模块的 NFC 块支持 NFC-A 标签,允许从低功耗模式进行近距离检测和场唤醒功能。这一特性通过实现 Out-Of-Band (OOB) 蓝牙配对

简化了设备配对,优化了物联网系统的部署。此外,该板具有一个未焊接的 6 针接头,用于直接访问模块的 GPIO 信号,提供了灵活性以进行自定义配置和扩展功能。板上还配备了四个红色 LED 指示灯(LED1-LED4),可配置为用户特定的应用,另有两个按钮:一个 RST 按钮用于重置模块,另一个通用 BTN 按钮用于额外功能。为了实现串行线调试,SWDIO 连接引脚可用于调试和开发。在板的背面有低功耗 (LP) 断开跳线,通过切断这些跳线,可以断开 LED 和 ClickID 部分的电源,大大降低功耗,在对能耗敏感的应用中实现高效运行。该 Click 板™ 仅可在 3.3V 逻辑电压下运行。在使用其他逻辑电平的 MCU 之前,必须进行适当的逻辑电压电平转换。此外,该 Click 板™ 配备了功能库和示例代码,可用作进一步开发的参考。

BT840 Click hardware overview image

功能概述

开发板

Nucleo-64 搭载 STM32F103RB MCU 提供了一种经济高效且灵活的平台,供开发者探索新想法并原型设计他们的项目。该板利用 STM32 微控制器的多功能性,使用户能够为他们的项目选择最佳的性能与功耗平衡。它配备了 LQFP64 封装的 STM32 微控制器,并包含了如用户 LED(同时作为 ARDUINO® 信号)、用户和复位按钮,以及 32.768kHz 晶体振荡器用于精确的计时操作等基本组件。Nucleo-64 板设计考虑到扩展性和灵活性,它特有的 ARDUINO® Uno

V3 扩展连接器和 ST morpho 扩展引脚头,提供了对 STM32 I/O 的完全访问,以实现全面的项目整合。电源供应选项灵活,支持 ST-LINK USB VBUS 或外部电源,确保在各种开发环境中的适应性。该板还配备了一个具有 USB 重枚举功能的板载 ST-LINK 调试器/编程器,简化了编程和调试过程。此外,该板设计旨在简化高级开发,它的外部 SMPS 为 Vcore 逻辑供电提供高效支持,支持 USB 设备全速或 USB SNK/UFP 全速,并内置加密功能,提升了项目的功效

和安全性。通过外部 SMPS 实验的专用连接器、 用于  ST-LINK 的 USB 连接器以及 MIPI® 调试连接器,提供了更多的硬件接口和实验可能性。开发者将通过 STM32Cube MCU Package 提供的全面免费软件库和示例得到广泛支持。这些,加上与多种集成开发环境(IDE)的兼容性,包括 IAR Embedded Workbench®、MDK-ARM 和 STM32CubeIDE,确保了流畅且高效的开发体验,使用户能够充分利用 Nucleo-64 板在他们的项目中的能力。

Nucleo 64 with STM32F103RB MCU double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

ARM Cortex-M3

MCU 内存 (KB)

128

硅供应商

STMicroelectronics

引脚数

64

RAM (字节)

20480

你完善了我!

配件

Click Shield for Nucleo-64 配备了两个专有的 mikroBUS™ 插座,使得所有的 Click board™ 设备都可以轻松地与 STM32 Nucleo-64 开发板连接。这样,Mikroe 允许其用户从不断增长的 Click boards™ 范围中添加任何功能,如 WiFi、GSM、GPS、蓝牙、ZigBee、环境传感器、LED、语音识别、电机控制、运动传感器等。您可以使用超过 1537 个 Click boards™,这些 Click boards™ 可以堆叠和集成。STM32 Nucleo-64 开发板基于 64 引脚封装的微控制器,采用 32 位 MCU,配备 ARM Cortex M4 处理器,运行速度为 84MHz,具有 512Kb Flash 和 96KB SRAM,分为两个区域,顶部区域代表 ST-Link/V2 调试器和编程器,而底部区域是一个实际的开发板。通过 USB 连接方便地控制和供电这些板子,以便直接对 Nucleo-64 开发板进行编程和高效调试,其中还需要额外的 USB 线连接到板子上的 USB 迷你接口。大多数 STM32 微控制器引脚都连接到了板子左右边缘的 IO 引脚上,然后连接到两个现有的 mikroBUS™ 插座上。该 Click Shield 还有几个开关,用于选择 mikroBUS™ 插座上模拟信号的逻辑电平和 mikroBUS™ 插座本身的逻辑电压电平。此外,用户还可以通过现有的双向电平转换器,使用任何 Click board™,无论 Click board™ 是否在 3.3V 或 5V 逻辑电压电平下运行。一旦将 STM32 Nucleo-64 开发板与我们的 Click Shield for Nucleo-64 连接,您就可以访问数百个工作于 3.3V 或 5V 逻辑电压电平的 Click boards™。

Click Shield for Nucleo-64 accessories 1 image

使用的MCU引脚

mikroBUS™映射器

Module Wake-Up
PC0
AN
Reset
PC12
RST
ID COMM
PB12
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Command Mode Control
PC8
PWM
NC
NC
INT
UART TX
PA2
TX
UART RX
PA3
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

BT840 Click Schematic schematic

一步一步来

项目组装

Click Shield for Nucleo-64 accessories 1 image hardware assembly

从选择您的开发板和Click板™开始。以Nucleo 64 with STM32F103RB MCU作为您的开发板开始。

Click Shield for Nucleo-64 accessories 1 image hardware assembly
Nucleo 64 with STM32F401RE MCU front image hardware assembly
LTE IoT 5 Click front image hardware assembly
Prog-cut hardware assembly
LTE IoT 5 Click complete accessories setup image hardware assembly
Board mapper by product8 hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
Clicker 4 for STM32F4 HA MCU Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

实时跟踪您的结果

应用程序输出

1. 应用程序输出 - 在调试模式下,“应用程序输出”窗口支持实时数据监控,直接提供执行结果的可视化。请按照提供的教程正确配置环境,以确保数据正确显示。

2. UART 终端 - 使用UART Terminal通过USB to UART converter监视数据传输,实现Click board™与开发系统之间的直接通信。请根据项目需求配置波特率和其他串行设置,以确保正常运行。有关分步设置说明,请参考提供的教程

3. Plot 输出 - Plot功能提供了一种强大的方式来可视化实时传感器数据,使趋势分析、调试和多个数据点的对比变得更加直观。要正确设置,请按照提供的教程,其中包含使用Plot功能显示Click board™读数的分步示例。在代码中使用Plot功能时,请使用以下函数:plot(insert_graph_name, variable_name);。这是一个通用格式,用户需要将“insert_graph_name”替换为实际图表名称,并将“variable_name”替换为要显示的参数。

软件支持

库描述

BT840 Click 演示应用程序使用 NECTO Studio开发,确保与 mikroSDK 的开源库和工具兼容。该演示设计为即插即用,可与所有具有 mikroBUS™ 插座的 开发板、入门板和 mikromedia 板完全兼容,用于快速实现和测试。

示例描述
本示例展示了如何使用 BT840 Click 板,通过处理来自连接的蓝牙 (BT) 设备的数据。

关键功能:

  • bt840_cfg_setup - 配置对象初始化函数。

  • bt840_init - 初始化函数。

  • bt840_cmd_run - 此函数向 Click 模块发送指定的命令。

  • bt840_cmd_set - 此函数向 Click 模块的指定命令设置一个值。

  • bt840_cmd_get - 此函数用于从 Click 模块的指定命令获取值。

应用初始化
初始化驱动程序和日志记录器。

应用任务
应用任务分为以下几个阶段:

  • BT840_POWER_UP: - 启动设备并读取系统信息。

  • BT840_CONFIG_EXAMPLE: - 设置 BT 设备名称。

  • BT840_EXAMPLE: - 执行一个 BT 终端示例,通过处理来自连接的 BT 设备的所有数据并发送适当的响应消息。

开源

代码示例

完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio 应用程序代码也可以在MIKROE的GitHub账户中找到。

/*!
 * @file main.c
 * @brief BT840 Click Example.
 *
 * # Description
 * This example demonstrates the use of BT840 Click board by processing data
 * from a connected BT device.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and logger.
 *
 * ## Application Task
 * Application task is split in few stages:
 *  - BT840_POWER_UP:
 * Powers up the device and reads the system information.
 *  - BT840_CONFIG_EXAMPLE:
 * Sets the BT device name.
 *  - BT840_EXAMPLE:
 * Performs a BT terminal example by processing all data from a connected BT device 
 * and sending back an adequate response messages.
 *
 * ## Additional Function
 * - static void bt840_clear_app_buf ( void )
 * - static void bt840_log_app_buf ( void )
 * - static err_t bt840_process ( bt840_t *ctx )
 * - static err_t bt840_read_response ( bt840_t *ctx, uint8_t *rsp )
 * - static err_t bt840_power_up ( bt840_t *ctx )
 * - static err_t bt840_config_example ( bt840_t *ctx )
 * - static err_t bt840_example ( bt840_t *ctx )
 *
 * @note
 * We have used the Serial Bluetooth Terminal smartphone application for the test. 
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "bt840.h"
#include "generic_pointer.h"

// Message content
#define MESSAGE_CONTENT     "BT840 Click board - demo example."

// Local device name.
#define DEVICE_NAME         "BT840 Click"

static bt840_t bt840;
static log_t logger;

// Application buffer size
#define APP_BUFFER_SIZE     600
#define PROCESS_BUFFER_SIZE 200

static uint8_t app_buf[ APP_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len = 0;

/**
 * @brief Example states.
 * @details Predefined enum values for application example state.
 */
typedef enum
{
    BT840_POWER_UP = 1,
    BT840_CONFIG_EXAMPLE,
    BT840_EXAMPLE

} bt840_app_state_t;

static bt840_app_state_t app_state = BT840_POWER_UP;

/**
 * @brief BT840 clearing application buffer.
 * @details This function clears memory of application buffer and reset its length.
 * @note None.
 */
static void bt840_clear_app_buf ( void );

/**
 * @brief BT840 log application buffer.
 * @details This function logs data from application buffer to USB UART.
 * @note None.
 */
static void bt840_log_app_buf ( void );

/**
 * @brief BT840 data reading function.
 * @details This function reads data from device and concatenates data to application buffer. 
 * @param[in] ctx : Click context object.
 * See #bt840_t object definition for detailed explanation.
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t bt840_process ( bt840_t *ctx );

/**
 * @brief BT840 read response function.
 * @details This function waits for a response message, reads and displays it on the USB UART.
 * @param[in] ctx : Click context object.
 * See #bt840_t object definition for detailed explanation.
 * @param[in] rsp  Expected response.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t bt840_read_response ( bt840_t *ctx, uint8_t *rsp );

/**
 * @brief BT840 power up function.
 * @details This function powers up the device, and reads the system information.
 * @param[in] ctx : Click context object.
 * See #bt840_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t bt840_power_up ( bt840_t *ctx );

/**
 * @brief BT840 config example function.
 * @details This function sets the BT device name.
 * @param[in] ctx : Click context object.
 * See #bt840_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t bt840_config_example ( bt840_t *ctx );

/**
 * @brief BT840 example function.
 * @details This function performs a BT terminal example by processing all data from
 * a connected BT device and sending back an adequate response messages.
 * @param[in] ctx : Click context object.
 * See #bt840_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t bt840_example ( bt840_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    bt840_cfg_t bt840_cfg;  /**< Click config object. */

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, " Application Init " );

    // Click initialization.
    bt840_cfg_setup( &bt840_cfg );
    BT840_MAP_MIKROBUS( bt840_cfg, MIKROBUS_1 );
    if ( UART_ERROR == bt840_init( &bt840, &bt840_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );

    app_state = BT840_POWER_UP;
    log_printf( &logger, ">>> APP STATE - POWER UP <<<\r\n\n" );
}

void application_task ( void ) 
{
    switch ( app_state )
    {
        case BT840_POWER_UP:
        {
            if ( BT840_OK == bt840_power_up( &bt840 ) )
            {
                app_state = BT840_CONFIG_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - CONFIG EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case BT840_CONFIG_EXAMPLE:
        {
            if ( BT840_OK == bt840_config_example( &bt840 ) )
            {
                app_state = BT840_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case BT840_EXAMPLE:
        {
            bt840_example( &bt840 );
            break;
        }
        default:
        {
            log_error( &logger, " APP STATE." );
            break;
        }
    }
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

static void bt840_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
}

static void bt840_log_app_buf ( void )
{
    for ( int32_t buf_cnt = 0; buf_cnt < app_buf_len; buf_cnt++ )
    {
        log_printf( &logger, "%c", app_buf[ buf_cnt ] );
    }
}

static err_t bt840_process ( bt840_t *ctx ) 
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t overflow_bytes = 0;
    int32_t rx_cnt = 0;
    int32_t rx_size = bt840_generic_read( ctx, rx_buf, PROCESS_BUFFER_SIZE );
    if ( ( rx_size > 0 ) && ( rx_size <= APP_BUFFER_SIZE ) ) 
    {
        if ( ( app_buf_len + rx_size ) > APP_BUFFER_SIZE ) 
        {
            overflow_bytes = ( app_buf_len + rx_size ) - APP_BUFFER_SIZE;
            app_buf_len = APP_BUFFER_SIZE - rx_size;
            memmove ( app_buf, &app_buf[ overflow_bytes ], app_buf_len );
            memset ( &app_buf[ app_buf_len ], 0, overflow_bytes );
        }
        for ( rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ ) 
        {
            if ( rx_buf[ rx_cnt ] ) 
            {
                app_buf[ app_buf_len++ ] = rx_buf[ rx_cnt ];
            }
        }
        return BT840_OK;
    }
    return BT840_ERROR;
}

static err_t bt840_read_response ( bt840_t *ctx, uint8_t *rsp ) 
{
    #define READ_RESPONSE_TIMEOUT_MS 30000
    uint32_t timeout_cnt = 0;
    bt840_clear_app_buf ( );
    bt840_process( ctx );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, BT840_RSP_FAIL ) ) )
    {
        bt840_process( ctx );
        if ( timeout_cnt++ > READ_RESPONSE_TIMEOUT_MS )
        {
            bt840_log_app_buf( );
            bt840_clear_app_buf( );
            log_error( &logger, " Timeout!" );
            return BT840_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    Delay_ms ( 200 );
    bt840_process( ctx );
    bt840_log_app_buf( );
    if ( strstr( app_buf, rsp ) )
    {
        log_printf( &logger, "--------------------------------\r\n" );
        return BT840_OK;
    }
    return BT840_ERROR_CMD;
}

static err_t bt840_power_up ( bt840_t *ctx )
{
    err_t error_flag = BT840_OK;
    
    log_printf( &logger, ">>> Reset device.\r\n" );
    bt840_set_cmd_mode( &bt840 );
    bt840_reset_device( &bt840 );
    bt840_wakeup_device( &bt840 );
    while ( BT840_OK == bt840_process( ctx ) )
    {
        bt840_log_app_buf( );
        bt840_clear_app_buf ( );
    }
    log_printf( &logger, "--------------------------------\r\n" );

    log_printf( &logger, ">>> Factory reset.\r\n" );
    bt840_cmd_run( &bt840, BT840_CMD_DEFAULT_RESET );
    error_flag |= bt840_read_response( &bt840, BT840_RSP_OK );

    log_printf( &logger, ">>> Check communication.\r\n" );
    bt840_cmd_run( &bt840, BT840_CMD_AT );
    error_flag |= bt840_read_response( &bt840, BT840_RSP_OK );

    log_printf( &logger, ">>> Get software version.\r\n" );
    bt840_cmd_get( ctx, BT840_CMD_GET_SW_VERSION );
    error_flag |= bt840_read_response( ctx, BT840_RSP_OK );

    log_printf( &logger, ">>> Get MAC address.\r\n" );
    bt840_cmd_get( ctx, BT840_CMD_GET_MAC );
    error_flag |= bt840_read_response( ctx, BT840_RSP_OK );

    return error_flag;
}

static err_t bt840_config_example ( bt840_t *ctx )
{
    err_t error_flag = BT840_OK;

    log_printf( &logger, ">>> Set device name to \"%s\".\r\n", ( char * ) DEVICE_NAME );
    bt840_cmd_set( ctx, BT840_CMD_DEVICE_NAME, DEVICE_NAME );
    error_flag |= bt840_read_response( ctx, BT840_RSP_OK );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    
    log_printf( &logger, ">>> Save settings.\r\n" );
    bt840_cmd_set( &bt840, BT840_CMD_SAVE_SETTINGS, "1" );
    error_flag |= bt840_read_response( ctx, BT840_RSP_OK );
    
    log_printf( &logger, ">>> Reboot.\r\n" );
    bt840_cmd_run( ctx, BT840_CMD_RESET );
    error_flag |= bt840_read_response( ctx, BT840_RSP_OK );
    
    return error_flag;
}

static err_t bt840_example ( bt840_t *ctx )
{
    err_t error_flag = BT840_OK;
    uint32_t timeout_cnt = 0;
    uint8_t data_len = 0;
    uint8_t byte_cnt = 0;
    uint8_t * __generic_ptr start_ptr = NULL;

    #define BT_TERMINAL_TIMEOUT_MS      60000
    #define BT_TERMINAL_MESSAGE_FREQ_MS 5000
    #define TERMINATION_CMD             "END"
    #define TERMINATION_RESPONSE        "END command received, the connection will be terminated in a few seconds."
    #define TERMINATION_TIMEOUT         "Timeout, closing the connection in a few seconds."
    #define NEW_LINE_STRING             "\r\n"
    
    log_printf( &logger, ">>> Waiting for a BT peer to establish connection with the Click board...\r\n" );
    for ( ; ; )
    {
        bt840_clear_app_buf( );
        if ( BT840_OK == bt840_process( ctx ) )
        {
            Delay_ms ( 200 );
            bt840_process( ctx );
            bt840_log_app_buf( );
            if ( strstr( app_buf, BT840_RSP_CONNECTED ) )
            {
                log_printf( &logger, "--------------------------------\r\n" );
                log_printf( &logger, ">>> BT peer has connected.\r\n" );
                bt840_set_data_mode( &bt840 );
                break;
            }
        }
    }

    log_printf( &logger, ">>> Waiting for data (up to 60 seconds)...\r\n" );
    log_printf( &logger, ">>> Connection will be terminated if the Click receives an \"END\" string.\r\n" );
    for ( ; ; )
    {
        bt840_clear_app_buf( );
        if ( BT840_OK == bt840_process( ctx ) )
        {
            Delay_ms ( 100 );
            timeout_cnt = 0;
            bt840_process( ctx );
            bt840_log_app_buf( );
            start_ptr = strstr( app_buf, BT840_RSP_RECEIVE );
            if ( start_ptr )
            {
                start_ptr += strlen ( BT840_RSP_RECEIVE );
                data_len = *start_ptr;
                log_printf( &logger, "<<< Received data (HEX): " );
                for ( byte_cnt = 0; byte_cnt < data_len; byte_cnt++ )
                {
                    log_printf( &logger, "0x%.2X ", *( start_ptr + byte_cnt + 1 ) );
                }
                log_printf( &logger, "\r\n" );
                log_printf( &logger, "<<< Received data (STR): %s", ( start_ptr + 1 ) );
                log_printf( &logger, "--------------------------------\r\n" );
            }
            if ( strstr( app_buf, TERMINATION_CMD ) )
            {
                log_printf( &logger, ">>> Terminating connection on demand.\r\n" );
                data_len = strlen ( TERMINATION_RESPONSE ) + strlen ( NEW_LINE_STRING );
                bt840_generic_write ( ctx, &data_len, 1 );
                bt840_generic_write ( ctx, TERMINATION_RESPONSE, strlen ( TERMINATION_RESPONSE ) );
                bt840_generic_write ( ctx, NEW_LINE_STRING, strlen ( NEW_LINE_STRING ) );
                Delay_ms ( 100 );
                error_flag |= bt840_read_response( ctx, BT840_RSP_SEND );

                log_printf( &logger, ">>> Disconnecting BT peer.\r\n" );
                bt840_set_cmd_mode( &bt840 );
                bt840_cmd_set( ctx, BT840_CMD_DISCONNECT, "1" );
                error_flag |= bt840_read_response( ctx, BT840_RSP_DISCONNECTED );
                break;
            }
            else if ( strstr( app_buf, BT840_RSP_DISCONNECTED ) )
            {
                log_printf( &logger, ">>> BT peer has disconnected.\r\n" );
                break;
            }
        }
        timeout_cnt++;
        if ( 0 == ( timeout_cnt % BT_TERMINAL_MESSAGE_FREQ_MS ) )
        {
            log_printf( &logger, ">>> Sending \"%s\" message to connected device.\r\n", ( char * ) MESSAGE_CONTENT );
            data_len = strlen ( MESSAGE_CONTENT ) + strlen ( NEW_LINE_STRING );
            bt840_generic_write ( ctx, &data_len, 1 );
            bt840_generic_write ( ctx, MESSAGE_CONTENT, strlen ( MESSAGE_CONTENT ) );
            bt840_generic_write ( ctx, NEW_LINE_STRING, strlen ( NEW_LINE_STRING ) );
            Delay_ms ( 100 );
            error_flag |= bt840_read_response( ctx, BT840_RSP_SEND );
        }
        if ( BT_TERMINAL_TIMEOUT_MS < timeout_cnt )
        {
            log_printf( &logger, ">>> Terminating connection due to 60s timeout expiration.\r\n" );
            data_len = strlen ( TERMINATION_TIMEOUT ) + strlen ( NEW_LINE_STRING );
            bt840_generic_write ( ctx, &data_len, 1 );
            bt840_generic_write ( ctx, TERMINATION_TIMEOUT, strlen ( TERMINATION_TIMEOUT ) );
            bt840_generic_write ( ctx, NEW_LINE_STRING, strlen ( NEW_LINE_STRING ) );
            Delay_ms ( 100 );
            error_flag |= bt840_read_response( ctx, BT840_RSP_SEND );

            log_printf( &logger, ">>> Disconnecting BT peer.\r\n" );
            bt840_set_cmd_mode( &bt840 );
            bt840_cmd_set( ctx, BT840_CMD_DISCONNECT, "1" );
            error_flag |= bt840_read_response( ctx, BT840_RSP_DISCONNECTED );
            break;
        }
        Delay_ms ( 1 );
    }

    log_printf( &logger, ">>> Reboot.\r\n" );
    bt840_cmd_run( ctx, BT840_CMD_RESET );
    error_flag |= bt840_read_response( ctx, BT840_RSP_OK );
    
    return error_flag;
}

// ------------------------------------------------------------------------ END

额外支持

资源

喜欢这个项目吗?

'购买此套件' 按钮会直接带您进入购物车,您可以在购物车中轻松添加或移除产品。