中级
30 分钟

使用 LARA-R6001 和 STM32F031K6 创建您的 4G LTE VoLTE 平台

面向多区域操作的 VoLTE 解决方案

4G LTE 2 Click - Voice with Nucleo 32 with STM32F031K6 MCU

已发布 10月 01, 2024

点击板

4G LTE 2 Click - Voice

开发板

Nucleo 32 with STM32F031K6 MCU

编译器

NECTO Studio

微控制器单元

STM32F031K6

全球覆盖的安全云VoLTE解决方案。

A

A

硬件概览

它是如何工作的?

4G LTE 2 Click - Voice基于u-blox的LARA-R6001,这是一款多频段和多模式模块。它支持LTE Cat 1 FDD和LTE Cat 1 TDD无线接入技术(18个LTE频段),同时支持3G UMTS/HSPA和2G GSM/GPRS/EGPRS后备,为全球连接提供了理想的解决方案。该Click board™代表了一种具有全球覆盖和所有相关MNO认证的数据和语音解决方案,提供了极大的灵活性。多功能的接口和功能使得LARA-R6001D非常适用于需要中等数据速度、优越覆盖范围和流媒体数据的各种应用,例如资产追踪、远程监控、销售终端等。LARA-R6001模块提供了Voice over LTE(VoLTE)和Circuit-Switched-Fall-Back(CSFB)从LTE到3G或2G无线承载的语音服务 - 通过板载3.5mm音频插孔进行访问。通过AT命令可以配置此音频接口,以便将数字音频数据传输到/从外部设备作为板载音频编解码器。该模块需要3.8V的电源供应。因此,Click board™集成了由德州仪器

提供的TPS7A7002集成型降压(降压DC-DC)转换器,它提供了稳定的3.8V电源,并且可以在输入端出现高电流峰值时减小电压下降(通常在设备启动时)。点火(上电)引脚标记为PWR,并路由到mikroBUS™插座上的RST引脚,使能够开关电源。LARA-R6001通过UART接口与MCU通信,该接口使用常用的UART RX和TX引脚,并具有用于硬件流控制的UART CTS、RTS、RI引脚(清除发送、准备发送和振铃指示)。默认情况下,它以115200 bps的速度运行,通过u-blox提供的AT命令与主机MCU进行数据传输和交换。除了UART接口,LARA-R6001还允许将I2C接口用作I2C主机,该接口可以遵循I2C总线规范与I2C本地设备通信。此Click board™还配备了一个USB Type C连接器,仅用于诊断目的。该模块是一个USB设备,可以连接到具有兼容驱动程序的任何USB主机。在使用的引脚中,此Click board™还具有两个额外的LED指示灯:黄色LED标记为

STATUS,同时路由到mikroBUS™ AN引脚(STS)和LED,用于直观地指示网络连接的状态;还有一个红色LED,标记为TX,用于指示模块的发送状态。LTE蜂窝网络使用空间复用天线技术,允许使用多于一个天线以获得更好的接收效果。因此,除了主要的TX/RX天线外,此Click board™还使用了一个辅助的多样性RX天线,这可以获得更好的信号接收效果。除了SMA连接器之外,4G LTE 2 Click还具有一个Nano-SIM卡槽,提供了多个连接和接口选项,同时还有几个标记为TP1到TP3的测试点,可实现模块的轻松重启和测试。该Click board™可以通过VCC SEL跳线选择3.3V或5V逻辑电压电平运行。通过这种方式,既可以使用3.3V也可以使用5V的MCU正确使用通信线路。然而,该Click board™配备了一个包含易于使用的功能和示例代码的库,可以用作进一步开发的参考。

4G LTE 2 Click - Voice hardware overview image

功能概述

开发板

Nucleo 32开发板搭载STM32F031K6 MCU,提供了一种经济且灵活的平台,适用于使用32引脚封装的STM32微控制器进行实验。该开发板具有Arduino™ Nano连接性,便于通过专用扩展板进行功能扩展,并且支持mbed,使其能够无缝集成在线资源。板载集成

ST-LINK/V2-1调试器/编程器,支持通过USB重新枚举,提供三种接口:虚拟串口(Virtual Com port)、大容量存储和调试端口。该开发板的电源供应灵活,可通过USB VBUS或外部电源供电。此外,还配备了三个LED指示灯(LD1用于USB通信,LD2用于电源

指示,LD3为用户可控LED)和一个复位按钮。STM32 Nucleo-32开发板支持多种集成开发环境(IDEs),如IAR™、Keil®和基于GCC的IDE(如AC6 SW4STM32),使其成为开发人员的多功能工具。

