中级
20 分钟

使用DA16200MOD-AA和PIC32MZ2048EFM100重新构想连接世界的方式

随时随地通过超低功耗Wi-Fi创新连接!

LP WiFi Click with Curiosity PIC32 MZ EF

已发布 6月 24, 2024

点击板

LP WiFi Click

开发板

Curiosity PIC32 MZ EF

编译器

NECTO Studio

微控制器单元

PIC32MZ2048EFM100

随着我们引入创新的超低功耗Wi-Fi解决方案,您将体验到无缝连接的新时代,重新定义您在移动时保持连接的方式。

A

A

硬件概览

它是如何工作的?

LP WiFi Click基于瑞萨的DA16200MOD-AA,这是一款高度集成的超低功耗WiFi模块,具有最佳的射频性能和舒适的开发环境。它由DA16200 SoC、4MB闪存存储器、射频组件、晶体振荡器、射频集成滤波器和一枚内置的2.4GHz芯片天线组成。这种低功耗操作可以延长电池寿命一年甚至更长,具体取决于应用场景,即使板上持续连接到WiFi网络。该模块还具有强大的物联网安全性,包括用于WiFi和更高堆栈层的身份验证和加密的WPA3和TLS。DA16200MOD具有集成的RTC块(36位实时计数器),带有32.768kHz时钟源,用于SoC的RTC块中的自由运行计数器,

提供电源管理和功能控制以进行低功耗操作。当正常操作期间板上标记为POWER的开关设置为适当的ON位置时,RTC块始终处于通电状态。这样可以优化功耗,并用于开关电源的目的。此外,板的左上角标记为RTC块Pads的标头连接到内部RTC块,用于连接并从外部设备(如传感器)接收外部事件信号。LP WiFi Click使用UART接口作为其默认通信协议与MCU通信。用户可以使用其他接口,如SPI和I2C,以改进和与外围设备通信。值得注意的是,DA16200模块附带的固件仅支持与主机微控制器的UART通信和与传感器等外围设备的I2C通信(SPI接口默认不支持,

可以通过固件更新启用)。此板还具有某些按钮和标头。板载的按钮标记为RESET和WPS,分别表示恢复出厂设置按钮和WiFi Protected Setup(WPS)按钮。至于标头,第一个标有TX和RX信号的标头适用于使用UART接口进行调试,而第二个标头可以通过将其连接到带有I2C信号的右上标头来向设计中添加一个小而明亮、清晰的OLED显示屏。此Click board™只能在3.3V逻辑电压级别下运行。在使用具有不同逻辑电压级别的MCU之前,板必须执行适当的逻辑电压级别转换。此外,它配备了一个包含函数和示例代码的库,可用作进一步开发的参考。

LP WiFi Click hardware overview image

功能概述

开发板

Curiosity PIC32 MZ EF 开发板是一个完全集成的 32 位开发平台,特点是高性能的 PIC32MZ EF 系列(PIC32MZ2048EFM),该系列具有 2MB Flash、512KB RAM、集成的浮点单元(FPU)、加密加速器和出色的连接选项。它包括一个集成的程序员和调试器,无需额外硬件。用户可以通过 MIKROE 

mikroBUS™ Click™ 适配器板扩展功能,通过 Microchip PHY 女儿板添加以太网连接功能,使用 Microchip 扩展板添加 WiFi 连接能力,并通过 Microchip 音频女儿板添加音频输入和输出功能。这些板完全集成到 PIC32 强大的软件框架 MPLAB Harmony 中,该框架提供了一个灵活且模块化的接口

来应用开发、一套丰富的互操作软件堆栈(TCP-IP、USB)和易于使用的功能。Curiosity PIC32 MZ EF 开发板提供了扩展能力,使其成为连接性、物联网和通用应用中快速原型设计的绝佳选择。

Curiosity PIC32MZ EF double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC32

MCU 内存 (KB)

2048

硅供应商

Microchip

引脚数

100

RAM (字节)

524288

使用的MCU引脚

mikroBUS™映射器

