中级
20 分钟

使用EG95EXGA-128-SGNS和PIC18F57Q43实现可靠的4G (LTE)连接,适用于欧洲M2M和物联网

符合3GPP Release 11标准的LTE Cat 4物联网/M2M解决方案

LTE Cat.4 Click (for Europe) with Curiosity Nano with PIC18F57Q43

已发布 8月 26, 2024

点击板

LTE Cat.4 Click (for Europe)

开发板

Curiosity Nano with PIC18F57Q43

编译器

NECTO Studio

微控制器单元

PIC18F57Q43

使用LTE Cat 4标准的蜂窝网络连接,适用于欧洲物联网/机器对机器(IoT/M2M)应用中的数据传输

A

A

硬件概览

它是如何工作的?

LTE Cat.4 Click 基于 Quectel 的 EG95EXGA-128-SGNS,这是一款面向欧洲地区的IoT/M2M优化的 LTE Cat.4 模块,具有接收分集功能。采用 3GPP Rel. 11 LTE 技术,最大下行数据速率可达 150Mbps,上行数据速率可达 50Mbps。它支持多个 LTE 频段(B1/B3/B7/B8/B20/B28),B1 和 B8 频段的 RX 分集,以及多星座 GNSS 支持,包括 GPS、GLONASS、BeiDou/Compass、Galileo 和 QZSS。该模块完全集成了如 TCP、UDP 和 PPP 等互联网服务协议,使其易于使用扩展的 AT 命令。这些广泛的功能使此 Click board™ 适用于广泛的 M2M 和 IoT 应用,例如工业路由器、工业 PDA、视频监控、数字标牌等。EG95EXGA-128-SGNS 和主机 MCU 之间的通信通过 UART 接口进行,使用标准的 UART RX 和 TX 引脚,以及硬件流控制引脚(CTS/RTS/RI - Clear to Send/Ready to Send/Ring Indicator)以实现高效的数据传输。默认通信速度设置为 115200bps,确保通过 AT 命令进行无缝的数据交换。值得注意的是,此模块版本还具有音频接口,可通过 I2C 接口访问。LTE Cat.4 Click 音频接口通过 MAX9860 操作,这是一款通过 I2C 接口可配置的 16 位单声道音频语音编

解码器。此设置适用于板背面设计用于 CTIA 标准耳机的插孔,这些耳机通常用于现代智能手机,并具有组合音频和麦克风连接器。该标准确保了与各种耳机和耳麦的兼容性。此外,音频接口支持高级功能,如回声消除和噪声抑制,增强了语音通信的清晰度和质量。LTE Cat.4 Click 还包括一个 USB Type C 连接器,用于电源和数据传输,符合 USB 2.0 规范(仅限从设备)。此接口支持高达 480Mbps 的数据传输速率,支持 AT 命令通信、数据传输、GNSS NMEA 语句输出、软件调试、固件升级和通过 USB 的语音通信。板上还配备了一个标记为 USB BOOT 的 USB 固件升级开关,用于管理固件升级。该开关有两个位置:0 为正常操作,1 为通过 USB 进行固件升级,确保了简便的升级过程。此外,此 Click board™ 还包括多项功能,增强了其可用性和控制性。PWR 按钮允许用户轻松开关模块,而 RESET 按钮提供快速重置模块的方法。这些功能还可以通过 mikroBUS™ 引脚 PWR 和 RST 数字控制,提供更大的灵活性。此外,这些控制功能具有专用测试点,便于调试和测试。该板还具有两个视觉指示器,用于提供实时状态更新。红色 NET LED 提供网络活动反馈:当搜索网络时缓