Nucleo 32 with STM32F031K6 MCU double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

ARM Cortex-M0

MCU 内存 (KB)

32

硅供应商

STMicroelectronics

引脚数

32

RAM (字节)

4096

你完善了我!

配件

Click Shield for Nucleo-32是扩展您的开发板功能的理想选择,专为STM32 Nucleo-32引脚布局设计。Click Shield for Nucleo-32提供了两个mikroBUS™插座,可以添加来自我们不断增长的Click板™系列中的任何功能。从传感器和WiFi收发器到电机控制和音频放大器,我们应有尽有。Click Shield for Nucleo-32与STM32 Nucleo-32开发板兼容,为用户提供了一种经济且灵活的方式,使用任何STM32微控制器快速创建原型,并尝试各种性能、功耗和功能的组合。STM32 Nucleo-32开发板无需任何独立的探针,因为它集成了ST-LINK/V2-1调试器/编程器,并随附STM32全面的软件HAL库和各种打包的软件示例。这个开发平台为用户提供了一种简便且通用的方式,将STM32 Nucleo-32兼容开发板与他们喜欢的Click板™结合,应用于即将开展的项目中。

Click Shield for Nucleo-32 accessories 1 image

这款多频段LTE橡胶天线带可调角度,是我们提供的所有3G/4G LTE Click板以及其他需要在全球主要蜂窝频段上实现出色吞吐量的设备的理想选择。该天线带有一个SMA公头连接器,可以直接安装在Click板™上或女性SMA模块连接器上。天线位置可以以45度的增量进行调整(0度/45度/90度)。

4G LTE 2 Click - Voice accessories image

使用的MCU引脚

mikroBUS™映射器

Module Status
PA0
AN
Power-ON
PA11
RST
UART RTS
PA4
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
UART RI
PA8
PWM
UART CTS
PA12
INT
UART TX
PA10
TX
UART RX
PA9
RX
I2C Clock
PB6
SCL
I2C Data
PB7
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

4G LTE 2 Click - Voice Schematic schematic

一步一步来

项目组装

Click Shield for Nucleo-144 front image hardware assembly

从选择您的开发板和Click板™开始。以Nucleo 32 with STM32F031K6 MCU作为您的开发板开始。

Click Shield for Nucleo-144 front image hardware assembly
Nucleo 144 with STM32L4A6ZG MCU front image hardware assembly
Stepper 22 Click front image hardware assembly
Prog-cut hardware assembly
Stepper 22 Click complete accessories setup image hardware assembly
Nucleo-32 with STM32 MCU Access MB 1 - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
STM32 M4 Clicker HA MCU/Select 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 2 Click - Voice 驱动程序的 API。

关键功能:

  • c4glte2voice_set_power_state - 此函数通过在高状态下切换PWR引脚的特定时间来设置所需的电源状态。

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

  • c4glte2voice_send_sms_text - 此函数向手机号发送文本消息。

开源

代码示例

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

/*!
 * @file main.c
 * @brief 4G LTE 2 Voice 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, restarts the module and tests the communication.
 *
 * ## Application Task
 * Application task is split in few stages:
 *  - C4GLTE2VOICE_CONFIGURE_FOR_NETWORK:
 * Sets configuration to device to be able to connect to the network.
 *
 *  - C4GLTE2VOICE_WAIT_FOR_CONNECTION:
 * Waits for the network registration indicated via CREG URC event and then checks the connection status.
 *
 *  - C4GLTE2VOICE_CONFIGURE_FOR_EXAMPLE:
 * Sets the device configuration for sending SMS or TCP/UDP messages depending on the selected demo example.
 *
 *  - C4GLTE2VOICE_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 c4glte2voice_clear_app_buf ( void )
 * - static err_t c4glte2voice_process ( void )
 * - static void c4glte2voice_error_check( err_t error_flag )
 * - static void c4glte2voice_log_app_buf ( void )
 * - static err_t c4glte2voice_rsp_check ( void )
 * - static err_t c4glte2voice_configure_for_network( void )
 * - static err_t c4glte2voice_check_connection( void )
 * - static err_t c4glte2voice_configure_for_example( void )
 * - static err_t c4glte2voice_example( void )
 *
 * @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_TO_MESSAGE.
 * Example:
    SIM_APN "internet"
    SIM_SMSC "+381610401"
    PHONE_NUMBER_TO_MESSAGE "+381659999999"
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "c4glte2voice.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                             ""              // Set valid SIM APN

// SMS example parameters
#define SIM_SMSC                            ""              // Set valid SMS Service Center Address - only in SMS PDU mode
#define PHONE_NUMBER_TO_MESSAGE             ""              // 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 2 Voice Click board - demo example."

// Application buffer size
#define PROCESS_BUFFER_SIZE                 300

/**
 * @brief Example states.
 * @details Predefined enum values for application example state.
 */