NC
NC
AN
Wake-Up
RA9
RST
SPI Chip Select
RPD4
CS
SPI Clock
RPD1
SCK
SPI Data OUT
RPD14
MISO
SPI Data IN
RPD3
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
PWM Signal
RPE8
PWM
Interrupt
RF13
INT
UART TX
RPD10
TX
UART RX
RPD15
RX
I2C Clock
RPA14
SCL
I2C Data
RPA15
SDA
NC
NC
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

LP WiFi Click Schematic schematic

一步一步来

项目组装

Curiosity PIC32MZ EF front image hardware assembly

从选择您的开发板和Click板™开始。以Curiosity PIC32 MZ EF作为您的开发板开始。

Curiosity PIC32MZ EF front image hardware assembly
GNSS2 Click front image hardware assembly
Prog-cut hardware assembly
Board mapper by product7 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
Curiosity PIC32 MZ EF 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

软件支持

库描述

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

关键功能:

  • lpwifi_send_cmd - 发送命令函数。

  • lpwifi_connect_to_ap - 连接到AP函数。

  • lpwifi_factory_reset_device - 设备恢复出厂设置函数。

开源

代码示例

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

/*!
 * @file main.c
 * @brief LP WiFi Click Example.
 *
 * # Description
 * This example reads and processes data from LP WiFi Clicks.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and powers up the module, then connects to the desired AP
 * and creates TCP and UDP servers on the desired local port.
 *
 * ## Application Task
 * Logs all the data received from TCP/UDP clients on the USB UART.
 *
 * ## Additional Function
 * - static void lpwifi_clear_app_buf ( void )
 * - static err_t lpwifi_process ( void )
 * - static void lpwifi_error_check( err_t error_flag )
 * - static void lpwifi_log_app_buf ( void )
 * - static err_t lpwifi_rsp_check ( void )
 * - static void lpwifi_check_connection( void )
 *
 * @note
 * In order for the example to work, user needs to set the AP SSID, password, and Local port
 * on which the TCP server and UDP socket will be created.
 * Enter valid data for the following macros: AP_SSID, AP_PASSWORD and LOCAL_PORT.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "lpwifi.h"
#include "string.h"
#include "generic_pointer.h"

// Set AP SSID
#define AP_SSID                     ""

// Set AP password - if the AP is OPEN remain this NULL
#define AP_PASSWORD                 ""

// Set Local port on which the TCP server and UDP socket will be created.
#define LOCAL_PORT                  1

#define APP_OK                      0
#define APP_ERROR_DRIVER            -1
#define APP_ERROR_OVERFLOW          -2
#define APP_ERROR_TIMEOUT           -3

#define RSP_OK                      "OK"
#define RSP_ERROR                   "ERROR"

#define PROCESS_BUFFER_SIZE         400

#define WAIT_FOR_CONNECTION         0
#define CONNECTED_TO_AP             1
#define NOT_CONNECTED_TO_AP         2

static lpwifi_t lpwifi;
static log_t logger;

static char app_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len                 = 0;
static int32_t app_buf_cnt                 = 0;

static char assigned_ip_address[ 25 ]      = { 0 };
static uint8_t app_connection_status       = WAIT_FOR_CONNECTION;

static err_t app_error_flag;

/**
 * @brief LP WiFi clearing application buffer.
 * @details This function clears memory of application buffer and reset it's length and counter.
 * @note None.
 */
static void lpwifi_clear_app_buf ( void );

/**
 * @brief LP WiFi data reading function.
 * @details This function reads data from device and concatenates data to application buffer.
 *
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 *         @li @c -2 - Application buffer overflow.
 *
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lpwifi_process ( void );

/**
 * @brief LP WiFi check for errors.
 * @details This function checks for different types of errors and logs them on UART.
 * @note None.
 */
static void lpwifi_error_check( err_t error_flag );

/**
 * @brief LP WiFi logs application buffer.
 * @details This function logs data from application buffer.
 * @note None.
 */
static void lpwifi_log_app_buf ( void );

/**
 * @brief LP WiFi response check.
 * @details This function checks for response and returns the status of response.
 *
 * @return application status.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lpwifi_rsp_check ( void );

/**
 * @brief LP WiFi check connection.
 * @details This function checks connection to the AP, and fills the assigned_ip_address buffer and
 *          logs the response on the USB UART if it is connected successfully.
 *
 * @note None.
 */