慢闪烁,在数据传输期间快速闪烁,在语音通话期间保持常亮。黄色 STAT LED 指示模块的电源状态,当模块关闭时熄灭,模块打开时点亮。该板还包括用于 UART 调试通信的 DBG TX/RX 接口测试点,简化了开发和故障排除过程。板上具有三个用于 GNSS、LTE 和 LTE/WCDMA RX 分集天线的 u.Fl 连接器,MIKROE 提供的天线包括 LTE 扁平旋转天线和带 IPEX-SMA 电缆的有源 GPS 天线,提供灵活高效的连接选项。此外,用户可以通过 GNSS ANT 跳线轻松选择 GNSS 天线的电源,在 3.3V 和 5V 之间进行选择。板上还配备了一个支持 1.8V 和 3.0V uSIM 卡的 micro SIM 卡座,确保与各种蜂窝网络的兼容性,并允许用户根据特定用例选择最合适的服务提供商。此 Click board™ 可以通过 VCC SEL 跳线选择使用 3.3V 或 5V 逻辑电压水平。由于 EG95EXGA-128-SGNS 模块在 3.8V 下工作,使用逻辑电平转换器 TXB0106 和 PCA9306 以实现正确操作和精确的信号电平转换。因此,3.3V 和 5V 兼容的 MCU 都可以正确使用通信线路。此外,这款 Click board™ 配备了包含易用功能的库和示例代码,可作为进一步开发的参考。

LTE Cat.4 Click (for Europe) hardware overview image

功能概述

开发板

PIC18F57Q43 Curiosity Nano 评估套件是一款尖端的硬件平台,旨在评估 PIC18-Q43 系列内的微控制器。其设计的核心是包含了功能强大的 PIC18F57Q43 微控制器(MCU),提供先进的功能和稳健的性能。这个评估套件的关键特点包括一个黄 色用户 LED 和一个响应灵敏的机械用户开关,提供无

缝的交互和测试。为一个 32.768kHz 水晶振荡器足迹提供支持,确保精准的定时能力。套件内置的调试器拥有一个绿色电源和状态 LED,使编程和调试变得直观高效。此外,增强其实用性的还有虚拟串行端口 (CDC)和一个调试 GPIO 通道(DGI GPIO),提供广泛的连接选项。该套件通过 USB 供电,拥有由

 MIC5353 LDO 调节器提供支持的可调目标电压功能,确保在 1.8V 至 5.1V 的输出电压范围内稳定运行,最大输出电流为 500mA,受环境温度和电压限制。

PIC18F57Q43 Curiosity Nano double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC

MCU 内存 (KB)

128

硅供应商

Microchip

引脚数

48

RAM (字节)

8196

你完善了我!

配件

Curiosity Nano Base for Click boards 是一款多功能硬件扩展平台,专为简化 Curiosity Nano 套件与扩展板之间的集成而设计,特别针对符合 mikroBUS™ 标准的 Click 板和 Xplained Pro 扩展板。这款创新的基板(屏蔽板)提供了无缝的连接和扩展可能性,简化了实验和开发过程。主要特点包括从 Curiosity Nano 套件提供 USB 电源兼容性,以及为增强灵活性而提供的另一种外部电源输入选项。板载锂离子/锂聚合物充电器和管理电路确保电池供电应用的平稳运行,简化了使用和管理。此外,基板内置了一个固定的 3.3V 电源供应单元,专用于目标和 mikroBUS™ 电源轨,以及一个固定的 5.0V 升压转换器,专供 mikroBUS™ 插座的 5V 电源轨,为各种连接设备提供稳定的电力供应。

Curiosity Nano Base for Click boards accessories 1 image

主动 GPS 天线旨在增强您的 GPS 和 GNSS Click 板™ 的性能。这款外置天线结构坚固,适用于各种天气条件。凭借 1575.42MHz 的频率范围和 50Ohm 的阻抗,它确保了可靠的信号接收。天线在较宽的角度范围内提供大于 -4dBic 的增益,确保超过 75% 的覆盖率。± 5MHz 的带宽进一步保证了精确的数据采集。天线采用右旋圆极化 (RHCP),提供稳定的信号接收。其紧凑的尺寸为 48.5×39×15mm,配有 2 米长的电缆,安装方便。磁性天线类型与 SMA 公连接器确保了安全便捷的连接。如果您需要为定位设备提供可靠的外置天线,我们的主动 GPS 天线是完美的解决方案。

LTE Cat.4 Click (for Europe) accessories 1 image

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

LTE Cat.4 Click (for Europe) accessories 2 image

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

LTE Cat.4 Click (for Europe) accessories 3 image

使用的MCU引脚

mikroBUS™映射器

Module Power-ON
PA0
AN
Reset / ID SEL
PA7
RST
UART RTS / ID COMM
PD4
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Ring Indicator
PB0
PWM
UART CTS
PA6
INT
UART tX
PC3
TX
UART RX
PC2
RX
I2C Clock
PB2
SCL
I2C Data
PB1
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