typedef enum
{
    C4GLTE2VOICE_CONFIGURE_FOR_NETWORK = 1,
    C4GLTE2VOICE_WAIT_FOR_CONNECTION,
    C4GLTE2VOICE_CONFIGURE_FOR_EXAMPLE,
    C4GLTE2VOICE_EXAMPLE

} c4glte2voice_example_state_t;

static c4glte2voice_t c4glte2voice;
static log_t logger;

/**
 * @brief Application example variables.
 * @details Variables used in application example.
 */
static char app_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len = 0;
static int32_t app_buf_cnt = 0;
static err_t error_flag;
static c4glte2voice_example_state_t example_state;

/**
 * @brief Clearing application buffer.
 * @details This function clears memory of application
 * buffer and reset its length and counter.
 */
static void c4glte2voice_clear_app_buf ( void );

/**
 * @brief Data reading function.
 * @details This function reads data from device and
 * appends it to the application buffer.
 * @return @li @c  0 - Some data is read.
 *         @li @c -1 - Nothing is read.
 * See #err_t definition for detailed explanation.
 */
static err_t c4glte2voice_process ( void );

/**
 * @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.
 */
static void c4glte2voice_error_check( err_t error_flag );

/**
 * @brief Logs application buffer.
 * @details This function logs data from application buffer.
 */
static void c4glte2voice_log_app_buf ( void );

/**
 * @brief Response check.
 * @details This function checks for response and
 * returns the status of 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.
 */
static err_t c4glte2voice_rsp_check ( void );

/**
 * @brief Configure device for connection to the network.
 * @details Sends commands to configure and enable
 * connection to the specified network.
 * @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.
 */
static err_t c4glte2voice_configure_for_network( void );

/**
 * @brief Wait for connection signal.
 * @details Wait for connection signal from CREG URC.
 * @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.
 */
static err_t c4glte2voice_check_connection( void );

/**
 * @brief Configure device for example.
 * @details Configure device for the specified example.
 * @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.
 */
static err_t c4glte2voice_configure_for_example( void );

/**
 * @brief Execute example.
 * @details This function executes SMS or TCP/UDP example depending on the DEMO_EXAMPLE macro.
 * @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.
 */
static err_t c4glte2voice_example( void );

void application_init ( void )
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    c4glte2voice_cfg_t c4glte2voice_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.
    c4glte2voice_cfg_setup( &c4glte2voice_cfg );
    C4GLTE2VOICE_MAP_MIKROBUS( c4glte2voice_cfg, MIKROBUS_1 );
    if ( UART_ERROR == c4glte2voice_init( &c4glte2voice, &c4glte2voice_cfg ) )
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );
        for ( ; ; );
    }
    c4glte2voice_set_power_state ( &c4glte2voice, C4GLTE2VOICE_POWER_STATE_OFF );
    c4glte2voice_set_power_state ( &c4glte2voice, C4GLTE2VOICE_POWER_STATE_ON );
    c4glte2voice_process( );
    c4glte2voice_clear_app_buf( );
    app_buf_len = 0;
    app_buf_cnt = 0;

    // Check communication
    c4glte2voice_send_cmd( &c4glte2voice, C4GLTE2VOICE_CMD_AT );
    error_flag = c4glte2voice_rsp_check( );
    c4glte2voice_error_check( error_flag );

    log_info( &logger, " Application Task " );
    example_state = C4GLTE2VOICE_CONFIGURE_FOR_NETWORK;
}

