中级
20 分钟

使用453-00139R和STM32F446RE实现低功耗的远距离安全可靠数据传输

适用于915MHz物联网应用的超低功耗LoraWAN解决方案

LR 11 Click - 915MHz with Nucleo 64 with STM32F446RE MCU

已发布 10月 08, 2024

点击板

LR 11 Click - 915MHz

开发板

Nucleo 64 with STM32F446RE MCU

编译器

NECTO Studio

微控制器单元

STM32F446RE

这是需要可靠的915MHz长距离广域通信应用的理想解决方案

A

A

硬件概览

它是如何工作的?

LR 11 Click - 915MHz 基于 Ezurio 的 453-00139R,这是一款超低功耗的 LoRaWAN 模块,属于 Ezurio RM126x 系列,特别是 RM1262。该模块集成了 Silicon Labs 的 EFR32 系列系统芯片 (SoC) 和 Semtech 的 SX1262 射频芯片,提供了一个高效、低功耗、长距离的解决方案,用于开发广泛的 LoRaWAN™ 应用,并获得 LoRa 联盟的认证。该模块配备了内置的 MHF4 连接器、温度补偿晶体振荡器 (TCXO) 和 DC-DC 转换器,确保在各种环境中的可靠性能。453-00139R 支持 LoRaWAN 类别 A、B 和 C,提供安全、可扩展和双向通信。它具有广泛的监管区域支持,包括美国、加拿大、澳大利亚和新西兰,并拥有 EU、UKCA、NCC、MIC 和 IN 等认证,使其成为各种需要可靠长距离通信的用例的高效解决方案。除了 LoRaWAN 功能外,453-00139R 还具有 LoRa 点对点 (LoRa P2P) 功能,支持在两个 RM126x 模块之间创建私有的超长距离无线电网络。该功能支持单播和广播模式,每个网络可容纳多达 64 台设备。该模块设计为可在主机模式和无主机模式下操作。在主机模式下,它通过 AT 命令集进行编程,

而在无主机模式下,它使用其内置的 Cortex-M33 核心,具有 512kB 闪存和 32kB RAM。模块的主要特性还包括 902-928MHz 的频率范围(典型为 915MHz)、最大发射功率可达 22dBm,以及在开放空间中的通信范围可达 15 公里。此 Click board™ 是物联网设备、资产跟踪与控制、智能家居系统、公用或私有网络、灌溉与农业应用、工业自动化及任何长距离、依赖电池供电的传感器应用的理想选择。453-00139R 模块与主机 MCU 通过 UART 接口进行通信,使用标准的 UART RX 和 TX 引脚,以及硬件流控制引脚(CTS/RTS- Clear to Send/Ready to Send)以实现高效的数据传输。默认通信速度为 115200bps,允许通过 AT 命令进行数据交换。在 LR 11 Click 的底部,有一个额外的未填充接头,提供对调试和编程功能的全面支持。通过此接头,用户可以使用串行线调试接口(通过 SWD 接口引脚 SWDIO、SWCLK 和 SWO 提供)进行编程和调试。除了接口引脚外,该 Click board™ 还具有一个复位引脚/按钮 (RESET) 用于直接重置模块,以及一个 BOOT 按钮用于确定何时执行引导加载程序。复位后,引导加载

程序开始执行。当按下 BOOT 按钮时,引导加载程序通过 UART 执行固件更新。松开按钮时,引导加载程序停止执行并将控制权交给主应用固件。LR 11 Click 的一个特殊功能是额外的 mikroBUS™ 插座,它通过 UART、SPI 或 I2C 接口与板载的 453-00139R 模块通信,为外设(如传感器和 LCD)扩展了板子的功能。由于 453-00139R 的 GPIO PD02 和 PD03 引脚在多个信号之间共享(可用于所有三个接口),因此在选择通信模式时必须适当地配置它们。这可以通过位于外围 mikroBUS™ 插座部分的六个焊盘来实现,使得模块的 PD02 和 PD03 引脚可以设置为所需的接口。默认情况下,选择 SPI 通信,并连接 CIPO 和 COPI 引脚。如果需要其他接口,则需要切断这些走线(断开它们),然后焊接所需接口引脚(UART 或 I2C)的焊盘。此 Click board™ 拥有外围 mikroBUS™ 插座所需的两个 mikroBUS™ 电源轨,但仅使用 3.3V 电压作为 453-00139R 模块的主电源。在使用具有不同逻辑电平的 MCU 之前,必须执行适当的逻辑电压电平转换。它还配备了包含功能和示例代码的库,可作为进一步开发的参考。

