高级
30 分钟

使用LEXI-R10801D和PIC32MZ1024EFH064在物联网应用中通过LTE网络提供高速数据连接

适用于EMEA、APAC和巴西地区的专业级单模LTE Cat 1bis解决方案

4G LTE 3 Click (for Europe) with PIC32MZ clicker

已发布 12月 16, 2024

点击板

4G LTE 3 Click (for Europe)

开发板

PIC32MZ clicker

编译器

NECTO Studio

微控制器单元

PIC32MZ1024EFH064

可靠的 LTE 连接,具备跟踪功能和中等数据速率,非常适合资产跟踪和远程信息处理

A

A

硬件概览

它是如何工作的?

4G LTE 3 Click(for Europe)基于来自u-blox的LEXI-R10801D单模LTE Cat 1bis模块,专为需要可靠连接、中等数据速率和广泛覆盖范围的应用而设计,适用于EMEA、APAC和巴西地区。LEXI-R10801D模块符合专业级标准,遵循u-blox的质量认证政策,并满足严格的AEC-Q104标准。它支持多种LTE FDD频段,包括1、3、5、7、8、20和28,确保跨多个区域网络的广泛兼容性和卓越性能。此外,该模块具有RED、NCC、RCM和Anatel等法规认证,支持在适用市场的无缝部署。除了LTE功能外,LEXI-R10801D集成了嵌入式Wi-Fi无线电,用于扫描Wi-Fi热点以增强室内定位,并支持u-blox的CellLocate®服务以实现高级地理定位功能。该紧凑且高度集成的模块提供中等容量的数据连接,实现高达10Mbps的下载速度和5Mbps的上传速度,同时保持极低的功耗。这些特性使其成为对成本敏感、需要适中数据速率和可靠覆盖的应用的理想选择。其多功能用例包括资产追踪、远程信息处理、医疗设备和可穿戴设备,在这些领域中高效且可靠的连接至关重要。LEXI-R10801D与主机MCU之间通过UART接口进行通信,使用标准

的UART RX和TX引脚以及硬件流控制引脚(CTS/RTS/RI - Clear to Send/Ready to Send/Ring Indicator)以实现高效的数据传输。模块默认通信速度为115200bps,支持通过AT命令实现无缝数据交换。此Click板™还包括一个用于电源和数据传输的USB Type C连接器,符合USB 2.0规范,最大数据速率为480Mbit/s(仅外设模式)。此外,板上还有一个USB固件升级开关,位于板背面,标注为USB BOOT,用于管理固件升级。此开关有两个位置:0用于正常操作,1用于通过USB进行固件升级,确保升级过程简单易行。4G LTE 3 Click还包含一些增强其实用性和可控性的附加功能。PWR按钮允许用户轻松开启或关闭模块,而RESET按钮提供了快速重置模块的方法。这些功能还可以通过mikroBUS™引脚PWR和RST以数字方式控制,提供更大的灵活性。此外,该板还包括一个未焊接的6个GPIO引脚的排针,以便进一步定制。此外,该板还有用于诊断目的的专用测试点TP1和TP2,当主机MCU使用USB和主UART接口时,以及两个用于提供实时状态更新的视觉指示器。第一个红色NET LED指示模块的当前网络状态。当

LED慢速闪烁时,设备已成功注册到网络;如果LED以正常节奏闪烁,设备尚未注册到网络;快速闪烁模式表示正在传输数据。当LED完全熄灭时,表示设备已关闭电源或处于节能模式(PSM)。第二个黄色STAT LED指示模块的电源状态,当模块关闭时熄灭,当模块开启或固件准备就绪时亮起。该板配备了一个u.Fl连接器,用于主LTE天线,例如MIKROE提供的LTE Flat Rotation Antenna,可结合IPEX-SMA电缆实现灵活且高效的连接。此外,该板还配有一个micro SIM卡插槽,支持1.8V和3.0V uSIM卡,允许用户根据特定用例选择最合适的服务提供商。此Click板™可在3.3V或5V逻辑电压水平下运行,通过VCC SEL跳线进行选择。由于LEXI-R10801D模块运行在3.8V,逻辑电平转换器TXB0106也用于确保正确操作和精确的信号电平转换。这样,3.3V和5V兼容的MCU都可以正常使用通信线。此外,此Click板™配备了一个包含易于使用的函数和示例代码的库,可用作进一步开发的参考。