static void lpwifi_check_connection( void );

/**
 * @brief LP WiFi str cut chr function.
 * @details This function removes all selected characters from string str,
 * and returns it to the same str without those characters.
 * @param str Address of string.
 * @param chr Character to cut.
 */
static void lpwifi_str_cut_chr ( uint8_t *str, uint8_t chr );

void application_init ( void )
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    lpwifi_cfg_t lpwifi_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 " );
    Delay_ms ( 100 );

    // Click initialization.

    lpwifi_cfg_setup( &lpwifi_cfg );
    LPWIFI_MAP_MIKROBUS( lpwifi_cfg, MIKROBUS_1 );
    err_t init_flag = lpwifi_init( &lpwifi, &lpwifi_cfg );
    if ( UART_ERROR == init_flag )
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );

        for ( ; ; );
    }

    lpwifi_default_cfg( &lpwifi );
    Delay_ms ( 500 );

    // Initiate the communication
    lpwifi_send_cmd( &lpwifi, LPWIFI_CMD_AT );
    Delay_ms ( 1000 );

    // Dummy read
    lpwifi_process( );
    lpwifi_clear_app_buf( );

    log_printf( &logger, "\r\n --- Factory reset --- \r\n" );
    lpwifi_factory_reset_device ( &lpwifi );
    Delay_ms ( 500 );
    // Enable Echo
    lpwifi_send_cmd( &lpwifi, LPWIFI_CMD_ATE );
    app_error_flag = lpwifi_rsp_check( );
    lpwifi_error_check( app_error_flag );
    Delay_ms ( 500 );
    log_printf( &logger, " ----------------------------------------------- \r\n" );

    log_printf( &logger, "\r\n --- Connecting to the access point --- \r\n" );
    // Connect to AP
    lpwifi_connect_to_ap( &lpwifi, AP_SSID, AP_PASSWORD );
    app_error_flag = lpwifi_rsp_check( );
    lpwifi_error_check( app_error_flag );
    lpwifi_check_connection();
    while ( CONNECTED_TO_AP != app_connection_status )
    {
        lpwifi_check_connection();
        if ( NOT_CONNECTED_TO_AP == app_connection_status )
        {
            Delay_ms ( 500 );
            app_connection_status = WAIT_FOR_CONNECTION;

            // Connect to AP
            lpwifi_connect_to_ap( &lpwifi, AP_SSID, AP_PASSWORD );
            app_error_flag = lpwifi_rsp_check( );
            lpwifi_error_check( app_error_flag );
        }
    }
    log_printf( &logger, " ----------------------------------------------- \r\n" );
    log_printf( &logger, "\r\n --- Creating a TCP server --- \r\n" );
    // Create TCP Server
    lpwifi_create_tcp_server( &lpwifi, LOCAL_PORT );
    app_error_flag = lpwifi_rsp_check( );
    lpwifi_error_check( app_error_flag );
    Delay_ms ( 500 );

    log_printf( &logger, " ----------------------------------------------- \r\n" );
    log_printf( &logger, "\r\n --- Creating a UDP socket --- \r\n" );
    // Create UDP socket
    lpwifi_create_udp_socket( &lpwifi, LOCAL_PORT );
    app_error_flag = lpwifi_rsp_check( );
    lpwifi_error_check( app_error_flag );
    Delay_ms ( 500 );

    log_printf( &logger, " ----------------------------------------------- \r\n" );
    log_printf( &logger, " TCP server and UDP socket are available at: \r\n" );
    log_printf( &logger, "    SSID: \"%s\"\r\n    IP: %s\r\n    Port: %u", ( char * ) AP_SSID,
                                                                           ( char * ) assigned_ip_address,
                                                                         ( uint16_t ) LOCAL_PORT );
    log_printf( &logger, "\r\n ----------------------------------------------- \r\n" );
    log_printf( &logger, " You can connect to it via a TCP/UDP client." );
    log_printf( &logger, "\r\n ----------------------------------------------- \r\n" );
}

void application_task ( void )
{
    lpwifi_process( );
    lpwifi_log_app_buf( );
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

static void lpwifi_clear_app_buf ( void )
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
    app_buf_cnt = 0;
}

