初学者
10 分钟

使用B102C和PIC32MX795F512L在短距离内连接设备

Bluetooth 5.0(BLE)通信,具有先进的连接性和能源效率

B102C Click with Explorer 16/32 development board

已发布 9月 02, 2024

点击板

B102C Click

开发板

Explorer 16/32 development board

编译器

NECTO Studio

微控制器单元

PIC32MX795F512L

完整的射频解决方案,集成了 2.4GHz 天线,适用于信标、楼宇自动化和遥控玩具等应用

A

A

硬件概览

它是如何工作的?

B102C Click 基于 Amphenol 的 B102C,这是一款支持 Bluetooth 5.0 (BLE) 的模块。该模块基于 Realtek RTL8762CMF,支持 BLE v5.0,内置强大的 20MHz Arm® Cortex® M4F 处理器,具有卓越的能效。它提供了一个完整的 RF 解决方案,集成了在 2.4GHz 范围(2402 - 2480MHz)内运行的天线,以及一个低功耗晶体,通过启用先进的省电模式来优化功耗。B102C Click 适用于多种应用,如信标、楼宇自动化、遥控玩具、照明产品等。B102C 模块预装了 Amphenol 固件,支持蓝牙低功耗串行端口服务,并能同时作为外围设备和中央角色,所有功能均可通过 AT 命令配置。它还允许在内置的 Cortex-M4 上添加

自定义应用程序,拥有 4Mbit 的闪存、4Kb eFuse 和 160KB SRAM。在连接功能方面,该 Click 板通过 UART 接口与主 MCU 通信,使用标准的 UART RX 和 TX 引脚交换 AT 命令。默认情况下,通信波特率为 115200bps。此外,该 Click 板配备了 USB Type-C 接口,允许通过 PC 进行供电和配置。通过集成的 CP2102 USB 转 UART 桥接器和 TPS727 LDO 稳压器,提供了模块所需的 3.3V 电源。板子的左侧还设计了一个未焊接的 PROG 接口,提供调试和编程支持。用户可以通过该接口利用串行线调试 (SWD) 接口进行编程和调试。除了通信和控制引脚之外,该 Click 板还包括一个复位引脚 (RST) 和一个 RESET 按钮

,便于模块重置。模块的所有 GPIO 引脚都连接到两个未焊接的 J1 和 J2 接口。这些引脚是完全可编程的,用户可以为每个引脚选择上拉和下拉电阻。当系统进入睡眠模式时,这些引脚会保持其最后的状态,并允许通过任何 GPIO 引脚在睡眠模式下唤醒模块。其中三个引脚还连接到 mikroBUS™ 插座 (P01, P22, 和 P23),可以用于 AN, PWM 和 INT 默认位置,用户可以根据需要配置这些可编程引脚。该 Click 板只能在 3.3V 逻辑电压下工作。使用不同逻辑电平的 MCU 之前,必须进行适当的逻辑电压电平转换。此外,该 Click 板还配备了一个包含函数库和示例代码的开发库,可作为进一步开发的参考。

B102C Click hardware overview image

功能概述

开发板

Explorer 16/32 开发板是一款灵活方便的开发、演示和测试平台,适用于 Microchip Technology 的 16 位 PIC24 MCU、dsPIC® DSC 和 32 位 PIC32 MCU。该板提供了开发和调试完整嵌入式应用所需的所有硬件。该板接受为 Explorer 16 或 Explorer 16/32 开发板设计的处理器插件模块(PIM),便于设备更换。除了板载提供的硬件功能外,还可以通过使用 PICtail™ Plus 女儿卡和 mikroBUS™ 配件板来扩展

硬件。与集成的 PICkit™-On-Board (PKOB) 和 MPLAB ICD 在线电路调试器的实时调试设施结合使用,可以加快应用程序的评估和原型制作。这款开发板支持所有 Explorer PIM,但并非所有 PIM 都由 PKOB 支持。要检查支持和不支持的 PIM 列表,请参考 PICkit™ On-Board 3 (PKOB3) 支持列表。对于不在 PKOB3 支持列表上的 PIM,使用 JP1 或 J14 连接 器通过新一代编程工具对设备进行编程。Explorer

 16/32 开发板仅提供主板,允许自定义其他必要组件。根据所考虑的 MCU 和 DSC,从广泛的处理器插件模块中选择您的 PIM。这款板是从经典的 Explorer 16 迁移到新的 Explorer 16/32 平台的客户的最佳选择,同时所有必要的附加组件,如处理器插件模块和 PICtail™ Plus 女儿板,已经可用。

Explorer 16/32 double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC32

MCU 内存 (KB)

512

硅供应商

Microchip

引脚数

100

RAM (字节)

131072

使用的MCU引脚

mikroBUS™映射器

P0_1 General-Purpose I/O
PB0
AN
Reset
PF3
RST
ID COMM
PG9
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
P2_2 General-Purpose I/O
PD0
PWM
P2_3 General-Purpose I/O
PA15
INT
UART TX
PF8
TX
UART RX
PF2
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

B102C Click Schematic schematic

一步一步来

项目组装

Explorer 16/32 front image hardware assembly

从选择您的开发板和Click板™开始。以Explorer 16/32 development board作为您的开发板开始。

Explorer 16/32 front image hardware assembly
Thermo 28 Click front image hardware assembly
PIM for PIC32MZ2048EFH100 front image hardware assembly
Prog-cut hardware assembly
Explorer 16/32 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
Explorer 16/32 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”替换为要显示的参数。

软件支持

库描述

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