4G LTE 3 Click (for Europe) hardware overview image

功能概述

开发板

PIC32MZ Clicker 是一款紧凑型入门开发板,它将 Click 板™的灵活性带给您喜爱的微控制器,使其成为实现您想法的完美入门套件。它配备了一款板载 32 位带有浮点单元的 Microchip PIC32MZ 微控制器,一个 USB 连接器,LED 指示灯,按钮,一个 mikroProg 连接器,以及一个用于与外部电子设备接口的头部。得益于其紧凑的设计和清晰易识别的丝网标记,它提供了流畅且沉浸式的工作体验,允许在任

何情况下、任何地方都能访问。PIC32MZ Clicker 开 发套件的每个部分都包含了使同一板块运行最高效的必要组件。除了可以选择 PIC32MZ Clicker 的编程方式,使用 USB HID mikroBootloader 或通过外部 mikroProg 连接器为 PIC,dsPIC 或 PIC32 编程外,Clicker 板还包括一个干净且调节过的开发套件电源供应模块。USB Micro-B 连接可以提供多达 500mA 的电流,这足以操作所有板载和附加模块。所有 

mikroBUS™ 本身支持的通信方法都在这块板上,包 括已经建立良好的 mikroBUS™ 插槽、重置按钮以及若干按钮和 LED 指示灯。PIC32MZ Clicker 是 Mikroe 生态系统的一个组成部分,允许您在几分钟内创建新的应用程序。它由 Mikroe 软件工具原生支持,得益于大量不同的 Click 板™(超过一千块板),其数量每天都在增长,它涵盖了原型制作的许多方面。

PIC32MZ clicker double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC32

MCU 内存 (KB)

1024

硅供应商

Microchip

引脚数

64

RAM (字节)

524288

你完善了我!

配件

LTE Flat Rotation Antenna 是增强 3G/4G LTE 设备性能的多功能选择。凭借 700-2700MHz 的宽频率范围,它确保在全球主要蜂窝频段上的最佳连接。该平板天线采用 SMA 公头连接器,便于直接连接到设备或 SMA 模块连接器。其亮点之一是可调角度,可按 45⁰ 增量(0⁰/45⁰/90⁰)设置,允许您微调天线的方向以获得最佳信号接收。具有 50Ω 阻抗和 <2.0:1 的电压驻波比 (VSWR),此天线确保可靠高效的连接。其 5dB 增益、垂直极化和全向辐射图形增强了信号强度,适用于各种应用。天线长度为 196mm,宽度为 38mm,提供紧凑但有效的解决方案来改善您的连接。最大输入功率为 50W,能够满足各种设备的需求。

4G LTE 3 Click (for Europe) accessories 1 image

IPEX-SMA 电缆是一种射频 (RF) 电缆组件。"IPEX" 指的是 IPEX 连接器,这是一种常用于小型电子设备的微型同轴连接器。"SMA" 代表 SubMiniature Version A,是另一种常用于射频应用的同轴连接器。IPEX-SMA 电缆组件的一端是 IPEX 连接器,另一端是 SMA 连接器,使其能够连接使用这些特定连接器的设备或组件。这些电缆常用于 WiFi 或蜂窝天线、GPS 模块以及其他需要可靠且低损耗连接的射频通信系统。

4G LTE 3 Click (for Europe) accessories 2 image

使用的MCU引脚

mikroBUS™映射器

Module Power-ON
RE4
AN
Reset / ID SEL
RE5
RST
UART RTS / ID COMM
RG9
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Ring Indicator
RB3
PWM
UART CTS
RB5
INT
UART TX
RB2
TX
UART RX
RB0
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

4G LTE 3 Click (for Europe) Schematic schematic

一步一步来

项目组装

PIC32MZ clicker front image hardware assembly

从选择您的开发板和Click板™开始。以PIC32MZ clicker作为您的开发板开始。

