中级
20 分钟

使用BlueNRG-M2和ATmega328P轻松连接所有设备并保持控制

现代专业人士的轻松数据共享!

BLE 6 Click with Arduino UNO Rev3

已发布 6月 27, 2024

点击板

BLE 6 Click

开发板

Arduino UNO Rev3

编译器

NECTO Studio

微控制器单元

ATmega328P

无论您是在外出还是在家中,我们的蓝牙解决方案都确保您始终保持连接,为您提供对您最喜爱的设备的不间断访问。

A

A

硬件概览

它是如何工作的?

BLE 6 Click基于STMicroelectronics的BlueNRG-M2,这是一款符合Bluetooth® v5.0标准的蓝牙低功耗应用处理器模块。BlueNRG-M2模块围绕ST BlueNRG-2 SoC设计,其Cortex-M0核心可以执行蓝牙协议和客户应用程序。优化的存储器架构包括256 kB的Flash存储器和24 kB的超低漏电RAM(具有完全的数据保留)。BLUENRG-M2模块具有32 MHz和32 kHz晶振。它被设计为利用BlueNRG-2集成的DC-DC降压转换器,以实现最佳的活动模式下的功耗。它还嵌入了高效的芯片天线。它可以配置为支持应用处

理器(无主机)和网络处理器(托管)模式。BlueNRG-M2模块在微小的尺寸(11.5 x 13.5 x 2.0 mm)上提供了完整的射频应用平台,并且作为认证解决方案优化了最终应用的上市时间。BlueNRG-M2模块允许应用程序满足使用标准纽扣电池时所需的严格建议的峰值电流要求。有两种可能的软件架构:无主机模式(应用处理器 - 客户应用程序在BlueNRG-M2模块上运行)和托管模式(网络处理器 - 该模块配置为由通过SPI或UART连接的外部主机控制的网络模块)。该模块配备了预编程的UART引导加载程序。

BlueNRG-M2嵌入了ARM串行线调试(SWD)端口,路由到SWD头连接器。它是一个具有两个引脚(时钟和单向双向数据)的调试接口,提供了所有调试功能以及对系统内存的实时访问,无需停止处理器或需要任何目标驻留代码。由于SPI外设可以用作主机或从机,因此标记为MODE SEL的板载SMD跳线允许在两者之间切换。此Click Board™仅设计为使用3.3V逻辑电平。在将Click Board™与具有5V逻辑电平的MCU一起使用之前,应进行适当的逻辑电压级转换。

BLE 6 hardware overview image

功能概述

开发板

Arduino UNO 是围绕 ATmega328P 芯片构建的多功能微控制器板。它为各种项目提供了广泛的连接选项,具有 14 个数字输入/输出引脚,其中六个支持 PWM 输出,以及六个模拟输入。其核心组件包括一个 16MHz 的陶瓷谐振器、一个 USB 连接器、一个电

源插孔、一个 ICSP 头和一个复位按钮,提供了为板 子供电和编程所需的一切。UNO 可以通过 USB 连接到计算机,也可以通过 AC-to-DC 适配器或电池供电。作为第一个 USB Arduino 板,它成为 Arduino 平台的基准,"Uno" 符号化其作为系列首款产品的地

位。这个名称选择,意为意大利语中的 "一",是为了 纪念 Arduino Software(IDE)1.0 的推出。最初与 Arduino Software(IDE)版本1.0 同时推出,Uno 自此成为后续 Arduino 发布的基础模型,体现了该平台的演进。

Arduino UNO Rev3 double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

AVR

MCU 内存 (KB)

32

硅供应商

Microchip

引脚数

28

RAM (字节)

2048

你完善了我!

配件