static err_t lpwifi_process ( void )
{
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };

    rx_size = lpwifi_generic_read( &lpwifi, rx_buff, PROCESS_BUFFER_SIZE );

    if ( rx_size > 0 )
    {
        int32_t buf_cnt = 0;

        if ( app_buf_len + rx_size >= PROCESS_BUFFER_SIZE )
        {
            lpwifi_clear_app_buf(  );
            return LPWIFI_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 LPWIFI_OK;
    }
    return LPWIFI_ERROR;
}

static err_t lpwifi_rsp_check ( void )
{
    uint16_t timeout_cnt = 0;
    uint16_t timeout = 10000;

    err_t error_flag = lpwifi_process(  );

    if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
    {
        return error_flag;
    }

    while ( ( strstr( app_buf, RSP_OK ) == 0 ) && ( strstr( app_buf, RSP_ERROR ) == 0 ) )
    {
        error_flag = lpwifi_process(  );
        if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
        {
            return error_flag;
        }

        timeout_cnt++;
        if ( timeout_cnt > timeout )
        {
            // Initialize AT command
            while ( ( 0 == strstr( app_buf, RSP_OK ) ) && ( 0 == strstr( app_buf, RSP_ERROR ) ) )
            {
                lpwifi_send_cmd( &lpwifi, LPWIFI_CMD_ATZ );
                lpwifi_process(  );
                Delay_ms ( 100 );
            }
            lpwifi_clear_app_buf(  );
            // Enable Echo
            while ( ( 0 == strstr( app_buf, RSP_OK ) ) && ( 0 == strstr( app_buf, RSP_ERROR ) ) )
            {
                lpwifi_send_cmd( &lpwifi, LPWIFI_CMD_ATE );
                lpwifi_process(  );
                Delay_ms ( 100 );
            }
            lpwifi_clear_app_buf(  );
            return APP_ERROR_TIMEOUT;
        }

        Delay_ms ( 1 );
    }

    lpwifi_log_app_buf();

    return APP_OK;
}

static void lpwifi_error_check( err_t error_flag )
{
    if ( ( error_flag != APP_OK ) && ( error_flag != APP_ERROR_DRIVER ) )
    {
        switch ( error_flag )
        {
            case APP_ERROR_OVERFLOW:
                log_error( &logger, " Overflow!" );
                break;
            case APP_ERROR_TIMEOUT:
                log_error( &logger, " Timeout!" );
                break;
            default:
                break;
        }
    }
}

static void lpwifi_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 ] );
    }

    lpwifi_clear_app_buf(  );
}

static void lpwifi_check_connection( void )
{
    #define CONNECTED     "+WFJAP:1"
    #define NOT_CONNECTED "+WFJAP:0"

    lpwifi_process( );
    if ( 0 != strstr( app_buf, CONNECTED ) )
    {
        #define IP_DELIMITER "',"
        char * __generic_ptr app_buf_ptr;
        Delay_ms ( 200 );
        lpwifi_process( );
        app_buf_ptr = strstr( app_buf, IP_DELIMITER );
        strcpy( assigned_ip_address, app_buf_ptr );
        lpwifi_str_cut_chr( assigned_ip_address, '\'' );
        lpwifi_str_cut_chr( assigned_ip_address, ',' );
        lpwifi_str_cut_chr( assigned_ip_address, '\r' );
        lpwifi_str_cut_chr( assigned_ip_address, '\n' );
        lpwifi_log_app_buf( );
        app_connection_status = CONNECTED_TO_AP;
    }
    else if ( 0 != strstr( app_buf, NOT_CONNECTED ) )
    {
        app_connection_status = NOT_CONNECTED_TO_AP;
    }
}

static void lpwifi_str_cut_chr ( uint8_t *str, uint8_t chr )
{
    uint16_t cnt_0, cnt_1;
    for ( cnt_0 = 0; cnt_0 < strlen( str ); cnt_0++ )
    {
        if ( str[ cnt_0 ] == chr )
        {
            for ( cnt_1 = cnt_0; cnt_1 < strlen( str ); cnt_1++ )
            {
                str[ cnt_1 ] = str[ cnt_1 + 1 ];
            }
        }
    }
}

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

额外支持

资源

喜欢这个项目吗?

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