LTE Cat.4 Click (for Europe) Schematic schematic

一步一步来

项目组装

Curiosity Nano Base for Click boards front image hardware assembly

从选择您的开发板和Click板™开始。以Curiosity Nano with PIC18F57Q43作为您的开发板开始。

Curiosity Nano Base for Click boards front image hardware assembly
Charger 27 Click front image hardware assembly
PIC18F47Q10 Curiosity Nano front image hardware assembly
Prog-cut hardware assembly
Charger 27 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
PIC18F57Q43 Curiosity 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”替换为要显示的参数。

软件支持

库描述

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

关键功能:

  • ltecat4_set_sim_apn - 此函数为SIM卡设置APN。

  • ltecat4_send_sms_text - 此函数向电话号码发送文本消息。

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

开源

代码示例

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

/*!
 * @file main.c
 * @brief LTE Cat.4 Click Example.
 *
 * # Description
 * Application example shows device capability of connecting to the network and
 * sending SMS or TCP/UDP messages, answering incoming calls, or retrieving data
 * from GNSS 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:
 *  - LTECAT4_POWER_UP:
 * Powers up the device, performs a factory reset and reads system information.
 *
 *  - LTECAT4_CONFIG_CONNECTION:
 * Sets configuration to device to be able to connect to the network 
 * (used only for SMS, CALL, or TCP/UDP demo examples).
 *
 *  - LTECAT4_CHECK_CONNECTION:
 * Waits for the network registration indicated via CREG command and then checks 
 * the signal quality report (used only for SMS, CALL, or TCP/UDP demo examples).
 *
 *  - LTECAT4_CONFIG_EXAMPLE:
 * Configures device for the selected example.
 *
 *  - LTECAT4_EXAMPLE:
 * Depending on the selected demo example, it sends an SMS message (in PDU or TXT mode)
 * or TCP/UDP message, waits for incoming calls and answers it, or waits for the GPS fix
 * to retrieve location info from GNSS.
 *
 * By default, the TCP/UDP example is selected.
 *
 * ## Additional Function
 * - static void ltecat4_clear_app_buf ( void )
 * - static void ltecat4_log_app_buf ( void )
 * - static err_t ltecat4_process ( ltecat4_t *ctx )
 * - static err_t ltecat4_read_response ( ltecat4_t *ctx, uint8_t *rsp )
 * - static err_t ltecat4_power_up ( ltecat4_t *ctx )
 * - static err_t ltecat4_config_connection ( ltecat4_t *ctx )
 * - static err_t ltecat4_check_connection ( ltecat4_t *ctx )
 * - static err_t ltecat4_config_example ( ltecat4_t *ctx )
 * - static err_t ltecat4_example ( ltecat4_t *ctx )
 *
 * @note
 * In order for the examples to work (except GNSS example), 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 "ltecat4.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 EXAMPLE_CALL                        2               // Example of answering incoming calls 
#define EXAMPLE_GNSS                        3               // Example of retrieving location info from GNSS
#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                     "LTE Cat.4 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
{
    LTECAT4_POWER_UP = 1,
    LTECAT4_CONFIG_CONNECTION,
    LTECAT4_CHECK_CONNECTION,
    LTECAT4_CONFIG_EXAMPLE,
    LTECAT4_EXAMPLE

} ltecat4_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 ltecat4_app_state_t app_state = LTECAT4_POWER_UP;

static ltecat4_t ltecat4;
static log_t logger;

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

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

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

/**
 * @brief LTE Cat.4 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 #ltecat4_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 ltecat4_read_response ( ltecat4_t *ctx, uint8_t *rsp );

/**
 * @brief LTE Cat.4 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 #ltecat4_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 ltecat4_power_up ( ltecat4_t *ctx );

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

/**
 * @brief LTE Cat.4 check connection function.
 * @details This function checks the connection to network.
 * @param[in] ctx : Click context object.
 * See #ltecat4_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 ltecat4_check_connection ( ltecat4_t *ctx );

/**
 * @brief LTE Cat.4 config example function.
 * @details This function configures device for the selected example.
 * @param[in] ctx : Click context object.
 * See #ltecat4_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 ltecat4_config_example ( ltecat4_t *ctx );

/**
 * @brief LTE Cat.4 example function.
 * @details This function executes SMS, TCP/UDP, CALL, or GNSS example depending on the DEMO_EXAMPLE macro.
 * @param[in] ctx : Click context object.
 * See #ltecat4_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 ltecat4_example ( ltecat4_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    ltecat4_cfg_t ltecat4_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.
    ltecat4_cfg_setup( &ltecat4_cfg );
    LTECAT4_MAP_MIKROBUS( ltecat4_cfg, MIKROBUS_1 );
    if ( UART_ERROR == ltecat4_init( &ltecat4, &ltecat4_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );

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

void application_task ( void ) 
{
    switch ( app_state )
    {
        case LTECAT4_POWER_UP:
        {
            if ( LTECAT4_OK == ltecat4_power_up( &ltecat4 ) )
            {
                app_state = LTECAT4_CONFIG_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CONFIG CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case LTECAT4_CONFIG_CONNECTION:
        {
            if ( LTECAT4_OK == ltecat4_config_connection( &ltecat4 ) )
            {
                app_state = LTECAT4_CHECK_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CHECK CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case LTECAT4_CHECK_CONNECTION:
        {
            if ( LTECAT4_OK == ltecat4_check_connection( &ltecat4 ) )
            {
                app_state = LTECAT4_CONFIG_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - CONFIG EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case LTECAT4_CONFIG_EXAMPLE:
        {
            if ( LTECAT4_OK == ltecat4_config_example( &ltecat4 ) )
            {
                app_state = LTECAT4_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case LTECAT4_EXAMPLE:
        {
            ltecat4_example( &ltecat4 );
            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 ltecat4_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
}

static void ltecat4_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 ltecat4_process ( ltecat4_t *ctx ) 
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t overflow_bytes = 0;
    int32_t rx_cnt = 0;
    int32_t rx_size = ltecat4_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 LTECAT4_OK;
    }
    return LTECAT4_ERROR;
}

static err_t ltecat4_read_response ( ltecat4_t *ctx, uint8_t *rsp ) 
{
    #define READ_RESPONSE_TIMEOUT_MS    120000
    uint32_t timeout_cnt = 0;
    ltecat4_clear_app_buf ( );
    ltecat4_process( ctx );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, LTECAT4_RSP_ERROR ) ) )
    {
        ltecat4_process( ctx );
        if ( timeout_cnt++ > READ_RESPONSE_TIMEOUT_MS )
        {
            ltecat4_clear_app_buf( );
            log_error( &logger, " Timeout!" );
            return LTECAT4_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    Delay_ms ( 200 );
    ltecat4_process( ctx );
    if ( strstr( app_buf, rsp ) )
    {
        ltecat4_log_app_buf( );
        log_printf( &logger, "--------------------------------\r\n" );
        return LTECAT4_OK;
    }
    else if ( strstr( app_buf, LTECAT4_RSP_ERROR ) )
    {
        log_error( &logger, " CMD!" );
        return LTECAT4_ERROR_CMD;
    }
    log_error( &logger, " Unknown!" );
    return LTECAT4_ERROR_UNKNOWN;
}

static err_t ltecat4_power_up ( ltecat4_t *ctx )
{
    err_t error_flag = LTECAT4_OK;

    for ( ; ; )
    {
        ltecat4_process( ctx );
        ltecat4_clear_app_buf ( );
        // Wake up UART interface
        ltecat4_send_cmd( ctx, LTECAT4_CMD_AT );

        log_printf( &logger, ">>> Check communication.\r\n" );
        ltecat4_send_cmd( ctx, LTECAT4_CMD_AT );
        if ( ( ( LTECAT4_OK == ltecat4_process( ctx ) ) && strstr( app_buf, LTECAT4_RSP_OK ) ) )
        {
            break;
        }
        else
        {
            log_printf( &logger, ">>> Power up device.\r\n" );
            ltecat4_set_power_state ( ctx, LTECAT4_POWER_STATE_ON );
        }
    }
    ltecat4_send_cmd( ctx, LTECAT4_CMD_AT );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    
    log_printf( &logger, ">>> Factory reset.\r\n" );
    ltecat4_send_cmd( ctx, LTECAT4_CMD_FACTORY_RESET );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );

    log_printf( &logger, ">>> Get device software version ID.\r\n" );
    ltecat4_send_cmd( ctx, LTECAT4_CMD_GET_SW_VERSION );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );

    log_printf( &logger, ">>> Get device serial number.\r\n" );
    ltecat4_send_cmd( ctx, LTECAT4_CMD_GET_SERIAL_NUM );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );

    return error_flag;
}

static err_t ltecat4_config_connection ( ltecat4_t *ctx )
{
    err_t error_flag = LTECAT4_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) || ( DEMO_EXAMPLE == EXAMPLE_CALL ) )
    log_printf( &logger, ">>> Deregister from network.\r\n" );
    #define DEREGISTER_FROM_NETWORK "2"
    ltecat4_send_cmd_par( ctx, LTECAT4_CMD_OPERATOR_SELECTION, DEREGISTER_FROM_NETWORK );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );

    log_printf( &logger, ">>> Set SIM APN.\r\n" );
    ltecat4_set_sim_apn( &ltecat4, SIM_APN );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    
    log_printf( &logger, ">>> Enable full functionality.\r\n" );
    #define FULL_FUNCTIONALITY "1"
    ltecat4_send_cmd_par( ctx, LTECAT4_CMD_SET_UE_FUNCTIONALITY, FULL_FUNCTIONALITY );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );

    log_printf( &logger, ">>> Enable network registration.\r\n" );
    #define ENABLE_REG "2"
    ltecat4_send_cmd_par( ctx, LTECAT4_CMD_NETWORK_REGISTRATION, ENABLE_REG );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );

    log_printf( &logger, ">>> Set automatic registration.\r\n" );
    #define AUTOMATIC_REGISTRATION "0"
    ltecat4_send_cmd_par( ctx, LTECAT4_CMD_OPERATOR_SELECTION, AUTOMATIC_REGISTRATION );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
#endif
    return error_flag;
}

static err_t ltecat4_check_connection ( ltecat4_t *ctx )
{
    err_t error_flag = LTECAT4_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) || ( DEMO_EXAMPLE == EXAMPLE_CALL ) )
    log_printf( &logger, ">>> Check network registration.\r\n" );
    #define CONNECTED "+CREG: 2,1"
    ltecat4_send_cmd_check ( &ltecat4, LTECAT4_CMD_NETWORK_REGISTRATION );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    if ( strstr( app_buf, CONNECTED ) )
    {
        Delay_ms ( 1000 );
        log_printf( &logger, ">>> Check signal quality.\r\n" );
        ltecat4_send_cmd( &ltecat4, LTECAT4_CMD_SIGNAL_QUALITY_REPORT );
        error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    }
    else
    {
        error_flag = LTECAT4_ERROR;
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
    }
#endif
    return error_flag;
}

static err_t ltecat4_config_example ( ltecat4_t *ctx )
{
    err_t error_flag = LTECAT4_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    log_printf( &logger, ">>> Activate PDP context.\r\n" );
    #define ACTIVATE_PDP_CONTEXT "1,1"
    ltecat4_send_cmd_par( &ltecat4, LTECAT4_CMD_ACTIVATE_PDP_CONTEXT, ACTIVATE_PDP_CONTEXT );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    log_printf( &logger, ">>> Show PDP address.\r\n" );
    #define PDP_CID "1"
    ltecat4_send_cmd_par( &ltecat4, LTECAT4_CMD_SHOW_PDP_ADDRESS, PDP_CID );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    log_printf( &logger, ">>> Select SMS format.\r\n" );
    ltecat4_send_cmd_par( &ltecat4, LTECAT4_CMD_SELECT_SMS_FORMAT, SMS_MODE );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
#elif ( DEMO_EXAMPLE == EXAMPLE_CALL )
    log_printf( &logger, ">>> Configure audio codec.\r\n" );
    if ( LTECAT4_OK != ltecat4_config_codec ( &ltecat4 ) )
    {
        log_error( &logger, " Audio codec config." );
        for ( ; ; );
    }
    log_printf( &logger, ">>> Set MIC gain.\r\n" );
    #define MIC_GAIN "14567,14567"
    ltecat4_send_cmd_par( &ltecat4, LTECAT4_CMD_SET_MIC_GAIN, MIC_GAIN );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    log_printf( &logger, ">>> Set MAX9860 as audio codec.\r\n" );
    #define CODEC_MAX9860 "4"
    ltecat4_send_cmd_par( &ltecat4, LTECAT4_CMD_CONFIG_AUDIO_INTERFACE, CODEC_MAX9860 );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    log_printf( &logger, ">>> Enable URC for all ports.\r\n" );
    #define URC_INDICATION_ALL_PORTS "\"urcport\",\"all\""
    ltecat4_send_cmd_par( &ltecat4, LTECAT4_CMD_CONFIG_URC_INDICATION, URC_INDICATION_ALL_PORTS );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
#elif ( DEMO_EXAMPLE == EXAMPLE_GNSS )
    log_printf( &logger, ">>> Check GNSS power state.\r\n" );
    #define GNSS_TURNED_OFF "+QGPS: 0"
    ltecat4_send_cmd_check( &ltecat4, LTECAT4_CMD_TURN_ON_GNSS );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    if ( strstr ( app_buf, GNSS_TURNED_OFF ) )
    {
        log_printf( &logger, ">>> Turn on GNSS power supply.\r\n" );
        #define GNSS_TURN_ON "1"
        ltecat4_send_cmd_par( &ltecat4, LTECAT4_CMD_TURN_ON_GNSS, GNSS_TURN_ON );
        error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    }
#endif
    return error_flag;
}

static err_t ltecat4_example ( ltecat4_t *ctx )
{
    err_t error_flag = LTECAT4_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    uint8_t cmd_buf[ 100 ] = { 0 };
    log_printf( &logger, ">>> Open TCP connection.\r\n" );
    #define PDP_CID "1"
    #define TCP_SOCKET_NUM "0"
    #define TCP_CONN_TYPE "TCP"
    strcpy( cmd_buf, PDP_CID );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, TCP_SOCKET_NUM );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, TCP_CONN_TYPE );
    strcat( cmd_buf, "\",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    ltecat4_send_cmd_par ( &ltecat4, LTECAT4_CMD_OPEN_SOCKET, cmd_buf );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );

    log_printf( &logger, ">>> Open UDP connection.\r\n" );
    #define UDP_SOCKET_NUM "1"
    #define UDP_CONN_TYPE "UDP"
    strcpy( cmd_buf, PDP_CID );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, UDP_SOCKET_NUM );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, UDP_CONN_TYPE );
    strcat( cmd_buf, "\",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    ltecat4_send_cmd_par ( &ltecat4, LTECAT4_CMD_OPEN_SOCKET, cmd_buf );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_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 );
    ltecat4_send_cmd_par ( &ltecat4, LTECAT4_CMD_SEND_DATA, cmd_buf );
    Delay_ms ( 100 );
    ltecat4_generic_write ( &ltecat4, MESSAGE_CONTENT, message_len );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_URC_RECEIVED_DATA );
    log_printf( &logger, ">>> Read response from TCP connection.\r\n" );
    ltecat4_send_cmd_par( &ltecat4, LTECAT4_CMD_RECEIVE_DATA, cmd_buf );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_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 );
    ltecat4_send_cmd_par ( &ltecat4, LTECAT4_CMD_SEND_DATA, cmd_buf );
    Delay_ms ( 100 );
    ltecat4_generic_write ( &ltecat4, MESSAGE_CONTENT, message_len );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_URC_RECEIVED_DATA );
    log_printf( &logger, ">>> Read response from UDP connection.\r\n" );
    ltecat4_send_cmd_par( &ltecat4, LTECAT4_CMD_RECEIVE_DATA, cmd_buf );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    
    log_printf( &logger, ">>> Close TCP connection.\r\n" );
    ltecat4_send_cmd_par ( &ltecat4, LTECAT4_CMD_CLOSE_SOCKET, TCP_SOCKET_NUM );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    
    log_printf( &logger, ">>> Close UDP connection.\r\n" );
    ltecat4_send_cmd_par ( &ltecat4, LTECAT4_CMD_CLOSE_SOCKET, UDP_SOCKET_NUM );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    // Check SMS mode
    #define CMGF_PDU "+CMGF: 0"
    #define CMGF_TXT "+CMGF: 1"
    log_printf( &logger, ">>> Check SMS format.\r\n" );
    ltecat4_send_cmd_check( &ltecat4, LTECAT4_CMD_SELECT_SMS_FORMAT );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    if ( strstr( app_buf, CMGF_PDU ) )
    {
        // Send SMS in PDU mode
        log_printf( &logger, ">>> Send SMS in PDU mode.\r\n" );
        ltecat4_send_sms_pdu( &ltecat4, SIM_SMSC, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    }
    else if ( strstr( app_buf, CMGF_TXT ) )
    {
        // Send SMS in TXT mode
        log_printf( &logger, ">>> Send SMS in TXT mode.\r\n" );
        ltecat4_send_sms_text ( &ltecat4, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    }
    // 30 seconds delay
    for ( uint8_t delay_cnt = 0; delay_cnt < 30; delay_cnt++ )
    {
        Delay_ms ( 1000 );
    }
#elif ( DEMO_EXAMPLE == EXAMPLE_CALL )
    log_printf( &logger, ">>> Waiting for a ring indication.\r\n" );
    ltecat4_clear_app_buf ( );
    for ( ; ; )
    {
        if ( ( LTECAT4_OK == ltecat4_process ( ctx ) ) && strstr ( app_buf, LTECAT4_URC_RING ) )
        {
            ltecat4_log_app_buf ( );
            Delay_ms ( 1000 );
            break;
        }
    }
    log_printf( &logger, "\r\n--------------------------------\r\n" );
        
    log_printf( &logger, ">>> List calls.\r\n" );
    ltecat4_send_cmd ( &ltecat4, LTECAT4_CMD_LIST_CALLS );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    log_printf( &logger, ">>> Answer an incoming call.\r\n" );
    ltecat4_send_cmd ( &ltecat4, LTECAT4_CMD_ANSWER_CALL );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    log_printf( &logger, ">>> Call in progress.\r\n" );
    if ( LTECAT4_OK != ltecat4_read_response( ctx, LTECAT4_URC_NO_CARRIER ) )
    {
        log_printf( &logger, ">>> Terminate call.\r\n" );
        ltecat4_send_cmd ( &ltecat4, LTECAT4_CMD_TERMINATE_CALL );
        error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    }
    log_printf( &logger, ">>> Call ended.\r\n" );

#elif ( DEMO_EXAMPLE == EXAMPLE_GNSS )
    uint8_t element_buf[ 100 ] = { 0 };
    #define GNSS_NMEA_GGA "\"GGA\""
    log_printf( &logger, ">>> Get GNSS NMEA sentence.\r\n" );
    ltecat4_send_cmd_par ( &ltecat4, LTECAT4_CMD_ACQUIRE_NMEA_SENTENCES, GNSS_NMEA_GGA );
    error_flag |= ltecat4_read_response( ctx, LTECAT4_RSP_OK );
    log_printf( &logger, ">>> Parse GNSS NMEA sentence.\r\n" );
    if ( LTECAT4_OK == ltecat4_parse_gga( app_buf, LTECAT4_GGA_LATITUDE, element_buf ) )
    {
        if ( strlen( element_buf ) > 0 )
        {
            log_printf( &logger, "Latitude: %.2s degrees, %s minutes\r\n", element_buf, &element_buf[ 2 ] );
            memset( element_buf, 0, sizeof( element_buf ) );
            ltecat4_parse_gga( app_buf, LTECAT4_GGA_LONGITUDE, element_buf );
            log_printf( &logger, "Longitude: %.3s degrees, %s minutes\r\n", element_buf, &element_buf[ 3 ] );
            memset( element_buf, 0, sizeof( element_buf ) );
            ltecat4_parse_gga( app_buf, LTECAT4_GGA_ALTITUDE, element_buf );
            log_printf( &logger, "Altitude: %s m\r\n", element_buf );
        }
        else
        {
            log_printf( &logger, "No position fix detected.\r\n" );
        }
    }
    log_printf( &logger, "--------------------------------\r\n" );
    Delay_ms ( 1000 );
#else
    #error "No demo example selected"
#endif
    return error_flag;
}

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

额外支持

资源

喜欢这个项目吗?

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

想查看更多

类似项目