Click Shield for Arduino UNO 具有两个专有的 mikroBUS™ 插座,使所有 Click board™ 设备能够轻松与 Arduino UNO 板进行接口连接。Arduino UNO 是一款基于 ATmega328P 的微控制器开发板,为用户提供了一种经济实惠且灵活的方式来测试新概念并构建基于 ATmega328P 微控制器的原型系统,结合了性能、功耗和功能的多种配置选择。Arduino UNO 具有 14 个数字输入/输出引脚(其中 6 个可用作 PWM 输出)、6 个模拟输入、16 MHz 陶瓷谐振器(CSTCE16M0V53-R0)、USB 接口、电源插座、ICSP 头和复位按钮。大多数 ATmega328P 微控制器的引脚都连接到开发板左右两侧的 IO 引脚,然后再连接到两个 mikroBUS™ 插座。这款 Click Shield 还配备了多个开关,可执行各种功能,例如选择 mikroBUS™ 插座上模拟信号的逻辑电平,以及选择 mikroBUS™ 插座本身的逻辑电压电平。此外,用户还可以通过现有的双向电平转换电压转换器使用任何 Click board™,无论 Click board™ 运行在 3.3V 还是 5V 逻辑电压电平。一旦将 Arduino UNO 板与 Click Shield for Arduino UNO 连接,用户即可访问数百种 Click board™,并兼容 3.3V 或 5V 逻辑电压电平的设备。

Click Shield for Arduino UNO accessories 1 image

使用的MCU引脚

mikroBUS™映射器

Analog Output
PC0
AN
Reset
PD2
RST
SPI Chip Select
PB2
CS
SPI Clock
PB5
SCK
SPI Data OUT
PB4
MISO
SPI Data IN
PB3
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Boot
PD6
PWM
NC
NC
INT
UART TX
PD0
TX
UART RX
PD1
RX
I2C Clock
PC5
SCL
I2C Data
PC4
SDA
NC
NC
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

BLE 6 Click Schematic schematic

一步一步来

项目组装

Click Shield for Arduino UNO front image hardware assembly

从选择您的开发板和Click板™开始。以Arduino UNO Rev3作为您的开发板开始。

Click Shield for Arduino UNO front image hardware assembly
Arduino UNO Rev3 front image hardware assembly
Charger 27 Click front image hardware assembly
Prog-cut hardware assembly
Arduino UNO Rev3 Access MB 1 - upright/background 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
Arduino UNO 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”替换为要显示的参数。

软件支持

库描述

该库包含 BLE 6 Click 驱动程序的 API。

关键功能:

  • ble6_set_response_handler - 设置响应处理函数。

  • ble6_set_handlers - 设置处理函数。

  • ble6_parser_rsp - 响应解析器函数。

开源

代码示例

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

/*!
 * @file main.c
 * @brief BLE 6 Click Example.
 *
 * # Description
 * This example reads and processes data from BLE 6 clicks.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initialization driver enables - UART, sets handlers initialize and enable UART interrupt, reset
 * and configures BLE module, initialize BLE Server Profile ( Services and Characteristics ).
 *
 * ## Application Task
 * The app starts by checking the system ready flag 
 * and returns the Bluetooth device address. After that,
 * the chain of commands creates Primary Server Profiles:
 * Device Information, Generic Access and Custom Service to Start Advertising.
 * For transmit messages, we use Generic Access Primary Service
 * with Write permissions of the characteristic Element.
 * In this example, transmitting message is limited to a maximum of 11 characters.
 * Results are being sent to the Usart Terminal where you can track their changes.
 *
 * ## Additional Function
 * - void ble6_module_init ( void );
 * - void ble6_event_handler ( void );
 * - void ble6_display_log ( void );
 * - void ble6_aci_gap_init ( void );
 * - void ble6_le_meta_event ( void );
 * - void ble6_handler ( void );
 * - void ble6_response_handler ( void );
 * - void ble6_local_version_info ( void );
 *
 * @note
 * For communication with BLE 6 click use the android application on the link:
 * https://play.google.com/store/apps/details?id=com.macdom.ble.blescanner
 *
 * @author Stefan Ilic
 *
 */

#include "board.h"
#include "log.h"
#include "ble6.h"

#define PROCESS_BUFFER_SIZE 256

static ble6_t ble6;
static log_t logger;
static ble6_rsp_t rsp_data;
static ble6_le_meta_event_t le_meta_event_data;
static ble6_rx_rsp_t ble6_rx_rsp;
uint8_t app_buf [ PROCESS_BUFFER_SIZE ];
uint8_t rx_response [ PROCESS_BUFFER_SIZE ];
uint8_t device_connected_flag = 0;