void application_task ( void )
{
    switch ( example_state )
    {
        case C4GLTE2VOICE_CONFIGURE_FOR_NETWORK:
        {
            if ( C4GLTE2VOICE_OK == c4glte2voice_configure_for_network( ) )
            {
                example_state = C4GLTE2VOICE_WAIT_FOR_CONNECTION;
            }
            break;
        }
        case C4GLTE2VOICE_WAIT_FOR_CONNECTION:
        {
            if ( C4GLTE2VOICE_OK == c4glte2voice_check_connection( ) )
            {
                example_state = C4GLTE2VOICE_CONFIGURE_FOR_EXAMPLE;
            }
            break;
        }
        case C4GLTE2VOICE_CONFIGURE_FOR_EXAMPLE:
        {
            if ( C4GLTE2VOICE_OK == c4glte2voice_configure_for_example( ) )
            {
                example_state = C4GLTE2VOICE_EXAMPLE;
            }
            break;
        }
        case C4GLTE2VOICE_EXAMPLE:
        {
            c4glte2voice_example( );
            break;
        }
        default:
        {
            log_error( &logger, " Example 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 c4glte2voice_clear_app_buf ( void )
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
    app_buf_cnt = 0;
}

static err_t c4glte2voice_process ( void )
{
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };
    rx_size = c4glte2voice_generic_read( &c4glte2voice, rx_buff, PROCESS_BUFFER_SIZE );
    if ( rx_size > 0 )
    {
        int32_t buf_cnt = 0;
        if ( ( app_buf_len + rx_size ) > PROCESS_BUFFER_SIZE )
        {
            c4glte2voice_clear_app_buf(  );
            return C4GLTE2VOICE_ERROR;
        }
        else
        {
            buf_cnt = app_buf_len;
            app_buf_len += rx_size;
        }
        for ( int32_t rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ )
        {
            if ( rx_buff[ rx_cnt ] != 0 )
            {
                app_buf[ ( buf_cnt + rx_cnt ) ] = rx_buff[ rx_cnt ];
            }
            else
            {
                app_buf_len--;
                buf_cnt--;
            }
        }
        return C4GLTE2VOICE_OK;
    }
    return C4GLTE2VOICE_ERROR;
}

static err_t c4glte2voice_rsp_check ( void )
{
    uint32_t timeout_cnt = 0;
    uint32_t timeout = 120000;
    err_t error_flag = c4glte2voice_process( );
    if ( ( C4GLTE2VOICE_OK != error_flag ) && ( C4GLTE2VOICE_ERROR != error_flag ) )
    {
        return error_flag;
    }
    while ( ( 0 == strstr( app_buf, C4GLTE2VOICE_RSP_OK ) ) &&
            ( 0 == strstr( app_buf, C4GLTE2VOICE_RSP_ERROR ) ) )
    {
        error_flag = c4glte2voice_process( );
        if ( ( C4GLTE2VOICE_OK != error_flag ) && ( C4GLTE2VOICE_ERROR != error_flag ) )
        {
            return error_flag;
        }
        if ( timeout_cnt++ > timeout )
        {
            c4glte2voice_clear_app_buf( );
            return C4GLTE2VOICE_ERROR_TIMEOUT;
        }
        Delay_ms ( 1 );
    }
    if ( strstr( app_buf, C4GLTE2VOICE_RSP_OK ) )
    {
        return C4GLTE2VOICE_OK;
    }
    else if ( strstr( app_buf, C4GLTE2VOICE_RSP_ERROR ) )
    {
        return C4GLTE2VOICE_ERROR_CMD;
    }
    else
    {
        return C4GLTE2VOICE_ERROR_UNKNOWN;
    }
}

static void c4glte2voice_error_check( err_t error_flag )
{
    switch ( error_flag )
    {
        case C4GLTE2VOICE_OK:
        {
            c4glte2voice_log_app_buf( );
            break;
        }
        case C4GLTE2VOICE_ERROR:
        {
            log_error( &logger, " Overflow!" );
            break;
        }
        case C4GLTE2VOICE_ERROR_TIMEOUT:
        {
            log_error( &logger, " Timeout!" );
            break;
        }
        case C4GLTE2VOICE_ERROR_CMD:
        {
            log_error( &logger, " CMD!" );
            break;
        }
        case C4GLTE2VOICE_ERROR_UNKNOWN:
        default:
        {
            log_error( &logger, " Unknown!" );
            break;
        }
    }
    c4glte2voice_clear_app_buf(  );
    Delay_ms ( 500 );
}

static void c4glte2voice_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 c4glte2voice_configure_for_network( void )
{
    err_t func_error = C4GLTE2VOICE_OK;
    // Deregister from network
    #define DEREGISTER_FROM_NETWORK "2"
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_COPS, DEREGISTER_FROM_NETWORK );
    error_flag = c4glte2voice_rsp_check();
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );
    
    // Set SIM APN
    c4glte2voice_set_sim_apn( &c4glte2voice, SIM_APN );
    error_flag = c4glte2voice_rsp_check();
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );

    // Enable full functionality
    #define FULL_FUNCTIONALITY "1"
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_CFUN, FULL_FUNCTIONALITY );
    error_flag = c4glte2voice_rsp_check();
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );

    // Automatic registration
    #define AUTOMATIC_REGISTRATION "0"
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_COPS, AUTOMATIC_REGISTRATION );
    error_flag = c4glte2voice_rsp_check();
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );

    // Enable network registartion
    #define ENABLE_REG "2"
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_CREG, ENABLE_REG );
    error_flag = c4glte2voice_rsp_check();
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );
    return func_error;
}