LR 11 Click - 915MHz hardware overview image

功能概述

开发板

Nucleo-64 搭载 STM32F446RE 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 STM32F446RE MCU double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

ARM Cortex-M4

MCU 内存 (KB)

512

硅供应商

STMicroelectronics

引脚数

64

RAM (字节)

131072

你完善了我!

配件

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

Sub-GHz FlexDIPOLE天线,特别是Ezurio 868/915 MHz型号,是一款紧凑且高度多功能的天线,设计用于在整个868和915 MHz ISM频段内提供强大且一致的性能。其小巧的尺寸与灵活的偶极天线技术相结合,使其成为LoRaWAN和其他Sub-GHz技术等苛刻应用的理想选择。该天线配备了MHF4L连接器选项,确保易于集成到各种系统中。此外,其背胶设计允许快速且简便的安装,在挑战性的实际环境中提供从863到928 MHz的可靠覆盖。

LR 11 Click - 915MHz accessories 1 image

使用的MCU引脚

mikroBUS™映射器

NC
NC
AN
Reset / ID SEL
PC12
RST
UART CTS / ID COMM
PB12
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
UART RTS
PC14
INT
UART TX
PA2
TX
UART RX
PA3
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

LR 11 Click - 915MHz Schematic schematic

一步一步来

项目组装

Click Shield for Nucleo-64 accessories 1 image hardware assembly

从选择您的开发板和Click板™开始。以Nucleo 64 with STM32F446RE 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
Nucleo-64 with STM32XXX MCU Access MB 1 Mini B Conn - 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
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”替换为要显示的参数。

软件支持

库描述

该库包含 LR 11 Click - 915MHz 驱动程序的 API

关键功能:

  • lr11915mhz_reset_device - 此函数通过切换复位引脚的逻辑状态来重置设备。

  • lr11915mhz_cmd_run - 此函数向 Click 模块发送指定的命令(带或不带参数)。

  • lr11915mhz_cmd_set - 此函数为 Click 模块的指定命令参数设置值。

开源

代码示例

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

/*!
 * @file main.c
 * @brief LR 11 915MHz Click Example.
 *
 * # Description
 * This example demonstrates the use of LR 11 915MHz click board by showing
 * the communication between two click boards configured in P2P network mode.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and logger.
 *
 * ## Application Task
 * Application task is split in few stages:
 *  - LR11915MHZ_POWER_UP:
 * Powers up the device, performs a device factory reset and reads system information.
 *  - LR11915MHZ_CONFIG_EXAMPLE:
 * Configures device for the LoRa P2P network mode.
 *  - LR11915MHZ_EXAMPLE:
 * Performs a LoRa P2P example by exchanging messages with another LR 11 915MHz click board.
 * One device should be set to NODE_0_ADDRESS, and the other to NODE_1_ADDRESS.
 *
 * ## Additional Function
 * - static void lr11915mhz_clear_app_buf ( void )
 * - static void lr11915mhz_log_app_buf ( void )
 * - static err_t lr11915mhz_process ( lr11915mhz_t *ctx )
 * - static err_t lr11915mhz_read_response ( lr11915mhz_t *ctx, uint8_t *rsp )
 * - static err_t lr11915mhz_power_up ( lr11915mhz_t *ctx )
 * - static err_t lr11915mhz_config_example ( lr11915mhz_t *ctx )
 * - static err_t lr11915mhz_example ( lr11915mhz_t *ctx )
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "lr11915mhz.h"
#include "conversions.h"
#include "generic_pointer.h"

// Node address selection macros
#define NODE_0_ADDRESS      0
#define NODE_1_ADDRESS      1
#define NODE_ADDRESS        NODE_0_ADDRESS

// Text message for transmittion
#define DEMO_TEXT_MESSAGE   "MIKROE - LR 11 915MHz click board"

static lr11915mhz_t lr11915mhz;
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
{
    LR11915MHZ_POWER_UP = 1,
    LR11915MHZ_CONFIG_EXAMPLE,
    LR11915MHZ_EXAMPLE

} lr11915mhz_app_state_t;

static lr11915mhz_app_state_t app_state = LR11915MHZ_POWER_UP;

/**
 * @brief LR 11 915MHz clearing application buffer.
 * @details This function clears memory of application buffer and reset its length.
 * @note None.
 */