uint8_t hci_le_meta_event_connect [ 5 ] = { 0x04, 0x3E, 0x13, 0x01, 0x00 };
uint8_t hci_le_serverwrite_event [ 1 ] = { 0x04 };
uint8_t hci_read_local_version_information [ 4 ] = { 0x01, 0x01, 0x10, 0x00 };
uint8_t hci_info_confirm [ 4 ] = { 0xFF, 0x01, 0x00, 0x00 };
uint8_t aci_hal_get_fw_version [ 4 ] = { 0x01, 0x01, 0xFC, 0x00 };
uint8_t hci_reset [ 4 ] = { 0x01, 0x03, 0x0C, 0x00 };
uint8_t aci_hal_write_config_data [ 12 ] = 
{ 
    0x01, 0x0C, 0xFC, 0x08, 0x00, 0x06, 0x03, 0xEE, 0x00, 0xE1, 0x80, 0x02 
};
uint8_t aci_hal_set_tx_power_level [ 6 ] = { 0x01, 0x0F, 0xFC, 0x02, 0x01, 0x04 };
uint8_t aci_gatt_init [ 4 ] = { 0x01, 0x01, 0xFD, 0x00 };
uint8_t aci_gap_init [ 7 ] = { 0x01, 0x8A, 0xFC, 0x03, 0x0F, 0x00, 0x0B };
uint8_t aci_gatt_update_value [ 21 ] = 
{ 
    0x01, 0x06, 0xFD, 0x11, 0x05, 0x00, 0x06, 0x00, 0x00, 0x0B,
    'B', 'L', 'E', ' ', '6', ' ', 'c', 'l', 'i', 'c', 'k' 
};
uint8_t hci_le_set_scan_response_data [ 36 ] = 
{ 
    0x01, 0x09, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 
};
uint8_t aci_gap_set_discoverable [ 30 ] =
{ 
    0x01, 0x83, 0xFC, 0x19, 0x00, 0x00, 0x08, 0x00, 0x09, 0x00, 0x00, 0x0C, 0x09,
    'B', 'L', 'E', ' ', '6', ' ', 'c', 'l', 'i', 'c', 'k', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

/**
 * @brief BLE 6 module initialisation.
 * @details This function is used for initialisation of BLE 6 Click.
 */
void ble6_module_init ( void );

/**
 * @brief BLE 6 event handler.
 * @details This function is used for checking conncection of BLE 6 Click.
 */
void ble6_event_handler ( void );

/**
 * @brief BLE 6 display log.
 * @details This function is used for displaying log of BLE 6 Click.
 */
void ble6_display_log ( void );

/**
 * @brief BLE 6 aci gap initialisation.
 * @details This function is used for aci gap initialisation of BLE 6 Click.
 */
void ble6_aci_gap_init ( void );

/**
 * @brief BLE 6 le meta event.
 * @details This function is used for low energy events of BLE 6 Click.
 */
void ble6_le_meta_event ( void );

/**
 * @brief BLE 6 handler.
 * @details This function is used for getting data of BLE 6 Click.
 */
void ble6_handler ( void );

/**
 * @brief BLE 6 response handler.
 * @details This function is used for displaying data response of BLE 6 Click.
 */
void ble6_response_handler ( void );

/**
 * @brief BLE 6 local version info.
 * @details This function is used for displaying local version info of BLE 6 Click.
 */
void ble6_local_version_info ( void );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    ble6_cfg_t ble6_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.
    ble6_cfg_setup( &ble6_cfg );
    BLE6_MAP_MIKROBUS( ble6_cfg, MIKROBUS_1 );
    err_t init_flag  = ble6_init( &ble6, &ble6_cfg );
    if ( UART_ERROR == init_flag ) 
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );

        for ( ; ; );
    }
    Delay_ms( 1000 );
    ble6_power_on( &ble6, BLE6_MODULE_POWER_ON );
    Delay_ms( 1000 );
    ble6_module_init( );
    Delay_ms( 100 );
    log_printf( &logger, "-> Local Version Information: \r\n" );
    ble6_send_command( &ble6, &hci_read_local_version_information[ 0 ], 4 );
    Delay_ms( 100 );
    ble6_handler( );
    ble6_display_log( );
    ble6_local_version_info( );
    Delay_ms( 100 );

    log_printf( &logger, "--------------------------------\r\n" );
    log_printf( &logger, "-> ACI GAP Update Value: \r\n" );
    ble6_send_command( &ble6, &aci_gatt_update_value[ 0 ], 21 );
    Delay_ms( 10 );
    ble6_handler( );
    ble6_display_log( );
    Delay_ms( 100 );

    log_printf( &logger, "--------------------------------\r\n" );
    log_printf( &logger, "-> HCI Set Scan. Response Data: \r\n" );
    ble6_send_command( &ble6, &hci_le_set_scan_response_data[ 0 ], 36 );
    Delay_ms( 10 );
    ble6_handler( );
    ble6_display_log( );
    Delay_ms( 100 );

    log_printf( &logger, "--------------------------------\r\n" );
    log_printf( &logger, "-> ACI GAP Set Discoverable: \r\n" );
    ble6_send_command( &ble6, &aci_gap_set_discoverable[ 0 ], 30 );
    Delay_ms( 10 );
    ble6_handler( );
    ble6_display_log( );
    Delay_ms( 100 );
    
    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    ble6_event_handler( );
    
    while ( device_connected_flag ) 
    {
        int32_t cnt = ble6_generic_read( &ble6, rx_response, PROCESS_BUFFER_SIZE );
        Delay_ms( 100 );
        if ( ( ble6_strncmp( rx_response, hci_le_serverwrite_event, 1 ) == 0 ) && ( cnt > 13 ) ) 
        {
            ble6_response_handler( );
        }
    }
}