PIC32MZ clicker front image hardware assembly
GNSS2 Click front image hardware assembly
Prog-cut hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
Micro B Connector Clicker Access - 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
Flip&Click PIC32MZ 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”替换为要显示的参数。

软件支持

库描述

该库包含 4G LTE 3 Click (for Europe) 驱动程序的 API。

关键功能:

  • c4glte3e_set_sim_apn - 此函数设置 SIM 卡的 APN。

  • c4glte3e_send_sms_text - 此函数向电话号码发送短信。

  • c4glte3e_cmd_run - 此函数向点击模块发送指定命令。

开源

代码示例

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

/*!
 * @file main.c
 * @brief 4G LTE 3 E Click Example.
 *
 * # Description
 * Application example shows device capability of connecting to the network and
 * sending SMS or TCP/UDP messages using standard "AT" commands.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and logger.
 *
 * ## Application Task
 * Application task is split in few stages:
 *  - C4GLTE3E_POWER_UP:
 * Powers up the device, performs a factory reset and reads system information.
 *
 *  - C4GLTE3E_CONFIG_CONNECTION:
 * Sets configuration to device to be able to connect to the network.
 *
 *  - C4GLTE3E_CHECK_CONNECTION:
 * Waits for the network registration indicated via CEREG command and then checks 
 * the signal quality report.
 *
 *  - C4GLTE3E_CONFIG_EXAMPLE:
 * Configures device for the selected example.
 *
 *  - C4GLTE3E_EXAMPLE:
 * Depending on the selected demo example, it sends an SMS message (in PDU or TXT mode) or TCP/UDP message.
 *
 * By default, the TCP/UDP example is selected.
 *
 * ## Additional Function
 * - static void c4glte3e_clear_app_buf ( void )
 * - static void c4glte3e_log_app_buf ( void )
 * - static err_t c4glte3e_process ( c4glte3e_t *ctx )
 * - static err_t c4glte3e_read_response ( c4glte3e_t *ctx, uint8_t *rsp )
 * - static err_t c4glte3e_power_up ( c4glte3e_t *ctx )
 * - static err_t c4glte3e_config_connection ( c4glte3e_t *ctx )
 * - static err_t c4glte3e_check_connection ( c4glte3e_t *ctx )
 * - static err_t c4glte3e_config_example ( c4glte3e_t *ctx )
 * - static err_t c4glte3e_example ( c4glte3e_t *ctx )
 *
 * @note
 * In order for the examples to work, user needs to set the APN and SMSC (SMS PDU mode only)
 * of entered SIM card as well as the phone number (SMS mode only) to which he wants to send an SMS.
 * Enter valid values for the following macros: SIM_APN, SIM_SMSC and PHONE_NUMBER.
 * Example:
    SIM_APN "internet"
    SIM_SMSC "+381610401"
    PHONE_NUMBER "+381659999999"
 *
 * @author Stefan Filipovic
 *
 */

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

// Example selection macros
#define EXAMPLE_TCP_UDP                     0               // Example of sending messages to a TCP/UDP echo server
#define EXAMPLE_SMS                         1               // Example of sending SMS to a phone number
#define DEMO_EXAMPLE                        EXAMPLE_TCP_UDP // Example selection macro

// SIM APN config
#define SIM_APN                             "internet"      // Set valid SIM APN

// SMS example parameters
#define SIM_SMSC                            ""              // Set valid SMS Service Center Address - only in SMS PDU mode
#define PHONE_NUMBER                        ""              // Set Phone number to message
#define SMS_MODE                            "1"             // SMS mode: "0" - PDU, "1" - TXT

// TCP/UDP example parameters
#define REMOTE_IP                           "77.46.162.162" // TCP/UDP echo server IP address
#define REMOTE_PORT                         "51111"         // TCP/UDP echo server port

// Message content
#define MESSAGE_CONTENT                     "4G LTE 3 E click board - demo example."

// Application buffer size
#define APP_BUFFER_SIZE                     256
#define PROCESS_BUFFER_SIZE                 256

/**
 * @brief Example states.
 * @details Predefined enum values for application example state.
 */