static err_t c4glte2voice_check_connection( void )
{
    #define CONNECTED "+CREG: 1"
    c4glte2voice_process( );
    if ( strstr( app_buf, CONNECTED ) )
    {
        Delay_ms ( 100 );
        c4glte2voice_process( );
        c4glte2voice_log_app_buf( );
        log_printf( &logger, "\r\n" );
        c4glte2voice_clear_app_buf( );
        // Check signal quality
        c4glte2voice_send_cmd( &c4glte2voice, C4GLTE2VOICE_CMD_CSQ );
        error_flag = c4glte2voice_rsp_check( );
        c4glte2voice_error_check( error_flag );
        return error_flag;
    }
    return C4GLTE2VOICE_ERROR;
}

static err_t c4glte2voice_configure_for_example( void )
{
    err_t func_error = C4GLTE2VOICE_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    #define ACTIVATE_PDP_CONTEXT "1,1"
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_CGACT, ACTIVATE_PDP_CONTEXT );
    error_flag = c4glte2voice_rsp_check( );
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_CMGF, SMS_MODE );
    error_flag = c4glte2voice_rsp_check( );
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );
#else
    #error "No demo example selected"
#endif
    return func_error;
}

static err_t c4glte2voice_example( void )
{
    err_t func_error = C4GLTE2VOICE_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    char cmd_buf[ 100 ] = { 0 };
    char urc_buf[ 20 ] = { 0 };
    uint16_t timeout_cnt = 0;
    uint16_t timeout = 30000;
    uint8_t * __generic_ptr socket_num_buf = 0;
    uint8_t tcp_socket_num[ 2 ] = { 0 };
    uint8_t udp_socket_num[ 2 ] = { 0 };

    // Create TCP socket
    #define RSP_USOCR "+USOCR: "
    #define TCP_PROTOCOL "6"
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_USOCR, TCP_PROTOCOL );
    error_flag = c4glte2voice_rsp_check( );
    func_error |= error_flag;
    socket_num_buf = strstr( app_buf, RSP_USOCR ) + strlen ( RSP_USOCR );
    tcp_socket_num[ 0 ] = *socket_num_buf;
    c4glte2voice_error_check( error_flag );

    // Create UDP socket
    #define UDP_PROTOCOL "17"
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_USOCR, UDP_PROTOCOL );
    error_flag = c4glte2voice_rsp_check( );
    func_error |= error_flag;
    socket_num_buf = strstr( app_buf, RSP_USOCR ) + strlen ( RSP_USOCR );
    udp_socket_num[ 0 ] = *socket_num_buf;
    c4glte2voice_error_check( error_flag );

    // Connect TCP socket to remote IP and port
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_USOCO, cmd_buf );
    error_flag = c4glte2voice_rsp_check( );
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );

    // Connect UDP socket to remote IP and port
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_USOCO, cmd_buf );
    error_flag = c4glte2voice_rsp_check( );
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );

    // Get message length
    uint8_t message_len_buf[ 5 ] = { 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 );

    // Write message to TCP socket
    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, "\"" );
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_USOWR, cmd_buf );
    error_flag = c4glte2voice_rsp_check( );
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );

    // Read response message from TCP socket
    #define URC_READ_SOCKET_DATA_TCP "+UUSORD: "
    strcpy( urc_buf, URC_READ_SOCKET_DATA_TCP );
    strcat( urc_buf, tcp_socket_num );
    for ( ; ; )
    {
        c4glte2voice_process( );
        uint8_t * __generic_ptr start_response_buf = strstr( app_buf, urc_buf );
        if ( start_response_buf )
        {
            Delay_ms ( 100 );
            c4glte2voice_process( );
            uint8_t response_len_buf[ 5 ] = { 0 };
            char * __generic_ptr start_response_len = strstr( start_response_buf, "," ) + 1;
            memcpy ( response_len_buf, start_response_len, app_buf_len - ( start_response_len - app_buf ) );
            strcpy( cmd_buf, tcp_socket_num );
            strcat( cmd_buf, "," );
            strcat( cmd_buf, response_len_buf );
            c4glte2voice_log_app_buf( );
            c4glte2voice_clear_app_buf( );
            c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_USORD, cmd_buf );
            error_flag = c4glte2voice_rsp_check( );
            func_error |= error_flag;
            c4glte2voice_error_check( error_flag );
            break;
        }
        if ( timeout_cnt++ > timeout )
        {
            break;
        }
        Delay_ms ( 1 );
    }
    timeout_cnt = 0;

    // Write message to UDP socket
    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, "\"" );
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_USOWR, cmd_buf );
    error_flag = c4glte2voice_rsp_check( );
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );

    // Read response message from UDP socket
    #define URC_READ_SOCKET_DATA_UDP "+UUSORD: "
    strcpy( urc_buf, URC_READ_SOCKET_DATA_UDP );
    strcat( urc_buf, udp_socket_num );
    for ( ; ; )
    {
        c4glte2voice_process( );
        uint8_t * __generic_ptr start_response_buf = strstr( app_buf, urc_buf );
        if ( start_response_buf )
        {
            Delay_ms ( 100 );
            c4glte2voice_process( );
            uint8_t response_len_buf[ 5 ] = { 0 };
            char * __generic_ptr start_response_len = strstr( start_response_buf, "," ) + 1;
            memcpy ( response_len_buf, start_response_len, app_buf_len - ( start_response_len - app_buf ) );
            strcpy( cmd_buf, udp_socket_num );
            strcat( cmd_buf, "," );
            strcat( cmd_buf, response_len_buf );
            c4glte2voice_log_app_buf( );
            c4glte2voice_clear_app_buf( );
            c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_USORD, cmd_buf );
            error_flag = c4glte2voice_rsp_check( );
            func_error |= error_flag;
            c4glte2voice_error_check( error_flag );
            break;
        }
        if ( timeout_cnt++ > timeout )
        {
            break;
        }
        Delay_ms ( 1 );
    }
    
    // Close TCP socket
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_USOCL, tcp_socket_num );
    error_flag = c4glte2voice_rsp_check( );
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );
    
    // Close UDP socket
    c4glte2voice_send_cmd_with_parameter( &c4glte2voice, C4GLTE2VOICE_CMD_USOCL, udp_socket_num );
    error_flag = c4glte2voice_rsp_check( );
    func_error |= error_flag;
    c4glte2voice_error_check( error_flag );
    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"
    c4glte2voice_send_cmd_check( &c4glte2voice, C4GLTE2VOICE_CMD_CMGF );
    error_flag = c4glte2voice_rsp_check( );
    func_error |= error_flag;
    if ( strstr( app_buf, CMGF_PDU ) )
    {
        c4glte2voice_error_check( error_flag );
        // Send SMS in PDU mode
        c4glte2voice_send_sms_pdu( &c4glte2voice, SIM_SMSC, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = c4glte2voice_rsp_check( );
        func_error |= error_flag;
    }
    else if ( strstr( app_buf, CMGF_TXT ) )
    {
        c4glte2voice_error_check( error_flag );
        // Send SMS in TXT mode
        c4glte2voice_send_sms_text ( &c4glte2voice, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = c4glte2voice_rsp_check( );
        func_error |= error_flag;
    }
    c4glte2voice_error_check( error_flag );
    // 30 seconds delay
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#else
    #error "No demo example selected"
#endif
    return func_error;
}

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

额外支持

资源

喜欢这个项目吗?

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