void main ( void ) 
{
    application_init( );

    for ( ; ; ) 
    {
        application_task( );
    }
}

void ble6_handler ( void ) 
{
    uint16_t tmp;
    uint8_t cnt;
    ble6_generic_read( &ble6, rx_response, PROCESS_BUFFER_SIZE );
    
    tmp = rx_response[ 5 ];
    tmp <<= 8;
    tmp |= rx_response[ 4 ];

    rsp_data.event_code = rx_response[ 1 ];
    rsp_data.length = rx_response[ 2 ];
    rsp_data.cmd_opcode = tmp;
    rsp_data.status = rx_response[ 6 ];

    for ( cnt = 0; cnt < rsp_data.length - 4; cnt++ ) 
    {
        rsp_data.payload[ cnt ] = rx_response[ cnt + 7 ];
    }
    
    memset( &rx_response[ 0 ], 0, PROCESS_BUFFER_SIZE );
}

void ble6_connect_handler ( void )
{
    uint8_t cr_len;
    uint16_t tmp;
    uint8_t cnt;
    ble6_generic_read( &ble6, rx_response, PROCESS_BUFFER_SIZE );
    Delay_ms( 100 );
    
    cr_len = 0;

    log_printf( &logger, "\r\n" );

    le_meta_event_data.le_event_code = rx_response[ 1 ];
    le_meta_event_data.le_length = rx_response[ 2 ];
    le_meta_event_data.le_subevent_code = rx_response[ 3 ];
    le_meta_event_data.le_status = rx_response[ 4 ];

    tmp = rx_response[ 6 ];
    tmp <<= 8;
    tmp |= rx_response[ 5 ];

    if ( le_meta_event_data.le_subevent_code == 0x01 )
    {
        cr_len = 8;
    }

    le_meta_event_data.le_conn_hdl = tmp;
    le_meta_event_data.le_role = rx_response[ 7 ];
    le_meta_event_data.le_peer_addr_type = rx_response[ 8 ];

    for ( cnt = 0; cnt < 6; cnt++ )
    {
        le_meta_event_data.le_peer_addr[ cnt ] = rx_response[ cnt + 9 ];
    }

    tmp = rx_response[ 16 - cr_len ];
    tmp <<= 8;
    tmp |= rx_response[ 15 - cr_len ];

    le_meta_event_data.le_conn_interval = tmp;

    tmp = rx_response[ 18 - cr_len ];
    tmp <<= 8;
    tmp |= rx_response[ 17 - cr_len ];

    le_meta_event_data.le_conn_latency = tmp;

    tmp = rx_response[ 20 - cr_len ];
    tmp <<= 8;
    tmp |= rx_response[ 19 - cr_len ];

    le_meta_event_data.le_sup_timeout = tmp;

    le_meta_event_data.le_master_clk_accuracy = rx_response[ 21 ];

    memset( rx_response, 0, PROCESS_BUFFER_SIZE );
}