关键功能:

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

  • b102c_send_cmd_with_params - 此函数向 Click 模块发送带有指定参数的命令。

  • b102c_send_cmd_params_check - 此函数检查已发送的命令。

开源

代码示例

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

/*!
 * @file main.c
 * @brief B102C Click Example.
 *
 * # Description
 * This example demonstrates the use of B102C click board by processing
 * the incoming data and displaying them on the USB UART.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver, then performs a factory reset and sets the local device name.
 *
 * ## Application Task
 * Reads and processes all incoming data and displays them on the USB UART.
 *
 * ## Additional Function
 * - static void b102c_clear_app_buf ( void )
 * - static void b102c_log_app_buf ( void )
 * - static err_t b102c_process ( b102c_t *ctx )
 * - static err_t b102c_rsp_check ( b102c_t *ctx, uint8_t *response )
 * - static void b102c_error_check( err_t error_flag )
 *
 * @note
 * For communication with B102C 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 "b102c.h"

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

static b102c_t b102c;
static log_t logger;

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

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

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

/**
 * @brief B102C data reading function.
 * @details This function reads data from device and concatenates data to application buffer. 
 * @param[in] ctx : Click context object.
 * See #b102c_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 b102c_process ( b102c_t *ctx );

/**
 * @brief B102C response read function.
 * @details This function reads the response of the sent command and writes it on the USB UART.
 * @param[in] ctx : Click context object.
 * See #b102c_t object definition for detailed explanation.
 * @param[in] response : Expected response.
 * @return @li @c  0 - Response OK.
 *         @li @c -1 - Response ERROR.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t b102c_rsp_check ( b102c_t *ctx, uint8_t *response );

/**
 * @brief Check for errors.
 * @details This function checks for different types of 
 * errors and logs them on UART or logs the response if no errors occured.
 * @param[in] error_flag : Response to be checked.
 * @note None.
 */
static void b102c_error_check( err_t error_flag );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    b102c_cfg_t b102c_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.
    b102c_cfg_setup( &b102c_cfg );
    B102C_MAP_MIKROBUS( b102c_cfg, MIKROBUS_1 );
    if ( UART_ERROR == b102c_init( &b102c, &b102c_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    Delay_ms ( 100 );

    log_printf( &logger, " Software reset. \r\n" );
    b102c_send_cmd( &b102c, B102C_CMD_RESET );
    app_error_flag = b102c_rsp_check( &b102c, B102C_RSP_READY );
    
    log_printf( &logger, " Factory reset. \r\n" );
    b102c_send_cmd( &b102c, B102C_CMD_DEFAULT );
    app_error_flag = b102c_rsp_check( &b102c, B102C_RSP_READY );

    log_printf( &logger, " Set device name. \r\n" );
    #define DEVICE_NAME             "B102C Click"
    b102c_send_cmd_with_params( &b102c, B102C_CMD_NAME, DEVICE_NAME );
    app_error_flag = b102c_rsp_check( &b102c, B102C_RSP_OK );

    log_printf( &logger, " Set Auto broadcast mode. \r\n" );
    #define AUTO_BROADCAST          "1"
    b102c_send_cmd_with_params( &b102c, B102C_CMD_ADVMOD, AUTO_BROADCAST );
    app_error_flag = b102c_rsp_check( &b102c, B102C_RSP_OK );

    log_info( &logger, " Application Task " );
    log_printf( &logger, " Connect to device. " );
}

void application_task ( void ) 
{
    if ( B102C_OK == b102c_process( &b102c ) ) 
    {
        b102c_log_app_buf( );
        b102c_clear_app_buf( );
    }
}

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 b102c_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
}

static void b102c_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 b102c_process ( b102c_t *ctx ) 
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t overflow_bytes = 0;
    int32_t rx_cnt = 0;
    int32_t rx_size = b102c_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 B102C_OK;
    }
    return B102C_ERROR;
}

static err_t b102c_rsp_check ( b102c_t *ctx, uint8_t *response ) 
{
    uint16_t timeout_cnt = 0;
    uint16_t timeout = 50000;
    
    err_t error_flag = b102c_process( ctx );
    if ( ( error_flag != 0 ) && ( error_flag != -1 ) ) 
    {
        return error_flag;
    }
    
    while ( ( strstr( app_buf, response ) == 0 ) && ( strstr( app_buf, B102C_RSP_ERROR ) == 0 ) ) 
    {
        error_flag = b102c_process( ctx );
        if ( ( error_flag != 0 ) && ( error_flag != -1 ) ) 
        {
            return error_flag;
        }
        
        timeout_cnt++;
        if ( timeout_cnt > timeout ) 
        {
            return B102C_TIMEOUT;
        }
        
        Delay_ms ( 1 );
    }
    
    for ( int32_t buf_cnt = 0; buf_cnt < app_buf_len; buf_cnt++ ) 
    {
        log_printf( &logger, "%c", app_buf[ buf_cnt ] );
    }
    log_printf( &logger, "\r\n" );
    b102c_clear_app_buf( );
    Delay_ms ( 500 );
    
    log_printf( &logger, "-----------------------------------\r\n" );
}

static void b102c_error_check( err_t error_flag ) 
{
    if ( ( error_flag != 0 ) && ( error_flag != B102C_ERROR ) ) 
    {
        switch ( error_flag ) 
        {
            case B102C_OVERFLOW: 
            {
                log_error( &logger, " Overflow!" );
                break;
            }
            case B102C_TIMEOUT:
            {
                log_error( &logger, " Timeout!" );
                break;
            }
            default: 
            {
                break;
            }
        }
    }
}


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

额外支持

资源

喜欢这个项目吗?

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