static void lr11915mhz_clear_app_buf ( void );

/**
 * @brief LR 11 915MHz log application buffer.
 * @details This function logs data from application buffer to USB UART.
 * @note None.
 */
static void lr11915mhz_log_app_buf ( void );

/**
 * @brief LR 11 915MHz data reading function.
 * @details This function reads data from device and concatenates data to application buffer. 
 * @param[in] ctx : Click context object.
 * See #lr11915mhz_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 lr11915mhz_process ( lr11915mhz_t *ctx );

/**
 * @brief LR 11 915MHz 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 #lr11915mhz_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 lr11915mhz_read_response ( lr11915mhz_t *ctx, uint8_t *rsp );

/**
 * @brief LR 11 915MHz power up function.
 * @details This function powers up the device, performs device factory reset and reads system information.
 * @param[in] ctx : Click context object.
 * See #lr11915mhz_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 lr11915mhz_power_up ( lr11915mhz_t *ctx );

/**
 * @brief LR 11 915MHz config example function.
 * @details This function configures device for LoRa P2P example.
 * @param[in] ctx : Click context object.
 * See #lr11915mhz_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 lr11915mhz_config_example ( lr11915mhz_t *ctx );

/**
 * @brief LR 11 915MHz example function.
 * @details This function performs a LoRa P2P example by exchanging messages with another LR 11 915MHz click board.
 * @param[in] ctx : Click context object.
 * See #lr11915mhz_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 lr11915mhz_example ( lr11915mhz_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    lr11915mhz_cfg_t lr11915mhz_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.
    lr11915mhz_cfg_setup( &lr11915mhz_cfg );
    LR11915MHZ_MAP_MIKROBUS( lr11915mhz_cfg, MIKROBUS_1 );
    if ( UART_ERROR == lr11915mhz_init( &lr11915mhz, &lr11915mhz_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );

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

void application_task ( void ) 
{
    switch ( app_state )
    {
        case LR11915MHZ_POWER_UP:
        {
            if ( LR11915MHZ_OK == lr11915mhz_power_up( &lr11915mhz ) )
            {
                app_state = LR11915MHZ_CONFIG_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - CONFIG EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case LR11915MHZ_CONFIG_EXAMPLE:
        {
            if ( LR11915MHZ_OK == lr11915mhz_config_example( &lr11915mhz ) )
            {
                app_state = LR11915MHZ_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case LR11915MHZ_EXAMPLE:
        {
            lr11915mhz_example( &lr11915mhz );
            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 lr11915mhz_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
}

static void lr11915mhz_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 lr11915mhz_process ( lr11915mhz_t *ctx ) 
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t overflow_bytes = 0;
    int32_t rx_cnt = 0;
    int32_t rx_size = lr11915mhz_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 LR11915MHZ_OK;
    }
    return LR11915MHZ_ERROR;
}

static err_t lr11915mhz_read_response ( lr11915mhz_t *ctx, uint8_t *rsp ) 
{
    #define READ_RESPONSE_TIMEOUT_MS 30000
    uint32_t timeout_cnt = 0;
    lr11915mhz_clear_app_buf ( );
    lr11915mhz_process( ctx );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, LR11915MHZ_RSP_ERROR ) ) )
    {
        lr11915mhz_process( ctx );
        if ( timeout_cnt++ > READ_RESPONSE_TIMEOUT_MS )
        {
            lr11915mhz_clear_app_buf( );
            log_error( &logger, " Timeout!" );
            return LR11915MHZ_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    Delay_ms ( 200 );
    lr11915mhz_process( ctx );
    if ( strstr( app_buf, rsp ) )
    {
        lr11915mhz_log_app_buf( );
        log_printf( &logger, "--------------------------------\r\n" );
        return LR11915MHZ_OK;
    }
    lr11915mhz_log_app_buf( );
    return LR11915MHZ_ERROR_CMD;
}

static err_t lr11915mhz_power_up ( lr11915mhz_t *ctx )
{
    err_t error_flag = LR11915MHZ_OK;
    
    log_printf( &logger, ">>> Reset device.\r\n" );
    lr11915mhz_reset_device( &lr11915mhz );
    while ( LR11915MHZ_OK == lr11915mhz_process( ctx ) )
    {
        lr11915mhz_log_app_buf( );
        lr11915mhz_clear_app_buf ( );
    }
    log_printf( &logger, "--------------------------------\r\n" );

    log_printf( &logger, ">>> Factory reset.\r\n" );
    lr11915mhz_cmd_run( &lr11915mhz, LR11915MHZ_CMD_FACTORY_RESET, NULL );
    error_flag |= lr11915mhz_read_response( &lr11915mhz, LR11915MHZ_RSP_OK );

    log_printf( &logger, ">>> Check communication.\r\n" );
    lr11915mhz_cmd_run( &lr11915mhz, LR11915MHZ_CMD_AT, NULL );
    error_flag |= lr11915mhz_read_response( &lr11915mhz, LR11915MHZ_RSP_OK );

    log_printf( &logger, ">>> Get device type.\r\n" );
    lr11915mhz_cmd_run( ctx, LR11915MHZ_CMD_GET_INFO, LR11915MHZ_PARAM_DEVICE_TYPE );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );

    log_printf( &logger, ">>> Get firmware version.\r\n" );
    lr11915mhz_cmd_run( ctx, LR11915MHZ_CMD_GET_INFO, LR11915MHZ_PARAM_APP_FW_VERSION );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );

    return error_flag;
}

static err_t lr11915mhz_config_example ( lr11915mhz_t *ctx )
{
    uint8_t data_buf[ 10 ] = { 0 };
    err_t error_flag = LR11915MHZ_OK;
    #define DEVICE_CLASS_P2P "3"
    log_printf( &logger, ">>> Set LoRa operation to P2P.\r\n" );
    lr11915mhz_cmd_set( ctx, LR11915MHZ_CMD_PARAM_ACCESS_NUM, LR11915MHZ_PARAM_ID_DEVICE_CLASS, DEVICE_CLASS_P2P );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );

    #define REGION_US902_928 "3"
    log_printf( &logger, ">>> Set operation region to US902_928.\r\n" );
    lr11915mhz_cmd_set( ctx, LR11915MHZ_CMD_PARAM_ACCESS_NUM, LR11915MHZ_PARAM_ID_REGION, REGION_US902_928 );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );

    int16_to_str ( NODE_ADDRESS, data_buf );
    l_trim ( data_buf );
    r_trim ( data_buf );
    log_printf( &logger, ">>> Set P2P node address to %s.\r\n", data_buf );
    lr11915mhz_cmd_set( ctx, LR11915MHZ_CMD_PARAM_ACCESS_NUM, LR11915MHZ_PARAM_ID_P2P_DEVICE_ADDRESS, data_buf );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );

    #define P2P_NETWORK_SIZE "2"
    log_printf( &logger, ">>> Set P2P network size to %s nodes.\r\n", ( char * ) P2P_NETWORK_SIZE );
    lr11915mhz_cmd_set( ctx, LR11915MHZ_CMD_PARAM_ACCESS_NUM, LR11915MHZ_PARAM_ID_P2P_NET_SIZE, P2P_NETWORK_SIZE );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    #define P2P_DATA_RATE_DR2 "2"
    log_printf( &logger, ">>> Set P2P data rate to DR2.\r\n" );
    lr11915mhz_cmd_set( ctx, LR11915MHZ_CMD_PARAM_ACCESS_NUM, LR11915MHZ_PARAM_ID_P2P_DATA_RATE, P2P_DATA_RATE_DR2 );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    #define P2P_LISTEN_DURATION "0"
    log_printf( &logger, ">>> Set P2P listen duration to 0.\r\n" );
    lr11915mhz_cmd_set( ctx, LR11915MHZ_CMD_PARAM_ACCESS_NUM, LR11915MHZ_PARAM_ID_P2P_LISTEN_DURATION, P2P_LISTEN_DURATION );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    #define P2P_LISTEN_INTERVAL "0"
    log_printf( &logger, ">>> Set P2P listen interval to 0.\r\n" );
    lr11915mhz_cmd_set( ctx, LR11915MHZ_CMD_PARAM_ACCESS_NUM, LR11915MHZ_PARAM_ID_P2P_LISTEN_INTERVAL, P2P_LISTEN_INTERVAL );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    #define P2P_BEACON_DATA_RATE_DR2 "2"
    log_printf( &logger, ">>> Set P2P beacon data rate to DR2.\r\n" );
    lr11915mhz_cmd_set( ctx, LR11915MHZ_CMD_PARAM_ACCESS_NUM, LR11915MHZ_PARAM_ID_P2P_BEACON_DATA_RATE, P2P_BEACON_DATA_RATE_DR2 );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    #define P2P_TX_POWER "1"
    log_printf( &logger, ">>> Set P2P TX power to %s dBm.\r\n", ( char * ) P2P_TX_POWER );
    lr11915mhz_cmd_set( ctx, LR11915MHZ_CMD_PARAM_ACCESS_NUM, LR11915MHZ_PARAM_ID_P2P_TX_POWER, P2P_TX_POWER );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Save settings.\r\n" );
    lr11915mhz_cmd_run( &lr11915mhz, LR11915MHZ_CMD_SAVE_SETTINGS, NULL );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Reboot.\r\n" );
    lr11915mhz_cmd_run( &lr11915mhz, LR11915MHZ_CMD_WARM_RESET, NULL );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Get P2P maximum packet size.\r\n" );
    lr11915mhz_cmd_run( ctx, LR11915MHZ_CMD_GET_INFO, LR11915MHZ_PARAM_P2P_SLOT_PACKET_SIZE );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    int16_t packet_size = atoi( &app_buf[ 1 ] ) - 1;
    if ( packet_size < strlen ( DEMO_TEXT_MESSAGE ) )
    {
        log_error( &logger, " DEMO_TEXT_MESSAGE length [%d] exceeds the packet size limit [%d].\r\n", 
                   ( int16_t ) strlen ( DEMO_TEXT_MESSAGE ), packet_size );
        for ( ; ; );
    }
    
    int16_to_str ( strlen ( DEMO_TEXT_MESSAGE ), data_buf );
    l_trim ( data_buf );
    r_trim ( data_buf );
    log_printf( &logger, ">>> Set P2P packet size to %s.\r\n", data_buf );
    lr11915mhz_cmd_set( ctx, LR11915MHZ_CMD_PARAM_ACCESS_NUM, LR11915MHZ_PARAM_ID_P2P_PACKET_SIZE, data_buf );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Save settings.\r\n" );
    lr11915mhz_cmd_run( &lr11915mhz, LR11915MHZ_CMD_SAVE_SETTINGS, NULL );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Reboot.\r\n" );
    lr11915mhz_cmd_run( &lr11915mhz, LR11915MHZ_CMD_WARM_RESET, NULL );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Get P2P minimum window length.\r\n" );
    lr11915mhz_cmd_run( ctx, LR11915MHZ_CMD_GET_INFO, LR11915MHZ_PARAM_P2P_MIN_WINDOW_LENGTH );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    int16_t window_len = atoi( &app_buf[ 1 ] ) + 1;
    int16_to_str ( window_len, data_buf );
    l_trim ( data_buf );
    r_trim ( data_buf );
    log_printf( &logger, ">>> Set P2P window length to %s.\r\n", data_buf );
    lr11915mhz_cmd_set( ctx, LR11915MHZ_CMD_PARAM_ACCESS_NUM, LR11915MHZ_PARAM_ID_P2P_WINDOW_LENGTH, data_buf );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Save settings.\r\n" );
    lr11915mhz_cmd_run( &lr11915mhz, LR11915MHZ_CMD_SAVE_SETTINGS, NULL );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Reboot.\r\n" );
    lr11915mhz_cmd_run( &lr11915mhz, LR11915MHZ_CMD_WARM_RESET, NULL );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Start a P2P session.\r\n" );
    lr11915mhz_cmd_run( &lr11915mhz, LR11915MHZ_CMD_P2P_START_SESSION, NULL );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );

    return error_flag;
}

static err_t lr11915mhz_example ( lr11915mhz_t *ctx )
{
    err_t error_flag = LR11915MHZ_OK;
    uint8_t param_buf[ 120 ] = { 0 };
    uint8_t msg_hex[ 101 ] = { 0 };
    uint8_t byte_hex[ 3 ] = { 0 };
    uint8_t src_addr[ 10 ] = { 0 };
    uint8_t source[ 10 ] = { 0 };
    uint8_t rssi[ 10 ] = { 0 };
    uint8_t snr[ 10 ] = { 0 };
    uint8_t cnt = 0;

    memset( msg_hex, 0, sizeof ( msg_hex ) );
    for ( cnt = 0; ( cnt < strlen ( DEMO_TEXT_MESSAGE ) ) && ( cnt < 50 ); cnt++ ) 
    {
        uint8_to_hex ( DEMO_TEXT_MESSAGE[ cnt ], byte_hex );
        strcat ( msg_hex, byte_hex );
    }
#if ( NODE_ADDRESS == NODE_0_ADDRESS )
    int16_to_str ( NODE_1_ADDRESS, param_buf );
    l_trim ( param_buf );
    r_trim ( param_buf );
#else
    int16_to_str ( NODE_0_ADDRESS, param_buf );
    l_trim ( param_buf );
    r_trim ( param_buf );
#endif
    log_printf( &logger, ">>> Send message \"%s\" to node address %s.\r\n", ( char * ) DEMO_TEXT_MESSAGE, param_buf );
    strcat ( param_buf, ", \"" );
    strcat ( param_buf, msg_hex );
    strcat ( param_buf, "\"" );
    lr11915mhz_cmd_run( ctx, LR11915MHZ_CMD_P2P_SEND_DATA, param_buf );
    error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_RSP_OK );
    
    if ( LR11915MHZ_OK == error_flag )
    {
        memset( msg_hex, 0, sizeof ( msg_hex ) );
        log_printf( &logger, ">>> Waiting for a P2P RX message.\r\n" );
        error_flag |= lr11915mhz_read_response( ctx, LR11915MHZ_EVT_RX_P2P );
    }
    else
    {
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
    }
    
    if ( LR11915MHZ_OK == error_flag )
    {
        uint8_t * __generic_ptr start_ptr = strstr( app_buf, LR11915MHZ_EVT_RX_P2P );
        uint8_t * __generic_ptr end_ptr = NULL;
        if ( start_ptr )
        {
            start_ptr = start_ptr + strlen ( LR11915MHZ_EVT_RX_P2P );
            end_ptr = strstr ( start_ptr, "," );
            memcpy ( source, start_ptr, end_ptr - start_ptr );
            
            start_ptr = strstr ( end_ptr, ":" ) + 1;
            end_ptr = strstr ( start_ptr, "," );
            memcpy ( rssi, start_ptr, end_ptr - start_ptr );

            start_ptr = strstr ( end_ptr, ":" ) + 1;
            end_ptr = strstr ( start_ptr, "," );
            memcpy ( snr, start_ptr, end_ptr - start_ptr );
            
            start_ptr = end_ptr + 2;
            end_ptr = strstr ( start_ptr, "\n" );
            memcpy ( msg_hex, start_ptr, end_ptr - start_ptr );

            for ( cnt = 0; cnt < strlen ( msg_hex ); cnt += 2 )
            {
                msg_hex[ cnt / 2 ] = hex_to_uint8 ( &msg_hex [ cnt ] );
            }
            msg_hex[ cnt / 2 ] = 0;
            log_printf( &logger, ">>> Parse received message.\r\n" );
            log_printf ( &logger, " Source address: %s\r\n", source );
            log_printf ( &logger, " Message: %s\r\n", msg_hex );
            log_printf ( &logger, " RSSI: %s\r\n", rssi );
            log_printf ( &logger, " SNR: %s\r\n", snr );
            log_printf( &logger, "--------------------------------\r\n" );
        }
    }
    return error_flag;
}

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

额外支持

资源

喜欢这个项目吗?

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