void ble6_response_handler ( void )
{
    uint16_t attr_pos;
    uint16_t tmp;    
    ble6_rx_rsp.event_code = rx_response[ 1 ];

    log_printf( &logger, " Event Code            : 0x%.4X\r\n", ( uint16_t ) ble6_rx_rsp.event_code );

    ble6_rx_rsp.length = rx_response[ 2 ];
    log_printf( &logger, " Length                : 0x%.4X\r\n", ( uint16_t ) ble6_rx_rsp.length );

    tmp = rx_response[ 4 ];
    tmp <<= 8;
    tmp |= rx_response[ 3 ];

    ble6_rx_rsp.e_code = tmp;
    log_printf( &logger, " Ecode                 : 0x%.4X\r\n", ( uint16_t ) ble6_rx_rsp.e_code );

    tmp = rx_response[ 6 ];
    tmp <<= 8;
    tmp |= rx_response[ 5 ];

    ble6_rx_rsp.conn_hdl = tmp;
    log_printf( &logger, " Connection Handle     : 0x%.4X\r\n", ( uint16_t ) ble6_rx_rsp.conn_hdl );

    tmp = rx_response[ 8 ];
    tmp <<= 8;
    tmp |= rx_response[ 7 ];

    ble6_rx_rsp.attr_hdl = tmp;
    log_printf( &logger, " Attr. Handle          : 0x%.2X\r\n", ( uint16_t ) ble6_rx_rsp.attr_hdl );

    tmp = rx_response[ 10 ];
    tmp <<= 8;
    tmp |= rx_response[ 9 ];

    ble6_rx_rsp.offset = tmp;
    log_printf( &logger, " Offset                : 0x%.2X\r\n", ( uint16_t ) ble6_rx_rsp.offset );

    tmp = rx_response[ 12 ];
    tmp <<= 8;
    tmp |= rx_response[ 11 ];

    ble6_rx_rsp.attr_data_len = tmp;
    log_printf( &logger, " Attr. Data Length     : 0x%.2X\r\n", ( uint16_t ) ble6_rx_rsp.attr_data_len );

    for ( attr_pos = 0; attr_pos < ble6_rx_rsp.attr_data_len; attr_pos++ )
    {
        ble6_rx_rsp.attr_data[ attr_pos ] = rx_response[ attr_pos + 13 ];
        app_buf[ attr_pos ] = ble6_rx_rsp.attr_data[ attr_pos ];
    }

    log_printf( &logger, "- - - - - - - - - - - - - - - - \r\n" );
    log_printf( &logger, " <--- RX DATA : %s \r\n", app_buf );
    log_printf( &logger, "--------------------------------\r\n" );
    memset( app_buf, 0, PROCESS_BUFFER_SIZE );
    memset( rx_response, 0, PROCESS_BUFFER_SIZE );
    memset( ble6_rx_rsp.attr_data, 0, PROCESS_BUFFER_SIZE );
}

void ble6_local_version_info ( void )
{
    uint16_t tmp;
    log_printf( &logger, "- - - - - - - - - - - - - - - - \r\n" );
    log_printf( &logger, " Local Version Information \r\n" );
    log_printf( &logger, "  HCI Version     : 0x%.4X\r\n", ( uint16_t ) rsp_data.payload[ 0 ] );

    tmp = rsp_data.payload[ 2 ];
    tmp <<= 8;
    tmp |= rsp_data.payload[ 1 ];

    log_printf( &logger, "  HCI Revision    : 0x%.2X\r\n", ( uint16_t ) tmp );

    log_printf( &logger, "  LMP/PAL Version : 0x%.2X\r\n", ( uint16_t ) rsp_data.payload[ 3 ] );

    tmp = rsp_data.payload[ 5 ];
    tmp <<= 8;
    tmp |= rsp_data.payload[ 4 ];

    log_printf( &logger, "  Manufacture Name: 0x%.2X\r\n", ( uint16_t ) tmp );

    tmp = rsp_data.payload[ 7 ];
    tmp <<= 8;
    tmp |= rsp_data.payload[ 6 ];

    log_printf( &logger, "  LMP/PAL Sub Ver : 0x%.2X\r\n", ( uint16_t ) tmp );

    memset( rsp_data.payload, 0, PROCESS_BUFFER_SIZE );
}