typedef enum
{
    C4GLTE3E_POWER_UP = 1,
    C4GLTE3E_CONFIG_CONNECTION,
    C4GLTE3E_CHECK_CONNECTION,
    C4GLTE3E_CONFIG_EXAMPLE,
    C4GLTE3E_EXAMPLE

} c4glte3e_app_state_t;

/**
 * @brief Application example variables.
 * @details Variables used in application example.
 */
static uint8_t app_buf[ APP_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len = 0;
static c4glte3e_app_state_t app_state = C4GLTE3E_POWER_UP;

static c4glte3e_t c4glte3e;
static log_t logger;

/**
 * @brief 4G LTE 3 E clearing application buffer.
 * @details This function clears memory of application buffer and reset its length.
 * @note None.
 */
static void c4glte3e_clear_app_buf ( void );

/**
 * @brief 4G LTE 3 E log application buffer.
 * @details This function logs data from application buffer to USB UART.
 * @note None.
 */
static void c4glte3e_log_app_buf ( void );

/**
 * @brief 4G LTE 3 E data reading function.
 * @details This function reads data from device and concatenates data to application buffer. 
 * @param[in] ctx : Click context object.
 * See #c4glte3e_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 c4glte3e_process ( c4glte3e_t *ctx );

/**
 * @brief 4G LTE 3 E 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 #c4glte3e_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.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t c4glte3e_read_response ( c4glte3e_t *ctx, uint8_t *rsp );

/**
 * @brief 4G LTE 3 E power up function.
 * @details This function powers up the device, performs a factory reset and reads system information.
 * @param[in] ctx : Click context object.
 * See #c4glte3e_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 c4glte3e_power_up ( c4glte3e_t *ctx );

/**
 * @brief 4G LTE 3 E config connection function.
 * @details This function configures and enables connection to the specified network.
 * @param[in] ctx : Click context object.
 * See #c4glte3e_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 c4glte3e_config_connection ( c4glte3e_t *ctx );

/**
 * @brief 4G LTE 3 E check connection function.
 * @details This function checks the connection to network.
 * @param[in] ctx : Click context object.
 * See #c4glte3e_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 c4glte3e_check_connection ( c4glte3e_t *ctx );

/**
 * @brief 4G LTE 3 E config example function.
 * @details This function configures device for the selected example.
 * @param[in] ctx : Click context object.
 * See #c4glte3e_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 c4glte3e_config_example ( c4glte3e_t *ctx );

/**
 * @brief 4G LTE 3 E example function.
 * @details This function executes SMS or TCP/UDP depending on the DEMO_EXAMPLE macro.
 * @param[in] ctx : Click context object.
 * See #c4glte3e_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 c4glte3e_example ( c4glte3e_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    c4glte3e_cfg_t c4glte3e_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.
    c4glte3e_cfg_setup( &c4glte3e_cfg );
    C4GLTE3E_MAP_MIKROBUS( c4glte3e_cfg, MIKROBUS_1 );
    if ( UART_ERROR == c4glte3e_init( &c4glte3e, &c4glte3e_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );

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

void application_task ( void ) 
{
    switch ( app_state )
    {
        case C4GLTE3E_POWER_UP:
        {
            if ( C4GLTE3E_OK == c4glte3e_power_up( &c4glte3e ) )
            {
                app_state = C4GLTE3E_CONFIG_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CONFIG CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case C4GLTE3E_CONFIG_CONNECTION:
        {
            if ( C4GLTE3E_OK == c4glte3e_config_connection( &c4glte3e ) )
            {
                app_state = C4GLTE3E_CHECK_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CHECK CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case C4GLTE3E_CHECK_CONNECTION:
        {
            if ( C4GLTE3E_OK == c4glte3e_check_connection( &c4glte3e ) )
            {
                app_state = C4GLTE3E_CONFIG_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - CONFIG EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case C4GLTE3E_CONFIG_EXAMPLE:
        {
            if ( C4GLTE3E_OK == c4glte3e_config_example( &c4glte3e ) )
            {
                app_state = C4GLTE3E_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case C4GLTE3E_EXAMPLE:
        {
            c4glte3e_example( &c4glte3e );
            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 c4glte3e_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
}

static void c4glte3e_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 c4glte3e_process ( c4glte3e_t *ctx ) 
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t overflow_bytes = 0;
    int32_t rx_cnt = 0;
    int32_t rx_size = c4glte3e_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 C4GLTE3E_OK;
    }
    return C4GLTE3E_ERROR;
}

static err_t c4glte3e_read_response ( c4glte3e_t *ctx, uint8_t *rsp ) 
{
    #define READ_RESPONSE_TIMEOUT_MS    120000
    uint32_t timeout_cnt = 0;
    c4glte3e_clear_app_buf ( );
    c4glte3e_process( ctx );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, C4GLTE3E_RSP_ERROR ) ) )
    {
        c4glte3e_process( ctx );
        if ( timeout_cnt++ > READ_RESPONSE_TIMEOUT_MS )
        {
            c4glte3e_clear_app_buf( );
            log_error( &logger, " Timeout!" );
            return C4GLTE3E_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    Delay_ms ( 200 );
    c4glte3e_process( ctx );
    c4glte3e_log_app_buf( );
    if ( strstr( app_buf, rsp ) )
    {
        log_printf( &logger, "--------------------------------\r\n" );
        return C4GLTE3E_OK;
    }
    else if ( strstr( app_buf, C4GLTE3E_RSP_ERROR ) )
    {
        log_error( &logger, " CMD!" );
        return C4GLTE3E_ERROR_CMD;
    }
    log_error( &logger, " Unknown!" );
    return C4GLTE3E_ERROR_UNKNOWN;
}

static err_t c4glte3e_power_up ( c4glte3e_t *ctx )
{
    err_t error_flag = C4GLTE3E_OK;

    uint8_t power_state = C4GLTE3E_POWER_STATE_OFF;
    for ( ; ; )
    {
        c4glte3e_process( ctx );
        c4glte3e_log_app_buf ( );
        c4glte3e_clear_app_buf ( );
        // Wake up UART interface
        c4glte3e_cmd_run( ctx, C4GLTE3E_CMD_AT );

        log_printf( &logger, ">>> Check communication.\r\n" );
        c4glte3e_cmd_run( ctx, C4GLTE3E_CMD_AT );
        if ( ( ( C4GLTE3E_OK == c4glte3e_process( ctx ) ) && strstr( app_buf, C4GLTE3E_RSP_OK ) ) )
        {
            power_state = C4GLTE3E_POWER_STATE_ON;
            break;
        }
        else if ( C4GLTE3E_POWER_STATE_OFF == power_state )
        {
            power_state = C4GLTE3E_POWER_STATE_ON;
            log_printf( &logger, ">>> Power up device.\r\n" );
            c4glte3e_set_power_state ( ctx, C4GLTE3E_POWER_STATE_ON );
        }
        else if ( C4GLTE3E_POWER_STATE_ON == power_state )
        {
            power_state = C4GLTE3E_POWER_STATE_OFF;
            log_printf( &logger, ">>> Power down device.\r\n" );
            c4glte3e_set_power_state ( ctx, C4GLTE3E_POWER_STATE_OFF );
        }
    }
    c4glte3e_cmd_run( ctx, C4GLTE3E_CMD_AT );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    
    log_printf( &logger, ">>> Factory reset.\r\n" );
    c4glte3e_cmd_run( ctx, C4GLTE3E_CMD_FACTORY_RESET );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );

    log_printf( &logger, ">>> Get device software version ID.\r\n" );
    c4glte3e_cmd_run( ctx, C4GLTE3E_CMD_GET_SW_VERSION );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );

    log_printf( &logger, ">>> Get device serial number.\r\n" );
    c4glte3e_cmd_run( ctx, C4GLTE3E_CMD_GET_SERIAL_NUM );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );

    return error_flag;
}

static err_t c4glte3e_config_connection ( c4glte3e_t *ctx )
{
    err_t error_flag = C4GLTE3E_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    log_printf( &logger, ">>> Configure network status LED.\r\n" );
    #define NETWORK_STATUS_LED "14,2"
    c4glte3e_cmd_set( ctx, C4GLTE3E_CMD_GPIO_CONFIG, NETWORK_STATUS_LED );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );

    log_printf( &logger, ">>> Configure module status LED.\r\n" );
    #define MODULE_STATUS_LED "15,10"
    c4glte3e_cmd_set( ctx, C4GLTE3E_CMD_GPIO_CONFIG, MODULE_STATUS_LED );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );

    log_printf( &logger, ">>> Deregister from network.\r\n" );
    #define DEREGISTER_FROM_NETWORK "2"
    c4glte3e_cmd_set( ctx, C4GLTE3E_CMD_OPERATOR_SELECTION, DEREGISTER_FROM_NETWORK );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );

    log_printf( &logger, ">>> Set SIM APN.\r\n" );
    c4glte3e_set_sim_apn( &c4glte3e, SIM_APN );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    
    log_printf( &logger, ">>> Enable full functionality.\r\n" );
    #define FULL_FUNCTIONALITY "1"
    c4glte3e_cmd_set( ctx, C4GLTE3E_CMD_SET_MODULE_FUNCTIONALITY, FULL_FUNCTIONALITY );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );

    log_printf( &logger, ">>> Enable network registration.\r\n" );
    #define ENABLE_REG "2"
    c4glte3e_cmd_set( ctx, C4GLTE3E_CMD_EPS_NETWORK_REGISTRATION, ENABLE_REG );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );

    log_printf( &logger, ">>> Set automatic registration.\r\n" );
    #define AUTOMATIC_REGISTRATION "0"
    c4glte3e_cmd_set( ctx, C4GLTE3E_CMD_OPERATOR_SELECTION, AUTOMATIC_REGISTRATION );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
#endif
    return error_flag;
}

static err_t c4glte3e_check_connection ( c4glte3e_t *ctx )
{
    err_t error_flag = C4GLTE3E_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    log_printf( &logger, ">>> Check network registration.\r\n" );
    #define CONNECTED "+CEREG: 2,1"
    c4glte3e_cmd_get ( &c4glte3e, C4GLTE3E_CMD_EPS_NETWORK_REGISTRATION );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    if ( strstr( app_buf, CONNECTED ) )
    {
        Delay_ms ( 1000 );
        log_printf( &logger, ">>> Check signal quality.\r\n" );
        c4glte3e_cmd_run ( &c4glte3e, C4GLTE3E_CMD_SIGNAL_QUALITY_REPORT );
        error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    }
    else
    {
        error_flag = C4GLTE3E_ERROR;
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
    }
#endif
    return error_flag;
}

static err_t c4glte3e_config_example ( c4glte3e_t *ctx )
{
    err_t error_flag = C4GLTE3E_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    log_printf( &logger, ">>> Activate PDP context.\r\n" );
    #define ACTIVATE_PDP_CONTEXT "1,1"
    c4glte3e_cmd_set( &c4glte3e, C4GLTE3E_CMD_ACTIVATE_PDP_CONTEXT, ACTIVATE_PDP_CONTEXT );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    log_printf( &logger, ">>> Show PDP address.\r\n" );
    #define PDP_CID "1"
    c4glte3e_cmd_set( &c4glte3e, C4GLTE3E_CMD_SHOW_PDP_ADDRESS, PDP_CID );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    log_printf( &logger, ">>> Select SMS format.\r\n" );
    c4glte3e_cmd_set( &c4glte3e, C4GLTE3E_CMD_SELECT_SMS_FORMAT, SMS_MODE );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
#endif
    return error_flag;
}

static err_t c4glte3e_example ( c4glte3e_t *ctx )
{
    err_t error_flag = C4GLTE3E_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    uint8_t cmd_buf[ 100 ] = { 0 };
    uint8_t * __generic_ptr socket_num_buf = 0;
    uint8_t tcp_socket_num[ 2 ] = { 0 };
    uint8_t udp_socket_num[ 2 ] = { 0 };
    log_printf( &logger, ">>> Create TCP socket.\r\n" );
    #define TCP_PROTOCOL "6"
    c4glte3e_cmd_set ( &c4glte3e, C4GLTE3E_CMD_CREATE_SOCKET, TCP_PROTOCOL );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    socket_num_buf = strstr( app_buf, C4GLTE3E_URC_CREATE_SOCKET ) + strlen ( C4GLTE3E_URC_CREATE_SOCKET );
    if ( NULL != socket_num_buf )
    {
        tcp_socket_num[ 0 ] = *socket_num_buf;
    }

    log_printf( &logger, ">>> Create UDP socket.\r\n" );
    #define UDP_PROTOCOL "17"
    c4glte3e_cmd_set ( &c4glte3e, C4GLTE3E_CMD_CREATE_SOCKET, UDP_PROTOCOL );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    socket_num_buf = strstr( app_buf, C4GLTE3E_URC_CREATE_SOCKET ) + strlen ( C4GLTE3E_URC_CREATE_SOCKET );
    if ( NULL != socket_num_buf )
    {
        udp_socket_num[ 0 ] = *socket_num_buf;
    }

    log_printf( &logger, ">>> Open TCP connection.\r\n" );
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    c4glte3e_cmd_set ( &c4glte3e, C4GLTE3E_CMD_CONNECT_SOCKET, cmd_buf );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );

    log_printf( &logger, ">>> Open UDP connection.\r\n" );
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    c4glte3e_cmd_set ( &c4glte3e, C4GLTE3E_CMD_CONNECT_SOCKET, cmd_buf );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );

    // Get message length
    uint8_t message_len_buf[ 10 ] = { 0 };
    uint16_t message_len = strlen( MESSAGE_CONTENT );
    uint16_to_str( message_len, message_len_buf );
    l_trim( message_len_buf );
    r_trim( message_len_buf );

    log_printf( &logger, ">>> Write message to TCP connection.\r\n" );
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, MESSAGE_CONTENT );
    strcat( cmd_buf, "\"" );
    c4glte3e_cmd_set ( &c4glte3e, C4GLTE3E_CMD_WRITE_SOCKET_DATA, cmd_buf );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_URC_RECEIVED_DATA );
    log_printf( &logger, ">>> Read response from TCP connection.\r\n" );
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    c4glte3e_cmd_set( &c4glte3e, C4GLTE3E_CMD_READ_SOCKET_DATA, cmd_buf );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );

    log_printf( &logger, ">>> Write message to UDP connection.\r\n" );
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, MESSAGE_CONTENT );
    strcat( cmd_buf, "\"" );
    c4glte3e_cmd_set ( &c4glte3e, C4GLTE3E_CMD_WRITE_SOCKET_DATA, cmd_buf );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_URC_RECEIVED_DATA );
    log_printf( &logger, ">>> Read response from UDP connection.\r\n" );
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    c4glte3e_cmd_set( &c4glte3e, C4GLTE3E_CMD_READ_SOCKET_DATA, cmd_buf );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    
    log_printf( &logger, ">>> Close TCP connection.\r\n" );
    c4glte3e_cmd_set ( &c4glte3e, C4GLTE3E_CMD_CLOSE_SOCKET, tcp_socket_num );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    
    log_printf( &logger, ">>> Close UDP connection.\r\n" );
    c4glte3e_cmd_set ( &c4glte3e, C4GLTE3E_CMD_CLOSE_SOCKET, udp_socket_num );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    #define CMGF_PDU "+CMGF: 0"
    #define CMGF_TXT "+CMGF: 1"
    log_printf( &logger, ">>> Check SMS format.\r\n" );
    c4glte3e_cmd_get( &c4glte3e, C4GLTE3E_CMD_SELECT_SMS_FORMAT );
    error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    if ( strstr( app_buf, CMGF_PDU ) )
    {
        log_printf( &logger, ">>> Send SMS in PDU mode.\r\n" );
        c4glte3e_send_sms_pdu( &c4glte3e, SIM_SMSC, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    }
    else if ( strstr( app_buf, CMGF_TXT ) )
    {
        log_printf( &logger, ">>> Send SMS in TXT mode.\r\n" );
        c4glte3e_send_sms_text ( &c4glte3e, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= c4glte3e_read_response( ctx, C4GLTE3E_RSP_OK );
    }
    // 30 seconds delay
    for ( uint8_t delay_cnt = 0; delay_cnt < 30; delay_cnt++ )
    {
        Delay_ms ( 1000 );
    }
#else
    #error "No demo example selected"
#endif
    return error_flag;
}

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

额外支持

资源

喜欢这个项目吗?

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