void ble6_le_meta_event ( void )
{
    uint8_t cnt;
    log_printf( &logger, "    CONNECT LE META EVENT    \r\n" );
    log_printf( &logger, " Event Code            : 0x%.4X\r\n", 
                ( uint16_t ) le_meta_event_data.le_event_code );

    log_printf( &logger, " Length                : 0x%.2X\r\n", 
                ( uint16_t ) le_meta_event_data.le_length );
    
    log_printf( &logger, " Subevent Code         : 0x%.2X\r\n", 
                ( uint16_t ) le_meta_event_data.le_subevent_code );

    log_printf( &logger, " Status                : " );
    if ( le_meta_event_data.le_status == 0x00 )
    {
        log_printf( &logger, "OK\r\n" );
    }
    else
    {
        log_printf( &logger, "ERROR 0x%.4X\r\n", ( uint16_t ) le_meta_event_data.le_status );
    }

    log_printf( &logger, " Connection Handle     : 0x%.4X\r\n", 
                ( uint16_t ) le_meta_event_data.le_conn_hdl );

    if ( le_meta_event_data.le_subevent_code == 0x01 )
    {
        log_printf( &logger, " Role                  : " );
        if ( le_meta_event_data.le_role == 0x00 )
        {
            log_printf( &logger, " Master\r\n" );
        }
        else
        {
            log_printf( &logger, "Slave\r\n" );
        }

        log_printf( &logger, " Peer Address Type     : " );
        if ( le_meta_event_data.le_role == 0x00 )
        {
            log_printf( &logger, "Specific\r\n" );
        }
        else
        {
            log_printf( &logger, "Random\r\n" );
        }
        
        log_printf( &logger, " Peer Address          : " );
        for ( cnt = 0; cnt < 5; cnt++ )
        {
            log_printf( &logger, "%.2X:", ( uint16_t ) le_meta_event_data.le_peer_addr[ cnt ] );
        }
        log_printf( &logger, "%.2X\r\n", ( uint16_t ) le_meta_event_data.le_peer_addr[ 5 ] );
    }
    
    log_printf( &logger, " Connection Interval   : 0x%.4X\r\n", 
                ( uint16_t ) le_meta_event_data.le_conn_interval );
    
    log_printf( &logger, " Connection Latency    : 0x%.4X\r\n", 
                ( uint16_t ) le_meta_event_data.le_conn_latency );
    
    log_printf( &logger, " Supervision Timeout   : 0x%.4X\r\n", 
                ( uint16_t ) le_meta_event_data.le_sup_timeout );
    
    if ( le_meta_event_data.le_subevent_code == 0x01 )
    {
        log_printf( &logger, " Master Clock Accurancy: 0x%.2X\r\n", 
                    ( uint16_t ) le_meta_event_data.le_master_clk_accuracy );
    }
}

void ble6_aci_gap_init ( void )
{
    uint16_t tmp;
    log_printf( &logger, "- - - - - - - - - - - - - - - - \r\n" );
    log_printf( &logger, "    ACI GAP Initialization   \r\n" );

    tmp = rsp_data.payload[ 1 ];
    tmp <<= 8;
    tmp |= rsp_data.payload[ 0 ];

    log_printf( &logger, "  Service Handle    : 0x%.2X\r\n", ( uint16_t ) tmp );
    
    tmp = rsp_data.payload[ 3 ];
    tmp <<= 8;
    tmp |= rsp_data.payload[ 2 ];

    log_printf( &logger, "  Dev Name Char Hdl.: 0x%.2X\r\n", ( uint16_t ) tmp );
    
    tmp = rsp_data.payload[ 5 ];
    tmp <<= 8;
    tmp |= rsp_data.payload[ 4 ];

    log_printf( &logger, "  Appearance Handle : 0x%.2X\r\n", ( uint16_t ) tmp );

    memset( rsp_data.payload, 0, PROCESS_BUFFER_SIZE );
}

void ble6_display_log ( void )
{
    log_printf( &logger, "<- New Response:\r\n" );

    log_printf( &logger, "    Opcode: 0x%.4X\r\n", ( uint16_t ) rsp_data.cmd_opcode );

    log_printf( &logger, "    Length: 0x%.2X\r\n", ( uint16_t ) rsp_data.length );

    log_printf( &logger, "    Status: " );

    if ( rsp_data.status == 0x00 )
    {
        log_printf( &logger, "OK\r\n" );
    }
    else
    {
        log_printf( &logger, "0x%.4X\r\n", ( uint16_t ) rsp_data.status );
    }
}

void ble6_event_handler ( void )
{
    ble6_generic_read( &ble6, rx_response, PROCESS_BUFFER_SIZE );
    Delay_ms( 100 );
    if ( ble6_strncmp( rx_response, hci_le_meta_event_connect, 5 )
         == 0 )
    {
        log_printf( &logger, "--------------------------------\r\n" );
        log_printf( &logger, " ***    Device connected    *** \r\n" );
        log_printf( &logger, "--------------------------------\r\n" );
        ble6_connect_handler( );
        ble6_le_meta_event( );
        log_printf( &logger, "--------------------------------\r\n" );
        Delay_ms( 100 );

        device_connected_flag = 1;
    }
}

void ble6_module_init ( void )
{
    log_printf( &logger, "--------------------------------\r\n" );
    log_printf( &logger, "        *** SW Reset ***        \r\n" );
    ble6_send_command( &ble6, hci_info_confirm, 4 );
    Delay_ms( 10 );
    memset( rx_response, 0, 255 );
    Delay_ms( 100 );
    ble6_generic_read( &ble6, rx_response, PROCESS_BUFFER_SIZE );
    Delay_ms( 100 );

    log_printf( &logger, "--------------------------------\r\n" );
    log_printf( &logger, "-> Firmware Details: \r\n" );
    ble6_send_command( &ble6, aci_hal_get_fw_version, 4 );
    Delay_ms( 10 );
    ble6_handler( );
    ble6_display_log( );
    Delay_ms( 100 );

    log_printf( &logger, "--------------------------------\r\n" );
    log_printf( &logger, "-> HCI Reset: \r\n" );
    ble6_send_command( &ble6, hci_reset, 4 );
    Delay_ms( 10 );
    ble6_handler( );
    ble6_display_log( );
    Delay_ms( 10 );
    ble6_handler( );
    ble6_display_log( );
    Delay_ms( 100 );

    log_printf( &logger, "--------------------------------\r\n" );
    log_printf( &logger, "-> ACI HAL Write Configuration: \r\n" );
    ble6_send_command( &ble6, aci_hal_write_config_data, 12 );
    Delay_ms( 10 );
    ble6_handler( );
    ble6_display_log( );
    Delay_ms( 100 );

    log_printf( &logger, "--------------------------------\r\n" );
    log_printf( &logger, "-> ACI Set Tx Power Level: \r\n" );
    ble6_send_command( &ble6, aci_hal_set_tx_power_level, 6 );
    Delay_ms( 10 );
    ble6_handler( );
    ble6_display_log( );
    Delay_ms( 100 );

    log_printf( &logger, "--------------------------------\r\n" );
    log_printf( &logger, "-> ACI GATT Init.: \r\n" );
    ble6_send_command( &ble6, aci_gatt_init, 4 );
    Delay_ms( 10 );
    ble6_handler( );
    ble6_display_log( );
    Delay_ms( 100 );

    log_printf( &logger, "--------------------------------\r\n" );
    log_printf( &logger, "-> ACI GAP Init.: \r\n" );
    ble6_send_command( &ble6, aci_gap_init, 7 );
    Delay_ms( 10 );
    ble6_handler( );
    ble6_display_log ();
    ble6_aci_gap_init( );
    Delay_ms( 100 );
}

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

额外支持

资源

喜欢这个项目吗?

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